-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathwn_module.c
More file actions
592 lines (503 loc) · 15.7 KB
/
wn_module.c
File metadata and controls
592 lines (503 loc) · 15.7 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
* File : wn_module.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This software is dual-licensed: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation. For the terms of this
* license, see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* Alternatively for commercial application, you can contact us
* by email <business@rt-thread.com> for commercial license.
*
* Change Logs:
* Date Author Notes
* 2011-08-02 Bernard the first version
* 2012-06-25 Bernard add SSI and Upload module
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <webnet.h>
#include <wn_module.h>
#include <wn_utils.h>
#ifdef RT_USING_DFS
#if RT_VER_NUM >= 0x40100
#include <dfs.h>
#include <unistd.h>
#else
#include <dfs_posix.h>
#endif /*RT_VER_NUM >= 0x40100*/
#endif
static int _webnet_module_system_init(struct webnet_session *session, int event)
{
#ifdef WEBNET_USING_LOG
webnet_module_log(session, event);
#endif
#ifdef WEBNET_USING_SSL
webnet_module_ssl(session, event);
#endif
#ifdef WEBNET_USING_CGI
webnet_module_cgi(session, event);
#endif
#ifdef WEBNET_USING_DMR
webnet_module_dmr(session, event);
#endif
return WEBNET_MODULE_CONTINUE;
}
static int _webnet_module_system_uri_physical(struct webnet_session *session, int event)
{
int result;
result = WEBNET_MODULE_CONTINUE;
#ifdef WEBNET_USING_LOG
webnet_module_log(session, event);
#endif
#ifdef WEBNET_USING_ALIAS
result = webnet_module_alias(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_AUTH
result = webnet_module_auth(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_CGI
result = webnet_module_cgi(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_DMR
result = webnet_module_dmr(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_UPLOAD
result = webnet_module_upload(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
return result;
}
static void _webnet_dofile_handle(struct webnet_session *session, int event)
{
int fd = session->user_data;
if (event & WEBNET_EVENT_WRITE)
{
rt_size_t readbytes;
rt_size_t length = RT_ALIGN_DOWN(WEBNET_SESSION_BUFSZ, 4);
#ifdef WEBNET_USING_RANGE
if(session->request->Range)
{
length = session->request->pos_end - session->request->pos_start;
if(length == 0)
{
goto __exit;
}
if(length > WEBNET_SESSION_BUFSZ)
{
length = WEBNET_SESSION_BUFSZ;
}
lseek(fd, session->request->pos_start, SEEK_SET);
session->request->pos_start += length;
}
#endif
readbytes = read(fd, session->buffer, length);
if (readbytes <= 0) /* end of file */
goto __exit;
if (webnet_session_write(session, session->buffer, readbytes) == 0)
goto __exit;
return;
}
__exit:
close(fd);
session->user_data = 0;
session->session_event_mask = 0; /* clean event */
/* destroy session */
session->session_phase = WEB_PHASE_CLOSE;
return;
}
static const struct webnet_session_ops _dofile_ops =
{
_webnet_dofile_handle,
RT_NULL
};
/* send a file to http client */
int webnet_module_system_dofile(struct webnet_session *session)
{
int fd = -1; /* file descriptor */
struct stat file_stat;
const char *mimetype;
rt_size_t file_length;
struct webnet_request *request;
#if WEBNET_CACHE_LEVEL > 0
char ctime_str[32];
char gmtime_str[32];
struct tm* info;
int stat_result = -1;
#endif /* WEBNET_CACHE_LEVEL */
RT_ASSERT(session != RT_NULL);
request = session->request;
RT_ASSERT(request != RT_NULL);
#if WEBNET_CACHE_LEVEL > 0
#ifdef WEBNET_USING_GZIP
/* get .gz Last-Modified. */
if (request->support_gzip)
{
struct stat file_stat;
char *path_gz = wn_malloc(strlen(request->path) + 4); /* ".gz\0" */
if (path_gz != RT_NULL)
{
rt_sprintf(path_gz, "%s.gz", request->path);
stat_result = stat(request->path, &file_stat);
wn_free(path_gz);
}
if (stat_result == 0)
{
rt_enter_critical();
strcpy(ctime_str, ctime((time_t *)&file_stat.st_mtime));
rt_exit_critical();
ctime_str[strlen(ctime_str) - 1] = '\0'; /* clear the end \n */
if ((request->modified != RT_NULL)
&& (strcmp(request->modified, ctime_str) == 0))
{
request->result_code = 304;
return WEBNET_MODULE_FINISHED;
}
}
}
/* .gz not exist, use raw. */
#endif /* WEBNET_USING_GZIP */
/* get Last-Modified. */
if (stat_result != 0)
{
struct stat file_stat;
stat_result = stat(request->path, &file_stat);
if (stat_result == 0)
{
rt_enter_critical();
info = localtime((time_t *)&file_stat.st_mtime);
rt_memset(gmtime_str,0,32);
strftime(gmtime_str,sizeof(ctime_str),"%a, %d %b %Y %H:%M:%S GMT",info);
strcpy(ctime_str, ctime((time_t *)&file_stat.st_mtime));
rt_exit_critical();
ctime_str[strlen(ctime_str) - 1] = '\0'; /* clear the end \n */
gmtime_str[strlen(gmtime_str)] = '\0'; /* clear the end \n */
if ((request->modified != RT_NULL)
&& ((strcmp(request->modified, ctime_str) == 0)||strcmp(request->modified, gmtime_str) == 0))
{
request->result_code = 304;
return WEBNET_MODULE_FINISHED;
}
}
}
#endif /* WEBNET_CACHE_LEVEL > 0 */
/* get mime type */
mimetype = mime_get_type(request->path);
#ifdef WEBNET_USING_GZIP
if (request->support_gzip)
{
char *path_gz = wn_malloc(strlen(request->path) + 4); /* ".gz\0" */
if (path_gz != RT_NULL)
{
rt_sprintf(path_gz, "%s.gz", request->path);
fd = open(path_gz, O_RDONLY, 0);
wn_free(path_gz);
if (fd < 0)
{
/* .gz not exist, use raw. */
request->support_gzip = RT_FALSE;
}
}
}
/* .gz not exist, use raw. */
#endif /* WEBNET_USING_GZIP */
if (fd < 0 && stat(request->path, &file_stat) >= 0 && !S_ISDIR(file_stat.st_mode))
{
fd = open(request->path, O_RDONLY, 0);
}
if (fd < 0)
{
request->result_code = 404;
return WEBNET_MODULE_FINISHED;
}
/* get file size */
file_length = lseek(fd, 0, SEEK_END);
/* seek to beginning of file */
lseek(fd, 0, SEEK_SET);
/*************todo**********************/
#ifdef WEBNET_USING_RANGE
if (request->Range)
{
char *range_start, *range_end;
int32_t pos_start = 0;
uint32_t pos_end = file_length - 1;
range_start = strstr(request->Range, "bytes=");
if (range_start)
{
range_start += 6;
range_end = strstr(range_start, "-");
if (range_start == range_end)
pos_start = 0;
else
pos_start = atoi(range_start);
/* send file to remote */
if ((!range_end) || (strstr(range_start, ",")))
{
request->result_code = 400;
goto _error_exit;
}
if (range_end)
{
*range_end = '\0';
range_end += 1;
pos_end = atoi(range_end);
}
}
#ifdef WEBNET_USING_GZIP
if (request->support_gzip)
{
pos_start = 0; /* */
}
#endif /* WEBNET_USING_GZIP */
if ((pos_start >= file_length) || (pos_end >= file_length))
{
request->result_code = 416;
webnet_session_set_header_status_line(session, request->result_code, "Requested Range Not Satisfiable");
goto _error_exit;
}
if (lseek(fd, pos_start, SEEK_SET) != pos_start)
{
request->result_code = 500;
goto _error_exit;
}
if (pos_end == 0)
{
pos_end = file_length - 1;
}
file_length = pos_end - pos_start + 1;
request->result_code = 216;
request->pos_start = pos_start;
request->pos_end = pos_end;
webnet_session_set_header_status_line(session, request->result_code, "Partial Content");
webnet_session_printf(session, "Content-Range: %d-%d/%d\r\n", pos_start, pos_end, file_length);
}else
#endif /* WEBNET_USING_RANGE */
/*************todo**********************/
{
/* send file to remote */
request->result_code = 200;
webnet_session_set_header_status_line(session, request->result_code, "OK");
}
#if WEBNET_CACHE_LEVEL > 0
/* send Last-Modified. */
webnet_session_printf(session,
"Last-Modified: %s\r\n",
ctime_str);
#endif /* WEBNET_CACHE_LEVEL > 0 */
#if WEBNET_CACHE_LEVEL > 1
/* Cache-Control. */
webnet_session_printf(session,
"Cache-Control: max-age=%d\r\n",
WEBNET_CACHE_MAX_AGE);
#endif /* WEBNET_CACHE_LEVEL > 1 */
/* send Content-Type. */
webnet_session_printf(session,
"Content-Type: %s\r\n",
mimetype);
/* send Content-Length. */
webnet_session_printf(session,
"Content-Length: %ld\r\n",
file_length);
#ifdef WEBNET_USING_KEEPALIVE
if(session->request->connection == WEBNET_CONN_KEEPALIVE)
{
webnet_session_printf(session,
"Connection: %s\r\n",
"Keep-Alive");
}
else
{
webnet_session_printf(session,
"Connection: %s\r\n",
"close");
}
#else
webnet_session_printf(session,
"Connection: %s\r\n",
"close");
#endif
#ifdef WEBNET_USING_GZIP
if (request->support_gzip)
{
/* gzip deflate. */
webnet_session_printf(session, "Content-Encoding: gzip\r\n");
}
#endif /* WEBNET_USING_GZIP */
/* send Access-Control-Allow-Origin. */
webnet_session_printf(session, "Access-Control-Allow-Origin:*\r\n");
/* send http header end. */
webnet_session_printf(session, "\r\n");
if (file_length <= 0)
{
close(fd);
return WEBNET_MODULE_FINISHED;
}
/*
* set session write context
*/
if (request->method != WEBNET_HEADER)
{
/* set dofile session ops */
session->session_event_mask = WEBNET_EVENT_WRITE;
session->user_data = (rt_uint32_t)fd;
session->session_ops = &_dofile_ops;
}
return WEBNET_MODULE_FINISHED;
_error_exit:
if (fd >= 0)
{
close(fd);
}
return WEBNET_MODULE_FINISHED;
}
static int _webnet_module_system_uri_post(struct webnet_session *session, int event)
{
int result;
result = WEBNET_MODULE_CONTINUE;
#ifdef WEBNET_USING_LOG
webnet_module_log(session, event);
#endif
#ifdef WEBNET_USING_LUA
result = webnet_module_lua(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_ASP
result = webnet_module_asp(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_SSI
result = webnet_module_ssi(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_DAV
result = webnet_module_dav(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
#ifdef WEBNET_USING_INDEX
result = webnet_module_dirindex(session, event);
if (result == WEBNET_MODULE_FINISHED) return result;
#endif
/* always module finished in dofile */
result = webnet_module_system_dofile(session);
if (result == WEBNET_MODULE_FINISHED) return result;
return WEBNET_MODULE_CONTINUE;
}
static int _webnet_module_system_response_header(struct webnet_session *session, int event)
{
int result;
result = WEBNET_MODULE_CONTINUE;
return result;
}
static int _webnet_module_system_response_file(struct webnet_session *session, int event)
{
int result;
result = WEBNET_MODULE_CONTINUE;
return result;
}
int webnet_module_handle_event(struct webnet_session *session, int event)
{
switch (event)
{
case WEBNET_EVENT_INIT:
return _webnet_module_system_init(session, event);
case WEBNET_EVENT_URI_PHYSICAL:
return _webnet_module_system_uri_physical(session, event);
case WEBNET_EVENT_URI_POST:
return _webnet_module_system_uri_post(session, event);
case WEBNET_EVENT_RSP_HEADER:
return _webnet_module_system_response_header(session, event);
case WEBNET_EVENT_RSP_FILE:
return _webnet_module_system_response_file(session, event);
default:
RT_ASSERT(0);
break;
}
return WEBNET_MODULE_CONTINUE;
}
/* default index file */
static const char *default_files[] =
{
"",
"/index.html",
"/index.htm",
RT_NULL
};
/**
* handle uri
* there are two phases on uri handling:
* - map url to physical
* - url handling
*/
int webnet_module_handle_uri(struct webnet_session *session)
{
int result;
char *full_path;
rt_uint32_t index;
struct webnet_request *request;
RT_ASSERT(session != RT_NULL);
/* get request */
request = session->request;
RT_ASSERT(request != RT_NULL);
/* map uri to physical */
result = webnet_module_handle_event(session, WEBNET_EVENT_URI_PHYSICAL);
if (result == WEBNET_MODULE_FINISHED) return result;
/* made a full physical path */
full_path = (char *) wn_malloc(WEBNET_PATH_MAX);
RT_ASSERT(full_path != RT_NULL);
/* only GET or POST need try default page. */
if ((session->request->method != WEBNET_GET)
&& (session->request->method != WEBNET_POST))
{
index = sizeof(default_files) / sizeof(default_files[0]);
index -= 1;
goto _end_default_files;
}
index = 0;
while (default_files[index] != RT_NULL)
{
struct stat file_stat;
/* made a full path */
rt_snprintf(full_path, WEBNET_PATH_MAX, "%s/%s%s",
webnet_get_root(), request->path, default_files[index]);
/* normalize path */
str_normalize_path(full_path);
if (stat(full_path, &file_stat) >= 0 && !S_ISDIR(file_stat.st_mode))
{
break;
}
index ++;
}
_end_default_files:
/* no this file */
if (default_files[index] == RT_NULL)
{
/* use old full path */
rt_snprintf(full_path, WEBNET_PATH_MAX, "%s/%s", webnet_get_root(), request->path);
/* normalize path */
str_normalize_path(full_path);
}
/* mark path as full physical path */
wn_free(request->path);
request->path = full_path;
/* check uri valid */
if (!str_begin_with(request->path, webnet_get_root()))
{
/* not found */
request->result_code = 404;
return WEBNET_MODULE_FINISHED;
}
/* uri post handle */
return webnet_module_handle_event(session, WEBNET_EVENT_URI_POST);
}