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