Check-in [1208332554]
Overview
Comment:Added support for sending correct MIME type
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:12083325549fb0a7c0fbc76a581221d28558275e
User & Date: rkeene on 2014-02-07 22:56:36
Other Links: manifest | tags
Context
2014-02-07
23:23
Added start of logging check-in: 08a3222e4b user: rkeene tags: trunk
22:56
Added support for sending correct MIME type check-in: 1208332554 user: rkeene tags: trunk
21:51
Updated wiki HTML page check-in: e7fa8416e1 user: rkeene tags: trunk
Changes

Modified Makefile from [d4f93cadc6] to [1dfba37223].

     9      9   mandir = $(prefix)/share/man
    10     10   
    11     11   all: filed
    12     12   
    13     13   filed: filed.o
    14     14   	$(CC) $(CFLAGS) $(LDFLAGS) -o "$@" $^ $(LIBS)
    15     15   
    16         -filed.o: filed.c
           16  +filed.o: filed.c filed-mime-types.h
           17  +
           18  +filed-mime-types.h: generate-mime-types
           19  +	./generate-mime-types > filed-mime-types.h
    17     20   
    18     21   install: filed filed.1
    19     22   	test -d "$(DESTDIR)$(mandir)/man1" || mkdir -p "$(DESTDIR)$(mandir)/man1"
    20     23   	test -d "$(DESTDIR)$(bindir)" || mkdir -p "$(DESTDIR)$(bindir)"
    21     24   	cp filed.1 "$(DESTDIR)$(mandir)/man1/"
    22     25   	cp filed "$(DESTDIR)$(bindir)/"
    23     26   
    24     27   clean:
    25     28   	rm -f filed filed.o
    26     29   
    27     30   distclean: clean
    28     31   
    29         -.PHONY: all install clean distclean
           32  +mrproper: distclean
           33  +	rm -f filed-mime-types.h
           34  +
           35  +.PHONY: all install clean distclean mrproper

Modified filed.c from [5138c920c1] to [dc9403b4bb].

    17     17   #include <errno.h>
    18     18   #include <time.h>
    19     19   #include <pwd.h>
    20     20   
    21     21   /* Compile time constants */
    22     22   #define FILED_SENDFILE_MAX 16777215
    23     23   #define MAX_FAILURE_COUNT 30
           24  +#define FILED_DEFAULT_TYPE "application/octet-stream"
    24     25   
    25     26   /* Default values */
    26     27   #define PORT 80
    27     28   #define THREAD_COUNT 5
    28     29   #define BIND_ADDR "::"
    29     30   #define CACHE_SIZE 8209
    30     31   
