Diff

Differences From Artifact [5335f1802e]:

To Artifact [1d3989ed81]:


20
21
22
23
24
25
26

27
28
29
30
31
32
33
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34







+







#include <pwd.h>

/* Compile time constants */
#define FILED_VERSION "1.1"
#define FILED_SENDFILE_MAX 16777215
#define FILED_MAX_FAILURE_COUNT 30
#define FILED_DEFAULT_TYPE "application/octet-stream"
#define FILED_PATH_BUFFER_SIZE 1010

/* Default values */
#define PORT 80
#define THREAD_COUNT 5
#define BIND_ADDR "::"
#define CACHE_SIZE 8209
#define LOG_FILE "-"
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
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







-
+
















+
+
+
+
+
+
+


+
+

+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+







	const char *type;
};

/* Request variables */
struct filed_http_request {
	/** Buffers **/
	struct filed_fileinfo fileinfo;
	char path_b[1010];
	char path_b[FILED_PATH_BUFFER_SIZE];
	char tmpbuf[1010];

	/** HTTP Request information **/
	char *path;     /*** Path being requested ***/

	struct {
		struct {
			int present;
			off_t offset;   /*** Range start ***/
			off_t length;   /*** Range length ***/
		} range;
	} headers;
};

/* Log record */
struct filed_log_entry {
	/* Type of log entry */
	enum {
		FILED_LOG_TYPE_MESSAGE,
		FILED_LOG_TYPE_TRANSFER
	} type;

	/* Linked list head/tail */
	struct filed_log_entry *_next;
	struct filed_log_entry *_prev;

	/* Thread from which this log entry eminates */
	pthread_t thread;

	/* Message buffer for type = MESSAGE */
	/* Path buffer for type = TRANSFER */
	char buffer[1010];
	int level;
	char buffer[FILED_PATH_BUFFER_SIZE];

	/* Items for type = TRANSFER */
	int http_code;
	const char *reason;
	time_t starttime;
	time_t endtime;
	off_t req_offset;
	off_t req_length;
	off_t sent_length;
	off_t file_length;
	char ip[128];
	int port;
};

/* Global variables */
/** Open File cache **/
struct filed_fileinfo *filed_fileinfo_fdcache = NULL;
unsigned int filed_fileinfo_fdcache_size = 0;

202
203
204
205
206
207
208

209
210
211

212


213
214
215
216
217
218
219
226
227
228
229
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245
246







+



+
-
+
+







	}

	return(fd);
}

/* Log a message */
#ifdef FILED_DONT_LOG
struct filed_log_entry filed_dummy_log_entry;
#  define filed_logging_thread_init(x) 0
#  define filed_log_msg_debug(x, ...) /**/
#  define filed_log_msg(x, ...) /**/
#  define filed_log_entry(x) /**/
#  define filed_log_ip(x, ...) "<unknown>"
#  define filed_log_ip(x, ...) NULL
#  define filed_log_new(x) &filed_dummy_log_entry
#else
#ifdef FILED_DEBUG
#  define filed_log_msg_debug(x, ...) { fprintf(stderr, x, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }
#else
#  define filed_log_msg_debug(x, ...) /**/
#endif

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
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







+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+















-
-
+
-
-
-
-
-
-
-
-
-
-

-










+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+




















-
-
-
-







			curr->_prev = prev;

			prev = curr;
		}

		curr = prev;
		while (curr) {
			switch (curr->type) {
				case FILED_LOG_TYPE_MESSAGE:
			fprintf(fp, "%s THREAD=%llu TIME=%llu\n",
				curr->buffer,
					fprintf(fp, "%s", curr->buffer);

					break;
				case FILED_LOG_TYPE_TRANSFER:
					if (curr->endtime == ((time_t) -1)) {
						curr->endtime = now;
					}

					fprintf(fp, "TRANSFER PATH=%s SRC=%s:%i TIME.START=%llu TIME.END=%llu CODE.VALUE=%u CODE.REASON=%s REQUEST.OFFSET=%llu REQUEST.LENGTH=%llu FILE.LENGTH=%llu TRANSFER.LENGTH=%llu",
						curr->buffer,
						curr->ip, curr->port,
						(unsigned long long) curr->starttime,
						(unsigned long long) curr->endtime,
						curr->http_code, curr->reason,
						(unsigned long long) curr->req_offset,
						(unsigned long long) curr->req_length,
						(unsigned long long) curr->file_length,
						(unsigned long long) curr->sent_length
					);

					break;
			}
			fprintf(fp, " THREAD=%llu TIME=%llu\n",
				(unsigned long long) curr->thread,
				(unsigned long long) now
			);
			fflush(fp);

			prev = curr;
			curr = curr->_prev;

			free(prev);
		}
	}

	return(NULL);
}

