]> granicus.if.org Git - apache/blob - modules/proxy/ajp.h
67c0d0b47dbb8a892db677ecd2612a715a03a812
[apache] / modules / proxy / ajp.h
1 /* Copyright 1999-2004 The Apache Software Foundation
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef AJP_H
17 #define AJP_H
18
19 #include "apr_version.h"
20 #include "apr.h"
21
22 #include "apr_hooks.h"
23 #include "apr_lib.h"
24 #include "apr_strings.h"
25 #include "apr_buckets.h"
26 #include "apr_md5.h"
27 #include "apr_network_io.h"
28 #include "apr_pools.h"
29 #include "apr_strings.h"
30 #include "apr_uri.h"
31 #include "apr_date.h"
32 #include "apr_fnmatch.h"
33 #define APR_WANT_STRFUNC
34 #include "apr_want.h"
35
36 #if APR_HAVE_NETINET_IN_H
37 #include <netinet/in.h>
38 #endif
39 #if APR_HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
41 #endif
42
43 #define AJP13_DEF_HOST "127.0.0.1"
44 #ifdef NETWARE
45 #define AJP13_DEF_PORT 9009     /* default to 9009 since 8009 is used by OS */
46 #else
47 #define AJP13_DEF_PORT 8009
48 #endif
49
50 /* The following environment variables match mod_ssl! */
51 #define AJP13_HTTPS_INDICATOR           "HTTPS"
52 #define AJP13_SSL_CLIENT_CERT_INDICATOR "SSL_CLIENT_CERT"
53 #define AJP13_SSL_CIPHER_INDICATOR      "SSL_CIPHER"
54 #define AJP13_SSL_SESSION_INDICATOR     "SSL_SESSION_ID"
55 #define AJP13_SSL_KEY_SIZE_INDICATOR    "SSL_CIPHER_USEKEYSIZE"
56
57 #if APR_CHARSET_EBCDIC
58
59 #define USE_CHARSET_EBCDIC
60 #define ajp_xlate_to_ascii(b, l) ap_xlate_proto_to_ascii(b, l)
61 #define ajp_xlate_from_ascii(b, l) ap_xlate_proto_from_ascii(b, l)
62
63 #else                           /* APR_CHARSET_EBCDIC */
64
65 #define ajp_xlate_to_ascii(b, l) 
66 #define ajp_xlate_from_ascii(b, l) 
67
68 #endif
69
70 #ifdef AJP_USE_HTTPD_WRAP
71 #include "httpd_wrap.h"
72 #else
73 #include "httpd.h"
74 #include "http_config.h"
75 #include "http_request.h"
76 #include "http_core.h"
77 #include "http_protocol.h"
78 #include "http_main.h"
79 #include "http_log.h"
80 #endif
81
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
104 /** A structure that represents ajp message */ 
105 typedef struct ajp_msg ajp_msg_t;
106
107 /** A structure that represents ajp message */ 
108 struct ajp_msg
109 {
110     /** The buffer holding a AJP message */ 
111     apr_byte_t  *buf;
112     /** The length of AJP message header (defaults to AJP_HEADER_LEN) */ 
113     apr_size_t  header_len;
114     /** The length of AJP message */ 
115     apr_size_t  len;
116     /** The current read position */ 
117     apr_size_t  pos;
118     /** Flag indicating the origing of the message */ 
119     int         server_side;
120 };
121
122 /**
123  * @defgroup AJP_defines AJP definitions 
124  * @{
125  */
126 /**
127  * Signature for the messages sent from Apache to tomcat
128  */
129 #define AJP13_WS_HEADER             0x1234
130 #define AJP_HEADER_LEN              4
131 #define AJP_HEADER_SZ_LEN           2
132 #define AJP_MSG_BUFFER_SZ           (8*1024)
133 #define AJP13_MAX_SEND_BODY_SZ      (AJP_MSG_BUFFER_SZ - 6)
134
135 /** Send a request from web server to container*/
136 #define CMD_AJP13_FORWARD_REQUEST   (unsigned char)2
137 /** Write a body chunk from the servlet container to the web server */
138 #define CMD_AJP13_SEND_BODY_CHUNK   (unsigned char)3
139 /** Send response headers from the servlet container to the web server. */
140 #define CMD_AJP13_SEND_HEADERS      (unsigned char)4
141 /** Marks the end of response. */
142 #define CMD_AJP13_END_RESPONSE      (unsigned char)5
143 /** Get further data from the web server if it hasn't all been transferred yet. */
144 #define CMD_AJP13_GET_BODY_CHUNK    (unsigned char)6
145 /** The web server asks the container to shut itself down. */
146 #define CMD_AJP13_SHUTDOWN          (unsigned char)7
147 /** Webserver ask container to take control (logon phase) */
148 #define CMD_AJP13_PING              (unsigned char)8
149 /** Container response to cping request */
150 #define CMD_AJP13_CPONG             (unsigned char)9
151 /** Webserver check if container is alive, since container should respond by cpong */
152 #define CMD_AJP13_CPING             (unsigned char)10
153
154 /** @} */
155
156 /**
157  * @defgroup AJP_api AJP API functions
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  * Mark the end of an AJP Message
179  *
180  * @param msg       AJP Message to end
181  * @return          APR_SUCCESS or error
182  */
183 apr_status_t ajp_msg_end(ajp_msg_t *msg);
184
185 /**
186  * Add an unsigned 32bits value to AJP Message
187  *
188  * @param msg       AJP Message to get value from
189  * @param value     value to add to AJP Message
190  * @return          APR_SUCCESS or error
191  */
192 apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
193
194 /**
195  * Add an unsigned 16bits value to AJP Message
196  *
197  * @param msg       AJP Message to get value from
198  * @param value     value to add to AJP Message
199  * @return          APR_SUCCESS or error
200  */
201 apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
202
203 /**
204  * Add an unsigned 8bits value to AJP Message
205  *
206  * @param msg       AJP Message to get value from
207  * @param value     value to add to AJP Message
208  * @return          APR_SUCCESS or error
209  */
210 apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
211
212 /**
213  *  Add a String in AJP message, and transform the String in ASCII 
214  *  if convert is set and we're on an EBCDIC machine    
215  *
216  * @param msg       AJP Message to get value from
217  * @param value     Pointer to String
218  * @param convert   When set told to convert String to ASCII
219  * @return          APR_SUCCESS or error
220  */
221 apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
222                                       int convert);
223 /**
224  *  Add a String in AJP message, and transform 
225  *  the String in ASCII if we're on an EBCDIC machine    
226  */
227 #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
228
229 /**
230  *  Add a String in AJP message. 
231  */
232 #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
233
234 /**
235  * Add a Byte array to AJP Message
236  *
237  * @param msg       AJP Message to get value from
238  * @param value     Pointer to Byte array
239  * @param valuelen  Byte array len
240  * @return          APR_SUCCESS or error
241  */
242 apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
243                                   apr_size_t valuelen);
244
245 /**
246  * Get a 32bits unsigned value from AJP Message
247  *
248  * @param msg       AJP Message to get value from
249  * @param rvalue    Pointer where value will be returned
250  * @return          APR_SUCCESS or error
251  */
252 apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
253
254 /**
255  * Get a 16bits unsigned value from AJP Message
256  *
257  * @param msg       AJP Message to get value from
258  * @param rvalue    Pointer where value will be returned
259  * @return          APR_SUCCESS or error
260  */
261 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
262
263 /**
264  * Peek a 16bits unsigned value from AJP Message, position in message
265  * is not updated
266  *
267  * @param msg       AJP Message to get value from
268  * @param rvalue    Pointer where value will be returned
269  * @return          APR_SUCCESS or error
270  */
271 apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
272
273 /**
274  * Get a 8bits unsigned value from AJP Message
275  *
276  * @param msg       AJP Message to get value from
277  * @param rvalue    Pointer where value will be returned
278  * @return          APR_SUCCESS or error
279  */
280 apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
281
282 /**
283  * Peek a 8bits unsigned value from AJP Message, position in message
284  * is not updated
285  *
286  * @param msg       AJP Message to get value from
287  * @param rvalue    Pointer where value will be returned
288  * @return          APR_SUCCESS or error
289  */
290 apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
291
292 /**
293  * Get a String value from AJP Message
294  *
295  * @param msg       AJP Message to get value from
296  * @param rvalue    Pointer where value will be returned
297  * @return          APR_SUCCESS or error
298  */
299 apr_status_t ajp_msg_get_string(ajp_msg_t *msg, char **rvalue);
300
301
302 /**
303  * Get a Byte array from AJP Message
304  *
305  * @param msg       AJP Message to get value from
306  * @param rvalue    Pointer where value will be returned
307  * @param rvalueLen Pointer where Byte array len will be returned
308  * @return          APR_SUCCESS or error
309  */
310 apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
311                                apr_size_t *rvalue_len);
312
313 /**
314  * Create an AJP Message from pool
315  *
316  * @param pool      memory pool to allocate AJP message from
317  * @param rmsg      Pointer to newly created AJP message
318  * @return          APR_SUCCESS or error
319  */
320 apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg);
321
322 /**
323  * Recopy an AJP Message to another
324  *
325  * @param smsg      source AJP message
326  * @param dmsg      destination AJP message
327  * @return          APR_SUCCESS or error
328  */
329 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
330
331 /**
332  * Serialize in an AJP Message a PING command
333  *
334  * +-----------------------+
335  * | PING CMD (1 byte)     |
336  * +-----------------------+
337  *
338  * @param smsg      AJP message to put serialized message
339  * @return          APR_SUCCESS or error
340  */
341 apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
342
343 /** 
344  * Serialize in an AJP Message a CPING command
345  *
346  * +-----------------------+
347  * | CPING CMD (1 byte)    |
348  * +-----------------------+
349  *
350  * @param smsg      AJP message to put serialized message
351  * @return          APR_SUCCESS or error
352  */
353 apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
354
355 /**
356  * Dump up to the first 1024 bytes on an AJP Message
357  *
358  * @param msg       AJP Message to dump
359  * @param err       error string to display
360  * @return          APR_SUCCESS or error
361  */
362 apr_status_t ajp_msg_dump(ajp_msg_t *msg, char *err);
363
364 /** 
365  * Send an AJP message to backend
366  *
367  * @param soct      backend socket
368  * @param smsg      AJP message to put serialized message
369  * @return          APR_SUCCESS or error
370  */
371 apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
372
373 /** 
374  * Receive an AJP message from backend
375  *
376  * @param sock      backend socket
377  * @param smsg      AJP message to put serialized message
378  * @return          APR_SUCCESS or error
379  */
380 apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
381
382 /**
383  * Build the ajp header message and send it
384  * @param sock      backend socket
385  * @param r         current request
386  * @return          APR_SUCCESS or error
387  */
388 apr_status_t ajp_send_header(apr_socket_t *sock, request_rec  *r);
389
390 /**
391  * Read the ajp message and return the type of the message.
392  * @param sock      backend socket
393  * @param r         current request
394  * @param msg       returned AJP message
395  * @return          APR_SUCCESS or error
396  */
397 apr_status_t ajp_read_header(apr_socket_t *sock,
398                              request_rec  *r,
399                              ajp_msg_t **msg);
400
401 /**
402  * Allocate a msg to send data
403  * @param r         current request
404  * @param ptr       data buffer
405  * @param len       the length of allocated data buffer
406  * @param msg       returned AJP message
407  * @return          APR_SUCCESS or error
408  */
409 apr_status_t  ajp_alloc_data_msg(request_rec *r, char **ptr, apr_size_t *len,
410                                  ajp_msg_t **msg);
411
412 /**
413  * Send the data message
414  * @param sock      backend socket
415  * @param r         current request
416  * @param msg       AJP message to send
417  * @param len       AJP message length      
418  * @return          APR_SUCCESS or error
419  */
420 apr_status_t  ajp_send_data_msg(apr_socket_t *sock, request_rec  *r,
421                                 ajp_msg_t *msg, apr_size_t len);
422
423 /**
424  * Parse the message type 
425  * @param r         current request
426  * @param msg       AJP message
427  * @return          AJP message type.
428  */
429 int ajp_parse_type(request_rec  *r, ajp_msg_t *msg);
430
431 /**
432  * Parse the header message from container 
433  * @param r         current request
434  * @param msg       AJP message
435  * @return          APR_SUCCESS or error
436  */
437 apr_status_t ajp_parse_header(request_rec  *r, ajp_msg_t *msg);
438
439 /** 
440  * Parse the message body and return data address and length 
441  * @param r         current request
442  * @param msg       AJP message
443  * @param len       returned AJP message length 
444  * @param ptr       returned data
445  * @return          APR_SUCCESS or error
446  */
447 apr_status_t  ajp_parse_data(request_rec  *r, ajp_msg_t *msg,
448                              apr_uint16_t *len, char **ptr);
449
450 /** @} */
451
452 #endif /* AJP_H */
453