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