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