1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 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 73 74 75 76 77 78
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "httpd.h"
/* ----------------------------------------------------------------- */
struct MIME {
char ext[8];
char type[64];
};
static char *mime_default;
static struct MIME *mime_types;
static int mime_count;
/* ----------------------------------------------------------------- */
static void
add_mime(char *ext, char *type)
{
if (0 == (mime_count % 64))
mime_types = realloc(mime_types,(mime_count+64)*sizeof(struct MIME));
strcpy(mime_types[mime_count].ext, ext);
strcpy(mime_types[mime_count].type,type);
mime_count++;
}
char*
get_mime(char *file)
{
int i;
char *ext;
ext = strrchr(file,'.');
if (NULL == ext)
return mime_default;
ext++;
for (i = 0; i < mime_count; i++) {
if (0 == strcasecmp(ext,mime_types[i].ext))
return mime_types[i].type;
}
return mime_default;
}
void
init_mime(char *file,char *def)
{
FILE *fp;
char line[128], type[64], ext[8];
int len,off;
mime_default = strdup(def);
if (NULL == (fp = fopen(file,"r"))) {
fprintf(stderr,"open %s: %s\n",file,strerror(errno));
return;
}
while (NULL != fgets(line,127,fp)) {
if (line[0] == '#')
continue;
if (1 != sscanf(line,"%63s%n",type,&len))
continue;
off = len;
for (;;) {
if (1 != sscanf(line+off,"%7s%n",ext,&len))
break;
off += len;
add_mime(ext,type);
}
}
fclose(fp);
}
|