- drop mhash support
[m6w6/ext-http] / http_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "php.h"
19
20 #include "SAPI.h"
21 #include "ext/standard/url.h"
22 #include "ext/standard/head.h"
23
24 #include "php_http.h"
25 #include "php_http_std_defs.h"
26 #include "php_http_api.h"
27 #include "php_http_headers_api.h"
28 #include "php_http_request_api.h"
29 #include "php_http_send_api.h"
30
31 #ifdef ZEND_ENGINE_2
32 # include "zend_exceptions.h"
33 # include "php_http_exception_object.h"
34 #endif
35
36 #include <ctype.h>
37
38 #ifdef HTTP_HAVE_MAGIC
39 # if defined(PHP_WIN32) && !defined(USE_MAGIC_DLL) && !defined(USE_MAGIC_STATIC)
40 # define USE_MAGIC_STATIC
41 # endif
42 # include <magic.h>
43 #endif
44
45 ZEND_EXTERN_MODULE_GLOBALS(http);
46
47 PHP_MINIT_FUNCTION(http_support)
48 {
49 HTTP_LONG_CONSTANT("HTTP_SUPPORT", HTTP_SUPPORT);
50 HTTP_LONG_CONSTANT("HTTP_SUPPORT_REQUESTS", HTTP_SUPPORT_REQUESTS);
51 HTTP_LONG_CONSTANT("HTTP_SUPPORT_MAGICMIME", HTTP_SUPPORT_MAGICMIME);
52 HTTP_LONG_CONSTANT("HTTP_SUPPORT_ENCODINGS", HTTP_SUPPORT_ENCODINGS);
53 HTTP_LONG_CONSTANT("HTTP_SUPPORT_SSLREQUESTS", HTTP_SUPPORT_SSLREQUESTS);
54
55 return SUCCESS;
56 }
57
58 PHP_HTTP_API long _http_support(long feature)
59 {
60 long support = HTTP_SUPPORT;
61
62 #ifdef HTTP_HAVE_CURL
63 support |= HTTP_SUPPORT_REQUESTS;
64 # ifdef HTTP_HAVE_SSL
65 support |= HTTP_SUPPORT_SSLREQUESTS;
66 # endif
67 #endif
68 #ifdef HTTP_HAVE_MAGIC
69 support |= HTTP_SUPPORT_MAGICMIME;
70 #endif
71 #if defined(HTTP_HAVE_ZLIB) || defined(HAVE_ZLIB)
72 support |= HTTP_SUPPORT_ENCODINGS;
73 #endif
74
75 if (feature) {
76 return (feature == (support & feature));
77 }
78 return support;
79 }
80
81 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
82 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
83 {
84 if (key && key_len) {
85 size_t i;
86 int wasalpha;
87 if ((wasalpha = isalpha((int) key[0]))) {
88 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
89 }
90 for (i = 1; i < key_len; i++) {
91 if (isalpha((int) key[i])) {
92 key[i] = (char) (((!wasalpha) && uctitle) ? toupper((int) key[i]) : tolower((int) key[i]));
93 wasalpha = 1;
94 } else {
95 if (xhyphen && (key[i] == '_')) {
96 key[i] = '-';
97 }
98 wasalpha = 0;
99 }
100 }
101 }
102 return key;
103 }
104 /* }}} */
105
106 /* {{{ */
107 void _http_key_list_default_decoder(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
108 {
109 *decoded = estrndup(encoded, encoded_len);
110 *decoded_len = (size_t) php_url_decode(*decoded, encoded_len);
111 }
112 /* }}} */
113
114 /* {{{ */
115 STATUS _http_parse_key_list(const char *list, HashTable *items, char separator, http_key_list_decode_t decode, zend_bool first_entry_is_name_value_pair TSRMLS_DC)
116 {
117 const char *key = list, *val = NULL;
118 int vallen = 0, keylen = 0, done = 0;
119 zval array;
120
121 INIT_ZARR(array, items);
122
123 if (!(val = strchr(list, '='))) {
124 return FAILURE;
125 }
126
127 #define HTTP_KEYLIST_VAL(array, k, str, len) \
128 { \
129 char *decoded; \
130 size_t decoded_len; \
131 if (decode) { \
132 decode(str, len, &decoded, &decoded_len TSRMLS_CC); \
133 } else { \
134 decoded_len = len; \
135 decoded = estrndup(str, decoded_len); \
136 } \
137 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
138 }
139 #define HTTP_KEYLIST_FIXKEY() \
140 { \
141 while (isspace(*key)) ++key; \
142 keylen = val - key; \
143 while (isspace(key[keylen - 1])) --keylen; \
144 }
145 #define HTTP_KEYLIST_FIXVAL() \
146 { \
147 ++val; \
148 while (isspace(*val)) ++val; \
149 vallen = key - val; \
150 while (isspace(val[vallen - 1])) --vallen; \
151 }
152
153 HTTP_KEYLIST_FIXKEY();
154
155 if (first_entry_is_name_value_pair) {
156 HTTP_KEYLIST_VAL(&array, "name", key, keylen);
157
158 /* just one name=value */
159 if (!(key = strchr(val, separator))) {
160 key = val + strlen(val);
161 HTTP_KEYLIST_FIXVAL();
162 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
163 return SUCCESS;
164 }
165 /* additional info appended */
166 else {
167 HTTP_KEYLIST_FIXVAL();
168 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
169 }
170 }
171
172 do {
173 char *keydup = NULL;
174
175 if (!(val = strchr(key, '='))) {
176 break;
177 }
178
179 /* start at 0 if first_entry_is_name_value_pair==0 */
180 if (zend_hash_num_elements(items)) {
181 ++key;
182 }
183
184 HTTP_KEYLIST_FIXKEY();
185 keydup = estrndup(key, keylen);
186 if (!(key = strchr(val, separator))) {
187 done = 1;
188 key = val + strlen(val);
189 }
190 HTTP_KEYLIST_FIXVAL();
191 HTTP_KEYLIST_VAL(&array, keydup, val, vallen);
192 efree(keydup);
193 } while (!done);
194
195 return SUCCESS;
196 }
197 /* }}} */
198
199 /* {{{ void http_error(long, long, char*) */
200 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
201 {
202 va_list args;
203
204 va_start(args, format);
205 #ifdef ZEND_ENGINE_2
206 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
207 char *message;
208
209 vspprintf(&message, 0, format, args);
210 zend_throw_exception(http_exception_get_for_code(code), message, code TSRMLS_CC);
211 efree(message);
212 } else
213 #endif
214 php_verror(NULL, "", type, format, args TSRMLS_CC);
215 va_end(args);
216 }
217 /* }}} */
218
219 /* {{{ void http_log(char *, char *, char *) */
220 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
221 {
222 time_t now;
223 struct tm nowtm;
224 char datetime[128];
225
226 time(&now);
227 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
228
229 #define HTTP_LOG_WRITE(file, type, msg) \
230 if (file && *file) { \
231 php_stream *log = php_stream_open_wrapper(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); \
232 \
233 if (log) { \
234 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
235 php_stream_close(log); \
236 } \
237 \
238 }
239
240 HTTP_LOG_WRITE(file, ident, message);
241 HTTP_LOG_WRITE(HTTP_G(log).composite, ident, message);
242 }
243 /* }}} */
244
245 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
246 {
247 *handled_output = ecalloc(1,1);
248 *handled_output_len = 0;
249 }
250
251 /* {{{ STATUS http_exit(int, char*, char*) */
252 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
253 {
254 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
255 (!send_header && status && (SUCCESS != http_send_status(status)))) {
256 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
257 STR_FREE(header);
258 STR_FREE(body);
259 return FAILURE;
260 }
261
262 php_end_ob_buffers(0 TSRMLS_CC);
263 if (php_header(TSRMLS_C) && body) {
264 PHPWRITE(body, strlen(body));
265 }
266
267 switch (status)
268 {
269 case 301: http_log(HTTP_G(log).redirect, "301-REDIRECT", header); break;
270 case 302: http_log(HTTP_G(log).redirect, "302-REDIRECT", header); break;
271 case 303: http_log(HTTP_G(log).redirect, "303-REDIRECT", header); break;
272 case 307: http_log(HTTP_G(log).redirect, "307-REDIRECT", header); break;
273 case 304: http_log(HTTP_G(log).cache, "304-CACHE", header); break;
274 case 405: http_log(HTTP_G(log).allowed_methods, "405-ALLOWED", header); break;
275 default: http_log(NULL, header, body); break;
276 }
277
278 STR_FREE(header);
279 STR_FREE(body);
280
281 if (HTTP_G(force_exit)) {
282 zend_bailout();
283 } else {
284 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
285 }
286
287 return SUCCESS;
288 }
289 /* }}} */
290
291 /* {{{ STATUS http_check_method(char *) */
292 STATUS _http_check_method_ex(const char *method, const char *methods)
293 {
294 const char *found;
295
296 if ( (found = strstr(methods, method)) &&
297 (found == method || !isalpha(found[-1])) &&
298 (!isalpha(found[strlen(method) + 1]))) {
299 return SUCCESS;
300 }
301 return FAILURE;
302 }
303 /* }}} */
304
305 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
306 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
307 {
308 zval **hsv;
309 zval **var;
310
311 if (SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) {
312 return NULL;
313 }
314 if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) {
315 return NULL;
316 }
317 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
318 return NULL;
319 }
320 return *var;
321 }
322 /* }}} */
323
324 /* {{{ STATUS http_get_request_body(char **, size_t *) */
325 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
326 {
327 *length = 0;
328 *body = NULL;
329
330 if (SG(request_info).raw_post_data) {
331 *length = SG(request_info).raw_post_data_length;
332 *body = (char *) (dup ? estrndup(SG(request_info).raw_post_data, *length) : SG(request_info).raw_post_data);
333 return SUCCESS;
334 }
335 return FAILURE;
336 }
337 /* }}} */
338
339
340 /* {{{ char *http_guess_content_type(char *magic_file, long magic_mode, void *data, size_t size, http_send_mode mode) */
341 PHP_HTTP_API char *_http_guess_content_type(const char *magicfile, long magicmode, void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
342 {
343 char *ct = NULL;
344
345 #ifdef HTTP_HAVE_MAGIC
346 /* magic_load() fails if MAGIC_MIME is set because it
347 cowardly adds .mime to the file name */
348 struct magic_set *magic = magic_open(magicmode &~ MAGIC_MIME);
349
350 if (!magic) {
351 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid magic mode: %ld", magicmode);
352 } else if (-1 == magic_load(magic, magicfile)) {
353 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s' (%s)", magicfile, magic_error(magic));
354 } else {
355 const char *ctype = NULL;
356
357 magic_setflags(magic, magicmode);
358
359 switch (data_mode)
360 {
361 case SEND_RSRC:
362 {
363 char *buffer;
364 size_t b_len;
365
366 b_len = php_stream_copy_to_mem(data_ptr, &buffer, 65536, 0);
367 ctype = magic_buffer(magic, buffer, b_len);
368 efree(buffer);
369 }
370 break;
371
372 case SEND_DATA:
373 ctype = magic_buffer(magic, data_ptr, data_len);
374 break;
375
376 default:
377 ctype = magic_file(magic, data_ptr);
378 break;
379 }
380
381 if (ctype) {
382 ct = estrdup(ctype);
383 } else {
384 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to guess Content-Type: %s", magic_error(magic));
385 }
386 }
387 if (magic) {
388 magic_close(magic);
389 }
390 #else
391 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
392 #endif
393
394 return ct;
395 }
396 /* }}} */
397 /*
398 * Local variables:
399 * tab-width: 4
400 * c-basic-offset: 4
401 * End:
402 * vim600: noet sw=4 ts=4 fdm=marker
403 * vim<600: noet sw=4 ts=4
404 */
405