-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.c
More file actions
158 lines (128 loc) · 4.04 KB
/
Copy pathrequest.c
File metadata and controls
158 lines (128 loc) · 4.04 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
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <sys/socket.h>
#include "macros.h"
#include "request.h"
#include "debug.h"
int init_http_request(int conn_fd, struct http_request_t *request)
{
int err = 0;
memset(request, 0, sizeof(struct http_request_t));
char buffer[MAX_HTTP_REQUEST_LENGTH];
memset(&buffer, 0, MAX_HTTP_REQUEST_LENGTH);
err = recv(conn_fd, buffer, MAX_HTTP_REQUEST_LENGTH, 0);
return_error(err, "recv");
// debug("Request", "%s", buffer);
// printf("\n%s\n", buffer);
char line_buffer[MAX_HTTP_HEADER_LENGTH];
int line = 0;
int header_start = 0, header_end = 0;
while (header_end < MAX_HTTP_REQUEST_LENGTH)
{
if (buffer[header_end] == '\n')
{
memset(line_buffer, 0, MAX_HTTP_HEADER_LENGTH);
memcpy(&line_buffer, &buffer[header_start], header_end - header_start);
if (line == 0)
{
read_http_request_starting_line(request, line_buffer, header_end - header_start);
}
else
{
add_http_request_header(request, line_buffer, header_end - header_start);
}
if (header_end == header_start + 1)
{
int body_len = get_content_length(request);
if (body_len > 0)
{
request->body_length = body_len;
char *body_ptr = malloc(sizeof(char) * request->body_length);
memcpy(body_ptr, &buffer[header_end + 1], request->body_length);
request->body = body_ptr;
printf("> Body:\n");
printf("%s\n", request->body);
printf("> BODY END\n");
}
}
line++;
header_start = header_end + 1;
}
header_end++;
}
return err;
}
int destroy_http_request(struct http_request_t *request)
{
free(request->body);
return 0;
}
int read_http_request_starting_line(struct http_request_t *request, char *line_buffer, int line_length)
{
int err = 0;
// read line data
int i_start = 0,
i_end = 0;
int word_c = 0;
char word_buffer[MAX_HTTP_HEADER_LENGTH];
while (i_end < line_length)
{
if (line_buffer[i_end] == ' ')
{
memset(&word_buffer, 0, MAX_HTTP_HEADER_LENGTH);
memcpy(&word_buffer, &line_buffer[i_start], i_end - i_start);
switch (word_c)
{
case 0:
request->method = method_to_code(word_buffer);
break;
case 1:
memcpy(&request->address, &line_buffer[i_start], i_end - i_start);
case 2:
request->protocol = get_protocol_code(word_buffer);
break;
}
i_start = i_end + 1;
i_end++;
word_c++;
}
i_end++;
}
printf("> %s %s %d\n", code_to_method(request->method), request->address, request->protocol);
return err;
}
int add_http_request_header(struct http_request_t *request, char *line_buffer, int line_length)
{
int err = 0;
const struct http_header_t *header_ptr = &request->headers[request->header_count];
// read header data
int i_start = 0,
i_end = 0;
while (i_end < line_length)
{
if (line_buffer[i_end] == ':')
{
memcpy((char *)header_ptr->key, &line_buffer[0], i_end);
memcpy((char *)header_ptr->value, &line_buffer[i_end + 2], line_length - i_end + 1);
printf("> %s=%s\n", header_ptr->key, header_ptr->value);
request->header_count++;
break;
}
i_end++;
}
return err;
}
int get_content_length(struct http_request_t *request)
{
for (int i = 0; i < request->header_count; ++i)
{
struct http_header_t *h = &request->headers[i];
if (strcmp(h->key, "Content-Length") == 0)
{
int result = atoi(h->value);
return result;
}
}
return -1;
}