summaryrefslogtreecommitdiff
path: root/main/SAPI.c
blob: 09b1823f187cfb60a7043045d40e5c63b83f3d4e (plain)
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
/* 
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997, 1998, 1999 The PHP Group                         |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.0 of the PHP license,       |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_0.txt.                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Design:  Shane Caraveo <shane@caraveo.com>                           |
   | Authors: Andi Gutmans <andi@zend.com>                                |
   |          Zeev Suraski <zeev@zend.com>                                |
   +----------------------------------------------------------------------+
*/

#include <ctype.h>

#include "php.h"
#include "SAPI.h"
#ifdef ZTS
#include "TSRM.h"
#endif

#include "rfc1867.h"

#if WIN32||WINNT
#define STRCASECMP stricmp
#else
#define STRCASECMP strcasecmp
#endif


SAPI_POST_READER_FUNC(sapi_read_standard_form_data);

#define DEFAULT_POST_CONTENT_TYPE "application/x-www-form-urlencoded"

static sapi_post_content_type_reader supported_post_content_types[] = {
	{ DEFAULT_POST_CONTENT_TYPE,	sizeof(DEFAULT_POST_CONTENT_TYPE)-1,	sapi_read_standard_form_data },
#if HAVE_FDFLIB
	{ "application/vnd.fdf",	sizeof("application/vnd.fdf")-1,	sapi_read_standard_form_data },
#endif
	{ NULL, 0, NULL }
};


static HashTable known_post_content_types;

SAPI_API void (*sapi_error)(int error_type, const char *message, ...);


#ifdef ZTS
SAPI_API int sapi_globals_id;
#else
sapi_globals_struct sapi_globals;
#endif


/* True globals (no need for thread safety) */
sapi_module_struct sapi_module;
SAPI_API void (*sapi_error)(int error_type, const char *message, ...);


SAPI_API void sapi_startup(sapi_module_struct *sf)
{
	sapi_module = *sf;
	zend_hash_init(&known_post_content_types, 5, NULL, NULL, 1);

	sapi_register_post_readers(supported_post_content_types);

#ifdef ZTS
	sapi_globals_id = ts_allocate_id(sizeof(sapi_globals_struct), NULL, NULL);
#endif

	php_global_startup_internal_extensions();
}

SAPI_API void sapi_shutdown(void)
{
	php_global_shutdown_internal_extensions();
	zend_hash_destroy(&known_post_content_types);
}


SAPI_API void sapi_free_header(sapi_header_struct *sapi_header)
{
	efree(sapi_header->header);
}


static void sapi_read_post_data(SLS_D)
{
	sapi_post_content_type_reader *post_content_type_reader;
	uint content_type_length = strlen(SG(request_info).content_type);
	char *content_type = estrndup(SG(request_info).content_type, content_type_length);
	char *p;
	char oldchar=0;
	void (*post_reader_func)(char *content_type_dup SLS_DC);


	/* dedicated implementation for increased performance:
	 * - Make the content type lowercase
	 * - Trim descriptive data, stay with the content-type only
	 */
	for (p=content_type; p<content_type+content_type_length; p++) {
		switch (*p) {
			case ';':
			case ',':
			case ' ':
				content_type_length = p-content_type;
				oldchar = *p;
				*p = 0;
				break;
			default:
				*p = tolower(*p);
				break;
		}
	}

	if (zend_hash_find(&known_post_content_types, content_type, content_type_length+1, (void **) &post_content_type_reader)==SUCCESS) {
		post_reader_func = post_content_type_reader->post_reader;
	} else {
		if (!sapi_module.default_post_reader) {
			sapi_module.sapi_error(E_COMPILE_ERROR, "Unsupported content type:  '%s'", content_type);
			return;
		}
		post_reader_func = sapi_module.default_post_reader;
	}
	if (oldchar) {
		*(p-1) = oldchar;
	}
	post_reader_func(content_type SLS_CC);
	efree(content_type);
}


