]> granicus.if.org Git - apache/blob - modules/proxy/ajp_header.c
mod_proxy_ajp: Respect "reuse" flag in END_REPONSE
[apache] / modules / proxy / ajp_header.c
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
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 #include "ajp_header.h"
18 #include "ajp.h"
19
20 APLOG_USE_MODULE(proxy_ajp);
21
22 static const char *response_trans_headers[] = {
23     "Content-Type",
24     "Content-Language",
25     "Content-Length",
26     "Date",
27     "Last-Modified",
28     "Location",
29     "Set-Cookie",
30     "Set-Cookie2",
31     "Servlet-Engine",
32     "Status",
33     "WWW-Authenticate"
34 };
35
36 static const char *long_res_header_for_sc(int sc)
37 {
38     const char *rc = NULL;
39     sc = sc & 0X00FF;
40     if(sc <= SC_RES_HEADERS_NUM && sc > 0) {
41         rc = response_trans_headers[sc - 1];
42     }
43
44     return rc;
45 }
46
47 #define UNKNOWN_METHOD (-1)
48
49 static int sc_for_req_header(const char *header_name)
50 {
51     char header[16];
52     apr_size_t len = strlen(header_name);
53     const char *p = header_name;
54     int i = 0;
55
56     /* ACCEPT-LANGUAGE is the longest header
57      * that is of interest.
58      */
59     if (len < 4 || len > 15)
60         return UNKNOWN_METHOD;
61
62     while (*p)
63         header[i++] = apr_toupper(*p++);
64     header[i] = '\0';
65     p = &header[1];
66
67     switch (header[0]) {
68         case 'A':
69             if (memcmp(p, "CCEPT", 5) == 0) {
70                 if (!header[6])
71                     return SC_ACCEPT;
72                 else if (header[6] == '-') {
73                     p += 6;
74                     if (strcmp(p, "CHARSET") == 0)
75                         return SC_ACCEPT_CHARSET;
76                     else if (strcmp(p,  "ENCODING") == 0)
77                         return SC_ACCEPT_ENCODING;
78                     else if (strcmp(p, "LANGUAGE") == 0)
79                         return SC_ACCEPT_LANGUAGE;
80                     else
81                         return UNKNOWN_METHOD;
82                 }
83                 else
84                     return UNKNOWN_METHOD;
85             }
86             else if (strcmp(p, "UTHORIZATION") == 0)
87                 return SC_AUTHORIZATION;
88             else
89                 return UNKNOWN_METHOD;
90         break;
91         case 'C':
92             if(strcmp(p, "OOKIE2") == 0)
93                 return SC_COOKIE2;
94             else if (strcmp(p, "OOKIE") == 0)
95                 return SC_COOKIE;
96             else if(strcmp(p, "ONNECTION") == 0)
97                 return SC_CONNECTION;
98             else if(strcmp(p, "ONTENT-TYPE") == 0)
99                 return SC_CONTENT_TYPE;
100             else if(strcmp(p, "ONTENT-LENGTH") == 0)
101                 return SC_CONTENT_LENGTH;
102             else
103                 return UNKNOWN_METHOD;
104         break;
105         case 'H':
106             if(strcmp(p, "OST") == 0)
107                 return SC_HOST;
108             else
109                 return UNKNOWN_METHOD;
110         break;
111         case 'P':
112             if(strcmp(p, "RAGMA") == 0)
113                 return SC_PRAGMA;
114             else
115                 return UNKNOWN_METHOD;
116         break;
117         case 'R':
118             if(strcmp(p, "EFERER") == 0)
119                 return SC_REFERER;
120             else
121                 return UNKNOWN_METHOD;
122         break;
123         case 'U':
124             if(strcmp(p, "SER-AGENT") == 0)
125                 return SC_USER_AGENT;
126             else
127                 return UNKNOWN_METHOD;
128         break;
129         default:
130             return UNKNOWN_METHOD;
131     }
132
133     /* NOTREACHED */
134 }
135
136 /* Apache method number to SC methods transform table */
137 static const unsigned char sc_for_req_method_table[] = {
138     SC_M_GET,
139     SC_M_PUT,
140     SC_M_POST,
141     SC_M_DELETE,
142     0,                      /* M_DELETE */
143     SC_M_OPTIONS,
144     SC_M_TRACE,
145     0,                      /* M_PATCH  */
146     SC_M_PROPFIND,
147     SC_M_PROPPATCH,
148     SC_M_MKCOL,
149     SC_M_COPY,
150     SC_M_MOVE,
151     SC_M_LOCK,
152     SC_M_UNLOCK,
153     SC_M_VERSION_CONTROL,
154     SC_M_CHECKOUT,
155     SC_M_UNCHECKOUT,
156     SC_M_CHECKIN,
157     SC_M_UPDATE,
158     SC_M_LABEL,
159     SC_M_REPORT,
160     SC_M_MKWORKSPACE,
161     SC_M_MKACTIVITY,
162     SC_M_BASELINE_CONTROL,
163     SC_M_MERGE,
164     0                       /* M_INVALID */
165 };
166
167 static int sc_for_req_method_by_id(request_rec *r)
168 {
169     int method_id = r->method_number;
170     if (method_id < 0 || method_id > M_INVALID) {
171         return UNKNOWN_METHOD;
172     }
173     else if (r->header_only) {
174         return SC_M_HEAD;
175     }
176     else {
177         return sc_for_req_method_table[method_id] ?
178                sc_for_req_method_table[method_id] : UNKNOWN_METHOD;
179     }
180 }
181
182 /*
183  * Message structure
184  *
185  *
186 AJPV13_REQUEST/AJPV14_REQUEST=
187     request_prefix (1) (byte)
188     method         (byte)
189     protocol       (string)
190     req_uri        (string)
191     remote_addr    (string)
192     remote_host    (string)
193     server_name    (string)
194     server_port    (short)
195     is_ssl         (boolean)
196     num_headers    (short)
197     num_headers*(req_header_name header_value)
198
199     ?context       (byte)(string)
200     ?servlet_path  (byte)(string)
201     ?remote_user   (byte)(string)
202     ?auth_type     (byte)(string)
203     ?query_string  (byte)(string)
204     ?jvm_route     (byte)(string)
205     ?ssl_cert      (byte)(string)
206     ?ssl_cipher    (byte)(string)
207     ?ssl_session   (byte)(string)
208     ?ssl_key_size  (byte)(int)      via JkOptions +ForwardKeySize
209     request_terminator (byte)
210     ?body          content_length*(var binary)
211
212  */
213
214 static apr_status_t ajp_marshal_into_msgb(ajp_msg_t *msg,
215                                           request_rec *r,
216                                           apr_uri_t *uri)
217 {
218     int method;
219     apr_uint32_t i, num_headers = 0;
220     apr_byte_t is_ssl;
221     char *remote_host;
222     const char *session_route, *envvar;
223     const apr_array_header_t *arr = apr_table_elts(r->subprocess_env);
224     const apr_table_entry_t *elts = (const apr_table_entry_t *)arr->elts;
225
226     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
227                          "Into ajp_marshal_into_msgb");
228
229     if ((method = sc_for_req_method_by_id(r)) == UNKNOWN_METHOD) {
230         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
231                "ajp_marshal_into_msgb - No such method %s",
232                r->method);
233         return AJP_EBAD_METHOD;
234     }
235
236     is_ssl = (apr_byte_t) ap_proxy_conn_is_https(r->connection);
237
238     if (r->headers_in && apr_table_elts(r->headers_in)) {
239         const apr_array_header_t *t = apr_table_elts(r->headers_in);
240         num_headers = t->nelts;
241     }
242
243     remote_host = (char *)ap_get_remote_host(r->connection, r->per_dir_config, REMOTE_HOST, NULL);
244
245     ajp_msg_reset(msg);
246
247     if (ajp_msg_append_uint8(msg, CMD_AJP13_FORWARD_REQUEST)     ||
248         ajp_msg_append_uint8(msg, (apr_byte_t) method)           ||
249         ajp_msg_append_string(msg, r->protocol)                  ||
250         ajp_msg_append_string(msg, uri->path)                    ||
251         ajp_msg_append_string(msg, r->connection->remote_ip)     ||
252         ajp_msg_append_string(msg, remote_host)                  ||
253         ajp_msg_append_string(msg, ap_get_server_name(r))        ||
254         ajp_msg_append_uint16(msg, (apr_uint16_t)r->connection->local_addr->port) ||
255         ajp_msg_append_uint8(msg, is_ssl)                        ||
256         ajp_msg_append_uint16(msg, (apr_uint16_t) num_headers)) {
257
258         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
259                "ajp_marshal_into_msgb: "
260                "Error appending the message begining");
261         return APR_EGENERAL;
262     }
263
264     for (i = 0 ; i < num_headers ; i++) {
265         int sc;
266         const apr_array_header_t *t = apr_table_elts(r->headers_in);
267         const apr_table_entry_t *elts = (apr_table_entry_t *)t->elts;
268
269         if ((sc = sc_for_req_header(elts[i].key)) != UNKNOWN_METHOD) {
270             if (ajp_msg_append_uint16(msg, (apr_uint16_t)sc)) {
271                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
272                        "ajp_marshal_into_msgb: "
273                        "Error appending the header name");
274                 return AJP_EOVERFLOW;
275             }
276         }
277         else {
278             if (ajp_msg_append_string(msg, elts[i].key)) {
279                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
280                        "ajp_marshal_into_msgb: "
281                        "Error appending the header name");
282                 return AJP_EOVERFLOW;
283             }
284         }
285
286         if (ajp_msg_append_string(msg, elts[i].val)) {
287             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
288                    "ajp_marshal_into_msgb: "
289                    "Error appending the header value");
290             return AJP_EOVERFLOW;
291         }
292         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
293                    "ajp_marshal_into_msgb: Header[%d] [%s] = [%s]",
294                    i, elts[i].key, elts[i].val);
295     }
296
297 /* XXXX need to figure out how to do this
298     if (s->secret) {
299         if (ajp_msg_append_uint8(msg, SC_A_SECRET) ||
300             ajp_msg_append_string(msg, s->secret)) {
301             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
302                    "Error ajp_marshal_into_msgb - "
303                    "Error appending secret");
304             return APR_EGENERAL;
305         }
306     }
307  */
308
309     if (r->user) {
310         if (ajp_msg_append_uint8(msg, SC_A_REMOTE_USER) ||
311             ajp_msg_append_string(msg, r->user)) {
312             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
313                    "ajp_marshal_into_msgb: "
314                    "Error appending the remote user");
315             return AJP_EOVERFLOW;
316         }
317     }
318     if (r->ap_auth_type) {
319         if (ajp_msg_append_uint8(msg, SC_A_AUTH_TYPE) ||
320             ajp_msg_append_string(msg, r->ap_auth_type)) {
321             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
322                    "ajp_marshal_into_msgb: "
323                    "Error appending the auth type");
324             return AJP_EOVERFLOW;
325         }
326     }
327     /* XXXX  ebcdic (args converted?) */
328     if (uri->query) {
329         if (ajp_msg_append_uint8(msg, SC_A_QUERY_STRING) ||
330             ajp_msg_append_string(msg, uri->query)) {
331             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
332                    "ajp_marshal_into_msgb: "
333                    "Error appending the query string");
334             return AJP_EOVERFLOW;
335         }
336     }
337     if ((session_route = apr_table_get(r->notes, "session-route"))) {
338         if (ajp_msg_append_uint8(msg, SC_A_JVM_ROUTE) ||
339             ajp_msg_append_string(msg, session_route)) {
340             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
341                    "ajp_marshal_into_msgb: "
342                    "Error appending the jvm route");
343             return AJP_EOVERFLOW;
344         }
345     }
346 /* XXX: Is the subprocess_env a right place?
347  * <Location /examples>
348  *   ProxyPass ajp://remote:8009/servlets-examples
349  *   SetEnv SSL_SESSION_ID CUSTOM_SSL_SESSION_ID
350  * </Location>
351  */
352     /*
353      * Only lookup SSL variables if we are currently running HTTPS.
354      * Furthermore ensure that only variables get set in the AJP message
355      * that are not NULL and not empty.
356      */
357     if (is_ssl) {
358         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
359                                        AJP13_SSL_CLIENT_CERT_INDICATOR))
360             && envvar[0]) {
361             if (ajp_msg_append_uint8(msg, SC_A_SSL_CERT)
362                 || ajp_msg_append_string(msg, envvar)) {
363                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
364                              "ajp_marshal_into_msgb: "
365                              "Error appending the SSL certificates");
366                 return AJP_EOVERFLOW;
367             }
368         }
369
370         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
371                                        AJP13_SSL_CIPHER_INDICATOR))
372             && envvar[0]) {
373             if (ajp_msg_append_uint8(msg, SC_A_SSL_CIPHER)
374                 || ajp_msg_append_string(msg, envvar)) {
375                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
376                              "ajp_marshal_into_msgb: "
377                              "Error appending the SSL ciphers");
378                 return AJP_EOVERFLOW;
379             }
380         }
381
382         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
383                                        AJP13_SSL_SESSION_INDICATOR))
384             && envvar[0]) {
385             if (ajp_msg_append_uint8(msg, SC_A_SSL_SESSION)
386                 || ajp_msg_append_string(msg, envvar)) {
387                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
388                              "ajp_marshal_into_msgb: "
389                              "Error appending the SSL session");
390                 return AJP_EOVERFLOW;
391             }
392         }
393
394         /* ssl_key_size is required by Servlet 2.3 API */
395         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
396                                        AJP13_SSL_KEY_SIZE_INDICATOR))
397             && envvar[0]) {
398
399             if (ajp_msg_append_uint8(msg, SC_A_SSL_KEY_SIZE)
400                 || ajp_msg_append_uint16(msg, (unsigned short) atoi(envvar))) {
401                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
402                              "Error ajp_marshal_into_msgb - "
403                              "Error appending the SSL key size");
404                 return APR_EGENERAL;
405             }
406         }
407     }
408     /* Forward the remote port information, which was forgotten
409      * from the builtin data of the AJP 13 protocol.
410      * Since the servlet spec allows to retrieve it via getRemotePort(),
411      * we provide the port to the Tomcat connector as a request
412      * attribute. Modern Tomcat versions know how to retrieve
413      * the remote port from this attribute.
414      */
415     {
416         const char *key = SC_A_REQ_REMOTE_PORT;
417         char *val = apr_itoa(r->pool, r->connection->remote_addr->port);
418         if (ajp_msg_append_uint8(msg, SC_A_REQ_ATTRIBUTE) ||
419             ajp_msg_append_string(msg, key)   ||
420             ajp_msg_append_string(msg, val)) {
421             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
422                     "ajp_marshal_into_msgb: "
423                     "Error appending attribute %s=%s",
424                     key, val);
425             return AJP_EOVERFLOW;
426         }
427     }
428     /* Use the environment vars prefixed with AJP_
429      * and pass it to the header striping that prefix.
430      */
431     for (i = 0; i < (apr_uint32_t)arr->nelts; i++) {
432         if (!strncmp(elts[i].key, "AJP_", 4)) {
433             if (ajp_msg_append_uint8(msg, SC_A_REQ_ATTRIBUTE) ||
434                 ajp_msg_append_string(msg, elts[i].key + 4)   ||
435                 ajp_msg_append_string(msg, elts[i].val)) {
436                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
437                         "ajp_marshal_into_msgb: "
438                         "Error appending attribute %s=%s",
439                         elts[i].key, elts[i].val);
440                 return AJP_EOVERFLOW;
441             }
442         }
443     }
444
445     if (ajp_msg_append_uint8(msg, SC_A_ARE_DONE)) {
446         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
447                "ajp_marshal_into_msgb: "
448                "Error appending the message end");
449         return AJP_EOVERFLOW;
450     }
451
452     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
453             "ajp_marshal_into_msgb: Done");
454     return APR_SUCCESS;
455 }
456
457 /*
458 AJPV13_RESPONSE/AJPV14_RESPONSE:=
459     response_prefix (2)
460     status          (short)
461     status_msg      (short)
462     num_headers     (short)
463     num_headers*(res_header_name header_value)
464     *body_chunk
465     terminator      boolean <! -- recycle connection or not  -->
466
467 req_header_name :=
468     sc_req_header_name | (string)
469
470 res_header_name :=
471     sc_res_header_name | (string)
472
473 header_value :=
474     (string)
475
476 body_chunk :=
477     length  (short)
478     body    length*(var binary)
479
480  */
481
482 static int addit_dammit(void *v, const char *key, const char *val)
483 {
484     apr_table_addn(v, key, val);
485     return 1;
486 }
487
488 static apr_status_t ajp_unmarshal_response(ajp_msg_t *msg,
489                                            request_rec *r,
490                                            proxy_dir_conf *dconf)
491 {
492     apr_uint16_t status;
493     apr_status_t rc;
494     const char *ptr;
495     apr_uint16_t  num_headers;
496     int i;
497
498     rc = ajp_msg_get_uint16(msg, &status);
499
500     if (rc != APR_SUCCESS) {
501          ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
502                 "ajp_unmarshal_response: Null status");
503         return rc;
504     }
505     r->status = status;
506
507     rc = ajp_msg_get_string(msg, &ptr);
508     if (rc == APR_SUCCESS) {
509 #if APR_CHARSET_EBCDIC /* copy only if we have to */
510         ptr = apr_pstrdup(r->pool, ptr);
511         ap_xlate_proto_from_ascii(ptr, strlen(ptr));
512 #endif
513         r->status_line =  apr_psprintf(r->pool, "%d %s", status, ptr);
514     } else {
515         r->status_line = NULL;
516     }
517
518     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
519            "ajp_unmarshal_response: status = %d", status);
520
521     rc = ajp_msg_get_uint16(msg, &num_headers);
522     if (rc == APR_SUCCESS) {
523         apr_table_t *save_table;
524
525         /* First, tuck away all already existing cookies */
526         /*
527          * Could optimize here, but just in case we want to
528          * also save other headers, keep this logic.
529          */
530         save_table = apr_table_make(r->pool, num_headers + 2);
531         apr_table_do(addit_dammit, save_table, r->headers_out,
532                      "Set-Cookie", NULL);
533         r->headers_out = save_table;
534     } else {
535         r->headers_out = NULL;
536         num_headers = 0;
537     }
538
539     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
540            "ajp_unmarshal_response: Number of headers is = %d",
541            num_headers);
542
543     for(i = 0 ; i < (int) num_headers ; i++) {
544         apr_uint16_t name;
545         const char *stringname;
546         const char *value;
547         rc  = ajp_msg_peek_uint16(msg, &name);
548         if (rc != APR_SUCCESS) {
549             return rc;
550         }
551
552         if ((name & 0XFF00) == 0XA000) {
553             ajp_msg_get_uint16(msg, &name);
554             stringname = long_res_header_for_sc(name);
555             if (stringname == NULL) {
556                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
557                        "ajp_unmarshal_response: "
558                        "No such sc (%08x)",
559                        name);
560                 return AJP_EBAD_HEADER;
561             }
562         } else {
563             name = 0;
564             rc = ajp_msg_get_string(msg, &stringname);
565             if (rc != APR_SUCCESS) {
566                 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
567                        "ajp_unmarshal_response: "
568                        "Null header name");
569                 return rc;
570             }
571             ap_xlate_proto_from_ascii(stringname, strlen(stringname));
572         }
573
574         rc = ajp_msg_get_string(msg, &value);
575         if (rc != APR_SUCCESS) {
576             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
577                    "ajp_unmarshal_response: "
578                    "Null header value");
579             return rc;
580         }
581
582         /* Set-Cookie need additional processing */
583         if (!strcasecmp(stringname, "Set-Cookie")) {
584             value = ap_proxy_cookie_reverse_map(r, dconf, value);
585         }
586         /* Location, Content-Location, URI and Destination need additional
587          * processing */
588         else if (!strcasecmp(stringname, "Location")
589                  || !strcasecmp(stringname, "Content-Location")
590                  || !strcasecmp(stringname, "URI")
591                  || !strcasecmp(stringname, "Destination"))
592         {
593           value = ap_proxy_location_reverse_map(r, dconf, value);
594         }
595
596         ap_xlate_proto_from_ascii(value, strlen(value));
597         ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
598                "ajp_unmarshal_response: Header[%d] [%s] = [%s]",
599                        i, stringname, value);
600
601         apr_table_add(r->headers_out, stringname, value);
602
603         /* Content-type needs an additional handling */
604         if (strcasecmp(stringname, "Content-Type") == 0) {
605              /* add corresponding filter */
606             ap_set_content_type(r, apr_pstrdup(r->pool, value));
607             ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
608                "ajp_unmarshal_response: ap_set_content_type done");
609         }
610     }
611
612     return APR_SUCCESS;
613 }
614
615 /*
616  * Build the ajp header message and send it
617  */
618 apr_status_t ajp_send_header(apr_socket_t *sock,
619                              request_rec *r,
620                              apr_size_t buffsize,
621                              apr_uri_t *uri)
622 {
623     ajp_msg_t *msg;
624     apr_status_t rc;
625
626     rc = ajp_msg_create(r->pool, buffsize, &msg);
627     if (rc != APR_SUCCESS) {
628         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
629                "ajp_send_header: ajp_msg_create failed");
630         return rc;
631     }
632
633     rc = ajp_marshal_into_msgb(msg, r, uri);
634     if (rc != APR_SUCCESS) {
635         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
636                "ajp_send_header: ajp_marshal_into_msgb failed");
637         return rc;
638     }
639
640     rc = ajp_ilink_send(sock, msg);
641     if (rc != APR_SUCCESS) {
642         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
643                "ajp_send_header: ajp_ilink_send failed");
644         return rc;
645     }
646
647     return APR_SUCCESS;
648 }
649
650 /*
651  * Read the ajp message and return the type of the message.
652  */
653 apr_status_t ajp_read_header(apr_socket_t *sock,
654                              request_rec  *r,
655                              apr_size_t buffsize,
656                              ajp_msg_t **msg)
657 {
658     apr_byte_t result;
659     apr_status_t rc;
660
661     if (*msg) {
662         rc = ajp_msg_reuse(*msg);
663         if (rc != APR_SUCCESS) {
664             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
665                    "ajp_read_header: ajp_msg_reuse failed");
666             return rc;
667         }
668     }
669     else {
670         rc = ajp_msg_create(r->pool, buffsize, msg);
671         if (rc != APR_SUCCESS) {
672             ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
673                    "ajp_read_header: ajp_msg_create failed");
674             return rc;
675         }
676     }
677     ajp_msg_reset(*msg);
678     rc = ajp_ilink_receive(sock, *msg);
679     if (rc != APR_SUCCESS) {
680         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
681                "ajp_read_header: ajp_ilink_receive failed");
682         return rc;
683     }
684     rc = ajp_msg_peek_uint8(*msg, &result);
685     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
686                "ajp_read_header: ajp_ilink_received %02x", result);
687     return APR_SUCCESS;
688 }
689
690 /* parse the msg to read the type */
691 int ajp_parse_type(request_rec  *r, ajp_msg_t *msg)
692 {
693     apr_byte_t result;
694     ajp_msg_peek_uint8(msg, &result);
695     ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
696                "ajp_parse_type: got %02x", result);
697     return (int) result;
698 }
699
700 /* parse the header */
701 apr_status_t ajp_parse_header(request_rec  *r, proxy_dir_conf *conf,
702                               ajp_msg_t *msg)
703 {
704     apr_byte_t result;
705     apr_status_t rc;
706
707     rc = ajp_msg_get_uint8(msg, &result);
708     if (rc != APR_SUCCESS) {
709         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
710                "ajp_parse_headers: ajp_msg_get_byte failed");
711         return rc;
712     }
713     if (result != CMD_AJP13_SEND_HEADERS) {
714         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
715                "ajp_parse_headers: wrong type 0x%02x expecting 0x%02x",
716                result, CMD_AJP13_SEND_HEADERS);
717         return AJP_EBAD_HEADER;
718     }
719     return ajp_unmarshal_response(msg, r, conf);
720 }
721
722 /* parse the body and return data address and length */
723 apr_status_t  ajp_parse_data(request_rec  *r, ajp_msg_t *msg,
724                              apr_uint16_t *len, char **ptr)
725 {
726     apr_byte_t result;
727     apr_status_t rc;
728     apr_uint16_t expected_len;
729
730     rc = ajp_msg_get_uint8(msg, &result);
731     if (rc != APR_SUCCESS) {
732         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
733                "ajp_parse_data: ajp_msg_get_byte failed");
734         return rc;
735     }
736     if (result != CMD_AJP13_SEND_BODY_CHUNK) {
737         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
738                "ajp_parse_data: wrong type 0x%02x expecting 0x%02x",
739                result, CMD_AJP13_SEND_BODY_CHUNK);
740         return AJP_EBAD_HEADER;
741     }
742     rc = ajp_msg_get_uint16(msg, len);
743     if (rc != APR_SUCCESS) {
744         return rc;
745     }
746     /*
747      * msg->len contains the complete length of the message including all
748      * headers. So the expected length for a CMD_AJP13_SEND_BODY_CHUNK is
749      * msg->len minus the sum of
750      * AJP_HEADER_LEN    : The length of the header to every AJP message.
751      * AJP_HEADER_SZ_LEN : The header giving the size of the chunk.
752      * 1                 : The CMD_AJP13_SEND_BODY_CHUNK indicator byte (0x03).
753      * 1                 : The last byte of this message always seems to be
754      *                     0x00 and is not part of the chunk.
755      */
756     expected_len = msg->len - (AJP_HEADER_LEN + AJP_HEADER_SZ_LEN + 1 + 1);
757     if (*len != expected_len) {
758         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
759                "ajp_parse_data: Wrong chunk length. Length of chunk is %i,"
760                " expected length is %i.", *len, expected_len);
761         return AJP_EBAD_HEADER;
762     }
763     *ptr = (char *)&(msg->buf[msg->pos]);
764     return APR_SUCCESS;
765 }
766
767 /* Check the reuse flag in CMD_AJP13_END_RESPONSE */
768 apr_status_t ajp_parse_reuse(request_rec *r, ajp_msg_t *msg,
769                              apr_byte_t *reuse)
770 {
771     apr_byte_t result;
772     apr_status_t rc;
773
774     rc = ajp_msg_get_uint8(msg, &result);
775     if (rc != APR_SUCCESS) {
776         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
777                "ajp_parse_reuse: ajp_msg_get_byte failed");
778         return rc;
779     }
780     if (result != CMD_AJP13_END_RESPONSE) {
781         ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
782                "ajp_parse_reuse: wrong type 0x%02x expecting 0x%02x",
783                result, CMD_AJP13_END_RESPONSE);
784         return AJP_EBAD_HEADER;
785     }
786     return ajp_msg_get_uint8(msg, reuse);
787 }
788
789 /*
790  * Allocate a msg to send data
791  */
792 apr_status_t  ajp_alloc_data_msg(apr_pool_t *pool, char **ptr, apr_size_t *len,
793                                  ajp_msg_t **msg)
794 {
795     apr_status_t rc;
796
797     if ((rc = ajp_msg_create(pool, *len, msg)) != APR_SUCCESS)
798         return rc;
799     ajp_msg_reset(*msg);
800     *ptr = (char *)&((*msg)->buf[6]);
801     *len =  *len - 6;
802
803     return APR_SUCCESS;
804 }
805
806 /*
807  * Send the data message
808  */
809 apr_status_t  ajp_send_data_msg(apr_socket_t *sock,
810                                 ajp_msg_t *msg, apr_size_t len)
811 {
812
813     msg->buf[4] = (apr_byte_t)((len >> 8) & 0xFF);
814     msg->buf[5] = (apr_byte_t)(len & 0xFF);
815
816     msg->len += len + 2; /* + 1 XXXX where is '\0' */
817
818     return ajp_ilink_send(sock, msg);
819
820 }