Liso(2) httpServer

文章目录
  1. 1. Log
  2. 2. parse
  3. 3. response
  4. 4. 参考资料

Log

增加 Log 功能方便调试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef LOG_H
#define LOG_H

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

// 整个项目的全局变量
char *logfile; // 文件名
FILE *fp; // 文件 handler

int init_log();
void dump_log(const char *fmt, ...);
void close_log();

#endif

实现比较简单,就是往全局文件里 dump log 字符串。

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
// 初始化日志文件
int init_log()
{
fp = fopen(logfile, "a");
if (fp == NULL)
{
printf("[ERROR] log file can not be created");
return -1;
}
return 0;
}

// 写入日志文件
void dump_log(const char *fmt, ...)
{
time_t tmp = time(NULL);
struct tm* cur_time = localtime(&tmp);
if (!cur_time)
{
return;
}
char arg_buffer[LOG_BUFFER_SIZE];
memset(arg_buffer, '\0', LOG_BUFFER_SIZE);
strftime(arg_buffer, LOG_BUFFER_SIZE - 1, "[ %x %X ] ", cur_time);

fwrite(arg_buffer, sizeof(arg_buffer), 1, fp);

va_list(args);
va_start(args, fmt);
vfprintf(fp, fmt, args);
va_end(args);
fprintf(fp, "\n");
fflush(fp);
}

// 关闭日志文件
void close_log()
{
fclose(fp);
}

parse

解析 http 协议采用 flex 和 bison 实现

response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef __RESPONSE__H
#define __RESPONSE__H

#include "../parse/parse.h"
#include "../client_pool/client_pool.h"
#include "../io/io.h"

int handle_http_request(int clientfd, char* buf, int nbytes);

int response_get(int clientfd, Request* request);
int response_head(int clientfd, Request* request);
int response_post(int clientfd, Request* request);

void get_mime_type(const char *mime, char *type);
void get_header_value(Request *request, const char *hname, char *hvalue);

void response404(int clientfd, char *response);
void response501(int clientfd);
void response505(int clientfd);
void response400(int clientfd);


#endif
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "response.h"
#include <string.h>

char *WWW;

const char* server_name = "liso/1.0";
const char* default_index_file = "index.html";
const char* http_version = "HTTP/1.1";

// 处理http请求
int handle_http_request(int clientfd, char* buf, int nbytes)
{
Request *request = parse(buf, nbytes, clientfd);
// 错误的请求
if (request == NULL) {
response400(clientfd);
}

// 不支持的HTTP版本
if(strcmp(http_version, request->http_version))
{
response505(clientfd);
}


if(!strcmp(request->http_method, "POST"))
{
response_post(clientfd, request);
}
else if(!strcmp(request->http_method, "GET"))
{
response_get(clientfd, request);
}
else if(!strcmp(request->http_method, "HEAD"))
{
response_head(clientfd, request);
}
else
{
response501(clientfd);
}

return 0;
}

int response_get(int clientfd, Request* request)
{
char response[SIZE];
char fullpath[1024];
char extension[16];
char mime_type[64];
char curr_time[256];
char last_modified[256];
size_t content_length;
char content_len_str[16];
char connection_header_val[32];

strcpy(fullpath, WWW);
strcat(fullpath, request->http_uri);

if (is_dir(fullpath))
strcat(fullpath, default_index_file);

// 如果没有那个文件就返回404
if (access(fullpath, F_OK) < 0)
{
dump_log("[response] request file not found\n");
response404(clientfd, response);
return 0;
}

// 得到 content type
get_extension(fullpath, extension);
get_mime_type(extension, mime_type);

// 得到 content length
content_length = get_file_len(fullpath);
sprintf(content_len_str, "%zu", content_length);

// 得到当前时间
get_curr_time(curr_time, 256);

// 得到最后修改时间
get_flmodified(fullpath, last_modified, 256);

// 构造 response
strcat(response, http_version);
strcat(response, " 200 OK\r\n");

strcat(response, "Server: ");
strcat(response, server_name);
strcat(response, "\r\n");

strcat(response, "Date: ");
strcat(response, curr_time);
strcat(response, "\r\n");

strcat(response, "Content-Length: ");
strcat(response, content_len_str);
strcat(response, "\r\n");

strcat(response, "Content-type: ");
strcat(response, mime_type);
strcat(response, "\r\n");

strcat(response, "Last-modified: ");
strcat(response, last_modified);
strcat(response, "\r\n");

// 检查connect是什么值
memset(connection_header_val, 0, sizeof(connection_header_val));
get_header_value(request, "Connection", connection_header_val);
if (!strcmp(connection_header_val, "close"))
{
strcat(response, "Connection: close\r\n");
}
else
{
strcat(response, "Connection: keep-alive\r\n");
}

strcat(response, "\r\n");

// printf("[response] response = \n%s\n", response);
// printf("[response] fullpath = %s\n", fullpath);

Sendn(clientfd, response, strlen(response));

send_file_to_client(clientfd, fullpath);

return 0;
}

