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 +----------------------------------------------------------------------+
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
28 #include "php_version.h"
29 #include "php_streams.h"
32 #include "ext/standard/md5.h"
33 #include "ext/standard/url.h"
34 #include "ext/standard/base64.h"
35 #include "ext/standard/php_string.h"
36 #include "ext/standard/php_smart_str.h"
37 #include "ext/standard/php_lcg.h"
42 # include "ext/standard/php_http.h"
46 #include "php_http_api.h"
47 #include "php_http_std_defs.h"
49 ZEND_DECLARE_MODULE_GLOBALS(http
)
51 /* {{{ day/month names */
52 static const char *days
[] = {
53 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
55 static const char *wkdays
[] = {
56 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
58 static const char *weekdays
[] = {
59 "Monday", "Tuesday", "Wednesday",
60 "Thursday", "Friday", "Saturday", "Sunday"
62 static const char *months
[] = {
63 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
64 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
71 static const struct time_zone
{
75 {"GMT", 0}, /* Greenwich Mean */
76 {"UTC", 0}, /* Universal (Coordinated) */
77 {"WET", 0}, /* Western European */
78 {"BST", 0}, /* British Summer */
79 {"WAT", 60}, /* West Africa */
80 {"AST", 240}, /* Atlantic Standard */
81 {"ADT", 240}, /* Atlantic Daylight */
82 {"EST", 300}, /* Eastern Standard */
83 {"EDT", 300}, /* Eastern Daylight */
84 {"CST", 360}, /* Central Standard */
85 {"CDT", 360}, /* Central Daylight */
86 {"MST", 420}, /* Mountain Standard */
87 {"MDT", 420}, /* Mountain Daylight */
88 {"PST", 480}, /* Pacific Standard */
89 {"PDT", 480}, /* Pacific Daylight */
90 {"YST", 540}, /* Yukon Standard */
91 {"YDT", 540}, /* Yukon Daylight */
92 {"HST", 600}, /* Hawaii Standard */
93 {"HDT", 600}, /* Hawaii Daylight */
94 {"CAT", 600}, /* Central Alaska */
95 {"AHST", 600}, /* Alaska-Hawaii Standard */
96 {"NT", 660}, /* Nome */
97 {"IDLW", 720}, /* International Date Line West */
98 {"CET", -60}, /* Central European */
99 {"MET", -60}, /* Middle European */
100 {"MEWT", -60}, /* Middle European Winter */
101 {"MEST", -120}, /* Middle European Summer */
102 {"CEST", -120}, /* Central European Summer */
103 {"MESZ", -60}, /* Middle European Summer */
104 {"FWT", -60}, /* French Winter */
105 {"FST", -60}, /* French Summer */
106 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
107 {"WAST", -420}, /* West Australian Standard */
108 {"WADT", -420}, /* West Australian Daylight */
109 {"CCT", -480}, /* China Coast, USSR Zone 7 */
110 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
111 {"EAST", -600}, /* Eastern Australian Standard */
112 {"EADT", -600}, /* Eastern Australian Daylight */
113 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
114 {"NZT", -720}, /* New Zealand */
115 {"NZST", -720}, /* New Zealand Standard */
116 {"NZDT", -720}, /* New Zealand Daylight */
117 {"IDLE", -720}, /* International Date Line East */
123 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
124 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
125 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
127 static int check_day(char *day
, size_t len
);
128 static int check_month(char *month
);
129 static int check_tzone(char *tzone
);
131 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
133 /* {{{ static int http_sort_q(const void *, const void *) */
134 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
137 zval result
, *first
, *second
;
139 f
= *((Bucket
**) a
);
140 s
= *((Bucket
**) b
);
142 first
= *((zval
**) f
->pData
);
143 second
= *((zval
**) s
->pData
);
145 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
148 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
152 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
154 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
155 const size_t end
, const http_send_mode mode TSRMLS_DC
)
159 long len
= end
- begin
;
165 s
= (php_stream
*) data
;
166 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
169 buf
= (char *) ecalloc(1, HTTP_SENDBUF_SIZE
);
170 /* read into buf and write out */
171 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
172 if (!(read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
))) {
176 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
182 /* read & write left over */
184 if (read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
+ len
)) {
185 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
199 return len
== php_body_write(((char *)data
) + begin
, len TSRMLS_CC
)
210 /* {{{ Day/Month/TZ checks for http_parse_date()
211 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
212 static int check_day(char *day
, size_t len
)
215 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
216 for (i
= 0; i
< 7; i
++) {
217 if (!strcmp(day
, check
[0])) {
225 static int check_month(char *month
)
228 const char * const *check
= &months
[0];
229 for (i
= 0; i
< 12; i
++) {
230 if (!strcmp(month
, check
[0])) {
238 /* return the time zone offset between GMT and the input one, in number
239 of seconds or -1 if the timezone wasn't found/legal */
241 static int check_tzone(char *tzone
)
244 const struct time_zone
*check
= time_zones
;
245 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
246 if (!strcmp(tzone
, check
->name
)) {
247 return check
->offset
* 60;
255 /* char *pretty_key(char *, int, int, int) */
256 char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
)
258 if (key
&& key_len
) {
260 if (wasalpha
= isalpha(key
[0])) {
261 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
263 for (i
= 1; i
< key_len
; i
++) {
264 if (isalpha(key
[i
])) {
265 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
268 if (xhyphen
&& (key
[i
] == '_')) {
279 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
280 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
283 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
284 b
->handler_name
= estrdup(o
->handler_name
);
285 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
286 b
->text_length
= o
->text_length
;
287 b
->chunk_size
= o
->chunk_size
;
298 /* {{{ char *http_date(time_t) */
299 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
301 struct tm
*gmtime
, tmbuf
;
303 if (gmtime
= php_gmtime_r(&t
, &tmbuf
)) {
304 char *date
= ecalloc(1, 31);
306 "%s, %02d %s %04d %02d:%02d:%02d GMT",
307 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
308 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
309 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
318 /* {{{ time_t http_parse_date(char *)
319 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
320 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
323 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
324 hours
= -1, minutes
= -1, seconds
= -1;
326 enum assume_next dignext
= DATE_MDAY
;
327 const char *indate
= date
;
329 int found
= 0, part
= 0; /* max 6 parts */
331 while (*date
&& (part
< 6)) {
334 while (*date
&& !isalnum(*date
)) {
338 if (isalpha(*date
)) {
339 /* a name coming up */
342 sscanf(date
, "%31[A-Za-z]", buf
);
346 weekday
= check_day(buf
, len
);
352 if (!found
&& (month
== -1)) {
353 month
= check_month(buf
);
359 if (!found
&& (tz_offset
== -1)) {
360 /* this just must be a time zone string */
361 tz_offset
= check_tzone(buf
);
362 if (tz_offset
!= -1) {
368 return -1; /* bad string */
372 else if (isdigit(*date
)) {
376 if ((seconds
== -1) &&
377 (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
383 val
= (int) strtol(date
, &end
, 10);
385 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) &&
386 (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
387 /* four digits and a value less than 1300 and it is preceeded with
388 a plus or minus. This is a time zone indication. */
390 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
392 /* the + and - prefix indicates the local time compared to GMT,
393 this we need ther reversed math to get what we want */
394 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
397 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
398 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
401 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
402 monthday
= val
% 100;
405 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
406 if ((val
> 0) && (val
< 32)) {
413 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
417 year
+= year
> 70 ? 1900 : 2000;
436 seconds
= minutes
= hours
= 0; /* no time, make it zero */
439 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
440 /* lacks vital info, fail */
444 if (sizeof(time_t) < 5) {
445 /* 32 bit time_t can only hold dates to the beginning of 2038 */
454 tm
.tm_mday
= monthday
;
456 tm
.tm_year
= year
- 1900;
463 /* time zone adjust */
465 struct tm
*gmt
, keeptime2
;
469 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
470 return -1; /* illegal date/time */
475 /* Add the time zone diff (between the given timezone and GMT) and the
476 diff between the local time zone and GMT. */
477 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
479 if((delta
> 0) && (t
+ delta
< t
)) {
480 return -1; /* time_t overflow */
490 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
491 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, const size_t data_len
,
492 const http_send_mode data_mode TSRMLS_DC
)
494 char ssb_buf
[128] = {0};
495 unsigned char digest
[16];
497 char *new_etag
= ecalloc(1, 33);
504 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
508 if (!HTTP_G(ssb
).sb
.st_ino
) {
509 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
513 snprintf(ssb_buf
, 127, "%ld=%ld=%ld",
514 HTTP_G(ssb
).sb
.st_mtime
,
515 HTTP_G(ssb
).sb
.st_ino
,
516 HTTP_G(ssb
).sb
.st_size
518 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
527 PHP_MD5Final(digest
, &ctx
);
528 make_digest(new_etag
, digest
);
534 /* {{{ time_t http_lmod(void *, http_send_mode) */
535 PHP_HTTP_API
time_t _http_lmod(const void *data_ptr
, const http_send_mode data_mode TSRMLS_DC
)
546 if (!HTTP_G(ssb
).sb
.st_mtime
) {
547 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
551 return HTTP_G(ssb
).sb
.st_mtime
;
556 if (!HTTP_G(ssb
).sb
.st_mtime
) {
557 if(php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
))) {
561 return HTTP_G(ssb
).sb
.st_mtime
;
567 /* {{{ STATUS http_send_status_header(int, char *) */
568 PHP_HTTP_API STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
570 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
571 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
575 /* {{{ zval *http_get_server_var(char *) */
576 PHP_HTTP_API zval
*_http_get_server_var(const char *key TSRMLS_DC
)
579 if (SUCCESS
== zend_hash_find(
581 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
588 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
589 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
590 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
592 char etag
[33] = { 0 };
593 unsigned char digest
[16];
595 if (mode
& PHP_OUTPUT_HANDLER_START
) {
596 PHP_MD5Init(&HTTP_G(etag_md5
));
599 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
601 if (mode
& PHP_OUTPUT_HANDLER_END
) {
602 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
604 /* just do that if desired */
605 if (HTTP_G(etag_started
)) {
606 make_digest(etag
, digest
);
608 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
609 http_send_status(304);
611 http_send_etag(etag
, 32);
616 *handled_output_len
= output_len
;
617 *handled_output
= estrndup(output
, output_len
);
621 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
622 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
623 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
625 php_ob_buffer
**stack
;
628 if (count
= OG(ob_nesting_level
)) {
629 stack
= ecalloc(count
, sizeof(php_ob_buffer
*));
632 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
633 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
637 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
640 while (OG(ob_nesting_level
)) {
641 php_end_ob_buffer(0, 0 TSRMLS_CC
);
645 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
647 for (i
= 0; i
< count
; i
++) {
648 php_ob_buffer
*s
= stack
[i
];
649 if (strcmp(s
->handler_name
, "default output handler")) {
650 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
652 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
653 efree(s
->handler_name
);
665 /* {{{ int http_modified_match(char *, int) */
666 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
670 char *modified
, *chr_ptr
;
672 HTTP_GSC(zmodified
, entry
, 0);
674 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
675 if (chr_ptr
= strrchr(modified
, ';')) {
678 retval
= (t
<= http_parse_date(modified
));
684 /* {{{ int http_etag_match(char *, char *) */
685 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
691 HTTP_GSC(zetag
, entry
, 0);
693 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
697 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
698 sprintf(quoted_etag
, "\"%s\"", etag
);
700 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
701 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
703 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
710 /* {{{ STATUS http_send_last_modified(int) */
711 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
714 if (date
= http_date(t
)) {
715 char modified
[96] = "Last-Modified: ";
716 strcat(modified
, date
);
722 return http_send_header(modified
);
728 /* {{{ static STATUS http_send_etag(char *, int) */
729 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
730 const int etag_len TSRMLS_DC
)
736 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
737 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
745 HTTP_G(etag
) = estrdup(etag
);
747 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
748 sprintf(etag_header
, "ETag: \"%s\"", etag
);
749 if (SUCCESS
!= (status
= http_send_header(etag_header
))) {
750 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", etag_header
);
757 /* {{{ STATUS http_send_cache_control(char *, size_t) */
758 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
,
759 const size_t cc_len TSRMLS_DC
)
762 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
764 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
765 if (SUCCESS
!= (status
= http_send_header(cc_header
))) {
766 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
,
767 "Could not send '%s' header", cc_header
);
774 /* {{{ STATUS http_send_content_type(char *, size_t) */
775 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
,
776 const size_t ct_len TSRMLS_DC
)
781 if (!strchr(content_type
, '/')) {
782 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
783 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
788 /* remember for multiple ranges */
790 efree(HTTP_G(ctype
));
792 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
794 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
795 sprintf(ct_header
, "Content-Type: %s", content_type
);
797 if (SUCCESS
!= (status
= http_send_header(ct_header
))) {
798 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
799 "Couldn't send '%s' header", ct_header
);
806 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
807 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
,
808 const size_t f_len
, const int send_inline TSRMLS_DC
)
814 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
815 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
817 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
818 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
821 if (SUCCESS
!= (status
= http_send_header(cd_header
))) {
822 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", cd_header
);
829 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
830 PHP_HTTP_API STATUS
_http_cache_last_modified(const time_t last_modified
,
831 const time_t send_modified
, const char *cache_control
, const size_t cc_len TSRMLS_DC
)
834 http_send_cache_control(cache_control
, cc_len
);
837 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
838 if (SUCCESS
== http_send_status(304)) {
841 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
845 return http_send_last_modified(send_modified
);
849 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
850 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, const size_t etag_len
,
851 const char *cache_control
, const size_t cc_len TSRMLS_DC
)
854 http_send_cache_control(cache_control
, cc_len
);
858 http_send_etag(etag
, etag_len
);
859 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
860 if (SUCCESS
== http_send_status(304)) {
863 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
869 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
870 if (!HTTP_G(etag_started
)) {
871 if (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)) {
872 HTTP_G(etag_started
) = 1;
875 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
883 /* {{{ char *http_absolute_uri(char *) */
884 PHP_HTTP_API
char *_http_absolute_uri_ex(
885 const char *url
, size_t url_len
,
886 const char *proto
, size_t proto_len
,
887 const char *host
, size_t host_len
,
888 unsigned port TSRMLS_DC
)
890 php_url
*purl
, furl
= {NULL
};
894 char *scheme
= NULL
, *URL
= ecalloc(1, HTTP_URI_MAXLEN
+ 1);
896 if ((!url
|| !url_len
) && (
897 (!(url
= SG(request_info
).request_uri
)) ||
898 (!(url_len
= strlen(SG(request_info
).request_uri
))))) {
899 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
900 "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
904 if (!(purl
= php_url_parse(url
))) {
905 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse supplied URL");
909 furl
.user
= purl
->user
;
910 furl
.pass
= purl
->pass
;
911 furl
.path
= purl
->path
;
912 furl
.fragment
= purl
->fragment
;
915 furl
.scheme
= scheme
= estrdup(proto
);
916 } else if (purl
->scheme
) {
917 furl
.scheme
= purl
->scheme
;
918 } else if (port
&& (se
= getservbyport(htons(port
), "tcp"))) {
919 furl
.scheme
= (scheme
= estrdup(se
->s_name
));
921 furl
.scheme
= "http";
926 } else if (purl
->port
) {
927 furl
.port
= purl
->port
;
928 } else if (strncmp(furl
.scheme
, "http", 4) && (se
= getservbyname(furl
.scheme
, "tcp"))) {
929 furl
.port
= ntohs(se
->s_port
);
931 furl
.port
= furl
.scheme
[5] ? 443 : 80;
935 furl
.host
= (char *) host
;
936 } else if (purl
->host
) {
937 furl
.host
= purl
->host
;
938 } else if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
939 (zhost
= http_get_server_var("SERVER_NAME"))) {
940 furl
.host
= Z_STRVAL_P(zhost
);
942 furl
.host
= "localhost";
945 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
946 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
947 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
948 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
949 php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
950 "Absolute URI would have exceeded max URI length (%d bytes) - " \
951 "tried to add %d bytes ('%s')", \
952 HTTP_URI_MAXLEN, add_len, add_string); \
956 php_url_free(purl); \
959 strcat(URL, add_string); \
962 HTTP_URI_STRLCATL(URL
, full_len
, furl
.scheme
);
963 HTTP_URI_STRLCATS(URL
, full_len
, "://");
966 HTTP_URI_STRLCATL(URL
, full_len
, furl
.user
);
968 HTTP_URI_STRLCATS(URL
, full_len
, ":");
969 HTTP_URI_STRLCATL(URL
, full_len
, furl
.pass
);
971 HTTP_URI_STRLCATS(URL
, full_len
, "@");
974 HTTP_URI_STRLCATL(URL
, full_len
, furl
.host
);
976 if ( (strcmp(furl
.scheme
, "http") && (furl
.port
!= 80)) ||
977 (strcmp(furl
.scheme
, "https") && (furl
.port
!= 443))) {
978 char port_string
[8] = {0};
979 snprintf(port_string
, 7, ":%u", furl
.port
);
980 HTTP_URI_STRLCATL(URL
, full_len
, port_string
);
984 HTTP_URI_STRLCATL(URL
, full_len
, furl
.path
);
986 HTTP_URI_STRLCATS(URL
, full_len
, "?");
987 HTTP_URI_STRLCATL(URL
, full_len
, furl
.query
);
990 HTTP_URI_STRLCATS(URL
, full_len
, "#");
991 HTTP_URI_STRLCATL(URL
, full_len
, furl
.fragment
);
1004 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1005 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1006 const char *def TSRMLS_DC
)
1008 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1009 char *q_ptr
, *result
;
1013 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1015 MAKE_STD_ZVAL(zarray
);
1018 MAKE_STD_ZVAL(zdelim
);
1019 ZVAL_STRING(zdelim
, ",", 0);
1020 php_explode(zdelim
, zaccept
, zarray
, -1);
1023 MAKE_STD_ZVAL(zentries
);
1024 array_init(zentries
);
1026 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1027 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1029 if (SUCCESS
!= zend_hash_get_current_data(
1030 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1031 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1032 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1036 /* check for qualifier */
1037 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1038 qual
= strtod(q_ptr
+ 3, NULL
);
1043 /* walk through the supported array */
1044 FOREACH_VAL(supported
, zsupp
) {
1045 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1046 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1055 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1057 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1058 http_sort_q
, 0 TSRMLS_CC
)) ||
1059 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1060 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1061 result
= estrdup(def
);
1064 zval_dtor(zentries
);
1071 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
1072 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
,
1073 const size_t length TSRMLS_DC
)
1077 long begin
= -1, end
= -1, *ptr
;
1079 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1080 range
= Z_STRVAL_P(zrange
);
1082 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
1083 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1088 range
+= sizeof("bytes=") - 1;
1091 switch (c
= *(range
++))
1097 case '1': case '2': case '3':
1098 case '4': case '5': case '6':
1099 case '7': case '8': case '9':
1101 * If the value of the pointer is already set (non-negative)
1102 * then multiply its value by ten and add the current value,
1103 * else initialise the pointers value with the current value
1105 * This let us recognize empty fields when validating the
1106 * ranges, i.e. a "-10" for begin and "12345" for the end
1107 * was the following range request: "Range: bytes=0-12345";
1108 * While a "-1" for begin and "12345" for the end would
1109 * have been: "Range: bytes=-12345".
1124 /* IE - ignore for now */
1131 /* validate ranges */
1136 if ((length
- end
) < 1) {
1144 if ((length
- end
) < 1) {
1147 begin
= length
- end
;
1157 if ((length
- begin
) < 1) {
1165 if ( ((length
- begin
) < 1) ||
1166 ((length
- end
) < 1) ||
1167 ((begin
- end
) >= 0)) {
1177 MAKE_STD_ZVAL(zentry
);
1179 add_index_long(zentry
, 0, begin
);
1180 add_index_long(zentry
, 1, end
);
1181 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
1199 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
1200 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1202 long **begin
, **end
;
1206 if (zend_hash_num_elements(ranges
) == 1) {
1207 char range_header
[256] = {0};
1209 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
1210 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1211 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1215 /* Send HTTP 206 Partial Content */
1216 http_send_status(206);
1218 /* send content range header */
1219 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1220 http_send_header(range_header
);
1222 /* send requested chunk */
1223 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1228 char bound
[23] = {0}, preface
[1024] = {0},
1229 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1231 /* Send HTTP 206 Partial Content */
1232 http_send_status(206);
1234 /* send multipart/byteranges header */
1235 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1236 strncat(multi_header
, bound
+ 2, 21);
1237 http_send_header(multi_header
);
1239 /* send each requested chunk */
1240 FOREACH_HASH_VAL(ranges
, zrange
) {
1241 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1242 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1246 snprintf(preface
, 1023,
1248 HTTP_CRLF
"Content-Type: %s"
1249 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1254 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1260 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1261 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1264 /* write boundary once more */
1265 php_body_write(HTTP_CRLF
, sizeof(HTTP_CRLF
) - 1 TSRMLS_CC
);
1266 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1267 php_body_write("--", 2 TSRMLS_CC
);
1274 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
1275 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1276 const http_send_mode data_mode TSRMLS_DC
)
1278 int is_range_request
= http_is_range_request();
1288 if (HTTP_G(etag_started
)) {
1291 HTTP_G(etag_started
) = 0;
1292 /* never ever use the output to compute the ETag if http_send() is used */
1293 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1294 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
1298 /* send 304 Not Modified if etag matches */
1299 if ((!is_range_request
) && http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1301 return http_send_status(304);
1304 http_send_etag(etag
, 32);
1308 /* send 304 Not Modified if last-modified matches*/
1309 if ((!is_range_request
) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1310 return http_send_status(304);
1313 if (is_range_request
) {
1315 /* only send ranges if entity hasn't changed */
1317 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_MATCH", 13)) ||
1318 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag
)))
1320 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1321 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
)))
1324 STATUS result
= FAILURE
;
1326 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1328 switch (http_get_request_ranges(&ranges
, data_size
))
1331 zend_hash_destroy(&ranges
);
1332 /* go ahead and send all */
1336 result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
1337 zend_hash_destroy(&ranges
);
1342 zend_hash_destroy(&ranges
);
1343 http_send_status(416);
1354 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1358 /* {{{ STATUS http_send_stream(php_stream *) */
1359 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
,
1360 zend_bool close_stream TSRMLS_DC
)
1364 if ((!file
) || php_stream_stat(file
, &HTTP_G(ssb
))) {
1368 status
= http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1371 php_stream_close(file
);
1378 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1379 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1380 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1386 *decoded
= ecalloc(1, encoded_len
);
1390 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1391 char hex_len
[9] = {0};
1392 size_t chunk_len
= 0;
1395 /* read in chunk size */
1396 while (isxdigit(*e_ptr
)) {
1398 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1399 "Chunk size is too long: 0x%s...", hex_len
);
1403 hex_len
[i
++] = *e_ptr
++;
1406 /* reached the end */
1407 if (!strcmp(hex_len
, "0")) {
1412 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1413 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1414 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1415 *e_ptr
, *(e_ptr
+ 1));
1423 chunk_len
= strtol(hex_len
, &error
, 16);
1424 if (error
== hex_len
) {
1425 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1426 "Invalid chunk size string: '%s'", hex_len
);
1432 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1434 e_ptr
+= chunk_len
+ 2;
1435 *decoded_len
+= chunk_len
;
1442 /* {{{ proto STATUS http_split_response_ex(char *, size_t, zval *, zval *) */
1443 PHP_HTTP_API STATUS
_http_split_response_ex(char *response
,
1444 size_t response_len
, HashTable
*headers
, char **body
, size_t *body_len TSRMLS_DC
)
1446 char *header
= response
;
1449 while (0 < (response_len
- (response
- header
+ 4))) {
1450 if ( (*response
++ == '\r') &&
1451 (*response
++ == '\n') &&
1452 (*response
++ == '\r') &&
1453 (*response
++ == '\n')) {
1459 if (*body
&& (*body_len
= response_len
- (*body
- header
))) {
1460 *body
= estrndup(*body
, *body_len
- 1);
1463 return http_parse_headers(header
, *body
? *body
- header
: response_len
, headers
);
1467 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1468 PHP_HTTP_API STATUS
_http_parse_headers(char *header
, int header_len
, HashTable
*headers TSRMLS_DC
)
1470 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1473 Z_ARRVAL(array
) = headers
;
1475 if (header_len
< 2) {
1480 if (!strncmp(header
, "HTTP/1.", 7)) {
1481 char *end
= strstr(header
, HTTP_CRLF
);
1482 add_assoc_stringl(&array
, "Status",
1483 header
+ sizeof("HTTP/1.x ") - 1,
1484 end
- (header
+ sizeof("HTTP/1.x ") - 1), 1);
1490 while (header_len
>= (line
- begin
)) {
1496 --value_len
; /* we don't have CR so value length is one char less */
1498 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
1500 /* skip empty key */
1501 if (header
!= colon
) {
1502 char *key
= estrndup(header
, colon
- header
);
1503 value_len
+= line
- colon
- 1;
1505 /* skip leading ws */
1506 while (isspace(*(++colon
))) --value_len
;
1507 /* skip trailing ws */
1508 while (isspace(colon
[value_len
- 1])) --value_len
;
1510 if (value_len
< 1) {
1511 /* hm, empty header? */
1512 add_assoc_stringl(&array
, key
, "", 0, 1);
1514 add_assoc_stringl(&array
, key
, colon
, value_len
, 1);
1521 header
+= line
- header
;
1536 /* {{{ void http_get_request_headers(zval *) */
1537 PHP_HTTP_API
void _http_get_request_headers(zval
*array TSRMLS_DC
)
1542 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
1543 if (key
&& !strncmp(key
, "HTTP_", 5)) {
1545 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
1546 add_assoc_stringl(array
, pretty_key(key
+ 5, strlen(key
) - 5, 1, 1), Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1552 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, int, char **, size_t *) */
1553 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, int override_argsep
,
1554 char *pre_encoded_data
, size_t pre_encoded_len
,
1555 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
1557 smart_str qstr
= {0};
1559 if (override_argsep
) {
1560 HTTP_URL_ARGSEP_OVERRIDE
;
1563 if (pre_encoded_len
&& pre_encoded_data
) {
1564 smart_str_appendl(&qstr
, pre_encoded_data
, pre_encoded_len
);
1567 if (SUCCESS
!= php_url_encode_hash_ex(hash
, &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
1568 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
1572 if (override_argsep
) {
1573 HTTP_URL_ARGSEP_RESTORE
;
1578 if (override_argsep
) {
1579 HTTP_URL_ARGSEP_RESTORE
;
1584 *encoded_data
= qstr
.c
;
1586 *encoded_len
= qstr
.len
;
1593 /* {{{ STATUS http_auth_header(char *, char*) */
1594 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1596 char realm_header
[1024] = {0};
1597 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1598 return http_send_status_header(401, realm_header
);
1602 /* {{{ STATUS http_auth_credentials(char **, char **) */
1603 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1605 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1606 zval
*zuser
, *zpass
;
1608 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1609 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1611 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1612 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1617 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1619 char *decoded
, *colon
;
1621 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1624 if (colon
= strchr(decoded
+ 6, ':')) {
1625 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1626 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1637 #ifndef ZEND_ENGINE_2
1638 /* {{{ php_url_encode_hash
1639 Author: Sara Golemon <pollita@php.net> */
1640 PHP_HTTP_API STATUS
php_url_encode_hash_ex(HashTable
*ht
, smart_str
*formstr
,
1641 const char *num_prefix
, int num_prefix_len
,
1642 const char *key_prefix
, int key_prefix_len
,
1643 const char *key_suffix
, int key_suffix_len
,
1644 zval
*type TSRMLS_DC
)
1646 char *arg_sep
= NULL
, *key
= NULL
, *ekey
, *newprefix
, *p
;
1647 int arg_sep_len
, key_len
, ekey_len
, key_type
, newprefix_len
;
1649 zval
**zdata
= NULL
, *copyzval
;
1655 if (ht
->nApplyCount
> 0) {
1656 /* Prevent recursion */
1660 arg_sep
= INI_STR("arg_separator.output");
1661 if (!arg_sep
|| !strlen(arg_sep
)) {
1662 arg_sep
= HTTP_URL_ARGSEP_DEFAULT
;
1664 arg_sep_len
= strlen(arg_sep
);
1666 for (zend_hash_internal_pointer_reset(ht
);
1667 (key_type
= zend_hash_get_current_key_ex(ht
, &key
, &key_len
, &idx
, 0, NULL
)) != HASH_KEY_NON_EXISTANT
;
1668 zend_hash_move_forward(ht
)
1670 if (key_type
== HASH_KEY_IS_STRING
&& key_len
&& key
[key_len
-1] == '\0') {
1671 /* We don't want that trailing NULL */
1675 #ifdef ZEND_ENGINE_2
1676 /* handling for private & protected object properties */
1677 if (key
&& *key
== '\0' && type
!= NULL
) {
1680 zend_object
*zobj
= zend_objects_get_address(type TSRMLS_CC
);
1681 if (zend_check_property_access(zobj
, key TSRMLS_CC
) != SUCCESS
) {
1682 /* private or protected property access outside of the class */
1685 zend_unmangle_property_name(key
, &tmp
, &key
);
1686 key_len
= strlen(key
);
1690 if (zend_hash_get_current_data_ex(ht
, (void **)&zdata
, NULL
) == FAILURE
|| !zdata
|| !(*zdata
)) {
1691 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Error traversing form data array.");
1694 if (Z_TYPE_PP(zdata
) == IS_ARRAY
|| Z_TYPE_PP(zdata
) == IS_OBJECT
) {
1695 if (key_type
== HASH_KEY_IS_STRING
) {
1696 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1697 newprefix_len
= key_suffix_len
+ ekey_len
+ key_prefix_len
+ 1;
1698 newprefix
= emalloc(newprefix_len
+ 1);
1702 memcpy(p
, key_prefix
, key_prefix_len
);
1703 p
+= key_prefix_len
;
1706 memcpy(p
, ekey
, ekey_len
);
1711 memcpy(p
, key_suffix
, key_suffix_len
);
1712 p
+= key_suffix_len
;
1718 /* Is an integer key */
1719 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1720 newprefix_len
= key_prefix_len
+ num_prefix_len
+ ekey_len
+ key_suffix_len
+ 1;
1721 newprefix
= emalloc(newprefix_len
+ 1);
1725 memcpy(p
, key_prefix
, key_prefix_len
);
1726 p
+= key_prefix_len
;
1729 memcpy(p
, num_prefix
, num_prefix_len
);
1730 p
+= num_prefix_len
;
1732 memcpy(p
, ekey
, ekey_len
);
1737 memcpy(p
, key_suffix
, key_suffix_len
);
1738 p
+= key_suffix_len
;
1744 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
);
1747 } else if (Z_TYPE_PP(zdata
) == IS_NULL
|| Z_TYPE_PP(zdata
) == IS_RESOURCE
) {
1748 /* Skip these types */
1752 smart_str_appendl(formstr
, arg_sep
, arg_sep_len
);
1754 /* Simple key=value */
1755 smart_str_appendl(formstr
, key_prefix
, key_prefix_len
);
1756 if (key_type
== HASH_KEY_IS_STRING
) {
1757 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1758 smart_str_appendl(formstr
, ekey
, ekey_len
);
1763 smart_str_appendl(formstr
, num_prefix
, num_prefix_len
);
1765 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1766 smart_str_appendl(formstr
, ekey
, ekey_len
);
1769 smart_str_appendl(formstr
, key_suffix
, key_suffix_len
);
1770 smart_str_appendl(formstr
, "=", 1);
1771 switch (Z_TYPE_PP(zdata
)) {
1773 ekey
= php_url_encode(Z_STRVAL_PP(zdata
), Z_STRLEN_PP(zdata
), &ekey_len
);
1777 ekey_len
= spprintf(&ekey
, 12, "%ld", Z_LVAL_PP(zdata
));
1780 ekey_len
= spprintf(&ekey
, 48, "%.*G", (int) EG(precision
), Z_DVAL_PP(zdata
));
1783 /* fall back on convert to string */
1784 MAKE_STD_ZVAL(copyzval
);
1785 *copyzval
= **zdata
;
1786 zval_copy_ctor(copyzval
);
1787 convert_to_string_ex(©zval
);
1788 ekey
= php_url_encode(Z_STRVAL_P(copyzval
), Z_STRLEN_P(copyzval
), &ekey_len
);
1789 zval_ptr_dtor(©zval
);
1791 smart_str_appendl(formstr
, ekey
, ekey_len
);
1799 #endif /* !ZEND_ENDGINE_2 */
1801 /* }}} public API */
1808 * vim600: noet sw=4 ts=4 fdm=marker
1809 * vim<600: noet sw=4 ts=4