]> granicus.if.org Git - apache/blob - modules/proxy/ajp.h
58bf173853a0efc66d4e750998c2ab13ce442b0e
[apache] / modules / proxy / ajp.h
1 /* Copyright 1999-2005 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 #if APR_CHARSET_EBCDIC
69
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)
73
74 #else                           /* APR_CHARSET_EBCDIC */
75
76 #define ajp_xlate_to_ascii(b, l) 
77 #define ajp_xlate_from_ascii(b, l) 
78
79 #endif
80
81 #ifdef AJP_USE_HTTPD_WRAP
82 #include "httpd_wrap.h"
83 #else
84 #include "httpd.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"
90 #include "http_log.h"
91 #endif
92
93 #include "mod_proxy.h"
94
95
96 /** AJP Specific error codes
97  */
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) 
112 /** Bad message */
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) 
118
119
120 /** A structure that represents ajp message */ 
121 typedef struct ajp_msg ajp_msg_t;
122
123 /** A structure that represents ajp message */ 
124 struct ajp_msg
125 {
126     /** The buffer holding a AJP message */ 
127     apr_byte_t  *buf;
128     /** The length of AJP message header (defaults to AJP_HEADER_LEN) */ 
129     apr_size_t  header_len;
130     /** The length of AJP message */ 
131     apr_size_t  len;
132     /** The current read position */ 
133     apr_size_t  pos;
134     /** Flag indicating the origing of the message */ 
135     int         server_side;
136 };
137
138 /**
139  * Signature for the messages sent from Apache to tomcat
140  */
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)
146
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
165
166 /** @} */
167
168 /**
169  * @defgroup AJP_api AJP API functions
170  * @ingroup  MOD_PROXY
171  * @{
172  */
173 /**
174  * Check a new AJP Message by looking at signature and return its size
175  *
176  * @param msg       AJP Message to check
177  * @param len       Pointer to returned len
178  * @return          APR_SUCCESS or error
179  */
180 apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len);
181
182 /**
183  * Reset an AJP Message
184  *
185  * @param msg       AJP Message to reset
186  * @return          APR_SUCCESS or error
187  */
188 apr_status_t ajp_msg_reset(ajp_msg_t *msg);
189
190 /**
191  * Reuse an AJP Message
192  *
193  * @param msg       AJP Message to reuse
194  * @return          APR_SUCCESS or error
195  */
196 apr_status_t ajp_msg_reuse(ajp_msg_t *msg);
197
198 /**
199  * Mark the end of an AJP Message
200  *
201  * @param msg       AJP Message to end
202  * @return          APR_SUCCESS or error
203  */
204 apr_status_t ajp_msg_end(ajp_msg_t *msg);
205
206 /**
207  * Add an unsigned 32bits value to AJP Message
208  *
209  * @param msg       AJP Message to get value from
210  * @param value     value to add to AJP Message
211  * @return          APR_SUCCESS or error
212  */
213 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
214
215 /**
216  * Add an unsigned 16bits value to AJP Message
217  *
218  * @param msg       AJP Message to get value from
219  * @param value     value to add to AJP Message
220  * @return          APR_SUCCESS or error
221  */
222 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
223
224 /**
225  * Add an unsigned 8bits value to AJP Message
226  *
227  * @param msg       AJP Message to get value from
228  * @param value     value to add to AJP Message
229  * @return          APR_SUCCESS or error
230  */
231 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
232
233 /**
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    
236  *
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
241  */
242 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
243                                       int convert);
244 /**
245  *  Add a String in AJP message, and transform 
246  *  the String in ASCII if we're on an EBCDIC machine    
247  */
248 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
249
250 /**
251  *  Add a String in AJP message. 
252  */
253 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
254
255 /**
256  * Add a Byte array to AJP Message
257  *
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
262  */
263 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
264                                   apr_size_t valuelen);
265
266 /**
267  * Get a 32bits unsigned value from AJP Message
268  *
269  * @param msg       AJP Message to get value from
270  * @param rvalue    Pointer where value will be returned
271  * @return          APR_SUCCESS or error
272  */
273 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
274
275 /**
276  * Get a 16bits unsigned value from AJP Message
277  *
278  * @param msg       AJP Message to get value from
279  * @param rvalue    Pointer where value will be returned
280  * @return          APR_SUCCESS or error
281  */
282 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
283
284 /**
285  * Peek a 16bits unsigned value from AJP Message, position in message
286  * is not updated
287  *
288  * @param msg       AJP Message to get value from
289  * @param rvalue    Pointer where value will be returned
290  * @return          APR_SUCCESS or error
291  */
292 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
293
294 /**
295  * Get a 8bits unsigned value from AJP Message
296  *
297  * @param msg       AJP Message to get value from
298  * @param rvalue    Pointer where value will be returned
299  * @return          APR_SUCCESS or error
300  */
301 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
302
303 /**
304  * Peek a 8bits unsigned value from AJP Message, position in message
305  * is not updated
306  *
307  * @param msg       AJP Message to get value from
308  * @param rvalue    Pointer where value will be returned
309  * @return          APR_SUCCESS or error
310  */
311 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
312
313 /**
314  * Get a String value from AJP Message
315  *
316  * @param msg       AJP Message to get value from
317  * @param rvalue    Pointer where value will be returned
318  * @return          APR_SUCCESS or error
319  */
320 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue);
321
322
323 /**
324  * Get a Byte array from AJP Message
325  *
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
330  */
331 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
332                                apr_size_t *rvalue_len);
333
334 /**
335  * Create an AJP Message from pool
336  *
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
340  */
341 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg);
342
343 /**
344  * Recopy an AJP Message to another
345  *
346  * @param smsg      source AJP message
347  * @param dmsg      destination AJP message
348  * @return          APR_SUCCESS or error
349  */
350 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
351
352 /**
353  * Serialize in an AJP Message a PING command
354  *
355  * +-----------------------+
356  * | PING CMD (1 byte)     |
357  * +-----------------------+
358  *
359  * @param smsg      AJP message to put serialized message
360  * @return          APR_SUCCESS or error
361  */
362 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
363
364 /** 
365  * Serialize in an AJP Message a CPING command
366  *
367  * +-----------------------+
368  * | CPING CMD (1 byte)    |
369  * +-----------------------+
370  *
371  * @param smsg      AJP message to put serialized message
372  * @return          APR_SUCCESS or error
373  */
374 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
375
376 /**
377  * Dump up to the first 1024 bytes on an AJP Message
378  *
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
383  */
384 char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err);
385
386 /** 
387  * Send an AJP message to backend
388  *
389  * @param soct      backend socket
390  * @param smsg      AJP message to put serialized message
391  * @return          APR_SUCCESS or error
392  */
393 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
394
395 /** 
396  * Receive an AJP message from backend
397  *
398  * @param sock      backend socket
399  * @param smsg      AJP message to put serialized message
400  * @return          APR_SUCCESS or error
401  */
402 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
403
404 /**
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
410  */
411 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r,
412                              apr_uri_t *uri);
413
414 /**
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
420  */
421 apr_status_t ajp_read_header(apr_socket_t *sock,
422                              request_rec  *r,
423                              ajp_msg_t **msg);
424
425 /**
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
432  */
433 apr_status_t  ajp_alloc_data_msg(apr_pool_t *pool, char **ptr,
434                                  apr_size_t *len, ajp_msg_t **msg);
435
436 /**
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
442  */
443 apr_status_t  ajp_send_data_msg(apr_socket_t *sock,
444                                 ajp_msg_t *msg, apr_size_t len);
445
446 /**
447  * Parse the message type 
448  * @param r         current request
449  * @param msg       AJP message
450  * @return          AJP message type.
451  */
452 int ajp_parse_type(request_rec  *r, ajp_msg_t *msg);
453
454 /**
455  * Parse the header message from container 
456  * @param r         current request
457  * @param msg       AJP message
458  * @return          APR_SUCCESS or error
459  */
460 apr_status_t ajp_parse_header(request_rec *r, proxy_dir_conf *conf,
461                               ajp_msg_t *msg);
462
463 /** 
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
470  */
471 apr_status_t  ajp_parse_data(request_rec  *r, ajp_msg_t *msg,
472                              apr_uint16_t *len, char **ptr);
473
474 /** @} */
475
476 #endif /* AJP_H */
477