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
|
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* mod_isapi.c - Internet Server Application (ISA) module for Apache
* by Alexei Kosut <akosut@apache.org>
*
* This module implements Microsoft's ISAPI, allowing Apache (when running
* under Windows) to load Internet Server Applications (ISAPI extensions).
* It implements all of the ISAPI 2.0 specification, except for the
* "Microsoft-only" extensions dealing with asynchronous I/O. All ISAPI
* extensions that use only synchronous I/O and are compatible with the
* ISAPI 2.0 specification should work (most ISAPI 1.0 extensions should
* function as well).
*
* To load, simply place the ISA in a location in the document tree.
* Then add an "AddHandler isapi-isa dll" into your config file.
* You should now be able to load ISAPI DLLs just be reffering to their
* URLs. Make sure the ExecCGI option is active in the directory
* the ISA is in.
*/
#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 "apr_portable.h"
#include "apr_strings.h"
/* We use the exact same header file as the original */
#include <HttpExt.h>
/* TODO: Unknown errors that must be researched for correct codes */
#define TODO_ERROR 1
/* Seems IIS does not enforce the requirement for \r\n termination on HSE_REQ_SEND_RESPONSE_HEADER,
define this to conform */
#define RELAX_HEADER_RULE
module isapi_module;
/* Declare the ISAPI functions */
BOOL WINAPI GetServerVariable (HCONN hConn, LPSTR lpszVariableName,
LPVOID lpvBuffer, LPDWORD lpdwSizeofBuffer);
BOOL WINAPI WriteClient (HCONN ConnID, LPVOID Buffer, LPDWORD lpwdwBytes,
DWORD dwReserved);
BOOL WINAPI ReadClient (HCONN ConnID, LPVOID lpvBuffer, LPDWORD lpdwSize);
BOOL WINAPI ServerSupportFunction (HCONN hConn, DWORD dwHSERequest,
LPVOID lpvBuffer, LPDWORD lpdwSize,
LPDWORD lpdwDataType);
/*
The optimiser blows it totally here. What happens is that autos are addressed relative to the
stack pointer, which, of course, moves around. The optimiser seems to lose track of it somewhere
between setting HttpExtensionProc's address and calling through it. We work around the problem by
forcing it to use frame pointers.
The revisions below may eliminate this artifact.
*/
#pragma optimize("y",off)
/* Our loaded isapi module description structure */
typedef struct {
HINSTANCE handle;
HSE_VERSION_INFO *pVer;
PFN_GETEXTENSIONVERSION GetExtensionVersion;
PFN_HTTPEXTENSIONPROC HttpExtensionProc;
PFN_TERMINATEEXTENSION TerminateExtension;
int refcount;
DWORD timeout;
BOOL fakeasync;
DWORD reportversion;
} isapi_loaded;
/* Our "Connection ID" structure */
typedef struct {
LPEXTENSION_CONTROL_BLOCK ecb;
isapi_loaded *isa;
request_rec *r;
PFN_HSE_IO_COMPLETION completion;
PVOID completion_arg;
HANDLE complete;
ap_status_t retval;
} isapi_cid;
ap_status_t isapi_handler (request_rec *r)
{
ap_table_t *e = r->subprocess_env;
isapi_loaded *isa;
isapi_cid *cid;
/* Use similar restrictions as CGIs
*
* If this fails, it's pointless to load the isapi dll.
*/
if (!(ap_allow_options(r) & OPT_EXECCGI))
return HTTP_FORBIDDEN;
if (r->finfo.protection == 0)
return HTTP_NOT_FOUND;
if (r->finfo.filetype == APR_DIR)
return HTTP_FORBIDDEN;
/* Load the module
*
* TODO: Critical section
*
* Warning: cid should not be allocated from pool if we
* cache the isapi process in-memory.
*
* This code could use cacheing... everything that follows
* should only be performed on the first isapi dll invocation,
* not with every HttpExtensionProc()
*/
isa = ap_pcalloc(r->pool, sizeof(isapi_module));
isa->pVer = ap_pcalloc(r->pool, sizeof(HSE_VERSION_INFO));
isa->refcount = 0;
/* TODO: These may need to become overrideable, so that we
* assure a given isapi can be fooled into behaving well.
*/
isa->timeout = INFINITE; /* microsecs */
isa->fakeasync = TRUE;
isa->reportversion = MAKELONG(0, 5); /* Revision 5.0 */
if (!(isa->handle = LoadLibraryEx(r->filename, NULL,
LOAD_WITH_ALTERED_SEARCH_PATH))) {
ap_log_rerror(APLOG_MARK, APLOG_ALERT, GetLastError(), r,
"ISAPI %s failed to load", r->filename);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (!(isa->GetExtensionVersion =
(void *)(GetProcAddress(isa->handle, "GetExtensionVersion")))) {
ap_log_rerror(APLOG_MARK, APLOG_ALERT, GetLastError(), r,
"ISAPI %s is missing GetExtensionVersion()",
r->filename);
FreeLibrary(isa->handle);
return HTTP_INTERNAL_SERVER_ERROR;
}
if (!(isa->HttpExtensionProc =
(void *)(GetProcAddress(isa->handle, "HttpExtensionProc")))) {
ap_log_rerror(APLOG_MARK, APLOG_ALERT, GetLastError(), r,
"ISAPI %s is missing HttpExtensionProc()",
r->filename);
FreeLibrary(isa->handle);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* TerminateExtension() is an optional interface */
isa->TerminateExtension = (void *)(GetProcAddress(isa->handle, "TerminateExtension"));
/* Run GetExtensionVersion() */
if (!(*isa->GetExtensionVersion)(isa->pVer)) {
/* ### euh... we're passing the wrong type of error code here */
ap_log_rerror(APLOG_MARK, APLOG_ALERT, HTTP_INTERNAL_SERVER_ERROR, r,
"ISAPI %s call GetExtensionVersion() failed",
r->filename);
FreeLibrary(isa->handle);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Load of this module completed, this is the point at which *isa
* could be cached for later invocation.
*
* on to invoking this request...
*/
/* Set up variables */
ap_add_common_vars(r);
ap_add_cgi_vars(r);
/* Set up connection structure and ecb */
cid = ap_pcalloc(r->pool, sizeof(isapi_cid));
cid->ecb = ap_pcalloc(r->pool, sizeof(struct _EXTENSION_CONTROL_BLOCK));
cid->ecb->ConnID = (HCONN)cid;
/* TODO: Critical section */
++isa->refcount;
cid->isa = isa;
cid->r = r;
cid->r->status = 0;
cid->complete = NULL;
cid->completion = NULL;
cid->retval = APR_SUCCESS;
cid->ecb->cbSize = sizeof(EXTENSION_CONTROL_BLOCK);
cid->ecb->dwVersion = isa->reportversion;
cid->ecb->dwHttpStatusCode = 0;
strcpy(cid->ecb->lpszLogData, "");
// TODO: are copies really needed here?
cid->ecb->lpszMethod = ap_pstrdup(r->pool, (char*) r->method);
cid->ecb->lpszQueryString = ap_pstrdup(r->pool,
(char*) ap_table_get(e, "QUERY_STRING"));
cid->ecb->lpszPathInfo = ap_pstrdup(r->pool,
(char*) ap_table_get(e, "PATH_INFO"));
cid->ecb->lpszPathTranslated = ap_pstrdup(r->pool,
(char*) ap_table_get(e, "PATH_TRANSLATED"));
cid->ecb->lpszContentType = ap_pstrdup(r->pool,
(char*) ap_table_get(e, "CONTENT_TYPE"));
/* Set up the callbacks */
cid->ecb->GetServerVariable = &GetServerVariable;
cid->ecb->WriteClient = &WriteClient;
cid->ecb->ReadClient = &ReadClient;
cid->ecb->ServerSupportFunction = &ServerSupportFunction;
/* Set up client input */
cid->retval = ap_setup_client_block(r, REQUEST_CHUNKED_ERROR);
if (cid->retval) {
if (isa->TerminateExtension) {
(*isa->TerminateExtension)(HSE_TERM_MUST_UNLOAD);
}
FreeLibrary(isa->handle);
return cid->retval;
}
if (ap_should_client_block(r)) {
/* Unlike IIS, which limits this to 48k, we read the whole
* sucker in. I suppose this could be bad for memory if someone
* uploaded the complete works of Shakespeare. Well, WebSite
* does the same thing.
*
* But we can be smarter and read up to our 48k and then allow
* the ISAPI app to read further blocks as desired.
*/
long to_read = atol(ap_table_get(e, "CONTENT_LENGTH"));
long read;
/* Actually, let's cap it at 48k, until we figure out what
* to do with this... we don't want a Content-Length: 1000000000
* taking out the machine.
*/
if (to_read > 49152) {
if (isa->TerminateExtension)
(*isa->TerminateExtension)(HSE_TERM_MUST_UNLOAD);
FreeLibrary(isa->handle);
return HTTP_REQUEST_ENTITY_TOO_LARGE;
}
cid->ecb->lpbData = ap_pcalloc(r->pool, 1 + to_read);
if ((read = ap_get_client_block(r, cid->ecb->lpbData, to_read)) < 0) {
if (isa->TerminateExtension)
(*isa->TerminateExtension)(HSE_TERM_MUST_UNLOAD);
FreeLibrary(isa->handle);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Although its not to spec, IIS seems to null-terminate
* its lpdData string. So we will too. To make sure
* cbAvailable matches cbTotalBytes, we'll up the latter
* and equalize them.
*/
cid->ecb->cbAvailable = cid->ecb->cbTotalBytes = read + 1;
cid->ecb->lpbData[read] = '\0';
}
else {
cid->ecb->cbTotalBytes = 0;
cid->ecb->cbAvailable = 0;
cid->ecb->lpbData = NULL;
}
/* All right... try and run the sucker */
cid->retval = (*isa->HttpExtensionProc)(cid->ecb);
/* Set the status (for logging) */
if (cid->ecb->dwHttpStatusCode) {
cid->r->status = cid->ecb->dwHttpStatusCode;
}
/* Check for a log message - and log it */
if (cid->ecb->lpszLogData && *cid->ecb->lpszLogData)
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
"ISAPI %s: %s", r->filename, cid->ecb->lpszLogData);
switch(cid->retval) {
case HSE_STATUS_SUCCESS:
case HSE_STATUS_SUCCESS_AND_KEEP_CONN:
/* Ignore the keepalive stuff; Apache handles it just fine without
* the ISA's "advice".
* Per Microsoft: "In IIS versions 4.0 and later, the return
* values HSE_STATUS_SUCCESS and HSE_STATUS_SUCCESS_AND_KEEP_CONN
* are functionally identical: Keep-Alive connections are
* maintained, if supported by the client."
* ... so we were pat all this time
*/
break;
case HSE_STATUS_PENDING:
/* emulating async behavior...
*
* Create a cid->completed event and wait on it for some timeout
* so that the app thinks is it running async.
*
* All async ServerSupportFunction calls will be handled through
* the registered IO_COMPLETION hook.
*/
if (!isa->fakeasync) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, APR_ENOTIMPL, r,
"ISAPI %s asynch I/O request refused",
r->filename);
cid->retval = APR_ENOTIMPL;
}
else {
cid->complete = CreateEvent(NULL, FALSE, FALSE, NULL);
if (WaitForSingleObject(cid->complete, isa->timeout)
== WAIT_TIMEOUT) {
/* TODO: Now what... if this hung, then do we kill our own
* thread to force it's death? For now leave timeout = -1
*/
}
}
break;
case HSE_STATUS_ERROR:
/* end response if we have yet to do so.
*/
cid->retval = HTTP_INTERNAL_SERVER_ERROR;
break;
default:
/* TODO: log unrecognized retval for debugging
*/
cid->retval = HTTP_INTERNAL_SERVER_ERROR;
break;
}
/* All done with the DLL... get rid of it...
*
* If optionally cached, pass HSE_TERM_ADVISORY_UNLOAD,
* and if it returns TRUE, unload, otherwise, cache it.
*/
if (isa->TerminateExtension) {
(*isa->TerminateExtension)(HSE_TERM_MUST_UNLOAD);
}
FreeLibrary(isa->handle);
/* TODO: Crit section */
cid->isa = NULL;
--isa->refcount;
isa->handle = NULL;
return cid->retval;
}
#pragma optimize("",on)
BOOL WINAPI GetServerVariable (HCONN hConn, LPSTR lpszVariableName,
LPVOID lpvBuffer, LPDWORD lpdwSizeofBuffer)
{
request_rec *r = ((isapi_cid *)hConn)->r;
ap_table_t *e = r->subprocess_env;
const char *result;
/* Mostly, we just grab it from the environment, but there are
* a couple of special cases
*/
if (!strcasecmp(lpszVariableName, "UNMAPPED_REMOTE_USER")) {
/* We don't support NT users, so this is always the same as
* REMOTE_USER
*/
result = ap_table_get(e, "REMOTE_USER");
}
else if (!strcasecmp(lpszVariableName, "SERVER_PORT_SECURE")) {
/* Apache doesn't support secure requests inherently, so
* we have no way of knowing. We'll be conservative, and say
* all requests are insecure.
*/
result = "0";
}
else if (!strcasecmp(lpszVariableName, "URL")) {
result = r->uri;
}
else {
result = ap_table_get(e, lpszVariableName);
}
if (result) {
if (strlen(result) > *lpdwSizeofBuffer) {
*lpdwSizeofBuffer = strlen(result);
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
strncpy(lpvBuffer, result, *lpdwSizeofBuffer);
return TRUE;
}
/* Didn't find it */
SetLastError(ERROR_INVALID_INDEX);
return FALSE;
}
BOOL WINAPI WriteClient (HCONN ConnID, LPVOID Buffer, LPDWORD lpwdwBytes,
DWORD dwReserved)
{
request_rec *r = ((isapi_cid *)ConnID)->r;
int writ; /* written, actually, but why shouldn't I make up words? */
/* We only support synchronous writing */
if (dwReserved && dwReserved != HSE_IO_SYNC) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, ERROR_INVALID_PARAMETER, r,
"ISAPI %s asynch write", r->filename);
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
if ((writ = ap_rwrite(Buffer, *lpwdwBytes, r)) == EOF) {
SetLastError(WSAEDISCON); /* TODO: Find the right error code */
return FALSE;
}
*lpwdwBytes = writ;
return TRUE;
}
BOOL WINAPI ReadClient (HCONN ConnID, LPVOID lpvBuffer, LPDWORD lpdwSize)
{
/* TODO: If the request was a huge transmit or chunked, continue piping the
* request here, but if it's of a sane size, continue to ...
*/
return TRUE;
}
static char* ComposeHeaders(request_rec *r, char* data)
{
/* We *should* break before this while loop ends */
while (*data)
{
char *value, *lf = strchr(data, '\n');
int p;
#ifdef RELAX_HEADER_RULE
if (lf)
*lf = '\0';
#else
if (!lf) { /* Huh? Invalid data, I think */
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"ISAPI %s sent invalid headers", r->filename);
SetLastError(TODO_ERROR);
return FALSE;
}
/* Get rid of \n and \r */
*lf = '\0';
#endif
p = strlen(data);
if (p > 0 && data[p-1] == '\r') data[p-1] = '\0';
/* End of headers */
if (*data == '\0') {
#ifdef RELAX_HEADER_RULE
if (lf)
#endif
data = lf + 1; /* Reset data */
break;
}
if (!(value = strchr(data, ':'))) {
SetLastError(TODO_ERROR);
/* ### euh... we're passing the wrong type of error
### code here */
ap_log_rerror(APLOG_MARK, APLOG_ERR, HTTP_INTERNAL_SERVER_ERROR, r,
"ISAPI %s sent invalid headers", r->filename);
return FALSE;
}
*value++ = '\0';
while (*value && ap_isspace(*value)) ++value;
/* Check all the special-case headers. Similar to what
* ap_scan_script_header_err() does (see that function for
* more detail)
*/
if (!strcasecmp(data, "Content-Type"))
{
/* Nuke trailing whitespace */
char *tmp;
char *endp = value + strlen(value) - 1;
while (endp > value && ap_isspace(*endp))
*endp-- = '\0';
tmp = ap_pstrdup (r->pool, value);
ap_str_tolower(tmp);
r->content_type = tmp;
}
else if (!strcasecmp(data, "Content-Length")) {
ap_table_set(r->headers_out, data, value);
}
else if (!strcasecmp(data, "Transfer-Encoding")) {
ap_table_set(r->headers_out, data, value);
}
else if (!strcasecmp(data, "Set-Cookie")) {
ap_table_add(r->err_headers_out, data, value);
}
else {
ap_table_merge(r->err_headers_out, data, value);
}
/* Reset data */
#ifdef RELAX_HEADER_RULE
if (!lf) {
data += p;
break;
}
#endif
data = lf + 1;
}
return data;
}
/* XXX: There is an O(n^2) attack possible here. */
BOOL WINAPI ServerSupportFunction (HCONN hConn, DWORD dwHSERequest,
LPVOID lpvBuffer, LPDWORD lpdwSize,
LPDWORD lpdwDataType)
{
isapi_cid *cid = (isapi_cid *)hConn;
request_rec *r = cid->r;
request_rec *subreq;
char *data;
switch (dwHSERequest) {
case HSE_REQ_SEND_URL_REDIRECT_RESP:
/* Set the status to be returned when the HttpExtensionProc()
* is done.
*/
ap_table_set (r->headers_out, "Location", lpvBuffer);
cid->r->status = cid->ecb->dwHttpStatusCode
= HTTP_MOVED_TEMPORARILY;
return TRUE;
case HSE_REQ_SEND_URL:
/* Read any additional 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 = ap_pstrdup(r->pool, "GET");
r->method_number = M_GET;
/* Don't let anyone think there's still data */
ap_table_unset(r->headers_in, "Content-Length");
ap_internal_redirect((char *)lpvBuffer, r);
return TRUE;
case HSE_REQ_SEND_RESPONSE_HEADER:
r->status_line = lpvBuffer ? lpvBuffer : ap_pstrdup(r->pool, "200 OK");
sscanf(r->status_line, "%d", &r->status);
cid->ecb->dwHttpStatusCode = r->status;
/* Now fill in the HTTP headers, and the rest of it. Ick.
* lpdwDataType contains a string that has headers (in MIME
* format), a blank like, then (possibly) data. We need
* to parse it.
*
* Easy case first:
*/
if (!lpdwDataType) {
ap_send_http_header(r);
return TRUE;
}
/* Make a copy - don't disturb the original */
data = ap_pstrdup(r->pool, (char *)lpdwDataType);
/* Parse them out, or die trying */
data = ComposeHeaders(r, data);
if (!data)
return FALSE;
/* All the headers should be set now */
ap_send_http_header(r);
/* Any data left should now be sent directly */
if (*data)
ap_rputs(data, r);
return TRUE;
case HSE_REQ_DONE_WITH_SESSION:
/* Signal to resume the thread completing this request
*/
if (cid->complete)
SetEvent(cid->complete);
return TRUE;
case HSE_REQ_MAP_URL_TO_PATH:
/* Map a URL to a filename */
subreq = ap_sub_req_lookup_uri(ap_pstrndup(r->pool, (char *)lpvBuffer,
*lpdwSize), r);
GetFullPathName(subreq->filename, *lpdwSize - 1, (char *)lpvBuffer, NULL);
/* IIS puts a trailing slash on directories, Apache doesn't */
if (subreq->finfo.filetype == APR_DIR) {
int l = strlen((char *)lpvBuffer);
((char *)lpvBuffer)[l] = '\\';
((char *)lpvBuffer)[l + 1] = '\0';
}
return TRUE;
case HSE_REQ_GET_SSPI_INFO:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_APPEND_LOG_PARAMETER:
/* Log lpvBuffer, of lpdwSize bytes, in the URI Query (cs-uri-query) field
* This code will do for now...
*/
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r,
"ISAPI %s: %s", cid->r->filename,
(char*) lpvBuffer);
return TRUE;
case HSE_REQ_IO_COMPLETION:
/* TODO: Emulate a completion port, if we can...
* Record the callback address and user defined argument...
* we will call this after any async request (e.g. transmitfile)
* as if the request had completed async execution.
* Per MS docs... HSE_REQ_IO_COMPLETION replaces any prior call
* to HSE_REQ_IO_COMPLETION, and lpvBuffer may be set to NULL.
*/
if (!cid->isa->fakeasync)
return FALSE;
cid->completion = (PFN_HSE_IO_COMPLETION) lpvBuffer;
cid->completion_arg = (PVOID) lpdwDataType;
return TRUE;
case HSE_REQ_TRANSMIT_FILE:
/* Use TransmitFile... nothing wrong with that :)
*/
/* ### euh... we're passing the wrong type of error code here */
ap_log_rerror(APLOG_MARK, APLOG_WARNING,
HTTP_INTERNAL_SERVER_ERROR, r,
"ISAPI asynchronous I/O not supported: %s",
r->filename);
return FALSE;
case HSE_REQ_REFRESH_ISAPI_ACL:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_REQ_IS_KEEP_CONN:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_REQ_ASYNC_READ_CLIENT:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_REQ_GET_IMPERSONATION_TOKEN: /* Added in ISAPI 4.0 */
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_REQ_MAP_URL_TO_PATH_EX:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
/* TODO: Not quite ready for prime time yet */
/* Map a URL to a filename */
subreq = ap_sub_req_lookup_uri(ap_pstrndup(r->pool, (char *)lpvBuffer,
*lpdwSize), r);
GetFullPathName(subreq->filename, *lpdwSize - 1, (char *)lpvBuffer, NULL);
/* IIS puts a trailing slash on directories, Apache doesn't */
if (subreq->finfo.filetype == APR_DIR) {
int l = strlen((char *)lpvBuffer);
((char *)lpvBuffer)[l] = '\\';
((char *)lpvBuffer)[l + 1] = '\0';
}
lpdwDataType = (LPDWORD) ap_palloc(r->pool, sizeof(HSE_URL_MAPEX_INFO));
strncpy(((LPHSE_URL_MAPEX_INFO)lpdwDataType)->lpszPath,
(char *) lpvBuffer, MAX_PATH);
((LPHSE_URL_MAPEX_INFO)lpdwDataType)->dwFlags = 0;
/* is a combination of:
* HSE_URL_FLAGS_READ Allow for read.
* HSE_URL_FLAGS_WRITE Allow for write.
* HSE_URL_FLAGS_EXECUTE Allow for execute.
* HSE_URL_FLAGS_SSL Require SSL.
* HSE_URL_FLAGS_DONT_CACHE Don't cache (virtual root only).
* HSE_URL_FLAGS_NEGO_CERT Allow client SSL certifications.
* HSE_URL_FLAGS_REQUIRE_CERT Require client SSL certifications.
* HSE_URL_FLAGS_MAP_CERT Map SSL certification to a Windows account.
* HSE_URL_FLAGS_SSL128 Requires a 128-bit SSL.
* HSE_URL_FLAGS_SCRIPT Allows for script execution.
*/
/* (LPHSE_URL_MAPEX_INFO)lpdwDataType)->cchMatchingPath
* (LPHSE_URL_MAPEX_INFO)lpdwDataType)->cchMatchingURL
*/
return TRUE;
case HSE_REQ_ABORTIVE_CLOSE:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_REQ_GET_CERT_INFO_EX: /* Added in ISAPI 4.0 */
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_REQ_SEND_RESPONSE_HEADER_EX: /* Added in ISAPI 4.0 */
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
/* TODO: Not quite ready for prime time */
if (((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->pszStatus
&& ((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->cchStatus) {
r->status_line = ap_pstrndup(r->pool,
((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->pszStatus,
((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->cchStatus);
}
else {
r->status_line = ap_pstrdup(r->pool, "200 OK");
}
sscanf(r->status_line, "%d", &r->status);
cid->ecb->dwHttpStatusCode = r->status;
if (((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->pszHeader
&& ((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->cchHeader)
{
/* Make a copy - don't disturb the original */
data = ap_pstrndup(r->pool,
((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->pszHeader,
((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->cchHeader);
/* Parse them out, or die trying */
data = ComposeHeaders(r, data);
if (!data)
return FALSE;
}
else {
data = "\0";
}
/* ((LPHSE_SEND_HEADER_EX_INFO)lpvBuffer)->fKeepConn;
*
* Now how are we about to start listening to an ISAPI's
* idea of keeping or closing a connection? Seriously :)
*/
/* All the headers should be set now */
ap_send_http_header(r);
/* Any data left should now be sent directly */
if (*data)
ap_rputs(data, r);
return TRUE;
case HSE_REQ_CLOSE_CONNECTION: /* Added after ISAPI 4.0 */
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
case HSE_REQ_IS_CONNECTED: /* Added after ISAPI 4.0 */
/* Returns True if client is connected c.f. Q188346*/
return TRUE;
/* case HSE_REQ_EXTENSION_TRIGGER:
* Added after ISAPI 4.0?
* Undocumented - from the Microsoft Jan '00 Platform SDK
*/
default:
/* TODO: log unrecognized ServerSupportCommand for debugging
*/
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
}
handler_rec isapi_handlers[] = {
{ "isapi-isa", isapi_handler },
{ NULL}
};
module isapi_module = {
STANDARD20_MODULE_STUFF,
NULL, /* create per-dir config */
NULL, /* merge per-dir config */
NULL, /* server config */
NULL, /* merge server config */
NULL, /* command ap_table_t */
isapi_handlers, /* handlers */
NULL /* register hooks */
};
|