1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
+
|
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <pthread.h>
#include <strings.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
|
︙ | | |
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
+
+
+
+
-
-
+
+
+
+
|
/** Buffers **/
struct filed_fileinfo fileinfo;
char path_b[1010];
char tmpbuf[1010];
/** HTTP Request information **/
char *path; /*** Path being requested ***/
struct {
struct {
int present;
off_t offset; /*** Range start ***/
ssize_t length; /*** Range length ***/
off_t offset; /*** Range start ***/
ssize_t length; /*** Range length ***/
} range;
} headers;
};
/* Global variables */
struct filed_fileinfo *filed_fileinfo_fdcache;
unsigned int filed_fileinfo_fdcache_size = CACHE_SIZE;
/* Initialize process */
|
︙ | | |
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
+
+
|
}
filed_fileinfo_fdcache[idx].path = strdup("");
filed_fileinfo_fdcache[idx].fd = -1;
filed_fileinfo_fdcache[idx].lastmod = "";
filed_fileinfo_fdcache[idx].type = "";
}
signal(SIGPIPE, SIG_IGN);
return(0);
}
/* Listen on a particular address/port */
static int filed_listen(const char *address, unsigned int port) {
struct sockaddr_in6 addr;
|
︙ | | |
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
-
+
|
lseek(fd, 0, SEEK_SET);
cache->fd = fd;
cache->len = len;
cache->path = strdup(path);
/* XXX:TODO: Determine */
cache->type = "application/octet-stream";
cache->type = "video/mp4";
cache->lastmod = filed_format_time(cache->lastmod_b, sizeof(cache->lastmod_b), time(NULL) - 30);
} else {
filed_log_msg_debug("Cache hit for idx: %lu: PATH \"%s\"", (unsigned long) cache_idx, path);
}
/*
* We have to make a duplicate FD, because once we release the cache
|
︙ | | |
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
+
+
|
/* Process an HTTP request and return the path requested */
static struct filed_http_request *filed_get_http_request(FILE *fp, struct filed_http_request *buffer_st) {
char *method, *path;
char *buffer, *tmpbuffer, *workbuffer, *workbuffer_next;
size_t buffer_len, tmpbuffer_len;
off_t range_start, range_end;
ssize_t range_length;
int range_request;
int i;
range_start = 0;
range_end = 0;
range_request = 0;
range_length = -1;
buffer = buffer_st->path_b;
buffer_len = sizeof(buffer_st->path_b);
tmpbuffer = buffer_st->tmpbuf;
tmpbuffer_len = sizeof(buffer_st->tmpbuf);
|
︙ | | |
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
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
|
+
+
-
+
-
+
|
if (strncasecmp(tmpbuffer, "Range: ", 7) == 0) {
workbuffer = tmpbuffer + 7;
if (strncasecmp(workbuffer, "bytes=", 6) == 0) {
workbuffer += 6;
range_request = 1;
range_start = strtoul(workbuffer, &workbuffer_next, 10);
range_start = strtoull(workbuffer, &workbuffer_next, 10);
workbuffer = workbuffer_next;
if (*workbuffer == '-') {
workbuffer++;
if (*workbuffer != '\r' && *workbuffer != '\n') {
range_end = strtoul(workbuffer, &workbuffer_next, 10);
range_end = strtoull(workbuffer, &workbuffer_next, 10);
}
}
}
}
if (memcmp(tmpbuffer, "\r\n", 2) == 0) {
break;
|
︙ | | |
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
+
-
-
+
+
|
}
range_length = range_end - range_start;
}
/* Fill up structure to return */
buffer_st->path = path;
buffer_st->headers.range.present = range_request;
buffer_st->offset = range_start;
buffer_st->length = range_length;
buffer_st->headers.range.offset = range_start;
buffer_st->headers.range.length = range_length;
return(buffer_st);
}
/* Return an error page */
static void filed_error_page(FILE *fp, const char *date_current, int error_number) {
char *error_string = "<html><head><title>ERROR</title></head><body>Unable to process request</body></html>";
|
︙ | | |
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
|
+
+
|
if (fp == NULL) {
close(fd);
return;
}
request = filed_get_http_request(fp, request);
fflush(fp);
path = request->path;
filed_log_msg("PROCESS_REPLY_START FD=... PATH=... RANGE_START=... RANGE_LENGTH=...");
if (path == NULL) {
filed_error_page(fp, date_current, 500);
|
︙ | | |
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
|
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
|
-
-
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
+
+
+
+
+
+
+
-
+
-
-
+
+
|
fileinfo = filed_open_file(path, &request->fileinfo);
if (fileinfo == NULL) {
filed_error_page(fp, date_current, 404);
filed_log_msg("PROCESS_REPLY_COMPLETE FD=... ERROR=404");
} else {
if (request->offset != 0 || request->length >= 0) {
if ((size_t) request->offset >= fileinfo->len) {
if (request->headers.range.offset != 0 || request->headers.range.length >= 0) {
if ((size_t) request->headers.range.offset >= fileinfo->len) {
filed_log_msg("PROCESS_REPLY_COMPLETE FD=... ERROR=416");
filed_error_page(fp, date_current, 416);
}
if (request->length < 0) {
request->length = fileinfo->len - request->offset;
}
} else {
if (request->headers.range.length < 0) {
request->headers.range.length = fileinfo->len - request->headers.range.offset;
}
filed_log_msg_debug("Partial request, starting at: %llu and running for %llu bytes", (unsigned long long) request->headers.range.offset, (unsigned long long) request->headers.range.length);
filed_log_msg_debug("Partial request, starting at: %lu and running for %lu bytes", (unsigned long) request->offset, (unsigned long) request->length);
http_code = 216;
} else {
http_code = 200;
request->offset = 0;
request->length = fileinfo->len;
http_code = 206;
}
} else {
if (request->headers.range.present) {
http_code = 206;
} else {
http_code = 200;
}
request->headers.range.offset = 0;
request->headers.range.length = fileinfo->len;
}
if (http_code > 0) {
fprintf(fp, "HTTP/1.1 %i OK\r\nDate: %s\r\nServer: filed\r\nLast-Modified: %s\r\nContent-Length: %llu\r\nContent-Range: %llu-%llu/%llu\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
fprintf(fp, "HTTP/1.1 %i OK\r\nDate: %s\r\nServer: filed\r\nLast-Modified: %s\r\nContent-Length: %llu\r\nAccept-Ranges: bytes\r\nContent-Type: %s\r\nConnection: close\r\n",
http_code,
date_current,
fileinfo->lastmod,
(unsigned long long) request->length,
(unsigned long long) request->headers.range.length,
(unsigned long long) request->offset, (unsigned long long) (request->offset + request->length - 1), (unsigned long long) fileinfo->len,
fileinfo->type
);
if (http_code == 206) {
fprintf(fp, "Content-Range: %llu-%llu/%llu\r\n",
(unsigned long long) request->headers.range.offset,
(unsigned long long) (request->headers.range.offset + request->headers.range.length - 1),
(unsigned long long) fileinfo->len
);
}
fprintf(fp, "\r\n");
fflush(fp);
filed_log_msg("PROCESS_REPLY_COMPLETE FD=... STATUS=200");
filed_log_msg("PROCESS_REPLY_COMPLETE FD=... STATUS=20X");
filed_log_msg("SEND_START IFD=... OFD=... BYTES=...");
sendfile_offset = request->offset;
sendfile_len = request->length;
sendfile_offset = request->headers.range.offset;
sendfile_len = request->headers.range.length;
while (1) {
sendfile_ret = sendfile(fd, fileinfo->fd, &sendfile_offset, sendfile_len);
if (sendfile_ret <= 0) {
break;
}
sendfile_len -= sendfile_ret;
|
︙ | | |