HttpRequest的返回null实体,不能在服务器上提取体(HttpRequest return

2019-10-21 02:35发布

我试图用解析HTTP请求的Apache的HttpCore组件 ,并希望抓住请求的主体。 它看起来像默认DefaultHttpRequestParser不解析从输入流中的机构/实体。 是否有一个类,将做到这一点?

不幸的是,我不能使用整个堆栈,需要从该输入流直拉请求。

我的解析代码如下。 寻找一些其他的答案似乎在请求的主体应该作为一个实体。 然而,每次我试图让在实体时间为null。

调试我看到缓冲区读取但不能用身体和DefaultHttpRequestParser似乎只是读头。 有没有我应该使用解析整个输入解析?

    InputStream is = socket.getInputStream();
    HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
    SessionInputBufferImpl buf = new SessionInputBufferImpl(metrics, 2048);
    buf.bind(is);

    DefaultHttpRequestParser reqParser = new DefaultHttpRequestParser(buf);
    HttpRequest req = reqParser.parse();
    if (req instanceof HttpEntityEnclosingRequest) {
        entity = ((HttpEntityEnclosingRequest)query).getEntity();
        //... entity is always null

如果我读的输入流我结束了:

POST / HTTP/1.1
User-Agent: curl/Q.XX.0 (linux-gnu) libcurl/Q.XX.0 OpenSSL/X.Y.Z zlib/A.B.C.D libidn/E.FF librtmp/G.H
Host: localhost:8088
Accept: */*
Content-Length: 333
Content-Type: multipart/form-data; boundary=----------------------------39203c7982df

------------------------------39203c7982df
Content-Disposition: form-data; name="fileupload"; filename="grun.sh"
Content-Type: application/octet-stream

#!/bin/bash -x
java -classpath lib/antlr-4.4-complete.jar:build/classes org.antlr.v4.runtime.misc.TestRig Typegroup "AHI" -tree

------------------------------39203c7982df--

[更新]奥列格有一个很好的答案,但我可以用请求的主体和流体关联还是我现在需要通过周围两件事情,? 我会寻找到

我得到了下面的工作,但它在最新版本中已过时。

        ...
        HttpEntityEnclosingRequest ereq = (HttpEntityEnclosingRequest) req;
        @SuppressWarnings("deprecation")
        EntityDeserializer ed = 
           new EntityDeserializer(new LaxContentLengthStrategy());
        @SuppressWarnings("deprecation")//ack!
        HttpEntity ent = ed.deserialize(buf, req);
        ereq.setEntity(ent);
        return ereq;

奥列格相结合的解决方案与我结束了与上面:

            HttpEntityEnclosingRequest ereq = (HttpEntityEnclosingRequest) req;

            ContentLengthStrategy contentLengthStrategy =
                        StrictContentLengthStrategy.INSTANCE;
            long len = contentLengthStrategy.determineLength(req);
            InputStream contentStream = null;
            if (len == ContentLengthStrategy.CHUNKED) {
                contentStream = new ChunkedInputStream(buf);
            } else if (len == ContentLengthStrategy.IDENTITY) {
                contentStream = new IdentityInputStream(buf);
            } else {
                contentStream = new ContentLengthInputStream(buf, len);
            }
            BasicHttpEntity ent = new BasicHttpEntity();
            ent.setContent(contentStream);
            ereq.setEntity(ent);
            return ereq;

Answer 1:

InputStream is = socket.getInputStream();
HttpTransportMetricsImpl metrics = new HttpTransportMetricsImpl();
SessionInputBufferImpl buf = new SessionInputBufferImpl(metrics, 2048);
buf.bind(is);

DefaultHttpRequestParser reqParser = new DefaultHttpRequestParser(buf);
HttpRequest req = reqParser.parse();
InputStream contentStream = null;
if (req instanceof HttpEntityEnclosingRequest) {
    ContentLengthStrategy contentLengthStrategy = StrictContentLengthStrategy.INSTANCE;
    long len = contentLengthStrategy.determineLength(req);
    if (len == ContentLengthStrategy.CHUNKED) {
        contentStream = new ChunkedInputStream(buf);
    } else if (len == ContentLengthStrategy.IDENTITY) {
        contentStream = new IdentityInputStream(buf);
    } else {
        contentStream = new ContentLengthInputStream(buf, len);
    }
}
// Do something useful with the content stream (if non null)

在的HttpCore消息解析器解析唯一的消息头。 然而,可以从会话输入缓冲器进行读取和阅读邮件正文内容,直到消息的结束(依赖于所使用的轮廓标)



文章来源: HttpRequest returning null entity, cannot extract body on server