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