-/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
- * applicable.
- *
- * 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
+/* 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
*
#include "ajp_header.h"
#include "ajp.h"
+APLOG_USE_MODULE(proxy_ajp);
+
static const char *response_trans_headers[] = {
- "Content-Type",
- "Content-Language",
- "Content-Length",
- "Date",
- "Last-Modified",
- "Location",
- "Set-Cookie",
- "Set-Cookie2",
- "Servlet-Engine",
- "Status",
+ "Content-Type",
+ "Content-Language",
+ "Content-Length",
+ "Date",
+ "Last-Modified",
+ "Location",
+ "Set-Cookie",
+ "Set-Cookie2",
+ "Servlet-Engine",
+ "Status",
"WWW-Authenticate"
};
-static const char *long_res_header_for_sc(int sc)
+static const char *long_res_header_for_sc(int sc)
{
const char *rc = NULL;
sc = sc & 0X00FF;
const char *p = header_name;
int i = 0;
- /* ACCEPT-LANGUAGE is the longest headeer
+ /* ACCEPT-LANGUAGE is the longest header
* that is of interest.
*/
if (len < 4 || len > 15)
return UNKNOWN_METHOD;
-
+
while (*p)
header[i++] = apr_toupper(*p++);
header[i] = '\0';
return SC_ACCEPT;
else if (header[6] == '-') {
p += 6;
- if (memcmp(p, "CHARSET", 7) == 0)
+ if (strcmp(p, "CHARSET") == 0)
return SC_ACCEPT_CHARSET;
- else if (memcmp(p, "ENCODING", 8) == 0)
+ else if (strcmp(p, "ENCODING") == 0)
return SC_ACCEPT_ENCODING;
- else if (memcmp(p, "LANGUAGE", 8) == 0)
+ else if (strcmp(p, "LANGUAGE") == 0)
return SC_ACCEPT_LANGUAGE;
else
return UNKNOWN_METHOD;
else
return UNKNOWN_METHOD;
}
- else if (memcmp(p, "UTHORIZATION", 12) == 0)
+ else if (strcmp(p, "UTHORIZATION") == 0)
return SC_AUTHORIZATION;
else
return UNKNOWN_METHOD;
break;
case 'C':
- if (memcmp(p, "OOKIE", 5) == 0)
+ if(strcmp(p, "OOKIE2") == 0)
+ return SC_COOKIE2;
+ else if (strcmp(p, "OOKIE") == 0)
return SC_COOKIE;
- else if(memcmp(p, "ONNECTION", 9) == 0)
+ else if(strcmp(p, "ONNECTION") == 0)
return SC_CONNECTION;
- else if(memcmp(p, "ONTENT-TYPE", 11) == 0)
+ else if(strcmp(p, "ONTENT-TYPE") == 0)
return SC_CONTENT_TYPE;
- else if(memcmp(p, "ONTENT-LENGTH", 13) == 0)
+ else if(strcmp(p, "ONTENT-LENGTH") == 0)
return SC_CONTENT_LENGTH;
- else if(memcmp(p, "OOKIE2", 6) == 0)
- return SC_COOKIE2;
else
return UNKNOWN_METHOD;
break;
case 'H':
- if(memcmp(p, "OST", 3) == 0)
+ if(strcmp(p, "OST") == 0)
return SC_HOST;
else
return UNKNOWN_METHOD;
break;
case 'P':
- if(memcmp(p, "RAGMA", 5) == 0)
+ if(strcmp(p, "RAGMA") == 0)
return SC_PRAGMA;
else
return UNKNOWN_METHOD;
break;
case 'R':
- if(memcmp(p, "EFERER", 6) == 0)
+ if(strcmp(p, "EFERER") == 0)
return SC_REFERER;
else
return UNKNOWN_METHOD;
break;
case 'U':
- if(memcmp(p, "SER-AGENT", 9) == 0)
+ if(strcmp(p, "SER-AGENT") == 0)
return SC_USER_AGENT;
else
return UNKNOWN_METHOD;
SC_M_MKWORKSPACE,
SC_M_MKACTIVITY,
SC_M_BASELINE_CONTROL,
- SC_M_MERGE,
+ SC_M_MERGE,
0 /* M_INVALID */
};
-static int sc_for_req_method_by_id(int method_id)
+static int sc_for_req_method_by_id(request_rec *r)
{
- if (method_id < 0 || method_id > M_INVALID)
+ int method_id = r->method_number;
+ if (method_id < 0 || method_id > M_INVALID) {
return UNKNOWN_METHOD;
- else
+ }
+ else if (r->header_only) {
+ return SC_M_HEAD;
+ }
+ else {
return sc_for_req_method_table[method_id] ?
sc_for_req_method_table[method_id] : UNKNOWN_METHOD;
+ }
}
/*
const apr_array_header_t *arr = apr_table_elts(r->subprocess_env);
const apr_table_entry_t *elts = (const apr_table_entry_t *)arr->elts;
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ ap_log_error(APLOG_MARK, APLOG_TRACE8, 0, r->server,
"Into ajp_marshal_into_msgb");
- if ((method = sc_for_req_method_by_id(r->method_number)) == UNKNOWN_METHOD) {
+ if ((method = sc_for_req_method_by_id(r)) == UNKNOWN_METHOD) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"ajp_marshal_into_msgb - No such method %s",
r->method);
ajp_msg_reset(msg);
if (ajp_msg_append_uint8(msg, CMD_AJP13_FORWARD_REQUEST) ||
- ajp_msg_append_uint8(msg, method) ||
+ ajp_msg_append_uint8(msg, (apr_byte_t) method) ||
ajp_msg_append_string(msg, r->protocol) ||
ajp_msg_append_string(msg, uri->path) ||
ajp_msg_append_string(msg, r->connection->remote_ip) ||
return AJP_EOVERFLOW;
}
}
-
+
if (ajp_msg_append_string(msg, elts[i].val)) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"ajp_marshal_into_msgb: "
"Error appending the header value");
return AJP_EOVERFLOW;
}
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ ap_log_error(APLOG_MARK, APLOG_TRACE5, 0, r->server,
"ajp_marshal_into_msgb: Header[%d] [%s] = [%s]",
i, elts[i].key, elts[i].val);
}
}
}
*/
-
+
if (r->user) {
if (ajp_msg_append_uint8(msg, SC_A_REMOTE_USER) ||
ajp_msg_append_string(msg, r->user)) {
}
/* XXX: Is the subprocess_env a right place?
* <Location /examples>
- * ProxyPass ajp://remote:8009/servlets-examples
+ * ProxyPass ajp://remote:8009/servlets-examples
* SetEnv SSL_SESSION_ID CUSTOM_SSL_SESSION_ID
* </Location>
*/
}
}
}
+ /* Forward the remote port information, which was forgotten
+ * from the builtin data of the AJP 13 protocol.
+ * Since the servlet spec allows to retrieve it via getRemotePort(),
+ * we provide the port to the Tomcat connector as a request
+ * attribute. Modern Tomcat versions know how to retrieve
+ * the remote port from this attribute.
+ */
+ {
+ const char *key = SC_A_REQ_REMOTE_PORT;
+ char *val = apr_itoa(r->pool, r->connection->remote_addr->port);
+ if (ajp_msg_append_uint8(msg, SC_A_REQ_ATTRIBUTE) ||
+ ajp_msg_append_string(msg, key) ||
+ ajp_msg_append_string(msg, val)) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+ "ajp_marshal_into_msgb: "
+ "Error appending attribute %s=%s",
+ key, val);
+ return AJP_EOVERFLOW;
+ }
+ }
/* Use the environment vars prefixed with AJP_
* and pass it to the header striping that prefix.
*/
return AJP_EOVERFLOW;
}
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ ap_log_error(APLOG_MARK, APLOG_TRACE8, 0, r->server,
"ajp_marshal_into_msgb: Done");
return APR_SUCCESS;
}
*body_chunk
terminator boolean <! -- recycle connection or not -->
-req_header_name :=
+req_header_name :=
sc_req_header_name | (string)
-res_header_name :=
+res_header_name :=
sc_res_header_name | (string)
header_value :=
*/
+static int addit_dammit(void *v, const char *key, const char *val)
+{
+ apr_table_addn(v, key, val);
+ return 1;
+}
static apr_status_t ajp_unmarshal_response(ajp_msg_t *msg,
request_rec *r,
rc = ajp_msg_get_string(msg, &ptr);
if (rc == APR_SUCCESS) {
- r->status_line = apr_psprintf(r->pool, "%d %s", status, ptr);
-#if defined(AS400) || defined(_OSD_POSIX)
- ap_xlate_proto_from_ascii(r->status_line, strlen(r->status_line));
+#if APR_CHARSET_EBCDIC /* copy only if we have to */
+ ptr = apr_pstrdup(r->pool, ptr);
+ ap_xlate_proto_from_ascii(ptr, strlen(ptr));
#endif
+ r->status_line = apr_psprintf(r->pool, "%d %s", status, ptr);
} else {
r->status_line = NULL;
}
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, r->server,
"ajp_unmarshal_response: status = %d", status);
rc = ajp_msg_get_uint16(msg, &num_headers);
if (rc == APR_SUCCESS) {
- r->headers_out = apr_table_make(r->pool, num_headers);
+ apr_table_t *save_table;
+
+ /* First, tuck away all already existing cookies */
+ /*
+ * Could optimize here, but just in case we want to
+ * also save other headers, keep this logic.
+ */
+ save_table = apr_table_make(r->pool, num_headers + 2);
+ apr_table_do(addit_dammit, save_table, r->headers_out,
+ "Set-Cookie", NULL);
+ r->headers_out = save_table;
} else {
r->headers_out = NULL;
num_headers = 0;
}
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
+ ap_log_error(APLOG_MARK, APLOG_TRACE4, 0, r->server,
"ajp_unmarshal_response: Number of headers is = %d",
num_headers);
if (rc != APR_SUCCESS) {
return rc;
}
-
+
if ((name & 0XFF00) == 0XA000) {
- ajp_msg_peek_uint16(msg, &name);
+ ajp_msg_get_uint16(msg, &name);
stringname = long_res_header_for_sc(name);
if (stringname == NULL) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"Null header name");
return rc;
}
-#if defined(AS400) || defined(_OSD_POSIX)
ap_xlate_proto_from_ascii(stringname, strlen(stringname));
-#endif
}
rc = ajp_msg_get_string(msg, &value);
value = ap_proxy_location_reverse_map(r, dconf, value);
}
-#if defined(AS400) || defined(_OSD_POSIX)
ap_xlate_proto_from_ascii(value, strlen(value));
-#endif
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "ajp_unmarshal_response: Header[%d] [%s] = [%s]",
+ ap_log_error(APLOG_MARK, APLOG_TRACE5, 0, r->server,
+ "ajp_unmarshal_response: Header[%d] [%s] = [%s]",
i, stringname, value);
apr_table_add(r->headers_out, stringname, value);
/* Content-type needs an additional handling */
- if (memcmp(stringname, "Content-Type", 12) == 0) {
+ if (strcasecmp(stringname, "Content-Type") == 0) {
/* add corresponding filter */
ap_set_content_type(r, apr_pstrdup(r->pool, value));
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "ajp_unmarshal_response: ap_set_content_type done");
+ ap_log_error(APLOG_MARK, APLOG_TRACE5, 0, r->server,
+ "ajp_unmarshal_response: ap_set_content_type to '%s'", value);
}
}
*/
apr_status_t ajp_send_header(apr_socket_t *sock,
request_rec *r,
+ apr_size_t buffsize,
apr_uri_t *uri)
{
ajp_msg_t *msg;
apr_status_t rc;
- rc = ajp_msg_create(r->pool, &msg);
+ rc = ajp_msg_create(r->pool, buffsize, &msg);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"ajp_send_header: ajp_msg_create failed");
return rc;
}
- rc = ajp_marshal_into_msgb(msg, r, uri);
+ rc = ajp_marshal_into_msgb(msg, r, uri);
if (rc != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
"ajp_send_header: ajp_marshal_into_msgb failed");
*/
apr_status_t ajp_read_header(apr_socket_t *sock,
request_rec *r,
+ apr_size_t buffsize,
ajp_msg_t **msg)
{
apr_byte_t result;
apr_status_t rc;
-
- rc = ajp_msg_create(r->pool, msg);
- if (rc != APR_SUCCESS) {
- ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
- "ajp_read_header: ajp_msg_create failed");
- return rc;
+
+ if (*msg) {
+ rc = ajp_msg_reuse(*msg);
+ if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+ "ajp_read_header: ajp_msg_reuse failed");
+ return rc;
+ }
+ }
+ else {
+ rc = ajp_msg_create(r->pool, buffsize, msg);
+ if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+ "ajp_read_header: ajp_msg_create failed");
+ return rc;
+ }
}
ajp_msg_reset(*msg);
rc = ajp_ilink_receive(sock, *msg);
return rc;
}
rc = ajp_msg_peek_uint8(*msg, &result);
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "ajp_read_header: ajp_ilink_received %02x", result);
+ ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, r->server,
+ "ajp_read_header: ajp_ilink_received 0x%02x", result);
return APR_SUCCESS;
}
{
apr_byte_t result;
ajp_msg_peek_uint8(msg, &result);
- ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
- "ajp_parse_type: got %02x", result);
+ ap_log_error(APLOG_MARK, APLOG_TRACE6, 0, r->server,
+ "ajp_parse_type: got 0x%02x", result);
return (int) result;
}
}
if (result != CMD_AJP13_SEND_HEADERS) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
- "ajp_parse_headers: wrong type %02x expecting 0x04", result);
+ "ajp_parse_headers: wrong type 0x%02x expecting 0x%02x",
+ result, CMD_AJP13_SEND_HEADERS);
return AJP_EBAD_HEADER;
}
return ajp_unmarshal_response(msg, r, conf);
{
apr_byte_t result;
apr_status_t rc;
+ apr_uint16_t expected_len;
rc = ajp_msg_get_uint8(msg, &result);
if (rc != APR_SUCCESS) {
}
if (result != CMD_AJP13_SEND_BODY_CHUNK) {
ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
- "ajp_parse_data: wrong type %02x expecting 0x03", result);
+ "ajp_parse_data: wrong type 0x%02x expecting 0x%02x",
+ result, CMD_AJP13_SEND_BODY_CHUNK);
return AJP_EBAD_HEADER;
}
rc = ajp_msg_get_uint16(msg, len);
if (rc != APR_SUCCESS) {
return rc;
}
+ /*
+ * msg->len contains the complete length of the message including all
+ * headers. So the expected length for a CMD_AJP13_SEND_BODY_CHUNK is
+ * msg->len minus the sum of
+ * AJP_HEADER_LEN : The length of the header to every AJP message.
+ * AJP_HEADER_SZ_LEN : The header giving the size of the chunk.
+ * 1 : The CMD_AJP13_SEND_BODY_CHUNK indicator byte (0x03).
+ * 1 : The last byte of this message always seems to be
+ * 0x00 and is not part of the chunk.
+ */
+ expected_len = msg->len - (AJP_HEADER_LEN + AJP_HEADER_SZ_LEN + 1 + 1);
+ if (*len != expected_len) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+ "ajp_parse_data: Wrong chunk length. Length of chunk is %i,"
+ " expected length is %i.", *len, expected_len);
+ return AJP_EBAD_HEADER;
+ }
*ptr = (char *)&(msg->buf[msg->pos]);
return APR_SUCCESS;
}
+/* Check the reuse flag in CMD_AJP13_END_RESPONSE */
+apr_status_t ajp_parse_reuse(request_rec *r, ajp_msg_t *msg,
+ apr_byte_t *reuse)
+{
+ apr_byte_t result;
+ apr_status_t rc;
+
+ rc = ajp_msg_get_uint8(msg, &result);
+ if (rc != APR_SUCCESS) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+ "ajp_parse_reuse: ajp_msg_get_byte failed");
+ return rc;
+ }
+ if (result != CMD_AJP13_END_RESPONSE) {
+ ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
+ "ajp_parse_reuse: wrong type 0x%02x expecting 0x%02x",
+ result, CMD_AJP13_END_RESPONSE);
+ return AJP_EBAD_HEADER;
+ }
+ return ajp_msg_get_uint8(msg, reuse);
+}
+
/*
* Allocate a msg to send data
*/
{
apr_status_t rc;
- if ((rc = ajp_msg_create(pool, msg)) != APR_SUCCESS)
+ if ((rc = ajp_msg_create(pool, *len, msg)) != APR_SUCCESS)
return rc;
ajp_msg_reset(*msg);
*ptr = (char *)&((*msg)->buf[6]);
- *len = AJP_MSG_BUFFER_SZ-6;
+ *len = *len - 6;
return APR_SUCCESS;
}