📄 protocol.c
字号:
tmp = last_char; } next_size = n - bytes_handled; rv = ap_rgetline_core(&tmp, next_size, &next_len, r, 0, bb); if (rv != APR_SUCCESS) { return rv; } if (do_alloc && next_len > 0) { char *new_buffer; apr_size_t new_size = bytes_handled + next_len + 1; /* we need to alloc an extra byte for a null */ new_buffer = apr_palloc(r->pool, new_size); /* Copy what we already had. */ memcpy(new_buffer, *s, bytes_handled); /* copy the new line, including the trailing null */ memcpy(new_buffer + bytes_handled, tmp, next_len + 1); *s = new_buffer; } bytes_handled += next_len; } } else { /* next character is not tab or space */ break; } } } *read = bytes_handled; return APR_SUCCESS;}#if APR_CHARSET_EBCDICAP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n, apr_size_t *read, request_rec *r, int fold, apr_bucket_brigade *bb){ /* on ASCII boxes, ap_rgetline is a macro which simply invokes * ap_rgetline_core with the same parms * * on EBCDIC boxes, each complete http protocol input line needs to be * translated into the code page used by the compiler. Since * ap_rgetline_core uses recursion, we do the translation in a wrapper * function to insure that each input character gets translated only once. */ apr_status_t rv; rv = ap_rgetline_core(s, n, read, r, fold, bb); if (rv == APR_SUCCESS) { ap_xlate_proto_from_ascii(*s, *read); } return rv;}#endifAP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold){ char *tmp_s = s; apr_status_t rv; apr_size_t len; apr_bucket_brigade *tmp_bb; tmp_bb = apr_brigade_create(r->pool, r->connection->bucket_alloc); rv = ap_rgetline(&tmp_s, n, &len, r, fold, tmp_bb); apr_brigade_destroy(tmp_bb); /* Map the out-of-space condition to the old API. */ if (rv == APR_ENOSPC) { return n; } /* Anything else is just bad. */ if (rv != APR_SUCCESS) { return -1; } return (int)len;}/* parse_uri: break apart the uri * Side Effects: * - sets r->args to rest after '?' (or NULL if no '?') * - sets r->uri to request uri (without r->args part) * - sets r->hostname (if not set already) from request (scheme://host:port) */AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri){ int status = HTTP_OK; r->unparsed_uri = apr_pstrdup(r->pool, uri); if (r->method_number == M_CONNECT) { status = apr_uri_parse_hostinfo(r->pool, uri, &r->parsed_uri); } else { /* Simple syntax Errors in URLs are trapped by * parse_uri_components(). */ status = apr_uri_parse(r->pool, uri, &r->parsed_uri); } if (status == APR_SUCCESS) { /* if it has a scheme we may need to do absoluteURI vhost stuff */ if (r->parsed_uri.scheme && !strcasecmp(r->parsed_uri.scheme, ap_http_method(r))) { r->hostname = r->parsed_uri.hostname; } else if (r->method_number == M_CONNECT) { r->hostname = r->parsed_uri.hostname; } r->args = r->parsed_uri.query; r->uri = r->parsed_uri.path ? r->parsed_uri.path : apr_pstrdup(r->pool, "/");#if defined(OS2) || defined(WIN32) /* Handle path translations for OS/2 and plug security hole. * This will prevent "http://www.wherever.com/..\..\/" from * returning a directory for the root drive. */ { char *x; for (x = r->uri; (x = strchr(x, '\\')) != NULL; ) *x = '/'; }#endif /* OS2 || WIN32 */ } else { r->args = NULL; r->hostname = NULL; r->status = HTTP_BAD_REQUEST; /* set error status */ r->uri = apr_pstrdup(r->pool, uri); }}static int read_request_line(request_rec *r, apr_bucket_brigade *bb){ const char *ll; const char *uri; const char *pro;#if 0 conn_rec *conn = r->connection;#endif int major = 1, minor = 0; /* Assume HTTP/1.0 if non-"HTTP" protocol */ char http[5]; apr_size_t len; int num_blank_lines = 0; int max_blank_lines = r->server->limit_req_fields; if (max_blank_lines <= 0) { max_blank_lines = DEFAULT_LIMIT_REQUEST_FIELDS; } /* Read past empty lines until we get a real request line, * a read error, the connection closes (EOF), or we timeout. * * We skip empty lines because browsers have to tack a CRLF on to the end * of POSTs to support old CERN webservers. But note that we may not * have flushed any previous response completely to the client yet. * We delay the flush as long as possible so that we can improve * performance for clients that are pipelining requests. If a request * is pipelined then we won't block during the (implicit) read() below. * If the requests aren't pipelined, then the client is still waiting * for the final buffer flush from us, and we will block in the implicit * read(). B_SAFEREAD ensures that the BUFF layer flushes if it will * have to block during a read. */ do { apr_status_t rv; /* insure ap_rgetline allocates memory each time thru the loop * if there are empty lines */ r->the_request = NULL; rv = ap_rgetline(&(r->the_request), (apr_size_t)(r->server->limit_req_line + 2), &len, r, 0, bb); if (rv != APR_SUCCESS) { r->request_time = apr_time_now(); /* ap_rgetline returns APR_ENOSPC if it fills up the * buffer before finding the end-of-line. This is only going to * happen if it exceeds the configured limit for a request-line. */ if (rv == APR_ENOSPC) { r->status = HTTP_REQUEST_URI_TOO_LARGE; r->proto_num = HTTP_VERSION(1,0); r->protocol = apr_pstrdup(r->pool, "HTTP/1.0"); } return 0; } } while ((len <= 0) && (++num_blank_lines < max_blank_lines)); /* we've probably got something to do, ignore graceful restart requests */ r->request_time = apr_time_now(); ll = r->the_request; r->method = ap_getword_white(r->pool, &ll);#if 0/* XXX If we want to keep track of the Method, the protocol module should do * it. That support isn't in the scoreboard yet. Hopefully next week * sometime. rbb */ ap_update_connection_status(AP_CHILD_THREAD_FROM_ID(conn->id), "Method", r->method);#endif uri = ap_getword_white(r->pool, &ll); /* Provide quick information about the request method as soon as known */ r->method_number = ap_method_number_of(r->method); if (r->method_number == M_GET && r->method[0] == 'H') { r->header_only = 1; } ap_parse_uri(r, uri); if (ll[0]) { r->assbackwards = 0; pro = ll; len = strlen(ll); } else { r->assbackwards = 1; pro = "HTTP/0.9"; len = 8; } r->protocol = apr_pstrmemdup(r->pool, pro, len); /* XXX ap_update_connection_status(conn->id, "Protocol", r->protocol); */ /* Avoid sscanf in the common case */ if (len == 8 && pro[0] == 'H' && pro[1] == 'T' && pro[2] == 'T' && pro[3] == 'P' && pro[4] == '/' && apr_isdigit(pro[5]) && pro[6] == '.' && apr_isdigit(pro[7])) { r->proto_num = HTTP_VERSION(pro[5] - '0', pro[7] - '0'); } else if (3 == sscanf(r->protocol, "%4s/%u.%u", http, &major, &minor) && (strcasecmp("http", http) == 0) && (minor < HTTP_VERSION(1, 0)) ) /* don't allow HTTP/0.1000 */ r->proto_num = HTTP_VERSION(major, minor); else r->proto_num = HTTP_VERSION(1, 0); return 1;}AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r, apr_bucket_brigade *bb){ char *last_field = NULL; apr_size_t last_len = 0; apr_size_t alloc_len = 0; char *field; char *value; apr_size_t len; int fields_read = 0; char *tmp_field; /* * Read header lines until we get the empty separator line, a read error, * the connection closes (EOF), reach the server limit, or we timeout. */ while(1) { apr_status_t rv; int folded = 0; field = NULL; rv = ap_rgetline(&field, r->server->limit_req_fieldsize + 2, &len, r, 0, bb); /* ap_rgetline returns APR_ENOSPC if it fills up the buffer before * finding the end-of-line. This is only going to happen if it * exceeds the configured limit for a field size. */ if (rv == APR_ENOSPC && field) { r->status = HTTP_BAD_REQUEST; /* insure ap_escape_html will terminate correctly */ field[len - 1] = '\0'; apr_table_setn(r->notes, "error-notes", apr_pstrcat(r->pool, "Size of a request header field " "exceeds server limit.<br />\n" "<pre>\n", ap_escape_html(r->pool, field), "</pre>\n", NULL)); return; } if (rv != APR_SUCCESS) { r->status = HTTP_BAD_REQUEST; return; } if (last_field != NULL) { if ((len > 0) && ((*field == '\t') || *field == ' ')) { /* This line is a continuation of the preceding line(s), * so append it to the line that we've set aside. * Note: this uses a power-of-two allocator to avoid * doing O(n) allocs and using O(n^2) space for * continuations that span many many lines. */ apr_size_t fold_len = last_len + len + 1; /* trailing null */ if ((fold_len - 1) > r->server->limit_req_fieldsize) { r->status = HTTP_BAD_REQUEST; /* report what we have accumulated so far before the * overflow (last_field) as the field with the problem */ apr_table_setn(r->notes, "error-notes", apr_pstrcat(r->pool, "Size of a request header field " "after folding " "exceeds server limit.<br />\n" "<pre>\n", ap_escape_html(r->pool, last_field), "</pre>\n", NULL)); return; } if (fold_len > alloc_len) { char *fold_buf; alloc_len += alloc_len; if (fold_len > alloc_len) { alloc_len = fold_len; } fold_buf = (char *)apr_palloc(r->pool, alloc_len); memcpy(fold_buf, last_field, last_len); last_field = fold_buf; } memcpy(last_field + last_len, field, len +1); /* +1 for nul */ last_len += len; folded = 1; } else /* not a continuation line */ { if (r->server->limit_req_fields && (++fields_read > r->server->limit_req_fields)) { r->status = HTTP_BAD_REQUEST; apr_table_setn(r->notes, "error-notes", "The number of request header fields " "exceeds this server's limit."); return; } if (!(value = strchr(last_field, ':'))) { /* Find ':' or */ r->status = HTTP_BAD_REQUEST; /* abort bad request */ apr_table_setn(r->notes, "error-notes", apr_pstrcat(r->pool, "Request header field is " "missing ':' separator.<br />\n" "<pre>\n", ap_escape_html(r->pool, last_field), "</pre>\n", NULL)); return; } tmp_field = value - 1; /* last character of field-name */ *value++ = '\0'; /* NUL-terminate at colon */ while (*value == ' ' || *value == '\t') { ++value; /* Skip to start of value */ } /* Strip LWS after field-name: */ while (tmp_field > last_field && (*tmp_field == ' ' || *tmp_field == '\t')) { *tmp_field-- = '\0'; } /* Strip LWS after field-value: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -