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