4 *** Copyright (c) 2003-2008, Lars Nilsson, <lars@quantumchamaeleon.com>
5 *** Copyright (c) 2009, ygrek, <ygrek@autistici.org>
8 #ifndef CAML_NAME_SPACE
9 #define CAML_NAME_SPACE
17 #include <caml/config.h> /* defines HAS_UNISTD */
21 /* suppress false gcc warning on seekFunction */
22 #define CURL_DISABLE_TYPECHECK
23 #include <curl/curl.h>
25 #include <caml/alloc.h>
26 #include <caml/memory.h>
27 #include <caml/mlvalues.h>
28 #include <caml/callback.h>
29 #include <caml/fail.h>
30 #include <caml/unixsupport.h>
31 #include <caml/custom.h>
32 #include <caml/threads.h>
35 #define CAMLdrop caml_local_roots = caml__frame
41 #pragma message("No config file given.")
48 #define Val_none Val_int(0)
55 some
= caml_alloc(1, 0);
56 Store_field( some
, 0, v
);
60 static value
Val_pair(value v1
, value v2
)
64 pair
= caml_alloc_small(2,0);
70 static value
Val_cons(value list
, value v
) { return Val_pair(v
,list
); }
73 * Based on curl hiperfifo.c example
78 #define Val_socket(v) win_alloc_socket(v)
81 #error Socket_val not defined in unixsupport.h
85 #define Socket_val(v) Long_val(v)
88 #define Val_socket(v) Val_int(v)
92 typedef struct Connection Connection
;
93 typedef struct ConnectionList ConnectionList
;
95 #define Connection_val(v) (*(Connection**)Data_custom_val(v))
97 typedef enum OcamlValues
101 Ocaml_HEADERFUNCTION
,
102 Ocaml_PROGRESSFUNCTION
,
103 Ocaml_XFERINFOFUNCTION
,
107 Ocaml_OPENSOCKETFUNCTION
,
108 /* Ocaml_CLOSESOCKETFUNCTION, */
109 Ocaml_SSH_KEYFUNCTION
,
114 /* Not used, last for size */
124 size_t refcount
; /* number of references to this structure */
126 char *curl_ERRORBUFFER
;
127 char *curl_POSTFIELDS
;
128 struct curl_slist
*curl_HTTPHEADER
;
129 struct curl_slist
*httpPostBuffers
;
130 struct curl_httppost
*httpPostFirst
;
131 struct curl_httppost
*httpPostLast
;
132 struct curl_slist
*curl_RESOLVE
;
133 struct curl_slist
*curl_QUOTE
;
134 struct curl_slist
*curl_POSTQUOTE
;
135 struct curl_slist
*curl_HTTP200ALIASES
;
136 struct curl_slist
*curl_MAIL_RCPT
;
137 struct curl_slist
*curl_CONNECT_TO
;
138 #if HAVE_DECL_CURLOPT_MIMEPOST
139 curl_mime
*curl_MIMEPOST
;
143 typedef struct CURLErrorMapping CURLErrorMapping
;
145 struct CURLErrorMapping
151 CURLErrorMapping errorMap
[] =
153 {"CURLE_OK", CURLE_OK
},
154 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
155 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL
},
157 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
159 #if HAVE_DECL_CURLE_FAILED_INIT
160 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT
},
162 {"CURLE_FAILED_INIT", -1},
164 #if HAVE_DECL_CURLE_URL_MALFORMAT
165 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT
},
167 {"CURLE_URL_MALFORMAT", -1},
169 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
170 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER
},
172 {"CURLE_URL_MALFORMAT_USER", -1},
174 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
175 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY
},
177 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
179 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
180 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST
},
182 {"CURLE_COULDNT_RESOLVE_HOST", -1},
184 #if HAVE_DECL_CURLE_COULDNT_CONNECT
185 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT
},
187 {"CURLE_COULDNT_CONNECT", -1},
189 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
190 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY
},
192 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
194 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
195 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED
},
197 {"CURLE_FTP_ACCESS_DENIED", -1},
199 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
200 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT
},
202 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
204 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
205 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY
},
207 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
209 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
210 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY
},
212 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
214 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
215 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY
},
217 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
219 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
220 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT
},
222 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
224 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
225 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST
},
227 {"CURLE_FTP_CANT_GET_HOST", -1},
229 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
230 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT
},
232 {"CURLE_FTP_CANT_RECONNECT", -1},
234 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
235 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY
},
237 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
239 #if HAVE_DECL_CURLE_PARTIAL_FILE
240 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE
},
242 {"CURLE_PARTIAL_FILE", -1},
244 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
245 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE
},
247 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
249 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
250 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR
},
252 {"CURLE_FTP_WRITE_ERROR", -1},
254 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
255 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR
},
257 {"CURLE_FTP_QUOTE_ERROR", -1},
259 #if HAVE_DECL_CURLE_HTTP_RETURNED_ERROR
260 {"CURLE_HTTP_RETURNED_ERROR", CURLE_HTTP_RETURNED_ERROR
},
262 {"CURLE_HTTP_RETURNED_ERROR", -1},
264 #if HAVE_DECL_CURLE_WRITE_ERROR
265 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR
},
267 {"CURLE_WRITE_ERROR", -1},
269 #if HAVE_DECL_CURLE_MALFORMAT_USER
270 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER
},
272 {"CURLE_MALFORMAT_USER", -1},
274 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
275 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE
},
277 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
279 #if HAVE_DECL_CURLE_READ_ERROR
280 {"CURLE_READ_ERROR", CURLE_READ_ERROR
},
282 {"CURLE_READ_ERROR", -1},
284 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
285 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY
},
287 {"CURLE_OUT_OF_MEMORY", -1},
289 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
290 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED
},
292 {"CURLE_OPERATION_TIMEOUTED", -1},
294 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
295 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII
},
297 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
299 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
300 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED
},
302 {"CURLE_FTP_PORT_FAILED", -1},
304 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
305 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST
},
307 {"CURLE_FTP_COULDNT_USE_REST", -1},
309 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
310 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE
},
312 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
314 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
315 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR
},
317 {"CURLE_HTTP_RANGE_ERROR", -1},
319 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
320 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR
},
322 {"CURLE_HTTP_POST_ERROR", -1},
324 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
325 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR
},
327 {"CURLE_SSL_CONNECT_ERROR", -1},
329 #if HAVE_DECL_CURLE_BAD_DOWNLOAD_RESUME
330 {"CURLE_BAD_DOWNLOAD_RESUME", CURLE_BAD_DOWNLOAD_RESUME
},
332 {"CURLE_BAD_DOWNLOAD_RESUME", -1},
334 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
335 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE
},
337 {"CURLE_FILE_COULDNT_READ_FILE", -1},
339 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
340 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND
},
342 {"CURLE_LDAP_CANNOT_BIND", -1},
344 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
345 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED
},
347 {"CURLE_LDAP_SEARCH_FAILED", -1},
349 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
350 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND
},
352 {"CURLE_LIBRARY_NOT_FOUND", -1},
354 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
355 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND
},
357 {"CURLE_FUNCTION_NOT_FOUND", -1},
359 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
360 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK
},
362 {"CURLE_ABORTED_BY_CALLBACK", -1},
364 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
365 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT
},
367 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
369 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
370 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER
},
372 {"CURLE_BAD_CALLING_ORDER", -1},
374 #if HAVE_DECL_CURLE_INTERFACE_FAILED
375 {"CURLE_INTERFACE_FAILED", CURLE_INTERFACE_FAILED
},
377 {"CURLE_INTERFACE_FAILED", -1},
379 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
380 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED
},
382 {"CURLE_BAD_PASSWORD_ENTERED", -1},
384 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
385 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS
},
387 {"CURLE_TOO_MANY_REDIRECTS", -1},
389 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
390 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION
},
392 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
394 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
395 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX
},
397 {"CURLE_TELNET_OPTION_SYNTAX", -1},
399 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
400 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE
},
402 {"CURLE_SSL_PEER_CERTIFICATE", -1},
404 #if HAVE_DECL_CURLE_GOT_NOTHING
405 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING
},
407 {"CURLE_GOT_NOTHING", -1},
409 #if HAVE_DECL_CURLE_SSL_ENGINE_NOTFOUND
410 {"CURLE_SSL_ENGINE_NOTFOUND", CURLE_SSL_ENGINE_NOTFOUND
},
412 {"CURLE_SSL_ENGINE_NOTFOUND", -1},
414 #if HAVE_DECL_CURLE_SSL_ENGINE_SETFAILED
415 {"CURLE_SSL_ENGINE_SETFAILED", CURLE_SSL_ENGINE_SETFAILED
},
417 {"CURLE_SSL_ENGINE_SETFAILED", -1},
419 #if HAVE_DECL_CURLE_SEND_ERROR
420 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR
},
422 {"CURLE_SEND_ERROR", -1},
424 #if HAVE_DECL_CURLE_RECV_ERROR
425 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR
},
427 {"CURLE_RECV_ERROR", -1},
429 #if HAVE_DECL_CURLE_SHARE_IN_USE
430 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE
},
432 {"CURLE_SHARE_IN_USE", -1},
434 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
435 {"CURLE_SSL_CERTPROBLEM", CURLE_SSL_CERTPROBLEM
},
437 {"CURLE_SSL_CERTPROBLEM", -1},
439 #if HAVE_DECL_CURLE_SSL_CIPHER
440 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER
},
442 {"CURLE_SSL_CIPHER", -1},
444 #if HAVE_DECL_CURLE_SSL_CACERT
445 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT
},
447 {"CURLE_SSL_CACERT", -1},
449 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
450 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING
},
452 {"CURLE_BAD_CONTENT_ENCODING", -1},
454 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
455 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL
},
457 {"CURLE_LDAP_INVALID_URL", -1},
459 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
460 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED
},
462 {"CURLE_FILESIZE_EXCEEDED", -1},
464 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
465 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED
},
467 {"CURLE_FTP_SSL_FAILED", -1},
469 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
470 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND
},
472 {"CURLE_SEND_FAIL_REWIND", -1},
474 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
475 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED
},
477 {"CURLE_SSL_ENGINE_INITFAILED", -1},
479 #if HAVE_DECL_CURLE_LOGIN_DENIED
480 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED
},
482 {"CURLE_LOGIN_DENIED", -1},
484 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
485 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND
},
487 {"CURLE_TFTP_NOTFOUND", -1},
489 #if HAVE_DECL_CURLE_TFTP_PERM
490 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM
},
492 {"CURLE_TFTP_PERM", -1},
494 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
495 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL
},
497 {"CURLE_REMOTE_DISK_FULL", -1},
499 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
500 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL
},
502 {"CURLE_TFTP_ILLEGAL", -1},
504 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
505 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID
},
507 {"CURLE_TFTP_UNKNOWNID", -1},
509 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
510 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS
},
512 {"CURLE_REMOTE_FILE_EXISTS", -1},
514 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
515 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER
},
517 {"CURLE_TFTP_NOSUCHUSER", -1},
519 #if HAVE_DECL_CURLE_CONV_FAILED
520 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED
},
522 {"CURLE_CONV_FAILED", -1},
524 #if HAVE_DECL_CURLE_CONV_REQD
525 {"CURLE_CONV_REQD", CURLE_CONV_REQD
},
527 {"CURLE_CONV_REQD", -1},
529 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
530 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE
},
532 {"CURLE_SSL_CACERT_BADFILE", -1},
534 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
535 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND
},
537 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
539 #if HAVE_DECL_CURLE_SSH
540 {"CURLE_SSH", CURLE_SSH
},
544 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
545 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED
},
547 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
549 #if HAVE_DECL_CURLE_AGAIN
550 {"CURLE_AGAIN", CURLE_AGAIN
},
554 /* sync check_enums */
558 typedef struct CURLOptionMapping CURLOptionMapping
;
560 struct CURLOptionMapping
562 void (*optionHandler
)(Connection
*, value
);
566 static char* strdup_ml(value v
)
569 p
= (char*)malloc(caml_string_length(v
)+1);
570 memcpy(p
,String_val(v
),caml_string_length(v
)+1); // caml strings have terminating zero
574 static value
ml_copy_string(char const* p
, size_t size
)
576 value v
= caml_alloc_string(size
);
577 memcpy(&Byte(v
,0),p
,size
);
581 /* prepends to the beginning of list */
582 static struct curl_slist
* curl_slist_prepend_ml(struct curl_slist
* list
, value v
)
584 /* FIXME check NULLs */
585 struct curl_slist
* new_item
= (struct curl_slist
*)malloc(sizeof(struct curl_slist
));
587 new_item
->next
= list
;
588 new_item
->data
= strdup_ml(v
);
593 static void free_curl_slist(struct curl_slist
*slist
)
598 curl_slist_free_all(slist
);
601 static void raiseError(Connection
*conn
, CURLcode code
)
604 CAMLlocal1(exceptionData
);
605 const value
*exception
;
606 char *errorString
= "Unknown Error";
609 for (i
= 0; errorMap
[i
].name
!= NULL
; i
++)
611 if (errorMap
[i
].error
== code
)
613 errorString
= errorMap
[i
].name
;
618 exceptionData
= caml_alloc_tuple(3);
620 Store_field(exceptionData
, 0, Val_int(code
));
621 Store_field(exceptionData
, 1, Val_int(code
));
622 Store_field(exceptionData
, 2, caml_copy_string(errorString
));
624 if (conn
!= NULL
&& conn
->curl_ERRORBUFFER
!= NULL
)
626 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
629 exception
= caml_named_value("CurlException");
631 if (exception
== NULL
)
632 caml_failwith("CurlException not registered");
634 caml_raise_with_arg(*exception
, exceptionData
);
639 static void resetOcamlValues(Connection
* connection
)
643 for (i
= 0; i
< OcamlValuesSize
; i
++)
644 Store_field(connection
->ocamlValues
, i
, Val_unit
);
647 static Connection
* allocConnection(CURL
* h
)
649 Connection
* connection
= (Connection
*)malloc(sizeof(Connection
));
651 connection
->ocamlValues
= caml_alloc(OcamlValuesSize
, 0);
652 resetOcamlValues(connection
);
653 caml_register_global_root(&connection
->ocamlValues
);
655 connection
->handle
= h
;
656 curl_easy_setopt(h
, CURLOPT_PRIVATE
, connection
);
658 connection
->refcount
= 0;
660 connection
->curl_ERRORBUFFER
= NULL
;
661 connection
->curl_POSTFIELDS
= NULL
;
662 connection
->curl_HTTPHEADER
= NULL
;
663 connection
->httpPostBuffers
= NULL
;
664 connection
->httpPostFirst
= NULL
;
665 connection
->httpPostLast
= NULL
;
666 connection
->curl_QUOTE
= NULL
;
667 connection
->curl_POSTQUOTE
= NULL
;
668 connection
->curl_HTTP200ALIASES
= NULL
;
669 connection
->curl_RESOLVE
= NULL
;
670 connection
->curl_MAIL_RCPT
= NULL
;
671 connection
->curl_CONNECT_TO
= NULL
;
672 #if HAVE_DECL_CURLOPT_MIMEPOST
673 connection
->curl_MIMEPOST
= NULL
;
679 static Connection
*newConnection(void)
683 caml_enter_blocking_section();
684 h
= curl_easy_init();
685 caml_leave_blocking_section();
687 return allocConnection(h
);
690 static void free_if(void* p
) { if (NULL
!= p
) free(p
); }
692 static void removeConnection(Connection
*connection
, int finalization
)
694 const char* fin_url
= NULL
;
696 if (!connection
->handle
)
698 return; /* already cleaned up */
703 /* cannot engage OCaml runtime at finalization, just report leak */
704 if (CURLE_OK
!= curl_easy_getinfo(connection
->handle
, CURLINFO_EFFECTIVE_URL
, &fin_url
) || NULL
== fin_url
)
708 fprintf(stderr
,"Curl: handle %p leaked, conn %p, url %s\n", connection
->handle
, connection
, fin_url
);
713 caml_enter_blocking_section();
714 curl_easy_cleanup(connection
->handle
);
715 caml_leave_blocking_section();
718 connection
->handle
= NULL
;
720 caml_remove_global_root(&connection
->ocamlValues
);
722 free_if(connection
->curl_ERRORBUFFER
);
723 free_if(connection
->curl_POSTFIELDS
);
724 free_curl_slist(connection
->curl_HTTPHEADER
);
725 free_curl_slist(connection
->httpPostBuffers
);
726 if (connection
->httpPostFirst
!= NULL
)
727 curl_formfree(connection
->httpPostFirst
);
728 free_curl_slist(connection
->curl_RESOLVE
);
729 free_curl_slist(connection
->curl_QUOTE
);
730 free_curl_slist(connection
->curl_POSTQUOTE
);
731 free_curl_slist(connection
->curl_HTTP200ALIASES
);
732 free_curl_slist(connection
->curl_MAIL_RCPT
);
733 free_curl_slist(connection
->curl_CONNECT_TO
);
734 #if HAVE_DECL_CURLOPT_MIMEPOST
735 curl_mime_free(connection
->curl_MIMEPOST
);
739 static Connection
* getConnection(CURL
* h
)
741 Connection
* p
= NULL
;
743 if (CURLE_OK
!= curl_easy_getinfo(h
, CURLINFO_PRIVATE
, &p
) || NULL
== p
)
745 caml_failwith("Unknown handle");
752 static void checkConnection(Connection
* connection
)
757 static void checkConnection(Connection
*connection
)
759 if (connection
!= getConnection(connection
->handle
))
761 caml_failwith("Invalid Connection");
766 void op_curl_easy_finalize(value v
)
768 Connection
* conn
= Connection_val(v
);
769 /* same connection may be referenced by several different
770 OCaml values, see e.g. caml_curl_multi_remove_finished */
772 if (0 == conn
->refcount
)
774 removeConnection(conn
, 1);
779 int op_curl_easy_compare(value v1
, value v2
)
781 size_t p1
= (size_t)Connection_val(v1
);
782 size_t p2
= (size_t)Connection_val(v2
);
783 return (p1
== p2
? 0 : (p1
> p2
? 1 : -1)); /* compare addresses */
786 intnat
op_curl_easy_hash(value v
)
788 return (size_t)Connection_val(v
); /* address */
791 static struct custom_operations curl_easy_ops
= {
793 op_curl_easy_finalize
,
794 op_curl_easy_compare
,
796 custom_serialize_default
,
797 custom_deserialize_default
,
798 #if defined(custom_compare_ext_default)
799 custom_compare_ext_default
,
803 value
caml_curl_alloc(Connection
* conn
)
805 value v
= caml_alloc_custom(&curl_easy_ops
, sizeof(Connection
*), 0, 1);
806 Connection_val(v
) = conn
;
811 static size_t cb_WRITEFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
813 caml_leave_blocking_section();
816 CAMLlocal2(result
, str
);
817 Connection
*conn
= (Connection
*)data
;
819 checkConnection(conn
);
821 str
= ml_copy_string(ptr
,size
*nmemb
);
823 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
825 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
828 caml_enter_blocking_section();
832 static size_t cb_WRITEFUNCTION2(char *ptr
, size_t size
, size_t nmemb
, void *data
)
834 caml_leave_blocking_section();
837 CAMLlocal2(result
, str
);
838 Connection
*conn
= (Connection
*)data
;
840 checkConnection(conn
);
842 str
= ml_copy_string(ptr
,size
*nmemb
);
844 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
848 if (!Is_exception_result(result
))
850 if (Is_block(result
)) /* Proceed */
856 if (0 == Int_val(result
)) /* Pause */
857 r
= CURL_WRITEFUNC_PAUSE
;
864 caml_enter_blocking_section();
868 static size_t cb_READFUNCTION(void *ptr
, size_t size
, size_t nmemb
, void *data
)
870 caml_leave_blocking_section();
874 Connection
*conn
= (Connection
*)data
;
877 checkConnection(conn
);
879 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
880 Val_int(size
*nmemb
));
882 size_t r
= CURL_READFUNC_ABORT
;
884 if (!Is_exception_result(result
))
886 length
= caml_string_length(result
);
888 if (length
<= size
*nmemb
)
890 memcpy(ptr
, String_val(result
), length
);
897 caml_enter_blocking_section();
901 static size_t cb_READFUNCTION2(void *ptr
, size_t size
, size_t nmemb
, void *data
)
903 caml_leave_blocking_section();
907 Connection
*conn
= (Connection
*)data
;
910 checkConnection(conn
);
912 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
913 Val_int(size
*nmemb
));
915 size_t r
= CURL_READFUNC_ABORT
;
917 if (!Is_exception_result(result
))
919 if (Is_block(result
)) /* Proceed */
921 result
= Field(result
,0);
923 length
= caml_string_length(result
);
925 if (length
<= size
*nmemb
)
927 memcpy(ptr
, String_val(result
), length
);
933 if (0 == Int_val(result
)) /* Pause */
934 r
= CURL_READFUNC_PAUSE
;
941 caml_enter_blocking_section();
945 static size_t cb_HEADERFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
947 caml_leave_blocking_section();
950 CAMLlocal2(result
,str
);
951 Connection
*conn
= (Connection
*)data
;
953 checkConnection(conn
);
955 str
= ml_copy_string(ptr
,size
*nmemb
);
957 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_HEADERFUNCTION
), str
);
959 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
962 caml_enter_blocking_section();
966 static int cb_PROGRESSFUNCTION(void *data
,
972 caml_leave_blocking_section();
976 CAMLlocalN(callbackData
, 4);
977 Connection
*conn
= (Connection
*)data
;
979 checkConnection(conn
);
981 callbackData
[0] = caml_copy_double(dlTotal
);
982 callbackData
[1] = caml_copy_double(dlNow
);
983 callbackData
[2] = caml_copy_double(ulTotal
);
984 callbackData
[3] = caml_copy_double(ulNow
);
986 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PROGRESSFUNCTION
),
989 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
992 caml_enter_blocking_section();
996 static int cb_XFERINFOFUNCTION(void *data
,
1002 caml_leave_blocking_section();
1006 CAMLlocalN(callbackData
, 4);
1007 Connection
*conn
= (Connection
*)data
;
1009 checkConnection(conn
);
1011 callbackData
[0] = caml_copy_int64(dlTotal
);
1012 callbackData
[1] = caml_copy_int64(dlNow
);
1013 callbackData
[2] = caml_copy_int64(ulTotal
);
1014 callbackData
[3] = caml_copy_int64(ulNow
);
1016 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_XFERINFOFUNCTION
),
1019 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
1022 caml_enter_blocking_section();
1026 static int cb_DEBUGFUNCTION(CURL
*debugConnection
,
1027 curl_infotype infoType
,
1029 size_t bufferLength
,
1032 caml_leave_blocking_section();
1035 CAMLlocal3(camlDebugConnection
, camlInfoType
, camlMessage
);
1036 Connection
*conn
= (Connection
*)data
;
1037 (void)debugConnection
; /* not used */
1039 checkConnection(conn
);
1041 camlDebugConnection
= caml_curl_alloc(conn
);
1042 camlMessage
= ml_copy_string(buffer
,bufferLength
);
1043 camlInfoType
= Val_long(infoType
<= CURLINFO_SSL_DATA_OUT
/* sync check_enums */ ? infoType
: CURLINFO_END
);
1045 caml_callback3_exn(Field(conn
->ocamlValues
, Ocaml_DEBUGFUNCTION
),
1046 camlDebugConnection
,
1052 caml_enter_blocking_section();
1056 static curlioerr
cb_IOCTLFUNCTION(CURL
*ioctl
,
1060 caml_leave_blocking_section();
1063 CAMLlocal3(camlResult
, camlConnection
, camlCmd
);
1064 Connection
*conn
= (Connection
*)data
;
1065 curlioerr result
= CURLIOE_OK
;
1066 (void)ioctl
; /* not used */
1068 checkConnection(conn
);
1070 if (cmd
== CURLIOCMD_NOP
)
1071 camlCmd
= Val_long(0);
1072 else if (cmd
== CURLIOCMD_RESTARTREAD
)
1073 camlCmd
= Val_long(1);
1075 caml_failwith("Invalid IOCTL Cmd!");
1077 camlConnection
= caml_curl_alloc(conn
);
1079 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_IOCTLFUNCTION
),
1083 if (Is_exception_result(camlResult
))
1085 result
= CURLIOE_FAILRESTART
;
1088 switch (Long_val(camlResult
))
1090 case 0: /* CURLIOE_OK */
1091 result
= CURLIOE_OK
;
1094 case 1: /* CURLIOE_UNKNOWNCMD */
1095 result
= CURLIOE_UNKNOWNCMD
;
1098 case 2: /* CURLIOE_FAILRESTART */
1099 result
= CURLIOE_FAILRESTART
;
1102 default: /* Incorrect return value, but let's handle it */
1103 result
= CURLIOE_FAILRESTART
;
1108 caml_enter_blocking_section();
1112 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1113 static int cb_SEEKFUNCTION(void *data
,
1117 caml_leave_blocking_section();
1120 CAMLlocal3(camlResult
, camlOffset
, camlOrigin
);
1121 Connection
*conn
= (Connection
*)data
;
1123 camlOffset
= caml_copy_int64(offset
);
1125 if (origin
== SEEK_SET
)
1126 camlOrigin
= Val_long(0);
1127 else if (origin
== SEEK_CUR
)
1128 camlOrigin
= Val_long(1);
1129 else if (origin
== SEEK_END
)
1130 camlOrigin
= Val_long(2);
1132 caml_failwith("Invalid seek code");
1134 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
,
1135 Ocaml_SEEKFUNCTION
),
1140 if (Is_exception_result(camlResult
))
1141 result
= CURL_SEEKFUNC_FAIL
;
1143 switch (Int_val(camlResult
))
1145 case 0: result
= CURL_SEEKFUNC_OK
; break;
1146 case 1: result
= CURL_SEEKFUNC_FAIL
; break;
1147 case 2: result
= CURL_SEEKFUNC_CANTSEEK
; break;
1148 default: caml_failwith("Invalid seek result");
1152 caml_enter_blocking_section();
1157 static int cb_OPENSOCKETFUNCTION(void *data
,
1158 curlsocktype purpose
,
1159 struct curl_sockaddr
*addr
)
1161 caml_leave_blocking_section();
1165 Connection
*conn
= (Connection
*)data
;
1167 (void)purpose
; /* not used */
1169 sock
= socket(addr
->family
, addr
->socktype
, addr
->protocol
);
1174 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_OPENSOCKETFUNCTION
), Val_int(sock
));
1175 if (Is_exception_result(result
))
1183 caml_enter_blocking_section();
1184 return ((sock
== -1) ? CURL_SOCKET_BAD
: sock
);
1188 static int cb_CLOSESOCKETFUNCTION(void *data,
1189 curl_socket_t socket)
1191 caml_leave_blocking_section();
1194 CAMLlocal1(camlResult);
1195 Connection *conn = (Connection *)data;
1198 camlResult = caml_callback_exn(Field(conn->ocamlValues, Ocaml_CLOSESOCKETFUNCTION), Val_int(socket));
1199 if (Is_exception_result(camlResult))
1205 caml_enter_blocking_section();
1210 static int cb_SSH_KEYFUNCTION(CURL
*easy
,
1211 const struct curl_khkey
*knownkey
,
1212 const struct curl_khkey
*foundkey
,
1213 enum curl_khmatch match
,
1216 caml_leave_blocking_section();
1219 CAMLlocal3(v_found
, v_match
, v_result
);
1220 Connection
*conn
= (Connection
*)clientp
;
1221 int res
= CURLKHSTAT_REJECT
;
1224 case CURLKHMATCH_OK
:
1225 v_match
= Val_int(0);
1227 case CURLKHMATCH_MISMATCH
:
1228 v_match
= caml_alloc_small(1, 0);
1229 Field(v_match
, 0) = ml_copy_string(knownkey
->key
, knownkey
->len
? knownkey
->len
: strlen(knownkey
->key
));
1231 case CURLKHMATCH_MISSING
:
1232 v_match
= Val_int(1);
1235 caml_failwith("Invalid CURL_SSH_KEYFUNCTION argument");
1239 v_found
= ml_copy_string(foundkey
->key
, foundkey
->len
? foundkey
->len
: strlen(foundkey
->key
));
1240 v_result
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_SSH_KEYFUNCTION
), v_match
, v_found
);
1242 if (!Is_exception_result(v_result
)) {
1243 switch (Int_val(v_result
)) {
1245 res
= CURLKHSTAT_FINE_ADD_TO_FILE
;
1248 res
= CURLKHSTAT_FINE
;
1251 res
= CURLKHSTAT_REJECT
;
1254 res
= CURLKHSTAT_DEFER
;
1257 caml_failwith("Invalid CURLOPT_SSH_KEYFUNCTION return value");
1264 caml_enter_blocking_section();
1268 /* Same order as in OCaml */
1269 curl_sslbackend sslBackendMap
[] = {
1270 #if HAVE_DECL_CURLSSLBACKEND_NONE
1271 CURLSSLBACKEND_NONE
,
1275 #if HAVE_DECL_CURLSSLBACKEND_OPENSSL
1276 CURLSSLBACKEND_OPENSSL
,
1280 #if HAVE_DECL_CURLSSLBACKEND_GNUTLS
1281 CURLSSLBACKEND_GNUTLS
,
1285 #if HAVE_DECL_CURLSSLBACKEND_NSS
1290 #if HAVE_DECL_CURLSSLBACKEND_GSKIT
1291 CURLSSLBACKEND_GSKIT
,
1295 #if HAVE_DECL_CURLSSLBACKEND_WOLFSSL
1296 CURLSSLBACKEND_WOLFSSL
,
1300 #if HAVE_DECL_CURLSSLBACKEND_SCHANNEL
1301 CURLSSLBACKEND_SCHANNEL
,
1305 #if HAVE_DECL_CURLSSLBACKEND_SECURETRANSPORT
1306 CURLSSLBACKEND_SECURETRANSPORT
,
1310 #if HAVE_DECL_CURLSSLBACKEND_MBEDTLS
1311 CURLSSLBACKEND_MBEDTLS
,
1315 #if HAVE_DECL_CURLSSLBACKEND_MESALINK
1316 CURLSSLBACKEND_MESALINK
,
1320 #if HAVE_DECL_CURLSSLBACKEND_BEARSSL
1321 CURLSSLBACKEND_BEARSSL
,
1327 #if HAVE_DECL_CURLSSLBACKEND_NONE
1328 /* Same order as in OCaml */
1329 CURLsslset sslsetMap
[] = {
1331 CURLSSLSET_UNKNOWN_BACKEND
,
1332 CURLSSLSET_TOO_LATE
,
1333 CURLSSLSET_NO_BACKENDS
,
1336 static void raiseSslsetError(CURLsslset err
)
1339 const value
*exception
;
1342 for (i
= 0, found
= -1; i
< sizeof(sslsetMap
) / sizeof(sslsetMap
[0]); i
++) {
1343 if (sslsetMap
[i
] == err
) {
1350 caml_invalid_argument("Unexpected CURLsslset value");
1352 exception
= caml_named_value("CurlSslSetException");
1353 if (exception
== NULL
) caml_invalid_argument("CurlSslSetException not registered");
1355 caml_raise_with_arg(*exception
, Val_int(found
));
1361 value
caml_curl_global_sslset(value v_backend
)
1363 CAMLparam1(v_backend
);
1365 curl_sslbackend backend
= sslBackendMap
[Int_val(v_backend
)];
1366 CURLsslset res
= curl_global_sslset(backend
, NULL
, NULL
);
1368 if (res
!= CURLSSLSET_OK
)
1369 raiseSslsetError(res
);
1371 CAMLreturn(Val_unit
);
1374 value
caml_curl_global_sslset_str(value v_backend_str
)
1376 CAMLparam1(v_backend_str
);
1378 CURLsslset res
= curl_global_sslset(-1, String_val(v_backend_str
), NULL
);
1380 if (res
!= CURLSSLSET_OK
)
1381 raiseSslsetError(res
);
1383 CAMLreturn(Val_unit
);
1386 value
caml_curl_global_sslsetavail(value v_unit
)
1390 const curl_ssl_backend
**backends
;
1394 res
= curl_global_sslset(-1, NULL
, &backends
);
1396 if (res
!= CURLSSLSET_UNKNOWN_BACKEND
)
1397 raiseSslsetError(res
);
1399 lst
= Val_emptylist
;
1401 for (i
= 0; backends
[i
] != NULL
; i
++) {
1404 for (j
= 0; j
< sizeof(sslBackendMap
) / sizeof(sslBackendMap
[0]); j
++) {
1405 if (sslBackendMap
[j
] == backends
[i
]->id
) {
1411 /* If an unknown backend is returned, it is ignored */
1413 lst
= Val_cons(lst
, Val_long(found
));
1420 value
caml_curl_global_sslsetavail_str(value v_unit
)
1424 const curl_ssl_backend
**backends
;
1428 res
= curl_global_sslset(-1, NULL
, &backends
);
1430 if (res
!= CURLSSLSET_UNKNOWN_BACKEND
)
1431 raiseSslsetError(res
);
1433 lst
= Val_emptylist
;
1435 for (i
= 0; backends
[i
] != NULL
; i
++) {
1436 lst
= Val_cons(lst
, caml_copy_string(backends
[i
]->name
));
1442 value
caml_curl_global_sslset(value v_ignored
)
1444 const value
*exception
= caml_named_value("Curl.NotImplemented");
1445 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented not registered");
1446 caml_raise_with_string(*exception
, "curl_global_sslset");
1451 value
caml_curl_global_sslset_str(value v_ignored
)
1453 /* Argument is ignored */
1454 return caml_curl_global_sslset(Val_unit
);
1456 value
caml_curl_global_sslsetavail(value v_ignored
)
1458 /* Argument is ignored */
1459 return caml_curl_global_sslset(Val_unit
);
1461 value
caml_curl_global_sslsetavail_str(value v_ignored
)
1463 /* Argument is ignored */
1464 return caml_curl_global_sslset(Val_unit
);
1469 ** curl_global_init helper function
1472 value
caml_curl_global_init(value initOption
)
1474 CAMLparam1(initOption
);
1476 switch (Long_val(initOption
))
1478 case 0: /* CURLINIT_GLOBALALL */
1479 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL
)));
1482 case 1: /* CURLINIT_GLOBALSSL */
1483 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL
)));
1486 case 2: /* CURLINIT_GLOBALWIN32 */
1487 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32
)));
1490 case 3: /* CURLINIT_GLOBALNOTHING */
1491 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING
)));
1495 caml_failwith("Invalid Initialization Option");
1499 /* Keep compiler happy, we should never get here due to caml_failwith() */
1500 CAMLreturn(Val_unit
);
1504 ** curl_global_cleanup helper function
1507 value
caml_curl_global_cleanup(void)
1511 curl_global_cleanup();
1513 CAMLreturn(Val_unit
);
1517 ** curl_easy_init helper function
1519 value
caml_curl_easy_init(void)
1524 result
= caml_curl_alloc(newConnection());
1529 value
caml_curl_easy_reset(value conn
)
1532 Connection
*connection
= Connection_val(conn
);
1534 checkConnection(connection
);
1535 curl_easy_reset(connection
->handle
);
1536 curl_easy_setopt(connection
->handle
, CURLOPT_PRIVATE
, connection
);
1537 resetOcamlValues(connection
);
1539 CAMLreturn(Val_unit
);
1543 ** curl_easy_setopt helper utility functions
1546 #define SETOPT_FUNCTION_(name,suffix) \
1547 static void handle_##name##suffix(Connection *conn, value option) \
1549 CAMLparam1(option); \
1550 CURLcode result = CURLE_OK; \
1551 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1552 result = curl_easy_setopt(conn->handle, CURLOPT_##name##FUNCTION, cb_##name##suffix); \
1553 if (result != CURLE_OK) raiseError(conn, result); \
1554 result = curl_easy_setopt(conn->handle, CURLOPT_##name##DATA, conn); \
1555 if (result != CURLE_OK) raiseError(conn, result); \
1559 #define SETOPT_FUNCTION(name) SETOPT_FUNCTION_(name,FUNCTION)
1560 #define SETOPT_FUNCTION2(name) SETOPT_FUNCTION_(name,FUNCTION2)
1562 SETOPT_FUNCTION( WRITE
)
1563 SETOPT_FUNCTION2( WRITE
)
1564 SETOPT_FUNCTION( READ
)
1565 SETOPT_FUNCTION2( READ
)
1566 SETOPT_FUNCTION( HEADER
)
1567 SETOPT_FUNCTION( PROGRESS
)
1568 SETOPT_FUNCTION( XFERINFO
)
1569 SETOPT_FUNCTION( DEBUG
)
1570 SETOPT_FUNCTION( SSH_KEY
)
1572 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1573 SETOPT_FUNCTION( SEEK
)
1576 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1577 SETOPT_FUNCTION( IOCTL
)
1580 SETOPT_FUNCTION( OPENSOCKET
)
1581 /* SETOPT_FUNCTION( CLOSESOCKET) */
1583 static void handle_slist(Connection
*conn
, struct curl_slist
** slist
, CURLoption curl_option
, value option
)
1586 CURLcode result
= CURLE_OK
;
1588 free_curl_slist(*slist
);
1591 while (Val_emptylist
!= option
)
1593 *slist
= curl_slist_append(*slist
, String_val(Field(option
, 0)));
1595 option
= Field(option
, 1);
1598 result
= curl_easy_setopt(conn
->handle
, curl_option
, *slist
);
1600 if (result
!= CURLE_OK
)
1601 raiseError(conn
, result
);
1606 static long convert_bit_list(long *map
, size_t map_size
, value option
)
1612 while (Val_emptylist
!= option
)
1614 index
= Int_val(Field(option
, 0));
1615 if ((index
< 0) || ((size_t)index
>= map_size
))
1616 caml_invalid_argument("convert_bit_list");
1620 option
= Field(option
, 1);
1623 CAMLreturnT(long, bits
);
1626 #define SETOPT_STRING(name) \
1627 static void handle_##name(Connection *conn, value option) \
1629 CAMLparam1(option); \
1630 CURLcode result = CURLE_OK; \
1632 result = curl_easy_setopt(conn->handle, CURLOPT_##name, String_val(option)); \
1634 if (result != CURLE_OK) \
1635 raiseError(conn, result); \
1640 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1641 static void func_name(Connection *conn, value option) \
1643 CAMLparam1(option); \
1644 CURLcode result = CURLE_OK; \
1646 result = curl_easy_setopt(conn->handle, curl_option, conv_val(option)); \
1648 if (result != CURLE_OK) \
1649 raiseError(conn, result); \
1654 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1655 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1656 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1657 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1659 #if HAVE_DECL_CURLOPT_MIMEPOST
1661 static void handle_part_with_name(Connection
* conn
, curl_mimepart
* part
, value v_part_name_info
)
1663 value v_data
= Field(v_part_name_info
, 0);
1664 value v_name
= Field(v_part_name_info
, 1);
1665 value v_filename
= Field(v_part_name_info
, 2);
1668 value v_str
= Field(v_data
, 0);
1670 switch (Tag_val(v_data
)) {
1672 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1675 result
= curl_mime_filedata(part
, String_val(v_str
));
1678 caml_failwith("Invalid MIMEPOST data value");
1682 if (result
!= CURLE_OK
) {
1683 raiseError(conn
, result
);
1686 if (Is_block(v_name
)) {
1687 result
= curl_mime_name(part
, String_val(Field(v_name
, 0)));
1689 if (result
!= CURLE_OK
) {
1690 raiseError(conn
, result
);
1694 if (Is_block(v_filename
)) {
1695 result
= curl_mime_filename(part
, String_val(Field(v_filename
, 0)));
1697 if (result
!= CURLE_OK
) {
1698 raiseError(conn
, result
);
1703 static void new_part(Connection
* conn
, curl_mime
* mime
, value v_part
)
1705 value v_encoding
= Field(v_part
, 0);
1706 value v_headers
= Field(v_part
, 1);
1707 value v_subparts
= Field(v_part
, 2);
1708 value v_data
= Field(v_part
, 3);
1709 value v_str
= Field(v_data
, 0);
1711 struct curl_slist
*headers
= NULL
;
1714 curl_mimepart
*part
= curl_mime_addpart(mime
);
1716 switch (Int_val(v_encoding
)) {
1718 result
= curl_mime_encoder(part
, "8bit");
1721 result
= curl_mime_encoder(part
, "binary");
1724 result
= curl_mime_encoder(part
, "7bit");
1727 result
= curl_mime_encoder(part
, "quoted-printable");
1730 result
= curl_mime_encoder(part
, "base64");
1736 caml_failwith("Invalid MIMEPOST encoding value");
1740 if (result
!= CURLE_OK
) {
1741 raiseError(conn
, result
);
1744 while (v_headers
!= Val_emptylist
) {
1745 headers
= curl_slist_append(headers
, String_val(Field(v_headers
, 0)));
1746 v_headers
= Field(v_headers
, 1);
1749 result
= curl_mime_headers(part
, headers
, 1);
1751 if (result
!= CURLE_OK
) {
1752 raiseError(conn
, result
);
1755 switch (Tag_val(v_data
)) {
1757 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1760 result
= curl_mime_filedata(part
, String_val(v_str
));
1763 handle_part_with_name(conn
, part
, v_data
);
1766 caml_failwith("Invalid MIMEPOST data value");
1770 if (result
!= CURLE_OK
) {
1771 raiseError(conn
, result
);
1774 if (v_subparts
!= Val_emptylist
) {
1775 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1777 while (v_subparts
!= Val_emptylist
) {
1778 new_part(conn
, mime
, Field(v_subparts
, 0));
1779 v_subparts
= Field(v_subparts
, 1);
1782 result
= curl_mime_subparts(part
, mime
);
1784 if (result
!= CURLE_OK
) {
1785 raiseError(conn
, result
);
1790 static void handle_MIMEPOST(Connection
* conn
, value v_subparts
)
1792 CAMLparam1(v_subparts
);
1793 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1796 curl_mime_free(conn
->curl_MIMEPOST
);
1797 conn
->curl_MIMEPOST
= mime
;
1799 while (v_subparts
!= Val_emptylist
) {
1800 new_part(conn
, mime
, Field(v_subparts
, 0));
1801 v_subparts
= Field(v_subparts
, 1);
1804 result
= curl_easy_setopt(conn
->handle
, CURLOPT_MIMEPOST
, mime
);
1806 if (result
!= CURLE_OK
) {
1807 raiseError(conn
, result
);
1815 #define SETOPT_SLIST(name) \
1816 static void handle_##name(Connection* conn, value option) \
1818 handle_slist(conn,&(conn->curl_##name),CURLOPT_##name,option); \
1822 SETOPT_LONG( INFILESIZE
)
1823 SETOPT_STRING( PROXY
)
1824 SETOPT_LONG( PROXYPORT
)
1825 SETOPT_BOOL( HTTPPROXYTUNNEL
)
1826 SETOPT_BOOL( VERBOSE
)
1827 SETOPT_BOOL( HEADER
)
1828 SETOPT_BOOL( NOPROGRESS
)
1830 #if HAVE_DECL_CURLOPT_NOSIGNAL
1831 SETOPT_BOOL( NOSIGNAL
)
1834 SETOPT_BOOL( NOBODY
)
1835 SETOPT_BOOL( FAILONERROR
)
1836 SETOPT_BOOL( UPLOAD
)
1838 SETOPT_BOOL( FTPLISTONLY
)
1839 SETOPT_BOOL( FTPAPPEND
)
1842 static void handle_NETRC(Connection
*conn
, value option
)
1845 CURLcode result
= CURLE_OK
;
1848 switch (Long_val(option
))
1850 case 0: /* CURL_NETRC_OPTIONAL */
1851 netrc
= CURL_NETRC_OPTIONAL
;
1854 case 1:/* CURL_NETRC_IGNORED */
1855 netrc
= CURL_NETRC_IGNORED
;
1858 case 2: /* CURL_NETRC_REQUIRED */
1859 netrc
= CURL_NETRC_REQUIRED
;
1863 caml_failwith("Invalid NETRC Option");
1867 result
= curl_easy_setopt(conn
->handle
,
1871 if (result
!= CURLE_OK
)
1872 raiseError(conn
, result
);
1877 #if HAVE_DECL_CURLOPT_ENCODING
1878 static void handle_ENCODING(Connection
*conn
, value option
)
1881 CURLcode result
= CURLE_OK
;
1883 switch (Long_val(option
))
1885 case 0: /* CURL_ENCODING_NONE */
1886 result
= curl_easy_setopt(conn
->handle
,
1891 case 1: /* CURL_ENCODING_DEFLATE */
1892 result
= curl_easy_setopt(conn
->handle
,
1897 case 2: /* CURL_ENCODING_GZIP */
1898 result
= curl_easy_setopt(conn
->handle
,
1903 case 3: /* CURL_ENCODING_ANY */
1904 result
= curl_easy_setopt(conn
->handle
,
1910 caml_failwith("Invalid Encoding Option");
1914 if (result
!= CURLE_OK
)
1915 raiseError(conn
, result
);
1922 SETOPT_BOOL( FOLLOWLOCATION
)
1923 SETOPT_BOOL( TRANSFERTEXT
)
1925 SETOPT_STRING( USERPWD
)
1926 SETOPT_STRING( PROXYUSERPWD
)
1927 SETOPT_STRING( RANGE
)
1929 static void handle_ERRORBUFFER(Connection
*conn
, value option
)
1932 CURLcode result
= CURLE_OK
;
1934 Store_field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
, option
);
1936 if (conn
->curl_ERRORBUFFER
!= NULL
)
1937 free(conn
->curl_ERRORBUFFER
);
1939 conn
->curl_ERRORBUFFER
= (char*)malloc(sizeof(char) * CURL_ERROR_SIZE
);
1941 result
= curl_easy_setopt(conn
->handle
,
1942 CURLOPT_ERRORBUFFER
,
1943 conn
->curl_ERRORBUFFER
);
1945 if (result
!= CURLE_OK
)
1946 raiseError(conn
, result
);
1951 SETOPT_LONG( TIMEOUT
)
1953 static void handle_POSTFIELDS(Connection
*conn
, value option
)
1956 CURLcode result
= CURLE_OK
;
1958 if (conn
->curl_POSTFIELDS
!= NULL
)
1959 free(conn
->curl_POSTFIELDS
);
1961 conn
->curl_POSTFIELDS
= strdup_ml(option
);
1963 result
= curl_easy_setopt(conn
->handle
,
1965 conn
->curl_POSTFIELDS
);
1967 if (result
!= CURLE_OK
)
1968 raiseError(conn
, result
);
1973 SETOPT_LONG( POSTFIELDSIZE
)
1974 SETOPT_STRING( REFERER
)
1975 SETOPT_STRING( USERAGENT
)
1976 SETOPT_STRING( FTPPORT
)
1977 SETOPT_LONG( LOW_SPEED_LIMIT
)
1978 SETOPT_LONG( LOW_SPEED_TIME
)
1979 SETOPT_LONG( RESUME_FROM
)
1980 SETOPT_STRING( COOKIE
)
1982 SETOPT_SLIST( HTTPHEADER
)
1984 long sslOptionMap
[] = {
1985 #ifdef CURLSSLOPT_ALLOW_BEAST
1986 CURLSSLOPT_ALLOW_BEAST
,
1990 #ifdef CURLSSLOPT_NO_REVOKE
1991 CURLSSLOPT_NO_REVOKE
,
1995 #ifdef CURLSSLOPT_NO_PARTIALCHAIN
1996 CURLSSLOPT_NO_PARTIALCHAIN
,
2000 #ifdef CURLSSLOPT_REVOKE_BEST_EFFORT
2001 CURLSSLOPT_REVOKE_BEST_EFFORT
,
2005 #ifdef CURLSSLOPT_NATIVE_CA
2006 CURLSSLOPT_NATIVE_CA
,
2010 #ifdef CURLSSLOPT_AUTO_CLIENT_CERT
2011 CURLSSLOPT_AUTO_CLIENT_CERT
,
2017 #if HAVE_DECL_CURLOPT_SSL_OPTIONS
2018 static void handle_SSL_OPTIONS(Connection
*conn
, value opts
)
2021 CURLcode result
= CURLE_OK
;
2022 long bits
= convert_bit_list(sslOptionMap
, sizeof(sslOptionMap
) / sizeof(sslOptionMap
[0]), opts
);
2024 result
= curl_easy_setopt(conn
->handle
, CURLOPT_SSL_OPTIONS
, bits
);
2026 if (result
!= CURLE_OK
)
2027 raiseError(conn
, result
);
2033 static void handle_HTTPPOST(Connection
*conn
, value option
)
2036 CAMLlocal3(listIter
, formItem
, contentType
);
2037 CURLcode result
= CURLE_OK
;
2041 free_curl_slist(conn
->httpPostBuffers
);
2042 if (conn
->httpPostFirst
!= NULL
)
2043 curl_formfree(conn
->httpPostFirst
);
2045 conn
->httpPostBuffers
= NULL
;
2046 conn
->httpPostFirst
= NULL
;
2047 conn
->httpPostLast
= NULL
;
2049 while (!Is_long(listIter
))
2051 formItem
= Field(listIter
, 0);
2053 switch (Tag_val(formItem
))
2055 case 0: /* CURLFORM_CONTENT */
2056 if (Wosize_val(formItem
) < 3)
2058 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
2061 if (Is_long(Field(formItem
, 2)) &&
2062 Long_val(Field(formItem
, 2)) == 0)
2064 curl_formadd(&conn
->httpPostFirst
,
2065 &conn
->httpPostLast
,
2067 String_val(Field(formItem
, 0)),
2068 CURLFORM_NAMELENGTH
,
2069 caml_string_length(Field(formItem
, 0)),
2070 CURLFORM_COPYCONTENTS
,
2071 String_val(Field(formItem
, 1)),
2072 CURLFORM_CONTENTSLENGTH
,
2073 caml_string_length(Field(formItem
, 1)),
2076 else if (Is_block(Field(formItem
, 2)))
2078 contentType
= Field(formItem
, 2);
2080 curl_formadd(&conn
->httpPostFirst
,
2081 &conn
->httpPostLast
,
2083 String_val(Field(formItem
, 0)),
2084 CURLFORM_NAMELENGTH
,
2085 caml_string_length(Field(formItem
, 0)),
2086 CURLFORM_COPYCONTENTS
,
2087 String_val(Field(formItem
, 1)),
2088 CURLFORM_CONTENTSLENGTH
,
2089 caml_string_length(Field(formItem
, 1)),
2090 CURLFORM_CONTENTTYPE
,
2091 String_val(Field(contentType
, 0)),
2096 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
2100 case 1: /* CURLFORM_FILECONTENT */
2101 if (Wosize_val(formItem
) < 3)
2103 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
2106 if (Is_long(Field(formItem
, 2)) &&
2107 Long_val(Field(formItem
, 2)) == 0)
2109 curl_formadd(&conn
->httpPostFirst
,
2110 &conn
->httpPostLast
,
2112 String_val(Field(formItem
, 0)),
2113 CURLFORM_NAMELENGTH
,
2114 caml_string_length(Field(formItem
, 0)),
2115 CURLFORM_FILECONTENT
,
2116 String_val(Field(formItem
, 1)),
2119 else if (Is_block(Field(formItem
, 2)))
2121 contentType
= Field(formItem
, 2);
2123 curl_formadd(&conn
->httpPostFirst
,
2124 &conn
->httpPostLast
,
2126 String_val(Field(formItem
, 0)),
2127 CURLFORM_NAMELENGTH
,
2128 caml_string_length(Field(formItem
, 0)),
2129 CURLFORM_FILECONTENT
,
2130 String_val(Field(formItem
, 1)),
2131 CURLFORM_CONTENTTYPE
,
2132 String_val(Field(contentType
, 0)),
2137 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
2141 case 2: /* CURLFORM_FILE */
2142 if (Wosize_val(formItem
) < 3)
2144 caml_failwith("Incorrect CURLFORM_FILE parameters");
2147 if (Is_long(Field(formItem
, 2)) &&
2148 Long_val(Field(formItem
, 2)) == 0)
2150 curl_formadd(&conn
->httpPostFirst
,
2151 &conn
->httpPostLast
,
2153 String_val(Field(formItem
, 0)),
2154 CURLFORM_NAMELENGTH
,
2155 caml_string_length(Field(formItem
, 0)),
2157 String_val(Field(formItem
, 1)),
2160 else if (Is_block(Field(formItem
, 2)))
2162 contentType
= Field(formItem
, 2);
2164 curl_formadd(&conn
->httpPostFirst
,
2165 &conn
->httpPostLast
,
2167 String_val(Field(formItem
, 0)),
2168 CURLFORM_NAMELENGTH
,
2169 caml_string_length(Field(formItem
, 0)),
2171 String_val(Field(formItem
, 1)),
2172 CURLFORM_CONTENTTYPE
,
2173 String_val(Field(contentType
, 0)),
2178 caml_failwith("Incorrect CURLFORM_FILE parameters");
2182 case 3: /* CURLFORM_BUFFER */
2183 if (Wosize_val(formItem
) < 4)
2185 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
2188 if (Is_long(Field(formItem
, 3)) &&
2189 Long_val(Field(formItem
, 3)) == 0)
2191 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
2193 curl_formadd(&conn
->httpPostFirst
,
2194 &conn
->httpPostLast
,
2196 String_val(Field(formItem
, 0)),
2197 CURLFORM_NAMELENGTH
,
2198 caml_string_length(Field(formItem
, 0)),
2200 String_val(Field(formItem
, 1)),
2202 conn
->httpPostBuffers
->data
,
2203 CURLFORM_BUFFERLENGTH
,
2204 caml_string_length(Field(formItem
, 2)),
2207 else if (Is_block(Field(formItem
, 3)))
2209 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
2211 contentType
= Field(formItem
, 3);
2213 curl_formadd(&conn
->httpPostFirst
,
2214 &conn
->httpPostLast
,
2216 String_val(Field(formItem
, 0)),
2217 CURLFORM_NAMELENGTH
,
2218 caml_string_length(Field(formItem
, 0)),
2220 String_val(Field(formItem
, 1)),
2222 conn
->httpPostBuffers
->data
,
2223 CURLFORM_BUFFERLENGTH
,
2224 caml_string_length(Field(formItem
, 2)),
2225 CURLFORM_CONTENTTYPE
,
2226 String_val(Field(contentType
, 0)),
2231 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
2236 listIter
= Field(listIter
, 1);
2239 result
= curl_easy_setopt(conn
->handle
,
2241 conn
->httpPostFirst
);
2243 if (result
!= CURLE_OK
)
2244 raiseError(conn
, result
);
2249 SETOPT_STRING( SSLCERT
)
2250 SETOPT_STRING( SSLCERTTYPE
)
2251 SETOPT_STRING( SSLCERTPASSWD
)
2252 SETOPT_STRING( SSLKEY
)
2253 SETOPT_STRING( SSLKEYTYPE
)
2254 SETOPT_STRING( SSLKEYPASSWD
)
2255 SETOPT_STRING( SSLENGINE
)
2256 SETOPT_BOOL( SSLENGINE_DEFAULT
)
2259 SETOPT_SLIST( QUOTE
)
2260 SETOPT_SLIST( POSTQUOTE
)
2262 SETOPT_STRING( COOKIEFILE
)
2263 #if HAVE_DECL_CURLOPT_CERTINFO
2264 SETOPT_BOOL( CERTINFO
)
2267 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_0
2268 #define CURL_SSLVERSION_TLSv1_0 CURL_SSLVERSION_TLSv1
2271 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_1
2272 #define CURL_SSLVERSION_TLSv1_1 CURL_SSLVERSION_TLSv1
2275 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_2
2276 #define CURL_SSLVERSION_TLSv1_2 CURL_SSLVERSION_TLSv1
2279 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_3
2280 #define CURL_SSLVERSION_TLSv1_3 CURL_SSLVERSION_TLSv1
2283 static void handle_SSLVERSION(Connection
*conn
, value option
)
2286 CURLcode result
= CURLE_OK
;
2287 int v
= CURL_SSLVERSION_DEFAULT
;
2289 switch (Long_val(option
))
2291 case 0: v
= CURL_SSLVERSION_DEFAULT
; break;
2292 case 1: v
= CURL_SSLVERSION_TLSv1
; break;
2293 case 2: v
= CURL_SSLVERSION_SSLv2
; break;
2294 case 3: v
= CURL_SSLVERSION_SSLv3
; break;
2295 case 4: v
= CURL_SSLVERSION_TLSv1_0
; break;
2296 case 5: v
= CURL_SSLVERSION_TLSv1_1
; break;
2297 case 6: v
= CURL_SSLVERSION_TLSv1_2
; break;
2298 case 7: v
= CURL_SSLVERSION_TLSv1_3
; break;
2300 caml_failwith("Invalid SSLVERSION Option");
2304 result
= curl_easy_setopt(conn
->handle
, CURLOPT_SSLVERSION
, v
);
2306 if (result
!= CURLE_OK
)
2307 raiseError(conn
, result
);
2312 static void handle_TIMECONDITION(Connection
*conn
, value option
)
2315 CURLcode result
= CURLE_OK
;
2316 int timecond
= CURL_TIMECOND_NONE
;
2318 switch (Long_val(option
))
2320 case 0: timecond
= CURL_TIMECOND_NONE
; break;
2321 case 1: timecond
= CURL_TIMECOND_IFMODSINCE
; break;
2322 case 2: timecond
= CURL_TIMECOND_IFUNMODSINCE
; break;
2323 case 3: timecond
= CURL_TIMECOND_LASTMOD
; break;
2325 caml_failwith("Invalid TIMECOND Option");
2329 result
= curl_easy_setopt(conn
->handle
, CURLOPT_TIMECONDITION
, timecond
);
2331 if (result
!= CURLE_OK
)
2332 raiseError(conn
, result
);
2337 SETOPT_VAL( TIMEVALUE
, Int32_val
)
2338 SETOPT_STRING( CUSTOMREQUEST
)
2339 SETOPT_STRING( INTERFACE
)
2341 static void handle_KRB4LEVEL(Connection
*conn
, value option
)
2344 CURLcode result
= CURLE_OK
;
2346 switch (Long_val(option
))
2348 case 0: /* KRB4_NONE */
2349 result
= curl_easy_setopt(conn
->handle
,
2354 case 1: /* KRB4_CLEAR */
2355 result
= curl_easy_setopt(conn
->handle
,
2360 case 2: /* KRB4_SAFE */
2361 result
= curl_easy_setopt(conn
->handle
,
2366 case 3: /* KRB4_CONFIDENTIAL */
2367 result
= curl_easy_setopt(conn
->handle
,
2372 case 4: /* KRB4_PRIVATE */
2373 result
= curl_easy_setopt(conn
->handle
,
2379 caml_failwith("Invalid KRB4 Option");
2383 if (result
!= CURLE_OK
)
2384 raiseError(conn
, result
);
2389 SETOPT_BOOL( SSL_VERIFYPEER
)
2390 SETOPT_STRING( CAINFO
)
2391 SETOPT_STRING( CAPATH
)
2392 SETOPT_BOOL( FILETIME
)
2393 SETOPT_LONG( MAXREDIRS
)
2394 SETOPT_LONG( MAXCONNECTS
)
2396 static void handle_CLOSEPOLICY(Connection
*conn
, value option
)
2399 CURLcode result
= CURLE_OK
;
2401 switch (Long_val(option
))
2403 case 0: /* CLOSEPOLICY_OLDEST */
2404 result
= curl_easy_setopt(conn
->handle
,
2405 CURLOPT_CLOSEPOLICY
,
2406 CURLCLOSEPOLICY_OLDEST
);
2409 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
2410 result
= curl_easy_setopt(conn
->handle
,
2411 CURLOPT_CLOSEPOLICY
,
2412 CURLCLOSEPOLICY_LEAST_RECENTLY_USED
);
2416 caml_failwith("Invalid CLOSEPOLICY Option");
2420 if (result
!= CURLE_OK
)
2421 raiseError(conn
, result
);
2426 SETOPT_BOOL( FRESH_CONNECT
)
2427 SETOPT_BOOL( FORBID_REUSE
)
2428 SETOPT_STRING( RANDOM_FILE
)
2429 SETOPT_STRING( EGDSOCKET
)
2430 SETOPT_LONG( CONNECTTIMEOUT
)
2431 SETOPT_BOOL( HTTPGET
)
2433 static void handle_SSL_VERIFYHOST(Connection
*conn
, value option
)
2436 CURLcode result
= CURLE_OK
;
2438 switch (Long_val(option
))
2440 case 0: /* SSLVERIFYHOST_NONE */
2441 case 1: /* SSLVERIFYHOST_EXISTENCE */
2442 case 2: /* SSLVERIFYHOST_HOSTNAME */
2443 result
= curl_easy_setopt(conn
->handle
,
2444 CURLOPT_SSL_VERIFYHOST
,
2445 /* map EXISTENCE to HOSTNAME */
2446 Long_val(option
) == 0 ? 0 : 2);
2450 caml_failwith("Invalid SSLVERIFYHOST Option");
2454 if (result
!= CURLE_OK
)
2455 raiseError(conn
, result
);
2460 SETOPT_STRING( COOKIEJAR
)
2461 SETOPT_STRING( SSL_CIPHER_LIST
)
2463 static void handle_HTTP_VERSION(Connection
*conn
, value option
)
2466 CURLcode result
= CURLE_OK
;
2468 long version
= CURL_HTTP_VERSION_NONE
;
2470 switch (Long_val(option
))
2472 case 0: version
= CURL_HTTP_VERSION_NONE
; break;
2473 case 1: version
= CURL_HTTP_VERSION_1_0
; break;
2474 case 2: version
= CURL_HTTP_VERSION_1_1
; break;
2476 #if HAVE_DECL_CURL_HTTP_VERSION_2
2477 version
= CURL_HTTP_VERSION_2
;
2478 #elif HAVE_DECL_CURL_HTTP_VERSION_2_0
2479 version
= CURL_HTTP_VERSION_2_0
;
2483 #if HAVE_DECL_CURL_HTTP_VERSION_2TLS
2484 version
= CURL_HTTP_VERSION_2TLS
;
2488 #if HAVE_DECL_CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
2489 version
= CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
;
2493 #if HAVE_DECL_CURL_HTTP_VERSION_3
2494 version
= CURL_HTTP_VERSION_3
;
2497 /* sync check_enums */
2499 caml_invalid_argument("CURLOPT_HTTP_VERSION");
2503 result
= curl_easy_setopt(conn
->handle
, CURLOPT_HTTP_VERSION
, version
);
2505 if (result
!= CURLE_OK
)
2506 raiseError(conn
, result
);
2511 static long ocaml_HTTP_VERSION(long curl_version
)
2513 switch (curl_version
)
2515 case CURL_HTTP_VERSION_NONE
: return 0;
2516 case CURL_HTTP_VERSION_1_0
: return 1;
2517 case CURL_HTTP_VERSION_1_1
: return 2;
2518 #if HAVE_DECL_CURL_HTTP_VERSION_2
2519 case CURL_HTTP_VERSION_2
: return 3;
2520 #elif HAVE_DECL_CURL_HTTP_VERSION_2_0
2521 case CURL_HTTP_VERSION_2_0
: return 3;
2523 #if HAVE_DECL_CURL_HTTP_VERSION_2TLS
2524 case CURL_HTTP_VERSION_2TLS
: return 4;
2526 #if HAVE_DECL_CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
2527 case CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
: return 5;
2529 #if HAVE_DECL_CURL_HTTP_VERSION_3
2530 case CURL_HTTP_VERSION_3
: return 6;
2532 /* sync check_enums */
2537 SETOPT_BOOL( FTP_USE_EPSV
)
2538 SETOPT_LONG( DNS_CACHE_TIMEOUT
)
2539 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE
)
2541 static void handle_PRIVATE(Connection
*conn
, value option
)
2544 Store_field(conn
->ocamlValues
, Ocaml_PRIVATE
, option
);
2548 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2549 SETOPT_SLIST( HTTP200ALIASES
)
2552 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2553 SETOPT_BOOL( UNRESTRICTED_AUTH
)
2556 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2557 SETOPT_BOOL( FTP_USE_EPRT
)
2560 #if HAVE_DECL_CURLOPT_HTTPAUTH
2561 static void handle_HTTPAUTH(Connection
*conn
, value option
)
2564 CAMLlocal1(listIter
);
2565 CURLcode result
= CURLE_OK
;
2566 long auth
= CURLAUTH_NONE
;
2570 while (!Is_long(listIter
))
2572 switch (Long_val(Field(listIter
, 0)))
2574 case 0: /* CURLAUTH_BASIC */
2575 auth
|= CURLAUTH_BASIC
;
2578 case 1: /* CURLAUTH_DIGEST */
2579 auth
|= CURLAUTH_DIGEST
;
2582 case 2: /* CURLAUTH_GSSNEGOTIATE */
2583 auth
|= CURLAUTH_GSSNEGOTIATE
;
2586 case 3: /* CURLAUTH_NTLM */
2587 auth
|= CURLAUTH_NTLM
;
2590 case 4: /* CURLAUTH_ANY */
2591 auth
|= CURLAUTH_ANY
;
2594 case 5: /* CURLAUTH_ANYSAFE */
2595 auth
|= CURLAUTH_ANYSAFE
;
2599 caml_failwith("Invalid HTTPAUTH Value");
2603 listIter
= Field(listIter
, 1);
2606 result
= curl_easy_setopt(conn
->handle
,
2610 if (result
!= CURLE_OK
)
2611 raiseError(conn
, result
);
2617 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2618 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS
)
2621 #if HAVE_DECL_CURLOPT_PROXYAUTH
2622 static void handle_PROXYAUTH(Connection
*conn
, value option
)
2625 CAMLlocal1(listIter
);
2626 CURLcode result
= CURLE_OK
;
2627 long auth
= CURLAUTH_NONE
;
2631 while (!Is_long(listIter
))
2633 switch (Long_val(Field(listIter
, 0)))
2635 case 0: /* CURLAUTH_BASIC */
2636 auth
|= CURLAUTH_BASIC
;
2639 case 1: /* CURLAUTH_DIGEST */
2640 auth
|= CURLAUTH_DIGEST
;
2643 case 2: /* CURLAUTH_GSSNEGOTIATE */
2644 auth
|= CURLAUTH_GSSNEGOTIATE
;
2647 case 3: /* CURLAUTH_NTLM */
2648 auth
|= CURLAUTH_NTLM
;
2651 case 4: /* CURLAUTH_ANY */
2652 auth
|= CURLAUTH_ANY
;
2655 case 5: /* CURLAUTH_ANYSAFE */
2656 auth
|= CURLAUTH_ANYSAFE
;
2660 caml_failwith("Invalid HTTPAUTH Value");
2664 listIter
= Field(listIter
, 1);
2667 result
= curl_easy_setopt(conn
->handle
,
2671 if (result
!= CURLE_OK
)
2672 raiseError(conn
, result
);
2678 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2679 SETOPT_LONG( FTP_RESPONSE_TIMEOUT
)
2682 #if HAVE_DECL_CURLOPT_IPRESOLVE
2683 static void handle_IPRESOLVE(Connection
*conn
, value option
)
2686 CURLcode result
= CURLE_OK
;
2688 switch (Long_val(option
))
2690 case 0: /* CURL_IPRESOLVE_WHATEVER */
2691 result
= curl_easy_setopt(conn
->handle
,
2693 CURL_IPRESOLVE_WHATEVER
);
2696 case 1: /* CURL_IPRESOLVE_V4 */
2697 result
= curl_easy_setopt(conn
->handle
,
2702 case 2: /* CURL_IPRESOLVE_V6 */
2703 result
= curl_easy_setopt(conn
->handle
,
2709 caml_failwith("Invalid IPRESOLVE Value");
2713 if (result
!= CURLE_OK
)
2714 raiseError(conn
, result
);
2720 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2721 SETOPT_VAL( MAXFILESIZE
, Int32_val
)
2724 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2725 SETOPT_INT64( INFILESIZE_LARGE
)
2728 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2729 SETOPT_INT64( RESUME_FROM_LARGE
)
2732 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2733 SETOPT_INT64( MAXFILESIZE_LARGE
)
2736 #if HAVE_DECL_CURLOPT_NETRC_FILE
2737 SETOPT_STRING( NETRC_FILE
)
2740 #if HAVE_DECL_CURLOPT_FTP_SSL
2741 static void handle_FTP_SSL(Connection
*conn
, value option
)
2744 CURLcode result
= CURLE_OK
;
2746 switch (Long_val(option
))
2748 case 0: /* CURLFTPSSL_NONE */
2749 result
= curl_easy_setopt(conn
->handle
,
2754 case 1: /* CURLFTPSSL_TRY */
2755 result
= curl_easy_setopt(conn
->handle
,
2760 case 2: /* CURLFTPSSL_CONTROL */
2761 result
= curl_easy_setopt(conn
->handle
,
2763 CURLFTPSSL_CONTROL
);
2766 case 3: /* CURLFTPSSL_ALL */
2767 result
= curl_easy_setopt(conn
->handle
,
2773 caml_failwith("Invalid FTP_SSL Value");
2777 if (result
!= CURLE_OK
)
2778 raiseError(conn
, result
);
2784 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2785 SETOPT_INT64( POSTFIELDSIZE_LARGE
)
2788 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2789 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2790 SETOPT_VAL_( handle_TCP_NODELAY
, CURLOPT_TCP_NODELAY
, Bool_val
)
2793 #if HAVE_DECL_CURLOPT_TCP_FASTOPEN
2794 SETOPT_BOOL( TCP_FASTOPEN
)
2797 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2798 static void handle_FTPSSLAUTH(Connection
*conn
, value option
)
2801 CURLcode result
= CURLE_OK
;
2803 switch (Long_val(option
))
2805 case 0: /* CURLFTPAUTH_DEFAULT */
2806 result
= curl_easy_setopt(conn
->handle
,
2808 CURLFTPAUTH_DEFAULT
);
2811 case 1: /* CURLFTPAUTH_SSL */
2812 result
= curl_easy_setopt(conn
->handle
,
2817 case 2: /* CURLFTPAUTH_TLS */
2818 result
= curl_easy_setopt(conn
->handle
,
2824 caml_failwith("Invalid FTPSSLAUTH value");
2828 if (result
!= CURLE_OK
)
2829 raiseError(conn
, result
);
2835 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2836 SETOPT_STRING( FTP_ACCOUNT
)
2839 #if HAVE_DECL_CURLOPT_COOKIELIST
2840 SETOPT_STRING( COOKIELIST
)
2843 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2844 SETOPT_BOOL( IGNORE_CONTENT_LENGTH
)
2847 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2848 SETOPT_BOOL( FTP_SKIP_PASV_IP
)
2851 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2852 static void handle_FTP_FILEMETHOD(Connection
*conn
, value option
)
2855 CURLcode result
= CURLE_OK
;
2857 switch (Long_val(option
))
2859 case 0: /* CURLFTPMETHOD_DEFAULT */
2860 result
= curl_easy_setopt(conn
->handle
,
2861 CURLOPT_FTP_FILEMETHOD
,
2862 CURLFTPMETHOD_DEFAULT
);
2865 case 1: /* CURLFTMETHOD_MULTICWD */
2866 result
= curl_easy_setopt(conn
->handle
,
2867 CURLOPT_FTP_FILEMETHOD
,
2868 CURLFTPMETHOD_MULTICWD
);
2871 case 2: /* CURLFTPMETHOD_NOCWD */
2872 result
= curl_easy_setopt(conn
->handle
,
2873 CURLOPT_FTP_FILEMETHOD
,
2874 CURLFTPMETHOD_NOCWD
);
2877 case 3: /* CURLFTPMETHOD_SINGLECWD */
2878 result
= curl_easy_setopt(conn
->handle
,
2879 CURLOPT_FTP_FILEMETHOD
,
2880 CURLFTPMETHOD_SINGLECWD
);
2883 caml_failwith("Invalid FTP_FILEMETHOD value");
2887 if (result
!= CURLE_OK
)
2888 raiseError(conn
, result
);
2894 #if HAVE_DECL_CURLOPT_LOCALPORT
2895 SETOPT_LONG( LOCALPORT
)
2898 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2899 SETOPT_LONG( LOCALPORTRANGE
)
2902 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2903 SETOPT_BOOL( CONNECT_ONLY
)
2906 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2907 SETOPT_INT64( MAX_SEND_SPEED_LARGE
)
2910 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2911 SETOPT_INT64( MAX_RECV_SPEED_LARGE
)
2914 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2915 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER
)
2918 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2919 SETOPT_BOOL( SSL_SESSIONID_CACHE
)
2922 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2923 static void handle_SSH_AUTH_TYPES(Connection
*conn
, value option
)
2926 CAMLlocal1(listIter
);
2927 CURLcode result
= CURLE_OK
;
2928 long authTypes
= CURLSSH_AUTH_NONE
;
2932 while (!Is_long(listIter
))
2934 switch (Long_val(Field(listIter
, 0)))
2936 case 0: /* CURLSSH_AUTH_ANY */
2937 authTypes
|= CURLSSH_AUTH_ANY
;
2940 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2941 authTypes
|= CURLSSH_AUTH_PUBLICKEY
;
2944 case 2: /* CURLSSH_AUTH_PASSWORD */
2945 authTypes
|= CURLSSH_AUTH_PASSWORD
;
2948 case 3: /* CURLSSH_AUTH_HOST */
2949 authTypes
|= CURLSSH_AUTH_HOST
;
2952 case 4: /* CURLSSH_AUTH_KEYBOARD */
2953 authTypes
|= CURLSSH_AUTH_KEYBOARD
;
2957 caml_failwith("Invalid CURLSSH_AUTH_TYPES Value");
2961 listIter
= Field(listIter
, 1);
2964 result
= curl_easy_setopt(conn
->handle
,
2965 CURLOPT_SSH_AUTH_TYPES
,
2968 if (result
!= CURLE_OK
)
2969 raiseError(conn
, result
);
2975 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2976 SETOPT_STRING( SSH_PUBLIC_KEYFILE
)
2979 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2980 SETOPT_STRING( SSH_PRIVATE_KEYFILE
)
2983 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2984 static void handle_FTP_SSL_CCC(Connection
*conn
, value option
)
2987 CURLcode result
= CURLE_OK
;
2989 switch (Long_val(option
))
2991 case 0: /* CURLFTPSSL_CCC_NONE */
2992 result
= curl_easy_setopt(conn
->handle
,
2993 CURLOPT_FTP_SSL_CCC
,
2994 CURLFTPSSL_CCC_NONE
);
2997 case 1: /* CURLFTPSSL_CCC_PASSIVE */
2998 result
= curl_easy_setopt(conn
->handle
,
2999 CURLOPT_FTP_SSL_CCC
,
3000 CURLFTPSSL_CCC_PASSIVE
);
3003 case 2: /* CURLFTPSSL_CCC_ACTIVE */
3004 result
= curl_easy_setopt(conn
->handle
,
3005 CURLOPT_FTP_SSL_CCC
,
3006 CURLFTPSSL_CCC_ACTIVE
);
3010 caml_failwith("Invalid FTPSSL_CCC value");
3014 if (result
!= CURLE_OK
)
3015 raiseError(conn
, result
);
3021 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3022 SETOPT_LONG( TIMEOUT_MS
)
3025 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3026 SETOPT_LONG( CONNECTTIMEOUT_MS
)
3029 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3030 SETOPT_BOOL( HTTP_TRANSFER_DECODING
)
3033 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3034 SETOPT_BOOL( HTTP_CONTENT_DECODING
)
3037 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3038 SETOPT_LONG( NEW_FILE_PERMS
)
3041 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3042 SETOPT_LONG( NEW_DIRECTORY_PERMS
)
3045 #if HAVE_DECL_CURLOPT_POST301
3046 SETOPT_BOOL( POST301
)
3049 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3050 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5
)
3053 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3054 SETOPT_STRING( COPYPOSTFIELDS
)
3057 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3058 SETOPT_BOOL( PROXY_TRANSFER_MODE
)
3061 #if HAVE_DECL_CURLOPT_AUTOREFERER
3062 SETOPT_BOOL( AUTOREFERER
)
3065 #if HAVE_DECL_CURLOPT_PROXYTYPE
3066 static void handle_PROXYTYPE(Connection
*conn
, value option
)
3069 CURLcode result
= CURLE_OK
;
3072 switch (Long_val(option
))
3074 case 0: proxy_type
= CURLPROXY_HTTP
; break;
3075 case 1: proxy_type
= CURLPROXY_HTTP_1_0
; break;
3076 case 2: proxy_type
= CURLPROXY_SOCKS4
; break;
3077 case 3: proxy_type
= CURLPROXY_SOCKS5
; break;
3078 case 4: proxy_type
= CURLPROXY_SOCKS4A
; break;
3079 case 5: proxy_type
= CURLPROXY_SOCKS5_HOSTNAME
; break;
3081 caml_failwith("Invalid curl proxy type");
3084 result
= curl_easy_setopt(conn
->handle
,
3088 if (result
!= CURLE_OK
)
3089 raiseError(conn
, result
);
3095 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3100 CURLPROTO_HTTP
, CURLPROTO_HTTPS
, CURLPROTO_FTP
, CURLPROTO_FTPS
, CURLPROTO_SCP
, CURLPROTO_SFTP
,
3101 CURLPROTO_TELNET
, CURLPROTO_LDAP
, CURLPROTO_LDAPS
, CURLPROTO_DICT
, CURLPROTO_FILE
, CURLPROTO_TFTP
,
3102 /* factor out with autoconf? */
3103 #if defined(CURLPROTO_IMAP)
3108 #if defined(CURLPROTO_IMAPS)
3113 #if defined(CURLPROTO_POP3)
3118 #if defined(CURLPROTO_POP3S)
3123 #if defined(CURLPROTO_SMTP)
3128 #if defined(CURLPROTO_SMTPS)
3133 #if defined(CURLPROTO_RTSP)
3138 #if defined(CURLPROTO_RTMP)
3143 #if defined(CURLPROTO_RTMPT)
3148 #if defined(CURLPROTO_RTMPE)
3153 #if defined(CURLPROTO_RTMPTE)
3158 #if defined(CURLPROTO_RTMPS)
3163 #if defined(CURLPROTO_RTMPTS)
3168 #if defined(CURLPROTO_GOPHER)
3175 static void handle_PROTOCOLSOPTION(CURLoption curlopt
, Connection
*conn
, value option
)
3178 CURLcode result
= CURLE_OK
;
3179 long bits
= convert_bit_list(protoMap
, sizeof(protoMap
) / sizeof(protoMap
[0]), option
);
3181 result
= curl_easy_setopt(conn
->handle
, curlopt
, bits
);
3183 if (result
!= CURLE_OK
)
3184 raiseError(conn
, result
);
3190 #if HAVE_DECL_CURLOPT_PROTOCOLS
3191 static void handle_PROTOCOLS(Connection
*conn
, value option
)
3193 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS
, conn
, option
);
3197 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3198 static void handle_REDIR_PROTOCOLS(Connection
*conn
, value option
)
3200 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS
, conn
, option
);
3204 #if HAVE_DECL_CURLOPT_RESOLVE
3205 SETOPT_SLIST( RESOLVE
)
3208 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3209 SETOPT_STRING( DNS_SERVERS
)
3212 #if HAVE_DECL_CURLOPT_MAIL_FROM
3213 SETOPT_STRING( MAIL_FROM
)
3216 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3217 SETOPT_SLIST( MAIL_RCPT
)
3220 #if HAVE_DECL_CURLOPT_PIPEWAIT
3221 SETOPT_BOOL( PIPEWAIT
)
3224 #if HAVE_DECL_CURLOPT_USERNAME
3225 SETOPT_STRING( USERNAME
)
3228 #if HAVE_DECL_CURLOPT_PASSWORD
3229 SETOPT_STRING( PASSWORD
)
3232 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
3233 SETOPT_STRING( LOGIN_OPTIONS
)
3236 #if HAVE_DECL_CURLOPT_CONNECT_TO
3237 SETOPT_SLIST( CONNECT_TO
)
3240 #if HAVE_DECL_CURLOPT_POSTREDIR
3242 static int curlPostRedir_table
[] = {
3243 CURL_REDIR_POST_ALL
,
3244 #if defined(CURL_REDIR_POST_301)
3245 CURL_REDIR_POST_301
,
3249 #if defined(CURL_REDIR_POST_302)
3250 CURL_REDIR_POST_302
,
3254 #if defined(CURL_REDIR_POST_303)
3255 CURL_REDIR_POST_303
,
3261 static void handle_POSTREDIR(Connection
*conn
, value option
)
3264 CURLcode result
= CURLE_OK
;
3265 long bitmask
= caml_convert_flag_list(option
, curlPostRedir_table
);
3267 result
= curl_easy_setopt(conn
->handle
,
3271 if (result
!= CURLE_OK
)
3272 raiseError(conn
, result
);
3278 SETOPT_VAL( SSH_KNOWNHOSTS
, String_val
)
3280 SETOPT_LONG( BUFFERSIZE
)
3282 #if HAVE_DECL_CURLOPT_DOH_URL
3283 SETOPT_STRING( DOH_URL
)
3288 ** curl_easy_setopt helper function
3291 #define HAVE(name) { handle_ ## name, "CURLOPT_"#name }
3292 #define HAVENOT(name) { NULL, "CURLOPT_"#name }
3294 CURLOptionMapping implementedOptionMap
[] =
3296 HAVE(WRITEFUNCTION
),
3302 HAVE(HTTPPROXYTUNNEL
),
3306 #if HAVE_DECL_CURLOPT_NOSIGNAL
3318 #if HAVE_DECL_CURLOPT_ENCODING
3323 HAVE(FOLLOWLOCATION
),
3329 HAVE(ERRORBUFFER
), /* mutable buffer, as output value, do not duplicate */
3332 HAVE(POSTFIELDSIZE
),
3336 HAVE(LOW_SPEED_LIMIT
),
3337 HAVE(LOW_SPEED_TIME
),
3344 HAVE(SSLCERTPASSWD
),
3349 HAVE(SSLENGINE_DEFAULT
),
3353 HAVE(HEADERFUNCTION
),
3356 HAVE(TIMECONDITION
),
3358 HAVE(CUSTOMREQUEST
),
3361 HAVE(PROGRESSFUNCTION
),
3362 HAVE(SSL_VERIFYPEER
),
3369 HAVE(FRESH_CONNECT
),
3373 HAVE(CONNECTTIMEOUT
),
3375 HAVE(SSL_VERIFYHOST
),
3377 HAVE(SSL_CIPHER_LIST
),
3380 HAVE(DNS_CACHE_TIMEOUT
),
3381 HAVE(DNS_USE_GLOBAL_CACHE
),
3382 HAVE(DEBUGFUNCTION
),
3384 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
3385 HAVE(HTTP200ALIASES
),
3387 HAVENOT(HTTP200ALIASES
),
3389 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
3390 HAVE(UNRESTRICTED_AUTH
),
3392 HAVENOT(UNRESTRICTED_AUTH
),
3394 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
3397 HAVENOT(FTP_USE_EPRT
),
3399 #if HAVE_DECL_CURLOPT_HTTPAUTH
3404 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
3405 HAVE(FTP_CREATE_MISSING_DIRS
),
3407 HAVENOT(FTP_CREATE_MISSING_DIRS
),
3409 #if HAVE_DECL_CURLOPT_PROXYAUTH
3414 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
3415 HAVE(FTP_RESPONSE_TIMEOUT
),
3417 HAVENOT(FTP_RESPONSE_TIMEOUT
),
3419 #if HAVE_DECL_CURLOPT_IPRESOLVE
3424 #if HAVE_DECL_CURLOPT_MAXFILESIZE
3427 HAVENOT(MAXFILESIZE
),
3429 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
3430 HAVE(INFILESIZE_LARGE
),
3432 HAVENOT(INFILESIZE_LARGE
),
3434 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
3435 HAVE(RESUME_FROM_LARGE
),
3437 HAVENOT(RESUME_FROM_LARGE
),
3439 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
3440 HAVE(MAXFILESIZE_LARGE
),
3442 HAVENOT(MAXFILESIZE_LARGE
),
3444 #if HAVE_DECL_CURLOPT_NETRC_FILE
3447 HAVENOT(NETRC_FILE
),
3449 #if HAVE_DECL_CURLOPT_FTP_SSL
3454 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
3455 HAVE(POSTFIELDSIZE_LARGE
),
3457 HAVENOT(POSTFIELDSIZE_LARGE
),
3459 #if HAVE_DECL_CURLOPT_TCP_NODELAY
3462 HAVENOT(TCP_NODELAY
),
3464 #if HAVE_DECL_CURLOPT_TCP_FASTOPEN
3467 HAVENOT(TCP_FASTOPEN
),
3469 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
3472 HAVENOT(FTPSSLAUTH
),
3474 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
3475 HAVE(IOCTLFUNCTION
),
3477 HAVENOT(IOCTLFUNCTION
),
3479 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
3482 HAVENOT(FTP_ACCOUNT
),
3484 #if HAVE_DECL_CURLOPT_COOKIELIST
3487 HAVENOT(COOKIELIST
),
3489 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
3490 HAVE(IGNORE_CONTENT_LENGTH
),
3492 HAVENOT(IGNORE_CONTENT_LENGTH
),
3494 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
3495 HAVE(FTP_SKIP_PASV_IP
),
3497 HAVENOT(FTP_SKIP_PASV_IP
),
3499 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
3500 HAVE(FTP_FILEMETHOD
),
3502 HAVENOT(FTP_FILEMETHOD
),
3504 #if HAVE_DECL_CURLOPT_LOCALPORT
3509 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
3510 HAVE(LOCALPORTRANGE
),
3512 HAVENOT(LOCALPORTRANGE
),
3514 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
3517 HAVENOT(CONNECT_ONLY
),
3519 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
3520 HAVE(MAX_SEND_SPEED_LARGE
),
3522 HAVENOT(MAX_SEND_SPEED_LARGE
),
3524 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
3525 HAVE(MAX_RECV_SPEED_LARGE
),
3527 HAVENOT(MAX_RECV_SPEED_LARGE
),
3529 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
3530 HAVE(FTP_ALTERNATIVE_TO_USER
),
3532 HAVENOT(FTP_ALTERNATIVE_TO_USER
),
3534 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
3535 HAVE(SSL_SESSIONID_CACHE
),
3537 HAVENOT(SSL_SESSIONID_CACHE
),
3539 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
3540 HAVE(SSH_AUTH_TYPES
),
3542 HAVENOT(SSH_AUTH_TYPES
),
3544 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
3545 HAVE(SSH_PUBLIC_KEYFILE
),
3547 HAVENOT(SSH_PUBLIC_KEYFILE
),
3549 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
3550 HAVE(SSH_PRIVATE_KEYFILE
),
3552 HAVENOT(SSH_PRIVATE_KEYFILE
),
3554 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3557 HAVENOT(FTP_SSL_CCC
),
3559 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3562 HAVENOT(TIMEOUT_MS
),
3564 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3565 HAVE(CONNECTTIMEOUT_MS
),
3567 HAVENOT(CONNECTTIMEOUT_MS
),
3569 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3570 HAVE(HTTP_TRANSFER_DECODING
),
3572 HAVENOT(HTTP_TRANSFER_DECODING
),
3574 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3575 HAVE(HTTP_CONTENT_DECODING
),
3577 HAVENOT(HTTP_CONTENT_DECODING
),
3579 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3580 HAVE(NEW_FILE_PERMS
),
3582 HAVENOT(NEW_FILE_PERMS
),
3584 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3585 HAVE(NEW_DIRECTORY_PERMS
),
3587 HAVENOT(NEW_DIRECTORY_PERMS
),
3589 #if HAVE_DECL_CURLOPT_POST301
3594 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3595 HAVE(SSH_HOST_PUBLIC_KEY_MD5
),
3597 HAVENOT(SSH_HOST_PUBLIC_KEY_MD5
),
3599 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3600 HAVE(COPYPOSTFIELDS
),
3602 HAVENOT(COPYPOSTFIELDS
),
3604 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3605 HAVE(PROXY_TRANSFER_MODE
),
3607 HAVENOT(PROXY_TRANSFER_MODE
),
3609 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3612 HAVENOT(SEEKFUNCTION
),
3614 #if HAVE_DECL_CURLOPT_AUTOREFERER
3617 HAVENOT(AUTOREFERER
),
3619 HAVE(OPENSOCKETFUNCTION
),
3620 /*HAVE(CLOSESOCKETFUNCTION),*/
3621 #if HAVE_DECL_CURLOPT_PROXYTYPE
3626 #if HAVE_DECL_CURLOPT_PROTOCOLS
3631 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3632 HAVE(REDIR_PROTOCOLS
),
3634 HAVENOT(REDIR_PROTOCOLS
),
3636 #if HAVE_DECL_CURLOPT_RESOLVE
3641 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3644 HAVENOT(DNS_SERVERS
),
3646 #if HAVE_DECL_CURLOPT_MAIL_FROM
3651 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3656 #if HAVE_DECL_CURLOPT_PIPEWAIT
3661 #if HAVE_DECL_CURLOPT_CERTINFO
3666 #if HAVE_DECL_CURLOPT_USERNAME
3671 #if HAVE_DECL_CURLOPT_PASSWORD
3676 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
3677 HAVE(LOGIN_OPTIONS
),
3679 HAVENOT(LOGIN_OPTIONS
),
3681 #if HAVE_DECL_CURLOPT_CONNECT_TO
3684 HAVENOT(CONNECT_TO
),
3686 #if HAVE_DECL_CURLOPT_POSTREDIR
3691 #if HAVE_DECL_CURLOPT_MIMEPOST
3696 HAVE(SSH_KNOWNHOSTS
),
3697 HAVE(SSH_KEYFUNCTION
),
3699 #if HAVE_DECL_CURLOPT_DOH_URL
3704 #if HAVE_DECL_CURLOPT_SSL_OPTIONS
3707 HAVENOT(SSL_OPTIONS
),
3709 HAVE(WRITEFUNCTION2
),
3710 HAVE(READFUNCTION2
),
3711 HAVE(XFERINFOFUNCTION
),
3714 value
caml_curl_easy_setopt(value conn
, value option
)
3716 CAMLparam2(conn
, option
);
3718 Connection
*connection
= Connection_val(conn
);
3719 CURLOptionMapping
* thisOption
= NULL
;
3720 static const value
* exception
= NULL
;
3722 checkConnection(connection
);
3724 data
= Field(option
, 0);
3726 if (Tag_val(option
) < sizeof(implementedOptionMap
)/sizeof(CURLOptionMapping
))
3728 thisOption
= &implementedOptionMap
[Tag_val(option
)];
3729 if (thisOption
->optionHandler
)
3731 thisOption
->optionHandler(connection
, data
);
3735 if (NULL
== exception
)
3737 exception
= caml_named_value("Curl.NotImplemented");
3738 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
3741 caml_raise_with_string(*exception
, thisOption
->name
);
3746 caml_failwith("Invalid CURLOPT Option");
3749 CAMLreturn(Val_unit
);
3753 ** curl_easy_perform helper function
3756 value
caml_curl_easy_perform(value conn
)
3759 CURLcode result
= CURLE_OK
;
3760 Connection
*connection
= Connection_val(conn
);
3762 checkConnection(connection
);
3764 caml_enter_blocking_section();
3765 result
= curl_easy_perform(connection
->handle
);
3766 caml_leave_blocking_section();
3768 if (result
!= CURLE_OK
)
3769 raiseError(connection
, result
);
3771 CAMLreturn(Val_unit
);
3775 ** curl_easy_cleanup helper function
3778 value
caml_curl_easy_cleanup(value conn
)
3781 Connection
*connection
= Connection_val(conn
);
3783 checkConnection(connection
);
3785 removeConnection(connection
, 0);
3787 CAMLreturn(Val_unit
);
3791 ** curl_easy_getinfo helper function
3794 enum GetInfoResultType
{
3795 StringValue
, LongValue
, DoubleValue
, StringListValue
, StringListListValue
,
3796 SocketValue
, OCamlValue
, /* keep last - no matching OCaml CURLINFO_ constructor */
3799 value
convertStringList(struct curl_slist
*p
)
3802 CAMLlocal3(result
, current
, next
);
3804 result
= Val_emptylist
;
3805 current
= Val_emptylist
;
3806 next
= Val_emptylist
;
3810 next
= caml_alloc_tuple(2);
3811 Store_field(next
, 0, caml_copy_string(p
->data
));
3812 Store_field(next
, 1, Val_emptylist
);
3814 if (result
== Val_emptylist
)
3817 if (current
!= Val_emptylist
)
3818 Store_field(current
, 1, next
);
3828 value
caml_curl_easy_getinfo(value conn
, value option
)
3830 CAMLparam2(conn
, option
);
3831 CAMLlocal3(result
, current
, next
);
3832 CURLcode curlResult
;
3833 Connection
*connection
= Connection_val(conn
);
3834 enum GetInfoResultType resultType
;
3835 char *strValue
= NULL
;
3838 curl_socket_t socketValue
;
3839 struct curl_slist
*stringListValue
= NULL
;
3840 #if HAVE_DECL_CURLINFO_CERTINFO
3843 struct curl_slist
*to_info
;
3844 struct curl_certinfo
*to_certinfo
;
3848 checkConnection(connection
);
3850 switch(Long_val(option
))
3852 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3853 case 0: /* CURLINFO_EFFECTIVE_URL */
3854 resultType
= StringValue
;
3856 curlResult
= curl_easy_getinfo(connection
->handle
,
3857 CURLINFO_EFFECTIVE_URL
,
3861 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3864 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3865 case 1: /* CURLINFO_HTTP_CODE */
3866 case 2: /* CURLINFO_RESPONSE_CODE */
3867 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3868 resultType
= LongValue
;
3870 curlResult
= curl_easy_getinfo(connection
->handle
,
3871 CURLINFO_RESPONSE_CODE
,
3874 resultType
= LongValue
;
3876 curlResult
= curl_easy_getinfo(connection
->handle
,
3883 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3884 case 3: /* CURLINFO_TOTAL_TIME */
3885 resultType
= DoubleValue
;
3887 curlResult
= curl_easy_getinfo(connection
->handle
,
3888 CURLINFO_TOTAL_TIME
,
3893 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3894 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3895 resultType
= DoubleValue
;
3897 curlResult
= curl_easy_getinfo(connection
->handle
,
3898 CURLINFO_NAMELOOKUP_TIME
,
3903 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3904 case 5: /* CURLINFO_CONNECT_TIME */
3905 resultType
= DoubleValue
;
3907 curlResult
= curl_easy_getinfo(connection
->handle
,
3908 CURLINFO_CONNECT_TIME
,
3913 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3914 case 6: /* CURLINFO_PRETRANSFER_TIME */
3915 resultType
= DoubleValue
;
3917 curlResult
= curl_easy_getinfo(connection
->handle
,
3918 CURLINFO_PRETRANSFER_TIME
,
3923 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3924 case 7: /* CURLINFO_SIZE_UPLOAD */
3925 resultType
= DoubleValue
;
3927 curlResult
= curl_easy_getinfo(connection
->handle
,
3928 CURLINFO_SIZE_UPLOAD
,
3933 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3934 case 8: /* CURLINFO_SIZE_DOWNLOAD */
3935 resultType
= DoubleValue
;
3937 curlResult
= curl_easy_getinfo(connection
->handle
,
3938 CURLINFO_SIZE_DOWNLOAD
,
3943 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
3944 case 9: /* CURLINFO_SPEED_DOWNLOAD */
3945 resultType
= DoubleValue
;
3947 curlResult
= curl_easy_getinfo(connection
->handle
,
3948 CURLINFO_SPEED_DOWNLOAD
,
3953 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
3954 case 10: /* CURLINFO_SPEED_UPLOAD */
3955 resultType
= DoubleValue
;
3957 curlResult
= curl_easy_getinfo(connection
->handle
,
3958 CURLINFO_SPEED_UPLOAD
,
3964 #if HAVE_DECL_CURLINFO_HEADER_SIZE
3965 case 11: /* CURLINFO_HEADER_SIZE */
3966 resultType
= LongValue
;
3968 curlResult
= curl_easy_getinfo(connection
->handle
,
3969 CURLINFO_HEADER_SIZE
,
3974 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
3975 case 12: /* CURLINFO_REQUEST_SIZE */
3976 resultType
= LongValue
;
3978 curlResult
= curl_easy_getinfo(connection
->handle
,
3979 CURLINFO_REQUEST_SIZE
,
3984 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
3985 case 13: /* CURLINFO_SSL_VERIFYRESULT */
3986 resultType
= LongValue
;
3988 curlResult
= curl_easy_getinfo(connection
->handle
,
3989 CURLINFO_SSL_VERIFYRESULT
,
3994 #if HAVE_DECL_CURLINFO_FILETIME
3995 case 14: /* CURLINFO_FILETIME */
3996 resultType
= DoubleValue
;
3998 curlResult
= curl_easy_getinfo(connection
->handle
,
4002 doubleValue
= longValue
;
4006 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
4007 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
4008 resultType
= DoubleValue
;
4010 curlResult
= curl_easy_getinfo(connection
->handle
,
4011 CURLINFO_CONTENT_LENGTH_DOWNLOAD
,
4016 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
4017 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
4018 resultType
= DoubleValue
;
4020 curlResult
= curl_easy_getinfo(connection
->handle
,
4021 CURLINFO_CONTENT_LENGTH_UPLOAD
,
4026 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
4027 case 17: /* CURLINFO_STARTTRANSFER_TIME */
4028 resultType
= DoubleValue
;
4030 curlResult
= curl_easy_getinfo(connection
->handle
,
4031 CURLINFO_STARTTRANSFER_TIME
,
4036 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
4037 case 18: /* CURLINFO_CONTENT_TYPE */
4038 resultType
= StringValue
;
4040 curlResult
= curl_easy_getinfo(connection
->handle
,
4041 CURLINFO_CONTENT_TYPE
,
4046 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
4047 case 19: /* CURLINFO_REDIRECT_TIME */
4048 resultType
= DoubleValue
;
4050 curlResult
= curl_easy_getinfo(connection
->handle
,
4051 CURLINFO_REDIRECT_TIME
,
4056 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
4057 case 20: /* CURLINFO_REDIRECT_COUNT */
4058 resultType
= LongValue
;
4060 curlResult
= curl_easy_getinfo(connection
->handle
,
4061 CURLINFO_REDIRECT_COUNT
,
4066 case 21: /* CURLINFO_PRIVATE */
4067 resultType
= OCamlValue
;
4068 curlResult
= CURLE_OK
;
4069 result
= caml_alloc(1, StringValue
);
4070 Store_field(result
, 0, Field(connection
->ocamlValues
, Ocaml_PRIVATE
));
4073 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
4074 case 22: /* CURLINFO_HTTP_CONNECTCODE */
4075 resultType
= LongValue
;
4077 curlResult
= curl_easy_getinfo(connection
->handle
,
4078 CURLINFO_HTTP_CONNECTCODE
,
4083 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
4084 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
4085 resultType
= LongValue
;
4087 curlResult
= curl_easy_getinfo(connection
->handle
,
4088 CURLINFO_HTTPAUTH_AVAIL
,
4093 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
4094 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
4095 resultType
= LongValue
;
4097 curlResult
= curl_easy_getinfo(connection
->handle
,
4098 CURLINFO_PROXYAUTH_AVAIL
,
4103 #if HAVE_DECL_CURLINFO_OS_ERRNO
4104 case 25: /* CURLINFO_OS_ERRNO */
4105 resultType
= LongValue
;
4107 curlResult
= curl_easy_getinfo(connection
->handle
,
4113 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
4114 case 26: /* CURLINFO_NUM_CONNECTS */
4115 resultType
= LongValue
;
4117 curlResult
= curl_easy_getinfo(connection
->handle
,
4118 CURLINFO_NUM_CONNECTS
,
4123 #if HAVE_DECL_CURLINFO_SSL_ENGINES
4124 case 27: /* CURLINFO_SSL_ENGINES */
4125 resultType
= StringListValue
;
4127 curlResult
= curl_easy_getinfo(connection
->handle
,
4128 CURLINFO_SSL_ENGINES
,
4133 #if HAVE_DECL_CURLINFO_COOKIELIST
4134 case 28: /* CURLINFO_COOKIELIST */
4135 resultType
= StringListValue
;
4137 curlResult
= curl_easy_getinfo(connection
->handle
,
4138 CURLINFO_COOKIELIST
,
4143 #if HAVE_DECL_CURLINFO_LASTSOCKET
4144 case 29: /* CURLINFO_LASTSOCKET */
4145 resultType
= LongValue
;
4147 curlResult
= curl_easy_getinfo(connection
->handle
,
4148 CURLINFO_LASTSOCKET
,
4153 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
4154 case 30: /* CURLINFO_FTP_ENTRY_PATH */
4155 resultType
= StringValue
;
4157 curlResult
= curl_easy_getinfo(connection
->handle
,
4158 CURLINFO_FTP_ENTRY_PATH
,
4163 #if HAVE_DECL_CURLINFO_REDIRECT_URL
4164 case 31: /* CURLINFO_REDIRECT_URL */
4165 resultType
= StringValue
;
4167 curlResult
= curl_easy_getinfo(connection
->handle
,
4168 CURLINFO_REDIRECT_URL
,
4172 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
4175 #if HAVE_DECL_CURLINFO_PRIMARY_IP
4176 case 32: /* CURLINFO_PRIMARY_IP */
4177 resultType
= StringValue
;
4179 curlResult
= curl_easy_getinfo(connection
->handle
,
4180 CURLINFO_PRIMARY_IP
,
4184 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
4187 #if HAVE_DECL_CURLINFO_LOCAL_IP
4188 case 33: /* CURLINFO_LOCAL_IP */
4189 resultType
= StringValue
;
4191 curlResult
= curl_easy_getinfo(connection
->handle
,
4196 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
4199 #if HAVE_DECL_CURLINFO_LOCAL_PORT
4200 case 34: /* CURLINFO_LOCAL_PORT */
4201 resultType
= LongValue
;
4203 curlResult
= curl_easy_getinfo(connection
->handle
,
4204 CURLINFO_LOCAL_PORT
,
4208 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
4211 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
4212 case 35: /* CURLINFO_CONDITION_UNMET */
4213 resultType
= LongValue
;
4215 curlResult
= curl_easy_getinfo(connection
->handle
,
4216 CURLINFO_CONDITION_UNMET
,
4220 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
4222 #if HAVE_DECL_CURLINFO_CERTINFO
4223 case 36: /* CURLINFO_CERTINFO */
4224 resultType
= StringListListValue
;
4226 curlResult
= curl_easy_getinfo(connection
->handle
,
4230 result
= Val_emptylist
;
4231 current
= Val_emptylist
;
4232 next
= Val_emptylist
;
4234 if (curlResult
!= CURLE_OK
|| !ptr
.to_info
)
4237 for (i
= 0; i
< ptr
.to_certinfo
->num_of_certs
; i
++) {
4238 next
= caml_alloc_tuple(2);
4239 Store_field(next
, 0, convertStringList(ptr
.to_certinfo
->certinfo
[i
]));
4240 Store_field(next
, 1, current
);
4245 #pragma message("libcurl does not provide CURLINFO_CERTINFO")
4247 #if HAVE_DECL_CURLINFO_ACTIVESOCKET
4248 case 37: /* CURLINFO_ACTIVESOCKET */
4249 resultType
= SocketValue
;
4251 curlResult
= curl_easy_getinfo(connection
->handle
,
4252 CURLINFO_ACTIVESOCKET
,
4256 #pragma message("libcurl does not provide CURLINFO_ACTIVESOCKET")
4258 #if HAVE_DECL_CURLINFO_HTTP_VERSION
4259 case 38: /* CURLINFO_HTTP_VERSION */
4260 resultType
= LongValue
;
4262 curlResult
= curl_easy_getinfo(connection
->handle
,
4263 CURLINFO_HTTP_VERSION
,
4266 longValue
= ocaml_HTTP_VERSION(longValue
);
4270 #pragma message("libcurl does not provide CURLINFO_HTTP_VERSION")
4272 /* sync check_enums */
4274 caml_failwith("Invalid CURLINFO Option");
4278 if (curlResult
!= CURLE_OK
)
4279 raiseError(connection
, curlResult
);
4284 result
= caml_alloc(1, StringValue
);
4285 Store_field(result
, 0, caml_copy_string(strValue
?strValue
:""));
4289 result
= caml_alloc(1, LongValue
);
4290 Store_field(result
, 0, Val_long(longValue
));
4294 result
= caml_alloc(1, DoubleValue
);
4295 Store_field(result
, 0, caml_copy_double(doubleValue
));
4298 case StringListValue
:
4299 result
= caml_alloc(1, StringListValue
);
4300 Store_field(result
, 0, convertStringList(stringListValue
));
4301 curl_slist_free_all(stringListValue
);
4304 case StringListListValue
:
4305 result
= caml_alloc(1, StringListListValue
);
4306 Store_field(result
, 0, current
);
4310 result
= caml_alloc(1, SocketValue
);
4311 Store_field(result
, 0, Val_socket(socketValue
));
4322 ** curl_escape helper function
4325 value
caml_curl_escape(value str
)
4331 curlResult
= curl_escape(String_val(str
), caml_string_length(str
));
4332 result
= caml_copy_string(curlResult
);
4339 ** curl_unescape helper function
4342 value
caml_curl_unescape(value str
)
4348 curlResult
= curl_unescape(String_val(str
), caml_string_length(str
));
4349 result
= caml_copy_string(curlResult
);
4356 ** curl_getdate helper function
4359 value
caml_curl_getdate(value str
, value now
)
4361 CAMLparam2(str
, now
);
4366 curlNow
= (time_t)Double_val(now
);
4367 curlResult
= curl_getdate(String_val(str
), &curlNow
);
4368 result
= caml_copy_double((double)curlResult
);
4374 ** curl_version helper function
4377 value
caml_curl_version(void)
4383 str
= curl_version();
4384 result
= caml_copy_string(str
);
4389 struct CURLVersionBitsMapping
4395 struct CURLVersionBitsMapping versionBitsMap
[] =
4397 {CURL_VERSION_IPV6
, "ipv6"},
4398 {CURL_VERSION_KERBEROS4
, "kerberos4"},
4399 {CURL_VERSION_SSL
, "ssl"},
4400 {CURL_VERSION_LIBZ
, "libz"},
4401 {CURL_VERSION_NTLM
, "ntlm"},
4402 {CURL_VERSION_GSSNEGOTIATE
, "gssnegotiate"},
4403 {CURL_VERSION_DEBUG
, "debug"},
4404 {CURL_VERSION_CURLDEBUG
, "curldebug"},
4405 {CURL_VERSION_ASYNCHDNS
, "asynchdns"},
4406 {CURL_VERSION_SPNEGO
, "spnego"},
4407 {CURL_VERSION_LARGEFILE
, "largefile"},
4408 {CURL_VERSION_IDN
, "idn"},
4409 {CURL_VERSION_SSPI
, "sspi"},
4410 {CURL_VERSION_CONV
, "conv"},
4411 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
4412 {CURL_VERSION_TLSAUTH_SRP
, "srp"},
4414 #if HAVE_DECL_CURL_VERSION_NTLM_WB
4415 {CURL_VERSION_NTLM_WB
, "wb"},
4419 value
caml_curl_version_info(value unit
)
4422 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4423 const char* const* p
= NULL
;
4426 curl_version_info_data
* data
= curl_version_info(CURLVERSION_NOW
);
4427 if (NULL
== data
) caml_failwith("curl_version_info");
4429 vlist
= Val_emptylist
;
4430 for (p
= data
->protocols
; NULL
!= *p
; p
++)
4432 vlist
= Val_cons(vlist
, caml_copy_string(*p
));
4435 vfeatures
= Val_emptylist
;
4436 for (i
= 0; i
< sizeof(versionBitsMap
)/sizeof(versionBitsMap
[0]); i
++)
4438 if (0 != (versionBitsMap
[i
].code
& data
->features
))
4439 vfeatures
= Val_cons(vfeatures
, caml_copy_string(versionBitsMap
[i
].name
));
4442 vnum
= caml_alloc_tuple(3);
4443 Store_field(vnum
,0,Val_int(0xFF & (data
->version_num
>> 16)));
4444 Store_field(vnum
,1,Val_int(0xFF & (data
->version_num
>> 8)));
4445 Store_field(vnum
,2,Val_int(0xFF & (data
->version_num
)));
4447 v
= caml_alloc_tuple(12);
4448 Store_field(v
,0,caml_copy_string(data
->version
));
4449 Store_field(v
,1,vnum
);
4450 Store_field(v
,2,caml_copy_string(data
->host
));
4451 Store_field(v
,3,vfeatures
);
4452 Store_field(v
,4,data
->ssl_version
? Val_some(caml_copy_string(data
->ssl_version
)) : Val_none
);
4453 Store_field(v
,5,data
->libz_version
? Val_some(caml_copy_string(data
->libz_version
)) : Val_none
);
4454 Store_field(v
,6,vlist
);
4455 Store_field(v
,7,caml_copy_string((data
->age
>= 1 && data
->ares
) ? data
->ares
: ""));
4456 Store_field(v
,8,Val_int((data
->age
>= 1) ? data
->ares_num
: 0));
4457 Store_field(v
,9,caml_copy_string((data
->age
>= 2 && data
->libidn
) ? data
->libidn
: ""));
4458 Store_field(v
,10,Val_int((data
->age
>= 3) ? data
->iconv_ver_num
: 0));
4459 Store_field(v
,11,caml_copy_string((data
->age
>= 3 && data
->libssh_version
) ? data
->libssh_version
: ""));
4464 value
caml_curl_pause(value conn
, value opts
)
4466 CAMLparam2(conn
, opts
);
4467 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4468 Connection
*connection
= Connection_val(conn
);
4472 while (Val_emptylist
!= opts
)
4474 switch (Int_val(Field(opts
,0)))
4476 case 0: bitmask
|= CURLPAUSE_SEND
; break;
4477 case 1: bitmask
|= CURLPAUSE_RECV
; break;
4478 case 2: bitmask
|= CURLPAUSE_ALL
; break;
4479 default: caml_failwith("wrong pauseOption");
4481 opts
= Field(opts
,1);
4484 caml_enter_blocking_section();
4485 result
= curl_easy_pause(connection
->handle
,bitmask
);
4486 caml_leave_blocking_section();
4488 if (result
!= CURLE_OK
)
4489 raiseError(connection
, result
);
4491 CAMLreturn(Val_unit
);
4495 * Curl multi stack support
4497 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
4498 * Other exported functions are prefixed with caml_curlm_, some of them
4499 * can/should be decomposed into smaller parts.
4502 struct ml_multi_handle
4505 value values
; /* callbacks */
4510 curlmopt_socket_function
,
4511 curlmopt_timer_function
,
4513 /* last, not used */
4517 typedef struct ml_multi_handle ml_multi_handle
;
4519 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
4520 #define CURLM_val(v) (Multi_val(v)->handle)
4522 static struct custom_operations curl_multi_ops
= {
4524 custom_finalize_default
,
4525 custom_compare_default
,
4526 custom_hash_default
,
4527 custom_serialize_default
,
4528 custom_deserialize_default
,
4529 #if defined(custom_compare_ext_default)
4530 custom_compare_ext_default
,
4534 static void raise_multi_error(char const* msg
)
4536 static const value
* exception
= NULL
;
4538 if (NULL
== exception
)
4540 exception
= caml_named_value("Curl.Multi.Error");
4541 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.Error");
4544 caml_raise_with_string(*exception
, msg
);
4547 static void raise_multi_cerror(char const* func
, CURLMcode code
)
4551 static const value
* exception
= NULL
;
4553 if (NULL
== exception
)
4555 exception
= caml_named_value("Curl.Multi.CError");
4556 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.CError");
4559 data
= caml_alloc(4, 0);
4561 Store_field(data
, 0, *exception
);
4562 Store_field(data
, 1, caml_copy_string(func
));
4563 Store_field(data
, 2, Val_int(code
));
4564 Store_field(data
, 3, caml_copy_string(curl_multi_strerror(code
)));
4571 static void check_mcode(char const* func
, CURLMcode code
)
4573 if (code
!= CURLM_OK
) raise_multi_cerror(func
,code
);
4576 value
caml_curl_multi_init(value unit
)
4580 ml_multi_handle
* multi
= (ml_multi_handle
*)caml_stat_alloc(sizeof(ml_multi_handle
));
4581 CURLM
* h
= curl_multi_init();
4585 caml_stat_free(multi
);
4586 raise_multi_error("caml_curl_multi_init");
4590 multi
->values
= caml_alloc(multi_values_total
, 0);
4591 caml_register_generational_global_root(&multi
->values
);
4593 v
= caml_alloc_custom(&curl_multi_ops
, sizeof(ml_multi_handle
*), 0, 1);
4594 Multi_val(v
) = multi
;
4599 value
caml_curl_multi_cleanup(value handle
)
4602 ml_multi_handle
* h
= Multi_val(handle
);
4605 CAMLreturn(Val_unit
);
4607 caml_remove_generational_global_root(&h
->values
);
4609 CURLcode rc
= curl_multi_cleanup(h
->handle
);
4612 Multi_val(handle
) = (ml_multi_handle
*)NULL
;
4614 check_mcode("curl_multi_cleanup",rc
);
4616 CAMLreturn(Val_unit
);
4619 static CURL
* curlm_remove_finished(CURLM
* multi_handle
, CURLcode
* result
)
4621 int msgs_in_queue
= 0;
4625 CURLMsg
* msg
= curl_multi_info_read(multi_handle
, &msgs_in_queue
);
4626 if (NULL
== msg
) return NULL
;
4627 if (CURLMSG_DONE
== msg
->msg
)
4629 CURL
* easy_handle
= msg
->easy_handle
;
4630 if (result
) *result
= msg
->data
.result
;
4631 if (CURLM_OK
!= curl_multi_remove_handle(multi_handle
, easy_handle
))
4633 /*caml_failwith("curlm_remove_finished");*/
4640 value
caml_curlm_remove_finished(value v_multi
)
4642 CAMLparam1(v_multi
);
4643 CAMLlocal2(v_easy
, v_tuple
);
4645 CURLM
* multi_handle
;
4647 Connection
* conn
= NULL
;
4649 multi_handle
= CURLM_val(v_multi
);
4651 caml_enter_blocking_section();
4652 handle
= curlm_remove_finished(multi_handle
,&result
);
4653 caml_leave_blocking_section();
4657 CAMLreturn(Val_none
);
4661 conn
= getConnection(handle
);
4662 if (conn
->curl_ERRORBUFFER
!= NULL
)
4664 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
4667 /* NB: same handle, but different block */
4668 v_easy
= caml_curl_alloc(conn
);
4669 v_tuple
= caml_alloc(2, 0);
4670 Store_field(v_tuple
,0,v_easy
);
4671 Store_field(v_tuple
,1,Val_int(result
)); /* CURLcode */
4672 CAMLreturn(Val_some(v_tuple
));
4676 value
caml_curl_multi_wait(value v_timeout_ms
, value v_multi
)
4678 CAMLparam2(v_timeout_ms
,v_multi
);
4679 CURLM
*multi_handle
= CURLM_val(v_multi
);
4680 int timeout_ms
= Int_val(v_timeout_ms
);
4684 caml_enter_blocking_section();
4685 rc
= curl_multi_wait(multi_handle
, NULL
, 0, timeout_ms
, &numfds
);
4686 caml_leave_blocking_section();
4688 check_mcode("curl_multi_wait",rc
);
4690 CAMLreturn(Val_bool(numfds
!= 0));
4693 value
caml_curl_multi_poll(value v_timeout_ms
, value v_multi
)
4695 CAMLparam2(v_timeout_ms
,v_multi
);
4696 CURLM
*multi_handle
= CURLM_val(v_multi
);
4697 int timeout_ms
= Int_val(v_timeout_ms
);
4701 caml_enter_blocking_section();
4702 #ifdef HAVE_CURL_MULTI_POLL
4703 rc
= curl_multi_poll(multi_handle
, NULL
, 0, timeout_ms
, &numfds
);
4705 rc
= curl_multi_wait(multi_handle
, NULL
, 0, timeout_ms
, &numfds
);
4707 caml_leave_blocking_section();
4709 check_mcode("curl_multi_poll",rc
);
4711 CAMLreturn(Val_bool(numfds
!= 0));
4714 value
caml_curl_multi_add_handle(value v_multi
, value v_easy
)
4716 CAMLparam2(v_multi
,v_easy
);
4717 CURLMcode rc
= CURLM_OK
;
4718 CURLM
* multi
= CURLM_val(v_multi
);
4719 Connection
* conn
= Connection_val(v_easy
);
4721 /* prevent collection of OCaml value while the easy handle is used
4722 and may invoke callbacks registered on OCaml side */
4725 /* may invoke callbacks so need to be consistent with locks */
4726 caml_enter_blocking_section();
4727 rc
= curl_multi_add_handle(multi
, conn
->handle
);
4730 conn
->refcount
--; /* not added, revert */
4731 caml_leave_blocking_section();
4732 check_mcode("curl_multi_add_handle",rc
);
4734 caml_leave_blocking_section();
4736 CAMLreturn(Val_unit
);
4739 value
caml_curl_multi_remove_handle(value v_multi
, value v_easy
)
4741 CAMLparam2(v_multi
,v_easy
);
4742 CURLM
* multi
= CURLM_val(v_multi
);
4743 CURLMcode rc
= CURLM_OK
;
4744 Connection
* conn
= Connection_val(v_easy
);
4746 /* may invoke callbacks so need to be consistent with locks */
4747 caml_enter_blocking_section();
4748 rc
= curl_multi_remove_handle(multi
, conn
->handle
);
4750 caml_leave_blocking_section();
4751 check_mcode("curl_multi_remove_handle",rc
);
4753 CAMLreturn(Val_unit
);
4756 value
caml_curl_multi_perform_all(value v_multi
)
4758 CAMLparam1(v_multi
);
4759 int still_running
= 0;
4760 CURLM
* h
= CURLM_val(v_multi
);
4762 caml_enter_blocking_section();
4763 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(h
, &still_running
));
4764 caml_leave_blocking_section();
4766 CAMLreturn(Val_int(still_running
));
4769 /* currently curlCode repr tag matches CURLE_ value
4770 * this is exploited below, but generally should use errorMap */
4772 value
caml_curl_strerror(value v_code
)
4775 CAMLreturn(caml_copy_string(curl_easy_strerror((CURLcode
)Int_val(v_code
))));
4778 value
caml_curl_int_of_curlCode(value v_code
)
4783 value
caml_curl_curlCode_of_int(value v
)
4785 return (Int_val(v
) < sizeof(errorMap
) / sizeof(errorMap
[0]) ? Val_some(v
) : Val_none
);
4788 value
caml_curl_multi_socket_action(value v_multi
, value v_fd
, value v_kind
)
4790 CAMLparam3(v_multi
, v_fd
, v_kind
);
4791 CURLM
* h
= CURLM_val(v_multi
);
4792 int still_running
= 0;
4793 CURLMcode rc
= CURLM_OK
;
4794 curl_socket_t socket
;
4797 if (Val_none
== v_fd
)
4799 socket
= CURL_SOCKET_TIMEOUT
;
4803 socket
= Socket_val(Field(v_fd
, 0));
4806 switch (Int_val(v_kind
))
4809 case 1 : kind
|= CURL_CSELECT_IN
; break;
4810 case 2 : kind
|= CURL_CSELECT_OUT
; break;
4811 case 3 : kind
|= CURL_CSELECT_IN
| CURL_CSELECT_OUT
; break;
4813 raise_multi_error("caml_curl_multi_socket_action");
4816 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4818 caml_enter_blocking_section();
4820 rc
= curl_multi_socket_action(h
, socket
, kind
, &still_running
);
4821 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4822 caml_leave_blocking_section();
4824 check_mcode("curl_multi_socket_action",rc
);
4826 CAMLreturn(Val_int(still_running
));
4829 value
caml_curl_multi_socket_all(value v_multi
)
4831 CAMLparam1(v_multi
);
4832 int still_running
= 0;
4833 CURLMcode rc
= CURLM_OK
;
4834 CURLM
* h
= CURLM_val(v_multi
);
4836 caml_enter_blocking_section();
4838 rc
= curl_multi_socket_all(h
, &still_running
);
4839 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4840 caml_leave_blocking_section();
4842 check_mcode("curl_multi_socket_all",rc
);
4844 CAMLreturn(Val_int(still_running
));
4847 static int curlm_sock_cb(CURL
*e
, curl_socket_t sock
, int what
, void *cbp
, void *sockp
)
4849 caml_leave_blocking_section();
4852 CAMLlocal2(v_what
,csock
);
4854 (void)sockp
; /* not used */
4856 /* v_what = Val_int(what); */
4859 case CURL_POLL_NONE
: v_what
= Val_int(0); break;
4860 case CURL_POLL_IN
: v_what
= Val_int(1); break;
4861 case CURL_POLL_OUT
: v_what
= Val_int(2); break;
4862 case CURL_POLL_INOUT
: v_what
= Val_int(3); break;
4863 case CURL_POLL_REMOVE
: v_what
= Val_int(4); break;
4865 fprintf(stderr
, "curlm_sock_cb sock=%lld what=%d\n", (long long)sock
, what
);
4867 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
4869 csock
=Val_socket(sock
);
4870 caml_callback2(Field(((ml_multi_handle
*)cbp
)->values
,curlmopt_socket_function
),
4874 caml_enter_blocking_section();
4878 value
caml_curl_multi_socketfunction(value v_multi
, value v_cb
)
4880 CAMLparam2(v_multi
, v_cb
);
4881 ml_multi_handle
* multi
= Multi_val(v_multi
);
4883 Store_field(multi
->values
, curlmopt_socket_function
, v_cb
);
4885 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETFUNCTION
, curlm_sock_cb
);
4886 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETDATA
, multi
);
4888 CAMLreturn(Val_unit
);
4891 static int curlm_timer_cb(CURLM
*multi
, long timeout_ms
, void *userp
)
4893 caml_leave_blocking_section();
4897 caml_callback(Field(((ml_multi_handle
*)userp
)->values
,curlmopt_timer_function
), Val_long(timeout_ms
));
4900 caml_enter_blocking_section();
4904 value
caml_curl_multi_timerfunction(value v_multi
, value v_cb
)
4906 CAMLparam2(v_multi
, v_cb
);
4907 ml_multi_handle
* multi
= Multi_val(v_multi
);
4909 Store_field(multi
->values
, curlmopt_timer_function
, v_cb
);
4911 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERFUNCTION
, curlm_timer_cb
);
4912 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERDATA
, multi
);
4914 CAMLreturn(Val_unit
);
4917 value
caml_curl_multi_timeout(value v_multi
)
4919 CAMLparam1(v_multi
);
4921 CURLMcode rc
= CURLM_OK
;
4922 ml_multi_handle
* multi
= Multi_val(v_multi
);
4924 rc
= curl_multi_timeout(multi
->handle
, &ms
);
4926 check_mcode("curl_multi_timeout",rc
);
4928 CAMLreturn(Val_long(ms
));
4931 #define SETMOPT_VAL_(func_name, curl_option, conv_val) \
4932 static void func_name(CURLM *handle, value option) \
4934 CAMLparam1(option); \
4935 CURLMcode result = CURLM_OK; \
4937 result = curl_multi_setopt(handle, curl_option, conv_val(option)); \
4939 check_mcode("curl_multi_setopt "#curl_option,result); \
4944 #define SETMOPT_VAL(name, conv) SETMOPT_VAL_(handle_multi_##name, CURLMOPT_##name, conv)
4945 #define SETMOPT_BOOL(name) SETMOPT_VAL(name, Bool_val)
4946 #define SETMOPT_LONG(name) SETMOPT_VAL(name, Long_val)
4947 #define SETMOPT_INT64(name) SETMOPT_VAL(name, Int64_val)
4949 long pipeliningMap
[] =
4951 0, /* CURLPIPE_NOTHING */
4952 1, /* CURLPIPE_HTTP1 */
4953 2, /* CURLPIPE_MULTIPLEX */
4956 static void handle_multi_PIPELINING(CURLM
* handle
, value option
)
4959 CURLMcode result
= CURLM_OK
;
4961 long bits
= convert_bit_list(pipeliningMap
, sizeof(pipeliningMap
) / sizeof(pipeliningMap
[0]), option
);
4963 result
= curl_multi_setopt(handle
, CURLMOPT_PIPELINING
, bits
);
4965 check_mcode("curl_multi_setopt CURLOPT_PIPELINING",result
);
4970 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
4971 SETMOPT_LONG( MAXCONNECTS
)
4974 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
4975 SETMOPT_LONG( MAX_PIPELINE_LENGTH
)
4978 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
4979 SETMOPT_LONG( MAX_HOST_CONNECTIONS
)
4982 #if HAVE_DECL_CURLMOPT_MAX_TOTAL_CONNECTIONS
4983 SETMOPT_LONG( MAX_TOTAL_CONNECTIONS
)
4986 typedef struct CURLMOptionMapping CURLMOptionMapping
;
4987 #define OPT(name) { handle_multi_## name, "CURLMOPT_"#name}
4988 #define NO_OPT(name) { NULL, "CURLMOPT_"#name}
4990 struct CURLMOptionMapping
4992 void (*optionHandler
)(CURLM
*, value
);
4996 CURLMOptionMapping implementedMOptionMap
[] = {
4998 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
5001 NO_OPT( MAXCONNECTS
),
5003 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
5004 OPT( MAX_PIPELINE_LENGTH
),
5006 NO_OPT( MAX_PIPELINE_LENGTH
),
5008 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
5009 OPT( MAX_HOST_CONNECTIONS
),
5011 NO_OPT( MAX_HOST_CONNECTIONS
),
5013 #if HAVE_DECL_CURLMOPT_MAX_TOTAL_CONNECTIONS
5014 OPT( MAX_TOTAL_CONNECTIONS
),
5016 NO_OPT( MAX_TOTAL_CONNECTIONS
),
5020 value
caml_curl_multi_setopt(value v_multi
, value option
)
5022 CAMLparam2(v_multi
, option
);
5024 CURLM
*handle
= Multi_val(v_multi
)->handle
;
5025 CURLMOptionMapping
* thisOption
= NULL
;
5026 static const value
* exception
= NULL
;
5028 data
= Field(option
, 0);
5030 if (Tag_val(option
) < sizeof(implementedMOptionMap
)/sizeof(CURLMOptionMapping
))
5032 thisOption
= &implementedMOptionMap
[Tag_val(option
)];
5033 if (thisOption
->optionHandler
)
5035 thisOption
->optionHandler(handle
, data
);
5039 if (NULL
== exception
)
5041 exception
= caml_named_value("Curl.NotImplemented");
5042 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
5045 caml_raise_with_string(*exception
, thisOption
->name
);
5050 raise_multi_error("Invalid CURLMOPT Option");
5053 CAMLreturn(Val_unit
);
5063 #define CURL_ENUM(name,last_used) { CURL_ ## name ## _ ## last_used, CURL_ ## name ## _LAST, #name }
5065 struct used_enum check_enums
[] = {
5066 { CURLINFO_SSL_DATA_OUT
, CURLINFO_END
, "DEBUGFUNCTION curl_infotype" },
5067 #if HAVE_DECL_CURL_HTTP_VERSION_3
5068 CURL_ENUM(HTTP_VERSION
, 3),
5070 { 38, CURLINFO_LASTONE
, "CURLINFO" },
5071 #if HAVE_DECL_CURLE_AGAIN
5072 { CURLE_AGAIN
, CURL_LAST
, "CURLcode" }
5076 value
caml_curl_check_enums(value v_unit
)
5081 size_t len
= sizeof(check_enums
) / sizeof(struct used_enum
);
5083 v_r
= caml_alloc_tuple(len
);
5085 for (i
= 0; i
< len
; i
++)
5087 v
= caml_alloc_tuple(3);
5088 Store_field(v
, 0, Val_int(check_enums
[i
].last_used
));
5089 Store_field(v
, 1, Val_int(check_enums
[i
].last
));
5090 Store_field(v
, 2, caml_copy_string(check_enums
[i
].name
));
5091 Store_field(v_r
, i
, v
);