Repository navigation
Expand file tree
/
Copy pathparse_request.c
More file actions
169 lines (125 loc) · 3.34 KB
/
Copy pathparse_request.c
File metadata and controls
169 lines (125 loc) · 3.34 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
//
//
//
//
//
//
//
//
//
#include <stdio.h>
#include "parse_request.h"
#include <stdlib.h>
#include <string.h>
void parse_request(char* buffer) {
printf("%s\n", buffer);
Request request;
int position = 0;
init_request(&request);
printf("$$$$$ Parsing Request $$$$$$ \n");
get_method(&request, buffer, &position);
printf("method: %d\n", request.method);
move_to_next_non_space(buffer, &position);
// position++;
// position++;
get_path(&request, buffer, &position);
printf("path_size: %d\n", request.path_size);
printf("paths: \n");
for (int i = 0; i < request.path_size; i++) {
printf("%s\n", request.path[i]);
}
free_paths(&request);
return;
}
void move_to_next_non_space(char* buffer, int* position) {
char curr = buffer[*position];
while (curr == ' ') {
*position = *position + 1;
printf("moved to %d\n", *position);
curr = buffer[*position];
}
printf("buffer[*position]: %c\n", buffer[*position]);
}
void init_request(Request* request) {
request->path_size = 0;
request->path = (char**)malloc(sizeof(char*));
}
void get_method(Request* request, char* buffer, int* position) {
printf("get_method \n");
int sum = 0;
// int i = 0;
while (*buffer != ' ') {
sum += *buffer;
buffer++;
*position = *position + 1;
printf("position %d\n", *position);
}
switch (sum) {
case 556:
request->method = OPTIONS;
break;
case 224:
request->method = GET;
break;
case 274:
request->method = HEAD;
break;
case 326:
request->method = POST;
break;
case 249:
request->method = PUT;
break;
case 'D' + 'E' + 'L' + 'E' + 'T' + 'E':
request->method = DELETE;
break;
case 'T' + 'R' + 'A' + 'C' + 'E':
request->method = TRACE;
break;
case 'C' + 'O' + 'N' + 'N' + 'E' + 'C' + 'T':
request->method = CONNECT;
break;
default:
request->method = INVALID;
}
request_method method = GET;
request->method = method;
return;
}
void get_path(Request* request, char* buffer, int* position) {
if (buffer[*position] == ' ') return;
// char* param = &buffer[*position];
char* head = &buffer[*position];
*position = *position + 1;
int param_length = 0;
while (buffer[*position] != '/' && buffer[*position] != ' ') {
printf("buffer[*position]: %c\n", buffer[*position]);
param_length++;
*position = *position + 1;
}
size_t param_size = (param_length + 1) * sizeof(char);
char* param = (char*)malloc(param_size);
strncpy(param, head, param_size );
printf("buffer[*position]: %c\n", buffer[*position]);
request->path = (char**)realloc(request->path, sizeof(char**) * request->path_size);
(request->path)[request->path_size] = param;
request->path_size++;
printf("ending get_path(): buffer[*position]: %c\n", buffer[*position]);
if (buffer[*position] == '/') {
printf("getting next param!\n");
get_path(request, buffer, position);
}
}
void free_paths(Request* request) {
for (int i = 0; i < request->path_size; i++) {
free(request->path[i]);
}
}
// OPTIONS = 79 + 80 + 84 + 73 + 79 + 78 + 83 = 556
// GET = 71 + 69 + 84 = 224
// HEAD = 72 + 69 + 65 + 68 = 274
// POST = 80 + 79 + 83 + 84 = 326
// PUT = 80 + 85 + 84 = 249
// DELETE = 68 + 69 + 76 + 69 + 84 + 69 = 435
// TRACE = 84 + 82 +
// CONNECT,