- a stream wrapper that defeats the idea of streams is not really useful, so remove...
[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
19 #include "php_http.h"
20
21 #include "SAPI.h"
22 #include "php_output.h"
23 #include "ext/standard/url.h"
24 #include "ext/standard/head.h"
25
26 #include "php_http_api.h"
27 #include "php_http_send_api.h"
28
29 #ifdef ZEND_ENGINE_2
30 # include "php_http_exception_object.h"
31 #endif
32
33 ZEND_EXTERN_MODULE_GLOBALS(http);
34
35 PHP_MINIT_FUNCTION(http_support)
36 {
37 HTTP_LONG_CONSTANT("HTTP_SUPPORT", HTTP_SUPPORT);
38 HTTP_LONG_CONSTANT("HTTP_SUPPORT_REQUESTS", HTTP_SUPPORT_REQUESTS);
39 HTTP_LONG_CONSTANT("HTTP_SUPPORT_MAGICMIME", HTTP_SUPPORT_MAGICMIME);
40 HTTP_LONG_CONSTANT("HTTP_SUPPORT_ENCODINGS", HTTP_SUPPORT_ENCODINGS);
41 HTTP_LONG_CONSTANT("HTTP_SUPPORT_SSLREQUESTS", HTTP_SUPPORT_SSLREQUESTS);
42
43 return SUCCESS;
44 }
45
46 PHP_HTTP_API long _http_support(long feature)
47 {
48 long support = HTTP_SUPPORT;
49
50 #ifdef HTTP_HAVE_CURL
51 support |= HTTP_SUPPORT_REQUESTS;
52 # ifdef HTTP_HAVE_SSL
53 support |= HTTP_SUPPORT_SSLREQUESTS;
54 # endif
55 #endif
56 #ifdef HTTP_HAVE_MAGIC
57 support |= HTTP_SUPPORT_MAGICMIME;
58 #endif
59 #if defined(HTTP_HAVE_ZLIB) || defined(HAVE_ZLIB)
60 support |= HTTP_SUPPORT_ENCODINGS;
61 #endif
62
63 if (feature) {
64 return (feature == (support & feature));
65 }
66 return support;
67 }
68
69 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
70 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
71 {
72 if (key && key_len) {
73 size_t i;
74 int wasalpha;
75 if ((wasalpha = isalpha((int) key[0]))) {
76 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
77 }
78 for (i = 1; i < key_len; i++) {
79 if (isalpha((int) key[i])) {
80 key[i] = (char) (((!wasalpha) && uctitle) ? toupper((int) key[i]) : tolower((int) key[i]));
81 wasalpha = 1;
82 } else {
83 if (xhyphen && (key[i] == '_')) {
84 key[i] = '-';
85 }
86 wasalpha = 0;
87 }
88 }
89 }
90 return key;
91 }
92 /* }}} */
93
94 /* {{{ */
95 void _http_key_list_default_decoder(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
96 {
97 *decoded = estrndup(encoded, encoded_len);
98 *decoded_len = (size_t) php_url_decode(*decoded, encoded_len);
99 }
100 /* }}} */
101
102 /* {{{ */
103 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)
104 {
105 const char *key = list, *val = NULL;
106 int vallen = 0, keylen = 0, done = 0;
107 zval array;
108
109 INIT_ZARR(array, items);
110
111 if (!(val = strchr(list, '='))) {
112 return FAILURE;
113 }
114
115 #define HTTP_KEYLIST_VAL(array, k, str, len) \
116 { \
117 char *decoded; \
118 size_t decoded_len; \
119 if (decode) { \
120 decode(str, len, &decoded, &decoded_len TSRMLS_CC); \
121 } else { \
122 decoded_len = len; \
123 decoded = estrndup(str, decoded_len); \
124 } \
125 add_assoc_stringl(array, k, decoded, decoded_len, 0); \
126 }
127 #define HTTP_KEYLIST_FIXKEY() \
128 { \
129 while (isspace(*key)) ++key; \
130 keylen = val - key; \
131 while (isspace(key[keylen - 1])) --keylen; \
132 }
133 #define HTTP_KEYLIST_FIXVAL() \
134 { \
135 ++val; \
136 while (isspace(*val)) ++val; \
137 vallen = key - val; \
138 while (isspace(val[vallen - 1])) --vallen; \
139 }
140
141 HTTP_KEYLIST_FIXKEY();
142
143 if (first_entry_is_name_value_pair) {
144 HTTP_KEYLIST_VAL(&array, "name", key, keylen);
145
146 /* just one name=value */
147 if (!(key = strchr(val, separator))) {
148 key = val + strlen(val);
149 HTTP_KEYLIST_FIXVAL();
150 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
151 return SUCCESS;
152 }
153 /* additional info appended */
154 else {
155 HTTP_KEYLIST_FIXVAL();
156 HTTP_KEYLIST_VAL(&array, "value", val, vallen);
157 }
158 }
159
160 do {
161 char *keydup = NULL;
162
163 if (!(val = strchr(key, '='))) {
164 break;
165 }
166
167 /* start at 0 if first_entry_is_name_value_pair==0 */
168 if (zend_hash_num_elements(items)) {
169 ++key;
170 }
171
172 HTTP_KEYLIST_FIXKEY();
173 keydup = estrndup(key, keylen);
174 if (!(key = strchr(val, separator))) {
175 done = 1;
176 key = val + strlen(val);
177 }
178 HTTP_KEYLIST_FIXVAL();
179 HTTP_KEYLIST_VAL(&array, keydup, val, vallen);
180 efree(keydup);
181 } while (!done);
182
183 return SUCCESS;
184 }
185 /* }}} */
186
187 /* {{{ void http_error(long, long, char*) */
188 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
189 {
190 va_list args;
191
192 va_start(args, format);
193 #ifdef ZEND_ENGINE_2
194 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
195 char *message;
196
197 vspprintf(&message, 0, format, args);
198 zend_throw_exception(http_exception_get_for_code(code), message, code TSRMLS_CC);
199 efree(message);
200 } else
201 #endif
202 php_verror(NULL, "", type, format, args TSRMLS_CC);
203 va_end(args);
204 }
205 /* }}} */
206
207 /* {{{ void http_log(char *, char *, char *) */
208 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
209 {
210 time_t now;
211 struct tm nowtm;
212 char datetime[128];
213
214 HTTP_CHECK_OPEN_BASEDIR(file, return);
215
216 time(&now);
217 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
218
219 #define HTTP_LOG_WRITE(file, type, msg) \
220 if (file && *file) { \
221 php_stream *log = php_stream_open_wrapper(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL); \
222 \
223 if (log) { \
224 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
225 php_stream_close(log); \
226 } \
227 \
228 }
229
230 HTTP_LOG_WRITE(file, ident, message);
231 HTTP_LOG_WRITE(HTTP_G(log).composite, ident, message);
232 }
233 /* }}} */
234
235 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
236 {
237 *handled_output = ecalloc(1,1);
238 *handled_output_len = 0;
239 }
240
241 /* {{{ STATUS http_exit(int, char*, char*) */
242 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
243 {
244 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
245 (!send_header && status && (SUCCESS != http_send_status(status)))) {
246 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
247 STR_FREE(header);
248 STR_FREE(body);
249 return FAILURE;
250 }
251
252 php_end_ob_buffers(0 TSRMLS_CC);
253 if (php_header(TSRMLS_C) && body) {
254 PHPWRITE(body, strlen(body));
255 }
256
257 switch (status)
258 {
259 case 301: http_log(HTTP_G(log).redirect, "301-REDIRECT", header); break;
260 case 302: http_log(HTTP_G(log).redirect, "302-REDIRECT", header); break;
261 case 303: http_log(HTTP_G(log).redirect, "303-REDIRECT", header); break;
262 case 307: http_log(HTTP_G(log).redirect, "307-REDIRECT", header); break;
263 case 304: http_log(HTTP_G(log).cache, "304-CACHE", header); break;
264 case 405: http_log(HTTP_G(log).allowed_methods, "405-ALLOWED", header); break;
265 default: http_log(NULL, header, body); break;
266 }
267
268 STR_FREE(header);
269 STR_FREE(body);
270
271 if (HTTP_G(force_exit)) {
272 zend_bailout();
273 } else {
274 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
275 }
276
277 return SUCCESS;
278 }
279 /* }}} */
280
281 /* {{{ STATUS http_check_method(char *) */
282 STATUS _http_check_method_ex(const char *method, const char *methods)
283 {
284 const char *found;
285
286 if ( (found = strstr(methods, method)) &&
287 (found == method || !isalpha(found[-1])) &&
288 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
289 return SUCCESS;
290 }
291 return FAILURE;
292 }
293 /* }}} */
294
295 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
296 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
297 {
298 zval **hsv;
299 zval **var;
300
301 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
302 return NULL;
303 }
304 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) || (Z_TYPE_PP(var) != IS_STRING)) {
305 return NULL;
306 }
307 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
308 return NULL;
309 }
310 return *var;
311 }
312 /* }}} */
313
314 /* {{{ STATUS http_get_request_body(char **, size_t *) */
315 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
316 {
317 *length = 0;
318 *body = NULL;
319
320 if (SG(request_info).raw_post_data) {
321 *length = SG(request_info).raw_post_data_length;
322 *body = (char *) (dup ? estrndup(SG(request_info).raw_post_data, *length) : SG(request_info).raw_post_data);
323 return SUCCESS;
324 }
325 return FAILURE;
326 }
327 /* }}} */
328
329
330 /*
331 * Local variables:
332 * tab-width: 4
333 * c-basic-offset: 4
334 * End:
335 * vim600: noet sw=4 ts=4 fdm=marker
336 * vim<600: noet sw=4 ts=4
337 */
338