📄 mod_negotiation.c
字号:
/* Given the text of the Content-Languages: line from the var map file, * return an array containing the languages of this variant */static array_header *do_languages_line(pool *p, const char **lang_line){ array_header *lang_recs = ap_make_array(p, 2, sizeof(char *)); if (!lang_line) { return lang_recs; } while (**lang_line) { char **new = (char **) ap_push_array(lang_recs); *new = ap_get_token(p, lang_line, 0); ap_str_tolower(*new); if (**lang_line == ',' || **lang_line == ';') { ++(*lang_line); } } return lang_recs;}/***************************************************************** * * Handling header lines from clients... */static negotiation_state *parse_accept_headers(request_rec *r){ negotiation_state *new = (negotiation_state *) ap_pcalloc(r->pool, sizeof(negotiation_state)); accept_rec *elts; table *hdrs = r->headers_in; int i; new->pool = r->pool; new->r = r; new->dir_name = ap_make_dirstr_parent(r->pool, r->filename); new->accepts = do_header_line(r->pool, ap_table_get(hdrs, "Accept")); /* calculate new->accept_q value */ if (new->accepts) { elts = (accept_rec *) new->accepts->elts; for (i = 0; i < new->accepts->nelts; ++i) { if (elts[i].quality < 1.0) { new->accept_q = 1; } } } new->accept_encodings = do_header_line(r->pool, ap_table_get(hdrs, "Accept-Encoding")); new->accept_langs = do_header_line(r->pool, ap_table_get(hdrs, "Accept-Language")); new->accept_charsets = do_header_line(r->pool, ap_table_get(hdrs, "Accept-Charset")); new->avail_vars = ap_make_array(r->pool, 40, sizeof(var_rec)); return new;}static void parse_negotiate_header(request_rec *r, negotiation_state *neg){ const char *negotiate = ap_table_get(r->headers_in, "Negotiate"); char *tok; /* First, default to no TCN, no Alternates, and the original Apache * negotiation algorithm with fiddles for broken browser configs. * * To save network bandwidth, we do not configure to send an * Alternates header to the user agent by default. User * agents that want an Alternates header for agent-driven * negotiation will have to request it by sending an * appropriate Negotiate header. */ neg->ua_supports_trans = 0; neg->send_alternates = 0; neg->may_choose = 1; neg->use_rvsa = 0; neg->dont_fiddle_headers = 0; if (!negotiate) return; if (strcmp(negotiate, "trans") == 0) { /* Lynx 2.7 and 2.8 send 'negotiate: trans' even though they * do not support transparent content negotiation, so for Lynx we * ignore the negotiate header when its contents are exactly "trans". * If future versions of Lynx ever need to say 'negotiate: trans', * they can send the equivalent 'negotiate: trans, trans' instead * to avoid triggering the workaround below. */ const char *ua = ap_table_get(r->headers_in, "User-Agent"); if (ua && (strncmp(ua, "Lynx", 4) == 0)) return; } neg->may_choose = 0; /* An empty Negotiate would require 300 response */ while ((tok = ap_get_list_item(neg->pool, &negotiate)) != NULL) { if (strcmp(tok, "trans") == 0 || strcmp(tok, "vlist") == 0 || strcmp(tok, "guess-small") == 0 || ap_isdigit(tok[0]) || strcmp(tok, "*") == 0) { /* The user agent supports transparent negotiation */ neg->ua_supports_trans = 1; /* Send-alternates could be configurable, but note * that it must be 1 if we have 'vlist' in the * negotiate header. */ neg->send_alternates = 1; if (strcmp(tok, "1.0") == 0) { /* we may use the RVSA/1.0 algorithm, configure for it */ neg->may_choose = 1; neg->use_rvsa = 1; neg->dont_fiddle_headers = 1; } else if (tok[0] == '*') { /* we may use any variant selection algorithm, configure * to use the Apache algorithm */ neg->may_choose = 1; /* We disable header fiddles on the assumption that a * client sending Negotiate knows how to send correct * headers which don't need fiddling. */ neg->dont_fiddle_headers = 1; } } }#ifdef NEG_DEBUG fprintf(stderr, "dont_fiddle_headers=%d use_rvsa=%d ua_supports_trans=%d " "send_alternates=%d, may_choose=%d\n", neg->dont_fiddle_headers, neg->use_rvsa, neg->ua_supports_trans, neg->send_alternates, neg->may_choose);#endif}/* Sometimes clients will give us no Accept info at all; this routine sets * up the standard default for that case, and also arranges for us to be * willing to run a CGI script if we find one. (In fact, we set up to * dramatically prefer CGI scripts in cases where that's appropriate, * e.g., POST or when URI includes query args or extra path info). */static void maybe_add_default_accepts(negotiation_state *neg, int prefer_scripts){ accept_rec *new_accept; if (!neg->accepts) { neg->accepts = ap_make_array(neg->pool, 4, sizeof(accept_rec)); new_accept = (accept_rec *) ap_push_array(neg->accepts); new_accept->name = "*/*"; new_accept->quality = 1.0f; new_accept->level = 0.0f; } new_accept = (accept_rec *) ap_push_array(neg->accepts); new_accept->name = CGI_MAGIC_TYPE; if (neg->use_rvsa) { new_accept->quality = 0; } else { new_accept->quality = prefer_scripts ? 2.0f : 0.001f; } new_accept->level = 0.0f;}/***************************************************************** * * Parsing type-map files, in Roy's meta/http format augmented with * #-comments. *//* Reading RFC822-style header lines, ignoring #-comments and * handling continuations. */enum header_state { header_eof, header_seen, header_sep};static enum header_state get_header_line(char *buffer, int len, FILE *map){ char *buf_end = buffer + len; char *cp; int c; /* Get a noncommented line */ do { if (fgets(buffer, MAX_STRING_LEN, map) == NULL) { return header_eof; } } while (buffer[0] == '#'); /* If blank, just return it --- this ends information on this variant */ for (cp = buffer; (*cp && ap_isspace(*cp)); ++cp) { continue; } if (*cp == '\0') { return header_sep; } /* If non-blank, go looking for header lines, but note that we still * have to treat comments specially... */ cp += strlen(cp); while ((c = getc(map)) != EOF) { if (c == '#') { /* Comment line */ while ((c = getc(map)) != EOF && c != '\n') { continue; } } else if (ap_isspace(c)) { /* Leading whitespace. POSSIBLE continuation line * Also, possibly blank --- if so, we ungetc() the final newline * so that we will pick up the blank line the next time 'round. */ while (c != EOF && c != '\n' && ap_isspace(c)) { c = getc(map); } ungetc(c, map); if (c == '\n') { return header_seen; /* Blank line */ } /* Continuation */ while (cp < buf_end - 2 && (c = getc(map)) != EOF && c != '\n') { *cp++ = c; } *cp++ = '\n'; *cp = '\0'; } else { /* Line beginning with something other than whitespace */ ungetc(c, map); return header_seen; } } return header_seen;}/* Stripping out RFC822 comments */static void strip_paren_comments(char *hdr){ /* Hmmm... is this correct? In Roy's latest draft, (comments) can nest! */ /* Nope, it isn't correct. Fails to handle backslash escape as well. */ while (*hdr) { if (*hdr == '"') { hdr = strchr(hdr, '"'); if (hdr == NULL) { return; } ++hdr; } else if (*hdr == '(') { while (*hdr && *hdr != ')') { *hdr++ = ' '; } if (*hdr) { *hdr++ = ' '; } } else { ++hdr; } }}/* Getting to a header body from the header */static char *lcase_header_name_return_body(char *header, request_rec *r){ char *cp = header; for ( ; *cp && *cp != ':' ; ++cp) { *cp = ap_tolower(*cp); } if (!*cp) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, "Syntax error in type map --- no ':': %s", r->filename); return NULL; } do { ++cp; } while (*cp && ap_isspace(*cp)); if (!*cp) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, "Syntax error in type map --- no header body: %s", r->filename); return NULL; } return cp;}static int read_type_map(negotiation_state *neg, request_rec *rr){ request_rec *r = neg->r; FILE *map; char buffer[MAX_STRING_LEN]; enum header_state hstate; struct var_rec mime_info; int has_content; /* We are not using multiviews */ neg->count_multiviews_variants = 0; map = ap_pfopen(neg->pool, rr->filename, "r"); if (map == NULL) { ap_log_rerror(APLOG_MARK, APLOG_ERR, r, "cannot access type map file: %s", rr->filename); return HTTP_FORBIDDEN; } clean_var_rec(&mime_info); has_content = 0; do { hstate = get_header_line(buffer, MAX_STRING_LEN, map); if (hstate == header_seen) { char *body1 = lcase_header_name_return_body(buffer, neg->r); const char *body; if (body1 == NULL) { return SERVER_ERROR; } strip_paren_comments(body1); body = body1; if (!strncmp(buffer, "uri:", 4)) { mime_info.file_name = ap_get_token(neg->pool, &body, 0); } else if (!strncmp(buffer, "content-type:", 13)) { struct accept_rec accept_info; get_entry(neg->pool, &accept_info, body); set_mime_fields(&mime_info, &accept_info); has_content = 1; } else if (!strncmp(buffer, "content-length:", 15)) { mime_info.bytes = (float)atof(body); has_content = 1; } else if (!strncmp(buffer, "content-language:", 17)) { mime_info.content_languages = do_languages_line(neg->pool, &body); has_content = 1; } else if (!strncmp(buffer, "content-encoding:", 17)) { mime_info.content_encoding = ap_get_token(neg->pool, &body, 0); has_content = 1; } else if (!strncmp(buffer, "description:", 12)) { char *desc = ap_pstrdup(neg->pool, body); char *cp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -