2005-09-15 13:58:31 +00:00
|
|
|
/* 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"
|
2021-08-08 19:25:08 +00:00
|
|
|
#include "intl/libintl.h"
|
2005-09-15 13:58:31 +00:00
|
|
|
#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 {
|
2021-01-02 15:20:27 +00:00
|
|
|
char *content_type;
|
|
|
|
char extension[1];
|
2005-09-15 13:58:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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[] = {
|
2022-01-19 21:49:13 +00:00
|
|
|
INIT_OPT_TREE(C_("mime"), N_("Mimetypes files"),
|
|
|
|
C_("mimetypes"), OPT_ZERO,
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 18:48:38 +00:00
|
|
|
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 "
|
2005-09-15 13:58:31 +00:00
|
|
|
"the extension of the file name.")),
|
|
|
|
|
2022-01-19 21:49:13 +00:00
|
|
|
INIT_OPT_BOOL(C_("mime.mimetypes"), N_("Enable"),
|
|
|
|
C_("enable"), OPT_ZERO, 1,
|
2005-09-15 13:58:31 +00:00
|
|
|
N_("Enable mime.types support.")),
|
|
|
|
|
2022-01-19 21:49:13 +00:00
|
|
|
INIT_OPT_STRING(C_("mime.mimetypes"), N_("Path"),
|
|
|
|
C_("path"), OPT_ZERO, DEFAULT_MIMETYPES_PATH,
|
Rewrap lines in option documentation.
Documentation strings of most options used to contain a "\n" at the
end of each source line. When the option manager displayed these
strings, it treated each "\n" as a hard newline. On 80x24 terminals
however, the option description window has only 60 columes available
for the text (with the default setup.h), and the hard newlines were
further apart, so the option manager wrapped the text a second time,
resulting in rather ugly output where long lones are interleaved with
short ones. This could also cause the text to take up too much
vertical space and not fit in the window.
Replace most of those hard newlines with spaces so that the option
manager (or perhaps BFU) will take care of the wrapping. At the same
time, rewrap the strings in source code so that the source lines are
at most 79 columns wide.
In some options though, there is a list of possible values and their
meanings. In those lists, if the description of one value does not
fit in one line, then continuation lines should be indented. The
option manager and BFU are not currently able to do that. So, keep
the hard newlines in those lists, but rewrap them to 60 columns so
that they are less likely to require further wrapping at runtime.
2009-03-07 18:48:38 +00:00
|
|
|
N_("The search path for mime.types files. "
|
|
|
|
"Colon-separated list of files.")),
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
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
|
2021-01-02 15:20:27 +00:00
|
|
|
parse_mimetypes_extensions(char *token, char *ctype)
|
2005-09-15 13:58:31 +00:00
|
|
|
{
|
|
|
|
int ctypelen = strlen(ctype);
|
|
|
|
|
|
|
|
/* Cycle through the file extensions */
|
|
|
|
while (*token) {
|
|
|
|
struct mimetypes_entry *entry;
|
2021-01-02 15:20:27 +00:00
|
|
|
char *extension;
|
2005-09-15 13:58:31 +00:00
|
|
|
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);
|
2005-09-15 13:58:31 +00:00
|
|
|
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)
|
2005-09-15 13:58:31 +00:00
|
|
|
done_mimetypes_entry(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-01-02 15:20:27 +00:00
|
|
|
parse_mimetypes_file(char *filename)
|
2005-09-15 13:58:31 +00:00
|
|
|
{
|
|
|
|
FILE *file = fopen(filename, "rb");
|
2021-01-02 15:20:27 +00:00
|
|
|
char line[MAX_STR_LEN];
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
while (fgets(line, MAX_STR_LEN - 1, file)) {
|
2021-01-02 15:20:27 +00:00
|
|
|
char *ctype = line;
|
|
|
|
char *token;
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
/* Weed out any comments */
|
2022-01-18 19:30:48 +00:00
|
|
|
token = strchr(line, '#');
|
2005-09-15 13:58:31 +00:00
|
|
|
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 */
|
2022-01-18 19:30:48 +00:00
|
|
|
if (!strchr(ctype, '/')) continue;
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
parse_mimetypes_extensions(token, ctype);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hash *
|
|
|
|
init_mimetypes_map(void)
|
|
|
|
{
|
2021-01-02 15:20:27 +00:00
|
|
|
char *path;
|
2005-09-15 13:58:31 +00:00
|
|
|
|
2006-05-31 17:17:01 +00:00
|
|
|
mimetypes_map = init_hash8();
|
2005-09-15 13:58:31 +00:00
|
|
|
if (!mimetypes_map)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Determine the path */
|
|
|
|
path = get_mimetypes_path();
|
|
|
|
if (!path || !*path) path = DEFAULT_MIMETYPES_PATH;
|
|
|
|
|
|
|
|
while (*path) {
|
2021-01-02 15:20:27 +00:00
|
|
|
char *filename = get_next_path_filename(&path, ':');
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
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;
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
done_mimetypes_entry(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-31 17:33:36 +00:00
|
|
|
free_hash(&mimetypes_map);
|
2005-09-15 13:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2007-01-27 17:00:47 +00:00
|
|
|
static const struct change_hook_info mimetypes_change_hooks[] = {
|
2005-09-15 13:58:31 +00:00
|
|
|
{ "mime.mimetypes", change_hook_mimetypes },
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
register_change_hooks(mimetypes_change_hooks);
|
|
|
|
|
|
|
|
if (get_cmd_opt_bool("anonymous"))
|
|
|
|
get_mimetypes_enable() = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 15:20:27 +00:00
|
|
|
static char *
|
|
|
|
get_content_type_mimetypes(char *extension)
|
2005-09-15 13:58:31 +00:00
|
|
|
{
|
|
|
|
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) {
|
2021-01-02 15:20:27 +00:00
|
|
|
char *trimmed;
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
/* 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;
|
2005-09-15 13:58:31 +00:00
|
|
|
|
|
|
|
return stracpy(entry->content_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to trim the extension from the left. */
|
2022-01-18 19:30:48 +00:00
|
|
|
trimmed = strchr(extension, '.');
|
2005-09-15 13:58:31 +00:00
|
|
|
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 = {
|
2005-09-15 13:58:31 +00:00
|
|
|
/* 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
|
|
|
|
);
|