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 +----------------------------------------------------------------------+
25 # include <winsock2.h>
26 #elif defined(HAVE_NETDB_H)
31 #include "php_version.h"
32 #include "php_streams.h"
34 #include "ext/standard/md5.h"
35 #include "ext/standard/url.h"
36 #include "ext/standard/base64.h"
37 #include "ext/standard/php_string.h"
38 #include "ext/standard/php_smart_str.h"
39 #include "ext/standard/php_lcg.h"
44 # include "ext/standard/php_http.h"
48 #include "php_http_api.h"
49 #include "php_http_std_defs.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_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
127 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
129 static int check_day(char *day
, size_t len
);
130 static int check_month(char *month
);
131 static int check_tzone(char *tzone
);
133 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
135 /* {{{ static int http_sort_q(const void *, const void *) */
136 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
139 zval result
, *first
, *second
;
141 f
= *((Bucket
**) a
);
142 s
= *((Bucket
**) b
);
144 first
= *((zval
**) f
->pData
);
145 second
= *((zval
**) s
->pData
);
147 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
150 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
154 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
156 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
157 const size_t end
, const http_send_mode mode TSRMLS_DC
)
161 long len
= end
- begin
;
167 s
= (php_stream
*) data
;
168 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
171 buf
= (char *) ecalloc(1, HTTP_SENDBUF_SIZE
);
172 /* read into buf and write out */
173 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
174 if (!(read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
))) {
178 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
184 /* read & write left over */
186 if (read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
+ len
)) {
187 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
201 return len
== php_body_write(((char *)data
) + begin
, len TSRMLS_CC
)
212 /* {{{ Day/Month/TZ checks for http_parse_date()
213 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
214 static int check_day(char *day
, size_t len
)
217 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
218 for (i
= 0; i
< 7; i
++) {
219 if (!strcmp(day
, check
[0])) {
227 static int check_month(char *month
)
230 const char * const *check
= &months
[0];
231 for (i
= 0; i
< 12; i
++) {
232 if (!strcmp(month
, check
[0])) {
240 /* return the time zone offset between GMT and the input one, in number
241 of seconds or -1 if the timezone wasn't found/legal */
243 static int check_tzone(char *tzone
)
246 const struct time_zone
*check
= time_zones
;
247 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
248 if (!strcmp(tzone
, check
->name
)) {
249 return check
->offset
* 60;
257 /* char *pretty_key(char *, int, int, int) */
258 char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
)
260 if (key
&& key_len
) {
262 if (wasalpha
= isalpha(key
[0])) {
263 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
265 for (i
= 1; i
< key_len
; i
++) {
266 if (isalpha(key
[i
])) {
267 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
270 if (xhyphen
&& (key
[i
] == '_')) {
281 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
282 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
285 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
286 b
->handler_name
= estrdup(o
->handler_name
);
287 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
288 b
->text_length
= o
->text_length
;
289 b
->chunk_size
= o
->chunk_size
;
300 /* {{{ char *http_date(time_t) */
301 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
303 struct tm
*gmtime
, tmbuf
;
305 if (gmtime
= php_gmtime_r(&t
, &tmbuf
)) {
306 char *date
= ecalloc(1, 31);
308 "%s, %02d %s %04d %02d:%02d:%02d GMT",
309 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
310 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
311 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
320 /* {{{ time_t http_parse_date(char *)
321 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
322 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
325 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
326 hours
= -1, minutes
= -1, seconds
= -1;
328 enum assume_next dignext
= DATE_MDAY
;
329 const char *indate
= date
;
331 int found
= 0, part
= 0; /* max 6 parts */
333 while (*date
&& (part
< 6)) {
336 while (*date
&& !isalnum(*date
)) {
340 if (isalpha(*date
)) {
341 /* a name coming up */
344 sscanf(date
, "%31[A-Za-z]", buf
);
348 weekday
= check_day(buf
, len
);
354 if (!found
&& (month
== -1)) {
355 month
= check_month(buf
);
361 if (!found
&& (tz_offset
== -1)) {
362 /* this just must be a time zone string */
363 tz_offset
= check_tzone(buf
);
364 if (tz_offset
!= -1) {
370 return -1; /* bad string */
374 else if (isdigit(*date
)) {
378 if ((seconds
== -1) &&
379 (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
385 val
= (int) strtol(date
, &end
, 10);
387 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) &&
388 (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
389 /* four digits and a value less than 1300 and it is preceeded with
390 a plus or minus. This is a time zone indication. */
392 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
394 /* the + and - prefix indicates the local time compared to GMT,
395 this we need ther reversed math to get what we want */
396 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
399 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
400 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
403 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
404 monthday
= val
% 100;
407 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
408 if ((val
> 0) && (val
< 32)) {
415 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
419 year
+= year
> 70 ? 1900 : 2000;
438 seconds
= minutes
= hours
= 0; /* no time, make it zero */
441 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
442 /* lacks vital info, fail */
446 if (sizeof(time_t) < 5) {
447 /* 32 bit time_t can only hold dates to the beginning of 2038 */
456 tm
.tm_mday
= monthday
;
458 tm
.tm_year
= year
- 1900;
465 /* time zone adjust */
467 struct tm
*gmt
, keeptime2
;
471 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
472 return -1; /* illegal date/time */
477 /* Add the time zone diff (between the given timezone and GMT) and the
478 diff between the local time zone and GMT. */
479 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
481 if((delta
> 0) && (t
+ delta
< t
)) {
482 return -1; /* time_t overflow */
492 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
493 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, const size_t data_len
,
494 const http_send_mode data_mode TSRMLS_DC
)
496 char ssb_buf
[128] = {0};
497 unsigned char digest
[16];
499 char *new_etag
= ecalloc(1, 33);
506 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
510 if (!HTTP_G(ssb
).sb
.st_ino
) {
511 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
515 snprintf(ssb_buf
, 127, "%ld=%ld=%ld",
516 HTTP_G(ssb
).sb
.st_mtime
,
517 HTTP_G(ssb
).sb
.st_ino
,
518 HTTP_G(ssb
).sb
.st_size
520 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
529 PHP_MD5Final(digest
, &ctx
);
530 make_digest(new_etag
, digest
);
536 /* {{{ time_t http_lmod(void *, http_send_mode) */
537 PHP_HTTP_API
time_t _http_lmod(const void *data_ptr
, const http_send_mode data_mode TSRMLS_DC
)
548 if (!HTTP_G(ssb
).sb
.st_mtime
) {
549 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
553 return HTTP_G(ssb
).sb
.st_mtime
;
558 if (!HTTP_G(ssb
).sb
.st_mtime
) {
559 if(php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
))) {
563 return HTTP_G(ssb
).sb
.st_mtime
;
569 /* {{{ STATUS http_send_status_header(int, char *) */
570 PHP_HTTP_API STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
572 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
573 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
577 /* {{{ zval *http_get_server_var(char *) */
578 PHP_HTTP_API zval
*_http_get_server_var(const char *key TSRMLS_DC
)
581 if (SUCCESS
== zend_hash_find(
583 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
590 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
591 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
592 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
594 char etag
[33] = { 0 };
595 unsigned char digest
[16];
597 if (mode
& PHP_OUTPUT_HANDLER_START
) {
598 PHP_MD5Init(&HTTP_G(etag_md5
));
601 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
603 if (mode
& PHP_OUTPUT_HANDLER_END
) {
604 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
606 /* just do that if desired */
607 if (HTTP_G(etag_started
)) {
608 make_digest(etag
, digest
);
610 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
611 http_send_status(304);
613 http_send_etag(etag
, 32);
618 *handled_output_len
= output_len
;
619 *handled_output
= estrndup(output
, output_len
);
623 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
624 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
625 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
627 php_ob_buffer
**stack
;
630 if (count
= OG(ob_nesting_level
)) {
631 stack
= ecalloc(count
, sizeof(php_ob_buffer
*));
634 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
635 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
639 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
642 while (OG(ob_nesting_level
)) {
643 php_end_ob_buffer(0, 0 TSRMLS_CC
);
647 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
649 for (i
= 0; i
< count
; i
++) {
650 php_ob_buffer
*s
= stack
[i
];
651 if (strcmp(s
->handler_name
, "default output handler")) {
652 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
654 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
655 efree(s
->handler_name
);
667 /* {{{ int http_modified_match(char *, int) */
668 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
672 char *modified
, *chr_ptr
;
674 HTTP_GSC(zmodified
, entry
, 0);
676 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
677 if (chr_ptr
= strrchr(modified
, ';')) {
680 retval
= (t
<= http_parse_date(modified
));
686 /* {{{ int http_etag_match(char *, char *) */
687 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
693 HTTP_GSC(zetag
, entry
, 0);
695 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
699 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
700 sprintf(quoted_etag
, "\"%s\"", etag
);
702 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
703 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
705 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
712 /* {{{ STATUS http_send_last_modified(int) */
713 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
716 if (date
= http_date(t
)) {
717 char modified
[96] = "Last-Modified: ";
718 strcat(modified
, date
);
724 return http_send_header(modified
);
730 /* {{{ static STATUS http_send_etag(char *, int) */
731 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
732 const int etag_len TSRMLS_DC
)
738 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
739 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
747 HTTP_G(etag
) = estrdup(etag
);
749 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
750 sprintf(etag_header
, "ETag: \"%s\"", etag
);
751 if (SUCCESS
!= (status
= http_send_header(etag_header
))) {
752 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", etag_header
);
759 /* {{{ STATUS http_send_cache_control(char *, size_t) */
760 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
,
761 const size_t cc_len TSRMLS_DC
)
764 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
766 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
767 if (SUCCESS
!= (status
= http_send_header(cc_header
))) {
768 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
,
769 "Could not send '%s' header", cc_header
);
776 /* {{{ STATUS http_send_content_type(char *, size_t) */
777 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
,
778 const size_t ct_len TSRMLS_DC
)
783 if (!strchr(content_type
, '/')) {
784 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
785 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
790 /* remember for multiple ranges */
792 efree(HTTP_G(ctype
));
794 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
796 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
797 sprintf(ct_header
, "Content-Type: %s", content_type
);
799 if (SUCCESS
!= (status
= http_send_header(ct_header
))) {
800 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
801 "Couldn't send '%s' header", ct_header
);
808 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
809 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
,
810 const size_t f_len
, const int send_inline TSRMLS_DC
)
816 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
817 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
819 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
820 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
823 if (SUCCESS
!= (status
= http_send_header(cd_header
))) {
824 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", cd_header
);
831 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
832 PHP_HTTP_API STATUS
_http_cache_last_modified(const time_t last_modified
,
833 const time_t send_modified
, const char *cache_control
, const size_t cc_len TSRMLS_DC
)
836 http_send_cache_control(cache_control
, cc_len
);
839 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
840 if (SUCCESS
== http_send_status(304)) {
843 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
847 return http_send_last_modified(send_modified
);
851 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
852 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, const size_t etag_len
,
853 const char *cache_control
, const size_t cc_len TSRMLS_DC
)
856 http_send_cache_control(cache_control
, cc_len
);
860 http_send_etag(etag
, etag_len
);
861 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
862 if (SUCCESS
== http_send_status(304)) {
865 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
871 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
872 if (!HTTP_G(etag_started
)) {
873 if (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)) {
874 HTTP_G(etag_started
) = 1;
877 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
885 /* {{{ char *http_absolute_uri(char *) */
886 PHP_HTTP_API
char *_http_absolute_uri_ex(
887 const char *url
, size_t url_len
,
888 const char *proto
, size_t proto_len
,
889 const char *host
, size_t host_len
,
890 unsigned port TSRMLS_DC
)
892 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
895 php_url
*purl
, furl
= {NULL
};
898 char *scheme
= NULL
, *URL
= ecalloc(1, HTTP_URI_MAXLEN
+ 1);
900 if ((!url
|| !url_len
) && (
901 (!(url
= SG(request_info
).request_uri
)) ||
902 (!(url_len
= strlen(SG(request_info
).request_uri
))))) {
903 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
904 "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
908 if (!(purl
= php_url_parse(url
))) {
909 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse supplied URL");
913 furl
.user
= purl
->user
;
914 furl
.pass
= purl
->pass
;
915 furl
.path
= purl
->path
;
916 furl
.query
= purl
->query
;
917 furl
.fragment
= purl
->fragment
;
920 furl
.scheme
= scheme
= estrdup(proto
);
921 } else if (purl
->scheme
) {
922 furl
.scheme
= purl
->scheme
;
923 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
924 } else if (port
&& (se
= getservbyport(port
, "tcp"))) {
925 furl
.scheme
= (scheme
= estrdup(se
->s_name
));
928 furl
.scheme
= "http";
933 } else if (purl
->port
) {
934 furl
.port
= purl
->port
;
935 } else if (strncmp(furl
.scheme
, "http", 4)) {
936 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
937 if (se
= getservbyname(furl
.scheme
, "tcp")) {
938 furl
.port
= se
->s_port
;
943 furl
.port
= (furl
.scheme
[5] == 's') ? 443 : 80;
947 furl
.host
= (char *) host
;
948 } else if (purl
->host
) {
949 furl
.host
= purl
->host
;
950 } else if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
951 (zhost
= http_get_server_var("SERVER_NAME"))) {
952 furl
.host
= Z_STRVAL_P(zhost
);
954 furl
.host
= "localhost";
957 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
958 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
959 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
960 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
961 php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
962 "Absolute URI would have exceeded max URI length (%d bytes) - " \
963 "tried to add %d bytes ('%s')", \
964 HTTP_URI_MAXLEN, add_len, add_string); \
968 php_url_free(purl); \
971 strcat(URL, add_string); \
974 HTTP_URI_STRLCATL(URL
, full_len
, furl
.scheme
);
975 HTTP_URI_STRLCATS(URL
, full_len
, "://");
978 HTTP_URI_STRLCATL(URL
, full_len
, furl
.user
);
980 HTTP_URI_STRLCATS(URL
, full_len
, ":");
981 HTTP_URI_STRLCATL(URL
, full_len
, furl
.pass
);
983 HTTP_URI_STRLCATS(URL
, full_len
, "@");
986 HTTP_URI_STRLCATL(URL
, full_len
, furl
.host
);
988 if ( (!strcmp(furl
.scheme
, "http") && (furl
.port
!= 80)) ||
989 (!strcmp(furl
.scheme
, "https") && (furl
.port
!= 443))) {
990 char port_string
[8] = {0};
991 snprintf(port_string
, 7, ":%u", furl
.port
);
992 HTTP_URI_STRLCATL(URL
, full_len
, port_string
);
996 if (furl
.path
[0] != '/') {
997 HTTP_URI_STRLCATS(URL
, full_len
, "/");
999 HTTP_URI_STRLCATL(URL
, full_len
, furl
.path
);
1001 HTTP_URI_STRLCATS(URL
, full_len
, "/");
1005 HTTP_URI_STRLCATS(URL
, full_len
, "?");
1006 HTTP_URI_STRLCATL(URL
, full_len
, furl
.query
);
1009 if (furl
.fragment
) {
1010 HTTP_URI_STRLCATS(URL
, full_len
, "#");
1011 HTTP_URI_STRLCATL(URL
, full_len
, furl
.fragment
);
1023 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1024 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1025 const char *def TSRMLS_DC
)
1027 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1028 char *q_ptr
, *result
;
1032 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1034 MAKE_STD_ZVAL(zarray
);
1037 MAKE_STD_ZVAL(zdelim
);
1038 ZVAL_STRING(zdelim
, ",", 0);
1039 php_explode(zdelim
, zaccept
, zarray
, -1);
1042 MAKE_STD_ZVAL(zentries
);
1043 array_init(zentries
);
1045 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1046 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1048 if (SUCCESS
!= zend_hash_get_current_data(
1049 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1050 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1051 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1055 /* check for qualifier */
1056 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1057 qual
= strtod(q_ptr
+ 3, NULL
);
1062 /* walk through the supported array */
1063 FOREACH_VAL(supported
, zsupp
) {
1064 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1065 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1074 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1076 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1077 http_sort_q
, 0 TSRMLS_CC
)) ||
1078 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1079 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1080 result
= estrdup(def
);
1083 zval_dtor(zentries
);
1090 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
1091 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
,
1092 const size_t length TSRMLS_DC
)
1096 long begin
= -1, end
= -1, *ptr
;
1098 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1099 range
= Z_STRVAL_P(zrange
);
1101 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
1102 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1107 range
+= sizeof("bytes=") - 1;
1110 switch (c
= *(range
++))
1116 case '1': case '2': case '3':
1117 case '4': case '5': case '6':
1118 case '7': case '8': case '9':
1120 * If the value of the pointer is already set (non-negative)
1121 * then multiply its value by ten and add the current value,
1122 * else initialise the pointers value with the current value
1124 * This let us recognize empty fields when validating the
1125 * ranges, i.e. a "-10" for begin and "12345" for the end
1126 * was the following range request: "Range: bytes=0-12345";
1127 * While a "-1" for begin and "12345" for the end would
1128 * have been: "Range: bytes=-12345".
1143 /* IE - ignore for now */
1150 /* validate ranges */
1155 if ((length
- end
) < 1) {
1163 if ((length
- end
) < 1) {
1166 begin
= length
- end
;
1176 if ((length
- begin
) < 1) {
1184 if ( ((length
- begin
) < 1) ||
1185 ((length
- end
) < 1) ||
1186 ((begin
- end
) >= 0)) {
1196 MAKE_STD_ZVAL(zentry
);
1198 add_index_long(zentry
, 0, begin
);
1199 add_index_long(zentry
, 1, end
);
1200 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
1218 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
1219 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1221 long **begin
, **end
;
1225 if (zend_hash_num_elements(ranges
) == 1) {
1226 char range_header
[256] = {0};
1228 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
1229 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1230 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1234 /* Send HTTP 206 Partial Content */
1235 http_send_status(206);
1237 /* send content range header */
1238 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1239 http_send_header(range_header
);
1241 /* send requested chunk */
1242 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1247 char bound
[23] = {0}, preface
[1024] = {0},
1248 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1250 /* Send HTTP 206 Partial Content */
1251 http_send_status(206);
1253 /* send multipart/byteranges header */
1254 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1255 strncat(multi_header
, bound
+ 2, 21);
1256 http_send_header(multi_header
);
1258 /* send each requested chunk */
1259 FOREACH_HASH_VAL(ranges
, zrange
) {
1260 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1261 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1265 snprintf(preface
, 1023,
1267 HTTP_CRLF
"Content-Type: %s"
1268 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1273 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1279 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1280 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1283 /* write boundary once more */
1284 php_body_write(HTTP_CRLF
, sizeof(HTTP_CRLF
) - 1 TSRMLS_CC
);
1285 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1286 php_body_write("--", 2 TSRMLS_CC
);
1293 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
1294 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1295 const http_send_mode data_mode TSRMLS_DC
)
1297 int is_range_request
= http_is_range_request();
1307 if (HTTP_G(etag_started
)) {
1310 HTTP_G(etag_started
) = 0;
1311 /* never ever use the output to compute the ETag if http_send() is used */
1312 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1313 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
1317 /* send 304 Not Modified if etag matches */
1318 if ((!is_range_request
) && http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1320 return http_send_status(304);
1323 http_send_etag(etag
, 32);
1327 /* send 304 Not Modified if last-modified matches*/
1328 if ((!is_range_request
) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1329 return http_send_status(304);
1332 if (is_range_request
) {
1334 /* only send ranges if entity hasn't changed */
1336 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_MATCH", 13)) ||
1337 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag
)))
1339 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1340 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
)))
1343 STATUS result
= FAILURE
;
1345 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1347 switch (http_get_request_ranges(&ranges
, data_size
))
1350 zend_hash_destroy(&ranges
);
1351 /* go ahead and send all */
1355 result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
1356 zend_hash_destroy(&ranges
);
1361 zend_hash_destroy(&ranges
);
1362 http_send_status(416);
1373 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1377 /* {{{ STATUS http_send_stream(php_stream *) */
1378 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
,
1379 zend_bool close_stream TSRMLS_DC
)
1383 if ((!file
) || php_stream_stat(file
, &HTTP_G(ssb
))) {
1387 status
= http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1390 php_stream_close(file
);
1397 /* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1398 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1399 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1405 *decoded
= ecalloc(1, encoded_len
);
1409 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1410 char hex_len
[9] = {0};
1411 size_t chunk_len
= 0;
1414 /* read in chunk size */
1415 while (isxdigit(*e_ptr
)) {
1417 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1418 "Chunk size is too long: 0x%s...", hex_len
);
1422 hex_len
[i
++] = *e_ptr
++;
1425 /* reached the end */
1426 if (!strcmp(hex_len
, "0")) {
1431 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1432 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1433 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1434 *e_ptr
, *(e_ptr
+ 1));
1442 chunk_len
= strtol(hex_len
, &error
, 16);
1443 if (error
== hex_len
) {
1444 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1445 "Invalid chunk size string: '%s'", hex_len
);
1451 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1453 e_ptr
+= chunk_len
+ 2;
1454 *decoded_len
+= chunk_len
;
1461 /* {{{ STATUS http_split_response(zval *, zval *, zval *) */
1462 PHP_HTTP_API STATUS
_http_split_response(zval
*response
, zval
*headers
, zval
*body TSRMLS_DC
)
1466 STATUS status
= http_split_response_ex(Z_STRVAL_P(response
), Z_STRLEN_P(response
), Z_ARRVAL_P(headers
), &b
, &l
);
1467 ZVAL_STRINGL(body
, b
, l
, 0);
1472 /* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
1473 PHP_HTTP_API STATUS
_http_split_response_ex(char *response
,
1474 size_t response_len
, HashTable
*headers
, char **body
, size_t *body_len TSRMLS_DC
)
1476 char *header
= response
, *real_body
= NULL
;
1478 while (0 < (response_len
- (response
- header
+ 4))) {
1479 if ( (*response
++ == '\r') &&
1480 (*response
++ == '\n') &&
1481 (*response
++ == '\r') &&
1482 (*response
++ == '\n')) {
1483 real_body
= response
;
1488 if (real_body
&& (*body_len
= (response_len
- (real_body
- header
)))) {
1489 *body
= ecalloc(1, *body_len
+ 1);
1490 memcpy(*body
, real_body
, *body_len
);
1493 return http_parse_headers_ex(header
, real_body
? response_len
- *body_len
: response_len
, headers
, 1);
1497 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1498 PHP_HTTP_API STATUS
_http_parse_headers_ex(char *header
, int header_len
,
1499 HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1501 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1504 Z_ARRVAL(array
) = headers
;
1506 if (header_len
< 2) {
1511 if (!strncmp(header
, "HTTP/1.", 7)) {
1512 char *end
= strstr(header
, HTTP_CRLF
);
1513 add_assoc_stringl(&array
, "Status",
1514 header
+ sizeof("HTTP/1.x ") - 1,
1515 end
- (header
+ sizeof("HTTP/1.x ") - 1), 1);
1521 while (header_len
>= (line
- begin
)) {
1527 --value_len
; /* we don't have CR so value length is one char less */
1529 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
1531 /* skip empty key */
1532 if (header
!= colon
) {
1533 char *key
= estrndup(header
, colon
- header
);
1536 key
= pretty_key(key
, colon
- header
, 1, 1);
1539 value_len
+= line
- colon
- 1;
1541 /* skip leading ws */
1542 while (isspace(*(++colon
))) --value_len
;
1543 /* skip trailing ws */
1544 while (isspace(colon
[value_len
- 1])) --value_len
;
1546 if (value_len
< 1) {
1547 /* hm, empty header? */
1548 add_assoc_stringl(&array
, key
, "", 0, 1);
1550 add_assoc_stringl(&array
, key
, colon
, value_len
, 1);
1557 header
+= line
- header
;
1572 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
1573 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1579 Z_ARRVAL(array
) = headers
;
1581 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
1582 if (key
&& !strncmp(key
, "HTTP_", 5)) {
1586 key
= pretty_key(key
+ 5, strlen(key
) - 5, 1, 1);
1589 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
1590 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1597 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, int, char **, size_t *) */
1598 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, int override_argsep
,
1599 char *pre_encoded_data
, size_t pre_encoded_len
,
1600 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
1602 smart_str qstr
= {0};
1604 if (override_argsep
) {
1605 HTTP_URL_ARGSEP_OVERRIDE
;
1608 if (pre_encoded_len
&& pre_encoded_data
) {
1609 smart_str_appendl(&qstr
, pre_encoded_data
, pre_encoded_len
);
1612 if (SUCCESS
!= php_url_encode_hash_ex(hash
, &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
1613 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
1617 if (override_argsep
) {
1618 HTTP_URL_ARGSEP_RESTORE
;
1623 if (override_argsep
) {
1624 HTTP_URL_ARGSEP_RESTORE
;
1629 *encoded_data
= qstr
.c
;
1631 *encoded_len
= qstr
.len
;
1638 /* {{{ STATUS http_auth_header(char *, char*) */
1639 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1641 char realm_header
[1024] = {0};
1642 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1643 return http_send_status_header(401, realm_header
);
1647 /* {{{ STATUS http_auth_credentials(char **, char **) */
1648 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1650 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1651 zval
*zuser
, *zpass
;
1653 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1654 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1656 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1657 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1662 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1664 char *decoded
, *colon
;
1666 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1669 if (colon
= strchr(decoded
+ 6, ':')) {
1670 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1671 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1682 #ifndef ZEND_ENGINE_2
1683 /* {{{ php_url_encode_hash
1684 Author: Sara Golemon <pollita@php.net> */
1685 PHP_HTTP_API STATUS
php_url_encode_hash_ex(HashTable
*ht
, smart_str
*formstr
,
1686 const char *num_prefix
, int num_prefix_len
,
1687 const char *key_prefix
, int key_prefix_len
,
1688 const char *key_suffix
, int key_suffix_len
,
1689 zval
*type TSRMLS_DC
)
1691 char *arg_sep
= NULL
, *key
= NULL
, *ekey
, *newprefix
, *p
;
1692 int arg_sep_len
, key_len
, ekey_len
, key_type
, newprefix_len
;
1694 zval
**zdata
= NULL
, *copyzval
;
1700 if (ht
->nApplyCount
> 0) {
1701 /* Prevent recursion */
1705 arg_sep
= INI_STR("arg_separator.output");
1706 if (!arg_sep
|| !strlen(arg_sep
)) {
1707 arg_sep
= HTTP_URL_ARGSEP_DEFAULT
;
1709 arg_sep_len
= strlen(arg_sep
);
1711 for (zend_hash_internal_pointer_reset(ht
);
1712 (key_type
= zend_hash_get_current_key_ex(ht
, &key
, &key_len
, &idx
, 0, NULL
)) != HASH_KEY_NON_EXISTANT
;
1713 zend_hash_move_forward(ht
)
1715 if (key_type
== HASH_KEY_IS_STRING
&& key_len
&& key
[key_len
-1] == '\0') {
1716 /* We don't want that trailing NULL */
1720 #ifdef ZEND_ENGINE_2
1721 /* handling for private & protected object properties */
1722 if (key
&& *key
== '\0' && type
!= NULL
) {
1725 zend_object
*zobj
= zend_objects_get_address(type TSRMLS_CC
);
1726 if (zend_check_property_access(zobj
, key TSRMLS_CC
) != SUCCESS
) {
1727 /* private or protected property access outside of the class */
1730 zend_unmangle_property_name(key
, &tmp
, &key
);
1731 key_len
= strlen(key
);
1735 if (zend_hash_get_current_data_ex(ht
, (void **)&zdata
, NULL
) == FAILURE
|| !zdata
|| !(*zdata
)) {
1736 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Error traversing form data array.");
1739 if (Z_TYPE_PP(zdata
) == IS_ARRAY
|| Z_TYPE_PP(zdata
) == IS_OBJECT
) {
1740 if (key_type
== HASH_KEY_IS_STRING
) {
1741 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1742 newprefix_len
= key_suffix_len
+ ekey_len
+ key_prefix_len
+ 1;
1743 newprefix
= emalloc(newprefix_len
+ 1);
1747 memcpy(p
, key_prefix
, key_prefix_len
);
1748 p
+= key_prefix_len
;
1751 memcpy(p
, ekey
, ekey_len
);
1756 memcpy(p
, key_suffix
, key_suffix_len
);
1757 p
+= key_suffix_len
;
1763 /* Is an integer key */
1764 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1765 newprefix_len
= key_prefix_len
+ num_prefix_len
+ ekey_len
+ key_suffix_len
+ 1;
1766 newprefix
= emalloc(newprefix_len
+ 1);
1770 memcpy(p
, key_prefix
, key_prefix_len
);
1771 p
+= key_prefix_len
;
1774 memcpy(p
, num_prefix
, num_prefix_len
);
1775 p
+= num_prefix_len
;
1777 memcpy(p
, ekey
, ekey_len
);
1782 memcpy(p
, key_suffix
, key_suffix_len
);
1783 p
+= key_suffix_len
;
1789 php_url_encode_hash_ex(HASH_OF(*zdata
), formstr
, NULL
, 0, newprefix
, newprefix_len
, "]", 1, (Z_TYPE_PP(zdata
) == IS_OBJECT
? *zdata
: NULL
) TSRMLS_CC
);
1792 } else if (Z_TYPE_PP(zdata
) == IS_NULL
|| Z_TYPE_PP(zdata
) == IS_RESOURCE
) {
1793 /* Skip these types */
1797 smart_str_appendl(formstr
, arg_sep
, arg_sep_len
);
1799 /* Simple key=value */
1800 smart_str_appendl(formstr
, key_prefix
, key_prefix_len
);
1801 if (key_type
== HASH_KEY_IS_STRING
) {
1802 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1803 smart_str_appendl(formstr
, ekey
, ekey_len
);
1808 smart_str_appendl(formstr
, num_prefix
, num_prefix_len
);
1810 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1811 smart_str_appendl(formstr
, ekey
, ekey_len
);
1814 smart_str_appendl(formstr
, key_suffix
, key_suffix_len
);
1815 smart_str_appendl(formstr
, "=", 1);
1816 switch (Z_TYPE_PP(zdata
)) {
1818 ekey
= php_url_encode(Z_STRVAL_PP(zdata
), Z_STRLEN_PP(zdata
), &ekey_len
);
1822 ekey_len
= spprintf(&ekey
, 12, "%ld", Z_LVAL_PP(zdata
));
1825 ekey_len
= spprintf(&ekey
, 48, "%.*G", (int) EG(precision
), Z_DVAL_PP(zdata
));
1828 /* fall back on convert to string */
1829 MAKE_STD_ZVAL(copyzval
);
1830 *copyzval
= **zdata
;
1831 zval_copy_ctor(copyzval
);
1832 convert_to_string_ex(©zval
);
1833 ekey
= php_url_encode(Z_STRVAL_P(copyzval
), Z_STRLEN_P(copyzval
), &ekey_len
);
1834 zval_ptr_dtor(©zval
);
1836 smart_str_appendl(formstr
, ekey
, ekey_len
);
1844 #endif /* !ZEND_ENDGINE_2 */
1846 /* }}} public API */
1853 * vim600: noet sw=4 ts=4 fdm=marker
1854 * vim<600: noet sw=4 ts=4