0
votes

I'm writing a code to read the bytes of a request body and this requires knowing the Content-Length or Transfer-encoding ahead of time to safely transfer the message to the client. According to the RCF2616 Section 14.13:

Any Content-Length greater than or equal to zero is a valid value.

In my code Implementation, I achieved this by getting the Content-Length: header field which returns 0,which I guess is a valid response but not the required amount of bytes. Have tried in the below code to read the InputStream from the socket still the amount is achieved but this seem to be failing.Any pointers achieving this? Can provide more code if necessary.

Here is the calling method to get content-length header and read the bytes in chunk till the exact amount is achieved:

//Gets the Content-Length header value
       int contLengthOffset = Integer.parseInt(newRequest.getHeaderField("Content-Length"));
               int Offset = contLengthOffset;           
        if(Offset >= 0) {
           //Any Content-Length greater than or equal to zero is a valid value.
        count = QueryStreamClass.ReadFullyHelper(socket.getInputStream(), Offset);
                }

Below is the method that reads the content-length:

/**
 * Read the content-length to determine the transfer-length of the message.
 * We need enough bytes to get the required message.
 * @param Stream
 * @param size
 */
public static String ReadFullyHelper(InputStream Stream, int size) {

    int Read;
    int totalRead = 0;
    int toRead = GMInjectHandler.buffer;;
    StringBuilder Request = new StringBuilder();

    if(toRead > size) {
        toRead = size;
    }

    while(true) {

        try {
            final byte[] by = new byte[toRead];
            Read = Stream.read(by, 0, toRead);

            if(Read == -1){
                break;
            }

            Request.append(new String(by, 0, Read));

            totalRead += Read;
            if (size - totalRead < toRead) {
                toRead = size - totalRead;
            }
            if (totalRead == size) {
                break;
            }

        } catch (IOException e) {
            Log.e(TAG, "Error reading stream", e);
        }
    }
    return Request.toString();
}
1
You may want to start with changing the getHeaderField() parameter from "Content_Length" to "Content-Length". The former is not an HTTP header name and won't give you any results. - yole
Your 'method that reads the content-length' does no such thing. All it does is read the content, in a probably invalid and certainly unnecessarily convoluted way. NB Why is your variable called contLengthOffset when there is no 'offset' about it? - user207421
Note that DataInputSteam has readFully() - Stefan Haustein
@yole that was a typo and have adjusted my code to reflect my issue. - CodeZero
@EJP My aim writing that method was to read the content until it got the write length. Any pointers correcting that? Am just calling it a name nothing name - CodeZero

1 Answers

1
votes

'This seems to be failing' is not a problem description, but:

public static String ReadFullyHelper(InputStream Stream, int size) {

You can reduce this entire method to the following:

DataInputStream din = new DataInputStream(Stream);
byte[][ buffer = new byte[size];
din.readFully(buffer);
return new String(buffer, 0, size); // or you may want to use a specific encoding here