Your code can be written more compactly, for example:
if (!strncmp(data, "GET ", 4) && strstr(data, "\nHost: www.bbc.co.uk\n"))
printf("BBC WEBSITE!\n");
However, although this may work 99.9% of the time, it does not process arbitrary empty space after the colon. Regular expressions would be useful, but you would need a third-party library that you donβt have.
One solution:
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");
}
. CR / LF "Host:". , HTTP/1.1 LWS ( ). , , sscanf :
(sscanf(++p, "Host:%*[ \t]%99[^ \t]", buf) == 1 ||
sscanf(p, "Host:%99[^ \t]", buf) == 1)
, .