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