Thread: http request parser problem

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    66

    http request parser problem

    Hi.

    I have a function which parses given parts from an http request send by the browser.
    The first parameter is the string received from the browser, the second is the start string from where i start parsing the main string, and the separator is the end string.

    For ex.:
    first parameter: "GET /a/b/c/d/e/234/234/234234/45645646/dsf/saaaaa HTTP/1.1 Host: 192.168.1.102:4444"...
    the second parameter is "GET /",
    the third is " ".

    First i calculate the start end the end positions of the searched part (strpos is a simple wrapper for strstr, with some extra logic...i can pass an offset as the third parameter, from where i start searching for the string`s position), then if start_pos and end_pos are both greater than -1, I allocate memory for the found part, and copy it to the return variable.

    The problem is that sometimes the result string contains some extra characters, like when you allocate memory for a string, and it still contains some data from a previous use. The weird part is that after malloc I bzero the whole string, and still got results like:

    a/b/c/d/e/234/234/234234/45645646/dsf/saaaaa - the GET received from the browser, which needs to be extracted
    a/b/c/d/e/234/234/234234/45645646/dsf/saaaaaÉ - the result of the parsing


    I tried to malloc just length-1 or strncpy length-1, but in this case i loose the last character.


    And here is the function:

    Code:
     
    const char *parse_part(const char *buffer, const char *str, const char *separator)
    {
    
    	int start_pos 		= strpos(buffer, str, 0)+strlen(str);
    	int end_pos			= strpos(buffer, separator, start_pos);
    	int length			= 0;
    	char *part			= NULL;
    
    	if (start_pos < 1 || end_pos < 1)
    	{
    		log_error("Invalid headers received!");
    		return NULL;
    	}
    	length = end_pos - start_pos;
    
    	part = (char *)malloc(length);
    	bzero(part, length);
    
    	strncpy(part, buffer+start_pos, length);
    
    	return part;
    }
    Any idea is welcome.

    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You say you bzero the whole string, and yet what do I see in the code?
    Code:
    bzero(part, length);
    That's not the whole string. (In fact, you don't have any way of knowing how much space there is available to you. Hope you don't overflow!) You need to, if nothing else, zero out the zero character and put +1 at the end.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    66
    First, thanks for your reply.

    Maybe I don`t understand what are you saying, but if the length I`m gonna work with is let say 30 bytes, and I allocate 30 bytes of memory for the string, and zero it, it doesn`t mean that the whole string will be filled with zeros?

    I also tried malloc(length * sizeof(char)), but on my architecture char is 1 byte long, so the result was the same.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Whoops, I was looking at buffer, not part. So you need to malloc length+1, make sure the last is a zero, then strncpy.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    66
    You were right tabstop, i had to allocate +1 bytes, now it`s ok.

    Thank you so much for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c programming http get request
    By daza166 in forum C Programming
    Replies: 2
    Last Post: 02-16-2011, 05:55 AM
  2. HTTP Get request to C Program
    By dcwang3 in forum C Programming
    Replies: 7
    Last Post: 11-16-2010, 06:11 PM
  3. HTTP GET request
    By aosmith in forum C Programming
    Replies: 4
    Last Post: 03-21-2010, 07:00 AM
  4. HTTP GET and POST REQUEST
    By strickey in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-20-2007, 04:23 PM