-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.c
More file actions
204 lines (162 loc) · 5.67 KB
/
Copy pathhttp.c
File metadata and controls
204 lines (162 loc) · 5.67 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
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
#ifdef __linux__
#include <malloc.h>
#elif __APPLE__
#include <stdlib.h>
#endif
#include "http.h"
#include "utils.h"
#define VERSION "0.1"
static const char *status_codes[] = {
"200",
"404",
"405",
"500"
};
static const char *status_list[] = {
"200 OK",
"404 Not Found",
"405 Method Not Allowed",
"500 Internal Server Error"
};
static const char *status_html = "<html><body><h1>%s</h1></body></html>";
static const char *response_header = "%s %s\r\n"
"Server: twes/"VERSION
"\r\n"
"Date: %s\r\n"
"Content-Length: %ld\r\n"
"Content-Type: %s\r\n\r\n";
static void clean_http_headers(header_t **headers);
static void insert_headers(header_t **headers, char *key, char *value);
static void parse_headers(char *buf, http_request_t *request);
static char *get_header_key(char *str);
static void parse_request_line(char *buf, http_request_t *request);
http_request_t *init_http_request(char *buf, char *path) {
char res[100];
bzero(res, 100);
http_request_t *request;
// create request struct
request = (http_request_t *) malloc(sizeof(http_request_t));
request->file.path = strdup(path);
// parse the request header fields. e.g.Accept-Language: en
parse_headers(buf, request);
// parse the request line. e.g GET / HTTP/1.1\r\n
parse_request_line(buf, request);
if (strcmp(request->resource, "/") == 0)
strcpy(res, "/index.html");
// append the res at the end of the resource
request->file.path = (char *) realloc(request->file.path,
strlen(request->file.path) +
(strlen(res) != 0 ? strlen(res) + 1 : strlen(request->resource)) + 1);
strcat(request->file.path, strlen(res) != 0 ? &res[1] : &request->resource[1]);
return request;
}
void send_http_response(char *buf, int client_socket, http_request_t *request, char *status) {
#define NUM_STATUS (sizeof(status_codes) / sizeof(char *))
char *mime = "text/html";
char *response_data;
long len;
char *st;
// allocate the buffer for the response
response_data = (char *) calloc(100, sizeof(char));
for (int i = 0; i < NUM_STATUS; ++i) {
if (strcmp(status, status_codes[i]) == 0) {
sprintf(response_data, status_html, status_list[i]);
st = (char *) status_list[i];
break;
}
}
#undef NUM_STATUS
#define is_image(mime) \
strcmp(mime,"image/jpeg") == 0 || \
strcmp(mime,"image/gif") == 0 || \
strcmp(mime,"image/png") == 0 || \
strcmp(mime,"image/x-icon") == 0
if (request->file.fd) {
// clean the response buffer
bzero(response_data, 100);
mime = get_mime_type(&request->file.path[1]);
// get the file size
fseek(request->file.fd, 0L, SEEK_END);
len = ftell(request->file.fd);
fseek(request->file.fd, 0L, SEEK_SET);
// reallocate the response buffer for the size of the file
response_data = (char *) realloc(response_data, (size_t) len + 1);
if (!(is_image(mime)))
fread(response_data, 1, (size_t) len, request->file.fd);
} else
len = strlen(response_data);
// create the response header and send
sprintf(buf, response_header, request->protocol, st, get_time(GMT), len, mime);
strcat(buf, response_data);
write(client_socket, buf, strlen(buf));
// images must be sent in chunks
if (request->file.fd && is_image(mime)) {
while (!feof(request->file.fd)) {
fread(response_data, 1, sizeof(response_data), request->file.fd);
write(client_socket, response_data, sizeof(response_data));
bzero(buf, sizeof(response_data));
}
}
free(response_data);
}
#undef is_image
static void parse_request_line(char *buf, http_request_t *request) {
strcpy(request->method, strtok(buf, " "));
strcpy(request->resource, strtok(NULL, " "));
strcpy(request->protocol, strtok(NULL, " "));
}
static void parse_headers(char *buf, http_request_t *request) {
char *found = strtok(buf, "\r\n");
while (found != NULL) {
char *pos = strchr(found, ':');
if (pos) {
char *key = get_header_key(found);
char *value = strdup(&pos[2]);
insert_headers(&request->headers, key, value);
}
found = strtok(NULL, "\r\n");
}
}
static void insert_headers(header_t **headers, char *key, char *value) {
if (*headers == NULL) {
*headers = (header_t *) malloc(sizeof(header_t));
(*headers)->key = key;
(*headers)->value = value;
} else {
insert_headers(&(*headers)->next, key, value);
}
}
static char *get_header_key(char *str) {
int ind = 0;
size_t size = strlen(str);
for (int i = 0; i < strlen(str); ++i) {
if (str[i] == ':') {
ind = i;
break;
}
}
char key[size];
bzero(key, size);
strncpy(key, str, (size_t) ind);
return strdup(key);
}
void clean_http_request(http_request_t *request) {
clean_http_headers(&request->headers);
free(request->file.path);
if (request->file.fd)
fclose(request->file.fd);
free(request);
request = NULL;
}
static void clean_http_headers(header_t **headers) {
if (*headers == NULL)
return;
clean_http_headers(&(*headers)->next);
free((*headers)->key);
free((*headers)->value);
free(*headers);
*headers = NULL;
}