bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / native / common / jk_ajp_common.h
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 /***************************************************************************
19  * Description: common stuff for bi-directional protocol ajp13/ajp14.      *
20  * Author:      Gal Shachor <shachor@il.ibm.com>                           *
21  * Author:      Henri Gomez <hgomez@apache.org>                            *
22  * Version:     $Revision: 1137200 $                                          *
23  ***************************************************************************/
24
25 #ifndef JK_AJP_COMMON_H
26 #define JK_AJP_COMMON_H
27
28 #include "jk_service.h"
29 #include "jk_msg_buff.h"
30 #include "jk_shm.h"
31 #include "jk_mt.h"
32
33 #ifdef __cplusplus
34 extern "C"
35 {
36 #endif                          /* __cplusplus */
37
38 #define JK_AJP_STATE_IDLE               (0)
39 #define JK_AJP_STATE_OK                 (1)
40 #define JK_AJP_STATE_ERROR              (2)
41 #define JK_AJP_STATE_PROBE              (3)
42 #define JK_AJP_STATE_DEF                (JK_AJP_STATE_IDLE)
43 #define JK_AJP_STATE_TEXT_IDLE          ("OK/IDLE")
44 #define JK_AJP_STATE_TEXT_OK            ("OK")
45 #define JK_AJP_STATE_TEXT_ERROR         ("ERR")
46 #define JK_AJP_STATE_TEXT_PROBE         ("ERR/PRB")
47 #define JK_AJP_STATE_TEXT_MAX           (JK_AJP_STATE_PROBE)
48 #define JK_AJP_STATE_TEXT_DEF           (JK_AJP_STATE_TEXT_IDLE)
49
50 /* We accept doing global maintenance if we are */
51 /* JK_AJP_MAINTAIN_TOLERANCE seconds early. */
52 #define JK_AJP_MAINTAIN_TOLERANCE (2)
53
54 /*
55  * Conditional request attributes
56  *
57  */
58 #define SC_A_CONTEXT            (unsigned char)1
59 #define SC_A_SERVLET_PATH       (unsigned char)2
60 #define SC_A_REMOTE_USER        (unsigned char)3
61 #define SC_A_AUTH_TYPE          (unsigned char)4
62 #define SC_A_QUERY_STRING       (unsigned char)5
63 #define SC_A_ROUTE              (unsigned char)6
64 #define SC_A_SSL_CERT           (unsigned char)7
65 #define SC_A_SSL_CIPHER         (unsigned char)8
66 #define SC_A_SSL_SESSION        (unsigned char)9
67 #define SC_A_REQ_ATTRIBUTE      (unsigned char)10
68 #define SC_A_SSL_KEY_SIZE       (unsigned char)11       /* only in if JkOptions +ForwardKeySize */
69 #define SC_A_SECRET             (unsigned char)12
70 #define SC_A_STORED_METHOD      (unsigned char)13
71 #define SC_A_ARE_DONE           (unsigned char)0xFF
72
73 /*
74  * AJP private request attributes
75  *
76  * The following request attribute is recognized by Tomcat
77  * to contain the forwarded remote port.
78  */
79 #define SC_A_REQ_REMOTE_PORT    ("AJP_REMOTE_PORT")
80
81 /*
82  * JK public request attributes
83  *
84  * Activation state of the worker in the load balancer.
85  * Can be any of the JK_LB_ACTIVATION_TEXT_* strings
86  * from jk_lb_worker.h.
87  */
88 #define SC_A_JK_LB_ACTIVATION   ("JK_LB_ACTIVATION")
89
90 /*
91  * Request methods, coded as numbers instead of strings.
92  * The list of methods was taken from Section 5.1.1 of RFC 2616,
93  * RFC 2518, the ACL IETF draft, and the DeltaV IESG Proposed Standard.
94  *          Method        = "OPTIONS"
95  *                        | "GET"
96  *                        | "HEAD"
97  *                        | "POST"
98  *                        | "PUT"
99  *                        | "DELETE"
100  *                        | "TRACE"
101  *                        | "PROPFIND"
102  *                        | "PROPPATCH"
103  *                        | "MKCOL"
104  *                        | "COPY"
105  *                        | "MOVE"
106  *                        | "LOCK"
107  *                        | "UNLOCK"
108  *                        | "ACL"
109  *                        | "REPORT"
110  *                        | "VERSION-CONTROL"
111  *                        | "CHECKIN"
112  *                        | "CHECKOUT"
113  *                        | "UNCHECKOUT"
114  *                        | "SEARCH"
115  *                        | "MKWORKSPACE"
116  *                        | "UPDATE"
117  *                        | "LABEL"
118  *                        | "MERGE"
119  *                        | "BASELINE-CONTROL"
120  *                        | "MKACTIVITY"
121  *
122  */
123 #define SC_M_OPTIONS            (unsigned char)1
124 #define SC_M_GET                (unsigned char)2
125 #define SC_M_HEAD               (unsigned char)3
126 #define SC_M_POST               (unsigned char)4
127 #define SC_M_PUT                (unsigned char)5
128 #define SC_M_DELETE             (unsigned char)6
129 #define SC_M_TRACE              (unsigned char)7
130 #define SC_M_PROPFIND           (unsigned char)8
131 #define SC_M_PROPPATCH          (unsigned char)9
132 #define SC_M_MKCOL              (unsigned char)10
133 #define SC_M_COPY               (unsigned char)11
134 #define SC_M_MOVE               (unsigned char)12
135 #define SC_M_LOCK               (unsigned char)13
136 #define SC_M_UNLOCK             (unsigned char)14
137 #define SC_M_ACL                (unsigned char)15
138 #define SC_M_REPORT             (unsigned char)16
139 #define SC_M_VERSION_CONTROL    (unsigned char)17
140 #define SC_M_CHECKIN            (unsigned char)18
141 #define SC_M_CHECKOUT           (unsigned char)19
142 #define SC_M_UNCHECKOUT         (unsigned char)20
143 #define SC_M_SEARCH             (unsigned char)21
144 #define SC_M_MKWORKSPACE        (unsigned char)22
145 #define SC_M_UPDATE             (unsigned char)23
146 #define SC_M_LABEL              (unsigned char)24
147 #define SC_M_MERGE              (unsigned char)25
148 #define SC_M_BASELINE_CONTROL   (unsigned char)26
149 #define SC_M_MKACTIVITY         (unsigned char)27
150 #define SC_M_JK_STORED          (unsigned char)0xFF
151
152 /*
153  * Frequent request headers, these headers are coded as numbers
154  * instead of strings.
155  *
156  * Accept
157  * Accept-Charset
158  * Accept-Encoding
159  * Accept-Language
160  * Authorization
161  * Connection
162  * Content-Type
163  * Content-Length
164  * Cookie
165  * Cookie2
166  * Host
167  * Pragma
168  * Referer
169  * User-Agent
170  *
171  */
172
173 #define SC_ACCEPT               (unsigned short)0xA001
174 #define SC_ACCEPT_CHARSET       (unsigned short)0xA002
175 #define SC_ACCEPT_ENCODING      (unsigned short)0xA003
176 #define SC_ACCEPT_LANGUAGE      (unsigned short)0xA004
177 #define SC_AUTHORIZATION        (unsigned short)0xA005
178 #define SC_CONNECTION           (unsigned short)0xA006
179 #define SC_CONTENT_TYPE         (unsigned short)0xA007
180 #define SC_CONTENT_LENGTH       (unsigned short)0xA008
181 #define SC_COOKIE               (unsigned short)0xA009
182 #define SC_COOKIE2              (unsigned short)0xA00A
183 #define SC_HOST                 (unsigned short)0xA00B
184 #define SC_PRAGMA               (unsigned short)0xA00C
185 #define SC_REFERER              (unsigned short)0xA00D
186 #define SC_USER_AGENT           (unsigned short)0xA00E
187
188 /*
189  * Frequent response headers, these headers are coded as numbers
190  * instead of strings.
191  *
192  * Content-Type
193  * Content-Language
194  * Content-Length
195  * Date
196  * Last-Modified
197  * Location
198  * Set-Cookie
199  * Servlet-Engine
200  * Status
201  * WWW-Authenticate
202  *
203  */
204
205 #define SC_RESP_CONTENT_TYPE        (unsigned short)0xA001
206 #define SC_RESP_CONTENT_LANGUAGE    (unsigned short)0xA002
207 #define SC_RESP_CONTENT_LENGTH      (unsigned short)0xA003
208 #define SC_RESP_DATE                (unsigned short)0xA004
209 #define SC_RESP_LAST_MODIFIED       (unsigned short)0xA005
210 #define SC_RESP_LOCATION            (unsigned short)0xA006
211 #define SC_RESP_SET_COOKIE          (unsigned short)0xA007
212 #define SC_RESP_SET_COOKIE2         (unsigned short)0xA008
213 #define SC_RESP_SERVLET_ENGINE      (unsigned short)0xA009
214 #define SC_RESP_STATUS              (unsigned short)0xA00A
215 #define SC_RESP_WWW_AUTHENTICATE    (unsigned short)0xA00B
216 #define SC_RES_HEADERS_NUM          11
217
218 /*
219  * AJP13/AJP14 use same message structure
220  */
221
222 #define AJP_DEF_RETRY_ATTEMPTS    (1)
223
224 #define AJP_HEADER_LEN            (4)
225 #define AJP_HEADER_SZ_LEN         (2)
226 #define CHUNK_BUFFER_PAD          (12)
227 #define AJP_DEF_CACHE_TIMEOUT     (0)
228 #define AJP_DEF_CONNECT_TIMEOUT   (0)   /* NO CONNECTION TIMEOUT => NO CPING/CPONG */
229 #define AJP_DEF_REPLY_TIMEOUT     (0)   /* NO REPLY TIMEOUT                        */
230 #define AJP_DEF_PREPOST_TIMEOUT   (0)   /* NO PREPOST TIMEOUT => NO CPING/CPONG    */
231 #define AJP_DEF_RECOVERY_OPTS     (0)   /* NO RECOVERY / NO    */
232 #define AJP_DEF_SOCKET_TIMEOUT    (0)   /* No timeout */
233 #define AJP_DEF_PING_TIMEOUT      (10000)  /* Default CPING/CPONG timeout (10 seconds) */
234
235 #define AJP_CPING_NONE            (0)   /* Do not send cping packets */
236 #define AJP_CPING_CONNECT         (1)   /* Send cping on fresh connection */
237 #define AJP_CPING_PREPOST         (2)   /* Send cping before sending request */
238 #define AJP_CPING_INTERVAL        (4)   /* Send cping on regular intervals */
239
240
241 #define RECOVER_ABORT_IF_TCGETREQUEST    0x0001 /* DON'T RECOVER IF TOMCAT FAILS AFTER RECEIVING REQUEST */
242 #define RECOVER_ABORT_IF_TCSENDHEADER    0x0002 /* DON'T RECOVER IF TOMCAT FAILS AFTER SENDING HEADERS */
243 #define RECOVER_ABORT_IF_CLIENTERROR     0x0004 /* CLOSE THE SOCKET IN CASE OF CLIENT ERROR */
244 #define RECOVER_ALWAYS_HTTP_HEAD         0x0008 /* RECOVER HTTP HEAD REQUESTS, EVEN IF ABORT OPTIONS ARE SET */
245 #define RECOVER_ALWAYS_HTTP_GET          0x0010 /* RECOVER HTTP GET REQUESTS, EVEN IF ABORT OPTIONS ARE SET */
246
247 #define JK_MAX_HTTP_STATUS_FAILS   32   /* Should be enough for most 400 and 500 statuses */
248
249 struct jk_res_data
250 {
251     int status;
252 #ifdef AS400
253     char *msg;
254 #else
255     const char *msg;
256 #endif
257     unsigned num_headers;
258     char **header_names;
259     char **header_values;
260 };
261 typedef struct jk_res_data jk_res_data_t;
262
263 #include "jk_ajp14.h"
264
265 struct ajp_operation;
266 typedef struct ajp_operation ajp_operation_t;
267
268 struct ajp_endpoint;
269 typedef struct ajp_endpoint ajp_endpoint_t;
270
271 struct ajp_worker;
272 typedef struct ajp_worker ajp_worker_t;
273
274 struct ajp_worker
275 {
276     jk_worker_t worker;
277     /* Shared memory worker data */
278     jk_shm_ajp_worker_t *s;
279
280     char         name[JK_SHM_STR_SIZ+1];
281     /* Sequence counter starting at 0 and increasing
282      * every time we change the config
283      */
284     volatile unsigned int sequence;
285
286     jk_pool_t p;
287     jk_pool_atom_t buf[TINY_POOL_SIZE];
288
289     JK_CRIT_SEC cs;
290
291     struct sockaddr_in worker_inet_addr;    /* Contains host and port */
292     unsigned connect_retry_attempts;
293     char host[JK_SHM_STR_SIZ+1];
294     int port;
295     int addr_sequence;  /* Whether the address is resolved */
296     int maintain_time;
297     /*
298      * Open connections cache...
299      *
300      * 1. Critical section object to protect the cache.
301      * 2. Cache size.
302      * 3. An array of "open" endpoints.
303      */
304     unsigned int ep_cache_sz;
305     unsigned int ep_mincache_sz;
306     unsigned int ep_maxcache_sz;
307     int cache_acquire_timeout;
308     ajp_endpoint_t **ep_cache;
309
310     int proto;              /* PROTOCOL USED AJP13/AJP14 */
311
312     jk_login_service_t *login;
313
314     /* Weak secret similar with ajp12, used in ajp13 */
315     const char *secret;
316
317     /*
318      * Post physical connect handler.
319      * AJP14 will set here its login handler
320      */
321     int (*logon) (ajp_endpoint_t * ae, jk_logger_t *l);
322
323     /*
324      * Handle Socket Timeouts
325      */
326     int socket_timeout;
327     int socket_connect_timeout;
328     int keepalive;
329     int socket_buf;
330     /*
331      * Handle Cache Timeouts
332      */
333     int cache_timeout;
334
335     /*
336      * Handle Connection/Reply Timeouts
337      */
338     int connect_timeout;      /* connect cping/cpong delay in ms (0 means disabled)  */
339     int reply_timeout;        /* reply timeout delay in ms (0 means disabled) */
340     int prepost_timeout;      /* before sending a request cping/cpong timeout delay in ms (0 means disabled) */
341     int conn_ping_interval;   /* interval for sending keepalive cping packets on
342                                * unused connection */
343     int ping_timeout;         /* generic cping/cpong timeout. Used for keepalive packets or
344                                * as default for boolean valued connect and prepost timeouts.
345                                */
346     unsigned int ping_mode;   /* Ping mode flags (which types of cpings should be used) */
347     /*
348      * Recovery options
349      */
350     unsigned int recovery_opts;
351
352     /*
353      * Public property to enable the number of retry attempts
354      * on this worker.
355      */
356     int retries;
357
358     unsigned int max_packet_size;  /*  Maximum AJP Packet size */
359
360     int retry_interval;            /*  Number of milliseconds to sleep before doing a retry */
361
362     /*
363      * HTTP status that will cause failover (0 means disabled)
364      */
365      unsigned int http_status_fail_num;
366      int http_status_fail[JK_MAX_HTTP_STATUS_FAILS];
367 };
368
369
370 /*
371  * endpoint, the remote connector which does the work
372  */
373 struct ajp_endpoint
374 {
375     ajp_worker_t *worker;
376
377     jk_pool_t pool;
378     jk_pool_atom_t buf[BIG_POOL_SIZE];
379
380     int proto;              /* PROTOCOL USED AJP13/AJP14 */
381
382     jk_sock_t sd;
383     int reuse;
384
385     /* Used with RECOVER_ABORT_IF_CLIENTERROR to hard abort
386        write of AJP response on client write errors */
387     int hard_close;
388
389     jk_endpoint_t endpoint;
390
391     jk_uint64_t left_bytes_to_send;
392
393     /* time of the last request
394        handled by this endpoint */
395     time_t last_access;
396     int last_errno;
397     /* Last operation performed via this endpoint */
398     int last_op;
399     int addr_sequence;  /* Whether the address is resolved */
400 };
401
402 /*
403  * little struct to avoid multiples ptr passing
404  * this struct is ready to hold upload file fd
405  * to add upload persistant storage
406  */
407 struct ajp_operation
408 {
409     jk_msg_buf_t *request;  /* original request storage */
410     jk_msg_buf_t *reply;    /* reply storage (chuncked by ajp13 */
411     jk_msg_buf_t *post;     /* small post data storage area */
412     int uploadfd;           /* future persistant storage id */
413     int recoverable;        /* if exchange could be conducted on another TC */
414 };
415
416 /*
417  * Functions
418  */
419
420
421 const char *jk_ajp_get_state(ajp_worker_t *aw, jk_logger_t *l);
422
423 int jk_ajp_get_state_code(const char *v);
424
425 int ajp_validate(jk_worker_t *pThis,
426                  jk_map_t *props,
427                  jk_worker_env_t *we, jk_logger_t *l, int proto);
428
429 int ajp_init(jk_worker_t *pThis,
430              jk_map_t *props,
431              jk_worker_env_t *we, jk_logger_t *l, int proto);
432
433 int JK_METHOD ajp_worker_factory(jk_worker_t **w,
434                                  const char *name, jk_logger_t *l);
435
436 int ajp_destroy(jk_worker_t **pThis, jk_logger_t *l, int proto);
437
438 int JK_METHOD ajp_done(jk_endpoint_t **e, jk_logger_t *l);
439
440 int ajp_get_endpoint(jk_worker_t *pThis,
441                      jk_endpoint_t **pend, jk_logger_t *l, int proto);
442
443 int ajp_connect_to_endpoint(ajp_endpoint_t * ae, jk_logger_t *l);
444
445 void ajp_close_endpoint(ajp_endpoint_t * ae, jk_logger_t *l);
446
447 void jk_ajp_pull(ajp_worker_t * aw, int locked, jk_logger_t *l);
448
449 void jk_ajp_push(ajp_worker_t * aw, int locked, jk_logger_t *l);
450
451 int ajp_connection_tcp_send_message(ajp_endpoint_t * ae,
452                                     jk_msg_buf_t *msg, jk_logger_t *l);
453
454 int ajp_connection_tcp_get_message(ajp_endpoint_t * ae,
455                                    jk_msg_buf_t *msg, jk_logger_t *l);
456
457 int JK_METHOD ajp_maintain(jk_worker_t *pThis, time_t now, jk_logger_t *l);
458
459 int jk_ajp_get_cping_mode(const char *m, int def);
460
461 int ajp_has_endpoint(jk_worker_t *pThis, jk_logger_t *l);
462
463 #ifdef __cplusplus
464 }
465 #endif                          /* __cplusplus */
466 #endif                          /* JK_AJP_COMMON_H */
467