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