* filestat fixes
[m6w6/ext-http] / http.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 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "php.h"
26 #include "php_ini.h"
27 #include "snprintf.h"
28 #include "ext/standard/info.h"
29 #include "ext/session/php_session.h"
30 #include "ext/standard/php_string.h"
31 #include "ext/standard/php_smart_str.h"
32
33 #include "SAPI.h"
34
35 #include "php_http.h"
36 #include "php_http_api.h"
37
38 #ifdef ZEND_ENGINE_2
39 # include "ext/standard/php_http.h"
40 #endif
41
42 #ifdef HTTP_HAVE_CURL
43
44 # ifdef PHP_WIN32
45 # include <winsock2.h>
46 # include <sys/types.h>
47 # endif
48
49 # include <curl/curl.h>
50
51 /* {{{ ARG_INFO */
52 # ifdef ZEND_BEGIN_ARG_INFO
53 ZEND_BEGIN_ARG_INFO(http_request_info_ref_3, 0)
54 ZEND_ARG_PASS_INFO(0)
55 ZEND_ARG_PASS_INFO(0)
56 ZEND_ARG_PASS_INFO(1)
57 ZEND_END_ARG_INFO();
58
59 ZEND_BEGIN_ARG_INFO(http_request_info_ref_4, 0)
60 ZEND_ARG_PASS_INFO(0)
61 ZEND_ARG_PASS_INFO(0)
62 ZEND_ARG_PASS_INFO(0)
63 ZEND_ARG_PASS_INFO(1)
64 ZEND_END_ARG_INFO();
65 # else
66 static unsigned char http_request_info_ref_3[] = {3, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
67 static unsigned char http_request_info_ref_4[] = {4, BYREF_NONE, BYREF_NONE, BYREF_NONE, BYREF_FORCE};
68 # endif
69 /* }}} ARG_INFO */
70
71 #endif /* HTTP_HAVE_CURL */
72
73 ZEND_DECLARE_MODULE_GLOBALS(http)
74
75 #ifdef COMPILE_DL_HTTP
76 ZEND_GET_MODULE(http)
77 #endif
78
79 /* {{{ http_functions[] */
80 function_entry http_functions[] = {
81 PHP_FE(http_date, NULL)
82 PHP_FE(http_absolute_uri, NULL)
83 PHP_FE(http_negotiate_language, NULL)
84 PHP_FE(http_negotiate_charset, NULL)
85 PHP_FE(http_redirect, NULL)
86 PHP_FE(http_send_status, NULL)
87 PHP_FE(http_send_last_modified, NULL)
88 PHP_FE(http_send_content_type, NULL)
89 PHP_FE(http_send_content_disposition, NULL)
90 PHP_FE(http_match_modified, NULL)
91 PHP_FE(http_match_etag, NULL)
92 PHP_FE(http_cache_last_modified, NULL)
93 PHP_FE(http_cache_etag, NULL)
94 PHP_FE(http_send_data, NULL)
95 PHP_FE(http_send_file, NULL)
96 PHP_FE(http_send_stream, NULL)
97 PHP_FE(http_chunked_decode, NULL)
98 PHP_FE(http_split_response, NULL)
99 PHP_FE(http_parse_headers, NULL)
100 PHP_FE(http_get_request_headers, NULL)
101 #ifdef HTTP_HAVE_CURL
102 PHP_FE(http_get, http_request_info_ref_3)
103 PHP_FE(http_head, http_request_info_ref_3)
104 PHP_FE(http_post_data, http_request_info_ref_4)
105 PHP_FE(http_post_array, http_request_info_ref_4)
106 #endif
107 PHP_FE(http_auth_basic, NULL)
108 PHP_FE(http_auth_basic_cb, NULL)
109 #ifndef ZEND_ENGINE_2
110 PHP_FE(http_build_query, NULL)
111 #endif
112 PHP_FE(ob_httpetaghandler, NULL)
113 {NULL, NULL, NULL}
114 };
115 /* }}} */
116
117 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
118 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
119 #define NO_ARGS if (ZEND_NUM_ARGS()) WRONG_PARAM_COUNT
120 #define HTTP_URL_ARGSEP_OVERRIDE zend_alter_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, "&", 1, ZEND_INI_ALL, ZEND_INI_STAGE_RUNTIME)
121 #define HTTP_URL_ARGSEP_RESTORE zend_restore_ini_entry("arg_separator.output", sizeof("arg_separator.output") - 1, ZEND_INI_STAGE_RUNTIME)
122
123 #ifdef ZEND_ENGINE_2
124
125 # define HTTP_REGISTER_CLASS_EX(classname, name, parent, flags) \
126 { \
127 zend_class_entry ce; \
128 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
129 ce.create_object = name## _new_object; \
130 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
131 name## _ce->ce_flags |= flags; \
132 memcpy(& name## _object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
133 name## _object_handlers.clone_obj = NULL; \
134 name## _declare_default_properties(name## _ce); \
135 }
136
137 # define HTTP_REGISTER_CLASS(classname, name, parent, flags) \
138 { \
139 zend_class_entry ce; \
140 INIT_CLASS_ENTRY(ce, #classname, name## _class_methods); \
141 ce.create_object = NULL; \
142 name## _ce = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
143 name## _ce->ce_flags |= flags; \
144 }
145
146 # define getObject(t, o) t * o = ((t *) zend_object_store_get_object(getThis() TSRMLS_CC))
147 # define OBJ_PROP(o) o->zo.properties
148 # define DCL_PROP(a, t, n, v) zend_declare_property_ ##t(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
149 # define DCL_PROP_Z(a, n, v) zend_declare_property(ce, (#n), sizeof(#n), (v), (ZEND_ACC_ ##a) TSRMLS_CC)
150 # define DCL_PROP_N(a, n) zend_declare_property_null(ce, (#n), sizeof(#n), (ZEND_ACC_ ##a) TSRMLS_CC)
151 # define UPD_PROP(o, t, n, v) zend_update_property_ ##t(o->zo.ce, getThis(), (#n), sizeof(#n), (v) TSRMLS_CC)
152 # define SET_PROP(o, n, z) zend_update_property(o->zo.ce, getThis(), (#n), sizeof(#n), (z) TSRMLS_CC)
153 # define GET_PROP(o, n) zend_read_property(o->zo.ce, getThis(), (#n), sizeof(#n), 0 TSRMLS_CC)
154
155 /* {{{ HTTPi */
156
157 zend_class_entry *httpi_ce;
158
159 #define HTTPi_ME(me, al, ai) ZEND_FENTRY(me, ZEND_FN(al), ai, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
160
161 zend_function_entry httpi_class_methods[] = {
162 HTTPi_ME(date, http_date, NULL)
163 HTTPi_ME(absoluteURI, http_absolute_uri, NULL)
164 HTTPi_ME(negotiateLanguage, http_negotiate_language, NULL)
165 HTTPi_ME(negotiateCharset, http_negotiate_charset, NULL)
166 HTTPi_ME(redirect, http_redirect, NULL)
167 HTTPi_ME(sendStatus, http_send_status, NULL)
168 HTTPi_ME(sendLastModified, http_send_last_modified, NULL)
169 HTTPi_ME(sendContentType, http_send_content_type, NULL)
170 HTTPi_ME(sendContentDisposition, http_send_content_disposition, NULL)
171 HTTPi_ME(matchModified, http_match_modified, NULL)
172 HTTPi_ME(matchEtag, http_match_etag, NULL)
173 HTTPi_ME(cacheLastModified, http_cache_last_modified, NULL)
174 HTTPi_ME(cacheEtag, http_cache_etag, NULL)
175 HTTPi_ME(chunkedDecode, http_chunked_decode, NULL)
176 HTTPi_ME(splitResponse, http_split_response, NULL)
177 HTTPi_ME(parseHeaders, http_parse_headers, NULL)
178 HTTPi_ME(getRequestHeaders, http_get_request_headers, NULL)
179 #ifdef HTTP_HAVE_CURL
180 HTTPi_ME(get, http_get, http_request_info_ref_3)
181 HTTPi_ME(head, http_head, http_request_info_ref_3)
182 HTTPi_ME(postData, http_post_data, http_request_info_ref_4)
183 HTTPi_ME(postArray, http_post_array, http_request_info_ref_4)
184 #endif
185 HTTPi_ME(authBasic, http_auth_basic, NULL)
186 HTTPi_ME(authBasicCallback, http_auth_basic_cb, NULL)
187 {NULL, NULL, NULL}
188 };
189 /* }}} HTTPi */
190
191 /* {{{ HTTPi_Response */
192
193 zend_class_entry *httpi_response_ce;
194 static zend_object_handlers httpi_response_object_handlers;
195
196 typedef struct {
197 zend_object zo;
198 } httpi_response_object;
199
200 #define httpi_response_declare_default_properties(ce) _httpi_response_declare_default_properties(ce TSRMLS_CC)
201 static inline void _httpi_response_declare_default_properties(zend_class_entry *ce TSRMLS_DC)
202 {
203 DCL_PROP(PROTECTED, string, contentType, "application/x-octetstream");
204 DCL_PROP(PROTECTED, string, eTag, "");
205 DCL_PROP(PROTECTED, string, dispoFile, "");
206 DCL_PROP(PROTECTED, string, cacheControl, "public");
207 DCL_PROP(PROTECTED, string, data, "");
208 DCL_PROP(PROTECTED, string, file, "");
209 DCL_PROP(PROTECTED, long, stream, 0);
210 DCL_PROP(PROTECTED, long, lastModified, 0);
211 DCL_PROP(PROTECTED, long, dispoInline, 0);
212 DCL_PROP(PROTECTED, long, cache, 0);
213 DCL_PROP(PROTECTED, long, gzip, 0);
214
215 DCL_PROP(PRIVATE, long, raw_cache_header, 0);
216 DCL_PROP(PRIVATE, long, send_mode, -1);
217 }
218
219 #define httpi_response_destroy_object _httpi_response_destroy_object
220 void _httpi_response_destroy_object(void *object, zend_object_handle handle TSRMLS_DC)
221 {
222 httpi_response_object *o = object;
223 if (OBJ_PROP(o)) {
224 zend_hash_destroy(OBJ_PROP(o));
225 FREE_HASHTABLE(OBJ_PROP(o));
226 }
227 efree(o);
228 }
229
230 #define httpi_response_new_object _httpi_response_new_object
231 zend_object_value _httpi_response_new_object(zend_class_entry *ce TSRMLS_DC)
232 {
233 zend_object_value ov;
234 httpi_response_object *o;
235
236 o = ecalloc(sizeof(httpi_response_object), 1);
237 o->zo.ce = ce;
238
239 ALLOC_HASHTABLE(OBJ_PROP(o));
240 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
241 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
242
243 ov.handle = zend_objects_store_put(o, httpi_response_destroy_object, NULL, NULL TSRMLS_CC);
244 ov.handlers = &httpi_response_object_handlers;
245
246 return ov;
247 }
248
249 zend_function_entry httpi_response_class_methods[] = {
250 PHP_ME(HTTPi_Response, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
251 /* PHP_ME(HTTPi_Response, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
252 */
253 PHP_ME(HTTPi_Response, setETag, NULL, ZEND_ACC_PUBLIC)
254 PHP_ME(HTTPi_Response, getETag, NULL, ZEND_ACC_PUBLIC)
255
256 PHP_ME(HTTPi_Response, setContentDisposition, NULL, ZEND_ACC_PUBLIC)
257 PHP_ME(HTTPi_Response, getContentDisposition, NULL, ZEND_ACC_PUBLIC)
258
259 PHP_ME(HTTPi_Response, setContentType, NULL, ZEND_ACC_PUBLIC)
260 PHP_ME(HTTPi_Response, getContentType, NULL, ZEND_ACC_PUBLIC)
261
262 PHP_ME(HTTPi_Response, setCache, NULL, ZEND_ACC_PUBLIC)
263 PHP_ME(HTTPi_Response, getCache, NULL, ZEND_ACC_PUBLIC)
264
265 PHP_ME(HTTPi_Response, setCacheControl, NULL, ZEND_ACC_PUBLIC)
266 PHP_ME(HTTPi_Response, getCacheControl, NULL, ZEND_ACC_PUBLIC)
267
268 PHP_ME(HTTPi_Response, setGzip, NULL, ZEND_ACC_PUBLIC)
269 PHP_ME(HTTPi_Response, getGzip, NULL, ZEND_ACC_PUBLIC)
270
271 PHP_ME(HTTPi_Response, setData, NULL, ZEND_ACC_PUBLIC)
272 PHP_ME(HTTPi_Response, getData, NULL, ZEND_ACC_PUBLIC)
273
274 PHP_ME(HTTPi_Response, setFile, NULL, ZEND_ACC_PUBLIC)
275 PHP_ME(HTTPi_Response, getFile, NULL, ZEND_ACC_PUBLIC)
276
277 PHP_ME(HTTPi_Response, setStream, NULL, ZEND_ACC_PUBLIC)
278 PHP_ME(HTTPi_Response, getStream, NULL, ZEND_ACC_PUBLIC)
279
280 PHP_ME(HTTPi_Response, send, NULL, ZEND_ACC_PUBLIC)
281
282 {NULL, NULL, NULL}
283 };
284
285 /* {{{ proto void HTTPi_Response::__construct(bool cache, bool gzip)
286 *
287 */
288 PHP_METHOD(HTTPi_Response, __construct)
289 {
290 zend_bool do_cache = 0, do_gzip = 0;
291 getObject(httpi_response_object, obj);
292
293 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) {
294 // throw exception
295 return;
296 }
297
298 UPD_PROP(obj, long, cache, do_cache);
299 UPD_PROP(obj, long, gzip, do_gzip);
300 }
301 /* }}} */
302
303 /* {{{ proto bool HTTPi_Response::setCache(bool cache)
304 *
305 */
306 PHP_METHOD(HTTPi_Response, setCache)
307 {
308 zend_bool do_cache = 0;
309 getObject(httpi_response_object, obj);
310
311 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_cache)) {
312 RETURN_FALSE;
313 }
314
315 UPD_PROP(obj, long, cache, do_cache);
316 RETURN_TRUE;
317 }
318 /* }}} */
319
320 /* {{{ proto bool HTTPi_Response::getCache()
321 *
322 */
323 PHP_METHOD(HTTPi_Response, getCache)
324 {
325 zval *do_cache = NULL;
326 getObject(httpi_response_object, obj);
327
328 NO_ARGS;
329
330 do_cache = GET_PROP(obj, cache);
331 RETURN_BOOL(Z_LVAL_P(do_cache));
332 }
333 /* }}}*/
334
335 /* {{{ proto bool HTTPi_Response::setGzip(bool gzip)
336 *
337 */
338 PHP_METHOD(HTTPi_Response, setGzip)
339 {
340 zend_bool do_gzip = 0;
341 getObject(httpi_response_object, obj);
342
343 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &do_gzip)) {
344 RETURN_FALSE;
345 }
346
347 UPD_PROP(obj, long, gzip, do_gzip);
348 RETURN_TRUE;
349 }
350 /* }}} */
351
352 /* {{{ proto bool HTTPi_Response::getGzip()
353 *
354 */
355 PHP_METHOD(HTTPi_Response, getGzip)
356 {
357 zval *do_gzip = NULL;
358 getObject(httpi_response_object, obj);
359
360 NO_ARGS;
361
362 do_gzip = GET_PROP(obj, gzip);
363 RETURN_BOOL(Z_LVAL_P(do_gzip));
364 }
365 /* }}} */
366
367 /* {{{ proto bool HTTPi_Response::setCacheControl(string control[, bool raw = false])
368 *
369 */
370 PHP_METHOD(HTTPi_Response, setCacheControl)
371 {
372 char *ccontrol;
373 int cc_len;
374 zend_bool raw = 0;
375 getObject(httpi_response_object, obj);
376
377 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &ccontrol, &cc_len, &raw)) {
378 RETURN_FALSE;
379 }
380
381 if ((!raw) && (strcmp(ccontrol, "public") && strcmp(ccontrol, "private") && strcmp(ccontrol, "no-cache"))) {
382 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cache-Control '%s' doesn't match public, private or no-cache", ccontrol);
383 RETURN_FALSE;
384 }
385
386 UPD_PROP(obj, long, raw_cache_header, raw);
387 UPD_PROP(obj, string, cacheControl, ccontrol);
388 RETURN_TRUE;
389 }
390 /* }}} */
391
392 /* {{{ proto string HTTPi_Response::getCacheControl()
393 *
394 */
395 PHP_METHOD(HTTPi_Response, getCacheControl)
396 {
397 zval *ccontrol;
398 getObject(httpi_response_object, obj);
399
400 NO_ARGS;
401
402 ccontrol = GET_PROP(obj, cacheControl);
403 RETURN_STRINGL(Z_STRVAL_P(ccontrol), Z_STRLEN_P(ccontrol), 1);
404 }
405 /* }}} */
406
407 /* {{{ proto bool HTTPi::setContentType(string content_type)
408 *
409 */
410 PHP_METHOD(HTTPi_Response, setContentType)
411 {
412 char *ctype;
413 int ctype_len;
414 getObject(httpi_response_object, obj);
415
416 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ctype_len)) {
417 RETURN_FALSE;
418 }
419
420 if (!strchr(ctype, '/')) {
421 php_error_docref(NULL TSRMLS_CC, E_WARNING,
422 "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
423 RETURN_FALSE;
424 }
425
426 UPD_PROP(obj, string, contentType, ctype);
427
428 RETURN_TRUE;
429 }
430 /* }}} */
431
432 /* {{{ proto string HTTPi_Response::getContentType()
433 *
434 */
435 PHP_METHOD(HTTPi_Response, getContentType)
436 {
437 zval *ctype;
438 getObject(httpi_response_object, obj);
439
440 NO_ARGS;
441
442 ctype = GET_PROP(obj, contentType);
443 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
444 }
445 /* }}} */
446
447 /* {{{ proto bool HTTPi_Response::setContentDisposition(string filename[, bool inline = false])
448 *
449 */
450 PHP_METHOD(HTTPi_Response, setContentDisposition)
451 {
452 char *file;
453 int file_len;
454 zend_bool is_inline = 0;
455 getObject(httpi_response_object, obj);
456
457 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &file, &file_len, &is_inline)) {
458 RETURN_FALSE;
459 }
460
461 UPD_PROP(obj, string, dispoFile, file);
462 UPD_PROP(obj, long, dispoInline, is_inline);
463 RETURN_TRUE;
464 }
465 /* }}} */
466
467 /* {{{ proto array HTTPi_Response::getContentDisposition()
468 *
469 */
470 PHP_METHOD(HTTPi_Response, getContentDisposition)
471 {
472 zval *file;
473 zval *is_inline;
474 getObject(httpi_response_object, obj);
475
476 if (ZEND_NUM_ARGS()) {
477 WRONG_PARAM_COUNT;
478 }
479
480 file = GET_PROP(obj, dispoFile);
481 is_inline = GET_PROP(obj, dispoInline);
482
483 array_init(return_value);
484 add_assoc_stringl(return_value, "filename", Z_STRVAL_P(file), Z_STRLEN_P(file), 1);
485 add_assoc_bool(return_value, "inline", Z_LVAL_P(is_inline));
486 }
487 /* }}} */
488
489 /* {{{ proto bool HTTPi_Response::setETag(string etag)
490 *
491 */
492 PHP_METHOD(HTTPi_Response, setETag)
493 {
494 char *etag;
495 int etag_len;
496 getObject(httpi_response_object, obj);
497
498 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len)) {
499 RETURN_FALSE;
500 }
501
502 UPD_PROP(obj, string, eTag, etag);
503 RETURN_TRUE;
504 }
505 /* }}} */
506
507 /* {{{ proto string HTTPi_Response::getETag()
508 *
509 */
510 PHP_METHOD(HTTPi_Response, getETag)
511 {
512 zval *etag;
513 getObject(httpi_response_object, obj);
514
515 NO_ARGS;
516
517 etag = GET_PROP(obj, eTag);
518 RETURN_STRINGL(Z_STRVAL_P(etag), Z_STRLEN_P(etag), 1);
519 }
520 /* }}} */
521
522 /* {{{ proto bool HTTPi_Response::setData(string data)
523 *
524 */
525 PHP_METHOD(HTTPi_Response, setData)
526 {
527 zval *the_data;
528 char *etag;
529 getObject(httpi_response_object, obj);
530
531 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_data)) {
532 RETURN_FALSE;
533 }
534
535 convert_to_string_ex(&the_data);
536 SET_PROP(obj, data, the_data);
537 UPD_PROP(obj, long, lastModified, http_lmod(the_data, SEND_DATA));
538 UPD_PROP(obj, long, send_mode, SEND_DATA);
539 RETURN_TRUE;
540 }
541 /* }}} */
542
543 /* {{{ proto string HTTPi_Response::getData()
544 *
545 */
546 PHP_METHOD(HTTPi_Response, getData)
547 {
548 zval *the_data;
549 getObject(httpi_response_object, obj);
550
551 NO_ARGS;
552
553 the_data = GET_PROP(obj, data);
554 RETURN_STRINGL(Z_STRVAL_P(the_data), Z_STRLEN_P(the_data), 1);
555 }
556 /* }}} */
557
558 /* {{{ proto bool HTTPi_Response::setStream(resource stream)
559 *
560 */
561 PHP_METHOD(HTTPi_Response, setStream)
562 {
563 zval *the_stream;
564 php_stream *the_real_stream;
565 char *etag;
566 getObject(httpi_response_object, obj);
567
568 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &the_stream)) {
569 RETURN_FALSE;
570 }
571
572 php_stream_from_zval(the_real_stream, &the_stream);
573
574 SET_PROP(obj, stream, the_stream);
575 UPD_PROP(obj, long, lastModified, http_lmod(the_real_stream, SEND_RSRC));
576 UPD_PROP(obj, long, send_mode, SEND_RSRC);
577 RETURN_TRUE;
578 }
579 /* }}} */
580
581 /* {{{ proto resource HTTPi_Response::getStream()
582 *
583 */
584 PHP_METHOD(HTTPi_Response, getStream)
585 {
586 zval *the_stream;
587 getObject(httpi_response_object, obj);
588
589 NO_ARGS;
590
591 the_stream = GET_PROP(obj, stream);
592 RETURN_RESOURCE(Z_LVAL_P(the_stream));
593 }
594 /* }}} */
595
596 /* {{{ proto bool HTTPi_Response::setFile(string file)
597 *
598 */
599 PHP_METHOD(HTTPi_Response, setFile)
600 {
601 zval *the_file;
602 getObject(httpi_response_object, obj);
603
604 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &the_file)) {
605 RETURN_FALSE;
606 }
607
608 convert_to_string_ex(&the_file);
609
610 UPD_PROP(obj, string, file, Z_STRVAL_P(the_file));
611 UPD_PROP(obj, long, lastModified, http_lmod(the_file, -1));
612 UPD_PROP(obj, long, send_mode, -1);
613 RETURN_TRUE;
614 }
615 /* }}} */
616
617 /* {{{ proto string HTTPi_Response::getFile()
618 *
619 */
620 PHP_METHOD(HTTPi_Response, getFile)
621 {
622 zval *the_file;
623 getObject(httpi_response_object, obj);
624
625 NO_ARGS;
626
627 the_file = GET_PROP(obj, file);
628 RETURN_STRINGL(Z_STRVAL_P(the_file), Z_STRLEN_P(the_file), 1);
629 }
630 /* }}} */
631
632 PHP_METHOD(HTTPi_Response, send)
633 {
634 zval *do_cache, *do_gzip;
635 getObject(httpi_response_object, obj);
636
637 do_cache = GET_PROP(obj, cache);
638 do_gzip = GET_PROP(obj, gzip);
639
640 /* caching */
641 if (Z_LVAL_P(do_cache)) {
642 zval *cctrl, *etag, *lmod, *ccraw;
643
644 etag = GET_PROP(obj, eTag);
645 lmod = GET_PROP(obj, lastModified);
646 cctrl = GET_PROP(obj, cacheControl);
647 ccraw = GET_PROP(obj, raw_cache_header);
648
649 if (Z_LVAL_P(ccraw)) {
650 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
651 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), Z_STRVAL_P(cctrl), Z_STRLEN_P(cctrl));
652 } else {
653 char cc_header[42] = {0};
654 sprintf(cc_header, "%s, must-revalidate, max-age=0", Z_STRVAL_P(cctrl));
655 http_cache_etag(Z_STRVAL_P(etag), Z_STRLEN_P(etag), cc_header, strlen(cc_header));
656 http_cache_last_modified(Z_LVAL_P(lmod), Z_LVAL_P(lmod) ? Z_LVAL_P(lmod) : time(NULL), cc_header, strlen(cc_header));
657 }
658 }
659
660 /* gzip */
661 if (Z_LVAL_P(do_gzip)) {
662 /* ... */
663 }
664
665 /* content type */
666 {
667 zval *ctype = GET_PROP(obj, contentType);
668 if (Z_STRLEN_P(ctype)) {
669 http_send_content_type(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype));
670 } else {
671 http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1);
672 }
673 }
674
675 /* content disposition */
676 {
677 zval *dispo_file = GET_PROP(obj, dispoFile);
678 if (Z_STRLEN_P(dispo_file)) {
679 zval *dispo_inline = GET_PROP(obj, dispoInline);
680 http_send_content_disposition(Z_STRVAL_P(dispo_file), Z_STRLEN_P(dispo_file), Z_LVAL_P(dispo_inline));
681 }
682 }
683
684 /* send */
685 {
686 zval *send_mode = GET_PROP(obj, send_mode);
687 switch (Z_LVAL_P(send_mode))
688 {
689 case SEND_DATA:
690 {
691 RETURN_SUCCESS(http_send_data(GET_PROP(obj, data)));
692 }
693
694 case SEND_RSRC:
695 {
696 php_stream *the_real_stream;
697 zval *the_stream = GET_PROP(obj, stream);
698 php_stream_from_zval(the_real_stream, &the_stream);
699 RETURN_SUCCESS(http_send_stream(the_real_stream));
700 }
701
702 default:
703 {
704 RETURN_SUCCESS(http_send_file(GET_PROP(obj, file)));
705 }
706 }
707 }
708 }
709 /* }}} */
710
711 /* {{{ HTTPi_Request */
712 #ifdef HTTP_HAVE_CURL
713
714 zend_class_entry *httpi_request_ce;
715 static zend_object_handlers httpi_request_object_handlers;
716
717 typedef struct {
718 zend_object zo;
719 CURL *ch;
720 } httpi_request_object;
721
722 #define httpi_request_declare_default_properties(ce) _httpi_request_declare_default_properties(ce TSRMLS_CC)
723 static inline void _httpi_request_declare_default_properties(zend_class_entry *ce TSRMLS_DC)
724 {
725 DCL_PROP_N(PROTECTED, options);
726 DCL_PROP_N(PROTECTED, responseInfo);
727 DCL_PROP_N(PROTECTED, responseData);
728
729 DCL_PROP(PROTECTED, long, method, HTTP_GET);
730
731 DCL_PROP(PROTECTED, string, url, "");
732 DCL_PROP(PROTECTED, string, contentType, "");
733 DCL_PROP(PROTECTED, string, queryData, "");
734 }
735
736 #define httpi_request_destroy_object _httpi_request_destroy_object
737 void _httpi_request_destroy_object(void *object, zend_object_handle handle TSRMLS_DC)
738 {
739 httpi_request_object *o = object;
740 if (OBJ_PROP(o)) {
741 zend_hash_destroy(OBJ_PROP(o));
742 FREE_HASHTABLE(OBJ_PROP(o));
743 }
744 if (o->ch) {
745 curl_easy_cleanup(o->ch);
746 }
747 efree(o);
748 }
749
750 #define httpi_request_new_object _httpi_request_new_object
751 zend_object_value _httpi_request_new_object(zend_class_entry *ce TSRMLS_DC)
752 {
753 zend_object_value ov;
754 httpi_request_object *o;
755
756 o = ecalloc(sizeof(httpi_request_object), 1);
757 o->zo.ce = ce;
758 o->ch = curl_easy_init();
759
760 ALLOC_HASHTABLE(OBJ_PROP(o));
761 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
762 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
763
764 ov.handle = zend_objects_store_put(o, httpi_request_destroy_object, NULL, NULL TSRMLS_CC);
765 ov.handlers = &httpi_request_object_handlers;
766
767 return ov;
768 }
769
770 zend_function_entry httpi_request_class_methods[] = {
771 PHP_ME(HTTPi_Request, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
772 /* PHP_ME(HTTPi_Request, __destruct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_DTOR)
773
774 PHP_ME(HTTPi_Request, setOptions, NULL, ZEND_ACC_PUBLIC)
775 PHP_ME(HTTPi_Request, getOptions, NULL, ZEND_ACC_PUBLIC)
776 */
777 PHP_ME(HTTPi_Request, setMethod, NULL, ZEND_ACC_PUBLIC)
778 PHP_ME(HTTPi_Request, getMethod, NULL, ZEND_ACC_PUBLIC)
779
780 PHP_ME(HTTPi_Request, setURL, NULL, ZEND_ACC_PUBLIC)
781 PHP_ME(HTTPi_Request, getURL, NULL, ZEND_ACC_PUBLIC)
782
783 PHP_ME(HTTPi_Request, setContentType, NULL, ZEND_ACC_PUBLIC)
784 PHP_ME(HTTPi_Request, getContentType, NULL, ZEND_ACC_PUBLIC)
785
786 PHP_ME(HTTPi_Request, setQueryData, NULL, ZEND_ACC_PUBLIC)
787 PHP_ME(HTTPi_Request, getQueryData, NULL, ZEND_ACC_PUBLIC)
788 PHP_ME(HTTPi_Request, addQueryData, NULL, ZEND_ACC_PUBLIC)
789 PHP_ME(HTTPi_Request, unsetQueryData, NULL, ZEND_ACC_PUBLIC)
790 /*
791 PHP_ME(HTTPi_Request, setPostData, NULL, ZEND_ACC_PUBLIC)
792 PHP_ME(HTTPi_Request, addPostData, NULL, ZEND_ACC_PUBLIC)
793 PHP_ME(HTTPi_Request, unsetPostData, NULL, ZEND_ACC_PUBLIC)
794
795 PHP_ME(HTTPi_Request, addPostFile, NULL, ZEND_ACC_PUBLIC)
796 */
797 PHP_ME(HTTPi_Request, send, NULL, ZEND_ACC_PUBLIC)
798
799 PHP_ME(HTTPi_Request, getResponseData, NULL, ZEND_ACC_PUBLIC)
800 PHP_ME(HTTPi_Request, getResponseHeaders, NULL, ZEND_ACC_PUBLIC)
801 PHP_ME(HTTPi_Request, getResponseBody, NULL, ZEND_ACC_PUBLIC)
802
803 {NULL, NULL, NULL}
804 };
805
806 /* {{{ proto void HTTPi_request::__construct([string url[, long request_method = HTTP_GET]])
807 *
808 */
809 PHP_METHOD(HTTPi_Request, __construct)
810 {
811 char *URL = NULL;
812 int URL_len;
813 long meth = -1;
814 zval *info, *opts, *resp;
815 getObject(httpi_request_object, obj);
816
817 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) {
818 return;
819 }
820
821 MAKE_STD_ZVAL(opts); array_init(opts); SET_PROP(obj, options, opts);
822 MAKE_STD_ZVAL(info); array_init(info); SET_PROP(obj, responseInfo, info);
823 MAKE_STD_ZVAL(resp); array_init(resp); SET_PROP(obj, responseData, resp);
824
825 if (URL) {
826 UPD_PROP(obj, string, url, URL);
827 }
828 if (meth > -1) {
829 UPD_PROP(obj, long, method, meth);
830 }
831 }
832 /* }}} */
833
834 /* {{{ proto bool HTTPi_request::setURL(string url)
835 *
836 */
837 PHP_METHOD(HTTPi_Request, setURL)
838 {
839 char *URL = NULL;
840 int URL_len;
841 getObject(httpi_request_object, obj);
842
843 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URL, &URL_len)) {
844 RETURN_FALSE;
845 }
846
847 UPD_PROP(obj, string, url, URL);
848 RETURN_TRUE;
849 }
850 /* }}} */
851
852 /* {{{ proto string HTTPi_request::getUrl()
853 *
854 */
855 PHP_METHOD(HTTPi_Request, getURL)
856 {
857 zval *URL;
858 getObject(httpi_request_object, obj);
859
860 NO_ARGS;
861
862 URL = GET_PROP(obj, url);
863 RETURN_STRINGL(Z_STRVAL_P(URL), Z_STRLEN_P(URL), 1);
864 }
865 /* }}} */
866
867 /* {{{ proto bool HTTPi_request::setMethod(long request_method)
868 *
869 */
870 PHP_METHOD(HTTPi_Request, setMethod)
871 {
872 long meth;
873 getObject(httpi_request_object, obj);
874
875 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &meth)) {
876 RETURN_FALSE;
877 }
878
879 UPD_PROP(obj, long, method, meth);
880 RETURN_TRUE;
881 }
882 /* }}} */
883
884 /* {{{ proto long HTTPi_request::getMethod()
885 *
886 */
887 PHP_METHOD(HTTPi_Request, getMethod)
888 {
889 zval *meth;
890 getObject(httpi_request_object, obj);
891
892 NO_ARGS;
893
894 meth = GET_PROP(obj, method);
895 RETURN_LONG(Z_LVAL_P(meth));
896 }
897 /* }}} */
898
899 /* {{{ proto bool HTTPi_request::setContentType(string content_type)
900 *
901 */
902 PHP_METHOD(HTTPi_Request, setContentType)
903 {
904 char *ctype;
905 int ct_len;
906 getObject(httpi_request_object, obj);
907
908 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ctype, &ct_len)) {
909 RETURN_FALSE;
910 }
911
912 if (!strchr(ctype, '/')) {
913 php_error_docref(NULL TSRMLS_CC, E_WARNING,
914 "Content-Type '%s' doesn't seem to contain a primary and a secondary part",
915 ctype);
916 RETURN_FALSE;
917 }
918
919 UPD_PROP(obj, string, contentType, ctype);
920 RETURN_TRUE;
921 }
922 /* }}} */
923
924 /* {{{ proto string HTTPi_request::getContentType()
925 *
926 */
927 PHP_METHOD(HTTPi_Request, getContentType)
928 {
929 zval *ctype;
930 getObject(httpi_request_object, obj);
931
932 NO_ARGS;
933
934 ctype = GET_PROP(obj, contentType);
935 RETURN_STRINGL(Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
936 }
937 /* }}} */
938
939 /* {{{ proto bool HTTPi_request::setQueryData(mixed query_data)
940 *
941 */
942 PHP_METHOD(HTTPi_Request, setQueryData)
943 {
944 zval *qdata;
945 getObject(httpi_request_object, obj);
946
947 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata)) {
948 RETURN_FALSE;
949 }
950
951 if ((Z_TYPE_P(qdata) == IS_ARRAY) || (Z_TYPE_P(qdata) == IS_OBJECT)) {
952 smart_str qstr = {0};
953 HTTP_URL_ARGSEP_OVERRIDE;
954 if (SUCCESS != php_url_encode_hash_ex(HASH_OF(qdata), &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) {
955 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data");
956 if (qstr.c) {
957 efree(qstr.c);
958 }
959 HTTP_URL_ARGSEP_RESTORE;
960 RETURN_FALSE;
961 }
962 HTTP_URL_ARGSEP_RESTORE;
963 smart_str_0(&qstr);
964 UPD_PROP(obj, string, queryData, qstr.c);
965 efree(qstr.c);
966 RETURN_TRUE;
967 }
968
969 convert_to_string(qdata);
970 UPD_PROP(obj, string, queryData, Z_STRVAL_P(qdata));
971 RETURN_TRUE;
972 }
973 /* }}} */
974
975 /* {{{ proto string HTTPi_request::getQueryData()
976 *
977 */
978 PHP_METHOD(HTTPi_Request, getQueryData)
979 {
980 zval *qdata;
981 getObject(httpi_request_object, obj);
982
983 NO_ARGS;
984
985 qdata = GET_PROP(obj, queryData);
986 RETURN_STRINGL(Z_STRVAL_P(qdata), Z_STRLEN_P(qdata), 1);
987 }
988 /* }}} */
989
990 /* {{{ proto bool HTTPi_request::addQueryData(array query_params)
991 *
992 */
993 PHP_METHOD(HTTPi_Request, addQueryData)
994 {
995 zval *qdata, *old_qdata;
996 smart_str qstr = {0};
997 char *separator;
998 getObject(httpi_request_object, obj);
999
1000 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &qdata)) {
1001 RETURN_FALSE;
1002 }
1003
1004 old_qdata = GET_PROP(obj, queryData);
1005 if (Z_STRLEN_P(old_qdata)) {
1006 smart_str_appendl(&qstr, Z_STRVAL_P(old_qdata), Z_STRLEN_P(old_qdata));
1007 }
1008
1009 HTTP_URL_ARGSEP_OVERRIDE;
1010 if (SUCCESS != php_url_encode_hash_ex(HASH_OF(qdata), &qstr, NULL, 0, NULL, 0, NULL, 0, NULL TSRMLS_CC)) {
1011 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data");
1012 if (qstr.c) {
1013 efree(qstr.c);
1014 }
1015 HTTP_URL_ARGSEP_RESTORE;
1016 RETURN_FALSE;
1017 }
1018 HTTP_URL_ARGSEP_RESTORE;
1019
1020 smart_str_0(&qstr);
1021
1022 UPD_PROP(obj, string, queryData, qstr.c);
1023 efree(qstr.c);
1024 RETURN_TRUE;
1025 }
1026 /* }}} */
1027
1028 /* {{{ proto void HTTPi_request::unsetQueryData()
1029 *
1030 */
1031 PHP_METHOD(HTTPi_Request, unsetQueryData)
1032 {
1033 getObject(httpi_request_object, obj);
1034
1035 NO_ARGS;
1036
1037 UPD_PROP(obj, string, queryData, "");
1038 }
1039 /* }}} */
1040
1041 /* {{{ proto array HTTPi_request::getResponseData()
1042 *
1043 */
1044 PHP_METHOD(HTTPi_Request, getResponseData)
1045 {
1046 zval *data;
1047 getObject(httpi_request_object, obj);
1048
1049 NO_ARGS;
1050
1051 data = GET_PROP(obj, responseData);
1052 array_init(return_value);
1053 zend_hash_copy(Z_ARRVAL_P(return_value), Z_ARRVAL_P(data), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1054 }
1055 /* }}} */
1056
1057 /* {{{ proto array HTTPi_request::getResponseHeaders()
1058 *
1059 */
1060 PHP_METHOD(HTTPi_Request, getResponseHeaders)
1061 {
1062 zval *data, **headers;
1063 getObject(httpi_request_object, obj);
1064
1065 NO_ARGS;
1066
1067 array_init(return_value);
1068 data = GET_PROP(obj, responseData);
1069 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) {
1070 zend_hash_copy(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(headers), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
1071 }
1072 }
1073 /* }}} */
1074
1075 /* {{{ proto string HTTPi_request::getResponseBody()
1076 *
1077 */
1078 PHP_METHOD(HTTPi_Request, getResponseBody)
1079 {
1080 zval *data, **body;
1081 getObject(httpi_request_object, obj);
1082
1083 NO_ARGS;
1084
1085 data = GET_PROP(obj, responseData);
1086 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body)) {
1087 RETURN_STRINGL(Z_STRVAL_PP(body), Z_STRLEN_PP(body), 1);
1088 } else {
1089 Z_TYPE_P(return_value) = IS_NULL;
1090 }
1091 }
1092 /* }}} */
1093
1094 /* {{{ proto bool HTTPi_request::send()
1095 *
1096 */
1097 PHP_METHOD(HTTPi_Request, send)
1098 {
1099 zval *meth, *URL, *qdata, *opts, *info, *resp;
1100 char *response_data, *request_uri;
1101 size_t response_len;
1102 getObject(httpi_request_object, obj);
1103
1104 NO_ARGS;
1105
1106 if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
1107 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initilaize cURL");
1108 RETURN_FALSE;
1109 }
1110
1111 meth = GET_PROP(obj, method);
1112 URL = GET_PROP(obj, url);
1113 qdata = GET_PROP(obj, queryData);
1114 opts = GET_PROP(obj, options);
1115 info = GET_PROP(obj, responseInfo);
1116 resp = GET_PROP(obj, responseData);
1117
1118 request_uri = http_absolute_uri(Z_STRVAL_P(URL), NULL);
1119 if (Z_STRLEN_P(qdata) && (strlen(request_uri) < HTTP_URI_MAXLEN)) {
1120 if (!strchr(request_uri, '?')) {
1121 strcat(request_uri, "?");
1122 } else {
1123 strcat(request_uri, "&");
1124 }
1125 strncat(request_uri, Z_STRVAL_P(qdata), HTTP_URI_MAXLEN - strlen(request_uri));
1126 }
1127
1128 switch (Z_LVAL_P(meth))
1129 {
1130 case HTTP_GET:
1131 if (SUCCESS != http_get_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len)) {
1132 RETURN_FALSE;
1133 }
1134 break;
1135
1136 case HTTP_HEAD:
1137 if (SUCCESS != http_head_ex(obj->ch, request_uri, Z_ARRVAL_P(opts), Z_ARRVAL_P(info), &response_data, &response_len)) {
1138 RETURN_FALSE;
1139 }
1140 break;
1141
1142 case HTTP_POST:
1143 break;
1144
1145 default:
1146 break;
1147 }
1148
1149 /* final data handling */
1150 {
1151 zval *zheaders, *zbody;
1152
1153 MAKE_STD_ZVAL(zbody);
1154 MAKE_STD_ZVAL(zheaders)
1155 array_init(zheaders);
1156
1157 if (SUCCESS != http_split_response_ex(response_data, response_len, zheaders, zbody)) {
1158 zval_dtor(zheaders);
1159 efree(zheaders),
1160 efree(zbody);
1161 RETURN_FALSE;
1162 }
1163
1164 add_assoc_zval(resp, "headers", zheaders);
1165 add_assoc_zval(resp, "body", zbody);
1166
1167 RETURN_TRUE;
1168 }
1169 /* */
1170 }
1171 /* }}} */
1172
1173 #endif /* HTTP_HAVE_CURL */
1174 /* }}} */
1175
1176 #endif /* ZEND_ENGINE_2 */
1177
1178 /* {{{ http_module_entry */
1179 zend_module_entry http_module_entry = {
1180 #if ZEND_MODULE_API_NO >= 20010901
1181 STANDARD_MODULE_HEADER,
1182 #endif
1183 "http",
1184 http_functions,
1185 PHP_MINIT(http),
1186 PHP_MSHUTDOWN(http),
1187 PHP_RINIT(http),
1188 PHP_RSHUTDOWN(http),
1189 PHP_MINFO(http),
1190 #if ZEND_MODULE_API_NO >= 20010901
1191 PHP_EXT_HTTP_VERSION,
1192 #endif
1193 STANDARD_MODULE_PROPERTIES
1194 };
1195 /* }}} */
1196
1197 /* {{{ proto string http_date([int timestamp])
1198 *
1199 * This function returns a valid HTTP date regarding RFC 822/1123
1200 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
1201 *
1202 */
1203 PHP_FUNCTION(http_date)
1204 {
1205 long t = -1;
1206
1207 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
1208 RETURN_FALSE;
1209 }
1210
1211 if (t == -1) {
1212 t = (long) time(NULL);
1213 }
1214
1215 RETURN_STRING(http_date(t), 0);
1216 }
1217 /* }}} */
1218
1219 /* {{{ proto string http_absolute_uri(string url[, string proto])
1220 *
1221 * This function returns an absolute URI constructed from url.
1222 * If the url is already abolute but a different proto was supplied,
1223 * only the proto part of the URI will be updated. If url has no
1224 * path specified, the path of the current REQUEST_URI will be taken.
1225 * The host will be taken either from the Host HTTP header of the client
1226 * the SERVER_NAME or just localhost if prior are not available.
1227 *
1228 * Some examples:
1229 * <pre>
1230 * url = "page.php" => http://www.example.com/current/path/page.php
1231 * url = "/page.php" => http://www.example.com/page.php
1232 * url = "/page.php", proto = "https" => https://www.example.com/page.php
1233 * </pre>
1234 *
1235 */
1236 PHP_FUNCTION(http_absolute_uri)
1237 {
1238 char *url = NULL, *proto = NULL;
1239 int url_len = 0, proto_len = 0;
1240
1241 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
1242 RETURN_FALSE;
1243 }
1244
1245 RETURN_STRING(http_absolute_uri(url, proto), 0);
1246 }
1247 /* }}} */
1248
1249 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
1250 *
1251 * This function negotiates the clients preferred language based on its
1252 * Accept-Language HTTP header. It returns the negotiated language or
1253 * the default language if none match.
1254 *
1255 * The qualifier is recognized and languages without qualifier are rated highest.
1256 *
1257 * The supported parameter is expected to be an array having
1258 * the supported languages as array values.
1259 *
1260 * Example:
1261 * <pre>
1262 * <?php
1263 * $langs = array(
1264 * 'en-US',// default
1265 * 'fr',
1266 * 'fr-FR',
1267 * 'de',
1268 * 'de-DE',
1269 * 'de-AT',
1270 * 'de-CH',
1271 * );
1272 * include './langs/'. http_negotiate_language($langs) .'.php';
1273 * ?>
1274 * </pre>
1275 *
1276 */
1277 PHP_FUNCTION(http_negotiate_language)
1278 {
1279 zval *supported;
1280 char *def = NULL;
1281 int def_len = 0;
1282
1283 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
1284 RETURN_FALSE;
1285 }
1286
1287 if (!def) {
1288 def = "en-US";
1289 }
1290
1291 RETURN_STRING(http_negotiate_language(supported, def), 0);
1292 }
1293 /* }}} */
1294
1295 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
1296 *
1297 * This function negotiates the clients preferred charset based on its
1298 * Accept-Charset HTTP header. It returns the negotiated charset or
1299 * the default charset if none match.
1300 *
1301 * The qualifier is recognized and charset without qualifier are rated highest.
1302 *
1303 * The supported parameter is expected to be an array having
1304 * the supported charsets as array values.
1305 *
1306 * Example:
1307 * <pre>
1308 * <?php
1309 * $charsets = array(
1310 * 'iso-8859-1', // default
1311 * 'iso-8859-2',
1312 * 'iso-8859-15',
1313 * 'utf-8'
1314 * );
1315 * $pref = http_negotiate_charset($charsets);
1316 * if (!strcmp($pref, 'iso-8859-1')) {
1317 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
1318 * iconv_set_encoding('output_encoding', $pref);
1319 * ob_start('ob_iconv_handler');
1320 * }
1321 * ?>
1322 * </pre>
1323 */
1324 PHP_FUNCTION(http_negotiate_charset)
1325 {
1326 zval *supported;
1327 char *def = NULL;
1328 int def_len = 0;
1329
1330 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
1331 RETURN_FALSE;
1332 }
1333
1334 if (!def) {
1335 def = "iso-8859-1";
1336 }
1337
1338 RETURN_STRING(http_negotiate_charset(supported, def), 0);
1339 }
1340 /* }}} */
1341
1342 /* {{{ proto bool http_send_status(int status)
1343 *
1344 * Send HTTP status code.
1345 *
1346 */
1347 PHP_FUNCTION(http_send_status)
1348 {
1349 int status = 0;
1350
1351 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
1352 RETURN_FALSE;
1353 }
1354 if (status < 100 || status > 510) {
1355 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
1356 RETURN_FALSE;
1357 }
1358
1359 RETURN_SUCCESS(http_send_status(status));
1360 }
1361 /* }}} */
1362
1363 /* {{{ proto bool http_send_last_modified([int timestamp])
1364 *
1365 * This converts the given timestamp to a valid HTTP date and
1366 * sends it as "Last-Modified" HTTP header. If timestamp is
1367 * omitted, current time is sent.
1368 *
1369 */
1370 PHP_FUNCTION(http_send_last_modified)
1371 {
1372 long t = -1;
1373
1374 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
1375 RETURN_FALSE;
1376 }
1377
1378 if (t == -1) {
1379 t = (long) time(NULL);
1380 }
1381
1382 RETURN_SUCCESS(http_send_last_modified(t));
1383 }
1384 /* }}} */
1385
1386 /* {{{ proto bool http_send_content_type([string content_type = 'application/x-octetstream'])
1387 *
1388 * Sets the content type.
1389 *
1390 */
1391 PHP_FUNCTION(http_send_content_type)
1392 {
1393 char *ct;
1394 int ct_len = 0;
1395
1396 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
1397 RETURN_FALSE;
1398 }
1399
1400 if (!ct_len) {
1401 RETURN_SUCCESS(http_send_content_type("application/x-octetstream", sizeof("application/x-octetstream") - 1));
1402 }
1403 RETURN_SUCCESS(http_send_content_type(ct, ct_len));
1404 }
1405 /* }}} */
1406
1407 /* {{{ proto bool http_send_content_disposition(string filename[, bool inline = false])
1408 *
1409 * Set the Content Disposition. The Content-Disposition header is very useful
1410 * if the data actually sent came from a file or something similar, that should
1411 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
1412 *
1413 */
1414 PHP_FUNCTION(http_send_content_disposition)
1415 {
1416 char *filename;
1417 int f_len;
1418 zend_bool send_inline = 0;
1419
1420 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
1421 RETURN_FALSE;
1422 }
1423 RETURN_SUCCESS(http_send_content_disposition(filename, f_len, send_inline));
1424 }
1425 /* }}} */
1426
1427 /* {{{ proto bool http_match_modified([int timestamp])
1428 *
1429 * Matches the given timestamp against the clients "If-Modified-Since" resp.
1430 * "If-Unmodified-Since" HTTP headers.
1431 *
1432 */
1433 PHP_FUNCTION(http_match_modified)
1434 {
1435 long t = -1;
1436
1437 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
1438 RETURN_FALSE;
1439 }
1440
1441 // current time if not supplied (senseless though)
1442 if (t == -1) {
1443 t = (long) time(NULL);
1444 }
1445
1446 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
1447 }
1448 /* }}} */
1449
1450 /* {{{ proto bool http_match_etag(string etag)
1451 *
1452 * This matches the given ETag against the clients
1453 * "If-Match" resp. "If-None-Match" HTTP headers.
1454 *
1455 */
1456 PHP_FUNCTION(http_match_etag)
1457 {
1458 int etag_len;
1459 char *etag;
1460
1461 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len) != SUCCESS) {
1462 RETURN_FALSE;
1463 }
1464
1465 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag) || http_etag_match("HTTP_IF_MATCH", etag));
1466 }
1467 /* }}} */
1468
1469 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
1470 *
1471 * If timestamp_or_exires is greater than 0, it is handled as timestamp
1472 * and will be sent as date of last modification. If it is 0 or omitted,
1473 * the current time will be sent as Last-Modified date. If it's negative,
1474 * it is handled as expiration time in seconds, which means that if the
1475 * requested last modification date is not between the calculated timespan,
1476 * the Last-Modified header is updated and the actual body will be sent.
1477 *
1478 */
1479 PHP_FUNCTION(http_cache_last_modified)
1480 {
1481 long last_modified = 0, send_modified = 0, t;
1482 zval *zlm;
1483
1484 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
1485 RETURN_FALSE;
1486 }
1487
1488 t = (long) time(NULL);
1489
1490 /* 0 or omitted */
1491 if (!last_modified) {
1492 /* does the client have? (att: caching "forever") */
1493 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
1494 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
1495 /* send current time */
1496 } else {
1497 send_modified = t;
1498 }
1499 /* negative value is supposed to be expiration time */
1500 } else if (last_modified < 0) {
1501 last_modified += t;
1502 send_modified = t;
1503 /* send supplied time explicitly */
1504 } else {
1505 send_modified = last_modified;
1506 }
1507
1508 RETURN_SUCCESS(http_cache_last_modified(last_modified, send_modified, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
1509 }
1510 /* }}} */
1511
1512 /* {{{ proto bool http_cache_etag([string etag])
1513 *
1514 * This function attempts to cache the HTTP body based on an ETag,
1515 * either supplied or generated through calculation of the MD5
1516 * checksum of the output (uses output buffering).
1517 *
1518 * If clients "If-None-Match" header matches the supplied/calculated
1519 * ETag, the body is considered cached on the clients side and
1520 * a "304 Not Modified" status code is issued.
1521 *
1522 */
1523 PHP_FUNCTION(http_cache_etag)
1524 {
1525 char *etag;
1526 int etag_len = 0;
1527
1528 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
1529 RETURN_FALSE;
1530 }
1531
1532 RETURN_SUCCESS(http_cache_etag(etag, etag_len, HTTP_DEFAULT_CACHECONTROL, sizeof(HTTP_DEFAULT_CACHECONTROL) - 1));
1533 }
1534 /* }}} */
1535
1536 /* {{{ proto string ob_httpetaghandler(string data, int mode)
1537 *
1538 * For use with ob_start().
1539 * Note that this has to be started as first output buffer.
1540 * WARNING: Don't use with http_send_*().
1541 */
1542 PHP_FUNCTION(ob_httpetaghandler)
1543 {
1544 char *data;
1545 int data_len;
1546 long mode;
1547
1548 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &data, &data_len, &mode)) {
1549 RETURN_FALSE;
1550 }
1551
1552 if (mode & PHP_OUTPUT_HANDLER_START) {
1553 if (HTTP_G(etag_started)) {
1554 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler can only be used once");
1555 RETURN_STRINGL(data, data_len, 1);
1556 }
1557 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL);
1558 HTTP_G(etag_started) = 1;
1559 }
1560
1561 if (OG(ob_nesting_level) > 1) {
1562 php_error_docref(NULL TSRMLS_CC, E_WARNING, "ob_httpetaghandler must be started prior to other output buffers");
1563 RETURN_STRINGL(data, data_len, 1);
1564 }
1565
1566 Z_TYPE_P(return_value) = IS_STRING;
1567 http_ob_etaghandler(data, data_len, &Z_STRVAL_P(return_value), &Z_STRLEN_P(return_value), mode);
1568 }
1569 /* }}} */
1570
1571 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
1572 *
1573 * Redirect to a given url.
1574 * The supplied url will be expanded with http_absolute_uri(), the params array will
1575 * be treated with http_build_query() and the session identification will be appended
1576 * if session is true.
1577 *
1578 * Depending on permanent the redirection will be issued with a permanent
1579 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
1580 * status code.
1581 *
1582 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
1583 * if the client doesn't redirect immediatly.
1584 */
1585 PHP_FUNCTION(http_redirect)
1586 {
1587 int url_len;
1588 zend_bool session = 0, permanent = 0;
1589 zval *params = NULL;
1590 smart_str qstr = {0};
1591 char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34];
1592
1593 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
1594 RETURN_FALSE;
1595 }
1596
1597 /* append session info */
1598 if (session && (PS(session_status) == php_session_active)) {
1599 if (!params) {
1600 MAKE_STD_ZVAL(params);
1601 array_init(params);
1602 }
1603 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
1604 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
1605 }
1606 }
1607
1608 /* treat params array with http_build_query() */
1609 if (params) {
1610 if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
1611 if (qstr.c) {
1612 efree(qstr.c);
1613 }
1614 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters");
1615 RETURN_FALSE;
1616 }
1617 smart_str_0(&qstr);
1618 }
1619
1620 URI = http_absolute_uri(url, NULL);
1621 if (qstr.c) {
1622 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s?%s", URI, qstr.c);
1623 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, qstr.c, URI, qstr.c);
1624 efree(qstr.c);
1625 } else {
1626 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI);
1627 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
1628 }
1629 efree(URI);
1630
1631 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
1632 php_body_write(RED, strlen(RED) TSRMLS_CC);
1633 RETURN_TRUE;
1634 }
1635 RETURN_FALSE;
1636 }
1637 /* }}} */
1638
1639 /* {{{ proto bool http_send_data(string data)
1640 *
1641 * Sends raw data with support for (multiple) range requests.
1642 *
1643 */
1644 PHP_FUNCTION(http_send_data)
1645 {
1646 zval *zdata;
1647
1648 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
1649 RETURN_FALSE;
1650 }
1651
1652 convert_to_string_ex(&zdata);
1653 http_send_header("Accept-Ranges: bytes");
1654 RETURN_SUCCESS(http_send_data(zdata));
1655 }
1656 /* }}} */
1657
1658 /* {{{ proto bool http_send_file(string file)
1659 *
1660 * Sends a file with support for (multiple) range requests.
1661 *
1662 */
1663 PHP_FUNCTION(http_send_file)
1664 {
1665 zval *zfile;
1666
1667 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfile) != SUCCESS) {
1668 RETURN_FALSE;
1669 }
1670
1671 convert_to_string_ex(&zfile);
1672 http_send_header("Accept-Ranges: bytes");
1673 RETURN_SUCCESS(http_send_file(zfile));
1674 }
1675 /* }}} */
1676
1677 /* {{{ proto bool http_send_stream(resource stream)
1678 *
1679 * Sends an already opened stream with support for (multiple) range requests.
1680 *
1681 */
1682 PHP_FUNCTION(http_send_stream)
1683 {
1684 zval *zstream;
1685 php_stream *file;
1686
1687 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
1688 RETURN_FALSE;
1689 }
1690
1691 php_stream_from_zval(file, &zstream);
1692 http_send_header("Accept-Ranges: bytes");
1693 RETURN_SUCCESS(http_send_stream(file));
1694 }
1695 /* }}} */
1696
1697 /* {{{ proto string http_chunked_decode(string encoded)
1698 *
1699 * This function decodes a string that was HTTP-chunked encoded.
1700 * Returns false on failure.
1701 */
1702 PHP_FUNCTION(http_chunked_decode)
1703 {
1704 char *encoded = NULL, *decoded = NULL;
1705 int encoded_len = 0, decoded_len = 0;
1706
1707 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
1708 RETURN_FALSE;
1709 }
1710
1711 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
1712 RETURN_STRINGL(decoded, decoded_len, 0);
1713 } else {
1714 RETURN_FALSE;
1715 }
1716 }
1717 /* }}} */
1718
1719 /* {{{ proto array http_split_response(string http_response)
1720 *
1721 * This function splits an HTTP response into an array with headers and the
1722 * content body. The returned array may look simliar to the following example:
1723 *
1724 * <pre>
1725 * <?php
1726 * array(
1727 * 0 => array(
1728 * 'Status' => '200 Ok',
1729 * 'Content-Type' => 'text/plain',
1730 * 'Content-Language' => 'en-US'
1731 * ),
1732 * 1 => "Hello World!"
1733 * );
1734 * ?>
1735 * </pre>
1736 */
1737 PHP_FUNCTION(http_split_response)
1738 {
1739 zval *zresponse, *zbody, *zheaders;
1740
1741 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
1742 RETURN_FALSE;
1743 }
1744
1745 convert_to_string_ex(&zresponse);
1746
1747 MAKE_STD_ZVAL(zbody);
1748 MAKE_STD_ZVAL(zheaders);
1749 array_init(zheaders);
1750
1751 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
1752 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
1753 RETURN_FALSE;
1754 }
1755
1756 array_init(return_value);
1757 add_index_zval(return_value, 0, zheaders);
1758 add_index_zval(return_value, 1, zbody);
1759 }
1760 /* }}} */
1761
1762 /* {{{ proto array http_parse_headers(string header)
1763 *
1764 */
1765 PHP_FUNCTION(http_parse_headers)
1766 {
1767 char *header, *rnrn;
1768 int header_len;
1769
1770 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
1771 RETURN_FALSE;
1772 }
1773
1774 array_init(return_value);
1775
1776 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
1777 header_len = rnrn - header + 2;
1778 }
1779 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
1780 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
1781 zval_dtor(return_value);
1782 RETURN_FALSE;
1783 }
1784 }
1785 /* }}}*/
1786
1787 /* {{{ proto array http_get_request_headers(void)
1788 *
1789 */
1790 PHP_FUNCTION(http_get_request_headers)
1791 {
1792 if (ZEND_NUM_ARGS()) {
1793 WRONG_PARAM_COUNT;
1794 }
1795
1796 array_init(return_value);
1797 http_get_request_headers(return_value);
1798 }
1799 /* }}} */
1800
1801 /* {{{ HAVE_CURL */
1802 #ifdef HTTP_HAVE_CURL
1803
1804 /* {{{ proto string http_get(string url[, array options[, array &info]])
1805 *
1806 * Performs an HTTP GET request on the supplied url.
1807 *
1808 * The second parameter is expected to be an associative
1809 * array where the following keys will be recognized:
1810 * <pre>
1811 * - redirect: int, whether and how many redirects to follow
1812 * - unrestrictedauth: bool, whether to continue sending credentials on
1813 * redirects to a different host
1814 * - proxyhost: string, proxy host in "host[:port]" format
1815 * - proxyport: int, use another proxy port as specified in proxyhost
1816 * - proxyauth: string, proxy credentials in "user:pass" format
1817 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
1818 * - httpauth: string, http credentials in "user:pass" format
1819 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
1820 * - compress: bool, whether to allow gzip/deflate content encoding
1821 * (defaults to true)
1822 * - port: int, use another port as specified in the url
1823 * - referer: string, the referer to sends
1824 * - useragent: string, the user agent to send
1825 * (defaults to PECL::HTTP/version (PHP/version)))
1826 * - headers: array, list of custom headers as associative array
1827 * like array("header" => "value")
1828 * - cookies: array, list of cookies as associative array
1829 * like array("cookie" => "value")
1830 * - cookiestore: string, path to a file where cookies are/will be stored
1831 * </pre>
1832 *
1833 * The optional third parameter will be filled with some additional information
1834 * in form af an associative array, if supplied, like the following example:
1835 * <pre>
1836 * <?php
1837 * array (
1838 * 'effective_url' => 'http://localhost',
1839 * 'response_code' => 403,
1840 * 'total_time' => 0.017,
1841 * 'namelookup_time' => 0.013,
1842 * 'connect_time' => 0.014,
1843 * 'pretransfer_time' => 0.014,
1844 * 'size_upload' => 0,
1845 * 'size_download' => 202,
1846 * 'speed_download' => 11882,
1847 * 'speed_upload' => 0,
1848 * 'header_size' => 145,
1849 * 'request_size' => 62,
1850 * 'ssl_verifyresult' => 0,
1851 * 'filetime' => -1,
1852 * 'content_length_download' => 202,
1853 * 'content_length_upload' => 0,
1854 * 'starttransfer_time' => 0.017,
1855 * 'content_type' => 'text/html; charset=iso-8859-1',
1856 * 'redirect_time' => 0,
1857 * 'redirect_count' => 0,
1858 * 'private' => '',
1859 * 'http_connectcode' => 0,
1860 * 'httpauth_avail' => 0,
1861 * 'proxyauth_avail' => 0,
1862 * )
1863 * ?>
1864 * </pre>
1865 */
1866 PHP_FUNCTION(http_get)
1867 {
1868 char *URL, *data = NULL;
1869 size_t data_len = 0;
1870 int URL_len;
1871 zval *options = NULL, *info = NULL;
1872
1873 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1874 RETURN_FALSE;
1875 }
1876
1877 if (info) {
1878 zval_dtor(info);
1879 array_init(info);
1880 }
1881
1882 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1883 RETURN_STRINGL(data, data_len, 0);
1884 } else {
1885 RETURN_FALSE;
1886 }
1887 }
1888 /* }}} */
1889
1890 /* {{{ proto string http_head(string url[, array options[, array &info]])
1891 *
1892 * Performs an HTTP HEAD request on the suppied url.
1893 * Returns the HTTP response as string.
1894 * See http_get() for a full list of available options.
1895 */
1896 PHP_FUNCTION(http_head)
1897 {
1898 char *URL, *data = NULL;
1899 size_t data_len = 0;
1900 int URL_len;
1901 zval *options = NULL, *info = NULL;
1902
1903 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
1904 RETURN_FALSE;
1905 }
1906
1907 if (info) {
1908 zval_dtor(info);
1909 array_init(info);
1910 }
1911
1912 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1913 RETURN_STRINGL(data, data_len, 0);
1914 } else {
1915 RETURN_FALSE;
1916 }
1917 }
1918 /* }}} */
1919
1920 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
1921 *
1922 * Performs an HTTP POST request, posting data.
1923 * Returns the HTTP response as string.
1924 * See http_get() for a full list of available options.
1925 */
1926 PHP_FUNCTION(http_post_data)
1927 {
1928 char *URL, *postdata, *data = NULL;
1929 size_t data_len = 0;
1930 int postdata_len, URL_len;
1931 zval *options = NULL, *info = NULL;
1932
1933 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
1934 RETURN_FALSE;
1935 }
1936
1937 if (info) {
1938 zval_dtor(info);
1939 array_init(info);
1940 }
1941
1942 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1943 RETURN_STRINGL(data, data_len, 0);
1944 } else {
1945 RETURN_FALSE;
1946 }
1947 }
1948 /* }}} */
1949
1950 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
1951 *
1952 * Performs an HTTP POST request, posting www-form-urlencoded array data.
1953 * Returns the HTTP response as string.
1954 * See http_get() for a full list of available options.
1955 */
1956 PHP_FUNCTION(http_post_array)
1957 {
1958 char *URL, *data = NULL;
1959 size_t data_len = 0;
1960 int URL_len;
1961 zval *options = NULL, *info = NULL, *postdata;
1962
1963 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
1964 RETURN_FALSE;
1965 }
1966
1967 if (info) {
1968 zval_dtor(info);
1969 array_init(info);
1970 }
1971
1972 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
1973 RETURN_STRINGL(data, data_len, 0);
1974 } else {
1975 RETURN_FALSE;
1976 }
1977 }
1978 /* }}} */
1979
1980 #endif
1981 /* }}} HAVE_CURL */
1982
1983
1984 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
1985 *
1986 * Example:
1987 * <pre>
1988 * <?php
1989 * if (!http_auth_basic('mike', 's3c|r3t')) {
1990 * die('<h1>Authorization failed!</h1>');
1991 * }
1992 * ?>
1993 * </pre>
1994 */
1995 PHP_FUNCTION(http_auth_basic)
1996 {
1997 char *realm = NULL, *user, *pass, *suser, *spass;
1998 int r_len, u_len, p_len;
1999
2000 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
2001 RETURN_FALSE;
2002 }
2003
2004 if (!realm) {
2005 realm = "Restricted";
2006 }
2007
2008 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
2009 http_auth_header("Basic", realm);
2010 RETURN_FALSE;
2011 }
2012
2013 if (strcasecmp(suser, user)) {
2014 http_auth_header("Basic", realm);
2015 RETURN_FALSE;
2016 }
2017
2018 if (strcmp(spass, pass)) {
2019 http_auth_header("Basic", realm);
2020 RETURN_FALSE;
2021 }
2022
2023 RETURN_TRUE;
2024 }
2025 /* }}} */
2026
2027 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
2028 *
2029 * Example:
2030 * <pre>
2031 * <?php
2032 * function auth_cb($user, $pass)
2033 * {
2034 * global $db;
2035 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
2036 * if (strlen($realpass = $db->getOne($query)) {
2037 * return $pass === $realpass;
2038 * }
2039 * return false;
2040 * }
2041 *
2042 * if (!http_auth_basic_cb('auth_cb')) {
2043 * die('<h1>Authorization failed</h1>');
2044 * }
2045 * ?>
2046 * </pre>
2047 */
2048 PHP_FUNCTION(http_auth_basic_cb)
2049 {
2050 zval *cb;
2051 char *realm = NULL, *user, *pass;
2052 int r_len;
2053
2054 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
2055 RETURN_FALSE;
2056 }
2057
2058 if (!realm) {
2059 realm = "Restricted";
2060 }
2061
2062 if (SUCCESS != http_auth_credentials(&user, &pass)) {
2063 http_auth_header("Basic", realm);
2064 RETURN_FALSE;
2065 }
2066 {
2067 zval *zparams[2] = {NULL, NULL}, retval;
2068 int result = 0;
2069
2070 MAKE_STD_ZVAL(zparams[0]);
2071 MAKE_STD_ZVAL(zparams[1]);
2072 ZVAL_STRING(zparams[0], user, 0);
2073 ZVAL_STRING(zparams[1], pass, 0);
2074
2075 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
2076 &retval, 2, zparams TSRMLS_CC)) {
2077 result = Z_LVAL(retval);
2078 }
2079
2080 efree(user);
2081 efree(pass);
2082 efree(zparams[0]);
2083 efree(zparams[1]);
2084
2085 if (!result) {
2086 http_auth_header("Basic", realm);
2087 }
2088
2089 RETURN_BOOL(result);
2090 }
2091 }
2092 /* }}}*/
2093
2094
2095 /* {{{ php_http_init_globals(zend_http_globals *) */
2096 static void php_http_init_globals(zend_http_globals *http_globals)
2097 {
2098 http_globals->etag_started = 0;
2099 http_globals->ctype = NULL;
2100 http_globals->etag = NULL;
2101 http_globals->lmod = 0;
2102 #ifdef HTTP_HAVE_CURL
2103 http_globals->curlbuf.body.data = NULL;
2104 http_globals->curlbuf.body.used = 0;
2105 http_globals->curlbuf.body.free = 0;
2106 http_globals->curlbuf.hdrs.data = NULL;
2107 http_globals->curlbuf.hdrs.used = 0;
2108 http_globals->curlbuf.hdrs.free = 0;
2109 #endif
2110 http_globals->allowed_methods = NULL;
2111 }
2112 /* }}} */
2113
2114 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
2115 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
2116 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
2117 {
2118 if (length && SG(request_info).request_method && (!strstr(methods, SG(request_info).request_method))) {
2119 char *allow_header = emalloc(length + sizeof("Allow: "));
2120 sprintf(allow_header, "Allow: %s", methods);
2121 http_send_header(allow_header);
2122 efree(allow_header);
2123 http_send_status(405);
2124 zend_bailout();
2125 }
2126 }
2127 /* }}} */
2128
2129 /* {{{ PHP_INI */
2130 PHP_INI_MH(update_allowed_methods)
2131 {
2132 http_check_allowed_methods(new_value, new_value_length);
2133 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
2134 }
2135
2136 PHP_INI_BEGIN()
2137 STD_PHP_INI_ENTRY("http.allowed_methods", "OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT", PHP_INI_ALL, update_allowed_methods, allowed_methods, zend_http_globals, http_globals)
2138 PHP_INI_END()
2139 /* }}} */
2140
2141 /* {{{ PHP_MINIT_FUNCTION */
2142 PHP_MINIT_FUNCTION(http)
2143 {
2144 ZEND_INIT_MODULE_GLOBALS(http, php_http_init_globals, NULL);
2145 REGISTER_INI_ENTRIES();
2146
2147 #ifdef HTTP_HAVE_CURL
2148 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC, CONST_CS | CONST_PERSISTENT);
2149 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS | CONST_PERSISTENT);
2150 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM, CONST_CS | CONST_PERSISTENT);
2151 #endif
2152
2153 #ifdef ZEND_ENGINE_2
2154 HTTP_REGISTER_CLASS(HTTPi, httpi, NULL, ZEND_ACC_FINAL_CLASS);
2155 HTTP_REGISTER_CLASS_EX(HTTPi_Response, httpi_response, NULL, 0);
2156 # ifdef HTTP_HAVE_CURL
2157 HTTP_REGISTER_CLASS_EX(HTTPi_Request, httpi_request, NULL, 0);
2158 REGISTER_LONG_CONSTANT("HTTP_GET", HTTP_GET, CONST_CS | CONST_PERSISTENT);
2159 REGISTER_LONG_CONSTANT("HTTP_HEAD", HTTP_HEAD, CONST_CS | CONST_PERSISTENT);
2160 REGISTER_LONG_CONSTANT("HTTP_POST", HTTP_POST, CONST_CS | CONST_PERSISTENT);
2161 # endif /* HTTP_HAVE_CURL */
2162 #endif /* ZEND_ENGINE_2 */
2163 return SUCCESS;
2164 }
2165 /* }}} */
2166
2167 /* {{{ PHP_MSHUTDOWN_FUNCTION */
2168 PHP_MSHUTDOWN_FUNCTION(http)
2169 {
2170 UNREGISTER_INI_ENTRIES();
2171 return SUCCESS;
2172 }
2173 /* }}} */
2174
2175 /* {{{ PHP_RINIT_FUNCTION */
2176 PHP_RINIT_FUNCTION(http)
2177 {
2178 char *allowed_methods = INI_STR("http.allowed_methods");
2179 http_check_allowed_methods(allowed_methods, strlen(allowed_methods));
2180 return SUCCESS;
2181 }
2182 /* }}} */
2183
2184 /* {{{ PHP_RSHUTDOWN_FUNCTION */
2185 PHP_RSHUTDOWN_FUNCTION(http)
2186 {
2187 HTTP_G(etag_started) = 0;
2188 HTTP_G(lmod) = 0;
2189
2190 if (HTTP_G(etag)) {
2191 efree(HTTP_G(etag));
2192 HTTP_G(etag) = NULL;
2193 }
2194
2195 if (HTTP_G(ctype)) {
2196 efree(HTTP_G(ctype));
2197 HTTP_G(ctype) = NULL;
2198 }
2199 #ifdef HTTP_HAVE_CURL
2200 if (HTTP_G(curlbuf).body.data) {
2201 efree(HTTP_G(curlbuf).body.data);
2202 HTTP_G(curlbuf).body.data = NULL;
2203 }
2204 if (HTTP_G(curlbuf).hdrs.data) {
2205 efree(HTTP_G(curlbuf).hdrs.data);
2206 HTTP_G(curlbuf).hdrs.data = NULL;
2207 }
2208 #endif
2209 return SUCCESS;
2210 }
2211 /* }}} */
2212
2213 /* {{{ PHP_MINFO_FUNCTION */
2214 PHP_MINFO_FUNCTION(http)
2215 {
2216 php_info_print_table_start();
2217 php_info_print_table_header(2, "Extended HTTP support", "enabled");
2218 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION);
2219 php_info_print_table_row(2, "cURL convenience functions:",
2220 #ifdef HTTP_HAVE_CURL
2221 "enabled"
2222 #else
2223 "disabled"
2224 #endif
2225 );
2226 php_info_print_table_end();
2227
2228 DISPLAY_INI_ENTRIES();
2229 }
2230 /* }}} */
2231
2232 /*
2233 * Local variables:
2234 * tab-width: 4
2235 * c-basic-offset: 4
2236 * End:
2237 * vim600: noet sw=4 ts=4 fdm=marker
2238 * vim<600: noet sw=4 ts=4
2239 */
2240