#recv won't read full http request

11 messages · Page 1 of 1 (latest)

minor quail
#

I am trying to write an http server. Right now I am working on reading an http request using the following function:

int read_http_request(int socket, char *buf, int len)                                                                                                                                                  
{                                                                                                                                                                                                          
        char *s = buf;                                                                                                                                                                                     
        int chars_read = 0;                                                                                                                                                                                
        int c;                                                                                                                                                                                             
        int count = 0;                                                                                                                                                                                     
        do {                                                                                                                                                                                               
                s += chars_read;                                                                                                                                                                           
                c = recv(socket, s, len - chars_read, 0);                                                                                                                                                  
                chars_read += c;                                                                                                                                                                           
        } while ((c > 0) && (strstr(s, "\r\n\n") == NULL) && (chars_read <= len));                                                                                                                         
                                                                                                                                                                                                           
        if (c < 0) return c;                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                           
        return chars_read;                                                                                                                                                                                 
}                       

The data in the buffer looks something like this:

GET / HTTP/1.1
Host: 127.0.0.1
Connection: keep

which is missing the "-alive" portion. When using postman, it reads the request line and just one header.

zinc knollBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

gaunt light
#

strstr(s, "\r\n\n") == NULL seems to be wrorg. are you sure buf is a c-string prior to this else buf[chars_read - 1] = '\0'; line?

minor quail
gaunt light
#

char buff[50] isn't a c-string. rather its a char array. char buff[50] = {0} will turn said array to a c-string.
remember - a c-string is a sequence of chars which is terminated by the null terminator (a 0 byte). any sequence who isn't terminated by said byte can't be treated as a c-string (i.e. cannot be passed into str* family of functions)

minor quail
# gaunt light `char buff[50]` _isn't_ a c-string. rather its a char array. `char buff[50] = {0...

Ok. Changing it to a c-string and deleted the line that adds the null terminator. This helped a little bit.

GET / HTTP/1.1                                                                                                                                                                                             
Host: 127.0.0.1                                                                                                                                                                                            
Connection: keep-    

The output is now this. There is a "-". I am just confused as to why the complete message is not put inside the buffer. Could it be that the server is not sending the entire message? I believe there should be "alive" at the end of the connection header.

gaunt light
#

is there a possibility your buffer can't hold these many chars in it? remember our buffer is 50 chars long. this means it can only hold 49 chars beside the nullterminator

zinc knollBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity