Trying to Write a HTTP Request Parsing Function in CS Course

In my CS course on edX, I need to write four functions that the staff wants us to write in a server implementation code written in C. One of those is parse() where I need to iterate over the request line and extract the method, HTTP-version, absolute path and query. The request line, absolute path and query were passed in to the function as strings line, abs_path and query, respectively. I've written some of the function, but I also want to know the best way to iterate until the end of the string and then move back while examining each character to check if it's part of the HTTP-version and that if it is, that it's HTTP/1.1.

This is the code I currently have for parse():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * Parses a request-line, storing its absolute-path at abs_path 
 * and its query string at query, both of which are assumed
 * to be at least of length LimitRequestLine + 1.
 */
bool parse(const char* line, char* abs_path, char* query)
{
    char* method;
    for (int i = 0, n = strlen(line); i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            method = strstr(line, "GET");
            if (method == NULL)
            {
                error(405);
                return false;
            }
            else if (strcmp(method, "GET") != 0)
            {
                error(405);
                return false;
            }
            
            if (line[i + 1] == ' ' && line[i + 2] != ' ')
            {
                if (line[i + 2] != '/')
                {
                    error(501);
                    return false;
                }
                abs_path = strchr(line, '/');
                char* temp_path = strchr(line, '/');
                if (abs_path == NULL)
                {
                    error(501);
                    return false;
                }
                int index = 0;
                for (int length = strlen(temp_path); index < length; index++)
                {
                    if (temp_path[index] == ' ')
                    {
                        break;
                    }
                }
                strncpy(abs_path, temp_path, index);
                abs_path[index - 1] = '\0';
            }
            
            char* cr = strchr(line, '\r');
            if (line[(int)(cr) - 1] == '1' && line[(int)(cr) - 2] == '.' )
        }
    }
    
    
    
    return true;
}


The variable cr is where I stored the CR from CRLF that's supposed to appear at the end of the request-line. I want to iterate until that and then move backwards.

If anyone can help me with this, it'd be appreciated. I don't want to just be given the solution, though, of course. Just let me know what I'm doing wrong and point me to the right direction.

Edit:

I'm also trying to use this for checking what the version is:

1
2
3
4
5
6
version = strstr(line, "HTTP/1.1");
if (version == NULL)
{
    error(505);
    return false;
}
Last edited on
Topic archived. No new replies allowed.