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