* added ob_httpetaghandler() (has major limitations compared to http_cache_etag())
[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 STATUS result;
1121
1122 count = OG(ob_nesting_level);
1123 stack = emalloc(sizeof(php_ob_buffer *) * count);
1124
1125 if (count > 1) {
1126 zend_stack_apply_with_argument(&OG(ob_buffers), ZEND_STACK_APPLY_BOTTOMUP,
1127 (int (*)(void *elem, void *)) http_ob_stack_get, stack);
1128 }
1129
1130 if (count > 0) {
1131 http_ob_stack_get(&OG(active_ob_buffer), stack);
1132 }
1133
1134 while (OG(ob_nesting_level)) {
1135 php_end_ob_buffer(0, 0 TSRMLS_CC);
1136 }
1137
1138 php_ob_set_internal_handler(handler_func, 0, handler_name, 0 TSRMLS_CC);
1139 result = php_start_ob_buffer_named(handler_name, chunk_size, erase TSRMLS_CC);
1140
1141 for (i = 0; i < count; i++) {
1142 php_ob_buffer *s = stack[i];
1143 php_start_ob_buffer_named(s->handler_name, s->chunk_size, s->erase TSRMLS_CC);
1144 php_body_write(s->buffer, s->text_length TSRMLS_CC);
1145 }
1146
1147 return result;
1148 }
1149 /* }}} */
1150
1151 /* {{{ int http_modified_match(char *, int) */
1152 PHP_HTTP_API int _http_modified_match(const char *entry, const time_t t TSRMLS_DC)
1153 {
1154 int retval;
1155 zval *zmodified;
1156 char *modified, *chr_ptr;
1157
1158 HTTP_GSC(zmodified, entry, 0);
1159
1160 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
1161 if (chr_ptr = strrchr(modified, ';')) {
1162 chr_ptr = 0;
1163 }
1164 retval = (t <= http_parse_date(modified));
1165 efree(modified);
1166 return retval;
1167 }
1168 /* }}} */
1169
1170 /* {{{ int http_etag_match(char *, char *) */
1171 PHP_HTTP_API int _http_etag_match(const char *entry, const char *etag TSRMLS_DC)
1172 {
1173 zval *zetag;
1174 char *quoted_etag;
1175 STATUS result;
1176
1177 HTTP_GSC(zetag, entry, 0);
1178
1179 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
1180 return 1;
1181 }
1182
1183 quoted_etag = (char *) emalloc(strlen(etag) + 3);
1184 sprintf(quoted_etag, "\"%s\"", etag);
1185
1186 if (!strchr(Z_STRVAL_P(zetag), ',')) {
1187 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
1188 } else {
1189 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
1190 }
1191 efree(quoted_etag);
1192 return result;
1193 }
1194 /* }}} */
1195
1196 /* {{{ STATUS http_send_last_modified(int) */
1197 PHP_HTTP_API STATUS _http_send_last_modified(const time_t t TSRMLS_DC)
1198 {
1199 char modified[96] = "Last-Modified: ", *date;
1200 date = http_date(t);
1201 strcat(modified, date);
1202 efree(date);
1203
1204 /* remember */
1205 HTTP_G(lmod) = t;
1206
1207 return http_send_header(modified);
1208 }
1209 /* }}} */
1210
1211 /* {{{ static STATUS http_send_etag(char *, int) */
1212 PHP_HTTP_API STATUS _http_send_etag(const char *etag,
1213 const int etag_len TSRMLS_DC)
1214 {
1215 STATUS ret;
1216 int header_len;
1217 char *etag_header;
1218
1219 header_len = sizeof("ETag: \"\"") + etag_len + 1;
1220 etag_header = ecalloc(header_len, 1);
1221 sprintf(etag_header, "ETag: \"%s\"", etag);
1222 ret = http_send_header(etag_header);
1223 efree(etag_header);
1224
1225 if (!etag_len){
1226 php_error_docref(NULL TSRMLS_CC,E_ERROR,
1227 "Sending empty Etag (previous: %s)\n", HTTP_G(etag));
1228 return FAILURE;
1229 }
1230 /* remember */
1231 if (HTTP_G(etag)) {
1232 efree(HTTP_G(etag));
1233 }
1234 HTTP_G(etag) = estrdup(etag);
1235
1236 return ret;
1237 }
1238 /* }}} */
1239
1240 /* {{{ char *http_absolute_uri(char *, char *) */
1241 PHP_HTTP_API char *_http_absolute_uri(const char *url,
1242 const char *proto TSRMLS_DC)
1243 {
1244 char URI[HTTP_URI_MAXLEN + 1], *PTR, *proto_ptr, *host, *path;
1245 zval *zhost;
1246
1247 if (!url || !strlen(url)) {
1248 if (!SG(request_info).request_uri) {
1249 return NULL;
1250 }
1251 url = SG(request_info).request_uri;
1252 }
1253 /* Mess around with already absolute URIs */
1254 else if (proto_ptr = strstr(url, "://")) {
1255 if (!proto || !strncmp(url, proto, strlen(proto))) {
1256 return estrdup(url);
1257 } else {
1258 snprintf(URI, HTTP_URI_MAXLEN, "%s%s", proto, proto_ptr + 3);
1259 return estrdup(URI);
1260 }
1261 }
1262
1263 /* protocol defaults to http */
1264 if (!proto || !strlen(proto)) {
1265 proto = "http";
1266 }
1267
1268 /* get host name */
1269 if ( (zhost = http_get_server_var("HTTP_HOST")) ||
1270 (zhost = http_get_server_var("SERVER_NAME"))) {
1271 host = Z_STRVAL_P(zhost);
1272 } else {
1273 host = "localhost";
1274 }
1275
1276
1277 /* glue together */
1278 if (url[0] == '/') {
1279 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s", proto, host, url);
1280 } else if (SG(request_info).request_uri) {
1281 path = estrdup(SG(request_info).request_uri);
1282 php_dirname(path, strlen(path));
1283 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s%s/%s", proto, host, path, url);
1284 efree(path);
1285 } else {
1286 snprintf(URI, HTTP_URI_MAXLEN, "%s://%s/%s", proto, host, url);
1287 }
1288
1289 /* strip everything after a new line */
1290 PTR = URI;
1291 while (*PTR != 0) {
1292 if (*PTR == '\n' || *PTR == '\r') {
1293 *PTR = 0;
1294 break;
1295 }
1296 PTR++;
1297 }
1298
1299 return estrdup(URI);
1300 }
1301 /* }}} */
1302
1303 /* {{{ char *http_negotiate_q(char *, zval *, char *, hash_entry_type) */
1304 PHP_HTTP_API char *_http_negotiate_q(const char *entry, const zval *supported,
1305 const char *def TSRMLS_DC)
1306 {
1307 zval *zaccept, *zarray, *zdelim, **zentry, *zentries, **zsupp;
1308 char *q_ptr, *result;
1309 int i, c;
1310 double qual;
1311
1312 HTTP_GSC(zaccept, entry, estrdup(def));
1313
1314 MAKE_STD_ZVAL(zarray);
1315 array_init(zarray);
1316
1317 MAKE_STD_ZVAL(zdelim);
1318 ZVAL_STRING(zdelim, ",", 0);
1319 php_explode(zdelim, zaccept, zarray, -1);
1320 efree(zdelim);
1321
1322 MAKE_STD_ZVAL(zentries);
1323 array_init(zentries);
1324
1325 c = zend_hash_num_elements(Z_ARRVAL_P(zarray));
1326 for (i = 0; i < c; i++, zend_hash_move_forward(Z_ARRVAL_P(zarray))) {
1327
1328 if (SUCCESS != zend_hash_get_current_data(
1329 Z_ARRVAL_P(zarray), (void **) &zentry)) {
1330 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1331 "Cannot parse %s header: %s", entry, Z_STRVAL_P(zaccept));
1332 break;
1333 }
1334
1335 /* check for qualifier */
1336 if (NULL != (q_ptr = strrchr(Z_STRVAL_PP(zentry), ';'))) {
1337 qual = strtod(q_ptr + 3, NULL);
1338 } else {
1339 qual = 1000.0 - i;
1340 }
1341
1342 /* walk through the supported array */
1343 for ( zend_hash_internal_pointer_reset(Z_ARRVAL_P(supported));
1344 SUCCESS == zend_hash_get_current_data(
1345 Z_ARRVAL_P(supported), (void **) &zsupp);
1346 zend_hash_move_forward(Z_ARRVAL_P(supported))) {
1347 if (!strcasecmp(Z_STRVAL_PP(zsupp), Z_STRVAL_PP(zentry))) {
1348 add_assoc_double(zentries, Z_STRVAL_PP(zsupp), qual);
1349 break;
1350 }
1351 }
1352 }
1353
1354 zval_dtor(zarray);
1355 efree(zarray);
1356
1357 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zentries));
1358
1359 if ( (SUCCESS != zend_hash_sort(Z_ARRVAL_P(zentries), zend_qsort,
1360 http_sort_q, 0 TSRMLS_CC)) ||
1361 (HASH_KEY_NON_EXISTANT == zend_hash_get_current_key(
1362 Z_ARRVAL_P(zentries), &result, 0, 1))) {
1363 result = estrdup(def);
1364 }
1365
1366 zval_dtor(zentries);
1367 efree(zentries);
1368
1369 return result;
1370 }
1371 /* }}} */
1372
1373 /* {{{ http_range_status http_get_request_ranges(zval *zranges, size_t) */
1374 PHP_HTTP_API http_range_status _http_get_request_ranges(zval *zranges,
1375 const size_t length TSRMLS_DC)
1376 {
1377 zval *zrange;
1378 char *range, c;
1379 long begin = -1, end = -1, *ptr;
1380
1381 HTTP_GSC(zrange, "HTTP_RANGE", RANGE_NO);
1382 range = Z_STRVAL_P(zrange);
1383
1384 if (strncmp(range, "bytes=", strlen("bytes="))) {
1385 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Range header misses bytes=");
1386 return RANGE_NO;
1387 }
1388
1389 ptr = &begin;
1390 range += strlen("bytes=");
1391
1392 do {
1393 switch (c = *(range++))
1394 {
1395 case '0':
1396 *ptr *= 10;
1397 break;
1398
1399 case '1': case '2': case '3':
1400 case '4': case '5': case '6':
1401 case '7': case '8': case '9':
1402 /*
1403 * If the value of the pointer is already set (non-negative)
1404 * then multiply its value by ten and add the current value,
1405 * else initialise the pointers value with the current value
1406 * --
1407 * This let us recognize empty fields when validating the
1408 * ranges, i.e. a "-10" for begin and "12345" for the end
1409 * was the following range request: "Range: bytes=0-12345";
1410 * While a "-1" for begin and "12345" for the end would
1411 * have been: "Range: bytes=-12345".
1412 */
1413 if (*ptr > 0) {
1414 *ptr *= 10;
1415 *ptr += c - '0';
1416 } else {
1417 *ptr = c - '0';
1418 }
1419 break;
1420
1421 case '-':
1422 ptr = &end;
1423 break;
1424
1425 case ' ':
1426 /* IE - ignore for now */
1427 break;
1428
1429 case 0:
1430 case ',':
1431
1432 if (length) {
1433 /* validate ranges */
1434 switch (begin)
1435 {
1436 /* "0-12345" */
1437 case -10:
1438 if ((length - end) < 1) {
1439 return RANGE_ERR;
1440 }
1441 begin = 0;
1442 break;
1443
1444 /* "-12345" */
1445 case -1:
1446 if ((length - end) < 1) {
1447 return RANGE_ERR;
1448 }
1449 begin = length - end;
1450 end = length;
1451 break;
1452
1453 /* "12345-(xxx)" */
1454 default:
1455 switch (end)
1456 {
1457 /* "12345-" */
1458 case -1:
1459 if ((length - begin) < 1) {
1460 return RANGE_ERR;
1461 }
1462 end = length - 1;
1463 break;
1464
1465 /* "12345-67890" */
1466 default:
1467 if ( ((length - begin) < 1) ||
1468 ((length - end) < 1) ||
1469 ((begin - end) >= 0)) {
1470 return RANGE_ERR;
1471 }
1472 break;
1473 }
1474 break;
1475 }
1476 }
1477 {
1478 zval *zentry;
1479 MAKE_STD_ZVAL(zentry);
1480 array_init(zentry);
1481 add_index_long(zentry, 0, begin);
1482 add_index_long(zentry, 1, end);
1483 add_next_index_zval(zranges, zentry);
1484
1485 begin = -1;
1486 end = -1;
1487 ptr = &begin;
1488 }
1489 break;
1490
1491 default:
1492 return RANGE_NO;
1493 break;
1494 }
1495 } while (c != 0);
1496
1497 return RANGE_OK;
1498 }
1499 /* }}} */
1500
1501 /* {{{ STATUS http_send_ranges(zval *, void *, size_t, http_send_mode) */
1502 PHP_HTTP_API STATUS _http_send_ranges(zval *zranges, const void *data, const size_t size, const http_send_mode mode TSRMLS_DC)
1503 {
1504 int c;
1505 long **begin, **end;
1506 zval **zrange;
1507
1508 /* Send HTTP 206 Partial Content */
1509 http_send_status(206);
1510
1511 /* single range */
1512 if ((c = zend_hash_num_elements(Z_ARRVAL_P(zranges))) == 1) {
1513 char range_header[256] = {0};
1514
1515 zend_hash_index_find(Z_ARRVAL_P(zranges), 0, (void **) &zrange);
1516 zend_hash_index_find(Z_ARRVAL_PP(zrange), 0, (void **) &begin);
1517 zend_hash_index_find(Z_ARRVAL_PP(zrange), 1, (void **) &end);
1518
1519 /* send content range header */
1520 snprintf(range_header, 255, "Content-Range: bytes %d-%d/%d", **begin, **end, size);
1521 http_send_header(range_header);
1522
1523 /* send requested chunk */
1524 return http_send_chunk(data, **begin, **end + 1, mode);
1525 }
1526
1527 /* multi range */
1528 else {
1529 int i;
1530 char bound[23] = {0}, preface[1024] = {0},
1531 multi_header[68] = "Content-Type: multipart/byteranges; boundary=";
1532
1533 snprintf(bound, 22, "--%d%0.9f", time(NULL), php_combined_lcg(TSRMLS_C));
1534 strncat(multi_header, bound + 2, 21);
1535 http_send_header(multi_header);
1536
1537 /* send each requested chunk */
1538 for ( i = 0, zend_hash_internal_pointer_reset(Z_ARRVAL_P(zranges));
1539 i < c;
1540 i++, zend_hash_move_forward(Z_ARRVAL_P(zranges))) {
1541 if ( HASH_KEY_NON_EXISTANT == zend_hash_get_current_data(
1542 Z_ARRVAL_P(zranges), (void **) &zrange) ||
1543 SUCCESS != zend_hash_index_find(
1544 Z_ARRVAL_PP(zrange), 0, (void **) &begin) ||
1545 SUCCESS != zend_hash_index_find(
1546 Z_ARRVAL_PP(zrange), 1, (void **) &end)) {
1547 break;
1548 }
1549
1550 snprintf(preface, 1023,
1551 HTTP_CRLF "%s"
1552 HTTP_CRLF "Content-Type: %s"
1553 HTTP_CRLF "Content-Range: bytes %ld-%ld/%ld"
1554 HTTP_CRLF
1555 HTTP_CRLF,
1556
1557 bound,
1558 HTTP_G(ctype) ? HTTP_G(ctype) : "application/x-octetstream",
1559 **begin,
1560 **end,
1561 size
1562 );
1563
1564 php_body_write(preface, strlen(preface) TSRMLS_CC);
1565 http_send_chunk(data, **begin, **end + 1, mode);
1566 }
1567
1568 /* write boundary once more */
1569 php_body_write(HTTP_CRLF, 2 TSRMLS_CC);
1570 php_body_write(bound, strlen(bound) TSRMLS_CC);
1571
1572 return SUCCESS;
1573 }
1574 }
1575 /* }}} */
1576
1577 /* {{{ STATUS http_send(void *, sizezo_t, http_send_mode) */
1578 PHP_HTTP_API STATUS _http_send(const void *data_ptr, const size_t data_size,
1579 const http_send_mode data_mode TSRMLS_DC)
1580 {
1581 int is_range_request = http_is_range_request();
1582
1583 if (!data_ptr) {
1584 return FAILURE;
1585 }
1586
1587 /* etag handling */
1588 if (HTTP_G(etag_started)) {
1589 char *etag = ecalloc(33, 1);
1590 /* interrupt */
1591 HTTP_G(etag_started) = 0;
1592 /* never ever use the output to compute the ETag if http_send() is used */
1593 php_end_ob_buffer(0, 0 TSRMLS_CC);
1594 if (NULL == http_etag(&etag, data_ptr, data_size, data_mode)) {
1595 efree(etag);
1596 return FAILURE;
1597 }
1598
1599 /* send 304 Not Modified if etag matches */
1600 if ((!is_range_request) && http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
1601 efree(etag);
1602 return http_send_status(304);
1603 }
1604
1605 http_send_etag(etag, 32);
1606 efree(etag);
1607 }
1608
1609 /* send 304 Not Modified if last-modified matches*/
1610 if ((!is_range_request) && http_modified_match("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod))) {
1611 return http_send_status(304);
1612 }
1613
1614 if (is_range_request) {
1615
1616 /* only send ranges if entity hasn't changed */
1617 if (
1618 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_MATCH", 13)) ||
1619 http_etag_match("HTTP_IF_MATCH", HTTP_G(etag)))
1620 &&
1621 ((!zend_hash_exists(HTTP_SERVER_VARS, "HTTP_IF_UNMODIFIED_SINCE", 25)) ||
1622 http_modified_match("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod)))
1623 ) {
1624
1625 STATUS result = FAILURE;
1626 zval *zranges = NULL;
1627 MAKE_STD_ZVAL(zranges);
1628 array_init(zranges);
1629
1630 switch (http_get_request_ranges(zranges, data_size))
1631 {
1632 case RANGE_NO:
1633 zval_dtor(zranges);
1634 efree(zranges);
1635 /* go ahead and send all */
1636 break;
1637
1638 case RANGE_OK:
1639 result = http_send_ranges(zranges, data_ptr, data_size, data_mode);
1640 zval_dtor(zranges);
1641 efree(zranges);
1642 return result;
1643 break;
1644
1645 case RANGE_ERR:
1646 zval_dtor(zranges);
1647 efree(zranges);
1648 http_send_status(416);
1649 return FAILURE;
1650 break;
1651
1652 default:
1653 return FAILURE;
1654 break;
1655 }
1656 }
1657 }
1658 /* send all */
1659 return http_send_chunk(data_ptr, 0, data_size, data_mode);
1660 }
1661 /* }}} */
1662
1663 /* {{{ STATUS http_send_data(zval *) */
1664 PHP_HTTP_API STATUS _http_send_data(const zval *zdata TSRMLS_DC)
1665 {
1666 if (!Z_STRLEN_P(zdata)) {
1667 return SUCCESS;
1668 }
1669 if (!Z_STRVAL_P(zdata)) {
1670 return FAILURE;
1671 }
1672
1673 return http_send(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata), SEND_DATA);
1674 }
1675 /* }}} */
1676
1677 /* {{{ STATUS http_send_stream(php_stream *) */
1678 PHP_HTTP_API STATUS _http_send_stream(const php_stream *file TSRMLS_DC)
1679 {
1680 if (php_stream_stat((php_stream *) file, &HTTP_G(ssb))) {
1681 return FAILURE;
1682 }
1683
1684 return http_send(file, HTTP_G(ssb).sb.st_size, SEND_RSRC);
1685 }
1686 /* }}} */
1687
1688 /* {{{ STATUS http_send_file(zval *) */
1689 PHP_HTTP_API STATUS _http_send_file(const zval *zfile TSRMLS_DC)
1690 {
1691 php_stream *file;
1692 STATUS ret;
1693
1694 if (!Z_STRLEN_P(zfile)) {
1695 return FAILURE;
1696 }
1697
1698 if (!(file = php_stream_open_wrapper(Z_STRVAL_P(zfile), "rb",
1699 REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL))) {
1700 return FAILURE;
1701 }
1702
1703 ret = http_send_stream(file);
1704 php_stream_close(file);
1705 return ret;
1706 }
1707 /* }}} */
1708
1709 /* {{{ proto STATUS http_chunked_decode(char *, size_t, char **, size_t *) */
1710 PHP_HTTP_API STATUS _http_chunked_decode(const char *encoded,
1711 const size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
1712 {
1713 const char *e_ptr;
1714 char *d_ptr;
1715
1716 *decoded_len = 0;
1717 *decoded = (char *) ecalloc(encoded_len, 1);
1718 d_ptr = *decoded;
1719 e_ptr = encoded;
1720
1721 while (((e_ptr - encoded) - encoded_len) > 0) {
1722 char hex_len[9] = {0};
1723 size_t chunk_len = 0;
1724 int i = 0;
1725
1726 /* read in chunk size */
1727 while (isxdigit(*e_ptr)) {
1728 if (i == 9) {
1729 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1730 "Chunk size is too long: 0x%s...", hex_len);
1731 efree(*decoded);
1732 return FAILURE;
1733 }
1734 hex_len[i++] = *e_ptr++;
1735 }
1736
1737 /* reached the end */
1738 if (!strcmp(hex_len, "0")) {
1739 break;
1740 }
1741
1742 /* new line */
1743 if (strncmp(e_ptr, HTTP_CRLF, 2)) {
1744 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1745 "Invalid character (expected 0x0D 0x0A; got: %x %x)",
1746 *e_ptr, *(e_ptr + 1));
1747 efree(*decoded);
1748 return FAILURE;
1749 }
1750
1751 /* hex to long */
1752 {
1753 char *error = NULL;
1754 chunk_len = strtol(hex_len, &error, 16);
1755 if (error == hex_len) {
1756 php_error_docref(NULL TSRMLS_CC, E_WARNING,
1757 "Invalid chunk size string: '%s'", hex_len);
1758 efree(*decoded);
1759 return FAILURE;
1760 }
1761 }
1762
1763 memcpy(d_ptr, e_ptr += 2, chunk_len);
1764 d_ptr += chunk_len;
1765 e_ptr += chunk_len + 2;
1766 *decoded_len += chunk_len;
1767 }
1768
1769 return SUCCESS;
1770 }
1771 /* }}} */
1772
1773 /* {{{ proto STATUS http_split_response(zval *, zval *, zval *) */
1774 PHP_HTTP_API STATUS _http_split_response(const zval *zresponse, zval *zheaders,
1775 zval *zbody TSRMLS_DC)
1776 {
1777 char *header, *response, *body = NULL;
1778 long response_len = Z_STRLEN_P(zresponse);
1779 header = response = Z_STRVAL_P(zresponse);
1780
1781 while ((response - Z_STRVAL_P(zresponse) + 3) < response_len) {
1782 if ( (*response++ == '\r') &&
1783 (*response++ == '\n') &&
1784 (*response++ == '\r') &&
1785 (*response++ == '\n')) {
1786 body = response;
1787 break;
1788 }
1789 }
1790
1791 if (body && (response_len - (body - header))) {
1792 ZVAL_STRINGL(zbody, body, response_len - (body - header) - 1, 1);
1793 } else {
1794 Z_TYPE_P(zbody) = IS_NULL;
1795 }
1796
1797 return http_parse_headers(header, body - Z_STRVAL_P(zresponse), zheaders);
1798 }
1799 /* }}} */
1800
1801 /* {{{ STATUS http_parse_headers(char *, long, zval *) */
1802 PHP_HTTP_API STATUS _http_parse_headers(char *header, int header_len, zval *array TSRMLS_DC)
1803 {
1804 char *colon = NULL, *line = NULL, *begin = header;
1805
1806 if (header_len < 2) {
1807 return FAILURE;
1808 }
1809
1810 /* status code */
1811 if (!strncmp(header, "HTTP/1.", 7)) {
1812 char *end = strstr(header, HTTP_CRLF);
1813 add_assoc_stringl(array, "Status",
1814 header + strlen("HTTP/1.x "),
1815 end - (header + strlen("HTTP/1.x ")), 1);
1816 header = end + 2;
1817 }
1818
1819 line = header;
1820
1821 while (header_len >= (line - begin)) {
1822 int value_len = 0;
1823
1824 switch (*line++)
1825 {
1826 case 0:
1827 --value_len; /* we don't have CR so value length is one char less */
1828 case '\n':
1829 if (colon && ((!(*line - 1)) || ((*line != ' ') && (*line != '\t')))) {
1830
1831 /* skip empty key */
1832 if (header != colon) {
1833 char *key = estrndup(header, colon - header);
1834 value_len += line - colon - 1;
1835
1836 /* skip leading ws */
1837 while (isspace(*(++colon))) --value_len;
1838 /* skip trailing ws */
1839 while (isspace(colon[value_len - 1])) --value_len;
1840
1841 if (value_len < 1) {
1842 /* hm, empty header? */
1843 add_assoc_stringl(array, key, "", 0, 1);
1844 } else {
1845 add_assoc_stringl(array, key, colon, value_len, 1);
1846 }
1847 efree(key);
1848 }
1849
1850 colon = NULL;
1851 value_len = 0;
1852 header += line - header;
1853 }
1854 break;
1855
1856 case ':':
1857 if (!colon) {
1858 colon = line - 1;
1859 }
1860 break;
1861 }
1862 }
1863 return SUCCESS;
1864 }
1865 /* }}} */
1866
1867 /* {{{ void http_get_request_headers(zval *) */
1868 PHP_HTTP_API void _http_get_request_headers(zval *array TSRMLS_DC)
1869 {
1870 char *key;
1871
1872 for ( zend_hash_internal_pointer_reset(HTTP_SERVER_VARS);
1873 zend_hash_get_current_key(HTTP_SERVER_VARS, &key, NULL, 0) != HASH_KEY_NON_EXISTANT;
1874 zend_hash_move_forward(HTTP_SERVER_VARS)) {
1875 if (!strncmp(key, "HTTP_", 5)) {
1876 zval **header;
1877 zend_hash_get_current_data(HTTP_SERVER_VARS, (void **) &header);
1878 add_assoc_stringl(array, pretty_key(key + 5, strlen(key) - 5, 1, 1), Z_STRVAL_PP(header), Z_STRLEN_PP(header), 1);
1879 }
1880 }
1881 }
1882 /* }}} */
1883
1884 /* {{{ HAVE_CURL */
1885 #ifdef HTTP_HAVE_CURL
1886
1887 /* {{{ STATUS http_get(char *, HashTable *, HashTable *, char **, size_t *) */
1888 PHP_HTTP_API STATUS _http_get(const char *URL, HashTable *options,
1889 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
1890 {
1891 CURL *ch = curl_easy_init();
1892
1893 if (!ch) {
1894 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
1895 return FAILURE;
1896 }
1897
1898 http_curl_initbuf(CURLBUF_EVRY);
1899 http_curl_setopts(ch, URL, options);
1900
1901 if (CURLE_OK != curl_easy_perform(ch)) {
1902 curl_easy_cleanup(ch);
1903 http_curl_freebuf(CURLBUF_EVRY);
1904 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
1905 return FAILURE;
1906 }
1907 if (info) {
1908 http_curl_getinfo(ch, info);
1909 }
1910 curl_easy_cleanup(ch);
1911
1912 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
1913
1914 return SUCCESS;
1915 }
1916 /* }}} */
1917
1918 /* {{{ STATUS http_head(char *, HashTable *, HashTable *, char **data, size_t *) */
1919 PHP_HTTP_API STATUS _http_head(const char *URL, HashTable *options,
1920 HashTable *info, char **data, size_t *data_len TSRMLS_DC)
1921 {
1922 CURL *ch = curl_easy_init();
1923
1924 if (!ch) {
1925 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
1926 return FAILURE;
1927 }
1928
1929 http_curl_initbuf(CURLBUF_HDRS);
1930 http_curl_setopts(ch, URL, options);
1931 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
1932
1933 if (CURLE_OK != curl_easy_perform(ch)) {
1934 curl_easy_cleanup(ch);
1935 http_curl_freebuf(CURLBUF_HDRS);
1936 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
1937 return FAILURE;
1938 }
1939 if (info) {
1940 http_curl_getinfo(ch, info);
1941 }
1942 curl_easy_cleanup(ch);
1943
1944 http_curl_movebuf(CURLBUF_HDRS, data, data_len);
1945
1946 return SUCCESS;
1947 }
1948 /* }}} */
1949
1950 /* {{{ STATUS http_post_data(char *, char *, size_t, HashTable *, HashTable *, char **, size_t *) */
1951 PHP_HTTP_API STATUS _http_post_data(const char *URL, char *postdata,
1952 size_t postdata_len, HashTable *options, HashTable *info, char **data,
1953 size_t *data_len TSRMLS_DC)
1954 {
1955 CURL *ch = curl_easy_init();
1956
1957 if (!ch) {
1958 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize curl");
1959 return FAILURE;
1960 }
1961
1962 http_curl_initbuf(CURLBUF_EVRY);
1963 http_curl_setopts(ch, URL, options);
1964 curl_easy_setopt(ch, CURLOPT_POST, 1);
1965 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, postdata);
1966 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, postdata_len);
1967
1968 if (CURLE_OK != curl_easy_perform(ch)) {
1969 curl_easy_cleanup(ch);
1970 http_curl_freebuf(CURLBUF_EVRY);
1971 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not perform request");
1972 return FAILURE;
1973 }
1974 if (info) {
1975 http_curl_getinfo(ch, info);
1976 }
1977 curl_easy_cleanup(ch);
1978
1979 http_curl_movebuf(CURLBUF_EVRY, data, data_len);
1980
1981 return SUCCESS;
1982 }
1983 /* }}} */
1984
1985 /* {{{ STATUS http_post_array(char *, HashTable *, HashTable *, HashTable *, char **, size_t *) */
1986 PHP_HTTP_API STATUS _http_post_array(const char *URL, HashTable *postarray,
1987 HashTable *options, HashTable *info, char **data, size_t *data_len TSRMLS_DC)
1988 {
1989 smart_str qstr = {0};
1990 STATUS status;
1991
1992 if (php_url_encode_hash_ex(postarray, &qstr, NULL,0,NULL,0,NULL,0,NULL TSRMLS_CC) != SUCCESS) {
1993 if (qstr.c) {
1994 efree(qstr.c);
1995 }
1996 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not encode post data");
1997 return FAILURE;
1998 }
1999 smart_str_0(&qstr);
2000
2001 status = http_post_data(URL, qstr.c, qstr.len, options, info, data, data_len);
2002 if (qstr.c) {
2003 efree(qstr.c);
2004 }
2005 return status;
2006 }
2007 /* }}} */
2008
2009 #endif
2010 /* }}} HAVE_CURL */
2011
2012 /* {{{ STATUS http_auth_header(char *, char*) */
2013 PHP_HTTP_API STATUS _http_auth_header(const char *type, const char *realm TSRMLS_DC)
2014 {
2015 char realm_header[1024] = {0};
2016 snprintf(realm_header, 1023, "WWW-Authenticate: %s realm=\"%s\"", type, realm);
2017 return http_send_status_header(401, realm_header);
2018 }
2019 /* }}} */
2020
2021 /* {{{ STATUS http_auth_credentials(char **, char **) */
2022 PHP_HTTP_API STATUS _http_auth_credentials(char **user, char **pass TSRMLS_DC)
2023 {
2024 if (strncmp(sapi_module.name, "isapi", 5)) {
2025 zval *zuser, *zpass;
2026
2027 HTTP_GSC(zuser, "PHP_AUTH_USER", FAILURE);
2028 HTTP_GSC(zpass, "PHP_AUTH_PW", FAILURE);
2029
2030 *user = estrndup(Z_STRVAL_P(zuser), Z_STRLEN_P(zuser));
2031 *pass = estrndup(Z_STRVAL_P(zpass), Z_STRLEN_P(zpass));
2032
2033 return SUCCESS;
2034 } else {
2035 zval *zauth = NULL;
2036 HTTP_GSC(zauth, "HTTP_AUTHORIZATION", FAILURE);
2037 {
2038 char *decoded, *colon;
2039 int decoded_len;
2040 decoded = php_base64_decode(Z_STRVAL_P(zauth), Z_STRLEN_P(zauth),
2041 &decoded_len);
2042
2043 if (colon = strchr(decoded + 6, ':')) {
2044 *user = estrndup(decoded + 6, colon - decoded - 6);
2045 *pass = estrndup(colon + 1, decoded + decoded_len - colon - 6 - 1);
2046
2047 return SUCCESS;
2048 } else {
2049 return FAILURE;
2050 }
2051 }
2052 }
2053 }
2054 /* }}} */
2055
2056 /* }}} public API */
2057
2058 /*
2059 * Local variables:
2060 * tab-width: 4
2061 * c-basic-offset: 4
2062 * End:
2063 * vim600: noet sw=4 ts=4 fdm=marker
2064 * vim<600: noet sw=4 ts=4
2065 */