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