Check-in [614c3eb427]
Overview
Comment:Updated to reduce use of malloc/strdup -- current run-time use is only for logging
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 614c3eb427073cf22ff2da5bc37157fb610b5568
User & Date: rkeene on 2014-02-11 05:01:40
Other Links: manifest | tags
Context
2014-02-11
05:14
Filed 1.2 check-in: b6fe901824 user: rkeene tags: trunk, 1.2
05:01
Updated to reduce use of malloc/strdup -- current run-time use is only for logging check-in: 614c3eb427 user: rkeene tags: trunk
04:42
Added note if logging is disabled check-in: 8396a85348 user: rkeene tags: trunk
Changes

Modified filed.c from [f8f87f8101] to [491e53c99b].

42
43
44
45
46
47
48
49

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


66
67
68
69
70
71
72
42
43
44
45
46
47
48

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

61
62
63

64
65
66
67
68
69
70
71
72







-
+











-



-
+
+







struct filed_logging_thread_args {
	FILE *fp;
};

/* File information */
struct filed_fileinfo {
	pthread_mutex_t mutex;
	char *path;
	char path[FILED_PATH_BUFFER_SIZE];
	int fd;
	off_t len;
	char *lastmod;
	char lastmod_b[64];
	const char *type;
};

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

	/** HTTP Request information **/
	char *path;     /*** Path being requested ***/
	/*** Path being requested ***/
	char path[FILED_PATH_BUFFER_SIZE]; 

	struct {
		struct {
			int present;
			off_t offset;   /*** Range start ***/
			off_t length;   /*** Range length ***/
		} range;
135
136
137
138
139
140
141
142

143
144
145
146
147
148
149
135
136
137
138
139
140
141

142
143
144
145
146
147
148
149







-
+







	/* Initialize cache entries */
	for (idx = 0; idx < filed_fileinfo_fdcache_size; idx++) {
		mutex_init_ret = pthread_mutex_init(&filed_fileinfo_fdcache[idx].mutex, NULL);
		if (mutex_init_ret != 0) {
			return(1);
		}

		filed_fileinfo_fdcache[idx].path = strdup("");
		filed_fileinfo_fdcache[idx].path[0] = '\0';
		filed_fileinfo_fdcache[idx].fd = -1;
		filed_fileinfo_fdcache[idx].lastmod = "";
		filed_fileinfo_fdcache[idx].type = "";
	}

	return(0);
}
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
511
512
513
514
515
516
517

518
519
520
521
522
523
524
525
526

527
528
529
530
531
532
533
534







-









-
+







		fd = open(open_path, O_RDONLY | O_LARGEFILE);
		if (fd < 0) {
			pthread_mutex_unlock(&cache->mutex);

			return(NULL);
		}

		free(cache->path);
		if (cache->fd >= 0) {
			close(cache->fd);
		}

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

		cache->fd = fd;
		cache->len = len;
		cache->path = strdup(path);
		strcpy(cache->path, path);
		cache->type = filed_determine_mimetype(open_path);

		/* 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);
	}
555
556
557
558
559
560
561
562
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
554
555
556
557
558
559
560


561
562
563
564
565
566
567
568
569
570
571
572
573
574





575
576
577
578
579
580
581
582
583







-
-
+
+












-
-
-
-
-
+
+








	return(buffer);
}

/* 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;
	char *buffer, *workbuffer, *workbuffer_next;
	size_t buffer_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);
	buffer = buffer_st->tmpbuf;
	buffer_len = sizeof(buffer_st->tmpbuf);

	fgets(buffer, buffer_len, fp);

	method = buffer;

	buffer = strchr(buffer, ' ');
	if (buffer == NULL) {
595
596
597
598
599
600
601








602

603

604
605
606


607
608
609
610
611
612
613
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607

608
609


610
611
612
613
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652

653
654
655
656
657
658
659







+
+
+
+
+
+
+
+

+
-
+

-
-
+
+




















-
+




-
-
-
-
-
















-








	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++) {
		buffer = buffer_st->tmpbuf;
		fgets(tmpbuffer, tmpbuffer_len, fp);
		fgets(buffer, buffer_len, fp);

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

			if (strncasecmp(workbuffer, "bytes=", 6) == 0) {
				workbuffer += 6;

				range_request = 1;

				range_start = strtoull(workbuffer, &workbuffer_next, 10);

				workbuffer = workbuffer_next;

				if (*workbuffer == '-') {
					workbuffer++;

					if (*workbuffer != '\r' && *workbuffer != '\n') {
						range_end = strtoull(workbuffer, &workbuffer_next, 10);
					}
				}
			}
		}

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

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

	/* Determine range */
	if (range_end != 0) {
		if (range_end <= range_start) {
			return(NULL);
		}

		range_length = range_end - range_start;

		filed_log_msg_debug("Computing length parameter: %llu = %llu - %llu",
			(unsigned long long) range_length,
			(unsigned long long) range_end,
			(unsigned long long) range_start
		);
	}

	/* Fill up structure to return */
	buffer_st->path   = path;
	buffer_st->headers.range.present = range_request;
	buffer_st->headers.range.offset  = range_start;
	buffer_st->headers.range.length  = range_length;

	return(buffer_st);

	/* Make compiler happy */
695
696
697
698
699
700
701
702

703
704
705
706
707
708
709
694
695
696
697
698
699
700

701
702
703
704
705
706
707
708







-
+







		close(fd);

		return;
	}

	request = filed_get_http_request(fp, request);

	if (request == NULL || request->path == NULL) {
	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);