From 5a4d9e05f6cddc0b44915079d31724d6b60ed7f5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 23 Jan 2006 01:17:14 +0000 Subject: port to apache 2.0 git-svn-id: file:///home/lennart/svn/public/mod_mime_xattr/trunk@12 f01872de-66d6-0310-9185-fc3b30f50adc --- src/mod_mime_xattr.c | 198 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 118 insertions(+), 80 deletions(-) (limited to 'src/mod_mime_xattr.c') diff --git a/src/mod_mime_xattr.c b/src/mod_mime_xattr.c index 3a18b1e..a08fb4d 100644 --- a/src/mod_mime_xattr.c +++ b/src/mod_mime_xattr.c @@ -1,8 +1,7 @@ /* $Id$ */ /*** - - Copyright 2004 Lennart Poettering + Copyright 2004-2006 Lennart Poettering Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You @@ -15,21 +14,32 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - ***/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include #include #include -#include "httpd.h" -#include "http_config.h" -#include "http_core.h" -#include "http_log.h" -#include "http_main.h" -#include "http_protocol.h" -#include "http_request.h" -#include "ap_config.h" +#ifdef __GNUC__ +#define __unused __attribute__ ((unused)) +#else +#define __unused +#endif + #define XATTR_NAME_MIMETYPE "user.mime_type" #define XATTR_NAME_MIMETYPE2 "user.mime-type" @@ -39,61 +49,68 @@ #define XATTR_NAME_HANDLER "user.apache_handler" #define XATTR_NAME_HANDLER2 "user.apache-handler" -module MODULE_VAR_EXPORT mime_xattr_module; +module AP_MODULE_DECLARE_DATA mime_xattr_module; struct mime_xattr_dir_config { int enable_mime_type; + int enable_mime_type_set; int enable_handler; + int enable_handler_set; }; -static void* create_mime_xattr_dir_config(pool *p, char*dummy) { - struct mime_xattr_dir_config *c = (struct mime_xattr_dir_config*) ap_palloc(p, sizeof(struct mime_xattr_dir_config)); +static void* create_mime_xattr_dir_config(apr_pool_t *p, __unused char *dummy) { + struct mime_xattr_dir_config *c = apr_palloc(p, sizeof(struct mime_xattr_dir_config)); + c->enable_mime_type = 0; + c->enable_mime_type_set = 0; c->enable_handler = 0; + c->enable_handler_set = 0; + + return c; +} + +static void *merge_mime_xattr_dir_config(apr_pool_t *p, void *basev, void *overridesv) { + struct mime_xattr_dir_config *base = basev, *override = overridesv, *c; + + c = apr_palloc(p, sizeof(struct mime_xattr_dir_config)); + + c->enable_mime_type = override->enable_mime_type_set ? override->enable_mime_type : base->enable_mime_type; + c->enable_mime_type_set = override->enable_mime_type_set || base->enable_mime_type_set; + c->enable_handler = override->enable_handler_set ? override->enable_handler : base->enable_handler; + c->enable_handler_set = override->enable_handler_set || base->enable_handler_set; + return c; } -static const char *enable_xattr_mime_type(cmd_parms *parms, void *mconfig, int flag) { - struct mime_xattr_dir_config *c = (struct mime_xattr_dir_config*) mconfig; +static const char *enable_xattr_mime_type(__unused cmd_parms *parms, void *mconfig, int flag) { + struct mime_xattr_dir_config *c = mconfig; + c->enable_mime_type = flag; + c->enable_mime_type_set = 1; + return NULL; } -static const char *enable_xattr_handler(cmd_parms *parms, void *mconfig, int flag) { - struct mime_xattr_dir_config *c = (struct mime_xattr_dir_config*) mconfig; +static const char *enable_xattr_handler(__unused cmd_parms *parms, void *mconfig, int flag) { + struct mime_xattr_dir_config *c = mconfig; + c->enable_handler = flag; + c->enable_handler_set = 1; + return NULL; } -static const command_rec mime_xattr_cmds[] = { - {"XAttrMimeType", - enable_xattr_mime_type, - NULL, - OR_FILEINFO, - FLAG, - "Requires 'On' or 'Off' for enabling resp. disabling usage of file system extended attribute data for MIME type detection." }, - - {"XAttrHandler", - enable_xattr_handler, - NULL, - OR_FILEINFO, - FLAG, - "Requires 'On' or 'Off' for enabling resp. disabling usage of file system extended attribute data for handler detection." }, - - { NULL } -}; - -static char* get_xattr(pool *p, const char *fn, const char *attr) { +static char* get_xattr(apr_pool_t *p, const char *fn, const char *attr) { char v[256]; ssize_t l; - if ((l = lgetxattr(fn, attr, v, sizeof(v)-1)) < 0) - if ((l = getxattr(fn, attr, v, sizeof(v)-1)) < 0) + if ((l = lgetxattr(fn, attr, v, sizeof(v)-1)) <= 0) + if ((l = getxattr(fn, attr, v, sizeof(v)-1)) <= 0) return NULL; v[l] = 0; - return ap_pstrdup(p, v); + return apr_pstrdup(p, v); } static char* validate_charset(char *f) { @@ -101,7 +118,9 @@ static char* validate_charset(char *f) { for (c = f; *c; c++) { *c = tolower(*c); - if (!(*c >= 'a' && *c <= 'z') && !(*c >= '0' && *c <= '9') && *c != '-') + if (!(*c >= 'a' && *c <= 'z') && + !(*c >= '0' && *c <= '9') && + *c != '-') return NULL; } @@ -125,7 +144,9 @@ static char* validate_mime_type(char *f) { return NULL; } - if (!(*c >= 'a' && *c <= 'z') && !(*c >= '0' && *c <= '9') && *c != '-') + if (!(*c >= 'a' && *c <= 'z') && + !(*c >= '0' && *c <= '9') && + *c != '-') return NULL; } @@ -135,28 +156,28 @@ static char* validate_mime_type(char *f) { static int find_ct(request_rec *r) { int result = DECLINED; struct mime_xattr_dir_config* c; - - if (r->finfo.st_mode == 0 || !r->filename || !S_ISREG(r->finfo.st_mode)) + + if (!(r->finfo.valid & APR_FINFO_TYPE) || !r->filename || r->finfo.filetype != APR_REG) return DECLINED; - c = (struct mime_xattr_dir_config*) ap_get_module_config(r->per_dir_config, &mime_xattr_module); + c = ap_get_module_config(r->per_dir_config, &mime_xattr_module); if (c->enable_mime_type) { char *mime_type, *charset, *encoding; if ((charset = get_xattr(r->pool, r->filename, XATTR_NAME_CHARSET))) if (!(charset = validate_charset(charset))) - ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server, "mod_mime_xattr: bad charset specification on file <%s>", r->filename); + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server, "Bad charset specification on file '%s'", r->filename); if (!(mime_type = get_xattr(r->pool, r->filename, XATTR_NAME_MIMETYPE))) mime_type = get_xattr(r->pool, r->filename, XATTR_NAME_MIMETYPE2); if (mime_type) { if (!(mime_type = validate_mime_type(mime_type))) - ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server, "mod_mime_xattr: bad mime type specification on file <%s>", r->filename); + ap_log_error(APLOG_MARK, APLOG_WARNING, 0, r->server, "Bad MIME type specification on file '%s'", r->filename); else { if (charset) - mime_type = ap_psprintf(r->pool, "%s; charset=%s", mime_type, charset); + mime_type = apr_psprintf(r->pool, "%s; charset=%s", mime_type, charset); r->content_type = mime_type; result = OK; @@ -164,7 +185,7 @@ static int find_ct(request_rec *r) { } if (charset && !mime_type && r->content_type) { - char *a, *ct = ap_pstrdup(r->pool, r->content_type); + char *a, *ct = apr_pstrdup(r->pool, r->content_type); static const char spec[] = "; charset="; if ((a = strstr(ct, spec))) { @@ -174,9 +195,9 @@ static int find_ct(request_rec *r) { *a = 0; - r->content_type = ap_psprintf(r->pool, "%s; charset=%s%s", ct, charset, e); + r->content_type = apr_psprintf(r->pool, "%s; charset=%s%s", ct, charset, e); } else - r->content_type = ap_psprintf(r->pool, "%s; charset=%s", ct, charset); + r->content_type = apr_psprintf(r->pool, "%s; charset=%s", ct, charset); } if (!(encoding = get_xattr(r->pool, r->filename, XATTR_NAME_ENCODING))) @@ -184,9 +205,11 @@ static int find_ct(request_rec *r) { if (encoding) { if (!(encoding = validate_encoding(encoding))) - ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server, "mod_mime_xattr: bad encoding specification on file <%s>", r->filename); - else + ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Bad encoding specification on file '%s'", r->filename); + else { r->content_encoding = encoding; + result = OK; + } } } @@ -198,7 +221,7 @@ static int find_ct(request_rec *r) { if (handler) { if (!(handler = validate_handler(handler))) - ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server, "mod_mime_xattr: bad apache handler specification on file <%s>", r->filename); + ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "Bad apache handler specification on file <%s>", r->filename); else { r->handler = handler; result = OK; @@ -209,31 +232,46 @@ static int find_ct(request_rec *r) { return result; } -/* Dispatch list for API hooks */ -module MODULE_VAR_EXPORT mime_xattr_module = { - STANDARD_MODULE_STUFF, - NULL, /* module initializer */ +static void register_hooks(__unused apr_pool_t *p){ + static const char * const pre[] = { + "mod_rewrite.c", + NULL + }; + + static const char * const post[] = { + "mod_mime.c", + "mod_mime_magic.c", + NULL + }; + + ap_hook_type_checker(find_ct, pre, post, APR_HOOK_MIDDLE); +} + +static const command_rec commands[] = { + + AP_INIT_FLAG( + "XAttrMimeType", + enable_xattr_mime_type, + NULL, + OR_FILEINFO, + "Takes 'On' or 'Off' for enabling resp. disabling usage of file system extended attribute data for MIME type detection."), + + AP_INIT_FLAG( + "XAttrHandler", + enable_xattr_handler, + NULL, + OR_FILEINFO, + "Takes 'On' or 'Off' for enabling resp. disabling usage of file system extended attribute data for handler detection."), + + { NULL } +}; + +module AP_MODULE_DECLARE_DATA mime_xattr_module = { + STANDARD20_MODULE_STUFF, create_mime_xattr_dir_config, /* create per-dir config structures */ - NULL, /* merge per-dir config structures */ - NULL, /* create per-server config structures */ - NULL, /* merge per-server config structures */ - mime_xattr_cmds, /* table of config file commands */ - NULL, /* [#8] MIME-typed-dispatched handlers */ - NULL, /* [#1] URI to filename translation */ - NULL, /* [#4] validate user id from request */ - NULL, /* [#5] check if the user is ok _here_ */ - NULL, /* [#3] check access by host address */ - find_ct, /* [#6] determine MIME type */ - NULL, /* [#7] pre-run fixups */ - NULL, /* [#9] log a transaction */ - NULL, /* [#2] header parser */ - NULL, /* child_init */ - NULL, /* child_exit */ - NULL /* [#0] post read-request */ -#ifdef EAPI - ,NULL, /* EAPI: add_module */ - NULL, /* EAPI: remove_module */ - NULL, /* EAPI: rewrite_command */ - NULL /* EAPI: new_connection */ -#endif + merge_mime_xattr_dir_config, /* merge per-dir config structures */ + NULL, /* create per-server config structures */ + NULL, /* merge per-server config structures */ + commands, /* table of config file commands */ + register_hooks /* register hooks */ }; -- cgit