int response_head(int clientfd, Request *request)
{
char response[SIZE];
char fullpath[1024];
char extension[16];
char mime_type[64];
char curr_time[256];
char last_modified[256];
size_t content_length;
char content_len_str[16];
char connection_header_val[32];

strcpy(fullpath, WWW);
strcat(fullpath, request->http_uri);

if (is_dir(fullpath))
strcat(fullpath, default_index_file);

// 如果没有那个文件就返回404
if (access(fullpath, F_OK) < 0)
{
dump_log("[response] request file not found\n");
response404(clientfd, response);
return 0;
}

// 得到 content type
get_extension(fullpath, extension);
get_mime_type(extension, mime_type);

// 得到 content length
content_length = get_file_len(fullpath);
sprintf(content_len_str, "%zu", content_length);

// 得到当前时间
get_curr_time(curr_time, 256);

// 得到最后修改时间
get_flmodified(fullpath, last_modified, 256);

// 构造 response
strcat(response, http_version);
strcat(response, " 200 OK\r\n");

strcat(response, "Server: ");
strcat(response, server_name);
strcat(response, "\r\n");

strcat(response, "Date: ");
strcat(response, curr_time);
strcat(response, "\r\n");

strcat(response, "Content-Length: ");
strcat(response, content_len_str);
strcat(response, "\r\n");

strcat(response, "Content-type: ");
strcat(response, mime_type);
strcat(response, "\r\n");

strcat(response, "Last-modified: ");
strcat(response, last_modified);
strcat(response, "\r\n");

// 检查connect是什么值
memset(connection_header_val, 0, sizeof(connection_header_val));
get_header_value(request, "Connection", connection_header_val);
if (!strcmp(connection_header_val, "close"))
{
strcat(response, "Connection: close\r\n");
}
else
{
strcat(response, "Connection: keep-alive\r\n");
}

strcat(response, "\r\n");

Sendn(clientfd, response, strlen(response));

return 0;
}

int response_post(int clientfd, Request *request)
{
char response[SIZE];
char content_length[32];

memset(content_length, 0, sizeof(content_length));
get_header_value(request, "Content-Length", content_length);
if (strlen(content_length) == 0)
{
strcat(response, http_version);
strcat(response, " 401 Length Reqired\r\n");
strcat(response, "\r\n");

Sendn(clientfd, response, strlen(response));
return 0;
}

strcat(response, http_version);
strcat(response, " 200 OK\r\n");
strcat(response, "\r\n");

Sendn(clientfd, response, strlen(response));
return 0;
}


// 根据扩展名确定MIME类型
void get_mime_type(const char *mime, char *type)
{
if (!strcmp(mime, "html"))
{
strcpy(type, "text/html");
}
else if (!strcmp(mime, "css"))
{
strcpy(type, "text/css");
}
else if (!strcmp(mime, "png"))
{
strcpy(type, "image/png");
}
else if (!strcmp(mime, "jpeg"))
{
strcpy(type, "image/jpeg");
}
else if (!strcmp(mime, "gif"))
{
strcpy(type, "image/gif");
}
else
{
strcpy(type, "application/octet-stream");
}
}

// 根据给定键,设置值
// 如果不存在就不设置hvalue
void get_header_value(Request *request, const char *hname, char *hvalue)
{
for (int i = 0; i < request->header_count; i++)
{
if (!strcmp(request->headers[i].header_name, hname))
{
strcpy(hvalue, request->headers[i].header_value);
return;
}
}
}

// 没有请求的指定文件,返回404
void response404(int clientfd, char *response)
{
strcat(response, http_version);
strcat(response, " 404 Not Found\r\n\r\n");
printf("[response] response = \n%s\n", response);
Sendn(clientfd, response, strlen(response));
}

// 未实现的请求功能,返回501
void response501(int clientfd)
{
char response[1024];
strcat(response, http_version);
strcat(response, " 501 Not Implemented\r\n\r\n");
// printf("[response] response = \n%s\n", response);
Sendn(clientfd, response, strlen(response));
}

// 不支持的HTTP版本
void response505(int clientfd)
{
char response[1024];
strcat(response, http_version);
strcat(response, " 505 HTTP Version not supported\r\n");
strcat(response, "Connection: close\r\n");
// printf("[response] response = \n%s\n", response);
Sendn(clientfd, response, strlen(response));
}


void response400(int clientfd)
{
char response[1024];
strcat(response, http_version);
strcat(response, " 400 Bad request\r\n");
// printf("[response] response = \n%s\n", response);
Sendn(clientfd, response, strlen(response));
}

参考资料