/* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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.
*/
/* *** mod_proxy_uwsgi ***
Copyright 2009-2017 Unbit S.a.s. <info@unbit.it> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, 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.
if (ap_strchr(host, ':')) { /* if literal IPv6 address */
host = apr_pstrcat(r->pool, "[", host, "]", NULL);
}
if (apr_table_get(r->notes, "proxy-nocanon")
|| apr_table_get(r->notes, "proxy-noencode")) {
path = url; /* this is the raw/encoded path */
} else {
core_dir_config *d = ap_get_core_module_config(r->per_dir_config); int flags = d->allow_encoded_slashes && !d->decode_encoded_slashes ? PROXY_CANONENC_NOENCODEDSLASHENCODING : 0;
path = ap_proxy_canonenc_ex(r->pool, url, strlen(url), enc_path, flags,
r->proxyreq); if (!path) { return HTTP_BAD_REQUEST;
}
} /* * If we have a raw control character or a ' ' in nocanon path, * correct encoding was missed.
*/ if (path == url && *ap_scan_vchar_obstext(path)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10417) "To be forwarded path contains control " "characters or spaces"); return HTTP_FORBIDDEN;
}
/* this is not a security problem (in Linux) as uWSGI destroy the env memory area readable in /proc and generally if you host untrusted apps in your server and allows them to read others uid /proc/<pid> files you have higher problems...
*/
auth = apr_table_get(r->headers_in, "Authorization"); if (auth) {
apr_table_setn(r->subprocess_env, "HTTP_AUTHORIZATION", auth);
}
if (keepchar != '\0') {
buffer[status_end] = keepchar;
} else { /* 2616 requires the space in Status-Line; the origin * server may have sent one but ap_rgetline_core will
* have stripped it. */
buffer[status_end] = ' ';
buffer[status_end + 1] = '\0';
}
r->status_line = apr_pstrdup(r->pool, &buffer[status_start]);
/* parse headers */ while ((len = ap_getline(buffer, sizeof(buffer), rp, 1)) > 0) { if ((apr_size_t)len >= sizeof(buffer)) { /* too long */
len = -1; break;
}
value = strchr(buffer, ':'); if (!value) { /* invalid header */
len = -1; break;
}
*value++ = '\0'; if (*ap_scan_http_token(buffer)) { /* invalid name */
len = -1; break;
} while (apr_isspace(*value))
++value; for (end = &value[strlen(value) - 1];
end > value && apr_isspace(*end); --end)
*end = '\0'; if (*ap_scan_http_field_content(value)) { /* invalid value */
len = -1; break;
}
apr_table_add(r->headers_out, buffer, value);
} if (len < 0) { /* Reset headers, but not to NULL because things below the chain expect * this to be non NULL e.g. the ap_content_length_filter.
*/
r->headers_out = apr_table_make(r->pool, 1); return HTTP_BAD_GATEWAY;
}
/* T-E wins over C-L */ if (apr_table_get(r->headers_out, "Transfer-Encoding")) {
apr_table_unset(r->headers_out, "Content-Length");
backend->close = 1;
}
if ((buf = apr_table_get(r->headers_out, "Content-Type"))) {
ap_set_content_type(r, apr_pstrdup(r->pool, buf));
}
/* honor ProxyErrorOverride and ErrorDocument */ #if AP_MODULE_MAGIC_AT_LEAST(20101106,0)
dconf =
ap_get_module_config(r->per_dir_config, &proxy_module); if (ap_proxy_should_override(dconf, r->status)) { #else if (ap_proxy_should_override(conf, r->status)) { #endif int status = r->status;
r->status = HTTP_OK;
r->status_line = NULL;
/* Create space for state information */
status = ap_proxy_acquire_connection(UWSGI_SCHEME, &backend, worker,
r->server); if (status != OK) { goto cleanup;
}
backend->is_ssl = 0;
/* Step One: Determine Who To Connect To */
status = ap_proxy_determine_connection(p, r, conf, worker, backend,
uri, &url, proxyname, proxyport,
server_portstr, sizeof(server_portstr)); if (status != OK) { goto cleanup;
}
/* Step Two: Make the Connection */ if (ap_proxy_connect_backend(UWSGI_SCHEME, backend, worker, r->server)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10101) "failed to make connection to backend: %s:%u",
backend->hostname, backend->port);
status = HTTP_SERVICE_UNAVAILABLE; goto cleanup;
}