fix JSON POST handler in PHP-5.6+
[m6w6/ext-http] / php_http_client_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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14 #include "php_http_client.h"
15
16 #if PHP_HTTP_HAVE_CURL
17
18 #if PHP_HTTP_HAVE_EVENT
19 # if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000)
20 # include <event.h>
21 # define event_base_new event_init
22 # define event_assign(e, b, s, a, cb, d) do {\
23 event_set(e, s, a, cb, d); \
24 event_base_set(b, e); \
25 } while(0)
26 # else
27 # if PHP_HTTP_HAVE_EVENT2
28 # include <event2/event.h>
29 # include <event2/event_struct.h>
30 # else
31 # error "libevent presence is unknown"
32 # endif
33 # endif
34 # ifndef DBG_EVENTS
35 # define DBG_EVENTS 0
36 # endif
37 #endif
38
39 typedef struct php_http_client_curl {
40 CURLM *handle;
41
42 int unfinished; /* int because of curl_multi_perform() */
43
44 #if PHP_HTTP_HAVE_EVENT
45 struct event *timeout;
46 unsigned useevents:1;
47 #endif
48 } php_http_client_curl_t;
49
50 typedef struct php_http_client_curl_handler {
51 CURL *handle;
52 php_resource_factory_t *rf;
53 php_http_client_t *client;
54 php_http_client_progress_state_t progress;
55
56 php_http_client_enqueue_t queue;
57
58 struct {
59 php_http_message_parser_t *parser;
60 php_http_message_t *message;
61 php_http_buffer_t *buffer;
62 } request;
63
64 struct {
65 php_http_message_parser_t *parser;
66 php_http_message_t *message;
67 php_http_buffer_t *buffer;
68 } response;
69
70 struct {
71 HashTable cache;
72
73 struct curl_slist *headers;
74 struct curl_slist *resolve;
75 php_http_buffer_t cookies;
76 php_http_buffer_t ranges;
77
78 long redirects;
79 unsigned range_request:1;
80 unsigned encode_cookies:1;
81
82 struct {
83 uint count;
84 double delay;
85 } retry;
86
87 } options;
88
89 } php_http_client_curl_handler_t;
90
91 typedef struct php_http_curle_storage {
92 char *url;
93 char *cookiestore;
94 char errorbuffer[0x100];
95 } php_http_curle_storage_t;
96
97 static inline php_http_curle_storage_t *php_http_curle_get_storage(CURL *ch) {
98 php_http_curle_storage_t *st = NULL;
99
100 curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st);
101
102 if (!st) {
103 st = pecalloc(1, sizeof(*st), 1);
104 curl_easy_setopt(ch, CURLOPT_PRIVATE, st);
105 curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer);
106 }
107
108 return st;
109 }
110
111 static void *php_http_curle_ctor(void *opaque, void *init_arg TSRMLS_DC)
112 {
113 void *ch;
114
115 if ((ch = curl_easy_init())) {
116 php_http_curle_get_storage(ch);
117 return ch;
118 }
119 return NULL;
120 }
121
122 static void *php_http_curle_copy(void *opaque, void *handle TSRMLS_DC)
123 {
124 void *ch;
125
126 if ((ch = curl_easy_duphandle(handle))) {
127 curl_easy_reset(ch);
128 php_http_curle_get_storage(ch);
129 return ch;
130 }
131 return NULL;
132 }
133
134 static void php_http_curle_dtor(void *opaque, void *handle TSRMLS_DC)
135 {
136 php_http_curle_storage_t *st = php_http_curle_get_storage(handle);
137
138 curl_easy_cleanup(handle);
139
140 if (st) {
141 if (st->url) {
142 pefree(st->url, 1);
143 }
144 if (st->cookiestore) {
145 pefree(st->cookiestore, 1);
146 }
147 pefree(st, 1);
148 }
149 }
150
151 static php_resource_factory_ops_t php_http_curle_resource_factory_ops = {
152 php_http_curle_ctor,
153 php_http_curle_copy,
154 php_http_curle_dtor
155 };
156
157 static void *php_http_curlm_ctor(void *opaque, void *init_arg 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 php_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
168 php_http_curlm_ctor,
169 NULL,
170 php_http_curlm_dtor
171 };
172
173 /* curl callbacks */
174
175 static size_t php_http_curle_read_callback(void *data, size_t len, size_t n, void *ctx)
176 {
177 php_http_message_body_t *body = ctx;
178
179 if (body && body->stream_id) {
180 php_stream *s = php_http_message_body_stream(body);
181
182 if (s) {
183 TSRMLS_FETCH_FROM_CTX(body->ts);
184 return php_stream_read(s, data, len * n);
185 } else abort();
186 }
187 return 0;
188 }
189
190 static int php_http_curle_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
191 {
192 php_http_client_curl_handler_t *h = ctx;
193 zend_bool update = 0;
194
195 if (h->progress.dl.total != dltotal
196 || h->progress.dl.now != dlnow
197 || h->progress.ul.total != ultotal
198 || h->progress.ul.now != ulnow
199 ) {
200 update = 1;
201
202 h->progress.dl.total = dltotal;
203 h->progress.dl.now = dlnow;
204 h->progress.ul.total = ultotal;
205 h->progress.ul.now = ulnow;
206 }
207
208 if (update && h->client->callback.progress.func) {
209 h->client->callback.progress.func(h->client->callback.progress.arg, h->client, &h->queue, &h->progress);
210 }
211
212 return 0;
213 }
214
215 static curlioerr php_http_curle_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
216 {
217 php_http_message_body_t *body = ctx;
218
219 if (cmd != CURLIOCMD_RESTARTREAD) {
220 return CURLIOE_UNKNOWNCMD;
221 }
222
223 if (body) {
224 TSRMLS_FETCH_FROM_CTX(body->ts);
225
226 if (SUCCESS == php_stream_rewind(php_http_message_body_stream(body))) {
227 return CURLIOE_OK;
228 }
229 }
230
231 return CURLIOE_FAILRESTART;
232 }
233
234 static int php_http_curle_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
235 {
236 php_http_client_curl_handler_t *h = ctx;
237 unsigned flags = 0;
238
239 /* catch progress */
240 switch (type) {
241 case CURLINFO_TEXT:
242 if (data[0] == '-') {
243 } else if (php_memnstr(data, ZEND_STRL("Adding handle:"), data + length)) {
244 h->progress.info = "setup";
245 } else if (php_memnstr(data, ZEND_STRL("addHandle"), data + length)) {
246 h->progress.info = "setup";
247 } else if (php_memnstr(data, ZEND_STRL("About to connect"), data + length)) {
248 h->progress.info = "resolve";
249 } else if (php_memnstr(data, ZEND_STRL("Trying"), data + length)) {
250 h->progress.info = "connect";
251 } else if (php_memnstr(data, ZEND_STRL("Found bundle for host"), data + length)) {
252 h->progress.info = "connect";
253 } else if (php_memnstr(data, ZEND_STRL("Connected"), data + length)) {
254 h->progress.info = "connected";
255 } else if (php_memnstr(data, ZEND_STRL("Re-using existing connection!"), data + length)) {
256 h->progress.info = "connected";
257 } else if (php_memnstr(data, ZEND_STRL("blacklisted"), data + length)) {
258 h->progress.info = "blacklist check";
259 } else if (php_memnstr(data, ZEND_STRL("SSL"), data + length)) {
260 h->progress.info = "ssl negotiation";
261 } else if (php_memnstr(data, ZEND_STRL("upload"), data + length)) {
262 h->progress.info = "uploaded";
263 } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) {
264 h->progress.info = "not disconnected";
265 } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) {
266 h->progress.info = "disconnected";
267 } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) {
268 h->progress.info = "redirect";
269 } else if (php_memnstr(data, ZEND_STRL("Operation timed out"), data + length)) {
270 h->progress.info = "timeout";
271 } else {
272 #if PHP_DEBUG
273 h->progress.info = data;
274 data[length - 1] = '\0';
275 #endif
276 }
277 if (h->client->callback.progress.func) {
278 h->client->callback.progress.func(h->client->callback.progress.arg, h->client, &h->queue, &h->progress);
279 }
280 break;
281 case CURLINFO_HEADER_OUT:
282 case CURLINFO_DATA_OUT:
283 case CURLINFO_SSL_DATA_OUT:
284 h->progress.info = "send";
285 break;
286 case CURLINFO_HEADER_IN:
287 case CURLINFO_DATA_IN:
288 case CURLINFO_SSL_DATA_IN:
289 h->progress.info = "receive";
290 break;
291 default:
292 break;
293 }
294 /* process data */
295 switch (type) {
296 case CURLINFO_HEADER_IN:
297 case CURLINFO_DATA_IN:
298 php_http_buffer_append(h->response.buffer, data, length);
299
300 if (h->options.redirects) {
301 flags |= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS;
302 }
303
304 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(h->response.parser, h->response.buffer, flags, &h->response.message)) {
305 return -1;
306 }
307 break;
308
309 case CURLINFO_HEADER_OUT:
310 case CURLINFO_DATA_OUT:
311 php_http_buffer_append(h->request.buffer, data, length);
312
313 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE == php_http_message_parser_parse(h->request.parser, h->request.buffer, flags, &h->request.message)) {
314 return -1;
315 }
316 break;
317 default:
318 break;
319 }
320
321 #if 0
322 /* debug */
323 _dpf(type, data, length);
324 #endif
325
326 return 0;
327 }
328
329 static int php_http_curle_dummy_callback(char *data, size_t n, size_t l, void *s)
330 {
331 return n*l;
332 }
333
334 static STATUS php_http_curle_get_info(CURL *ch, HashTable *info)
335 {
336 char *c;
337 long l;
338 double d;
339 struct curl_slist *s, *p;
340 zval *subarray, array;
341 INIT_PZVAL_ARRAY(&array, info);
342
343 /* BEGIN::CURLINFO */
344 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) {
345 add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1);
346 }
347 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) {
348 add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l);
349 }
350 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) {
351 add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d);
352 }
353 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) {
354 add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d);
355 }
356 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) {
357 add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d);
358 }
359 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) {
360 add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d);
361 }
362 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) {
363 add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d);
364 }
365 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) {
366 add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d);
367 }
368 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) {
369 add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d);
370 }
371 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) {
372 add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d);
373 }
374 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) {
375 add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l);
376 }
377 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) {
378 add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l);
379 }
380 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) {
381 add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l);
382 }
383 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) {
384 add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l);
385 }
386 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
387 add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d);
388 }
389 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) {
390 add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d);
391 }
392 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) {
393 add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d);
394 }
395 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) {
396 add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1);
397 }
398 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) {
399 add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d);
400 }
401 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) {
402 add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l);
403 }
404 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) {
405 add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l);
406 }
407 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) {
408 add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l);
409 }
410 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) {
411 add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l);
412 }
413 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) {
414 add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l);
415 }
416 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) {
417 add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l);
418 }
419 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) {
420 MAKE_STD_ZVAL(subarray);
421 array_init(subarray);
422 for (p = s; p; p = p->next) {
423 if (p->data) {
424 add_next_index_string(subarray, p->data, 1);
425 }
426 }
427 add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray);
428 curl_slist_free_all(s);
429 }
430 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) {
431 MAKE_STD_ZVAL(subarray);
432 array_init(subarray);
433 for (p = s; p; p = p->next) {
434 if (p->data) {
435 add_next_index_string(subarray, p->data, 1);
436 }
437 }
438 add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray);
439 curl_slist_free_all(s);
440 }
441 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) {
442 add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1);
443 }
444 #if PHP_HTTP_CURL_VERSION(7,19,0)
445 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) {
446 add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1);
447 }
448 #endif
449 #if PHP_HTTP_CURL_VERSION(7,19,0)
450 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) {
451 add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d);
452 }
453 #endif
454 #if PHP_HTTP_CURL_VERSION(7,19,4)
455 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) {
456 add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l);
457 }
458 #endif
459 #if PHP_HTTP_CURL_VERSION(7,21,0)
460 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) {
461 add_assoc_long_ex(&array, "primary_port", sizeof("primary_port"), l);
462 }
463 #endif
464 #if PHP_HTTP_CURL_VERSION(7,21,0)
465 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) {
466 add_assoc_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1);
467 }
468 #endif
469 #if PHP_HTTP_CURL_VERSION(7,21,0)
470 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) {
471 add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l);
472 }
473 #endif
474
475 /* END::CURLINFO */
476
477 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
478 {
479 int i;
480 zval *ci_array;
481 struct curl_certinfo *ci;
482 char *colon, *keyname;
483
484 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) {
485 MAKE_STD_ZVAL(ci_array);
486 array_init(ci_array);
487
488 for (i = 0; i < ci->num_of_certs; ++i) {
489 s = ci->certinfo[i];
490
491 MAKE_STD_ZVAL(subarray);
492 array_init(subarray);
493 for (p = s; p; p = p->next) {
494 if (p->data) {
495 if ((colon = strchr(p->data, ':'))) {
496 keyname = estrndup(p->data, colon - p->data);
497 add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1);
498 efree(keyname);
499 } else {
500 add_next_index_string(subarray, p->data, 1);
501 }
502 }
503 }
504 add_next_index_zval(ci_array, subarray);
505 }
506 add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array);
507 }
508 }
509 #endif
510 add_assoc_string_ex(&array, "error", sizeof("error"), php_http_curle_get_storage(ch)->errorbuffer, 1);
511
512 return SUCCESS;
513 }
514
515 static int compare_queue(php_http_client_enqueue_t *e, void *handle)
516 {
517 return handle == ((php_http_client_curl_handler_t *) e->opaque)->handle;
518 }
519
520 static void php_http_curlm_responsehandler(php_http_client_t *context)
521 {
522 int remaining = 0;
523 php_http_client_enqueue_t *enqueue;
524 php_http_client_curl_t *curl = context->ctx;
525 TSRMLS_FETCH_FROM_CTX(context->ts);
526
527 do {
528 CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining);
529
530 if (msg && CURLMSG_DONE == msg->msg) {
531 if (CURLE_OK != msg->data.result) {
532 php_http_curle_storage_t *st = php_http_curle_get_storage(msg->easy_handle);
533 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s; %s (%s)", curl_easy_strerror(msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url));
534 }
535
536 if ((enqueue = php_http_client_enqueued(context, msg->easy_handle, compare_queue))) {
537 php_http_client_curl_handler_t *handler = enqueue->opaque;
538
539 context->callback.response.func(context->callback.response.arg, context, &handler->queue, &handler->request.message, &handler->response.message);
540 }
541 }
542 } while (remaining);
543 }
544
545 #if PHP_HTTP_HAVE_EVENT
546
547 typedef struct php_http_curlm_event {
548 struct event evnt;
549 php_http_client_t *context;
550 } php_http_curlm_event_t;
551
552 static inline int etoca(short action) {
553 switch (action & (EV_READ|EV_WRITE)) {
554 case EV_READ:
555 return CURL_CSELECT_IN;
556 break;
557 case EV_WRITE:
558 return CURL_CSELECT_OUT;
559 break;
560 case EV_READ|EV_WRITE:
561 return CURL_CSELECT_IN|CURL_CSELECT_OUT;
562 break;
563 default:
564 return 0;
565 }
566 }
567
568 static void php_http_curlm_timeout_callback(int socket, short action, void *event_data)
569 {
570 php_http_client_t *context = event_data;
571 php_http_client_curl_t *curl = context->ctx;
572
573 #if DBG_EVENTS
574 fprintf(stderr, "T");
575 #endif
576 if (curl->useevents) {
577 CURLMcode rc;
578 TSRMLS_FETCH_FROM_CTX(context->ts);
579
580 /* ignore and use -1,0 on timeout */
581 (void) socket;
582 (void) action;
583
584 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, CURL_SOCKET_TIMEOUT, 0, &curl->unfinished)));
585
586 if (CURLM_OK != rc) {
587 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_multi_strerror(rc));
588 }
589
590 php_http_curlm_responsehandler(context);
591 }
592 }
593
594 static void php_http_curlm_event_callback(int socket, short action, void *event_data)
595 {
596 php_http_client_t *context = event_data;
597 php_http_client_curl_t *curl = context->ctx;
598
599 #if DBG_EVENTS
600 fprintf(stderr, "E");
601 #endif
602 if (curl->useevents) {
603 CURLMcode rc = CURLM_OK;
604 TSRMLS_FETCH_FROM_CTX(context->ts);
605
606 while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
607
608 if (CURLM_OK != rc) {
609 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_multi_strerror(rc));
610 }
611
612 php_http_curlm_responsehandler(context);
613
614 /* remove timeout if there are no transfers left */
615 if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
616 event_del(curl->timeout);
617 }
618 }
619 }
620
621 static int php_http_curlm_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
622 {
623 php_http_client_t *context = socket_data;
624 php_http_client_curl_t *curl = context->ctx;
625
626 #if DBG_EVENTS
627 fprintf(stderr, "S");
628 #endif
629 if (curl->useevents) {
630 int events = EV_PERSIST;
631 php_http_curlm_event_t *ev = assign_data;
632 TSRMLS_FETCH_FROM_CTX(context->ts);
633
634 if (!ev) {
635 ev = ecalloc(1, sizeof(php_http_curlm_event_t));
636 ev->context = context;
637 curl_multi_assign(curl->handle, sock, ev);
638 } else {
639 event_del(&ev->evnt);
640 }
641
642 switch (action) {
643 case CURL_POLL_IN:
644 events |= EV_READ;
645 break;
646 case CURL_POLL_OUT:
647 events |= EV_WRITE;
648 break;
649 case CURL_POLL_INOUT:
650 events |= EV_READ|EV_WRITE;
651 break;
652
653 case CURL_POLL_REMOVE:
654 efree(ev);
655 /* no break */
656 case CURL_POLL_NONE:
657 return 0;
658
659 default:
660 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown socket action %d", action);
661 return -1;
662 }
663
664 event_assign(&ev->evnt, PHP_HTTP_G->curl.event_base, sock, events, php_http_curlm_event_callback, context);
665 event_add(&ev->evnt, NULL);
666 }
667
668 return 0;
669 }
670
671 static void php_http_curlm_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
672 {
673 php_http_client_t *context = timer_data;
674 php_http_client_curl_t *curl = context->ctx;
675
676 #if DBG_EVENTS
677 fprintf(stderr, "\ntimer <- timeout_ms: %ld\n", timeout_ms);
678 #endif
679 if (curl->useevents) {
680
681 if (timeout_ms < 0) {
682 php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT, /*EV_READ|EV_WRITE*/0, context);
683 } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
684 struct timeval timeout;
685 TSRMLS_FETCH_FROM_CTX(context->ts);
686
687 if (!event_initialized(curl->timeout)) {
688 event_assign(curl->timeout, PHP_HTTP_G->curl.event_base, CURL_SOCKET_TIMEOUT, 0, php_http_curlm_timeout_callback, context);
689 } else if (event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
690 event_del(curl->timeout);
691 }
692
693 timeout.tv_sec = timeout_ms / 1000;
694 timeout.tv_usec = (timeout_ms % 1000) * 1000;
695
696 event_add(curl->timeout, &timeout);
697 }
698 }
699 }
700
701 #endif /* HAVE_EVENT */
702
703 /* curl options */
704
705 static php_http_options_t php_http_curle_options;
706
707 #define PHP_HTTP_CURLE_OPTION_CHECK_STRLEN 0x0001
708 #define PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR 0x0002
709 #define PHP_HTTP_CURLE_OPTION_TRANSFORM_MS 0x0004
710
711 static STATUS php_http_curle_option_set_ssl_verifyhost(php_http_option_t *opt, zval *val, void *userdata)
712 {
713 php_http_client_curl_handler_t *curl = userdata;
714 CURL *ch = curl->handle;
715
716 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_BVAL_P(val) ? 2 : 0)) {
717 return FAILURE;
718 }
719 return SUCCESS;
720 }
721
722 static STATUS php_http_curle_option_set_cookiestore(php_http_option_t *opt, zval *val, void *userdata)
723 {
724 php_http_client_curl_handler_t *curl = userdata;
725 CURL *ch = curl->handle;
726
727 if (val) {
728 php_http_curle_storage_t *storage = php_http_curle_get_storage(curl->handle);
729
730 if (storage->cookiestore) {
731 pefree(storage->cookiestore, 1);
732 }
733 storage->cookiestore = pestrndup(Z_STRVAL_P(val), Z_STRLEN_P(val), 1);
734 if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore)
735 || CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore)
736 ) {
737 return FAILURE;
738 }
739 }
740 return SUCCESS;
741 }
742
743 static STATUS php_http_curle_option_set_cookies(php_http_option_t *opt, zval *val, void *userdata)
744 {
745 php_http_client_curl_handler_t *curl = userdata;
746 CURL *ch = curl->handle;
747 TSRMLS_FETCH_FROM_CTX(curl->client->ts);
748
749 if (val && Z_TYPE_P(val) != IS_NULL) {
750 if (curl->options.encode_cookies) {
751 if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(val), &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0 TSRMLS_CC)) {
752 php_http_buffer_fix(&curl->options.cookies);
753 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) {
754 return FAILURE;
755 }
756 } else {
757 return FAILURE;
758 }
759 } else {
760 HashPosition pos;
761 php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0);
762 zval **cookie_val;
763
764 FOREACH_KEYVAL(pos, val, cookie_key, cookie_val) {
765 zval *zv = php_http_ztyp(IS_STRING, *cookie_val);
766
767 php_http_array_hashkey_stringify(&cookie_key);
768 php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(zv));
769 php_http_array_hashkey_stringfree(&cookie_key);
770
771 zval_ptr_dtor(&zv);
772 }
773
774 php_http_buffer_fix(&curl->options.cookies);
775 if (curl->options.cookies.used) {
776 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) {
777 return FAILURE;
778 }
779 }
780 }
781 }
782 return SUCCESS;
783 }
784
785 static STATUS php_http_curle_option_set_encodecookies(php_http_option_t *opt, zval *val, void *userdata)
786 {
787 php_http_client_curl_handler_t *curl = userdata;
788
789 curl->options.encode_cookies = Z_BVAL_P(val);
790 return SUCCESS;
791 }
792
793 static STATUS php_http_curle_option_set_lastmodified(php_http_option_t *opt, zval *val, void *userdata)
794 {
795 php_http_client_curl_handler_t *curl = userdata;
796 CURL *ch = curl->handle;
797 TSRMLS_FETCH_FROM_CTX(curl->client->ts);
798
799 if (Z_LVAL_P(val)) {
800 if (Z_LVAL_P(val) > 0) {
801 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(val))) {
802 return FAILURE;
803 }
804 } else {
805 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(val))) {
806 return FAILURE;
807 }
808 }
809 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (curl->options.range_request ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE))) {
810 return FAILURE;
811 }
812 } else {
813 if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0)
814 || CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0)
815 ) {
816 return FAILURE;
817 }
818 }
819 return SUCCESS;
820 }
821
822 static STATUS php_http_curle_option_set_compress(php_http_option_t *opt, zval *val, void *userdata)
823 {
824 php_http_client_curl_handler_t *curl = userdata;
825
826 if (Z_BVAL_P(val)) {
827 curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
828 }
829 return SUCCESS;
830 }
831
832 static STATUS php_http_curle_option_set_etag(php_http_option_t *opt, zval *val, void *userdata)
833 {
834 php_http_client_curl_handler_t *curl = userdata;
835 php_http_buffer_t header;
836 zend_bool is_quoted = !((Z_STRVAL_P(val)[0] != '"') || (Z_STRVAL_P(val)[Z_STRLEN_P(val)-1] != '"'));
837
838 php_http_buffer_init(&header);
839 php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", curl->options.range_request?"If-Match":"If-None-Match", Z_STRVAL_P(val));
840 php_http_buffer_fix(&header);
841 curl->options.headers = curl_slist_append(curl->options.headers, header.data);
842 php_http_buffer_dtor(&header);
843 return SUCCESS;
844 }
845
846 static STATUS php_http_curle_option_set_range(php_http_option_t *opt, zval *val, void *userdata)
847 {
848 php_http_client_curl_handler_t *curl = userdata;
849 CURL *ch = curl->handle;
850 TSRMLS_FETCH_FROM_CTX(curl->client->ts);
851
852 php_http_buffer_reset(&curl->options.ranges);
853
854 if (val && Z_TYPE_P(val) != IS_NULL) {
855 HashPosition pos;
856 zval **rr, **rb, **re;
857
858 FOREACH_VAL(pos, val, rr) {
859 if (Z_TYPE_PP(rr) == IS_ARRAY) {
860 if (2 == php_http_array_list(Z_ARRVAL_PP(rr) TSRMLS_CC, 2, &rb, &re)) {
861 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))) &&
862 ((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)))) {
863 zval *rbl = php_http_ztyp(IS_LONG, *rb);
864 zval *rel = php_http_ztyp(IS_LONG, *re);
865
866 if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
867 php_http_buffer_appendf(&curl->options.ranges, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
868 }
869 zval_ptr_dtor(&rbl);
870 zval_ptr_dtor(&rel);
871 }
872
873 }
874 }
875 }
876
877 if (curl->options.ranges.used) {
878 curl->options.range_request = 1;
879 /* ditch last comma */
880 curl->options.ranges.data[curl->options.ranges.used - 1] = '\0';
881 }
882 }
883
884 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RANGE, curl->options.ranges.data)) {
885 return FAILURE;
886 }
887 return SUCCESS;
888 }
889
890 static STATUS php_http_curle_option_set_resume(php_http_option_t *opt, zval *val, void *userdata)
891 {
892 php_http_client_curl_handler_t *curl = userdata;
893 CURL *ch = curl->handle;
894
895 if (Z_LVAL_P(val) > 0) {
896 curl->options.range_request = 1;
897 }
898 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(val))) {
899 return FAILURE;
900 }
901 return SUCCESS;
902 }
903
904 static STATUS php_http_curle_option_set_retrydelay(php_http_option_t *opt, zval *val, void *userdata)
905 {
906 php_http_client_curl_handler_t *curl = userdata;
907
908 curl->options.retry.delay = Z_DVAL_P(val);
909 return SUCCESS;
910 }
911
912 static STATUS php_http_curle_option_set_retrycount(php_http_option_t *opt, zval *val, void *userdata)
913 {
914 php_http_client_curl_handler_t *curl = userdata;
915
916 curl->options.retry.count = Z_LVAL_P(val);
917 return SUCCESS;
918 }
919
920 static STATUS php_http_curle_option_set_redirect(php_http_option_t *opt, zval *val, void *userdata)
921 {
922 php_http_client_curl_handler_t *curl = userdata;
923 CURL *ch = curl->handle;
924
925 if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(val) ? 1L : 0L)
926 || CURLE_OK != curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(val))
927 ) {
928 return FAILURE;
929 }
930 return SUCCESS;
931 }
932
933 static STATUS php_http_curle_option_set_portrange(php_http_option_t *opt, zval *val, void *userdata)
934 {
935 php_http_client_curl_handler_t *curl = userdata;
936 CURL *ch = curl->handle;
937 long localport = 0, localportrange = 0;
938 TSRMLS_FETCH_FROM_CTX(curl->client->ts);
939
940 if (val && Z_TYPE_P(val) != IS_NULL) {
941 zval **z_port_start, *zps_copy = NULL, **z_port_end, *zpe_copy = NULL;
942
943 switch (php_http_array_list(Z_ARRVAL_P(val) TSRMLS_CC, 2, &z_port_start, &z_port_end)) {
944 case 2:
945 zps_copy = php_http_ztyp(IS_LONG, *z_port_start);
946 zpe_copy = php_http_ztyp(IS_LONG, *z_port_end);
947 localportrange = labs(Z_LVAL_P(zps_copy)-Z_LVAL_P(zpe_copy))+1L;
948 /* no break */
949 case 1:
950 if (!zps_copy) {
951 zps_copy = php_http_ztyp(IS_LONG, *z_port_start);
952 }
953 localport = (zpe_copy && Z_LVAL_P(zpe_copy) > 0) ? MIN(Z_LVAL_P(zps_copy), Z_LVAL_P(zpe_copy)) : Z_LVAL_P(zps_copy);
954 zval_ptr_dtor(&zps_copy);
955 if (zpe_copy) {
956 zval_ptr_dtor(&zpe_copy);
957 }
958 break;
959 default:
960 break;
961 }
962 }
963 if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORT, localport)
964 || CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, localportrange)
965 ) {
966 return FAILURE;
967 }
968 return SUCCESS;
969 }
970
971 #if PHP_HTTP_CURL_VERSION(7,21,3)
972 static STATUS php_http_curle_option_set_resolve(php_http_option_t *opt, zval *val, void *userdata)
973 {
974 php_http_client_curl_handler_t *curl = userdata;
975 CURL *ch = curl->handle;
976 TSRMLS_FETCH_FROM_CTX(curl->client->ts);
977
978 if (val && Z_TYPE_P(val) != IS_NULL) {
979 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
980 HashPosition pos;
981 zval **data;
982
983 FOREACH_KEYVAL(pos, val, key, data) {
984 zval *cpy = php_http_ztyp(IS_STRING, *data);
985 curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy));
986 zval_ptr_dtor(&cpy);
987 }
988
989 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, curl->options.resolve)) {
990 return FAILURE;
991 }
992 } else {
993 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL)) {
994 return FAILURE;
995 }
996 }
997 return SUCCESS;
998 }
999 #endif
1000
1001 static void php_http_curle_options_init(php_http_options_t *registry TSRMLS_DC)
1002 {
1003 php_http_option_t *opt;
1004
1005 /* proxy */
1006 if ((opt = php_http_option_register(registry, ZEND_STRL("proxyhost"), CURLOPT_PROXY, IS_STRING))) {
1007 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1008 }
1009 php_http_option_register(registry, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE, IS_LONG);
1010 php_http_option_register(registry, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT, IS_LONG);
1011 if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD, IS_STRING))) {
1012 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1013 }
1014 if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH, IS_LONG))) {
1015 Z_LVAL(opt->defval) = CURLAUTH_ANYSAFE;
1016 }
1017 php_http_option_register(registry, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL, IS_BOOL);
1018 #if PHP_HTTP_CURL_VERSION(7,19,4)
1019 php_http_option_register(registry, ZEND_STRL("noproxy"), CURLOPT_NOPROXY, IS_STRING);
1020 #endif
1021
1022 /* dns */
1023 if ((opt = php_http_option_register(registry, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT, IS_LONG))) {
1024 Z_LVAL(opt->defval) = 60;
1025 }
1026 php_http_option_register(registry, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE, IS_LONG);
1027 #if PHP_HTTP_CURL_VERSION(7,21,3)
1028 if ((opt = php_http_option_register(registry, ZEND_STRL("resolve"), CURLOPT_RESOLVE, IS_ARRAY))) {
1029 opt->setter = php_http_curle_option_set_resolve;
1030 }
1031 #endif
1032 #if PHP_HTTP_CURL_VERSION(7,24,0)
1033 if ((opt = php_http_option_register(registry, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS, IS_STRING))) {
1034 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1035 }
1036 #endif
1037
1038 /* limits */
1039 php_http_option_register(registry, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT, IS_LONG);
1040 php_http_option_register(registry, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME, IS_LONG);
1041
1042 /* LSF weirdance
1043 php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG);
1044 php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG);
1045 */
1046
1047 /* connection handling */
1048 /* crashes
1049 if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) {
1050 Z_LVAL(opt->defval) = 5;
1051 }
1052 */
1053 php_http_option_register(registry, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT, IS_BOOL);
1054 php_http_option_register(registry, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE, IS_BOOL);
1055
1056 /* outgoing interface */
1057 php_http_option_register(registry, ZEND_STRL("interface"), CURLOPT_INTERFACE, IS_STRING);
1058 if ((opt = php_http_option_register(registry, ZEND_STRL("portrange"), CURLOPT_LOCALPORT, IS_ARRAY))) {
1059 opt->setter = php_http_curle_option_set_portrange;
1060 }
1061
1062 /* another endpoint port */
1063 php_http_option_register(registry, ZEND_STRL("port"), CURLOPT_PORT, IS_LONG);
1064
1065 /* RFC4007 zone_id */
1066 #if PHP_HTTP_CURL_VERSION(7,19,0)
1067 php_http_option_register(registry, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE, IS_LONG);
1068 #endif
1069
1070 /* auth */
1071 if ((opt = php_http_option_register(registry, ZEND_STRL("httpauth"), CURLOPT_USERPWD, IS_STRING))) {
1072 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1073 }
1074 if ((opt = php_http_option_register(registry, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH, IS_LONG))) {
1075 Z_LVAL(opt->defval) = CURLAUTH_ANYSAFE;
1076 }
1077
1078 /* redirects */
1079 if ((opt = php_http_option_register(registry, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION, IS_LONG))) {
1080 opt->setter = php_http_curle_option_set_redirect;
1081 }
1082 php_http_option_register(registry, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH, IS_BOOL);
1083 #if PHP_HTTP_CURL_VERSION(7,19,1)
1084 php_http_option_register(registry, ZEND_STRL("postredir"), CURLOPT_POSTREDIR, IS_LONG);
1085 #endif
1086
1087 /* retries */
1088 if ((opt = php_http_option_register(registry, ZEND_STRL("retrycount"), 0, IS_LONG))) {
1089 opt->setter = php_http_curle_option_set_retrycount;
1090 }
1091 if ((opt = php_http_option_register(registry, ZEND_STRL("retrydelay"), 0, IS_DOUBLE))) {
1092 opt->setter = php_http_curle_option_set_retrydelay;
1093 }
1094
1095 /* referer */
1096 if ((opt = php_http_option_register(registry, ZEND_STRL("referer"), CURLOPT_REFERER, IS_STRING))) {
1097 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1098 }
1099 if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, IS_BOOL))) {
1100 ZVAL_BOOL(&opt->defval, 1);
1101 }
1102
1103 /* useragent */
1104 if ((opt = php_http_option_register(registry, ZEND_STRL("useragent"), CURLOPT_USERAGENT, IS_STRING))) {
1105 /* don't check strlen, to allow sending no useragent at all */
1106 ZVAL_STRING(&opt->defval, "PECL::HTTP/" PHP_PECL_HTTP_VERSION " (PHP/" PHP_VERSION ")", 0);
1107 }
1108
1109 /* resume */
1110 if ((opt = php_http_option_register(registry, ZEND_STRL("resume"), CURLOPT_RESUME_FROM, IS_LONG))) {
1111 opt->setter = php_http_curle_option_set_resume;
1112 }
1113 /* ranges */
1114 if ((opt = php_http_option_register(registry, ZEND_STRL("range"), CURLOPT_RANGE, IS_ARRAY))) {
1115 opt->setter = php_http_curle_option_set_range;
1116 }
1117
1118 /* etag */
1119 if ((opt = php_http_option_register(registry, ZEND_STRL("etag"), 0, IS_STRING))) {
1120 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1121 opt->setter = php_http_curle_option_set_etag;
1122 }
1123
1124 /* compression */
1125 if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, IS_BOOL))) {
1126 opt->setter = php_http_curle_option_set_compress;
1127 }
1128
1129 /* lastmodified */
1130 if ((opt = php_http_option_register(registry, ZEND_STRL("lastmodified"), 0, IS_LONG))) {
1131 opt->setter = php_http_curle_option_set_lastmodified;
1132 }
1133
1134 /* cookies */
1135 if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, IS_BOOL))) {
1136 opt->setter = php_http_curle_option_set_encodecookies;
1137 ZVAL_BOOL(&opt->defval, 1);
1138 }
1139 if ((opt = php_http_option_register(registry, ZEND_STRL("cookies"), 0, IS_ARRAY))) {
1140 opt->setter = php_http_curle_option_set_cookies;
1141 }
1142
1143 /* cookiesession, don't load session cookies from cookiestore */
1144 php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, IS_BOOL);
1145 /* cookiestore, read initial cookies from that file and store cookies back into that file */
1146 if ((opt = php_http_option_register(registry, ZEND_STRL("cookiestore"), 0, IS_STRING))) {
1147 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1148 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1149 opt->setter = php_http_curle_option_set_cookiestore;
1150 }
1151
1152 /* maxfilesize */
1153 php_http_option_register(registry, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE, IS_LONG);
1154
1155 /* http protocol version */
1156 php_http_option_register(registry, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION, IS_LONG);
1157
1158 /* timeouts */
1159 if ((opt = php_http_option_register(registry, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS, IS_DOUBLE))) {
1160 opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
1161 }
1162 if ((opt = php_http_option_register(registry, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS, IS_DOUBLE))) {
1163 opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
1164 Z_DVAL(opt->defval) = 3;
1165 }
1166
1167 /* tcp */
1168 #if PHP_HTTP_CURL_VERSION(7,25,0)
1169 php_http_option_register(registry, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE, IS_BOOL);
1170 if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE, IS_LONG))) {
1171 Z_LVAL(opt->defval) = 60;
1172 }
1173 if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL, IS_LONG))) {
1174 Z_LVAL(opt->defval) = 60;
1175 }
1176 #endif
1177
1178 /* ssl */
1179 if ((opt = php_http_option_register(registry, ZEND_STRL("ssl"), 0, IS_ARRAY))) {
1180 registry = &opt->suboptions;
1181
1182 if ((opt = php_http_option_register(registry, ZEND_STRL("cert"), CURLOPT_SSLCERT, IS_STRING))) {
1183 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1184 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1185 }
1186 php_http_option_register(registry, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE, IS_STRING);
1187
1188 if ((opt = php_http_option_register(registry, ZEND_STRL("key"), CURLOPT_SSLKEY, IS_STRING))) {
1189 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1190 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1191 }
1192 php_http_option_register(registry, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE, IS_STRING);
1193 php_http_option_register(registry, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD, IS_STRING);
1194 php_http_option_register(registry, ZEND_STRL("engine"), CURLOPT_SSLENGINE, IS_STRING);
1195 php_http_option_register(registry, ZEND_STRL("version"), CURLOPT_SSLVERSION, IS_LONG);
1196 if ((opt = php_http_option_register(registry, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER, IS_BOOL))) {
1197 ZVAL_BOOL(&opt->defval, 1);
1198 }
1199 if ((opt = php_http_option_register(registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, IS_BOOL))) {
1200 ZVAL_BOOL(&opt->defval, 1);
1201 opt->setter = php_http_curle_option_set_ssl_verifyhost;
1202 }
1203 php_http_option_register(registry, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST, IS_STRING);
1204 if ((opt = php_http_option_register(registry, ZEND_STRL("cainfo"), CURLOPT_CAINFO, IS_STRING))) {
1205 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1206 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1207 #ifdef PHP_HTTP_CURL_CAINFO
1208 ZVAL_STRING(&opt->defval, PHP_HTTP_CURL_CAINFO, 0);
1209 #endif
1210 }
1211 if ((opt = php_http_option_register(registry, ZEND_STRL("capath"), CURLOPT_CAPATH, IS_STRING))) {
1212 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1213 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1214 }
1215 if ((opt = php_http_option_register(registry, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE, IS_STRING))) {
1216 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1217 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1218 }
1219 if ((opt = php_http_option_register(registry, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET, IS_STRING))) {
1220 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1221 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1222 }
1223 #if PHP_HTTP_CURL_VERSION(7,19,0)
1224 if ((opt = php_http_option_register(registry, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT, IS_STRING))) {
1225 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1226 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1227 }
1228 # ifdef PHP_HTTP_HAVE_OPENSSL
1229 if ((opt = php_http_option_register(registry, ZEND_STRL("crlfile"), CURLOPT_CRLFILE, IS_STRING))) {
1230 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1231 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1232 }
1233 # endif
1234 #endif
1235 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1236 php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, IS_BOOL);
1237 #endif
1238 }
1239 }
1240
1241 static zval *php_http_curle_get_option(php_http_option_t *opt, HashTable *options, void *userdata)
1242 {
1243 php_http_client_curl_handler_t *curl = userdata;
1244 zval *option;
1245
1246 if ((option = php_http_option_get(opt, options, NULL))) {
1247 option = php_http_ztyp(opt->type, option);
1248 zend_hash_quick_update(&curl->options.cache, opt->name.s, opt->name.l, opt->name.h, &option, sizeof(zval *), NULL);
1249 }
1250 return option;
1251 }
1252
1253 static STATUS php_http_curle_set_option(php_http_option_t *opt, zval *val, void *userdata)
1254 {
1255 php_http_client_curl_handler_t *curl = userdata;
1256 CURL *ch = curl->handle;
1257 zval tmp;
1258 STATUS rv = SUCCESS;
1259 TSRMLS_FETCH_FROM_CTX(curl->client->ts);
1260
1261 if (!val) {
1262 val = &opt->defval;
1263 }
1264
1265 switch (opt->type) {
1266 case IS_BOOL:
1267 if (opt->setter) {
1268 rv = opt->setter(opt, val, curl);
1269 } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_BVAL_P(val))) {
1270 rv = FAILURE;
1271 }
1272 break;
1273
1274 case IS_LONG:
1275 if (opt->setter) {
1276 rv = opt->setter(opt, val, curl);
1277 } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, Z_LVAL_P(val))) {
1278 rv = FAILURE;
1279 }
1280 break;
1281
1282 case IS_STRING:
1283 if (!(opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_STRLEN) || Z_STRLEN_P(val)) {
1284 if (!(opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR) || !Z_STRVAL_P(val) || SUCCESS == php_check_open_basedir(Z_STRVAL_P(val) TSRMLS_CC)) {
1285 if (opt->setter) {
1286 rv = opt->setter(opt, val, curl);
1287 } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, Z_STRVAL_P(val))) {
1288 rv = FAILURE;
1289 }
1290 }
1291 }
1292 break;
1293
1294 case IS_DOUBLE:
1295 if (opt->flags & PHP_HTTP_CURLE_OPTION_TRANSFORM_MS) {
1296 tmp = *val;
1297 Z_DVAL(tmp) *= 1000;
1298 val = &tmp;
1299 }
1300 if (opt->setter) {
1301 rv = opt->setter(opt, val, curl);
1302 } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_DVAL_P(val))) {
1303 rv = FAILURE;
1304 }
1305 break;
1306
1307 case IS_ARRAY:
1308 if (opt->setter) {
1309 rv = opt->setter(opt, val, curl);
1310 } else if (Z_TYPE_P(val) != IS_NULL) {
1311 rv = php_http_options_apply(&opt->suboptions, Z_ARRVAL_P(val), curl);
1312 }
1313 break;
1314
1315 default:
1316 if (opt->setter) {
1317 rv = opt->setter(opt, val, curl);
1318 } else {
1319 rv = FAILURE;
1320 }
1321 break;
1322 }
1323 if (rv != SUCCESS) {
1324 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s", opt->name.s);
1325 }
1326 return rv;
1327 }
1328
1329
1330 /* client ops */
1331
1332 static STATUS php_http_client_curl_handler_reset(php_http_client_curl_handler_t *curl)
1333 {
1334 CURL *ch = curl->handle;
1335 php_http_curle_storage_t *st;
1336
1337 if ((st = php_http_curle_get_storage(ch))) {
1338 if (st->url) {
1339 pefree(st->url, 1);
1340 st->url = NULL;
1341 }
1342 if (st->cookiestore) {
1343 pefree(st->cookiestore, 1);
1344 st->cookiestore = NULL;
1345 }
1346 st->errorbuffer[0] = '\0';
1347 }
1348
1349 curl_easy_setopt(ch, CURLOPT_URL, NULL);
1350 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1351 #if PHP_HTTP_CURL_VERSION(7,19,1)
1352 curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL);
1353 curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL);
1354 curl_easy_setopt(ch, CURLOPT_USERNAME, NULL);
1355 curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL);
1356 #endif
1357
1358 #if PHP_HTTP_CURL_VERSION(7,21,3)
1359 if (curl->options.resolve) {
1360 curl_slist_free_all(curl->options.resolve);
1361 curl->options.resolve = NULL;
1362 }
1363 #endif
1364 curl->options.retry.count = 0;
1365 curl->options.retry.delay = 0;
1366 curl->options.redirects = 0;
1367 curl->options.encode_cookies = 1;
1368
1369 if (curl->options.headers) {
1370 curl_slist_free_all(curl->options.headers);
1371 curl->options.headers = NULL;
1372 }
1373
1374 php_http_buffer_reset(&curl->options.cookies);
1375 php_http_buffer_reset(&curl->options.ranges);
1376
1377 return SUCCESS;
1378 }
1379
1380 static php_http_client_curl_handler_t *php_http_client_curl_handler_init(php_http_client_t *h, php_resource_factory_t *rf)
1381 {
1382 void *handle;
1383 php_http_client_curl_handler_t *handler;
1384 TSRMLS_FETCH_FROM_CTX(h->ts);
1385
1386 if (!(handle = php_resource_factory_handle_ctor(rf, NULL TSRMLS_CC))) {
1387 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize curl handle");
1388 return NULL;
1389 }
1390
1391 handler = ecalloc(1, sizeof(*handler));
1392 handler->rf = rf;
1393 handler->client = h;
1394 handler->handle = handle;
1395 handler->request.buffer = php_http_buffer_init(NULL);
1396 handler->request.parser = php_http_message_parser_init(NULL TSRMLS_CC);
1397 handler->request.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
1398 handler->response.buffer = php_http_buffer_init(NULL);
1399 handler->response.parser = php_http_message_parser_init(NULL TSRMLS_CC);
1400 handler->response.message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
1401 php_http_buffer_init(&handler->options.cookies);
1402 php_http_buffer_init(&handler->options.ranges);
1403 zend_hash_init(&handler->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
1404
1405 #if defined(ZTS)
1406 curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
1407 #endif
1408 curl_easy_setopt(handle, CURLOPT_HEADER, 0L);
1409 curl_easy_setopt(handle, CURLOPT_FILETIME, 1L);
1410 curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
1411 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
1412 curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
1413 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL);
1414 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, php_http_curle_dummy_callback);
1415 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, php_http_curle_raw_callback);
1416 curl_easy_setopt(handle, CURLOPT_READFUNCTION, php_http_curle_read_callback);
1417 curl_easy_setopt(handle, CURLOPT_IOCTLFUNCTION, php_http_curle_ioctl_callback);
1418 curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, php_http_curle_progress_callback);
1419 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, handler);
1420 curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, handler);
1421
1422 php_http_client_curl_handler_reset(handler);
1423
1424 return handler;
1425 }
1426
1427
1428 static STATUS php_http_client_curl_handler_prepare(php_http_client_curl_handler_t *curl, php_http_client_enqueue_t *enqueue)
1429 {
1430 size_t body_size;
1431 php_http_message_t *msg = enqueue->request;
1432 php_http_curle_storage_t *storage = php_http_curle_get_storage(curl->handle);
1433 TSRMLS_FETCH_FROM_CTX(curl->client->ts);
1434
1435 /* request url */
1436 if (!PHP_HTTP_INFO(msg).request.url) {
1437 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL");
1438 return FAILURE;
1439 }
1440 storage->errorbuffer[0] = '\0';
1441 if (storage->url) {
1442 pefree(storage->url, 1);
1443 }
1444 storage->url = pestrdup(PHP_HTTP_INFO(msg).request.url, 1);
1445 curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url);
1446
1447 /* request method */
1448 switch (php_http_select_str(PHP_HTTP_INFO(msg).request.method, 4, "GET", "HEAD", "POST", "PUT")) {
1449 case 0:
1450 curl_easy_setopt(curl->handle, CURLOPT_HTTPGET, 1L);
1451 break;
1452
1453 case 1:
1454 curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L);
1455 break;
1456
1457 case 2:
1458 curl_easy_setopt(curl->handle, CURLOPT_POST, 1L);
1459 break;
1460
1461 case 3:
1462 curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L);
1463 break;
1464
1465 default: {
1466 if (PHP_HTTP_INFO(msg).request.method) {
1467 curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, PHP_HTTP_INFO(msg).request.method);
1468 } else {
1469 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot use empty request method");
1470 return FAILURE;
1471 }
1472 break;
1473 }
1474 }
1475
1476 /* request headers */
1477 php_http_message_update_headers(msg);
1478 if (zend_hash_num_elements(&msg->hdrs)) {
1479 php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
1480 zval **header_val;
1481 HashPosition pos;
1482 php_http_buffer_t header;
1483
1484 php_http_buffer_init(&header);
1485 FOREACH_HASH_KEYVAL(pos, &msg->hdrs, header_key, header_val) {
1486 if (header_key.type == HASH_KEY_IS_STRING) {
1487 zval *header_cpy = php_http_ztyp(IS_STRING, *header_val);
1488
1489 php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
1490 php_http_buffer_fix(&header);
1491 curl->options.headers = curl_slist_append(curl->options.headers, header.data);
1492 php_http_buffer_reset(&header);
1493
1494 zval_ptr_dtor(&header_cpy);
1495 }
1496 }
1497 php_http_buffer_dtor(&header);
1498 }
1499 curl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, curl->options.headers);
1500
1501 /* attach request body */
1502 if ((body_size = php_http_message_body_size(msg->body))) {
1503 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
1504 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
1505 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
1506 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
1507 * does not allow a request body.
1508 */
1509 php_stream_rewind(php_http_message_body_stream(msg->body));
1510 curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, msg->body);
1511 curl_easy_setopt(curl->handle, CURLOPT_READDATA, msg->body);
1512 curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size);
1513 curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size);
1514 } else {
1515 curl_easy_setopt(curl->handle, CURLOPT_IOCTLDATA, NULL);
1516 curl_easy_setopt(curl->handle, CURLOPT_READDATA, NULL);
1517 curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, 0L);
1518 curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, 0L);
1519 }
1520
1521 php_http_options_apply(&php_http_curle_options, enqueue->options, curl);
1522
1523 return SUCCESS;
1524 }
1525
1526 static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t *handler)
1527 {
1528 TSRMLS_FETCH_FROM_CTX(handler->client->ts);
1529
1530 curl_easy_setopt(handler->handle, CURLOPT_NOPROGRESS, 1L);
1531 curl_easy_setopt(handler->handle, CURLOPT_PROGRESSFUNCTION, NULL);
1532 curl_easy_setopt(handler->handle, CURLOPT_VERBOSE, 0L);
1533 curl_easy_setopt(handler->handle, CURLOPT_DEBUGFUNCTION, NULL);
1534
1535 php_resource_factory_handle_dtor(handler->rf, handler->handle TSRMLS_CC);
1536 php_resource_factory_free(&handler->rf);
1537
1538 php_http_message_parser_free(&handler->request.parser);
1539 php_http_message_free(&handler->request.message);
1540 php_http_buffer_free(&handler->request.buffer);
1541 php_http_message_parser_free(&handler->response.parser);
1542 php_http_message_free(&handler->response.message);
1543 php_http_buffer_free(&handler->response.buffer);
1544 php_http_buffer_dtor(&handler->options.ranges);
1545 php_http_buffer_dtor(&handler->options.cookies);
1546 zend_hash_destroy(&handler->options.cache);
1547
1548 if (handler->options.headers) {
1549 curl_slist_free_all(handler->options.headers);
1550 handler->options.headers = NULL;
1551 }
1552
1553 efree(handler);
1554 }
1555
1556 static php_http_client_t *php_http_client_curl_init(php_http_client_t *h, void *handle)
1557 {
1558 php_http_client_curl_t *curl;
1559 TSRMLS_FETCH_FROM_CTX(h->ts);
1560
1561 if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL TSRMLS_CC))) {
1562 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize curl handle");
1563 return NULL;
1564 }
1565
1566 curl = ecalloc(1, sizeof(*curl));
1567 curl->handle = handle;
1568 curl->unfinished = 0;
1569 h->ctx = curl;
1570
1571 return h;
1572 }
1573
1574 static void php_http_client_curl_dtor(php_http_client_t *h)
1575 {
1576 php_http_client_curl_t *curl = h->ctx;
1577 TSRMLS_FETCH_FROM_CTX(h->ts);
1578
1579 #if PHP_HTTP_HAVE_EVENT
1580 if (curl->timeout) {
1581 efree(curl->timeout);
1582 curl->timeout = NULL;
1583 }
1584 #endif
1585 curl->unfinished = 0;
1586
1587 php_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
1588
1589 efree(curl);
1590 h->ctx = NULL;
1591 }
1592
1593 static void queue_dtor(php_http_client_enqueue_t *e)
1594 {
1595 php_http_client_curl_handler_t *handler = e->opaque;
1596
1597 if (handler->queue.dtor) {
1598 e->opaque = handler->queue.opaque;
1599 handler->queue.dtor(e);
1600 }
1601 php_http_client_curl_handler_dtor(handler);
1602 }
1603
1604 static php_resource_factory_t *create_rf(const char *url TSRMLS_DC)
1605 {
1606 php_url *purl;
1607 php_resource_factory_t *rf = NULL;
1608
1609 if (!url || !*url) {
1610 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL");
1611 return NULL;
1612 }
1613
1614 purl = php_url_parse(url);
1615
1616 if (!purl) {
1617 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse URL '%s'", url);
1618 return NULL;
1619 } else {
1620 char *id_str = NULL;
1621 size_t id_len = spprintf(&id_str, 0, "%s:%d", STR_PTR(purl->host), purl->port ? purl->port : 80);
1622 php_persistent_handle_factory_t *pf = php_persistent_handle_concede(NULL, ZEND_STRL("http\\Client\\Curl\\Request"), id_str, id_len, NULL, NULL TSRMLS_CC);
1623
1624 if (pf) {
1625 rf = php_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void*)) php_persistent_handle_abandon);
1626 }
1627
1628 php_url_free(purl);
1629 efree(id_str);
1630 }
1631
1632 if (!rf) {
1633 rf = php_resource_factory_init(NULL, &php_http_curle_resource_factory_ops, NULL, NULL);
1634 }
1635
1636 return rf;
1637 }
1638
1639 static STATUS php_http_client_curl_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
1640 {
1641 CURLMcode rs;
1642 php_http_client_curl_t *curl = h->ctx;
1643 php_http_client_curl_handler_t *handler;
1644 php_http_client_progress_state_t *progress;
1645 php_resource_factory_t *rf;
1646 TSRMLS_FETCH_FROM_CTX(h->ts);
1647
1648 rf = create_rf(enqueue->request->http.info.request.url TSRMLS_CC);
1649 if (!rf) {
1650 return FAILURE;
1651 }
1652
1653 handler = php_http_client_curl_handler_init(h, rf);
1654 if (!handler) {
1655 return FAILURE;
1656 }
1657
1658 if (SUCCESS != php_http_client_curl_handler_prepare(handler, enqueue)) {
1659 php_http_client_curl_handler_dtor(handler);
1660 return FAILURE;
1661 }
1662
1663 handler->queue = *enqueue;
1664 enqueue->opaque = handler;
1665 enqueue->dtor = queue_dtor;
1666
1667 if (CURLM_OK == (rs = curl_multi_add_handle(curl->handle, handler->handle))) {
1668 zend_llist_add_element(&h->requests, enqueue);
1669 ++curl->unfinished;
1670
1671 if (h->callback.progress.func && SUCCESS == php_http_client_getopt(h, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, enqueue->request, &progress)) {
1672 progress->info = "start";
1673 h->callback.progress.func(h->callback.progress.arg, h, &handler->queue, progress);
1674 progress->started = 1;
1675 }
1676
1677 return SUCCESS;
1678 } else {
1679 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs));
1680 return FAILURE;
1681 }
1682 }
1683
1684 static STATUS php_http_client_curl_dequeue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
1685 {
1686 CURLMcode rs;
1687 php_http_client_curl_t *curl = h->ctx;
1688 php_http_client_curl_handler_t *handler = enqueue->opaque;
1689 TSRMLS_FETCH_FROM_CTX(h->ts);
1690
1691 if (CURLM_OK == (rs = curl_multi_remove_handle(curl->handle, handler->handle))) {
1692 zend_llist_del_element(&h->requests, handler->handle, (int (*)(void *, void *)) compare_queue);
1693 return SUCCESS;
1694 } else {
1695 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs));
1696 }
1697
1698 return FAILURE;
1699 }
1700
1701 static void php_http_client_curl_reset(php_http_client_t *h)
1702 {
1703 zend_llist_element *next_el, *this_el;
1704
1705 for (this_el = h->requests.head; this_el; this_el = next_el) {
1706 next_el = this_el->next;
1707 php_http_client_curl_dequeue(h, (void *) this_el->data);
1708 }
1709 }
1710
1711 #ifdef PHP_WIN32
1712 # define SELECT_ERROR SOCKET_ERROR
1713 #else
1714 # define SELECT_ERROR -1
1715 #endif
1716
1717 static STATUS php_http_client_curl_wait(php_http_client_t *h, struct timeval *custom_timeout)
1718 {
1719 int MAX;
1720 fd_set R, W, E;
1721 struct timeval timeout;
1722 php_http_client_curl_t *curl = h->ctx;
1723
1724 #if PHP_HTTP_HAVE_EVENT
1725 if (curl->useevents) {
1726 TSRMLS_FETCH_FROM_CTX(h->ts);
1727
1728 php_error_docref(NULL TSRMLS_CC, E_WARNING, "not implemented");
1729 return FAILURE;
1730 }
1731 #endif
1732
1733 FD_ZERO(&R);
1734 FD_ZERO(&W);
1735 FD_ZERO(&E);
1736
1737 if (CURLM_OK == curl_multi_fdset(curl->handle, &R, &W, &E, &MAX)) {
1738 if (custom_timeout && timerisset(custom_timeout)) {
1739 timeout = *custom_timeout;
1740 } else {
1741 long max_tout = 1000;
1742
1743 if ((CURLM_OK == curl_multi_timeout(curl->handle, &max_tout)) && (max_tout > 0)) {
1744 timeout.tv_sec = max_tout / 1000;
1745 timeout.tv_usec = (max_tout % 1000) * 1000;
1746 } else {
1747 timeout.tv_sec = 0;
1748 timeout.tv_usec = 1000;
1749 }
1750 }
1751
1752 if (MAX == -1) {
1753 php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC));
1754 return SUCCESS;
1755 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
1756 return SUCCESS;
1757 }
1758 }
1759 return FAILURE;
1760 }
1761
1762 static int php_http_client_curl_once(php_http_client_t *h)
1763 {
1764 php_http_client_curl_t *curl = h->ctx;
1765
1766 #if PHP_HTTP_HAVE_EVENT
1767 if (curl->useevents) {
1768 TSRMLS_FETCH_FROM_CTX(h->ts);
1769 php_error_docref(NULL TSRMLS_CC, E_WARNING, "not implemented");
1770 return FAILURE;
1771 }
1772 #endif
1773
1774 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle, &curl->unfinished));
1775
1776 php_http_curlm_responsehandler(h);
1777
1778 return curl->unfinished;
1779
1780 }
1781
1782 static STATUS php_http_client_curl_exec(php_http_client_t *h)
1783 {
1784 #if PHP_HTTP_HAVE_EVENT
1785 php_http_client_curl_t *curl = h->ctx;
1786 #endif
1787 TSRMLS_FETCH_FROM_CTX(h->ts);
1788
1789 #if PHP_HTTP_HAVE_EVENT
1790 if (curl->useevents) {
1791 php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT, /*EV_READ|EV_WRITE*/0, h);
1792 do {
1793 int ev_rc = event_base_dispatch(PHP_HTTP_G->curl.event_base);
1794
1795 #if DBG_EVENTS
1796 fprintf(stderr, "%c", "X.0"[ev_rc+1]);
1797 #endif
1798
1799 if (ev_rc < 0) {
1800 php_error_docref(NULL TSRMLS_CC, E_ERROR, "Error in event_base_dispatch()");
1801 return FAILURE;
1802 }
1803 } while (curl->unfinished);
1804 } else
1805 #endif
1806 {
1807 while (php_http_client_curl_once(h)) {
1808 if (SUCCESS != php_http_client_curl_wait(h, NULL)) {
1809 #ifdef PHP_WIN32
1810 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
1811 php_error_docref(NULL TSRMLS_CC, E_WARNING, "WinSock error: %d", WSAGetLastError());
1812 #else
1813 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
1814 #endif
1815 return FAILURE;
1816 }
1817 }
1818 }
1819
1820 return SUCCESS;
1821 }
1822
1823 static STATUS php_http_client_curl_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
1824 {
1825 php_http_client_curl_t *curl = h->ctx;
1826
1827 switch (opt) {
1828 case PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING:
1829 if (CURLM_OK != curl_multi_setopt(curl->handle, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) {
1830 return FAILURE;
1831 }
1832 break;
1833
1834 case PHP_HTTP_CLIENT_OPT_USE_EVENTS:
1835 #if PHP_HTTP_HAVE_EVENT
1836 if ((curl->useevents = *((zend_bool *) arg))) {
1837 if (!curl->timeout) {
1838 curl->timeout = ecalloc(1, sizeof(struct event));
1839 }
1840 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, h);
1841 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, php_http_curlm_socket_callback);
1842 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, h);
1843 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, php_http_curlm_timer_callback);
1844 } else {
1845 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, NULL);
1846 curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, NULL);
1847 curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, NULL);
1848 curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, NULL);
1849 }
1850 break;
1851 #endif
1852
1853 default:
1854 return FAILURE;
1855 }
1856 return SUCCESS;
1857 }
1858
1859 static STATUS php_http_client_curl_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void **res)
1860 {
1861 php_http_client_enqueue_t *enqueue;
1862
1863 switch (opt) {
1864 case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO:
1865 if ((enqueue = php_http_client_enqueued(h, arg, NULL))) {
1866 php_http_client_curl_handler_t *handler = enqueue->opaque;
1867
1868 *((php_http_client_progress_state_t **) res) = &handler->progress;
1869 return SUCCESS;
1870 }
1871 break;
1872
1873 case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO:
1874 if ((enqueue = php_http_client_enqueued(h, arg, NULL))) {
1875 php_http_client_curl_handler_t *handler = enqueue->opaque;
1876
1877 php_http_curle_get_info(handler->handle, *(HashTable **) res);
1878 return SUCCESS;
1879 }
1880 break;
1881
1882 default:
1883 break;
1884 }
1885
1886 return FAILURE;
1887 }
1888
1889 static php_http_client_ops_t php_http_client_curl_ops = {
1890 &php_http_curlm_resource_factory_ops,
1891 php_http_client_curl_init,
1892 NULL /* copy */,
1893 php_http_client_curl_dtor,
1894 php_http_client_curl_reset,
1895 php_http_client_curl_exec,
1896 php_http_client_curl_wait,
1897 php_http_client_curl_once,
1898 php_http_client_curl_enqueue,
1899 php_http_client_curl_dequeue,
1900 php_http_client_curl_setopt,
1901 php_http_client_curl_getopt
1902 };
1903
1904 php_http_client_ops_t *php_http_client_curl_get_ops(void)
1905 {
1906 return &php_http_client_curl_ops;
1907 }
1908
1909 PHP_MINIT_FUNCTION(http_client_curl)
1910 {
1911 php_http_options_t *options;
1912 php_http_client_driver_t driver = {
1913 ZEND_STRL("curl"),
1914 &php_http_client_curl_ops
1915 };
1916
1917 if (SUCCESS != php_http_client_driver_add(&driver)) {
1918 return FAILURE;
1919 }
1920
1921 if (SUCCESS != php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl"), &php_http_curlm_resource_factory_ops, NULL, NULL TSRMLS_CC)) {
1922 return FAILURE;
1923 }
1924 if (SUCCESS != php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl\\Request"), &php_http_curle_resource_factory_ops, NULL, NULL TSRMLS_CC)) {
1925 return FAILURE;
1926 }
1927
1928 if ((options = php_http_options_init(&php_http_curle_options, 1))) {
1929 options->getter = php_http_curle_get_option;
1930 options->setter = php_http_curle_set_option;
1931
1932 php_http_curle_options_init(options TSRMLS_CC);
1933 }
1934
1935 /*
1936 * HTTP Protocol Version Constants
1937 */
1938 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0, CONST_CS|CONST_PERSISTENT);
1939 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1, CONST_CS|CONST_PERSISTENT);
1940 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE, CONST_CS|CONST_PERSISTENT);
1941
1942 /*
1943 * SSL Version Constants
1944 */
1945 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1, CONST_CS|CONST_PERSISTENT);
1946 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2, CONST_CS|CONST_PERSISTENT);
1947 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3, CONST_CS|CONST_PERSISTENT);
1948 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT, CONST_CS|CONST_PERSISTENT);
1949
1950 /*
1951 * DNS IPvX resolving
1952 */
1953 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V4", CURL_IPRESOLVE_V4, CONST_CS|CONST_PERSISTENT);
1954 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V6", CURL_IPRESOLVE_V6, CONST_CS|CONST_PERSISTENT);
1955 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER, CONST_CS|CONST_PERSISTENT);
1956
1957 /*
1958 * Auth Constants
1959 */
1960 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_BASIC", CURLAUTH_BASIC, CONST_CS|CONST_PERSISTENT);
1961 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS|CONST_PERSISTENT);
1962 #if PHP_HTTP_CURL_VERSION(7,19,3)
1963 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE, CONST_CS|CONST_PERSISTENT);
1964 #endif
1965 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_NTLM", CURLAUTH_NTLM, CONST_CS|CONST_PERSISTENT);
1966 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE, CONST_CS|CONST_PERSISTENT);
1967 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_ANY", CURLAUTH_ANY, CONST_CS|CONST_PERSISTENT);
1968
1969 /*
1970 * Proxy Type Constants
1971 */
1972 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4", CURLPROXY_SOCKS4, CONST_CS|CONST_PERSISTENT);
1973 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4A", CURLPROXY_SOCKS5, CONST_CS|CONST_PERSISTENT);
1974 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5, CONST_CS|CONST_PERSISTENT);
1975 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5", CURLPROXY_SOCKS5, CONST_CS|CONST_PERSISTENT);
1976 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP", CURLPROXY_HTTP, CONST_CS|CONST_PERSISTENT);
1977 # if PHP_HTTP_CURL_VERSION(7,19,4)
1978 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0, CONST_CS|CONST_PERSISTENT);
1979 # endif
1980
1981 /*
1982 * Post Redirection Constants
1983 */
1984 #if PHP_HTTP_CURL_VERSION(7,19,1)
1985 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_301", CURL_REDIR_POST_301, CONST_CS|CONST_PERSISTENT);
1986 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_302", CURL_REDIR_POST_302, CONST_CS|CONST_PERSISTENT);
1987 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_ALL", CURL_REDIR_POST_ALL, CONST_CS|CONST_PERSISTENT);
1988 #endif
1989
1990 return SUCCESS;
1991 }
1992
1993 PHP_MSHUTDOWN_FUNCTION(http_client_curl)
1994 {
1995 php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl"), NULL, 0 TSRMLS_CC);
1996 php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl\\Request"), NULL, 0 TSRMLS_CC);
1997
1998 php_http_options_dtor(&php_http_curle_options);
1999
2000 return SUCCESS;
2001 }
2002
2003 #if PHP_HTTP_HAVE_EVENT
2004 PHP_RINIT_FUNCTION(http_client_curl)
2005 {
2006 if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_base_new())) {
2007 return FAILURE;
2008 }
2009 return SUCCESS;
2010 }
2011 PHP_RSHUTDOWN_FUNCTION(http_client_curl)
2012 {
2013 if (PHP_HTTP_G->curl.event_base) {
2014 event_base_free(PHP_HTTP_G->curl.event_base);
2015 PHP_HTTP_G->curl.event_base = NULL;
2016 }
2017 return SUCCESS;
2018 }
2019 #endif /* PHP_HTTP_HAVE_EVENT */
2020
2021 #endif /* PHP_HTTP_HAVE_CURL */
2022
2023 /*
2024 * Local variables:
2025 * tab-width: 4
2026 * c-basic-offset: 4
2027 * End:
2028 * vim600: noet sw=4 ts=4 fdm=marker
2029 * vim<600: noet sw=4 ts=4
2030 */