SAPI_POST_READER_FUNC(sapi_read_standard_form_data)
{
	int read_bytes, total_read_bytes=0;
	int allocated_bytes=SAPI_POST_BLOCK_SIZE+1;

	SG(request_info).post_data = emalloc(allocated_bytes);

	for (;;) {
		read_bytes = sapi_module.read_post(SG(request_info).post_data+total_read_bytes, SAPI_POST_BLOCK_SIZE SLS_CC);
		if (read_bytes<=0) {
			break;
		}
		total_read_bytes += read_bytes;
		if (read_bytes < SAPI_POST_BLOCK_SIZE) {
			break;
		}
		if (total_read_bytes+SAPI_POST_BLOCK_SIZE >= allocated_bytes) {
			allocated_bytes = total_read_bytes+SAPI_POST_BLOCK_SIZE+1;
			SG(request_info).post_data = erealloc(SG(request_info).post_data, allocated_bytes);
		}
	}
	SG(request_info).post_data[total_read_bytes] = 0;  /* terminating NULL */
	SG(request_info).post_data_length = total_read_bytes;
}


SAPI_API void sapi_activate(SLS_D)
{
	zend_llist_init(&SG(sapi_headers).headers, sizeof(sapi_header_struct), (void (*)(void *)) sapi_free_header, 0);
	SG(sapi_headers).send_default_content_type = 1;
	SG(sapi_headers).http_response_code = 200;
	SG(sapi_headers).http_status_line = NULL;
	SG(headers_sent) = 0;
	SG(read_post_bytes) = 0;
	SG(request_info).post_data = NULL;

	if (SG(request_info).request_method && !strcmp(SG(request_info).request_method, "HEAD")) {
		SG(request_info).headers_only = 1;
	} else {
		SG(request_info).headers_only = 0;
	}

	if (SG(server_context)) {
		if (SG(request_info).request_method 
			&& !strcmp(SG(request_info).request_method, "POST")) {
			if (!SG(request_info).content_type) {
				sapi_module.sapi_error(E_COMPILE_ERROR, "No content-type in POST request");
			}
			sapi_read_post_data(SLS_C);
		}
		SG(request_info).cookie_data = sapi_module.read_cookies(SLS_C);
	}
}


SAPI_API void sapi_deactivate(SLS_D)
{
	zend_llist_destroy(&SG(sapi_headers).headers);
	if (SG(request_info).post_data) {
		efree(SG(request_info).post_data);
	}
}

static int sapi_extract_response_code(const char *header_line)
{
	int code = 200;
	const char *ptr;

	for (ptr = header_line; *ptr; ptr++) {
		if (*ptr == ' ' && *(ptr + 1) != ' ') {
			code = atoi(ptr + 1);
			break;
		}
	}
	
	return code;
}

/* This function expects a *duplicated* string, that was previously emalloc()'d.
 * Pointers sent to this functions will be automatically freed by the framework.
 */
