File: isapi_sapi.c

package info (click to toggle)
php3 1%3A3.0-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 6,512 kB
  • ctags: 6,320
  • sloc: ansic: 58,611; sh: 2,279; yacc: 1,090; php: 1,031; makefile: 911; cpp: 529; perl: 411; awk: 90; sql: 11
file content (242 lines) | stat: -rw-r--r-- 6,312 bytes parent folder | download | duplicates (2)
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
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <httpext.h>
#include <stdarg.h>
#include "php.h"
#define SAPI_INTERFACE 1
#include "sapi.h"

#if DEBUG
void logmessage(void *scid,char *message, char *funcname){
	char logmessage[81];
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;
	snprintf(logmessage,79,"%d:%s:%s\n",lpEcb->ConnID,funcname,message);
#if 0
	_php3_error_log(4,logmessage,"/phplog.txt",NULL);
#else
	/* this will output right to the msvc debugger window,
	   or to DBMON which comes with msvc */
	OutputDebugString(logmessage);
#endif
}
#endif

int sapi_readclient(void * scid, char *buf, int size, int len){
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;
	if (!lpEcb->ReadClient(lpEcb->ConnID,(LPVOID)buf,&len)){
		OutputDebugString("ReadClient Failed\n");
		return 0;
	}
	return 1;
}

char *sapi_getenv(void *scid, char *string){
	char var[1024];
	DWORD dwLen;
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;

	if (!string)
		return NULL;
	
	dwLen=lpEcb->GetServerVariable(lpEcb->ConnID,string,var,&dwLen);

	if (dwLen == 0 || dwLen >1024)
		return NULL;
	else
		return estrdup(var);
}

void sapi_header(void *scid, char *header){
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;
	int len=strlen(header);
	if (!lpEcb->ServerSupportFunction(lpEcb->ConnID,
		HSE_REQ_SEND_URL_REDIRECT_RESP,
		NULL,&len,(LPDWORD)header))
			OutputDebugString("Header Failed\n");

}

/*FIXME*/
void sapi_flush(void *scid){ 
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;
	/*fflush(stdout);*/
}

void sapi_puts(void *scid, char *string){
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;
	int n;
	n = strlen(string);
	if (!lpEcb->WriteClient(lpEcb->ConnID,string,&n,0))
		OutputDebugString("PUTS Failed\n");
	return;
}

void sapi_putc(void *scid, char c){
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;
	int n = 1;
	if (!lpEcb->WriteClient(lpEcb->ConnID,&c,&n,0)){
		OutputDebugString("PUTC Failed\n");
	}
	return;
}

int sapi_writeclient(void *scid, char *string, int len){
	LPEXTENSION_CONTROL_BLOCK lpEcb=scid;
	if (!lpEcb->WriteClient(lpEcb->ConnID,string,&len,0)){
		OutputDebugString("WriteClient Failed\n");
		return 0;
	}
	return 1;
}

void sapi_log(void *scid, char *message){}
void sapi_block(void *scid){}
void sapi_unblock(void *scid){}

#define ENTRY_NAME_COLOR "#999999"
#define CONTENTS_COLOR "#DDDDDD"
#define HEADER_COLOR "#00DDDD"
char *isapi_env[]={
	"AUTH_TYPE",
	"CONTENT_LENGTH",
	"CONTENT_TYPE",
	"PATH_INFO",
	"PATH_TRANSLATED",
	"QUERY_STRING",
	"REQUEST_METHOD",
	"REMOTE_ADDR",
	"REMOTE_HOST",
	"REMOTE_USER",
	"SCRIPT_NAME",
	"SERVER_NAME",
	"SERVER_PORT",
	"SERVER_PORT_SECURE",
	"SERVER_PROTOCOL",
	"SERVER_SOFTWARE",
	"HTTP_ACCEPT",
	"HTTP_ACCEPT_ENCODING",
	"HTTP_ACCEPT_LANGUAGE",
	"HTTP_CONNECTION",
	"HTTP_HOST",
	"HTTP_USER_AGENT",
	"HTTP_REFERER",
	"URL",
	"ALL_HTTP",
	NULL
};

