2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
23 #include "php_version.h"
24 #include "php_streams.h"
25 #include "main/snprintf.h"
26 #include "ext/standard/md5.h"
27 #include "ext/standard/url.h"
28 #include "ext/standard/base64.h"
29 #include "ext/standard/php_string.h"
30 #include "ext/standard/php_lcg.h"
34 #if (PHP_MAJOR_VERSION >= 5)
35 #include "ext/standard/php_http.h"
37 #include "php_http_build_query.h"
38 #include "http_build_query.c"
42 #include "php_http_api.h"
46 #if defined(HAVE_CURL) && HAVE_CURL
47 #include <curl/curl.h>
48 #include <curl/easy.h>
51 ZEND_DECLARE_MODULE_GLOBALS(http
)
53 /* {{{ day/month names */
54 static const char *days
[] = {
55 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
57 static const char *wkdays
[] = {
58 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
60 static const char *weekdays
[] = {
61 "Monday", "Tuesday", "Wednesday",
62 "Thursday", "Friday", "Saturday", "Sunday"
64 static const char *months
[] = {
65 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
66 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
73 static const struct time_zone
{
77 {"GMT", 0}, /* Greenwich Mean */
78 {"UTC", 0}, /* Universal (Coordinated) */
79 {"WET", 0}, /* Western European */
80 {"BST", 0}, /* British Summer */
81 {"WAT", 60}, /* West Africa */
82 {"AST", 240}, /* Atlantic Standard */
83 {"ADT", 240}, /* Atlantic Daylight */
84 {"EST", 300}, /* Eastern Standard */
85 {"EDT", 300}, /* Eastern Daylight */
86 {"CST", 360}, /* Central Standard */
87 {"CDT", 360}, /* Central Daylight */
88 {"MST", 420}, /* Mountain Standard */
89 {"MDT", 420}, /* Mountain Daylight */
90 {"PST", 480}, /* Pacific Standard */
91 {"PDT", 480}, /* Pacific Daylight */
92 {"YST", 540}, /* Yukon Standard */
93 {"YDT", 540}, /* Yukon Daylight */
94 {"HST", 600}, /* Hawaii Standard */
95 {"HDT", 600}, /* Hawaii Daylight */
96 {"CAT", 600}, /* Central Alaska */
97 {"AHST", 600}, /* Alaska-Hawaii Standard */
98 {"NT", 660}, /* Nome */
99 {"IDLW", 720}, /* International Date Line West */
100 {"CET", -60}, /* Central European */
101 {"MET", -60}, /* Middle European */
102 {"MEWT", -60}, /* Middle European Winter */
103 {"MEST", -120}, /* Middle European Summer */
104 {"CEST", -120}, /* Central European Summer */
105 {"MESZ", -60}, /* Middle European Summer */
106 {"FWT", -60}, /* French Winter */
107 {"FST", -60}, /* French Summer */
108 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
109 {"WAST", -420}, /* West Australian Standard */
110 {"WADT", -420}, /* West Australian Daylight */
111 {"CCT", -480}, /* China Coast, USSR Zone 7 */
112 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
113 {"EAST", -600}, /* Eastern Australian Standard */
114 {"EADT", -600}, /* Eastern Australian Daylight */
115 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
116 {"NZT", -720}, /* New Zealand */
117 {"NZST", -720}, /* New Zealand Standard */
118 {"NZDT", -720}, /* New Zealand Daylight */
119 {"IDLE", -720}, /* International Date Line East */
125 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
126 #define http_etag(e, p, l, m) _http_etag((e), (p), (l), (m) TSRMLS_CC)
127 static inline char *_http_etag(char **new_etag
, const void *data_ptr
, const size_t data_len
, const http_send_mode data_mode TSRMLS_DC
);
128 #define http_is_range_request() _http_is_range_request(TSRMLS_C)
129 static inline int _http_is_range_request(TSRMLS_D
);
130 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
131 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
133 static int check_day(char *day
, size_t len
);
134 static int check_month(char *month
);
135 static int check_tzone(char *tzone
);
138 #if defined(HAVE_CURL) && HAVE_CURL
139 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
140 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
);
141 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
142 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
);
143 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
144 static inline void _http_curl_sizebuf(http_curlbuf_member member
, size_t len TSRMLS_DC
);
145 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
146 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
147 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
148 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
149 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
150 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
);
152 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
153 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
154 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
155 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
157 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
158 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
162 /* {{{ static int http_sort_q(const void *, const void *) */
163 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
166 zval result
, *first
, *second
;
168 f
= *((Bucket
**) a
);
169 s
= *((Bucket
**) b
);
171 first
= *((zval
**) f
->pData
);
172 second
= *((zval
**) s
->pData
);
174 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
177 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
181 /* {{{ static inline char *http_etag(char **, void *, size_t, http_send_mode) */
182 static inline char *_http_etag(char **new_etag
, const void *data_ptr
,
183 const size_t data_len
, const http_send_mode data_mode TSRMLS_DC
)
185 char ssb_buf
[127], digest
[16];
193 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
197 snprintf(ssb_buf
, 127, "%l=%l=%l",
198 HTTP_G(ssb
).sb
.st_mtime
,
199 HTTP_G(ssb
).sb
.st_ino
,
200 HTTP_G(ssb
).sb
.st_size
202 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
210 PHP_MD5Final(digest
, &ctx
);
211 make_digest(*new_etag
, digest
);
217 /* {{{ static inline int http_is_range_request(void) */
218 static inline int _http_is_range_request(TSRMLS_D
)
220 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
221 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
225 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
227 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
228 const size_t end
, const http_send_mode mode TSRMLS_DC
)
232 long len
= end
- begin
;
238 s
= (php_stream
*) data
;
239 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
242 buf
= (char *) ecalloc(1, HTTP_BUF_SIZE
);
243 /* read into buf and write out */
244 while ((len
-= HTTP_BUF_SIZE
) >= 0) {
245 if (!(read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
))) {
249 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
255 /* read & write left over */
257 if (read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
+ len
)) {
258 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
272 return len
== php_body_write(
273 Z_STRVAL_P((zval
*) data
) + begin
, len TSRMLS_CC
)
285 #if defined(HAVE_CURL) && HAVE_CURL
287 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
288 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
)
290 http_curl_freebuf(member
);
292 if (member
& CURLBUF_HDRS
) {
293 HTTP_G(curlbuf
).hdrs
.data
= emalloc(HTTP_CURLBUF_HDRSSIZE
);
294 HTTP_G(curlbuf
).hdrs
.free
= HTTP_CURLBUF_HDRSSIZE
;
296 if (member
& CURLBUF_BODY
) {
297 HTTP_G(curlbuf
).body
.data
= emalloc(HTTP_CURLBUF_BODYSIZE
);
298 HTTP_G(curlbuf
).body
.free
= HTTP_CURLBUF_BODYSIZE
;
303 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
304 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
)
306 if (member
& CURLBUF_HDRS
) {
307 if (HTTP_G(curlbuf
).hdrs
.data
) {
308 efree(HTTP_G(curlbuf
).hdrs
.data
);
309 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
311 HTTP_G(curlbuf
).hdrs
.used
= 0;
312 HTTP_G(curlbuf
).hdrs
.free
= 0;
314 if (member
& CURLBUF_BODY
) {
315 if (HTTP_G(curlbuf
).body
.data
) {
316 efree(HTTP_G(curlbuf
).body
.data
);
317 HTTP_G(curlbuf
).body
.data
= NULL
;
319 HTTP_G(curlbuf
).body
.used
= 0;
320 HTTP_G(curlbuf
).body
.free
= 0;
325 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
327 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
,
328 size_t *data_len TSRMLS_DC
)
333 if ((member
& CURLBUF_HDRS
) && HTTP_G(curlbuf
).hdrs
.used
) {
334 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
335 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
+ 1);
337 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ 1);
339 memcpy(*data
, HTTP_G(curlbuf
).hdrs
.data
, HTTP_G(curlbuf
).hdrs
.used
);
340 *data_len
= HTTP_G(curlbuf
).hdrs
.used
;
343 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
345 memcpy((*data
) + HTTP_G(curlbuf
).hdrs
.used
,
346 HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
347 *data_len
= HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
;
349 emalloc(HTTP_G(curlbuf
).body
.used
+ 1);
350 memcpy(*data
, HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
351 *data_len
= HTTP_G(curlbuf
).body
.used
;
355 (*data
)[*data_len
] = 0;
362 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
364 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
,
365 size_t *data_len TSRMLS_DC
)
367 http_curl_copybuf(member
, data
, data_len
);
368 http_curl_freebuf(member
);
372 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
373 static size_t http_curl_body_callback(char *buf
, size_t len
, size_t n
, void *s
)
377 if ((len
*= n
) > HTTP_G(curlbuf
).body
.free
) {
378 size_t bsize
= HTTP_CURLBUF_BODYSIZE
;
379 while (bsize
< len
) {
382 HTTP_G(curlbuf
).body
.data
= erealloc(HTTP_G(curlbuf
).body
.data
,
383 HTTP_G(curlbuf
).body
.used
+ bsize
);
384 HTTP_G(curlbuf
).body
.free
+= bsize
;
387 memcpy(HTTP_G(curlbuf
).body
.data
+ HTTP_G(curlbuf
).body
.used
, buf
, len
);
388 HTTP_G(curlbuf
).body
.free
-= len
;
389 HTTP_G(curlbuf
).body
.used
+= len
;
395 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
396 static size_t http_curl_hdrs_callback(char *buf
, size_t len
, size_t n
, void *s
)
400 /* discard previous headers */
401 if ((HTTP_G(curlbuf
).hdrs
.used
) && (!strncmp(buf
, "HTTP/1.", strlen("HTTP/1.")))) {
402 http_curl_initbuf(CURLBUF_HDRS
);
405 if ((len
*= n
) > HTTP_G(curlbuf
).hdrs
.free
) {
406 size_t bsize
= HTTP_CURLBUF_HDRSSIZE
;
407 while (bsize
< len
) {
410 HTTP_G(curlbuf
).hdrs
.data
= erealloc(HTTP_G(curlbuf
).hdrs
.data
,
411 HTTP_G(curlbuf
).hdrs
.used
+ bsize
);
412 HTTP_G(curlbuf
).hdrs
.free
+= bsize
;
415 memcpy(HTTP_G(curlbuf
).hdrs
.data
+ HTTP_G(curlbuf
).hdrs
.used
, buf
, len
);
416 HTTP_G(curlbuf
).hdrs
.free
-= len
;
417 HTTP_G(curlbuf
).hdrs
.used
+= len
;
423 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
424 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
430 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
437 va_start(types
, checks
);
438 for (i
= 0; i
< checks
; ++i
) {
439 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
449 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
450 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
)
454 /* standard options */
455 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
456 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
457 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
458 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
459 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
460 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
462 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
464 #if defined(PHP_DEBUG)
465 curl_easy_setopt(ch
, CURLOPT_VERBOSE
, 1);
468 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
473 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
474 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
475 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
476 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
477 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
480 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
484 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
485 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
487 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
488 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
491 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
492 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
495 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
496 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
501 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
502 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
504 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
505 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
509 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
510 /* empty string enables deflate and gzip */
511 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
515 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
516 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
520 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
521 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
525 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
526 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
530 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
532 ZVAL_STRINGL(&glue
, "; ", 2, 0);
533 php_implode(&glue
, zoption
, &cookies
);
534 if (Z_STRVAL(cookies
)) {
535 curl_easy_setopt(ch
, CURLOPT_COOKIE
, Z_STRVAL(cookies
));
540 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
541 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, Z_STRVAL_P(zoption
));
542 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, Z_STRVAL_P(zoption
));
545 /* additional headers */
546 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
548 struct curl_slist
*headers
= NULL
;
549 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
550 while (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header
)) {
551 headers
= curl_slist_append(headers
, Z_STRVAL_PP(header
));
552 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
555 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
564 static int check_day(char *day
, size_t len
)
567 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
568 for (i
= 0; i
< 7; i
++) {
569 if (!strcmp(day
, check
[0])) {
577 static int check_month(char *month
)
580 const char * const *check
= &months
[0];
581 for (i
= 0; i
< 12; i
++) {
582 if (!strcmp(month
, check
[0])) {
590 /* return the time zone offset between GMT and the input one, in number
591 of seconds or -1 if the timezone wasn't found/legal */
593 static int check_tzone(char *tzone
)
596 const struct time_zone
*check
= time_zones
;
597 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
598 if (!strcmp(tzone
, check
->name
)) {
599 return check
->offset
* 60;
610 /* {{{ char *http_date(time_t) */
611 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
613 struct tm
*gmtime
, tmbuf
;
614 char *date
= ecalloc(1, 30);
616 gmtime
= php_gmtime_r(&t
, &tmbuf
);
618 "%s, %02d %s %04d %02d:%02d:%02d GMT",
619 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
620 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
621 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
627 /* time_t http_parse_date(char *)
628 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
629 PHP_HTTP_API
time_t http_parse_date(const char *date
)
632 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
633 hours
= -1, minutes
= -1, seconds
= -1;
635 enum assume_next dignext
= DATE_MDAY
;
636 const char *indate
= date
;
638 int found
= 0, part
= 0; /* max 6 parts */
640 while (*date
&& (part
< 6)) {
643 while (*date
&& !isalnum(*date
)) {
647 if (isalpha(*date
)) {
648 /* a name coming up */
651 sscanf(date
, "%31[A-Za-z]", buf
);
655 weekday
= check_day(buf
, len
);
661 if (!found
&& (month
== -1)) {
662 month
= check_month(buf
);
668 if (!found
&& (tz_offset
== -1)) {
669 /* this just must be a time zone string */
670 tz_offset
= check_tzone(buf
);
671 if (tz_offset
!= -1) {
677 return -1; /* bad string */
681 else if (isdigit(*date
)) {
685 if((seconds
== -1) && (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
691 val
= (int) strtol(date
, &end
, 10);
693 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) && (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
694 /* four digits and a value less than 1300 and it is preceeded with
695 a plus or minus. This is a time zone indication. */
697 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
699 /* the + and - prefix indicates the local time compared to GMT,
700 this we need ther reversed math to get what we want */
701 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
704 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
705 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
708 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
709 monthday
= val
% 100;
712 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
713 if ((val
> 0) && (val
< 32)) {
720 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
724 year
+= year
> 70 ? 1900 : 2000;
743 seconds
= minutes
= hours
= 0; /* no time, make it zero */
746 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
747 /* lacks vital info, fail */
751 if (sizeof(time_t) < 5) {
752 /* 32 bit time_t can only hold dates to the beginning of 2038 */
761 tm
.tm_mday
= monthday
;
763 tm
.tm_year
= year
- 1900;
770 /* time zone adjust */
772 struct tm
*gmt
, keeptime2
;
776 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
777 return -1; /* illegal date/time */
782 /* Add the time zone diff (between the given timezone and GMT) and the
783 diff between the local time zone and GMT. */
784 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
786 if((delta
> 0) && (t
+ delta
< t
)) {
787 return -1; /* time_t overflow */
797 /* {{{ inline STATUS http_send_status(int) */
798 PHP_HTTP_API
inline STATUS
_http_send_status(const int status TSRMLS_DC
)
801 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) s TSRMLS_CC
);
805 /* {{{ inline STATUS http_send_header(char *) */
806 PHP_HTTP_API
inline STATUS
_http_send_header(const char *header TSRMLS_DC
)
808 return http_send_status_header(0, header
);
812 /* {{{ inline STATUS http_send_status_header(int, char *) */
813 PHP_HTTP_API
inline STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
815 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
816 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
820 /* {{{ inline zval *http_get_server_var(char *) */
821 PHP_HTTP_API
inline zval
*_http_get_server_var(const char *key TSRMLS_DC
)
824 if (SUCCESS
== zend_hash_find(
825 Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
826 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
833 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
834 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
835 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
837 char etag
[33] = { 0 };
838 unsigned char digest
[16];
840 if (mode
& PHP_OUTPUT_HANDLER_START
) {
841 PHP_MD5Init(&HTTP_G(etag_md5
));
844 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
846 if (mode
& PHP_OUTPUT_HANDLER_END
) {
847 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
848 make_digest(etag
, digest
);
850 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
851 http_send_status(304);
853 http_send_etag(etag
, 32);
857 *handled_output_len
= output_len
;
858 *handled_output
= estrndup(output
, output_len
);
862 /* {{{ int http_modified_match(char *, int) */
863 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
867 char *modified
, *chr_ptr
;
869 HTTP_GSC(zmodified
, entry
, 0);
871 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
872 if (chr_ptr
= strrchr(modified
, ';')) {
875 retval
= (t
<= http_parse_date(modified
));
881 /* {{{ int http_etag_match(char *, char *) */
882 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
888 HTTP_GSC(zetag
, entry
, 0);
890 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
894 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
895 sprintf(quoted_etag
, "\"%s\"", etag
);
897 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
898 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
900 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
908 /* {{{ STATUS http_send_last_modified(int) */
909 PHP_HTTP_API STATUS
_http_send_last_modified(const int t TSRMLS_DC
)
911 char modified
[96] = "Last-Modified: ", *date
;
913 strcat(modified
, date
);
915 return http_send_header(modified
);
919 /* {{{ static STATUS http_send_etag(char *, int) */
920 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
921 const int etag_len TSRMLS_DC
)
927 header_len
= strlen("ETag: \"\"") + etag_len
+ 1;
928 etag_header
= (char *) emalloc(header_len
);
929 snprintf(etag_header
, header_len
, "ETag: \"%s\"", etag
);
930 ret
= http_send_header(etag_header
);
936 /* {{{ char *http_absolute_uri(char *, char *) */
937 PHP_HTTP_API
char *_http_absolute_uri(const char *url
,
938 const char *proto TSRMLS_DC
)
940 char URI
[HTTP_URI_MAXLEN
+ 1], *PTR
, *proto_ptr
, *host
, *path
;
943 if (!url
|| !strlen(url
)) {
944 if (!SG(request_info
).request_uri
) {
947 url
= SG(request_info
).request_uri
;
949 /* Mess around with already absolute URIs */
950 else if (proto_ptr
= strstr(url
, "://")) {
951 if (!proto
|| !strncmp(url
, proto
, strlen(proto
))) {
954 snprintf(URI
, HTTP_URI_MAXLEN
, "%s%s", proto
, proto_ptr
+ 3);
959 /* protocol defaults to http */
960 if (!proto
|| !strlen(proto
)) {
965 if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
966 (zhost
= http_get_server_var("SERVER_NAME"))) {
967 host
= Z_STRVAL_P(zhost
);
975 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s", proto
, host
, url
);
976 } else if (SG(request_info
).request_uri
) {
977 path
= estrdup(SG(request_info
).request_uri
);
978 php_dirname(path
, strlen(path
));
979 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s/%s", proto
, host
, path
, url
);
982 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s/%s", proto
, host
, url
);
985 /* strip everything after a new line */
988 if (*PTR
== '\n' || *PTR
== '\r') {
999 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1000 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1001 const char *def TSRMLS_DC
)
1003 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1004 char *q_ptr
, *result
;
1008 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1010 MAKE_STD_ZVAL(zarray
);
1013 MAKE_STD_ZVAL(zdelim
);
1014 ZVAL_STRING(zdelim
, ",", 0);
1015 php_explode(zdelim
, zaccept
, zarray
, -1);
1018 MAKE_STD_ZVAL(zentries
);
1019 array_init(zentries
);
1021 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1022 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1024 if (SUCCESS
!= zend_hash_get_current_data(
1025 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1026 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1027 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1031 /* check for qualifier */
1032 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1033 qual
= strtod(q_ptr
+ 3, NULL
);
1038 /* walk through the supported array */
1039 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported
));
1040 SUCCESS
== zend_hash_get_current_data(
1041 Z_ARRVAL_P(supported
), (void **) &zsupp
);
1042 zend_hash_move_forward(Z_ARRVAL_P(supported
))) {
1043 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1044 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1053 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1055 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1056 http_sort_q
, 0 TSRMLS_CC
)) ||
1057 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1058 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1059 result
= estrdup(def
);
1062 zval_dtor(zentries
);
1069 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1070 PHP_HTTP_API http_range_status
_http_get_request_ranges(zval
*zranges
,
1071 const size_t length TSRMLS_DC
)
1073 zval
*zrange
, *zentry
, **zentries
;
1075 long begin
= -1, end
= -1, *ptr
;
1077 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1078 range
= Z_STRVAL_P(zrange
);
1080 if (strncmp(range
, "bytes=", strlen("bytes="))) {
1081 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1086 range
+= strlen("bytes=");
1089 switch (c
= *(range
++))
1095 case '1': case '2': case '3':
1096 case '4': case '5': case '6':
1097 case '7': case '8': case '9':
1099 * If the value of the pointer is already set (non-negative)
1100 * then multiply its value by ten and add the current value,
1101 * else initialise the pointers value with the current value
1103 * This let us recognize empty fields when validating the
1104 * ranges, i.e. a "-10" for begin and "12345" for the end
1105 * was the following range request: "Range: bytes=0-12345";
1106 * While a "-1" for begin and "12345" for the end would
1107 * have been: "Range: bytes=-12345".
1125 /* validate ranges */
1130 if ((length
- end
) < 1) {
1138 if ((length
- end
) < 1) {
1141 begin
= length
- end
;
1151 if ((length
- begin
) < 1) {
1159 if ( ((length
- begin
) < 1) ||
1160 ((length
- end
) < 1) ||
1161 ((begin
- end
) >= 0)) {
1170 zentry
= (zval
*) zentries
++;
1171 MAKE_STD_ZVAL(zentry
);
1173 add_index_long(zentry
, 0, begin
);
1174 add_index_long(zentry
, 1, end
);
1175 add_next_index_zval(zranges
, zentry
);
1192 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1193 PHP_HTTP_API STATUS
_http_send_ranges(zval
*zranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1196 long b
, e
, **begin
, **end
;
1197 char range_header
[255], multi_header
[68] = "Content-Type: multipart/byteranges; boundary=", bound
[23], preface
[1024];
1200 /* Send HTTP 206 Partial Content */
1201 http_send_status(206);
1204 if ((c
= zend_hash_num_elements(Z_ARRVAL_P(zranges
))) == 1) {
1205 zend_hash_index_find(Z_ARRVAL_P(zranges
), 0, (void **) &zrange
);
1206 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
);
1207 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
);
1209 /* so zranges can be freed */
1217 /* send content range header */
1218 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", b
, e
, size
);
1219 http_send_header(range_header
);
1221 /* send requested chunk */
1222 return http_send_chunk(data
, b
, e
+ 1, mode
);
1227 snprintf(bound
, 23, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1228 strncat(multi_header
, bound
+ 2, 21);
1229 http_send_header(multi_header
);
1231 /* send each requested chunk */
1232 for ( i
= 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges
));
1234 i
++, zend_hash_move_forward(Z_ARRVAL_P(zranges
))) {
1235 if ( HASH_KEY_NON_EXISTANT
== zend_hash_get_current_data(
1236 Z_ARRVAL_P(zranges
), (void **) &zrange
) ||
1237 SUCCESS
!= zend_hash_index_find(
1238 Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1239 SUCCESS
!= zend_hash_index_find(
1240 Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1244 snprintf(preface
, 1024,
1245 "\r\n%s\r\nContent-Type: %s\r\nContent-Range: bytes %d-%d/%d\r\n\r\n", bound
,
1246 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1247 **begin
, **end
, size
);
1248 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1249 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1257 /* write boundary once more */
1258 php_body_write("\n", 1 TSRMLS_CC
);
1259 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1265 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1266 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1267 const http_send_mode data_mode TSRMLS_DC
)
1269 zval
*zranges
= NULL
;
1275 /* never ever use the output to compute the ETag if http_send() is used */
1276 if (HTTP_G(etag_started
)) {
1277 char *new_etag
= (char *) emalloc(33);
1279 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1280 if (NULL
== http_etag(&new_etag
, data_ptr
, data_size
, data_mode
)) {
1285 /* send 304 Not Modified if etag matches */
1286 if (http_etag_match("HTTP_IF_NONE_MATCH", new_etag
)) {
1288 return http_send_status(304);
1291 http_send_etag(new_etag
, 32);
1295 if (http_is_range_request()) {
1297 MAKE_STD_ZVAL(zranges
);
1298 array_init(zranges
);
1300 switch (http_get_request_ranges(zranges
, data_size
))
1305 /* go ahead and send all */
1309 return http_send_ranges(zranges
, data_ptr
, data_size
, data_mode
);
1315 http_send_status(416);
1325 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1329 /* {{{ STATUS http_send_data(zval *) */
1330 PHP_HTTP_API STATUS
_http_send_data(const zval
*zdata TSRMLS_DC
)
1332 if (!Z_STRLEN_P(zdata
)) {
1335 if (!Z_STRVAL_P(zdata
)) {
1339 return http_send((void *) zdata
, Z_STRLEN_P(zdata
), SEND_DATA
);
1343 /* {{{ STATUS http_send_stream(php_stream *) */
1344 PHP_HTTP_API STATUS
_http_send_stream(const php_stream
*file TSRMLS_DC
)
1346 if (php_stream_stat((php_stream
*) file
, &HTTP_G(ssb
))) {
1350 return http_send((void *) file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1354 /* {{{ STATUS http_send_file(zval *) */
1355 PHP_HTTP_API STATUS
_http_send_file(const zval
*zfile TSRMLS_DC
)
1360 if (!Z_STRLEN_P(zfile
)) {
1364 if (!(file
= php_stream_open_wrapper(Z_STRVAL_P(zfile
), "rb",
1365 REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1369 ret
= http_send_stream(file
);
1370 php_stream_close(file
);
1375 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1376 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1377 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1383 *decoded
= (char *) ecalloc(1, encoded_len
);
1387 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1388 char hex_len
[9] = {0};
1389 size_t chunk_len
= 0;
1392 /* read in chunk size */
1393 while (isxdigit(*e_ptr
)) {
1395 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1396 "Chunk size is too long: 0x%s...", hex_len
);
1400 hex_len
[i
++] = *e_ptr
++;
1404 if (strcmp(hex_len
, "0")) {
1406 chunk_len
= strtol(hex_len
, &error
, 16);
1407 if (error
== hex_len
) {
1408 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1409 "Invalid chunk size string: '%s'", hex_len
);
1418 if (*e_ptr
++ != '\r' || *e_ptr
++ != '\n') {
1419 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1420 "Invalid character (expected 0x0A, 0x0D; got: %x)",
1426 memcpy(d_ptr
, e_ptr
, chunk_len
);
1428 e_ptr
+= chunk_len
+ 2;
1429 *decoded_len
+= chunk_len
;
1436 /* {{{ proto void http_split_response(zval *, zval *, zval *) */
1437 PHP_HTTP_API
void _http_split_response(const zval
*zresponse
, zval
*zheaders
,
1438 zval
*zbody TSRMLS_DC
)
1440 char *header
, *response
, *body
= NULL
;
1441 long response_len
= Z_STRLEN_P(zresponse
);
1442 header
= response
= Z_STRVAL_P(zresponse
);
1444 while ((response
- Z_STRVAL_P(zresponse
) + 3) < response_len
) {
1445 if ( (*response
++ == '\r') &&
1446 (*response
++ == '\n') &&
1447 (*response
++ == '\r') &&
1448 (*response
++ == '\n')) {
1454 if (body
&& (response_len
- (body
- header
))) {
1455 ZVAL_STRINGL(zbody
, body
, response_len
- (body
- header
) - 1, 1);
1457 Z_TYPE_P(zbody
) = IS_NULL
;
1460 /* check for HTTP status - FIXXME: strchr() */
1461 if (!strncmp(header
, "HTTP/1.", 7)) {
1462 char *end
= strchr(header
, '\r');
1463 add_assoc_stringl(zheaders
, "Status",
1464 header
+ strlen("HTTP/1.x "),
1465 end
- (header
+ strlen("HTTP/1.x ")), 1);
1470 char *colon
= NULL
, *line
= header
;
1472 while ( (line
- Z_STRVAL_P(zresponse
) + 3) <
1473 (body
- Z_STRVAL_P(zresponse
))) {
1477 if (colon
&& (*line
== '\n')) {
1478 char *key
= estrndup(header
, colon
- header
);
1479 add_assoc_stringl(zheaders
, key
,
1480 colon
+ 2, line
- colon
- 3, 1);
1484 header
+= line
- header
+ 1;
1500 #if defined(HAVE_CURL) && HAVE_CURL
1502 /* {{{ STATUS http_get(char *, HashTable *, char **, size_t *) */
1503 PHP_HTTP_API STATUS
_http_get(const char *URL
, HashTable
*options
,
1504 char **data
, size_t *data_len TSRMLS_DC
)
1506 CURL
*ch
= curl_easy_init();
1509 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1513 http_curl_initbuf(CURLBUF_EVRY
);
1514 http_curl_setopts(ch
, URL
, options
);
1516 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1517 curl_easy_cleanup(ch
);
1518 http_curl_freebuf(CURLBUF_EVRY
);
1519 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1522 curl_easy_cleanup(ch
);
1524 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1530 /* {{{ STATUS http_head(char *, HashTable *, char **data, size_t *) */
1531 PHP_HTTP_API STATUS
_http_head(const char *URL
, HashTable
*options
,
1532 char **data
, size_t *data_len TSRMLS_DC
)
1534 CURL
*ch
= curl_easy_init();
1537 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1541 http_curl_initbuf(CURLBUF_HDRS
);
1542 http_curl_setopts(ch
, URL
, options
);
1543 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
1545 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1546 curl_easy_cleanup(ch
);
1547 http_curl_freebuf(CURLBUF_HDRS
);
1548 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1551 curl_easy_cleanup(ch
);
1553 http_curl_movebuf(CURLBUF_HDRS
, data
, data_len
);
1559 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, char **, size_t *) */
1560 PHP_HTTP_API STATUS
_http_post_data(const char *URL
, char *postdata
,
1561 size_t postdata_len
, HashTable
*options
, char **data
,
1562 size_t *data_len TSRMLS_DC
)
1564 CURL
*ch
= curl_easy_init();
1567 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1571 http_curl_initbuf(CURLBUF_EVRY
);
1572 http_curl_setopts(ch
, URL
, options
);
1573 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
1574 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
1575 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
1577 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1578 curl_easy_cleanup(ch
);
1579 http_curl_freebuf(CURLBUF_EVRY
);
1580 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1583 curl_easy_cleanup(ch
);
1585 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1591 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, char **, size_t *) */
1592 PHP_HTTP_API STATUS
_http_post_array(const char *URL
, HashTable
*postarray
,
1593 HashTable
*options
, char **data
, size_t *data_len TSRMLS_DC
)
1595 smart_str qstr
= {0};
1598 if (php_url_encode_hash_ex(postarray
, &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
1602 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
1607 status
= http_post_data(URL
, qstr
.c
, qstr
.len
, options
, data
, data_len
);
1618 /* {{{ void http_auth_header(char *, char*) */
1619 PHP_HTTP_API
void _http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1621 char realm_header
[1024];
1622 snprintf(realm_header
, 1024, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1623 http_send_status_header(401, realm_header
);
1627 /* {{{ STATUS http_auth_credentials(char **, char **) */
1628 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1630 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1631 zval
*zuser
, *zpass
;
1633 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1634 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1636 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1637 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1642 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1644 char *decoded
, *colon
;
1646 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1649 if (colon
= strchr(decoded
+ 6, ':')) {
1650 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1651 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1662 /* }}} public API */
1669 * vim600: noet sw=4 ts=4 fdm=marker
1670 * vim<600: noet sw=4 ts=4