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"
31 #include "ext/standard/md5.h"
32 #include "ext/standard/url.h"
33 #include "ext/standard/base64.h"
34 #include "ext/standard/php_string.h"
35 #include "ext/standard/php_smart_str.h"
36 #include "ext/standard/php_lcg.h"
41 # include "ext/standard/php_http.h"
45 #include "php_http_api.h"
46 #include "php_http_std_defs.h"
48 ZEND_DECLARE_MODULE_GLOBALS(http
)
50 /* {{{ day/month names */
51 static const char *days
[] = {
52 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
54 static const char *wkdays
[] = {
55 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
57 static const char *weekdays
[] = {
58 "Monday", "Tuesday", "Wednesday",
59 "Thursday", "Friday", "Saturday", "Sunday"
61 static const char *months
[] = {
62 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
63 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
70 static const struct time_zone
{
74 {"GMT", 0}, /* Greenwich Mean */
75 {"UTC", 0}, /* Universal (Coordinated) */
76 {"WET", 0}, /* Western European */
77 {"BST", 0}, /* British Summer */
78 {"WAT", 60}, /* West Africa */
79 {"AST", 240}, /* Atlantic Standard */
80 {"ADT", 240}, /* Atlantic Daylight */
81 {"EST", 300}, /* Eastern Standard */
82 {"EDT", 300}, /* Eastern Daylight */
83 {"CST", 360}, /* Central Standard */
84 {"CDT", 360}, /* Central Daylight */
85 {"MST", 420}, /* Mountain Standard */
86 {"MDT", 420}, /* Mountain Daylight */
87 {"PST", 480}, /* Pacific Standard */
88 {"PDT", 480}, /* Pacific Daylight */
89 {"YST", 540}, /* Yukon Standard */
90 {"YDT", 540}, /* Yukon Daylight */
91 {"HST", 600}, /* Hawaii Standard */
92 {"HDT", 600}, /* Hawaii Daylight */
93 {"CAT", 600}, /* Central Alaska */
94 {"AHST", 600}, /* Alaska-Hawaii Standard */
95 {"NT", 660}, /* Nome */
96 {"IDLW", 720}, /* International Date Line West */
97 {"CET", -60}, /* Central European */
98 {"MET", -60}, /* Middle European */
99 {"MEWT", -60}, /* Middle European Winter */
100 {"MEST", -120}, /* Middle European Summer */
101 {"CEST", -120}, /* Central European Summer */
102 {"MESZ", -60}, /* Middle European Summer */
103 {"FWT", -60}, /* French Winter */
104 {"FST", -60}, /* French Summer */
105 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
106 {"WAST", -420}, /* West Australian Standard */
107 {"WADT", -420}, /* West Australian Daylight */
108 {"CCT", -480}, /* China Coast, USSR Zone 7 */
109 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
110 {"EAST", -600}, /* Eastern Australian Standard */
111 {"EADT", -600}, /* Eastern Australian Daylight */
112 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
113 {"NZT", -720}, /* New Zealand */
114 {"NZST", -720}, /* New Zealand Standard */
115 {"NZDT", -720}, /* New Zealand Daylight */
116 {"IDLE", -720}, /* International Date Line East */
122 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
);
123 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
124 static STATUS
_http_send_chunk(const void *data
, const size_t begin
, const size_t end
, const http_send_mode mode TSRMLS_DC
);
126 static int check_day(char *day
, size_t len
);
127 static int check_month(char *month
);
128 static int check_tzone(char *tzone
);
130 static int http_ob_stack_get(php_ob_buffer
*, php_ob_buffer
**);
132 /* {{{ static int http_sort_q(const void *, const void *) */
133 static int http_sort_q(const void *a
, const void *b TSRMLS_DC
)
136 zval result
, *first
, *second
;
138 f
= *((Bucket
**) a
);
139 s
= *((Bucket
**) b
);
141 first
= *((zval
**) f
->pData
);
142 second
= *((zval
**) s
->pData
);
144 if (numeric_compare_function(&result
, first
, second TSRMLS_CC
) != SUCCESS
) {
147 return (Z_LVAL(result
) > 0 ? -1 : (Z_LVAL(result
) < 0 ? 1 : 0));
151 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
153 static STATUS
_http_send_chunk(const void *data
, const size_t begin
,
154 const size_t end
, const http_send_mode mode TSRMLS_DC
)
158 long len
= end
- begin
;
164 s
= (php_stream
*) data
;
165 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
168 buf
= (char *) ecalloc(1, HTTP_SENDBUF_SIZE
);
169 /* read into buf and write out */
170 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
171 if (!(read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
))) {
175 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
181 /* read & write left over */
183 if (read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
+ len
)) {
184 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
198 return len
== php_body_write(((char *)data
) + begin
, len TSRMLS_CC
)
209 /* {{{ Day/Month/TZ checks for http_parse_date()
210 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
211 static int check_day(char *day
, size_t len
)
214 const char * const *check
= (len
> 3) ? &weekdays
[0] : &wkdays
[0];
215 for (i
= 0; i
< 7; i
++) {
216 if (!strcmp(day
, check
[0])) {
224 static int check_month(char *month
)
227 const char * const *check
= &months
[0];
228 for (i
= 0; i
< 12; i
++) {
229 if (!strcmp(month
, check
[0])) {
237 /* return the time zone offset between GMT and the input one, in number
238 of seconds or -1 if the timezone wasn't found/legal */
240 static int check_tzone(char *tzone
)
243 const struct time_zone
*check
= time_zones
;
244 for (i
= 0; i
< sizeof(time_zones
) / sizeof(time_zones
[0]); i
++) {
245 if (!strcmp(tzone
, check
->name
)) {
246 return check
->offset
* 60;
254 /* char *pretty_key(char *, int, int, int) */
255 char *pretty_key(char *key
, int key_len
, int uctitle
, int xhyphen
)
257 if (key
&& key_len
) {
259 if (wasalpha
= isalpha(key
[0])) {
260 key
[0] = uctitle
? toupper(key
[0]) : tolower(key
[0]);
262 for (i
= 1; i
< key_len
; i
++) {
263 if (isalpha(key
[i
])) {
264 key
[i
] = ((!wasalpha
) && uctitle
) ? toupper(key
[i
]) : tolower(key
[i
]);
267 if (xhyphen
&& (key
[i
] == '_')) {
278 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
279 static STATUS
http_ob_stack_get(php_ob_buffer
*o
, php_ob_buffer
**s
)
282 php_ob_buffer
*b
= emalloc(sizeof(php_ob_buffer
));
283 b
->handler_name
= estrdup(o
->handler_name
);
284 b
->buffer
= estrndup(o
->buffer
, o
->text_length
);
285 b
->text_length
= o
->text_length
;
286 b
->chunk_size
= o
->chunk_size
;
297 /* {{{ char *http_date(time_t) */
298 PHP_HTTP_API
char *_http_date(time_t t TSRMLS_DC
)
300 struct tm
*gmtime
, tmbuf
;
302 if (gmtime
= php_gmtime_r(&t
, &tmbuf
)) {
303 char *date
= ecalloc(1, 31);
305 "%s, %02d %s %04d %02d:%02d:%02d GMT",
306 days
[gmtime
->tm_wday
], gmtime
->tm_mday
,
307 months
[gmtime
->tm_mon
], gmtime
->tm_year
+ 1900,
308 gmtime
->tm_hour
, gmtime
->tm_min
, gmtime
->tm_sec
317 /* {{{ time_t http_parse_date(char *)
318 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
319 PHP_HTTP_API
time_t _http_parse_date(const char *date
)
322 int tz_offset
= -1, year
= -1, month
= -1, monthday
= -1, weekday
= -1,
323 hours
= -1, minutes
= -1, seconds
= -1;
325 enum assume_next dignext
= DATE_MDAY
;
326 const char *indate
= date
;
328 int found
= 0, part
= 0; /* max 6 parts */
330 while (*date
&& (part
< 6)) {
333 while (*date
&& !isalnum(*date
)) {
337 if (isalpha(*date
)) {
338 /* a name coming up */
341 sscanf(date
, "%31[A-Za-z]", buf
);
345 weekday
= check_day(buf
, len
);
351 if (!found
&& (month
== -1)) {
352 month
= check_month(buf
);
358 if (!found
&& (tz_offset
== -1)) {
359 /* this just must be a time zone string */
360 tz_offset
= check_tzone(buf
);
361 if (tz_offset
!= -1) {
367 return -1; /* bad string */
371 else if (isdigit(*date
)) {
375 if ((seconds
== -1) &&
376 (3 == sscanf(date
, "%02d:%02d:%02d", &hours
, &minutes
, &seconds
))) {
382 val
= (int) strtol(date
, &end
, 10);
384 if ((tz_offset
== -1) && ((end
- date
) == 4) && (val
< 1300) &&
385 (indate
< date
) && ((date
[-1] == '+' || date
[-1] == '-'))) {
386 /* four digits and a value less than 1300 and it is preceeded with
387 a plus or minus. This is a time zone indication. */
389 tz_offset
= (val
/ 100 * 60 + val
% 100) * 60;
391 /* the + and - prefix indicates the local time compared to GMT,
392 this we need ther reversed math to get what we want */
393 tz_offset
= date
[-1] == '+' ? -tz_offset
: tz_offset
;
396 if (((end
- date
) == 8) && (year
== -1) && (month
== -1) && (monthday
== -1)) {
397 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
400 month
= (val
% 10000) / 100 - 1; /* month is 0 - 11 */
401 monthday
= val
% 100;
404 if (!found
&& (dignext
== DATE_MDAY
) && (monthday
== -1)) {
405 if ((val
> 0) && (val
< 32)) {
412 if (!found
&& (dignext
== DATE_YEAR
) && (year
== -1)) {
416 year
+= year
> 70 ? 1900 : 2000;
435 seconds
= minutes
= hours
= 0; /* no time, make it zero */
438 if ((-1 == monthday
) || (-1 == month
) || (-1 == year
)) {
439 /* lacks vital info, fail */
443 if (sizeof(time_t) < 5) {
444 /* 32 bit time_t can only hold dates to the beginning of 2038 */
453 tm
.tm_mday
= monthday
;
455 tm
.tm_year
= year
- 1900;
462 /* time zone adjust */
464 struct tm
*gmt
, keeptime2
;
468 if(!(gmt
= php_gmtime_r(&t
, &keeptime2
))) {
469 return -1; /* illegal date/time */
474 /* Add the time zone diff (between the given timezone and GMT) and the
475 diff between the local time zone and GMT. */
476 delta
= (tz_offset
!= -1 ? tz_offset
: 0) + (t
- t2
);
478 if((delta
> 0) && (t
+ delta
< t
)) {
479 return -1; /* time_t overflow */
489 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
490 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, const size_t data_len
,
491 const http_send_mode data_mode TSRMLS_DC
)
493 char ssb_buf
[128] = {0};
494 unsigned char digest
[16];
496 char *new_etag
= ecalloc(1, 33);
503 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
507 if (!HTTP_G(ssb
).sb
.st_ino
) {
508 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
512 snprintf(ssb_buf
, 127, "%ld=%ld=%ld",
513 HTTP_G(ssb
).sb
.st_mtime
,
514 HTTP_G(ssb
).sb
.st_ino
,
515 HTTP_G(ssb
).sb
.st_size
517 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
526 PHP_MD5Final(digest
, &ctx
);
527 make_digest(new_etag
, digest
);
533 /* {{{ time_t http_lmod(void *, http_send_mode) */
534 PHP_HTTP_API
time_t _http_lmod(const void *data_ptr
, const http_send_mode data_mode TSRMLS_DC
)
545 if (!HTTP_G(ssb
).sb
.st_mtime
) {
546 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
550 return HTTP_G(ssb
).sb
.st_mtime
;
555 if (!HTTP_G(ssb
).sb
.st_mtime
) {
556 if(php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
))) {
560 return HTTP_G(ssb
).sb
.st_mtime
;
566 /* {{{ STATUS http_send_status_header(int, char *) */
567 PHP_HTTP_API STATUS
_http_send_status_header(const int status
, const char *header TSRMLS_DC
)
569 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
570 return sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
);
574 /* {{{ zval *http_get_server_var(char *) */
575 PHP_HTTP_API zval
*_http_get_server_var(const char *key TSRMLS_DC
)
578 if (SUCCESS
== zend_hash_find(
580 (char *) key
, strlen(key
) + 1, (void **) &var
)) {
587 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
588 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
589 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
591 char etag
[33] = { 0 };
592 unsigned char digest
[16];
594 if (mode
& PHP_OUTPUT_HANDLER_START
) {
595 PHP_MD5Init(&HTTP_G(etag_md5
));
598 PHP_MD5Update(&HTTP_G(etag_md5
), output
, output_len
);
600 if (mode
& PHP_OUTPUT_HANDLER_END
) {
601 PHP_MD5Final(digest
, &HTTP_G(etag_md5
));
603 /* just do that if desired */
604 if (HTTP_G(etag_started
)) {
605 make_digest(etag
, digest
);
607 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
608 http_send_status(304);
610 http_send_etag(etag
, 32);
615 *handled_output_len
= output_len
;
616 *handled_output
= estrndup(output
, output_len
);
620 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
621 PHP_HTTP_API STATUS
_http_start_ob_handler(php_output_handler_func_t handler_func
,
622 char *handler_name
, uint chunk_size
, zend_bool erase TSRMLS_DC
)
624 php_ob_buffer
**stack
;
627 if (count
= OG(ob_nesting_level
)) {
628 stack
= ecalloc(count
, sizeof(php_ob_buffer
*));
631 zend_stack_apply_with_argument(&OG(ob_buffers
), ZEND_STACK_APPLY_BOTTOMUP
,
632 (int (*)(void *elem
, void *)) http_ob_stack_get
, stack
);
636 http_ob_stack_get(&OG(active_ob_buffer
), stack
);
639 while (OG(ob_nesting_level
)) {
640 php_end_ob_buffer(0, 0 TSRMLS_CC
);
644 php_ob_set_internal_handler(handler_func
, chunk_size
, handler_name
, erase TSRMLS_CC
);
646 for (i
= 0; i
< count
; i
++) {
647 php_ob_buffer
*s
= stack
[i
];
648 if (strcmp(s
->handler_name
, "default output handler")) {
649 php_start_ob_buffer_named(s
->handler_name
, s
->chunk_size
, s
->erase TSRMLS_CC
);
651 php_body_write(s
->buffer
, s
->text_length TSRMLS_CC
);
652 efree(s
->handler_name
);
664 /* {{{ int http_modified_match(char *, int) */
665 PHP_HTTP_API
int _http_modified_match(const char *entry
, const time_t t TSRMLS_DC
)
669 char *modified
, *chr_ptr
;
671 HTTP_GSC(zmodified
, entry
, 0);
673 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
674 if (chr_ptr
= strrchr(modified
, ';')) {
677 retval
= (t
<= http_parse_date(modified
));
683 /* {{{ int http_etag_match(char *, char *) */
684 PHP_HTTP_API
int _http_etag_match(const char *entry
, const char *etag TSRMLS_DC
)
690 HTTP_GSC(zetag
, entry
, 0);
692 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
696 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
697 sprintf(quoted_etag
, "\"%s\"", etag
);
699 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
700 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
702 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
709 /* {{{ STATUS http_send_last_modified(int) */
710 PHP_HTTP_API STATUS
_http_send_last_modified(const time_t t TSRMLS_DC
)
713 if (date
= http_date(t
)) {
714 char modified
[96] = "Last-Modified: ";
715 strcat(modified
, date
);
721 return http_send_header(modified
);
727 /* {{{ static STATUS http_send_etag(char *, int) */
728 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
,
729 const int etag_len TSRMLS_DC
)
735 php_error_docref(NULL TSRMLS_CC
,E_ERROR
,
736 "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
744 HTTP_G(etag
) = estrdup(etag
);
746 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
747 sprintf(etag_header
, "ETag: \"%s\"", etag
);
748 if (SUCCESS
!= (status
= http_send_header(etag_header
))) {
749 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", etag_header
);
756 /* {{{ STATUS http_send_cache_control(char *, size_t) */
757 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
,
758 const size_t cc_len TSRMLS_DC
)
761 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
763 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
764 if (SUCCESS
!= (status
= http_send_header(cc_header
))) {
765 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
,
766 "Could not send '%s' header", cc_header
);
773 /* {{{ STATUS http_send_content_type(char *, size_t) */
774 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
,
775 const size_t ct_len TSRMLS_DC
)
780 if (!strchr(content_type
, '/')) {
781 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
782 "Content-Type '%s' doesn't seem to consist of a primary and a secondary part",
787 /* remember for multiple ranges */
789 efree(HTTP_G(ctype
));
791 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
793 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
794 sprintf(ct_header
, "Content-Type: %s", content_type
);
796 if (SUCCESS
!= (status
= http_send_header(ct_header
))) {
797 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
798 "Couldn't send '%s' header", ct_header
);
805 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
806 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
,
807 const size_t f_len
, const int send_inline TSRMLS_DC
)
813 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
814 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
816 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
817 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
820 if (SUCCESS
!= (status
= http_send_header(cd_header
))) {
821 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't send '%s' header", cd_header
);
828 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
829 PHP_HTTP_API STATUS
_http_cache_last_modified(const time_t last_modified
,
830 const time_t send_modified
, const char *cache_control
, const size_t cc_len TSRMLS_DC
)
833 http_send_cache_control(cache_control
, cc_len
);
836 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
837 if (SUCCESS
== http_send_status(304)) {
840 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
844 return http_send_last_modified(send_modified
);
848 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
849 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, const size_t etag_len
,
850 const char *cache_control
, const size_t cc_len TSRMLS_DC
)
853 http_send_cache_control(cache_control
, cc_len
);
857 http_send_etag(etag
, etag_len
);
858 if (http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
859 if (SUCCESS
== http_send_status(304)) {
862 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
868 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
869 if (!HTTP_G(etag_started
)) {
870 if (SUCCESS
== http_start_ob_handler(_http_ob_etaghandler
, "ob_etaghandler", 4096, 1)) {
871 HTTP_G(etag_started
) = 1;
874 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
882 /* {{{ char *http_absolute_uri(char *) */
883 PHP_HTTP_API
char *_http_absolute_uri_ex(
884 const char *url
, size_t url_len
,
885 const char *proto
, size_t proto_len
,
886 const char *host
, size_t host_len
,
887 unsigned port TSRMLS_DC
)
892 php_url
*purl
, furl
= {NULL
};
895 char *scheme
= NULL
, *URL
= ecalloc(1, HTTP_URI_MAXLEN
+ 1);
897 if ((!url
|| !url_len
) && (
898 (!(url
= SG(request_info
).request_uri
)) ||
899 (!(url_len
= strlen(SG(request_info
).request_uri
))))) {
900 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
901 "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
905 if (!(purl
= php_url_parse(url
))) {
906 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not parse supplied URL");
910 furl
.user
= purl
->user
;
911 furl
.pass
= purl
->pass
;
912 furl
.path
= purl
->path
;
913 furl
.query
= purl
->query
;
914 furl
.fragment
= purl
->fragment
;
917 furl
.scheme
= scheme
= estrdup(proto
);
918 } else if (purl
->scheme
) {
919 furl
.scheme
= purl
->scheme
;
921 } else if (port
&& (se
= getservbyport(htons(port
), "tcp"))) {
922 furl
.scheme
= (scheme
= estrdup(se
->s_name
));
925 furl
.scheme
= "http";
930 } else if (purl
->port
) {
931 furl
.port
= purl
->port
;
932 } else if (strncmp(furl
.scheme
, "http", 4)) {
934 if (se
= getservbyname(furl
.scheme
, "tcp")) {
935 furl
.port
= ntohs(se
->s_port
);
940 furl
.port
= (furl
.scheme
[5] == 's') ? 443 : 80;
944 furl
.host
= (char *) host
;
945 } else if (purl
->host
) {
946 furl
.host
= purl
->host
;
947 } else if ( (zhost
= http_get_server_var("HTTP_HOST")) ||
948 (zhost
= http_get_server_var("SERVER_NAME"))) {
949 furl
.host
= Z_STRVAL_P(zhost
);
951 furl
.host
= "localhost";
954 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
955 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
956 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
957 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
958 php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
959 "Absolute URI would have exceeded max URI length (%d bytes) - " \
960 "tried to add %d bytes ('%s')", \
961 HTTP_URI_MAXLEN, add_len, add_string); \
965 php_url_free(purl); \
968 strcat(URL, add_string); \
971 HTTP_URI_STRLCATL(URL
, full_len
, furl
.scheme
);
972 HTTP_URI_STRLCATS(URL
, full_len
, "://");
975 HTTP_URI_STRLCATL(URL
, full_len
, furl
.user
);
977 HTTP_URI_STRLCATS(URL
, full_len
, ":");
978 HTTP_URI_STRLCATL(URL
, full_len
, furl
.pass
);
980 HTTP_URI_STRLCATS(URL
, full_len
, "@");
983 HTTP_URI_STRLCATL(URL
, full_len
, furl
.host
);
985 if ( (strcmp(furl
.scheme
, "http") && (furl
.port
!= 80)) ||
986 (strcmp(furl
.scheme
, "https") && (furl
.port
!= 443))) {
987 char port_string
[8] = {0};
988 snprintf(port_string
, 7, ":%u", furl
.port
);
989 HTTP_URI_STRLCATL(URL
, full_len
, port_string
);
993 HTTP_URI_STRLCATL(URL
, full_len
, furl
.path
);
995 HTTP_URI_STRLCATS(URL
, full_len
, "/");
999 HTTP_URI_STRLCATS(URL
, full_len
, "?");
1000 HTTP_URI_STRLCATL(URL
, full_len
, furl
.query
);
1003 if (furl
.fragment
) {
1004 HTTP_URI_STRLCATS(URL
, full_len
, "#");
1005 HTTP_URI_STRLCATL(URL
, full_len
, furl
.fragment
);
1017 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1018 PHP_HTTP_API
char *_http_negotiate_q(const char *entry
, const zval
*supported
,
1019 const char *def TSRMLS_DC
)
1021 zval
*zaccept
, *zarray
, *zdelim
, **zentry
, *zentries
, **zsupp
;
1022 char *q_ptr
, *result
;
1026 HTTP_GSC(zaccept
, entry
, estrdup(def
));
1028 MAKE_STD_ZVAL(zarray
);
1031 MAKE_STD_ZVAL(zdelim
);
1032 ZVAL_STRING(zdelim
, ",", 0);
1033 php_explode(zdelim
, zaccept
, zarray
, -1);
1036 MAKE_STD_ZVAL(zentries
);
1037 array_init(zentries
);
1039 c
= zend_hash_num_elements(Z_ARRVAL_P(zarray
));
1040 for (i
= 0; i
< c
; i
++, zend_hash_move_forward(Z_ARRVAL_P(zarray
))) {
1042 if (SUCCESS
!= zend_hash_get_current_data(
1043 Z_ARRVAL_P(zarray
), (void **) &zentry
)) {
1044 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1045 "Cannot parse %s header: %s", entry
, Z_STRVAL_P(zaccept
));
1049 /* check for qualifier */
1050 if (NULL
!= (q_ptr
= strrchr(Z_STRVAL_PP(zentry
), ';'))) {
1051 qual
= strtod(q_ptr
+ 3, NULL
);
1056 /* walk through the supported array */
1057 FOREACH_VAL(supported
, zsupp
) {
1058 if (!strcasecmp(Z_STRVAL_PP(zsupp
), Z_STRVAL_PP(zentry
))) {
1059 add_assoc_double(zentries
, Z_STRVAL_PP(zsupp
), qual
);
1068 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries
));
1070 if ( (SUCCESS
!= zend_hash_sort(Z_ARRVAL_P(zentries
), zend_qsort
,
1071 http_sort_q
, 0 TSRMLS_CC
)) ||
1072 (HASH_KEY_NON_EXISTANT
== zend_hash_get_current_key(
1073 Z_ARRVAL_P(zentries
), &result
, 0, 1))) {
1074 result
= estrdup(def
);
1077 zval_dtor(zentries
);
1084 /* {{{ http_range_status http_get_request_ranges(HashTable *ranges, size_t) */
1085 PHP_HTTP_API http_range_status
_http_get_request_ranges(HashTable
*ranges
,
1086 const size_t length TSRMLS_DC
)
1090 long begin
= -1, end
= -1, *ptr
;
1092 HTTP_GSC(zrange
, "HTTP_RANGE", RANGE_NO
);
1093 range
= Z_STRVAL_P(zrange
);
1095 if (strncmp(range
, "bytes=", sizeof("bytes=") - 1)) {
1096 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Range header misses bytes=");
1101 range
+= sizeof("bytes=") - 1;
1104 switch (c
= *(range
++))
1110 case '1': case '2': case '3':
1111 case '4': case '5': case '6':
1112 case '7': case '8': case '9':
1114 * If the value of the pointer is already set (non-negative)
1115 * then multiply its value by ten and add the current value,
1116 * else initialise the pointers value with the current value
1118 * This let us recognize empty fields when validating the
1119 * ranges, i.e. a "-10" for begin and "12345" for the end
1120 * was the following range request: "Range: bytes=0-12345";
1121 * While a "-1" for begin and "12345" for the end would
1122 * have been: "Range: bytes=-12345".
1137 /* IE - ignore for now */
1144 /* validate ranges */
1149 if ((length
- end
) < 1) {
1157 if ((length
- end
) < 1) {
1160 begin
= length
- end
;
1170 if ((length
- begin
) < 1) {
1178 if ( ((length
- begin
) < 1) ||
1179 ((length
- end
) < 1) ||
1180 ((begin
- end
) >= 0)) {
1190 MAKE_STD_ZVAL(zentry
);
1192 add_index_long(zentry
, 0, begin
);
1193 add_index_long(zentry
, 1, end
);
1194 zend_hash_next_index_insert(ranges
, &zentry
, sizeof(zval
*), NULL
);
1212 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
1213 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, const size_t size
, const http_send_mode mode TSRMLS_DC
)
1215 long **begin
, **end
;
1219 if (zend_hash_num_elements(ranges
) == 1) {
1220 char range_header
[256] = {0};
1222 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
1223 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1224 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1228 /* Send HTTP 206 Partial Content */
1229 http_send_status(206);
1231 /* send content range header */
1232 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
1233 http_send_header(range_header
);
1235 /* send requested chunk */
1236 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1241 char bound
[23] = {0}, preface
[1024] = {0},
1242 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
1244 /* Send HTTP 206 Partial Content */
1245 http_send_status(206);
1247 /* send multipart/byteranges header */
1248 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
1249 strncat(multi_header
, bound
+ 2, 21);
1250 http_send_header(multi_header
);
1252 /* send each requested chunk */
1253 FOREACH_HASH_VAL(ranges
, zrange
) {
1254 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
1255 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
1259 snprintf(preface
, 1023,
1261 HTTP_CRLF
"Content-Type: %s"
1262 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
1267 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
1273 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
1274 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
1277 /* write boundary once more */
1278 php_body_write(HTTP_CRLF
, sizeof(HTTP_CRLF
) - 1 TSRMLS_CC
);
1279 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
1280 php_body_write("--", 2 TSRMLS_CC
);
1287 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
1288 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, const size_t data_size
,
1289 const http_send_mode data_mode TSRMLS_DC
)
1291 int is_range_request
= http_is_range_request();
1301 if (HTTP_G(etag_started
)) {
1304 HTTP_G(etag_started
) = 0;
1305 /* never ever use the output to compute the ETag if http_send() is used */
1306 php_end_ob_buffer(0, 0 TSRMLS_CC
);
1307 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
1311 /* send 304 Not Modified if etag matches */
1312 if ((!is_range_request
) && http_etag_match("HTTP_IF_NONE_MATCH", etag
)) {
1314 return http_send_status(304);
1317 http_send_etag(etag
, 32);
1321 /* send 304 Not Modified if last-modified matches*/
1322 if ((!is_range_request
) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
1323 return http_send_status(304);
1326 if (is_range_request
) {
1328 /* only send ranges if entity hasn't changed */
1330 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_MATCH", 13)) ||
1331 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag
)))
1333 ((!zend_hash_exists(HTTP_SERVER_VARS
, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1334 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
)))
1337 STATUS result
= FAILURE
;
1339 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1341 switch (http_get_request_ranges(&ranges
, data_size
))
1344 zend_hash_destroy(&ranges
);
1345 /* go ahead and send all */
1349 result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
1350 zend_hash_destroy(&ranges
);
1355 zend_hash_destroy(&ranges
);
1356 http_send_status(416);
1367 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
1371 /* {{{ STATUS http_send_stream(php_stream *) */
1372 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
,
1373 zend_bool close_stream TSRMLS_DC
)
1377 if ((!file
) || php_stream_stat(file
, &HTTP_G(ssb
))) {
1381 status
= http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
1384 php_stream_close(file
);
1391 /* {{{ STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1392 PHP_HTTP_API STATUS
_http_chunked_decode(const char *encoded
,
1393 const size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
1399 *decoded
= ecalloc(1, encoded_len
);
1403 while (((e_ptr
- encoded
) - encoded_len
) > 0) {
1404 char hex_len
[9] = {0};
1405 size_t chunk_len
= 0;
1408 /* read in chunk size */
1409 while (isxdigit(*e_ptr
)) {
1411 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1412 "Chunk size is too long: 0x%s...", hex_len
);
1416 hex_len
[i
++] = *e_ptr
++;
1419 /* reached the end */
1420 if (!strcmp(hex_len
, "0")) {
1425 if (strncmp(e_ptr
, HTTP_CRLF
, 2)) {
1426 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1427 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1428 *e_ptr
, *(e_ptr
+ 1));
1436 chunk_len
= strtol(hex_len
, &error
, 16);
1437 if (error
== hex_len
) {
1438 php_error_docref(NULL TSRMLS_CC
, E_WARNING
,
1439 "Invalid chunk size string: '%s'", hex_len
);
1445 memcpy(d_ptr
, e_ptr
+= 2, chunk_len
);
1447 e_ptr
+= chunk_len
+ 2;
1448 *decoded_len
+= chunk_len
;
1455 /* {{{ STATUS http_split_response(zval *, zval *, zval *) */
1456 PHP_HTTP_API STATUS
_http_split_response(zval
*response
, zval
*headers
, zval
*body TSRMLS_DC
)
1460 STATUS status
= http_split_response_ex(Z_STRVAL_P(response
), Z_STRLEN_P(response
), Z_ARRVAL_P(headers
), &b
, &l
);
1461 ZVAL_STRINGL(body
, b
, l
, 0);
1466 /* {{{ STATUS http_split_response(char *, size_t, HashTable *, char **, size_t *) */
1467 PHP_HTTP_API STATUS
_http_split_response_ex(char *response
,
1468 size_t response_len
, HashTable
*headers
, char **body
, size_t *body_len TSRMLS_DC
)
1470 char *header
= response
, *real_body
= NULL
;
1472 while (0 < (response_len
- (response
- header
+ 4))) {
1473 if ( (*response
++ == '\r') &&
1474 (*response
++ == '\n') &&
1475 (*response
++ == '\r') &&
1476 (*response
++ == '\n')) {
1477 real_body
= response
;
1482 if (real_body
&& (*body_len
= (response_len
- (real_body
- header
)))) {
1483 *body
= ecalloc(1, *body_len
+ 1);
1484 memcpy(*body
, real_body
, *body_len
);
1487 return http_parse_headers_ex(header
, real_body
? response_len
- *body_len
: response_len
, headers
, 1);
1491 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1492 PHP_HTTP_API STATUS
_http_parse_headers_ex(char *header
, int header_len
,
1493 HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1495 char *colon
= NULL
, *line
= NULL
, *begin
= header
;
1498 Z_ARRVAL(array
) = headers
;
1500 if (header_len
< 2) {
1505 if (!strncmp(header
, "HTTP/1.", 7)) {
1506 char *end
= strstr(header
, HTTP_CRLF
);
1507 add_assoc_stringl(&array
, "Status",
1508 header
+ sizeof("HTTP/1.x ") - 1,
1509 end
- (header
+ sizeof("HTTP/1.x ") - 1), 1);
1515 while (header_len
>= (line
- begin
)) {
1521 --value_len
; /* we don't have CR so value length is one char less */
1523 if (colon
&& ((!(*line
- 1)) || ((*line
!= ' ') && (*line
!= '\t')))) {
1525 /* skip empty key */
1526 if (header
!= colon
) {
1527 char *key
= estrndup(header
, colon
- header
);
1530 key
= pretty_key(key
, colon
- header
, 1, 1);
1533 value_len
+= line
- colon
- 1;
1535 /* skip leading ws */
1536 while (isspace(*(++colon
))) --value_len
;
1537 /* skip trailing ws */
1538 while (isspace(colon
[value_len
- 1])) --value_len
;
1540 if (value_len
< 1) {
1541 /* hm, empty header? */
1542 add_assoc_stringl(&array
, key
, "", 0, 1);
1544 add_assoc_stringl(&array
, key
, colon
, value_len
, 1);
1551 header
+= line
- header
;
1566 /* {{{ void http_get_request_headers_ex(HashTable *, zend_bool) */
1567 PHP_HTTP_API
void _http_get_request_headers_ex(HashTable
*headers
, zend_bool prettify TSRMLS_DC
)
1573 Z_ARRVAL(array
) = headers
;
1575 FOREACH_HASH_KEY(HTTP_SERVER_VARS
, key
, idx
) {
1576 if (key
&& !strncmp(key
, "HTTP_", 5)) {
1580 key
= pretty_key(key
+ 5, strlen(key
) - 5, 1, 1);
1583 zend_hash_get_current_data(HTTP_SERVER_VARS
, (void **) &header
);
1584 add_assoc_stringl(&array
, key
, Z_STRVAL_PP(header
), Z_STRLEN_PP(header
), 1);
1591 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, int, char **, size_t *) */
1592 PHP_HTTP_API STATUS
_http_urlencode_hash_ex(HashTable
*hash
, int override_argsep
,
1593 char *pre_encoded_data
, size_t pre_encoded_len
,
1594 char **encoded_data
, size_t *encoded_len TSRMLS_DC
)
1596 smart_str qstr
= {0};
1598 if (override_argsep
) {
1599 HTTP_URL_ARGSEP_OVERRIDE
;
1602 if (pre_encoded_len
&& pre_encoded_data
) {
1603 smart_str_appendl(&qstr
, pre_encoded_data
, pre_encoded_len
);
1606 if (SUCCESS
!= php_url_encode_hash_ex(hash
, &qstr
, NULL
, 0, NULL
, 0, NULL
, 0, NULL TSRMLS_CC
)) {
1607 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Couldn't encode query data");
1611 if (override_argsep
) {
1612 HTTP_URL_ARGSEP_RESTORE
;
1617 if (override_argsep
) {
1618 HTTP_URL_ARGSEP_RESTORE
;
1623 *encoded_data
= qstr
.c
;
1625 *encoded_len
= qstr
.len
;
1632 /* {{{ STATUS http_auth_header(char *, char*) */
1633 PHP_HTTP_API STATUS
_http_auth_header(const char *type
, const char *realm TSRMLS_DC
)
1635 char realm_header
[1024] = {0};
1636 snprintf(realm_header
, 1023, "WWW-Authenticate: %s realm=\"%s\"", type
, realm
);
1637 return http_send_status_header(401, realm_header
);
1641 /* {{{ STATUS http_auth_credentials(char **, char **) */
1642 PHP_HTTP_API STATUS
_http_auth_credentials(char **user
, char **pass TSRMLS_DC
)
1644 if (strncmp(sapi_module
.name
, "isapi", 5)) {
1645 zval
*zuser
, *zpass
;
1647 HTTP_GSC(zuser
, "PHP_AUTH_USER", FAILURE
);
1648 HTTP_GSC(zpass
, "PHP_AUTH_PW", FAILURE
);
1650 *user
= estrndup(Z_STRVAL_P(zuser
), Z_STRLEN_P(zuser
));
1651 *pass
= estrndup(Z_STRVAL_P(zpass
), Z_STRLEN_P(zpass
));
1656 HTTP_GSC(zauth
, "HTTP_AUTHORIZATION", FAILURE
);
1658 char *decoded
, *colon
;
1660 decoded
= php_base64_decode(Z_STRVAL_P(zauth
), Z_STRLEN_P(zauth
),
1663 if (colon
= strchr(decoded
+ 6, ':')) {
1664 *user
= estrndup(decoded
+ 6, colon
- decoded
- 6);
1665 *pass
= estrndup(colon
+ 1, decoded
+ decoded_len
- colon
- 6 - 1);
1676 #ifndef ZEND_ENGINE_2
1677 /* {{{ php_url_encode_hash
1678 Author: Sara Golemon <pollita@php.net> */
1679 PHP_HTTP_API STATUS
php_url_encode_hash_ex(HashTable
*ht
, smart_str
*formstr
,
1680 const char *num_prefix
, int num_prefix_len
,
1681 const char *key_prefix
, int key_prefix_len
,
1682 const char *key_suffix
, int key_suffix_len
,
1683 zval
*type TSRMLS_DC
)
1685 char *arg_sep
= NULL
, *key
= NULL
, *ekey
, *newprefix
, *p
;
1686 int arg_sep_len
, key_len
, ekey_len
, key_type
, newprefix_len
;
1688 zval
**zdata
= NULL
, *copyzval
;
1694 if (ht
->nApplyCount
> 0) {
1695 /* Prevent recursion */
1699 arg_sep
= INI_STR("arg_separator.output");
1700 if (!arg_sep
|| !strlen(arg_sep
)) {
1701 arg_sep
= HTTP_URL_ARGSEP_DEFAULT
;
1703 arg_sep_len
= strlen(arg_sep
);
1705 for (zend_hash_internal_pointer_reset(ht
);
1706 (key_type
= zend_hash_get_current_key_ex(ht
, &key
, &key_len
, &idx
, 0, NULL
)) != HASH_KEY_NON_EXISTANT
;
1707 zend_hash_move_forward(ht
)
1709 if (key_type
== HASH_KEY_IS_STRING
&& key_len
&& key
[key_len
-1] == '\0') {
1710 /* We don't want that trailing NULL */
1714 #ifdef ZEND_ENGINE_2
1715 /* handling for private & protected object properties */
1716 if (key
&& *key
== '\0' && type
!= NULL
) {
1719 zend_object
*zobj
= zend_objects_get_address(type TSRMLS_CC
);
1720 if (zend_check_property_access(zobj
, key TSRMLS_CC
) != SUCCESS
) {
1721 /* private or protected property access outside of the class */
1724 zend_unmangle_property_name(key
, &tmp
, &key
);
1725 key_len
= strlen(key
);
1729 if (zend_hash_get_current_data_ex(ht
, (void **)&zdata
, NULL
) == FAILURE
|| !zdata
|| !(*zdata
)) {
1730 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Error traversing form data array.");
1733 if (Z_TYPE_PP(zdata
) == IS_ARRAY
|| Z_TYPE_PP(zdata
) == IS_OBJECT
) {
1734 if (key_type
== HASH_KEY_IS_STRING
) {
1735 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1736 newprefix_len
= key_suffix_len
+ ekey_len
+ key_prefix_len
+ 1;
1737 newprefix
= emalloc(newprefix_len
+ 1);
1741 memcpy(p
, key_prefix
, key_prefix_len
);
1742 p
+= key_prefix_len
;
1745 memcpy(p
, ekey
, ekey_len
);
1750 memcpy(p
, key_suffix
, key_suffix_len
);
1751 p
+= key_suffix_len
;
1757 /* Is an integer key */
1758 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1759 newprefix_len
= key_prefix_len
+ num_prefix_len
+ ekey_len
+ key_suffix_len
+ 1;
1760 newprefix
= emalloc(newprefix_len
+ 1);
1764 memcpy(p
, key_prefix
, key_prefix_len
);
1765 p
+= key_prefix_len
;
1768 memcpy(p
, num_prefix
, num_prefix_len
);
1769 p
+= num_prefix_len
;
1771 memcpy(p
, ekey
, ekey_len
);
1776 memcpy(p
, key_suffix
, key_suffix_len
);
1777 p
+= key_suffix_len
;
1783 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
);
1786 } else if (Z_TYPE_PP(zdata
) == IS_NULL
|| Z_TYPE_PP(zdata
) == IS_RESOURCE
) {
1787 /* Skip these types */
1791 smart_str_appendl(formstr
, arg_sep
, arg_sep_len
);
1793 /* Simple key=value */
1794 smart_str_appendl(formstr
, key_prefix
, key_prefix_len
);
1795 if (key_type
== HASH_KEY_IS_STRING
) {
1796 ekey
= php_url_encode(key
, key_len
, &ekey_len
);
1797 smart_str_appendl(formstr
, ekey
, ekey_len
);
1802 smart_str_appendl(formstr
, num_prefix
, num_prefix_len
);
1804 ekey_len
= spprintf(&ekey
, 12, "%ld", idx
);
1805 smart_str_appendl(formstr
, ekey
, ekey_len
);
1808 smart_str_appendl(formstr
, key_suffix
, key_suffix_len
);
1809 smart_str_appendl(formstr
, "=", 1);
1810 switch (Z_TYPE_PP(zdata
)) {
1812 ekey
= php_url_encode(Z_STRVAL_PP(zdata
), Z_STRLEN_PP(zdata
), &ekey_len
);
1816 ekey_len
= spprintf(&ekey
, 12, "%ld", Z_LVAL_PP(zdata
));
1819 ekey_len
= spprintf(&ekey
, 48, "%.*G", (int) EG(precision
), Z_DVAL_PP(zdata
));
1822 /* fall back on convert to string */
1823 MAKE_STD_ZVAL(copyzval
);
1824 *copyzval
= **zdata
;
1825 zval_copy_ctor(copyzval
);
1826 convert_to_string_ex(©zval
);
1827 ekey
= php_url_encode(Z_STRVAL_P(copyzval
), Z_STRLEN_P(copyzval
), &ekey_len
);
1828 zval_ptr_dtor(©zval
);
1830 smart_str_appendl(formstr
, ekey
, ekey_len
);
1838 #endif /* !ZEND_ENDGINE_2 */
1840 /* }}} public API */
1847 * vim600: noet sw=4 ts=4 fdm=marker
1848 * vim<600: noet sw=4 ts=4