📄 mod_mime.c
字号:
} } return (res);}static content_type *analyze_ct(pool *p, char *s){ char *tp, *mp, *cp; char *attribute, *value; int quoted = 0; content_type *ctp; param *pp, *npp; /* initialize ctp */ ctp = (content_type *) ap_palloc(p, sizeof(content_type)); ctp->type = NULL; ctp->subtype = NULL; ctp->param = NULL; tp = ap_pstrdup(p, s); mp = tp; cp = mp; /* getting a type */ if (!(cp = strchr(mp, '/'))) { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "mod_mime: analyze_ct: cannot get media type from '%s'", mp); return (NULL); } ctp->type = ap_pstrndup(p, mp, cp - mp); ctp->type = zap_sp(ctp->type); if (ctp->type == NULL || *(ctp->type) == '\0' || strchr(ctp->type, ';') || strchr(ctp->type, ' ') || strchr(ctp->type, '\t')) { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media subtype."); return (NULL); } /* getting a subtype */ cp++; mp = cp; for (; *cp != ';' && *cp != '\0'; cp++); ctp->subtype = ap_pstrndup(p, mp, cp - mp); ctp->subtype = zap_sp(ctp->subtype); if ((ctp->subtype == NULL) || (*(ctp->subtype) == '\0') || strchr(ctp->subtype, ' ') || strchr(ctp->subtype, '\t')) { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media subtype."); return (NULL); } cp = zap_sp(cp); if (cp == NULL || *cp == '\0') { return (ctp); } /* getting parameters */ cp++; cp = zap_sp(cp); if (cp == NULL || *cp == '\0') { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return (NULL); } mp = cp; attribute = NULL; value = NULL; while (cp != NULL && *cp != '\0') { if (attribute == NULL) { if (is_token((int) *cp) > 0) { cp++; continue; } else if (*cp == ' ' || *cp == '\t' || *cp == '\n') { cp++; continue; } else if (*cp == '=') { attribute = ap_pstrndup(p, mp, cp - mp); attribute = zap_sp(attribute); if (attribute == NULL || *attribute == '\0') { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return (NULL); } cp++; cp = zap_sp(cp); if (cp == NULL || *cp == '\0') { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return (NULL); } mp = cp; continue; } else { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return (NULL); } } else { if (mp == cp) { if (*cp == '"') { quoted = 1; cp++; } else { quoted = 0; } } if (quoted > 0) { while (quoted && *cp != '\0') { if (is_qtext((int) *cp) > 0) { cp++; } else if (is_quoted_pair(cp) > 0) { cp += 2; } else if (*cp == '"') { cp++; while (*cp == ' ' || *cp == '\t' || *cp == '\n') { cp++; } if (*cp != ';' && *cp != '\0') { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return(NULL); } quoted = 0; } else { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return (NULL); } } } else { while (1) { if (is_token((int) *cp) > 0) { cp++; } else if (*cp == '\0' || *cp == ';') { break; } else { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return (NULL); } } } value = ap_pstrndup(p, mp, cp - mp); value = zap_sp(value); if (value == NULL || *value == '\0') { ap_log_error(APLOG_MARK, APLOG_WARNING, NULL, "Cannot get media parameter."); return (NULL); } pp = ap_palloc(p, sizeof(param)); pp->attr = attribute; pp->val = value; pp->next = NULL; if (ctp->param == NULL) { ctp->param = pp; } else { npp = ctp->param; while (npp->next) { npp = npp->next; } npp->next = pp; } quoted = 0; attribute = NULL; value = NULL; if (*cp == '\0') { break; } cp++; mp = cp; } } return (ctp);}static int find_ct(request_rec *r){ mime_dir_config *conf; array_header *exception_list; const char *fn; char *ext; const char *type; const char *charset = NULL; int found_metadata = 0; if (S_ISDIR(r->finfo.st_mode)) { r->content_type = DIR_MAGIC_TYPE; return OK; } conf = (mime_dir_config *) ap_get_module_config(r->per_dir_config, &mime_module); exception_list = ap_make_array(r->pool, 2, sizeof(char *)); /* Always drop the leading element */ fn = strrchr(r->filename, '/'); if (fn == NULL) fn = r->filename; else ++fn; /* The exception list keeps track of those filename components that * are not associated with extensions indicating metadata. * The base name is always the first exception (i.e., "txt.html" has * a basename of "txt" even though it might look like an extension). */ ext = ap_getword(r->pool, &fn, '.'); *((const char **) ap_push_array(exception_list)) = ext; /* Parse filename extensions, which can be in any order */ while ((ext = ap_getword(r->pool, &fn, '.')) && *ext) { int found = 0; /* Check for Content-Type */ if ((type = ap_table_get(conf->forced_types, ext)) || (type = ap_table_get(hash_buckets[hash(*ext)], ext))) { r->content_type = type; found = 1; } /* Add charset to Content-Type */ if ((type = ap_table_get(conf->charset_types, ext))) { charset = type; found = 1; } /* Check for Content-Language */ if ((type = ap_table_get(conf->language_types, ext))) { const char **new; r->content_language = type; /* back compat. only */ if (!r->content_languages) r->content_languages = ap_make_array(r->pool, 2, sizeof(char *)); new = (const char **) ap_push_array(r->content_languages); *new = type; found = 1; } /* Check for Content-Encoding */ if ((type = ap_table_get(conf->encoding_types, ext))) { if (!r->content_encoding) r->content_encoding = type; else r->content_encoding = ap_pstrcat(r->pool, r->content_encoding, ", ", type, NULL); found = 1; } /* Check for a special handler, but not for proxy request */ if ((type = ap_table_get(conf->handlers, ext)) && r->proxyreq == NOT_PROXY) { r->handler = type; found = 1; } if (found) found_metadata = 1; else *((const char **) ap_push_array(exception_list)) = ext; } /* Need to see a notes entry on r for unrecognized elements. * Somebody better claim them! If we did absolutly nothing, * skip the notes to alert mod_negotiation we are clueless. */ if (found_metadata) { ap_table_setn(r->notes, "ap-mime-exceptions-list", (void *) exception_list); } if (r->content_type) { content_type *ctp; char *ct; int override = 0; ct = (char *) ap_palloc(r->pool, sizeof(char) * (strlen(r->content_type) + 1)); strcpy(ct, r->content_type); if ((ctp = analyze_ct(r->pool, ct))) { param *pp = ctp->param; r->content_type = ap_pstrcat(r->pool, ctp->type, "/", ctp->subtype, NULL); while (pp != NULL) { if (charset && !strcmp(pp->attr, "charset")) { if (!override) { r->content_type = ap_pstrcat(r->pool, r->content_type, "; charset=", charset, NULL); override = 1; } } else { r->content_type = ap_pstrcat(r->pool, r->content_type, "; ", pp->attr, "=", pp->val, NULL); } pp = pp->next; } if (charset && !override) { r->content_type = ap_pstrcat(r->pool, r->content_type, "; charset=", charset, NULL); } } } /* Set default language, if none was specified by the extensions * and we have a DefaultLanguage setting in force */ if (!r->content_languages && conf->default_language) { const char **new; r->content_language = conf->default_language; /* back compat. only */ if (!r->content_languages) r->content_languages = ap_make_array(r->pool, 2, sizeof(char *)); new = (const char **) ap_push_array(r->content_languages); *new = conf->default_language; } /* Check for overrides with ForceType/SetHandler */ if (conf->type && strcmp(conf->type, "none")) r->content_type = conf->type; if (conf->handler && strcmp(conf->handler, "none")) r->handler = conf->handler; if (!r->content_type) return DECLINED; return OK;}module MODULE_VAR_EXPORT mime_module ={ STANDARD_MODULE_STUFF, init_mime, /* initializer */ create_mime_dir_config, /* dir config creator */ merge_mime_dir_configs, /* dir config merger */ NULL, /* server config */ NULL, /* merge server config */ mime_cmds, /* command table */ NULL, /* handlers */ NULL, /* filename translation */ NULL, /* check_user_id */ NULL, /* check auth */ NULL, /* check access */ find_ct, /* type_checker */ NULL, /* fixups */ NULL, /* logger */ NULL, /* header parser */ NULL, /* child_init */ NULL, /* child_exit */ NULL /* post read-request */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -