Prokuma
Prokuma

Reputation: 1

How to split HTTP header in C?

I receive HTTP request at socket.

I want to split HTTP request at header and content.

So, I tried this source.

//p is char* type, response too
p = strtok(response, "\r\n\r\n");
while(NULL != p){
    printf("%s\n", p);
    p  = strtok(NULL, "\r\n\r\n");
}

But strtok() replaced "\r\n" by NULL too.

I want replace only "\r\n\r\n".

How should I?

Upvotes: 0

Views: 2623

Answers (1)

pmg
pmg

Reputation: 108968

Try strstr

//p1 is char* type, response and p2 too
p1 = response;
p2 = strstr(response, "\r\n\r\n");
while(NULL != p2){
    printf("%.*s\n", p2 - p1, p1);
    p1 = p2;
    p2 = strstr(p2 + 1, "\r\n\r\n");
}

Upvotes: 3

Related Questions