c - Parse url path of GET request -


i'm new c , i've been working on task 7 hours - please don't didn't try.

i want parse path of self-written webserver in c. let's call

http://localhost:8080/hello/this/is/a/test.html 

then browser gets

get /hello/this/is/a/test.html http/1.1 

i want parse /hello/this/is/a/test.html, complete string between "get " (note white space after get) , first white space after /../../..html.

what tried far:

int main() {   ...   char * getpathofgetrequest(char *);   char *pathname = getpathofgetrequest(buf);    printf("%s\n\n%s", buf, pathname);   ... }  char * getpathofgetrequest(char *buf) {    char *startingget = "get ";   char buf_cpy[buflen];   memcpy(buf_cpy, buf, sizeof(buf));    char *urlpath = malloc(1000);   char *path = malloc(1000);   urlpath = strstr(buf_cpy, startingget);    char delimiter[] = " ";    path = strtok(urlpath, delimiter);   path = strtok(null, delimiter);      return path; } 

the pathname has 4 correct chars , may or may not filled other unrelated chars, /hell32984cn)/$"§$. guess has strlen(startingget), can't see relationship between it. mistake?

question code commentary:

char * getpathofgetrequest(char *buf) {    char *startingget = "get ";   char buf_cpy[buflen];   memcpy(buf_cpy, buf, sizeof(buf)); 

the above memcpy copy 4 bytes buf buf_cpy. due buf being pointer char. sizeof(buf) size of pointer (likely: 4). perhaps, instead of using 'sizeof()', have been better use 'strlen()'.

  char *urlpath = malloc(1000);   char *path = malloc(1000);    urlpath = strstr(buf_cpy, startingget); 

perhaps questioner not clear on why urlpath allocated 1000 bytes of memory. in case, above assignment cause 1000 bytes leaked, , defeats purpose of 'urlpath=malloc(1000)'.

the actual effect of above statements urlpath = buf_cpy;, strstr() return position of beginning of 'get ' in buf_copy.

  char delimiter[] = " ";    path = strtok(urlpath, delimiter); 

likewise, above assignment cause 1000 bytes allocated path leaked, , defeats purpose of 'path=malloc(1000)' above.

  path = strtok(null, delimiter);     return path; } 

an alternitive coding:

char *getpathofgetrequest(const char *buf)     {    const char *start = buf;    const char *end;    char       *path=null;    size_t      pathlen;     /* verify there 'get ' @ beginning of string. */    if(strncmp("get ", start, 4))       {       fprintf(stderr, "parse error: 'get ' missing.\n");       goto cleanup;       }     /* set start pointer @ first character beyond 'get '. */    start += 4;     /* start position, set end pointer first white-space character found in string. */    end=start;    while(*end && !isspace(*end))       ++end;     /* calculate path length, , allocate sufficient memory path plus string termination. */    pathlen = (end - start);    path = malloc(pathlen + 1);    if(null == path)       {       fprintf(stderr, "malloc() failed. \n");       goto cleanup;       }     /* copy path string path storage. */    memcpy(path, start, pathlen);     /* terminate string. */    path[pathlen] = '\0';  cleanup:     /* return allocated storage, or null in event of error, caller. */    return(path);    } 

and, finally, if 'strtok()' must used:

char *getpathofgetrequest(char *buf)    {    char *path  = null;     if(strtok(buf, " "))       {       path = strtok(null, " ");       if(path)          path=strdup(path);       }     return(path);    } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

jquery - Keeping Kendo Datepicker in min/max range -