Diff

Differences From Artifact [dc9403b4bb]:

To Artifact [cff7d51a56]:


8
9
10
11
12
13
14

15
16
17
18
19
20
21
#include <pthread.h>
#include <strings.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>

#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <pwd.h>

/* Compile time constants */







>







8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <pthread.h>
#include <strings.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <stdarg.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <pwd.h>

/* Compile time constants */
174
175
176
177
178
179
180
181


182






183
184
185
186
187
188
189
	/* XXX:TODO: Unimplemented */
	return(0);
}

/* XXX:TODO: Unimplemented */
#define filed_log_msg_debug(x, ...) { fprintf(stderr, x, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }

static void filed_log_msg(const char *buffer) {


	/* XXX:TODO: Unimplemented */






	fprintf(stderr, "%s\n", buffer);

	return;
}
#endif

/* Format time per RFC2616 */







|
>
>
|
>
>
>
>
>
>







175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
	/* XXX:TODO: Unimplemented */
	return(0);
}

/* XXX:TODO: Unimplemented */
#define filed_log_msg_debug(x, ...) { fprintf(stderr, x, __VA_ARGS__); fprintf(stderr, "\n"); fflush(stderr); }

static void filed_log_msg(const char *fmt, ...) {
	char buffer[1010];
	va_list args;

	va_start(args, fmt);

	vsnprintf(buffer, sizeof(buffer), fmt, args);

	va_end(args);

	fprintf(stderr, "%s\n", buffer);

	return;
}
#endif

/* Format time per RFC2616 */
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
/* 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, 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);

	filed_log_msg("WAIT_FOR_REQUEST FD=...");

	fgets(buffer, buffer_len, fp);

	method = buffer;

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

		return(NULL);
	}

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

	path = buffer;

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

	filed_log_msg("GOT_REQUEST FD=... PATH=...");

	filed_log_msg("WAIT_FOR_HEADERS FD=...");

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

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








>

>
>












|







|















|

|







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
/* 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, range_length;
	int range_request;
	int fd;
	int i;

	fd = fileno(fp);

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

	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;

401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
		}

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

	filed_log_msg("GOT_HEADERS FD=...");

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

	/* Determine range */







|







413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
		}

		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 */
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
		close(fd);

		return;
	}

	request = filed_get_http_request(fp, request);

	filed_log_msg("PROCESS_REPLY_START FD=... PATH=... RANGE_START=... RANGE_LENGTH=...");

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

		filed_log_msg("PROCESS_REPLY_COMPLETE FD=... ERROR=500");

		fclose(fp);

		return;
	}












	path = request->path;

	http_code = -1;

	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->headers.range.offset != 0 || request->headers.range.length >= 0) {
			if (request->headers.range.offset >= fileinfo->len) {
				filed_log_msg("PROCESS_REPLY_COMPLETE FD=... ERROR=416");

				filed_error_page(fp, date_current, 416);
			} else {
				if (request->headers.range.length < 0) {
					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







<
<



|





>
>
>
>
>
>
>
>
>
>
>









|



|







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
		close(fd);

		return;
	}

	request = filed_get_http_request(fp, request);



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

		filed_log_msg("INVALID_REQUEST FD=%i ERROR=500", fd);

		fclose(fp);

		return;
	}

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

	path = request->path;

	http_code = -1;

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

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

				filed_error_page(fp, date_current, 416);
			} else {
				if (request->headers.range.length < 0) {
					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
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
					(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=20X");

#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 IFD=... OFD=... BYTES=...");






			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;







|
















|
>
>
>
>
>







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
593
594
595
596
597
598
599
					(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);

#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;
612
613
614
615
616
617
618
619






620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
				sendfile_len -= sendfile_ret;
				sendfile_sent += sendfile_ret;
				if (sendfile_len == 0) {
					break;
				}
			}

			filed_log_msg("SEND_COMPLETE STATUS=... IFD=... OFD=... BYTES=... BYTES_SENT=...");






		}

		close(fileinfo->fd);

		filed_log_msg("CLOSE_FILE FD=...");
	}

	filed_log_msg("CLOSE_CONNECTION FD=...");

	fclose(fp);

	return;
}

/* Handle incoming connections */







|
>
>
>
>
>
>




|


|







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
				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",
				"<unknown>",
				fileinfo->fd,
				fd,
				(unsigned long long) request->headers.range.length,
				(unsigned long long) sendfile_sent
			);
		}

		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 */
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679

			failure_count++;

			continue;
		}

		/* Log the new connection */
		filed_log_msg("NEW_CONNECTION SRC_ADDR=... SRC_PORT=... FD=...");

		/* Reset failure count*/
		failure_count = 0;

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







|







697
698
699
700
701
702
703
704
705
706
707
708
709
710
711

			failure_count++;

			continue;
		}

		/* Log the new connection */
		filed_log_msg("NEW_CONNECTION SRC_ADDR=... SRC_PORT=... FD=%i", fd);

		/* Reset failure count*/
		failure_count = 0;

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