-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsing-test.c
More file actions
297 lines (272 loc) · 11.4 KB
/
Copy pathparsing-test.c
File metadata and controls
297 lines (272 loc) · 11.4 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//
// this is a little jank, but whatever, it's school not real life
// Created by dran on 3/8/22.
//
#include <stdio.h>
#include <string.h>
#include "parsing.h"
int test_parse_request_string() {
char request_string[JOB_REQUEST_BUFFER_SIZE + 1];
bool is_get;
char url[MAX_URL_SIZE];
char hostname[MAX_URL_SIZE];
enum http_version version;
bool keep_alive;
int result;
strncpy(request_string, "GET http://none.exist.com/index.html HTTP/1.1\r\n"
"Host: none.exist.com\r\n"
"User-Agent: Wget/1.20.3 (linux-gnu)\r\n"
"Accept: */*\r\n"
"Accept-Language: en-US,en;q=0.5\r\n"
"Accept-Encoding: identity\r\n"
"Connection: keep-alive\r\n"
"Proxy-Connection: Keep-Alive\r\n", JOB_REQUEST_BUFFER_SIZE);
result = parse_request_string(request_string ,&is_get, hostname, url, &version, &keep_alive);
printf("---------case 1---------\n"
"%s\n"
"expected : actual\n"
"return: %d : %d\n"
"GET: %d : %d\n"
"URL: %s : %s\n"
"Host: %s : %s\n"
"version: %d : %d\n"
"keep alive: %d : %d\n"
,request_string, SUCCESS, result,
true, is_get,
"http://none.exist.com/index.html", url,
"none.exist.com", hostname,
DOT_ONE, version,
true, keep_alive);
strncpy(request_string, "GET https://none.exist.com/index.html HTTP/1.1\r\n"
"Host: none.exist.com\r\n"
"User-Agent: Wget/1.20.3 (linux-gnu)\r\n"
"Accept: */*\r\n"
"Accept-Language: en-US,en;q=0.5\r\n"
"Accept-Encoding: identity\r\n"
"Connection: keep-alive\r\n"
"Proxy-Connection: Keep-Alive\r\n", JOB_REQUEST_BUFFER_SIZE);
result = parse_request_string(request_string ,&is_get, hostname, url, &version, &keep_alive);
printf("---------case 2---------\n"
"%s\n"
"expected : actual\n"
"return: %d : %d\n"
"GET: %d : %d\n"
"URL: %s : %s\n"
"Host: %s : %s\n"
"version: %d : %d\n"
"keep alive: %d : %d\n"
,request_string, SUCCESS, result,
-1, is_get,
"?", url,
"?", hostname,
NOT_SUPPORTED, version,
-1, keep_alive);
strncpy(request_string, "GET http://prefetch.job.com/file.jpg HTTP/1.1\r\n"
"Host: prefetch.job.com\r\n"
"Accept: */*\r\n", JOB_REQUEST_BUFFER_SIZE);
result = parse_request_string(request_string ,&is_get, hostname, url, &version, &keep_alive);
printf("---------case 3---------\n"
"%s\n"
"expected : actual\n"
"return: %d : %d\n"
"GET: %d : %d\n"
"URL: %s : %s\n"
"Host: %s : %s\n"
"version: %d : %d\n"
"keep alive: %d : %d\n"
,request_string, SUCCESS, result,
true, is_get,
"http://prefetch.job.com/file.jpg", url,
"prefetch.job.com", hostname,
DOT_ONE, version,
false, keep_alive);
strncpy(request_string, "GET http://lazy.request.no.host/file.jpg HTTP/1.1\r\n", JOB_REQUEST_BUFFER_SIZE);
result = parse_request_string(request_string ,&is_get, hostname, url, &version, &keep_alive);
printf("---------case 4---------\n"
"%s\n"
"expected : actual\n"
"return: %d : %d\n"
"GET: %d : %d\n"
"URL: %s : %s\n"
"Host: %s : %s\n"
"version: %d : %d\n"
"keep alive: %d : %d\n"
,request_string, SUCCESS, result,
true, is_get,
"/file.jpg", url,
"lazy.request.no.host", hostname,
DOT_ONE, version,
false, keep_alive);
strncpy(request_string, "GET http://host.name.funky.port:2684/file.jpg HTTP/1.1\r\n", JOB_REQUEST_BUFFER_SIZE);
result = parse_request_string(request_string ,&is_get, hostname, url, &version, &keep_alive);
printf("---------case 5---------\n"
"%s\n"
"expected : actual\n"
"return: %d : %d\n"
"GET: %d : %d\n"
"URL: %s : %s\n"
"Host: %s : %s\n"
"version: %d : %d\n"
"keep alive: %d : %d\n"
,request_string, SUCCESS, result,
true, is_get,
"/file.jpg", url,
"host.name.funky.port:2684", hostname,
DOT_ONE, version,
false, keep_alive);
strncpy(request_string, "GET http://all.i.have.is.hostname.com/ HTTP/1.1\r\n", JOB_REQUEST_BUFFER_SIZE);
result = parse_request_string(request_string ,&is_get, hostname, url, &version, &keep_alive);
printf("---------case 5---------\n"
"%s\n"
"expected : actual\n"
"return: %d : %d\n"
"GET: %d : %d\n"
"URL: %s : %s\n"
"Host: %s : %s\n"
"version: %d : %d\n"
"keep alive: %d : %d\n"
,request_string, SUCCESS, result,
true, is_get,
"/", url,
"all.i.have.is.hostname.com", hostname,
DOT_ONE, version,
false, keep_alive);
printf("\n\n\n\n");
return 0;
}
int test_parse_response_string() {
char response_string[JOB_REQUEST_BUFFER_SIZE + 1];
int response_header_length;
int response_content_length;
strncpy(response_string,
"HTTP/1.1 200 OK\r\n"
"Date: Sat, 09 Apr 2022 20:22:04 GMT\r\n"
"Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9 PHP/5.4.16\r\n"
"Last-Modified: Wed, 13 Nov 2019 17:15:57 GMT\r\n"
"ETag: \"d14-5973d84996d40\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: 3348\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n"
"<head>\n"
"<link rel=\"shortcut icon\" href=\"graphics/html.gif\" type=\"image/gif\" />\n"
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n"
"<meta name=\"description\" content=\"Website short description.\" />\n"
"<meta name=\"keywords\" content=\"website main keywords\" />\n"
"\t<title>Welcome to SHS</title>\n"
"<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js\"></script>\n"
"<script>\n"
"\t!window.jQuery && document.write('<script src=\"jquery-1.4.3.min.js\"><\\/script>');\n"
"</script>\n"
"<script type=\"text/javascript\" src=\"./fancybox/jquery.mousewh"
, JOB_REQUEST_BUFFER_SIZE);
parse_response_header(response_string, &response_header_length, &response_content_length);
printf("---------case 0---------\n"
"header-length %d/%ld\n"
"content-length %d/%d\n",
response_header_length, strlen("HTTP/1.1 200 OK\r\nDate: Sat, 09 Apr 2022 20:22:04 GMT\r\nServer: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9 PHP/5.4.16\r\nLast-Modified: Wed, 13 Nov 2019 17:15:57 GMT\r\nETag: \"d14-5973d84996d40\"\r\nAccept-Ranges: bytes\r\nContent-Length: 3348\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n"),
response_content_length, 3348);
strncpy(response_string,
"HTTP/1.1 404 Not Found\r\n"
"Date: Sat, 09 Apr 2022 20:14:25 GMT\r\n"
"Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9 PHP/5.4.16\r\n"
"Content-Length: 214\r\n"
"Content-Type: text/html; charset=iso-8859-1\r\n"
"\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
"<html><head>\n"
"<title>404 Not Found</title>\n"
"</head><body>\n"
"<h1>Not Found</h1>\n"
"<p>The requested URL /files/done.exist was not found on this server.</p>\n"
"</body></html>\n"
"\r\n"
, JOB_REQUEST_BUFFER_SIZE);
parse_response_header(response_string, &response_header_length, &response_content_length);
printf("---------case 1---------\n"
"total-length: %d/%d\n"
"header-length %d/%d\n"
"content-length %d/%d\n"
,response_content_length + response_header_length,421,
response_header_length, 421 - 214,
response_content_length, 214);
strncpy(response_string,
"HTTP/1.1 404 Not Found\r\n"
"Date: Sat, 09 Apr 2022 19:36:17 GMT\r\n"
"Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9 PHP/5.4.16\r\n"
, JOB_REQUEST_BUFFER_SIZE);
parse_response_header(response_string, &response_header_length, &response_content_length);
printf("---------case 2---------\n"
"header-length %d/%ld\n"
"content-length %d/%d\n",
response_header_length, strlen(response_string),
response_content_length, -1);
strncpy(response_string,
"Content-Length: 9\r\n\r\n123456789"
, JOB_REQUEST_BUFFER_SIZE);
parse_response_header(response_string, &response_header_length, &response_content_length);
printf("---------case 3---------\n"
"header-length %d/%d\n"
"content-length %d/%d\n",
response_header_length, 25,
response_content_length, 11);
strncpy(response_string,
"HTTP/1.1 200 OK\r\n"
"Date: Sat, 09 Apr 2022 20:09:07 GMT\r\n"
"Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips mod_fcgid/2.3.9 PHP/5.4.16\r\n"
"Last-Modified: Tue, 01 Sep 2015 15:49:52 GMT\r\n"
"ETag: \"29-51eb1802c8800\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: 41\r\n"
"Content-Type: text/plain; charset=UTF-8\r\n"
"\r\n"
"This is a text File. Welcome to My Server"
, JOB_REQUEST_BUFFER_SIZE);
parse_response_header(response_string, &response_header_length, &response_content_length);
printf("---------case 4---------\n"
"total-length: %d/%d\n"
"header-length %d/%d\n"
"content-length %d/%d\n",
response_content_length + response_header_length,330,
response_header_length, 330 - 41,
response_content_length, 41);
printf("\n\n\n\n");
return 0;
}
int test_find_href_from_file() {
int result;
FILE* text_file;
char found_link[MAX_URL_SIZE + 1];
text_file = fopen("../testing/index.html", "r");
while (1) {
result = find_href(text_file, found_link);
if (result == SUCCESS) {
printf("found link: %s\n", found_link);
} else if (result == FINISHED) {
break;
}
}
return 0;
}
int test_find_href_from_binary() {
int result;
FILE* text_file;
char found_link[MAX_URL_SIZE + 1];
text_file = fopen("../testing/foo2.jpg", "r");
while (1) {
result = find_href(text_file, found_link);
if (result == SUCCESS) {
printf("found link: %s\n", found_link);
} else if (result == FINISHED) {
break;
}
}
return 0;
}
int main(int argc, char* argv[]) {
test_find_href_from_file();
test_find_href_from_binary();
test_parse_request_string();
test_parse_response_string();
return 0;
}