Revision 1.2 |
Revision 1.5 |
Line 4 |
Line 4 |
* This file is still quite primitive as you can see. */  
|
* This file is still quite primitive as you can see. */  
|
# include <stdlib.h>
# include <string.h>
# include "mime.h"
# include "utl.h"
|
#include "cow.h"
#include "cow_config.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];
|
char default_type[16];
|
|
void add_mime_type(FILE * fp, char *media_type) {
|
void add_mime_type(FILE * fp, char *media_type) {
|
Line 22 |
Line 24 |
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 52 |
} 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 57 |
Line 65 |
int h; char mt_buf[128];
|
int h; char mt_buf[128];
|
default_type[0] = '\0';
strcat(default_type, "text/html");
|
/*
* If a global mime type was set in the config file, we'll just use that
|
|
* and we don't need to read the list.
*/
if (global_mime)
return 0;
|
mime_idx = 0; for (;;) {
|
mime_idx = 0; for (;;) {
|
Line 92 |
Line 104 |
} }
|
} }
|
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;
|
|
/* If there's a global mime type defined, return that */
if (global_mime)
return global_mime;
|
suffix = strrchr(filename, '.');
|
suffix = strrchr(filename, '.');
|
|
|
if (!suffix || '\0' == *suffix)
|
if (!suffix || '\0' == *suffix)
|
return default_type;
|
return default_mime ? default_mime : "text/plain";
|
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;
}
}
|
}
|
}
|
return default_type;
|
/*
|
|
* Suffix not found in list. If a default was set in the config file,
* return that. If not, just return text/plain.
*/
return default_mime ? default_mime : "text/plain";
|
}
|
}
|