1 /* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include "apr_version.h"
23 #include "apr_hooks.h"
25 #include "apr_strings.h"
26 #include "apr_buckets.h"
28 #include "apr_network_io.h"
29 #include "apr_pools.h"
30 #include "apr_strings.h"
33 #include "apr_fnmatch.h"
34 #define APR_WANT_STRFUNC
37 #if APR_HAVE_NETINET_IN_H
38 #include <netinet/in.h>
40 #if APR_HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
44 #define AJP13_DEF_HOST "127.0.0.1"
46 #define AJP13_DEF_PORT 9009 /* default to 9009 since 8009 is used by OS */
48 #define AJP13_DEF_PORT 8009
51 /* The following environment variables match mod_ssl! */
52 #define AJP13_HTTPS_INDICATOR "HTTPS"
53 #define AJP13_SSL_CLIENT_CERT_INDICATOR "SSL_CLIENT_CERT"
54 #define AJP13_SSL_CIPHER_INDICATOR "SSL_CIPHER"
55 #define AJP13_SSL_SESSION_INDICATOR "SSL_SESSION_ID"
56 #define AJP13_SSL_KEY_SIZE_INDICATOR "SSL_CIPHER_USEKEYSIZE"
58 #if APR_CHARSET_EBCDIC
60 #define USE_CHARSET_EBCDIC
61 #define ajp_xlate_to_ascii(b, l) ap_xlate_proto_to_ascii(b, l)
62 #define ajp_xlate_from_ascii(b, l) ap_xlate_proto_from_ascii(b, l)
64 #else /* APR_CHARSET_EBCDIC */
66 #define ajp_xlate_to_ascii(b, l)
67 #define ajp_xlate_from_ascii(b, l)
71 #ifdef AJP_USE_HTTPD_WRAP
72 #include "httpd_wrap.h"
75 #include "http_config.h"
76 #include "http_request.h"
77 #include "http_core.h"
78 #include "http_protocol.h"
79 #include "http_main.h"
83 #include "mod_proxy.h"
86 /** AJP Specific error codes
88 /** Buffer overflow exception */
89 #define AJP_EOVERFLOW (APR_OS_START_USERERR + 1)
90 /** Destination Buffer is to small */
91 #define AJP_ETOSMALL (APR_OS_START_USERERR + 2)
92 /** Invalid input parameters */
93 #define AJP_EINVAL (APR_OS_START_USERERR + 3)
94 /** Bad message signature */
95 #define AJP_EBAD_SIGNATURE (APR_OS_START_USERERR + 4)
96 /** Incoming message too bg */
97 #define AJP_ETOBIG (APR_OS_START_USERERR + 5)
98 /** Missing message header */
99 #define AJP_ENO_HEADER (APR_OS_START_USERERR + 6)
100 /** Bad message header */
101 #define AJP_EBAD_HEADER (APR_OS_START_USERERR + 7)
103 #define AJP_EBAD_MESSAGE (APR_OS_START_USERERR + 8)
104 /** Cant log via AJP14 */
105 #define AJP_ELOGFAIL (APR_OS_START_USERERR + 9)
107 /** A structure that represents ajp message */
108 typedef struct ajp_msg ajp_msg_t;
110 /** A structure that represents ajp message */
113 /** The buffer holding a AJP message */
115 /** The length of AJP message header (defaults to AJP_HEADER_LEN) */
116 apr_size_t header_len;
117 /** The length of AJP message */
119 /** The current read position */
121 /** Flag indicating the origing of the message */
126 * @defgroup AJP_defines AJP definitions
130 * Signature for the messages sent from Apache to tomcat
132 #define AJP13_WS_HEADER 0x1234
133 #define AJP_HEADER_LEN 4
134 #define AJP_HEADER_SZ_LEN 2
135 #define AJP_MSG_BUFFER_SZ (8*1024)
136 #define AJP13_MAX_SEND_BODY_SZ (AJP_MSG_BUFFER_SZ - 6)
138 /** Send a request from web server to container*/
139 #define CMD_AJP13_FORWARD_REQUEST (unsigned char)2
140 /** Write a body chunk from the servlet container to the web server */
141 #define CMD_AJP13_SEND_BODY_CHUNK (unsigned char)3
142 /** Send response headers from the servlet container to the web server. */
143 #define CMD_AJP13_SEND_HEADERS (unsigned char)4
144 /** Marks the end of response. */
145 #define CMD_AJP13_END_RESPONSE (unsigned char)5
146 /** Get further data from the web server if it hasn't all been transferred yet. */
147 #define CMD_AJP13_GET_BODY_CHUNK (unsigned char)6
148 /** The web server asks the container to shut itself down. */
149 #define CMD_AJP13_SHUTDOWN (unsigned char)7
150 /** Webserver ask container to take control (logon phase) */
151 #define CMD_AJP13_PING (unsigned char)8
152 /** Container response to cping request */
153 #define CMD_AJP13_CPONG (unsigned char)9
154 /** Webserver check if container is alive, since container should respond by cpong */
155 #define CMD_AJP13_CPING (unsigned char)10
160 * @defgroup AJP_api AJP API functions
164 * Check a new AJP Message by looking at signature and return its size
166 * @param msg AJP Message to check
167 * @param len Pointer to returned len
168 * @return APR_SUCCESS or error
170 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len);
173 * Reset an AJP Message
175 * @param msg AJP Message to reset
176 * @return APR_SUCCESS or error
178 apr_status_t ajp_msg_reset(ajp_msg_t *msg);
181 * Mark the end of an AJP Message
183 * @param msg AJP Message to end
184 * @return APR_SUCCESS or error
186 apr_status_t ajp_msg_end(ajp_msg_t *msg);
189 * Add an unsigned 32bits value to AJP Message
191 * @param msg AJP Message to get value from
192 * @param value value to add to AJP Message
193 * @return APR_SUCCESS or error
195 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
198 * Add an unsigned 16bits value to AJP Message
200 * @param msg AJP Message to get value from
201 * @param value value to add to AJP Message
202 * @return APR_SUCCESS or error
204 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
207 * Add an unsigned 8bits value to AJP Message
209 * @param msg AJP Message to get value from
210 * @param value value to add to AJP Message
211 * @return APR_SUCCESS or error
213 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
216 * Add a String in AJP message, and transform the String in ASCII
217 * if convert is set and we're on an EBCDIC machine
219 * @param msg AJP Message to get value from
220 * @param value Pointer to String
221 * @param convert When set told to convert String to ASCII
222 * @return APR_SUCCESS or error
224 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
227 * Add a String in AJP message, and transform
228 * the String in ASCII if we're on an EBCDIC machine
230 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
233 * Add a String in AJP message.
235 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
238 * Add a Byte array to AJP Message
240 * @param msg AJP Message to get value from
241 * @param value Pointer to Byte array
242 * @param valuelen Byte array len
243 * @return APR_SUCCESS or error
245 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
246 apr_size_t valuelen);
249 * Get a 32bits unsigned value from AJP Message
251 * @param msg AJP Message to get value from
252 * @param rvalue Pointer where value will be returned
253 * @return APR_SUCCESS or error
255 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
258 * Get a 16bits unsigned value from AJP Message
260 * @param msg AJP Message to get value from
261 * @param rvalue Pointer where value will be returned
262 * @return APR_SUCCESS or error
264 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
267 * Peek a 16bits unsigned value from AJP Message, position in message
270 * @param msg AJP Message to get value from
271 * @param rvalue Pointer where value will be returned
272 * @return APR_SUCCESS or error
274 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
277 * Get a 8bits unsigned value from AJP Message
279 * @param msg AJP Message to get value from
280 * @param rvalue Pointer where value will be returned
281 * @return APR_SUCCESS or error
283 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
286 * Peek a 8bits unsigned value from AJP Message, position in message
289 * @param msg AJP Message to get value from
290 * @param rvalue Pointer where value will be returned
291 * @return APR_SUCCESS or error
293 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
296 * Get a String value from AJP Message
298 * @param msg AJP Message to get value from
299 * @param rvalue Pointer where value will be returned
300 * @return APR_SUCCESS or error
302 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue);
306 * Get a Byte array from AJP Message
308 * @param msg AJP Message to get value from
309 * @param rvalue Pointer where value will be returned
310 * @param rvalueLen Pointer where Byte array len will be returned
311 * @return APR_SUCCESS or error
313 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
314 apr_size_t *rvalue_len);
317 * Create an AJP Message from pool
319 * @param pool memory pool to allocate AJP message from
320 * @param rmsg Pointer to newly created AJP message
321 * @return APR_SUCCESS or error
323 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg);
326 * Recopy an AJP Message to another
328 * @param smsg source AJP message
329 * @param dmsg destination AJP message
330 * @return APR_SUCCESS or error
332 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
335 * Serialize in an AJP Message a PING command
337 * +-----------------------+
338 * | PING CMD (1 byte) |
339 * +-----------------------+
341 * @param smsg AJP message to put serialized message
342 * @return APR_SUCCESS or error
344 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
347 * Serialize in an AJP Message a CPING command
349 * +-----------------------+
350 * | CPING CMD (1 byte) |
351 * +-----------------------+
353 * @param smsg AJP message to put serialized message
354 * @return APR_SUCCESS or error
356 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
359 * Dump up to the first 1024 bytes on an AJP Message
361 * @param pool pool to allocate from
362 * @param msg AJP Message to dump
363 * @param err error string to display
364 * @return dump message
366 char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err);
369 * Send an AJP message to backend
371 * @param soct backend socket
372 * @param smsg AJP message to put serialized message
373 * @return APR_SUCCESS or error
375 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
378 * Receive an AJP message from backend
380 * @param sock backend socket
381 * @param smsg AJP message to put serialized message
382 * @return APR_SUCCESS or error
384 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
387 * Build the ajp header message and send it
388 * @param sock backend socket
389 * @param r current request
390 * @uri uri requested uri
391 * @return APR_SUCCESS or error
393 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r,
397 * Read the ajp message and return the type of the message.
398 * @param sock backend socket
399 * @param r current request
400 * @param msg returned AJP message
401 * @return APR_SUCCESS or error
403 apr_status_t ajp_read_header(apr_socket_t *sock,
408 * Allocate a msg to send data
409 * @param pool pool to allocate from
410 * @param ptr data buffer
411 * @param len the length of allocated data buffer
412 * @param msg returned AJP message
413 * @return APR_SUCCESS or error
415 apr_status_t ajp_alloc_data_msg(apr_pool_t *pool, char **ptr,
416 apr_size_t *len, ajp_msg_t **msg);
419 * Send the data message
420 * @param sock backend socket
421 * @param msg AJP message to send
422 * @param len AJP message length
423 * @return APR_SUCCESS or error
425 apr_status_t ajp_send_data_msg(apr_socket_t *sock,
426 ajp_msg_t *msg, apr_size_t len);
429 * Parse the message type
430 * @param r current request
431 * @param msg AJP message
432 * @return AJP message type.
434 int ajp_parse_type(request_rec *r, ajp_msg_t *msg);
437 * Parse the header message from container
438 * @param r current request
439 * @param msg AJP message
440 * @return APR_SUCCESS or error
442 apr_status_t ajp_parse_header(request_rec *r, proxy_server_conf *conf,
446 * Parse the message body and return data address and length
447 * @param r current request
448 * @param msg AJP message
449 * @param len returned AJP message length
450 * @param ptr returned data
451 * @return APR_SUCCESS or error
453 apr_status_t ajp_parse_data(request_rec *r, ajp_msg_t *msg,
454 apr_uint16_t *len, char **ptr);