Newer
Older
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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.
*/
/*
* mod_isapi.c - Internet Server Application (ISA) module for Apache
* by Alexei Kosut <akosut@apache.org>, significant overhauls and
* redesign by William Rowe <wrowe@covalent.net>, and hints from many
* other developer/users who have hit on specific flaws.
*
* This module implements the ISAPI Handler architecture, allowing
* Apache to load Internet Server Applications (ISAPI extensions),
* similar to the support in IIS, Zope, O'Reilly's WebSite and others.
*
* It is a complete implementation of the ISAPI 2.0 specification,
* except for "Microsoft extensions" to the API which provide
* asynchronous I/O. It is further extended to include additional
* "Microsoft extensions" through IIS 5.0, with some deficiencies
* where one-to-one mappings don't exist.
*
* Refer to /manual/mod/mod_isapi.html for additional details on
* configuration and use, but check this source for specific support
* of the API,
*/
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"
#include "http_log.h"
#include "util_script.h"
#include "mod_core.h"
#include "apr_lib.h"
#include "apr_strings.h"
#include "apr_portable.h"
#include "apr_buckets.h"
#include "apr_thread_mutex.h"
#include "apr_thread_rwlock.h"
#include "apr_hash.h"
#include "mod_isapi.h"
/* Retry frequency for a failed-to-load isapi .dll */
#define ISAPI_RETRY apr_time_from_sec(30)
/**********************************************************
*
* ISAPI Module Configuration
*
**********************************************************/
module AP_MODULE_DECLARE_DATA isapi_module;
#define ISAPI_UNDEF -1
/* Our isapi per-dir config structure */
typedef struct isapi_dir_conf {
int read_ahead_buflen;
int log_unsupported;
int log_to_errlog;
int log_to_query;
int fake_async;
} isapi_dir_conf;
typedef struct isapi_loaded isapi_loaded;
apr_status_t isapi_lookup(apr_pool_t *p, server_rec *s, request_rec *r,
const char *fpath, isapi_loaded** isa);
static void *create_isapi_dir_config(apr_pool_t *p, char *dummy)
{
isapi_dir_conf *dir = apr_palloc(p, sizeof(isapi_dir_conf));
dir->read_ahead_buflen = ISAPI_UNDEF;
dir->log_unsupported = ISAPI_UNDEF;
dir->log_to_errlog = ISAPI_UNDEF;
dir->log_to_query = ISAPI_UNDEF;
dir->fake_async = ISAPI_UNDEF;
return dir;
}
static void *merge_isapi_dir_configs(apr_pool_t *p, void *base_, void *add_)
{
isapi_dir_conf *base = (isapi_dir_conf *) base_;
isapi_dir_conf *add = (isapi_dir_conf *) add_;
isapi_dir_conf *dir = apr_palloc(p, sizeof(isapi_dir_conf));
dir->read_ahead_buflen = (add->read_ahead_buflen == ISAPI_UNDEF)
? base->read_ahead_buflen
: add->read_ahead_buflen;
dir->log_unsupported = (add->log_unsupported == ISAPI_UNDEF)
? base->log_unsupported
: add->log_unsupported;
dir->log_to_errlog = (add->log_to_errlog == ISAPI_UNDEF)
? base->log_to_errlog
: add->log_to_errlog;
dir->log_to_query = (add->log_to_query == ISAPI_UNDEF)
? base->log_to_query
: add->log_to_query;
dir->fake_async = (add->fake_async == ISAPI_UNDEF)
? base->fake_async
: add->fake_async;
return dir;
}
static const char *isapi_cmd_cachefile(cmd_parms *cmd, void *dummy,
const char *filename)
{
isapi_loaded *isa;
apr_finfo_t tmp;
apr_status_t rv;
char *fspec;
/* ### Just an observation ... it would be terribly cool to be
* able to use this per-dir, relative to the directory block being
* defined. The hash result remains global, but shorthand of
* <Directory "c:/webapps/isapi">
* ISAPICacheFile myapp.dll anotherapp.dll thirdapp.dll
* </Directory>
* would be very convienent.
*/
fspec = ap_server_root_relative(cmd->pool, filename);
if (!fspec) {
ap_log_error(APLOG_MARK, APLOG_WARNING, APR_EBADPATH, cmd->server, APLOGNO(02103)
"invalid module path, skipping %s", filename);
return NULL;
}
if ((rv = apr_stat(&tmp, fspec, APR_FINFO_TYPE,
cmd->temp_pool)) != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, cmd->server, APLOGNO(02104)
"unable to stat, skipping %s", fspec);
return NULL;
}
if (tmp.filetype != APR_REG) {
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(02105)
"not a regular file, skipping %s", fspec);
return NULL;
}
/* Load the extension as cached (with null request_rec) */
rv = isapi_lookup(cmd->pool, cmd->server, NULL, fspec, &isa);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_WARNING, rv, cmd->server, APLOGNO(02106)
"unable to cache, skipping %s", fspec);
return NULL;
}
return NULL;
}
static const command_rec isapi_cmds[] = {
AP_INIT_TAKE1("ISAPIReadAheadBuffer", ap_set_int_slot,
(void *)APR_OFFSETOF(isapi_dir_conf, read_ahead_buflen),
OR_FILEINFO, "Maximum client request body to initially pass to the"
" ISAPI handler (default: 49152)"),
AP_INIT_FLAG("ISAPILogNotSupported", ap_set_flag_slot,
(void *)APR_OFFSETOF(isapi_dir_conf, log_unsupported),
OR_FILEINFO, "Log requests not supported by the ISAPI server"
" on or off (default: off)"),
AP_INIT_FLAG("ISAPIAppendLogToErrors", ap_set_flag_slot,
(void *)APR_OFFSETOF(isapi_dir_conf, log_to_errlog),
OR_FILEINFO, "Send all Append Log requests to the error log"
" on or off (default: off)"),
AP_INIT_FLAG("ISAPIAppendLogToQuery", ap_set_flag_slot,
(void *)APR_OFFSETOF(isapi_dir_conf, log_to_query),
OR_FILEINFO, "Append Log requests are concatinated to the query args"
" on or off (default: on)"),
AP_INIT_FLAG("ISAPIFakeAsync", ap_set_flag_slot,
(void *)APR_OFFSETOF(isapi_dir_conf, fake_async),
OR_FILEINFO, "Fake Asynchronous support for isapi callbacks"
" on or off [Experimental] (default: off)"),
AP_INIT_ITERATE("ISAPICacheFile", isapi_cmd_cachefile, NULL,
RSRC_CONF, "Cache the specified ISAPI extension in-process"),
{NULL}
};
/**********************************************************
*
* ISAPI Module Cache handling section
*
**********************************************************/
/* Our isapi global config values */
static struct isapi_global_conf {
apr_pool_t *pool;
apr_thread_mutex_t *lock;
apr_hash_t *hash;
} loaded;
/* Our loaded isapi module description structure */
struct isapi_loaded {
const char *filename;
apr_thread_rwlock_t *in_progress;
apr_status_t last_load_rv;
apr_time_t last_load_time;
apr_dso_handle_t *handle;
HSE_VERSION_INFO *isapi_version;
apr_uint32_t report_version;
apr_uint32_t timeout;
PFN_GETEXTENSIONVERSION GetExtensionVersion;
PFN_HTTPEXTENSIONPROC HttpExtensionProc;
PFN_TERMINATEEXTENSION TerminateExtension;
};
static apr_status_t isapi_unload(isapi_loaded *isa, int force)
{
/* All done with the DLL... get rid of it...
*
* If optionally cached, and we weren't asked to force the unload,
* pass HSE_TERM_ADVISORY_UNLOAD, and if it returns 1, unload,
* otherwise, leave it alone (it didn't choose to cooperate.)
*/
if (!isa->handle) {
return APR_SUCCESS;
}
if (isa->TerminateExtension) {
if (force) {
(*isa->TerminateExtension)(HSE_TERM_MUST_UNLOAD);
}
else if (!(*isa->TerminateExtension)(HSE_TERM_ADVISORY_UNLOAD)) {
return APR_EGENERAL;
}
}
apr_dso_unload(isa->handle);
isa->handle = NULL;
return APR_SUCCESS;
}
static apr_status_t cleanup_isapi(void *isa_)
{
isapi_loaded* isa = (isapi_loaded*) isa_;
/* We must force the module to unload, we are about
* to lose the isapi structure's allocation entirely.
*/
return isapi_unload(isa, 1);
}
static apr_status_t isapi_load(apr_pool_t *p, server_rec *s, isapi_loaded *isa)
{
apr_status_t rv;
isa->isapi_version = apr_pcalloc(p, sizeof(HSE_VERSION_INFO));
/* TODO: These aught to become overrideable, so that we
* assure a given isapi can be fooled into behaving well.
*
* The tricky bit, they aren't really a per-dir sort of
* config, they will always be constant across every
* reference to the .dll no matter what context (vhost,
* location, etc) they apply to.
*/
isa->report_version = 0x500; /* Revision 5.0 */
isa->timeout = 300 * 1000000; /* microsecs, not used */
rv = apr_dso_load(&isa->handle, isa->filename, p);
if (rv)
{
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(02107)
"failed to load %s", isa->filename);
isa->handle = NULL;
return rv;
}
rv = apr_dso_sym((void**)&isa->GetExtensionVersion, isa->handle,
"GetExtensionVersion");
if (rv)
{
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(02108)
"missing GetExtensionVersion() in %s",
isa->filename);
apr_dso_unload(isa->handle);
isa->handle = NULL;
return rv;
}
rv = apr_dso_sym((void**)&isa->HttpExtensionProc, isa->handle,
"HttpExtensionProc");
if (rv)
{
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(02109)
"missing HttpExtensionProc() in %s",
isa->filename);
apr_dso_unload(isa->handle);
isa->handle = NULL;
return rv;
}
/* TerminateExtension() is an optional interface */
rv = apr_dso_sym((void**)&isa->TerminateExtension, isa->handle,
"TerminateExtension");
apr_set_os_error(0);
/* Run GetExtensionVersion() */
if (!(isa->GetExtensionVersion)(isa->isapi_version)) {
apr_status_t rv = apr_get_os_error();
ap_log_error(APLOG_MARK, APLOG_ERR, rv, s, APLOGNO(02110)
"failed call to GetExtensionVersion() in %s",
isa->filename);
apr_dso_unload(isa->handle);
isa->handle = NULL;
return rv;
}
apr_pool_cleanup_register(p, isa, cleanup_isapi,
apr_pool_cleanup_null);
return APR_SUCCESS;
}
apr_status_t isapi_lookup(apr_pool_t *p, server_rec *s, request_rec *r,
const char *fpath, isapi_loaded** isa)
{
apr_status_t rv;
const char *key;
if ((rv = apr_thread_mutex_lock(loaded.lock)) != APR_SUCCESS) {
return rv;
}
*isa = apr_hash_get(loaded.hash, fpath, APR_HASH_KEY_STRING);
if (*isa) {
/* If we find this lock exists, use a set-aside copy of gainlock
* to avoid race conditions on NULLing the in_progress variable
* when the load has completed. Release the global isapi hash
* lock so other requests can proceed, then rdlock for completion
* of loading our desired dll or wrlock if we would like to retry
* loading the dll (because last_load_rv failed and retry is up.)
*/
apr_thread_rwlock_t *gainlock = (*isa)->in_progress;
/* gainlock is NULLed after the module loads successfully.
* This free-threaded module can be used without any locking.
*/
if (!gainlock) {
rv = (*isa)->last_load_rv;
apr_thread_mutex_unlock(loaded.lock);
return rv;
}
if ((*isa)->last_load_rv == APR_SUCCESS) {
apr_thread_mutex_unlock(loaded.lock);
if ((rv = apr_thread_rwlock_rdlock(gainlock))
!= APR_SUCCESS) {
return rv;
}
rv = (*isa)->last_load_rv;
apr_thread_rwlock_unlock(gainlock);
return rv;
}
if (apr_time_now() > (*isa)->last_load_time + ISAPI_RETRY) {
/* Remember last_load_time before releasing the global
* hash lock to avoid colliding with another thread
* that hit this exception at the same time as our
* retry attempt, since we unlock the global mutex
* before attempting a write lock for this module.
*/
apr_time_t check_time = (*isa)->last_load_time;
apr_thread_mutex_unlock(loaded.lock);
if ((rv = apr_thread_rwlock_wrlock(gainlock))
!= APR_SUCCESS) {
return rv;
}
/* If last_load_time is unchanged, we still own this
* retry, otherwise presume another thread provided
* our retry (for good or ill). Relock the global
* hash for updating last_load_ vars, so their update
* is always atomic to the global lock.
*/
if (check_time == (*isa)->last_load_time) {
rv = isapi_load(loaded.pool, s, *isa);
apr_thread_mutex_lock(loaded.lock);
(*isa)->last_load_rv = rv;
(*isa)->last_load_time = apr_time_now();
apr_thread_mutex_unlock(loaded.lock);
}
else {
rv = (*isa)->last_load_rv;
}
apr_thread_rwlock_unlock(gainlock);
return rv;
}
/* We haven't hit timeup on retry, let's grab the last_rv
* within the hash mutex before unlocking.
*/
rv = (*isa)->last_load_rv;
apr_thread_mutex_unlock(loaded.lock);
return rv;
}
/* If the module was not found, it's time to create a hash key entry
* before releasing the hash lock to avoid multiple threads from
* loading the same module.
*/
key = apr_pstrdup(loaded.pool, fpath);
*isa = apr_pcalloc(loaded.pool, sizeof(isapi_loaded));
(*isa)->filename = key;
if (r) {
/* A mutex that exists only long enough to attempt to
* load this isapi dll, the release this module to all
* other takers that came along during the one-time
* load process. Short lifetime for this lock would
* be great, however, using r->pool is nasty if those
* blocked on the lock haven't all unlocked before we
* attempt to destroy. A nastier race condition than
* I want to deal with at this moment...
*/
apr_thread_rwlock_create(&(*isa)->in_progress, loaded.pool);
apr_thread_rwlock_wrlock((*isa)->in_progress);
}
apr_hash_set(loaded.hash, key, APR_HASH_KEY_STRING, *isa);
/* Now attempt to load the isapi on our own time,
* allow other isapi processing to resume.
*/
apr_thread_mutex_unlock(loaded.lock);
rv = isapi_load(loaded.pool, s, *isa);
(*isa)->last_load_time = apr_time_now();
(*isa)->last_load_rv = rv;
if (r && (rv == APR_SUCCESS)) {
/* Let others who are blocked on this particular
* module resume their requests, for better or worse.
*/
apr_thread_rwlock_t *unlock = (*isa)->in_progress;
(*isa)->in_progress = NULL;
apr_thread_rwlock_unlock(unlock);
}
else if (!r && (rv != APR_SUCCESS)) {
/* We must leave a rwlock around for requests to retry
* loading this dll after timeup... since we were in
* the setup code we had avoided creating this lock.
*/
apr_thread_rwlock_create(&(*isa)->in_progress, loaded.pool);
}
return (*isa)->last_load_rv;
}
/**********************************************************
*
* ISAPI Module request callbacks section
*
**********************************************************/
/* Our "Connection ID" structure */
struct isapi_cid {
EXTENSION_CONTROL_BLOCK *ecb;
isapi_dir_conf dconf;
isapi_loaded *isa;
request_rec *r;
int headers_set;
int response_sent;
PFN_HSE_IO_COMPLETION completion;
void *completion_arg;
apr_thread_mutex_t *completed;
};
static int APR_THREAD_FUNC regfnGetServerVariable(isapi_cid *cid,
char *variable_name,
void *buf_ptr,
apr_uint32_t *buf_size)
{
request_rec *r = cid->r;
const char *result;
char *buf_data = (char*)buf_ptr;
apr_uint32_t len;
if (!strcmp(variable_name, "ALL_HTTP"))
{
/* crlf delimited, colon split, comma separated and
* null terminated list of HTTP_ vars
*/
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;
int i;
for (len = 0, i = 0; i < arr->nelts; i++) {
if (!strncmp(elts[i].key, "HTTP_", 5)) {
len += strlen(elts[i].key) + strlen(elts[i].val) + 3;
}
}
if (*buf_size < len + 1) {
*buf_size = len + 1;
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_INSUFFICIENT_BUFFER));
return 0;
}
for (i = 0; i < arr->nelts; i++) {
if (!strncmp(elts[i].key, "HTTP_", 5)) {
strcpy(buf_data, elts[i].key);
buf_data += strlen(elts[i].key);
*(buf_data++) = ':';
strcpy(buf_data, elts[i].val);
buf_data += strlen(elts[i].val);
*(buf_data++) = '\r';
*(buf_data++) = '\n';
}
}
*(buf_data++) = '\0';
*buf_size = len + 1;
return 1;
}
if (!strcmp(variable_name, "ALL_RAW"))
{
/* crlf delimited, colon split, comma separated and
* null terminated list of the raw request header
*/
const apr_array_header_t *arr = apr_table_elts(r->headers_in);
const apr_table_entry_t *elts = (const apr_table_entry_t *)arr->elts;
int i;
for (len = 0, i = 0; i < arr->nelts; i++) {
len += strlen(elts[i].key) + strlen(elts[i].val) + 4;
}
if (*buf_size < len + 1) {
*buf_size = len + 1;
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_INSUFFICIENT_BUFFER));
return 0;
}
for (i = 0; i < arr->nelts; i++) {
strcpy(buf_data, elts[i].key);
buf_data += strlen(elts[i].key);
*(buf_data++) = ':';
*(buf_data++) = ' ';
strcpy(buf_data, elts[i].val);
buf_data += strlen(elts[i].val);
*(buf_data++) = '\r';
*(buf_data++) = '\n';
}
*(buf_data++) = '\0';
*buf_size = len + 1;
return 1;
}
/* Not a special case */
result = apr_table_get(r->subprocess_env, variable_name);
if (result) {
len = strlen(result);
if (*buf_size < len + 1) {
*buf_size = len + 1;
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_INSUFFICIENT_BUFFER));
return 0;
}
strcpy(buf_data, result);
*buf_size = len + 1;
return 1;
}
/* Not Found */
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_INVALID_INDEX));
return 0;
}
static int APR_THREAD_FUNC regfnReadClient(isapi_cid *cid,
void *buf_data,
apr_uint32_t *buf_size)
{
request_rec *r = cid->r;
apr_uint32_t read = 0;
int res = 0;
if (r->remaining < *buf_size) {
*buf_size = (apr_size_t)r->remaining;
}
while (read < *buf_size &&
((res = ap_get_client_block(r, (char*)buf_data + read,
*buf_size - read)) > 0)) {
read += res;
}
*buf_size = read;
if (res < 0) {
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_READ_FAULT));
}
return (res >= 0);
}
/* Common code invoked for both HSE_REQ_SEND_RESPONSE_HEADER and
* the newer HSE_REQ_SEND_RESPONSE_HEADER_EX ServerSupportFunction(s)
* as well as other functions that write responses and presume that
* the support functions above are optional.
*
* Other callers trying to split headers and body bytes should pass
* head/headlen alone (leaving stat/statlen NULL/0), so that they
* get a proper count of bytes consumed. The argument passed to stat
* isn't counted as the head bytes are.
*/
static apr_ssize_t send_response_header(isapi_cid *cid,
const char *stat,
const char *head,
apr_size_t statlen,
apr_size_t headlen)
{
int head_present = 1;
int termarg;
int res;
int old_status;
const char *termch;
apr_size_t ate = 0;
if (!head || headlen == 0 || !*head) {
head = stat;
stat = NULL;
headlen = statlen;
statlen = 0;
head_present = 0; /* Don't eat the header */
}
if (!stat || statlen == 0 || !*stat) {
if (head && headlen && *head && ((stat = memchr(head, '\r', headlen))
|| (stat = memchr(head, '\n', headlen))
|| (stat = memchr(head, '\0', headlen))
|| (stat = head + headlen))) {
statlen = stat - head;
if (memchr(head, ':', statlen)) {
stat = "Status: 200 OK";
statlen = strlen(stat);
}
else {
const char *flip = head;
head = stat;
stat = flip;
headlen -= statlen;
ate += statlen;
if (*head == '\r' && headlen)
++head, --headlen, ++ate;
if (*head == '\n' && headlen)
++head, --headlen, ++ate;
}
}
}
if (stat && (statlen > 0) && *stat) {
char *newstat;
if (!apr_isdigit(*stat)) {
const char *stattok = stat;
int toklen = statlen;
while (toklen && *stattok && !apr_isspace(*stattok)) {
++stattok; --toklen;
}
while (toklen && apr_isspace(*stattok)) {
++stattok; --toklen;
}
/* Now decide if we follow the xxx message
* or the http/x.x xxx message format
*/
if (toklen && apr_isdigit(*stattok)) {
statlen = toklen;
stat = stattok;
}
}
newstat = apr_palloc(cid->r->pool, statlen + 9);
strcpy(newstat, "Status: ");
apr_cpystrn(newstat + 8, stat, statlen + 1);
stat = newstat;
statlen += 8;
}
if (!head || headlen == 0 || !*head) {
head = "\r\n";
headlen = 2;
}
else
{
if (head[headlen - 1] && head[headlen]) {
/* Whoops... not NULL terminated */
head = apr_pstrndup(cid->r->pool, head, headlen);
}
}
/* Seems IIS does not enforce the requirement for \r\n termination
* on HSE_REQ_SEND_RESPONSE_HEADER, but we won't panic...
* ap_scan_script_header_err_strs handles this aspect for us.
*
* Parse them out, or die trying
*/
old_status = cid->r->status;
if (stat) {
res = ap_scan_script_header_err_strs_ex(cid->r, NULL,
APLOG_MODULE_INDEX, &termch, &termarg, stat, head, NULL);
}
else {
res = ap_scan_script_header_err_strs_ex(cid->r, NULL,
APLOG_MODULE_INDEX, &termch, &termarg, head, NULL);
}
/* Set our status. */
if (res) {
/* This is an immediate error result from the parser
*/
cid->r->status = res;
cid->r->status_line = ap_get_status_line(cid->r->status);
cid->ecb->dwHttpStatusCode = cid->r->status;
}
else if (cid->r->status) {
/* We have a status in r->status, so let's just use it.
* This is likely to be the Status: parsed above, and
* may also be a delayed error result from the parser.
* If it was filled in, status_line should also have
* been filled in.
*/
cid->ecb->dwHttpStatusCode = cid->r->status;
}
else if (cid->ecb->dwHttpStatusCode
&& cid->ecb->dwHttpStatusCode != HTTP_OK) {
/* Now we fall back on dwHttpStatusCode if it appears
* ap_scan_script_header fell back on the default code.
* Any other results set dwHttpStatusCode to the decoded
* status value.
*/
cid->r->status = cid->ecb->dwHttpStatusCode;
cid->r->status_line = ap_get_status_line(cid->r->status);
}
else if (old_status) {
/* Well... either there is no dwHttpStatusCode or it's HTTP_OK.
* In any case, we don't have a good status to return yet...
* Perhaps the one we came in with will be better. Let's use it,
* if we were given one (note this is a pedantic case, it would
* normally be covered above unless the scan script code unset
* the r->status). Should there be a check here as to whether
* we are setting a valid response code?
*/
cid->r->status = old_status;
cid->r->status_line = ap_get_status_line(cid->r->status);
cid->ecb->dwHttpStatusCode = cid->r->status;
}
else {
/* None of dwHttpStatusCode, the parser's r->status nor the
* old value of r->status were helpful, and nothing was decoded
* from Status: string passed to us. Let's just say HTTP_OK
* and get the data out, this was the isapi dev's oversight.
*/
cid->r->status = HTTP_OK;
cid->r->status_line = ap_get_status_line(cid->r->status);
cid->ecb->dwHttpStatusCode = cid->r->status;
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, cid->r, APLOGNO(02111)
"Could not determine HTTP response code; using %d",
cid->r->status);
}
if (cid->r->status == HTTP_INTERNAL_SERVER_ERROR) {
return -1;
}
/* If only Status was passed, we consumed nothing
*/
if (!head_present)
return 0;
cid->headers_set = 1;
/* If all went well, tell the caller we consumed the headers complete
*/
if (!termch)
return(ate + headlen);
/* Any data left must be sent directly by the caller, all we
* give back is the size of the headers we consumed (which only
* happens if the parser got to the head arg, which varies based
* on whether we passed stat+head to scan, or only head.
*/
if (termch && (termarg == (stat ? 1 : 0))
&& head_present && head + headlen > termch) {
return ate + termch - head;
}
return ate;
}
static int APR_THREAD_FUNC regfnWriteClient(isapi_cid *cid,
void *buf_ptr,
apr_uint32_t *size_arg,
apr_uint32_t flags)
{
request_rec *r = cid->r;
conn_rec *c = r->connection;
apr_uint32_t buf_size = *size_arg;
char *buf_data = (char*)buf_ptr;
apr_bucket_brigade *bb;
apr_bucket *b;
apr_status_t rv = APR_SUCCESS;
if (!cid->headers_set) {
/* It appears that the foxisapi module and other clients
* presume that WriteClient("headers\n\nbody") will work.
* Parse them out, or die trying.
*/
apr_ssize_t ate;
ate = send_response_header(cid, NULL, buf_data, 0, buf_size);
if (ate < 0) {
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_INVALID_PARAMETER));
return 0;
}
buf_data += ate;
buf_size -= ate;
}
if (buf_size) {
bb = apr_brigade_create(r->pool, c->bucket_alloc);
b = apr_bucket_transient_create(buf_data, buf_size, c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
b = apr_bucket_flush_create(c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
rv = ap_pass_brigade(r->output_filters, bb);
cid->response_sent = 1;
if (rv != APR_SUCCESS)
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, APLOGNO(02984)
"WriteClient ap_pass_brigade failed: %s",
r->filename);
}
if ((flags & HSE_IO_ASYNC) && cid->completion) {
if (rv == APR_SUCCESS) {
cid->completion(cid->ecb, cid->completion_arg,
*size_arg, ERROR_SUCCESS);
}
else {
cid->completion(cid->ecb, cid->completion_arg,
*size_arg, ERROR_WRITE_FAULT);
}
}
return (rv == APR_SUCCESS);
}
static int APR_THREAD_FUNC regfnServerSupportFunction(isapi_cid *cid,
apr_uint32_t HSE_code,
void *buf_ptr,
apr_uint32_t *buf_size,
apr_uint32_t *data_type)
{
request_rec *r = cid->r;
conn_rec *c = r->connection;
char *buf_data = (char*)buf_ptr;
request_rec *subreq;
apr_status_t rv;
switch (HSE_code) {
case HSE_REQ_SEND_URL_REDIRECT_RESP:
/* Set the status to be returned when the HttpExtensionProc()
* is done.
* WARNING: Microsoft now advertises HSE_REQ_SEND_URL_REDIRECT_RESP
* and HSE_REQ_SEND_URL as equivalent per the Jan 2000 SDK.
* They most definitely are not, even in their own samples.
*/
apr_table_set (r->headers_out, "Location", buf_data);
cid->r->status = cid->ecb->dwHttpStatusCode = HTTP_MOVED_TEMPORARILY;
cid->r->status_line = ap_get_status_line(cid->r->status);
cid->headers_set = 1;
return 1;
case HSE_REQ_SEND_URL:
/* Soak up remaining input */
if (r->remaining > 0) {
char argsbuffer[HUGE_STRING_LEN];
while (ap_get_client_block(r, argsbuffer, HUGE_STRING_LEN));
}
/* Reset the method to GET */
r->method = "GET";
r->method_number = M_GET;
/* Don't let anyone think there's still data */
apr_table_unset(r->headers_in, "Content-Length");
/* AV fault per PR3598 - redirected path is lost! */
buf_data = apr_pstrdup(r->pool, (char*)buf_data);
ap_internal_redirect(buf_data, r);
return 1;
case HSE_REQ_SEND_RESPONSE_HEADER:
{
/* Parse them out, or die trying */
apr_size_t statlen = 0, headlen = 0;
apr_ssize_t ate;
if (buf_data)
statlen = strlen((char*) buf_data);
if (data_type)
headlen = strlen((char*) data_type);
ate = send_response_header(cid, (char*) buf_data,
(char*) data_type,
statlen, headlen);
if (ate < 0) {
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_INVALID_PARAMETER));
return 0;
}
else if ((apr_size_t)ate < headlen) {
apr_bucket_brigade *bb;
apr_bucket *b;
bb = apr_brigade_create(cid->r->pool, c->bucket_alloc);
b = apr_bucket_transient_create((char*) data_type + ate,
headlen - ate, c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
b = apr_bucket_flush_create(c->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bb, b);
rv = ap_pass_brigade(cid->r->output_filters, bb);
cid->response_sent = 1;
if (rv != APR_SUCCESS)
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, rv, r, APLOGNO(03177)
"ServerSupportFunction "
"HSE_REQ_SEND_RESPONSE_HEADER "
"ap_pass_brigade failed: %s", r->filename);
return (rv == APR_SUCCESS);
}
/* Deliberately hold off sending 'just the headers' to begin to
* accumulate the body and speed up the overall response, or at
* least wait for the end the session.
*/
return 1;
}
case HSE_REQ_DONE_WITH_SESSION:
/* Signal to resume the thread completing this request,
* leave it to the pool cleanup to dispose of our mutex.
*/
if (cid->completed) {
(void)apr_thread_mutex_unlock(cid->completed);
return 1;
}
else if (cid->dconf.log_unsupported) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, APLOGNO(02671)
"ServerSupportFunction "
"HSE_REQ_DONE_WITH_SESSION is not supported: %s",
r->filename);
}
apr_set_os_error(APR_FROM_OS_ERROR(ERROR_INVALID_PARAMETER));
return 0;
case HSE_REQ_MAP_URL_TO_PATH:
{
/* Map a URL to a filename */
char *file = (char *)buf_data;
apr_uint32_t len;
subreq = ap_sub_req_lookup_uri(
apr_pstrndup(cid->r->pool, file, *buf_size), r, NULL);
if (!subreq->filename) {
ap_destroy_sub_req(subreq);
return 0;
}
len = (apr_uint32_t)strlen(r->filename);
if ((subreq->finfo.filetype == APR_DIR)
&& (!subreq->path_info)
&& (file[len - 1] != '/'))
file = apr_pstrcat(cid->r->pool, subreq->filename, "/", NULL);
else
file = apr_pstrcat(cid->r->pool, subreq->filename,
subreq->path_info, NULL);
ap_destroy_sub_req(subreq);
#ifdef WIN32
/* We need to make this a real Windows path name */
apr_filepath_merge(&file, "", file, APR_FILEPATH_NATIVE, r->pool);
#endif
*buf_size = apr_cpystrn(buf_data, file, *buf_size) - buf_data;
return 1;
}