* keep winsock crap outta door
[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
80 /* {{{ http_functions[] */
81 function_entry http_functions[] = {
82 PHP_FE(http_date, NULL)
83 PHP_FE(http_absolute_uri, NULL)
84 PHP_FE(http_negotiate_language, NULL)
85 PHP_FE(http_negotiate_charset, NULL)
86 PHP_FE(http_redirect, NULL)
87 PHP_FE(http_send_status, NULL)
88 PHP_FE(http_send_last_modified, NULL)
89 PHP_FE(http_match_modified, NULL)
90 PHP_FE(http_match_etag, NULL)
91 PHP_FE(http_cache_last_modified, NULL)
92 PHP_FE(http_cache_etag, NULL)
93 PHP_FE(http_content_type, NULL)
94 PHP_FE(http_content_disposition, NULL)
95 PHP_FE(http_send_data, NULL)
96 PHP_FE(http_send_file, NULL)
97 PHP_FE(http_send_stream, NULL)
98 PHP_FE(http_chunked_decode, NULL)
99 PHP_FE(http_split_response, NULL)
100 PHP_FE(http_parse_headers, NULL)
101 PHP_FE(http_get_request_headers, NULL)
102 #ifdef HTTP_HAVE_CURL
103 PHP_FE(http_get, http_request_info_ref_3)
104 PHP_FE(http_head, http_request_info_ref_3)
105 PHP_FE(http_post_data, http_request_info_ref_4)
106 PHP_FE(http_post_array, http_request_info_ref_4)
107 #endif
108 PHP_FE(http_auth_basic, NULL)
109 PHP_FE(http_auth_basic_cb, NULL)
110 #ifndef ZEND_ENGINE_2
111 PHP_FE(http_build_query, NULL)
112 #endif
113 {NULL, NULL, NULL}
114 };
115 /* }}} */
116
117 /* {{{ http_module_entry */
118 zend_module_entry http_module_entry = {
119 #if ZEND_MODULE_API_NO >= 20010901
120 STANDARD_MODULE_HEADER,
121 #endif
122 "http",
123 http_functions,
124 PHP_MINIT(http),
125 NULL,
126 NULL,
127 PHP_RSHUTDOWN(http),
128 PHP_MINFO(http),
129 #if ZEND_MODULE_API_NO >= 20010901
130 PHP_EXT_HTTP_VERSION,
131 #endif
132 STANDARD_MODULE_PROPERTIES
133 };
134 /* }}} */
135
136 #define RETURN_SUCCESS(v) RETURN_BOOL(SUCCESS == (v))
137 #define HASH_ORNULL(z) ((z) ? Z_ARRVAL_P(z) : NULL)
138
139 /* {{{ proto string http_date([int timestamp])
140 *
141 * This function returns a valid HTTP date regarding RFC 822/1123
142 * looking like: "Wed, 22 Dec 2004 11:34:47 GMT"
143 *
144 */
145 PHP_FUNCTION(http_date)
146 {
147 long t = -1;
148
149 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
150 RETURN_FALSE;
151 }
152
153 if (t == -1) {
154 t = (long) time(NULL);
155 }
156
157 RETURN_STRING(http_date(t), 0);
158 }
159 /* }}} */
160
161 /* {{{ proto string http_absolute_uri(string url[, string proto])
162 *
163 * This function returns an absolute URI constructed from url.
164 * If the url is already abolute but a different proto was supplied,
165 * only the proto part of the URI will be updated. If url has no
166 * path specified, the path of the current REQUEST_URI will be taken.
167 * The host will be taken either from the Host HTTP header of the client
168 * the SERVER_NAME or just localhost if prior are not available.
169 *
170 * Some examples:
171 * <pre>
172 * url = "page.php" => http://www.example.com/current/path/page.php
173 * url = "/page.php" => http://www.example.com/page.php
174 * url = "/page.php", proto = "https" => https://www.example.com/page.php
175 * </pre>
176 *
177 */
178 PHP_FUNCTION(http_absolute_uri)
179 {
180 char *url = NULL, *proto = NULL;
181 int url_len = 0, proto_len = 0;
182
183 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &url, &url_len, &proto, &proto_len) != SUCCESS) {
184 RETURN_FALSE;
185 }
186
187 RETURN_STRING(http_absolute_uri(url, proto), 0);
188 }
189 /* }}} */
190
191 /* {{{ proto string http_negotiate_language(array supported[, string default = 'en-US'])
192 *
193 * This function negotiates the clients preferred language based on its
194 * Accept-Language HTTP header. It returns the negotiated language or
195 * the default language if none match.
196 *
197 * The qualifier is recognized and languages without qualifier are rated highest.
198 *
199 * The supported parameter is expected to be an array having
200 * the supported languages as array values.
201 *
202 * Example:
203 * <pre>
204 * <?php
205 * $langs = array(
206 * 'en-US',// default
207 * 'fr',
208 * 'fr-FR',
209 * 'de',
210 * 'de-DE',
211 * 'de-AT',
212 * 'de-CH',
213 * );
214 * include './langs/'. http_negotiate_language($langs) .'.php';
215 * ?>
216 * </pre>
217 *
218 */
219 PHP_FUNCTION(http_negotiate_language)
220 {
221 zval *supported;
222 char *def = NULL;
223 int def_len = 0;
224
225 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
226 RETURN_FALSE;
227 }
228
229 if (!def) {
230 def = "en-US";
231 }
232
233 RETURN_STRING(http_negotiate_language(supported, def), 0);
234 }
235 /* }}} */
236
237 /* {{{ proto string http_negotiate_charset(array supported[, string default = 'iso-8859-1'])
238 *
239 * This function negotiates the clients preferred charset based on its
240 * Accept-Charset HTTP header. It returns the negotiated charset or
241 * the default charset if none match.
242 *
243 * The qualifier is recognized and charset without qualifier are rated highest.
244 *
245 * The supported parameter is expected to be an array having
246 * the supported charsets as array values.
247 *
248 * Example:
249 * <pre>
250 * <?php
251 * $charsets = array(
252 * 'iso-8859-1', // default
253 * 'iso-8859-2',
254 * 'iso-8859-15',
255 * 'utf-8'
256 * );
257 * $pref = http_negotiate_charset($charsets);
258 * if (!strcmp($pref, 'iso-8859-1')) {
259 * iconv_set_encoding('internal_encoding', 'iso-8859-1');
260 * iconv_set_encoding('output_encoding', $pref);
261 * ob_start('ob_iconv_handler');
262 * }
263 * ?>
264 * </pre>
265 */
266 PHP_FUNCTION(http_negotiate_charset)
267 {
268 zval *supported;
269 char *def = NULL;
270 int def_len = 0;
271
272 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|s", &supported, &def, &def_len) != SUCCESS) {
273 RETURN_FALSE;
274 }
275
276 if (!def) {
277 def = "iso-8859-1";
278 }
279
280 RETURN_STRING(http_negotiate_charset(supported, def), 0);
281 }
282 /* }}} */
283
284 /* {{{ proto bool http_send_status(int status)
285 *
286 * Send HTTP status code.
287 *
288 */
289 PHP_FUNCTION(http_send_status)
290 {
291 int status = 0;
292
293 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &status) != SUCCESS) {
294 RETURN_FALSE;
295 }
296 if (status < 100 || status > 510) {
297 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid HTTP status code (100-510): %d", status);
298 RETURN_FALSE;
299 }
300
301 RETURN_SUCCESS(http_send_status(status));
302 }
303 /* }}} */
304
305 /* {{{ proto bool http_send_last_modified([int timestamp])
306 *
307 * This converts the given timestamp to a valid HTTP date and
308 * sends it as "Last-Modified" HTTP header. If timestamp is
309 * omitted, current time is sent.
310 *
311 */
312 PHP_FUNCTION(http_send_last_modified)
313 {
314 long t = -1;
315
316 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
317 RETURN_FALSE;
318 }
319
320 if (t == -1) {
321 t = (long) time(NULL);
322 }
323
324 RETURN_SUCCESS(http_send_last_modified(t));
325 }
326 /* }}} */
327
328 /* {{{ proto bool http_match_modified([int timestamp])
329 *
330 * Matches the given timestamp against the clients "If-Modified-Since" resp.
331 * "If-Unmodified-Since" HTTP headers.
332 *
333 */
334 PHP_FUNCTION(http_match_modified)
335 {
336 long t = -1;
337
338 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &t) != SUCCESS) {
339 RETURN_FALSE;
340 }
341
342 // current time if not supplied (senseless though)
343 if (t == -1) {
344 t = (long) time(NULL);
345 }
346
347 RETURN_BOOL(http_modified_match("HTTP_IF_MODIFIED_SINCE", t) || http_modified_match("HTTP_IF_UNMODIFIED_SINCE", t));
348 }
349 /* }}} */
350
351 /* {{{ proto bool http_match_etag(string etag)
352 *
353 * This matches the given ETag against the clients
354 * "If-Match" resp. "If-None-Match" HTTP headers.
355 *
356 */
357 PHP_FUNCTION(http_match_etag)
358 {
359 int etag_len;
360 char *etag;
361
362 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &etag, &etag_len) != SUCCESS) {
363 RETURN_FALSE;
364 }
365
366 RETURN_BOOL(http_etag_match("HTTP_IF_NONE_MATCH", etag) || http_etag_match("HTTP_IF_MATCH", etag));
367 }
368 /* }}} */
369
370 /* {{{ proto bool http_cache_last_modified([int timestamp_or_expires]])
371 *
372 * If timestamp_or_exires is greater than 0, it is handled as timestamp
373 * and will be sent as date of last modification. If it is 0 or omitted,
374 * the current time will be sent as Last-Modified date. If it's negative,
375 * it is handled as expiration time in seconds, which means that if the
376 * requested last modification date is not between the calculated timespan,
377 * the Last-Modified header is updated and the actual body will be sent.
378 *
379 */
380 PHP_FUNCTION(http_cache_last_modified)
381 {
382 long last_modified = 0, send_modified = 0, t;
383 zval *zlm;
384
385 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &last_modified) != SUCCESS) {
386 RETURN_FALSE;
387 }
388
389 t = (long) time(NULL);
390
391 /* 0 or omitted */
392 if (!last_modified) {
393 /* does the client have? (att: caching "forever") */
394 if (zlm = http_get_server_var("HTTP_IF_MODIFIED_SINCE")) {
395 last_modified = send_modified = http_parse_date(Z_STRVAL_P(zlm));
396 /* send current time */
397 } else {
398 send_modified = t;
399 }
400 /* negative value is supposed to be expiration time */
401 } else if (last_modified < 0) {
402 last_modified += t;
403 send_modified = t;
404 /* send supplied time explicitly */
405 } else {
406 send_modified = last_modified;
407 }
408
409 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
410
411 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
412 if (SUCCESS == http_send_status(304)) {
413 zend_bailout();
414 } else {
415 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
416 RETURN_FALSE;
417 }
418 }
419 RETURN_SUCCESS(http_send_last_modified(send_modified));
420 }
421 /* }}} */
422
423 /* {{{ proto bool http_cache_etag([string etag])
424 *
425 * This function attempts to cache the HTTP body based on an ETag,
426 * either supplied or generated through calculation of the MD5
427 * checksum of the output (uses output buffering).
428 *
429 * If clients "If-None-Match" header matches the supplied/calculated
430 * ETag, the body is considered cached on the clients side and
431 * a "304 Not Modified" status code is issued.
432 *
433 */
434 PHP_FUNCTION(http_cache_etag)
435 {
436 char *etag;
437 int etag_len = 0;
438
439 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &etag, &etag_len) != SUCCESS) {
440 RETURN_FALSE;
441 }
442
443 php_end_ob_buffers(0 TSRMLS_CC);
444 http_send_header("Cache-Control: private, must-revalidate, max-age=0");
445
446 if (etag_len) {
447 RETURN_SUCCESS(http_send_etag(etag, etag_len));
448 }
449
450 /* if no etag is given and we didn't already
451 * start ob_etaghandler -- start it
452 */
453 if (!HTTP_G(etag_started)) {
454 php_ob_set_internal_handler(_http_ob_etaghandler, (uint) 4096, "etag output handler", 0 TSRMLS_CC);
455 HTTP_G(etag_started) = 1;
456 RETURN_BOOL(php_start_ob_buffer_named("etag output handler", (uint) 4096, 0 TSRMLS_CC));
457 }
458
459 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
460 if (SUCCESS == http_send_status(304)) {
461 zend_bailout();
462 } else {
463 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
464 RETURN_FALSE;
465 }
466 }
467
468 }
469 /* }}} */
470
471 /* {{{ proto void http_redirect([string url[, array params[, bool session,[ bool permanent]]]])
472 *
473 * Redirect to a given url.
474 * The supplied url will be expanded with http_absolute_uri(), the params array will
475 * be treated with http_build_query() and the session identification will be appended
476 * if session is true.
477 *
478 * Depending on permanent the redirection will be issued with a permanent
479 * ("301 Moved Permanently") or a temporary ("302 Found") redirection
480 * status code.
481 *
482 * To be RFC compliant, "Redirecting to <a>URI</a>." will be displayed,
483 * if the client doesn't redirect immediatly.
484 */
485 PHP_FUNCTION(http_redirect)
486 {
487 int url_len;
488 zend_bool session = 0, permanent = 0;
489 zval *params = NULL;
490 smart_str qstr = {0};
491 char *url, *URI, LOC[HTTP_URI_MAXLEN + 9], RED[HTTP_URI_MAXLEN * 2 + 34];
492
493 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sa!/bb", &url, &url_len, &params, &session, &permanent) != SUCCESS) {
494 RETURN_FALSE;
495 }
496
497 /* append session info */
498 if (session && (PS(session_status) == php_session_active)) {
499 if (!params) {
500 MAKE_STD_ZVAL(params);
501 array_init(params);
502 }
503 if (add_assoc_string(params, PS(session_name), PS(id), 1) != SUCCESS) {
504 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not append session information");
505 }
506 }
507
508 /* treat params array with http_build_query() */
509 if (params) {
510 if (php_url_encode_hash_ex(Z_ARRVAL_P(params), &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
511 if (qstr.c) {
512 efree(qstr.c);
513 }
514 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode query parameters");
515 RETURN_FALSE;
516 }
517 smart_str_0(&qstr);
518 }
519
520 URI = http_absolute_uri(url, NULL);
521 if (qstr.c) {
522 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s?%s", URI, qstr.c);
523 sprintf(RED, "Redirecting to <a href=\"%s?%s\">%s?%s</a>.\n", URI, qstr.c, URI, qstr.c);
524 efree(qstr.c);
525 } else {
526 snprintf(LOC, HTTP_URI_MAXLEN + strlen("Location: "), "Location: %s", URI);
527 sprintf(RED, "Redirecting to <a href=\"%s\">%s</a>.\n", URI, URI);
528 }
529 efree(URI);
530
531 if ((SUCCESS == http_send_header(LOC)) && (SUCCESS == http_send_status((permanent ? 301 : 302)))) {
532 php_body_write(RED, strlen(RED) TSRMLS_CC);
533 RETURN_TRUE;
534 }
535 RETURN_FALSE;
536 }
537 /* }}} */
538
539 /* {{{ proto bool http_send_data(string data)
540 *
541 * Sends raw data with support for (multiple) range requests.
542 *
543 */
544 PHP_FUNCTION(http_send_data)
545 {
546 zval *zdata;
547
548 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zdata) != SUCCESS) {
549 RETURN_FALSE;
550 }
551
552 convert_to_string_ex(&zdata);
553 http_send_header("Accept-Ranges: bytes");
554 RETURN_SUCCESS(http_send_data(zdata));
555 }
556 /* }}} */
557
558 /* {{{ proto bool http_send_file(string file)
559 *
560 * Sends a file with support for (multiple) range requests.
561 *
562 */
563 PHP_FUNCTION(http_send_file)
564 {
565 zval *zfile;
566
567 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zfile) != SUCCESS) {
568 RETURN_FALSE;
569 }
570
571 convert_to_string_ex(&zfile);
572 http_send_header("Accept-Ranges: bytes");
573 RETURN_SUCCESS(http_send_file(zfile));
574 }
575 /* }}} */
576
577 /* {{{ proto bool http_send_stream(resource stream)
578 *
579 * Sends an already opened stream with support for (multiple) range requests.
580 *
581 */
582 PHP_FUNCTION(http_send_stream)
583 {
584 zval *zstream;
585 php_stream *file;
586
587 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream) != SUCCESS) {
588 RETURN_FALSE;
589 }
590
591 php_stream_from_zval(file, &zstream);
592 http_send_header("Accept-Ranges: bytes");
593 RETURN_SUCCESS(http_send_stream(file));
594 }
595 /* }}} */
596
597 /* {{{ proto bool http_content_type([string content_type = 'application/x-octetstream'])
598 *
599 * Sets the content type.
600 *
601 */
602 PHP_FUNCTION(http_content_type)
603 {
604 char *ct, *content_type;
605 int ct_len = 0;
606
607 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ct, &ct_len) != SUCCESS) {
608 RETURN_FALSE;
609 }
610
611 if (!ct_len) {
612 RETURN_SUCCESS(http_send_header("Content-Type: application/x-octetstream"));
613 }
614
615 /* remember for multiple ranges */
616 if (HTTP_G(ctype)) {
617 efree(HTTP_G(ctype));
618 }
619 HTTP_G(ctype) = estrndup(ct, ct_len);
620
621 content_type = (char *) emalloc(strlen("Content-Type: ") + ct_len + 1);
622 sprintf(content_type, "Content-Type: %s", ct);
623
624 RETVAL_BOOL(SUCCESS == http_send_header(content_type));
625 efree(content_type);
626 }
627 /* }}} */
628
629 /* {{{ proto bool http_content_disposition(string filename[, bool inline = false])
630 *
631 * Set the Content Disposition. The Content-Disposition header is very useful
632 * if the data actually sent came from a file or something similar, that should
633 * be "saved" by the client/user (i.e. by browsers "Save as..." popup window).
634 *
635 */
636 PHP_FUNCTION(http_content_disposition)
637 {
638 char *filename, *header;
639 int f_len;
640 zend_bool send_inline = 0;
641
642 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &filename, &f_len, &send_inline) != SUCCESS) {
643 RETURN_FALSE;
644 }
645
646 if (send_inline) {
647 header = (char *) emalloc(strlen("Content-Disposition: inline; filename=\"\"") + f_len + 1);
648 sprintf(header, "Content-Disposition: inline; filename=\"%s\"", filename);
649 } else {
650 header = (char *) emalloc(strlen("Content-Disposition: attachment; filename=\"\"") + f_len + 1);
651 sprintf(header, "Content-Disposition: attachment; filename=\"%s\"", filename);
652 }
653
654 RETVAL_BOOL(SUCCESS == http_send_header(header));
655 efree(header);
656 }
657 /* }}} */
658
659 /* {{{ proto string http_chunked_decode(string encoded)
660 *
661 * This function decodes a string that was HTTP-chunked encoded.
662 * Returns false on failure.
663 */
664 PHP_FUNCTION(http_chunked_decode)
665 {
666 char *encoded = NULL, *decoded = NULL;
667 int encoded_len = 0, decoded_len = 0;
668
669 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &encoded, &encoded_len) != SUCCESS) {
670 RETURN_FALSE;
671 }
672
673 if (SUCCESS == http_chunked_decode(encoded, encoded_len, &decoded, &decoded_len)) {
674 RETURN_STRINGL(decoded, decoded_len, 0);
675 } else {
676 RETURN_FALSE;
677 }
678 }
679 /* }}} */
680
681 /* {{{ proto array http_split_response(string http_response)
682 *
683 * This function splits an HTTP response into an array with headers and the
684 * content body. The returned array may look simliar to the following example:
685 *
686 * <pre>
687 * <?php
688 * array(
689 * 0 => array(
690 * 'Status' => '200 Ok',
691 * 'Content-Type' => 'text/plain',
692 * 'Content-Language' => 'en-US'
693 * ),
694 * 1 => "Hello World!"
695 * );
696 * ?>
697 * </pre>
698 */
699 PHP_FUNCTION(http_split_response)
700 {
701 zval *zresponse, *zbody, *zheaders;
702
703 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zresponse) != SUCCESS) {
704 RETURN_FALSE;
705 }
706
707 convert_to_string_ex(&zresponse);
708
709 MAKE_STD_ZVAL(zbody);
710 MAKE_STD_ZVAL(zheaders);
711 array_init(zheaders);
712
713 if (SUCCESS != http_split_response(zresponse, zheaders, zbody)) {
714 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP response");
715 RETURN_FALSE;
716 }
717
718 array_init(return_value);
719 add_index_zval(return_value, 0, zheaders);
720 add_index_zval(return_value, 1, zbody);
721 }
722 /* }}} */
723
724 /* {{{ proto array http_parse_headers(string header)
725 *
726 */
727 PHP_FUNCTION(http_parse_headers)
728 {
729 char *header, *rnrn;
730 int header_len;
731
732 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &header, &header_len)) {
733 RETURN_FALSE;
734 }
735
736 array_init(return_value);
737
738 if (rnrn = strstr(header, HTTP_CRLF HTTP_CRLF)) {
739 header_len = rnrn - header + 2;
740 }
741 if (SUCCESS != http_parse_headers(header, header_len, return_value)) {
742 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse HTTP header");
743 zval_dtor(return_value);
744 RETURN_FALSE;
745 }
746 }
747 /* }}}*/
748
749 /* {{{ proto array http_get_request_headers(void)
750 *
751 */
752 PHP_FUNCTION(http_get_request_headers)
753 {
754 if (ZEND_NUM_ARGS()) {
755 WRONG_PARAM_COUNT;
756 }
757
758 array_init(return_value);
759 http_get_request_headers(return_value);
760 }
761 /* }}} */
762
763 /* {{{ HAVE_CURL */
764 #ifdef HTTP_HAVE_CURL
765
766 /* {{{ proto string http_get(string url[, array options[, array &info]])
767 *
768 * Performs an HTTP GET request on the supplied url.
769 *
770 * The second parameter is expected to be an associative
771 * array where the following keys will be recognized:
772 * <pre>
773 * - redirect: int, whether and how many redirects to follow
774 * - unrestrictedauth: bool, whether to continue sending credentials on
775 * redirects to a different host
776 * - proxyhost: string, proxy host in "host[:port]" format
777 * - proxyport: int, use another proxy port as specified in proxyhost
778 * - proxyauth: string, proxy credentials in "user:pass" format
779 * - proxyauthtype: int, HTTP_AUTH_BASIC and/or HTTP_AUTH_NTLM
780 * - httpauth: string, http credentials in "user:pass" format
781 * - httpauthtype: int, HTTP_AUTH_BASIC, DIGEST and/or NTLM
782 * - compress: bool, whether to allow gzip/deflate content encoding
783 * (defaults to true)
784 * - port: int, use another port as specified in the url
785 * - referer: string, the referer to sends
786 * - useragent: string, the user agent to send
787 * (defaults to PECL::HTTP/version (PHP/version)))
788 * - headers: array, list of custom headers as associative array
789 * like array("header" => "value")
790 * - cookies: array, list of cookies as associative array
791 * like array("cookie" => "value")
792 * - cookiestore: string, path to a file where cookies are/will be stored
793 * </pre>
794 *
795 * The optional third parameter will be filled with some additional information
796 * in form af an associative array, if supplied, like the following example:
797 * <pre>
798 * <?php
799 * array (
800 * 'effective_url' => 'http://localhost',
801 * 'response_code' => 403,
802 * 'total_time' => 0.017,
803 * 'namelookup_time' => 0.013,
804 * 'connect_time' => 0.014,
805 * 'pretransfer_time' => 0.014,
806 * 'size_upload' => 0,
807 * 'size_download' => 202,
808 * 'speed_download' => 11882,
809 * 'speed_upload' => 0,
810 * 'header_size' => 145,
811 * 'request_size' => 62,
812 * 'ssl_verifyresult' => 0,
813 * 'filetime' => -1,
814 * 'content_length_download' => 202,
815 * 'content_length_upload' => 0,
816 * 'starttransfer_time' => 0.017,
817 * 'content_type' => 'text/html; charset=iso-8859-1',
818 * 'redirect_time' => 0,
819 * 'redirect_count' => 0,
820 * 'private' => '',
821 * 'http_connectcode' => 0,
822 * 'httpauth_avail' => 0,
823 * 'proxyauth_avail' => 0,
824 * )
825 * ?>
826 * </pre>
827 */
828 PHP_FUNCTION(http_get)
829 {
830 char *URL, *data = NULL;
831 size_t data_len = 0;
832 int URL_len;
833 zval *options = NULL, *info = NULL;
834
835 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
836 RETURN_FALSE;
837 }
838
839 if (info) {
840 zval_dtor(info);
841 array_init(info);
842 }
843
844 if (SUCCESS == http_get(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
845 RETURN_STRINGL(data, data_len, 0);
846 } else {
847 RETURN_FALSE;
848 }
849 }
850 /* }}} */
851
852 /* {{{ proto string http_head(string url[, array options[, array &info]])
853 *
854 * Performs an HTTP HEAD request on the suppied url.
855 * Returns the HTTP response as string.
856 * See http_get() for a full list of available options.
857 */
858 PHP_FUNCTION(http_head)
859 {
860 char *URL, *data = NULL;
861 size_t data_len = 0;
862 int URL_len;
863 zval *options = NULL, *info = NULL;
864
865 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a/!z", &URL, &URL_len, &options, &info) != SUCCESS) {
866 RETURN_FALSE;
867 }
868
869 if (info) {
870 zval_dtor(info);
871 array_init(info);
872 }
873
874 if (SUCCESS == http_head(URL, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
875 RETURN_STRINGL(data, data_len, 0);
876 } else {
877 RETURN_FALSE;
878 }
879 }
880 /* }}} */
881
882 /* {{{ proto string http_post_data(string url, string data[, array options[, &info]])
883 *
884 * Performs an HTTP POST request, posting data.
885 * Returns the HTTP response as string.
886 * See http_get() for a full list of available options.
887 */
888 PHP_FUNCTION(http_post_data)
889 {
890 char *URL, *postdata, *data = NULL;
891 size_t data_len = 0;
892 int postdata_len, URL_len;
893 zval *options = NULL, *info = NULL;
894
895 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|a/!z", &URL, &URL_len, &postdata, &postdata_len, &options, &info) != SUCCESS) {
896 RETURN_FALSE;
897 }
898
899 if (info) {
900 zval_dtor(info);
901 array_init(info);
902 }
903
904 if (SUCCESS == http_post_data(URL, postdata, (size_t) postdata_len, HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
905 RETURN_STRINGL(data, data_len, 0);
906 } else {
907 RETURN_FALSE;
908 }
909 }
910 /* }}} */
911
912 /* {{{ proto string http_post_array(string url, array data[, array options[, array &info]])
913 *
914 * Performs an HTTP POST request, posting www-form-urlencoded array data.
915 * Returns the HTTP response as string.
916 * See http_get() for a full list of available options.
917 */
918 PHP_FUNCTION(http_post_array)
919 {
920 char *URL, *data = NULL;
921 size_t data_len = 0;
922 int URL_len;
923 zval *options = NULL, *info = NULL, *postdata;
924
925 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|a/!z", &URL, &URL_len, &postdata, &options, &info) != SUCCESS) {
926 RETURN_FALSE;
927 }
928
929 if (info) {
930 zval_dtor(info);
931 array_init(info);
932 }
933
934 if (SUCCESS == http_post_array(URL, Z_ARRVAL_P(postdata), HASH_ORNULL(options), HASH_ORNULL(info), &data, &data_len)) {
935 RETURN_STRINGL(data, data_len, 0);
936 } else {
937 RETURN_FALSE;
938 }
939 }
940 /* }}} */
941
942 #endif
943 /* }}} HAVE_CURL */
944
945
946 /* {{{ proto bool http_auth_basic(string user, string pass[, string realm = "Restricted"])
947 *
948 * Example:
949 * <pre>
950 * <?php
951 * if (!http_auth_basic('mike', 's3c|r3t')) {
952 * die('<h1>Authorization failed!</h1>');
953 * }
954 * ?>
955 * </pre>
956 */
957 PHP_FUNCTION(http_auth_basic)
958 {
959 char *realm = NULL, *user, *pass, *suser, *spass;
960 int r_len, u_len, p_len;
961
962 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s", &user, &u_len, &pass, &p_len, &realm, &r_len) != SUCCESS) {
963 RETURN_FALSE;
964 }
965
966 if (!realm) {
967 realm = "Restricted";
968 }
969
970 if (SUCCESS != http_auth_credentials(&suser, &spass)) {
971 http_auth_header("Basic", realm);
972 RETURN_FALSE;
973 }
974
975 if (strcasecmp(suser, user)) {
976 http_auth_header("Basic", realm);
977 RETURN_FALSE;
978 }
979
980 if (strcmp(spass, pass)) {
981 http_auth_header("Basic", realm);
982 RETURN_FALSE;
983 }
984
985 RETURN_TRUE;
986 }
987 /* }}} */
988
989 /* {{{ proto bool http_auth_basic_cb(mixed callback[, string realm = "Restricted"])
990 *
991 * Example:
992 * <pre>
993 * <?php
994 * function auth_cb($user, $pass)
995 * {
996 * global $db;
997 * $query = 'SELECT pass FROM users WHERE user='. $db->quoteSmart($user);
998 * if (strlen($realpass = $db->getOne($query)) {
999 * return $pass === $realpass;
1000 * }
1001 * return false;
1002 * }
1003 *
1004 * if (!http_auth_basic_cb('auth_cb')) {
1005 * die('<h1>Authorization failed</h1>');
1006 * }
1007 * ?>
1008 * </pre>
1009 */
1010 PHP_FUNCTION(http_auth_basic_cb)
1011 {
1012 zval *cb;
1013 char *realm = NULL, *user, *pass;
1014 int r_len;
1015
1016 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &cb, &realm, &r_len) != SUCCESS) {
1017 RETURN_FALSE;
1018 }
1019
1020 if (!realm) {
1021 realm = "Restricted";
1022 }
1023
1024 if (SUCCESS != http_auth_credentials(&user, &pass)) {
1025 http_auth_header("Basic", realm);
1026 RETURN_FALSE;
1027 }
1028 {
1029 zval *zparams[2] = {NULL, NULL}, retval;
1030 int result = 0;
1031
1032 MAKE_STD_ZVAL(zparams[0]);
1033 MAKE_STD_ZVAL(zparams[1]);
1034 ZVAL_STRING(zparams[0], user, 0);
1035 ZVAL_STRING(zparams[1], pass, 0);
1036
1037 if (SUCCESS == call_user_function(EG(function_table), NULL, cb,
1038 &retval, 2, zparams TSRMLS_CC)) {
1039 result = Z_LVAL(retval);
1040 }
1041
1042 efree(user);
1043 efree(pass);
1044 efree(zparams[0]);
1045 efree(zparams[1]);
1046
1047 if (!result) {
1048 http_auth_header("Basic", realm);
1049 }
1050
1051 RETURN_BOOL(result);
1052 }
1053 }
1054 /* }}}*/
1055
1056
1057 /* {{{ php_http_init_globals(zend_http_globals *) */
1058 static void php_http_init_globals(zend_http_globals *http_globals)
1059 {
1060 http_globals->etag_started = 0;
1061 http_globals->ctype = NULL;
1062 http_globals->etag = NULL;
1063 http_globals->lmod = 0;
1064 #ifdef HTTP_HAVE_CURL
1065 http_globals->curlbuf.body.data = NULL;
1066 http_globals->curlbuf.body.used = 0;
1067 http_globals->curlbuf.body.free = 0;
1068 http_globals->curlbuf.hdrs.data = NULL;
1069 http_globals->curlbuf.hdrs.used = 0;
1070 http_globals->curlbuf.hdrs.free = 0;
1071 #endif
1072 http_globals->allowed_methods = NULL;
1073 }
1074 /* }}} */
1075
1076 /* {{{ static inline STATUS http_check_allowed_methods(char *, int) */
1077 #define http_check_allowed_methods(m, l) _http_check_allowed_methods((m), (l) TSRMLS_CC)
1078 static inline void _http_check_allowed_methods(char *methods, int length TSRMLS_DC)
1079 {
1080 if (length && SG(request_info).request_method && (!strstr(methods, SG(request_info).request_method))) {
1081 char *allow_header = emalloc(length + sizeof("Allow: "));
1082 sprintf(allow_header, "Allow: %s", methods);
1083 http_send_header(allow_header);
1084 efree(allow_header);
1085 http_send_status(405);
1086 zend_bailout();
1087 }
1088 }
1089 /* }}} */
1090
1091 /* {{{ PHP_INI */
1092 PHP_INI_MH(update_allowed_methods)
1093 {
1094 http_check_allowed_methods(new_value, new_value_length);
1095 return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
1096 }
1097
1098 PHP_INI_BEGIN()
1099 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)
1100 PHP_INI_END()
1101 /* }}} */
1102
1103 /* {{{ PHP_MINIT_FUNCTION */
1104 PHP_MINIT_FUNCTION(http)
1105 {
1106 ZEND_INIT_MODULE_GLOBALS(http, php_http_init_globals, NULL);
1107 REGISTER_INI_ENTRIES();
1108 #ifdef HTTP_HAVE_CURL
1109 REGISTER_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC, CONST_CS | CONST_PERSISTENT);
1110 REGISTER_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS | CONST_PERSISTENT);
1111 REGISTER_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM, CONST_CS | CONST_PERSISTENT);
1112 #endif
1113 return SUCCESS;
1114 }
1115 /* }}} */
1116
1117 /* {{{ PHP_MSHUTDOWN_FUNCTION */
1118 PHP_MSHUTDOWN_FUNCTION(http)
1119 {
1120 UNREGISTER_INI_ENTRIES();
1121 return SUCCESS;
1122 }
1123 /* }}} */
1124
1125 /* {{{ PHP_RINIT_FUNCTION */
1126 PHP_RINIT_FUNCTION(http)
1127 {
1128 char *allowed_methods = INI_STR("http.allowed_methods");
1129 http_check_allowed_methods(allowed_methods, strlen(allowed_methods));
1130 return SUCCESS;
1131 }
1132 /* }}} */
1133
1134 /* {{{ PHP_RSHUTDOWN_FUNCTION */
1135 PHP_RSHUTDOWN_FUNCTION(http)
1136 {
1137 HTTP_G(etag_started) = 0;
1138 HTTP_G(lmod) = 0;
1139
1140 if (HTTP_G(etag)) {
1141 efree(HTTP_G(etag));
1142 HTTP_G(etag) = NULL;
1143 }
1144
1145 if (HTTP_G(ctype)) {
1146 efree(HTTP_G(ctype));
1147 HTTP_G(ctype) = NULL;
1148 }
1149 #ifdef HTTP_HAVE_CURL
1150 if (HTTP_G(curlbuf).body.data) {
1151 efree(HTTP_G(curlbuf).body.data);
1152 HTTP_G(curlbuf).body.data = NULL;
1153 }
1154 if (HTTP_G(curlbuf).hdrs.data) {
1155 efree(HTTP_G(curlbuf).hdrs.data);
1156 HTTP_G(curlbuf).hdrs.data = NULL;
1157 }
1158 #endif
1159 return SUCCESS;
1160 }
1161 /* }}} */
1162
1163 /* {{{ PHP_MINFO_FUNCTION */
1164 PHP_MINFO_FUNCTION(http)
1165 {
1166 php_info_print_table_start();
1167 php_info_print_table_header(2, "Extended HTTP support", "enabled");
1168 php_info_print_table_row(2, "Version:", PHP_EXT_HTTP_VERSION);
1169 php_info_print_table_row(2, "cURL convenience functions:",
1170 #ifdef HTTP_HAVE_CURL
1171 "enabled"
1172 #else
1173 "disabled"
1174 #endif
1175 );
1176 php_info_print_table_end();
1177
1178 DISPLAY_INI_ENTRIES();
1179 }
1180 /* }}} */
1181
1182 /*
1183 * Local variables:
1184 * tab-width: 4
1185 * c-basic-offset: 4
1186 * End:
1187 * vim600: noet sw=4 ts=4 fdm=marker
1188 * vim<600: noet sw=4 ts=4
1189 */
1190