Oleg Kalnichevski <[email protected]> wrote on 02/02/2011 12:40:14 PM:
> On Wed, 2011-02-02 at 12:16 -0500, John Kiffmeyer wrote: > > Hi all, > > I'm working with an application that requires requests to be chunked a > > certain way. More or less, the body contains an application request and > > some data associated with it. If chunked normally, the first chunk > > contains the application request and the beginning of the data. If the > > first chunk is this large, however, the server refuses to parse the > > application request. I want to force a chunk boundary at the end of the > > application request/beginning of the data. > > > > I (understandably) don't see a way to do this in the api. Is it feasible > > to accomplish this by subclassing some stuff? I've traced through > > httpclient and httpcore code but it's not obvious to me yet what I need to > > extend/override (EntitySerializer looks promising, but I'm not clear on > > where I need to shoehorn a subclass of that into the path after > > HttpClient.execute()). So, I'm looking for some wisdom. > > > > Thanks! > > jk > > John > > While it is not going to be pretty it is certainly doable. > > Assuming you intend to use blocking I/O model > > (1) Create a custom version of the ChunkedOutputStream. Most likely will > need to copy the entire class and tweak it quite a bit to make it work > the way you want > > (2) Subclass EntitySerializer and make it use your custom > ChunkedOutputStream when serializing chunked entities or a specific type > of entities. > > (2) If you intend to use HttpCore only, subclass > DefaultHttpClientConnection and make it use your custom > EntitySerializer. If you would like to use HttpClient with all bells and > whistles, follow these instructions of the HttpClient tutorial: > > http://hc.apache.org/httpcomponents-client- > ga/tutorial/html/advanced.html#d4e1333 > > Hope this helps > > Oleg > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > Hi Oleg, Thanks for your reply! I started writing a custom ChunkedOutputStream and it appears that in the current implementation calling flush() causes it to write out whatever it has in its buffer as the next chunk. Which means, the next call to write() will start the chunk after that. So I backed up and wrote an HttpEntity based on InputStreamEntity that takes two input streams, one for the request and one for the data. Its writeTo() method writes out the request stream, flush()es, then writes out the data stream. This seems to put a chunk boundary where I want it with only having to subclass the entity. But... I foresee some cases where it may write a chunk naturally, buffer the last little bit of the first stream, then write that tiny chunk when I tell it to flush. (Which to fix, I think I'd have to do like you said and write a more intelligent ChunkedOutputStream and plumb it through.) Other than the behavior in that case being kind of suboptimal, is there anything wrong with that? Is there any other problem with doing it this way? Thanks! jk
