4

I'm trying to write code that parses a HTTP GET request and checks if the "Host" is www.bbc.co.uk.

This is my working code:

char data[] = "GET /news/ HTTP/1.1\nHost: www.bbc.co.uk\nConnection: keep-alive";
    unsigned int size = strlen(data);

    if (size>3 && data[0] == 'G' && data[1] == 'E' && data[2] == 'T'){ //If GET Request
        int host_index = -1;

        for (int i=4; i<size-4; i++){
            if (data[i] == 'H' && data[i+1] == 'o' && data[i+2] == 's' && data[i+3] == 't'
                    && data[i+4] == ':' && data[i+5] == ' '){
                host_index = i+6;
            }
        }

        if ( host_index != -1 && size > host_index+11 &&
                data[host_index] == 'w' && data[host_index+1] == 'w' && data[host_index+2] == 'w' &&
                data[host_index+3] == '.' && data[host_index+4] == 'b' && data[host_index+5] == 'b' &&
                data[host_index+6] == 'c' && data[host_index+7] == '.' && data[host_index+8] == 'c' &&
                data[host_index+9] == 'o' && data[host_index+10] == '.' && data[host_index+11] == 'u' &&
                data[host_index+12] == 'k')
        {
            printf("BBC WEBSITE!\n");
        }

    }

I think this is a lot of code for not a lot. How can I make this code more compact?

[Please keep it to plain C. No 3rd party libs]

MANY THANKS!

1
  • It depends on how pedantic you want to be and this detail is entirely missing in the post. How much of the string needs to be in the expected format? If you do not care, just do if strstr(data,"www.bbc.co.uk")) printf("BBC WEBSITE!\n"); Commented Dec 4, 2014 at 21:02

4 Answers 4

4

Why don't you use strstr() ?

Split big string into chunks using strstr(), and then parse smaller chunks by separate routines

Sign up to request clarification or add additional context in comments.

Comments

2

Your code can be written more compactly as:

   if (!strncmp(data, "GET ", 4) && strstr(data, "\nHost: www.bbc.co.uk\n"))
       printf("BBC WEBSITE!\n");

However, while this may work 99.9% of the time, it doesn't handle arbitrary white space after the colon. Regular expressions would be helpful, but this would need a third-party library, which you can't have.

One solution is:

  if (!strncmp(data, "GET ", 4)) {
      const char *p = data;
      char buf[99 + 1];
      buf[0] = 0;
      while ((p = strchr(p, '\n')) && sscanf(++p, "Host: %99s", buf) != 1)
          ;
      if (!strcmp(buf, "www.bbc.co.uk"))
          printf("BBC WEBSITE!\n");
  }

Edit: The above solution permits any number of CRs and/or LFs after "Host:". I am unclear whether the HTTP/1.1 LWS (Linear White Space) permits this. If not, and to allow only zero or more spaces and tabs, change the sscanf to read:

   (sscanf(++p, "Host:%*[ \t]%99[^ \t]", buf) == 1 || 
    sscanf(p,   "Host:%99[^ \t]",        buf) == 1)

As you can see, it starts to get messy.

Comments

2

Using only standard library functions, you could do:

char data[] = 
    "GET /news/ HTTP/1.1\n"
    "Host: www.bbc.co.uk\n"
    "Connection: keep-alive";

char *found_host = strstr(data, "Host: ");

if (found_host != NULL) {
    found_host += sizeof("Host: ") - 1;

    char *end_of_host = strpbrk(found_host, "\r\n");

    if (end_of_host != NULL) {
        int equal = strncmp(found_host, "www.bbc.co.uk", end_of_host - found_host);
    }
}

Note that this does not account for an arbitrary amount of whitespace between the colon and the value.

Comments

1
char data[] = "GET /news/ HTTP/1.1\nHost: www.bbc.co.uk\nConnection: keep-alive";
unsigned int size = strlen(data);
char buff[size];
sscanf(data, "%*[^:]:%s", buff);
if(strcmp(buff, "www.bbc.co.uk")==0)
    puts("BBC");

3 Comments

Maybe if (sscanf(data, "%*[^\n] Host:%s", buff) == 1) ...`?
@chux It by the needs.
can you explain the sscanf part or comment just that line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.