- fixed access of super globals
[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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #include "php_http.h"
17
18 #include "php_output.h"
19 #include "ext/standard/url.h"
20
21 #include "php_http_api.h"
22 #include "php_http_send_api.h"
23
24 #ifdef ZEND_ENGINE_2
25 # include "php_http_exception_object.h"
26 #endif
27
28 PHP_MINIT_FUNCTION(http_support)
29 {
30 HTTP_LONG_CONSTANT("HTTP_SUPPORT", HTTP_SUPPORT);
31 HTTP_LONG_CONSTANT("HTTP_SUPPORT_REQUESTS", HTTP_SUPPORT_REQUESTS);
32 HTTP_LONG_CONSTANT("HTTP_SUPPORT_MAGICMIME", HTTP_SUPPORT_MAGICMIME);
33 HTTP_LONG_CONSTANT("HTTP_SUPPORT_ENCODINGS", HTTP_SUPPORT_ENCODINGS);
34 HTTP_LONG_CONSTANT("HTTP_SUPPORT_SSLREQUESTS", HTTP_SUPPORT_SSLREQUESTS);
35
36 return SUCCESS;
37 }
38
39 PHP_HTTP_API long _http_support(long feature)
40 {
41 long support = HTTP_SUPPORT;
42
43 #ifdef HTTP_HAVE_CURL
44 support |= HTTP_SUPPORT_REQUESTS;
45 # ifdef HTTP_HAVE_SSL
46 support |= HTTP_SUPPORT_SSLREQUESTS;
47 # endif
48 #endif
49 #ifdef HTTP_HAVE_MAGIC
50 support |= HTTP_SUPPORT_MAGICMIME;
51 #endif
52 #ifdef HTTP_HAVE_ZLIB
53 support |= HTTP_SUPPORT_ENCODINGS;
54 #endif
55
56 if (feature) {
57 return (feature == (support & feature));
58 }
59 return support;
60 }
61
62 /* char *pretty_key(char *, size_t, zend_bool, zend_bool) */
63 char *_http_pretty_key(char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen)
64 {
65 if (key && key_len) {
66 size_t i;
67 int wasalpha;
68 if ((wasalpha = isalpha((int) key[0]))) {
69 key[0] = (char) (uctitle ? toupper((int) key[0]) : tolower((int) key[0]));
70 }
71 for (i = 1; i < key_len; i++) {
72 if (isalpha((int) key[i])) {
73 key[i] = (char) (((!wasalpha) && uctitle) ? toupper((int) key[i]) : tolower((int) key[i]));
74 wasalpha = 1;
75 } else {
76 if (xhyphen && (key[i] == '_')) {
77 key[i] = '-';
78 }
79 wasalpha = 0;
80 }
81 }
82 }
83 return key;
84 }
85 /* }}} */
86
87 /* {{{ void http_error(long, long, char*) */
88 void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...)
89 {
90 va_list args;
91
92 va_start(args, format);
93 #ifdef ZEND_ENGINE_2
94 if ((type == E_THROW) || (PG(error_handling) == EH_THROW)) {
95 char *message;
96
97 vspprintf(&message, 0, format, args);
98 zend_throw_exception(http_exception_get_for_code(code), message, code TSRMLS_CC);
99 efree(message);
100 } else
101 #endif
102 php_verror(NULL, "", type, format, args TSRMLS_CC);
103 va_end(args);
104 }
105 /* }}} */
106
107 /* {{{ void http_log(char *, char *, char *) */
108 void _http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC)
109 {
110 time_t now;
111 struct tm nowtm;
112 char datetime[20] = {0};
113
114 now = HTTP_GET_REQUEST_TIME();
115 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
116
117 #define HTTP_LOG_WRITE(file, type, msg) \
118 if (file && *file) { \
119 php_stream *log = php_stream_open_wrapper_ex(file, "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT); \
120 \
121 if (log) { \
122 php_stream_printf(log TSRMLS_CC, "%s\t[%s]\t%s\t<%s>%s", datetime, type, msg, SG(request_info).request_uri, PHP_EOL); \
123 php_stream_close(log); \
124 } \
125 \
126 }
127
128 HTTP_LOG_WRITE(file, ident, message);
129 HTTP_LOG_WRITE(HTTP_G->log.composite, ident, message);
130 }
131 /* }}} */
132
133 static void http_ob_blackhole(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
134 {
135 *handled_output = ecalloc(1,1);
136 *handled_output_len = 0;
137 }
138
139 /* {{{ STATUS http_exit(int, char*, char*) */
140 STATUS _http_exit_ex(int status, char *header, char *body, zend_bool send_header TSRMLS_DC)
141 {
142 if ( (send_header && (SUCCESS != http_send_status_header(status, header))) ||
143 (!send_header && status && (SUCCESS != http_send_status(status)))) {
144 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Failed to exit with status/header: %d - %s", status, header ? header : "");
145 STR_FREE(header);
146 STR_FREE(body);
147 return FAILURE;
148 }
149
150 php_end_ob_buffers(0 TSRMLS_CC);
151 if ((SUCCESS == sapi_send_headers(TSRMLS_C)) && body) {
152 PHPWRITE(body, strlen(body));
153 }
154
155 switch (status)
156 {
157 case 301: http_log(HTTP_G->log.redirect, "301-REDIRECT", header); break;
158 case 302: http_log(HTTP_G->log.redirect, "302-REDIRECT", header); break;
159 case 303: http_log(HTTP_G->log.redirect, "303-REDIRECT", header); break;
160 case 305: http_log(HTTP_G->log.redirect, "305-REDIRECT", header); break;
161 case 307: http_log(HTTP_G->log.redirect, "307-REDIRECT", header); break;
162 case 304: http_log(HTTP_G->log.cache, "304-CACHE", header); break;
163 case 405: http_log(HTTP_G->log.allowed_methods, "405-ALLOWED", header); break;
164 default: http_log(NULL, header, body); break;
165 }
166
167 STR_FREE(header);
168 STR_FREE(body);
169
170 if (HTTP_G->force_exit) {
171 zend_bailout();
172 } else {
173 php_ob_set_internal_handler(http_ob_blackhole, 4096, "blackhole", 0 TSRMLS_CC);
174 }
175
176 return SUCCESS;
177 }
178 /* }}} */
179
180 /* {{{ STATUS http_check_method(char *) */
181 STATUS _http_check_method_ex(const char *method, const char *methods)
182 {
183 const char *found;
184
185 if ( (found = strstr(methods, method)) &&
186 (found == method || !isalpha(found[-1])) &&
187 (strlen(found) >= strlen(method) && !isalpha(found[strlen(method)]))) {
188 return SUCCESS;
189 }
190 return FAILURE;
191 }
192 /* }}} */
193
194 /* {{{ zval *http_get_server_var_ex(char *, size_t) */
195 PHP_HTTP_API zval *_http_get_server_var_ex(const char *key, size_t key_size, zend_bool check TSRMLS_DC)
196 {
197 zval **hsv;
198 zval **var;
199 #ifdef ZEND_ENGINE_2
200 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
201 #endif
202 if ((SUCCESS != zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
203 return NULL;
204 }
205 if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(hsv), (char *) key, key_size, (void **) &var)) || (Z_TYPE_PP(var) != IS_STRING)) {
206 return NULL;
207 }
208 if (check && !(Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
209 return NULL;
210 }
211 return *var;
212 }
213 /* }}} */
214
215 /* {{{ STATUS http_get_request_body(char **, size_t *) */
216 PHP_HTTP_API STATUS _http_get_request_body_ex(char **body, size_t *length, zend_bool dup TSRMLS_DC)
217 {
218 *length = 0;
219 *body = NULL;
220
221 if (SG(request_info).raw_post_data) {
222 *length = SG(request_info).raw_post_data_length;
223 *body = SG(request_info).raw_post_data;
224
225 if (dup) {
226 *body = estrndup(*body, *length);
227 }
228 return SUCCESS;
229 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
230 char buf[4096];
231 int len;
232
233 HTTP_G->read_post_data = 1;
234
235 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
236 *body = erealloc(*body, *length + len + 1);
237 memcpy(*body + *length, buf, len);
238 *length += len;
239 (*body)[*length] = '\0';
240 }
241
242 /* check for error */
243 if (len < 0) {
244 STR_FREE(*body);
245 *length = 0;
246 return FAILURE;
247 }
248
249 SG(request_info).raw_post_data = *body;
250 SG(request_info).raw_post_data_length = *length;
251
252 if (dup) {
253 *body = estrndup(*body, *length);
254 }
255 return SUCCESS;
256 }
257
258 return FAILURE;
259 }
260 /* }}} */
261
262 /* {{{ php_stream *_http_get_request_body_stream(void) */
263 PHP_HTTP_API php_stream *_http_get_request_body_stream(TSRMLS_D)
264 {
265 php_stream *s = NULL;
266
267 if (SG(request_info).raw_post_data) {
268 s = php_stream_open_wrapper("php://input", "rb", 0, NULL);
269 } else if (sapi_module.read_post && !HTTP_G->read_post_data) {
270 HTTP_G->read_post_data = 1;
271
272 if ((s = php_stream_temp_new())) {
273 char buf[4096];
274 int len;
275
276 while (0 < (len = sapi_module.read_post(buf, sizeof(buf) TSRMLS_CC))) {
277 php_stream_write(s, buf, len);
278 }
279
280 if (len < 0) {
281 php_stream_close(s);
282 s = NULL;
283 } else {
284 php_stream_rewind(s);
285 }
286 }
287 }
288
289 return s;
290 }
291 /* }}} */
292
293 /*
294 * Local variables:
295 * tab-width: 4
296 * c-basic-offset: 4
297 * End:
298 * vim600: noet sw=4 ts=4 fdm=marker
299 * vim<600: noet sw=4 ts=4
300 */
301