-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing.c
More file actions
197 lines (181 loc) · 6.2 KB
/
parsing.c
File metadata and controls
197 lines (181 loc) · 6.2 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// Created by dran on 4/9/22.
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "parsing.h"
int parse_request_string(char* working_request, bool* is_get, char* hostname, char* path,
enum http_version* version, bool* proxy_keep_alive) {
char* end_of_first_line;
char* divider_a;
char* divider_b;
char* slash;
// will not parse request line unless there is a complete line
end_of_first_line = strchr(working_request, '\n');
if (end_of_first_line == NULL) {
return FAIL;
}
// parse request line
// starting with http version
*end_of_first_line = '\0';
divider_b = strrchr(working_request, ' '); // TODO : what if the parts of a request line is seperated by more than one space char?
if (divider_b == NULL) {
*version = MALFORMED;
return SUCCESS;
}
divider_b ++;
if (!matches_command(divider_b, "HTTP/")) {
*version = MALFORMED;
return SUCCESS;
} else if (matches_command(divider_b, "HTTP/1.1")){
*version = DOT_ONE;
} else if (matches_command(divider_b, "HTTP/1.0")) {
*version = DOT_ZERO;
} else {
*version = NOT_SUPPORTED;
return SUCCESS;
}
// request method
if (matches_command(working_request, "GET")) {
*is_get = true;
} else if (matches_command(working_request, "HEAD")) {
*is_get = false;
} else if (matches_command(working_request, "POST")) {
*is_get = false;
} else if (matches_command(working_request, "PUT")) {
*is_get = false;
} else if (matches_command(working_request, "DELETE")) {
*is_get = false;
} else if (matches_command(working_request, "CONNECT")) {
*is_get = false;
} else if (matches_command(working_request, "TRACE")) {
*is_get = false;
} else if (matches_command(working_request, "PATCH")) {
*is_get = false;
} else if (matches_command(working_request, "OPTIONS")) {
*is_get = false;
}else {
*version = MALFORMED;
return SUCCESS;
}
// copy url
divider_a = strchr(working_request, ' ');
divider_a ++;
divider_b --;
*divider_b = '\0';
if (divider_a >= divider_b) {
*version = MALFORMED;
return SUCCESS;
} else if (!matches_command_case_insensitive(divider_a, "http://")) {
*version = MALFORMED;
} else {
slash = strchr(divider_a + 7, '/');
strncpy(hostname, divider_a + 7, slash - divider_a - 7);
hostname[slash - divider_a - 7] = '\0';
strncpy(path, slash, MAX_URL_SIZE);
// strip the last char if it is '/', except when url is "/"
slash = strrchr(path, '/');
if (slash != path && (slash + 1) == strrchr(path, '\0')) {
*slash = '\0';
}
}
*divider_b = ' ';
// *end_of_first_line = '\n';
// parse any request headers
*proxy_keep_alive = false;
divider_a = end_of_first_line + 1;
divider_b = strchr(divider_a, '\n');
while (divider_b != NULL) {
if (matches_command_case_insensitive(divider_a, "Proxy-Connection: keep-alive")) {
*proxy_keep_alive = true;
} else if (matches_command_case_insensitive(divider_a, "Host: ")) {
// capture hostname
divider_a = strchr(divider_a, ' ');
while (*divider_a == ' ') {
divider_a++;
}
strncpy(hostname, divider_a, divider_b - divider_a - 2);
hostname[divider_b - divider_a - 1] = '\0';
}
divider_a = divider_b + 1;
divider_b = strchr(divider_a, '\n');
}
return SUCCESS;
}
void parse_response_header(char* response_string, int* response_header_length, int* response_content_length) {
char* divider_a;
char* divider_b;
*response_header_length = -1;
*response_content_length = -1;
// parse any response headers
divider_a = response_string;
divider_b = strchr(divider_a, '\n');
while (divider_b != NULL) {
if (matches_command_case_insensitive(divider_a, "Content-length: ")) {
divider_a = strchr(divider_a, ' ');
*response_content_length = atoi(divider_a);
break;
}
divider_a = divider_b + 1;
divider_b = strchr(divider_a, '\n');
}
if (*response_content_length == -1) { // header only, no body
divider_b = strrchr(response_string, '\n');
*response_header_length = divider_b - response_string + 1;
} else {
// find double /r/n
divider_a = strchr(response_string, '\r');
while (divider_a != NULL) {
if (matches_command(divider_a, "\r\n\r\n")) {
*response_header_length = divider_a - response_string + 4;
return;
} else {
divider_a = strchr(divider_a + 1, '\r');
}
}
}
}
int find_href(FILE* text_file, char* output_buffer) {
int bytes_read;
char file_buffer[JOB_REQUEST_BUFFER_SIZE + 1];
char* found_h;
char* closing_quote;
found_h = NULL;
while (1) {
bytes_read = fread(file_buffer, sizeof(char ), MAX_URL_SIZE, text_file);
if (bytes_read == 0) {
return FINISHED;
}
found_h = strchr(file_buffer, 'h');
while (found_h != NULL) {
if (matches_command(found_h, "href=")) {
closing_quote = strchr(found_h + 6, '\"');
if (closing_quote != NULL) {
strncpy(output_buffer, found_h + 6, closing_quote - found_h - 6);
output_buffer[closing_quote - found_h - 6] = '\0';
fseek(text_file, -1 * (bytes_read - (closing_quote - file_buffer)), SEEK_CUR );
return SUCCESS;
} else {
return FAIL;
}
}
found_h = strchr(found_h + 1, 'h');
}
}
}
int matches_command(char* target, char* command) {
if (strncmp(target, command, strlen(command)) == 0) {
return 1;
} else {
return 0;
}
}
int matches_command_case_insensitive(char* target, char* command) {
if (strncasecmp(target, command, strlen(command)) == 0) {
return 1;
} else {
return 0;
}
}