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