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