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