Check-in [5a054ee091]
Overview
Comment:Updated to support the HEAD HTTP method
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5a054ee091543db480d8bdf83e8d9f9e91afa864
User & Date: rkeene on 2014-02-11 07:43:06
Original Comment: Updated to support the HEAD HTTP method5~
Other Links: manifest | tags
Context
2014-02-11
08:11
Added ETag check-in: 63a88bc1bc user: rkeene tags: trunk
07:43
Updated to support the HEAD HTTP method check-in: 5a054ee091 user: rkeene tags: trunk
05:53
Removed unneeded assignment check-in: 8cb189c633 user: rkeene tags: trunk
Changes

Modified filed.c from [8c74e1e76e] to [93c05a56c1].

57
58
59
60
61
62
63






64
65
66
67
68
69
70
/* Request variables */
struct filed_http_request {
	/** Buffers **/
	struct filed_fileinfo fileinfo;
	char tmpbuf[1010];

	/** HTTP Request information **/






	/*** Path being requested ***/
	char path[FILED_PATH_BUFFER_SIZE]; 

	struct {
		struct {
			int present;
			off_t offset;   /*** Range start ***/







>
>
>
>
>
>







57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Request variables */
struct filed_http_request {
	/** Buffers **/
	struct filed_fileinfo fileinfo;
	char tmpbuf[1010];

	/** HTTP Request information **/
	/*** Type of request (HEAD or GET) ***/
	enum {
		FILED_REQUEST_METHOD_GET,
		FILED_REQUEST_METHOD_HEAD
	} method;

	/*** Path being requested ***/
	char path[FILED_PATH_BUFFER_SIZE]; 

