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