Check-in [63a88bc1bc]
Overview
Comment:Added ETag
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 63a88bc1bc0116825571d5d5520b4a8ebb73cb4a
User & Date: rkeene on 2014-02-11 08:11:02
Other Links: manifest | tags
Context
2014-02-11
08:12
Updated to provide method earlier to log check-in: 5f36930fec user: rkeene tags: trunk
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
Changes

Modified filed.c from [93c05a56c1] to [288fd58a21].

48
49
50
51
52
53
54

55
56
57
58
59
60
61
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62







+







	pthread_mutex_t mutex;
	char path[FILED_PATH_BUFFER_SIZE];
	int fd;
	off_t len;
	char *lastmod;
	char lastmod_b[64];
	const char *type;
	char etag[64];
};

/* Request variables */
struct filed_http_request {
	/** Buffers **/
	struct filed_fileinfo fileinfo;
	char tmpbuf[1010];
154
155
156
157
158
159
160

161

162
163
164
165
166
167
168

169
170

171
172

173
174
175
176














177
178
179
180
181
182
183
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203







+

+







+


+


+




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








	return(0);
}

/* Initialize process */
static int filed_init(unsigned int cache_size) {
	static int called = 0;
	unsigned int random_value = 0;
	int cache_ret;
	int random_fd;

	if (called) {
		return(0);
	}

	called = 1;

	/* Attempt to lock all memory to physical RAM (but don't care if we can't) */
	mlockall(MCL_CURRENT | MCL_FUTURE);

	/* Ignore SIGPIPE */
	signal(SIGPIPE, SIG_IGN);

	/* Initialize cache structure */
	cache_ret = filed_init_cache(cache_size);
	if (cache_ret != 0) {
		return(cache_ret);
	}

	/* Initialize random number generator */
	random_fd = open("/dev/urandom", O_RDONLY);
	if (random_fd >= 0) {
		read(random_fd, &random_value, sizeof(random_value));

		close(random_fd);
	}

	random_value ^= getpid();
	random_value ^= getuid();
	random_value ^= time(NULL);

	srandom(random_value);

	return(0);
}

/* Listen on a particular address/port */
static int filed_listen(const char *address, unsigned int port) {
	struct sockaddr_in6 addr_v6;
497
498
499
500
501
502
503










504
505
506
507
508
509
510
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540







+
+
+
+
+
+
+
+
+
+








	filed_log_msg_debug("Looking up MIME type for %s (hash = %llu)", p, (unsigned long long) filed_hash((const unsigned char *) p, 16777259));

#include "filed-mime-types.h"

	return(FILED_DEFAULT_TYPE);
}

/* Generate a unique identifier */
static void filed_generate_etag(char *etag, size_t length) {
	snprintf(etag, length, "%llx%llx%llx%llx",
		(unsigned long long) random(),
		(unsigned long long) random(),
		(unsigned long long) random(),
		(unsigned long long) random()
	);
}

/* Open a file and return file information */
static struct filed_fileinfo *filed_open_file(const char *path, struct filed_fileinfo *buffer) {
	struct filed_fileinfo *cache;
	const char *open_path;
	unsigned int cache_idx;
	off_t len;
544
545
546
547
548
549
550

551
552
553
554
555
556
557
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588







+







		len = lseek(fd, 0, SEEK_END);
		lseek(fd, 0, SEEK_SET);

		cache->fd = fd;
		cache->len = len;
		strcpy(cache->path, path);
		cache->type = filed_determine_mimetype(open_path);
		filed_generate_etag(cache->etag, sizeof(cache->etag));

		/* XXX:TODO: Determine */
		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);
	}

566
567
568
569
570
571
572

573
574
575
576
577
578
579
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611







+







		return(NULL);
	}

	buffer->fd = fd;
	buffer->len = cache->len;
	buffer->type = cache->type;
	memcpy(buffer->lastmod_b, cache->lastmod_b, sizeof(buffer->lastmod_b));
	memcpy(buffer->etag, cache->etag, sizeof(buffer->etag));
	buffer->lastmod = buffer->lastmod_b + (cache->lastmod - cache->lastmod_b);

	pthread_mutex_unlock(&cache->mutex);

	return(buffer);
}

794
795
796
797
798
799
800
801

802
803
804
805
806


807
808
809
810
811
812
813
826
827
828
829
830
831
832

833
834
835
836
837

838
839
840
841
842
843
844
845
846







-
+




-
+
+







				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\nAccept-Ranges: bytes\r\nContent-Type: %s\r\nConnection: close\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\nETag: \"%s\"\r\n",
				http_code,
				date_current,
				fileinfo->lastmod,
				(unsigned long long) request->headers.range.length,
				fileinfo->type
				fileinfo->type,
				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),
					(unsigned long long) fileinfo->len
				);