1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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 mod_proxy AJP definitions
22 * @ingroup APACHE_INTERNAL
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_PROTOCOL_INDICATOR "SSL_PROTOCOL"
64 #define AJP13_SSL_CLIENT_CERT_INDICATOR "SSL_CLIENT_CERT"
65 #define AJP13_SSL_CIPHER_INDICATOR "SSL_CIPHER"
66 #define AJP13_SSL_SESSION_INDICATOR "SSL_SESSION_ID"
67 #define AJP13_SSL_KEY_SIZE_INDICATOR "SSL_CIPHER_USEKEYSIZE"
69 #ifdef AJP_USE_HTTPD_WRAP
70 #include "httpd_wrap.h"
73 #include "http_config.h"
74 #include "http_request.h"
75 #include "http_core.h"
76 #include "http_protocol.h"
77 #include "http_main.h"
81 #include "mod_proxy.h"
82 #include "util_ebcdic.h"
84 /** AJP Specific error codes
86 /** Buffer overflow exception */
87 #define AJP_EOVERFLOW (APR_OS_START_USERERR + 1)
88 /** Destination Buffer is to small */
89 #define AJP_ETOSMALL (APR_OS_START_USERERR + 2)
90 /** Invalid input parameters */
91 #define AJP_EINVAL (APR_OS_START_USERERR + 3)
92 /** Bad message signature */
93 #define AJP_EBAD_SIGNATURE (APR_OS_START_USERERR + 4)
94 /** Incoming message too bg */
95 #define AJP_ETOBIG (APR_OS_START_USERERR + 5)
96 /** Missing message header */
97 #define AJP_ENO_HEADER (APR_OS_START_USERERR + 6)
98 /** Bad message header */
99 #define AJP_EBAD_HEADER (APR_OS_START_USERERR + 7)
101 #define AJP_EBAD_MESSAGE (APR_OS_START_USERERR + 8)
102 /** Cant log via AJP14 */
103 #define AJP_ELOGFAIL (APR_OS_START_USERERR + 9)
104 /** Bad request method */
105 #define AJP_EBAD_METHOD (APR_OS_START_USERERR + 10)
108 /** A structure that represents ajp message */
109 typedef struct ajp_msg ajp_msg_t;
111 /** A structure that represents ajp message */
114 /** The buffer holding a AJP message */
116 /** The length of AJP message header (defaults to AJP_HEADER_LEN) */
117 apr_size_t header_len;
118 /** The length of AJP message */
120 /** The current read position */
122 /** Flag indicating the origing of the message */
124 /** The size of the buffer */
129 * Signature for the messages sent from Apache to tomcat
131 #define AJP13_WS_HEADER 0x1234
132 #define AJP_HEADER_LEN 4
133 #define AJP_HEADER_SZ_LEN 2
134 #define AJP_HEADER_SZ 6
135 #define AJP_MSG_BUFFER_SZ 8192
136 #define AJP_MAX_BUFFER_SZ 65536
137 #define AJP13_MAX_SEND_BODY_SZ (AJP_MAX_BUFFER_SZ - AJP_HEADER_SZ)
138 #define AJP_PING_PONG_SZ 128
140 /** Send a request from web server to container*/
141 #define CMD_AJP13_FORWARD_REQUEST (unsigned char)2
142 /** Write a body chunk from the servlet container to the web server */
143 #define CMD_AJP13_SEND_BODY_CHUNK (unsigned char)3
144 /** Send response headers from the servlet container to the web server. */
145 #define CMD_AJP13_SEND_HEADERS (unsigned char)4
146 /** Marks the end of response. */
147 #define CMD_AJP13_END_RESPONSE (unsigned char)5
148 /** Get further data from the web server if it hasn't all been transferred yet. */
149 #define CMD_AJP13_GET_BODY_CHUNK (unsigned char)6
150 /** The web server asks the container to shut itself down. */
151 #define CMD_AJP13_SHUTDOWN (unsigned char)7
152 /** Webserver ask container to take control (logon phase) */
153 #define CMD_AJP13_PING (unsigned char)8
154 /** Container response to cping request */
155 #define CMD_AJP13_CPONG (unsigned char)9
156 /** Webserver check if container is alive, since container should respond by cpong */
157 #define CMD_AJP13_CPING (unsigned char)10
162 * @defgroup AJP_api AJP API functions
167 * Check a new AJP Message by looking at signature and return its size
169 * @param msg AJP Message to check
170 * @param len Pointer to returned len
171 * @return APR_SUCCESS or error
173 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len);
176 * Reset an AJP Message
178 * @param msg AJP Message to reset
179 * @return APR_SUCCESS or error
181 apr_status_t ajp_msg_reset(ajp_msg_t *msg);
184 * Reuse an AJP Message
186 * @param msg AJP Message to reuse
187 * @return APR_SUCCESS or error
189 apr_status_t ajp_msg_reuse(ajp_msg_t *msg);
192 * Mark the end of an AJP Message
194 * @param msg AJP Message to end
195 * @return APR_SUCCESS or error
197 apr_status_t ajp_msg_end(ajp_msg_t *msg);
200 * Add an unsigned 32bits value to AJP Message
202 * @param msg AJP Message to get value from
203 * @param value value to add to AJP Message
204 * @return APR_SUCCESS or error
206 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
209 * Add an unsigned 16bits value to AJP Message
211 * @param msg AJP Message to get value from
212 * @param value value to add to AJP Message
213 * @return APR_SUCCESS or error
215 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
218 * Add an unsigned 8bits value to AJP Message
220 * @param msg AJP Message to get value from
221 * @param value value to add to AJP Message
222 * @return APR_SUCCESS or error
224 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
227 * Add a String in AJP message, and transform the String in ASCII
228 * if convert is set and we're on an EBCDIC machine
230 * @param msg AJP Message to get value from
231 * @param value Pointer to String
232 * @param convert When set told to convert String to ASCII
233 * @return APR_SUCCESS or error
235 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
238 * Add a String in AJP message, and transform
239 * the String in ASCII if we're on an EBCDIC machine
241 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
244 * Add a String in AJP message.
246 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
249 * Add a Byte array to AJP Message
251 * @param msg AJP Message to get value from
252 * @param value Pointer to Byte array
253 * @param valuelen Byte array len
254 * @return APR_SUCCESS or error
256 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
257 apr_size_t valuelen);
260 * Get a 32bits unsigned value from AJP Message
262 * @param msg AJP Message to get value from
263 * @param rvalue Pointer where value will be returned
264 * @return APR_SUCCESS or error
266 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
269 * Get a 16bits unsigned value from AJP Message
271 * @param msg AJP Message to get value from
272 * @param rvalue Pointer where value will be returned
273 * @return APR_SUCCESS or error
275 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
278 * Peek a 16bits unsigned value from AJP Message, position in message
281 * @param msg AJP Message to get value from
282 * @param rvalue Pointer where value will be returned
283 * @return APR_SUCCESS or error
285 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
288 * Get a 8bits unsigned value from AJP Message
290 * @param msg AJP Message to get value from
291 * @param rvalue Pointer where value will be returned
292 * @return APR_SUCCESS or error
294 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
297 * Peek a 8bits unsigned value from AJP Message, position in message
300 * @param msg AJP Message to get value from
301 * @param rvalue Pointer where value will be returned
302 * @return APR_SUCCESS or error
304 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
307 * Get a String value from AJP Message
309 * @param msg AJP Message to get value from
310 * @param rvalue Pointer where value will be returned
311 * @return APR_SUCCESS or error
313 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue);
317 * Get a Byte array from AJP Message
319 * @param msg AJP Message to get value from
320 * @param rvalue Pointer where value will be returned
321 * @param rvalue_len Pointer where Byte array len will be returned
322 * @return APR_SUCCESS or error
324 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
325 apr_size_t *rvalue_len);
328 * Create an AJP Message from pool
330 * @param pool memory pool to allocate AJP message from
331 * @param size size of the buffer to create
332 * @param rmsg Pointer to newly created AJP message
333 * @return APR_SUCCESS or error
335 apr_status_t ajp_msg_create(apr_pool_t *pool, apr_size_t size, ajp_msg_t **rmsg);
338 * Recopy an AJP Message to another
340 * @param smsg source AJP message
341 * @param dmsg destination AJP message
342 * @return APR_SUCCESS or error
344 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
347 * Serialize in an AJP Message a PING command
349 * +-----------------------+
350 * | PING CMD (1 byte) |
351 * +-----------------------+
353 * @param msg AJP message to put serialized message
354 * @return APR_SUCCESS or error
356 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
359 * Serialize in an AJP Message a CPING command
361 * +-----------------------+
362 * | CPING CMD (1 byte) |
363 * +-----------------------+
365 * @param msg AJP message to put serialized message
366 * @return APR_SUCCESS or error
368 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
371 * Dump up to the first 1024 bytes on an AJP Message
373 * @param pool pool to allocate from
374 * @param msg AJP Message to dump
375 * @param err error string to display
376 * @param count the number of bytes to dump
377 * @param buf buffer pointer for dump message
378 * @return APR_SUCCESS or error
380 apr_status_t ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err,
381 apr_size_t count, char **buf);
386 * @param r The current request
387 * @param msg AJP Message to dump
388 * @param err error string to display
389 * @return APR_SUCCESS or error
391 apr_status_t ajp_msg_log(request_rec *r, ajp_msg_t *msg, char *err);
394 * Send an AJP message to backend
396 * @param sock backend socket
397 * @param msg AJP message to put serialized message
398 * @return APR_SUCCESS or error
400 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
403 * Receive an AJP message from backend
405 * @param sock backend socket
406 * @param msg AJP message to put serialized message
407 * @return APR_SUCCESS or error
409 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
412 * Build the ajp header message and send it
413 * @param sock backend socket
414 * @param r current request
415 * @param buffsize max size of the AJP packet.
416 * @param uri requested uri
417 * @return APR_SUCCESS or error
419 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r,
424 * Read the ajp message and return the type of the message.
425 * @param sock backend socket
426 * @param r current request
427 * @param buffsize size of the buffer.
428 * @param msg returned AJP message
429 * @return APR_SUCCESS or error
431 apr_status_t ajp_read_header(apr_socket_t *sock,
437 * Allocate a msg to send data
438 * @param pool pool to allocate from
439 * @param ptr data buffer
440 * @param len the length of allocated data buffer
441 * @param msg returned AJP message
442 * @return APR_SUCCESS or error
444 apr_status_t ajp_alloc_data_msg(apr_pool_t *pool, char **ptr,
445 apr_size_t *len, ajp_msg_t **msg);
448 * Send the data message
449 * @param sock backend socket
450 * @param msg AJP message to send
451 * @param len AJP message length
452 * @return APR_SUCCESS or error
454 apr_status_t ajp_send_data_msg(apr_socket_t *sock,
455 ajp_msg_t *msg, apr_size_t len);
458 * Parse the message type
459 * @param r current request
460 * @param msg AJP message
461 * @return AJP message type.
463 int ajp_parse_type(request_rec *r, ajp_msg_t *msg);
466 * Parse the header message from container
467 * @param r current request
468 * @param conf proxy config
469 * @param msg AJP message
470 * @return APR_SUCCESS or error
472 apr_status_t ajp_parse_header(request_rec *r, proxy_dir_conf *conf,
476 * Parse the message body and return data address and length
477 * @param r current request
478 * @param msg AJP message
479 * @param len returned AJP message length
480 * @param ptr returned data
481 * @return APR_SUCCESS or error
483 apr_status_t ajp_parse_data(request_rec *r, ajp_msg_t *msg,
484 apr_uint16_t *len, char **ptr);
488 * Check the reuse flag in CMD_AJP13_END_RESPONSE
489 * @param r current request
490 * @param msg AJP message
491 * @param reuse returned reuse flag
492 * @return APR_SUCCESS or error
494 apr_status_t ajp_parse_reuse(request_rec *r, ajp_msg_t *msg,
499 * Handle the CPING/CPONG messages
500 * @param sock backend socket
501 * @param r current request
502 * @param timeout time window for receiving cpong reply
503 * @return APR_SUCCESS or error
505 apr_status_t ajp_handle_cping_cpong(apr_socket_t *sock,
507 apr_interval_time_t timeout);
511 * Convert numeric message type into string
512 * @param type AJP message type
513 * @return AJP message type as a string
515 const char *ajp_type_str(int type);