Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split String by Carriage Return C

I am having a problem where I am trying to split an HTTP request by a carriage return for a web proxy. The request does not seem to split.

Here is an example request: GET /pub/WWW/TheProject.html HTTP/1.1\r\nHost: www.w3.org\r\n

My attempt is:

char* split_request;
split_request = strtok(request, "\r\n");

But it never gets split? I am not sure what I am missing. It seems to split when I am using wget or the browser to test the web proxy, but doesn't with telnet.

like image 872
user1816561 Avatar asked Sep 15 '26 15:09

user1816561


1 Answers

Are you doing this way?

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

int main (void)
{
    char str[] = "GET /pub/WWW/TheProject.html HTTP/1.1\r\nHost: www.w3.org\r\n";
    char* pch = NULL;

    pch = strtok(str, "\r\n");

    while (pch != NULL)
    {
        printf("%s\n", pch);
        pch = strtok(NULL, "\r\n");
    }
    return 0;
}

Output:

GET /pub/WWW/TheProject.html HTTP/1.1   
Host: www.w3.org
like image 144
Jagannath Avatar answered Sep 17 '26 07:09

Jagannath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!