C - strtok and strcmp

I am having problems using strtok with strcmp.

//Handles the header sent by the browser
char* handleHeader(char *header){
        //Method given by browser (will only take GET, POST, and HEAD)
        char *method,*path, *httpVer;

        method = (char*)malloc(strlen(header)+1);
        strcpy(method,header);
        method = strtok(method," ");


        path = strtok(NULL," ");
        httpVer = strtok(NULL, " ");
        printf("\nMethod: %s\nPath: %s\nHTTP: %s\n",method,path,httpVer);


        printf("\nc1: %d\nc2: %d\n",strcmp(httpVer,"HTTP/1.0"),strcmp(httpVer,"HTTP/1.1"));

        if(!(!strcmp(httpVer,"HTTP/1.0") || (!strcmp(httpVer,"HTTP/1.1")))){
                printf("\ngive a 400 error\n");
                return "400 foo";
        }


        if(!strcmp(method,"GET")){
                //char *path = strtok(NULL," ");

                //If they request the root file, change the path to index.html
                if(!strcmp(path,"/")){
                        path = (char*)malloc(strlen(BASE_DIR) + strlen("/index.html")+1);
                        strcpy(path,"/index.html");
                }
                 return readPage(path,2);
        }
}

      

If I give it the following title

GET / HTTP/1.0

      

I am getting this output:

Method: GET
Path: /
HTTP: HTTP/1.0


c1: 1
c2: -1

give a 400 error

      

As you can see, strtok () parses the string correctly, but the values ​​c1 and c2 are meaningless (c1 should return 0, but instead it returns 1).

What's going on here?

+2


source to share


4 answers


I assume you are not giving this:

GET / HTTP/1.0

      

but rather this:

GET / HTTP/1.0\n

      

or perhaps this:



GET / HTTP/1.0\r\n

      

If you look at your code, there should be one blank line between the output line " HTTP

" and " c1

", but you have two, assuming the value " HTTP

" itself contains a newline.

Print some quotes around the meanings - I'm sure you see this:

HTTP: "HTTP/1.0
"

      

+6


source


As you can see from the blank lines in your output (and as several people have already said), HTTP/1.0

there are control characters at the end of yours . You can fix this.



But why are you writing a new HTTP request parser in C? It's 2009! There are already many of them, some of them are even correct, many are licensed. And even if you need to write your own for some reason, you should use a safe language (Python, Java, Lua, C #, Perl, whatever) so that if you make the trivial mistake of counting characters, you don't get caught with a big hole in your program. (And even if you have to use C somehow, strtok

is a particularly egregious C feature.)

+2


source


Does your output show that there might be "/ n" at the end of the HTTP / 1.0 line? Richie is too fast for me;)

Try trimming / removing any whitespace on the input string before making it tokenized.

+1


source


Try to use

strncmp(httpVer, "HTTP/1.0", 8)

      

to ignore trailing spaces.

-1


source







All Articles