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 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
#if APR_CHARSET_EBCDIC
70
#define USE_CHARSET_EBCDIC
71
#define ajp_xlate_to_ascii(b, l) ap_xlate_proto_to_ascii(b, l)
72
#define ajp_xlate_from_ascii(b, l) ap_xlate_proto_from_ascii(b, l)
74
#else /* APR_CHARSET_EBCDIC */
76
#define ajp_xlate_to_ascii(b, l)
77
#define ajp_xlate_from_ascii(b, l)
81
#ifdef AJP_USE_HTTPD_WRAP
82
#include "httpd_wrap.h"
85
#include "http_config.h"
86
#include "http_request.h"
87
#include "http_core.h"
88
#include "http_protocol.h"
89
#include "http_main.h"
93
#include "mod_proxy.h"
96
/** AJP Specific error codes
98
/** Buffer overflow exception */
99
#define AJP_EOVERFLOW (APR_OS_START_USERERR + 1)
100
/** Destination Buffer is to small */
101
#define AJP_ETOSMALL (APR_OS_START_USERERR + 2)
102
/** Invalid input parameters */
103
#define AJP_EINVAL (APR_OS_START_USERERR + 3)
104
/** Bad message signature */
105
#define AJP_EBAD_SIGNATURE (APR_OS_START_USERERR + 4)
106
/** Incoming message too bg */
107
#define AJP_ETOBIG (APR_OS_START_USERERR + 5)
108
/** Missing message header */
109
#define AJP_ENO_HEADER (APR_OS_START_USERERR + 6)
110
/** Bad message header */
111
#define AJP_EBAD_HEADER (APR_OS_START_USERERR + 7)
113
#define AJP_EBAD_MESSAGE (APR_OS_START_USERERR + 8)
114
/** Cant log via AJP14 */
115
#define AJP_ELOGFAIL (APR_OS_START_USERERR + 9)
116
/** Bad request method */
117
#define AJP_EBAD_METHOD (APR_OS_START_USERERR + 10)
120
/** A structure that represents ajp message */
121
typedef struct ajp_msg ajp_msg_t;
123
/** A structure that represents ajp message */
126
/** The buffer holding a AJP message */
128
/** The length of AJP message header (defaults to AJP_HEADER_LEN) */
129
apr_size_t header_len;
130
/** The length of AJP message */
132
/** The current read position */
134
/** Flag indicating the origing of the message */
139
* Signature for the messages sent from Apache to tomcat
141
#define AJP13_WS_HEADER 0x1234
142
#define AJP_HEADER_LEN 4
143
#define AJP_HEADER_SZ_LEN 2
144
#define AJP_MSG_BUFFER_SZ (8*1024)
145
#define AJP13_MAX_SEND_BODY_SZ (AJP_MSG_BUFFER_SZ - 6)
147
/** Send a request from web server to container*/
148
#define CMD_AJP13_FORWARD_REQUEST (unsigned char)2
149
/** Write a body chunk from the servlet container to the web server */
150
#define CMD_AJP13_SEND_BODY_CHUNK (unsigned char)3
151
/** Send response headers from the servlet container to the web server. */
152
#define CMD_AJP13_SEND_HEADERS (unsigned char)4
153
/** Marks the end of response. */
154
#define CMD_AJP13_END_RESPONSE (unsigned char)5
155
/** Get further data from the web server if it hasn't all been transferred yet. */
156
#define CMD_AJP13_GET_BODY_CHUNK (unsigned char)6
157
/** The web server asks the container to shut itself down. */
158
#define CMD_AJP13_SHUTDOWN (unsigned char)7
159
/** Webserver ask container to take control (logon phase) */
160
#define CMD_AJP13_PING (unsigned char)8
161
/** Container response to cping request */
162
#define CMD_AJP13_CPONG (unsigned char)9
163
/** Webserver check if container is alive, since container should respond by cpong */
164
#define CMD_AJP13_CPING (unsigned char)10
169
* @defgroup AJP_api AJP API functions
174
* Check a new AJP Message by looking at signature and return its size
176
* @param msg AJP Message to check
177
* @param len Pointer to returned len
178
* @return APR_SUCCESS or error
180
apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len);
183
* Reset an AJP Message
185
* @param msg AJP Message to reset
186
* @return APR_SUCCESS or error
188
apr_status_t ajp_msg_reset(ajp_msg_t *msg);
191
* Reuse an AJP Message
193
* @param msg AJP Message to reuse
194
* @return APR_SUCCESS or error
196
apr_status_t ajp_msg_reuse(ajp_msg_t *msg);
199
* Mark the end of an AJP Message
201
* @param msg AJP Message to end
202
* @return APR_SUCCESS or error
204
apr_status_t ajp_msg_end(ajp_msg_t *msg);
207
* Add an unsigned 32bits 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_uint32(ajp_msg_t *msg, apr_uint32_t value);
216
* Add an unsigned 16bits value to AJP Message
218
* @param msg AJP Message to get value from
219
* @param value value to add to AJP Message
220
* @return APR_SUCCESS or error
222
apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
225
* Add an unsigned 8bits value to AJP Message
227
* @param msg AJP Message to get value from
228
* @param value value to add to AJP Message
229
* @return APR_SUCCESS or error
231
apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
234
* Add a String in AJP message, and transform the String in ASCII
235
* if convert is set and we're on an EBCDIC machine
237
* @param msg AJP Message to get value from
238
* @param value Pointer to String
239
* @param convert When set told to convert String to ASCII
240
* @return APR_SUCCESS or error
242
apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
245
* Add a String in AJP message, and transform
246
* the String in ASCII if we're on an EBCDIC machine
248
#define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
251
* Add a String in AJP message.
253
#define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
256
* Add a Byte array to AJP Message
258
* @param msg AJP Message to get value from
259
* @param value Pointer to Byte array
260
* @param valuelen Byte array len
261
* @return APR_SUCCESS or error
263
apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
264
apr_size_t valuelen);
267
* Get a 32bits unsigned value from AJP Message
269
* @param msg AJP Message to get value from
270
* @param rvalue Pointer where value will be returned
271
* @return APR_SUCCESS or error
273
apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
276
* Get a 16bits unsigned value from AJP Message
278
* @param msg AJP Message to get value from
279
* @param rvalue Pointer where value will be returned
280
* @return APR_SUCCESS or error
282
apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
285
* Peek a 16bits unsigned value from AJP Message, position in message
288
* @param msg AJP Message to get value from
289
* @param rvalue Pointer where value will be returned
290
* @return APR_SUCCESS or error
292
apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
295
* Get a 8bits unsigned value from AJP Message
297
* @param msg AJP Message to get value from
298
* @param rvalue Pointer where value will be returned
299
* @return APR_SUCCESS or error
301
apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
304
* Peek a 8bits unsigned value from AJP Message, position in message
307
* @param msg AJP Message to get value from
308
* @param rvalue Pointer where value will be returned
309
* @return APR_SUCCESS or error
311
apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
314
* Get a String value from AJP Message
316
* @param msg AJP Message to get value from
317
* @param rvalue Pointer where value will be returned
318
* @return APR_SUCCESS or error
320
apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue);
324
* Get a Byte array from AJP Message
326
* @param msg AJP Message to get value from
327
* @param rvalue Pointer where value will be returned
328
* @param rvalueLen Pointer where Byte array len will be returned
329
* @return APR_SUCCESS or error
331
apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
332
apr_size_t *rvalue_len);
335
* Create an AJP Message from pool
337
* @param pool memory pool to allocate AJP message from
338
* @param rmsg Pointer to newly created AJP message
339
* @return APR_SUCCESS or error
341
apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg);
344
* Recopy an AJP Message to another
346
* @param smsg source AJP message
347
* @param dmsg destination AJP message
348
* @return APR_SUCCESS or error
350
apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
353
* Serialize in an AJP Message a PING command
355
* +-----------------------+
356
* | PING CMD (1 byte) |
357
* +-----------------------+
359
* @param smsg AJP message to put serialized message
360
* @return APR_SUCCESS or error
362
apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
365
* Serialize in an AJP Message a CPING command
367
* +-----------------------+
368
* | CPING CMD (1 byte) |
369
* +-----------------------+
371
* @param smsg AJP message to put serialized message
372
* @return APR_SUCCESS or error
374
apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
377
* Dump up to the first 1024 bytes on an AJP Message
379
* @param pool pool to allocate from
380
* @param msg AJP Message to dump
381
* @param err error string to display
382
* @return dump message
384
char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err);
387
* Send an AJP message to backend
389
* @param soct backend socket
390
* @param smsg AJP message to put serialized message
391
* @return APR_SUCCESS or error
393
apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
396
* Receive an AJP message from backend
398
* @param sock backend socket
399
* @param smsg AJP message to put serialized message
400
* @return APR_SUCCESS or error
402
apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
405
* Build the ajp header message and send it
406
* @param sock backend socket
407
* @param r current request
408
* @uri uri requested uri
409
* @return APR_SUCCESS or error
411
apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r,
415
* Read the ajp message and return the type of the message.
416
* @param sock backend socket
417
* @param r current request
418
* @param msg returned AJP message
419
* @return APR_SUCCESS or error
421
apr_status_t ajp_read_header(apr_socket_t *sock,
426
* Allocate a msg to send data
427
* @param pool pool to allocate from
428
* @param ptr data buffer
429
* @param len the length of allocated data buffer
430
* @param msg returned AJP message
431
* @return APR_SUCCESS or error
433
apr_status_t ajp_alloc_data_msg(apr_pool_t *pool, char **ptr,
434
apr_size_t *len, ajp_msg_t **msg);
437
* Send the data message
438
* @param sock backend socket
439
* @param msg AJP message to send
440
* @param len AJP message length
441
* @return APR_SUCCESS or error
443
apr_status_t ajp_send_data_msg(apr_socket_t *sock,
444
ajp_msg_t *msg, apr_size_t len);
447
* Parse the message type
448
* @param r current request
449
* @param msg AJP message
450
* @return AJP message type.
452
int ajp_parse_type(request_rec *r, ajp_msg_t *msg);
455
* Parse the header message from container
456
* @param r current request
457
* @param msg AJP message
458
* @return APR_SUCCESS or error
460
apr_status_t ajp_parse_header(request_rec *r, proxy_dir_conf *conf,
464
* Parse the message body and return data address and length
465
* @param r current request
466
* @param msg AJP message
467
* @param len returned AJP message length
468
* @param ptr returned data
469
* @return APR_SUCCESS or error
471
apr_status_t ajp_parse_data(request_rec *r, ajp_msg_t *msg,
472
apr_uint16_t *len, char **ptr);