4 *** Copyright (c) 2003-2008, Lars Nilsson, <lars@quantumchamaeleon.com>
5 *** Copyright (c) 2009, ygrek, <ygrek@autistici.org>
13 /* suppress false gcc warning on seekFunction */
14 #define CURL_DISABLE_TYPECHECK
15 #include <curl/curl.h>
17 #include <caml/alloc.h>
18 #include <caml/memory.h>
19 #include <caml/mlvalues.h>
20 #include <caml/callback.h>
21 #include <caml/fail.h>
22 #include <caml/unixsupport.h>
23 #include <caml/custom.h>
28 #pragma message("No config file given.")
31 void leave_blocking_section(void);
32 void enter_blocking_section(void);
34 #define Val_none Val_int(0)
41 some
= caml_alloc(1, 0);
42 Store_field( some
, 0, v
);
46 static value
Val_pair(value v1
, value v2
)
50 pair
= caml_alloc_small(2,0);
56 static value
Val_cons(value list
, value v
) { return Val_pair(v
,list
); }
58 typedef struct Connection Connection
;
59 typedef struct ConnectionList ConnectionList
;
61 #define Connection_val(v) (*(Connection**)Data_custom_val(v))
63 typedef enum OcamlValues
74 Ocaml_PROGRESSFUNCTION
,
79 Ocaml_OPENSOCKETFUNCTION
,
90 Ocaml_HTTPPOSTSTRINGS
,
106 Ocaml_SSL_CIPHER_LIST
,
111 Ocaml_FTP_ALTERNATIVE_TO_USER
,
112 Ocaml_SSH_PUBLIC_KEYFILE
,
113 Ocaml_SSH_PRIVATE_KEYFILE
,
114 Ocaml_SSH_HOST_PUBLIC_KEY_MD5
,
115 Ocaml_COPYPOSTFIELDS
,
124 /* Not used, last for size */
136 size_t refcount
; /* number of references to this structure */
141 char *curl_PROXYUSERPWD
;
143 char *curl_ERRORBUFFER
;
144 char *curl_POSTFIELDS
;
145 int curl_POSTFIELDSIZE
;
147 char *curl_USERAGENT
;
150 struct curl_slist
*curl_HTTPHEADER
;
151 struct curl_slist
*httpPostBuffers
;
152 struct curl_httppost
*httpPostFirst
;
153 struct curl_httppost
*httpPostLast
;
154 struct curl_slist
*curl_RESOLVE
;
156 char *curl_SSLCERTTYPE
;
157 char *curl_SSLCERTPASSWD
;
159 char *curl_SSLKEYTYPE
;
160 char *curl_SSLKEYPASSWD
;
161 char *curl_SSLENGINE
;
162 struct curl_slist
*curl_QUOTE
;
163 struct curl_slist
*curl_POSTQUOTE
;
164 char *curl_COOKIEFILE
;
165 char *curl_CUSTOMREQUEST
;
166 char *curl_INTERFACE
;
169 char *curl_RANDOM_FILE
;
170 char *curl_EGDSOCKET
;
171 char *curl_COOKIEJAR
;
172 char *curl_SSL_CIPHER_LIST
;
174 struct curl_slist
*curl_HTTP200ALIASES
;
175 char *curl_NETRC_FILE
;
176 char *curl_FTP_ACCOUNT
;
177 char *curl_COOKIELIST
;
178 char *curl_FTP_ALTERNATIVE_TO_USER
;
179 char *curl_SSH_PUBLIC_KEYFILE
;
180 char *curl_SSH_PRIVATE_KEYFILE
;
181 char *curl_SSH_HOST_PUBLIC_KEY_MD5
;
182 char *curl_COPYPOSTFIELDS
;
183 char *curl_DNS_SERVERS
;
184 char *curl_MAIL_FROM
;
185 struct curl_slist
*curl_MAIL_RCPT
;
188 struct ConnectionList
194 static ConnectionList connectionList
= {NULL
, NULL
};
196 typedef struct CURLErrorMapping CURLErrorMapping
;
198 struct CURLErrorMapping
204 CURLErrorMapping errorMap
[] =
206 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
207 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL
},
209 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
211 #if HAVE_DECL_CURLE_FAILED_INIT
212 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT
},
214 {"CURLE_FAILED_INIT", -1},
216 #if HAVE_DECL_CURLE_URL_MALFORMAT
217 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT
},
219 {"CURLE_URL_MALFORMAT", -1},
221 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
222 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER
},
224 {"CURLE_URL_MALFORMAT_USER", -1},
226 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
227 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY
},
229 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
231 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
232 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST
},
234 {"CURLE_COULDNT_RESOLVE_HOST", -1},
236 #if HAVE_DECL_CURLE_COULDNT_CONNECT
237 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT
},
239 {"CURLE_COULDNT_CONNECT", -1},
241 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
242 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY
},
244 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
246 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
247 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED
},
249 {"CURLE_FTP_ACCESS_DENIED", -1},
251 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
252 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT
},
254 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
256 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
257 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY
},
259 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
261 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
262 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY
},
264 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
266 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
267 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY
},
269 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
271 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
272 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT
},
274 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
276 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
277 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST
},
279 {"CURLE_FTP_CANT_GET_HOST", -1},
281 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
282 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT
},
284 {"CURLE_FTP_CANT_RECONNECT", -1},
286 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
287 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY
},
289 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
291 #if HAVE_DECL_CURLE_PARTIAL_FILE
292 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE
},
294 {"CURLE_PARTIAL_FILE", -1},
296 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
297 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE
},
299 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
301 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
302 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR
},
304 {"CURLE_FTP_WRITE_ERROR", -1},
306 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
307 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR
},
309 {"CURLE_FTP_QUOTE_ERROR", -1},
311 #if HAVE_DECL_CURLE_HTTP_NOT_FOUND
312 {"CURLE_HTTP_NOT_FOUND", CURLE_HTTP_NOT_FOUND
},
314 {"CURLE_HTTP_NOT_FOUND", -1},
316 #if HAVE_DECL_CURLE_WRITE_ERROR
317 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR
},
319 {"CURLE_WRITE_ERROR", -1},
321 #if HAVE_DECL_CURLE_MALFORMAT_USER
322 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER
},
324 {"CURLE_MALFORMAT_USER", -1},
326 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
327 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE
},
329 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
331 #if HAVE_DECL_CURLE_READ_ERROR
332 {"CURLE_READ_ERROR", CURLE_READ_ERROR
},
334 {"CURLE_READ_ERROR", -1},
336 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
337 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY
},
339 {"CURLE_OUT_OF_MEMORY", -1},
341 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
342 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED
},
344 {"CURLE_OPERATION_TIMEOUTED", -1},
346 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
347 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII
},
349 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
351 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
352 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED
},
354 {"CURLE_FTP_PORT_FAILED", -1},
356 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
357 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST
},
359 {"CURLE_FTP_COULDNT_USE_REST", -1},
361 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
362 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE
},
364 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
366 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
367 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR
},
369 {"CURLE_HTTP_RANGE_ERROR", -1},
371 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
372 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR
},
374 {"CURLE_HTTP_POST_ERROR", -1},
376 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
377 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR
},
379 {"CURLE_SSL_CONNECT_ERROR", -1},
381 #if HAVE_DECL_CURLE_FTP_BAD_DOWNLOAD_RESUME
382 {"CURLE_FTP_BAD_DOWNLOAD_RESUME", CURLE_FTP_BAD_DOWNLOAD_RESUME
},
384 {"CURLE_FTP_BAD_DOWNLOAD_RESUME", -1},
386 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
387 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE
},
389 {"CURLE_FILE_COULDNT_READ_FILE", -1},
391 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
392 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND
},
394 {"CURLE_LDAP_CANNOT_BIND", -1},
396 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
397 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED
},
399 {"CURLE_LDAP_SEARCH_FAILED", -1},
401 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
402 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND
},
404 {"CURLE_LIBRARY_NOT_FOUND", -1},
406 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
407 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND
},
409 {"CURLE_FUNCTION_NOT_FOUND", -1},
411 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
412 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK
},
414 {"CURLE_ABORTED_BY_CALLBACK", -1},
416 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
417 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT
},
419 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
421 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
422 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER
},
424 {"CURLE_BAD_CALLING_ORDER", -1},
426 #if HAVE_DECL_CURLE_HTTP_PORT_FAILED
427 {"CURLE_HTTP_PORT_FAILED", CURLE_HTTP_PORT_FAILED
},
429 {"CURLE_HTTP_PORT_FAILED", -1},
431 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
432 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED
},
434 {"CURLE_BAD_PASSWORD_ENTERED", -1},
436 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
437 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS
},
439 {"CURLE_TOO_MANY_REDIRECTS", -1},
441 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
442 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION
},
444 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
446 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
447 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX
},
449 {"CURLE_TELNET_OPTION_SYNTAX", -1},
451 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
452 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE
},
454 {"CURLE_SSL_PEER_CERTIFICATE", -1},
456 #if HAVE_DECL_CURLE_GOT_NOTHING
457 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING
},
459 {"CURLE_GOT_NOTHING", -1},
461 #if HAVE_DECL_CURLE_SSL_ENGINE_NOT_FOUND
462 {"CURLE_SSL_ENGINE_NOT_FOUND", CURLE_SSL_ENGINE_NOTFOUND
},
464 {"CURLE_SSL_ENGINE_NOT_FOUND", -1},
466 #if HAVE_DECL_CURLE_SSL_ENGINE_SET_FAILED
467 {"CURLE_SSL_ENGINE_SET_FAILED", CURLE_SSL_ENGINE_SETFAILED
},
469 {"CURLE_SSL_ENGINE_SET_FAILED", -1},
471 #if HAVE_DECL_CURLE_SEND_ERROR
472 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR
},
474 {"CURLE_SEND_ERROR", -1},
476 #if HAVE_DECL_CURLE_RECV_ERROR
477 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR
},
479 {"CURLE_RECV_ERROR", -1},
481 #if HAVE_DECL_CURLE_SHARE_IN_USE
482 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE
},
484 {"CURLE_SHARE_IN_USE", -1},
486 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
487 {"CURLE_SSL_CERTPROBLEN", CURLE_SSL_CERTPROBLEM
},
489 {"CURLE_SSL_CERTPROBLEN", -1},
491 #if HAVE_DECL_CURLE_SSL_CIPHER
492 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER
},
494 {"CURLE_SSL_CIPHER", -1},
496 #if HAVE_DECL_CURLE_SSL_CACERT
497 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT
},
499 {"CURLE_SSL_CACERT", -1},
501 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
502 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING
},
504 {"CURLE_BAD_CONTENT_ENCODING", -1},
506 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
507 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL
},
509 {"CURLE_LDAP_INVALID_URL", -1},
511 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
512 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED
},
514 {"CURLE_FILESIZE_EXCEEDED", -1},
516 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
517 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED
},
519 {"CURLE_FTP_SSL_FAILED", -1},
521 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
522 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND
},
524 {"CURLE_SEND_FAIL_REWIND", -1},
526 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
527 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED
},
529 {"CURLE_SSL_ENGINE_INITFAILED", -1},
531 #if HAVE_DECL_CURLE_LOGIN_DENIED
532 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED
},
534 {"CURLE_LOGIN_DENIED", -1},
536 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
537 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND
},
539 {"CURLE_TFTP_NOTFOUND", -1},
541 #if HAVE_DECL_CURLE_TFTP_PERM
542 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM
},
544 {"CURLE_TFTP_PERM", -1},
546 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
547 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL
},
549 {"CURLE_REMOTE_DISK_FULL", -1},
551 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
552 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL
},
554 {"CURLE_TFTP_ILLEGAL", -1},
556 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
557 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID
},
559 {"CURLE_TFTP_UNKNOWNID", -1},
561 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
562 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS
},
564 {"CURLE_REMOTE_FILE_EXISTS", -1},
566 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
567 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER
},
569 {"CURLE_TFTP_NOSUCHUSER", -1},
571 #if HAVE_DECL_CURLE_CONV_FAILED
572 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED
},
574 {"CURLE_CONV_FAILED", -1},
576 #if HAVE_DECL_CURLE_CONV_REQUIRED
577 {"CURLE_CONV_REQUIRED", CURLE_CONV_REQUIRED
},
579 {"CURLE_CONV_REQUIRED", -1},
581 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
582 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE
},
584 {"CURLE_SSL_CACERT_BADFILE", -1},
586 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
587 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND
},
589 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
591 #if HAVE_DECL_CURLE_SSH
592 {"CURLE_SSH", CURLE_SSH
},
596 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
597 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED
},
599 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
601 #if HAVE_DECL_CURLE_AGAIN
602 {"CURLE_AGAIN", CURLE_AGAIN
},
606 {"CURLE_OK", CURLE_OK
},
610 typedef struct CURLOptionMapping CURLOptionMapping
;
612 struct CURLOptionMapping
614 void (*optionHandler
)(Connection
*, value
);
616 /* CURLoption option; */
619 static char* strdup_ml(value v
)
622 p
= malloc(caml_string_length(v
)+1);
623 memcpy(p
,String_val(v
),caml_string_length(v
)+1); // caml strings have terminating zero
627 static void free_curl_slist(struct curl_slist
*slist
)
632 curl_slist_free_all(slist
);
635 static void raiseError(Connection
*conn
, CURLcode code
)
638 CAMLlocal1(exceptionData
);
640 char *errorString
= "Unknown Error";
643 for (i
= 0; errorMap
[i
].name
!= NULL
; i
++)
645 if (errorMap
[i
].error
== code
)
647 errorString
= errorMap
[i
].name
;
652 exceptionData
= caml_alloc(3, 0);
654 Store_field(exceptionData
, 0, Val_int(code
));
655 Store_field(exceptionData
, 1, Val_int(code
));
656 Store_field(exceptionData
, 2, copy_string(errorString
));
658 if (conn
!= NULL
&& conn
->curl_ERRORBUFFER
!= NULL
)
660 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0,
661 copy_string(conn
->curl_ERRORBUFFER
));
664 exception
= caml_named_value("CurlException");
666 if (exception
== NULL
)
667 caml_failwith("CurlException not registered");
669 raise_with_arg(*exception
, exceptionData
);
674 static void resetOcamlValues(Connection
* connection
)
678 for (i
= 0; i
< OcamlValuesSize
; i
++)
679 Store_field(connection
->ocamlValues
, i
, Val_unit
);
682 static Connection
* allocConnection(CURL
* h
)
684 Connection
* connection
= (Connection
*)malloc(sizeof(Connection
));
686 connection
->ocamlValues
= caml_alloc(OcamlValuesSize
, 0);
687 resetOcamlValues(connection
);
688 register_global_root(&connection
->ocamlValues
);
690 connection
->connection
= h
;
692 connection
->next
= NULL
;
693 connection
->prev
= NULL
;
695 if (connectionList
.tail
== NULL
)
697 connectionList
.tail
= connection
;
698 connectionList
.head
= connection
;
702 connection
->prev
= connectionList
.head
;
703 connectionList
.head
->next
= connection
;
704 connectionList
.head
= connection
;
707 connection
->refcount
= 0;
709 connection
->curl_URL
= NULL
;
710 connection
->curl_PROXY
= NULL
;
711 connection
->curl_USERPWD
= NULL
;
712 connection
->curl_PROXYUSERPWD
= NULL
;
713 connection
->curl_RANGE
= NULL
;
714 connection
->curl_ERRORBUFFER
= NULL
;
715 connection
->curl_POSTFIELDS
= NULL
;
716 connection
->curl_POSTFIELDSIZE
= -1;
717 connection
->curl_REFERER
= NULL
;
718 connection
->curl_USERAGENT
= NULL
;
719 connection
->curl_FTPPORT
= NULL
;
720 connection
->curl_COOKIE
= NULL
;
721 connection
->curl_HTTPHEADER
= NULL
;
722 connection
->httpPostBuffers
= NULL
;
723 connection
->httpPostFirst
= NULL
;
724 connection
->httpPostLast
= NULL
;
725 connection
->curl_SSLCERT
= NULL
;
726 connection
->curl_SSLCERTTYPE
= NULL
;
727 connection
->curl_SSLCERTPASSWD
= NULL
;
728 connection
->curl_SSLKEY
= NULL
;
729 connection
->curl_SSLKEYTYPE
= NULL
;
730 connection
->curl_SSLKEYPASSWD
= NULL
;
731 connection
->curl_SSLENGINE
= NULL
;
732 connection
->curl_QUOTE
= NULL
;
733 connection
->curl_POSTQUOTE
= NULL
;
734 connection
->curl_COOKIEFILE
= NULL
;
735 connection
->curl_CUSTOMREQUEST
= NULL
;
736 connection
->curl_INTERFACE
= NULL
;
737 connection
->curl_CAINFO
= NULL
;
738 connection
->curl_CAPATH
= NULL
;
739 connection
->curl_RANDOM_FILE
= NULL
;
740 connection
->curl_EGDSOCKET
= NULL
;
741 connection
->curl_COOKIEJAR
= NULL
;
742 connection
->curl_SSL_CIPHER_LIST
= NULL
;
743 connection
->curl_PRIVATE
= NULL
;
744 connection
->curl_HTTP200ALIASES
= NULL
;
745 connection
->curl_NETRC_FILE
= NULL
;
746 connection
->curl_FTP_ACCOUNT
= NULL
;
747 connection
->curl_COOKIELIST
= NULL
;
748 connection
->curl_FTP_ALTERNATIVE_TO_USER
= NULL
;
749 connection
->curl_SSH_PUBLIC_KEYFILE
= NULL
;
750 connection
->curl_SSH_PRIVATE_KEYFILE
= NULL
;
751 connection
->curl_COPYPOSTFIELDS
= NULL
;
752 connection
->curl_RESOLVE
= NULL
;
753 connection
->curl_DNS_SERVERS
= NULL
;
754 connection
->curl_MAIL_FROM
= NULL
;
755 connection
->curl_MAIL_RCPT
= NULL
;
760 static Connection
*newConnection(void)
764 caml_enter_blocking_section();
765 h
= curl_easy_init();
766 caml_leave_blocking_section();
768 return allocConnection(h
);
771 static void free_if(void* p
) { if (NULL
!= p
) free(p
); }
773 static void removeConnection(Connection
*connection
, int finalization
)
775 const char* fin_url
= NULL
;
777 if (!connection
->connection
)
779 return; /* already cleaned up */
784 /* cannot engage OCaml runtime at finalization, just report leak */
785 if (CURLE_OK
!= curl_easy_getinfo(connection
->connection
, CURLINFO_EFFECTIVE_URL
, &fin_url
) || NULL
== fin_url
)
789 fprintf(stderr
,"Curl: handle %p leaked, conn %p, url %s\n", connection
->connection
, connection
, fin_url
);
794 enter_blocking_section();
795 curl_easy_cleanup(connection
->connection
);
796 leave_blocking_section();
799 connection
->connection
= NULL
;
801 if (connectionList
.tail
== connection
)
802 connectionList
.tail
= connectionList
.tail
->next
;
803 if (connectionList
.head
== connection
)
804 connectionList
.head
= connectionList
.head
->prev
;
806 if (connection
->next
!= NULL
)
807 connection
->next
->prev
= connection
->prev
;
808 if (connection
->prev
!= NULL
)
809 connection
->prev
->next
= connection
->next
;
811 remove_global_root(&connection
->ocamlValues
);
813 free_if(connection
->curl_URL
);
814 free_if(connection
->curl_PROXY
);
815 free_if(connection
->curl_USERPWD
);
816 free_if(connection
->curl_PROXYUSERPWD
);
817 free_if(connection
->curl_RANGE
);
818 free_if(connection
->curl_ERRORBUFFER
);
819 free_if(connection
->curl_POSTFIELDS
);
820 free_if(connection
->curl_REFERER
);
821 free_if(connection
->curl_USERAGENT
);
822 free_if(connection
->curl_FTPPORT
);
823 free_if(connection
->curl_COOKIE
);
824 free_curl_slist(connection
->curl_HTTPHEADER
);
825 free_curl_slist(connection
->httpPostBuffers
);
826 if (connection
->httpPostFirst
!= NULL
)
827 curl_formfree(connection
->httpPostFirst
);
828 free_curl_slist(connection
->curl_RESOLVE
);
829 free_if(connection
->curl_SSLCERT
);
830 free_if(connection
->curl_SSLCERTTYPE
);
831 free_if(connection
->curl_SSLCERTPASSWD
);
832 free_if(connection
->curl_SSLKEY
);
833 free_if(connection
->curl_SSLKEYTYPE
);
834 free_if(connection
->curl_SSLKEYPASSWD
);
835 free_if(connection
->curl_SSLENGINE
);
836 free_curl_slist(connection
->curl_QUOTE
);
837 free_curl_slist(connection
->curl_POSTQUOTE
);
838 free_if(connection
->curl_COOKIEFILE
);
839 free_if(connection
->curl_CUSTOMREQUEST
);
840 free_if(connection
->curl_INTERFACE
);
841 free_if(connection
->curl_CAINFO
);
842 free_if(connection
->curl_CAPATH
);
843 free_if(connection
->curl_RANDOM_FILE
);
844 free_if(connection
->curl_EGDSOCKET
);
845 free_if(connection
->curl_COOKIEJAR
);
846 free_if(connection
->curl_SSL_CIPHER_LIST
);
847 free_if(connection
->curl_PRIVATE
);
848 free_curl_slist(connection
->curl_HTTP200ALIASES
);
849 free_if(connection
->curl_NETRC_FILE
);
850 free_if(connection
->curl_FTP_ACCOUNT
);
851 free_if(connection
->curl_COOKIELIST
);
852 free_if(connection
->curl_FTP_ALTERNATIVE_TO_USER
);
853 free_if(connection
->curl_SSH_PUBLIC_KEYFILE
);
854 free_if(connection
->curl_SSH_PRIVATE_KEYFILE
);
855 free_if(connection
->curl_COPYPOSTFIELDS
);
856 free_if(connection
->curl_DNS_SERVERS
);
857 free_if(connection
->curl_MAIL_FROM
);
858 free_curl_slist(connection
->curl_MAIL_RCPT
);
862 static void checkConnection(Connection
* connection
)
867 static void checkConnection(Connection
*connection
)
869 Connection
*listIter
;
871 listIter
= connectionList
.tail
;
873 while (listIter
!= NULL
)
875 if (listIter
== connection
)
878 listIter
= listIter
->next
;
881 failwith("Invalid Connection");
885 static Connection
* findConnection(CURL
* h
)
887 Connection
*listIter
;
889 listIter
= connectionList
.tail
;
891 while (listIter
!= NULL
)
893 if (listIter
->connection
== h
)
896 listIter
= listIter
->next
;
899 failwith("Unknown handle");
902 void op_curl_easy_finalize(value v
)
904 Connection
* conn
= Connection_val(v
);
905 /* same connection may be referenced by several different
906 OCaml values, see e.g. caml_curl_multi_remove_finished */
908 if (0 == conn
->refcount
)
910 removeConnection(conn
, 1);
915 int op_curl_easy_compare(value v1
, value v2
)
917 size_t p1
= (size_t)Connection_val(v1
);
918 size_t p2
= (size_t)Connection_val(v2
);
919 return (p1
== p2
? 0 : (p1
> p2
? 1 : -1)); /* compare addresses */
922 intnat
op_curl_easy_hash(value v
)
924 return (size_t)Connection_val(v
); /* address */
927 static struct custom_operations curl_easy_ops
= {
929 op_curl_easy_finalize
,
930 op_curl_easy_compare
,
932 custom_serialize_default
,
933 custom_deserialize_default
,
934 #if defined(custom_compare_ext_default)
935 custom_compare_ext_default
,
939 value
caml_curl_alloc(Connection
* conn
)
941 value v
= caml_alloc_custom(&curl_easy_ops
, sizeof(Connection
*), 0, 1);
942 Connection_val(v
) = conn
;
947 #define WRAP_DATA_CALLBACK(f) \
948 static size_t f(char *ptr, size_t size, size_t nmemb, void *data)\
951 leave_blocking_section();\
952 result = f##_nolock(ptr,size,nmemb,data);\
953 enter_blocking_section();\
957 static size_t writeFunction_nolock(char *ptr
, size_t size
, size_t nmemb
, void *data
)
960 CAMLlocal2(result
, str
);
961 Connection
*conn
= (Connection
*)data
;
964 checkConnection(conn
);
966 str
= alloc_string(size
*nmemb
);
968 for (i
= 0; i
< size
*nmemb
; i
++)
969 Byte(str
, i
) = ptr
[i
];
971 result
= callback_exn(Field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
), str
);
973 CAMLreturnT(size_t, Is_exception_result(result
) ? 0 : Int_val(result
));
976 WRAP_DATA_CALLBACK(writeFunction
)
978 static size_t readFunction_nolock(void *ptr
, size_t size
, size_t nmemb
, void *data
)
982 Connection
*conn
= (Connection
*)data
;
985 checkConnection(conn
);
987 result
= callback_exn(Field(conn
->ocamlValues
, Ocaml_READFUNCTION
),
988 Val_int(size
*nmemb
));
990 if (Is_exception_result(result
))
992 CAMLreturnT(size_t,CURL_READFUNC_ABORT
);
995 length
= string_length(result
);
997 if (length
<= size
*nmemb
)
999 memcpy(ptr
, String_val(result
), length
);
1001 CAMLreturnT(size_t,length
);
1005 CAMLreturnT(size_t,CURL_READFUNC_ABORT
);
1009 WRAP_DATA_CALLBACK(readFunction
)
1011 static size_t headerFunction_nolock(char *ptr
, size_t size
, size_t nmemb
, void *data
)
1014 CAMLlocal2(result
,str
);
1015 Connection
*conn
= (Connection
*)data
;
1018 checkConnection(conn
);
1020 str
= alloc_string(size
*nmemb
);
1022 for (i
= 0; i
< size
*nmemb
; i
++)
1023 Byte(str
, i
) = ptr
[i
];
1025 result
= callback_exn(Field(conn
->ocamlValues
, Ocaml_HEADERFUNCTION
), str
);
1027 CAMLreturnT(size_t, Is_exception_result(result
) ? 0 : Int_val(result
));
1030 WRAP_DATA_CALLBACK(headerFunction
)
1032 static int progressFunction_nolock(void *data
,
1040 CAMLlocalN(callbackData
, 4);
1041 Connection
*conn
= (Connection
*)data
;
1043 checkConnection(conn
);
1045 callbackData
[0] = copy_double(dlTotal
);
1046 callbackData
[1] = copy_double(dlNow
);
1047 callbackData
[2] = copy_double(ulTotal
);
1048 callbackData
[3] = copy_double(ulNow
);
1050 result
= callbackN_exn(Field(conn
->ocamlValues
, Ocaml_PROGRESSFUNCTION
),
1053 CAMLreturnT(int, Is_exception_result(result
) ? 1 : Bool_val(result
));
1056 static int progressFunction(void *data
,
1063 leave_blocking_section();
1064 r
= progressFunction_nolock(data
,dlTotal
,dlNow
,ulTotal
,ulNow
);
1065 enter_blocking_section();
1069 static int debugFunction_nolock(CURL
*debugConnection
,
1070 curl_infotype infoType
,
1072 size_t bufferLength
,
1076 CAMLlocal3(camlDebugConnection
, camlInfoType
, camlMessage
);
1078 Connection
*conn
= (Connection
*)data
;
1079 (void)debugConnection
; /* not used */
1081 checkConnection(conn
);
1083 camlDebugConnection
= (value
)conn
;
1084 camlInfoType
= Val_long(infoType
);
1085 camlMessage
= alloc_string(bufferLength
);
1087 for (i
= 0; i
< bufferLength
; i
++)
1088 Byte(camlMessage
, i
) = buffer
[i
];
1090 callback3_exn(Field(conn
->ocamlValues
, Ocaml_DEBUGFUNCTION
),
1091 camlDebugConnection
,
1095 CAMLreturnT(int, 0);
1098 static int debugFunction(CURL
*debugConnection
,
1099 curl_infotype infoType
,
1101 size_t bufferLength
,
1105 leave_blocking_section();
1106 r
= debugFunction_nolock(debugConnection
, infoType
, buffer
, bufferLength
, data
);
1107 enter_blocking_section();
1111 static curlioerr
ioctlFunction_nolock(CURL
*ioctl
,
1116 CAMLlocal3(camlResult
, camlConnection
, camlCmd
);
1117 Connection
*conn
= (Connection
*)data
;
1118 curlioerr result
= CURLIOE_OK
;
1119 (void)ioctl
; /* not used */
1121 checkConnection(conn
);
1123 if (cmd
== CURLIOCMD_NOP
)
1124 camlCmd
= Val_long(0);
1125 else if (cmd
== CURLIOCMD_RESTARTREAD
)
1126 camlCmd
= Val_long(1);
1128 failwith("Invalid IOCTL Cmd!");
1130 camlConnection
= caml_curl_alloc(conn
);
1132 camlResult
= callback2_exn(Field(conn
->ocamlValues
, Ocaml_IOCTLFUNCTION
),
1136 if (Is_exception_result(camlResult
))
1138 result
= CURLIOE_FAILRESTART
;
1141 switch (Long_val(camlResult
))
1143 case 0: /* CURLIOE_OK */
1144 result
= CURLIOE_OK
;
1147 case 1: /* CURLIOE_UNKNOWNCMD */
1148 result
= CURLIOE_UNKNOWNCMD
;
1151 case 2: /* CURLIOE_FAILRESTART */
1152 result
= CURLIOE_FAILRESTART
;
1155 default: /* Incorrect return value, but let's handle it */
1156 result
= CURLIOE_FAILRESTART
;
1160 CAMLreturnT(curlioerr
, result
);
1163 static curlioerr
ioctlFunction(CURL
*ioctl
,
1168 leave_blocking_section();
1169 r
= ioctlFunction_nolock(ioctl
, cmd
, data
);
1170 enter_blocking_section();
1174 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1175 static int seekFunction_nolock(void *data
,
1180 CAMLlocal3(camlResult
, camlOffset
, camlOrigin
);
1181 Connection
*conn
= (Connection
*)data
;
1183 camlOffset
= copy_int64(offset
);
1185 if (origin
== SEEK_SET
)
1186 camlOrigin
= Val_long(0);
1187 else if (origin
== SEEK_CUR
)
1188 camlOrigin
= Val_long(1);
1189 else if (origin
== SEEK_END
)
1190 camlOrigin
= Val_long(2);
1192 failwith("Invalid seek code");
1194 camlResult
= callback2_exn(Field(conn
->ocamlValues
,
1195 Ocaml_SEEKFUNCTION
),
1200 if (Is_exception_result(camlResult
))
1201 result
= CURL_SEEKFUNC_FAIL
;
1203 switch (Int_val(camlResult
))
1205 case 0: result
= CURL_SEEKFUNC_OK
; break;
1206 case 1: result
= CURL_SEEKFUNC_FAIL
; break;
1207 case 2: result
= CURL_SEEKFUNC_CANTSEEK
; break;
1208 default: failwith("Invalid seek result");
1211 CAMLreturnT(int, result
);
1214 static int seekFunction(void *data
,
1219 leave_blocking_section();
1220 r
= seekFunction_nolock(data
,offset
,origin
);
1221 enter_blocking_section();
1227 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1228 static int openSocketFunction_nolock(void *data
,
1229 curlsocktype purpose
,
1230 struct curl_sockaddr
*addr
)
1234 Connection
*conn
= (Connection
*)data
;
1236 (void)purpose
; /* not used */
1238 sock
= socket(addr
->family
, addr
->socktype
, addr
->protocol
);
1243 result
= callback_exn(Field(conn
->ocamlValues
, Ocaml_OPENSOCKETFUNCTION
), Val_int(sock
));
1244 if (Is_exception_result(result
))
1251 CAMLreturnT(int, (sock
== -1) ? CURL_SOCKET_BAD
: sock
);
1254 static int openSocketFunction(void *data
,
1255 curlsocktype purpose
,
1256 struct curl_sockaddr
*address
)
1259 leave_blocking_section();
1260 r
= openSocketFunction_nolock(data
,purpose
,address
);
1261 enter_blocking_section();
1268 ** curl_global_init helper function
1271 CAMLprim value
helper_curl_global_init(value initOption
)
1273 CAMLparam1(initOption
);
1275 switch (Long_val(initOption
))
1277 case 0: /* CURLINIT_GLOBALALL */
1278 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_ALL
)));
1281 case 1: /* CURLINIT_GLOBALSSL */
1282 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL
)));
1285 case 2: /* CURLINIT_GLOBALWIN32 */
1286 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32
)));
1289 case 3: /* CURLINIT_GLOBALNOTHING */
1290 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING
)));
1294 failwith("Invalid Initialization Option");
1298 /* Keep compiler happy, we should never get here due to failwith() */
1299 CAMLreturn(Val_unit
);
1303 ** curl_global_cleanup helper function
1306 CAMLprim value
helper_curl_global_cleanup(void)
1310 curl_global_cleanup();
1312 CAMLreturn(Val_unit
);
1316 ** curl_easy_init helper function
1318 CAMLprim value
helper_curl_easy_init(void)
1323 result
= caml_curl_alloc(newConnection());
1328 CAMLprim value
helper_curl_easy_reset(value conn
)
1331 Connection
*connection
= Connection_val(conn
);
1333 checkConnection(connection
);
1334 curl_easy_reset(connection
->connection
);
1335 resetOcamlValues(connection
);
1337 CAMLreturn(Val_unit
);
1341 ** curl_easy_setopt helper utility functions
1344 static void handle_WRITEFUNCTION(Connection
*conn
, value option
)
1347 CURLcode result
= CURLE_OK
;
1349 if (Tag_val(option
) == Closure_tag
)
1350 Store_field(conn
->ocamlValues
, Ocaml_WRITEFUNCTION
, option
);
1352 failwith("Not a proper closure");
1354 result
= curl_easy_setopt(conn
->connection
,
1355 CURLOPT_WRITEFUNCTION
,
1358 if (result
!= CURLE_OK
)
1359 raiseError(conn
, result
);
1361 result
= curl_easy_setopt(conn
->connection
,
1365 if (result
!= CURLE_OK
)
1366 raiseError(conn
, result
);
1371 static void handle_READFUNCTION(Connection
*conn
, value option
)
1374 CURLcode result
= CURLE_OK
;
1376 if (Tag_val(option
) == Closure_tag
)
1377 Store_field(conn
->ocamlValues
, Ocaml_READFUNCTION
, option
);
1379 failwith("Not a proper closure");
1381 result
= curl_easy_setopt(conn
->connection
,
1382 CURLOPT_READFUNCTION
,
1385 if (result
!= CURLE_OK
)
1386 raiseError(conn
, result
);
1388 result
= curl_easy_setopt(conn
->connection
,
1392 if (result
!= CURLE_OK
)
1393 raiseError(conn
, result
);
1398 static void handle_slist(Connection
*conn
, struct curl_slist
** slist
, OcamlValue caml_option
, CURLoption curl_option
, value option
)
1401 CURLcode result
= CURLE_OK
;
1403 Store_field(conn
->ocamlValues
, caml_option
, option
);
1405 free_curl_slist(*slist
);
1408 while (Val_emptylist
!= option
)
1410 *slist
= curl_slist_append(*slist
, String_val(Field(option
, 0)));
1412 option
= Field(option
, 1);
1415 result
= curl_easy_setopt(conn
->connection
, curl_option
, *slist
);
1417 if (result
!= CURLE_OK
)
1418 raiseError(conn
, result
);
1423 #define SETOPT_STRING(name) \
1424 static void handle_##name(Connection *conn, value option) \
1426 CAMLparam1(option); \
1427 CURLcode result = CURLE_OK; \
1429 Store_field(conn->ocamlValues, Ocaml_##name, option); \
1431 if (conn->curl_##name != NULL) \
1432 free(conn->curl_##name); \
1434 conn->curl_##name = strdup(String_val(option)); \
1436 result = curl_easy_setopt(conn->connection, CURLOPT_##name, conn->curl_##name); \
1438 if (result != CURLE_OK) \
1439 raiseError(conn, result); \
1444 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1445 static void func_name(Connection *conn, value option) \
1447 CAMLparam1(option); \
1448 CURLcode result = CURLE_OK; \
1450 result = curl_easy_setopt(conn->connection, curl_option, conv_val(option)); \
1452 if (result != CURLE_OK) \
1453 raiseError(conn, result); \
1458 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1459 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1460 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1461 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1463 #define SETOPT_SLIST(name) \
1464 static void handle_##name(Connection* conn, value option) \
1466 handle_slist(conn,&(conn->curl_##name),Ocaml_##name,CURLOPT_##name,option); \
1470 SETOPT_LONG( INFILESIZE
)
1471 SETOPT_STRING( PROXY
)
1472 SETOPT_LONG( PROXYPORT
)
1473 SETOPT_BOOL( HTTPPROXYTUNNEL
)
1474 SETOPT_BOOL( VERBOSE
)
1475 SETOPT_BOOL( HEADER
)
1476 SETOPT_BOOL( NOPROGRESS
)
1478 #if HAVE_DECL_CURLOPT_NOSIGNAL
1479 SETOPT_BOOL( NOSIGNAL
)
1482 SETOPT_BOOL( NOBODY
)
1483 SETOPT_BOOL( FAILONERROR
)
1484 SETOPT_BOOL( UPLOAD
)
1486 SETOPT_BOOL( FTPLISTONLY
)
1487 SETOPT_BOOL( FTPAPPEND
)
1490 static void handle_NETRC(Connection
*conn
, value option
)
1493 CURLcode result
= CURLE_OK
;
1496 switch (Long_val(option
))
1498 case 0: /* CURL_NETRC_OPTIONAL */
1499 netrc
= CURL_NETRC_OPTIONAL
;
1502 case 1:/* CURL_NETRC_IGNORED */
1503 netrc
= CURL_NETRC_IGNORED
;
1506 case 2: /* CURL_NETRC_REQUIRED */
1507 netrc
= CURL_NETRC_REQUIRED
;
1511 failwith("Invalid NETRC Option");
1515 result
= curl_easy_setopt(conn
->connection
,
1519 if (result
!= CURLE_OK
)
1520 raiseError(conn
, result
);
1525 #if HAVE_DECL_CURLOPT_ENCODING
1526 static void handle_ENCODING(Connection
*conn
, value option
)
1529 CURLcode result
= CURLE_OK
;
1531 switch (Long_val(option
))
1533 case 0: /* CURL_ENCODING_NONE */
1534 result
= curl_easy_setopt(conn
->connection
,
1539 case 1: /* CURL_ENCODING_DEFLATE */
1540 result
= curl_easy_setopt(conn
->connection
,
1545 case 2: /* CURL_ENCODING_GZIP */
1546 result
= curl_easy_setopt(conn
->connection
,
1551 case 3: /* CURL_ENCODING_ANY */
1552 result
= curl_easy_setopt(conn
->connection
,
1558 failwith("Invalid Encoding Option");
1562 if (result
!= CURLE_OK
)
1563 raiseError(conn
, result
);
1570 SETOPT_BOOL( FOLLOWLOCATION
)
1571 SETOPT_BOOL( TRANSFERTEXT
)
1573 SETOPT_STRING( USERPWD
)
1574 SETOPT_STRING( PROXYUSERPWD
)
1575 SETOPT_STRING( RANGE
)
1577 static void handle_ERRORBUFFER(Connection
*conn
, value option
)
1580 CURLcode result
= CURLE_OK
;
1582 Store_field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
, option
);
1584 if (conn
->curl_ERRORBUFFER
!= NULL
)
1585 free(conn
->curl_ERRORBUFFER
);
1587 conn
->curl_ERRORBUFFER
= malloc(sizeof(char) * CURL_ERROR_SIZE
);
1589 result
= curl_easy_setopt(conn
->connection
,
1590 CURLOPT_ERRORBUFFER
,
1591 conn
->curl_ERRORBUFFER
);
1593 if (result
!= CURLE_OK
)
1594 raiseError(conn
, result
);
1599 SETOPT_LONG( TIMEOUT
)
1601 static void handle_POSTFIELDS(Connection
*conn
, value option
)
1604 CURLcode result
= CURLE_OK
;
1606 Store_field(conn
->ocamlValues
, Ocaml_POSTFIELDS
, option
);
1608 if (conn
->curl_POSTFIELDS
!= NULL
)
1609 free(conn
->curl_POSTFIELDS
);
1611 conn
->curl_POSTFIELDS
= strdup_ml(option
);
1613 result
= curl_easy_setopt(conn
->connection
,
1615 conn
->curl_POSTFIELDS
);
1617 if (result
!= CURLE_OK
)
1618 raiseError(conn
, result
);
1623 SETOPT_LONG( POSTFIELDSIZE
)
1624 SETOPT_STRING( REFERER
)
1625 SETOPT_STRING( USERAGENT
)
1626 SETOPT_STRING( FTPPORT
)
1627 SETOPT_LONG( LOW_SPEED_LIMIT
)
1628 SETOPT_LONG( LOW_SPEED_TIME
)
1629 SETOPT_LONG( RESUME_FROM
)
1630 SETOPT_STRING( COOKIE
)
1632 SETOPT_SLIST( HTTPHEADER
)
1634 static void handle_HTTPPOST(Connection
*conn
, value option
)
1637 CAMLlocal3(listIter
, formItem
, contentType
);
1638 CURLcode result
= CURLE_OK
;
1642 Store_field(conn
->ocamlValues
, Ocaml_HTTPPOST
, option
);
1644 free_curl_slist(conn
->httpPostBuffers
);
1645 if (conn
->httpPostFirst
!= NULL
)
1646 curl_formfree(conn
->httpPostFirst
);
1648 conn
->httpPostBuffers
= NULL
;
1649 conn
->httpPostFirst
= NULL
;
1650 conn
->httpPostLast
= NULL
;
1652 while (!Is_long(listIter
))
1654 formItem
= Field(listIter
, 0);
1656 switch (Tag_val(formItem
))
1658 case 0: /* CURLFORM_CONTENT */
1659 if (Wosize_val(formItem
) < 3)
1661 failwith("Incorrect CURLFORM_CONTENT parameters");
1664 if (Is_long(Field(formItem
, 2)) &&
1665 Long_val(Field(formItem
, 2)) == 0)
1667 curl_formadd(&conn
->httpPostFirst
,
1668 &conn
->httpPostLast
,
1670 String_val(Field(formItem
, 0)),
1671 CURLFORM_NAMELENGTH
,
1672 string_length(Field(formItem
, 0)),
1673 CURLFORM_COPYCONTENTS
,
1674 String_val(Field(formItem
, 1)),
1675 CURLFORM_CONTENTSLENGTH
,
1676 string_length(Field(formItem
, 1)),
1679 else if (Is_block(Field(formItem
, 2)))
1681 contentType
= Field(formItem
, 2);
1683 curl_formadd(&conn
->httpPostFirst
,
1684 &conn
->httpPostLast
,
1686 String_val(Field(formItem
, 0)),
1687 CURLFORM_NAMELENGTH
,
1688 string_length(Field(formItem
, 0)),
1689 CURLFORM_PTRCONTENTS
,
1690 String_val(Field(formItem
, 1)),
1691 CURLFORM_CONTENTSLENGTH
,
1692 string_length(Field(formItem
, 1)),
1693 CURLFORM_CONTENTTYPE
,
1694 String_val(Field(contentType
, 0)),
1699 failwith("Incorrect CURLFORM_CONTENT parameters");
1703 case 1: /* CURLFORM_FILECONTENT */
1704 if (Wosize_val(formItem
) < 3)
1706 failwith("Incorrect CURLFORM_FILECONTENT parameters");
1709 if (Is_long(Field(formItem
, 2)) &&
1710 Long_val(Field(formItem
, 2)) == 0)
1712 curl_formadd(&conn
->httpPostFirst
,
1713 &conn
->httpPostLast
,
1715 String_val(Field(formItem
, 0)),
1716 CURLFORM_NAMELENGTH
,
1717 string_length(Field(formItem
, 0)),
1718 CURLFORM_FILECONTENT
,
1719 String_val(Field(formItem
, 1)),
1722 else if (Is_block(Field(formItem
, 2)))
1724 contentType
= Field(formItem
, 2);
1726 curl_formadd(&conn
->httpPostFirst
,
1727 &conn
->httpPostLast
,
1729 String_val(Field(formItem
, 0)),
1730 CURLFORM_NAMELENGTH
,
1731 string_length(Field(formItem
, 0)),
1732 CURLFORM_FILECONTENT
,
1733 String_val(Field(formItem
, 1)),
1734 CURLFORM_CONTENTTYPE
,
1735 String_val(Field(contentType
, 0)),
1740 failwith("Incorrect CURLFORM_FILECONTENT parameters");
1744 case 2: /* CURLFORM_FILE */
1745 if (Wosize_val(formItem
) < 3)
1747 failwith("Incorrect CURLFORM_FILE parameters");
1750 if (Is_long(Field(formItem
, 2)) &&
1751 Long_val(Field(formItem
, 2)) == 0)
1753 curl_formadd(&conn
->httpPostFirst
,
1754 &conn
->httpPostLast
,
1756 String_val(Field(formItem
, 0)),
1757 CURLFORM_NAMELENGTH
,
1758 string_length(Field(formItem
, 0)),
1760 String_val(Field(formItem
, 1)),
1763 else if (Is_block(Field(formItem
, 2)))
1765 contentType
= Field(formItem
, 2);
1767 curl_formadd(&conn
->httpPostFirst
,
1768 &conn
->httpPostLast
,
1770 String_val(Field(formItem
, 0)),
1771 CURLFORM_NAMELENGTH
,
1772 string_length(Field(formItem
, 0)),
1774 String_val(Field(formItem
, 1)),
1775 CURLFORM_CONTENTTYPE
,
1776 String_val(Field(contentType
, 0)),
1781 failwith("Incorrect CURLFORM_FILE parameters");
1785 case 3: /* CURLFORM_BUFFER */
1786 if (Wosize_val(formItem
) < 4)
1788 failwith("Incorrect CURLFORM_BUFFER parameters");
1791 if (Is_long(Field(formItem
, 3)) &&
1792 Long_val(Field(formItem
, 3)) == 0)
1794 conn
->httpPostBuffers
= curl_slist_append(conn
->httpPostBuffers
, String_val(Field(formItem
, 2)));
1796 curl_formadd(&conn
->httpPostFirst
,
1797 &conn
->httpPostLast
,
1799 String_val(Field(formItem
, 0)),
1800 CURLFORM_NAMELENGTH
,
1801 string_length(Field(formItem
, 0)),
1803 String_val(Field(formItem
, 1)),
1805 conn
->httpPostBuffers
->data
,
1806 CURLFORM_BUFFERLENGTH
,
1807 string_length(Field(formItem
, 2)),
1810 else if (Is_block(Field(formItem
, 3)))
1812 conn
->httpPostBuffers
= curl_slist_append(conn
->httpPostBuffers
, String_val(Field(formItem
, 2)));
1814 contentType
= Field(formItem
, 3);
1816 curl_formadd(&conn
->httpPostFirst
,
1817 &conn
->httpPostLast
,
1819 String_val(Field(formItem
, 0)),
1820 CURLFORM_NAMELENGTH
,
1821 string_length(Field(formItem
, 0)),
1823 String_val(Field(formItem
, 1)),
1825 conn
->httpPostBuffers
->data
,
1826 CURLFORM_BUFFERLENGTH
,
1827 string_length(Field(formItem
, 2)),
1828 CURLFORM_CONTENTTYPE
,
1829 String_val(Field(contentType
, 0)),
1834 failwith("Incorrect CURLFORM_BUFFER parameters");
1839 listIter
= Field(listIter
, 1);
1842 result
= curl_easy_setopt(conn
->connection
,
1844 conn
->httpPostFirst
);
1846 if (result
!= CURLE_OK
)
1847 raiseError(conn
, result
);
1852 SETOPT_STRING( SSLCERT
)
1853 SETOPT_STRING( SSLCERTTYPE
)
1854 SETOPT_STRING( SSLCERTPASSWD
)
1855 SETOPT_STRING( SSLKEY
)
1856 SETOPT_STRING( SSLKEYTYPE
)
1857 SETOPT_STRING( SSLKEYPASSWD
)
1858 SETOPT_STRING( SSLENGINE
)
1859 SETOPT_BOOL( SSLENGINE_DEFAULT
)
1862 SETOPT_SLIST( QUOTE
)
1863 SETOPT_SLIST( POSTQUOTE
)
1865 static void handle_HEADERFUNCTION(Connection
*conn
, value option
)
1868 CURLcode result
= CURLE_OK
;
1870 if (Tag_val(option
) == Closure_tag
)
1871 Store_field(conn
->ocamlValues
, Ocaml_HEADERFUNCTION
, option
);
1873 failwith("Not a proper closure");
1875 result
= curl_easy_setopt(conn
->connection
,
1876 CURLOPT_HEADERFUNCTION
,
1879 if (result
!= CURLE_OK
)
1880 raiseError(conn
, result
);
1882 result
= curl_easy_setopt(conn
->connection
,
1883 CURLOPT_WRITEHEADER
,
1886 if (result
!= CURLE_OK
)
1887 raiseError(conn
, result
);
1892 SETOPT_STRING( COOKIEFILE
)
1893 SETOPT_LONG( SSLVERSION
)
1895 static void handle_TIMECONDITION(Connection
*conn
, value option
)
1898 CURLcode result
= CURLE_OK
;
1899 int timecond
= CURL_TIMECOND_NONE
;
1901 switch (Long_val(option
))
1903 case 0: timecond
= CURL_TIMECOND_NONE
; break;
1904 case 1: timecond
= CURL_TIMECOND_IFMODSINCE
; break;
1905 case 2: timecond
= CURL_TIMECOND_IFUNMODSINCE
; break;
1906 case 3: timecond
= CURL_TIMECOND_LASTMOD
; break;
1908 failwith("Invalid TIMECOND Option");
1912 result
= curl_easy_setopt(conn
->connection
, CURLOPT_TIMECONDITION
, timecond
);
1914 if (result
!= CURLE_OK
)
1915 raiseError(conn
, result
);
1920 SETOPT_VAL( TIMEVALUE
, Int32_val
)
1921 SETOPT_STRING( CUSTOMREQUEST
)
1922 SETOPT_STRING( INTERFACE
)
1924 static void handle_KRB4LEVEL(Connection
*conn
, value option
)
1927 CURLcode result
= CURLE_OK
;
1929 switch (Long_val(option
))
1931 case 0: /* KRB4_NONE */
1932 result
= curl_easy_setopt(conn
->connection
,
1937 case 1: /* KRB4_CLEAR */
1938 result
= curl_easy_setopt(conn
->connection
,
1943 case 2: /* KRB4_SAFE */
1944 result
= curl_easy_setopt(conn
->connection
,
1949 case 3: /* KRB4_CONFIDENTIAL */
1950 result
= curl_easy_setopt(conn
->connection
,
1955 case 4: /* KRB4_PRIVATE */
1956 result
= curl_easy_setopt(conn
->connection
,
1962 failwith("Invalid KRB4 Option");
1966 if (result
!= CURLE_OK
)
1967 raiseError(conn
, result
);
1972 static void handle_PROGRESSFUNCTION(Connection
*conn
, value option
)
1975 CURLcode result
= CURLE_OK
;
1977 if (Tag_val(option
) == Closure_tag
)
1978 Store_field(conn
->ocamlValues
, Ocaml_PROGRESSFUNCTION
, option
);
1980 failwith("Not a proper closure");
1982 result
= curl_easy_setopt(conn
->connection
,
1983 CURLOPT_PROGRESSFUNCTION
,
1985 if (result
!= CURLE_OK
)
1986 raiseError(conn
, result
);
1988 result
= curl_easy_setopt(conn
->connection
,
1989 CURLOPT_PROGRESSDATA
,
1992 if (result
!= CURLE_OK
)
1993 raiseError(conn
, result
);
1998 SETOPT_BOOL( SSL_VERIFYPEER
)
1999 SETOPT_STRING( CAINFO
)
2000 SETOPT_STRING( CAPATH
)
2001 SETOPT_BOOL( FILETIME
)
2002 SETOPT_LONG( MAXREDIRS
)
2003 SETOPT_LONG( MAXCONNECTS
)
2005 static void handle_CLOSEPOLICY(Connection
*conn
, value option
)
2008 CURLcode result
= CURLE_OK
;
2010 switch (Long_val(option
))
2012 case 0: /* CLOSEPOLICY_OLDEST */
2013 result
= curl_easy_setopt(conn
->connection
,
2014 CURLOPT_CLOSEPOLICY
,
2015 CURLCLOSEPOLICY_OLDEST
);
2018 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
2019 result
= curl_easy_setopt(conn
->connection
,
2020 CURLOPT_CLOSEPOLICY
,
2021 CURLCLOSEPOLICY_LEAST_RECENTLY_USED
);
2025 failwith("Invalid CLOSEPOLICY Option");
2029 if (result
!= CURLE_OK
)
2030 raiseError(conn
, result
);
2035 SETOPT_BOOL( FRESH_CONNECT
)
2036 SETOPT_BOOL( FORBID_REUSE
)
2037 SETOPT_STRING( RANDOM_FILE
)
2038 SETOPT_STRING( EGDSOCKET
)
2039 SETOPT_LONG( CONNECTTIMEOUT
)
2040 SETOPT_BOOL( HTTPGET
)
2042 static void handle_SSL_VERIFYHOST(Connection
*conn
, value option
)
2045 CURLcode result
= CURLE_OK
;
2047 switch (Long_val(option
))
2049 case 0: /* SSLVERIFYHOST_NONE */
2050 case 1: /* SSLVERIFYHOST_EXISTENCE */
2051 case 2: /* SSLVERIFYHOST_HOSTNAME */
2052 result
= curl_easy_setopt(conn
->connection
,
2053 CURLOPT_SSL_VERIFYHOST
,
2054 /* map EXISTENCE to HOSTNAME */
2055 Long_val(option
) == 0 ? 0 : 2);
2059 failwith("Invalid SSLVERIFYHOST Option");
2063 if (result
!= CURLE_OK
)
2064 raiseError(conn
, result
);
2069 SETOPT_STRING( COOKIEJAR
)
2070 SETOPT_STRING( SSL_CIPHER_LIST
)
2072 static void handle_HTTP_VERSION(Connection
*conn
, value option
)
2075 CURLcode result
= CURLE_OK
;
2077 switch (Long_val(option
))
2079 case 0: /* HTTP_VERSION_NONE */
2080 result
= curl_easy_setopt(conn
->connection
,
2081 CURLOPT_HTTP_VERSION
,
2082 CURL_HTTP_VERSION_NONE
);
2085 case 1: /* HTTP_VERSION_1_0 */
2086 result
= curl_easy_setopt(conn
->connection
,
2087 CURLOPT_HTTP_VERSION
,
2088 CURL_HTTP_VERSION_1_0
);
2091 case 2: /* HTTP_VERSION_1_1 */
2092 result
= curl_easy_setopt(conn
->connection
,
2093 CURLOPT_HTTP_VERSION
,
2094 CURL_HTTP_VERSION_1_1
);
2098 failwith("Invalid HTTP_VERSION Option");
2102 if (result
!= CURLE_OK
)
2103 raiseError(conn
, result
);
2108 SETOPT_BOOL( FTP_USE_EPSV
)
2109 SETOPT_LONG( DNS_CACHE_TIMEOUT
)
2110 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE
)
2112 static void handle_DEBUGFUNCTION(Connection
*conn
, value option
)
2115 CURLcode result
= CURLE_OK
;
2117 if (Tag_val(option
) == Closure_tag
)
2118 Store_field(conn
->ocamlValues
, Ocaml_DEBUGFUNCTION
, option
);
2120 failwith("Not a proper closure");
2122 result
= curl_easy_setopt(conn
->connection
,
2123 CURLOPT_DEBUGFUNCTION
,
2125 if (result
!= CURLE_OK
)
2126 raiseError(conn
, result
);
2128 result
= curl_easy_setopt(conn
->connection
,
2132 if (result
!= CURLE_OK
)
2133 raiseError(conn
, result
);
2138 #if HAVE_DECL_CURLOPT_PRIVATE
2139 SETOPT_STRING( PRIVATE
)
2142 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2143 SETOPT_SLIST( HTTP200ALIASES
)
2146 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2147 SETOPT_BOOL( UNRESTRICTED_AUTH
)
2150 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2151 SETOPT_BOOL( FTP_USE_EPRT
)
2154 #if HAVE_DECL_CURLOPT_HTTPAUTH
2155 static void handle_HTTPAUTH(Connection
*conn
, value option
)
2158 CAMLlocal1(listIter
);
2159 CURLcode result
= CURLE_OK
;
2160 long auth
= CURLAUTH_NONE
;
2164 while (!Is_long(listIter
))
2166 switch (Long_val(Field(listIter
, 0)))
2168 case 0: /* CURLAUTH_BASIC */
2169 auth
|= CURLAUTH_BASIC
;
2172 case 1: /* CURLAUTH_DIGEST */
2173 auth
|= CURLAUTH_DIGEST
;
2176 case 2: /* CURLAUTH_GSSNEGOTIATE */
2177 auth
|= CURLAUTH_GSSNEGOTIATE
;
2180 case 3: /* CURLAUTH_NTLM */
2181 auth
|= CURLAUTH_NTLM
;
2184 case 4: /* CURLAUTH_ANY */
2185 auth
|= CURLAUTH_ANY
;
2188 case 5: /* CURLAUTH_ANYSAFE */
2189 auth
|= CURLAUTH_ANYSAFE
;
2193 failwith("Invalid HTTPAUTH Value");
2197 listIter
= Field(listIter
, 1);
2200 result
= curl_easy_setopt(conn
->connection
,
2204 if (result
!= CURLE_OK
)
2205 raiseError(conn
, result
);
2211 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2212 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS
)
2215 #if HAVE_DECL_CURLOPT_PROXYAUTH
2216 static void handle_PROXYAUTH(Connection
*conn
, value option
)
2219 CAMLlocal1(listIter
);
2220 CURLcode result
= CURLE_OK
;
2221 long auth
= CURLAUTH_NONE
;
2225 while (!Is_long(listIter
))
2227 switch (Long_val(Field(listIter
, 0)))
2229 case 0: /* CURLAUTH_BASIC */
2230 auth
|= CURLAUTH_BASIC
;
2233 case 1: /* CURLAUTH_DIGEST */
2234 auth
|= CURLAUTH_DIGEST
;
2237 case 2: /* CURLAUTH_GSSNEGOTIATE */
2238 auth
|= CURLAUTH_GSSNEGOTIATE
;
2241 case 3: /* CURLAUTH_NTLM */
2242 auth
|= CURLAUTH_NTLM
;
2245 case 4: /* CURLAUTH_ANY */
2246 auth
|= CURLAUTH_ANY
;
2249 case 5: /* CURLAUTH_ANYSAFE */
2250 auth
|= CURLAUTH_ANYSAFE
;
2254 failwith("Invalid HTTPAUTH Value");
2258 listIter
= Field(listIter
, 1);
2261 result
= curl_easy_setopt(conn
->connection
,
2265 if (result
!= CURLE_OK
)
2266 raiseError(conn
, result
);
2272 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2273 SETOPT_LONG( FTP_RESPONSE_TIMEOUT
)
2276 #if HAVE_DECL_CURLOPT_IPRESOLVE
2277 static void handle_IPRESOLVE(Connection
*conn
, value option
)
2280 CURLcode result
= CURLE_OK
;
2282 switch (Long_val(option
))
2284 case 0: /* CURL_IPRESOLVE_WHATEVER */
2285 result
= curl_easy_setopt(conn
->connection
,
2287 CURL_IPRESOLVE_WHATEVER
);
2290 case 1: /* CURL_IPRESOLVE_V4 */
2291 result
= curl_easy_setopt(conn
->connection
,
2296 case 2: /* CURL_IPRESOLVE_V6 */
2297 result
= curl_easy_setopt(conn
->connection
,
2303 failwith("Invalid IPRESOLVE Value");
2307 if (result
!= CURLE_OK
)
2308 raiseError(conn
, result
);
2314 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2315 SETOPT_VAL( MAXFILESIZE
, Int32_val
)
2318 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2319 SETOPT_INT64( INFILESIZE_LARGE
)
2322 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2323 SETOPT_INT64( RESUME_FROM_LARGE
)
2326 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2327 SETOPT_INT64( MAXFILESIZE_LARGE
)
2330 #if HAVE_DECL_CURLOPT_NETRC_FILE
2331 SETOPT_STRING( NETRC_FILE
)
2334 #if HAVE_DECL_CURLOPT_FTP_SSL
2335 static void handle_FTP_SSL(Connection
*conn
, value option
)
2338 CURLcode result
= CURLE_OK
;
2340 switch (Long_val(option
))
2342 case 0: /* CURLFTPSSL_NONE */
2343 result
= curl_easy_setopt(conn
->connection
,
2348 case 1: /* CURLFTPSSL_TRY */
2349 result
= curl_easy_setopt(conn
->connection
,
2354 case 2: /* CURLFTPSSL_CONTROL */
2355 result
= curl_easy_setopt(conn
->connection
,
2357 CURLFTPSSL_CONTROL
);
2360 case 3: /* CURLFTPSSL_ALL */
2361 result
= curl_easy_setopt(conn
->connection
,
2367 failwith("Invalid FTP_SSL Value");
2371 if (result
!= CURLE_OK
)
2372 raiseError(conn
, result
);
2378 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2379 SETOPT_INT64( POSTFIELDSIZE_LARGE
)
2382 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2383 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2384 SETOPT_VAL_( handle_TCP_NODELAY
, CURLOPT_TCP_NODELAY
, Bool_val
)
2387 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2388 static void handle_FTPSSLAUTH(Connection
*conn
, value option
)
2391 CURLcode result
= CURLE_OK
;
2393 switch (Long_val(option
))
2395 case 0: /* CURLFTPAUTH_DEFAULT */
2396 result
= curl_easy_setopt(conn
->connection
,
2398 CURLFTPAUTH_DEFAULT
);
2401 case 1: /* CURLFTPAUTH_SSL */
2402 result
= curl_easy_setopt(conn
->connection
,
2407 case 2: /* CURLFTPAUTH_TLS */
2408 result
= curl_easy_setopt(conn
->connection
,
2414 failwith("Invalid FTPSSLAUTH value");
2418 if (result
!= CURLE_OK
)
2419 raiseError(conn
, result
);
2425 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
2426 static void handle_IOCTLFUNCTION(Connection
*conn
, value option
)
2429 CURLcode result
= CURLE_OK
;
2431 if (Tag_val(option
) == Closure_tag
)
2432 Store_field(conn
->ocamlValues
, Ocaml_IOCTLFUNCTION
, option
);
2434 failwith("Not a proper closure");
2436 result
= curl_easy_setopt(conn
->connection
,
2437 CURLOPT_IOCTLFUNCTION
,
2439 if (result
!= CURLE_OK
)
2440 raiseError(conn
, result
);
2442 result
= curl_easy_setopt(conn
->connection
,
2446 if (result
!= CURLE_OK
)
2447 raiseError(conn
, result
);
2453 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2454 SETOPT_STRING( FTP_ACCOUNT
)
2457 #if HAVE_DECL_CURLOPT_COOKIELIST
2458 SETOPT_STRING( COOKIELIST
)
2461 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2462 SETOPT_BOOL( IGNORE_CONTENT_LENGTH
)
2465 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2466 SETOPT_BOOL( FTP_SKIP_PASV_IP
)
2469 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2470 static void handle_FTP_FILEMETHOD(Connection
*conn
, value option
)
2473 CURLcode result
= CURLE_OK
;
2475 switch (Long_val(option
))
2477 case 0: /* CURLFTPMETHOD_DEFAULT */
2478 result
= curl_easy_setopt(conn
->connection
,
2479 CURLOPT_FTP_FILEMETHOD
,
2480 CURLFTPMETHOD_DEFAULT
);
2483 case 1: /* CURLFTMETHOD_MULTICWD */
2484 result
= curl_easy_setopt(conn
->connection
,
2485 CURLOPT_FTP_FILEMETHOD
,
2486 CURLFTPMETHOD_MULTICWD
);
2489 case 2: /* CURLFTPMETHOD_NOCWD */
2490 result
= curl_easy_setopt(conn
->connection
,
2491 CURLOPT_FTP_FILEMETHOD
,
2492 CURLFTPMETHOD_NOCWD
);
2495 case 3: /* CURLFTPMETHOD_SINGLECWD */
2496 result
= curl_easy_setopt(conn
->connection
,
2497 CURLOPT_FTP_FILEMETHOD
,
2498 CURLFTPMETHOD_SINGLECWD
);
2501 failwith("Invalid FTP_FILEMETHOD value");
2505 if (result
!= CURLE_OK
)
2506 raiseError(conn
, result
);
2512 #if HAVE_DECL_CURLOPT_LOCALPORT
2513 SETOPT_LONG( LOCALPORT
)
2516 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2517 SETOPT_LONG( LOCALPORTRANGE
)
2520 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2521 SETOPT_BOOL( CONNECT_ONLY
)
2524 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2525 SETOPT_INT64( MAX_SEND_SPEED_LARGE
)
2528 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2529 SETOPT_INT64( MAX_RECV_SPEED_LARGE
)
2532 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2533 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER
)
2536 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2537 SETOPT_BOOL( SSL_SESSIONID_CACHE
)
2540 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2541 static void handle_SSH_AUTH_TYPES(Connection
*conn
, value option
)
2544 CAMLlocal1(listIter
);
2545 CURLcode result
= CURLE_OK
;
2546 long authTypes
= CURLSSH_AUTH_NONE
;
2550 while (!Is_long(listIter
))
2552 switch (Long_val(Field(listIter
, 0)))
2554 case 0: /* CURLSSH_AUTH_ANY */
2555 authTypes
|= CURLSSH_AUTH_ANY
;
2558 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2559 authTypes
|= CURLSSH_AUTH_PUBLICKEY
;
2562 case 2: /* CURLSSH_AUTH_PASSWORD */
2563 authTypes
|= CURLSSH_AUTH_PASSWORD
;
2566 case 3: /* CURLSSH_AUTH_HOST */
2567 authTypes
|= CURLSSH_AUTH_HOST
;
2570 case 4: /* CURLSSH_AUTH_KEYBOARD */
2571 authTypes
|= CURLSSH_AUTH_KEYBOARD
;
2575 failwith("Invalid CURLSSH_AUTH_TYPES Value");
2579 listIter
= Field(listIter
, 1);
2582 result
= curl_easy_setopt(conn
->connection
,
2583 CURLOPT_SSH_AUTH_TYPES
,
2586 if (result
!= CURLE_OK
)
2587 raiseError(conn
, result
);
2593 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2594 SETOPT_STRING( SSH_PUBLIC_KEYFILE
)
2597 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2598 SETOPT_STRING( SSH_PRIVATE_KEYFILE
)
2601 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2602 static void handle_FTP_SSL_CCC(Connection
*conn
, value option
)
2605 CURLcode result
= CURLE_OK
;
2607 switch (Long_val(option
))
2609 case 0: /* CURLFTPSSL_CCC_NONE */
2610 result
= curl_easy_setopt(conn
->connection
,
2611 CURLOPT_FTP_SSL_CCC
,
2612 CURLFTPSSL_CCC_NONE
);
2615 case 1: /* CURLFTPSSL_CCC_PASSIVE */
2616 result
= curl_easy_setopt(conn
->connection
,
2617 CURLOPT_FTP_SSL_CCC
,
2618 CURLFTPSSL_CCC_PASSIVE
);
2621 case 2: /* CURLFTPSSL_CCC_ACTIVE */
2622 result
= curl_easy_setopt(conn
->connection
,
2623 CURLOPT_FTP_SSL_CCC
,
2624 CURLFTPSSL_CCC_ACTIVE
);
2628 failwith("Invalid FTPSSL_CCC value");
2632 if (result
!= CURLE_OK
)
2633 raiseError(conn
, result
);
2639 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2640 SETOPT_LONG( TIMEOUT_MS
)
2643 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2644 SETOPT_LONG( CONNECTTIMEOUT_MS
)
2647 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2648 SETOPT_BOOL( HTTP_TRANSFER_DECODING
)
2651 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2652 SETOPT_BOOL( HTTP_CONTENT_DECODING
)
2655 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
2656 SETOPT_LONG( NEW_FILE_PERMS
)
2659 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
2660 SETOPT_LONG( NEW_DIRECTORY_PERMS
)
2663 #if HAVE_DECL_CURLOPT_POST301
2664 SETOPT_BOOL( POST301
)
2667 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
2668 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5
)
2671 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
2672 SETOPT_STRING( COPYPOSTFIELDS
)
2675 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
2676 SETOPT_BOOL( PROXY_TRANSFER_MODE
)
2679 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
2680 static void handle_SEEKFUNCTION(Connection
*conn
, value option
)
2683 CURLcode result
= CURLE_OK
;
2685 if (Tag_val(option
) == Closure_tag
)
2686 Store_field(conn
->ocamlValues
, Ocaml_SEEKFUNCTION
, option
);
2688 failwith("Not a proper closure");
2690 result
= curl_easy_setopt(conn
->connection
,
2691 CURLOPT_SEEKFUNCTION
,
2694 if (result
!= CURLE_OK
)
2695 raiseError(conn
, result
);
2697 result
= curl_easy_setopt(conn
->connection
,
2701 if (result
!= CURLE_OK
)
2702 raiseError(conn
, result
);
2708 #if HAVE_DECL_CURLOPT_AUTOREFERER
2709 SETOPT_BOOL( AUTOREFERER
)
2712 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
2713 static void handle_OPENSOCKETFUNCTION(Connection
*conn
, value option
)
2716 CURLcode result
= CURLE_OK
;
2718 Store_field(conn
->ocamlValues
, Ocaml_OPENSOCKETFUNCTION
, option
);
2720 result
= curl_easy_setopt(conn
->connection
,
2721 CURLOPT_OPENSOCKETDATA
,
2724 if (result
!= CURLE_OK
)
2725 raiseError(conn
, result
);
2727 result
= curl_easy_setopt(conn
->connection
,
2728 CURLOPT_OPENSOCKETFUNCTION
,
2729 openSocketFunction
);
2731 if (result
!= CURLE_OK
)
2732 raiseError(conn
, result
);
2738 #if HAVE_DECL_CURLOPT_PROXYTYPE
2739 static void handle_PROXYTYPE(Connection
*conn
, value option
)
2742 CURLcode result
= CURLE_OK
;
2745 switch (Long_val(option
))
2747 case 0: proxy_type
= CURLPROXY_HTTP
; break;
2748 case 1: proxy_type
= CURLPROXY_HTTP_1_0
; break;
2749 case 2: proxy_type
= CURLPROXY_SOCKS4
; break;
2750 case 3: proxy_type
= CURLPROXY_SOCKS5
; break;
2751 case 4: proxy_type
= CURLPROXY_SOCKS4A
; break;
2752 case 5: proxy_type
= CURLPROXY_SOCKS5_HOSTNAME
; break;
2754 failwith("Invalid curl proxy type");
2757 result
= curl_easy_setopt(conn
->connection
,
2761 if (result
!= CURLE_OK
)
2762 raiseError(conn
, result
);
2768 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2773 CURLPROTO_HTTP
, CURLPROTO_HTTPS
, CURLPROTO_FTP
, CURLPROTO_FTPS
, CURLPROTO_SCP
, CURLPROTO_SFTP
,
2774 CURLPROTO_TELNET
, CURLPROTO_LDAP
, CURLPROTO_LDAPS
, CURLPROTO_DICT
, CURLPROTO_FILE
, CURLPROTO_TFTP
,
2775 /* factor out with autoconf? */
2776 #if defined(CURLPROTO_IMAP)
2781 #if defined(CURLPROTO_IMAPS)
2786 #if defined(CURLPROTO_POP3)
2791 #if defined(CURLPROTO_POP3S)
2796 #if defined(CURLPROTO_SMTP)
2801 #if defined(CURLPROTO_SMTPS)
2806 #if defined(CURLPROTO_RTSP)
2811 #if defined(CURLPROTO_RTMP)
2816 #if defined(CURLPROTO_RTMPT)
2821 #if defined(CURLPROTO_RTMPE)
2826 #if defined(CURLPROTO_RTMPTE)
2831 #if defined(CURLPROTO_RTMPS)
2836 #if defined(CURLPROTO_RTMPTS)
2841 #if defined(CURLPROTO_GOPHER)
2848 static void handle_PROTOCOLSOPTION(CURLoption curlopt
, Connection
*conn
, value option
)
2851 CURLcode result
= CURLE_OK
;
2855 while (Val_emptylist
!= option
)
2857 index
= Int_val(Field(option
, 0));
2858 if ((index
< 0) || ((size_t)index
>= sizeof(protoMap
) / sizeof(protoMap
[0])))
2859 failwith("Invalid curl protocol");
2861 protocols
= protocols
| protoMap
[index
];
2863 option
= Field(option
, 1);
2866 result
= curl_easy_setopt(conn
->connection
,
2870 if (result
!= CURLE_OK
)
2871 raiseError(conn
, result
);
2877 #if HAVE_DECL_CURLOPT_PROTOCOLS
2878 static void handle_PROTOCOLS(Connection
*conn
, value option
)
2880 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS
, conn
, option
);
2884 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2885 static void handle_REDIR_PROTOCOLS(Connection
*conn
, value option
)
2887 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS
, conn
, option
);
2891 #if HAVE_DECL_CURLOPT_RESOLVE
2892 SETOPT_SLIST( RESOLVE
)
2895 #if HAVE_DECL_CURLOPT_DNS_SERVERS
2896 SETOPT_STRING( DNS_SERVERS
)
2899 #if HAVE_DECL_CURLOPT_MAIL_FROM
2900 SETOPT_STRING( MAIL_FROM
)
2903 #if HAVE_DECL_CURLOPT_MAIL_RCPT
2904 SETOPT_SLIST( MAIL_RCPT
)
2907 static Connection
*duplicateConnection(Connection
*original
)
2909 Connection
*connection
;
2912 caml_enter_blocking_section();
2913 h
= curl_easy_duphandle(original
->connection
);
2914 caml_leave_blocking_section();
2916 connection
= allocConnection(h
);
2918 Store_field(connection
->ocamlValues
, Ocaml_WRITEFUNCTION
,
2919 Field(original
->ocamlValues
, Ocaml_WRITEFUNCTION
));
2920 Store_field(connection
->ocamlValues
, Ocaml_READFUNCTION
,
2921 Field(original
->ocamlValues
, Ocaml_READFUNCTION
));
2922 Store_field(connection
->ocamlValues
, Ocaml_ERRORBUFFER
,
2923 Field(original
->ocamlValues
, Ocaml_ERRORBUFFER
));
2924 Store_field(connection
->ocamlValues
, Ocaml_POSTFIELDS
,
2925 Field(original
->ocamlValues
, Ocaml_POSTFIELDS
));
2926 Store_field(connection
->ocamlValues
, Ocaml_HTTPHEADER
,
2927 Field(original
->ocamlValues
, Ocaml_HTTPHEADER
));
2928 Store_field(connection
->ocamlValues
, Ocaml_QUOTE
,
2929 Field(original
->ocamlValues
, Ocaml_QUOTE
));
2930 Store_field(connection
->ocamlValues
, Ocaml_POSTQUOTE
,
2931 Field(original
->ocamlValues
, Ocaml_POSTQUOTE
));
2932 Store_field(connection
->ocamlValues
, Ocaml_HEADERFUNCTION
,
2933 Field(original
->ocamlValues
, Ocaml_HEADERFUNCTION
));
2934 Store_field(connection
->ocamlValues
, Ocaml_PROGRESSFUNCTION
,
2935 Field(original
->ocamlValues
, Ocaml_PROGRESSFUNCTION
));
2936 Store_field(connection
->ocamlValues
, Ocaml_DEBUGFUNCTION
,
2937 Field(original
->ocamlValues
, Ocaml_DEBUGFUNCTION
));
2938 Store_field(connection
->ocamlValues
, Ocaml_HTTP200ALIASES
,
2939 Field(original
->ocamlValues
, Ocaml_HTTP200ALIASES
));
2940 Store_field(connection
->ocamlValues
, Ocaml_IOCTLFUNCTION
,
2941 Field(original
->ocamlValues
, Ocaml_IOCTLFUNCTION
));
2942 Store_field(connection
->ocamlValues
, Ocaml_SEEKFUNCTION
,
2943 Field(original
->ocamlValues
, Ocaml_SEEKFUNCTION
));
2945 if (Field(original
->ocamlValues
, Ocaml_URL
) != Val_unit
)
2946 handle_URL(connection
, Field(original
->ocamlValues
,
2948 if (Field(original
->ocamlValues
, Ocaml_PROXY
) != Val_unit
)
2949 handle_PROXY(connection
, Field(original
->ocamlValues
,
2951 if (Field(original
->ocamlValues
, Ocaml_USERPWD
) != Val_unit
)
2952 handle_USERPWD(connection
, Field(original
->ocamlValues
,
2954 if (Field(original
->ocamlValues
, Ocaml_PROXYUSERPWD
) != Val_unit
)
2955 handle_PROXYUSERPWD(connection
, Field(original
->ocamlValues
,
2956 Ocaml_PROXYUSERPWD
));
2957 if (Field(original
->ocamlValues
, Ocaml_RANGE
) != Val_unit
)
2958 handle_RANGE(connection
, Field(original
->ocamlValues
,
2960 if (Field(original
->ocamlValues
, Ocaml_ERRORBUFFER
) != Val_unit
)
2961 handle_ERRORBUFFER(connection
, Field(original
->ocamlValues
,
2962 Ocaml_ERRORBUFFER
));
2963 if (Field(original
->ocamlValues
, Ocaml_POSTFIELDS
) != Val_unit
)
2964 handle_POSTFIELDS(connection
, Field(original
->ocamlValues
,
2966 if (Field(original
->ocamlValues
, Ocaml_REFERER
) != Val_unit
)
2967 handle_REFERER(connection
, Field(original
->ocamlValues
,
2969 if (Field(original
->ocamlValues
, Ocaml_USERAGENT
) != Val_unit
)
2970 handle_USERAGENT(connection
, Field(original
->ocamlValues
,
2972 if (Field(original
->ocamlValues
, Ocaml_FTPPORT
) != Val_unit
)
2973 handle_FTPPORT(connection
, Field(original
->ocamlValues
,
2975 if (Field(original
->ocamlValues
, Ocaml_COOKIE
) != Val_unit
)
2976 handle_COOKIE(connection
, Field(original
->ocamlValues
,
2978 if (Field(original
->ocamlValues
, Ocaml_HTTPHEADER
) != Val_unit
)
2979 handle_HTTPHEADER(connection
, Field(original
->ocamlValues
,
2981 if (Field(original
->ocamlValues
, Ocaml_HTTPPOST
) != Val_unit
)
2982 handle_HTTPPOST(connection
, Field(original
->ocamlValues
,
2984 if (Field(original
->ocamlValues
, Ocaml_SSLCERT
) != Val_unit
)
2985 handle_SSLCERT(connection
, Field(original
->ocamlValues
,
2987 if (Field(original
->ocamlValues
, Ocaml_SSLCERTTYPE
) != Val_unit
)
2988 handle_SSLCERTTYPE(connection
, Field(original
->ocamlValues
,
2989 Ocaml_SSLCERTTYPE
));
2990 if (Field(original
->ocamlValues
, Ocaml_SSLCERTPASSWD
) != Val_unit
)
2991 handle_SSLCERTPASSWD(connection
, Field(original
->ocamlValues
,
2992 Ocaml_SSLCERTPASSWD
));
2993 if (Field(original
->ocamlValues
, Ocaml_SSLKEY
) != Val_unit
)
2994 handle_SSLKEY(connection
, Field(original
->ocamlValues
,
2996 if (Field(original
->ocamlValues
, Ocaml_SSLKEYTYPE
) != Val_unit
)
2997 handle_SSLKEYTYPE(connection
, Field(original
->ocamlValues
,
2999 if (Field(original
->ocamlValues
, Ocaml_SSLKEYPASSWD
) != Val_unit
)
3000 handle_SSLKEYPASSWD(connection
, Field(original
->ocamlValues
,
3001 Ocaml_SSLKEYPASSWD
));
3002 if (Field(original
->ocamlValues
, Ocaml_SSLENGINE
) != Val_unit
)
3003 handle_SSLENGINE(connection
, Field(original
->ocamlValues
,
3005 if (Field(original
->ocamlValues
, Ocaml_QUOTE
) != Val_unit
)
3006 handle_QUOTE(connection
, Field(original
->ocamlValues
,
3008 if (Field(original
->ocamlValues
, Ocaml_POSTQUOTE
) != Val_unit
)
3009 handle_POSTQUOTE(connection
, Field(original
->ocamlValues
,
3011 if (Field(original
->ocamlValues
, Ocaml_COOKIEFILE
) != Val_unit
)
3012 handle_COOKIEFILE(connection
, Field(original
->ocamlValues
,
3014 if (Field(original
->ocamlValues
, Ocaml_CUSTOMREQUEST
) != Val_unit
)
3015 handle_CUSTOMREQUEST(connection
, Field(original
->ocamlValues
,
3016 Ocaml_CUSTOMREQUEST
));
3017 if (Field(original
->ocamlValues
, Ocaml_INTERFACE
) != Val_unit
)
3018 handle_INTERFACE(connection
, Field(original
->ocamlValues
,
3020 if (Field(original
->ocamlValues
, Ocaml_CAINFO
) != Val_unit
)
3021 handle_CAINFO(connection
, Field(original
->ocamlValues
,
3023 if (Field(original
->ocamlValues
, Ocaml_CAPATH
) != Val_unit
)
3024 handle_CAPATH(connection
, Field(original
->ocamlValues
,
3026 if (Field(original
->ocamlValues
, Ocaml_RANDOM_FILE
) != Val_unit
)
3027 handle_RANDOM_FILE(connection
, Field(original
->ocamlValues
,
3028 Ocaml_RANDOM_FILE
));
3029 if (Field(original
->ocamlValues
, Ocaml_EGDSOCKET
) != Val_unit
)
3030 handle_EGDSOCKET(connection
, Field(original
->ocamlValues
,
3032 if (Field(original
->ocamlValues
, Ocaml_COOKIEJAR
) != Val_unit
)
3033 handle_COOKIEJAR(connection
, Field(original
->ocamlValues
,
3035 if (Field(original
->ocamlValues
, Ocaml_SSL_CIPHER_LIST
) != Val_unit
)
3036 handle_SSL_CIPHER_LIST(connection
, Field(original
->ocamlValues
,
3037 Ocaml_SSL_CIPHER_LIST
));
3038 if (Field(original
->ocamlValues
, Ocaml_PRIVATE
) != Val_unit
)
3039 handle_PRIVATE(connection
, Field(original
->ocamlValues
,
3041 if (Field(original
->ocamlValues
, Ocaml_HTTP200ALIASES
) != Val_unit
)
3042 handle_HTTP200ALIASES(connection
, Field(original
->ocamlValues
,
3043 Ocaml_HTTP200ALIASES
));
3044 if (Field(original
->ocamlValues
, Ocaml_NETRC_FILE
) != Val_unit
)
3045 handle_NETRC_FILE(connection
, Field(original
->ocamlValues
,
3047 if (Field(original
->ocamlValues
, Ocaml_FTP_ACCOUNT
) != Val_unit
)
3048 handle_FTP_ACCOUNT(connection
, Field(original
->ocamlValues
,
3049 Ocaml_FTP_ACCOUNT
));
3050 if (Field(original
->ocamlValues
, Ocaml_COOKIELIST
) != Val_unit
)
3051 handle_COOKIELIST(connection
, Field(original
->ocamlValues
,
3053 if (Field(original
->ocamlValues
, Ocaml_FTP_ALTERNATIVE_TO_USER
) != Val_unit
)
3054 handle_FTP_ALTERNATIVE_TO_USER(connection
,
3055 Field(original
->ocamlValues
,
3056 Ocaml_FTP_ALTERNATIVE_TO_USER
));
3057 if (Field(original
->ocamlValues
, Ocaml_SSH_PUBLIC_KEYFILE
) != Val_unit
)
3058 handle_SSH_PUBLIC_KEYFILE(connection
,
3059 Field(original
->ocamlValues
,
3060 Ocaml_SSH_PUBLIC_KEYFILE
));
3061 if (Field(original
->ocamlValues
, Ocaml_SSH_PRIVATE_KEYFILE
) != Val_unit
)
3062 handle_SSH_PRIVATE_KEYFILE(connection
,
3063 Field(original
->ocamlValues
,
3064 Ocaml_SSH_PRIVATE_KEYFILE
));
3065 if (Field(original
->ocamlValues
, Ocaml_COPYPOSTFIELDS
) != Val_unit
)
3066 handle_COPYPOSTFIELDS(connection
,
3067 Field(original
->ocamlValues
,
3068 Ocaml_COPYPOSTFIELDS
));
3069 if (Field(original
->ocamlValues
, Ocaml_DNS_SERVERS
) != Val_unit
)
3070 handle_DNS_SERVERS(connection
,
3071 Field(original
->ocamlValues
,
3072 Ocaml_DNS_SERVERS
));
3073 if (Field(original
->ocamlValues
, Ocaml_MAIL_FROM
) != Val_unit
)
3074 handle_MAIL_FROM(connection
,
3075 Field(original
->ocamlValues
,
3077 if (Field(original
->ocamlValues
, Ocaml_MAIL_RCPT
) != Val_unit
)
3078 handle_MAIL_RCPT(connection
,
3079 Field(original
->ocamlValues
,
3086 ** curl_easy_setopt helper function
3089 #define MAP(name) { handle_ ## name, "CURLOPT_"#name /*, CURLOPT_##name */ }
3090 #define MAP_NO(name) { NULL, "CURLOPT_"#name /*, CURLOPT_##name */ }
3092 CURLOptionMapping implementedOptionMap
[] =
3100 MAP(HTTPPROXYTUNNEL
),
3104 #if HAVE_DECL_CURLOPT_NOSIGNAL
3116 #if HAVE_DECL_CURLOPT_ENCODING
3121 MAP(FOLLOWLOCATION
),
3134 MAP(LOW_SPEED_LIMIT
),
3135 MAP(LOW_SPEED_TIME
),
3147 MAP(SSLENGINE_DEFAULT
),
3151 MAP(HEADERFUNCTION
),
3159 MAP(PROGRESSFUNCTION
),
3160 MAP(SSL_VERIFYPEER
),
3171 MAP(CONNECTTIMEOUT
),
3173 MAP(SSL_VERIFYHOST
),
3175 MAP(SSL_CIPHER_LIST
),
3178 MAP(DNS_CACHE_TIMEOUT
),
3179 MAP(DNS_USE_GLOBAL_CACHE
),
3181 #if HAVE_DECL_CURLOPT_PRIVATE
3186 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
3187 MAP(HTTP200ALIASES
),
3189 MAP_NO(HTTP200ALIASES
),
3191 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
3192 MAP(UNRESTRICTED_AUTH
),
3194 MAP_NO(UNRESTRICTED_AUTH
),
3196 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
3199 MAP_NO(FTP_USE_EPRT
),
3201 #if HAVE_DECL_CURLOPT_HTTPAUTH
3206 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
3207 MAP(FTP_CREATE_MISSING_DIRS
),
3209 MAP_NO(FTP_CREATE_MISSING_DIRS
),
3211 #if HAVE_DECL_CURLOPT_PROXYAUTH
3216 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
3217 MAP(FTP_RESPONSE_TIMEOUT
),
3219 MAP_NO(FTP_RESPONSE_TIMEOUT
),
3221 #if HAVE_DECL_CURLOPT_IPRESOLVE
3226 #if HAVE_DECL_CURLOPT_MAXFILESIZE
3229 MAP_NO(MAXFILESIZE
),
3231 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
3232 MAP(INFILESIZE_LARGE
),
3234 MAP_NO(INFILESIZE_LARGE
),
3236 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
3237 MAP(RESUME_FROM_LARGE
),
3239 MAP_NO(RESUME_FROM_LARGE
),
3241 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
3242 MAP(MAXFILESIZE_LARGE
),
3244 MAP_NO(MAXFILESIZE_LARGE
),
3246 #if HAVE_DECL_CURLOPT_NETRC_FILE
3251 #if HAVE_DECL_CURLOPT_FTP_SSL
3256 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
3257 MAP(POSTFIELDSIZE_LARGE
),
3259 MAP_NO(POSTFIELDSIZE_LARGE
),
3261 #if HAVE_DECL_CURLOPT_TCP_NODELAY
3264 MAP_NO(TCP_NODELAY
),
3266 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
3271 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
3274 MAP_NO(IOCTLFUNCTION
),
3276 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
3279 MAP_NO(FTP_ACCOUNT
),
3281 #if HAVE_DECL_CURLOPT_COOKIELIST
3286 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
3287 MAP(IGNORE_CONTENT_LENGTH
),
3289 MAP_NO(IGNORE_CONTENT_LENGTH
),
3291 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
3292 MAP(FTP_SKIP_PASV_IP
),
3294 MAP_NO(FTP_SKIP_PASV_IP
),
3296 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
3297 MAP(FTP_FILEMETHOD
),
3299 MAP_NO(FTP_FILEMETHOD
),
3301 #if HAVE_DECL_CURLOPT_LOCALPORT
3306 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
3307 MAP(LOCALPORTRANGE
),
3309 MAP_NO(LOCALPORTRANGE
),
3311 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
3314 MAP_NO(CONNECT_ONLY
),
3316 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
3317 MAP(MAX_SEND_SPEED_LARGE
),
3319 MAP_NO(MAX_SEND_SPEED_LARGE
),
3321 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
3322 MAP(MAX_RECV_SPEED_LARGE
),
3324 MAP_NO(MAX_RECV_SPEED_LARGE
),
3326 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
3327 MAP(FTP_ALTERNATIVE_TO_USER
),
3329 MAP_NO(FTP_ALTERNATIVE_TO_USER
),
3331 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
3332 MAP(SSL_SESSIONID_CACHE
),
3334 MAP_NO(SSL_SESSIONID_CACHE
),
3336 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
3337 MAP(SSH_AUTH_TYPES
),
3339 MAP_NO(SSH_AUTH_TYPES
),
3341 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
3342 MAP(SSH_PUBLIC_KEYFILE
),
3344 MAP_NO(SSH_PUBLIC_KEYFILE
),
3346 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
3347 MAP(SSH_PRIVATE_KEYFILE
),
3349 MAP_NO(SSH_PRIVATE_KEYFILE
),
3351 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
3354 MAP_NO(FTP_SSL_CCC
),
3356 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
3361 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3362 MAP(CONNECTTIMEOUT_MS
),
3364 MAP_NO(CONNECTTIMEOUT_MS
),
3366 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3367 MAP(HTTP_TRANSFER_DECODING
),
3369 MAP_NO(HTTP_TRANSFER_DECODING
),
3371 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3372 MAP(HTTP_CONTENT_DECODING
),
3374 MAP_NO(HTTP_CONTENT_DECODING
),
3376 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3377 MAP(NEW_FILE_PERMS
),
3379 MAP_NO(NEW_FILE_PERMS
),
3381 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3382 MAP(NEW_DIRECTORY_PERMS
),
3384 MAP_NO(NEW_DIRECTORY_PERMS
),
3386 #if HAVE_DECL_CURLOPT_POST301
3391 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3392 MAP(SSH_HOST_PUBLIC_KEY_MD5
),
3394 MAP_NO(SSH_HOST_PUBLIC_KEY_MD5
),
3396 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3397 MAP(COPYPOSTFIELDS
),
3399 MAP_NO(COPYPOSTFIELDS
),
3401 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3402 MAP(PROXY_TRANSFER_MODE
),
3404 MAP_NO(PROXY_TRANSFER_MODE
),
3406 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3409 MAP_NO(SEEKFUNCTION
),
3411 #if HAVE_DECL_CURLOPT_AUTOREFERER
3414 MAP_NO(AUTOREFERER
),
3416 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
3417 MAP(OPENSOCKETFUNCTION
),
3419 MAP_NO(OPENSOCKETFUNCTION
),
3421 #if HAVE_DECL_CURLOPT_PROXYTYPE
3426 #if HAVE_DECL_CURLOPT_PROTOCOLS
3431 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3432 MAP(REDIR_PROTOCOLS
),
3434 MAP_NO(REDIR_PROTOCOLS
),
3436 #if HAVE_DECL_CURLOPT_RESOLVE
3441 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3444 MAP_NO(DNS_SERVERS
),
3446 #if HAVE_DECL_CURLOPT_MAIL_FROM
3451 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3458 CAMLprim value
helper_curl_easy_setopt(value conn
, value option
)
3460 CAMLparam2(conn
, option
);
3462 Connection
*connection
= Connection_val(conn
);
3463 CURLOptionMapping
* thisOption
= NULL
;
3464 static value
* exception
= NULL
;
3466 checkConnection(connection
);
3468 if (!Is_block(option
))
3469 failwith("Not a block");
3471 if (Wosize_val(option
) < 1)
3472 failwith("Insufficient data in block");
3474 data
= Field(option
, 0);
3476 if (Tag_val(option
) < sizeof(implementedOptionMap
)/sizeof(CURLOptionMapping
))
3478 thisOption
= &implementedOptionMap
[Tag_val(option
)];
3479 if (thisOption
->optionHandler
)
3480 thisOption
->optionHandler(connection
, data
);
3483 if (NULL
== exception
)
3485 exception
= caml_named_value("Curl.NotImplemented");
3486 if (NULL
== exception
) caml_invalid_argument("Curl.NotImplemented");
3489 caml_raise_with_string(*exception
, thisOption
->name
);
3493 failwith("Invalid CURLOPT Option");
3495 CAMLreturn(Val_unit
);
3499 ** curl_easy_perform helper function
3502 CAMLprim value
helper_curl_easy_perform(value conn
)
3505 CURLcode result
= CURLE_OK
;
3506 Connection
*connection
= Connection_val(conn
);
3508 checkConnection(connection
);
3510 enter_blocking_section();
3511 result
= curl_easy_perform(connection
->connection
);
3512 leave_blocking_section();
3514 if (result
!= CURLE_OK
)
3515 raiseError(connection
, result
);
3517 CAMLreturn(Val_unit
);
3521 ** curl_easy_cleanup helper function
3524 CAMLprim value
helper_curl_easy_cleanup(value conn
)
3527 Connection
*connection
= Connection_val(conn
);
3529 checkConnection(connection
);
3531 removeConnection(connection
, 0);
3533 CAMLreturn(Val_unit
);
3537 ** curl_easy_duphandle helper function
3540 CAMLprim value
helper_curl_easy_duphandle(value conn
)
3544 Connection
*connection
= Connection_val(conn
);
3546 checkConnection(connection
);
3548 result
= caml_curl_alloc(duplicateConnection(connection
));
3554 ** curl_easy_getinfo helper function
3557 enum GetInfoResultType
{
3558 StringValue
, LongValue
, DoubleValue
, StringListValue
3561 value
convertStringList(struct curl_slist
*slist
)
3564 CAMLlocal3(result
, current
, next
);
3565 struct curl_slist
*p
= slist
;
3567 result
= Val_int(0);
3568 current
= Val_int(0);
3573 next
= alloc_tuple(2);
3574 Store_field(next
, 0, copy_string(p
->data
));
3575 Store_field(next
, 1, Val_int(0));
3577 if (result
== Val_int(0))
3580 if (current
!= Val_int(0))
3581 Store_field(current
, 1, next
);
3588 curl_slist_free_all(slist
);
3593 CAMLprim value
helper_curl_easy_getinfo(value conn
, value option
)
3595 CAMLparam2(conn
, option
);
3597 CURLcode curlResult
;
3598 Connection
*connection
= Connection_val(conn
);
3599 enum GetInfoResultType resultType
;
3600 char *strValue
= NULL
;
3603 struct curl_slist
*stringListValue
= NULL
;
3605 checkConnection(connection
);
3607 switch(Long_val(option
))
3609 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3610 case 0: /* CURLINFO_EFFECTIVE_URL */
3611 resultType
= StringValue
;
3613 curlResult
= curl_easy_getinfo(connection
->connection
,
3614 CURLINFO_EFFECTIVE_URL
,
3618 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3621 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3622 case 1: /* CURLINFO_HTTP_CODE */
3623 case 2: /* CURLINFO_RESPONSE_CODE */
3624 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3625 resultType
= LongValue
;
3627 curlResult
= curl_easy_getinfo(connection
->connection
,
3628 CURLINFO_RESPONSE_CODE
,
3631 resultType
= LongValue
;
3633 curlResult
= curl_easy_getinfo(connection
->connection
,
3640 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3641 case 3: /* CURLINFO_TOTAL_TIME */
3642 resultType
= DoubleValue
;
3644 curlResult
= curl_easy_getinfo(connection
->connection
,
3645 CURLINFO_TOTAL_TIME
,
3650 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3651 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3652 resultType
= DoubleValue
;
3654 curlResult
= curl_easy_getinfo(connection
->connection
,
3655 CURLINFO_NAMELOOKUP_TIME
,
3660 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3661 case 5: /* CURLINFO_CONNECT_TIME */
3662 resultType
= DoubleValue
;
3664 curlResult
= curl_easy_getinfo(connection
->connection
,
3665 CURLINFO_CONNECT_TIME
,
3670 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3671 case 6: /* CURLINFO_PRETRANSFER_TIME */
3672 resultType
= DoubleValue
;
3674 curlResult
= curl_easy_getinfo(connection
->connection
,
3675 CURLINFO_PRETRANSFER_TIME
,
3680 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3681 case 7: /* CURLINFO_SIZE_UPLOAD */
3682 resultType
= DoubleValue
;
3684 curlResult
= curl_easy_getinfo(connection
->connection
,
3685 CURLINFO_SIZE_UPLOAD
,
3690 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3691 case 8: /* CURLINFO_SIZE_DOWNLOAD */
3692 resultType
= DoubleValue
;
3694 curlResult
= curl_easy_getinfo(connection
->connection
,
3695 CURLINFO_SIZE_DOWNLOAD
,
3700 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
3701 case 9: /* CURLINFO_SPEED_DOWNLOAD */
3702 resultType
= DoubleValue
;
3704 curlResult
= curl_easy_getinfo(connection
->connection
,
3705 CURLINFO_SPEED_DOWNLOAD
,
3710 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
3711 case 10: /* CURLINFO_SPEED_UPLOAD */
3712 resultType
= DoubleValue
;
3714 curlResult
= curl_easy_getinfo(connection
->connection
,
3715 CURLINFO_SPEED_UPLOAD
,
3721 #if HAVE_DECL_CURLINFO_HEADER_SIZE
3722 case 11: /* CURLINFO_HEADER_SIZE */
3723 resultType
= LongValue
;
3725 curlResult
= curl_easy_getinfo(connection
->connection
,
3726 CURLINFO_HEADER_SIZE
,
3731 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
3732 case 12: /* CURLINFO_REQUEST_SIZE */
3733 resultType
= LongValue
;
3735 curlResult
= curl_easy_getinfo(connection
->connection
,
3736 CURLINFO_REQUEST_SIZE
,
3741 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
3742 case 13: /* CURLINFO_SSL_VERIFYRESULT */
3743 resultType
= LongValue
;
3745 curlResult
= curl_easy_getinfo(connection
->connection
,
3746 CURLINFO_SSL_VERIFYRESULT
,
3751 #if HAVE_DECL_CURLINFO_FILETIME
3752 case 14: /* CURLINFO_FILETIME */
3753 resultType
= DoubleValue
;
3755 curlResult
= curl_easy_getinfo(connection
->connection
,
3759 doubleValue
= longValue
;
3763 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
3764 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
3765 resultType
= DoubleValue
;
3767 curlResult
= curl_easy_getinfo(connection
->connection
,
3768 CURLINFO_CONTENT_LENGTH_DOWNLOAD
,
3773 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
3774 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
3775 resultType
= DoubleValue
;
3777 curlResult
= curl_easy_getinfo(connection
->connection
,
3778 CURLINFO_CONTENT_LENGTH_UPLOAD
,
3783 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
3784 case 17: /* CURLINFO_STARTTRANSFER_TIME */
3785 resultType
= DoubleValue
;
3787 curlResult
= curl_easy_getinfo(connection
->connection
,
3788 CURLINFO_STARTTRANSFER_TIME
,
3793 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
3794 case 18: /* CURLINFO_CONTENT_TYPE */
3795 resultType
= StringValue
;
3797 curlResult
= curl_easy_getinfo(connection
->connection
,
3798 CURLINFO_CONTENT_TYPE
,
3803 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
3804 case 19: /* CURLINFO_REDIRECT_TIME */
3805 resultType
= DoubleValue
;
3807 curlResult
= curl_easy_getinfo(connection
->connection
,
3808 CURLINFO_REDIRECT_TIME
,
3813 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
3814 case 20: /* CURLINFO_REDIRECT_COUNT */
3815 resultType
= LongValue
;
3817 curlResult
= curl_easy_getinfo(connection
->connection
,
3818 CURLINFO_REDIRECT_COUNT
,
3823 #if HAVE_DECL_CURLINFO_PRIVATE
3824 case 21: /* CURLINFO_PRIVATE */
3825 resultType
= StringValue
;
3827 curlResult
= curl_easy_getinfo(connection
->connection
,
3833 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
3834 case 22: /* CURLINFO_HTTP_CONNECTCODE */
3835 resultType
= LongValue
;
3837 curlResult
= curl_easy_getinfo(connection
->connection
,
3838 CURLINFO_HTTP_CONNECTCODE
,
3843 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
3844 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
3845 resultType
= LongValue
;
3847 curlResult
= curl_easy_getinfo(connection
->connection
,
3848 CURLINFO_HTTPAUTH_AVAIL
,
3853 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
3854 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
3855 resultType
= LongValue
;
3857 curlResult
= curl_easy_getinfo(connection
->connection
,
3858 CURLINFO_PROXYAUTH_AVAIL
,
3863 #if HAVE_DECL_CURLINFO_OS_ERRNO
3864 case 25: /* CURLINFO_OS_ERRNO */
3865 resultType
= LongValue
;
3867 curlResult
= curl_easy_getinfo(connection
->connection
,
3873 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
3874 case 26: /* CURLINFO_NUM_CONNECTS */
3875 resultType
= LongValue
;
3877 curlResult
= curl_easy_getinfo(connection
->connection
,
3878 CURLINFO_NUM_CONNECTS
,
3883 #if HAVE_DECL_CURLINFO_SSL_ENGINES
3884 case 27: /* CURLINFO_SSL_ENGINES */
3885 resultType
= StringListValue
;
3887 curlResult
= curl_easy_getinfo(connection
->connection
,
3888 CURLINFO_SSL_ENGINES
,
3893 #if HAVE_DECL_CURLINFO_COOKIELIST
3894 case 28: /* CURLINFO_COOKIELIST */
3895 resultType
= StringListValue
;
3897 curlResult
= curl_easy_getinfo(connection
->connection
,
3898 CURLINFO_COOKIELIST
,
3903 #if HAVE_DECL_CURLINFO_LASTSOCKET
3904 case 29: /* CURLINFO_LASTSOCKET */
3905 resultType
= LongValue
;
3907 curlResult
= curl_easy_getinfo(connection
->connection
,
3908 CURLINFO_LASTSOCKET
,
3913 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
3914 case 30: /* CURLINFO_FTP_ENTRY_PATH */
3915 resultType
= StringValue
;
3917 curlResult
= curl_easy_getinfo(connection
->connection
,
3918 CURLINFO_FTP_ENTRY_PATH
,
3923 #if HAVE_DECL_CURLINFO_REDIRECT_URL
3924 case 31: /* CURLINFO_REDIRECT_URL */
3925 resultType
= StringValue
;
3927 curlResult
= curl_easy_getinfo(connection
->connection
,
3928 CURLINFO_REDIRECT_URL
,
3932 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
3935 #if HAVE_DECL_CURLINFO_PRIMARY_IP
3936 case 32: /* CURLINFO_PRIMARY_IP */
3937 resultType
= StringValue
;
3939 curlResult
= curl_easy_getinfo(connection
->connection
,
3940 CURLINFO_PRIMARY_IP
,
3944 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
3947 #if HAVE_DECL_CURLINFO_LOCAL_IP
3948 case 33: /* CURLINFO_LOCAL_IP */
3949 resultType
= StringValue
;
3951 curlResult
= curl_easy_getinfo(connection
->connection
,
3956 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
3959 #if HAVE_DECL_CURLINFO_LOCAL_PORT
3960 case 34: /* CURLINFO_LOCAL_PORT */
3961 resultType
= LongValue
;
3963 curlResult
= curl_easy_getinfo(connection
->connection
,
3964 CURLINFO_LOCAL_PORT
,
3968 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
3971 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
3972 case 35: /* CURLINFO_CONDITION_UNMET */
3973 resultType
= LongValue
;
3975 curlResult
= curl_easy_getinfo(connection
->connection
,
3976 CURLINFO_CONDITION_UNMET
,
3980 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
3984 failwith("Invalid CURLINFO Option");
3988 if (curlResult
!= CURLE_OK
)
3989 raiseError(connection
, curlResult
);
3994 result
= alloc(1, StringValue
);
3995 Store_field(result
, 0, copy_string(strValue
?strValue
:""));
3999 result
= alloc(1, LongValue
);
4000 Store_field(result
, 0, Val_long(longValue
));
4004 result
= alloc(1, DoubleValue
);
4005 Store_field(result
, 0, copy_double(doubleValue
));
4008 case StringListValue
:
4009 result
= alloc(1, StringListValue
);
4010 Store_field(result
, 0, convertStringList(stringListValue
));
4018 ** curl_escape helper function
4021 CAMLprim value
helper_curl_escape(value str
)
4027 curlResult
= curl_escape(String_val(str
), string_length(str
));
4028 result
= copy_string(curlResult
);
4035 ** curl_unescape helper function
4038 CAMLprim value
helper_curl_unescape(value str
)
4044 curlResult
= curl_unescape(String_val(str
), string_length(str
));
4045 result
= copy_string(curlResult
);
4052 ** curl_getdate helper function
4055 CAMLprim value
helper_curl_getdate(value str
, value now
)
4057 CAMLparam2(str
, now
);
4062 curlNow
= (time_t)Double_val(now
);
4063 curlResult
= curl_getdate(String_val(str
), &curlNow
);
4064 result
= copy_double((double)curlResult
);
4070 ** curl_version helper function
4073 CAMLprim value
helper_curl_version(void)
4079 str
= curl_version();
4080 result
= copy_string(str
);
4085 struct CURLVersionBitsMapping
4091 struct CURLVersionBitsMapping versionBitsMap
[] =
4093 {CURL_VERSION_IPV6
, "ipv6"},
4094 {CURL_VERSION_KERBEROS4
, "kerberos4"},
4095 {CURL_VERSION_SSL
, "ssl"},
4096 {CURL_VERSION_LIBZ
, "libz"},
4097 {CURL_VERSION_NTLM
, "ntlm"},
4098 {CURL_VERSION_GSSNEGOTIATE
, "gssnegotiate"},
4099 {CURL_VERSION_DEBUG
, "debug"},
4100 {CURL_VERSION_CURLDEBUG
, "curldebug"},
4101 {CURL_VERSION_ASYNCHDNS
, "asynchdns"},
4102 {CURL_VERSION_SPNEGO
, "spnego"},
4103 {CURL_VERSION_LARGEFILE
, "largefile"},
4104 {CURL_VERSION_IDN
, "idn"},
4105 {CURL_VERSION_SSPI
, "sspi"},
4106 {CURL_VERSION_CONV
, "conv"},
4107 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
4108 {CURL_VERSION_TLSAUTH_SRP
, "srp"},
4110 #if HAVE_DECL_CURL_VERSION_NTLM_WB
4111 {CURL_VERSION_NTLM_WB
, "wb"},
4115 CAMLprim value
caml_curl_version_info(value unit
)
4118 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4119 const char* const* p
= NULL
;
4122 curl_version_info_data
* data
= curl_version_info(CURLVERSION_NOW
);
4123 if (NULL
== data
) caml_failwith("curl_version_info");
4125 vlist
= Val_emptylist
;
4126 for (p
= data
->protocols
; NULL
!= *p
; p
++)
4128 vlist
= Val_cons(vlist
, caml_copy_string(*p
));
4131 vfeatures
= Val_emptylist
;
4132 for (i
= 0; i
< sizeof(versionBitsMap
)/sizeof(versionBitsMap
[0]); i
++)
4134 if (0 != (versionBitsMap
[i
].code
& data
->features
))
4135 vfeatures
= Val_cons(vfeatures
, caml_copy_string(versionBitsMap
[i
].name
));
4138 vnum
= caml_alloc_tuple(3);
4139 Store_field(vnum
,0,Val_int(0xFF & (data
->version_num
>> 16)));
4140 Store_field(vnum
,1,Val_int(0xFF & (data
->version_num
>> 8)));
4141 Store_field(vnum
,2,Val_int(0xFF & (data
->version_num
)));
4143 v
= caml_alloc_tuple(12);
4144 Store_field(v
,0,caml_copy_string(data
->version
));
4145 Store_field(v
,1,vnum
);
4146 Store_field(v
,2,caml_copy_string(data
->host
));
4147 Store_field(v
,3,vfeatures
);
4148 Store_field(v
,4,data
->ssl_version
? Val_some(caml_copy_string(data
->ssl_version
)) : Val_none
);
4149 Store_field(v
,5,data
->libz_version
? Val_some(caml_copy_string(data
->libz_version
)) : Val_none
);
4150 Store_field(v
,6,vlist
);
4151 Store_field(v
,7,caml_copy_string((data
->age
>= 1 && data
->ares
) ? data
->ares
: ""));
4152 Store_field(v
,8,Val_int((data
->age
>= 1) ? data
->ares_num
: 0));
4153 Store_field(v
,9,caml_copy_string((data
->age
>= 2 && data
->libidn
) ? data
->libidn
: ""));
4154 Store_field(v
,10,Val_int((data
->age
>= 3) ? data
->iconv_ver_num
: 0));
4155 Store_field(v
,11,caml_copy_string((data
->age
>= 3 && data
->libssh_version
) ? data
->libssh_version
: ""));
4160 CAMLprim value
caml_curl_pause(value conn
, value opts
)
4162 CAMLparam2(conn
, opts
);
4163 CAMLlocal4(v
, vlist
, vnum
, vfeatures
);
4164 Connection
*connection
= Connection_val(conn
);
4168 while (Val_emptylist
!= opts
)
4170 switch (Int_val(Field(opts
,0)))
4172 case 0: bitmask
|= CURLPAUSE_SEND
; break;
4173 case 1: bitmask
|= CURLPAUSE_RECV
; break;
4174 case 2: bitmask
|= CURLPAUSE_ALL
; break;
4175 default: caml_failwith("wrong pauseOption");
4177 opts
= Field(opts
,1);
4180 result
= curl_easy_pause(connection
->connection
,bitmask
);
4181 if (result
!= CURLE_OK
)
4182 raiseError(connection
, result
);
4184 CAMLreturn(Val_unit
);
4188 * Curl multi stack support
4190 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
4191 * Other exported functions are prefixed with caml_curlm_, some of them
4192 * can/should be decomposed into smaller parts.
4195 struct ml_multi_handle
4198 value values
; /* callbacks */
4203 curlmopt_socket_function
,
4204 curlmopt_timer_function
,
4206 /* last, not used */
4210 typedef struct ml_multi_handle ml_multi_handle
;
4212 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
4213 #define CURLM_val(v) (Multi_val(v)->handle)
4215 static struct custom_operations curl_multi_ops
= {
4217 custom_finalize_default
,
4218 custom_compare_default
,
4219 custom_hash_default
,
4220 custom_serialize_default
,
4221 custom_deserialize_default
,
4222 #if defined(custom_compare_ext_default)
4223 custom_compare_ext_default
,
4227 CAMLprim value
caml_curl_multi_init(value unit
)
4231 ml_multi_handle
* multi
= (ml_multi_handle
*)caml_stat_alloc(sizeof(ml_multi_handle
));
4232 CURLM
* h
= curl_multi_init();
4236 caml_stat_free(multi
);
4237 failwith("caml_curl_multi_init");
4241 multi
->values
= caml_alloc(multi_values_total
, 0);
4242 caml_register_generational_global_root(&multi
->values
);
4244 v
= caml_alloc_custom(&curl_multi_ops
, sizeof(ml_multi_handle
*), 0, 1);
4245 Multi_val(v
) = multi
;
4250 CAMLprim value
caml_curl_multi_cleanup(value handle
)
4253 ml_multi_handle
* h
= Multi_val(handle
);
4256 CAMLreturn(Val_unit
);
4258 caml_remove_generational_global_root(&h
->values
);
4260 if (CURLM_OK
!= curl_multi_cleanup(h
->handle
))
4261 failwith("caml_curl_multi_cleanup");
4263 Multi_val(handle
) = (ml_multi_handle
*)NULL
;
4265 CAMLreturn(Val_unit
);
4268 static CURL
* curlm_remove_finished(CURLM
* multi_handle
, CURLcode
* result
)
4270 int msgs_in_queue
= 0;
4274 CURLMsg
* msg
= curl_multi_info_read(multi_handle
, &msgs_in_queue
);
4275 if (NULL
== msg
) return NULL
;
4276 if (CURLMSG_DONE
== msg
->msg
)
4278 CURL
* easy_handle
= msg
->easy_handle
;
4279 if (result
) *result
= msg
->data
.result
;
4280 if (CURLM_OK
!= curl_multi_remove_handle(multi_handle
, easy_handle
))
4282 /*failwith("curlm_remove_finished");*/
4289 CAMLprim value
caml_curlm_remove_finished(value v_multi
)
4291 CAMLparam1(v_multi
);
4292 CAMLlocal2(v_easy
, v_tuple
);
4294 CURLM
* multi_handle
;
4296 Connection
* conn
= NULL
;
4298 multi_handle
= CURLM_val(v_multi
);
4300 caml_enter_blocking_section();
4301 handle
= curlm_remove_finished(multi_handle
,&result
);
4302 caml_leave_blocking_section();
4306 CAMLreturn(Val_none
);
4310 conn
= findConnection(handle
);
4311 if (conn
->curl_ERRORBUFFER
!= NULL
)
4313 Store_field(Field(conn
->ocamlValues
, Ocaml_ERRORBUFFER
), 0, caml_copy_string(conn
->curl_ERRORBUFFER
));
4316 /* NB: same handle, but different block */
4317 v_easy
= caml_curl_alloc(conn
);
4318 v_tuple
= caml_alloc(2, 0);
4319 Store_field(v_tuple
,0,v_easy
);
4320 Store_field(v_tuple
,1,Val_int(result
)); /* CURLcode */
4321 CAMLreturn(Val_some(v_tuple
));
4325 static int curlm_wait_data(CURLM
* multi_handle
)
4327 struct timeval timeout
;
4339 /* set a suitable timeout */
4341 timeout
.tv_usec
= 0;
4343 /* get file descriptors from the transfers */
4344 ret
= curl_multi_fdset(multi_handle
, &fdread
, &fdwrite
, &fdexcep
, &maxfd
);
4346 if (ret
== CURLM_OK
&& maxfd
>= 0)
4348 int rc
= select(maxfd
+1, &fdread
, &fdwrite
, &fdexcep
, &timeout
);
4349 if (-1 != rc
) return 0;
4354 CAMLprim value
caml_curlm_wait_data(value v_multi
)
4356 CAMLparam1(v_multi
);
4358 CURLM
* h
= CURLM_val(v_multi
);
4360 caml_enter_blocking_section();
4361 ret
= curlm_wait_data(h
);
4362 caml_leave_blocking_section();
4364 CAMLreturn(Val_bool(0 == ret
));
4367 CAMLprim value
caml_curl_multi_add_handle(value v_multi
, value v_easy
)
4369 CAMLparam2(v_multi
,v_easy
);
4370 CURLM
* multi
= CURLM_val(v_multi
);
4371 Connection
* conn
= Connection_val(v_easy
);
4373 /* prevent collection of OCaml value while the easy handle is used
4374 and may invoke callbacks registered on OCaml side */
4377 /* may invoke callbacks so need to be consistent with locks */
4378 caml_enter_blocking_section();
4379 if (CURLM_OK
!= curl_multi_add_handle(multi
, conn
->connection
))
4381 conn
->refcount
--; /* not added, revert */
4382 caml_leave_blocking_section();
4383 failwith("caml_curl_multi_add_handle");
4385 caml_leave_blocking_section();
4387 CAMLreturn(Val_unit
);
4390 CAMLprim value
caml_curl_multi_remove_handle(value v_multi
, value v_easy
)
4392 CAMLparam2(v_multi
,v_easy
);
4393 CURLM
* multi
= CURLM_val(v_multi
);
4394 Connection
* conn
= Connection_val(v_easy
);
4396 /* may invoke callbacks so need to be consistent with locks */
4397 caml_enter_blocking_section();
4398 if (CURLM_OK
!= curl_multi_remove_handle(multi
, conn
->connection
))
4400 caml_leave_blocking_section();
4401 failwith("caml_curl_multi_remove_handle");
4404 caml_leave_blocking_section();
4406 CAMLreturn(Val_unit
);
4409 CAMLprim value
caml_curl_multi_perform_all(value v_multi
)
4411 CAMLparam1(v_multi
);
4412 int still_running
= 0;
4413 CURLM
* h
= CURLM_val(v_multi
);
4415 caml_enter_blocking_section();
4416 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(h
, &still_running
));
4417 caml_leave_blocking_section();
4419 CAMLreturn(Val_int(still_running
));
4422 CAMLprim value
helper_curl_easy_strerror(value v_code
)
4425 CAMLreturn(caml_copy_string(curl_easy_strerror(Int_val(v_code
))));
4429 * Wrappers for the curl_multi_socket_action infrastructure
4430 * Based on curl hiperfifo.c example
4435 #define Val_socket(v) win_alloc_socket(v)
4438 #error Socket_val not defined in unixsupport.h
4442 #define Socket_val(v) Long_val(v)
4445 #define Val_socket(v) Val_int(v)
4449 static void raise_multi_error(char const* msg
)
4451 static value
* exception
= NULL
;
4453 if (NULL
== exception
)
4455 exception
= caml_named_value("Curl.Multi.Error");
4456 if (NULL
== exception
) caml_invalid_argument("Curl.Multi.Error");
4459 caml_raise_with_string(*exception
, msg
);
4462 static void check_mcode(CURLMcode code
)
4464 char const *s
= NULL
;
4467 case CURLM_OK
: return;
4468 case CURLM_CALL_MULTI_PERFORM
: s
="CURLM_CALL_MULTI_PERFORM"; break;
4469 case CURLM_BAD_HANDLE
: s
="CURLM_BAD_HANDLE"; break;
4470 case CURLM_BAD_EASY_HANDLE
: s
="CURLM_BAD_EASY_HANDLE"; break;
4471 case CURLM_OUT_OF_MEMORY
: s
="CURLM_OUT_OF_MEMORY"; break;
4472 case CURLM_INTERNAL_ERROR
: s
="CURLM_INTERNAL_ERROR"; break;
4473 case CURLM_UNKNOWN_OPTION
: s
="CURLM_UNKNOWN_OPTION"; break;
4474 case CURLM_LAST
: s
="CURLM_LAST"; break;
4475 case CURLM_BAD_SOCKET
: s
="CURLM_BAD_SOCKET"; break;
4476 default : s
="CURLM_unknown"; break;
4478 raise_multi_error(s
);
4481 CAMLprim value
caml_curl_multi_socket_action(value v_multi
, value v_fd
, value v_kind
)
4483 CAMLparam3(v_multi
, v_fd
, v_kind
);
4484 CURLM
* h
= CURLM_val(v_multi
);
4485 int still_running
= 0;
4486 CURLMcode rc
= CURLM_OK
;
4487 curl_socket_t socket
;
4490 if (Val_none
== v_fd
)
4492 socket
= CURL_SOCKET_TIMEOUT
;
4496 socket
= Socket_val(Field(v_fd
, 0));
4499 switch (Int_val(v_kind
))
4502 case 1 : kind
|= CURL_CSELECT_IN
; break;
4503 case 2 : kind
|= CURL_CSELECT_OUT
; break;
4504 case 3 : kind
|= CURL_CSELECT_IN
| CURL_CSELECT_OUT
; break;
4506 raise_multi_error("caml_curl_multi_socket_action");
4509 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4511 caml_enter_blocking_section();
4513 rc
= curl_multi_socket_action(h
, socket
, kind
, &still_running
);
4514 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4515 caml_leave_blocking_section();
4519 CAMLreturn(Val_int(still_running
));
4522 CAMLprim value
caml_curl_multi_socket_all(value v_multi
)
4524 CAMLparam1(v_multi
);
4525 int still_running
= 0;
4526 CURLMcode rc
= CURLM_OK
;
4527 CURLM
* h
= CURLM_val(v_multi
);
4529 caml_enter_blocking_section();
4531 rc
= curl_multi_socket_all(h
, &still_running
);
4532 } while (rc
== CURLM_CALL_MULTI_PERFORM
);
4533 caml_leave_blocking_section();
4537 CAMLreturn(Val_int(still_running
));
4540 static int curlm_sock_cb_nolock(CURL
*e
, curl_socket_t sock
, int what
, ml_multi_handle
* multi
, void *sockp
)
4543 CAMLlocal2(v_what
,csock
);
4545 (void)sockp
; /* not used */
4547 /* v_what = Val_int(what); */
4550 case CURL_POLL_NONE
: v_what
= Val_int(0); break;
4551 case CURL_POLL_IN
: v_what
= Val_int(1); break;
4552 case CURL_POLL_OUT
: v_what
= Val_int(2); break;
4553 case CURL_POLL_INOUT
: v_what
= Val_int(3); break;
4554 case CURL_POLL_REMOVE
: v_what
= Val_int(4); break;
4556 fprintf(stderr
, "curlm_sock_cb sock=%d what=%d\n", sock
, what
);
4558 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
4560 csock
=Val_socket(sock
);
4561 caml_callback2(Field(multi
->values
,curlmopt_socket_function
),
4567 static int curlm_sock_cb(CURL
*e
, curl_socket_t sock
, int what
, void *cbp
, void *sockp
)
4570 caml_leave_blocking_section();
4571 ret
= curlm_sock_cb_nolock(e
, sock
, what
, (ml_multi_handle
*)cbp
, sockp
);
4572 caml_enter_blocking_section();
4576 CAMLprim value
caml_curl_multi_socketfunction(value v_multi
, value v_cb
)
4578 CAMLparam2(v_multi
, v_cb
);
4579 ml_multi_handle
* multi
= Multi_val(v_multi
);
4581 Store_field(multi
->values
, curlmopt_socket_function
, v_cb
);
4583 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETFUNCTION
, curlm_sock_cb
);
4584 curl_multi_setopt(multi
->handle
, CURLMOPT_SOCKETDATA
, multi
);
4586 CAMLreturn(Val_unit
);
4589 static void curlm_timer_cb_nolock(ml_multi_handle
*multi
, long timeout_ms
)
4592 caml_callback(Field(multi
->values
,curlmopt_timer_function
), Val_long(timeout_ms
));
4596 static int curlm_timer_cb(CURLM
*multi
, long timeout_ms
, void *userp
)
4600 caml_leave_blocking_section();
4601 curlm_timer_cb_nolock((ml_multi_handle
*)userp
, timeout_ms
);
4602 caml_enter_blocking_section();
4606 CAMLprim value
caml_curl_multi_timerfunction(value v_multi
, value v_cb
)
4608 CAMLparam2(v_multi
, v_cb
);
4609 ml_multi_handle
* multi
= Multi_val(v_multi
);
4611 Store_field(multi
->values
, curlmopt_timer_function
, v_cb
);
4613 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERFUNCTION
, curlm_timer_cb
);
4614 curl_multi_setopt(multi
->handle
, CURLMOPT_TIMERDATA
, multi
);
4616 CAMLreturn(Val_unit
);
4619 CAMLprim value
caml_curl_multi_timeout(value v_multi
)
4621 CAMLparam1(v_multi
);
4623 CURLMcode rc
= CURLM_OK
;
4624 ml_multi_handle
* multi
= Multi_val(v_multi
);
4626 rc
= curl_multi_timeout(multi
->handle
, &ms
);
4630 CAMLreturn(Val_long(ms
));