Monday 15 September 2014

java - HttpRequest returning null entity, cannot extract body on server -



java - HttpRequest returning null entity, cannot extract body on server -

i'm trying parse http request using apache httpcore components , want grab body of request. looks default defaulthttprequestparser doesn't parse body/entity input stream. there class this?

unfortunately can't utilize entire stack , need pull request straight input stream.

my parsing code below. looking @ of other answers appears body of request should available entity. however, every time seek @ entity null.

debugging see buffer has read not used body , defaulthttprequestparser seems read header. there parse should using parse entire input?

inputstream = 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 null

if read input stream end with:

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--

[update] oleg has answer, can associate body request or need pass 2 things around, body , stream? i'll looking

i got next work, it's deprecated in latest release.

... httpentityenclosingrequest ereq = (httpentityenclosingrequest) req; @suppresswarnings("deprecation") entitydeserializer ed = new entitydeserializer(new laxcontentlengthstrategy()); @suppresswarnings("deprecation")//ack! httpentity ent = ed.deserialize(buf, req); ereq.setentity(ent); homecoming ereq;

combining oleg's solution above ended with:

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); homecoming ereq;

inputstream = 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); } } // useful content stream (if non null)

message parsers in httpcore parse message heads only. however, 1 can proceed reading session input buffer , read message body content until end of message (depending on delineator used)

java http apache-httpcomponents

No comments:

Post a Comment