void sapi_print_info(void *sapi_rqst){
	char *temp;
	int i=0;
	struct sapi_request_info *sapi_info=sapi_rqst;
	sapi_puts(sapi_info->scid,"<center><h2>ISAPI Request Environment</h2></center>");
	sapi_puts(sapi_info->scid,"<table border=5 width=\"600\">\n");
	sapi_puts(sapi_info->scid,"<tr><th bgcolor=\"" HEADER_COLOR "\">Variable</th><th bgcolor=\"" HEADER_COLOR "\">Value</th></tr>\n");
	while(isapi_env[i]!=NULL){
		temp=sapi_getenv(sapi_info->scid,isapi_env[i]);
		sapi_puts(sapi_info->scid,"<tr><td bgcolor=\"" ENTRY_NAME_COLOR "\">");
		sapi_puts(sapi_info->scid,isapi_env[i]);
		sapi_puts(sapi_info->scid,"</td><td bgcolor=\"" CONTENTS_COLOR "\">");
		if(temp)sapi_puts(sapi_info->scid,temp);
		sapi_puts(sapi_info->scid,"&nbsp;</td></tr>\n");
		if(temp)efree(temp);
		i++;
	}
	sapi_puts(sapi_info->scid,"</table>\n");

}

struct sapi_request_info *sapi_init(LPEXTENSION_CONTROL_BLOCK lpEcb)
{
	struct sapi_request_info *sapi_info=malloc(sizeof(struct sapi_request_info));

	char *buf;
	sapi_info->scid=lpEcb;
	sapi_info->path_info = lpEcb->lpszPathInfo;
	sapi_info->path_translated = lpEcb->lpszPathTranslated;
	sapi_info->filename = strdup(sapi_info->path_translated);
	sapi_info->query_string = lpEcb->lpszQueryString;
	sapi_info->current_user = NULL;
	sapi_info->current_user_length=0;
	sapi_info->request_method = lpEcb->lpszMethod;
	sapi_info->script_name = sapi_getenv(lpEcb,"SCRIPT_NAME");

	buf = sapi_getenv(lpEcb,"CONTENT_LENGTH");
	sapi_info->content_length = (buf ? atoi(buf) : 0);

	sapi_info->content_type = lpEcb->lpszContentType;
	sapi_info->cookies = sapi_getenv(lpEcb,"HTTP_COOKIE");
	sapi_info->puts=sapi_puts;
	sapi_info->putc=sapi_putc;
	sapi_info->getenv=sapi_getenv;
	sapi_info->writeclient=sapi_writeclient;
	sapi_info->flush=sapi_flush;
	sapi_info->header=sapi_header;
	sapi_info->readclient=sapi_readclient;
	sapi_info->block=sapi_block;
	sapi_info->unblock=sapi_unblock;
	sapi_info->log=sapi_log;
	sapi_info->info=sapi_print_info;

	/*these are used in cgi, what do we do with them now*/
	sapi_info->cgi=0;
	sapi_info->display_source_mode=0;
	sapi_info->preprocess=0;
	sapi_info->info_only=0;
	sapi_info->quiet_mode=0;

	return sapi_info;
}

void sapi_shutdown(struct sapi_request_info *sapi_info){
	free(sapi_info->filename);
	free(sapi_info);
}

BOOL WINAPI GetExtensionVersion (HSE_VERSION_INFO  *version)
{
    version->dwExtensionVersion = MAKELONG( HSE_VERSION_MINOR,
                                            HSE_VERSION_MAJOR );
    strncpy( version->lpszExtensionDesc,
            "PHP v3.0 ISAPI Extension",
			HSE_MAX_EXT_DLL_NAME_LEN);

    return TRUE;
}

/* this is php3_isapi_main()!!! */
DWORD WINAPI HttpExtensionProc (LPEXTENSION_CONTROL_BLOCK lpEcb)
{
	int ret=0;
	struct sapi_request_info *sapi_info;
#if DEBUG
	char logmessage[80];
	snprintf(logmessage,79,"%d:isapi_main: request start\n",lpEcb->ConnID);
	OutputDebugString(logmessage);
#endif

	sapi_info=sapi_init(lpEcb);

#if DEBUG
	snprintf(logmessage,79,"%d:isapi_main:%s\n",lpEcb->ConnID,sapi_info->filename);
	OutputDebugString(logmessage);
#endif

	ret=php3_sapi_main(sapi_info);

	/*make sure we free any loose allocations*/
	sapi_shutdown(sapi_info);
	if(!ret){ //ret should be 0 on success
#if DEBUG
	snprintf(logmessage,79,"%d:isapi_main:HSE_STATUS_SUCCESS: %d\n",lpEcb->ConnID,ret);
	OutputDebugString(logmessage);
#endif
		return HSE_STATUS_SUCCESS;
	}
#if DEBUG
	snprintf(logmessage,79,"%d:isapi_main:HSE_STATUS_ERROR: %d\n",lpEcb->ConnID,ret);
	OutputDebugString(logmessage);
#endif
	return HSE_STATUS_ERROR;
}