]> granicus.if.org Git - apache/blob - modules/proxy/ajp_header.c
Silence a sparse warning about inconsistent indenting + some minor style issues
[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_CONNECT */
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_rerror(APLOG_MARK, APLOG_TRACE8, 0, r, "Into ajp_marshal_into_msgb");
227
228     if ((method = sc_for_req_method_by_id(r)) == UNKNOWN_METHOD) {
229         ap_log_rerror(APLOG_MARK, APLOG_TRACE8, 0, r, APLOGNO(02437)
230                "ajp_marshal_into_msgb - Sending unknown method %s as request attribute",
231                r->method);
232         method = SC_M_JK_STORED;
233     }
234
235     is_ssl = (apr_byte_t) ap_proxy_conn_is_https(r->connection);
236
237     if (r->headers_in && apr_table_elts(r->headers_in)) {
238         const apr_array_header_t *t = apr_table_elts(r->headers_in);
239         num_headers = t->nelts;
240     }
241
242     remote_host = (char *)ap_get_remote_host(r->connection, r->per_dir_config, REMOTE_HOST, NULL);
243
244     ajp_msg_reset(msg);
245
246     if (ajp_msg_append_uint8(msg, CMD_AJP13_FORWARD_REQUEST)     ||
247         ajp_msg_append_uint8(msg, (apr_byte_t) method)           ||
248         ajp_msg_append_string(msg, r->protocol)                  ||
249         ajp_msg_append_string(msg, uri->path)                    ||
250         ajp_msg_append_string(msg, r->useragent_ip)              ||
251         ajp_msg_append_string(msg, remote_host)                  ||
252         ajp_msg_append_string(msg, ap_get_server_name(r))        ||
253         ajp_msg_append_uint16(msg, (apr_uint16_t)r->connection->local_addr->port) ||
254         ajp_msg_append_uint8(msg, is_ssl)                        ||
255         ajp_msg_append_uint16(msg, (apr_uint16_t) num_headers)) {
256
257         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00968)
258                "ajp_marshal_into_msgb: "
259                "Error appending the message beginning");
260         return APR_EGENERAL;
261     }
262
263     for (i = 0 ; i < num_headers ; i++) {
264         int sc;
265         const apr_array_header_t *t = apr_table_elts(r->headers_in);
266         const apr_table_entry_t *elts = (apr_table_entry_t *)t->elts;
267
268         if ((sc = sc_for_req_header(elts[i].key)) != UNKNOWN_METHOD) {
269             if (ajp_msg_append_uint16(msg, (apr_uint16_t)sc)) {
270                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00969)
271                        "ajp_marshal_into_msgb: "
272                        "Error appending the header name");
273                 return AJP_EOVERFLOW;
274             }
275         }
276         else {
277             if (ajp_msg_append_string(msg, elts[i].key)) {
278                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00970)
279                        "ajp_marshal_into_msgb: "
280                        "Error appending the header name");
281                 return AJP_EOVERFLOW;
282             }
283         }
284
285         if (ajp_msg_append_string(msg, elts[i].val)) {
286             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00971)
287                    "ajp_marshal_into_msgb: "
288                    "Error appending the header value");
289             return AJP_EOVERFLOW;
290         }
291         ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r,
292                    "ajp_marshal_into_msgb: Header[%d] [%s] = [%s]",
293                    i, elts[i].key, elts[i].val);
294     }
295
296 /* XXXX need to figure out how to do this
297     if (s->secret) {
298         if (ajp_msg_append_uint8(msg, SC_A_SECRET) ||
299             ajp_msg_append_string(msg, s->secret)) {
300             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
301                    "Error ajp_marshal_into_msgb - "
302                    "Error appending secret");
303             return APR_EGENERAL;
304         }
305     }
306  */
307
308     if (r->user) {
309         if (ajp_msg_append_uint8(msg, SC_A_REMOTE_USER) ||
310             ajp_msg_append_string(msg, r->user)) {
311             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00972)
312                    "ajp_marshal_into_msgb: "
313                    "Error appending the remote user");
314             return AJP_EOVERFLOW;
315         }
316     }
317     if (r->ap_auth_type) {
318         if (ajp_msg_append_uint8(msg, SC_A_AUTH_TYPE) ||
319             ajp_msg_append_string(msg, r->ap_auth_type)) {
320             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00973)
321                    "ajp_marshal_into_msgb: "
322                    "Error appending the auth type");
323             return AJP_EOVERFLOW;
324         }
325     }
326     /* XXXX  ebcdic (args converted?) */
327     if (uri->query) {
328         if (ajp_msg_append_uint8(msg, SC_A_QUERY_STRING) ||
329             ajp_msg_append_string(msg, uri->query)) {
330             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00974)
331                    "ajp_marshal_into_msgb: "
332                    "Error appending the query string");
333             return AJP_EOVERFLOW;
334         }
335     }
336     if ((session_route = apr_table_get(r->notes, "session-route"))) {
337         if (ajp_msg_append_uint8(msg, SC_A_JVM_ROUTE) ||
338             ajp_msg_append_string(msg, session_route)) {
339             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00975)
340                    "ajp_marshal_into_msgb: "
341                    "Error appending the jvm route");
342             return AJP_EOVERFLOW;
343         }
344     }
345 /* XXX: Is the subprocess_env a right place?
346  * <Location /examples>
347  *   ProxyPass ajp://remote:8009/servlets-examples
348  *   SetEnv SSL_SESSION_ID CUSTOM_SSL_SESSION_ID
349  * </Location>
350  */
351     /*
352      * Only lookup SSL variables if we are currently running HTTPS.
353      * Furthermore ensure that only variables get set in the AJP message
354      * that are not NULL and not empty.
355      */
356     if (is_ssl) {
357         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
358                                        AJP13_SSL_CLIENT_CERT_INDICATOR))
359             && envvar[0]) {
360             if (ajp_msg_append_uint8(msg, SC_A_SSL_CERT)
361                 || ajp_msg_append_string(msg, envvar)) {
362                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00976)
363                               "ajp_marshal_into_msgb: "
364                               "Error appending the SSL certificates");
365                 return AJP_EOVERFLOW;
366             }
367         }
368
369         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
370                                        AJP13_SSL_CIPHER_INDICATOR))
371             && envvar[0]) {
372             if (ajp_msg_append_uint8(msg, SC_A_SSL_CIPHER)
373                 || ajp_msg_append_string(msg, envvar)) {
374                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00977)
375                               "ajp_marshal_into_msgb: "
376                               "Error appending the SSL ciphers");
377                 return AJP_EOVERFLOW;
378             }
379         }
380
381         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
382                                        AJP13_SSL_SESSION_INDICATOR))
383             && envvar[0]) {
384             if (ajp_msg_append_uint8(msg, SC_A_SSL_SESSION)
385                 || ajp_msg_append_string(msg, envvar)) {
386                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00978)
387                               "ajp_marshal_into_msgb: "
388                               "Error appending the SSL session");
389                 return AJP_EOVERFLOW;
390             }
391         }
392
393         /* ssl_key_size is required by Servlet 2.3 API */
394         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
395                                        AJP13_SSL_KEY_SIZE_INDICATOR))
396             && envvar[0]) {
397
398             if (ajp_msg_append_uint8(msg, SC_A_SSL_KEY_SIZE)
399                 || ajp_msg_append_uint16(msg, (unsigned short) atoi(envvar))) {
400                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00979)
401                               "ajp_marshal_into_msgb: "
402                               "Error appending the SSL key size");
403                 return APR_EGENERAL;
404             }
405         }
406     }
407     /* If the method was unrecognized, encode it as an attribute */
408     if (method == SC_M_JK_STORED) {
409         if (ajp_msg_append_uint8(msg, SC_A_STORED_METHOD)
410             || ajp_msg_append_string(msg, r->method)) {
411             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02438)
412                           "ajp_marshal_into_msgb: "
413                           "Error appending the method '%s' as request attribute",
414                           r->method);
415             return AJP_EOVERFLOW;
416         }
417     }
418     /* Forward the SSL protocol name.
419      * Modern Tomcat versions know how to retrieve
420      * the protocol name from this attribute.
421      */
422     if (is_ssl) {
423         if ((envvar = ap_proxy_ssl_val(r->pool, r->server, r->connection, r,
424                                        AJP13_SSL_PROTOCOL_INDICATOR))
425             && envvar[0]) {
426             const char *key = SC_A_SSL_PROTOCOL;
427             if (ajp_msg_append_uint8(msg, SC_A_REQ_ATTRIBUTE) ||
428                 ajp_msg_append_string(msg, key)   ||
429                 ajp_msg_append_string(msg, envvar)) {
430                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02830)
431                         "ajp_marshal_into_msgb: "
432                         "Error appending attribute %s=%s",
433                         key, envvar);
434                 return AJP_EOVERFLOW;
435             }
436         }
437     }
438     /* Forward the remote port information, which was forgotten
439      * from the builtin data of the AJP 13 protocol.
440      * Since the servlet spec allows to retrieve it via getRemotePort(),
441      * we provide the port to the Tomcat connector as a request
442      * attribute. Modern Tomcat versions know how to retrieve
443      * the remote port from this attribute.
444      */
445     {
446         const char *key = SC_A_REQ_REMOTE_PORT;
447         char *val = apr_itoa(r->pool, r->useragent_addr->port);
448         if (ajp_msg_append_uint8(msg, SC_A_REQ_ATTRIBUTE) ||
449             ajp_msg_append_string(msg, key)   ||
450             ajp_msg_append_string(msg, val)) {
451             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00980)
452                     "ajp_marshal_into_msgb: "
453                     "Error appending attribute %s=%s",
454                     key, val);
455             return AJP_EOVERFLOW;
456         }
457     }
458     /* Forward the local ip address information, which was forgotten
459      * from the builtin data of the AJP 13 protocol.
460      * Since the servlet spec allows to retrieve it via getLocalAddr(),
461      * we provide the address to the Tomcat connector as a request
462      * attribute. Modern Tomcat versions know how to retrieve
463      * the local address from this attribute.
464      */
465     {
466         const char *key = SC_A_REQ_LOCAL_ADDR;
467         char *val = r->connection->local_ip;
468         if (ajp_msg_append_uint8(msg, SC_A_REQ_ATTRIBUTE) ||
469             ajp_msg_append_string(msg, key)   ||
470             ajp_msg_append_string(msg, val)) {
471             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02646)
472                     "ajp_marshal_into_msgb: "
473                     "Error appending attribute %s=%s",
474                     key, val);
475             return AJP_EOVERFLOW;
476         }
477     }
478     /* Use the environment vars prefixed with AJP_
479      * and pass it to the header striping that prefix.
480      */
481     for (i = 0; i < (apr_uint32_t)arr->nelts; i++) {
482         if (!strncmp(elts[i].key, "AJP_", 4)) {
483             if (ajp_msg_append_uint8(msg, SC_A_REQ_ATTRIBUTE) ||
484                 ajp_msg_append_string(msg, elts[i].key + 4)   ||
485                 ajp_msg_append_string(msg, elts[i].val)) {
486                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00981)
487                         "ajp_marshal_into_msgb: "
488                         "Error appending attribute %s=%s",
489                         elts[i].key, elts[i].val);
490                 return AJP_EOVERFLOW;
491             }
492         }
493     }
494
495     if (ajp_msg_append_uint8(msg, SC_A_ARE_DONE)) {
496         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00982)
497                "ajp_marshal_into_msgb: "
498                "Error appending the message end");
499         return AJP_EOVERFLOW;
500     }
501
502     ap_log_rerror(APLOG_MARK, APLOG_TRACE8, 0, r,
503             "ajp_marshal_into_msgb: Done");
504     return APR_SUCCESS;
505 }
506
507 /*
508 AJPV13_RESPONSE/AJPV14_RESPONSE:=
509     response_prefix (2)
510     status          (short)
511     status_msg      (short)
512     num_headers     (short)
513     num_headers*(res_header_name header_value)
514     *body_chunk
515     terminator      boolean <! -- recycle connection or not  -->
516
517 req_header_name :=
518     sc_req_header_name | (string)
519
520 res_header_name :=
521     sc_res_header_name | (string)
522
523 header_value :=
524     (string)
525
526 body_chunk :=
527     length  (short)
528     body    length*(var binary)
529
530  */
531
532 static int addit_dammit(void *v, const char *key, const char *val)
533 {
534     apr_table_addn(v, key, val);
535     return 1;
536 }
537
538 static apr_status_t ajp_unmarshal_response(ajp_msg_t *msg,
539                                            request_rec *r,
540                                            proxy_dir_conf *dconf)
541 {
542     apr_uint16_t status;
543     apr_status_t rc;
544     const char *ptr;
545     apr_uint16_t  num_headers;
546     int i;
547
548     rc = ajp_msg_get_uint16(msg, &status);
549
550     if (rc != APR_SUCCESS) {
551         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00983)
552                 "ajp_unmarshal_response: Null status");
553         return rc;
554     }
555     r->status = status;
556
557     rc = ajp_msg_get_string(msg, &ptr);
558     if (rc == APR_SUCCESS) {
559 #if APR_CHARSET_EBCDIC /* copy only if we have to */
560         ptr = apr_pstrdup(r->pool, ptr);
561         ap_xlate_proto_from_ascii(ptr, strlen(ptr));
562 #endif
563         r->status_line =  apr_psprintf(r->pool, "%d %s", status, ptr);
564     }
565     else {
566         r->status_line = NULL;
567     }
568
569     ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r,
570            "ajp_unmarshal_response: status = %d", status);
571
572     rc = ajp_msg_get_uint16(msg, &num_headers);
573     if (rc == APR_SUCCESS) {
574         apr_table_t *save_table;
575
576         /* First, tuck away all already existing cookies */
577         /*
578          * Could optimize here, but just in case we want to
579          * also save other headers, keep this logic.
580          */
581         save_table = apr_table_make(r->pool, num_headers + 2);
582         apr_table_do(addit_dammit, save_table, r->headers_out,
583                      "Set-Cookie", NULL);
584         r->headers_out = save_table;
585     }
586     else {
587         r->headers_out = NULL;
588         num_headers = 0;
589     }
590
591     ap_log_rerror(APLOG_MARK, APLOG_TRACE4, 0, r,
592            "ajp_unmarshal_response: Number of headers is = %d",
593            num_headers);
594
595     for (i = 0; i < (int)num_headers; i++) {
596         apr_uint16_t name;
597         const char *stringname;
598         const char *value;
599         rc  = ajp_msg_peek_uint16(msg, &name);
600         if (rc != APR_SUCCESS) {
601             return rc;
602         }
603
604         if ((name & 0XFF00) == 0XA000) {
605             ajp_msg_get_uint16(msg, &name);
606             stringname = long_res_header_for_sc(name);
607             if (stringname == NULL) {
608                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00984)
609                        "ajp_unmarshal_response: "
610                        "No such sc (%08x)",
611                        name);
612                 return AJP_EBAD_HEADER;
613             }
614         }
615         else {
616             name = 0;
617             rc = ajp_msg_get_string(msg, &stringname);
618             if (rc != APR_SUCCESS) {
619                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00985)
620                        "ajp_unmarshal_response: "
621                        "Null header name");
622                 return rc;
623             }
624             ap_xlate_proto_from_ascii(stringname, strlen(stringname));
625         }
626
627         rc = ajp_msg_get_string(msg, &value);
628         if (rc != APR_SUCCESS) {
629             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00986)
630                    "ajp_unmarshal_response: "
631                    "Null header value");
632             return rc;
633         }
634
635         /* Set-Cookie need additional processing */
636         if (!strcasecmp(stringname, "Set-Cookie")) {
637             value = ap_proxy_cookie_reverse_map(r, dconf, value);
638         }
639         /* Location, Content-Location, URI and Destination need additional
640          * processing */
641         else if (!strcasecmp(stringname, "Location")
642                  || !strcasecmp(stringname, "Content-Location")
643                  || !strcasecmp(stringname, "URI")
644                  || !strcasecmp(stringname, "Destination"))
645         {
646           value = ap_proxy_location_reverse_map(r, dconf, value);
647         }
648
649         ap_xlate_proto_from_ascii(value, strlen(value));
650         ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r,
651                "ajp_unmarshal_response: Header[%d] [%s] = [%s]",
652                        i, stringname, value);
653
654         apr_table_add(r->headers_out, stringname, value);
655
656         /* Content-type needs an additional handling */
657         if (strcasecmp(stringname, "Content-Type") == 0) {
658              /* add corresponding filter */
659             ap_set_content_type(r, apr_pstrdup(r->pool, value));
660             ap_log_rerror(APLOG_MARK, APLOG_TRACE5, 0, r,
661                "ajp_unmarshal_response: ap_set_content_type to '%s'", value);
662         }
663     }
664
665     return APR_SUCCESS;
666 }
667
668 /*
669  * Build the ajp header message and send it
670  */
671 apr_status_t ajp_send_header(apr_socket_t *sock,
672                              request_rec *r,
673                              apr_size_t buffsize,
674                              apr_uri_t *uri)
675 {
676     ajp_msg_t *msg;
677     apr_status_t rc;
678
679     rc = ajp_msg_create(r->pool, buffsize, &msg);
680     if (rc != APR_SUCCESS) {
681         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00987)
682                "ajp_send_header: ajp_msg_create failed");
683         return rc;
684     }
685
686     rc = ajp_marshal_into_msgb(msg, r, uri);
687     if (rc != APR_SUCCESS) {
688         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00988)
689                "ajp_send_header: ajp_marshal_into_msgb failed");
690         return rc;
691     }
692
693     rc = ajp_ilink_send(sock, msg);
694     ajp_msg_log(r, msg, "ajp_send_header: ajp_ilink_send packet dump");
695     if (rc != APR_SUCCESS) {
696         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00989)
697                "ajp_send_header: ajp_ilink_send failed");
698         return rc;
699     }
700
701     return APR_SUCCESS;
702 }
703
704 /*
705  * Read the ajp message and return the type of the message.
706  */
707 apr_status_t ajp_read_header(apr_socket_t *sock,
708                              request_rec  *r,
709                              apr_size_t buffsize,
710                              ajp_msg_t **msg)
711 {
712     apr_byte_t result;
713     apr_status_t rc;
714
715     if (*msg) {
716         rc = ajp_msg_reuse(*msg);
717         if (rc != APR_SUCCESS) {
718             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00990)
719                    "ajp_read_header: ajp_msg_reuse failed");
720             return rc;
721         }
722     }
723     else {
724         rc = ajp_msg_create(r->pool, buffsize, msg);
725         if (rc != APR_SUCCESS) {
726             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00991)
727                    "ajp_read_header: ajp_msg_create failed");
728             return rc;
729         }
730     }
731     ajp_msg_reset(*msg);
732     rc = ajp_ilink_receive(sock, *msg);
733     if (rc != APR_SUCCESS) {
734         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00992)
735                "ajp_read_header: ajp_ilink_receive failed");
736         return rc;
737     }
738     ajp_msg_log(r, *msg, "ajp_read_header: ajp_ilink_receive packet dump");
739     rc = ajp_msg_peek_uint8(*msg, &result);
740     if (rc != APR_SUCCESS) {
741         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00993)
742                       "ajp_read_header: ajp_msg_peek_uint8 failed");
743         return rc;
744     }
745     ap_log_rerror(APLOG_MARK, APLOG_TRACE1, 0, r,
746                "ajp_read_header: ajp_ilink_received %s (0x%02x)",
747                ajp_type_str(result), result);
748     return APR_SUCCESS;
749 }
750
751 /* parse the msg to read the type */
752 int ajp_parse_type(request_rec  *r, ajp_msg_t *msg)
753 {
754     apr_byte_t result;
755     ajp_msg_peek_uint8(msg, &result);
756     ap_log_rerror(APLOG_MARK, APLOG_TRACE6, 0, r,
757                "ajp_parse_type: got %s (0x%02x)",
758                ajp_type_str(result), result);
759     return (int) result;
760 }
761
762 /* parse the header */
763 apr_status_t ajp_parse_header(request_rec  *r, proxy_dir_conf *conf,
764                               ajp_msg_t *msg)
765 {
766     apr_byte_t result;
767     apr_status_t rc;
768
769     rc = ajp_msg_get_uint8(msg, &result);
770     if (rc != APR_SUCCESS) {
771         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00994)
772                "ajp_parse_headers: ajp_msg_get_byte failed");
773         return rc;
774     }
775     if (result != CMD_AJP13_SEND_HEADERS) {
776         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00995)
777                "ajp_parse_headers: wrong type %s (0x%02x) expecting %s (0x%02x)",
778                ajp_type_str(result), result,
779                ajp_type_str(CMD_AJP13_SEND_HEADERS), CMD_AJP13_SEND_HEADERS);
780         return AJP_EBAD_HEADER;
781     }
782     return ajp_unmarshal_response(msg, r, conf);
783 }
784
785 /* parse the body and return data address and length */
786 apr_status_t  ajp_parse_data(request_rec  *r, ajp_msg_t *msg,
787                              apr_uint16_t *len, char **ptr)
788 {
789     apr_byte_t result;
790     apr_status_t rc;
791     apr_uint16_t expected_len;
792
793     rc = ajp_msg_get_uint8(msg, &result);
794     if (rc != APR_SUCCESS) {
795         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00996)
796                "ajp_parse_data: ajp_msg_get_byte failed");
797         return rc;
798     }
799     if (result != CMD_AJP13_SEND_BODY_CHUNK) {
800         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00997)
801                "ajp_parse_data: wrong type %s (0x%02x) expecting %s (0x%02x)",
802                ajp_type_str(result), result,
803                ajp_type_str(CMD_AJP13_SEND_BODY_CHUNK), CMD_AJP13_SEND_BODY_CHUNK);
804         return AJP_EBAD_HEADER;
805     }
806     rc = ajp_msg_get_uint16(msg, len);
807     if (rc != APR_SUCCESS) {
808         return rc;
809     }
810     /*
811      * msg->len contains the complete length of the message including all
812      * headers. So the expected length for a CMD_AJP13_SEND_BODY_CHUNK is
813      * msg->len minus the sum of
814      * AJP_HEADER_LEN    : The length of the header to every AJP message.
815      * AJP_HEADER_SZ_LEN : The header giving the size of the chunk.
816      * 1                 : The CMD_AJP13_SEND_BODY_CHUNK indicator byte (0x03).
817      * 1                 : The last byte of this message always seems to be
818      *                     0x00 and is not part of the chunk.
819      */
820     expected_len = msg->len - (AJP_HEADER_LEN + AJP_HEADER_SZ_LEN + 1 + 1);
821     if (*len != expected_len) {
822         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00998)
823                "ajp_parse_data: Wrong chunk length. Length of chunk is %i,"
824                " expected length is %i.", *len, expected_len);
825         return AJP_EBAD_HEADER;
826     }
827     *ptr = (char *)&(msg->buf[msg->pos]);
828     return APR_SUCCESS;
829 }
830
831 /* Check the reuse flag in CMD_AJP13_END_RESPONSE */
832 apr_status_t ajp_parse_reuse(request_rec *r, ajp_msg_t *msg,
833                              apr_byte_t *reuse)
834 {
835     apr_byte_t result;
836     apr_status_t rc;
837
838     rc = ajp_msg_get_uint8(msg, &result);
839     if (rc != APR_SUCCESS) {
840         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00999)
841                "ajp_parse_reuse: ajp_msg_get_byte failed");
842         return rc;
843     }
844     if (result != CMD_AJP13_END_RESPONSE) {
845         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01000)
846                "ajp_parse_reuse: wrong type %s (0x%02x) expecting %s (0x%02x)",
847                ajp_type_str(result), result,
848                ajp_type_str(CMD_AJP13_END_RESPONSE), CMD_AJP13_END_RESPONSE);
849         return AJP_EBAD_HEADER;
850     }
851     return ajp_msg_get_uint8(msg, reuse);
852 }
853
854 /*
855  * Allocate a msg to send data
856  */
857 apr_status_t  ajp_alloc_data_msg(apr_pool_t *pool, char **ptr, apr_size_t *len,
858                                  ajp_msg_t **msg)
859 {
860     apr_status_t rc;
861
862     if ((rc = ajp_msg_create(pool, *len, msg)) != APR_SUCCESS)
863         return rc;
864     ajp_msg_reset(*msg);
865     *ptr = (char *)&((*msg)->buf[6]);
866     *len =  *len - 6;
867
868     return APR_SUCCESS;
869 }
870
871 /*
872  * Send the data message
873  */
874 apr_status_t  ajp_send_data_msg(apr_socket_t *sock,
875                                 ajp_msg_t *msg, apr_size_t len)
876 {
877
878     msg->buf[4] = (apr_byte_t)((len >> 8) & 0xFF);
879     msg->buf[5] = (apr_byte_t)(len & 0xFF);
880
881     msg->len += len + 2; /* + 1 XXXX where is '\0' */
882
883     return ajp_ilink_send(sock, msg);
884
885 }