]> granicus.if.org Git - apache/blob - modules/proxy/ajp.h
small cleanup of EBCDIC support in mod_proxy_ajp
[apache] / modules / proxy / ajp.h
1 /* Copyright 1999-2006 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file ajp.h
19  * @brief Apache Jserv Protocol
20  *
21  * @defgroup AJP_defines AJP definitions
22  * @ingroup  MOD_PROXY
23  * @{
24  */
25
26 #ifndef AJP_H
27 #define AJP_H
28
29 #include "apr_version.h"
30 #include "apr.h"
31
32 #include "apr_hooks.h"
33 #include "apr_lib.h"
34 #include "apr_strings.h"
35 #include "apr_buckets.h"
36 #include "apr_md5.h"
37 #include "apr_network_io.h"
38 #include "apr_poll.h"
39 #include "apr_pools.h"
40 #include "apr_strings.h"
41 #include "apr_uri.h"
42 #include "apr_date.h"
43 #include "apr_fnmatch.h"
44 #define APR_WANT_STRFUNC
45 #include "apr_want.h"
46
47 #if APR_HAVE_NETINET_IN_H
48 #include <netinet/in.h>
49 #endif
50 #if APR_HAVE_ARPA_INET_H
51 #include <arpa/inet.h>
52 #endif
53
54 #define AJP13_DEF_HOST "127.0.0.1"
55 #ifdef NETWARE
56 #define AJP13_DEF_PORT 9009     /* default to 9009 since 8009 is used by OS */
57 #else
58 #define AJP13_DEF_PORT 8009
59 #endif
60
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"
67
68 #ifdef AJP_USE_HTTPD_WRAP
69 #include "httpd_wrap.h"
70 #else
71 #include "httpd.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"
77 #include "http_log.h"
78 #endif
79
80 #include "mod_proxy.h"
81 #include "util_ebcdic.h"
82
83 /** AJP Specific error codes
84  */
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) 
99 /** Bad message */
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) 
105
106
107 /** A structure that represents ajp message */ 
108 typedef struct ajp_msg ajp_msg_t;
109
110 /** A structure that represents ajp message */ 
111 struct ajp_msg
112 {
113     /** The buffer holding a AJP message */ 
114     apr_byte_t  *buf;
115     /** The length of AJP message header (defaults to AJP_HEADER_LEN) */ 
116     apr_size_t  header_len;
117     /** The length of AJP message */ 
118     apr_size_t  len;
119     /** The current read position */ 
120     apr_size_t  pos;
121     /** Flag indicating the origing of the message */ 
122     int         server_side;
123 };
124
125 /**
126  * Signature for the messages sent from Apache to tomcat
127  */
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)
133
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
152
153 /** @} */
154
155 /**
156  * @defgroup AJP_api AJP API functions
157  * @ingroup  MOD_PROXY
158  * @{
159  */
160 /**
161  * Check a new AJP Message by looking at signature and return its size
162  *
163  * @param msg       AJP Message to check
164  * @param len       Pointer to returned len
165  * @return          APR_SUCCESS or error
166  */
167 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len);
168
169 /**
170  * Reset an AJP Message
171  *
172  * @param msg       AJP Message to reset
173  * @return          APR_SUCCESS or error
174  */
175 apr_status_t ajp_msg_reset(ajp_msg_t *msg);
176
177 /**
178  * Reuse an AJP Message
179  *
180  * @param msg       AJP Message to reuse
181  * @return          APR_SUCCESS or error
182  */
183 apr_status_t ajp_msg_reuse(ajp_msg_t *msg);
184
185 /**
186  * Mark the end of an AJP Message
187  *
188  * @param msg       AJP Message to end
189  * @return          APR_SUCCESS or error
190  */
191 apr_status_t ajp_msg_end(ajp_msg_t *msg);
192
193 /**
194  * Add an unsigned 32bits value to AJP Message
195  *
196  * @param msg       AJP Message to get value from
197  * @param value     value to add to AJP Message
198  * @return          APR_SUCCESS or error
199  */
200 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
201
202 /**
203  * Add an unsigned 16bits value to AJP Message
204  *
205  * @param msg       AJP Message to get value from
206  * @param value     value to add to AJP Message
207  * @return          APR_SUCCESS or error
208  */
209 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
210
211 /**
212  * Add an unsigned 8bits value to AJP Message
213  *
214  * @param msg       AJP Message to get value from
215  * @param value     value to add to AJP Message
216  * @return          APR_SUCCESS or error
217  */
218 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
219
220 /**
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    
223  *
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
228  */
229 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
230                                       int convert);
231 /**
232  *  Add a String in AJP message, and transform 
233  *  the String in ASCII if we're on an EBCDIC machine    
234  */
235 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
236
237 /**
238  *  Add a String in AJP message. 
239  */
240 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
241
242 /**
243  * Add a Byte array to AJP Message
244  *
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
249  */
250 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
251                                   apr_size_t valuelen);
252
253 /**
254  * Get a 32bits unsigned value from AJP Message
255  *
256  * @param msg       AJP Message to get value from
257  * @param rvalue    Pointer where value will be returned
258  * @return          APR_SUCCESS or error
259  */
260 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
261
262 /**
263  * Get a 16bits unsigned value from AJP Message
264  *
265  * @param msg       AJP Message to get value from
266  * @param rvalue    Pointer where value will be returned
267  * @return          APR_SUCCESS or error
268  */
269 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
270
271 /**
272  * Peek a 16bits unsigned value from AJP Message, position in message
273  * is not updated
274  *
275  * @param msg       AJP Message to get value from
276  * @param rvalue    Pointer where value will be returned
277  * @return          APR_SUCCESS or error
278  */
279 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
280
281 /**
282  * Get a 8bits unsigned value from AJP Message
283  *
284  * @param msg       AJP Message to get value from
285  * @param rvalue    Pointer where value will be returned
286  * @return          APR_SUCCESS or error
287  */
288 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
289
290 /**
291  * Peek a 8bits unsigned value from AJP Message, position in message
292  * is not updated
293  *
294  * @param msg       AJP Message to get value from
295  * @param rvalue    Pointer where value will be returned
296  * @return          APR_SUCCESS or error
297  */
298 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
299
300 /**
301  * Get a String value from AJP Message
302  *
303  * @param msg       AJP Message to get value from
304  * @param rvalue    Pointer where value will be returned
305  * @return          APR_SUCCESS or error
306  */
307 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue);
308
309
310 /**
311  * Get a Byte array from AJP Message
312  *
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
317  */
318 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
319                                apr_size_t *rvalue_len);
320
321 /**
322  * Create an AJP Message from pool
323  *
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
327  */
328 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg);
329
330 /**
331  * Recopy an AJP Message to another
332  *
333  * @param smsg      source AJP message
334  * @param dmsg      destination AJP message
335  * @return          APR_SUCCESS or error
336  */
337 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
338
339 /**
340  * Serialize in an AJP Message a PING command
341  *
342  * +-----------------------+
343  * | PING CMD (1 byte)     |
344  * +-----------------------+
345  *
346  * @param smsg      AJP message to put serialized message
347  * @return          APR_SUCCESS or error
348  */
349 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
350
351 /** 
352  * Serialize in an AJP Message a CPING command
353  *
354  * +-----------------------+
355  * | CPING CMD (1 byte)    |
356  * +-----------------------+
357  *
358  * @param smsg      AJP message to put serialized message
359  * @return          APR_SUCCESS or error
360  */
361 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
362
363 /**
364  * Dump up to the first 1024 bytes on an AJP Message
365  *
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
370  */
371 char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err);
372
373 /** 
374  * Send an AJP message to backend
375  *
376  * @param soct      backend socket
377  * @param smsg      AJP message to put serialized message
378  * @return          APR_SUCCESS or error
379  */
380 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
381
382 /** 
383  * Receive an AJP message from backend
384  *
385  * @param sock      backend socket
386  * @param smsg      AJP message to put serialized message
387  * @return          APR_SUCCESS or error
388  */
389 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
390
391 /**
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
397  */
398 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r,
399                              apr_uri_t *uri);
400
401 /**
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
407  */
408 apr_status_t ajp_read_header(apr_socket_t *sock,
409                              request_rec  *r,
410                              ajp_msg_t **msg);
411
412 /**
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
419  */
420 apr_status_t  ajp_alloc_data_msg(apr_pool_t *pool, char **ptr,
421                                  apr_size_t *len, ajp_msg_t **msg);
422
423 /**
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
429  */
430 apr_status_t  ajp_send_data_msg(apr_socket_t *sock,
431                                 ajp_msg_t *msg, apr_size_t len);
432
433 /**
434  * Parse the message type 
435  * @param r         current request
436  * @param msg       AJP message
437  * @return          AJP message type.
438  */
439 int ajp_parse_type(request_rec  *r, ajp_msg_t *msg);
440
441 /**
442  * Parse the header message from container 
443  * @param r         current request
444  * @param msg       AJP message
445  * @return          APR_SUCCESS or error
446  */
447 apr_status_t ajp_parse_header(request_rec *r, proxy_dir_conf *conf,
448                               ajp_msg_t *msg);
449
450 /** 
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
457  */
458 apr_status_t  ajp_parse_data(request_rec  *r, ajp_msg_t *msg,
459                              apr_uint16_t *len, char **ptr);
460
461 /** @} */
462
463 #endif /* AJP_H */
464