	struct {
		struct {
			int present;
			off_t offset;   /*** Range start ***/
99
100
101
102
103
104
105

106
107
108
109
110
111
112
	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;








>







105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	time_t endtime;
	off_t req_offset;
	off_t req_length;
	off_t sent_length;
	off_t file_length;
	char ip[128];
	int port;
	int method;
};

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

243
244
245
246
247
248
249

250
251
252
253
254
255
256
#    define filed_log_msg_debug(x, ...) /**/
#  endif

/* Initialize logging thread */
static void *filed_logging_thread(void *arg_p) {
	struct filed_logging_thread_args *arg;
	struct filed_log_entry *curr, *prev;

	time_t now;
	FILE *fp;

	arg = arg_p;

	fp = arg->fp;








>







250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#    define filed_log_msg_debug(x, ...) /**/
#  endif

/* Initialize logging thread */
static void *filed_logging_thread(void *arg_p) {
	struct filed_logging_thread_args *arg;
	struct filed_log_entry *curr, *prev;
	const char *method;
	time_t now;
	FILE *fp;

	arg = arg_p;

	fp = arg->fp;

276
277
278
279
280
281
282












283
284
285
286
287

288
289
290
291
292
293
294
		while (curr) {
			switch (curr->type) {
				case FILED_LOG_TYPE_MESSAGE:
					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,







>
>
>
>
>
>
>
>
>
>
>
>




|
>







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
		while (curr) {
			switch (curr->type) {
				case FILED_LOG_TYPE_MESSAGE:
					fprintf(fp, "%s", curr->buffer);

					break;
				case FILED_LOG_TYPE_TRANSFER:
					switch (curr->method) {
						case FILED_REQUEST_METHOD_GET:
							method="GET";
							break;
						case FILED_REQUEST_METHOD_HEAD:
							method="HEAD";
							break;
						default:
							method="<unknown>";
							break;
					}

					if (curr->endtime == ((time_t) -1)) {
						curr->endtime = now;
					}

					fprintf(fp, "TRANSFER METHOD=%s 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",
						method,
						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,
341
342
343
344
345
346
347

348
349
350
351
352
353
354
		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;







>







362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
		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;
		retval->method = -1;
	}

	return(retval);
}

static void filed_log_msg(const char *fmt, ...) {
	struct filed_log_entry *entry;
592
593
594
595
596
597
598
599

600
601







602
603
604
605
606
607
608

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

	/* We only handle the "GET" method */

	if (strcasecmp(method, "get") != 0) {
		return(NULL);







	}

	/* Note path */
	strcpy(buffer_st->path, path);

	for (i = 0; i < 100; i++) {
		fgets_ret = fgets(buffer, buffer_len, fp);







|
>
|
|
>
>
>
>
>
>
>







614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

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

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

		/* GET request */
		buffer_st->method = FILED_REQUEST_METHOD_GET;
	} else {
		/* HEAD request */
		buffer_st->method = FILED_REQUEST_METHOD_HEAD;
	}

	/* Note path */
	strcpy(buffer_st->path, path);

	for (i = 0; i < 100; i++) {
		fgets_ret = fgets(buffer, buffer_len, fp);
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
	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>";

	fprintf(fp, "HTTP/1.1 %i Not OK\r\nDate: %s\r\nServer: filed\r\nLast-Modified: %s\r\nContent-Length: %llu\r\nContent-Type: %s\r\nConnection: close\r\n\r\n%s",
		error_number,
		date_current,
		date_current,
		(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, struct filed_log_entry *log) {
	struct filed_fileinfo *fileinfo;
	ssize_t sendfile_ret;
	size_t sendfile_size;







|


|




|
<

>
>
>
>
>
>
>







687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702

703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
	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, int method) {
	char *error_string = "<html><head><title>ERROR</title></head><body>Unable to process request</body></html>";

	fprintf(fp, "HTTP/1.1 %i Not OK\r\nDate: %s\r\nServer: filed\r\nLast-Modified: %s\r\nContent-Length: %llu\r\nContent-Type: %s\r\nConnection: close\r\n\r\n",
		error_number,
		date_current,
		date_current,
		(unsigned long long) strlen(error_string),
		"text/html"

	);

	/* silence error string for HEAD requests */
	if (method != FILED_REQUEST_METHOD_HEAD) {
		fprintf(fp, "%s", error_string);
	}

	return;
}

/* Handle a single request from a client */
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;
695
696
697
698
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

		return;
	}

	request = filed_get_http_request(fp, request);

	if (request == NULL) {
		filed_error_page(fp, date_current, 500);

		log->buffer[0] = '\0';
		log->http_code = 500;
		log->reason = "format";

		filed_log_entry(log);

		fclose(fp);

		return;
	}

	path = request->path;
	strcpy(log->buffer, 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_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);

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

				filed_log_entry(log);
			} else {
				if (request->headers.range.length == ((off_t) -1)) {







|



















|








|







731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774

		return;
	}

	request = filed_get_http_request(fp, request);

	if (request == NULL) {
		filed_error_page(fp, date_current, 500, FILED_REQUEST_METHOD_GET);

		log->buffer[0] = '\0';
		log->http_code = 500;
		log->reason = "format";

		filed_log_entry(log);

		fclose(fp);

		return;
	}

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

	http_code = -1;

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

		log->http_code = 404;
		log->reason = "open_failed";

		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, request->method);

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

				filed_log_entry(log);
			} else {
				if (request->headers.range.length == ((off_t) -1)) {
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
813
814

			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

			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 {
					sendfile_size = sendfile_len;
				}

				sendfile_ret = sendfile(fd, fileinfo->fd, &sendfile_offset, sendfile_size);







>



















|







817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851

			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;
			log->method = request->method;

#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

			sendfile_offset = request->headers.range.offset;
			sendfile_len = request->headers.range.length;
			sendfile_sent = 0;
			while (request->method == FILED_REQUEST_METHOD_GET) {
				if (sendfile_len > FILED_SENDFILE_MAX) {
					sendfile_size = FILED_SENDFILE_MAX;
				} else {
					sendfile_size = sendfile_len;
				}

				sendfile_ret = sendfile(fd, fileinfo->fd, &sendfile_offset, sendfile_size);