................................................................................
    37     38   struct filed_fileinfo {
    38     39   	pthread_mutex_t mutex;
    39     40   	char *path;
    40     41   	int fd;
    41     42   	off_t len;
    42     43   	char *lastmod;
    43     44   	char lastmod_b[64];
    44         -	char *type;
           45  +	const char *type;
    45     46   };
    46     47   
    47     48   /* Request variables */
    48     49   struct filed_http_request {
    49     50   	/** Buffers **/
    50     51   	struct filed_fileinfo fileinfo;
    51     52   	char path_b[1010];
................................................................................
   218    219   		}
   219    220   
   220    221   		if (prev < curr) {
   221    222   			diff = curr - prev;
   222    223   		} else {
   223    224   			diff = prev - curr;
   224    225   		}
          226  +
          227  +		prev = curr;
   225    228   
   226    229   		retval <<= 3;
          230  +		retval &= 0xFFFFFFFFLU;
   227    231   		retval ^= diff;
   228    232   
   229    233   		value++;
   230    234   	}
   231    235   
   232    236   	retval = retval % modulus;
   233    237   
   234    238   	return(retval);
   235    239   }
          240  +
          241  +/* Find a mime-type based on the filename */
          242  +static const char *filed_determine_mimetype(const char *path) {
          243  +	const char *p;
          244  +
          245  +	p = strrchr(path, '.');
          246  +	if (p == NULL) {
          247  +		return(FILED_DEFAULT_TYPE);
          248  +	}
          249  +
          250  +	p++;
          251  +	if (*p == '\0') {
          252  +		return(FILED_DEFAULT_TYPE);
          253  +	}
          254  +
          255  +	filed_log_msg_debug("Looking up MIME type for %s (hash = %llu)", p, (unsigned long long) filed_hash((const unsigned char *) p, 16777259));
          256  +
          257  +#include "filed-mime-types.h"
          258  +
          259  +	return(FILED_DEFAULT_TYPE);
          260  +}
   236    261   
   237    262   /* Open a file and return file information */
   238    263   static struct filed_fileinfo *filed_open_file(const char *path, struct filed_fileinfo *buffer) {
   239    264   	struct filed_fileinfo *cache;
   240    265   	unsigned int cache_idx;
   241    266   	off_t len;
   242    267   	int fd;
................................................................................
   268    293   
   269    294   		len = lseek(fd, 0, SEEK_END);
   270    295   		lseek(fd, 0, SEEK_SET);
   271    296   
   272    297   		cache->fd = fd;
   273    298   		cache->len = len;
   274    299   		cache->path = strdup(path);
          300  +		cache->type = filed_determine_mimetype(path);
   275    301   
   276    302   		/* XXX:TODO: Determine */
   277         -		cache->type = "video/mp4";
   278    303   		cache->lastmod = filed_format_time(cache->lastmod_b, sizeof(cache->lastmod_b), time(NULL) - 30);
   279    304   	} else {
   280    305   		filed_log_msg_debug("Cache hit for idx: %lu: PATH \"%s\"", (unsigned long) cache_idx, path);
   281    306   	}
   282    307   
   283    308   	/*
   284    309   	 * We have to make a duplicate FD, because once we release the cache

Added generate-mime-types version [a953fb49d0].

            1  +#! /usr/bin/env tclsh
            2  +
            3  +set modulus 16777259
            4  +
            5  +proc filed_hash {str mod} {
            6  +	set retval [expr {$mod - 1}]
            7  +	set prev [expr {$mod % 255}]
            8  +	for {set idx 0} {$idx < [string length $str]} {incr idx} {
            9  +		set curr [string index $str $idx]
           10  +		binary scan $curr H* curr
           11  +		set curr [format %u 0x$curr]
           12  +
           13  +		if {$curr < 32} {
           14  +			set curr [expr {255 - $curr}]
           15  +		} else {
           16  +			set curr [expr {$curr - 32}]
           17  +		}
           18  +
           19  +		if {$prev < $curr} {
           20  +			set diff [expr {$curr - $prev}]
           21  +		} else {
           22  +			set diff [expr {$prev - $curr}]
           23  +		}
           24  +
           25  +		set prev $curr
           26  +
           27  +		set retval [expr {($retval << 3) & 0xffffffff}]
           28  +		set retval [expr {$retval ^ $diff}]
           29  +	}
           30  +
           31  +	set retval [expr {$retval % $mod}]
           32  +
           33  +	return $retval
           34  +
           35  +}
           36  +
           37  +set mimeinfofile "/etc/httpd/mime.types"
           38  +
           39  +set fd [open $mimeinfofile]
           40  +set mimeinfo [read $fd]
           41  +close $fd
           42  +
           43  +foreach line [split $mimeinfo "\n"] {
           44  +	regsub {#.*} $line {} line
           45  +	set line [string trim $line]
           46  +
           47  +	if {$line == ""} {
           48  +		continue
           49  +	}
           50  +
           51  +	set line [split $line]
           52  +
           53  +	set mime [lindex $line 0]
           54  +	set extensions [lrange $line 1 end]
           55  +
           56  +	foreach extension $extensions {
           57  +		if {$extension == ""} {
           58  +			continue
           59  +		}
           60  +
           61  +		set extensioninfo($extension) $mime
           62  +	}
           63  +}
           64  +
           65  +foreach extension [array names extensioninfo] {
           66  +	set hash_id [filed_hash $extension $modulus]
           67  +
           68  +	lappend hashinfo($hash_id) $extension
           69  +
           70  +}
           71  +puts "\tswitch (filed_hash((const unsigned char *) p, $modulus)) \{"
           72  +foreach hash [lsort -integer -increasing [array names hashinfo]] {
           73  +	puts "\t\tcase $hash:"
           74  +	foreach extension $hashinfo($hash) {
           75  +		puts "\t\t\tif (strcmp(p, \"$extension\") == 0) \{"
           76  +		puts "\t\t\t\treturn(\"$extensioninfo($extension)\");"
           77  +		puts "\t\t\t\}"
           78  +	}
           79  +	puts "\t\t\treturn(FILED_DEFAULT_TYPE);"
           80  +}
           81  +puts "\t\}"