static void filed_log_msg(const char *fmt, ...) {
	struct filed_log_entry *entry;
static void filed_log_entry(struct filed_log_entry *entry) {
	va_list args;

	entry = malloc(sizeof(*entry));

	va_start(args, fmt);

	vsnprintf(entry->buffer, sizeof(entry->buffer), fmt, args);

	va_end(args);

	entry->thread = pthread_self();
	entry->level = 0;

	pthread_mutex_lock(&filed_log_msg_list_mutex);

	entry->_next = filed_log_msg_list;
	filed_log_msg_list = entry;

	pthread_mutex_unlock(&filed_log_msg_list_mutex);

	pthread_cond_signal(&filed_log_msg_list_ready);

	return;
}

static struct filed_log_entry *filed_log_new(int initialize) {
	struct filed_log_entry *retval;

	retval = malloc(sizeof(*retval));

	if (initialize) {
		retval->buffer[0] = '\0';
		retval->http_code = -1;
		retval->starttime = 0;
		retval->endtime = 0;
		retval->req_offset = 0;
		retval->req_length = 0;
		retval->sent_length = 0;
		retval->file_length = 0;
		retval->ip[0] = '\0';
		retval->port = -1;
	}

	return(retval);
}

static void filed_log_msg(const char *fmt, ...) {
	struct filed_log_entry *entry;
	va_list args;

	entry = filed_log_new(0);

	va_start(args, fmt);

	vsnprintf(entry->buffer, sizeof(entry->buffer), fmt, args);

	va_end(args);

	entry->type = FILED_LOG_TYPE_MESSAGE;

	filed_log_entry(entry);

	return;
}

static const char *filed_log_ip(struct sockaddr *addr, char *buffer, size_t bufferlen) {
	struct sockaddr_in *addr_v4;
	struct sockaddr_in6 *addr_v6;
	const char *retval = NULL;

	addr_v6 = (struct sockaddr_in6 *) addr;

	switch (addr_v6->sin6_family) {
		case AF_INET:
			addr_v4 = (struct sockaddr_in *) addr;
			retval = inet_ntop(AF_INET, &addr_v4->sin_addr, buffer, bufferlen);
			break;
		case AF_INET6:
			retval = inet_ntop(AF_INET6, &addr_v6->sin6_addr, buffer, bufferlen);
			break;
	}

	if (retval == NULL) {
		retval = "<unknown>";
	}

	return(retval);
}

static int filed_logging_thread_init(FILE *logfp) {
	struct filed_logging_thread_args *args;
	pthread_t thread_id;

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
576
577
578
579
580
581
582


583
584
585
586
587
588


589
590
591
592
593
594
595
596
597
598
599
600
601
602




603
604
605
606
607
608
609







-
-






-
-














-
-
-
-








	buffer = buffer_st->path_b;
	buffer_len = sizeof(buffer_st->path_b);

	tmpbuffer = buffer_st->tmpbuf;
	tmpbuffer_len = sizeof(buffer_st->tmpbuf);

	filed_log_msg("WAIT_FOR_REQUEST FD=%i", fd);

	fgets(buffer, buffer_len, fp);

	method = buffer;

	buffer = strchr(buffer, ' ');
	if (buffer == NULL) {
		filed_log_msg("GOT_REQUEST FD=%i ERROR=format", fd);

		return(NULL);
	}

	*buffer = '\0';
	buffer++;

	path = buffer;

	buffer = strchr(buffer, ' ');
	if (buffer != NULL) {
		*buffer = '\0';
		buffer++;
	}

	filed_log_msg("GOT_REQUEST FD=%i PATH=%s", fd, path);

	filed_log_msg("WAIT_FOR_HEADERS FD=%i", fd);

	for (i = 0; i < 100; i++) {
		fgets(tmpbuffer, tmpbuffer_len, fp);

		if (strncasecmp(tmpbuffer, "Range: ", 7) == 0) {
			workbuffer = tmpbuffer + 7;

			if (strncasecmp(workbuffer, "bytes=", 6) == 0) {
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
626
627
628
629
630
631
632


633
634
635
636
637
638
639







-
-







		}

		if (memcmp(tmpbuffer, "\r\n", 2) == 0) {
			break;
		}
	}

	filed_log_msg("GOT_HEADERS FD=%i", fd);

	/* We only handle the "GET" method */
	if (strcasecmp(method, "get") != 0) {
		return(NULL);
	}

	/* Determine range */
	if (range_end != 0) {
609
610
611
612
613
614
615
616

617
618
619
620
621
622
623
673
674
675
676
677
678
679

680
681
682
683
684
685
686
687







-
+







		(unsigned long long) strlen(error_string),
		"text/html",
		error_string
	);
}

/* Handle a single request from a client */
static void filed_handle_client(int fd, struct filed_http_request *request) {
static void filed_handle_client(int fd, struct filed_http_request *request, struct filed_log_entry *log) {
	struct filed_fileinfo *fileinfo;
	ssize_t sendfile_ret;
	size_t sendfile_size;
	off_t sendfile_offset, sendfile_sent, sendfile_len;
	char *path;
	char *date_current, date_current_b[64];
	int http_code;
635
636
637
638
639
640
641



642


643
644
645
646
647
648
649
650
651
652

653
654
655
656
657
658
659

660
661
662
663
664
665
666
667


668


669
670
671

672



673
674

675
676
677
678
679
680
681
699
700
701
702
703
704
705
706
707
708

709
710
711
712
713
714
715
716




717







718

719
720
721
722
723
724
725
726
727

728
729
730
731
732
733

734
735
736
737

738
739
740
741
742
743
744
745







+
+
+
-
+
+






-
-
-
-
+
-
-
-
-
-
-
-
+
-







+
+
-
+
+



+
-
+
+
+

-
+







	}

	request = filed_get_http_request(fp, request);

	if (request == NULL || request->path == NULL) {
		filed_error_page(fp, date_current, 500);

		log->buffer[0] = '\0';
		log->http_code = 500;
		log->reason = "format";
		filed_log_msg("INVALID_REQUEST FD=%i ERROR=500", fd);

		filed_log_entry(log);

		fclose(fp);

		return;
	}

	if (request->headers.range.present) {
		filed_log_msg("PROCESS_REPLY_START FD=%i PATH=%s RANGE_START=%llu RANGE_LENGTH=%lli",
			fd,
			request->path,
	path = request->path;
			(unsigned long long) request->headers.range.offset,
			(long long) request->headers.range.length
		);
	} else {
		filed_log_msg("PROCESS_REPLY_START FD=%i PATH=%s", fd, request->path);
	}

	strcpy(log->buffer, path);
	path = request->path;

	http_code = -1;

	fileinfo = filed_open_file(path, &request->fileinfo);
	if (fileinfo == NULL) {
		filed_error_page(fp, date_current, 404);

		log->http_code = 404;
		log->reason = "open_failed";
		filed_log_msg("PROCESS_REPLY_COMPLETE FD=%i ERROR=404", fd);

		filed_log_entry(log);
	} else {
		if (request->headers.range.offset != 0 || request->headers.range.length >= 0) {
			if (request->headers.range.offset >= fileinfo->len) {
				filed_error_page(fp, date_current, 416);
				filed_log_msg("PROCESS_REPLY_COMPLETE FD=%i ERROR=416", fd);

				log->http_code = 416;
				log->reason = "range_invalid";

				filed_error_page(fp, date_current, 416);
				filed_log_entry(log);
			} else {
				if (request->headers.range.length == ((off_t) -1)) {
					filed_log_msg_debug("Computing length to fit in bounds: fileinfo->len = %llu, request->headers.range.offset = %llu",
						(unsigned long long) fileinfo->len,
						(unsigned long long) request->headers.range.offset
					);

713
714
715
716
717
718
719
720






721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
777
778
779
780
781
782
783

784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805







806
807
808
809
810
811
812







-
+
+
+
+
+
+
















-
-
-
-
-
-
-







					(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=%i STATUS=%i", fd, http_code);
			log->http_code = http_code;
			log->reason = "OK";
			log->starttime = time(NULL);
			log->req_offset = request->headers.range.offset;
			log->req_length = request->headers.range.length;
			log->file_length = fileinfo->len;

#ifdef FILED_NONBLOCK_HTTP
			int socket_flags;
			fd_set rfd, wfd;
			char sinkbuf[8192];
			ssize_t read_ret;

			FD_ZERO(&rfd);
			FD_ZERO(&wfd);
			FD_SET(fd, &rfd);
			FD_SET(fd, &wfd);

			socket_flags = fcntl(fd, F_GETFL);
			fcntl(fd, F_SETFL, socket_flags | O_NONBLOCK);
#endif

			filed_log_msg("SEND_START FILE_FD=%i FD=%i BYTES=%llu OFFSET=%llu",
				fileinfo->fd,
				fd,
				(unsigned long long) request->headers.range.length,
				(unsigned long long) request->headers.range.offset
			);

			sendfile_offset = request->headers.range.offset;
			sendfile_len = request->headers.range.length;
			sendfile_sent = 0;
			while (1) {
				if (sendfile_len > FILED_SENDFILE_MAX) {
					sendfile_size = FILED_SENDFILE_MAX;
				} else {
788
789
790
791
792
793
794
795
796
797



798
799
800
801

802
803
804
805

806
807

808
809
810
811
812
813
814
815
816
817
818
819

820
821
822
823
824
825
826
850
851
852
853
854
855
856



857
858
859




860
861
862
863

864


865



866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882







-
-
-
+
+
+
-
-
-
-
+



-
+
-
-
+
-
-
-









+







				sendfile_len -= sendfile_ret;
				sendfile_sent += sendfile_ret;
				if (sendfile_len == 0) {
					break;
				}
			}

			filed_log_msg("SEND_COMPLETE STATUS=%s FILE_FD=%i FD=%i BYTES=%llu BYTES_SENT=%llu",
				(sendfile_sent == request->headers.range.length) ? "OK" : "PARTIAL",
				fileinfo->fd,
			log->endtime = (time_t) -1;
			log->sent_length = sendfile_sent;

				fd,
				(unsigned long long) request->headers.range.length,
				(unsigned long long) sendfile_sent
			);
			filed_log_entry(log);
		}

		close(fileinfo->fd);

	}
		filed_log_msg("CLOSE_FILE FD=%i", fd);
	}


	filed_log_msg("CLOSE_CONNECTION FD=%i", fd);

	fclose(fp);

	return;
}

/* Handle incoming connections */
static void *filed_worker_thread(void *arg_v) {
	struct filed_worker_thread_args *arg;
	struct filed_http_request request;
	struct filed_log_entry *log;
	struct sockaddr_in6 addr;
	char logbuf_ip[128];
	socklen_t addrlen;
	int failure_count = 0, max_failure_count = FILED_MAX_FAILURE_COUNT;
	int master_fd, fd;

	/* Read arguments */
848
849
850
851
852
853
854



855
856
857









858
859
860
861
862
863
864
865

866
867
868
869
870
871
872
904
905
906
907
908
909
910
911
912
913



914
915
916
917
918
919
920
921
922


923
924
925
926
927

928
929
930
931
932
933
934
935







+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
-
-





-
+








			failure_count++;

			continue;
		}

		/* Log the new connection */
		log = filed_log_new(1);
		if (log == NULL) {
			close(fd);
		filed_log_msg("NEW_CONNECTION SRC_ADDR=%s SRC_PORT=%lu FD=%i",
			filed_log_ip((struct sockaddr *) &addr, logbuf_ip, sizeof(logbuf_ip)),
			(unsigned long) addr.sin6_port,

			continue;
		}

		log->type = FILED_LOG_TYPE_TRANSFER;
		if (filed_log_ip((struct sockaddr *) &addr, log->ip, sizeof(log->ip)) == NULL) {
			log->ip[0] = '\0';
		}
		log->port = addr.sin6_port;
			fd
		);

		/* Reset failure count*/
		failure_count = 0;

		/* Handle socket */
		filed_handle_client(fd, &request);
		filed_handle_client(fd, &request, log);
	}

	/* Report error */
	filed_log_msg("THREAD_DIED ABNORMAL");

	return(NULL);