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