* fix fatal erealloc error
[m6w6/ext-http] / http_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
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 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #define _WINSOCKAPI_
19 #define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <ctype.h>
26
27 #include "php.h"
28 #include "php_version.h"
29 #include "php_streams.h"
30 #include "snprintf.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"
37
38 #include "SAPI.h"
39
40 #ifdef ZEND_ENGINE_2
41 # include "ext/standard/php_http.h"
42 #else
43 #include "http_build_query.c"
44 #endif
45
46 #include "php_http.h"
47 #include "php_http_api.h"
48
49 #ifdef HTTP_HAVE_CURL
50
51 # ifdef PHP_WIN32
52 # include <winsock2.h>
53 # include <sys/types.h>
54 # endif
55
56 # include <curl/curl.h>
57 # include <curl/easy.h>
58
59 #endif
60
61
62 ZEND_DECLARE_MODULE_GLOBALS(http)
63
64 /* {{{ day/month names */
65 static const char *days[] = {
66 "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
67 };
68 static const char *wkdays[] = {
69 "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
70 };
71 static const char *weekdays[] = {
72 "Monday", "Tuesday", "Wednesday",
73 "Thursday", "Friday", "Saturday", "Sunday"
74 };
75 static const char *months[] = {
76 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
77 "Jul", "Aug", "Sep", "Okt", "Nov", "Dec"
78 };
79 enum assume_next {
80 DATE_MDAY,
81 DATE_YEAR,
82 DATE_TIME
83 };
84 static const struct time_zone {
85 const char *name;
86 const int offset;
87 } time_zones[] = {
88 {"GMT", 0}, /* Greenwich Mean */
89 {"UTC", 0}, /* Universal (Coordinated) */
90 {"WET", 0}, /* Western European */
91 {"BST", 0}, /* British Summer */
92 {"WAT", 60}, /* West Africa */
93 {"AST", 240}, /* Atlantic Standard */
94 {"ADT", 240}, /* Atlantic Daylight */
95 {"EST", 300}, /* Eastern Standard */
96 {"EDT", 300}, /* Eastern Daylight */
97 {"CST", 360}, /* Central Standard */
98 {"CDT", 360}, /* Central Daylight */
99 {"MST", 420}, /* Mountain Standard */
100 {"MDT", 420}, /* Mountain Daylight */
101 {"PST", 480}, /* Pacific Standard */
102 {"PDT", 480}, /* Pacific Daylight */
103 {"YST", 540}, /* Yukon Standard */
104 {"YDT", 540}, /* Yukon Daylight */
105 {"HST", 600}, /* Hawaii Standard */
106 {"HDT", 600}, /* Hawaii Daylight */
107 {"CAT", 600}, /* Central Alaska */
108 {"AHST", 600}, /* Alaska-Hawaii Standard */
109 {"NT", 660}, /* Nome */
110 {"IDLW", 720}, /* International Date Line West */
111 {"CET", -60}, /* Central European */
112 {"MET", -60}, /* Middle European */
113 {"MEWT", -60}, /* Middle European Winter */
114 {"MEST", -120}, /* Middle European Summer */
115 {"CEST", -120}, /* Central European Summer */
116 {"MESZ", -60}, /* Middle European Summer */
117 {"FWT", -60}, /* French Winter */
118 {"FST", -60}, /* French Summer */
119 {"EET", -120}, /* Eastern Europe, USSR Zone 1 */
120 {"WAST", -420}, /* West Australian Standard */
121 {"WADT", -420}, /* West Australian Daylight */
122 {"CCT", -480}, /* China Coast, USSR Zone 7 */
123 {"JST", -540}, /* Japan Standard, USSR Zone 8 */
124 {"EAST", -600}, /* Eastern Australian Standard */
125 {"EADT", -600}, /* Eastern Australian Daylight */
126 {"GST", -600}, /* Guam Standard, USSR Zone 9 */
127 {"NZT", -720}, /* New Zealand */
128 {"NZST", -720}, /* New Zealand Standard */
129 {"NZDT", -720}, /* New Zealand Daylight */
130 {"IDLE", -720}, /* International Date Line East */
131 };
132 /* }}} */
133
134 /* {{{ internals */
135
136 static int http_sort_q(const void *a, const void *b TSRMLS_DC);
137 #define http_etag(e, p, l, m) _http_etag((e), (p), (l), (m) TSRMLS_CC)
138 static inline char *_http_etag(char **new_etag, const void *data_ptr, const size_t data_len, const http_send_mode data_mode TSRMLS_DC);
139 #define http_is_range_request() _http_is_range_request(TSRMLS_C)
140 static inline int _http_is_range_request(TSRMLS_D);
141 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
142 static STATUS _http_send_chunk(const void *data, const size_t begin, const size_t end, const http_send_mode mode TSRMLS_DC);
143
144 static int check_day(char *day, size_t len);
145 static int check_month(char *month);
146 static int check_tzone(char *tzone);
147
148 static char *pretty_key(char *key, int key_len, int uctitle, int xhyphen);
149
150 static int http_ob_stack_get(php_ob_buffer *, php_ob_buffer **);
151
152 /* {{{ HAVE_CURL */
153 #ifdef HTTP_HAVE_CURL
154 #define http_curl_initbuf(m) _http_curl_initbuf((m) TSRMLS_CC)
155 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC);
156 #define http_curl_freebuf(m) _http_curl_freebuf((m) TSRMLS_CC)
157 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC);
158 #define http_curl_sizebuf(m, l) _http_curl_sizebuf((m), (l) TSRMLS_CC)
159 static inline void _http_curl_sizebuf(http_curlbuf_member member, size_t len TSRMLS_DC);
160 #define http_curl_movebuf(m, d, l) _http_curl_movebuf((m), (d), (l) TSRMLS_CC)
161 static inline void _http_curl_movebuf(http_curlbuf_member member, char **data, size_t *data_len TSRMLS_DC);
162 #define http_curl_copybuf(m, d, l) _http_curl_copybuf((m), (d), (l) TSRMLS_CC)
163 static inline void _http_curl_copybuf(http_curlbuf_member member, char **data, size_t *data_len TSRMLS_DC);
164 #define http_curl_setopts(c, u, o) _http_curl_setopts((c), (u), (o) TSRMLS_CC)
165 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC);
166
167 #define http_curl_getopt(o, k) _http_curl_getopt((o), (k) TSRMLS_CC, 0)
168 #define http_curl_getopt1(o, k, t1) _http_curl_getopt((o), (k) TSRMLS_CC, 1, (t1))
169 #define http_curl_getopt2(o, k, t1, t2) _http_curl_getopt((o), (k) TSRMLS_CC, 2, (t1), (t2))
170 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...);
171
172 static size_t http_curl_body_callback(char *, size_t, size_t, void *);
173 static size_t http_curl_hdrs_callback(char *, size_t, size_t, void *);
174
175 #define http_curl_getinfo(c, h) _http_curl_getinfo((c), (h) TSRMLS_CC)
176 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC);
177 #define http_curl_getinfo_ex(c, i, a) _http_curl_getinfo_ex((c), (i), (a) TSRMLS_CC)
178 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC);
179 #define http_curl_getinfoname(i) _http_curl_getinfoname((i) TSRMLS_CC)
180 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC);
181
182 #endif
183 /* }}} HAVE_CURL */
184
185 /* {{{ static int http_sort_q(const void *, const void *) */
186 static int http_sort_q(const void *a, const void *b TSRMLS_DC)
187 {
188 Bucket *f, *s;
189 zval result, *first, *second;
190
191 f = *((Bucket **) a);
192 s = *((Bucket **) b);
193
194 first = *((zval **) f->pData);
195 second= *((zval **) s->pData);
196
197 if (numeric_compare_function(&result, first, second TSRMLS_CC) != SUCCESS) {
198 return 0;
199 }
200 return (Z_LVAL(result) > 0 ? -1 : (Z_LVAL(result) < 0 ? 1 : 0));
201 }
202 /* }}} */
203
204 /* {{{ static inline char *http_etag(char **, void *, size_t, http_send_mode) */
205 static inline char *_http_etag(char **new_etag, const void *data_ptr,
206 const size_t data_len, const http_send_mode data_mode TSRMLS_DC)
207 {
208 char ssb_buf[127];
209 unsigned char digest[16];
210 PHP_MD5_CTX ctx;
211
212 PHP_MD5Init(&ctx);
213
214 switch (data_mode)
215 {
216 case SEND_DATA:
217 PHP_MD5Update(&ctx, data_ptr, data_len);
218 break;
219
220 case SEND_RSRC:
221 snprintf(ssb_buf, 127, "%l=%l=%l",
222 HTTP_G(ssb).sb.st_mtime,
223 HTTP_G(ssb).sb.st_ino,
224 HTTP_G(ssb).sb.st_size
225 );
226 PHP_MD5Update(&ctx, ssb_buf, strlen(ssb_buf));
227 break;
228
229 default:
230 return NULL;
231 break;
232 }
233
234 PHP_MD5Final(digest, &ctx);
235 make_digest(*new_etag, digest);
236
237 return *new_etag;
238 }
239 /* }}} */
240
241 /* {{{ static inline int http_is_range_request(void) */
242 static inline int _http_is_range_request(TSRMLS_D)
243 {
244 return zend_hash_exists(Z_ARRVAL_P(PG(http_globals)[TRACK_VARS_SERVER]),
245 "HTTP_RANGE", strlen("HTTP_RANGE") + 1);
246 }
247 /* }}} */
248
249 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t,
250 http_send_mode) */
251 static STATUS _http_send_chunk(const void *data, const size_t begin,
252 const size_t end, const http_send_mode mode TSRMLS_DC)
253 {
254 char *buf;
255 size_t read = 0;
256 long len = end - begin;
257 php_stream *s;
258
259 switch (mode)
260 {
261 case SEND_RSRC:
262 s = (php_stream *) data;
263 if (php_stream_seek(s, begin, SEEK_SET)) {
264 return FAILURE;
265 }
266 buf = (char *) ecalloc(1, HTTP_BUF_SIZE);
267 /* read into buf and write out */
268 while ((len -= HTTP_BUF_SIZE) >= 0) {
269 if (!(read = php_stream_read(s, buf, HTTP_BUF_SIZE))) {
270 efree(buf);
271 return FAILURE;
272 }
273 if (read - php_body_write(buf, read TSRMLS_CC)) {
274 efree(buf);
275 return FAILURE;
276 }
277 }
278
279 /* read & write left over */
280 if (len) {
281 if (read = php_stream_read(s, buf, HTTP_BUF_SIZE + len)) {
282 if (read - php_body_write(buf, read TSRMLS_CC)) {
283 efree(buf);
284 return FAILURE;
285 }
286 } else {
287 efree(buf);
288 return FAILURE;
289 }
290 }
291 efree(buf);
292 return SUCCESS;
293 break;
294
295 case SEND_DATA:
296 return len == php_body_write(((char *)data) + begin, len TSRMLS_CC)
297 ? SUCCESS : FAILURE;
298 break;
299
300 default:
301 return FAILURE;
302 break;
303 }
304 }
305 /* }}} */
306
307 /* {{{ HAVE_CURL */
308 #ifdef HTTP_HAVE_CURL
309
310 /* {{{ static inline void http_curl_initbuf(http_curlbuf_member) */
311 static inline void _http_curl_initbuf(http_curlbuf_member member TSRMLS_DC)
312 {
313 http_curl_freebuf(member);
314
315 if (member & CURLBUF_HDRS) {
316 HTTP_G(curlbuf).hdrs.data = emalloc(HTTP_CURLBUF_HDRSSIZE);
317 HTTP_G(curlbuf).hdrs.free = HTTP_CURLBUF_HDRSSIZE;
318 }
319 if (member & CURLBUF_BODY) {
320 HTTP_G(curlbuf).body.data = emalloc(HTTP_CURLBUF_BODYSIZE);
321 HTTP_G(curlbuf).body.free = HTTP_CURLBUF_BODYSIZE;
322 }
323 }
324 /* }}} */
325
326 /* {{{ static inline void http_curl_freebuf(http_curlbuf_member) */
327 static inline void _http_curl_freebuf(http_curlbuf_member member TSRMLS_DC)
328 {
329 if (member & CURLBUF_HDRS) {
330 if (HTTP_G(curlbuf).hdrs.data) {
331 efree(HTTP_G(curlbuf).hdrs.data);
332 HTTP_G(curlbuf).hdrs.data = NULL;
333 }
334 HTTP_G(curlbuf).hdrs.used = 0;
335 HTTP_G(curlbuf).hdrs.free = 0;
336 }
337 if (member & CURLBUF_BODY) {
338 if (HTTP_G(curlbuf).body.data) {
339 efree(HTTP_G(curlbuf).body.data);
340 HTTP_G(curlbuf).body.data = NULL;
341 }
342 HTTP_G(curlbuf).body.used = 0;
343 HTTP_G(curlbuf).body.free = 0;
344 }
345 }
346 /* }}} */
347
348 /* {{{ static inline void http_curl_copybuf(http_curlbuf_member, char **,
349 size_t *) */
350 static inline void _http_curl_copybuf(http_curlbuf_member member, char **data,
351 size_t *data_len TSRMLS_DC)
352 {
353 *data = NULL;
354 *data_len = 0;
355
356 if ((member & CURLBUF_HDRS) && HTTP_G(curlbuf).hdrs.used) {
357 if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
358 *data = emalloc(HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used + 1);
359 } else {
360 *data = emalloc(HTTP_G(curlbuf).hdrs.used + 1);
361 }
362 memcpy(*data, HTTP_G(curlbuf).hdrs.data, HTTP_G(curlbuf).hdrs.used);
363 *data_len = HTTP_G(curlbuf).hdrs.used;
364 }
365
366 if ((member & CURLBUF_BODY) && HTTP_G(curlbuf).body.used) {
367 if (*data) {
368 memcpy((*data) + HTTP_G(curlbuf).hdrs.used,
369 HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
370 *data_len = HTTP_G(curlbuf).hdrs.used + HTTP_G(curlbuf).body.used;
371 } else {
372 emalloc(HTTP_G(curlbuf).body.used + 1);
373 memcpy(*data, HTTP_G(curlbuf).body.data, HTTP_G(curlbuf).body.used);
374 *data_len = HTTP_G(curlbuf).body.used;
375 }
376 }
377 if (*data) {
378 (*data)[*data_len] = 0;
379 } else {
380 *data = "";
381 }
382 }
383 /* }}} */
384
385 /* {{{ static inline void http_curl_movebuf(http_curlbuf_member, char **,
386 size_t *) */
387 static inline void _http_curl_movebuf(http_curlbuf_member member, char **data,
388 size_t *data_len TSRMLS_DC)
389 {
390 http_curl_copybuf(member, data, data_len);
391 http_curl_freebuf(member);
392 }
393 /* }}} */
394
395 /* {{{ static size_t http_curl_body_callback(char *, size_t, size_t, void *) */
396 static size_t http_curl_body_callback(char *buf, size_t len, size_t n, void *s)
397 {
398 TSRMLS_FETCH();
399
400 if ((len *= n) > HTTP_G(curlbuf).body.free) {
401 size_t bsize = HTTP_CURLBUF_BODYSIZE;
402 while (bsize < len) {
403 bsize *= 2;
404 }
405 HTTP_G(curlbuf).body.data = erealloc(HTTP_G(curlbuf).body.data,
406 HTTP_G(curlbuf).body.used + bsize);
407 HTTP_G(curlbuf).body.free += bsize;
408 }
409
410 memcpy(HTTP_G(curlbuf).body.data + HTTP_G(curlbuf).body.used, buf, len);
411 HTTP_G(curlbuf).body.free -= len;
412 HTTP_G(curlbuf).body.used += len;
413
414 return len;
415 }
416 /* }}} */
417
418 /* {{{ static size_t http_curl_hdrs_callback(char*, size_t, size_t, void *) */
419 static size_t http_curl_hdrs_callback(char *buf, size_t len, size_t n, void *s)
420 {
421 TSRMLS_FETCH();
422
423 /* discard previous headers */
424 if ((HTTP_G(curlbuf).hdrs.used) && (!strncmp(buf, "HTTP/1.", strlen("HTTP/1.")))) {
425 http_curl_initbuf(CURLBUF_HDRS);
426 }
427
428 if ((len *= n) > HTTP_G(curlbuf).hdrs.free) {
429 size_t bsize = HTTP_CURLBUF_HDRSSIZE;
430 while (bsize < len) {
431 bsize *= 2;
432 }
433 HTTP_G(curlbuf).hdrs.data = erealloc(HTTP_G(curlbuf).hdrs.data,
434 HTTP_G(curlbuf).hdrs.used + bsize);
435 HTTP_G(curlbuf).hdrs.free += bsize;
436 }
437
438 memcpy(HTTP_G(curlbuf).hdrs.data + HTTP_G(curlbuf).hdrs.used, buf, len);
439 HTTP_G(curlbuf).hdrs.free -= len;
440 HTTP_G(curlbuf).hdrs.used += len;
441
442 return len;
443 }
444 /* }}} */
445
446 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, int, ...) */
447 static inline zval *_http_curl_getopt(HashTable *options, char *key TSRMLS_DC, int checks, ...)
448 {
449 zval **zoption;
450 va_list types;
451 int i;
452
453 if (SUCCESS != zend_hash_find(options, key, strlen(key) + 1, (void **) &zoption)) {
454 return NULL;
455 }
456 if (checks < 1) {
457 return *zoption;
458 }
459
460 va_start(types, checks);
461 for (i = 0; i < checks; ++i) {
462 if ((va_arg(types, int)) == (Z_TYPE_PP(zoption))) {
463 va_end(types);
464 return *zoption;
465 }
466 }
467 va_end(types);
468 return NULL;
469 }
470 /* }}} */
471
472 /* {{{ static inline void http_curl_setopts(CURL *, char *, HashTable *) */
473 static inline void _http_curl_setopts(CURL *ch, const char *url, HashTable *options TSRMLS_DC)
474 {
475 zval *zoption;
476
477 /* standard options */
478 curl_easy_setopt(ch, CURLOPT_URL, url);
479 curl_easy_setopt(ch, CURLOPT_HEADER, 0);
480 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1);
481 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1);
482 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_body_callback);
483 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, http_curl_hdrs_callback);
484 #ifdef ZTS
485 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1);
486 #endif
487
488 if ((!options) || (1 > zend_hash_num_elements(options))) {
489 return;
490 }
491
492 /* redirects, defaults to 0 */
493 if (zoption = http_curl_getopt1(options, "redirect", IS_LONG)) {
494 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
495 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
496 if (zoption = http_curl_getopt2(options, "unrestrictedauth", IS_LONG, IS_BOOL)) {
497 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
498 }
499 } else {
500 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0);
501 }
502
503 /* proxy */
504 if (zoption = http_curl_getopt1(options, "proxyhost", IS_STRING)) {
505 curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
506 /* port */
507 if (zoption = http_curl_getopt1(options, "proxyport", IS_LONG)) {
508 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
509 }
510 /* user:pass */
511 if (zoption = http_curl_getopt1(options, "proxyauth", IS_STRING)) {
512 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
513 }
514 /* auth method */
515 if (zoption = http_curl_getopt1(options, "proxyauthtype", IS_LONG)) {
516 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
517 }
518 }
519
520 /* auth */
521 if (zoption = http_curl_getopt1(options, "httpauth", IS_STRING)) {
522 curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
523 }
524 if (zoption = http_curl_getopt1(options, "httpauthtype", IS_LONG)) {
525 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
526 }
527
528 /* compress, enabled by default (empty string enables deflate and gzip) */
529 if (zoption = http_curl_getopt2(options, "compress", IS_LONG, IS_BOOL)) {
530 if (Z_LVAL_P(zoption)) {
531 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
532 }
533 } else {
534 curl_easy_setopt(ch, CURLOPT_ENCODING, "");
535 }
536
537 /* another port */
538 if (zoption = http_curl_getopt1(options, "port", IS_LONG)) {
539 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
540 }
541
542 /* referer */
543 if (zoption = http_curl_getopt1(options, "referer", IS_STRING)) {
544 curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
545 }
546
547 /* useragent, default "PECL::HTTP/version (PHP/version)" */
548 if (zoption = http_curl_getopt1(options, "useragent", IS_STRING)) {
549 curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
550 } else {
551 curl_easy_setopt(ch, CURLOPT_USERAGENT,
552 "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
553 }
554
555 /* cookies, array('name' => 'value') */
556 if (zoption = http_curl_getopt1(options, "cookies", IS_ARRAY)) {
557 char *cookie_key;
558 zval **cookie_val;
559 int key_type;
560 smart_str qstr = {0};
561
562 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
563 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
564 if (key_type == HASH_KEY_IS_STRING) {
565 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &cookie_key, NULL, 0);
566 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val);
567 smart_str_appends(&qstr, cookie_key);
568 smart_str_appendl(&qstr, "=", 1);
569 smart_str_appendl(&qstr, Z_STRVAL_PP(cookie_val), Z_STRLEN_PP(cookie_val));
570 smart_str_appendl(&qstr, "; ", 2);
571 zend_hash_move_forward(Z_ARRVAL_P(zoption));
572 }
573 }
574 smart_str_0(&qstr);
575
576 if (qstr.c) {
577 curl_easy_setopt(ch, CURLOPT_COOKIE, qstr.c);
578 }
579 }
580
581 /* cookiestore */
582 if (zoption = http_curl_getopt1(options, "cookiestore", IS_STRING)) {
583 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
584 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
585 }
586
587 /* additional headers, array('name' => 'value') */
588 if (zoption = http_curl_getopt1(options, "headers", IS_ARRAY)) {
589 int key_type;
590 char *header_key, header[1024] = {0};
591 zval **header_val;
592 struct curl_slist *headers = NULL;
593
594 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
595 while (HASH_KEY_NON_EXISTANT != (key_type = zend_hash_get_current_key_type(Z_ARRVAL_P(zoption)))) {
596 if (key_type == HASH_KEY_IS_STRING) {
597 zend_hash_get_current_key(Z_ARRVAL_P(zoption), &header_key, NULL, 0);
598 zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val);
599 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
600 headers = curl_slist_append(headers, header);
601 zend_hash_move_forward(Z_ARRVAL_P(zoption));
602 }
603 }
604 if (headers) {
605 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headers);
606 }
607 }
608 }
609 /* }}} */
610
611 /* {{{ static inline char *http_curl_getinfoname(CURLINFO) */
612 static inline char *_http_curl_getinfoname(CURLINFO i TSRMLS_DC)
613 {
614 #define CASE(I) case CURLINFO_ ##I : return pretty_key(estrdup( #I ), strlen(#I), 0, 0)
615 switch (i)
616 {
617 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
618 CASE(EFFECTIVE_URL);
619 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
620 CASE(RESPONSE_CODE);
621 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
622 CASE(TOTAL_TIME);
623 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
624 CASE(NAMELOOKUP_TIME);
625 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
626 CASE(CONNECT_TIME);
627 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
628 CASE(PRETRANSFER_TIME);
629 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
630 CASE(SIZE_UPLOAD);
631 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
632 CASE(SIZE_DOWNLOAD);
633 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
634 CASE(SPEED_DOWNLOAD);
635 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
636 CASE(SPEED_UPLOAD);
637 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
638 CASE(HEADER_SIZE);
639 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
640 CASE(REQUEST_SIZE);
641 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
642 CASE(SSL_VERIFYRESULT);
643 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
644 CASE(FILETIME);
645 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
646 CASE(CONTENT_LENGTH_DOWNLOAD);
647 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
648 CASE(CONTENT_LENGTH_UPLOAD);
649 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
650 CASE(STARTTRANSFER_TIME);
651 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
652 CASE(CONTENT_TYPE);
653 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
654 CASE(REDIRECT_TIME);
655 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
656 CASE(REDIRECT_COUNT);
657 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
658 CASE(PRIVATE);
659 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
660 CASE(HTTP_CONNECTCODE);
661 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
662 CASE(HTTPAUTH_AVAIL);
663 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
664 CASE(PROXYAUTH_AVAIL);
665 }
666 #undef CASE
667 return NULL;
668 }
669 /* }}} */
670
671 /* {{{ static inline void http_curl_getinfo_ex(CURL, CURLINFO, zval *) */
672 static inline void _http_curl_getinfo_ex(CURL *ch, CURLINFO i, zval *array TSRMLS_DC)
673 {
674 char *key;
675 if (key = http_curl_getinfoname(i)) {
676 switch (i & ~CURLINFO_MASK)
677 {
678 case CURLINFO_STRING:
679 {
680 char *c;
681 if (CURLE_OK == curl_easy_getinfo(ch, i, &c)) {
682 add_assoc_string(array, key, c ? c : "", 1);
683 }
684 }
685 break;
686
687 case CURLINFO_DOUBLE:
688 {
689 double d;
690 if (CURLE_OK == curl_easy_getinfo(ch, i, &d)) {
691 add_assoc_double(array, key, d);
692 }
693 }
694 break;
695
696 case CURLINFO_LONG:
697 {
698 long l;
699 if (CURLE_OK == curl_easy_getinfo(ch, i, &l)) {
700 add_assoc_long(array, key, l);
701 }
702 }
703 break;
704 }
705 }
706 }
707 /* }}} */
708
709 /* {{{ static inline http_curl_getinfo(CURL, HashTable *) */
710 static inline void _http_curl_getinfo(CURL *ch, HashTable *info TSRMLS_DC)
711 {
712 zval array;
713 Z_ARRVAL(array) = info;
714
715 #define INFO(I) http_curl_getinfo_ex(ch, CURLINFO_ ##I , &array)
716 /* CURLINFO_EFFECTIVE_URL = CURLINFO_STRING +1, */
717 INFO(EFFECTIVE_URL);
718 /* CURLINFO_RESPONSE_CODE = CURLINFO_LONG +2, */
719 INFO(RESPONSE_CODE);
720 /* CURLINFO_TOTAL_TIME = CURLINFO_DOUBLE +3, */
721 INFO(TOTAL_TIME);
722 /* CURLINFO_NAMELOOKUP_TIME = CURLINFO_DOUBLE +4, */
723 INFO(NAMELOOKUP_TIME);
724 /* CURLINFO_CONNECT_TIME = CURLINFO_DOUBLE +5, */
725 INFO(CONNECT_TIME);
726 /* CURLINFO_PRETRANSFER_TIME = CURLINFO_DOUBLE +6, */
727 INFO(PRETRANSFER_TIME);
728 /* CURLINFO_SIZE_UPLOAD = CURLINFO_DOUBLE +7, */
729 INFO(SIZE_UPLOAD);
730 /* CURLINFO_SIZE_DOWNLOAD = CURLINFO_DOUBLE +8, */
731 INFO(SIZE_DOWNLOAD);
732 /* CURLINFO_SPEED_DOWNLOAD = CURLINFO_DOUBLE +9, */
733 INFO(SPEED_DOWNLOAD);
734 /* CURLINFO_SPEED_UPLOAD = CURLINFO_DOUBLE +10, */
735 INFO(SPEED_UPLOAD);
736 /* CURLINFO_HEADER_SIZE = CURLINFO_LONG +11, */
737 INFO(HEADER_SIZE);
738 /* CURLINFO_REQUEST_SIZE = CURLINFO_LONG +12, */
739 INFO(REQUEST_SIZE);
740 /* CURLINFO_SSL_VERIFYRESULT = CURLINFO_LONG +13, */
741 INFO(SSL_VERIFYRESULT);
742 /* CURLINFO_FILETIME = CURLINFO_LONG +14, */
743 INFO(FILETIME);
744 /* CURLINFO_CONTENT_LENGTH_DOWNLOAD = CURLINFO_DOUBLE +15, */
745 INFO(CONTENT_LENGTH_DOWNLOAD);
746 /* CURLINFO_CONTENT_LENGTH_UPLOAD = CURLINFO_DOUBLE +16, */
747 INFO(CONTENT_LENGTH_UPLOAD);
748 /* CURLINFO_STARTTRANSFER_TIME = CURLINFO_DOUBLE +17, */
749 INFO(STARTTRANSFER_TIME);
750 /* CURLINFO_CONTENT_TYPE = CURLINFO_STRING +18, */
751 INFO(CONTENT_TYPE);
752 /* CURLINFO_REDIRECT_TIME = CURLINFO_DOUBLE +19, */
753 INFO(REDIRECT_TIME);
754 /* CURLINFO_REDIRECT_COUNT = CURLINFO_LONG +20, */
755 INFO(REDIRECT_COUNT);
756 /* CURLINFO_PRIVATE = CURLINFO_STRING +21, */
757 INFO(PRIVATE);
758 /* CURLINFO_HTTP_CONNECTCODE = CURLINFO_LONG +22, */
759 INFO(HTTP_CONNECTCODE);
760 /* CURLINFO_HTTPAUTH_AVAIL = CURLINFO_LONG +23, */
761 INFO(HTTPAUTH_AVAIL);
762 /* CURLINFO_PROXYAUTH_AVAIL = CURLINFO_LONG +24, */
763 INFO(PROXYAUTH_AVAIL);
764 #undef INFO
765 }
766 /* }}} */
767
768 #endif
769 /* }}} HAVE_CURL */
770
771 /* {{{ Day/Month/TZ checks for http_parse_date()
772 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
773 static int check_day(char *day, size_t len)
774 {
775 int i;
776 const char * const *check = (len > 3) ? &weekdays[0] : &wkdays[0];
777 for (i = 0; i < 7; i++) {
778 if (!strcmp(day, check[0])) {
779 return i;
780 }
781 check++;
782 }
783 return -1;
784 }
785
786 static int check_month(char *month)
787 {
788 int i;
789 const char * const *check = &months[0];
790 for (i = 0; i < 12; i++) {
791 if (!strcmp(month, check[0])) {
792 return i;
793 }
794 check++;
795 }
796 return -1;
797 }
798
799 /* return the time zone offset between GMT and the input one, in number
800 of seconds or -1 if the timezone wasn't found/legal */
801
802 static int check_tzone(char *tzone)
803 {
804 int i;
805 const struct time_zone *check = time_zones;
806 for (i = 0; i < sizeof(time_zones) / sizeof(time_zones[0]); i++) {
807 if (!strcmp(tzone, check->name)) {
808 return check->offset * 60;
809 }
810 check++;
811 }
812 return -1;
813 }
814 /* }}} */
815
816 /* static char *pretty_key(char *, int, int, int) */
817 static char *pretty_key(char *key, int key_len, int uctitle, int xhyphen)
818 {
819 if (key && key_len) {
820 int i, wasalpha;
821 if (wasalpha = isalpha(key[0])) {
822 key[0] = uctitle ? toupper(key[0]) : tolower(key[0]);
823 }
824 for (i = 1; i < key_len; i++) {
825 if (isalpha(key[i])) {
826 key[i] = ((!wasalpha) && uctitle) ? toupper(key[i]) : tolower(key[i]);
827 wasalpha = 1;
828 } else {
829 if (xhyphen && (key[i] == '_')) {
830 key[i] = '-';
831 }
832 wasalpha = 0;
833 }
834 }
835 }
836 return key;
837 }
838 /* }}} */
839
840 /* {{{ static STATUS http_ob_stack_get(php_ob_buffer *, php_ob_buffer **) */
841 static STATUS http_ob_stack_get(php_ob_buffer *o, php_ob_buffer **s)
842 {
843 static int i = 0;
844 php_ob_buffer *b = emalloc(sizeof(php_ob_buffer));
845 b->handler_name = estrdup(o->handler_name);
846 b->buffer = estrndup(o->buffer, o->text_length);
847 b->chunk_size = o->chunk_size;
848 b->erase = o->erase;
849 s[i++] = b;
850 return SUCCESS;
851 }
852 /* }}} */
853
854 /* }}} internals */
855
856 /* {{{ public API */
857
858 /* {{{ char *http_date(time_t) */
859 PHP_HTTP_API char *_http_date(time_t t TSRMLS_DC)
860 {
861 struct tm *gmtime, tmbuf;
862 char *date = ecalloc(31, 1);
863
864 gmtime = php_gmtime_r(&t, &tmbuf);
865 snprintf(date, 30,
866 "%s, %02d %s %04d %02d:%02d:%02d GMT",
867 days[gmtime->tm_wday], gmtime->tm_mday,
868 months[gmtime->tm_mon], gmtime->tm_year + 1900,
869 gmtime->tm_hour, gmtime->tm_min, gmtime->tm_sec
870 );
871 return date;
872 }
873 /* }}} */
874
875 /* {{{ time_t http_parse_date(char *)
876 Originally by libcurl, Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al. */
877 PHP_HTTP_API time_t _http_parse_date(const char *date)
878 {
879 time_t t = 0;
880 int tz_offset = -1, year = -1, month = -1, monthday = -1, weekday = -1,
881 hours = -1, minutes = -1, seconds = -1;
882 struct tm tm;
883 enum assume_next dignext = DATE_MDAY;
884 const char *indate = date;
885
886 int found = 0, part = 0; /* max 6 parts */
887
888 while (*date && (part < 6)) {
889 int found = 0;
890
891 while (*date && !isalnum(*date)) {
892 date++;
893 }
894
895 if (isalpha(*date)) {
896 /* a name coming up */
897 char buf[32] = "";
898 size_t len;
899 sscanf(date, "%31[A-Za-z]", buf);
900 len = strlen(buf);
901
902 if (weekday == -1) {
903 weekday = check_day(buf, len);
904 if (weekday != -1) {
905 found = 1;
906 }
907 }
908
909 if (!found && (month == -1)) {
910 month = check_month(buf);
911 if (month != -1) {
912 found = 1;
913 }
914 }
915
916 if (!found && (tz_offset == -1)) {
917 /* this just must be a time zone string */
918 tz_offset = check_tzone(buf);
919 if (tz_offset != -1) {
920 found = 1;
921 }
922 }
923
924 if (!found) {
925 return -1; /* bad string */
926 }
927 date += len;
928 }
929 else if (isdigit(*date)) {
930 /* a digit */
931 int val;
932 char *end;
933 if((seconds == -1) && (3 == sscanf(date, "%02d:%02d:%02d", &hours, &minutes, &seconds))) {
934 /* time stamp! */
935 date += 8;
936 found = 1;
937 }
938 else {
939 val = (int) strtol(date, &end, 10);
940
941 if ((tz_offset == -1) && ((end - date) == 4) && (val < 1300) && (indate < date) && ((date[-1] == '+' || date[-1] == '-'))) {
942 /* four digits and a value less than 1300 and it is preceeded with
943 a plus or minus. This is a time zone indication. */
944 found = 1;
945 tz_offset = (val / 100 * 60 + val % 100) * 60;
946
947 /* the + and - prefix indicates the local time compared to GMT,
948 this we need ther reversed math to get what we want */
949 tz_offset = date[-1] == '+' ? -tz_offset : tz_offset;
950 }
951
952 if (((end - date) == 8) && (year == -1) && (month == -1) && (monthday == -1)) {
953 /* 8 digits, no year, month or day yet. This is YYYYMMDD */
954 found = 1;
955 year = val / 10000;
956 month = (val % 10000) / 100 - 1; /* month is 0 - 11 */
957 monthday = val % 100;
958 }
959
960 if (!found && (dignext == DATE_MDAY) && (monthday == -1)) {
961 if ((val > 0) && (val < 32)) {
962 monthday = val;
963 found = 1;
964 }
965 dignext = DATE_YEAR;
966 }
967
968 if (!found && (dignext == DATE_YEAR) && (year == -1)) {
969 year = val;
970 found = 1;
971 if (year < 1900) {
972 year += year > 70 ? 1900 : 2000;
973 }
974 if(monthday == -1) {
975 dignext = DATE_MDAY;
976 }
977 }
978
979 if (!found) {
980 return -1;
981 }
982
983 date = end;
984 }
985 }
986
987 part++;
988 }
989
990 if (-1 == seconds) {
991 seconds = minutes = hours = 0; /* no time, make it zero */
992 }
993
994 if ((-1 == monthday) || (-1 == month) || (-1 == year)) {
995 /* lacks vital info, fail */
996 return -1;
997 }
998
999 if (sizeof(time_t) < 5) {
1000 /* 32 bit time_t can only hold dates to the beginning of 2038 */
1001 if (year > 2037) {
1002 return 0x7fffffff;
1003 }
1004 }
1005
1006 tm.tm_sec = seconds;
1007 tm.tm_min = minutes;
1008 tm.tm_hour = hours;
1009 tm.tm_mday = monthday;
1010 tm.tm_mon = month;
1011 tm.tm_year = year - 1900;
1012 tm.tm_wday = 0;
1013 tm.tm_yday = 0;
1014 tm.tm_isdst = 0;
1015
1016 t = mktime(&tm);
1017
1018 /* time zone adjust */
1019 {
1020 struct tm *gmt, keeptime2;
1021 long delta;
1022 time_t t2;
1023
1024 if(!(gmt = php_gmtime_r(&t, &keeptime2))) {
1025 return -1; /* illegal date/time */
1026 }
1027
1028 t2 = mktime(gmt);
1029
1030 /* Add the time zone diff (between the given timezone and GMT) and the
1031 diff between the local time zone and GMT. */
1032 delta = (tz_offset != -1 ? tz_offset : 0) + (t - t2);
1033
1034 if((delta > 0) && (t + delta < t)) {
1035 return -1; /* time_t overflow */
1036 }
1037
1038 t += delta;
1039 }
1040
1041 return t;
1042 }
1043 /* }}} */
1044
1045 /* {{{ inline STATUS http_send_status(int) */
1046 PHP_HTTP_API inline STATUS _http_send_status(const int status TSRMLS_DC)
1047 {
1048 int s = status;
1049 return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) s TSRMLS_CC);
1050 }
1051 /* }}} */
1052
1053 /* {{{ inline STATUS http_send_header(char *) */
1054 PHP_HTTP_API inline STATUS _http_send_header(const char *header TSRMLS_DC)
1055 {
1056 return http_send_status_header(0, header);
1057 }
1058 /* }}} */
1059
1060 /* {{{ inline STATUS http_send_status_header(int, char *) */
1061 PHP_HTTP_API inline STATUS _http_send_status_header(const int status, const char *header TSRMLS_DC)
1062 {
1063 sapi_header_line h = {(char *) header, strlen(header), status};
1064 return sapi_header_op(SAPI_HEADER_REPLACE, &h TSRMLS_CC);
1065 }
1066 /* }}} */
1067
1068 /* {{{ inline zval *http_get_server_var(char *) */
1069 PHP_HTTP_API inline zval *_http_get_server_var(const char *key TSRMLS_DC)
1070 {
1071 zval **var;
1072 if (SUCCESS == zend_hash_find(
1073 HTTP_SERVER_VARS,
1074 (char *) key, strlen(key) + 1, (void **) &var)) {
1075 return *var;
1076 }
1077 return NULL;
1078 }
1079 /* }}} */
1080
1081 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
1082 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
1083 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
1084 {
1085 char etag[33] = { 0 };
1086 unsigned char digest[16];
1087
1088 if (mode & PHP_OUTPUT_HANDLER_START) {
1089 PHP_MD5Init(&HTTP_G(etag_md5));
1090 }
1091
1092 PHP_MD5Update(&HTTP_G(etag_md5), output, output_len);
1093
1094 if (mode & PHP_OUTPUT_HANDLER_END) {
1095 PHP_MD5Final(digest, &HTTP_G(etag_md5));
1096
1097 /* just do that if desired */
1098 if (HTTP_G(etag_started)) {
1099 make_digest(etag, digest);
1100
1101 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1102 http_send_status(304);
1103 } else {
1104 http_send_etag(etag, 32);
1105 }
1106 }
1107 }
1108
1109 *handled_output_len = output_len;
1110 *handled_output = estrndup(output, output_len);
1111 }
1112 /* }}} */
1113
1114 /* {{{ STATUS http_start_ob_handler(php_output_handler_func_t, char *, uint, zend_bool) */
1115 PHP_HTTP_API STATUS _http_start_ob_handler(php_output_handler_func_t handler_func,
1116 char *handler_name, uint chunk_size, zend_bool erase TSRMLS_DC)
1117 {
1118 php_ob_buffer **stack;
1119 int count, i;
1120
1121 if (count = OG(ob_nesting_level)) {
1122 stack = ecalloc(sizeof(php_ob_buffer), count);
1123
1124 if (count > 1) {
1125 zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
1126 (int (*)(void *elem, void *)) http_ob_stack_get, stack);
1127 }
1128
1129 if (count > 0) {
1130 http_ob_stack_get(&OG(active_ob_buffer), stack);
1131 }
1132
1133 while (OG(ob_nesting_level)) {
1134 php_end_ob_buffer(0, 0 TSRMLS_CC);
1135 }
1136 }
1137
1138 php_ob_set_internal_handler(handler_func, chunk_size, handler_name, erase TSRMLS_CC);
1139
1140 for (i = 0; i < count; i++) {
1141 php_ob_buffer *s = stack[i];
1142 php_start_ob_buffer_named(s->handler_name, s->chunk_size, s->erase TSRMLS_CC);
1143 php_body_write(s->buffer, s->text_length TSRMLS_CC);
1144 }
1145
1146 return SUCCESS;
1147 }
1148 /* }}} */
1149
1150 /* {{{ int http_modified_match(char *, int) */
1151 PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
1152 {
1153 int retval;
1154 zval *zmodified;
1155 char *modified, *chr_ptr;
1156
1157 HTTP_GSC(zmodified, entry, 0);
1158
1159 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
1160 if (chr_ptr = strrchr(modified, ';')) {
1161 chr_ptr = 0;
1162 }
1163 retval = (t <= http_parse_date(modified));
1164 efree(modified);
1165 return retval;
1166 }
1167 /* }}} */
1168
1169 /* {{{ int http_etag_match(char *, char *) */
1170 PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
1171 {
1172 zval *zetag;
1173 char *quoted_etag;
1174 STATUS result;
1175
1176 HTTP_GSC(zetag, entry, 0);
1177
1178 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
1179 return 1;
1180 }
1181
1182 quoted_etag = (char *) emalloc(strlen(etag) + 3);
1183 sprintf(quoted_etag, "\"%s\"", etag);
1184
1185 if (!strchr(Z_STRVAL_P(zetag), ',')) {
1186 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
1187 } else {
1188 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
1189 }
1190 efree(quoted_etag);
1191 return result;
1192 }
1193 /* }}} */
1194
1195 /* {{{ STATUS http_send_last_modified(int) */
1196 PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
1197 {
1198 char modified[96] = "Last-Modified: ", *date;
1199 date = http_date(t);
1200 strcat(modified, date);
1201 efree(date);
1202
1203 /* remember */
1204 HTTP_G(lmod) = t;
1205
1206 return http_send_header(modified);
1207 }
1208 /* }}} */
1209
1210 /* {{{ static STATUS http_send_etag(char *, int) */
1211 PHP_HTTP_API STATUS _http_send_etag(const char *etag,
1212 const int etag_len TSRMLS_DC)
1213 {
1214 STATUS ret;
1215 int header_len;
1216 char *etag_header;
1217
1218 header_len = sizeof("ETag: \"\"") + etag_len + 1;
1219 etag_header = ecalloc(header_len, 1);
1220 sprintf(etag_header, "ETag: \"%s\"", etag);
1221 ret = http_send_header(etag_header);
1222 efree(etag_header);
1223
1224 if (!etag_len){
1225 php_error_docref(NULL TSRMLS_CC,E_ERROR,
1226 "Sending empty Etag (previous: %s)\n", HTTP_G(etag));
1227 return FAILURE;
1228 }
1229 /* remember */
1230 if (HTTP_G(etag)) {
1231 efree(HTTP_G(etag));
1232 }
1233 HTTP_G(etag) = estrdup(etag);
1234
1235 return ret;
1236 }
1237 /* }}} */
1238
1239 /* {{{ char *http_absolute_uri(char *, char *) */
1240 PHP_HTTP_API char *_http_absolute_uri(const char *url,
1241 const char *proto TSRMLS_DC)
1242 {
1243 char URI[HTTP_URI_MAXLEN + 1], *PTR, *proto_ptr, *host, *path;
1244 zval *zhost;
1245
1246 if (!url || !strlen(url)) {
1247 if (!SG(request_info).request_uri) {
1248 return NULL;
1249 }
1250 url = SG(request_info).request_uri;
1251 }
1252 /* Mess around with already absolute URIs */
1253 else if (proto_ptr = strstr(url, "://")) {
1254 if (!proto || !strncmp(url, proto, strlen(proto))) {
1255 return estrdup(url);
1256 } else {
1257 snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
1258 return estrdup(URI);
1259 }
1260 }
1261
1262 /* protocol defaults to http */
1263 if (!proto || !strlen(proto)) {
1264 proto = "http";
1265 }
1266
1267 /* get host name */
1268 if ( (zhost = http_get_server_var("HTTP_HOST")) ||
1269 (zhost = http_get_server_var("SERVER_NAME"))) {
1270 host = Z_STRVAL_P(zhost);
1271 } else {
1272 host = "localhost";
1273 }
1274
1275
1276 /* glue together */
1277 if (url[0] == '/') {
1278 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s", proto, host, url);
1279 } else if (SG(request_info).request_uri) {
1280 path = estrdup(SG(request_info).request_uri);
1281 php_dirname(path, strlen(path));
1282 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s/%s", proto, host, path, url);
1283 efree(path);
1284 } else {
1285 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
1286 }
1287
1288 /* strip everything after a new line */
1289 PTR = URI;
1290 while (*PTR != 0) {
1291 if (*PTR == '\n' || *PTR == '\r') {
1292 *PTR = 0;
1293 break;
1294 }
1295 PTR++;
1296 }
1297
1298 return estrdup(URI);
1299 }
1300 /* }}} */
1301
1302 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1303 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported,
1304 const char *def TSRMLS_DC)
1305 {
1306 zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
1307 char *q_ptr, *result;
1308 int i, c;
1309 double qual;
1310
1311 HTTP_GSC(zaccept, entry, estrdup(def));
1312
1313 MAKE_STD_ZVAL(zarray);
1314 array_init(zarray);
1315
1316 MAKE_STD_ZVAL(zdelim);
1317 ZVAL_STRING(zdelim, ",", 0);
1318 php_explode(zdelim, zaccept, zarray, -1);
1319 efree(zdelim);
1320
1321 MAKE_STD_ZVAL(zentries);
1322 array_init(zentries);
1323
1324 c = zend_hash_num_elements(Z_ARRVAL_P(zarray));
1325 for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) {
1326
1327 if (SUCCESS != zend_hash_get_current_data(
1328 Z_ARRVAL_P(zarray), (void **) &zentry)) {
1329 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1330 "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept));
1331 break;
1332 }
1333
1334 /* check for qualifier */
1335 if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
1336 qual = strtod(q_ptr + 3, NULL);
1337 } else {
1338 qual = 1000.0 - i;
1339 }
1340
1341 /* walk through the supported array */
1342 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported));
1343 SUCCESS == zend_hash_get_current_data(
1344 Z_ARRVAL_P(supported), (void **) &zsupp);
1345 zend_hash_move_forward(Z_ARRVAL_P(supported))) {
1346 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
1347 add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual);
1348 break;
1349 }
1350 }
1351 }
1352
1353 zval_dtor(zarray);
1354 efree(zarray);
1355
1356 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
1357
1358 if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort,
1359 http_sort_q, 0 TSRMLS_CC)) ||
1360 (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key(
1361 Z_ARRVAL_P(zentries), &result, 0, 1))) {
1362 result = estrdup(def);
1363 }
1364
1365 zval_dtor(zentries);
1366 efree(zentries);
1367
1368 return result;
1369 }
1370 /* }}} */
1371
1372 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1373 PHP_HTTP_API http_range_status _http_get_request_ranges(zval *zranges,
1374 const size_t length TSRMLS_DC)
1375 {
1376 zval *zrange;
1377 char *range, c;
1378 long begin = -1, end = -1, *ptr;
1379
1380 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
1381 range = Z_STRVAL_P(zrange);
1382
1383 if (strncmp(range, "bytes=", strlen("bytes="))) {
1384 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Range header misses bytes=");
1385 return RANGE_NO;
1386 }
1387
1388 ptr = &begin;
1389 range += strlen("bytes=");
1390
1391 do {
1392 switch (c = *(range++))
1393 {
1394 case '0':
1395 *ptr *= 10;
1396 break;
1397
1398 case '1': case '2': case '3':
1399 case '4': case '5': case '6':
1400 case '7': case '8': case '9':
1401 /*
1402 * If the value of the pointer is already set (non-negative)
1403 * then multiply its value by ten and add the current value,
1404 * else initialise the pointers value with the current value
1405 * --
1406 * This let us recognize empty fields when validating the
1407 * ranges, i.e. a "-10" for begin and "12345" for the end
1408 * was the following range request: "Range: bytes=0-12345";
1409 * While a "-1" for begin and "12345" for the end would
1410 * have been: "Range: bytes=-12345".
1411 */
1412 if (*ptr > 0) {
1413 *ptr *= 10;
1414 *ptr += c - '0';
1415 } else {
1416 *ptr = c - '0';
1417 }
1418 break;
1419
1420 case '-':
1421 ptr = &end;
1422 break;
1423
1424 case ' ':
1425 /* IE - ignore for now */
1426 break;
1427
1428 case 0:
1429 case ',':
1430
1431 if (length) {
1432 /* validate ranges */
1433 switch (begin)
1434 {
1435 /* "0-12345" */
1436 case -10:
1437 if ((length - end) < 1) {
1438 return RANGE_ERR;
1439 }
1440 begin = 0;
1441 break;
1442
1443 /* "-12345" */
1444 case -1:
1445 if ((length - end) < 1) {
1446 return RANGE_ERR;
1447 }
1448 begin = length - end;
1449 end = length;
1450 break;
1451
1452 /* "12345-(xxx)" */
1453 default:
1454 switch (end)
1455 {
1456 /* "12345-" */
1457 case -1:
1458 if ((length - begin) < 1) {
1459 return RANGE_ERR;
1460 }
1461 end = length - 1;
1462 break;
1463
1464 /* "12345-67890" */
1465 default:
1466 if ( ((length - begin) < 1) ||
1467 ((length - end) < 1) ||
1468 ((begin - end) >= 0)) {
1469 return RANGE_ERR;
1470 }
1471 break;
1472 }
1473 break;
1474 }
1475 }
1476 {
1477 zval *zentry;
1478 MAKE_STD_ZVAL(zentry);
1479 array_init(zentry);
1480 add_index_long(zentry, 0, begin);
1481 add_index_long(zentry, 1, end);
1482 add_next_index_zval(zranges, zentry);
1483
1484 begin = -1;
1485 end = -1;
1486 ptr = &begin;
1487 }
1488 break;
1489
1490 default:
1491 return RANGE_NO;
1492 break;
1493 }
1494 } while (c != 0);
1495
1496 return RANGE_OK;
1497 }
1498 /* }}} */
1499
1500 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1501 PHP_HTTP_API STATUS _http_send_ranges(zval *zranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
1502 {
1503 int c;
1504 long **begin, **end;
1505 zval **zrange;
1506
1507 /* Send HTTP 206 Partial Content */
1508 http_send_status(206);
1509
1510 /* single range */
1511 if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
1512 char range_header[256] = {0};
1513
1514 zend_hash_index_find(Z_ARRVAL_P(zranges), 0, (void **) &zrange);
1515 zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin);
1516 zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end);
1517
1518 /* send content range header */
1519 snprintf(range_header, 255, "Content-Range: bytes %d-%d/%d", **begin, **end, size);
1520 http_send_header(range_header);
1521
1522 /* send requested chunk */
1523 return http_send_chunk(data, **begin, **end + 1, mode);
1524 }
1525
1526 /* multi range */
1527 else {
1528 int i;
1529 char bound[23] = {0}, preface[1024] = {0},
1530 multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
1531
1532 snprintf(bound, 22, "--%d%0.9f", time(NULL), php_combined_lcg(TSRMLS_C));
1533 strncat(multi_header, bound + 2, 21);
1534 http_send_header(multi_header);
1535
1536 /* send each requested chunk */
1537 for ( i = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges));
1538 i < c;
1539 i++, zend_hash_move_forward(Z_ARRVAL_P(zranges))) {
1540 if ( HASH_KEY_NON_EXISTANT == zend_hash_get_current_data(
1541 Z_ARRVAL_P(zranges), (void **) &zrange) ||
1542 SUCCESS != zend_hash_index_find(
1543 Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
1544 SUCCESS != zend_hash_index_find(
1545 Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
1546 break;
1547 }
1548
1549 snprintf(preface, 1023,
1550 HTTP_CRLF "%s"
1551 HTTP_CRLF "Content-Type: %s"
1552 HTTP_CRLF "Content-Range: bytes %ld-%ld/%ld"
1553 HTTP_CRLF
1554 HTTP_CRLF,
1555
1556 bound,
1557 HTTP_G(ctype) ? HTTP_G(ctype) : "application/x-octetstream",
1558 **begin,
1559 **end,
1560 size
1561 );
1562
1563 php_body_write(preface, strlen(preface) TSRMLS_CC);
1564 http_send_chunk(data, **begin, **end + 1, mode);
1565 }
1566
1567 /* write boundary once more */
1568 php_body_write(HTTP_CRLF, 2 TSRMLS_CC);
1569 php_body_write(bound, strlen(bound) TSRMLS_CC);
1570
1571 return SUCCESS;
1572 }
1573 }
1574 /* }}} */
1575
1576 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1577 PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size,
1578 const http_send_mode data_mode TSRMLS_DC)
1579 {
1580 int is_range_request = http_is_range_request();
1581
1582 if (!data_ptr) {
1583 return FAILURE;
1584 }
1585
1586 /* etag handling */
1587 if (HTTP_G(etag_started)) {
1588 char *etag = ecalloc(33, 1);
1589 /* interrupt */
1590 HTTP_G(etag_started) = 0;
1591 /* never ever use the output to compute the ETag if http_send() is used */
1592 php_end_ob_buffer(0, 0 TSRMLS_CC);
1593 if (NULL == http_etag(&etag, data_ptr, data_size, data_mode)) {
1594 efree(etag);
1595 return FAILURE;
1596 }
1597
1598 /* send 304 Not Modified if etag matches */
1599 if ((!is_range_request) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1600 efree(etag);
1601 return http_send_status(304);
1602 }
1603
1604 http_send_etag(etag, 32);
1605 efree(etag);
1606 }
1607
1608 /* send 304 Not Modified if last-modified matches*/
1609 if ((!is_range_request) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
1610 return http_send_status(304);
1611 }
1612
1613 if (is_range_request) {
1614
1615 /* only send ranges if entity hasn't changed */
1616 if (
1617 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_MATCH", 13)) ||
1618 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag)))
1619 &&
1620 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1621 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod)))
1622 ) {
1623
1624 STATUS result = FAILURE;
1625 zval *zranges = NULL;
1626 MAKE_STD_ZVAL(zranges);
1627 array_init(zranges);
1628
1629 switch (http_get_request_ranges(zranges, data_size))
1630 {
1631 case RANGE_NO:
1632 zval_dtor(zranges);
1633 efree(zranges);
1634 /* go ahead and send all */
1635 break;
1636
1637 case RANGE_OK:
1638 result = http_send_ranges(zranges, data_ptr, data_size, data_mode);
1639 zval_dtor(zranges);
1640 efree(zranges);
1641 return result;
1642 break;
1643
1644 case RANGE_ERR:
1645 zval_dtor(zranges);
1646 efree(zranges);
1647 http_send_status(416);
1648 return FAILURE;
1649 break;
1650
1651 default:
1652 return FAILURE;
1653 break;
1654 }
1655 }
1656 }
1657 /* send all */
1658 return http_send_chunk(data_ptr, 0, data_size, data_mode);
1659 }
1660 /* }}} */
1661
1662 /* {{{ STATUS http_send_data(zval *) */
1663 PHP_HTTP_API STATUS _http_send_data(const zval *zdata TSRMLS_DC)
1664 {
1665 if (!Z_STRLEN_P(zdata)) {
1666 return SUCCESS;
1667 }
1668 if (!Z_STRVAL_P(zdata)) {
1669 return FAILURE;
1670 }
1671
1672 return http_send(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), SEND_DATA);
1673 }
1674 /* }}} */
1675
1676 /* {{{ STATUS http_send_stream(php_stream *) */
1677 PHP_HTTP_API STATUS _http_send_stream(const php_stream *file TSRMLS_DC)
1678 {
1679 if (php_stream_stat((php_stream *) file, &HTTP_G(ssb))) {
1680 return FAILURE;
1681 }
1682
1683 return http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
1684 }
1685 /* }}} */
1686
1687 /* {{{ STATUS http_send_file(zval *) */
1688 PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC)
1689 {
1690 php_stream *file;
1691 STATUS ret;
1692
1693 if (!Z_STRLEN_P(zfile)) {
1694 return FAILURE;
1695 }
1696
1697 if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
1698 REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1699 return FAILURE;
1700 }
1701
1702 ret = http_send_stream(file);
1703 php_stream_close(file);
1704 return ret;
1705 }
1706 /* }}} */
1707
1708 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1709 PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
1710 const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
1711 {
1712 const char *e_ptr;
1713 char *d_ptr;
1714
1715 *decoded_len = 0;
1716 *decoded = (char *) ecalloc(encoded_len, 1);
1717 d_ptr = *decoded;
1718 e_ptr = encoded;
1719
1720 while (((e_ptr - encoded) - encoded_len) > 0) {
1721 char hex_len[9] = {0};
1722 size_t chunk_len = 0;
1723 int i = 0;
1724
1725 /* read in chunk size */
1726 while (isxdigit(*e_ptr)) {
1727 if (i == 9) {
1728 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1729 "Chunk size is too long: 0x%s...", hex_len);
1730 efree(*decoded);
1731 return FAILURE;
1732 }
1733 hex_len[i++] = *e_ptr++;
1734 }
1735
1736 /* reached the end */
1737 if (!strcmp(hex_len, "0")) {
1738 break;
1739 }
1740
1741 /* new line */
1742 if (strncmp(e_ptr, HTTP_CRLF, 2)) {
1743 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1744 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1745 *e_ptr, *(e_ptr + 1));
1746 efree(*decoded);
1747 return FAILURE;
1748 }
1749
1750 /* hex to long */
1751 {
1752 char *error = NULL;
1753 chunk_len = strtol(hex_len, &error, 16);
1754 if (error == hex_len) {
1755 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1756 "Invalid chunk size string: '%s'", hex_len);
1757 efree(*decoded);
1758 return FAILURE;
1759 }
1760 }
1761
1762 memcpy(d_ptr, e_ptr += 2, chunk_len);
1763 d_ptr += chunk_len;
1764 e_ptr += chunk_len + 2;
1765 *decoded_len += chunk_len;
1766 }
1767
1768 return SUCCESS;
1769 }
1770 /* }}} */
1771
1772 /* {{{ proto STATUS http_split_response(zval *, zval *, zval *) */
1773 PHP_HTTP_API STATUS _http_split_response(const zval *zresponse, zval *zheaders,
1774 zval *zbody TSRMLS_DC)
1775 {
1776 char *header, *response, *body = NULL;
1777 long response_len = Z_STRLEN_P(zresponse);
1778 header = response = Z_STRVAL_P(zresponse);
1779
1780 while ((response - Z_STRVAL_P(zresponse) + 3) < response_len) {
1781 if ( (*response++ == '\r') &&
1782 (*response++ == '\n') &&
1783 (*response++ == '\r') &&
1784 (*response++ == '\n')) {
1785 body = response;
1786 break;
1787 }
1788 }
1789
1790 if (body && (response_len - (body - header))) {
1791 ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
1792 } else {
1793 Z_TYPE_P(zbody) = IS_NULL;
1794 }
1795
1796 return http_parse_headers(header, body - Z_STRVAL_P(zresponse), zheaders);
1797 }
1798 /* }}} */
1799
1800 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1801 PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *array TSRMLS_DC)
1802 {
1803 char *colon = NULL, *line = NULL, *begin = header;
1804
1805 if (header_len < 2) {
1806 return FAILURE;
1807 }
1808
1809 /* status code */
1810 if (!strncmp(header, "HTTP/1.", 7)) {
1811 char *end = strstr(header, HTTP_CRLF);
1812 add_assoc_stringl(array, "Status",
1813 header + strlen("HTTP/1.x "),
1814 end - (header + strlen("HTTP/1.x ")), 1);
1815 header = end + 2;
1816 }
1817
1818 line = header;
1819
1820 while (header_len >= (line - begin)) {
1821 int value_len = 0;
1822
1823 switch (*line++)
1824 {
1825 case 0:
1826 --value_len; /* we don't have CR so value length is one char less */
1827 case '\n':
1828 if (colon && ((!(*line - 1)) || ((*line != ' ') && (*line != '\t')))) {
1829
1830 /* skip empty key */
1831 if (header != colon) {
1832 char *key = estrndup(header, colon - header);
1833 value_len += line - colon - 1;
1834
1835 /* skip leading ws */
1836 while (isspace(*(++colon))) --value_len;
1837 /* skip trailing ws */
1838 while (isspace(colon[value_len - 1])) --value_len;
1839
1840 if (value_len < 1) {
1841 /* hm, empty header? */
1842 add_assoc_stringl(array, key, "", 0, 1);
1843 } else {
1844 add_assoc_stringl(array, key, colon, value_len, 1);
1845 }
1846 efree(key);
1847 }
1848
1849 colon = NULL;
1850 value_len = 0;
1851 header += line - header;
1852 }
1853 break;
1854
1855 case ':':
1856 if (!colon) {
1857 colon = line - 1;
1858 }
1859 break;
1860 }
1861 }
1862 return SUCCESS;
1863 }
1864 /* }}} */
1865
1866 /* {{{ void http_get_request_headers(zval *) */
1867 PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
1868 {
1869 char *key;
1870
1871 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS);
1872 zend_hash_get_current_key(HTTP_SERVER_VARS, &key, NULL, 0) != HASH_KEY_NON_EXISTANT;
1873 zend_hash_move_forward(HTTP_SERVER_VARS)) {
1874 if (!strncmp(key, "HTTP_", 5)) {
1875 zval **header;
1876 zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
1877 add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1878 }
1879 }
1880 }
1881 /* }}} */
1882
1883 /* {{{ HAVE_CURL */
1884 #ifdef HTTP_HAVE_CURL
1885
1886 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
1887 PHP_HTTP_API STATUS _http_get(const char *URL, HashTable *options,
1888 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
1889 {
1890 CURL *ch = curl_easy_init();
1891
1892 if (!ch) {
1893 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
1894 return FAILURE;
1895 }
1896
1897 http_curl_initbuf(CURLBUF_EVRY);
1898 http_curl_setopts(ch, URL, options);
1899
1900 if (CURLE_OK != curl_easy_perform(ch)) {
1901 curl_easy_cleanup(ch);
1902 http_curl_freebuf(CURLBUF_EVRY);
1903 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
1904 return FAILURE;
1905 }
1906 if (info) {
1907 http_curl_getinfo(ch, info);
1908 }
1909 curl_easy_cleanup(ch);
1910
1911 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
1912
1913 return SUCCESS;
1914 }
1915 /* }}} */
1916
1917 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
1918 PHP_HTTP_API STATUS _http_head(const char *URL, HashTable *options,
1919 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
1920 {
1921 CURL *ch = curl_easy_init();
1922
1923 if (!ch) {
1924 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
1925 return FAILURE;
1926 }
1927
1928 http_curl_initbuf(CURLBUF_HDRS);
1929 http_curl_setopts(ch, URL, options);
1930 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
1931
1932 if (CURLE_OK != curl_easy_perform(ch)) {
1933 curl_easy_cleanup(ch);
1934 http_curl_freebuf(CURLBUF_HDRS);
1935 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
1936 return FAILURE;
1937 }
1938 if (info) {
1939 http_curl_getinfo(ch, info);
1940 }
1941 curl_easy_cleanup(ch);
1942
1943 http_curl_movebuf(CURLBUF_HDRS, data, data_len);
1944
1945 return SUCCESS;
1946 }
1947 /* }}} */
1948
1949 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
1950 PHP_HTTP_API STATUS _http_post_data(const char *URL, char *postdata,
1951 size_t postdata_len, HashTable *options, HashTable *info, char **data,
1952 size_t *data_len TSRMLS_DC)
1953 {
1954 CURL *ch = curl_easy_init();
1955
1956 if (!ch) {
1957 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
1958 return FAILURE;
1959 }
1960
1961 http_curl_initbuf(CURLBUF_EVRY);
1962 http_curl_setopts(ch, URL, options);
1963 curl_easy_setopt(ch, CURLOPT_POST, 1);
1964 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
1965 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
1966
1967 if (CURLE_OK != curl_easy_perform(ch)) {
1968 curl_easy_cleanup(ch);
1969 http_curl_freebuf(CURLBUF_EVRY);
1970 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
1971 return FAILURE;
1972 }
1973 if (info) {
1974 http_curl_getinfo(ch, info);
1975 }
1976 curl_easy_cleanup(ch);
1977
1978 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
1979
1980 return SUCCESS;
1981 }
1982 /* }}} */
1983
1984 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
1985 PHP_HTTP_API STATUS _http_post_array(const char *URL, HashTable *postarray,
1986 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
1987 {
1988 smart_str qstr = {0};
1989 STATUS status;
1990
1991 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
1992 if (qstr.c) {
1993 efree(qstr.c);
1994 }
1995 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
1996 return FAILURE;
1997 }
1998 smart_str_0(&qstr);
1999
2000 status = http_post_data(URL, qstr.c, qstr.len, options, info, data, data_len);
2001 if (qstr.c) {
2002 efree(qstr.c);
2003 }
2004 return status;
2005 }
2006 /* }}} */
2007
2008 #endif
2009 /* }}} HAVE_CURL */
2010
2011 /* {{{ STATUS http_auth_header(char *, char*) */
2012 PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC)
2013 {
2014 char realm_header[1024] = {0};
2015 snprintf(realm_header, 1023, "WWW-Authenticate: %s realm=\"%s\"", type, realm);
2016 return http_send_status_header(401, realm_header);
2017 }
2018 /* }}} */
2019
2020 /* {{{ STATUS http_auth_credentials(char **, char **) */
2021 PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC)
2022 {
2023 if (strncmp(sapi_module.name, "isapi", 5)) {
2024 zval *zuser, *zpass;
2025
2026 HTTP_GSC(zuser, "PHP_AUTH_USER", FAILURE);
2027 HTTP_GSC(zpass, "PHP_AUTH_PW", FAILURE);
2028
2029 *user = estrndup(Z_STRVAL_P(zuser), Z_STRLEN_P(zuser));
2030 *pass = estrndup(Z_STRVAL_P(zpass), Z_STRLEN_P(zpass));
2031
2032 return SUCCESS;
2033 } else {
2034 zval *zauth = NULL;
2035 HTTP_GSC(zauth, "HTTP_AUTHORIZATION", FAILURE);
2036 {
2037 char *decoded, *colon;
2038 int decoded_len;
2039 decoded = php_base64_decode(Z_STRVAL_P(zauth), Z_STRLEN_P(zauth),
2040 &decoded_len);
2041
2042 if (colon = strchr(decoded + 6, ':')) {
2043 *user = estrndup(decoded + 6, colon - decoded - 6);
2044 *pass = estrndup(colon + 1, decoded + decoded_len - colon - 6 - 1);
2045
2046 return SUCCESS;
2047 } else {
2048 return FAILURE;
2049 }
2050 }
2051 }
2052 }
2053 /* }}} */
2054
2055 /* }}} public API */
2056
2057 /*
2058 * Local variables:
2059 * tab-width: 4
2060 * c-basic-offset: 4
2061 * End:
2062 * vim600: noet sw=4 ts=4 fdm=marker
2063 * vim<600: noet sw=4 ts=4
2064 */