SAPI_API int sapi_add_header(char *header_line, uint header_line_len)
{
	int retval;
	sapi_header_struct sapi_header;
	char *colon_offset;
	SLS_FETCH();

	if (SG(headers_sent)) {
		sapi_module.sapi_error(E_WARNING, "Cannot add header information - headers already sent");
		efree(header_line);
		return FAILURE;
	}

	sapi_header.header = header_line;
	sapi_header.header_len = header_line_len;

	/* Check the header for a few cases that we have special support for in SAPI */
	if (header_line_len>=5 
		&& !memcmp(header_line, "HTTP/", 5)) {
		/* filter out the response code */
		SG(sapi_headers).http_response_code = sapi_extract_response_code(header_line);
		SG(sapi_headers).http_status_line = header_line;
		return SUCCESS;
	} else {
		colon_offset = strchr(header_line, ':');
		if (colon_offset) {
			*colon_offset = 0;
			if (!STRCASECMP(header_line, "Content-Type")) {
				SG(sapi_headers).send_default_content_type = 0;
			} else if (!STRCASECMP(header_line, "Location")) {
				SG(sapi_headers).http_response_code = 302; /* redirect */
			} else if (!STRCASECMP(header_line, "WWW-Authenticate")) { /* HTTP Authentication */
				SG(sapi_headers).http_response_code = 401; /* authentication-required */
			}
			*colon_offset = ':';
		}
	}

	if (sapi_module.header_handler) {
		retval = sapi_module.header_handler(&sapi_header, &SG(sapi_headers) SLS_CC);
	} else {
		retval = SAPI_HEADER_ADD;
	}
	if (retval & SAPI_HEADER_DELETE_ALL) {
		zend_llist_clean(&SG(sapi_headers).headers);
	}
	if (retval & SAPI_HEADER_ADD) {
		zend_llist_add_element(&SG(sapi_headers).headers, (void *) &sapi_header);
	}
	return SUCCESS;
}


SAPI_API int sapi_send_headers()
{
	int retval;
	int ret = FAILURE;
	sapi_header_struct default_header = { SAPI_DEFAULT_CONTENT_TYPE, sizeof(SAPI_DEFAULT_CONTENT_TYPE)-1 };
	SLS_FETCH();

	if (SG(headers_sent)) {
		return SUCCESS;
	}

	if (sapi_module.send_headers) {
		retval = sapi_module.send_headers(&SG(sapi_headers) SLS_CC);
	} else {
		retval = SAPI_HEADER_DO_SEND;
	}

	switch (retval) {
		case SAPI_HEADER_SENT_SUCCESSFULLY:
			SG(headers_sent) = 1;
			ret = SUCCESS;
			break;
		case SAPI_HEADER_DO_SEND:
			if (SG(sapi_headers).http_status_line) {
				sapi_header_struct http_status_line;

				http_status_line.header = SG(sapi_headers).http_status_line;
				http_status_line.header_len = strlen(SG(sapi_headers).http_status_line);
				sapi_module.send_header(&http_status_line, SG(server_context));
			}
			if (SG(sapi_headers).send_default_content_type) {
				sapi_module.send_header(&default_header, SG(server_context));
			}
			zend_llist_apply_with_argument(&SG(sapi_headers).headers, (void (*)(void *, void *)) sapi_module.send_header, SG(server_context));
			sapi_module.send_header(NULL, SG(server_context));
			SG(headers_sent) = 1;
			ret = SUCCESS;
			break;
		case SAPI_HEADER_SEND_FAILED:
			ret = FAILURE;
			break;
	}
	
	if (SG(sapi_headers).http_status_line) {
		efree(SG(sapi_headers).http_status_line);
	}
	
	return ret;
}


SAPI_API int sapi_register_post_readers(sapi_post_content_type_reader *post_content_type_readers)
{
	sapi_post_content_type_reader *p=post_content_type_readers;

	while (p->content_type) {
		if (sapi_register_post_reader(p)==FAILURE) {
			return FAILURE;
		}
		p++;
	}
	return SUCCESS;
}


SAPI_API int sapi_register_post_reader(sapi_post_content_type_reader *post_content_type_reader)
{
	return zend_hash_add(&known_post_content_types, post_content_type_reader->content_type, post_content_type_reader->content_type_len+1, (void *) post_content_type_reader, sizeof(sapi_post_content_type_reader), NULL);
}


SAPI_API void sapi_unregister_post_reader(sapi_post_content_type_reader *post_content_type_reader)
{
	zend_hash_del(&known_post_content_types, post_content_type_reader->content_type, post_content_type_reader->content_type_len+1);
}


SAPI_API int sapi_register_default_post_reader(void (*default_post_reader)(char *content_type_dup SLS_DC))
{
	sapi_module.default_post_reader = default_post_reader;
	return SUCCESS;
}