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 "ext/standard/md5.h"
26 #include "ext/standard/url.h"
27 #include "ext/standard/base64.h"
28 #include "ext/standard/php_string.h"
29 #include "ext/standard/php_lcg.h"
33 #if (PHP_MAJOR_VERSION >= 5)
34 #include "ext/standard/php_http.h"
36 #include "php_http_build_query.h"
37 #include "http_build_query.c"
41 #include "php_http_api.h"
45 #if defined(HAVE_CURL) && HAVE_CURL
46 #include <curl/curl.h>
47 #include <curl/easy.h>
50 ZEND_DECLARE_MODULE_GLOBALS(http
)
52 /* {{{ day/month names */
53 static const char *days
[] = {
54 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
56 static const char *wkdays
[] = {
57 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
59 static const char *weekdays
[] = {
60 "Monday", "Tuesday", "Wednesday",
61 "Thursday", "Friday", "Saturday", "Sunday"
63 static const char *months
[] = {
64 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
65 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
72 static const struct time_zone
{
76 {"GMT", 0}, /* Greenwich Mean */
77 {"UTC", 0}, /* Universal (Coordinated) */
78 {"WET", 0}, /* Western European */
79 {"BST", 0}, /* British Summer */
80 {"WAT", 60}, /* West Africa */
81 {"AST", 240}, /* Atlantic Standard */
82 {"ADT", 240}, /* Atlantic Daylight */
83 {"EST", 300}, /* Eastern Standard */
84 {"EDT", 300}, /* Eastern Daylight */
85 {"CST", 360}, /* Central Standard */
86 {"CDT", 360}, /* Central Daylight */
87 {"MST", 420}, /* Mountain Standard */
88 {"MDT", 420}, /* Mountain Daylight */
89 {"PST", 480}, /* Pacific Standard */
90 {"PDT", 480}, /* Pacific Daylight */
91 {"YST", 540}, /* Yukon Standard */
92 {"YDT", 540}, /* Yukon Daylight */
93 {"HST", 600}, /* Hawaii Standard */
94 {"HDT", 600}, /* Hawaii Daylight */
95 {"CAT", 600}, /* Central Alaska */
96 {"AHST", 600}, /* Alaska-Hawaii Standard */
97 {"NT", 660}, /* Nome */
98 {"IDLW", 720}, /* International Date Line West */
99 {"CET", -60}, /* Central European */
100 {"MET", -60}, /* Middle European */
101 {"MEWT", -60}, /* Middle European Winter */
102 {"MEST", -120}, /* Middle European Summer */
103 {"CEST", -120}, /* Central European Summer */
104 {"MESZ", -60}, /* Middle European Summer */
105 {"FWT", -60}, /* French Winter */
106 {"FST", -60}, /* French Summer */
107 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
108 {"WAST", -420}, /* West Australian Standard */
109 {"WADT", -420}, /* West Australian Daylight */
110 {"CCT", -480}, /* China Coast, USSR Zone 7 */
111 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
112 {"EAST", -600}, /* Eastern Australian Standard */
113 {"EADT", -600}, /* Eastern Australian Daylight */
114 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
115 {"NZT", -720}, /* New Zealand */
116 {"NZST", -720}, /* New Zealand Standard */
117 {"NZDT", -720}, /* New Zealand Daylight */
118 {"IDLE", -720}, /* International Date Line East */
124 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
125 #define http_etag(e, p, l, m) _http_etag((e), (p), (l), (m) TSRMLS_CC)
126 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
);
127 #define http_is_range_request() _http_is_range_request(TSRMLS_C)
128 static inline int _http_is_range_request(TSRMLS_D
);
129 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
130 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
132 static int check_day(char *day
, size_t len
);
133 static int check_month(char *month
);
134 static int check_tzone(char *tzone
);
137 #if defined(HAVE_CURL) && HAVE_CURL
138 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
139 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
);
140 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
141 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
);
142 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
143 static inline void _http_curl_sizebuf(http_curlbuf_member member
, size_t len TSRMLS_DC
);
144 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
145 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
146 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
147 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
, size_t *data_len TSRMLS_DC
);
148 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
149 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
);
151 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
152 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
153 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
154 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...);
156 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
157 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
161 /* {{{ static int http_sort_q(const void *, const void *) */
162 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
165 zval result
, *first
, *second
;
167 f
= *((Bucket
**) a
);
168 s
= *((Bucket
**) b
);
170 first
= *((zval
**) f
->pData
);
171 second
= *((zval
**) s
->pData
);
173 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
176 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
180 /* {{{ static inline char *http_etag(char **, void *, size_t, http_send_mode) */
181 static inline char *_http_etag(char **new_etag
, const void *data_ptr
,
182 const size_t data_len
, const http_send_mode data_mode TSRMLS_DC
)
184 char ssb_buf
[127], digest
[16];
192 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
196 snprintf(ssb_buf
, 127, "%l=%l=%l",
197 HTTP_G(ssb
).sb
.st_mtime
,
198 HTTP_G(ssb
).sb
.st_ino
,
199 HTTP_G(ssb
).sb
.st_size
201 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
209 PHP_MD5Final(digest
, &ctx
);
210 make_digest(*new_etag
, digest
);
216 /* {{{ static inline int http_is_range_request(void) */
217 static inline int _http_is_range_request(TSRMLS_D
)
219 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
220 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
224 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
226 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
227 const size_t end
, const http_send_mode mode TSRMLS_DC
)
231 long len
= end
- begin
;
237 s
= (php_stream
*) data
;
238 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
241 buf
= (char *) ecalloc(1, HTTP_BUF_SIZE
);
242 /* read into buf and write out */
243 while ((len
-= HTTP_BUF_SIZE
) >= 0) {
244 if (!(read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
))) {
248 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
254 /* read & write left over */
256 if (read
= php_stream_read(s
, buf
, HTTP_BUF_SIZE
+ len
)) {
257 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
271 return len
== php_body_write(
272 Z_STRVAL_P((zval
*) data
) + begin
, len TSRMLS_CC
)
284 #if defined(HAVE_CURL) && HAVE_CURL
286 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
287 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC
)
289 http_curl_freebuf(member
);
291 if (member
& CURLBUF_HDRS
) {
292 HTTP_G(curlbuf
).hdrs
.data
= emalloc(HTTP_CURLBUF_HDRSSIZE
);
293 HTTP_G(curlbuf
).hdrs
.free
= HTTP_CURLBUF_HDRSSIZE
;
295 if (member
& CURLBUF_BODY
) {
296 HTTP_G(curlbuf
).body
.data
= emalloc(HTTP_CURLBUF_BODYSIZE
);
297 HTTP_G(curlbuf
).body
.free
= HTTP_CURLBUF_BODYSIZE
;
302 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
303 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC
)
305 if (member
& CURLBUF_HDRS
) {
306 if (HTTP_G(curlbuf
).hdrs
.data
) {
307 efree(HTTP_G(curlbuf
).hdrs
.data
);
308 HTTP_G(curlbuf
).hdrs
.data
= NULL
;
310 HTTP_G(curlbuf
).hdrs
.used
= 0;
311 HTTP_G(curlbuf
).hdrs
.free
= 0;
313 if (member
& CURLBUF_BODY
) {
314 if (HTTP_G(curlbuf
).body
.data
) {
315 efree(HTTP_G(curlbuf
).body
.data
);
316 HTTP_G(curlbuf
).body
.data
= NULL
;
318 HTTP_G(curlbuf
).body
.used
= 0;
319 HTTP_G(curlbuf
).body
.free
= 0;
324 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
326 static inline void _http_curl_copybuf(http_curlbuf_member member
, char **data
,
327 size_t *data_len TSRMLS_DC
)
332 if ((member
& CURLBUF_HDRS
) && HTTP_G(curlbuf
).hdrs
.used
) {
333 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
334 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
+ 1);
336 *data
= emalloc(HTTP_G(curlbuf
).hdrs
.used
+ 1);
338 memcpy(*data
, HTTP_G(curlbuf
).hdrs
.data
, HTTP_G(curlbuf
).hdrs
.used
);
339 *data_len
= HTTP_G(curlbuf
).hdrs
.used
;
342 if ((member
& CURLBUF_BODY
) && HTTP_G(curlbuf
).body
.used
) {
344 memcpy((*data
) + HTTP_G(curlbuf
).hdrs
.used
,
345 HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
346 *data_len
= HTTP_G(curlbuf
).hdrs
.used
+ HTTP_G(curlbuf
).body
.used
;
348 emalloc(HTTP_G(curlbuf
).body
.used
+ 1);
349 memcpy(*data
, HTTP_G(curlbuf
).body
.data
, HTTP_G(curlbuf
).body
.used
);
350 *data_len
= HTTP_G(curlbuf
).body
.used
;
354 (*data
)[*data_len
] = 0;
361 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
363 static inline void _http_curl_movebuf(http_curlbuf_member member
, char **data
,
364 size_t *data_len TSRMLS_DC
)
366 http_curl_copybuf(member
, data
, data_len
);
367 http_curl_freebuf(member
);
371 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
372 static size_t http_curl_body_callback(char *buf
, size_t len
, size_t n
, void *s
)
376 if ((len
*= n
) > HTTP_G(curlbuf
).body
.free
) {
377 size_t bsize
= HTTP_CURLBUF_BODYSIZE
;
378 while (bsize
< len
) {
381 HTTP_G(curlbuf
).body
.data
= erealloc(HTTP_G(curlbuf
).body
.data
,
382 HTTP_G(curlbuf
).body
.used
+ bsize
);
383 HTTP_G(curlbuf
).body
.free
+= bsize
;
386 memcpy(HTTP_G(curlbuf
).body
.data
+ HTTP_G(curlbuf
).body
.used
, buf
, len
);
387 HTTP_G(curlbuf
).body
.free
-= len
;
388 HTTP_G(curlbuf
).body
.used
+= len
;
394 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
395 static size_t http_curl_hdrs_callback(char *buf
, size_t len
, size_t n
, void *s
)
399 /* discard previous headers */
400 if ((HTTP_G(curlbuf
).hdrs
.used
) && (!strncmp(buf
, "HTTP/1.", strlen("HTTP/1.")))) {
401 http_curl_initbuf(CURLBUF_HDRS
);
404 if ((len
*= n
) > HTTP_G(curlbuf
).hdrs
.free
) {
405 size_t bsize
= HTTP_CURLBUF_HDRSSIZE
;
406 while (bsize
< len
) {
409 HTTP_G(curlbuf
).hdrs
.data
= erealloc(HTTP_G(curlbuf
).hdrs
.data
,
410 HTTP_G(curlbuf
).hdrs
.used
+ bsize
);
411 HTTP_G(curlbuf
).hdrs
.free
+= bsize
;
414 memcpy(HTTP_G(curlbuf
).hdrs
.data
+ HTTP_G(curlbuf
).hdrs
.used
, buf
, len
);
415 HTTP_G(curlbuf
).hdrs
.free
-= len
;
416 HTTP_G(curlbuf
).hdrs
.used
+= len
;
422 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
423 static inline zval
*_http_curl_getopt(HashTable
*options
, char *key TSRMLS_DC
, int checks
, ...)
429 if (SUCCESS
!= zend_hash_find(options
, key
, strlen(key
) + 1, (void **) &zoption
)) {
436 va_start(types
, checks
);
437 for (i
= 0; i
< checks
; ++i
) {
438 if ((va_arg(types
, int)) == (Z_TYPE_PP(zoption
))) {
448 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
449 static inline void _http_curl_setopts(CURL
*ch
, const char *url
, HashTable
*options TSRMLS_DC
)
453 /* standard options */
454 curl_easy_setopt(ch
, CURLOPT_URL
, url
);
455 curl_easy_setopt(ch
, CURLOPT_HEADER
, 0);
456 curl_easy_setopt(ch
, CURLOPT_NOPROGRESS
, 1);
457 curl_easy_setopt(ch
, CURLOPT_AUTOREFERER
, 1);
458 curl_easy_setopt(ch
, CURLOPT_WRITEFUNCTION
, http_curl_body_callback
);
459 curl_easy_setopt(ch
, CURLOPT_HEADERFUNCTION
, http_curl_hdrs_callback
);
461 curl_easy_setopt(ch
, CURLOPT_NOSIGNAL
, 1);
463 #if defined(PHP_DEBUG)
464 curl_easy_setopt(ch
, CURLOPT_VERBOSE
, 1);
467 if ((!options
) || (1 > zend_hash_num_elements(options
))) {
472 if (zoption
= http_curl_getopt1(options
, "redirect", IS_LONG
)) {
473 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1 : 0);
474 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, Z_LVAL_P(zoption
));
475 if (zoption
= http_curl_getopt2(options
, "unrestrictedauth", IS_LONG
, IS_BOOL
)) {
476 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
479 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0);
483 if (zoption
= http_curl_getopt1(options
, "proxyhost", IS_STRING
)) {
484 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
486 if (zoption
= http_curl_getopt1(options
, "proxyport", IS_LONG
)) {
487 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
490 if (zoption
= http_curl_getopt1(options
, "proxyauth", IS_STRING
)) {
491 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
494 if (zoption
= http_curl_getopt1(options
, "proxyauthtype", IS_LONG
)) {
495 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
500 if (zoption
= http_curl_getopt1(options
, "httpauth", IS_STRING
)) {
501 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
503 if (zoption
= http_curl_getopt1(options
, "httpauthtype", IS_LONG
)) {
504 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
508 if (zoption
= http_curl_getopt2(options
, "compress", IS_LONG
, IS_BOOL
)) {
509 /* empty string enables deflate and gzip */
510 curl_easy_setopt(ch
, CURLOPT_ENCODING
, "");
514 if (zoption
= http_curl_getopt1(options
, "port", IS_LONG
)) {
515 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
519 if (zoption
= http_curl_getopt1(options
, "referer", IS_STRING
)) {
520 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
524 if (zoption
= http_curl_getopt1(options
, "useragent", IS_STRING
)) {
525 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
529 if (zoption
= http_curl_getopt1(options
, "cookies", IS_ARRAY
)) {
531 ZVAL_STRINGL(&glue
, "; ", 2, 0);
532 php_implode(&glue
, zoption
, &cookies
);
533 if (Z_STRVAL(cookies
)) {
534 curl_easy_setopt(ch
, CURLOPT_COOKIE
, Z_STRVAL(cookies
));
539 if (zoption
= http_curl_getopt1(options
, "cookiestore", IS_STRING
)) {
540 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, Z_STRVAL_P(zoption
));
541 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, Z_STRVAL_P(zoption
));
544 /* additional headers */
545 if (zoption
= http_curl_getopt1(options
, "headers", IS_ARRAY
)) {
547 struct curl_slist
*headers
= NULL
;
548 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
549 while (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void **) &header
)) {
550 headers
= curl_slist_append(headers
, Z_STRVAL_PP(header
));
551 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
554 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, headers
);
563 static int check_day(char *day
, size_t len
)
566 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
567 for (i
= 0; i
< 7; i
++) {
568 if (!strcmp(day
, check
[0])) {
576 static int check_month(char *month
)
579 const char * const *check
= &months
[0];
580 for (i
= 0; i
< 12; i
++) {
581 if (!strcmp(month
, check
[0])) {
589 /* return the time zone offset between GMT and the input one, in number
590 of seconds or -1 if the timezone wasn't found/legal */
592 static int check_tzone(char *tzone
)
595 const struct time_zone
*check
= time_zones
;
596 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
597 if (!strcmp(tzone
, check
->name
)) {
598 return check
->offset
* 60;
609 /* {{{ char *http_date(time_t) */
610 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
612 struct tm
*gmtime
, tmbuf
;
613 char *date
= ecalloc(1, 30);
615 gmtime
= php_gmtime_r(&t
, &tmbuf
);
617 "%s, %02d %s %04d %02d:%02d:%02d GMT",
618 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
619 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
620 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
626 /* time_t http_parse_date(char *)
627 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
628 PHP_HTTP_API
time_t http_parse_date(const char *date
)
631 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
632 hours
= -1, minutes
= -1, seconds
= -1;
634 enum assume_next dignext
= DATE_MDAY
;
635 const char *indate
= date
;
637 int found
= 0, part
= 0; /* max 6 parts */
639 while (*date
&& (part
< 6)) {
642 while (*date
&& !isalnum(*date
)) {
646 if (isalpha(*date
)) {
647 /* a name coming up */
650 sscanf(date
, "%31[A-Za-z]", buf
);
654 weekday
= check_day(buf
, len
);
660 if (!found
&& (month
== -1)) {
661 month
= check_month(buf
);
667 if (!found
&& (tz_offset
== -1)) {
668 /* this just must be a time zone string */
669 tz_offset
= check_tzone(buf
);
670 if (tz_offset
!= -1) {
676 return -1; /* bad string */
680 else if (isdigit(*date
)) {
684 if((seconds
== -1) && (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
690 val
= (int) strtol(date
, &end
, 10);
692 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) && (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
693 /* four digits and a value less than 1300 and it is preceeded with
694 a plus or minus. This is a time zone indication. */
696 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
698 /* the + and - prefix indicates the local time compared to GMT,
699 this we need ther reversed math to get what we want */
700 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
703 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
704 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
707 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
708 monthday
= val
% 100;
711 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
712 if ((val
> 0) && (val
< 32)) {
719 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
723 year
+= year
> 70 ? 1900 : 2000;
742 seconds
= minutes
= hours
= 0; /* no time, make it zero */
745 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
746 /* lacks vital info, fail */
750 if (sizeof(time_t) < 5) {
751 /* 32 bit time_t can only hold dates to the beginning of 2038 */
760 tm
.tm_mday
= monthday
;
762 tm
.tm_year
= year
- 1900;
769 /* time zone adjust */
771 struct tm
*gmt
, keeptime2
;
775 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
776 return -1; /* illegal date/time */
781 /* Add the time zone diff (between the given timezone and GMT) and the
782 diff between the local time zone and GMT. */
783 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
785 if((delta
> 0) && (t
+ delta
< t
)) {
786 return -1; /* time_t overflow */
796 /* {{{ inline STATUS http_send_status(int) */
797 PHP_HTTP_API
inline STATUS
_http_send_status(const int status TSRMLS_DC
)
800 return sapi_header_op(SAPI_HEADER_SET_STATUS
, (void *) s TSRMLS_CC
);
804 /* {{{ inline STATUS http_send_header(char *) */
805 PHP_HTTP_API
inline STATUS
_http_send_header(const char *header TSRMLS_DC
)
807 return http_send_status_header(0, header
);
811 /* {{{ inline STATUS http_send_status_header(int, char *) */
812 PHP_HTTP_API
inline STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
814 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
815 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
819 /* {{{ inline zval *http_get_server_var(char *) */
820 PHP_HTTP_API
inline zval
*_http_get_server_var(const char *key TSRMLS_DC
)
823 if (SUCCESS
== zend_hash_find(
824 Z_ARRVAL_P(PG(http_globals
)[TRACK_VARS_SERVER
]),
825 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
832 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
833 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
834 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
836 char etag
[33] = { 0 };
837 unsigned char digest
[16];
839 if (mode
& PHP_OUTPUT_HANDLER_START
) {
840 PHP_MD5Init(&HTTP_G(etag_md5
));
843 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
845 if (mode
& PHP_OUTPUT_HANDLER_END
) {
846 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
847 make_digest(etag
, digest
);
849 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
850 http_send_status(304);
852 http_send_etag(etag
, 32);
856 *handled_output_len
= output_len
;
857 *handled_output
= estrndup(output
, output_len
);
861 /* {{{ int http_modified_match(char *, int) */
862 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
866 char *modified
, *chr_ptr
;
868 HTTP_GSC(zmodified
, entry
, 0);
870 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
871 if (chr_ptr
= strrchr(modified
, ';')) {
874 retval
= (t
<= http_parse_date(modified
));
880 /* {{{ int http_etag_match(char *, char *) */
881 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
887 HTTP_GSC(zetag
, entry
, 0);
889 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
893 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
894 sprintf(quoted_etag
, "\"%s\"", etag
);
896 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
897 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
899 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
907 /* {{{ STATUS http_send_last_modified(int) */
908 PHP_HTTP_API STATUS
_http_send_last_modified(const int t TSRMLS_DC
)
910 char modified
[96] = "Last-Modified: ", *date
;
912 strcat(modified
, date
);
914 return http_send_header(modified
);
918 /* {{{ static STATUS http_send_etag(char *, int) */
919 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
920 const int etag_len TSRMLS_DC
)
926 header_len
= strlen("ETag: \"\"") + etag_len
+ 1;
927 etag_header
= (char *) emalloc(header_len
);
928 snprintf(etag_header
, header_len
, "ETag: \"%s\"", etag
);
929 ret
= http_send_header(etag_header
);
935 /* {{{ char *http_absolute_uri(char *, char *) */
936 PHP_HTTP_API
char *_http_absolute_uri(const char *url
,
937 const char *proto TSRMLS_DC
)
939 char URI
[HTTP_URI_MAXLEN
+ 1], *PTR
, *proto_ptr
, *host
, *path
;
942 if (!url
|| !strlen(url
)) {
943 if (!SG(request_info
).request_uri
) {
946 url
= SG(request_info
).request_uri
;
948 /* Mess around with already absolute URIs */
949 else if (proto_ptr
= strstr(url
, "://")) {
950 if (!proto
|| !strncmp(url
, proto
, strlen(proto
))) {
953 snprintf(URI
, HTTP_URI_MAXLEN
, "%s%s", proto
, proto_ptr
+ 3);
958 /* protocol defaults to http */
959 if (!proto
|| !strlen(proto
)) {
964 if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
965 (zhost
= http_get_server_var("SERVER_NAME"))) {
966 host
= Z_STRVAL_P(zhost
);
974 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s", proto
, host
, url
);
975 } else if (SG(request_info
).request_uri
) {
976 path
= estrdup(SG(request_info
).request_uri
);
977 php_dirname(path
, strlen(path
));
978 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s%s/%s", proto
, host
, path
, url
);
981 snprintf(URI
, HTTP_URI_MAXLEN
, "%s://%s/%s", proto
, host
, url
);
984 /* strip everything after a new line */
987 if (*PTR
== '\n' || *PTR
== '\r') {
998 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
999 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1000 const char *def TSRMLS_DC
)
1002 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1003 char *q_ptr
, *result
;
1007 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1009 MAKE_STD_ZVAL(zarray
);
1012 MAKE_STD_ZVAL(zdelim
);
1013 ZVAL_STRING(zdelim
, ",", 0);
1014 php_explode(zdelim
, zaccept
, zarray
, -1);
1017 MAKE_STD_ZVAL(zentries
);
1018 array_init(zentries
);
1020 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1021 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1023 if (SUCCESS
!= zend_hash_get_current_data(
1024 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1025 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1026 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1030 /* check for qualifier */
1031 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1032 qual
= strtod(q_ptr
+ 3, NULL
);
1037 /* walk through the supported array */
1038 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported
));
1039 SUCCESS
== zend_hash_get_current_data(
1040 Z_ARRVAL_P(supported
), (void **) &zsupp
);
1041 zend_hash_move_forward(Z_ARRVAL_P(supported
))) {
1042 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1043 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1052 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1054 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1055 http_sort_q
, 0 TSRMLS_CC
)) ||
1056 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1057 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1058 result
= estrdup(def
);
1061 zval_dtor(zentries
);
1068 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1069 PHP_HTTP_API http_range_status
_http_get_request_ranges(zval
*zranges
,
1070 const size_t length TSRMLS_DC
)
1072 zval
*zrange
, *zentry
, **zentries
;
1074 long begin
= -1, end
= -1, *ptr
;
1076 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1077 range
= Z_STRVAL_P(zrange
);
1079 if (strncmp(range
, "bytes=", strlen("bytes="))) {
1080 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1085 range
+= strlen("bytes=");
1088 switch (c
= *(range
++))
1094 case '1': case '2': case '3':
1095 case '4': case '5': case '6':
1096 case '7': case '8': case '9':
1098 * If the value of the pointer is already set (non-negative)
1099 * then multiply its value by ten and add the current value,
1100 * else initialise the pointers value with the current value
1102 * This let us recognize empty fields when validating the
1103 * ranges, i.e. a "-10" for begin and "12345" for the end
1104 * was the following range request: "Range: bytes=0-12345";
1105 * While a "-1" for begin and "12345" for the end would
1106 * have been: "Range: bytes=-12345".
1124 /* validate ranges */
1129 if ((length
- end
) < 1) {
1137 if ((length
- end
) < 1) {
1140 begin
= length
- end
;
1150 if ((length
- begin
) < 1) {
1158 if ( ((length
- begin
) < 1) ||
1159 ((length
- end
) < 1) ||
1160 ((begin
- end
) >= 0)) {
1169 zentry
= (zval
*) zentries
++;
1170 MAKE_STD_ZVAL(zentry
);
1172 add_index_long(zentry
, 0, begin
);
1173 add_index_long(zentry
, 1, end
);
1174 add_next_index_zval(zranges
, zentry
);
1191 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1192 PHP_HTTP_API STATUS
_http_send_ranges(zval
*zranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1195 long b
, e
, **begin
, **end
;
1196 char range_header
[255], multi_header
[68] = "Content-Type: multipart/byteranges; boundary=", bound
[23], preface
[1024];
1199 /* Send HTTP 206 Partial Content */
1200 http_send_status(206);
1203 if ((c
= zend_hash_num_elements(Z_ARRVAL_P(zranges
))) == 1) {
1204 zend_hash_index_find(Z_ARRVAL_P(zranges
), 0, (void **) &zrange
);
1205 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
);
1206 zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
);
1208 /* so zranges can be freed */
1216 /* send content range header */
1217 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", b
, e
, size
);
1218 http_send_header(range_header
);
1220 /* send requested chunk */
1221 return http_send_chunk(data
, b
, e
+ 1, mode
);
1226 snprintf(bound
, 23, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1227 strncat(multi_header
, bound
+ 2, 21);
1228 http_send_header(multi_header
);
1230 /* send each requested chunk */
1231 for ( i
= 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges
));
1233 i
++, zend_hash_move_forward(Z_ARRVAL_P(zranges
))) {
1234 if ( HASH_KEY_NON_EXISTANT
== zend_hash_get_current_data(
1235 Z_ARRVAL_P(zranges
), (void **) &zrange
) ||
1236 SUCCESS
!= zend_hash_index_find(
1237 Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1238 SUCCESS
!= zend_hash_index_find(
1239 Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1243 snprintf(preface
, 1024,
1244 "\r\n%s\r\nContent-Type: %s\r\nContent-Range: bytes %d-%d/%d\r\n\r\n", bound
,
1245 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1246 **begin
, **end
, size
);
1247 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1248 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1256 /* write boundary once more */
1257 php_body_write("\n", 1 TSRMLS_CC
);
1258 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1264 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1265 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1266 const http_send_mode data_mode TSRMLS_DC
)
1268 zval
*zranges
= NULL
;
1274 /* never ever use the output to compute the ETag if http_send() is used */
1275 if (HTTP_G(etag_started
)) {
1276 char *new_etag
= (char *) emalloc(33);
1278 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1279 if (NULL
== http_etag(&new_etag
, data_ptr
, data_size
, data_mode
)) {
1284 /* send 304 Not Modified if etag matches */
1285 if (http_etag_match("HTTP_IF_NONE_MATCH", new_etag
)) {
1287 return http_send_status(304);
1290 http_send_etag(new_etag
, 32);
1294 if (http_is_range_request()) {
1296 MAKE_STD_ZVAL(zranges
);
1297 array_init(zranges
);
1299 switch (http_get_request_ranges(zranges
, data_size
))
1304 /* go ahead and send all */
1308 return http_send_ranges(zranges
, data_ptr
, data_size
, data_mode
);
1314 http_send_status(416);
1324 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1328 /* {{{ STATUS http_send_data(zval *) */
1329 PHP_HTTP_API STATUS
_http_send_data(const zval
*zdata TSRMLS_DC
)
1331 if (!Z_STRLEN_P(zdata
)) {
1334 if (!Z_STRVAL_P(zdata
)) {
1338 return http_send((void *) zdata
, Z_STRLEN_P(zdata
), SEND_DATA
);
1342 /* {{{ STATUS http_send_stream(php_stream *) */
1343 PHP_HTTP_API STATUS
_http_send_stream(const php_stream
*file TSRMLS_DC
)
1345 if (php_stream_stat((php_stream
*) file
, &HTTP_G(ssb
))) {
1349 return http_send((void *) file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1353 /* {{{ STATUS http_send_file(zval *) */
1354 PHP_HTTP_API STATUS
_http_send_file(const zval
*zfile TSRMLS_DC
)
1359 if (!Z_STRLEN_P(zfile
)) {
1363 if (!(file
= php_stream_open_wrapper(Z_STRVAL_P(zfile
), "rb",
1364 REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
))) {
1368 ret
= http_send_stream(file
);
1369 php_stream_close(file
);
1374 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1375 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1376 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1382 *decoded
= (char *) ecalloc(1, encoded_len
);
1386 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1387 char hex_len
[9] = {0};
1388 size_t chunk_len
= 0;
1391 /* read in chunk size */
1392 while (isxdigit(*e_ptr
)) {
1394 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1395 "Chunk size is too long: 0x%s...", hex_len
);
1399 hex_len
[i
++] = *e_ptr
++;
1403 if (strcmp(hex_len
, "0")) {
1405 chunk_len
= strtol(hex_len
, &error
, 16);
1406 if (error
== hex_len
) {
1407 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1408 "Invalid chunk size string: '%s'", hex_len
);
1417 if (*e_ptr
++ != '\r' || *e_ptr
++ != '\n') {
1418 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1419 "Invalid character (expected 0x0A, 0x0D; got: %x)",
1425 memcpy(d_ptr
, e_ptr
, chunk_len
);
1427 e_ptr
+= chunk_len
+ 2;
1428 *decoded_len
+= chunk_len
;
1435 /* {{{ proto void http_split_response(zval *, zval *, zval *) */
1436 PHP_HTTP_API
void _http_split_response(const zval
*zresponse
, zval
*zheaders
,
1437 zval
*zbody TSRMLS_DC
)
1439 char *header
, *response
, *body
= NULL
;
1440 long response_len
= Z_STRLEN_P(zresponse
);
1441 header
= response
= Z_STRVAL_P(zresponse
);
1443 while ((response
- Z_STRVAL_P(zresponse
) + 3) < response_len
) {
1444 if ( (*response
++ == '\r') &&
1445 (*response
++ == '\n') &&
1446 (*response
++ == '\r') &&
1447 (*response
++ == '\n')) {
1453 if (body
&& (response_len
- (body
- header
))) {
1454 ZVAL_STRINGL(zbody
, body
, response_len
- (body
- header
) - 1, 1);
1456 Z_TYPE_P(zbody
) = IS_NULL
;
1459 /* check for HTTP status - FIXXME: strchr() */
1460 if (!strncmp(header
, "HTTP/1.", 7)) {
1461 char *end
= strchr(header
, '\r');
1462 add_assoc_stringl(zheaders
, "Status",
1463 header
+ strlen("HTTP/1.x "),
1464 end
- (header
+ strlen("HTTP/1.x ")), 1);
1469 char *colon
= NULL
, *line
= header
;
1471 while ( (line
- Z_STRVAL_P(zresponse
) + 3) <
1472 (body
- Z_STRVAL_P(zresponse
))) {
1476 if (colon
&& (*line
== '\n')) {
1477 char *key
= estrndup(header
, colon
- header
);
1478 add_assoc_stringl(zheaders
, key
,
1479 colon
+ 2, line
- colon
- 3, 1);
1483 header
+= line
- header
+ 1;
1499 #if defined(HAVE_CURL) && HAVE_CURL
1501 /* {{{ STATUS http_get(char *, HashTable *, char **, size_t *) */
1502 PHP_HTTP_API STATUS
_http_get(const char *URL
, HashTable
*options
,
1503 char **data
, size_t *data_len TSRMLS_DC
)
1505 CURL
*ch
= curl_easy_init();
1508 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1512 http_curl_initbuf(CURLBUF_EVRY
);
1513 http_curl_setopts(ch
, URL
, options
);
1515 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1516 curl_easy_cleanup(ch
);
1517 http_curl_freebuf(CURLBUF_EVRY
);
1518 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1521 curl_easy_cleanup(ch
);
1523 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1529 /* {{{ STATUS http_head(char *, HashTable *, char **data, size_t *) */
1530 PHP_HTTP_API STATUS
_http_head(const char *URL
, HashTable
*options
,
1531 char **data
, size_t *data_len TSRMLS_DC
)
1533 CURL
*ch
= curl_easy_init();
1536 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1540 http_curl_initbuf(CURLBUF_HDRS
);
1541 http_curl_setopts(ch
, URL
, options
);
1542 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 1);
1544 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1545 curl_easy_cleanup(ch
);
1546 http_curl_freebuf(CURLBUF_HDRS
);
1547 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1550 curl_easy_cleanup(ch
);
1552 http_curl_movebuf(CURLBUF_HDRS
, data
, data_len
);
1558 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, char **, size_t *) */
1559 PHP_HTTP_API STATUS
_http_post_data(const char *URL
, char *postdata
,
1560 size_t postdata_len
, HashTable
*options
, char **data
,
1561 size_t *data_len TSRMLS_DC
)
1563 CURL
*ch
= curl_easy_init();
1566 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not initialize curl");
1570 http_curl_initbuf(CURLBUF_EVRY
);
1571 http_curl_setopts(ch
, URL
, options
);
1572 curl_easy_setopt(ch
, CURLOPT_POST
, 1);
1573 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, postdata
);
1574 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, postdata_len
);
1576 if (CURLE_OK
!= curl_easy_perform(ch
)) {
1577 curl_easy_cleanup(ch
);
1578 http_curl_freebuf(CURLBUF_EVRY
);
1579 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not perform request");
1582 curl_easy_cleanup(ch
);
1584 http_curl_movebuf(CURLBUF_EVRY
, data
, data_len
);
1590 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, char **, size_t *) */
1591 PHP_HTTP_API STATUS
_http_post_array(const char *URL
, HashTable
*postarray
,
1592 HashTable
*options
, char **data
, size_t *data_len TSRMLS_DC
)
1594 smart_str qstr
= {0};
1597 if (php_url_encode_hash_ex(postarray
, &qstr
, NULL
,0,NULL
,0,NULL
,0,NULL TSRMLS_CC
) != SUCCESS
) {
1601 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not encode post data");
1606 status
= http_post_data(URL
, qstr
.c
, qstr
.len
, options
, data
, data_len
);
1617 /* {{{ void http_auth_header(char *, char*) */
1618 PHP_HTTP_API
void _http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1620 char realm_header
[1024];
1621 snprintf(realm_header
, 1024, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1622 http_send_status_header(401, realm_header
);
1626 /* {{{ STATUS http_auth_credentials(char **, char **) */
1627 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1629 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1630 zval
*zuser
, *zpass
;
1632 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1633 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1635 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1636 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1641 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1643 char *decoded
, *colon
;
1645 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1648 if (colon
= strchr(decoded
+ 6, ':')) {
1649 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1650 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1661 /* }}} public API */
1668 * vim600: noet sw=4 ts=4 fdm=marker
1669 * vim<600: noet sw=4 ts=4