2

I'm writing a simple function that split binary/post-data/html from a HTTP response. The HTTP headers are terminated by \r\n\r\n the rest is the message.

I have wrote this:

 #define MAX_BUFFER_SIZE 256
    //... 
    int size = 0;
    int buf_size = MAX_BUFFER_SIZE;
    char * headers = malloc(MAX_BUFFER_SIZE);
    char * newbuf;

    while(httpresponse[size]) {
        if(httpresponse[size]     == '\r' &&
           httpresponse[size + 1] == '\n' &&
           httpresponse[size + 2] == '\r' &&
           httpresponse[size + 3] == '\n') {
            break;
        }

        headers[size] = httpresponse[size];     

        if(size >= buf_size) {
            buf_size += MAX_BUFFER_SIZE;
            newbuf = realloc(headers, buf_size);

           if(NULL ==  newbuf) exit(1);
           headers = newbuf;

         }

           size ++;
        }

        printf("%s\n", headers);

the httpresponse variable, has value-like:

HTTP/1.1 200 OK
Date: Fri, 23 Mar 2012 15:28:17 GMT
Expires: Sat, 23 Mar 2013 15:28:17 GMT
Cache-Control: public, max-age=31536000
Last-Modified: Thu, 14 Apr 2011 15:46:35 GMT
Content-Type: image/jpeg
Content-Length: 12745
X-XSS-Protection: 1; mode=block
Connection: close

���������I1��} �g������'�B�f�p���ohd]sft�����J�������1����瘿ٱ����$3�G�8��4=�E�i����ܼG����H��nbi�"�1��b[Ǘl��++���OPt�W��>�����i�]t�QT�N/,Q�Qz������0�`    N7���M��f��S�Š�x9k��$*

//more binary... 

but the above C program, print the following text:

HTTP/1.1 200 OK
Date: Fri, 23 Mar 2012 17:12:09 GMT
Expires: Sat, 23 Mar 2013 17:12:09 GMT
Last-Modified: Thu, 14 Apr 2011 15:46:35 GMT
Content-Type: image/jpeg
Content-Length: 12745
X-XSS-Protection: 1; mode=block
Cache-Control: public, max-age=31536000
Age: 3746
�2�/���ms���|ނ����LQr2K3�v��J.�,�z��^Oy����s(ct���X`iA����I����U�{

instead of:

    HTTP/1.1 200 OK
    Date: Fri, 23 Mar 2012 15:28:17 GMT
    Expires: Sat, 23 Mar 2013 15:28:17 GMT
    Cache-Control: public, max-age=31536000
    Last-Modified: Thu, 14 Apr 2011 15:46:35 GMT
    Content-Type: image/jpeg
    Content-Length: 12745
    X-XSS-Protection: 1; mode=block
    Connection: close

how to fix this? Thanks in advance.

0

3 Answers 3

1

I use following code:

const QByteArray& data = socket->readAll();
int index = data.indexOf("\r\n\r\n");
QString sHeader;
QString sBody;
if (index < 0)
    sHeader = QString::fromUtf8(data);
else
{
    sHeader = QString::fromUtf8(data.left(index));
    sBody = QString::fromUtf8(data.mid(index + 4));
}

QIssHttpRequestHeader requestHeader(sHeader); // QIssHttpRequestHeader is a copy of QHttpRequestHeader from Qt4

if (requestHeader.method() != "GET")
{
    send501Error(socket);
    return;
}

QUrl url(requestHeader.path());
...
Sign up to request clarification or add additional context in comments.

Comments

0

I think your code is OK except that malloc doens't initialize the memory it allocates. This means when you BREAK out of your while loop the "headers" string you've captured doesn't have a null byte terminating it.

Two ways to fix - ether use "calloc" which initializes the memory to binary zeroes or insert one line of code before your call to printf:

 headers[size] = '\0'

Cheers

3 Comments

Hmm -- and httpresponse is a "single-byte wide" string -- how about using the "strstr" function to look for the <cr><lf><cr><lf> instead of using the while loop?
I have tried it. but if I split the \r\n\r\n with strstr() it returns the binary only. And I want split headers and body(message), for this reason I have used while loop.
ahh -- well if you want both pieces why not use a capturing regular expression? /^(.+?)\r\n\r\n(.+)$/s
0

Are you sure about the outputs? you said httpresponse, ends with X-XSS-Protection: 1; mode=block and Connection: close in this case, but your ouptput ends like this: X-XSS-Protection: 1; mode=block andCache-Control: public, max-age=31536000 ??!!?

if you are sure about copying the outputs right, well it seems that the end of your while is incorrect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.