5a9093690b4e32384e0b73f8188d09d5ca0b97ec
[m6w6/ext-http] / http_request_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 #ifdef PHP_WIN32
23 # include <winsock2.h>
24 #endif
25
26 #include <curl/curl.h>
27
28 #include "phpstr/phpstr.h"
29
30 #include "php.h"
31 #include "php_http.h"
32 #include "php_http_std_defs.h"
33 #include "php_http_api.h"
34 #include "php_http_request_api.h"
35 #include "php_http_request_object.h"
36 #include "php_http_requestpool_object.h"
37 #include "php_http_url_api.h"
38
39 #ifndef HTTP_CURL_USE_ZEND_MM
40 # define HTTP_CURL_USE_ZEND_MM 0
41 #endif
42
43 ZEND_EXTERN_MODULE_GLOBALS(http)
44
45 #if LIBCURL_VERSION_NUM < 0x070c00
46 # define curl_easy_strerror(code) HTTP_G(request).curl.error
47 #endif
48
49 #define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
50 #define HTTP_CURL_INFO_EX(I, X) \
51 switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
52 { \
53 case CURLINFO_STRING: \
54 { \
55 char *c; \
56 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &c)) { \
57 add_assoc_string(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), c ? c : "", 1); \
58 } \
59 } \
60 break; \
61 \
62 case CURLINFO_DOUBLE: \
63 { \
64 double d; \
65 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &d)) { \
66 add_assoc_double(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), d); \
67 } \
68 } \
69 break; \
70 \
71 case CURLINFO_LONG: \
72 { \
73 long l; \
74 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_ ##I, &l)) { \
75 add_assoc_long(&array, pretty_key(http_request_data_copy(COPY_STRING, #X), sizeof(#X)-1, 0, 0), l); \
76 } \
77 } \
78 break; \
79 }
80
81 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(ch, CURLOPT_##OPTION, (p))
82 #define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
83 #define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
84 #define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
85 #define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
86 if (!strcasecmp(key, #keyname)) { \
87 convert_to_string_ex(param); \
88 HTTP_CURL_OPT(optname, http_request_data_copy(COPY_STRING, Z_STRVAL_PP(param))); \
89 key = NULL; \
90 continue; \
91 }
92 #define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
93 #define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
94 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
95 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
96 if (!strcasecmp(key, #keyname)) { \
97 convert_to_long_ex(param); \
98 HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
99 key = NULL; \
100 continue; \
101 }
102
103
104 static const char *const http_request_methods[HTTP_MAX_REQUEST_METHOD + 1];
105 #define http_curl_getopt(o, k, t) _http_curl_getopt_ex((o), (k), sizeof(k), (t) TSRMLS_CC)
106 #define http_curl_getopt_ex(o, k, l, t) _http_curl_getopt_ex((o), (k), (l), (t) TSRMLS_CC)
107 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
108 static size_t http_curl_write_callback(char *, size_t, size_t, void *);
109 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
110 static int http_curl_progress_callback(void *, double, double, double, double);
111 static int http_curl_debug_callback(CURL *, curl_infotype, char *, size_t, void *);
112
113 typedef struct {
114 void ***tsrm_ctx;
115 void *data;
116 } http_curl_callback_ctx;
117
118 #define HTTP_CURL_CALLBACK_DATA(from, type, var) \
119 http_curl_callback_ctx *__CTX = (http_curl_callback_ctx *) (from); \
120 TSRMLS_FETCH_FROM_CTX(__CTX->tsrm_ctx); \
121 type (var) = (type) (__CTX->data)
122
123 #define http_curl_callback_data(data) _http_curl_callback_data((data) TSRMLS_CC)
124 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC);
125
126 static void http_request_pool_freebody(http_request_body **body);
127 static void http_request_pool_freehandle(zval **request);
128 static void http_request_pool_responsehandler(zval **req TSRMLS_DC);
129 static inline STATUS http_request_pool_select(http_request_pool *pool);
130 static inline void http_request_pool_perform(http_request_pool *pool);
131
132 #if HTTP_CURL_USE_ZEND_MM
133 static void http_curl_free(void *p) { efree(p); }
134 static char *http_curl_strdup(const char *p) { return estrdup(p); }
135 static void *http_curl_malloc(size_t s) { return emalloc(s); }
136 static void *http_curl_realloc(void *p, size_t s) { return erealloc(p, s); }
137 static void *http_curl_calloc(size_t n, size_t s) { return ecalloc(n, s); }
138 #endif
139
140 /* {{{ STATUS http_request_global_init() */
141 STATUS _http_request_global_init(void)
142 {
143 #if HTTP_CURL_USE_ZEND_MM
144 if (CURLE_OK != curl_global_init_mem(CURL_GLOBAL_ALL,
145 http_curl_malloc,
146 http_curl_free,
147 http_curl_realloc,
148 http_curl_strdup,
149 http_curl_calloc)) {
150 return FAILURE;
151 }
152 #else
153 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
154 return FAILURE;
155 }
156 #endif
157 return SUCCESS;
158 }
159 /* }}} */
160
161 /* {{{ void *http_request_data_copy(int, void *) */
162 void *_http_request_data_copy(int type, void *data TSRMLS_DC)
163 {
164 switch (type)
165 {
166 case COPY_STRING:
167 {
168 char *new_str = estrdup((const char*) data);
169 zend_llist_add_element(&HTTP_G(request).copies.strings, &new_str);
170 return new_str;
171 }
172
173 case COPY_SLIST:
174 {
175 zend_llist_add_element(&HTTP_G(request).copies.slists, &data);
176 return data;
177 }
178
179 default:
180 {
181 return data;
182 }
183 }
184 }
185 /* }}} */
186
187 /* {{{ void http_request_data_free_string(char **) */
188 void _http_request_data_free_string(void *string)
189 {
190 efree(*((char **)string));
191 }
192 /* }}} */
193
194 /* {{{ void http_request_data_free_slist(struct curl_slist **) */
195 void _http_request_data_free_slist(void *list)
196 {
197 curl_slist_free_all(*((struct curl_slist **) list));
198 }
199 /* }}} */
200
201 /* {{{ http_request_body *http_request_body_new() */
202 PHP_HTTP_API http_request_body *_http_request_body_new(TSRMLS_D)
203 {
204 http_request_body *body = ecalloc(1, sizeof(http_request_body));
205 return body;
206 }
207 /* }}} */
208
209 /* {{{ STATUS http_request_body_fill(http_request_body *body, HashTable *, HashTable *) */
210 PHP_HTTP_API STATUS _http_request_body_fill(http_request_body *body, HashTable *fields, HashTable *files TSRMLS_DC)
211 {
212 if (files && (zend_hash_num_elements(files) > 0)) {
213 char *key = NULL;
214 ulong idx;
215 zval **data;
216 struct curl_httppost *http_post_data[2] = {NULL, NULL};
217
218 /* normal data */
219 FOREACH_HASH_KEYVAL(fields, key, idx, data) {
220 CURLcode err;
221 if (key) {
222 convert_to_string_ex(data);
223 err = curl_formadd(&http_post_data[0], &http_post_data[1],
224 CURLFORM_COPYNAME, key,
225 CURLFORM_COPYCONTENTS, Z_STRVAL_PP(data),
226 CURLFORM_CONTENTSLENGTH, Z_STRLEN_PP(data),
227 CURLFORM_END
228 );
229 if (CURLE_OK != err) {
230 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post fields: %s", curl_easy_strerror(err));
231 curl_formfree(http_post_data[0]);
232 return FAILURE;
233 }
234
235 /* reset */
236 key = NULL;
237 }
238 }
239
240 /* file data */
241 FOREACH_HASH_VAL(files, data) {
242 CURLcode err;
243 zval **file, **type, **name;
244 if ( SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "name", sizeof("name"), (void **) &name) &&
245 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "type", sizeof("type"), (void **) &type) &&
246 SUCCESS == zend_hash_find(Z_ARRVAL_PP(data), "file", sizeof("file"), (void **) &file)) {
247 err = curl_formadd(&http_post_data[0], &http_post_data[1],
248 CURLFORM_COPYNAME, Z_STRVAL_PP(name),
249 CURLFORM_FILE, Z_STRVAL_PP(file),
250 CURLFORM_CONTENTTYPE, Z_STRVAL_PP(type),
251 CURLFORM_END
252 );
253 if (CURLE_OK != err) {
254 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not encode post files: %s", curl_easy_strerror(err));
255 curl_formfree(http_post_data[0]);
256 return FAILURE;
257 }
258 } else {
259 http_error(E_NOTICE, HTTP_E_PARAM, "Post file array entry misses either 'name', 'type' or 'file' entry");
260 }
261 }
262
263 body->type = HTTP_REQUEST_BODY_CURLPOST;
264 body->data = http_post_data[0];
265 body->size = 0;
266
267 } else {
268 char *encoded;
269 size_t encoded_len;
270
271 if (SUCCESS != http_urlencode_hash_ex(fields, 1, NULL, 0, &encoded, &encoded_len)) {
272 http_error(E_WARNING, HTTP_E_ENCODE, "Could not encode post data");
273 return FAILURE;
274 }
275
276 body->type = HTTP_REQUEST_BODY_CSTRING;
277 body->data = encoded;
278 body->size = encoded_len;
279 }
280
281 return SUCCESS;
282 }
283 /* }}} */
284
285 /* {{{ void http_request_body_dtor(http_request_body *) */
286 PHP_HTTP_API void _http_request_body_dtor(http_request_body *body TSRMLS_DC)
287 {
288 if (body) {
289 switch (body->type)
290 {
291 case HTTP_REQUEST_BODY_CSTRING:
292 if (body->data) {
293 efree(body->data);
294 }
295 break;
296
297 case HTTP_REQUEST_BODY_CURLPOST:
298 curl_formfree(body->data);
299 break;
300
301 case HTTP_REQUEST_BODY_UPLOADFILE:
302 php_stream_close(body->data);
303 break;
304 }
305 }
306 }
307 /* }}} */
308
309 /* {{{ void http_request_body_free(http_request_body *) */
310 PHP_HTTP_API void _http_request_body_free(http_request_body *body TSRMLS_DC)
311 {
312 if (body) {
313 http_request_body_dtor(body);
314 efree(body);
315 }
316 }
317 /* }}} */
318
319 /* {{{ STATUS http_request_init(CURL *, http_request_method, char *, http_request_body *, HashTable *, phpstr *) */
320 PHP_HTTP_API STATUS _http_request_init(CURL *ch, http_request_method meth, const char *url, http_request_body *body, HashTable *options, phpstr *response TSRMLS_DC)
321 {
322 zval *zoption;
323 zend_bool range_req = 0;
324
325 /* reset CURL handle */
326 #if LIBCURL_VERSION_NUM >= 0x070c01
327 curl_easy_reset(ch);
328 #endif
329
330 /* set options */
331 if (url) {
332 HTTP_CURL_OPT(URL, http_request_data_copy(COPY_STRING, (void *) url));
333 }
334
335 if (response) {
336 HTTP_CURL_OPT(WRITEDATA, http_curl_callback_data(response));
337 HTTP_CURL_OPT(WRITEHEADER, http_curl_callback_data(response));
338 }
339
340 HTTP_CURL_OPT(HEADER, 0);
341 HTTP_CURL_OPT(FILETIME, 1);
342 HTTP_CURL_OPT(AUTOREFERER, 1);
343 HTTP_CURL_OPT(READFUNCTION, http_curl_read_callback);
344 HTTP_CURL_OPT(WRITEFUNCTION, http_curl_write_callback);
345 HTTP_CURL_OPT(HEADERFUNCTION, http_curl_write_callback);
346
347 #if defined(ZTS) && (LIBCURL_VERSION_NUM >= 0x070a00)
348 HTTP_CURL_OPT(NOSIGNAL, 1);
349 #endif
350 #if LIBCURL_VERSION_NUM < 0x070c00
351 HTTP_CURL_OPT(ERRORBUFFER, HTTP_G(request).curl.error);
352 #endif
353
354 /* progress callback */
355 if (zoption = http_curl_getopt(options, "onprogress", 0)) {
356 HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
357 HTTP_CURL_OPT(PROGRESSDATA, http_curl_callback_data(zoption));
358 } else {
359 HTTP_CURL_OPT(NOPROGRESS, 1);
360 }
361
362 /* debug callback */
363 if (zoption = http_curl_getopt(options, "ondebug", 0)) {
364 HTTP_CURL_OPT(VERBOSE, 1);
365 HTTP_CURL_OPT(DEBUGFUNCTION, http_curl_debug_callback);
366 HTTP_CURL_OPT(DEBUGDATA, http_curl_callback_data(zoption));
367 } else {
368 HTTP_CURL_OPT(VERBOSE, 0);
369 }
370
371 /* proxy */
372 if (zoption = http_curl_getopt(options, "proxyhost", IS_STRING)) {
373 HTTP_CURL_OPT(PROXY, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
374 /* port */
375 if (zoption = http_curl_getopt(options, "proxyport", IS_LONG)) {
376 HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
377 }
378 /* user:pass */
379 if (zoption = http_curl_getopt(options, "proxyauth", IS_STRING)) {
380 HTTP_CURL_OPT(PROXYUSERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
381 }
382 #if LIBCURL_VERSION_NUM >= 0x070a07
383 /* auth method */
384 if (zoption = http_curl_getopt(options, "proxyauthtype", IS_LONG)) {
385 HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
386 }
387 #endif
388 }
389
390 /* outgoing interface */
391 if (zoption = http_curl_getopt(options, "interface", IS_STRING)) {
392 HTTP_CURL_OPT(INTERFACE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
393 }
394
395 /* another port */
396 if (zoption = http_curl_getopt(options, "port", IS_LONG)) {
397 HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
398 }
399
400 /* auth */
401 if (zoption = http_curl_getopt(options, "httpauth", IS_STRING)) {
402 HTTP_CURL_OPT(USERPWD, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
403 }
404 #if LIBCURL_VERSION_NUM >= 0x070a06
405 if (zoption = http_curl_getopt(options, "httpauthtype", IS_LONG)) {
406 HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
407 }
408 #endif
409
410 /* compress, empty string enables deflate and gzip */
411 if (zoption = http_curl_getopt(options, "compress", IS_BOOL)) {
412 if (Z_LVAL_P(zoption)) {
413 HTTP_CURL_OPT(ENCODING, http_request_data_copy(COPY_STRING, ""));
414 }
415 }
416
417 /* redirects, defaults to 0 */
418 if (zoption = http_curl_getopt(options, "redirect", IS_LONG)) {
419 HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
420 HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
421 if (zoption = http_curl_getopt(options, "unrestrictedauth", IS_BOOL)) {
422 HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
423 }
424 } else {
425 HTTP_CURL_OPT(FOLLOWLOCATION, 0);
426 }
427
428 /* referer */
429 if (zoption = http_curl_getopt(options, "referer", IS_STRING)) {
430 HTTP_CURL_OPT(REFERER, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
431 }
432
433 /* useragent, default "PECL::HTTP/version (PHP/version)" */
434 if (zoption = http_curl_getopt(options, "useragent", IS_STRING)) {
435 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
436 } else {
437 HTTP_CURL_OPT(USERAGENT, http_request_data_copy(COPY_STRING, "PECL::HTTP/" HTTP_PEXT_VERSION " (PHP/" PHP_VERSION ")"));
438 }
439
440 /* additional headers, array('name' => 'value') */
441 if (zoption = http_curl_getopt(options, "headers", IS_ARRAY)) {
442 char *header_key;
443 ulong header_idx;
444 struct curl_slist *headers = NULL;
445
446 FOREACH_KEY(zoption, header_key, header_idx) {
447 if (header_key) {
448 zval **header_val;
449 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &header_val)) {
450 char header[1024] = {0};
451 snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
452 headers = curl_slist_append(headers, http_request_data_copy(COPY_STRING, header));
453 }
454
455 /* reset */
456 header_key = NULL;
457 }
458 }
459
460 if (headers) {
461 HTTP_CURL_OPT(HTTPHEADER, http_request_data_copy(COPY_SLIST, headers));
462 }
463 } else {
464 HTTP_CURL_OPT(HTTPHEADER, NULL);
465 }
466
467 /* cookies, array('name' => 'value') */
468 if (zoption = http_curl_getopt(options, "cookies", IS_ARRAY)) {
469 char *cookie_key = NULL;
470 ulong cookie_idx = 0;
471 phpstr *qstr = phpstr_new();
472
473 FOREACH_KEY(zoption, cookie_key, cookie_idx) {
474 if (cookie_key) {
475 zval **cookie_val;
476 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void **) &cookie_val)) {
477 phpstr_appendf(qstr, "%s=%s; ", cookie_key, Z_STRVAL_PP(cookie_val));
478 }
479
480 /* reset */
481 cookie_key = NULL;
482 }
483 }
484
485 if (qstr->used) {
486 phpstr_fix(qstr);
487 HTTP_CURL_OPT(COOKIE, http_request_data_copy(COPY_STRING, qstr->data));
488 }
489 phpstr_free(qstr);
490 }
491
492 /* cookiestore */
493 if (zoption = http_curl_getopt(options, "cookiestore", IS_STRING)) {
494 HTTP_CURL_OPT(COOKIEFILE, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
495 HTTP_CURL_OPT(COOKIEJAR, http_request_data_copy(COPY_STRING, Z_STRVAL_P(zoption)));
496 }
497
498 /* resume */
499 if (zoption = http_curl_getopt(options, "resume", IS_LONG)) {
500 range_req = 1;
501 HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
502 }
503
504 /* maxfilesize */
505 if (zoption = http_curl_getopt(options, "maxfilesize", IS_LONG)) {
506 HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
507 }
508
509 /* lastmodified */
510 if (zoption = http_curl_getopt(options, "lastmodified", IS_LONG)) {
511 HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
512 HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
513 }
514
515 /* timeout */
516 if (zoption = http_curl_getopt(options, "timeout", IS_LONG)) {
517 HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
518 }
519
520 /* connecttimeout, defaults to 1 */
521 if (zoption = http_curl_getopt(options, "connecttimeout", IS_LONG)) {
522 HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
523 } else {
524 HTTP_CURL_OPT(CONNECTTIMEOUT, 1);
525 }
526
527 /* ssl */
528 if (zoption = http_curl_getopt(options, "ssl", IS_ARRAY)) {
529 ulong idx;
530 char *key = NULL;
531 zval **param;
532
533 FOREACH_KEYVAL(zoption, key, idx, param) {
534 if (key) {
535 HTTP_CURL_OPT_SSL_STRING(CERT);
536 #if LIBCURL_VERSION_NUM >= 0x070903
537 HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
538 #endif
539 HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
540
541 HTTP_CURL_OPT_SSL_STRING(KEY);
542 HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
543 HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
544
545 HTTP_CURL_OPT_SSL_STRING(ENGINE);
546 HTTP_CURL_OPT_SSL_LONG(VERSION);
547
548 HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
549 HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
550 HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
551
552
553 HTTP_CURL_OPT_STRING(CAINFO);
554 #if LIBCURL_VERSION_NUM >= 0x070908
555 HTTP_CURL_OPT_STRING(CAPATH);
556 #endif
557 HTTP_CURL_OPT_STRING(RANDOM_FILE);
558 HTTP_CURL_OPT_STRING(EGDSOCKET);
559
560 /* reset key */
561 key = NULL;
562 }
563 }
564 } else {
565 /* disable SSL verification by default */
566 HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
567 HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
568 }
569
570 /* request method */
571 switch (meth)
572 {
573 case HTTP_GET:
574 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1);
575 break;
576
577 case HTTP_HEAD:
578 curl_easy_setopt(ch, CURLOPT_NOBODY, 1);
579 break;
580
581 case HTTP_POST:
582 curl_easy_setopt(ch, CURLOPT_POST, 1);
583 break;
584
585 case HTTP_PUT:
586 curl_easy_setopt(ch, CURLOPT_UPLOAD, 1);
587 break;
588
589 default:
590 if (http_request_method_exists(0, meth, NULL)) {
591 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, http_request_method_name(meth));
592 } else {
593 http_error_ex(E_WARNING, HTTP_E_CURL, "Unsupported request method: %d", meth);
594 return FAILURE;
595 }
596 break;
597 }
598
599 /* attach request body */
600 if (body && (meth != HTTP_GET) && (meth != HTTP_HEAD)) {
601 switch (body->type)
602 {
603 case HTTP_REQUEST_BODY_CSTRING:
604 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, (char *) body->data);
605 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, body->size);
606 break;
607
608 case HTTP_REQUEST_BODY_CURLPOST:
609 curl_easy_setopt(ch, CURLOPT_HTTPPOST, (struct curl_httppost *) body->data);
610 break;
611
612 case HTTP_REQUEST_BODY_UPLOADFILE:
613 case HTTP_REQUEST_BODY_UPLOADDATA:
614 curl_easy_setopt(ch, CURLOPT_READDATA, body);
615 curl_easy_setopt(ch, CURLOPT_INFILESIZE, body->size);
616 break;
617
618 default:
619 http_error_ex(E_WARNING, HTTP_E_CURL, "Unknown request body type: %d", body->type);
620 return FAILURE;
621 break;
622 }
623 }
624
625 return SUCCESS;
626 }
627 /* }}} */
628
629 /* {{{ STATUS http_request_exec(CURL *, HashTable *) */
630 PHP_HTTP_API STATUS _http_request_exec(CURL *ch, HashTable *info TSRMLS_DC)
631 {
632 CURLcode result;
633
634 /* perform request */
635 if (CURLE_OK != (result = curl_easy_perform(ch))) {
636 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not perform request: %s", curl_easy_strerror(result));
637 return FAILURE;
638 } else {
639 /* get curl info */
640 if (info) {
641 http_request_info(ch, info);
642 }
643 return SUCCESS;
644 }
645 }
646 /* }}} */
647
648 /* {{{ void http_request_info(CURL *, HashTable *) */
649 PHP_HTTP_API void _http_request_info(CURL *ch, HashTable *info TSRMLS_DC)
650 {
651 zval array;
652 Z_ARRVAL(array) = info;
653
654 HTTP_CURL_INFO(EFFECTIVE_URL);
655 #if LIBCURL_VERSION_NUM >= 0x070a07
656 HTTP_CURL_INFO(RESPONSE_CODE);
657 #else
658 HTTP_CURL_INFO_EX(HTTP_CODE, RESPONSE_CODE);
659 #endif
660 HTTP_CURL_INFO(HTTP_CONNECTCODE);
661 #if LIBCURL_VERSION_NUM >= 0x070500
662 HTTP_CURL_INFO(FILETIME);
663 #endif
664 HTTP_CURL_INFO(TOTAL_TIME);
665 HTTP_CURL_INFO(NAMELOOKUP_TIME);
666 HTTP_CURL_INFO(CONNECT_TIME);
667 HTTP_CURL_INFO(PRETRANSFER_TIME);
668 HTTP_CURL_INFO(STARTTRANSFER_TIME);
669 #if LIBCURL_VERSION_NUM >= 0x070907
670 HTTP_CURL_INFO(REDIRECT_TIME);
671 HTTP_CURL_INFO(REDIRECT_COUNT);
672 #endif
673 HTTP_CURL_INFO(SIZE_UPLOAD);
674 HTTP_CURL_INFO(SIZE_DOWNLOAD);
675 HTTP_CURL_INFO(SPEED_DOWNLOAD);
676 HTTP_CURL_INFO(SPEED_UPLOAD);
677 HTTP_CURL_INFO(HEADER_SIZE);
678 HTTP_CURL_INFO(REQUEST_SIZE);
679 HTTP_CURL_INFO(SSL_VERIFYRESULT);
680 #if LIBCURL_VERSION_NUM >= 0x070c03
681 /*HTTP_CURL_INFO(SSL_ENGINES); todo: CURLINFO_SLIST */
682 #endif
683 HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
684 HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
685 HTTP_CURL_INFO(CONTENT_TYPE);
686 #if LIBCURL_VERSION_NUM >= 0x070a03
687 /*HTTP_CURL_INFO(PRIVATE);*/
688 #endif
689 #if LIBCURL_VERSION_NUM >= 0x070a08
690 HTTP_CURL_INFO(HTTPAUTH_AVAIL);
691 HTTP_CURL_INFO(PROXYAUTH_AVAIL);
692 #endif
693 #if LIBCURL_VERSION_NUM >= 0x070c02
694 /*HTTP_CURL_INFO(OS_ERRNO);*/
695 #endif
696 #if LIBCURL_VERSION_NUM >= 0x070c03
697 HTTP_CURL_INFO(NUM_CONNECTS);
698 #endif
699 }
700 /* }}} */
701
702 /* {{{ STATUS http_request_ex(CURL *, http_request_method, char *, http_request_body, HashTable, HashTable, phpstr *) */
703 PHP_HTTP_API STATUS _http_request_ex(CURL *ch, http_request_method meth, const char *url, http_request_body *body, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
704 {
705 if (SUCCESS != http_request_init(ch, meth, url, body, options, response)) {
706 return FAILURE;
707 }
708 return http_request_exec(ch, info);
709 }
710 /* }}} */
711
712 /* {{{ char *http_request_method_name(http_request_method) */
713 PHP_HTTP_API const char *_http_request_method_name(http_request_method m TSRMLS_DC)
714 {
715 zval **meth;
716
717 if (HTTP_STD_REQUEST_METHOD(m)) {
718 return http_request_methods[m];
719 }
720
721 if (SUCCESS == zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(m), (void **) &meth)) {
722 return Z_STRVAL_PP(meth);
723 }
724
725 return http_request_methods[0];
726 }
727 /* }}} */
728
729 /* {{{ unsigned long http_request_method_exists(zend_bool, unsigned long, char *) */
730 PHP_HTTP_API unsigned long _http_request_method_exists(zend_bool by_name, unsigned long id, const char *name TSRMLS_DC)
731 {
732 if (by_name) {
733 unsigned i;
734
735 for (i = HTTP_NO_REQUEST_METHOD + 1; i < HTTP_MAX_REQUEST_METHOD; ++i) {
736 if (!strcmp(name, http_request_methods[i])) {
737 return i;
738 }
739 }
740 {
741 zval **data;
742 char *key;
743 ulong idx;
744
745 FOREACH_HASH_KEYVAL(&HTTP_G(request).methods.custom, key, idx, data) {
746 if (!strcmp(name, Z_STRVAL_PP(data))) {
747 return idx + HTTP_MAX_REQUEST_METHOD;
748 }
749 }
750 }
751 return 0;
752 } else {
753 return HTTP_STD_REQUEST_METHOD(id) || zend_hash_index_exists(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(id)) ? id : 0;
754 }
755 }
756 /* }}} */
757
758 /* {{{ unsigned long http_request_method_register(char *) */
759 PHP_HTTP_API unsigned long _http_request_method_register(const char *method TSRMLS_DC)
760 {
761 zval array;
762 char *http_method;
763 unsigned long meth_num = HTTP_G(request).methods.custom.nNextFreeElement + HTTP_MAX_REQUEST_METHOD;
764
765 Z_ARRVAL(array) = &HTTP_G(request).methods.custom;
766 add_next_index_string(&array, estrdup(method), 0);
767
768 spprintf(&http_method, 0, "HTTP_%s", method);
769 zend_register_long_constant(http_method, strlen(http_method) + 1, meth_num, CONST_CS, http_module_number TSRMLS_CC);
770 efree(http_method);
771
772 return meth_num;
773 }
774 /* }}} */
775
776 /* {{{ STATUS http_request_method_unregister(usngigned long) */
777 PHP_HTTP_API STATUS _http_request_method_unregister(unsigned long method TSRMLS_DC)
778 {
779 zval **zmethod;
780 char *http_method;
781
782 if (SUCCESS != zend_hash_index_find(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method), (void **) &zmethod)) {
783 http_error_ex(E_NOTICE, HTTP_E_PARAM, "Request method with id %lu does not exist", method);
784 return FAILURE;
785 }
786
787 spprintf(&http_method, 0, "HTTP_%s", Z_STRVAL_PP(zmethod));
788
789 if ( (SUCCESS != zend_hash_index_del(&HTTP_G(request).methods.custom, HTTP_CUSTOM_REQUEST_METHOD(method)))
790 || (SUCCESS != zend_hash_del(EG(zend_constants), http_method, strlen(http_method) + 1))) {
791 http_error_ex(E_NOTICE, 0, "Could not unregister request method: %s", http_method);
792 efree(http_method);
793 return FAILURE;
794 }
795
796 efree(http_method);
797 return SUCCESS;
798 }
799 /* }}} */
800
801
802 /* {{{ http_request_pool *http_request_pool_init(http_request_pool *) */
803 PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool TSRMLS_DC)
804 {
805 zend_bool free_pool;
806
807 if ((free_pool = (!pool))) {
808 pool = emalloc(sizeof(http_request_pool));
809 pool->ch = NULL;
810 }
811
812 if (!pool->ch) {
813 if (!(pool->ch = curl_multi_init())) {
814 http_error(E_WARNING, HTTP_E_CURL, "Could not initialize curl");
815 if (free_pool) {
816 efree(pool);
817 }
818 return NULL;
819 }
820 }
821
822 pool->unfinished = 0;
823 zend_llist_init(&pool->handles, sizeof(zval *), (llist_dtor_func_t) ZVAL_PTR_DTOR, 0);
824 zend_llist_init(&pool->bodies, sizeof(http_request_body *), (llist_dtor_func_t) http_request_pool_freebody, 0);
825
826 return pool;
827 }
828 /* }}} */
829
830 /* {{{ STATUS http_request_pool_attach(http_request_pool *, zval *) */
831 PHP_HTTP_API STATUS _http_request_pool_attach(http_request_pool *pool, zval *request TSRMLS_DC)
832 {
833 getObjectEx(http_request_object, req, request);
834
835 if (req->pool) {
836 http_error(E_WARNING, HTTP_E_CURL, "HttpRequest object is already member of an HttpRequestPool");
837 } else {
838 CURLMcode code;
839 http_request_body *body = http_request_body_new();
840 zval *info = GET_PROP_EX(req, request, responseInfo);
841
842 if (SUCCESS != http_request_object_requesthandler(req, request, body)) {
843 efree(body);
844 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not initialize HttpRequest object for attaching to the HttpRequestPool");
845 } else if (CURLM_OK != (code = curl_multi_add_handle(pool->ch, req->ch))) {
846 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not attach HttpRequest object to the HttpRequestPool: %s", curl_multi_strerror(code));
847 } else {
848 req->pool = pool;
849 zval_add_ref(&request);
850 zend_llist_add_element(&pool->handles, &request);
851 zend_llist_add_element(&pool->bodies, &body);
852 return SUCCESS;
853 }
854 }
855 return FAILURE;
856 }
857 /* }}} */
858
859 /* {{{ STATUS http_request_pool_detach(http_request_pool *, zval *) */
860 PHP_HTTP_API STATUS _http_request_pool_detach(http_request_pool *pool, zval *request TSRMLS_DC)
861 {
862 getObjectEx(http_request_object, req, request);
863
864 if (req->pool != pool) {
865 http_error(E_WARNING, HTTP_E_CURL, "HttpRequest object is not attached to this HttpRequestPool");
866 } else {
867 CURLMcode code;
868
869 if (CURLM_OK != (code = curl_multi_remove_handle(pool->ch, req->ch))) {
870 http_error_ex(E_WARNING, HTTP_E_CURL, "Could not detach HttpRequest object from the HttpRequestPool: %s", curl_multi_strerror(code));
871 } else {
872 req->pool = NULL;
873 zval_ptr_dtor(&request);
874 return SUCCESS;
875 }
876 }
877 return FAILURE;
878 }
879 /* }}} */
880
881 /* {{{ STATUS http_request_pool_send(http_request_pool *) */
882 PHP_HTTP_API STATUS _http_request_pool_send(http_request_pool *pool TSRMLS_DC)
883 {
884 http_request_pool_perform(pool);
885 while (pool->unfinished) {
886 if (SUCCESS != http_request_pool_select(pool)) {
887 http_error(E_WARNING, HTTP_E_CURL, "Socket error");
888 return FAILURE;
889 }
890 http_request_pool_perform(pool);
891 }
892 zend_llist_apply(&pool->handles, (llist_apply_func_t) http_request_pool_responsehandler TSRMLS_CC);
893 return SUCCESS;
894 }
895 /* }}} */
896
897 /* {{{ void http_request_pool_dtor(http_request_pool *) */
898 PHP_HTTP_API void _http_request_pool_dtor(http_request_pool *pool TSRMLS_DC)
899 {
900 pool->unfinished = 0;
901 zend_llist_clean(&pool->handles);
902 zend_llist_clean(&pool->bodies);
903 curl_multi_cleanup(pool->ch);
904 }
905 /* }}} */
906
907 /*#*/
908
909 /* {{{ static void http_request_pool_freebody(http_request_body **) */
910 static void http_request_pool_freebody(http_request_body **body)
911 {
912 TSRMLS_FETCH();
913 http_request_body_free(*body);
914 }
915 /* }}} */
916
917 /* {{{ static void http_request_pool_responsehandler(zval **) */
918 static void http_request_pool_responsehandler(zval **req TSRMLS_DC)
919 {
920 getObjectEx(http_request_object, obj, *req);
921 http_request_object_responsehandler(obj, *req, NULL);
922 }
923 /* }}} */
924
925 /* {{{ static inline STATUS http_request_pool_select(http_request_pool *) */
926 static inline STATUS http_request_pool_select(http_request_pool *pool)
927 {
928 int MAX;
929 fd_set R, W, E;
930 struct timeval timeout = {1, 0};
931
932 FD_ZERO(&R);
933 FD_ZERO(&W);
934 FD_ZERO(&E);
935
936 curl_multi_fdset(pool->ch, &R, &W, &E, &MAX);
937 return (-1 != select(MAX + 1, &R, &W, &E, &timeout)) ? SUCCESS : FAILURE;
938 }
939 /* }}} */
940
941 /* {{{ static inline void http_request_pool_perform(http_request_pool *) */
942 static inline void http_request_pool_perform(http_request_pool *pool)
943 {
944 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(pool->ch, &pool->unfinished));
945 }
946 /* }}} */
947
948 /* {{{ char *http_request_methods[] */
949 static const char *const http_request_methods[] = {
950 "UNKOWN",
951 /* HTTP/1.1 */
952 "GET",
953 "HEAD",
954 "POST",
955 "PUT",
956 "DELETE",
957 "OPTIONS",
958 "TRACE",
959 "CONNECT",
960 /* WebDAV - RFC 2518 */
961 "PROPFIND",
962 "PROPPATCH",
963 "MKCOL",
964 "COPY",
965 "MOVE",
966 "LOCK",
967 "UNLOCK",
968 /* WebDAV Versioning - RFC 3253 */
969 "VERSION-CONTROL",
970 "REPORT",
971 "CHECKOUT",
972 "CHECKIN",
973 "UNCHECKOUT",
974 "MKWORKSPACE",
975 "UPDATE",
976 "LABEL",
977 "MERGE",
978 "BASELINE-CONTROL",
979 "MKACTIVITY",
980 /* WebDAV Access Control - RFC 3744 */
981 "ACL",
982 NULL
983 };
984 /* }}} */
985
986 /* {{{ static size_t http_curl_write_callback(char *, size_t, size_t, void *) */
987 static size_t http_curl_write_callback(char *buf, size_t len, size_t n, void *s)
988 {
989 HTTP_CURL_CALLBACK_DATA(s, phpstr *, str);
990 return str ? phpstr_append(PHPSTR(str), buf, len * n) : len * n;
991 }
992 /* }}} */
993
994 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
995 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *s)
996 {
997 static char *offset = NULL, *original = NULL;
998 HTTP_CURL_CALLBACK_DATA(s, http_request_body *, body);
999
1000 switch (body->type)
1001 {
1002 case HTTP_REQUEST_BODY_UPLOADFILE:
1003 {
1004 TSRMLS_FETCH();
1005 return php_stream_read((php_stream *) body->data, data, len * n);
1006 }
1007 break;
1008
1009 case HTTP_REQUEST_BODY_UPLOADDATA:
1010 {
1011 size_t avail;
1012 if (original != s) {
1013 original = offset = s;
1014 }
1015 if ((avail = body->size - (offset - original)) < 1) {
1016 return 0;
1017 }
1018 if (avail < (len * n)) {
1019 memcpy(data, offset, avail);
1020 offset += avail;
1021 return avail;
1022 } else {
1023 memcpy(data, offset, len * n);
1024 offset += len * n;
1025 return len * n;
1026 }
1027 }
1028 break;
1029
1030 default:
1031 return 0;
1032 break;
1033 }
1034 }
1035 /* }}} */
1036
1037 /* {{{ http_curl_callback_ctx http_curl_callback_data(void *) */
1038 static http_curl_callback_ctx *_http_curl_callback_data(void *data TSRMLS_DC)
1039 {
1040 http_curl_callback_ctx *ctx = emalloc(sizeof(http_curl_callback_ctx));
1041 TSRMLS_SET_CTX(ctx->tsrm_ctx);
1042 ctx->data = data;
1043 return ctx;
1044 }
1045 /* }}} */
1046
1047 /* {{{ static int http_curl_progress_callback(void *, double, double, double, double) */
1048 static int http_curl_progress_callback(void *data, double dltotal, double dlnow, double ultotal, double ulnow)
1049 {
1050 zval *params_pass[4], params_local[4], retval;
1051 HTTP_CURL_CALLBACK_DATA(data, zval *, func);
1052
1053 params_pass[0] = &params_local[0];
1054 params_pass[1] = &params_local[1];
1055 params_pass[2] = &params_local[2];
1056 params_pass[3] = &params_local[3];
1057
1058 ZVAL_DOUBLE(params_pass[0], dltotal);
1059 ZVAL_DOUBLE(params_pass[1], dlnow);
1060 ZVAL_DOUBLE(params_pass[2], ultotal);
1061 ZVAL_DOUBLE(params_pass[3], ulnow);
1062
1063 return call_user_function(EG(function_table), NULL, func, &retval, 4, params_pass TSRMLS_CC);
1064 }
1065 /* }}} */
1066
1067 static int http_curl_debug_callback(CURL *ch, curl_infotype type, char *string, size_t length, void *data)
1068 {
1069 zval *params_pass[2], params_local[2], retval;
1070 HTTP_CURL_CALLBACK_DATA(data, zval *, func);
1071
1072 params_pass[0] = &params_local[0];
1073 params_pass[1] = &params_local[1];
1074
1075 ZVAL_LONG(params_pass[0], type);
1076 ZVAL_STRINGL(params_pass[1], string, length, 1);
1077
1078 call_user_function(EG(function_table), NULL, func, &retval, 2, params_pass TSRMLS_CC);
1079
1080 return 0;
1081 }
1082 /* {{{ static inline zval *http_curl_getopt(HashTable *, char *, size_t, int) */
1083 static inline zval *_http_curl_getopt_ex(HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
1084 {
1085 zval **zoption;
1086
1087 if (!options || (SUCCESS != zend_hash_find(options, key, keylen, (void **) &zoption))) {
1088 return NULL;
1089 }
1090
1091 if (Z_TYPE_PP(zoption) != type) {
1092 switch (type)
1093 {
1094 case IS_BOOL: convert_to_boolean_ex(zoption); break;
1095 case IS_LONG: convert_to_long_ex(zoption); break;
1096 case IS_DOUBLE: convert_to_double_ex(zoption); break;
1097 case IS_STRING: convert_to_string_ex(zoption); break;
1098 case IS_ARRAY: convert_to_array_ex(zoption); break;
1099 case IS_OBJECT: convert_to_object_ex(zoption); break;
1100 default:
1101 break;
1102 }
1103 }
1104
1105 return *zoption;
1106 }
1107 /* }}} */
1108
1109 /*
1110 * Local variables:
1111 * tab-width: 4
1112 * c-basic-offset: 4
1113 * End:
1114 * vim600: noet sw=4 ts=4 fdm=marker
1115 * vim<600: noet sw=4 ts=4
1116 */
1117