Diff

Differences From Artifact [9ca8af3757]:

To Artifact [d1e3cfdb7f]:


90
91
92
93
94
95
96





97
98
99
100
101
102
103
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108







+
+
+
+
+







			off_t length;   /*** Range length ***/
		} range;

		struct {
			int present;
			char host[FILED_PATH_BUFFER_SIZE];
		} host;

		enum {
			FILED_CONNECTION_CLOSE,
			FILED_CONNECTION_KEEP_ALIVE
		} connection;
	} headers;
};

/* Log record */
struct filed_log_entry {
	/* Type of log entry */
	enum {
685
686
687
688
689
690
691

692
693
694
695
696
697
698
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704







+








	/* Set to default values */
	range_start = 0;
	range_end   = 0;
	range_request = 0;
	range_length = -1;
	buffer_st->headers.host.present = 0;
	buffer_st->headers.connection = FILED_CONNECTION_CLOSE;

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

	fgets_ret = fgets(buffer, buffer_len, fp);
	if (fgets_ret == NULL) {
		return(NULL);
784
785
786
787
788
789
790


791
792
793
794
795
796
797
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805







+
+








			workbuffer = buffer + 5;
			while (*workbuffer == ' ') {
				workbuffer++;
			}

			strcpy(buffer_st->headers.host.host, workbuffer);
		} else if (strncasecmp(buffer, "Connection: Keep-Alive", 22) == 0) {
			buffer_st->headers.connection = FILED_CONNECTION_KEEP_ALIVE;
		}

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

887
888
889
890
891
892
893
894












895
896

897
898
899
900
901
902
903
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

916
917
918
919
920
921
922
923








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

-
+







	fclose(fp);

	return;

	/* Currently unused: path */
	path = path;
}

/* Convert an enum representing the "Connection" header value to a string */
static const char *filed_connection_str(int connection_value) {
	switch (connection_value) {
		case FILED_CONNECTION_CLOSE:
			return("close");
		case FILED_CONNECTION_KEEP_ALIVE:
			return("keep-alive");
	}

	return("close");
}

/* 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_options *options) {
static int filed_handle_client(int fd, struct filed_http_request *request, struct filed_log_entry *log, struct filed_options *options) {
	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;
913
914
915
916
917
918
919
920

921
922
923
924
925
926
927
928
929
930

931
932
933
934
935
936
937
938
939
940
941

942
943
944
945
946
947
948

949
950
951
952
953
954
955
956
957
958

959
960
961
962
963
964
965
933
934
935
936
937
938
939

940
941
942
943
944
945
946
947
948
949

950
951
952
953
954
955
956
957
958
959
960

961
962
963
964
965
966
967

968
969
970
971
972
973
974
975
976
977

978
979
980
981
982
983
984
985







-
+









-
+










-
+






-
+









-
+








		log->buffer[0] = '\0';
		log->http_code = -1;
		log->reason = "fdopen_failed";

		filed_log_entry(log);

		return;
		return(0);
	}

	request = filed_get_http_request(fp, request, options);

	if (request == NULL) {
		log->buffer[0] = '\0';

		filed_error_page(fp, date_current, 500, FILED_REQUEST_METHOD_GET, "format", log);

		return;
		return(0);
	}

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

	/* If the requested path is a directory, redirect to index page */
	if (request->type == FILED_REQUEST_TYPE_DIRECTORY) {
		filed_redirect_index(fp, date_current, path, log);

		return;
		return(0);
	}

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

		return;
		return(0);
	}

	if (request->headers.range.present) {
		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, "range_invalid", log);

				close(fileinfo->fd);

				return;
				return(0);
			}

			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
				);
979
980
981
982
983
984
985
986

987
988
989
990
991

992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005

1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019







-
+





+







		http_code = 200;

		/* Compute fake range parameters that includes the entire file */
		request->headers.range.offset = 0;
		request->headers.range.length = fileinfo->len;
	}

	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\nETag: \"%s\"\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: %s\r\nETag: \"%s\"\r\n",
		http_code,
		date_current,
		fileinfo->lastmod,
		(unsigned long long) request->headers.range.length,
		fileinfo->type,
		filed_connection_str(request->headers.connection),
		fileinfo->etag
	);

	if (http_code == 206) {
		fprintf(fp, "Content-Range: bytes %llu-%llu/%llu\r\n",
			(unsigned long long) request->headers.range.offset,
			(unsigned long long) (request->headers.range.offset + request->headers.range.length - 1),
1075
1076
1077
1078
1079
1080
1081

1082

1083
1084

1085
1086




1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097

1098

1099
1100
1101
1102
1103
1104
1105
1096
1097
1098
1099
1100
1101
1102
1103

1104
1105

1106
1107

1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123

1124
1125
1126
1127
1128
1129
1130
1131







+
-
+

-
+

-
+
+
+
+











+
-
+







	}

	log->endtime = (time_t) -1;
	log->sent_length = sendfile_sent;

	filed_log_entry(log);

	if (request->headers.connection != FILED_CONNECTION_KEEP_ALIVE) {
	close(fileinfo->fd);
		close(fileinfo->fd);

	fclose(fp);
		fclose(fp);

	return;
		return(0);
	}

	return(1);
}

/* 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, local_dummy_log;
	struct filed_options *options;
	struct sockaddr_in6 addr;
	socklen_t addrlen;
	int failure_count = 0, max_failure_count = FILED_MAX_FAILURE_COUNT;
	int accept_new = 1;
	int master_fd, fd;
	int master_fd, fd = -1;

	/* Read arguments */
	arg = arg_v;

	master_fd = arg->fd;
	options = &arg->options;

1116
1117
1118
1119
1120
1121
1122

1123
1124



1125
1126
1127
1128
1129
1130
1131
1142
1143
1144
1145
1146
1147
1148
1149


1150
1151
1152
1153
1154
1155
1156
1157
1158
1159







+
-
-
+
+
+








			break;
		}

		log->type = FILED_LOG_TYPE_TRANSFER;

		/* Accept a new client */
		if (accept_new) {
		addrlen = sizeof(addr);
		fd = accept(master_fd, (struct sockaddr *) &addr, &addrlen);
			addrlen = sizeof(addr);
			fd = accept(master_fd, (struct sockaddr *) &addr, &addrlen);
		}

		/*
		 * If we fail, make a note of it so we don't go into a loop of
		 * accept() failing
		 */
		if (fd < 0) {
			/* Log the new connection */
1146
1147
1148
1149
1150
1151
1152
1153

1154
1155
1156
1157
1158
1159
1160
1174
1175
1176
1177
1178
1179
1180

1181
1182
1183
1184
1185
1186
1187
1188







-
+







			log->port = addr.sin6_port;
		}

		/* Reset failure count*/
		failure_count = 0;

		/* Handle socket */
		filed_handle_client(fd, &request, log, options);
		accept_new = !filed_handle_client(fd, &request, log, options);
	}

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

	return(NULL);