I am having a bit of trouble 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 header
GET / HTTP/1.0
I get 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 properly, but the values c1, and c2 dont seem to make sense (c1 should return 0, but instead it returns 1).
Whats going on here?
I’m guessing that you’re not giving it this:
but rather this:
or possibly this:
Looking at your code, there should be one blank line between the “
HTTP” output line and the “c1” line, but you have two, implying that the “HTTP” value itself contains a newline.Output some quotes around the values – I bet you see this: