1 /* Copyright 1999-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
19 #include "apr_version.h"
22 #include "apr_hooks.h"
24 #include "apr_strings.h"
25 #include "apr_buckets.h"
27 #include "apr_network_io.h"
28 #include "apr_pools.h"
29 #include "apr_strings.h"
32 #include "apr_fnmatch.h"
33 #define APR_WANT_STRFUNC
36 #if APR_HAVE_NETINET_IN_H
37 #include <netinet/in.h>
39 #if APR_HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
43 #define AJP13_DEF_HOST "127.0.0.1"
45 #define AJP13_DEF_PORT 9009 /* default to 9009 since 8009 is used by OS */
47 #define AJP13_DEF_PORT 8009
50 /* The following environment variables match mod_ssl! */
51 #define AJP13_HTTPS_INDICATOR "HTTPS"
52 #define AJP13_SSL_CLIENT_CERT_INDICATOR "SSL_CLIENT_CERT"
53 #define AJP13_SSL_CIPHER_INDICATOR "SSL_CIPHER"
54 #define AJP13_SSL_SESSION_INDICATOR "SSL_SESSION_ID"
55 #define AJP13_SSL_KEY_SIZE_INDICATOR "SSL_CIPHER_USEKEYSIZE"
57 #if APR_CHARSET_EBCDIC
59 #define USE_CHARSET_EBCDIC
60 #define ajp_xlate_to_ascii(b, l) ap_xlate_proto_to_ascii(b, l)
61 #define ajp_xlate_from_ascii(b, l) ap_xlate_proto_from_ascii(b, l)
63 #else /* APR_CHARSET_EBCDIC */
65 #define ajp_xlate_to_ascii(b, l)
66 #define ajp_xlate_from_ascii(b, l)
70 #ifdef AJP_USE_HTTPD_WRAP
71 #include "httpd_wrap.h"
74 #include "http_config.h"
75 #include "http_request.h"
76 #include "http_core.h"
77 #include "http_protocol.h"
78 #include "http_main.h"
83 /** AJP Specific error codes
85 /** Buffer overflow exception */
86 #define AJP_EOVERFLOW (APR_OS_START_USERERR + 1)
87 /** Destination Buffer is to small */
88 #define AJP_ETOSMALL (APR_OS_START_USERERR + 2)
89 /** Invalid input parameters */
90 #define AJP_EINVAL (APR_OS_START_USERERR + 3)
91 /** Bad message signature */
92 #define AJP_EBAD_SIGNATURE (APR_OS_START_USERERR + 4)
93 /** Incoming message too bg */
94 #define AJP_ETOBIG (APR_OS_START_USERERR + 5)
95 /** Missing message header */
96 #define AJP_ENO_HEADER (APR_OS_START_USERERR + 6)
97 /** Bad message header */
98 #define AJP_EBAD_HEADER (APR_OS_START_USERERR + 7)
100 #define AJP_EBAD_MESSAGE (APR_OS_START_USERERR + 8)
101 /** Cant log via AJP14 */
102 #define AJP_ELOGFAIL (APR_OS_START_USERERR + 9)
104 /** A structure that represents ajp message */
105 typedef struct ajp_msg ajp_msg_t;
107 /** A structure that represents ajp message */
110 /** The buffer holding a AJP message */
112 /** The length of AJP message header (defaults to AJP_HEADER_LEN) */
113 apr_size_t header_len;
114 /** The length of AJP message */
116 /** The current read position */
118 /** Flag indicating the origing of the message */
123 * @defgroup AJP_defines AJP definitions
127 * Signature for the messages sent from Apache to tomcat
129 #define AJP13_WS_HEADER 0x1234
130 #define AJP_HEADER_LEN 4
131 #define AJP_HEADER_SZ_LEN 2
132 #define AJP_MSG_BUFFER_SZ (8*1024)
133 #define AJP13_MAX_SEND_BODY_SZ (AJP_MSG_BUFFER_SZ - 6)
135 /** Send a request from web server to container*/
136 #define CMD_AJP13_FORWARD_REQUEST (unsigned char)2
137 /** Write a body chunk from the servlet container to the web server */
138 #define CMD_AJP13_SEND_BODY_CHUNK (unsigned char)3
139 /** Send response headers from the servlet container to the web server. */
140 #define CMD_AJP13_SEND_HEADERS (unsigned char)4
141 /** Marks the end of response. */
142 #define CMD_AJP13_END_RESPONSE (unsigned char)5
143 /** Get further data from the web server if it hasn't all been transferred yet. */
144 #define CMD_AJP13_GET_BODY_CHUNK (unsigned char)6
145 /** The web server asks the container to shut itself down. */
146 #define CMD_AJP13_SHUTDOWN (unsigned char)7
147 /** Webserver ask container to take control (logon phase) */
148 #define CMD_AJP13_PING (unsigned char)8
149 /** Container response to cping request */
150 #define CMD_AJP13_CPONG (unsigned char)9
151 /** Webserver check if container is alive, since container should respond by cpong */
152 #define CMD_AJP13_CPING (unsigned char)10
157 * @defgroup AJP_api AJP API functions
161 * Check a new AJP Message by looking at signature and return its size
163 * @param msg AJP Message to check
164 * @param len Pointer to returned len
165 * @return APR_SUCCESS or error
167 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len);
170 * Reset an AJP Message
172 * @param msg AJP Message to reset
173 * @return APR_SUCCESS or error
175 apr_status_t ajp_msg_reset(ajp_msg_t *msg);
178 * Mark the end of an AJP Message
180 * @param msg AJP Message to end
181 * @return APR_SUCCESS or error
183 apr_status_t ajp_msg_end(ajp_msg_t *msg);
186 * Add an unsigned 32bits value to AJP Message
188 * @param msg AJP Message to get value from
189 * @param value value to add to AJP Message
190 * @return APR_SUCCESS or error
192 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
195 * Add an unsigned 16bits value to AJP Message
197 * @param msg AJP Message to get value from
198 * @param value value to add to AJP Message
199 * @return APR_SUCCESS or error
201 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
204 * Add an unsigned 8bits value to AJP Message
206 * @param msg AJP Message to get value from
207 * @param value value to add to AJP Message
208 * @return APR_SUCCESS or error
210 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
213 * Add a String in AJP message, and transform the String in ASCII
214 * if convert is set and we're on an EBCDIC machine
216 * @param msg AJP Message to get value from
217 * @param value Pointer to String
218 * @param convert When set told to convert String to ASCII
219 * @return APR_SUCCESS or error
221 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
224 * Add a String in AJP message, and transform
225 * the String in ASCII if we're on an EBCDIC machine
227 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
230 * Add a String in AJP message.
232 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
235 * Add a Byte array to AJP Message
237 * @param msg AJP Message to get value from
238 * @param value Pointer to Byte array
239 * @param valuelen Byte array len
240 * @return APR_SUCCESS or error
242 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
243 apr_size_t valuelen);
246 * Get a 32bits unsigned value from AJP Message
248 * @param msg AJP Message to get value from
249 * @param rvalue Pointer where value will be returned
250 * @return APR_SUCCESS or error
252 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
255 * Get a 16bits unsigned value from AJP Message
257 * @param msg AJP Message to get value from
258 * @param rvalue Pointer where value will be returned
259 * @return APR_SUCCESS or error
261 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
264 * Peek a 16bits unsigned value from AJP Message, position in message
267 * @param msg AJP Message to get value from
268 * @param rvalue Pointer where value will be returned
269 * @return APR_SUCCESS or error
271 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
274 * Get a 8bits unsigned value from AJP Message
276 * @param msg AJP Message to get value from
277 * @param rvalue Pointer where value will be returned
278 * @return APR_SUCCESS or error
280 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
283 * Peek a 8bits unsigned value from AJP Message, position in message
286 * @param msg AJP Message to get value from
287 * @param rvalue Pointer where value will be returned
288 * @return APR_SUCCESS or error
290 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
293 * Get a String value from AJP Message
295 * @param msg AJP Message to get value from
296 * @param rvalue Pointer where value will be returned
297 * @return APR_SUCCESS or error
299 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, char **rvalue);
303 * Get a Byte array from AJP Message
305 * @param msg AJP Message to get value from
306 * @param rvalue Pointer where value will be returned
307 * @param rvalueLen Pointer where Byte array len will be returned
308 * @return APR_SUCCESS or error
310 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
311 apr_size_t *rvalue_len);
314 * Create an AJP Message from pool
316 * @param pool memory pool to allocate AJP message from
317 * @param rmsg Pointer to newly created AJP message
318 * @return APR_SUCCESS or error
320 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg);
323 * Recopy an AJP Message to another
325 * @param smsg source AJP message
326 * @param dmsg destination AJP message
327 * @return APR_SUCCESS or error
329 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
332 * Serialize in an AJP Message a PING command
334 * +-----------------------+
335 * | PING CMD (1 byte) |
336 * +-----------------------+
338 * @param smsg AJP message to put serialized message
339 * @return APR_SUCCESS or error
341 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
344 * Serialize in an AJP Message a CPING command
346 * +-----------------------+
347 * | CPING CMD (1 byte) |
348 * +-----------------------+
350 * @param smsg AJP message to put serialized message
351 * @return APR_SUCCESS or error
353 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
356 * Dump up to the first 1024 bytes on an AJP Message
358 * @param msg AJP Message to dump
359 * @param err error string to display
360 * @return APR_SUCCESS or error
362 apr_status_t ajp_msg_dump(ajp_msg_t *msg, char *err);
365 * Send an AJP message to backend
367 * @param soct backend socket
368 * @param smsg AJP message to put serialized message
369 * @return APR_SUCCESS or error
371 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
374 * Receive an AJP message from backend
376 * @param sock backend socket
377 * @param smsg AJP message to put serialized message
378 * @return APR_SUCCESS or error
380 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
383 * Build the ajp header message and send it
384 * @param sock backend socket
385 * @param r current request
386 * @return APR_SUCCESS or error
388 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r);
391 * Read the ajp message and return the type of the message.
392 * @param sock backend socket
393 * @param r current request
394 * @param msg returned AJP message
395 * @return APR_SUCCESS or error
397 apr_status_t ajp_read_header(apr_socket_t *sock,
402 * Allocate a msg to send data
403 * @param r current request
404 * @param ptr data buffer
405 * @param len the length of allocated data buffer
406 * @param msg returned AJP message
407 * @return APR_SUCCESS or error
409 apr_status_t ajp_alloc_data_msg(request_rec *r, char **ptr, apr_size_t *len,
413 * Send the data message
414 * @param sock backend socket
415 * @param r current request
416 * @param msg AJP message to send
417 * @param len AJP message length
418 * @return APR_SUCCESS or error
420 apr_status_t ajp_send_data_msg(apr_socket_t *sock, request_rec *r,
421 ajp_msg_t *msg, apr_size_t len);
424 * Parse the message type
425 * @param r current request
426 * @param msg AJP message
427 * @return AJP message type.
429 int ajp_parse_type(request_rec *r, ajp_msg_t *msg);
432 * Parse the header message from container
433 * @param r current request
434 * @param msg AJP message
435 * @return APR_SUCCESS or error
437 apr_status_t ajp_parse_header(request_rec *r, ajp_msg_t *msg);
440 * Parse the message body and return data address and length
441 * @param r current request
442 * @param msg AJP message
443 * @param len returned AJP message length
444 * @param ptr returned data
445 * @return APR_SUCCESS or error
447 apr_status_t ajp_parse_data(request_rec *r, ajp_msg_t *msg,
448 apr_uint16_t *len, char **ptr);