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