Вопрос

I'm new to C specifically and I'm trying to check some strings.

The following is my code, commented to indicate the issues that I don't understand why they are occuring:

if (strstr(recBuff, "GET / HTTP/1.0\r\n\r\n") != NULL)
    //Send HTTP/1.0 200
    //This gets recognised fine
else if (strstr(recBuff, "GET / HTTP/1.0\r\r") != NULL)
    //Send HTTP/1.0 200
    //This gets recognised fine
else if (strstr(recBuff, "GET / HTTP/1.0\r\n") != NULL)
    //Do something else
    //This never gets picked up, and instead goes to the final else...
else
    //HTTP/1.0 404
    //Etc

I guess my question is why is strstr picking up \r\n\r\n and acting on it, but just \r\n by itself goes through all the way until the final else? There's an else for \r\n\r\n that works, but the else for a single \r\n doesn't work for a single \r\n.

TL;DR "GET / HTTP/1.0\r\n\r\n" gets picked up, but "GET / HTTP/1.0\r\n" doesn't.

Это было полезно?

Решение

You've not reduced your code to an an SSCCE (Short, Self-Contained, Correct Example) so we can't tell what you're doing wrong. However, it is most likely that the data you think has two carriage returns actually doesn't contain the two adjacent carriage returns. However, only some sort of hex dump or something similar will show that for sure.

Here's an SSCCE which shows that your code can work if given the correct data:

#include <stdio.h>
#include <string.h>

int main(void)
{
    char *examples[] =
    {
        "YYYYGET / HTTP/1.0\r\nExample 1 Single CRLF",
        "YYYYGET / HTTP/1.0\r\n\r\nExample 2 Double CRLF",
        "YYYYGET / HTTP/1.0\r\r\nExample 3 Double CR",
        "YYYYGET / HTTP/1.0\n\nExample 4 Double NL",
    };

    for (int i = 0; i < 4; i++)
    {
        char *recBuff = examples[i];
        printf("Data:\n%s\n", recBuff);
        if (strstr(recBuff, "GET / HTTP/1.0\r\n\r\n") != NULL)
            printf("Option 1 - double CRLF\n");
        else if (strstr(recBuff, "GET / HTTP/1.0\r\r") != NULL)
            printf("Option 2 - double CR\n");
        else if (strstr(recBuff, "GET / HTTP/1.0\r\n") != NULL)
            printf("Option 3 - single CRLF\n");
        else
            printf("Option 4 - no match\n");
    }

    return 0;
}

Sample output

$ ./counter-example
Data:
YYYYGET / HTTP/1.0
Example 1 Single CRLF
Option 3 - single CRLF
Data:
YYYYGET / HTTP/1.0

Example 2 Double CRLF
Option 1 - double CRLF
Data:
YYYYGET / HTTP/1.0
Example 3 Double CR
Option 2 - double CR
Data:
YYYYGET / HTTP/1.0

Example 4 Double NL
Option 4 - no match
$

So, if you are not seeing something similar with your code, you aren't getting the data you thought you were getting.

The YYYY part is not necessary to the reproduction; neither is the Example n information. The trailing part makes sure the fairly difficult to discriminate strings are recognizable; the YYYY is arguably fluff since the HTTP protocol would not start with such garbage.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top