How to read HTTP chunk-size from servlet?

Discussion in 'Java' started by Scott Harper, Jan 17, 2007.

  1. Scott Harper

    Scott Harper Guest

    I have a servlet that accepts POSTs from a J2ME client. I need the length of
    the message body.

    Ordinarily, I'd just read the Content-Length header (or call the
    ServletRequest.getContentLength() method). Unfortunately, that header isn't
    present. I'm pretty sure this is because there is a Transfer-Encoding header
    present with a value of "chunked". Section 4.4 of the HTTP spec makes it
    sound like the Transfer-Encoding and Content-Length headers don't always play
    well together.

    I'm not sure how the Transfer-Encoding header is getting added (I'm not
    explicitly putting it there). I'm guessing that the MIDlet implementation is
    putting it in there by default. At any rate, the size of data being passed is
    certainly not large enough to warrant chunking.

    Anyway, sections 3.6 and 3.6.1 of the HTTP spec seem to describe a way to get
    the chunk size from the message body. But it sort of sounds like you need to
    parse through the binary http packet to get at that data. There don't appear
    to be any ServletRequest methods to get to it...

    Does anyone have any pointers or examples on how to get this data?


    thanks,
    Scott
     
    Scott Harper, Jan 17, 2007
    #1
    1. Advertising

  2. Scott Harper

    Daniel Pitts Guest

    Scott Harper wrote:
    > I have a servlet that accepts POSTs from a J2ME client. I need the length of
    > the message body.
    >
    > Ordinarily, I'd just read the Content-Length header (or call the
    > ServletRequest.getContentLength() method). Unfortunately, that header isn't
    > present. I'm pretty sure this is because there is a Transfer-Encoding header
    > present with a value of "chunked". Section 4.4 of the HTTP spec makes it
    > sound like the Transfer-Encoding and Content-Length headers don't always play
    > well together.
    >
    > I'm not sure how the Transfer-Encoding header is getting added (I'm not
    > explicitly putting it there). I'm guessing that the MIDlet implementation is
    > putting it in there by default. At any rate, the size of data being passed is
    > certainly not large enough to warrant chunking.
    >
    > Anyway, sections 3.6 and 3.6.1 of the HTTP spec seem to describe a way to get
    > the chunk size from the message body. But it sort of sounds like you need to
    > parse through the binary http packet to get at that data. There don't appear
    > to be any ServletRequest methods to get to it...
    >
    > Does anyone have any pointers or examples on how to get this data?
    >
    >
    > thanks,
    > Scott

    My first thought is "why do you need to know the length of the POST
    data"? That should all be taken care of for you by the framework.

    My second thought is "http metadata is text, there isn't a binary http
    packet for the headers"...
    My third thought is "why am I iterating my thoughts this way"?

    As far as I know, you shouldn't need to concern yourself with the
    Content-Length header from a user-agent. If you give me a good reason
    why, I'll remember it when I need it :)
     
    Daniel Pitts, Jan 18, 2007
    #2
    1. Advertising

  3. Scott Harper

    vahan Guest

    Hi Scott as I know, there isn't spec. method of servlet for reading
    chunked data length , but there is
    algoritm to do this :
    HTTP spec. chapter 19.4.6
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.4.6


    Scott Harper wrote:
    > I have a servlet that accepts POSTs from a J2ME client. I need the length of
    > the message body.
    >
    > Ordinarily, I'd just read the Content-Length header (or call the
    > ServletRequest.getContentLength() method). Unfortunately, that header isn't
    > present. I'm pretty sure this is because there is a Transfer-Encoding header
    > present with a value of "chunked". Section 4.4 of the HTTP spec makes it
    > sound like the Transfer-Encoding and Content-Length headers don't always play
    > well together.
    >
    > I'm not sure how the Transfer-Encoding header is getting added (I'm not
    > explicitly putting it there). I'm guessing that the MIDlet implementation is
    > putting it in there by default. At any rate, the size of data being passed is
    > certainly not large enough to warrant chunking.
    >
    > Anyway, sections 3.6 and 3.6.1 of the HTTP spec seem to describe a way to get
    > the chunk size from the message body. But it sort of sounds like you need to
    > parse through the binary http packet to get at that data. There don't appear
    > to be any ServletRequest methods to get to it...
    >
    > Does anyone have any pointers or examples on how to get this data?
    >
    >
    > thanks,
    > Scott
     
    vahan, Jan 18, 2007
    #3
  4. Scott Harper

    Rogan Dawes Guest

    Daniel Pitts wrote:
    > Scott Harper wrote:
    >> I have a servlet that accepts POSTs from a J2ME client. I need the length of
    >> the message body.
    >>
    >> Ordinarily, I'd just read the Content-Length header (or call the
    >> ServletRequest.getContentLength() method). Unfortunately, that header isn't
    >> present. I'm pretty sure this is because there is a Transfer-Encoding header
    >> present with a value of "chunked". Section 4.4 of the HTTP spec makes it
    >> sound like the Transfer-Encoding and Content-Length headers don't always play
    >> well together.
    >>
    >> I'm not sure how the Transfer-Encoding header is getting added (I'm not
    >> explicitly putting it there). I'm guessing that the MIDlet implementation is
    >> putting it in there by default. At any rate, the size of data being passed is
    >> certainly not large enough to warrant chunking.
    >>
    >> Anyway, sections 3.6 and 3.6.1 of the HTTP spec seem to describe a way to get
    >> the chunk size from the message body. But it sort of sounds like you need to
    >> parse through the binary http packet to get at that data. There don't appear
    >> to be any ServletRequest methods to get to it...
    >>
    >> Does anyone have any pointers or examples on how to get this data?
    >>
    >>
    >> thanks,
    >> Scott

    > My first thought is "why do you need to know the length of the POST
    > data"? That should all be taken care of for you by the framework.
    >
    > My second thought is "http metadata is text, there isn't a binary http
    > packet for the headers"...
    > My third thought is "why am I iterating my thoughts this way"?
    >
    > As far as I know, you shouldn't need to concern yourself with the
    > Content-Length header from a user-agent. If you give me a good reason
    > why, I'll remember it when I need it :)
    >


    Use ServletRequest.getInputStream() or .getReader(), and simply read
    from them.

    If you are trying to limit the size of the body you will accept, simply
    keep a tally of how many bytes you have read, and bail if you read too many.

    In case you are not familiar with the idiom, something like this will
    probably work for you:

    InputStream is = request.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buff = new byte[2048];
    while ((int got=is.read(buff))>0) {
    baos.write(buff,0,got);
    if (baos.size()>MAX)
    throw new Exception("Too big");
    }
    byte[] requestBody = baos.toByteArray();

    Rogan

    P.S. Daniel: The InputStream that you get from getInputStream() does the
    de-chunking for you automatically, so you don't actually get to see what
    size the chunks were. Google for ChunkedInputStream for more info.
     
    Rogan Dawes, Jan 18, 2007
    #4
  5. Scott Harper

    Lew Guest

    Rogan Dawes wrote:
    > If you are trying to limit the size of the body you will accept, simply
    > keep a tally of how many bytes you have read, and bail if you read too
    > many.
    >
    > In case you are not familiar with the idiom, something like this will
    > probably work for you:
    >
    > InputStream is = request.getInputStream();
    > ByteArrayOutputStream baos = new ByteArrayOutputStream();
    > byte[] buff = new byte[2048];
    > while ((int got=is.read(buff))>0) {
    > baos.write(buff,0,got);
    > if (baos.size()>MAX)
    > throw new Exception("Too big");
    > }
    > byte[] requestBody = baos.toByteArray();


    Or, to prevent going over before you get there, put the test before the write().

    for ( int got; (got = is.read(buff)) > 0; )
    {
    if ( (baos.size() + got) > MAX )
    throw new Exception("Too big");
    baos.write(buff,0,got);
    }

    Works out about the same if the first version of MAX is buff.length larger
    than the second. (My use of the 'for' idiom vs. the 'while' idiom is not
    relevant, just my own style. I thought folks might benefit from seeing both
    forms.)

    - Lew
     
    Lew, Jan 19, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ray Thomas
    Replies:
    1
    Views:
    3,341
    Ray Thomas
    Jul 21, 2004
  2. Sanjeeb
    Replies:
    3
    Views:
    447
    Ryan Kelly
    Aug 3, 2010
  3. Stephen Beacom via .NET 247

    can you set http chunk size in c#.net?

    Stephen Beacom via .NET 247, May 31, 2005, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    356
    Stephen Beacom via .NET 247
    May 31, 2005
  4. bwv549
    Replies:
    3
    Views:
    490
    Eleanor McHugh
    Jun 17, 2009
  5. Leif Wessman
    Replies:
    0
    Views:
    155
    Leif Wessman
    Sep 9, 2004
Loading...

Share This Page