1 /* Copyright 1999-2006 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.
19 * @brief Apache Jserv Protocol
21 * @defgroup AJP_defines AJP definitions
29 #include "apr_version.h"
32 #include "apr_hooks.h"
34 #include "apr_strings.h"
35 #include "apr_buckets.h"
37 #include "apr_network_io.h"
39 #include "apr_pools.h"
40 #include "apr_strings.h"
43 #include "apr_fnmatch.h"
44 #define APR_WANT_STRFUNC
47 #if APR_HAVE_NETINET_IN_H
48 #include <netinet/in.h>
50 #if APR_HAVE_ARPA_INET_H
51 #include <arpa/inet.h>
54 #define AJP13_DEF_HOST "127.0.0.1"
56 #define AJP13_DEF_PORT 9009 /* default to 9009 since 8009 is used by OS */
58 #define AJP13_DEF_PORT 8009
61 /* The following environment variables match mod_ssl! */
62 #define AJP13_HTTPS_INDICATOR "HTTPS"
63 #define AJP13_SSL_CLIENT_CERT_INDICATOR "SSL_CLIENT_CERT"
64 #define AJP13_SSL_CIPHER_INDICATOR "SSL_CIPHER"
65 #define AJP13_SSL_SESSION_INDICATOR "SSL_SESSION_ID"
66 #define AJP13_SSL_KEY_SIZE_INDICATOR "SSL_CIPHER_USEKEYSIZE"
68 #ifdef AJP_USE_HTTPD_WRAP
69 #include "httpd_wrap.h"
72 #include "http_config.h"
73 #include "http_request.h"
74 #include "http_core.h"
75 #include "http_protocol.h"
76 #include "http_main.h"
80 #include "mod_proxy.h"
81 #include "util_ebcdic.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)
103 /** Bad request method */
104 #define AJP_EBAD_METHOD (APR_OS_START_USERERR + 10)
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 * Signature for the messages sent from Apache to tomcat
128 #define AJP13_WS_HEADER 0x1234
129 #define AJP_HEADER_LEN 4
130 #define AJP_HEADER_SZ_LEN 2
131 #define AJP_MSG_BUFFER_SZ (8*1024)
132 #define AJP13_MAX_SEND_BODY_SZ (AJP_MSG_BUFFER_SZ - 6)
134 /** Send a request from web server to container*/
135 #define CMD_AJP13_FORWARD_REQUEST (unsigned char)2
136 /** Write a body chunk from the servlet container to the web server */
137 #define CMD_AJP13_SEND_BODY_CHUNK (unsigned char)3
138 /** Send response headers from the servlet container to the web server. */
139 #define CMD_AJP13_SEND_HEADERS (unsigned char)4
140 /** Marks the end of response. */
141 #define CMD_AJP13_END_RESPONSE (unsigned char)5
142 /** Get further data from the web server if it hasn't all been transferred yet. */
143 #define CMD_AJP13_GET_BODY_CHUNK (unsigned char)6
144 /** The web server asks the container to shut itself down. */
145 #define CMD_AJP13_SHUTDOWN (unsigned char)7
146 /** Webserver ask container to take control (logon phase) */
147 #define CMD_AJP13_PING (unsigned char)8
148 /** Container response to cping request */
149 #define CMD_AJP13_CPONG (unsigned char)9
150 /** Webserver check if container is alive, since container should respond by cpong */
151 #define CMD_AJP13_CPING (unsigned char)10
156 * @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 * Reuse an AJP Message
180 * @param msg AJP Message to reuse
181 * @return APR_SUCCESS or error
183 apr_status_t ajp_msg_reuse(ajp_msg_t *msg);
186 * Mark the end of an AJP Message
188 * @param msg AJP Message to end
189 * @return APR_SUCCESS or error
191 apr_status_t ajp_msg_end(ajp_msg_t *msg);
194 * Add an unsigned 32bits value to AJP Message
196 * @param msg AJP Message to get value from
197 * @param value value to add to AJP Message
198 * @return APR_SUCCESS or error
200 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
203 * Add an unsigned 16bits value to AJP Message
205 * @param msg AJP Message to get value from
206 * @param value value to add to AJP Message
207 * @return APR_SUCCESS or error
209 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
212 * Add an unsigned 8bits value to AJP Message
214 * @param msg AJP Message to get value from
215 * @param value value to add to AJP Message
216 * @return APR_SUCCESS or error
218 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
221 * Add a String in AJP message, and transform the String in ASCII
222 * if convert is set and we're on an EBCDIC machine
224 * @param msg AJP Message to get value from
225 * @param value Pointer to String
226 * @param convert When set told to convert String to ASCII
227 * @return APR_SUCCESS or error
229 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
232 * Add a String in AJP message, and transform
233 * the String in ASCII if we're on an EBCDIC machine
235 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
238 * Add a String in AJP message.
240 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
243 * Add a Byte array to AJP Message
245 * @param msg AJP Message to get value from
246 * @param value Pointer to Byte array
247 * @param valuelen Byte array len
248 * @return APR_SUCCESS or error
250 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
251 apr_size_t valuelen);
254 * Get a 32bits unsigned value from AJP Message
256 * @param msg AJP Message to get value from
257 * @param rvalue Pointer where value will be returned
258 * @return APR_SUCCESS or error
260 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
263 * Get a 16bits unsigned value from AJP Message
265 * @param msg AJP Message to get value from
266 * @param rvalue Pointer where value will be returned
267 * @return APR_SUCCESS or error
269 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
272 * Peek a 16bits unsigned value from AJP Message, position in message
275 * @param msg AJP Message to get value from
276 * @param rvalue Pointer where value will be returned
277 * @return APR_SUCCESS or error
279 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
282 * Get a 8bits unsigned value from AJP Message
284 * @param msg AJP Message to get value from
285 * @param rvalue Pointer where value will be returned
286 * @return APR_SUCCESS or error
288 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
291 * Peek a 8bits unsigned value from AJP Message, position in message
294 * @param msg AJP Message to get value from
295 * @param rvalue Pointer where value will be returned
296 * @return APR_SUCCESS or error
298 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
301 * Get a String value from AJP Message
303 * @param msg AJP Message to get value from
304 * @param rvalue Pointer where value will be returned
305 * @return APR_SUCCESS or error
307 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue);
311 * Get a Byte array from AJP Message
313 * @param msg AJP Message to get value from
314 * @param rvalue Pointer where value will be returned
315 * @param rvalueLen Pointer where Byte array len will be returned
316 * @return APR_SUCCESS or error
318 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
319 apr_size_t *rvalue_len);
322 * Create an AJP Message from pool
324 * @param pool memory pool to allocate AJP message from
325 * @param rmsg Pointer to newly created AJP message
326 * @return APR_SUCCESS or error
328 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg);
331 * Recopy an AJP Message to another
333 * @param smsg source AJP message
334 * @param dmsg destination AJP message
335 * @return APR_SUCCESS or error
337 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
340 * Serialize in an AJP Message a PING command
342 * +-----------------------+
343 * | PING CMD (1 byte) |
344 * +-----------------------+
346 * @param smsg AJP message to put serialized message
347 * @return APR_SUCCESS or error
349 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
352 * Serialize in an AJP Message a CPING command
354 * +-----------------------+
355 * | CPING CMD (1 byte) |
356 * +-----------------------+
358 * @param smsg AJP message to put serialized message
359 * @return APR_SUCCESS or error
361 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
364 * Dump up to the first 1024 bytes on an AJP Message
366 * @param pool pool to allocate from
367 * @param msg AJP Message to dump
368 * @param err error string to display
369 * @return dump message
371 char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err);
374 * Send an AJP message to backend
376 * @param soct backend socket
377 * @param smsg AJP message to put serialized message
378 * @return APR_SUCCESS or error
380 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
383 * Receive an AJP message from backend
385 * @param sock backend socket
386 * @param smsg AJP message to put serialized message
387 * @return APR_SUCCESS or error
389 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
392 * Build the ajp header message and send it
393 * @param sock backend socket
394 * @param r current request
395 * @uri uri requested uri
396 * @return APR_SUCCESS or error
398 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r,
402 * Read the ajp message and return the type of the message.
403 * @param sock backend socket
404 * @param r current request
405 * @param msg returned AJP message
406 * @return APR_SUCCESS or error
408 apr_status_t ajp_read_header(apr_socket_t *sock,
413 * Allocate a msg to send data
414 * @param pool pool to allocate from
415 * @param ptr data buffer
416 * @param len the length of allocated data buffer
417 * @param msg returned AJP message
418 * @return APR_SUCCESS or error
420 apr_status_t ajp_alloc_data_msg(apr_pool_t *pool, char **ptr,
421 apr_size_t *len, ajp_msg_t **msg);
424 * Send the data message
425 * @param sock backend socket
426 * @param msg AJP message to send
427 * @param len AJP message length
428 * @return APR_SUCCESS or error
430 apr_status_t ajp_send_data_msg(apr_socket_t *sock,
431 ajp_msg_t *msg, apr_size_t len);
434 * Parse the message type
435 * @param r current request
436 * @param msg AJP message
437 * @return AJP message type.
439 int ajp_parse_type(request_rec *r, ajp_msg_t *msg);
442 * Parse the header message from container
443 * @param r current request
444 * @param msg AJP message
445 * @return APR_SUCCESS or error
447 apr_status_t ajp_parse_header(request_rec *r, proxy_dir_conf *conf,
451 * Parse the message body and return data address and length
452 * @param r current request
453 * @param msg AJP message
454 * @param len returned AJP message length
455 * @param ptr returned data
456 * @return APR_SUCCESS or error
458 apr_status_t ajp_parse_data(request_rec *r, ajp_msg_t *msg,
459 apr_uint16_t *len, char **ptr);