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