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
,
104 Ocaml_PREREQFUNCTION
,
108 Ocaml_OPENSOCKETFUNCTION
,
109 /* Ocaml_CLOSESOCKETFUNCTION, */
110 Ocaml_SSH_KEYFUNCTION
,
115 /* Not used, last for size */
125 size_t refcount
; /* number of references to this structure */
127 char *curl_ERRORBUFFER
;
128 char *curl_POSTFIELDS
;
129 struct curl_slist
*curl_HTTPHEADER
;
130 struct curl_slist
*httpPostBuffers
;
131 struct curl_httppost
*httpPostFirst
;
132 struct curl_httppost
*httpPostLast
;
133 struct curl_slist
*curl_RESOLVE
;
134 struct curl_slist
*curl_QUOTE
;
135 struct curl_slist
*curl_POSTQUOTE
;
136 struct curl_slist
*curl_HTTP200ALIASES
;
137 struct curl_slist
*curl_MAIL_RCPT
;
138 struct curl_slist
*curl_CONNECT_TO
;
139 #if HAVE_DECL_CURLOPT_MIMEPOST
140 curl_mime
*curl_MIMEPOST
;
144 typedef struct CURLErrorMapping CURLErrorMapping
;
146 struct CURLErrorMapping
152 CURLErrorMapping errorMap
[] =
154 {"CURLE_OK", CURLE_OK
},
155 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
156 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL
},
158 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
160 #if HAVE_DECL_CURLE_FAILED_INIT
161 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT
},
163 {"CURLE_FAILED_INIT", -1},
165 #if HAVE_DECL_CURLE_URL_MALFORMAT
166 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT
},
168 {"CURLE_URL_MALFORMAT", -1},
170 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
171 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER
},
173 {"CURLE_URL_MALFORMAT_USER", -1},
175 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
176 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY
},
178 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
180 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
181 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST
},
183 {"CURLE_COULDNT_RESOLVE_HOST", -1},
185 #if HAVE_DECL_CURLE_COULDNT_CONNECT
186 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT
},
188 {"CURLE_COULDNT_CONNECT", -1},
190 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
191 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY
},
193 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
195 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
196 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED
},
198 {"CURLE_FTP_ACCESS_DENIED", -1},
200 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
201 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT
},
203 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
205 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
206 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY
},
208 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
210 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
211 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY
},
213 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
215 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
216 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY
},
218 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
220 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
221 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT
},
223 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
225 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
226 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST
},
228 {"CURLE_FTP_CANT_GET_HOST", -1},
230 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
231 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT
},
233 {"CURLE_FTP_CANT_RECONNECT", -1},
235 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
236 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY
},
238 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
240 #if HAVE_DECL_CURLE_PARTIAL_FILE
241 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE
},
243 {"CURLE_PARTIAL_FILE", -1},
245 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
246 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE
},
248 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
250 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
251 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR
},
253 {"CURLE_FTP_WRITE_ERROR", -1},
255 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
256 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR
},
258 {"CURLE_FTP_QUOTE_ERROR", -1},
260 #if HAVE_DECL_CURLE_HTTP_RETURNED_ERROR
261 {"CURLE_HTTP_RETURNED_ERROR", CURLE_HTTP_RETURNED_ERROR
},
263 {"CURLE_HTTP_RETURNED_ERROR", -1},
265 #if HAVE_DECL_CURLE_WRITE_ERROR
266 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR
},
268 {"CURLE_WRITE_ERROR", -1},
270 #if HAVE_DECL_CURLE_MALFORMAT_USER
271 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER
},
273 {"CURLE_MALFORMAT_USER", -1},
275 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
276 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE
},
278 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
280 #if HAVE_DECL_CURLE_READ_ERROR
281 {"CURLE_READ_ERROR", CURLE_READ_ERROR
},
283 {"CURLE_READ_ERROR", -1},
285 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
286 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY
},
288 {"CURLE_OUT_OF_MEMORY", -1},
290 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
291 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED
},
293 {"CURLE_OPERATION_TIMEOUTED", -1},
295 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
296 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII
},
298 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
300 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
301 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED
},
303 {"CURLE_FTP_PORT_FAILED", -1},
305 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
306 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST
},
308 {"CURLE_FTP_COULDNT_USE_REST", -1},
310 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
311 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE
},
313 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
315 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
316 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR
},
318 {"CURLE_HTTP_RANGE_ERROR", -1},
320 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
321 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR
},
323 {"CURLE_HTTP_POST_ERROR", -1},
325 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
326 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR
},
328 {"CURLE_SSL_CONNECT_ERROR", -1},
330 #if HAVE_DECL_CURLE_BAD_DOWNLOAD_RESUME
331 {"CURLE_BAD_DOWNLOAD_RESUME", CURLE_BAD_DOWNLOAD_RESUME
},
333 {"CURLE_BAD_DOWNLOAD_RESUME", -1},
335 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
336 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE
},
338 {"CURLE_FILE_COULDNT_READ_FILE", -1},
340 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
341 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND
},
343 {"CURLE_LDAP_CANNOT_BIND", -1},
345 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
346 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED
},
348 {"CURLE_LDAP_SEARCH_FAILED", -1},
350 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
351 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND
},
353 {"CURLE_LIBRARY_NOT_FOUND", -1},
355 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
356 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND
},
358 {"CURLE_FUNCTION_NOT_FOUND", -1},
360 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
361 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK
},
363 {"CURLE_ABORTED_BY_CALLBACK", -1},
365 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
366 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT
},
368 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
370 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
371 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER
},
373 {"CURLE_BAD_CALLING_ORDER", -1},
375 #if HAVE_DECL_CURLE_INTERFACE_FAILED
376 {"CURLE_INTERFACE_FAILED", CURLE_INTERFACE_FAILED
},
378 {"CURLE_INTERFACE_FAILED", -1},
380 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
381 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED
},
383 {"CURLE_BAD_PASSWORD_ENTERED", -1},
385 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
386 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS
},
388 {"CURLE_TOO_MANY_REDIRECTS", -1},
390 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
391 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION
},
393 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
395 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
396 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX
},
398 {"CURLE_TELNET_OPTION_SYNTAX", -1},
400 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
401 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE
},
403 {"CURLE_SSL_PEER_CERTIFICATE", -1},
405 #if HAVE_DECL_CURLE_GOT_NOTHING
406 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING
},
408 {"CURLE_GOT_NOTHING", -1},
410 #if HAVE_DECL_CURLE_SSL_ENGINE_NOTFOUND
411 {"CURLE_SSL_ENGINE_NOTFOUND", CURLE_SSL_ENGINE_NOTFOUND
},
413 {"CURLE_SSL_ENGINE_NOTFOUND", -1},
415 #if HAVE_DECL_CURLE_SSL_ENGINE_SETFAILED
416 {"CURLE_SSL_ENGINE_SETFAILED", CURLE_SSL_ENGINE_SETFAILED
},
418 {"CURLE_SSL_ENGINE_SETFAILED", -1},
420 #if HAVE_DECL_CURLE_SEND_ERROR
421 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR
},
423 {"CURLE_SEND_ERROR", -1},
425 #if HAVE_DECL_CURLE_RECV_ERROR
426 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR
},
428 {"CURLE_RECV_ERROR", -1},
430 #if HAVE_DECL_CURLE_SHARE_IN_USE
431 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE
},
433 {"CURLE_SHARE_IN_USE", -1},
435 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
436 {"CURLE_SSL_CERTPROBLEM", CURLE_SSL_CERTPROBLEM
},
438 {"CURLE_SSL_CERTPROBLEM", -1},
440 #if HAVE_DECL_CURLE_SSL_CIPHER
441 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER
},
443 {"CURLE_SSL_CIPHER", -1},
445 #if HAVE_DECL_CURLE_SSL_CACERT
446 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT
},
448 {"CURLE_SSL_CACERT", -1},
450 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
451 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING
},
453 {"CURLE_BAD_CONTENT_ENCODING", -1},
455 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
456 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL
},
458 {"CURLE_LDAP_INVALID_URL", -1},
460 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
461 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED
},
463 {"CURLE_FILESIZE_EXCEEDED", -1},
465 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
466 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED
},
468 {"CURLE_FTP_SSL_FAILED", -1},
470 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
471 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND
},
473 {"CURLE_SEND_FAIL_REWIND", -1},
475 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
476 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED
},
478 {"CURLE_SSL_ENGINE_INITFAILED", -1},
480 #if HAVE_DECL_CURLE_LOGIN_DENIED
481 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED
},
483 {"CURLE_LOGIN_DENIED", -1},
485 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
486 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND
},
488 {"CURLE_TFTP_NOTFOUND", -1},
490 #if HAVE_DECL_CURLE_TFTP_PERM
491 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM
},
493 {"CURLE_TFTP_PERM", -1},
495 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
496 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL
},
498 {"CURLE_REMOTE_DISK_FULL", -1},
500 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
501 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL
},
503 {"CURLE_TFTP_ILLEGAL", -1},
505 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
506 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID
},
508 {"CURLE_TFTP_UNKNOWNID", -1},
510 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
511 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS
},
513 {"CURLE_REMOTE_FILE_EXISTS", -1},
515 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
516 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER
},
518 {"CURLE_TFTP_NOSUCHUSER", -1},
520 #if HAVE_DECL_CURLE_CONV_FAILED
521 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED
},
523 {"CURLE_CONV_FAILED", -1},
525 #if HAVE_DECL_CURLE_CONV_REQD
526 {"CURLE_CONV_REQD", CURLE_CONV_REQD
},
528 {"CURLE_CONV_REQD", -1},
530 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
531 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE
},
533 {"CURLE_SSL_CACERT_BADFILE", -1},
535 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
536 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND
},
538 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
540 #if HAVE_DECL_CURLE_SSH
541 {"CURLE_SSH", CURLE_SSH
},
545 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
546 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED
},
548 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
550 #if HAVE_DECL_CURLE_AGAIN
551 {"CURLE_AGAIN", CURLE_AGAIN
},
555 /* sync check_enums */
559 typedef struct CURLOptionMapping CURLOptionMapping
;
561 struct CURLOptionMapping
563 void (*optionHandler
)(Connection
*, value
);
567 static char* strdup_ml(value v
)
570 p
= (char*)malloc(caml_string_length(v
)+1);
571 memcpy(p
,String_val(v
),caml_string_length(v
)+1); // caml strings have terminating zero
575 static value
ml_copy_string(char const* p
, size_t size
)
577 value v
= caml_alloc_string(size
);
578 memcpy(&Byte(v
,0),p
,size
);
582 /* prepends to the beginning of list */
583 static struct curl_slist
* curl_slist_prepend_ml(struct curl_slist
* list
, value v
)
585 /* FIXME check NULLs */
586 struct curl_slist
* new_item
= (struct curl_slist
*)malloc(sizeof(struct curl_slist
));
588 new_item
->next
= list
;
589 new_item
->data
= strdup_ml(v
);
594 static void free_curl_slist(struct curl_slist
*slist
)
599 curl_slist_free_all(slist
);
602 static void raiseError(Connection
*conn
, CURLcode code
)
605 CAMLlocal1(exceptionData
);
606 const value
*exception
;
607 char *errorString
= "Unknown Error";
610 for (i
= 0; errorMap
[i
].name
!= NULL
; i
++)
612 if (errorMap
[i
].error
== code
)
614 errorString
= errorMap
[i
].name
;
619 exceptionData
= caml_alloc_tuple(3);
621 Store_field(exceptionData
, 0, Val_int(code
));
622 Store_field(exceptionData
, 1, Val_int(code
));
623 Store_field(exceptionData
, 2, caml_copy_string(errorString
));
625 if (conn
!= NULL
&& conn
->curl_ERRORBUFFER
!= NULL
)
627 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
630 exception
= caml_named_value("CurlException");
632 if (exception
== NULL
)
633 caml_failwith("CurlException not registered");
635 caml_raise_with_arg(*exception
, exceptionData
);
640 static void resetOcamlValues(Connection
* connection
)
644 for (i
= 0; i
< OcamlValuesSize
; i
++)
645 Store_field(connection
->ocamlValues
, i
, Val_unit
);
648 static Connection
* allocConnection(CURL
* h
)
650 Connection
* connection
= (Connection
*)malloc(sizeof(Connection
));
652 connection
->ocamlValues
= caml_alloc(OcamlValuesSize
, 0);
653 resetOcamlValues(connection
);
654 caml_register_global_root(&connection
->ocamlValues
);
656 connection
->handle
= h
;
657 curl_easy_setopt(h
, CURLOPT_PRIVATE
, connection
);
659 connection
->refcount
= 0;
661 connection
->curl_ERRORBUFFER
= NULL
;
662 connection
->curl_POSTFIELDS
= NULL
;
663 connection
->curl_HTTPHEADER
= NULL
;
664 connection
->httpPostBuffers
= NULL
;
665 connection
->httpPostFirst
= NULL
;
666 connection
->httpPostLast
= NULL
;
667 connection
->curl_QUOTE
= NULL
;
668 connection
->curl_POSTQUOTE
= NULL
;
669 connection
->curl_HTTP200ALIASES
= NULL
;
670 connection
->curl_RESOLVE
= NULL
;
671 connection
->curl_MAIL_RCPT
= NULL
;
672 connection
->curl_CONNECT_TO
= NULL
;
673 #if HAVE_DECL_CURLOPT_MIMEPOST
674 connection
->curl_MIMEPOST
= NULL
;
680 static Connection
*newConnection(void)
684 caml_enter_blocking_section();
685 h
= curl_easy_init();
686 caml_leave_blocking_section();
688 return allocConnection(h
);
691 static void free_if(void* p
) { if (NULL
!= p
) free(p
); }
693 static void removeConnection(Connection
*connection
, int finalization
)
695 const char* fin_url
= NULL
;
697 if (!connection
->handle
)
699 return; /* already cleaned up */
704 /* cannot engage OCaml runtime at finalization, just report leak */
705 if (CURLE_OK
!= curl_easy_getinfo(connection
->handle
, CURLINFO_EFFECTIVE_URL
, &fin_url
) || NULL
== fin_url
)
709 fprintf(stderr
,"Curl: handle %p leaked, conn %p, url %s\n", connection
->handle
, connection
, fin_url
);
714 caml_enter_blocking_section();
715 curl_easy_cleanup(connection
->handle
);
716 caml_leave_blocking_section();
719 connection
->handle
= NULL
;
721 caml_remove_global_root(&connection
->ocamlValues
);
723 free_if(connection
->curl_ERRORBUFFER
);
724 free_if(connection
->curl_POSTFIELDS
);
725 free_curl_slist(connection
->curl_HTTPHEADER
);
726 free_curl_slist(connection
->httpPostBuffers
);
727 if (connection
->httpPostFirst
!= NULL
)
728 curl_formfree(connection
->httpPostFirst
);
729 free_curl_slist(connection
->curl_RESOLVE
);
730 free_curl_slist(connection
->curl_QUOTE
);
731 free_curl_slist(connection
->curl_POSTQUOTE
);
732 free_curl_slist(connection
->curl_HTTP200ALIASES
);
733 free_curl_slist(connection
->curl_MAIL_RCPT
);
734 free_curl_slist(connection
->curl_CONNECT_TO
);
735 #if HAVE_DECL_CURLOPT_MIMEPOST
736 curl_mime_free(connection
->curl_MIMEPOST
);
740 static Connection
* getConnection(CURL
* h
)
742 Connection
* p
= NULL
;
744 if (CURLE_OK
!= curl_easy_getinfo(h
, CURLINFO_PRIVATE
, &p
) || NULL
== p
)
746 caml_failwith("Unknown handle");
753 static void checkConnection(Connection
* connection
)
758 static void checkConnection(Connection
*connection
)
760 if (connection
!= getConnection(connection
->handle
))
762 caml_failwith("Invalid Connection");
767 void op_curl_easy_finalize(value v
)
769 Connection
* conn
= Connection_val(v
);
770 /* same connection may be referenced by several different
771 OCaml values, see e.g. caml_curl_multi_remove_finished */
773 if (0 == conn
->refcount
)
775 removeConnection(conn
, 1);
780 int op_curl_easy_compare(value v1
, value v2
)
782 size_t p1
= (size_t)Connection_val(v1
);
783 size_t p2
= (size_t)Connection_val(v2
);
784 return (p1
== p2
? 0 : (p1
> p2
? 1 : -1)); /* compare addresses */
787 intnat
op_curl_easy_hash(value v
)
789 return (size_t)Connection_val(v
); /* address */
792 static struct custom_operations curl_easy_ops
= {
794 op_curl_easy_finalize
,
795 op_curl_easy_compare
,
797 custom_serialize_default
,
798 custom_deserialize_default
,
799 #if defined(custom_compare_ext_default)
800 custom_compare_ext_default
,
804 value
caml_curl_alloc(Connection
* conn
)
806 value v
= caml_alloc_custom(&curl_easy_ops
, sizeof(Connection
*), 0, 1);
807 Connection_val(v
) = conn
;
812 static size_t cb_WRITEFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
814 caml_leave_blocking_section();
817 CAMLlocal2(result
, str
);
818 Connection
*conn
= (Connection
*)data
;
820 checkConnection(conn
);
822 str
= ml_copy_string(ptr
,size
*nmemb
);
824 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
826 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
829 caml_enter_blocking_section();
833 static size_t cb_WRITEFUNCTION2(char *ptr
, size_t size
, size_t nmemb
, void *data
)
835 caml_leave_blocking_section();
838 CAMLlocal2(result
, str
);
839 Connection
*conn
= (Connection
*)data
;
841 checkConnection(conn
);
843 str
= ml_copy_string(ptr
,size
*nmemb
);
845 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
849 if (!Is_exception_result(result
))
851 if (Is_block(result
)) /* Proceed */
857 if (0 == Int_val(result
)) /* Pause */
858 r
= CURL_WRITEFUNC_PAUSE
;
865 caml_enter_blocking_section();
869 static size_t cb_READFUNCTION(void *ptr
, size_t size
, size_t nmemb
, void *data
)
871 caml_leave_blocking_section();
875 Connection
*conn
= (Connection
*)data
;
878 checkConnection(conn
);
880 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
881 Val_int(size
*nmemb
));
883 size_t r
= CURL_READFUNC_ABORT
;
885 if (!Is_exception_result(result
))
887 length
= caml_string_length(result
);
889 if (length
<= size
*nmemb
)
891 memcpy(ptr
, String_val(result
), length
);
898 caml_enter_blocking_section();
902 static size_t cb_READFUNCTION2(void *ptr
, size_t size
, size_t nmemb
, void *data
)
904 caml_leave_blocking_section();
908 Connection
*conn
= (Connection
*)data
;
911 checkConnection(conn
);
913 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
914 Val_int(size
*nmemb
));
916 size_t r
= CURL_READFUNC_ABORT
;
918 if (!Is_exception_result(result
))
920 if (Is_block(result
)) /* Proceed */
922 result
= Field(result
,0);
924 length
= caml_string_length(result
);
926 if (length
<= size
*nmemb
)
928 memcpy(ptr
, String_val(result
), length
);
934 if (0 == Int_val(result
)) /* Pause */
935 r
= CURL_READFUNC_PAUSE
;
942 caml_enter_blocking_section();
946 static size_t cb_HEADERFUNCTION(char *ptr
, size_t size
, size_t nmemb
, void *data
)
948 caml_leave_blocking_section();
951 CAMLlocal2(result
,str
);
952 Connection
*conn
= (Connection
*)data
;
954 checkConnection(conn
);
956 str
= ml_copy_string(ptr
,size
*nmemb
);
958 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_HEADERFUNCTION
), str
);
960 size_t r
= Is_exception_result(result
) ? 0 : Int_val(result
);
963 caml_enter_blocking_section();
967 static int cb_PROGRESSFUNCTION(void *data
,
973 caml_leave_blocking_section();
977 CAMLlocalN(callbackData
, 4);
978 Connection
*conn
= (Connection
*)data
;
980 checkConnection(conn
);
982 callbackData
[0] = caml_copy_double(dlTotal
);
983 callbackData
[1] = caml_copy_double(dlNow
);
984 callbackData
[2] = caml_copy_double(ulTotal
);
985 callbackData
[3] = caml_copy_double(ulNow
);
987 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PROGRESSFUNCTION
),
990 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
993 caml_enter_blocking_section();
997 static int cb_XFERINFOFUNCTION(void *data
,
1003 caml_leave_blocking_section();
1007 CAMLlocalN(callbackData
, 4);
1008 Connection
*conn
= (Connection
*)data
;
1010 checkConnection(conn
);
1012 callbackData
[0] = caml_copy_int64(dlTotal
);
1013 callbackData
[1] = caml_copy_int64(dlNow
);
1014 callbackData
[2] = caml_copy_int64(ulTotal
);
1015 callbackData
[3] = caml_copy_int64(ulNow
);
1017 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_XFERINFOFUNCTION
),
1020 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
1023 caml_enter_blocking_section();
1027 #if HAVE_DECL_CURLOPT_PREREQFUNCTION
1028 static int cb_PREREQFUNCTION(void *data
,
1029 char *conn_primary_ip
,
1030 char *conn_local_ip
,
1031 int conn_primary_port
,
1032 int conn_local_port
)
1034 caml_leave_blocking_section();
1038 CAMLlocalN(callbackData
, 4);
1039 Connection
*conn
= (Connection
*)data
;
1041 checkConnection(conn
);
1043 callbackData
[0] = caml_copy_string(conn_primary_ip
);
1044 callbackData
[1] = caml_copy_string(conn_local_ip
);
1045 callbackData
[2] = Val_int(conn_primary_port
);
1046 callbackData
[3] = Val_int(conn_local_port
);
1048 result
= caml_callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PREREQFUNCTION
),
1051 int r
= Is_exception_result(result
) ? 1 : Bool_val(result
);
1054 caml_enter_blocking_section();
1059 static int cb_DEBUGFUNCTION(CURL
*debugConnection
,
1060 curl_infotype infoType
,
1062 size_t bufferLength
,
1065 caml_leave_blocking_section();
1068 CAMLlocal3(camlDebugConnection
, camlInfoType
, camlMessage
);
1069 Connection
*conn
= (Connection
*)data
;
1070 (void)debugConnection
; /* not used */
1072 checkConnection(conn
);
1074 camlDebugConnection
= caml_curl_alloc(conn
);
1075 camlMessage
= ml_copy_string(buffer
,bufferLength
);
1076 camlInfoType
= Val_long(infoType
<= CURLINFO_SSL_DATA_OUT
/* sync check_enums */ ? infoType
: CURLINFO_END
);
1078 caml_callback3_exn(Field(conn
->ocamlValues
, Ocaml_DEBUGFUNCTION
),
1079 camlDebugConnection
,
1085 caml_enter_blocking_section();
1089 static curlioerr
cb_IOCTLFUNCTION(CURL
*ioctl
,
1093 caml_leave_blocking_section();
1096 CAMLlocal3(camlResult
, camlConnection
, camlCmd
);
1097 Connection
*conn
= (Connection
*)data
;
1098 curlioerr result
= CURLIOE_OK
;
1099 (void)ioctl
; /* not used */
1101 checkConnection(conn
);
1103 if (cmd
== CURLIOCMD_NOP
)
1104 camlCmd
= Val_long(0);
1105 else if (cmd
== CURLIOCMD_RESTARTREAD
)
1106 camlCmd
= Val_long(1);
1108 caml_failwith("Invalid IOCTL Cmd!");
1110 camlConnection
= caml_curl_alloc(conn
);
1112 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_IOCTLFUNCTION
),
1116 if (Is_exception_result(camlResult
))
1118 result
= CURLIOE_FAILRESTART
;
1121 switch (Long_val(camlResult
))
1123 case 0: /* CURLIOE_OK */
1124 result
= CURLIOE_OK
;
1127 case 1: /* CURLIOE_UNKNOWNCMD */
1128 result
= CURLIOE_UNKNOWNCMD
;
1131 case 2: /* CURLIOE_FAILRESTART */
1132 result
= CURLIOE_FAILRESTART
;
1135 default: /* Incorrect return value, but let's handle it */
1136 result
= CURLIOE_FAILRESTART
;
1141 caml_enter_blocking_section();
1145 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1146 static int cb_SEEKFUNCTION(void *data
,
1150 caml_leave_blocking_section();
1153 CAMLlocal3(camlResult
, camlOffset
, camlOrigin
);
1154 Connection
*conn
= (Connection
*)data
;
1156 camlOffset
= caml_copy_int64(offset
);
1158 if (origin
== SEEK_SET
)
1159 camlOrigin
= Val_long(0);
1160 else if (origin
== SEEK_CUR
)
1161 camlOrigin
= Val_long(1);
1162 else if (origin
== SEEK_END
)
1163 camlOrigin
= Val_long(2);
1165 caml_failwith("Invalid seek code");
1167 camlResult
= caml_callback2_exn(Field(conn
->ocamlValues
,
1168 Ocaml_SEEKFUNCTION
),
1173 if (Is_exception_result(camlResult
))
1174 result
= CURL_SEEKFUNC_FAIL
;
1176 switch (Int_val(camlResult
))
1178 case 0: result
= CURL_SEEKFUNC_OK
; break;
1179 case 1: result
= CURL_SEEKFUNC_FAIL
; break;
1180 case 2: result
= CURL_SEEKFUNC_CANTSEEK
; break;
1181 default: caml_failwith("Invalid seek result");
1185 caml_enter_blocking_section();
1190 static int cb_OPENSOCKETFUNCTION(void *data
,
1191 curlsocktype purpose
,
1192 struct curl_sockaddr
*addr
)
1194 caml_leave_blocking_section();
1198 Connection
*conn
= (Connection
*)data
;
1200 (void)purpose
; /* not used */
1202 sock
= socket(addr
->family
, addr
->socktype
, addr
->protocol
);
1207 result
= caml_callback_exn(Field(conn
->ocamlValues
, Ocaml_OPENSOCKETFUNCTION
), Val_int(sock
));
1208 if (Is_exception_result(result
))
1216 caml_enter_blocking_section();
1217 return ((sock
== -1) ? CURL_SOCKET_BAD
: sock
);
1221 static int cb_CLOSESOCKETFUNCTION(void *data,
1222 curl_socket_t socket)
1224 caml_leave_blocking_section();
1227 CAMLlocal1(camlResult);
1228 Connection *conn = (Connection *)data;
1231 camlResult = caml_callback_exn(Field(conn->ocamlValues, Ocaml_CLOSESOCKETFUNCTION), Val_int(socket));
1232 if (Is_exception_result(camlResult))
1238 caml_enter_blocking_section();
1243 static int cb_SSH_KEYFUNCTION(CURL
*easy
,
1244 const struct curl_khkey
*knownkey
,
1245 const struct curl_khkey
*foundkey
,
1246 enum curl_khmatch match
,
1249 caml_leave_blocking_section();
1252 CAMLlocal3(v_found
, v_match
, v_result
);
1253 Connection
*conn
= (Connection
*)clientp
;
1254 int res
= CURLKHSTAT_REJECT
;
1257 case CURLKHMATCH_OK
:
1258 v_match
= Val_int(0);
1260 case CURLKHMATCH_MISMATCH
:
1261 v_match
= caml_alloc_small(1, 0);
1262 Field(v_match
, 0) = ml_copy_string(knownkey
->key
, knownkey
->len
? knownkey
->len
: strlen(knownkey
->key
));
1264 case CURLKHMATCH_MISSING
:
1265 v_match
= Val_int(1);
1268 caml_failwith("Invalid CURL_SSH_KEYFUNCTION argument");
1272 v_found
= ml_copy_string(foundkey
->key
, foundkey
->len
? foundkey
->len
: strlen(foundkey
->key
));
1273 v_result
= caml_callback2_exn(Field(conn
->ocamlValues
, Ocaml_SSH_KEYFUNCTION
), v_match
, v_found
);
1275 if (!Is_exception_result(v_result
)) {
1276 switch (Int_val(v_result
)) {
1278 res
= CURLKHSTAT_FINE_ADD_TO_FILE
;
1281 res
= CURLKHSTAT_FINE
;
1284 res
= CURLKHSTAT_REJECT
;
1287 res
= CURLKHSTAT_DEFER
;
1290 caml_failwith("Invalid CURLOPT_SSH_KEYFUNCTION return value");
1297 caml_enter_blocking_section();
1301 #if HAVE_DECL_CURL_GLOBAL_SSLSET
1303 /* Same order as in OCaml */
1304 curl_sslbackend sslBackendMap
[] = {
1305 #if HAVE_DECL_CURLSSLBACKEND_NONE
1306 CURLSSLBACKEND_NONE
,
1310 #if HAVE_DECL_CURLSSLBACKEND_OPENSSL
1311 CURLSSLBACKEND_OPENSSL
,
1315 #if HAVE_DECL_CURLSSLBACKEND_GNUTLS
1316 CURLSSLBACKEND_GNUTLS
,
1320 #if HAVE_DECL_CURLSSLBACKEND_NSS
1325 #if HAVE_DECL_CURLSSLBACKEND_GSKIT
1326 CURLSSLBACKEND_GSKIT
,
1330 #if HAVE_DECL_CURLSSLBACKEND_WOLFSSL
1331 CURLSSLBACKEND_WOLFSSL
,
1335 #if HAVE_DECL_CURLSSLBACKEND_SCHANNEL
1336 CURLSSLBACKEND_SCHANNEL
,
1340 #if HAVE_DECL_CURLSSLBACKEND_SECURETRANSPORT
1341 CURLSSLBACKEND_SECURETRANSPORT
,
1345 #if HAVE_DECL_CURLSSLBACKEND_MBEDTLS
1346 CURLSSLBACKEND_MBEDTLS
,
1350 #if HAVE_DECL_CURLSSLBACKEND_MESALINK
1351 CURLSSLBACKEND_MESALINK
,
1355 #if HAVE_DECL_CURLSSLBACKEND_BEARSSL
1356 CURLSSLBACKEND_BEARSSL
,
1362 /* Same order as in OCaml */
1363 CURLsslset sslsetMap
[] = {
1365 CURLSSLSET_UNKNOWN_BACKEND
,
1366 CURLSSLSET_TOO_LATE
,
1367 CURLSSLSET_NO_BACKENDS
,
1370 static void raiseSslsetError(CURLsslset err
)
1373 const value
*exception
;
1376 for (i
= 0, found
= -1; i
< sizeof(sslsetMap
) / sizeof(sslsetMap
[0]); i
++) {
1377 if (sslsetMap
[i
] == err
) {
1384 caml_invalid_argument("Unexpected CURLsslset value");
1386 exception
= caml_named_value("CurlSslSetException");
1387 if (exception
== NULL
) caml_invalid_argument("CurlSslSetException not registered");
1389 caml_raise_with_arg(*exception
, Val_int(found
));
1395 value
caml_curl_global_sslset(value v_backend
)
1397 CAMLparam1(v_backend
);
1399 curl_sslbackend backend
= sslBackendMap
[Int_val(v_backend
)];
1400 CURLsslset res
= curl_global_sslset(backend
, NULL
, NULL
);
1402 if (res
!= CURLSSLSET_OK
)
1403 raiseSslsetError(res
);
1405 CAMLreturn(Val_unit
);
1408 value
caml_curl_global_sslset_str(value v_backend_str
)
1410 CAMLparam1(v_backend_str
);
1412 CURLsslset res
= curl_global_sslset(-1, String_val(v_backend_str
), NULL
);
1414 if (res
!= CURLSSLSET_OK
)
1415 raiseSslsetError(res
);
1417 CAMLreturn(Val_unit
);
1420 value
caml_curl_global_sslsetavail(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
++) {
1438 for (j
= 0; j
< sizeof(sslBackendMap
) / sizeof(sslBackendMap
[0]); j
++) {
1439 if (sslBackendMap
[j
] == backends
[i
]->id
) {
1445 /* If an unknown backend is returned, it is ignored */
1447 lst
= Val_cons(lst
, Val_long(found
));
1454 value
caml_curl_global_sslsetavail_str(value v_unit
)
1458 const curl_ssl_backend
**backends
;
1462 res
= curl_global_sslset(-1, NULL
, &backends
);
1464 if (res
!= CURLSSLSET_UNKNOWN_BACKEND
)
1465 raiseSslsetError(res
);
1467 lst
= Val_emptylist
;
1469 for (i
= 0; backends
[i
] != NULL
; i
++) {
1470 lst
= Val_cons(lst
, caml_copy_string(backends
[i
]->name
));
1476 value
caml_curl_global_sslset(value v_ignored
)
1478 const value
*exception
= caml_named_value("Curl.NotImplemented");
1479 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented not registered");
1480 caml_raise_with_string(*exception
, "curl_global_sslset");
1485 value
caml_curl_global_sslset_str(value v_ignored
)
1487 /* Argument is ignored */
1488 return caml_curl_global_sslset(Val_unit
);
1490 value
caml_curl_global_sslsetavail(value v_ignored
)
1492 /* Argument is ignored */
1493 return caml_curl_global_sslset(Val_unit
);
1495 value
caml_curl_global_sslsetavail_str(value v_ignored
)
1497 /* Argument is ignored */
1498 return caml_curl_global_sslset(Val_unit
);
1500 #endif /* HAVE_DECL_CURL_GLOBAL_SSLSET */
1503 ** curl_global_init helper function
1506 value
caml_curl_global_init(value initOption
)
1508 CAMLparam1(initOption
);
1510 switch (Long_val(initOption
))
1512 case 0: /* CURLINIT_GLOBALALL */
1513 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL
)));
1516 case 1: /* CURLINIT_GLOBALSSL */
1517 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL
)));
1520 case 2: /* CURLINIT_GLOBALWIN32 */
1521 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32
)));
1524 case 3: /* CURLINIT_GLOBALNOTHING */
1525 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING
)));
1529 caml_failwith("Invalid Initialization Option");
1533 /* Keep compiler happy, we should never get here due to caml_failwith() */
1534 CAMLreturn(Val_unit
);
1538 ** curl_global_cleanup helper function
1541 value
caml_curl_global_cleanup(void)
1545 curl_global_cleanup();
1547 CAMLreturn(Val_unit
);
1551 ** curl_easy_init helper function
1553 value
caml_curl_easy_init(void)
1558 result
= caml_curl_alloc(newConnection());
1563 value
caml_curl_easy_reset(value conn
)
1566 Connection
*connection
= Connection_val(conn
);
1568 checkConnection(connection
);
1569 curl_easy_reset(connection
->handle
);
1570 curl_easy_setopt(connection
->handle
, CURLOPT_PRIVATE
, connection
);
1571 resetOcamlValues(connection
);
1573 CAMLreturn(Val_unit
);
1577 ** curl_easy_setopt helper utility functions
1580 #define SETOPT_FUNCTION_(name,suffix) \
1581 static void handle_##name##suffix(Connection *conn, value option) \
1583 CAMLparam1(option); \
1584 CURLcode result = CURLE_OK; \
1585 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1586 result = curl_easy_setopt(conn->handle, CURLOPT_##name##FUNCTION, cb_##name##suffix); \
1587 if (result != CURLE_OK) raiseError(conn, result); \
1588 result = curl_easy_setopt(conn->handle, CURLOPT_##name##DATA, conn); \
1589 if (result != CURLE_OK) raiseError(conn, result); \
1593 #define SETOPT_FUNCTION(name) SETOPT_FUNCTION_(name,FUNCTION)
1594 #define SETOPT_FUNCTION2(name) SETOPT_FUNCTION_(name,FUNCTION2)
1596 SETOPT_FUNCTION( WRITE
)
1597 SETOPT_FUNCTION2( WRITE
)
1598 SETOPT_FUNCTION( READ
)
1599 SETOPT_FUNCTION2( READ
)
1600 SETOPT_FUNCTION( HEADER
)
1601 SETOPT_FUNCTION( PROGRESS
)
1602 #if HAVE_DECL_CURLOPT_XFERINFOFUNCTION
1603 SETOPT_FUNCTION( XFERINFO
)
1605 #if HAVE_DECL_CURLOPT_PREREQFUNCTION
1606 SETOPT_FUNCTION( PREREQ
)
1608 SETOPT_FUNCTION( DEBUG
)
1609 SETOPT_FUNCTION( SSH_KEY
)
1611 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1612 SETOPT_FUNCTION( SEEK
)
1615 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1616 SETOPT_FUNCTION( IOCTL
)
1619 SETOPT_FUNCTION( OPENSOCKET
)
1620 /* SETOPT_FUNCTION( CLOSESOCKET) */
1622 static void handle_slist(Connection
*conn
, struct curl_slist
** slist
, CURLoption curl_option
, value option
)
1625 CURLcode result
= CURLE_OK
;
1627 free_curl_slist(*slist
);
1630 while (Val_emptylist
!= option
)
1632 *slist
= curl_slist_append(*slist
, String_val(Field(option
, 0)));
1634 option
= Field(option
, 1);
1637 result
= curl_easy_setopt(conn
->handle
, curl_option
, *slist
);
1639 if (result
!= CURLE_OK
)
1640 raiseError(conn
, result
);
1645 static long convert_bit_list(long *map
, size_t map_size
, value option
)
1651 while (Val_emptylist
!= option
)
1653 index
= Int_val(Field(option
, 0));
1654 if ((index
< 0) || ((size_t)index
>= map_size
))
1655 caml_invalid_argument("convert_bit_list");
1659 option
= Field(option
, 1);
1662 CAMLreturnT(long, bits
);
1665 #define SETOPT_STRING(name) \
1666 static void handle_##name(Connection *conn, value option) \
1668 CAMLparam1(option); \
1669 CURLcode result = CURLE_OK; \
1671 result = curl_easy_setopt(conn->handle, CURLOPT_##name, String_val(option)); \
1673 if (result != CURLE_OK) \
1674 raiseError(conn, result); \
1679 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1680 static void func_name(Connection *conn, value option) \
1682 CAMLparam1(option); \
1683 CURLcode result = CURLE_OK; \
1685 result = curl_easy_setopt(conn->handle, curl_option, conv_val(option)); \
1687 if (result != CURLE_OK) \
1688 raiseError(conn, result); \
1693 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1694 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1695 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1696 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1698 #if HAVE_DECL_CURLOPT_MIMEPOST
1700 static void handle_part_with_name(Connection
* conn
, curl_mimepart
* part
, value v_part_name_info
)
1702 value v_data
= Field(v_part_name_info
, 0);
1703 value v_name
= Field(v_part_name_info
, 1);
1704 value v_filename
= Field(v_part_name_info
, 2);
1707 value v_str
= Field(v_data
, 0);
1709 switch (Tag_val(v_data
)) {
1711 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1714 result
= curl_mime_filedata(part
, String_val(v_str
));
1717 caml_failwith("Invalid MIMEPOST data value");
1721 if (result
!= CURLE_OK
) {
1722 raiseError(conn
, result
);
1725 if (Is_block(v_name
)) {
1726 result
= curl_mime_name(part
, String_val(Field(v_name
, 0)));
1728 if (result
!= CURLE_OK
) {
1729 raiseError(conn
, result
);
1733 if (Is_block(v_filename
)) {
1734 result
= curl_mime_filename(part
, String_val(Field(v_filename
, 0)));
1736 if (result
!= CURLE_OK
) {
1737 raiseError(conn
, result
);
1742 static void new_part(Connection
* conn
, curl_mime
* mime
, value v_part
)
1744 value v_encoding
= Field(v_part
, 0);
1745 value v_headers
= Field(v_part
, 1);
1746 value v_subparts
= Field(v_part
, 2);
1747 value v_data
= Field(v_part
, 3);
1748 value v_str
= Field(v_data
, 0);
1750 struct curl_slist
*headers
= NULL
;
1753 curl_mimepart
*part
= curl_mime_addpart(mime
);
1755 switch (Int_val(v_encoding
)) {
1757 result
= curl_mime_encoder(part
, "8bit");
1760 result
= curl_mime_encoder(part
, "binary");
1763 result
= curl_mime_encoder(part
, "7bit");
1766 result
= curl_mime_encoder(part
, "quoted-printable");
1769 result
= curl_mime_encoder(part
, "base64");
1775 caml_failwith("Invalid MIMEPOST encoding value");
1779 if (result
!= CURLE_OK
) {
1780 raiseError(conn
, result
);
1783 while (v_headers
!= Val_emptylist
) {
1784 headers
= curl_slist_append(headers
, String_val(Field(v_headers
, 0)));
1785 v_headers
= Field(v_headers
, 1);
1788 result
= curl_mime_headers(part
, headers
, 1);
1790 if (result
!= CURLE_OK
) {
1791 raiseError(conn
, result
);
1794 switch (Tag_val(v_data
)) {
1796 result
= curl_mime_data(part
, String_val(v_str
), caml_string_length(v_str
));
1799 result
= curl_mime_filedata(part
, String_val(v_str
));
1802 handle_part_with_name(conn
, part
, v_data
);
1805 caml_failwith("Invalid MIMEPOST data value");
1809 if (result
!= CURLE_OK
) {
1810 raiseError(conn
, result
);
1813 if (v_subparts
!= Val_emptylist
) {
1814 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1816 while (v_subparts
!= Val_emptylist
) {
1817 new_part(conn
, mime
, Field(v_subparts
, 0));
1818 v_subparts
= Field(v_subparts
, 1);
1821 result
= curl_mime_subparts(part
, mime
);
1823 if (result
!= CURLE_OK
) {
1824 raiseError(conn
, result
);
1829 static void handle_MIMEPOST(Connection
* conn
, value v_subparts
)
1831 CAMLparam1(v_subparts
);
1832 curl_mime
*mime
= curl_mime_init(conn
->handle
);
1835 curl_mime_free(conn
->curl_MIMEPOST
);
1836 conn
->curl_MIMEPOST
= mime
;
1838 while (v_subparts
!= Val_emptylist
) {
1839 new_part(conn
, mime
, Field(v_subparts
, 0));
1840 v_subparts
= Field(v_subparts
, 1);
1843 result
= curl_easy_setopt(conn
->handle
, CURLOPT_MIMEPOST
, mime
);
1845 if (result
!= CURLE_OK
) {
1846 raiseError(conn
, result
);
1854 #define SETOPT_SLIST(name) \
1855 static void handle_##name(Connection* conn, value option) \
1857 handle_slist(conn,&(conn->curl_##name),CURLOPT_##name,option); \
1861 SETOPT_LONG( INFILESIZE
)
1862 SETOPT_STRING( PROXY
)
1863 SETOPT_LONG( PROXYPORT
)
1864 SETOPT_BOOL( HTTPPROXYTUNNEL
)
1865 SETOPT_BOOL( VERBOSE
)
1866 SETOPT_BOOL( HEADER
)
1867 SETOPT_BOOL( NOPROGRESS
)
1869 #if HAVE_DECL_CURLOPT_NOSIGNAL
1870 SETOPT_BOOL( NOSIGNAL
)
1873 SETOPT_BOOL( NOBODY
)
1874 SETOPT_BOOL( FAILONERROR
)
1875 SETOPT_BOOL( UPLOAD
)
1877 SETOPT_BOOL( FTPLISTONLY
)
1878 SETOPT_BOOL( FTPAPPEND
)
1881 static void handle_NETRC(Connection
*conn
, value option
)
1884 CURLcode result
= CURLE_OK
;
1887 switch (Long_val(option
))
1889 case 0: /* CURL_NETRC_OPTIONAL */
1890 netrc
= CURL_NETRC_OPTIONAL
;
1893 case 1:/* CURL_NETRC_IGNORED */
1894 netrc
= CURL_NETRC_IGNORED
;
1897 case 2: /* CURL_NETRC_REQUIRED */
1898 netrc
= CURL_NETRC_REQUIRED
;
1902 caml_failwith("Invalid NETRC Option");
1906 result
= curl_easy_setopt(conn
->handle
,
1910 if (result
!= CURLE_OK
)
1911 raiseError(conn
, result
);
1916 #if HAVE_DECL_CURLOPT_ENCODING
1917 static void handle_ENCODING(Connection
*conn
, value option
)
1920 CURLcode result
= CURLE_OK
;
1922 switch (Long_val(option
))
1924 case 0: /* CURL_ENCODING_NONE */
1925 result
= curl_easy_setopt(conn
->handle
,
1930 case 1: /* CURL_ENCODING_DEFLATE */
1931 result
= curl_easy_setopt(conn
->handle
,
1936 case 2: /* CURL_ENCODING_GZIP */
1937 result
= curl_easy_setopt(conn
->handle
,
1942 case 3: /* CURL_ENCODING_ANY */
1943 result
= curl_easy_setopt(conn
->handle
,
1949 caml_failwith("Invalid Encoding Option");
1953 if (result
!= CURLE_OK
)
1954 raiseError(conn
, result
);
1961 SETOPT_BOOL( FOLLOWLOCATION
)
1962 SETOPT_BOOL( TRANSFERTEXT
)
1964 SETOPT_STRING( USERPWD
)
1965 SETOPT_STRING( PROXYUSERPWD
)
1966 SETOPT_STRING( RANGE
)
1968 static void handle_ERRORBUFFER(Connection
*conn
, value option
)
1971 CURLcode result
= CURLE_OK
;
1973 Store_field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
, option
);
1975 if (conn
->curl_ERRORBUFFER
!= NULL
)
1976 free(conn
->curl_ERRORBUFFER
);
1978 conn
->curl_ERRORBUFFER
= (char*)malloc(sizeof(char) * CURL_ERROR_SIZE
);
1980 result
= curl_easy_setopt(conn
->handle
,
1981 CURLOPT_ERRORBUFFER
,
1982 conn
->curl_ERRORBUFFER
);
1984 if (result
!= CURLE_OK
)
1985 raiseError(conn
, result
);
1990 SETOPT_LONG( TIMEOUT
)
1992 static void handle_POSTFIELDS(Connection
*conn
, value option
)
1995 CURLcode result
= CURLE_OK
;
1997 if (conn
->curl_POSTFIELDS
!= NULL
)
1998 free(conn
->curl_POSTFIELDS
);
2000 conn
->curl_POSTFIELDS
= strdup_ml(option
);
2002 result
= curl_easy_setopt(conn
->handle
,
2004 conn
->curl_POSTFIELDS
);
2006 if (result
!= CURLE_OK
)
2007 raiseError(conn
, result
);
2012 SETOPT_LONG( POSTFIELDSIZE
)
2013 SETOPT_STRING( REFERER
)
2014 SETOPT_STRING( USERAGENT
)
2015 SETOPT_STRING( FTPPORT
)
2016 SETOPT_LONG( LOW_SPEED_LIMIT
)
2017 SETOPT_LONG( LOW_SPEED_TIME
)
2018 SETOPT_LONG( RESUME_FROM
)
2019 SETOPT_STRING( COOKIE
)
2021 SETOPT_SLIST( HTTPHEADER
)
2023 long sslOptionMap
[] = {
2024 #ifdef CURLSSLOPT_ALLOW_BEAST
2025 CURLSSLOPT_ALLOW_BEAST
,
2029 #ifdef CURLSSLOPT_NO_REVOKE
2030 CURLSSLOPT_NO_REVOKE
,
2034 #ifdef CURLSSLOPT_NO_PARTIALCHAIN
2035 CURLSSLOPT_NO_PARTIALCHAIN
,
2039 #ifdef CURLSSLOPT_REVOKE_BEST_EFFORT
2040 CURLSSLOPT_REVOKE_BEST_EFFORT
,
2044 #ifdef CURLSSLOPT_NATIVE_CA
2045 CURLSSLOPT_NATIVE_CA
,
2049 #ifdef CURLSSLOPT_AUTO_CLIENT_CERT
2050 CURLSSLOPT_AUTO_CLIENT_CERT
,
2056 #if HAVE_DECL_CURLOPT_SSL_OPTIONS
2057 static void handle_SSL_OPTIONS(Connection
*conn
, value opts
)
2060 CURLcode result
= CURLE_OK
;
2061 long bits
= convert_bit_list(sslOptionMap
, sizeof(sslOptionMap
) / sizeof(sslOptionMap
[0]), opts
);
2063 result
= curl_easy_setopt(conn
->handle
, CURLOPT_SSL_OPTIONS
, bits
);
2065 if (result
!= CURLE_OK
)
2066 raiseError(conn
, result
);
2072 static void handle_HTTPPOST(Connection
*conn
, value option
)
2075 CAMLlocal3(listIter
, formItem
, contentType
);
2076 CURLcode result
= CURLE_OK
;
2080 free_curl_slist(conn
->httpPostBuffers
);
2081 if (conn
->httpPostFirst
!= NULL
)
2082 curl_formfree(conn
->httpPostFirst
);
2084 conn
->httpPostBuffers
= NULL
;
2085 conn
->httpPostFirst
= NULL
;
2086 conn
->httpPostLast
= NULL
;
2088 while (!Is_long(listIter
))
2090 formItem
= Field(listIter
, 0);
2092 switch (Tag_val(formItem
))
2094 case 0: /* CURLFORM_CONTENT */
2095 if (Wosize_val(formItem
) < 3)
2097 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
2100 if (Is_long(Field(formItem
, 2)) &&
2101 Long_val(Field(formItem
, 2)) == 0)
2103 curl_formadd(&conn
->httpPostFirst
,
2104 &conn
->httpPostLast
,
2106 String_val(Field(formItem
, 0)),
2107 CURLFORM_NAMELENGTH
,
2108 caml_string_length(Field(formItem
, 0)),
2109 CURLFORM_COPYCONTENTS
,
2110 String_val(Field(formItem
, 1)),
2111 CURLFORM_CONTENTSLENGTH
,
2112 caml_string_length(Field(formItem
, 1)),
2115 else if (Is_block(Field(formItem
, 2)))
2117 contentType
= Field(formItem
, 2);
2119 curl_formadd(&conn
->httpPostFirst
,
2120 &conn
->httpPostLast
,
2122 String_val(Field(formItem
, 0)),
2123 CURLFORM_NAMELENGTH
,
2124 caml_string_length(Field(formItem
, 0)),
2125 CURLFORM_COPYCONTENTS
,
2126 String_val(Field(formItem
, 1)),
2127 CURLFORM_CONTENTSLENGTH
,
2128 caml_string_length(Field(formItem
, 1)),
2129 CURLFORM_CONTENTTYPE
,
2130 String_val(Field(contentType
, 0)),
2135 caml_failwith("Incorrect CURLFORM_CONTENT parameters");
2139 case 1: /* CURLFORM_FILECONTENT */
2140 if (Wosize_val(formItem
) < 3)
2142 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
2145 if (Is_long(Field(formItem
, 2)) &&
2146 Long_val(Field(formItem
, 2)) == 0)
2148 curl_formadd(&conn
->httpPostFirst
,
2149 &conn
->httpPostLast
,
2151 String_val(Field(formItem
, 0)),
2152 CURLFORM_NAMELENGTH
,
2153 caml_string_length(Field(formItem
, 0)),
2154 CURLFORM_FILECONTENT
,
2155 String_val(Field(formItem
, 1)),
2158 else if (Is_block(Field(formItem
, 2)))
2160 contentType
= Field(formItem
, 2);
2162 curl_formadd(&conn
->httpPostFirst
,
2163 &conn
->httpPostLast
,
2165 String_val(Field(formItem
, 0)),
2166 CURLFORM_NAMELENGTH
,
2167 caml_string_length(Field(formItem
, 0)),
2168 CURLFORM_FILECONTENT
,
2169 String_val(Field(formItem
, 1)),
2170 CURLFORM_CONTENTTYPE
,
2171 String_val(Field(contentType
, 0)),
2176 caml_failwith("Incorrect CURLFORM_FILECONTENT parameters");
2180 case 2: /* CURLFORM_FILE */
2181 if (Wosize_val(formItem
) < 3)
2183 caml_failwith("Incorrect CURLFORM_FILE parameters");
2186 if (Is_long(Field(formItem
, 2)) &&
2187 Long_val(Field(formItem
, 2)) == 0)
2189 curl_formadd(&conn
->httpPostFirst
,
2190 &conn
->httpPostLast
,
2192 String_val(Field(formItem
, 0)),
2193 CURLFORM_NAMELENGTH
,
2194 caml_string_length(Field(formItem
, 0)),
2196 String_val(Field(formItem
, 1)),
2199 else if (Is_block(Field(formItem
, 2)))
2201 contentType
= Field(formItem
, 2);
2203 curl_formadd(&conn
->httpPostFirst
,
2204 &conn
->httpPostLast
,
2206 String_val(Field(formItem
, 0)),
2207 CURLFORM_NAMELENGTH
,
2208 caml_string_length(Field(formItem
, 0)),
2210 String_val(Field(formItem
, 1)),
2211 CURLFORM_CONTENTTYPE
,
2212 String_val(Field(contentType
, 0)),
2217 caml_failwith("Incorrect CURLFORM_FILE parameters");
2221 case 3: /* CURLFORM_BUFFER */
2222 if (Wosize_val(formItem
) < 4)
2224 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
2227 if (Is_long(Field(formItem
, 3)) &&
2228 Long_val(Field(formItem
, 3)) == 0)
2230 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
2232 curl_formadd(&conn
->httpPostFirst
,
2233 &conn
->httpPostLast
,
2235 String_val(Field(formItem
, 0)),
2236 CURLFORM_NAMELENGTH
,
2237 caml_string_length(Field(formItem
, 0)),
2239 String_val(Field(formItem
, 1)),
2241 conn
->httpPostBuffers
->data
,
2242 CURLFORM_BUFFERLENGTH
,
2243 caml_string_length(Field(formItem
, 2)),
2246 else if (Is_block(Field(formItem
, 3)))
2248 conn
->httpPostBuffers
= curl_slist_prepend_ml(conn
->httpPostBuffers
, Field(formItem
, 2));
2250 contentType
= Field(formItem
, 3);
2252 curl_formadd(&conn
->httpPostFirst
,
2253 &conn
->httpPostLast
,
2255 String_val(Field(formItem
, 0)),
2256 CURLFORM_NAMELENGTH
,
2257 caml_string_length(Field(formItem
, 0)),
2259 String_val(Field(formItem
, 1)),
2261 conn
->httpPostBuffers
->data
,
2262 CURLFORM_BUFFERLENGTH
,
2263 caml_string_length(Field(formItem
, 2)),
2264 CURLFORM_CONTENTTYPE
,
2265 String_val(Field(contentType
, 0)),
2270 caml_failwith("Incorrect CURLFORM_BUFFER parameters");
2275 listIter
= Field(listIter
, 1);
2278 result
= curl_easy_setopt(conn
->handle
,
2280 conn
->httpPostFirst
);
2282 if (result
!= CURLE_OK
)
2283 raiseError(conn
, result
);
2288 SETOPT_STRING( SSLCERT
)
2289 SETOPT_STRING( SSLCERTTYPE
)
2290 SETOPT_STRING( SSLCERTPASSWD
)
2291 SETOPT_STRING( SSLKEY
)
2292 SETOPT_STRING( SSLKEYTYPE
)
2293 SETOPT_STRING( SSLKEYPASSWD
)
2294 SETOPT_STRING( SSLENGINE
)
2295 SETOPT_BOOL( SSLENGINE_DEFAULT
)
2298 SETOPT_SLIST( QUOTE
)
2299 SETOPT_SLIST( POSTQUOTE
)
2301 SETOPT_STRING( COOKIEFILE
)
2302 #if HAVE_DECL_CURLOPT_CERTINFO
2303 SETOPT_BOOL( CERTINFO
)
2306 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_0
2307 #define CURL_SSLVERSION_TLSv1_0 CURL_SSLVERSION_TLSv1
2310 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_1
2311 #define CURL_SSLVERSION_TLSv1_1 CURL_SSLVERSION_TLSv1
2314 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_2
2315 #define CURL_SSLVERSION_TLSv1_2 CURL_SSLVERSION_TLSv1
2318 #if !HAVE_DECL_CURL_SSLVERSION_TLSV1_3
2319 #define CURL_SSLVERSION_TLSv1_3 CURL_SSLVERSION_TLSv1
2322 static void handle_SSLVERSION(Connection
*conn
, value option
)
2325 CURLcode result
= CURLE_OK
;
2326 int v
= CURL_SSLVERSION_DEFAULT
;
2328 switch (Long_val(option
))
2330 case 0: v
= CURL_SSLVERSION_DEFAULT
; break;
2331 case 1: v
= CURL_SSLVERSION_TLSv1
; break;
2332 case 2: v
= CURL_SSLVERSION_SSLv2
; break;
2333 case 3: v
= CURL_SSLVERSION_SSLv3
; break;
2334 case 4: v
= CURL_SSLVERSION_TLSv1_0
; break;
2335 case 5: v
= CURL_SSLVERSION_TLSv1_1
; break;
2336 case 6: v
= CURL_SSLVERSION_TLSv1_2
; break;
2337 case 7: v
= CURL_SSLVERSION_TLSv1_3
; break;
2339 caml_failwith("Invalid SSLVERSION Option");
2343 result
= curl_easy_setopt(conn
->handle
, CURLOPT_SSLVERSION
, v
);
2345 if (result
!= CURLE_OK
)
2346 raiseError(conn
, result
);
2351 static void handle_TIMECONDITION(Connection
*conn
, value option
)
2354 CURLcode result
= CURLE_OK
;
2355 int timecond
= CURL_TIMECOND_NONE
;
2357 switch (Long_val(option
))
2359 case 0: timecond
= CURL_TIMECOND_NONE
; break;
2360 case 1: timecond
= CURL_TIMECOND_IFMODSINCE
; break;
2361 case 2: timecond
= CURL_TIMECOND_IFUNMODSINCE
; break;
2362 case 3: timecond
= CURL_TIMECOND_LASTMOD
; break;
2364 caml_failwith("Invalid TIMECOND Option");
2368 result
= curl_easy_setopt(conn
->handle
, CURLOPT_TIMECONDITION
, timecond
);
2370 if (result
!= CURLE_OK
)
2371 raiseError(conn
, result
);
2376 SETOPT_VAL( TIMEVALUE
, Int32_val
)
2377 SETOPT_STRING( CUSTOMREQUEST
)
2378 SETOPT_STRING( INTERFACE
)
2380 static void handle_KRB4LEVEL(Connection
*conn
, value option
)
2383 CURLcode result
= CURLE_OK
;
2385 switch (Long_val(option
))
2387 case 0: /* KRB4_NONE */
2388 result
= curl_easy_setopt(conn
->handle
,
2393 case 1: /* KRB4_CLEAR */
2394 result
= curl_easy_setopt(conn
->handle
,
2399 case 2: /* KRB4_SAFE */
2400 result
= curl_easy_setopt(conn
->handle
,
2405 case 3: /* KRB4_CONFIDENTIAL */
2406 result
= curl_easy_setopt(conn
->handle
,
2411 case 4: /* KRB4_PRIVATE */
2412 result
= curl_easy_setopt(conn
->handle
,
2418 caml_failwith("Invalid KRB4 Option");
2422 if (result
!= CURLE_OK
)
2423 raiseError(conn
, result
);
2428 SETOPT_BOOL( SSL_VERIFYPEER
)
2429 SETOPT_STRING( CAINFO
)
2430 SETOPT_STRING( CAPATH
)
2431 SETOPT_BOOL( FILETIME
)
2432 SETOPT_LONG( MAXREDIRS
)
2433 SETOPT_LONG( MAXCONNECTS
)
2435 static void handle_CLOSEPOLICY(Connection
*conn
, value option
)
2438 CURLcode result
= CURLE_OK
;
2440 switch (Long_val(option
))
2442 case 0: /* CLOSEPOLICY_OLDEST */
2443 result
= curl_easy_setopt(conn
->handle
,
2444 CURLOPT_CLOSEPOLICY
,
2445 CURLCLOSEPOLICY_OLDEST
);
2448 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
2449 result
= curl_easy_setopt(conn
->handle
,
2450 CURLOPT_CLOSEPOLICY
,
2451 CURLCLOSEPOLICY_LEAST_RECENTLY_USED
);
2455 caml_failwith("Invalid CLOSEPOLICY Option");
2459 if (result
!= CURLE_OK
)
2460 raiseError(conn
, result
);
2465 SETOPT_BOOL( FRESH_CONNECT
)
2466 SETOPT_BOOL( FORBID_REUSE
)
2467 SETOPT_STRING( RANDOM_FILE
)
2468 SETOPT_STRING( EGDSOCKET
)
2469 SETOPT_LONG( CONNECTTIMEOUT
)
2470 SETOPT_BOOL( HTTPGET
)
2472 static void handle_SSL_VERIFYHOST(Connection
*conn
, value option
)
2475 CURLcode result
= CURLE_OK
;
2477 switch (Long_val(option
))
2479 case 0: /* SSLVERIFYHOST_NONE */
2480 case 1: /* SSLVERIFYHOST_EXISTENCE */
2481 case 2: /* SSLVERIFYHOST_HOSTNAME */
2482 result
= curl_easy_setopt(conn
->handle
,
2483 CURLOPT_SSL_VERIFYHOST
,
2484 /* map EXISTENCE to HOSTNAME */
2485 Long_val(option
) == 0 ? 0 : 2);
2489 caml_failwith("Invalid SSLVERIFYHOST Option");
2493 if (result
!= CURLE_OK
)
2494 raiseError(conn
, result
);
2499 SETOPT_STRING( COOKIEJAR
)
2500 SETOPT_STRING( SSL_CIPHER_LIST
)
2502 static void handle_HTTP_VERSION(Connection
*conn
, value option
)
2505 CURLcode result
= CURLE_OK
;
2507 long version
= CURL_HTTP_VERSION_NONE
;
2509 switch (Long_val(option
))
2511 case 0: version
= CURL_HTTP_VERSION_NONE
; break;
2512 case 1: version
= CURL_HTTP_VERSION_1_0
; break;
2513 case 2: version
= CURL_HTTP_VERSION_1_1
; break;
2515 #if HAVE_DECL_CURL_HTTP_VERSION_2
2516 version
= CURL_HTTP_VERSION_2
;
2517 #elif HAVE_DECL_CURL_HTTP_VERSION_2_0
2518 version
= CURL_HTTP_VERSION_2_0
;
2522 #if HAVE_DECL_CURL_HTTP_VERSION_2TLS
2523 version
= CURL_HTTP_VERSION_2TLS
;
2527 #if HAVE_DECL_CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
2528 version
= CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
;
2532 #if HAVE_DECL_CURL_HTTP_VERSION_3
2533 version
= CURL_HTTP_VERSION_3
;
2536 /* sync check_enums */
2538 caml_invalid_argument("CURLOPT_HTTP_VERSION");
2542 result
= curl_easy_setopt(conn
->handle
, CURLOPT_HTTP_VERSION
, version
);
2544 if (result
!= CURLE_OK
)
2545 raiseError(conn
, result
);
2550 static long ocaml_HTTP_VERSION(long curl_version
)
2552 switch (curl_version
)
2554 case CURL_HTTP_VERSION_NONE
: return 0;
2555 case CURL_HTTP_VERSION_1_0
: return 1;
2556 case CURL_HTTP_VERSION_1_1
: return 2;
2557 #if HAVE_DECL_CURL_HTTP_VERSION_2
2558 case CURL_HTTP_VERSION_2
: return 3;
2559 #elif HAVE_DECL_CURL_HTTP_VERSION_2_0
2560 case CURL_HTTP_VERSION_2_0
: return 3;
2562 #if HAVE_DECL_CURL_HTTP_VERSION_2TLS
2563 case CURL_HTTP_VERSION_2TLS
: return 4;
2565 #if HAVE_DECL_CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
2566 case CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE
: return 5;
2568 #if HAVE_DECL_CURL_HTTP_VERSION_3
2569 case CURL_HTTP_VERSION_3
: return 6;
2571 /* sync check_enums */
2576 SETOPT_BOOL( FTP_USE_EPSV
)
2577 SETOPT_LONG( DNS_CACHE_TIMEOUT
)
2578 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE
)
2580 static void handle_PRIVATE(Connection
*conn
, value option
)
2583 Store_field(conn
->ocamlValues
, Ocaml_PRIVATE
, option
);
2587 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2588 SETOPT_SLIST( HTTP200ALIASES
)
2591 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2592 SETOPT_BOOL( UNRESTRICTED_AUTH
)
2595 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2596 SETOPT_BOOL( FTP_USE_EPRT
)
2599 #if HAVE_DECL_CURLOPT_HTTPAUTH
2600 static void handle_HTTPAUTH(Connection
*conn
, value option
)
2603 CAMLlocal1(listIter
);
2604 CURLcode result
= CURLE_OK
;
2605 long auth
= CURLAUTH_NONE
;
2609 while (!Is_long(listIter
))
2611 switch (Long_val(Field(listIter
, 0)))
2613 case 0: /* CURLAUTH_BASIC */
2614 auth
|= CURLAUTH_BASIC
;
2617 case 1: /* CURLAUTH_DIGEST */
2618 auth
|= CURLAUTH_DIGEST
;
2621 case 2: /* CURLAUTH_GSSNEGOTIATE */
2622 auth
|= CURLAUTH_GSSNEGOTIATE
;
2625 case 3: /* CURLAUTH_NTLM */
2626 auth
|= CURLAUTH_NTLM
;
2629 case 4: /* CURLAUTH_ANY */
2630 auth
|= CURLAUTH_ANY
;
2633 case 5: /* CURLAUTH_ANYSAFE */
2634 auth
|= CURLAUTH_ANYSAFE
;
2638 caml_failwith("Invalid HTTPAUTH Value");
2642 listIter
= Field(listIter
, 1);
2645 result
= curl_easy_setopt(conn
->handle
,
2649 if (result
!= CURLE_OK
)
2650 raiseError(conn
, result
);
2656 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2657 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS
)
2660 #if HAVE_DECL_CURLOPT_PROXYAUTH
2661 static void handle_PROXYAUTH(Connection
*conn
, value option
)
2664 CAMLlocal1(listIter
);
2665 CURLcode result
= CURLE_OK
;
2666 long auth
= CURLAUTH_NONE
;
2670 while (!Is_long(listIter
))
2672 switch (Long_val(Field(listIter
, 0)))
2674 case 0: /* CURLAUTH_BASIC */
2675 auth
|= CURLAUTH_BASIC
;
2678 case 1: /* CURLAUTH_DIGEST */
2679 auth
|= CURLAUTH_DIGEST
;
2682 case 2: /* CURLAUTH_GSSNEGOTIATE */
2683 auth
|= CURLAUTH_GSSNEGOTIATE
;
2686 case 3: /* CURLAUTH_NTLM */
2687 auth
|= CURLAUTH_NTLM
;
2690 case 4: /* CURLAUTH_ANY */
2691 auth
|= CURLAUTH_ANY
;
2694 case 5: /* CURLAUTH_ANYSAFE */
2695 auth
|= CURLAUTH_ANYSAFE
;
2699 caml_failwith("Invalid HTTPAUTH Value");
2703 listIter
= Field(listIter
, 1);
2706 result
= curl_easy_setopt(conn
->handle
,
2710 if (result
!= CURLE_OK
)
2711 raiseError(conn
, result
);
2717 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2718 SETOPT_LONG( FTP_RESPONSE_TIMEOUT
)
2721 #if HAVE_DECL_CURLOPT_IPRESOLVE
2722 static void handle_IPRESOLVE(Connection
*conn
, value option
)
2725 CURLcode result
= CURLE_OK
;
2727 switch (Long_val(option
))
2729 case 0: /* CURL_IPRESOLVE_WHATEVER */
2730 result
= curl_easy_setopt(conn
->handle
,
2732 CURL_IPRESOLVE_WHATEVER
);
2735 case 1: /* CURL_IPRESOLVE_V4 */
2736 result
= curl_easy_setopt(conn
->handle
,
2741 case 2: /* CURL_IPRESOLVE_V6 */
2742 result
= curl_easy_setopt(conn
->handle
,
2748 caml_failwith("Invalid IPRESOLVE Value");
2752 if (result
!= CURLE_OK
)
2753 raiseError(conn
, result
);
2759 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2760 SETOPT_VAL( MAXFILESIZE
, Int32_val
)
2763 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2764 SETOPT_INT64( INFILESIZE_LARGE
)
2767 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2768 SETOPT_INT64( RESUME_FROM_LARGE
)
2771 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2772 SETOPT_INT64( MAXFILESIZE_LARGE
)
2775 #if HAVE_DECL_CURLOPT_NETRC_FILE
2776 SETOPT_STRING( NETRC_FILE
)
2779 #if HAVE_DECL_CURLOPT_FTP_SSL
2780 static void handle_FTP_SSL(Connection
*conn
, value option
)
2783 CURLcode result
= CURLE_OK
;
2785 switch (Long_val(option
))
2787 case 0: /* CURLFTPSSL_NONE */
2788 result
= curl_easy_setopt(conn
->handle
,
2793 case 1: /* CURLFTPSSL_TRY */
2794 result
= curl_easy_setopt(conn
->handle
,
2799 case 2: /* CURLFTPSSL_CONTROL */
2800 result
= curl_easy_setopt(conn
->handle
,
2802 CURLFTPSSL_CONTROL
);
2805 case 3: /* CURLFTPSSL_ALL */
2806 result
= curl_easy_setopt(conn
->handle
,
2812 caml_failwith("Invalid FTP_SSL Value");
2816 if (result
!= CURLE_OK
)
2817 raiseError(conn
, result
);
2823 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2824 SETOPT_INT64( POSTFIELDSIZE_LARGE
)
2827 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2828 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2829 SETOPT_VAL_( handle_TCP_NODELAY
, CURLOPT_TCP_NODELAY
, Bool_val
)
2832 /* windows headers define TCP_FASTOPEN and there is no (sane) way to prevent macro expansion of the symbol below */
2835 #if HAVE_DECL_CURLOPT_TCP_FASTOPEN
2836 SETOPT_BOOL( TCP_FASTOPEN
)
2839 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2840 static void handle_FTPSSLAUTH(Connection
*conn
, value option
)
2843 CURLcode result
= CURLE_OK
;
2845 switch (Long_val(option
))
2847 case 0: /* CURLFTPAUTH_DEFAULT */
2848 result
= curl_easy_setopt(conn
->handle
,
2850 CURLFTPAUTH_DEFAULT
);
2853 case 1: /* CURLFTPAUTH_SSL */
2854 result
= curl_easy_setopt(conn
->handle
,
2859 case 2: /* CURLFTPAUTH_TLS */
2860 result
= curl_easy_setopt(conn
->handle
,
2866 caml_failwith("Invalid FTPSSLAUTH value");
2870 if (result
!= CURLE_OK
)
2871 raiseError(conn
, result
);
2877 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2878 SETOPT_STRING( FTP_ACCOUNT
)
2881 #if HAVE_DECL_CURLOPT_COOKIELIST
2882 SETOPT_STRING( COOKIELIST
)
2885 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2886 SETOPT_BOOL( IGNORE_CONTENT_LENGTH
)
2889 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2890 SETOPT_BOOL( FTP_SKIP_PASV_IP
)
2893 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2894 static void handle_FTP_FILEMETHOD(Connection
*conn
, value option
)
2897 CURLcode result
= CURLE_OK
;
2899 switch (Long_val(option
))
2901 case 0: /* CURLFTPMETHOD_DEFAULT */
2902 result
= curl_easy_setopt(conn
->handle
,
2903 CURLOPT_FTP_FILEMETHOD
,
2904 CURLFTPMETHOD_DEFAULT
);
2907 case 1: /* CURLFTMETHOD_MULTICWD */
2908 result
= curl_easy_setopt(conn
->handle
,
2909 CURLOPT_FTP_FILEMETHOD
,
2910 CURLFTPMETHOD_MULTICWD
);
2913 case 2: /* CURLFTPMETHOD_NOCWD */
2914 result
= curl_easy_setopt(conn
->handle
,
2915 CURLOPT_FTP_FILEMETHOD
,
2916 CURLFTPMETHOD_NOCWD
);
2919 case 3: /* CURLFTPMETHOD_SINGLECWD */
2920 result
= curl_easy_setopt(conn
->handle
,
2921 CURLOPT_FTP_FILEMETHOD
,
2922 CURLFTPMETHOD_SINGLECWD
);
2925 caml_failwith("Invalid FTP_FILEMETHOD value");
2929 if (result
!= CURLE_OK
)
2930 raiseError(conn
, result
);
2936 #if HAVE_DECL_CURLOPT_LOCALPORT
2937 SETOPT_LONG( LOCALPORT
)
2940 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2941 SETOPT_LONG( LOCALPORTRANGE
)
2944 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2945 SETOPT_BOOL( CONNECT_ONLY
)
2948 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2949 SETOPT_INT64( MAX_SEND_SPEED_LARGE
)
2952 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2953 SETOPT_INT64( MAX_RECV_SPEED_LARGE
)
2956 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2957 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER
)
2960 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2961 SETOPT_BOOL( SSL_SESSIONID_CACHE
)
2964 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2965 static void handle_SSH_AUTH_TYPES(Connection
*conn
, value option
)
2968 CAMLlocal1(listIter
);
2969 CURLcode result
= CURLE_OK
;
2970 long authTypes
= CURLSSH_AUTH_NONE
;
2974 while (!Is_long(listIter
))
2976 switch (Long_val(Field(listIter
, 0)))
2978 case 0: /* CURLSSH_AUTH_ANY */
2979 authTypes
|= CURLSSH_AUTH_ANY
;
2982 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2983 authTypes
|= CURLSSH_AUTH_PUBLICKEY
;
2986 case 2: /* CURLSSH_AUTH_PASSWORD */
2987 authTypes
|= CURLSSH_AUTH_PASSWORD
;
2990 case 3: /* CURLSSH_AUTH_HOST */
2991 authTypes
|= CURLSSH_AUTH_HOST
;
2994 case 4: /* CURLSSH_AUTH_KEYBOARD */
2995 authTypes
|= CURLSSH_AUTH_KEYBOARD
;
2999 caml_failwith("Invalid CURLSSH_AUTH_TYPES Value");
3003 listIter
= Field(listIter
, 1);
3006 result
= curl_easy_setopt(conn
->handle
,
3007 CURLOPT_SSH_AUTH_TYPES
,
3010 if (result
!= CURLE_OK
)
3011 raiseError(conn
, result
);
3017 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
3018 SETOPT_STRING( SSH_PUBLIC_KEYFILE
)
3021 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
3022 SETOPT_STRING( SSH_PRIVATE_KEYFILE
)
3025 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3026 static void handle_FTP_SSL_CCC(Connection
*conn
, value option
)
3029 CURLcode result
= CURLE_OK
;
3031 switch (Long_val(option
))
3033 case 0: /* CURLFTPSSL_CCC_NONE */
3034 result
= curl_easy_setopt(conn
->handle
,
3035 CURLOPT_FTP_SSL_CCC
,
3036 CURLFTPSSL_CCC_NONE
);
3039 case 1: /* CURLFTPSSL_CCC_PASSIVE */
3040 result
= curl_easy_setopt(conn
->handle
,
3041 CURLOPT_FTP_SSL_CCC
,
3042 CURLFTPSSL_CCC_PASSIVE
);
3045 case 2: /* CURLFTPSSL_CCC_ACTIVE */
3046 result
= curl_easy_setopt(conn
->handle
,
3047 CURLOPT_FTP_SSL_CCC
,
3048 CURLFTPSSL_CCC_ACTIVE
);
3052 caml_failwith("Invalid FTPSSL_CCC value");
3056 if (result
!= CURLE_OK
)
3057 raiseError(conn
, result
);
3063 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3064 SETOPT_LONG( TIMEOUT_MS
)
3067 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3068 SETOPT_LONG( CONNECTTIMEOUT_MS
)
3071 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3072 SETOPT_BOOL( HTTP_TRANSFER_DECODING
)
3075 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3076 SETOPT_BOOL( HTTP_CONTENT_DECODING
)
3079 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3080 SETOPT_LONG( NEW_FILE_PERMS
)
3083 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3084 SETOPT_LONG( NEW_DIRECTORY_PERMS
)
3087 #if HAVE_DECL_CURLOPT_POST301
3088 SETOPT_BOOL( POST301
)
3091 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3092 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5
)
3095 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3096 SETOPT_STRING( COPYPOSTFIELDS
)
3099 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3100 SETOPT_BOOL( PROXY_TRANSFER_MODE
)
3103 #if HAVE_DECL_CURLOPT_AUTOREFERER
3104 SETOPT_BOOL( AUTOREFERER
)
3107 #if HAVE_DECL_CURLOPT_PROXYTYPE
3108 static void handle_PROXYTYPE(Connection
*conn
, value option
)
3111 CURLcode result
= CURLE_OK
;
3114 switch (Long_val(option
))
3116 case 0: proxy_type
= CURLPROXY_HTTP
; break;
3117 case 1: proxy_type
= CURLPROXY_HTTP_1_0
; break;
3118 case 2: proxy_type
= CURLPROXY_SOCKS4
; break;
3119 case 3: proxy_type
= CURLPROXY_SOCKS5
; break;
3120 case 4: proxy_type
= CURLPROXY_SOCKS4A
; break;
3121 case 5: proxy_type
= CURLPROXY_SOCKS5_HOSTNAME
; break;
3123 caml_failwith("Invalid curl proxy type");
3126 result
= curl_easy_setopt(conn
->handle
,
3130 if (result
!= CURLE_OK
)
3131 raiseError(conn
, result
);
3137 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3142 CURLPROTO_HTTP
, CURLPROTO_HTTPS
, CURLPROTO_FTP
, CURLPROTO_FTPS
, CURLPROTO_SCP
, CURLPROTO_SFTP
,
3143 CURLPROTO_TELNET
, CURLPROTO_LDAP
, CURLPROTO_LDAPS
, CURLPROTO_DICT
, CURLPROTO_FILE
, CURLPROTO_TFTP
,
3144 /* factor out with autoconf? */
3145 #if defined(CURLPROTO_IMAP)
3150 #if defined(CURLPROTO_IMAPS)
3155 #if defined(CURLPROTO_POP3)
3160 #if defined(CURLPROTO_POP3S)
3165 #if defined(CURLPROTO_SMTP)
3170 #if defined(CURLPROTO_SMTPS)
3175 #if defined(CURLPROTO_RTSP)
3180 #if defined(CURLPROTO_RTMP)
3185 #if defined(CURLPROTO_RTMPT)
3190 #if defined(CURLPROTO_RTMPE)
3195 #if defined(CURLPROTO_RTMPTE)
3200 #if defined(CURLPROTO_RTMPS)
3205 #if defined(CURLPROTO_RTMPTS)
3210 #if defined(CURLPROTO_GOPHER)
3217 static void handle_PROTOCOLSOPTION(CURLoption curlopt
, Connection
*conn
, value option
)
3220 CURLcode result
= CURLE_OK
;
3221 long bits
= convert_bit_list(protoMap
, sizeof(protoMap
) / sizeof(protoMap
[0]), option
);
3223 result
= curl_easy_setopt(conn
->handle
, curlopt
, bits
);
3225 if (result
!= CURLE_OK
)
3226 raiseError(conn
, result
);
3232 #if HAVE_DECL_CURLOPT_PROTOCOLS
3233 static void handle_PROTOCOLS(Connection
*conn
, value option
)
3235 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS
, conn
, option
);
3239 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3240 static void handle_REDIR_PROTOCOLS(Connection
*conn
, value option
)
3242 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS
, conn
, option
);
3246 #if HAVE_DECL_CURLOPT_RESOLVE
3247 SETOPT_SLIST( RESOLVE
)
3250 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3251 SETOPT_STRING( DNS_SERVERS
)
3254 #if HAVE_DECL_CURLOPT_MAIL_FROM
3255 SETOPT_STRING( MAIL_FROM
)
3258 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3259 SETOPT_SLIST( MAIL_RCPT
)
3262 #if HAVE_DECL_CURLOPT_PIPEWAIT
3263 SETOPT_BOOL( PIPEWAIT
)
3266 #if HAVE_DECL_CURLOPT_USERNAME
3267 SETOPT_STRING( USERNAME
)
3270 #if HAVE_DECL_CURLOPT_PASSWORD
3271 SETOPT_STRING( PASSWORD
)
3274 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
3275 SETOPT_STRING( LOGIN_OPTIONS
)
3278 #if HAVE_DECL_CURLOPT_CONNECT_TO
3279 SETOPT_SLIST( CONNECT_TO
)
3282 #if HAVE_DECL_CURLOPT_POSTREDIR
3284 static int curlPostRedir_table
[] = {
3285 CURL_REDIR_POST_ALL
,
3286 #if defined(CURL_REDIR_POST_301)
3287 CURL_REDIR_POST_301
,
3291 #if defined(CURL_REDIR_POST_302)
3292 CURL_REDIR_POST_302
,
3296 #if defined(CURL_REDIR_POST_303)
3297 CURL_REDIR_POST_303
,
3303 static void handle_POSTREDIR(Connection
*conn
, value option
)
3306 CURLcode result
= CURLE_OK
;
3307 long bitmask
= caml_convert_flag_list(option
, curlPostRedir_table
);
3309 result
= curl_easy_setopt(conn
->handle
,
3313 if (result
!= CURLE_OK
)
3314 raiseError(conn
, result
);
3320 SETOPT_VAL( SSH_KNOWNHOSTS
, String_val
)
3322 SETOPT_LONG( BUFFERSIZE
)
3324 #if HAVE_DECL_CURLOPT_DOH_URL
3325 SETOPT_STRING( DOH_URL
)
3328 #if HAVE_DECL_CURLOPT_AWS_SIGV4
3329 SETOPT_STRING( AWS_SIGV4
)
3332 SETOPT_BOOL( TCP_KEEPALIVE
)
3334 SETOPT_LONG( TCP_KEEPIDLE
)
3336 SETOPT_LONG( TCP_KEEPINTVL
)
3339 ** curl_easy_setopt helper function
3342 #define HAVE(name) { handle_ ## name, "CURLOPT_"#name }
3343 #define HAVENOT(name) { NULL, "CURLOPT_"#name }
3345 CURLOptionMapping implementedOptionMap
[] =
3347 HAVE(WRITEFUNCTION
),
3353 HAVE(HTTPPROXYTUNNEL
),
3357 #if HAVE_DECL_CURLOPT_NOSIGNAL
3369 #if HAVE_DECL_CURLOPT_ENCODING
3374 HAVE(FOLLOWLOCATION
),
3380 HAVE(ERRORBUFFER
), /* mutable buffer, as output value, do not duplicate */
3383 HAVE(POSTFIELDSIZE
),
3387 HAVE(LOW_SPEED_LIMIT
),
3388 HAVE(LOW_SPEED_TIME
),
3395 HAVE(SSLCERTPASSWD
),
3400 HAVE(SSLENGINE_DEFAULT
),
3404 HAVE(HEADERFUNCTION
),
3407 HAVE(TIMECONDITION
),
3409 HAVE(CUSTOMREQUEST
),
3412 HAVE(PROGRESSFUNCTION
),
3413 HAVE(SSL_VERIFYPEER
),
3420 HAVE(FRESH_CONNECT
),
3424 HAVE(CONNECTTIMEOUT
),
3426 HAVE(SSL_VERIFYHOST
),
3428 HAVE(SSL_CIPHER_LIST
),
3431 HAVE(DNS_CACHE_TIMEOUT
),
3432 HAVE(DNS_USE_GLOBAL_CACHE
),
3433 HAVE(DEBUGFUNCTION
),
3435 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
3436 HAVE(HTTP200ALIASES
),
3438 HAVENOT(HTTP200ALIASES
),
3440 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
3441 HAVE(UNRESTRICTED_AUTH
),
3443 HAVENOT(UNRESTRICTED_AUTH
),
3445 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
3448 HAVENOT(FTP_USE_EPRT
),
3450 #if HAVE_DECL_CURLOPT_HTTPAUTH
3455 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
3456 HAVE(FTP_CREATE_MISSING_DIRS
),
3458 HAVENOT(FTP_CREATE_MISSING_DIRS
),
3460 #if HAVE_DECL_CURLOPT_PROXYAUTH
3465 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
3466 HAVE(FTP_RESPONSE_TIMEOUT
),
3468 HAVENOT(FTP_RESPONSE_TIMEOUT
),
3470 #if HAVE_DECL_CURLOPT_IPRESOLVE
3475 #if HAVE_DECL_CURLOPT_MAXFILESIZE
3478 HAVENOT(MAXFILESIZE
),
3480 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
3481 HAVE(INFILESIZE_LARGE
),
3483 HAVENOT(INFILESIZE_LARGE
),
3485 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
3486 HAVE(RESUME_FROM_LARGE
),
3488 HAVENOT(RESUME_FROM_LARGE
),
3490 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
3491 HAVE(MAXFILESIZE_LARGE
),
3493 HAVENOT(MAXFILESIZE_LARGE
),
3495 #if HAVE_DECL_CURLOPT_NETRC_FILE
3498 HAVENOT(NETRC_FILE
),
3500 #if HAVE_DECL_CURLOPT_FTP_SSL
3505 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
3506 HAVE(POSTFIELDSIZE_LARGE
),
3508 HAVENOT(POSTFIELDSIZE_LARGE
),
3510 #if HAVE_DECL_CURLOPT_TCP_NODELAY
3513 HAVENOT(TCP_NODELAY
),
3515 #if HAVE_DECL_CURLOPT_TCP_FASTOPEN
3518 HAVENOT(TCP_FASTOPEN
),
3520 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
3523 HAVENOT(FTPSSLAUTH
),
3525 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
3526 HAVE(IOCTLFUNCTION
),
3528 HAVENOT(IOCTLFUNCTION
),
3530 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
3533 HAVENOT(FTP_ACCOUNT
),
3535 #if HAVE_DECL_CURLOPT_COOKIELIST
3538 HAVENOT(COOKIELIST
),
3540 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
3541 HAVE(IGNORE_CONTENT_LENGTH
),
3543 HAVENOT(IGNORE_CONTENT_LENGTH
),
3545 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
3546 HAVE(FTP_SKIP_PASV_IP
),
3548 HAVENOT(FTP_SKIP_PASV_IP
),
3550 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
3551 HAVE(FTP_FILEMETHOD
),
3553 HAVENOT(FTP_FILEMETHOD
),
3555 #if HAVE_DECL_CURLOPT_LOCALPORT
3560 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
3561 HAVE(LOCALPORTRANGE
),
3563 HAVENOT(LOCALPORTRANGE
),
3565 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
3568 HAVENOT(CONNECT_ONLY
),
3570 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
3571 HAVE(MAX_SEND_SPEED_LARGE
),
3573 HAVENOT(MAX_SEND_SPEED_LARGE
),
3575 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
3576 HAVE(MAX_RECV_SPEED_LARGE
),
3578 HAVENOT(MAX_RECV_SPEED_LARGE
),
3580 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
3581 HAVE(FTP_ALTERNATIVE_TO_USER
),
3583 HAVENOT(FTP_ALTERNATIVE_TO_USER
),
3585 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
3586 HAVE(SSL_SESSIONID_CACHE
),
3588 HAVENOT(SSL_SESSIONID_CACHE
),
3590 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
3591 HAVE(SSH_AUTH_TYPES
),
3593 HAVENOT(SSH_AUTH_TYPES
),
3595 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
3596 HAVE(SSH_PUBLIC_KEYFILE
),
3598 HAVENOT(SSH_PUBLIC_KEYFILE
),
3600 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
3601 HAVE(SSH_PRIVATE_KEYFILE
),
3603 HAVENOT(SSH_PRIVATE_KEYFILE
),
3605 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3608 HAVENOT(FTP_SSL_CCC
),
3610 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3613 HAVENOT(TIMEOUT_MS
),
3615 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3616 HAVE(CONNECTTIMEOUT_MS
),
3618 HAVENOT(CONNECTTIMEOUT_MS
),
3620 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3621 HAVE(HTTP_TRANSFER_DECODING
),
3623 HAVENOT(HTTP_TRANSFER_DECODING
),
3625 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3626 HAVE(HTTP_CONTENT_DECODING
),
3628 HAVENOT(HTTP_CONTENT_DECODING
),
3630 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3631 HAVE(NEW_FILE_PERMS
),
3633 HAVENOT(NEW_FILE_PERMS
),
3635 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3636 HAVE(NEW_DIRECTORY_PERMS
),
3638 HAVENOT(NEW_DIRECTORY_PERMS
),
3640 #if HAVE_DECL_CURLOPT_POST301
3645 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3646 HAVE(SSH_HOST_PUBLIC_KEY_MD5
),
3648 HAVENOT(SSH_HOST_PUBLIC_KEY_MD5
),
3650 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3651 HAVE(COPYPOSTFIELDS
),
3653 HAVENOT(COPYPOSTFIELDS
),
3655 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3656 HAVE(PROXY_TRANSFER_MODE
),
3658 HAVENOT(PROXY_TRANSFER_MODE
),
3660 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3663 HAVENOT(SEEKFUNCTION
),
3665 #if HAVE_DECL_CURLOPT_AUTOREFERER
3668 HAVENOT(AUTOREFERER
),
3670 HAVE(OPENSOCKETFUNCTION
),
3671 /*HAVE(CLOSESOCKETFUNCTION),*/
3672 #if HAVE_DECL_CURLOPT_PROXYTYPE
3677 #if HAVE_DECL_CURLOPT_PROTOCOLS
3682 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3683 HAVE(REDIR_PROTOCOLS
),
3685 HAVENOT(REDIR_PROTOCOLS
),
3687 #if HAVE_DECL_CURLOPT_RESOLVE
3692 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3695 HAVENOT(DNS_SERVERS
),
3697 #if HAVE_DECL_CURLOPT_MAIL_FROM
3702 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3707 #if HAVE_DECL_CURLOPT_PIPEWAIT
3712 #if HAVE_DECL_CURLOPT_CERTINFO
3717 #if HAVE_DECL_CURLOPT_USERNAME
3722 #if HAVE_DECL_CURLOPT_PASSWORD
3727 #if HAVE_DECL_CURLOPT_LOGIN_OPTIONS
3728 HAVE(LOGIN_OPTIONS
),
3730 HAVENOT(LOGIN_OPTIONS
),
3732 #if HAVE_DECL_CURLOPT_CONNECT_TO
3735 HAVENOT(CONNECT_TO
),
3737 #if HAVE_DECL_CURLOPT_POSTREDIR
3742 #if HAVE_DECL_CURLOPT_MIMEPOST
3747 HAVE(SSH_KNOWNHOSTS
),
3748 HAVE(SSH_KEYFUNCTION
),
3750 #if HAVE_DECL_CURLOPT_DOH_URL
3755 #if HAVE_DECL_CURLOPT_SSL_OPTIONS
3758 HAVENOT(SSL_OPTIONS
),
3760 HAVE(WRITEFUNCTION2
),
3761 HAVE(READFUNCTION2
),
3762 #if HAVE_DECL_CURLOPT_XFERINFOFUNCTION
3763 HAVE(XFERINFOFUNCTION
),
3765 HAVENOT(XFERINFOFUNCTION
),
3767 #if HAVE_DECL_CURLOPT_PREREQFUNCTION
3768 HAVE(PREREQFUNCTION
),
3770 HAVENOT(PREREQFUNCTION
),
3772 #if HAVE_DECL_CURLOPT_AWS_SIGV4
3777 HAVE(TCP_KEEPALIVE
),
3779 HAVE(TCP_KEEPINTVL
),
3782 value
caml_curl_easy_setopt(value conn
, value option
)
3784 CAMLparam2(conn
, option
);
3786 Connection
*connection
= Connection_val(conn
);
3787 CURLOptionMapping
* thisOption
= NULL
;
3788 static const value
* exception
= NULL
;
3790 checkConnection(connection
);
3792 data
= Field(option
, 0);
3794 if (Tag_val(option
) < sizeof(implementedOptionMap
)/sizeof(CURLOptionMapping
))
3796 thisOption
= &implementedOptionMap
[Tag_val(option
)];
3797 if (thisOption
->optionHandler
)
3799 thisOption
->optionHandler(connection
, data
);
3803 if (NULL
== exception
)
3805 exception
= caml_named_value("Curl.NotImplemented");
3806 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
3809 caml_raise_with_string(*exception
, thisOption
->name
);
3814 caml_failwith("Invalid CURLOPT Option");
3817 CAMLreturn(Val_unit
);
3821 ** curl_easy_perform helper function
3824 value
caml_curl_easy_perform(value conn
)
3827 CURLcode result
= CURLE_OK
;
3828 Connection
*connection
= Connection_val(conn
);
3830 checkConnection(connection
);
3832 caml_enter_blocking_section();
3833 result
= curl_easy_perform(connection
->handle
);
3834 caml_leave_blocking_section();
3836 if (result
!= CURLE_OK
)
3837 raiseError(connection
, result
);
3839 CAMLreturn(Val_unit
);
3843 ** curl_easy_cleanup helper function
3846 value
caml_curl_easy_cleanup(value conn
)
3849 Connection
*connection
= Connection_val(conn
);
3851 checkConnection(connection
);
3853 removeConnection(connection
, 0);
3855 CAMLreturn(Val_unit
);
3859 ** curl_easy_getinfo helper function
3862 enum GetInfoResultType
{
3863 StringValue
, LongValue
, DoubleValue
, StringListValue
, StringListListValue
,
3864 SocketValue
, OCamlValue
, /* keep last - no matching OCaml CURLINFO_ constructor */
3867 value
convertStringList(struct curl_slist
*p
)
3870 CAMLlocal3(result
, current
, next
);
3872 result
= Val_emptylist
;
3873 current
= Val_emptylist
;
3874 next
= Val_emptylist
;
3878 next
= caml_alloc_tuple(2);
3879 Store_field(next
, 0, caml_copy_string(p
->data
));
3880 Store_field(next
, 1, Val_emptylist
);
3882 if (result
== Val_emptylist
)
3885 if (current
!= Val_emptylist
)
3886 Store_field(current
, 1, next
);
3896 value
caml_curl_easy_getinfo(value conn
, value option
)
3898 CAMLparam2(conn
, option
);
3899 CAMLlocal3(result
, current
, next
);
3900 CURLcode curlResult
;
3901 Connection
*connection
= Connection_val(conn
);
3902 enum GetInfoResultType resultType
;
3903 char *strValue
= NULL
;
3906 curl_socket_t socketValue
;
3907 struct curl_slist
*stringListValue
= NULL
;
3908 #if HAVE_DECL_CURLINFO_CERTINFO
3911 struct curl_slist
*to_info
;
3912 struct curl_certinfo
*to_certinfo
;
3916 checkConnection(connection
);
3918 switch(Long_val(option
))
3920 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3921 case 0: /* CURLINFO_EFFECTIVE_URL */
3922 resultType
= StringValue
;
3924 curlResult
= curl_easy_getinfo(connection
->handle
,
3925 CURLINFO_EFFECTIVE_URL
,
3929 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3932 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3933 case 1: /* CURLINFO_HTTP_CODE */
3934 case 2: /* CURLINFO_RESPONSE_CODE */
3935 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3936 resultType
= LongValue
;
3938 curlResult
= curl_easy_getinfo(connection
->handle
,
3939 CURLINFO_RESPONSE_CODE
,
3942 resultType
= LongValue
;
3944 curlResult
= curl_easy_getinfo(connection
->handle
,
3951 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3952 case 3: /* CURLINFO_TOTAL_TIME */
3953 resultType
= DoubleValue
;
3955 curlResult
= curl_easy_getinfo(connection
->handle
,
3956 CURLINFO_TOTAL_TIME
,
3961 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3962 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3963 resultType
= DoubleValue
;
3965 curlResult
= curl_easy_getinfo(connection
->handle
,
3966 CURLINFO_NAMELOOKUP_TIME
,
3971 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3972 case 5: /* CURLINFO_CONNECT_TIME */
3973 resultType
= DoubleValue
;
3975 curlResult
= curl_easy_getinfo(connection
->handle
,
3976 CURLINFO_CONNECT_TIME
,
3981 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3982 case 6: /* CURLINFO_PRETRANSFER_TIME */
3983 resultType
= DoubleValue
;
3985 curlResult
= curl_easy_getinfo(connection
->handle
,
3986 CURLINFO_PRETRANSFER_TIME
,
3991 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3992 case 7: /* CURLINFO_SIZE_UPLOAD */
3993 resultType
= DoubleValue
;
3995 curlResult
= curl_easy_getinfo(connection
->handle
,
3996 CURLINFO_SIZE_UPLOAD
,
4001 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
4002 case 8: /* CURLINFO_SIZE_DOWNLOAD */
4003 resultType
= DoubleValue
;
4005 curlResult
= curl_easy_getinfo(connection
->handle
,
4006 CURLINFO_SIZE_DOWNLOAD
,
4011 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
4012 case 9: /* CURLINFO_SPEED_DOWNLOAD */
4013 resultType
= DoubleValue
;
4015 curlResult
= curl_easy_getinfo(connection
->handle
,
4016 CURLINFO_SPEED_DOWNLOAD
,
4021 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
4022 case 10: /* CURLINFO_SPEED_UPLOAD */
4023 resultType
= DoubleValue
;
4025 curlResult
= curl_easy_getinfo(connection
->handle
,
4026 CURLINFO_SPEED_UPLOAD
,
4032 #if HAVE_DECL_CURLINFO_HEADER_SIZE
4033 case 11: /* CURLINFO_HEADER_SIZE */
4034 resultType
= LongValue
;
4036 curlResult
= curl_easy_getinfo(connection
->handle
,
4037 CURLINFO_HEADER_SIZE
,
4042 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
4043 case 12: /* CURLINFO_REQUEST_SIZE */
4044 resultType
= LongValue
;
4046 curlResult
= curl_easy_getinfo(connection
->handle
,
4047 CURLINFO_REQUEST_SIZE
,
4052 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
4053 case 13: /* CURLINFO_SSL_VERIFYRESULT */
4054 resultType
= LongValue
;
4056 curlResult
= curl_easy_getinfo(connection
->handle
,
4057 CURLINFO_SSL_VERIFYRESULT
,
4062 #if HAVE_DECL_CURLINFO_FILETIME
4063 case 14: /* CURLINFO_FILETIME */
4064 resultType
= DoubleValue
;
4066 curlResult
= curl_easy_getinfo(connection
->handle
,
4070 doubleValue
= longValue
;
4074 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
4075 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
4076 resultType
= DoubleValue
;
4078 curlResult
= curl_easy_getinfo(connection
->handle
,
4079 CURLINFO_CONTENT_LENGTH_DOWNLOAD
,
4084 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
4085 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
4086 resultType
= DoubleValue
;
4088 curlResult
= curl_easy_getinfo(connection
->handle
,
4089 CURLINFO_CONTENT_LENGTH_UPLOAD
,
4094 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
4095 case 17: /* CURLINFO_STARTTRANSFER_TIME */
4096 resultType
= DoubleValue
;
4098 curlResult
= curl_easy_getinfo(connection
->handle
,
4099 CURLINFO_STARTTRANSFER_TIME
,
4104 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
4105 case 18: /* CURLINFO_CONTENT_TYPE */
4106 resultType
= StringValue
;
4108 curlResult
= curl_easy_getinfo(connection
->handle
,
4109 CURLINFO_CONTENT_TYPE
,
4114 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
4115 case 19: /* CURLINFO_REDIRECT_TIME */
4116 resultType
= DoubleValue
;
4118 curlResult
= curl_easy_getinfo(connection
->handle
,
4119 CURLINFO_REDIRECT_TIME
,
4124 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
4125 case 20: /* CURLINFO_REDIRECT_COUNT */
4126 resultType
= LongValue
;
4128 curlResult
= curl_easy_getinfo(connection
->handle
,
4129 CURLINFO_REDIRECT_COUNT
,
4134 case 21: /* CURLINFO_PRIVATE */
4135 resultType
= OCamlValue
;
4136 curlResult
= CURLE_OK
;
4137 result
= caml_alloc(1, StringValue
);
4138 Store_field(result
, 0, Field(connection
->ocamlValues
, Ocaml_PRIVATE
));
4141 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
4142 case 22: /* CURLINFO_HTTP_CONNECTCODE */
4143 resultType
= LongValue
;
4145 curlResult
= curl_easy_getinfo(connection
->handle
,
4146 CURLINFO_HTTP_CONNECTCODE
,
4151 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
4152 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
4153 resultType
= LongValue
;
4155 curlResult
= curl_easy_getinfo(connection
->handle
,
4156 CURLINFO_HTTPAUTH_AVAIL
,
4161 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
4162 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
4163 resultType
= LongValue
;
4165 curlResult
= curl_easy_getinfo(connection
->handle
,
4166 CURLINFO_PROXYAUTH_AVAIL
,
4171 #if HAVE_DECL_CURLINFO_OS_ERRNO
4172 case 25: /* CURLINFO_OS_ERRNO */
4173 resultType
= LongValue
;
4175 curlResult
= curl_easy_getinfo(connection
->handle
,
4181 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
4182 case 26: /* CURLINFO_NUM_CONNECTS */
4183 resultType
= LongValue
;
4185 curlResult
= curl_easy_getinfo(connection
->handle
,
4186 CURLINFO_NUM_CONNECTS
,
4191 #if HAVE_DECL_CURLINFO_SSL_ENGINES
4192 case 27: /* CURLINFO_SSL_ENGINES */
4193 resultType
= StringListValue
;
4195 curlResult
= curl_easy_getinfo(connection
->handle
,
4196 CURLINFO_SSL_ENGINES
,
4201 #if HAVE_DECL_CURLINFO_COOKIELIST
4202 case 28: /* CURLINFO_COOKIELIST */
4203 resultType
= StringListValue
;
4205 curlResult
= curl_easy_getinfo(connection
->handle
,
4206 CURLINFO_COOKIELIST
,
4211 #if HAVE_DECL_CURLINFO_LASTSOCKET
4212 case 29: /* CURLINFO_LASTSOCKET */
4213 resultType
= LongValue
;
4215 curlResult
= curl_easy_getinfo(connection
->handle
,
4216 CURLINFO_LASTSOCKET
,
4221 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
4222 case 30: /* CURLINFO_FTP_ENTRY_PATH */
4223 resultType
= StringValue
;
4225 curlResult
= curl_easy_getinfo(connection
->handle
,
4226 CURLINFO_FTP_ENTRY_PATH
,
4231 #if HAVE_DECL_CURLINFO_REDIRECT_URL
4232 case 31: /* CURLINFO_REDIRECT_URL */
4233 resultType
= StringValue
;
4235 curlResult
= curl_easy_getinfo(connection
->handle
,
4236 CURLINFO_REDIRECT_URL
,
4240 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
4243 #if HAVE_DECL_CURLINFO_PRIMARY_IP
4244 case 32: /* CURLINFO_PRIMARY_IP */
4245 resultType
= StringValue
;
4247 curlResult
= curl_easy_getinfo(connection
->handle
,
4248 CURLINFO_PRIMARY_IP
,
4252 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
4255 #if HAVE_DECL_CURLINFO_LOCAL_IP
4256 case 33: /* CURLINFO_LOCAL_IP */
4257 resultType
= StringValue
;
4259 curlResult
= curl_easy_getinfo(connection
->handle
,
4264 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
4267 #if HAVE_DECL_CURLINFO_LOCAL_PORT
4268 case 34: /* CURLINFO_LOCAL_PORT */
4269 resultType
= LongValue
;
4271 curlResult
= curl_easy_getinfo(connection
->handle
,
4272 CURLINFO_LOCAL_PORT
,
4276 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
4279 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
4280 case 35: /* CURLINFO_CONDITION_UNMET */
4281 resultType
= LongValue
;
4283 curlResult
= curl_easy_getinfo(connection
->handle
,
4284 CURLINFO_CONDITION_UNMET
,
4288 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
4290 #if HAVE_DECL_CURLINFO_CERTINFO
4291 case 36: /* CURLINFO_CERTINFO */
4292 resultType
= StringListListValue
;
4294 curlResult
= curl_easy_getinfo(connection
->handle
,
4298 result
= Val_emptylist
;
4299 current
= Val_emptylist
;
4300 next
= Val_emptylist
;
4302 if (curlResult
!= CURLE_OK
|| !ptr
.to_info
)
4305 for (i
= 0; i
< ptr
.to_certinfo
->num_of_certs
; i
++) {
4306 next
= caml_alloc_tuple(2);
4307 Store_field(next
, 0, convertStringList(ptr
.to_certinfo
->certinfo
[i
]));
4308 Store_field(next
, 1, current
);
4313 #pragma message("libcurl does not provide CURLINFO_CERTINFO")
4315 #if HAVE_DECL_CURLINFO_ACTIVESOCKET
4316 case 37: /* CURLINFO_ACTIVESOCKET */
4317 resultType
= SocketValue
;
4319 curlResult
= curl_easy_getinfo(connection
->handle
,
4320 CURLINFO_ACTIVESOCKET
,
4324 #pragma message("libcurl does not provide CURLINFO_ACTIVESOCKET")
4326 #if HAVE_DECL_CURLINFO_HTTP_VERSION
4327 case 38: /* CURLINFO_HTTP_VERSION */
4328 resultType
= LongValue
;
4330 curlResult
= curl_easy_getinfo(connection
->handle
,
4331 CURLINFO_HTTP_VERSION
,
4334 longValue
= ocaml_HTTP_VERSION(longValue
);
4338 #pragma message("libcurl does not provide CURLINFO_HTTP_VERSION")
4340 /* sync check_enums */
4342 caml_failwith("Invalid CURLINFO Option");
4346 if (curlResult
!= CURLE_OK
)
4347 raiseError(connection
, curlResult
);
4352 result
= caml_alloc(1, StringValue
);
4353 Store_field(result
, 0, caml_copy_string(strValue
?strValue
:""));
4357 result
= caml_alloc(1, LongValue
);
4358 Store_field(result
, 0, Val_long(longValue
));
4362 result
= caml_alloc(1, DoubleValue
);
4363 Store_field(result
, 0, caml_copy_double(doubleValue
));
4366 case StringListValue
:
4367 result
= caml_alloc(1, StringListValue
);
4368 Store_field(result
, 0, convertStringList(stringListValue
));
4369 curl_slist_free_all(stringListValue
);
4372 case StringListListValue
:
4373 result
= caml_alloc(1, StringListListValue
);
4374 Store_field(result
, 0, current
);
4378 result
= caml_alloc(1, SocketValue
);
4379 Store_field(result
, 0, Val_socket(socketValue
));
4390 ** curl_escape helper function
4393 value
caml_curl_escape(value str
)
4399 curlResult
= curl_escape(String_val(str
), caml_string_length(str
));
4400 result
= caml_copy_string(curlResult
);
4407 ** curl_unescape helper function
4410 value
caml_curl_unescape(value str
)
4416 curlResult
= curl_unescape(String_val(str
), caml_string_length(str
));
4417 result
= caml_copy_string(curlResult
);
4424 ** curl_getdate helper function
4427 value
caml_curl_getdate(value str
, value now
)
4429 CAMLparam2(str
, now
);
4434 curlNow
= (time_t)Double_val(now
);
4435 curlResult
= curl_getdate(String_val(str
), &curlNow
);
4436 result
= caml_copy_double((double)curlResult
);
4442 ** curl_version helper function
4445 value
caml_curl_version(void)
4451 str
= curl_version();
4452 result
= caml_copy_string(str
);
4457 struct CURLVersionBitsMapping
4463 struct CURLVersionBitsMapping versionBitsMap
[] =
4465 {CURL_VERSION_IPV6
, "ipv6"},
4466 {CURL_VERSION_KERBEROS4
, "kerberos4"},
4467 {CURL_VERSION_SSL
, "ssl"},
4468 {CURL_VERSION_LIBZ
, "libz"},
4469 {CURL_VERSION_NTLM
, "ntlm"},
4470 {CURL_VERSION_GSSNEGOTIATE
, "gssnegotiate"},
4471 {CURL_VERSION_DEBUG
, "debug"},
4472 {CURL_VERSION_CURLDEBUG
, "curldebug"},
4473 {CURL_VERSION_ASYNCHDNS
, "asynchdns"},
4474 {CURL_VERSION_SPNEGO
, "spnego"},
4475 {CURL_VERSION_LARGEFILE
, "largefile"},
4476 {CURL_VERSION_IDN
, "idn"},
4477 {CURL_VERSION_SSPI
, "sspi"},
4478 {CURL_VERSION_CONV
, "conv"},
4479 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
4480 {CURL_VERSION_TLSAUTH_SRP
, "srp"},
4482 #if HAVE_DECL_CURL_VERSION_NTLM_WB
4483 {CURL_VERSION_NTLM_WB
, "wb"},
4487 value
caml_curl_version_info(value unit
)
4490 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4491 const char* const* p
= NULL
;
4494 curl_version_info_data
* data
= curl_version_info(CURLVERSION_NOW
);
4495 if (NULL
== data
) caml_failwith("curl_version_info");
4497 vlist
= Val_emptylist
;
4498 for (p
= data
->protocols
; NULL
!= *p
; p
++)
4500 vlist
= Val_cons(vlist
, caml_copy_string(*p
));
4503 vfeatures
= Val_emptylist
;
4504 for (i
= 0; i
< sizeof(versionBitsMap
)/sizeof(versionBitsMap
[0]); i
++)
4506 if (0 != (versionBitsMap
[i
].code
& data
->features
))
4507 vfeatures
= Val_cons(vfeatures
, caml_copy_string(versionBitsMap
[i
].name
));
4510 vnum
= caml_alloc_tuple(3);
4511 Store_field(vnum
,0,Val_int(0xFF & (data
->version_num
>> 16)));
4512 Store_field(vnum
,1,Val_int(0xFF & (data
->version_num
>> 8)));
4513 Store_field(vnum
,2,Val_int(0xFF & (data
->version_num
)));
4515 v
= caml_alloc_tuple(12);
4516 Store_field(v
,0,caml_copy_string(data
->version
));
4517 Store_field(v
,1,vnum
);
4518 Store_field(v
,2,caml_copy_string(data
->host
));
4519 Store_field(v
,3,vfeatures
);
4520 Store_field(v
,4,data
->ssl_version
? Val_some(caml_copy_string(data
->ssl_version
)) : Val_none
);
4521 Store_field(v
,5,data
->libz_version
? Val_some(caml_copy_string(data
->libz_version
)) : Val_none
);
4522 Store_field(v
,6,vlist
);
4523 Store_field(v
,7,caml_copy_string((data
->age
>= 1 && data
->ares
) ? data
->ares
: ""));
4524 Store_field(v
,8,Val_int((data
->age
>= 1) ? data
->ares_num
: 0));
4525 Store_field(v
,9,caml_copy_string((data
->age
>= 2 && data
->libidn
) ? data
->libidn
: ""));
4526 Store_field(v
,10,Val_int((data
->age
>= 3) ? data
->iconv_ver_num
: 0));
4527 Store_field(v
,11,caml_copy_string((data
->age
>= 3 && data
->libssh_version
) ? data
->libssh_version
: ""));
4532 value
caml_curl_pause(value conn
, value opts
)
4534 CAMLparam2(conn
, opts
);
4535 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4536 Connection
*connection
= Connection_val(conn
);
4540 while (Val_emptylist
!= opts
)
4542 switch (Int_val(Field(opts
,0)))
4544 case 0: bitmask
|= CURLPAUSE_SEND
; break;
4545 case 1: bitmask
|= CURLPAUSE_RECV
; break;
4546 case 2: bitmask
|= CURLPAUSE_ALL
; break;
4547 default: caml_failwith("wrong pauseOption");
4549 opts
= Field(opts
,1);
4552 caml_enter_blocking_section();
4553 result
= curl_easy_pause(connection
->handle
,bitmask
);
4554 caml_leave_blocking_section();
4556 if (result
!= CURLE_OK
)
4557 raiseError(connection
, result
);
4559 CAMLreturn(Val_unit
);
4563 * Curl multi stack support
4565 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
4566 * Other exported functions are prefixed with caml_curlm_, some of them
4567 * can/should be decomposed into smaller parts.
4570 struct ml_multi_handle
4573 value values
; /* callbacks */
4578 curlmopt_socket_function
,
4579 curlmopt_timer_function
,
4581 /* last, not used */
4585 typedef struct ml_multi_handle ml_multi_handle
;
4587 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
4588 #define CURLM_val(v) (Multi_val(v)->handle)
4590 static struct custom_operations curl_multi_ops
= {
4592 custom_finalize_default
,
4593 custom_compare_default
,
4594 custom_hash_default
,
4595 custom_serialize_default
,
4596 custom_deserialize_default
,
4597 #if defined(custom_compare_ext_default)
4598 custom_compare_ext_default
,
4602 static void raise_multi_error(char const* msg
)
4604 static const value
* exception
= NULL
;
4606 if (NULL
== exception
)
4608 exception
= caml_named_value("Curl.Multi.Error");
4609 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.Error");
4612 caml_raise_with_string(*exception
, msg
);
4615 static void raise_multi_cerror(char const* func
, CURLMcode code
)
4619 static const value
* exception
= NULL
;
4621 if (NULL
== exception
)
4623 exception
= caml_named_value("Curl.Multi.CError");
4624 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.CError");
4627 data
= caml_alloc(4, 0);
4629 Store_field(data
, 0, *exception
);
4630 Store_field(data
, 1, caml_copy_string(func
));
4631 Store_field(data
, 2, Val_int(code
));
4632 Store_field(data
, 3, caml_copy_string(curl_multi_strerror(code
)));
4639 static void check_mcode(char const* func
, CURLMcode code
)
4641 if (code
!= CURLM_OK
) raise_multi_cerror(func
,code
);
4644 value
caml_curl_multi_init(value unit
)
4648 ml_multi_handle
* multi
= (ml_multi_handle
*)caml_stat_alloc(sizeof(ml_multi_handle
));
4649 CURLM
* h
= curl_multi_init();
4653 caml_stat_free(multi
);
4654 raise_multi_error("caml_curl_multi_init");
4658 multi
->values
= caml_alloc(multi_values_total
, 0);
4659 caml_register_generational_global_root(&multi
->values
);
4661 v
= caml_alloc_custom(&curl_multi_ops
, sizeof(ml_multi_handle
*), 0, 1);
4662 Multi_val(v
) = multi
;
4667 value
caml_curl_multi_cleanup(value handle
)
4670 ml_multi_handle
* h
= Multi_val(handle
);
4673 CAMLreturn(Val_unit
);
4675 caml_remove_generational_global_root(&h
->values
);
4677 CURLcode rc
= curl_multi_cleanup(h
->handle
);
4680 Multi_val(handle
) = (ml_multi_handle
*)NULL
;
4682 check_mcode("curl_multi_cleanup",rc
);
4684 CAMLreturn(Val_unit
);
4687 static CURL
* curlm_remove_finished(CURLM
* multi_handle
, CURLcode
* result
)
4689 int msgs_in_queue
= 0;
4693 CURLMsg
* msg
= curl_multi_info_read(multi_handle
, &msgs_in_queue
);
4694 if (NULL
== msg
) return NULL
;
4695 if (CURLMSG_DONE
== msg
->msg
)
4697 CURL
* easy_handle
= msg
->easy_handle
;
4698 if (result
) *result
= msg
->data
.result
;
4699 if (CURLM_OK
!= curl_multi_remove_handle(multi_handle
, easy_handle
))
4701 /*caml_failwith("curlm_remove_finished");*/
4708 value
caml_curlm_remove_finished(value v_multi
)
4710 CAMLparam1(v_multi
);
4711 CAMLlocal2(v_easy
, v_tuple
);
4713 CURLM
* multi_handle
;
4715 Connection
* conn
= NULL
;
4717 multi_handle
= CURLM_val(v_multi
);
4719 caml_enter_blocking_section();
4720 handle
= curlm_remove_finished(multi_handle
,&result
);
4721 caml_leave_blocking_section();
4725 CAMLreturn(Val_none
);
4729 conn
= getConnection(handle
);
4730 if (conn
->curl_ERRORBUFFER
!= NULL
)
4732 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
4735 /* NB: same handle, but different block */
4736 v_easy
= caml_curl_alloc(conn
);
4737 v_tuple
= caml_alloc(2, 0);
4738 Store_field(v_tuple
,0,v_easy
);
4739 Store_field(v_tuple
,1,Val_int(result
)); /* CURLcode */
4740 CAMLreturn(Val_some(v_tuple
));
4744 static int curlWait_table
[] = {
4750 static int list_length(value v
)
4753 while (v
!= Val_emptylist
) {
4760 static struct curl_waitfd
*convert_extra_fds(value v_extra_fds
, int *nr_extra_fds
)
4762 /* NB this function doesn't trigger GC so can avoid registering local roots */
4765 int nfds
= list_length(v_extra_fds
);
4766 *nr_extra_fds
= nfds
;
4768 if (nfds
== 0) return NULL
;
4770 struct curl_waitfd
*extra_fds
= caml_stat_alloc(sizeof(extra_fds
[0]) * nfds
);
4773 while (v_extra_fds
!= Val_emptylist
)
4775 v_extra_fd
= Field(v_extra_fds
, 0);
4776 extra_fds
[i
].fd
= Socket_val(Field(v_extra_fd
, 0));
4777 extra_fds
[i
].events
= caml_convert_flag_list(Field(v_extra_fd
, 1), curlWait_table
);
4778 extra_fds
[i
].revents
= 0;
4780 v_extra_fds
= Field(v_extra_fds
, 1);
4786 /* Assumes v_extra_fds and extra_fds to have the same number of elements */
4787 static void update_extra_fds(value v_extra_fds
, struct curl_waitfd
*extra_fds
)
4789 CAMLparam1(v_extra_fds
);
4790 CAMLlocal2(v_extra_fd
, lst
);
4793 while (v_extra_fds
!= Val_emptylist
)
4795 v_extra_fd
= Field(v_extra_fds
, 0);
4796 lst
= Val_emptylist
;
4797 for (int j
= 0; j
< sizeof(curlWait_table
)/sizeof(curlWait_table
[0]); j
++)
4799 if (curlWait_table
[j
] & extra_fds
[i
].revents
)
4800 lst
= Val_cons(lst
, Val_int(j
));
4802 Store_field(v_extra_fd
, 2, lst
);
4804 v_extra_fds
= Field(v_extra_fds
, 1);
4810 value
caml_curl_multi_wait(value v_timeout_ms
, value v_extra_fds
, value v_multi
)
4812 CAMLparam3(v_timeout_ms
,v_extra_fds
,v_multi
);
4813 CURLM
*multi_handle
= CURLM_val(v_multi
);
4814 int timeout_ms
= Int_val(v_timeout_ms
);
4816 int nr_extra_fds
= 0;
4817 struct curl_waitfd
*extra_fds
= convert_extra_fds(v_extra_fds
, &nr_extra_fds
);
4820 caml_enter_blocking_section();
4821 rc
= curl_multi_wait(multi_handle
, extra_fds
, nr_extra_fds
, timeout_ms
, &numfds
);
4822 caml_leave_blocking_section();
4824 if (extra_fds
!= NULL
) {
4825 update_extra_fds(v_extra_fds
, extra_fds
);
4826 caml_stat_free(extra_fds
);
4829 check_mcode("curl_multi_wait",rc
);
4831 CAMLreturn(Val_bool(numfds
!= 0));
4834 value
caml_curl_multi_poll(value v_timeout_ms
, value v_extra_fds
, value v_multi
)
4836 CAMLparam3(v_timeout_ms
,v_extra_fds
,v_multi
);
4837 CURLM
*multi_handle
= CURLM_val(v_multi
);
4838 int timeout_ms
= Int_val(v_timeout_ms
);
4840 int nr_extra_fds
= 0;
4841 struct curl_waitfd
*extra_fds
= convert_extra_fds(v_extra_fds
, &nr_extra_fds
);
4844 caml_enter_blocking_section();
4845 #if HAVE_DECL_CURL_MULTI_POLL
4846 rc
= curl_multi_poll(multi_handle
, extra_fds
, nr_extra_fds
, timeout_ms
, &numfds
);
4848 rc
= curl_multi_wait(multi_handle
, extra_fds
, nr_extra_fds
, timeout_ms
, &numfds
);
4850 caml_leave_blocking_section();
4852 if (extra_fds
!= NULL
) {
4853 update_extra_fds(v_extra_fds
, extra_fds
);
4854 caml_stat_free(extra_fds
);
4857 check_mcode("curl_multi_poll",rc
);
4859 CAMLreturn(Val_bool(numfds
!= 0));
4862 value
caml_curl_multi_add_handle(value v_multi
, value v_easy
)
4864 CAMLparam2(v_multi
,v_easy
);
4865 CURLMcode rc
= CURLM_OK
;
4866 CURLM
* multi
= CURLM_val(v_multi
);
4867 Connection
* conn
= Connection_val(v_easy
);
4869 /* prevent collection of OCaml value while the easy handle is used
4870 and may invoke callbacks registered on OCaml side */
4873 /* may invoke callbacks so need to be consistent with locks */
4874 caml_enter_blocking_section();
4875 rc
= curl_multi_add_handle(multi
, conn
->handle
);
4878 conn
->refcount
--; /* not added, revert */
4879 caml_leave_blocking_section();
4880 check_mcode("curl_multi_add_handle",rc
);
4882 caml_leave_blocking_section();
4884 CAMLreturn(Val_unit
);
4887 value
caml_curl_multi_remove_handle(value v_multi
, value v_easy
)
4889 CAMLparam2(v_multi
,v_easy
);
4890 CURLM
* multi
= CURLM_val(v_multi
);
4891 CURLMcode rc
= CURLM_OK
;
4892 Connection
* conn
= Connection_val(v_easy
);
4894 /* may invoke callbacks so need to be consistent with locks */
4895 caml_enter_blocking_section();
4896 rc
= curl_multi_remove_handle(multi
, conn
->handle
);
4898 caml_leave_blocking_section();
4899 check_mcode("curl_multi_remove_handle",rc
);
4901 CAMLreturn(Val_unit
);
4904 value
caml_curl_multi_perform_all(value v_multi
)
4906 CAMLparam1(v_multi
);
4907 int still_running
= 0;
4908 CURLM
* h
= CURLM_val(v_multi
);
4910 caml_enter_blocking_section();
4911 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(h
, &still_running
));
4912 caml_leave_blocking_section();
4914 CAMLreturn(Val_int(still_running
));
4917 /* currently curlCode repr tag matches CURLE_ value
4918 * this is exploited below, but generally should use errorMap */
4920 value
caml_curl_strerror(value v_code
)
4923 CAMLreturn(caml_copy_string(curl_easy_strerror((CURLcode
)Int_val(v_code
))));
4926 value
caml_curl_int_of_curlCode(value v_code
)
4931 value
caml_curl_curlCode_of_int(value v
)
4933 return (Int_val(v
) < sizeof(errorMap
) / sizeof(errorMap
[0]) ? Val_some(v
) : Val_none
);
4936 value
caml_curl_multi_socket_action(value v_multi
, value v_fd
, value v_kind
)
4938 CAMLparam3(v_multi
, v_fd
, v_kind
);
4939 CURLM
* h
= CURLM_val(v_multi
);
4940 int still_running
= 0;
4941 CURLMcode rc
= CURLM_OK
;
4942 curl_socket_t socket
;
4945 if (Val_none
== v_fd
)
4947 socket
= CURL_SOCKET_TIMEOUT
;
4951 socket
= Socket_val(Field(v_fd
, 0));
4954 switch (Int_val(v_kind
))
4957 case 1 : kind
|= CURL_CSELECT_IN
; break;
4958 case 2 : kind
|= CURL_CSELECT_OUT
; break;
4959 case 3 : kind
|= CURL_CSELECT_IN
| CURL_CSELECT_OUT
; break;
4961 raise_multi_error("caml_curl_multi_socket_action");
4964 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4966 caml_enter_blocking_section();
4968 rc
= curl_multi_socket_action(h
, socket
, kind
, &still_running
);
4969 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4970 caml_leave_blocking_section();
4972 check_mcode("curl_multi_socket_action",rc
);
4974 CAMLreturn(Val_int(still_running
));
4977 value
caml_curl_multi_socket_all(value v_multi
)
4979 CAMLparam1(v_multi
);
4980 int still_running
= 0;
4981 CURLMcode rc
= CURLM_OK
;
4982 CURLM
* h
= CURLM_val(v_multi
);
4984 caml_enter_blocking_section();
4986 rc
= curl_multi_socket_all(h
, &still_running
);
4987 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4988 caml_leave_blocking_section();
4990 check_mcode("curl_multi_socket_all",rc
);
4992 CAMLreturn(Val_int(still_running
));
4995 static int curlm_sock_cb(CURL
*e
, curl_socket_t sock
, int what
, void *cbp
, void *sockp
)
4997 caml_leave_blocking_section();
5000 CAMLlocal2(v_what
,csock
);
5002 (void)sockp
; /* not used */
5004 /* v_what = Val_int(what); */
5007 case CURL_POLL_NONE
: v_what
= Val_int(0); break;
5008 case CURL_POLL_IN
: v_what
= Val_int(1); break;
5009 case CURL_POLL_OUT
: v_what
= Val_int(2); break;
5010 case CURL_POLL_INOUT
: v_what
= Val_int(3); break;
5011 case CURL_POLL_REMOVE
: v_what
= Val_int(4); break;
5013 fprintf(stderr
, "curlm_sock_cb sock=%lld what=%d\n", (long long)sock
, what
);
5015 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
5017 csock
=Val_socket(sock
);
5018 caml_callback2(Field(((ml_multi_handle
*)cbp
)->values
,curlmopt_socket_function
),
5022 caml_enter_blocking_section();
5026 value
caml_curl_multi_socketfunction(value v_multi
, value v_cb
)
5028 CAMLparam2(v_multi
, v_cb
);
5029 ml_multi_handle
* multi
= Multi_val(v_multi
);
5031 Store_field(multi
->values
, curlmopt_socket_function
, v_cb
);
5033 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETFUNCTION
, curlm_sock_cb
);
5034 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETDATA
, multi
);
5036 CAMLreturn(Val_unit
);
5039 static int curlm_timer_cb(CURLM
*multi
, long timeout_ms
, void *userp
)
5041 caml_leave_blocking_section();
5045 caml_callback(Field(((ml_multi_handle
*)userp
)->values
,curlmopt_timer_function
), Val_long(timeout_ms
));
5048 caml_enter_blocking_section();
5052 value
caml_curl_multi_timerfunction(value v_multi
, value v_cb
)
5054 CAMLparam2(v_multi
, v_cb
);
5055 ml_multi_handle
* multi
= Multi_val(v_multi
);
5057 Store_field(multi
->values
, curlmopt_timer_function
, v_cb
);
5059 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERFUNCTION
, curlm_timer_cb
);
5060 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERDATA
, multi
);
5062 CAMLreturn(Val_unit
);
5065 value
caml_curl_multi_timeout(value v_multi
)
5067 CAMLparam1(v_multi
);
5069 CURLMcode rc
= CURLM_OK
;
5070 ml_multi_handle
* multi
= Multi_val(v_multi
);
5072 rc
= curl_multi_timeout(multi
->handle
, &ms
);
5074 check_mcode("curl_multi_timeout",rc
);
5076 CAMLreturn(Val_long(ms
));
5079 #define SETMOPT_VAL_(func_name, curl_option, conv_val) \
5080 static void func_name(CURLM *handle, value option) \
5082 CAMLparam1(option); \
5083 CURLMcode result = CURLM_OK; \
5085 result = curl_multi_setopt(handle, curl_option, conv_val(option)); \
5087 check_mcode("curl_multi_setopt "#curl_option,result); \
5092 #define SETMOPT_VAL(name, conv) SETMOPT_VAL_(handle_multi_##name, CURLMOPT_##name, conv)
5093 #define SETMOPT_BOOL(name) SETMOPT_VAL(name, Bool_val)
5094 #define SETMOPT_LONG(name) SETMOPT_VAL(name, Long_val)
5095 #define SETMOPT_INT64(name) SETMOPT_VAL(name, Int64_val)
5097 long pipeliningMap
[] =
5099 0, /* CURLPIPE_NOTHING */
5100 1, /* CURLPIPE_HTTP1 */
5101 2, /* CURLPIPE_MULTIPLEX */
5104 static void handle_multi_PIPELINING(CURLM
* handle
, value option
)
5107 CURLMcode result
= CURLM_OK
;
5109 long bits
= convert_bit_list(pipeliningMap
, sizeof(pipeliningMap
) / sizeof(pipeliningMap
[0]), option
);
5111 result
= curl_multi_setopt(handle
, CURLMOPT_PIPELINING
, bits
);
5113 check_mcode("curl_multi_setopt CURLOPT_PIPELINING",result
);
5118 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
5119 SETMOPT_LONG( MAXCONNECTS
)
5122 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
5123 SETMOPT_LONG( MAX_PIPELINE_LENGTH
)
5126 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
5127 SETMOPT_LONG( MAX_HOST_CONNECTIONS
)
5130 #if HAVE_DECL_CURLMOPT_MAX_TOTAL_CONNECTIONS
5131 SETMOPT_LONG( MAX_TOTAL_CONNECTIONS
)
5134 typedef struct CURLMOptionMapping CURLMOptionMapping
;
5135 #define OPT(name) { handle_multi_## name, "CURLMOPT_"#name}
5136 #define NO_OPT(name) { NULL, "CURLMOPT_"#name}
5138 struct CURLMOptionMapping
5140 void (*optionHandler
)(CURLM
*, value
);
5144 CURLMOptionMapping implementedMOptionMap
[] = {
5146 #if HAVE_DECL_CURLMOPT_MAXCONNECTS
5149 NO_OPT( MAXCONNECTS
),
5151 #if HAVE_DECL_CURLMOPT_MAX_PIPELINE_LENGTH
5152 OPT( MAX_PIPELINE_LENGTH
),
5154 NO_OPT( MAX_PIPELINE_LENGTH
),
5156 #if HAVE_DECL_CURLMOPT_MAX_HOST_CONNECTIONS
5157 OPT( MAX_HOST_CONNECTIONS
),
5159 NO_OPT( MAX_HOST_CONNECTIONS
),
5161 #if HAVE_DECL_CURLMOPT_MAX_TOTAL_CONNECTIONS
5162 OPT( MAX_TOTAL_CONNECTIONS
),
5164 NO_OPT( MAX_TOTAL_CONNECTIONS
),
5168 value
caml_curl_multi_setopt(value v_multi
, value option
)
5170 CAMLparam2(v_multi
, option
);
5172 CURLM
*handle
= Multi_val(v_multi
)->handle
;
5173 CURLMOptionMapping
* thisOption
= NULL
;
5174 static const value
* exception
= NULL
;
5176 data
= Field(option
, 0);
5178 if (Tag_val(option
) < sizeof(implementedMOptionMap
)/sizeof(CURLMOptionMapping
))
5180 thisOption
= &implementedMOptionMap
[Tag_val(option
)];
5181 if (thisOption
->optionHandler
)
5183 thisOption
->optionHandler(handle
, data
);
5187 if (NULL
== exception
)
5189 exception
= caml_named_value("Curl.NotImplemented");
5190 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
5193 caml_raise_with_string(*exception
, thisOption
->name
);
5198 raise_multi_error("Invalid CURLMOPT Option");
5201 CAMLreturn(Val_unit
);
5211 #define CURL_ENUM(name,last_used) { CURL_ ## name ## _ ## last_used, CURL_ ## name ## _LAST, #name }
5213 struct used_enum check_enums
[] = {
5214 { CURLINFO_SSL_DATA_OUT
, CURLINFO_END
, "DEBUGFUNCTION curl_infotype" },
5215 #if HAVE_DECL_CURL_HTTP_VERSION_3
5216 CURL_ENUM(HTTP_VERSION
, 3),
5218 { 38, CURLINFO_LASTONE
, "CURLINFO" },
5219 #if HAVE_DECL_CURLE_AGAIN
5220 { CURLE_AGAIN
, CURL_LAST
, "CURLcode" }
5224 value
caml_curl_check_enums(value v_unit
)
5229 size_t len
= sizeof(check_enums
) / sizeof(struct used_enum
);
5231 v_r
= caml_alloc_tuple(len
);
5233 for (i
= 0; i
< len
; i
++)
5235 v
= caml_alloc_tuple(3);
5236 Store_field(v
, 0, Val_int(check_enums
[i
].last_used
));
5237 Store_field(v
, 1, Val_int(check_enums
[i
].last
));
5238 Store_field(v
, 2, caml_copy_string(check_enums
[i
].name
));
5239 Store_field(v_r
, i
, v
);