Revision 1.3 |
Revision 1.4 |
Line 10 |
Line 10 |
#include "mime.h" #include "utl.h"  
|
#include "mime.h" #include "utl.h"  
|
Mime mime[256];
|
#define NUMMIME 512
|
|
Mime mime[NUMMIME];
Mime* mimepop[NUMMIME]; /* Pointers into mime, sorted by popularity */
|
int mime_idx; // char* mime_tab[3];
|
int mime_idx; // char* mime_tab[3];
|
Line 22 |
Line 25 |
int k; int flg = 0; char sux[16];
|
int k; int flg = 0; char sux[16];
|
|
if (mime_idx >= NUMMIME)
return;
|
for (;;) { j = 0; k = 1;
|
for (;;) { j = 0; k = 1;
|
Line 46 |
Line 53 |
} mime[mime_idx].suffix[0] = '\0'; strncat(mime[mime_idx].suffix, sux, strlen(sux));
|
} mime[mime_idx].suffix[0] = '\0'; strncat(mime[mime_idx].suffix, sux, strlen(sux));
|
|
mime[mime_idx].hits = 0;
mimepop[mime_idx] = &mime[mime_idx];
|
mime_idx++; if (flg) return;
|
mime_idx++; if (flg) return;
|
Line 96 |
Line 105 |
} }
|
} }
|
char *get_mime_type(char *filename)
{
|
/****
|
|
* get_mime_type()
*
* Returns the mimetype for a file based on filename extension. This
* routine will slowly sort its list of mime types over time so that
* the more popular types bubble up to the top. Since most servers only
* serve a couple types of files, the loop will essentially be eliminated.
*
* @param filename The file to determine the mime type for.
*
* @return The mime type associated with the specified file. This is
* generally based on an entry in /etc/mime.types but can also
* be overridden in the config file.
****/
char *get_mime_type(char *filename) {
|
char *suffix; int i = 0;
|
char *suffix; int i = 0;
|
Line 112 |
Line 135 |
suffix++; // don't want '.' for (; i < mime_idx; i++) {
|
suffix++; // don't want '.' for (; i < mime_idx; i++) {
|
if (!strcmp(mime[i].suffix, suffix))
return mime[i].types;
|
if (!strcmp(mimepop[i]->suffix, suffix)) {
/*
|
|
* This is the mime type we want.
*/
Mime* tmp;
/*
* Bubble this type up (but only by a single slot) if it's more
* popular than the one above it.
*/
if (i > 0 && (++(mimepop[i]->hits) >= mimepop[i-1]->hits)) {
/* Swap this type with the one above it */
tmp = mimepop[i-1];
mimepop[i-1] = mimepop[i];
mimepop[i] = tmp;
/* Since we've swapped positions, return i-1 instead of i */
return mimepop[i-1]->types;
} else {
return mimepop[i]->types;
}
}
|
} /*
|
} /*
|
Line 123 |
Line 166 |
return default_mime ? default_mime : "text/plain"; }
|
return default_mime ? default_mime : "text/plain"; }
|