简体   繁体   English

C - strtok和strcmp

[英]C - strtok and strcmp

I am having a bit of trouble using strtok with strcmp. 使用strtok和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). 正如您所看到的,strtok()正确地解析了字符串,但值c1和c2似乎没有意义(c1应该返回0,而是返回1)。

Whats going on here? 这里发生了什么?

I'm guessing that you're not giving it this: 我猜你没有给它这个:

GET / HTTP/1.0

but rather this: 而是这个:

GET / HTTP/1.0\n

or possibly this: 或者可能这样:

GET / HTTP/1.0\r\n

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. 查看代码,“ HTTP ”输出行和“ c1 ”行之间应该有一个空行,但是您有两行,这意味着“ HTTP ”值本身包含换行符。

Output some quotes around the values - I bet you see this: 在值周围输出一些引号 - 我打赌你看到这个:

HTTP: "HTTP/1.0
"

As you can see from the blank lines in your output (and as several people have said already) there are control characters on the end of your HTTP/1.0 . 正如您从输出中的空白行中看到的那样(并且已经有几个人已经说过), HTTP/1.0末尾有控制字符。 You could fix this. 可以解决这个问题。

But why are you writing a new HTTP request parser in C? 但是为什么要在C中编写新的HTTP请求解析器? It's 2009! 这是2009年! There are loads of them already out there, some of which are even correct, many liberally licensed. 已经有很多它们已经存在,其中一些甚至是正确的,许多是自由许可的。 And even if you do need to write your own for some reason, you should use a safe language (Python, Java, Lua, C#, Perl, something ) so that if you make a trivial error counting characters, you don't end up with a big security hole in your program. 即使你确实需要编写自己出于某种原因,你应该使用一个安全的语言(Python和Java的,Lua中,C#,Perl和东西 ),这样,如果你犯了一个微不足道的错误计数的字符,你不结束在你的程序中有一个很大的安全漏洞。 (And even if you somehow have to use C, strtok is a particularly egregious C function.) (即使你不知何故必须使用C, strtok也是一个特别令人震惊的C函数。)

From your output it looks like there may be a '/n' at the end of the HTTP/1.0 string? 从您的输出看起来,在HTTP / 1.0字符串的末尾可能有一个'/ n'? Richie is too fast for me ;) 里奇对我来说太快了;)

Try trimming/removing any white space on the input string before you tokenize it. 在对其进行标记之前,请尝试修剪/删除输入字符串上的任何空白区域。

Try using 尝试使用

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

so that you ignore trailing whitespace. 这样你就可以忽略尾随空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM