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