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