5fea9fea4f864a06baf78124bcc1ca5d8a442860
[m6w6/ext-http] / php_http_curl.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
15 #if PHP_HTTP_HAVE_CURL
16
17 #include <curl/curl.h>
18 #define PHP_HTTP_CURL_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= (((x)<<16) + ((y)<<8) + (z)))
19
20 #if PHP_HTTP_HAVE_EVENT
21 # include <event.h>
22 #endif
23
24 typedef struct php_http_curl_request {
25 CURL *handle;
26
27 struct {
28 HashTable cache;
29
30 struct curl_slist *headers;
31 struct curl_slist *resolve;
32 php_http_buffer_t cookies;
33
34 long redirects;
35
36 struct {
37 uint count;
38 double delay;
39 } retry;
40
41 } options;
42
43 php_http_request_progress_t progress;
44
45 } php_http_curl_request_t;
46
47 typedef struct php_http_curl_request_storage {
48 char *url;
49 char *cookiestore;
50 char errorbuffer[0x100];
51 } php_http_curl_request_storage_t;
52
53 typedef struct php_http_curl_request_pool {
54 CURLM *handle;
55
56 int unfinished; /* int because of curl_multi_perform() */
57
58 #if PHP_HTTP_HAVE_EVENT
59 struct event *timeout;
60 unsigned useevents:1;
61 unsigned runsocket:1;
62 #endif
63 } php_http_curl_request_pool_t;
64
65 typedef struct php_http_curl_request_datashare {
66 CURLSH *handle;
67 } php_http_curl_request_datashare_t;
68
69 #define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
70 { \
71 char *K = #OPTION; \
72 PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
73 }
74 #define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
75 if (!strcasecmp(key.str, keyname)) { \
76 zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \
77 if (obdc) { \
78 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \
79 return FAILURE; \
80 } \
81 } \
82 curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \
83 zval_ptr_dtor(&copy); \
84 continue; \
85 }
86 #define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \
87 { \
88 char *K = #OPTION; \
89 PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
90 }
91 #define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \
92 if (!strcasecmp(key.str, keyname)) { \
93 zval *copy = php_http_ztyp(IS_LONG, *param); \
94 curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \
95 zval_ptr_dtor(&copy); \
96 continue; \
97 }
98
99 static inline php_http_curl_request_storage_t *get_storage(CURL *ch) {
100 php_http_curl_request_storage_t *st = NULL;
101
102 curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st);
103
104 if (!st) {
105 st = pecalloc(1, sizeof(*st), 1);
106 curl_easy_setopt(ch, CURLOPT_PRIVATE, st);
107 curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer);
108 }
109
110 return st;
111 }
112
113 /* resource_factory ops */
114
115 static void *php_http_curl_ctor(void *opaque TSRMLS_DC)
116 {
117 void *ch;
118
119 if ((ch = curl_easy_init())) {
120 get_storage(ch);
121 return ch;
122 }
123 return NULL;
124 }
125
126 static void *php_http_curl_copy(void *opaque, void *handle TSRMLS_DC)
127 {
128 void *ch;
129
130 if ((ch = curl_easy_duphandle(handle))) {
131 curl_easy_reset(ch);
132 get_storage(ch);
133 return ch;
134 }
135 return NULL;
136 }
137
138 static void php_http_curl_dtor(void *opaque, void *handle TSRMLS_DC)
139 {
140 php_http_curl_request_storage_t *st = get_storage(handle);
141
142 curl_easy_cleanup(handle);
143
144 if (st) {
145 if (st->url) {
146 pefree(st->url, 1);
147 }
148 if (st->cookiestore) {
149 pefree(st->cookiestore, 1);
150 }
151 pefree(st, 1);
152 }
153 }
154
155 static void *php_http_curlm_ctor(void *opaque TSRMLS_DC)
156 {
157 return curl_multi_init();
158 }
159
160 static void php_http_curlm_dtor(void *opaque, void *handle TSRMLS_DC)
161 {
162 curl_multi_cleanup(handle);
163 }
164
165 static void *php_http_curlsh_ctor(void *opaque TSRMLS_DC)
166 {
167 return curl_share_init();
168 }
169
170 static void php_http_curlsh_dtor(void *opaque, void *handle TSRMLS_DC)
171 {
172 curl_share_cleanup(handle);
173 }
174
175 /* callbacks */
176
177 static size_t php_http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
178 {
179 php_http_message_body_t *body = ctx;
180
181 if (body) {
182 TSRMLS_FETCH_FROM_CTX(body->ts);
183 return php_stream_read(php_http_message_body_stream(body), data, len * n);
184 }
185 return 0;
186 }
187
188 static int php_http_curl_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
189 {
190 php_http_request_t *h = ctx;
191 php_http_curl_request_t *curl = h->ctx;
192 TSRMLS_FETCH_FROM_CTX(h->ts);
193
194 curl->progress.state.dl.total = dltotal;
195 curl->progress.state.dl.now = dlnow;
196 curl->progress.state.ul.total = ultotal;
197 curl->progress.state.ul.now = ulnow;
198
199 php_http_request_progress_notify(&curl->progress TSRMLS_CC);
200
201 return 0;
202 }
203
204 static curlioerr php_http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
205 {
206 php_http_message_body_t *body = ctx;
207
208 if (cmd != CURLIOCMD_RESTARTREAD) {
209 return CURLIOE_UNKNOWNCMD;
210 }
211
212 if (body) {
213 TSRMLS_FETCH_FROM_CTX(body->ts);
214
215 if (SUCCESS == php_stream_rewind(php_http_message_body_stream(body))) {
216 return CURLIOE_OK;
217 }
218 }
219
220 return CURLIOE_FAILRESTART;
221 }
222
223 static int php_http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
224 {
225 php_http_request_t *h = ctx;
226 php_http_curl_request_t *curl = h->ctx;
227 unsigned flags = 0;
228 TSRMLS_FETCH_FROM_CTX(h->ts);
229
230 /* catch progress */
231 switch (type) {
232 case CURLINFO_TEXT:
233 if (php_memnstr(data, ZEND_STRL("About to connect"), data + length)) {
234 curl->progress.state.info = "resolve";
235 } else if (php_memnstr(data, ZEND_STRL("Trying"), data + length)) {
236 curl->progress.state.info = "connect";
237 } else if (php_memnstr(data, ZEND_STRL("Connected"), data + length)) {
238 curl->progress.state.info = "connected";
239 } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) {
240 curl->progress.state.info = "not disconnected";
241 } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) {
242 curl->progress.state.info = "disconnected";
243 } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) {
244 curl->progress.state.info = "redirect";
245 }
246 php_http_request_progress_notify(&curl->progress TSRMLS_CC);
247 break;
248 case CURLINFO_HEADER_OUT:
249 case CURLINFO_DATA_OUT:
250 case CURLINFO_SSL_DATA_OUT:
251 curl->progress.state.info = "send";
252 break;
253 case CURLINFO_HEADER_IN:
254 case CURLINFO_DATA_IN:
255 case CURLINFO_SSL_DATA_IN:
256 curl->progress.state.info = "receive";
257 break;
258 default:
259 break;
260 }
261 /* process data */
262 switch (type) {
263 case CURLINFO_HEADER_IN:
264 case CURLINFO_DATA_IN:
265 case CURLINFO_HEADER_OUT:
266 case CURLINFO_DATA_OUT:
267 php_http_buffer_append(h->buffer, data, length);
268
269 if (curl->options.redirects) {
270 flags |= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS;
271 }
272
273 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(h->parser, h->buffer, flags, &h->message)) {
274 return -1;
275 }
276 break;
277 default:
278 break;
279 }
280
281 #if 0
282 /* debug */
283 _dpf(type, data, length);
284 #endif
285
286 return 0;
287 }
288
289 static int php_http_curl_dummy_callback(char *data, size_t n, size_t l, void *s)
290 {
291 return n*l;
292 }
293
294 static STATUS php_http_curl_request_prepare(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body)
295 {
296 php_http_curl_request_t *curl = h->ctx;
297 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
298 TSRMLS_FETCH_FROM_CTX(h->ts);
299
300 storage->errorbuffer[0] = '\0';
301 if (storage->url) {
302 pefree(storage->url, 1);
303 }
304 storage->url = pestrdup(url, 1);
305 curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url);
306
307 /* request method */
308 switch (php_http_select_str(meth, 4, "GET", "HEAD", "POST", "PUT")) {
309 case 0:
310 curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L);
311 break;
312
313 case 1:
314 curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L);
315 break;
316
317 case 2:
318 curl_easy_setopt(curl->handle, CURLOPT_POST, 1L);
319 break;
320
321 case 3:
322 curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L);
323 break;
324
325 default: {
326 if (meth) {
327 curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, meth);
328 } else {
329 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_METHOD, "Unsupported request method: '%s' (%s)", meth, url);
330 return FAILURE;
331 }
332 break;
333 }
334 }
335
336 /* attach request body */
337 if (body) {
338 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
339 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
340 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
341 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
342 * does not allow a request body.
343 */
344 size_t body_size = php_http_message_body_size(body);
345
346 curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, body);
347 curl_easy_setopt(curl->handle, CURLOPT_READDATA, body);
348 curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size);
349 curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size);
350 }
351
352 return SUCCESS;
353 }
354
355 static void php_http_curl_request_pool_responsehandler(php_http_request_pool_t *pool)
356 {
357 int remaining = 0;
358 zval **requests;
359 php_http_curl_request_pool_t *curl = pool->ctx;
360 TSRMLS_FETCH_FROM_CTX(pool->ts);
361
362 do {
363 CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining);
364
365 if (msg && CURLMSG_DONE == msg->msg) {
366 zval **request;
367
368 if (CURLE_OK != msg->data.result) {
369 php_http_curl_request_storage_t *st = get_storage(msg->easy_handle);
370 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url));
371 }
372
373 php_http_request_pool_requests(pool, &requests, NULL);
374 for (request = requests; *request; ++request) {
375 php_http_request_object_t *obj = zend_object_store_get_object(*request TSRMLS_CC);
376
377 if (msg->easy_handle == ((php_http_curl_request_t *) (obj->request->ctx))->handle) {
378 Z_ADDREF_PP(request);
379 zend_llist_add_element(&pool->requests.finished, request);
380 php_http_request_object_responsehandler(obj, *request TSRMLS_CC);
381 }
382
383 zval_ptr_dtor(request);
384 }
385 efree(requests);
386 }
387 } while (remaining);
388 }
389
390
391 #if PHP_HTTP_HAVE_EVENT
392
393 typedef struct php_http_request_pool_event {
394 struct event evnt;
395 php_http_request_pool_t *pool;
396 } php_http_request_pool_event_t;
397
398 static inline int etoca(short action) {
399 switch (action & (EV_READ|EV_WRITE)) {
400 case EV_READ:
401 return CURL_CSELECT_IN;
402 break;
403 case EV_WRITE:
404 return CURL_CSELECT_OUT;
405 break;
406 case EV_READ|EV_WRITE:
407 return CURL_CSELECT_IN|CURL_CSELECT_OUT;
408 break;
409 default:
410 return 0;
411 }
412 }
413
414 static void php_http_curl_request_pool_timeout_callback(int socket, short action, void *event_data)
415 {
416 php_http_request_pool_t *pool = event_data;
417 php_http_curl_request_pool_t *curl = pool->ctx;
418
419 #if DBG_EVENTS
420 fprintf(stderr, "T");
421 #endif
422 if (curl->useevents) {
423 CURLMcode rc;
424 TSRMLS_FETCH_FROM_CTX(pool->ts);
425
426 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
427
428 if (CURLM_OK != rc) {
429 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc));
430 }
431
432 php_http_curl_request_pool_responsehandler(pool);
433 }
434 }
435
436 static void php_http_curl_request_pool_event_callback(int socket, short action, void *event_data)
437 {
438 php_http_request_pool_t *pool = event_data;
439 php_http_curl_request_pool_t *curl = pool->ctx;
440
441 #if DBG_EVENTS
442 fprintf(stderr, "E");
443 #endif
444 if (curl->useevents) {
445 CURLMcode rc = CURLE_OK;
446 TSRMLS_FETCH_FROM_CTX(pool->ts);
447
448 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
449
450 if (CURLM_OK != rc) {
451 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "%s", curl_multi_strerror(rc));
452 }
453
454 php_http_curl_request_pool_responsehandler(pool);
455
456 /* remove timeout if there are no transfers left */
457 if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
458 event_del(curl->timeout);
459 }
460 }
461 }
462
463 static int php_http_curl_request_pool_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
464 {
465 php_http_request_pool_t *pool = socket_data;
466 php_http_curl_request_pool_t *curl = pool->ctx;
467
468 #if DBG_EVENTS
469 fprintf(stderr, "S");
470 #endif
471 if (curl->useevents) {
472 int events = EV_PERSIST;
473 php_http_request_pool_event_t *ev = assign_data;
474 TSRMLS_FETCH_FROM_CTX(pool->ts);
475
476 if (!ev) {
477 ev = ecalloc(1, sizeof(php_http_request_pool_event_t));
478 ev->pool = pool;
479 curl_multi_assign(curl->handle, sock, ev);
480 event_base_set(PHP_HTTP_G->curl.event_base, &ev->evnt);
481 } else {
482 event_del(&ev->evnt);
483 }
484
485 switch (action) {
486 case CURL_POLL_IN:
487 events |= EV_READ;
488 break;
489 case CURL_POLL_OUT:
490 events |= EV_WRITE;
491 break;
492 case CURL_POLL_INOUT:
493 events |= EV_READ|EV_WRITE;
494 break;
495
496 case CURL_POLL_REMOVE:
497 efree(ev);
498 /* no break */
499 case CURL_POLL_NONE:
500 return 0;
501
502 default:
503 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "Unknown socket action %d", action);
504 return -1;
505 }
506
507 event_set(&ev->evnt, sock, events, php_http_curl_request_pool_event_callback, pool);
508 event_add(&ev->evnt, NULL);
509 }
510
511 return 0;
512 }
513
514 static void php_http_curl_request_pool_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
515 {
516 php_http_request_pool_t *pool = timer_data;
517 php_http_curl_request_pool_t *curl = pool->ctx;
518
519 #if DBG_EVENTS
520 fprintf(stderr, "%ld", timeout_ms);
521 #endif
522 if (curl->useevents) {
523
524 if (timeout_ms < 0) {
525 php_http_curl_request_pool_timeout_callback(CURL_SOCKET_TIMEOUT, CURL_CSELECT_IN|CURL_CSELECT_OUT, pool);
526 } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
527 struct timeval timeout;
528 TSRMLS_FETCH_FROM_CTX(pool->ts);
529
530 if (!event_initialized(curl->timeout)) {
531 event_set(curl->timeout, -1, 0, php_http_curl_request_pool_timeout_callback, pool);
532 event_base_set(PHP_HTTP_G->curl.event_base, curl->timeout);
533 } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
534 event_del(curl->timeout);
535 }
536
537 timeout.tv_sec = timeout_ms / 1000;
538 timeout.tv_usec = (timeout_ms % 1000) * 1000;
539
540 event_add(curl->timeout, &timeout);
541 }
542 }
543 }
544
545 #endif /* HAVE_EVENT */
546
547
548 static inline zval *cache_option(HashTable *cache, char *key, size_t keylen, ulong h, zval *opt)
549 {
550 Z_ADDREF_P(opt);
551
552 if (h) {
553 zend_hash_quick_update(cache, key, keylen, h, &opt, sizeof(zval *), NULL);
554 } else {
555 zend_hash_update(cache, key, keylen, &opt, sizeof(zval *), NULL);
556 }
557
558 return opt;
559 }
560
561 static inline zval *get_option(HashTable *cache, HashTable *options, char *key, size_t keylen, int type)
562 {
563 if (options) {
564 zval **zoption;
565 ulong h = zend_hash_func(key, keylen);
566
567 if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) {
568 zval *option = php_http_ztyp(type, *zoption);
569
570 if (cache) {
571 zval *cached = cache_option(cache, key, keylen, h, option);
572
573 zval_ptr_dtor(&option);
574 return cached;
575 }
576 return option;
577 }
578 }
579
580 return NULL;
581 }
582
583 static STATUS set_options(php_http_request_t *h, HashTable *options)
584 {
585 zval *zoption;
586 int range_req = 0;
587 php_http_curl_request_t *curl = h->ctx;
588 CURL *ch = curl->handle;
589 TSRMLS_FETCH_FROM_CTX(h->ts);
590
591 /* proxy */
592 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyhost"), IS_STRING))) {
593 curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption));
594 /* type */
595 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytype"), IS_LONG))) {
596 curl_easy_setopt(ch, CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
597 }
598 /* port */
599 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyport"), IS_LONG))) {
600 curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
601 }
602 /* user:pass */
603 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
604 curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
605 }
606 /* auth method */
607 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauthtype"), IS_LONG))) {
608 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
609 }
610 /* tunnel */
611 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytunnel"), IS_BOOL)) && Z_BVAL_P(zoption)) {
612 curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 1L);
613 }
614 }
615 #if PHP_HTTP_CURL_VERSION(7,19,4)
616 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("noproxy"), IS_STRING))) {
617 curl_easy_setopt(ch, CURLOPT_NOPROXY, Z_STRVAL_P(zoption));
618 }
619 #endif
620
621 /* dns */
622 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_cache_timeout"), IS_LONG))) {
623 curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption));
624 }
625 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) {
626 curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption));
627 }
628 #if PHP_HTTP_CURL_VERSION(7,21,3)
629 if (curl->options.resolve) {
630 curl_slist_free_all(curl->options.resolve);
631 curl->options.resolve = NULL;
632 }
633 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resolve"), IS_ARRAY))) {
634 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
635 HashPosition pos;
636 zval **data;
637
638 FOREACH_KEYVAL(pos, zoption, key, data) {
639 zval *cpy = php_http_ztyp(IS_STRING, *data);
640
641 curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy));
642
643 zval_ptr_dtor(&cpy);
644 }
645 }
646 #endif
647 #if PHP_HTTP_CURL_VERSION(7,24,0)
648 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_servers"), IS_STRING)) && Z_STRLEN_P(zoption)) {
649 curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, Z_STRVAL_P(zoption));
650 }
651 #endif
652
653 /* limits */
654 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) {
655 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption));
656 }
657 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_time"), IS_LONG))) {
658 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption));
659 }
660 /* LSF weirdance
661 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) {
662 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
663 }
664 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) {
665 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
666 }
667 */
668 /* crashes
669 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) {
670 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
671 } */
672 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("fresh_connect"), IS_BOOL)) && Z_BVAL_P(zoption)) {
673 curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
674 }
675 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("forbid_reuse"), IS_BOOL)) && Z_BVAL_P(zoption)) {
676 curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 1L);
677 }
678
679 /* outgoing interface */
680 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("interface"), IS_STRING))) {
681 curl_easy_setopt(ch, CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
682
683 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("portrange"), IS_ARRAY))) {
684 zval **prs, **pre;
685
686 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
687 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) {
688 zend_hash_move_forward(Z_ARRVAL_P(zoption));
689 if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
690 zval *prs_cpy = php_http_ztyp(IS_LONG, *prs);
691 zval *pre_cpy = php_http_ztyp(IS_LONG, *pre);
692
693 if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
694 curl_easy_setopt(ch, CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
695 curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
696 }
697 zval_ptr_dtor(&prs_cpy);
698 zval_ptr_dtor(&pre_cpy);
699 }
700 }
701 }
702 }
703
704 /* another port */
705 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("port"), IS_LONG))) {
706 curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption));
707 }
708
709 /* RFC4007 zone_id */
710 #if PHP_HTTP_CURL_VERSION(7,19,0)
711 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("address_scope"), IS_LONG))) {
712 curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption));
713 }
714 #endif
715
716 /* auth */
717 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauth"), IS_STRING)) && Z_STRLEN_P(zoption)) {
718 curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption));
719 }
720 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauthtype"), IS_LONG))) {
721 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
722 }
723
724 /* redirects, defaults to 0 */
725 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("redirect"), IS_LONG))) {
726 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L);
727 curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(zoption));
728 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("unrestrictedauth"), IS_BOOL))) {
729 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
730 }
731 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("postredir"), IS_BOOL))) {
732 #if PHP_HTTP_CURL_VERSION(7,19,1)
733 curl_easy_setopt(ch, CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L);
734 #else
735 curl_easy_setopt(ch, CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L);
736 #endif
737 }
738 } else {
739 curl->options.redirects = 0;
740 }
741
742 /* retries, defaults to 0 */
743 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrycount"), IS_LONG))) {
744 curl->options.retry.count = Z_LVAL_P(zoption);
745 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrydelay"), IS_DOUBLE))) {
746 curl->options.retry.delay = Z_DVAL_P(zoption);
747 } else {
748 curl->options.retry.delay = 0;
749 }
750 } else {
751 curl->options.retry.count = 0;
752 }
753
754 /* referer */
755 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) {
756 curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption));
757 }
758
759 /* useragent, default "PECL::HTTP/version (PHP/version)" */
760 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("useragent"), IS_STRING))) {
761 /* allow to send no user agent, not even default one */
762 if (Z_STRLEN_P(zoption)) {
763 curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
764 } else {
765 curl_easy_setopt(ch, CURLOPT_USERAGENT, NULL);
766 }
767 }
768
769 /* resume */
770 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
771 range_req = 1;
772 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
773 }
774 /* or range of kind array(array(0,499), array(100,1499)) */
775 else if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("range"), IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
776 HashPosition pos1, pos2;
777 zval **rr, **rb, **re;
778 php_http_buffer_t rs;
779
780 php_http_buffer_init(&rs);
781 FOREACH_VAL(pos1, zoption, rr) {
782 if (Z_TYPE_PP(rr) == IS_ARRAY) {
783 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
784 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) {
785 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
786 if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) {
787 if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) &&
788 ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) {
789 zval *rbl = php_http_ztyp(IS_LONG, *rb);
790 zval *rel = php_http_ztyp(IS_LONG, *re);
791
792 if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
793 php_http_buffer_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
794 }
795 zval_ptr_dtor(&rbl);
796 zval_ptr_dtor(&rel);
797 }
798 }
799 }
800 }
801 }
802
803 if (PHP_HTTP_BUFFER_LEN(&rs)) {
804 zval *cached_range;
805
806 /* ditch last comma */
807 PHP_HTTP_BUFFER_VAL(&rs)[PHP_HTTP_BUFFER_LEN(&rs)-- -1] = '\0';
808 /* cache string */
809 MAKE_STD_ZVAL(cached_range);
810 ZVAL_STRINGL(cached_range, PHP_HTTP_BUFFER_VAL(&rs), PHP_HTTP_BUFFER_LEN(&rs), 0);
811 curl_easy_setopt(ch, CURLOPT_RANGE, Z_STRVAL_P(cache_option(&curl->options.cache, ZEND_STRS("range"), 0, cached_range)));
812 zval_ptr_dtor(&cached_range);
813 }
814 }
815
816 /* additional headers, array('name' => 'value') */
817 if (curl->options.headers) {
818 curl_slist_free_all(curl->options.headers);
819 curl->options.headers = NULL;
820 }
821 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("headers"), IS_ARRAY))) {
822 php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
823 zval **header_val;
824 HashPosition pos;
825 php_http_buffer_t header;
826
827 php_http_buffer_init(&header);
828 FOREACH_KEYVAL(pos, zoption, header_key, header_val) {
829 if (header_key.type == HASH_KEY_IS_STRING) {
830 zval *header_cpy = php_http_ztyp(IS_STRING, *header_val);
831
832 if (!strcasecmp(header_key.str, "range")) {
833 range_req = 1;
834 }
835
836 php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
837 php_http_buffer_fix(&header);
838 curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header));
839 php_http_buffer_reset(&header);
840
841 zval_ptr_dtor(&header_cpy);
842 }
843 }
844 php_http_buffer_dtor(&header);
845 }
846 /* etag */
847 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("etag"), IS_STRING)) && Z_STRLEN_P(zoption)) {
848 zend_bool is_quoted = !((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"'));
849 php_http_buffer_t header;
850
851 php_http_buffer_init(&header);
852 php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", range_req?"If-Match":"If-None-Match", Z_STRVAL_P(zoption));
853 php_http_buffer_fix(&header);
854 curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header));
855 php_http_buffer_dtor(&header);
856 }
857 /* compression */
858 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) {
859 curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
860 }
861 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, curl->options.headers);
862
863 /* lastmodified */
864 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("lastmodified"), IS_LONG))) {
865 if (Z_LVAL_P(zoption)) {
866 if (Z_LVAL_P(zoption) > 0) {
867 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
868 } else {
869 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption));
870 }
871 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
872 } else {
873 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
874 }
875 }
876
877 /* cookies, array('name' => 'value') */
878 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookies"), IS_ARRAY))) {
879 php_http_buffer_dtor(&curl->options.cookies);
880 if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
881 zval *urlenc_cookies = NULL;
882 /* check whether cookies should not be urlencoded; default is to urlencode them */
883 if ((!(urlenc_cookies = get_option(&curl->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
884 if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(zoption), &curl->options.cookies, ZEND_STRS(";"), ZEND_STRS("="), NULL, 0 TSRMLS_CC)) {
885 php_http_buffer_fix(&curl->options.cookies);
886 curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data);
887 }
888 } else {
889 HashPosition pos;
890 php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0);
891 zval **cookie_val;
892
893 FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) {
894 if (cookie_key.type == HASH_KEY_IS_STRING) {
895 zval *val = php_http_ztyp(IS_STRING, *cookie_val);
896 php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
897 zval_ptr_dtor(&val);
898 }
899 }
900
901 php_http_buffer_fix(&curl->options.cookies);
902 if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) {
903 curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies));
904 }
905 }
906 }
907 }
908
909 /* don't load session cookies from cookiestore */
910 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) {
911 curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 1L);
912 }
913
914 /* cookiestore, read initial cookies from that file and store cookies back into that file */
915 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiestore"), IS_STRING))) {
916 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
917
918 if (Z_STRLEN_P(zoption)) {
919 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(zoption) TSRMLS_CC)) {
920 return FAILURE;
921 }
922 }
923 if (storage->cookiestore) {
924 pefree(storage->cookiestore, 1);
925 }
926 storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1);
927 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore);
928 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore);
929 }
930
931 /* maxfilesize */
932 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("maxfilesize"), IS_LONG))) {
933 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
934 }
935
936 /* http protocol */
937 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("protocol"), IS_LONG))) {
938 curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
939 }
940
941 /* timeout, defaults to 0 */
942 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("timeout"), IS_DOUBLE))) {
943 curl_easy_setopt(ch, CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
944 }
945 /* connecttimeout, defaults to 0 */
946 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) {
947 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
948 }
949
950 /* ssl */
951 if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) {
952 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
953 zval **param;
954 HashPosition pos;
955
956 FOREACH_KEYVAL(pos, zoption, key, param) {
957 if (key.type == HASH_KEY_IS_STRING) {
958 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
959 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
960 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
961
962 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
963 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
964 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
965
966 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
967 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
968
969 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
970 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
971 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
972
973 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
974 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
975 PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
976 PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
977 #if PHP_HTTP_CURL_VERSION(7,19,0)
978 PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1);
979 #if defined(PHP_HTTP_HAVE_OPENSSL)
980 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1);
981 #endif
982 #endif
983 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
984 PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3);
985 #endif
986 }
987 }
988 }
989 return SUCCESS;
990 }
991
992 static STATUS get_info(CURL *ch, HashTable *info)
993 {
994 char *c;
995 long l;
996 double d;
997 struct curl_slist *s, *p;
998 zval *subarray, array;
999 INIT_PZVAL_ARRAY(&array, info);
1000
1001 /* BEGIN::CURLINFO */
1002 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) {
1003 add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1);
1004 }
1005 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) {
1006 add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l);
1007 }
1008 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) {
1009 add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d);
1010 }
1011 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) {
1012 add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d);
1013 }
1014 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) {
1015 add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d);
1016 }
1017 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) {
1018 add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d);
1019 }
1020 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) {
1021 add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d);
1022 }
1023 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) {
1024 add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d);
1025 }
1026 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) {
1027 add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d);
1028 }
1029 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) {
1030 add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d);
1031 }
1032 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) {
1033 add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l);
1034 }
1035 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) {
1036 add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l);
1037 }
1038 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) {
1039 add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l);
1040 }
1041 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) {
1042 add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l);
1043 }
1044 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
1045 add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d);
1046 }
1047 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) {
1048 add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d);
1049 }
1050 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) {
1051 add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d);
1052 }
1053 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) {
1054 add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1);
1055 }
1056 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) {
1057 add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d);
1058 }
1059 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) {
1060 add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l);
1061 }
1062 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) {
1063 add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l);
1064 }
1065 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) {
1066 add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l);
1067 }
1068 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) {
1069 add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l);
1070 }
1071 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) {
1072 add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l);
1073 }
1074 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) {
1075 add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l);
1076 }
1077 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) {
1078 MAKE_STD_ZVAL(subarray);
1079 array_init(subarray);
1080 for (p = s; p; p = p->next) {
1081 if (p->data) {
1082 add_next_index_string(subarray, p->data, 1);
1083 }
1084 }
1085 add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray);
1086 curl_slist_free_all(s);
1087 }
1088 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) {
1089 MAKE_STD_ZVAL(subarray);
1090 array_init(subarray);
1091 for (p = s; p; p = p->next) {
1092 if (p->data) {
1093 add_next_index_string(subarray, p->data, 1);
1094 }
1095 }
1096 add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray);
1097 curl_slist_free_all(s);
1098 }
1099 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) {
1100 add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1);
1101 }
1102 #if PHP_HTTP_CURL_VERSION(7,19,0)
1103 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) {
1104 add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1);
1105 }
1106 #endif
1107 #if PHP_HTTP_CURL_VERSION(7,19,0)
1108 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) {
1109 add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d);
1110 }
1111 #endif
1112 #if PHP_HTTP_CURL_VERSION(7,19,4)
1113 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) {
1114 add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l);
1115 }
1116 #endif
1117 #if PHP_HTTP_CURL_VERSION(7,21,0)
1118 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) {
1119 add_assoc_long_ex(&array, "primary_port", sizeof("primary_port"), l);
1120 }
1121 #endif
1122 #if PHP_HTTP_CURL_VERSION(7,21,0)
1123 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) {
1124 add_assoc_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1);
1125 }
1126 #endif
1127 #if PHP_HTTP_CURL_VERSION(7,21,0)
1128 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) {
1129 add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l);
1130 }
1131 #endif
1132
1133 /* END::CURLINFO */
1134
1135 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1136 {
1137 int i;
1138 zval *ci_array;
1139 struct curl_certinfo *ci;
1140 char *colon, *keyname;
1141
1142 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) {
1143 MAKE_STD_ZVAL(ci_array);
1144 array_init(ci_array);
1145
1146 for (i = 0; i < ci->num_of_certs; ++i) {
1147 s = ci->certinfo[i];
1148
1149 MAKE_STD_ZVAL(subarray);
1150 array_init(subarray);
1151 for (p = s; p; p = p->next) {
1152 if (p->data) {
1153 if ((colon = strchr(p->data, ':'))) {
1154 keyname = estrndup(p->data, colon - p->data);
1155 add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1);
1156 efree(keyname);
1157 } else {
1158 add_next_index_string(subarray, p->data, 1);
1159 }
1160 }
1161 }
1162 add_next_index_zval(ci_array, subarray);
1163 }
1164 add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array);
1165 }
1166 }
1167 #endif
1168 add_assoc_string_ex(&array, "error", sizeof("error"), get_storage(ch)->errorbuffer, 1);
1169
1170 return SUCCESS;
1171 }
1172
1173
1174 /* request datashare handler ops */
1175
1176 static php_http_request_datashare_t *php_http_curl_request_datashare_init(php_http_request_datashare_t *h, void *handle)
1177 {
1178 php_http_curl_request_datashare_t *curl;
1179 TSRMLS_FETCH_FROM_CTX(h->ts);
1180
1181 if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) {
1182 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "could not initialize curl share handle");
1183 return NULL;
1184 }
1185
1186 curl = ecalloc(1, sizeof(*curl));
1187 curl->handle = handle;
1188 h->ctx = curl;
1189
1190 return h;
1191 }
1192
1193 static void php_http_curl_request_datashare_dtor(php_http_request_datashare_t *h)
1194 {
1195 php_http_curl_request_datashare_t *curl = h->ctx;
1196 TSRMLS_FETCH_FROM_CTX(h->ts);
1197
1198 php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
1199
1200 efree(curl);
1201 h->ctx = NULL;
1202 }
1203
1204 static STATUS php_http_curl_request_datashare_attach(php_http_request_datashare_t *h, php_http_request_t *r)
1205 {
1206 CURLcode rc;
1207 php_http_curl_request_datashare_t *curl = h->ctx;
1208 php_http_curl_request_t *recurl = r->ctx;
1209 TSRMLS_FETCH_FROM_CTX(h->ts);
1210
1211 if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, curl->handle))) {
1212 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not attach request to the datashare: %s", curl_easy_strerror(rc));
1213 return FAILURE;
1214 }
1215 return SUCCESS;
1216 }
1217
1218 static STATUS php_http_curl_request_datashare_detach(php_http_request_datashare_t *h, php_http_request_t *r)
1219 {
1220 CURLcode rc;
1221 php_http_curl_request_t *recurl = r->ctx;
1222 TSRMLS_FETCH_FROM_CTX(h->ts);
1223
1224
1225 if (CURLE_OK != (rc = curl_easy_setopt(recurl->handle, CURLOPT_SHARE, NULL))) {
1226 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not detach request from the datashare: %s", curl_share_strerror(rc));
1227 return FAILURE;
1228 }
1229 return SUCCESS;
1230 }
1231
1232 static STATUS php_http_curl_request_datashare_setopt(php_http_request_datashare_t *h, php_http_request_datashare_setopt_opt_t opt, void *arg)
1233 {
1234 CURLSHcode rc;
1235 php_http_curl_request_datashare_t *curl = h->ctx;
1236
1237 switch (opt) {
1238 case PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES:
1239 if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE))) {
1240 TSRMLS_FETCH_FROM_CTX(h->ts);
1241
1242 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of cookie data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
1243 return FAILURE;
1244 }
1245 break;
1246
1247 case PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER:
1248 if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_DNS))) {
1249 TSRMLS_FETCH_FROM_CTX(h->ts);
1250
1251 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of resolver data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
1252 return FAILURE;
1253 }
1254 break;
1255
1256 #if PHP_HTTP_CURL_VERSION(7,23,0)
1257 case PHP_HTTP_REQUEST_DATASHARE_OPT_SSLSESSIONS:
1258 if (CURLSHE_OK != (rc = curl_share_setopt(curl->handle, *((zend_bool *) arg) ? CURLSHOPT_SHARE : CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION))) {
1259 TSRMLS_FETCH_FROM_CTX(h->ts);
1260
1261 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_DATASHARE, "Could not %s sharing of SSL session data: %s", *((zend_bool *) arg) ? "enable" : "disable", curl_share_strerror(rc));
1262 return FAILURE;
1263 }
1264 break;
1265 #endif
1266
1267 default:
1268 return FAILURE;
1269 }
1270
1271 return SUCCESS;
1272 }
1273
1274 static php_http_resource_factory_ops_t php_http_curlsh_resource_factory_ops = {
1275 php_http_curlsh_ctor,
1276 NULL,
1277 php_http_curlsh_dtor
1278 };
1279
1280 static php_http_request_datashare_ops_t php_http_curl_request_datashare_ops = {
1281 &php_http_curlsh_resource_factory_ops,
1282 php_http_curl_request_datashare_init,
1283 NULL /* copy */,
1284 php_http_curl_request_datashare_dtor,
1285 NULL /*reset */,
1286 php_http_curl_request_datashare_attach,
1287 php_http_curl_request_datashare_detach,
1288 php_http_curl_request_datashare_setopt,
1289 };
1290
1291 PHP_HTTP_API php_http_request_datashare_ops_t *php_http_curl_get_request_datashare_ops(void)
1292 {
1293 return &php_http_curl_request_datashare_ops;
1294 }
1295
1296
1297 /* request pool handler ops */
1298
1299 static php_http_request_pool_t *php_http_curl_request_pool_init(php_http_request_pool_t *h, void *handle)
1300 {
1301 php_http_curl_request_pool_t *curl;
1302 TSRMLS_FETCH_FROM_CTX(h->ts);
1303
1304 if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) {
1305 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "could not initialize curl pool handle");
1306 return NULL;
1307 }
1308
1309 curl = ecalloc(1, sizeof(*curl));
1310 curl->handle = handle;
1311 curl->unfinished = 0;
1312 h->ctx = curl;
1313
1314 return h;
1315 }
1316
1317 static void php_http_curl_request_pool_dtor(php_http_request_pool_t *h)
1318 {
1319 php_http_curl_request_pool_t *curl = h->ctx;
1320 TSRMLS_FETCH_FROM_CTX(h->ts);
1321
1322 #if PHP_HTTP_HAVE_EVENT
1323 if (curl->timeout) {
1324 efree(curl->timeout);
1325 curl->timeout = NULL;
1326 }
1327 #endif
1328 curl->unfinished = 0;
1329 php_http_request_pool_reset(h);
1330
1331 php_http_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
1332
1333 efree(curl);
1334 h->ctx = NULL;
1335 }
1336
1337 static STATUS php_http_curl_request_pool_attach(php_http_request_pool_t *h, php_http_request_t *r, const char *m, const char *url, php_http_message_body_t *body)
1338 {
1339 php_http_curl_request_pool_t *curl = h->ctx;
1340 php_http_curl_request_t *recurl = r->ctx;
1341 CURLMcode rs;
1342 TSRMLS_FETCH_FROM_CTX(h->ts);
1343
1344 if (SUCCESS != php_http_curl_request_prepare(r, m, url, body)) {
1345 return FAILURE;
1346 }
1347
1348 if (CURLM_OK == (rs = curl_multi_add_handle(curl->handle, recurl->handle))) {
1349 ++curl->unfinished;
1350 return SUCCESS;
1351 } else {
1352 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not attach request to pool: %s", curl_multi_strerror(rs));
1353 return FAILURE;
1354 }
1355 }
1356
1357 static STATUS php_http_curl_request_pool_detach(php_http_request_pool_t *h, php_http_request_t *r)
1358 {
1359 php_http_curl_request_pool_t *curl = h->ctx;
1360 php_http_curl_request_t *recurl = r->ctx;
1361 CURLMcode rs = curl_multi_remove_handle(curl->handle, recurl->handle);
1362 TSRMLS_FETCH_FROM_CTX(h->ts);
1363
1364 if (CURLM_OK == rs) {
1365 return SUCCESS;
1366 } else {
1367 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_POOL, "Could not detach request from pool: %s", curl_multi_strerror(rs));
1368 return FAILURE;
1369 }
1370 }
1371
1372 #ifdef PHP_WIN32
1373 # define SELECT_ERROR SOCKET_ERROR
1374 #else
1375 # define SELECT_ERROR -1
1376 #endif
1377
1378 static STATUS php_http_curl_request_pool_wait(php_http_request_pool_t *h, struct timeval *custom_timeout)
1379 {
1380 int MAX;
1381 fd_set R, W, E;
1382 struct timeval timeout;
1383 php_http_curl_request_pool_t *curl = h->ctx;
1384
1385 #if PHP_HTTP_HAVE_EVENT
1386 if (curl->useevents) {
1387 TSRMLS_FETCH_FROM_CTX(h->ts);
1388
1389 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented");
1390 return FAILURE;
1391 }
1392 #endif
1393
1394 if (custom_timeout && timerisset(custom_timeout)) {
1395 timeout = *custom_timeout;
1396 } else {
1397 long max_tout = 1000;
1398
1399 if ((CURLM_OK == curl_multi_timeout(curl->handle, &max_tout)) && (max_tout > 0)) {
1400 timeout.tv_sec = max_tout / 1000;
1401 timeout.tv_usec = (max_tout % 1000) * 1000;
1402 } else {
1403 timeout.tv_sec = 0;
1404 timeout.tv_usec = 1000;
1405 }
1406 }
1407
1408 FD_ZERO(&R);
1409 FD_ZERO(&W);
1410 FD_ZERO(&E);
1411
1412 if (CURLM_OK == curl_multi_fdset(curl->handle, &R, &W, &E, &MAX)) {
1413 if (MAX == -1) {
1414 php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC));
1415 return SUCCESS;
1416 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
1417 return SUCCESS;
1418 }
1419 }
1420 return FAILURE;
1421 }
1422
1423 static int php_http_curl_request_pool_once(php_http_request_pool_t *h)
1424 {
1425 php_http_curl_request_pool_t *curl = h->ctx;
1426
1427 #if PHP_HTTP_HAVE_EVENT
1428 if (curl->useevents) {
1429 TSRMLS_FETCH_FROM_CTX(h->ts);
1430 php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "not implemented");
1431 return FAILURE;
1432 }
1433 #endif
1434
1435 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle, &curl->unfinished));
1436
1437 php_http_curl_request_pool_responsehandler(h);
1438
1439 return curl->unfinished;
1440
1441 }
1442 #if PHP_HTTP_HAVE_EVENT
1443 static void dolog(int i, const char *m) {
1444 fprintf(stderr, "%d: %s\n", i, m);
1445 }
1446 #endif
1447 static STATUS php_http_curl_request_pool_exec(php_http_request_pool_t *h)
1448 {
1449 TSRMLS_FETCH_FROM_CTX(h->ts);
1450
1451 #if PHP_HTTP_HAVE_EVENT
1452 php_http_curl_request_pool_t *curl = h->ctx;
1453
1454 if (curl->useevents) {
1455 event_set_log_callback(dolog);
1456 do {
1457 #if DBG_EVENTS
1458 fprintf(stderr, "X");
1459 #endif
1460 event_base_dispatch(PHP_HTTP_G->curl.event_base);
1461 } while (curl->unfinished);
1462 } else
1463 #endif
1464 {
1465 while (php_http_curl_request_pool_once(h)) {
1466 if (SUCCESS != php_http_curl_request_pool_wait(h, NULL)) {
1467 #ifdef PHP_WIN32
1468 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
1469 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, "WinSock error: %d", WSAGetLastError());
1470 #else
1471 php_http_error(HE_WARNING, PHP_HTTP_E_SOCKET, strerror(errno));
1472 #endif
1473 return FAILURE;
1474 }
1475 }
1476 }
1477
1478 return SUCCESS;
1479 }
1480
1481 static STATUS php_http_curl_request_pool_setopt(php_http_request_pool_t *h, php_http_request_pool_setopt_opt_t opt, void *arg)
1482 {
1483 php_http_curl_request_pool_t *curl = h->ctx;
1484
1485 switch (opt) {
1486 case PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING:
1487 if (CURLM_OK != curl_multi_setopt(curl->handle, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) {
1488 return FAILURE;
1489 }
1490 break;
1491
1492 case PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS:
1493 #if PHP_HTTP_HAVE_EVENT
1494 if ((curl->useevents = *((zend_bool *) arg))) {
1495 if (!curl->timeout) {
1496 curl->timeout = ecalloc(1, sizeof(struct event));
1497 }
1498 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, h);
1499 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, php_http_curl_request_pool_socket_callback);
1500 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, h);
1501 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, php_http_curl_request_pool_timer_callback);
1502 } else {
1503 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, NULL);
1504 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, NULL);
1505 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, NULL);
1506 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, NULL);
1507 }
1508 break;
1509 #endif
1510
1511 default:
1512 return FAILURE;
1513 }
1514 return SUCCESS;
1515 }
1516
1517 static php_http_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
1518 php_http_curlm_ctor,
1519 NULL,
1520 php_http_curlm_dtor
1521 };
1522
1523 static php_http_request_pool_ops_t php_http_curl_request_pool_ops = {
1524 &php_http_curlm_resource_factory_ops,
1525 php_http_curl_request_pool_init,
1526 NULL /* copy */,
1527 php_http_curl_request_pool_dtor,
1528 NULL /*reset */,
1529 php_http_curl_request_pool_exec,
1530 php_http_curl_request_pool_wait,
1531 php_http_curl_request_pool_once,
1532 php_http_curl_request_pool_attach,
1533 php_http_curl_request_pool_detach,
1534 php_http_curl_request_pool_setopt,
1535 };
1536
1537 PHP_HTTP_API php_http_request_pool_ops_t *php_http_curl_get_request_pool_ops(void)
1538 {
1539 return &php_http_curl_request_pool_ops;
1540 }
1541
1542 /* request handler ops */
1543
1544 static STATUS php_http_curl_request_reset(php_http_request_t *h);
1545
1546 static php_http_request_t *php_http_curl_request_init(php_http_request_t *h, void *handle)
1547 {
1548 php_http_curl_request_t *ctx;
1549 TSRMLS_FETCH_FROM_CTX(h->ts);
1550
1551 if (!handle && !(handle = php_http_resource_factory_handle_ctor(h->rf TSRMLS_CC))) {
1552 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "could not initialize curl handle");
1553 return NULL;
1554 }
1555
1556 ctx = ecalloc(1, sizeof(*ctx));
1557 ctx->handle = handle;
1558 php_http_buffer_init(&ctx->options.cookies);
1559 zend_hash_init(&ctx->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
1560 h->ctx = ctx;
1561
1562 #if defined(ZTS)
1563 curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
1564 #endif
1565 curl_easy_setopt(handle, CURLOPT_HEADER, 0L);
1566 curl_easy_setopt(handle, CURLOPT_FILETIME, 1L);
1567 curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
1568 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
1569 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
1570 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL);
1571 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, php_http_curl_dummy_callback);
1572 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, php_http_curl_raw_callback);
1573 curl_easy_setopt(handle, CURLOPT_READFUNCTION, php_http_curl_read_callback);
1574 curl_easy_setopt(handle, CURLOPT_IOCTLFUNCTION, php_http_curl_ioctl_callback);
1575 curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, php_http_curl_progress_callback);
1576 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, h);
1577 curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, h);
1578
1579 php_http_curl_request_reset(h);
1580
1581 return h;
1582 }
1583
1584 static php_http_request_t *php_http_curl_request_copy(php_http_request_t *from, php_http_request_t *to)
1585 {
1586 php_http_curl_request_t *ctx = from->ctx;
1587 void *copy;
1588 TSRMLS_FETCH_FROM_CTX(from->ts);
1589
1590 if (!(copy = php_http_resource_factory_handle_copy(from->rf, ctx->handle TSRMLS_CC))) {
1591 return NULL;
1592 }
1593
1594 if (to) {
1595 return php_http_curl_request_init(to, copy);
1596 } else {
1597 return php_http_request_init(NULL, from->ops, from->rf, copy TSRMLS_CC);
1598 }
1599 }
1600
1601 static void php_http_curl_request_dtor(php_http_request_t *h)
1602 {
1603 php_http_curl_request_t *ctx = h->ctx;
1604 TSRMLS_FETCH_FROM_CTX(h->ts);
1605
1606 curl_easy_setopt(ctx->handle, CURLOPT_NOPROGRESS, 1L);
1607 curl_easy_setopt(ctx->handle, CURLOPT_PROGRESSFUNCTION, NULL);
1608 curl_easy_setopt(ctx->handle, CURLOPT_VERBOSE, 0L);
1609 curl_easy_setopt(ctx->handle, CURLOPT_DEBUGFUNCTION, NULL);
1610
1611 php_http_resource_factory_handle_dtor(h->rf, ctx->handle TSRMLS_CC);
1612
1613 php_http_buffer_dtor(&ctx->options.cookies);
1614 zend_hash_destroy(&ctx->options.cache);
1615
1616 if (ctx->options.headers) {
1617 curl_slist_free_all(ctx->options.headers);
1618 ctx->options.headers = NULL;
1619 }
1620 php_http_request_progress_dtor(&ctx->progress TSRMLS_CC);
1621
1622 efree(ctx);
1623 h->ctx = NULL;
1624 }
1625 static STATUS php_http_curl_request_reset(php_http_request_t *h)
1626 {
1627 CURL *ch = ((php_http_curl_request_t *) h->ctx)->handle;
1628 php_http_curl_request_storage_t *st;
1629
1630 if ((st = get_storage(ch))) {
1631 if (st->url) {
1632 pefree(st->url, 1);
1633 st->url = NULL;
1634 }
1635 if (st->cookiestore) {
1636 pefree(st->cookiestore, 1);
1637 st->cookiestore = NULL;
1638 }
1639 st->errorbuffer[0] = '\0';
1640 }
1641
1642 curl_easy_setopt(ch, CURLOPT_URL, NULL);
1643 #if PHP_HTTP_CURL_VERSION(7,19,4)
1644 curl_easy_setopt(ch, CURLOPT_NOPROXY, NULL);
1645 #endif
1646 curl_easy_setopt(ch, CURLOPT_PROXY, NULL);
1647 curl_easy_setopt(ch, CURLOPT_PROXYPORT, 0L);
1648 curl_easy_setopt(ch, CURLOPT_PROXYTYPE, 0L);
1649 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1650 #if PHP_HTTP_CURL_VERSION(7,19,1)
1651 curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL);
1652 curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL);
1653 #endif
1654 curl_easy_setopt(ch, CURLOPT_PROXYAUTH, 0L);
1655 curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 0L);
1656 curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 60L);
1657 curl_easy_setopt(ch, CURLOPT_IPRESOLVE, 0);
1658 #if PHP_HTTP_CURL_VERSION(7,21,3)
1659 curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL);
1660 #endif
1661 #if PHP_HTTP_CURL_VERSION(7,24,0)
1662 curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, NULL);
1663 #endif
1664 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, 0L);
1665 curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, 0L);
1666 /* LFS weirdance
1667 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
1668 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
1669 */
1670 /* crashes
1671 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */
1672 curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 0L);
1673 curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 0L);
1674 curl_easy_setopt(ch, CURLOPT_INTERFACE, NULL);
1675 curl_easy_setopt(ch, CURLOPT_PORT, 0L);
1676 #if PHP_HTTP_CURL_VERSION(7,19,0)
1677 curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, 0L);
1678 #endif
1679 curl_easy_setopt(ch, CURLOPT_LOCALPORT, 0L);
1680 curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, 0L);
1681 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1682 #if PHP_HTTP_CURL_VERSION(7,19,1)
1683 curl_easy_setopt(ch, CURLOPT_USERNAME, NULL);
1684 curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL);
1685 #endif
1686 curl_easy_setopt(ch, CURLOPT_HTTPAUTH, 0L);
1687 curl_easy_setopt(ch, CURLOPT_ENCODING, NULL);
1688 /* we do this ourself anyway */
1689 curl_easy_setopt(ch, CURLOPT_HTTP_CONTENT_DECODING, 0L);
1690 curl_easy_setopt(ch, CURLOPT_HTTP_TRANSFER_DECODING, 0L);
1691 curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0L);
1692 #if PHP_HTTP_CURL_VERSION(7,19,1)
1693 curl_easy_setopt(ch, CURLOPT_POSTREDIR, 0L);
1694 #else
1695 curl_easy_setopt(ch, CURLOPT_POST301, 0L);
1696 #endif
1697 curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, 0L);
1698 curl_easy_setopt(ch, CURLOPT_REFERER, NULL);
1699 curl_easy_setopt(ch, CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_EXT_VERSION " (PHP/" PHP_VERSION ")");
1700 curl_easy_setopt(ch, CURLOPT_HTTPHEADER, NULL);
1701 curl_easy_setopt(ch, CURLOPT_COOKIE, NULL);
1702 curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 0L);
1703 /* these options would enable curl's cookie engine by default which we don't want
1704 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL);
1705 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */
1706 curl_easy_setopt(ch, CURLOPT_COOKIELIST, NULL);
1707 curl_easy_setopt(ch, CURLOPT_RANGE, NULL);
1708 curl_easy_setopt(ch, CURLOPT_RESUME_FROM, 0L);
1709 curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, 0L);
1710 curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0L);
1711 curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0L);
1712 curl_easy_setopt(ch, CURLOPT_TIMEOUT, 0L);
1713 curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3);
1714 curl_easy_setopt(ch, CURLOPT_SSLCERT, NULL);
1715 curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, NULL);
1716 curl_easy_setopt(ch, CURLOPT_SSLCERTPASSWD, NULL);
1717 curl_easy_setopt(ch, CURLOPT_SSLKEY, NULL);
1718 curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, NULL);
1719 curl_easy_setopt(ch, CURLOPT_SSLKEYPASSWD, NULL);
1720 curl_easy_setopt(ch, CURLOPT_SSLENGINE, NULL);
1721 curl_easy_setopt(ch, CURLOPT_SSLVERSION, 0L);
1722 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L);
1723 curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L);
1724 curl_easy_setopt(ch, CURLOPT_SSL_CIPHER_LIST, NULL);
1725 #if PHP_HTTP_CURL_VERSION(7,19,0)
1726 curl_easy_setopt(ch, CURLOPT_ISSUERCERT, NULL);
1727 #if defined(PHP_HTTP_HAVE_OPENSSL)
1728 curl_easy_setopt(ch, CURLOPT_CRLFILE, NULL);
1729 #endif
1730 #endif
1731 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1732 curl_easy_setopt(ch, CURLOPT_CERTINFO, NULL);
1733 #endif
1734 #ifdef PHP_HTTP_CURL_CAINFO
1735 curl_easy_setopt(ch, CURLOPT_CAINFO, PHP_HTTP_CURL_CAINFO);
1736 #else
1737 curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
1738 #endif
1739 curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
1740 curl_easy_setopt(ch, CURLOPT_RANDOM_FILE, NULL);
1741 curl_easy_setopt(ch, CURLOPT_EGDSOCKET, NULL);
1742 curl_easy_setopt(ch, CURLOPT_POSTFIELDS, NULL);
1743 curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, 0L);
1744 curl_easy_setopt(ch, CURLOPT_HTTPPOST, NULL);
1745 curl_easy_setopt(ch, CURLOPT_IOCTLDATA, NULL);
1746 curl_easy_setopt(ch, CURLOPT_READDATA, NULL);
1747 curl_easy_setopt(ch, CURLOPT_INFILESIZE, 0L);
1748 curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
1749 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL);
1750 curl_easy_setopt(ch, CURLOPT_NOBODY, 0L);
1751 curl_easy_setopt(ch, CURLOPT_POST, 0L);
1752 curl_easy_setopt(ch, CURLOPT_UPLOAD, 0L);
1753 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L);
1754
1755 return SUCCESS;
1756 }
1757
1758 static STATUS php_http_curl_request_exec(php_http_request_t *h, const char *meth, const char *url, php_http_message_body_t *body)
1759 {
1760 uint tries = 0;
1761 CURLcode result;
1762 php_http_curl_request_t *curl = h->ctx;
1763 php_http_curl_request_storage_t *storage = get_storage(curl->handle);
1764 TSRMLS_FETCH_FROM_CTX(h->ts);
1765
1766 if (SUCCESS != php_http_curl_request_prepare(h, meth, url, body)) {
1767 return FAILURE;
1768 }
1769
1770 retry:
1771 if (CURLE_OK != (result = curl_easy_perform(curl->handle))) {
1772 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), storage->errorbuffer, storage->url);
1773
1774 if (EG(exception)) {
1775 add_property_long(EG(exception), "curlCode", result);
1776 }
1777
1778 if (curl->options.retry.count > tries++) {
1779 switch (result) {
1780 case CURLE_COULDNT_RESOLVE_PROXY:
1781 case CURLE_COULDNT_RESOLVE_HOST:
1782 case CURLE_COULDNT_CONNECT:
1783 case CURLE_WRITE_ERROR:
1784 case CURLE_READ_ERROR:
1785 case CURLE_OPERATION_TIMEDOUT:
1786 case CURLE_SSL_CONNECT_ERROR:
1787 case CURLE_GOT_NOTHING:
1788 case CURLE_SSL_ENGINE_SETFAILED:
1789 case CURLE_SEND_ERROR:
1790 case CURLE_RECV_ERROR:
1791 case CURLE_SSL_ENGINE_INITFAILED:
1792 case CURLE_LOGIN_DENIED:
1793 if (curl->options.retry.delay >= PHP_HTTP_DIFFSEC) {
1794 php_http_sleep(curl->options.retry.delay);
1795 }
1796 goto retry;
1797 default:
1798 break;
1799 }
1800 } else {
1801 return FAILURE;
1802 }
1803 }
1804
1805 return SUCCESS;
1806 }
1807
1808 static STATUS php_http_curl_request_setopt(php_http_request_t *h, php_http_request_setopt_opt_t opt, void *arg)
1809 {
1810 php_http_curl_request_t *curl = h->ctx;
1811 TSRMLS_FETCH_FROM_CTX(h->ts);
1812
1813 switch (opt) {
1814 case PHP_HTTP_REQUEST_OPT_SETTINGS:
1815 return set_options(h, arg);
1816 break;
1817
1818 case PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK:
1819 if (curl->progress.in_cb) {
1820 php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST, "Cannot change progress callback while executing it");
1821 return FAILURE;
1822 }
1823 if (curl->progress.callback) {
1824 php_http_request_progress_dtor(&curl->progress TSRMLS_CC);
1825 }
1826 curl->progress.callback = arg;
1827 break;
1828
1829 case PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE:
1830 /* are cookies already enabled anyway? */
1831 if (!get_storage(curl->handle)->cookiestore) {
1832 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIEFILE, "")) {
1833 return FAILURE;
1834 }
1835 }
1836 break;
1837
1838 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET:
1839 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "ALL")) {
1840 return FAILURE;
1841 }
1842 break;
1843
1844 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION:
1845 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "SESS")) {
1846 return FAILURE;
1847 }
1848 break;
1849
1850 case PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH:
1851 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_COOKIELIST, "FLUSH")) {
1852 return FAILURE;
1853 }
1854 break;
1855
1856 default:
1857 return FAILURE;
1858 }
1859
1860 return SUCCESS;
1861 }
1862
1863 static STATUS php_http_curl_request_getopt(php_http_request_t *h, php_http_request_getopt_opt_t opt, void *arg)
1864 {
1865 php_http_curl_request_t *curl = h->ctx;
1866
1867 switch (opt) {
1868 case PHP_HTTP_REQUEST_OPT_PROGRESS_INFO:
1869 *((php_http_request_progress_t **) arg) = &curl->progress;
1870 break;
1871
1872 case PHP_HTTP_REQUEST_OPT_TRANSFER_INFO:
1873 get_info(curl->handle, arg);
1874 break;
1875
1876 default:
1877 return FAILURE;
1878 }
1879
1880 return SUCCESS;
1881 }
1882
1883 static php_http_resource_factory_ops_t php_http_curl_resource_factory_ops = {
1884 php_http_curl_ctor,
1885 php_http_curl_copy,
1886 php_http_curl_dtor
1887 };
1888
1889 static php_http_request_ops_t php_http_curl_request_ops = {
1890 &php_http_curl_resource_factory_ops,
1891 php_http_curl_request_init,
1892 php_http_curl_request_copy,
1893 php_http_curl_request_dtor,
1894 php_http_curl_request_reset,
1895 php_http_curl_request_exec,
1896 php_http_curl_request_setopt,
1897 php_http_curl_request_getopt
1898 };
1899
1900 PHP_HTTP_API php_http_request_ops_t *php_http_curl_get_request_ops(void)
1901 {
1902 return &php_http_curl_request_ops;
1903 }
1904
1905 #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL)
1906 # ifdef PHP_WIN32
1907 # define PHP_HTTP_NEED_OPENSSL_TSL
1908 # include <openssl/crypto.h>
1909 # else /* !PHP_WIN32 */
1910 # if defined(PHP_HTTP_HAVE_OPENSSL)
1911 # define PHP_HTTP_NEED_OPENSSL_TSL
1912 # include <openssl/crypto.h>
1913 # elif defined(PHP_HTTP_HAVE_GNUTLS)
1914 # define PHP_HTTP_NEED_GNUTLS_TSL
1915 # include <gcrypt.h>
1916 # else
1917 # warning \
1918 "libcurl was compiled with SSL support, but configure could not determine which" \
1919 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
1920 "cause random crashes on SSL requests"
1921 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
1922 # endif /* PHP_WIN32 */
1923 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
1924
1925
1926 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1927 static MUTEX_T *php_http_openssl_tsl = NULL;
1928
1929 static void php_http_openssl_thread_lock(int mode, int n, const char * file, int line)
1930 {
1931 if (mode & CRYPTO_LOCK) {
1932 tsrm_mutex_lock(php_http_openssl_tsl[n]);
1933 } else {
1934 tsrm_mutex_unlock(php_http_openssl_tsl[n]);
1935 }
1936 }
1937
1938 static ulong php_http_openssl_thread_id(void)
1939 {
1940 return (ulong) tsrm_thread_id();
1941 }
1942 #endif
1943 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1944 static int php_http_gnutls_mutex_create(void **m)
1945 {
1946 if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
1947 return SUCCESS;
1948 } else {
1949 return FAILURE;
1950 }
1951 }
1952
1953 static int php_http_gnutls_mutex_destroy(void **m)
1954 {
1955 tsrm_mutex_free(*((MUTEX_T *) m));
1956 return SUCCESS;
1957 }
1958
1959 static int php_http_gnutls_mutex_lock(void **m)
1960 {
1961 return tsrm_mutex_lock(*((MUTEX_T *) m));
1962 }
1963
1964 static int php_http_gnutls_mutex_unlock(void **m)
1965 {
1966 return tsrm_mutex_unlock(*((MUTEX_T *) m));
1967 }
1968
1969 static struct gcry_thread_cbs php_http_gnutls_tsl = {
1970 GCRY_THREAD_OPTION_USER,
1971 NULL,
1972 php_http_gnutls_mutex_create,
1973 php_http_gnutls_mutex_destroy,
1974 php_http_gnutls_mutex_lock,
1975 php_http_gnutls_mutex_unlock
1976 };
1977 #endif
1978
1979 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpCURL, method, 0, req_args)
1980 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpCURL, method, 0)
1981 #define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpCURL, method, PHP_HTTP_ARGS(HttpCURL, method), visibility)
1982 #define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpCURL, method))
1983 #define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpCURL_##al), PHP_HTTP_ARGS(HttpCURL, al), vis)
1984
1985 PHP_HTTP_EMPTY_ARGS(__construct);
1986
1987 zend_class_entry *php_http_curl_class_entry;
1988 zend_function_entry php_http_curl_method_entry[] = {
1989 PHP_HTTP_CURL_ME(__construct, ZEND_ACC_PRIVATE|ZEND_ACC_CTOR)
1990
1991 EMPTY_FUNCTION_ENTRY
1992 };
1993
1994 PHP_METHOD(HttpCURL, __construct) {
1995 }
1996
1997 PHP_MINIT_FUNCTION(http_curl)
1998 {
1999 php_http_request_factory_driver_t driver = {
2000 &php_http_curl_request_ops,
2001 &php_http_curl_request_pool_ops,
2002 &php_http_curl_request_datashare_ops
2003 };
2004
2005 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
2006 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
2007 if (!CRYPTO_get_id_callback()) {
2008 int i, c = CRYPTO_num_locks();
2009
2010 php_http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
2011
2012 for (i = 0; i < c; ++i) {
2013 php_http_openssl_tsl[i] = tsrm_mutex_alloc();
2014 }
2015
2016 CRYPTO_set_id_callback(php_http_openssl_thread_id);
2017 CRYPTO_set_locking_callback(php_http_openssl_thread_lock);
2018 }
2019 #endif
2020 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
2021 gcry_control(GCRYCTL_SET_THREAD_CBS, &php_http_gnutls_tsl);
2022 #endif
2023
2024 if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
2025 return FAILURE;
2026 }
2027
2028 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_datashare.curl"), &php_http_curlsh_resource_factory_ops, NULL, NULL)) {
2029 return FAILURE;
2030 }
2031
2032 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request_pool.curl"), &php_http_curlm_resource_factory_ops, NULL, NULL)) {
2033 return FAILURE;
2034 }
2035
2036 if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_request.curl"), &php_http_curl_resource_factory_ops, NULL, NULL)) {
2037 return FAILURE;
2038 }
2039
2040 if (SUCCESS != php_http_request_factory_add_driver(ZEND_STRL("curl"), &driver)) {
2041 return FAILURE;
2042 }
2043
2044 PHP_HTTP_REGISTER_CLASS(http, CURL, http_curl, php_http_curl_class_entry, 0);
2045
2046 /*
2047 * HTTP Protocol Version Constants
2048 */
2049 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC);
2050 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC);
2051 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC); /* to be removed */
2052 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC);
2053
2054 /*
2055 * SSL Version Constants
2056 */
2057 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC);
2058 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC);
2059 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC);
2060 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC);
2061
2062 /*
2063 * DNS IPvX resolving
2064 */
2065 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC);
2066 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC);
2067 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC);
2068
2069 /*
2070 * Auth Constants
2071 */
2072 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC);
2073 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC);
2074 #if PHP_HTTP_CURL_VERSION(7,19,3)
2075 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC);
2076 #endif
2077 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC);
2078 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC);
2079 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC);
2080
2081 /*
2082 * Proxy Type Constants
2083 */
2084 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC);
2085 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC);
2086 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC);
2087 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC);
2088 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC);
2089 # if PHP_HTTP_CURL_VERSION(7,19,4)
2090 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC);
2091 # endif
2092
2093 /*
2094 * Post Redirection Constants
2095 */
2096 #if PHP_HTTP_CURL_VERSION(7,19,1)
2097 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC);
2098 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC);
2099 zend_declare_class_constant_long(php_http_curl_class_entry, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC);
2100 #endif
2101
2102 return SUCCESS;
2103 }
2104
2105 PHP_MSHUTDOWN_FUNCTION(http_curl)
2106 {
2107 curl_global_cleanup();
2108 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
2109 if (php_http_openssl_tsl) {
2110 int i, c = CRYPTO_num_locks();
2111
2112 CRYPTO_set_id_callback(NULL);
2113 CRYPTO_set_locking_callback(NULL);
2114
2115 for (i = 0; i < c; ++i) {
2116 tsrm_mutex_free(php_http_openssl_tsl[i]);
2117 }
2118
2119 free(php_http_openssl_tsl);
2120 php_http_openssl_tsl = NULL;
2121 }
2122 #endif
2123 return SUCCESS;
2124 }
2125
2126 PHP_RINIT_FUNCTION(http_curl)
2127 {
2128 #if PHP_HTTP_HAVE_EVENT
2129 if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_init())) {
2130 return FAILURE;
2131 }
2132 #endif
2133
2134 return SUCCESS;
2135 }
2136
2137 #endif /* PHP_HTTP_HAVE_CURL */
2138
2139 /*
2140 * Local variables:
2141 * tab-width: 4
2142 * c-basic-offset: 4
2143 * End:
2144 * vim600: noet sw=4 ts=4 fdm=marker
2145 * vim<600: noet sw=4 ts=4
2146 */
2147