1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-28 01:35:32 +00:00
elinks/src/mime/backend/mimetypes.c

288 lines
6.1 KiB
C
Raw Normal View History

/* Support for mime.types files for mapping file extensions to content types */
/* Copyright (C) 1996-2000 Michael R. Elkins <me@cs.hmc.edu>
* Copyright (C) 2003-2004 The ELinks Project */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "config/options.h"
#include "intl/libintl.h"
#include "main/module.h"
#include "mime/backend/common.h"
#include "mime/backend/mimetypes.h"
#include "mime/mime.h"
#include "session/session.h"
#include "util/hash.h"
#include "util/lists.h"
#include "util/memory.h"
#define BACKEND_NAME "mimetypes"
struct mimetypes_entry {
char *content_type;
char extension[1];
};
enum mimetypes_option {
MIMETYPES_TREE,
MIMETYPES_ENABLE,
MIMETYPES_PATH,
MIMETYPES_OPTIONS
};
/* Keep options in alphabetical order. */
bug 764: Initialize the right member of union option_value INIT_OPTION used to initialize union option_value at compile time by casting the default value to LIST_OF(struct option) *, which is the type of the first member. On sparc64 and other big-endian systems where sizeof(int) < sizeof(struct list_head *), this tended to leave option->value.number as zero, thus messing up OPT_INT and OPT_BOOL at least. OPT_LONG however tended to work right. This would be easy to fix with C99 designated initializers, but doc/hacking.txt says ELinks must be kept C89 compatible. Another solution would be to make register_options() read the value from option->value.tree (the first member), cast it back to the right type, and write it to the appropriate member; but that would still require somewhat dubious conversions between integers, data pointers, and function pointers. So here's a rather more invasive solution. Add struct option_init, which is somewhat similar to struct option but has non-overlapping members for different types of values, to ensure nothing is lost in compile-time conversions. Move unsigned char *path from struct option_info to struct option_init, and replace struct option_info with a union that contains struct option_init and struct option. Now, this union can be initialized with no portability problems, and register_options() then moves the values from struct option_init to their final places in struct option. In my x86 ELinks build with plenty of options configured in, this change bloated the text section by 340 bytes but compressed the data section by 2784 bytes, presumably because union option_info is a pointer smaller than struct option_info was. (cherry picked from elinks-0.12 commit e5f6592ee20780a61f70feeb1f9e17631b9c5835) Conflicts: src/protocol/fsp/fsp.c: All options had been removed in 0.13.GIT. src/protocol/smb/smb2.c: Ditto.
2009-08-15 19:39:07 +00:00
static union option_info mimetypes_options[] = {
INIT_OPT_TREE("mime", N_("Mimetypes files"),
"mimetypes", OPT_ZERO,
N_("Options for the support of mime.types files. These files "
"can be used to find the content type of a URL by looking at "
"the extension of the file name.")),
INIT_OPT_BOOL("mime.mimetypes", N_("Enable"),
"enable", OPT_ZERO, 1,
N_("Enable mime.types support.")),
INIT_OPT_STRING("mime.mimetypes", N_("Path"),
"path", OPT_ZERO, DEFAULT_MIMETYPES_PATH,
N_("The search path for mime.types files. "
"Colon-separated list of files.")),
NULL_OPTION_INFO,
};
#define get_opt_mimetypes(which) mimetypes_options[(which)].option
#define get_mimetypes(which) get_opt_mimetypes(which).value
#define get_mimetypes_enable() get_mimetypes(MIMETYPES_ENABLE).number
#define get_mimetypes_path() get_mimetypes(MIMETYPES_PATH).string
/* State variables */
static struct hash *mimetypes_map = NULL;
static void
done_mimetypes_entry(struct mimetypes_entry *entry)
{
if (!entry) return;
mem_free_if(entry->content_type);
mem_free(entry);
}
/* Parsing of a mime.types file with the format:
*
* basetype/subtype extension1 [extension2 ... extensionN]
*
* Comments starts with '#'. */
static inline void
parse_mimetypes_extensions(char *token, char *ctype)
{
int ctypelen = strlen(ctype);
/* Cycle through the file extensions */
while (*token) {
struct mimetypes_entry *entry;
char *extension;
struct hash_item *item;
int extlen;
skip_space(token);
extension = token;
skip_nonspace(token);
if (!*token) break;
*token++ = '\0';
extlen = strlen(extension);
/* First check if the type is already known. If it is
* drop it. This way first files are priotized. */
item = get_hash_item(mimetypes_map, extension, extlen);
if (item) continue;
2022-01-16 20:08:50 +00:00
entry = (struct mimetypes_entry *)mem_calloc(1, sizeof(*entry) + extlen);
if (!entry) continue;
entry->content_type = memacpy(ctype, ctypelen);
if (!entry->content_type) {
done_mimetypes_entry(entry);
continue;
}
memcpy(entry->extension, extension, extlen);
item = add_hash_item(mimetypes_map, entry->extension, extlen,
entry);
2006-06-06 14:33:16 +00:00
if (!item)
done_mimetypes_entry(entry);
}
}
static void
parse_mimetypes_file(char *filename)
{
FILE *file = fopen(filename, "rb");
char line[MAX_STR_LEN];
if (!file) return;
while (fgets(line, MAX_STR_LEN - 1, file)) {
char *ctype = line;
char *token;
/* Weed out any comments */
token = strchr(line, '#');
if (token)
*token = '\0';
skip_space(ctype);
/* Position on the next field in this line */
token = ctype;
skip_nonspace(token);
if (!*token) continue;
*token++ = '\0';
/* Check if malformed content type */
if (!strchr(ctype, '/')) continue;
parse_mimetypes_extensions(token, ctype);
}
fclose(file);
}
static struct hash *
init_mimetypes_map(void)
{
char *path;
mimetypes_map = init_hash8();
if (!mimetypes_map)
return NULL;
/* Determine the path */
path = get_mimetypes_path();
2022-02-18 16:09:54 +00:00
if (!path || !*path) path = (char *)DEFAULT_MIMETYPES_PATH;
while (*path) {
char *filename = get_next_path_filename(&path, ':');
if (!filename) continue;
parse_mimetypes_file(filename);
mem_free(filename);
}
return mimetypes_map;
}
static void
done_mimetypes(struct module *module)
{
struct hash_item *item;
int i;
if (!mimetypes_map)
return;
foreach_hash_item (item, *mimetypes_map, i) {
if (item->value) {
2022-01-25 17:25:58 +00:00
struct mimetypes_entry *entry = (struct mimetypes_entry *)item->value;
done_mimetypes_entry(entry);
}
}
free_hash(&mimetypes_map);
}
static int
change_hook_mimetypes(struct session *ses, struct option *current, struct option *changed)
{
if (changed == &get_opt_mimetypes(MIMETYPES_PATH)
|| (changed == &get_opt_mimetypes(MIMETYPES_ENABLE)
&& !get_mimetypes_enable())) {
done_mimetypes(&mimetypes_mime_module);
}
return 0;
}
static void
init_mimetypes(struct module *module)
{
static const struct change_hook_info mimetypes_change_hooks[] = {
{ "mime.mimetypes", change_hook_mimetypes },
{ NULL, NULL },
};
register_change_hooks(mimetypes_change_hooks);
if (get_cmd_opt_bool("anonymous"))
get_mimetypes_enable() = 0;
}
static char *
get_content_type_mimetypes(char *extension)
{
struct hash_item *item;
int extensionlen;
if (!get_mimetypes_enable()
|| (!mimetypes_map && !init_mimetypes_map()))
return NULL;
extension++; /* Skip the leading '.' */
extensionlen = strlen(extension);
while (extensionlen) {
char *trimmed;
/* First the given type is looked up. */
item = get_hash_item(mimetypes_map, extension, extensionlen);
/* Check list of entries */
if (item && item->value) {
2022-01-25 17:25:58 +00:00
struct mimetypes_entry *entry = (struct mimetypes_entry *)item->value;
return stracpy(entry->content_type);
}
/* Try to trim the extension from the left. */
trimmed = strchr(extension, '.');
if (!trimmed)
break;
extensionlen -= trimmed - extension + 1;
extension = trimmed + 1;
}
return NULL;
}
2007-02-04 11:34:48 +00:00
const struct mime_backend mimetypes_mime_backend = {
/* get_content_type: */ get_content_type_mimetypes,
/* get_mime_handler: */ NULL,
};
struct module mimetypes_mime_module = struct_module(
/* name: */ N_("Mimetypes files"),
/* options: */ mimetypes_options,
/* hooks: */ NULL,
/* submodules: */ NULL,
/* data: */ NULL,
/* init: */ init_mimetypes,
/* done: */ done_mimetypes
);