release 0.7.5
[ocurl.git] / curl-helper.c
bloba968800aca9a3bafeacc893ee77690a82b95290b
1 /***
2 *** curl-helper.c
3 ***
4 *** Copyright (c) 2003-2008, Lars Nilsson, <lars@quantumchamaeleon.com>
5 *** Copyright (c) 2009, ygrek, <ygrek@autistici.org>
6 ***/
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <unistd.h>
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>
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #else
28 #pragma message("No config file given.")
29 #endif
31 void leave_blocking_section(void);
32 void enter_blocking_section(void);
34 #define Val_none Val_int(0)
36 static __inline value
37 Val_some( value v )
39 CAMLparam1( v );
40 CAMLlocal1( some );
41 some = caml_alloc(1, 0);
42 Store_field( some, 0, v );
43 CAMLreturn( some );
46 static value Val_pair(value v1, value v2)
48 CAMLparam2(v1,v2);
49 CAMLlocal1(pair);
50 pair = caml_alloc_small(2,0);
51 Field(pair,0) = v1;
52 Field(pair,1) = v2;
53 CAMLreturn(pair);
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
65 Ocaml_WRITEFUNCTION,
66 Ocaml_READFUNCTION,
67 Ocaml_ERRORBUFFER,
68 Ocaml_POSTFIELDS,
69 Ocaml_HTTPHEADER,
70 Ocaml_HTTPPOST,
71 Ocaml_QUOTE,
72 Ocaml_POSTQUOTE,
73 Ocaml_HEADERFUNCTION,
74 Ocaml_PROGRESSFUNCTION,
75 Ocaml_DEBUGFUNCTION,
76 Ocaml_HTTP200ALIASES,
77 Ocaml_IOCTLFUNCTION,
78 Ocaml_SEEKFUNCTION,
79 Ocaml_OPENSOCKETFUNCTION,
81 Ocaml_URL,
82 Ocaml_PROXY,
83 Ocaml_USERPWD,
84 Ocaml_PROXYUSERPWD,
85 Ocaml_RANGE,
86 Ocaml_REFERER,
87 Ocaml_USERAGENT,
88 Ocaml_FTPPORT,
89 Ocaml_COOKIE,
90 Ocaml_HTTPPOSTSTRINGS,
91 Ocaml_SSLCERT,
92 Ocaml_SSLCERTTYPE,
93 Ocaml_SSLCERTPASSWD,
94 Ocaml_SSLKEY,
95 Ocaml_SSLKEYTYPE,
96 Ocaml_SSLKEYPASSWD,
97 Ocaml_SSLENGINE,
98 Ocaml_COOKIEFILE,
99 Ocaml_CUSTOMREQUEST,
100 Ocaml_INTERFACE,
101 Ocaml_CAINFO,
102 Ocaml_CAPATH,
103 Ocaml_RANDOM_FILE,
104 Ocaml_EGDSOCKET,
105 Ocaml_COOKIEJAR,
106 Ocaml_SSL_CIPHER_LIST,
107 Ocaml_PRIVATE,
108 Ocaml_NETRC_FILE,
109 Ocaml_FTP_ACCOUNT,
110 Ocaml_COOKIELIST,
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,
117 Ocaml_DNS_SERVERS,
119 Ocaml_MAIL_FROM,
120 Ocaml_MAIL_RCPT,
122 Ocaml_RESOLVE,
124 /* Not used, last for size */
125 OcamlValuesSize
126 } OcamlValue;
128 struct Connection
130 CURL *connection;
131 Connection *next;
132 Connection *prev;
134 value ocamlValues;
136 size_t refcount; /* number of references to this structure */
138 char *curl_URL;
139 char *curl_PROXY;
140 char *curl_USERPWD;
141 char *curl_PROXYUSERPWD;
142 char *curl_RANGE;
143 char *curl_ERRORBUFFER;
144 char *curl_POSTFIELDS;
145 int curl_POSTFIELDSIZE;
146 char *curl_REFERER;
147 char *curl_USERAGENT;
148 char *curl_FTPPORT;
149 char *curl_COOKIE;
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;
155 char *curl_SSLCERT;
156 char *curl_SSLCERTTYPE;
157 char *curl_SSLCERTPASSWD;
158 char *curl_SSLKEY;
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;
167 char *curl_CAINFO;
168 char *curl_CAPATH;
169 char *curl_RANDOM_FILE;
170 char *curl_EGDSOCKET;
171 char *curl_COOKIEJAR;
172 char *curl_SSL_CIPHER_LIST;
173 char *curl_PRIVATE;
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
190 Connection *head;
191 Connection *tail;
194 static ConnectionList connectionList = {NULL, NULL};
196 typedef struct CURLErrorMapping CURLErrorMapping;
198 struct CURLErrorMapping
200 char *name;
201 CURLcode error;
204 CURLErrorMapping errorMap[] =
206 #if HAVE_DECL_CURLE_UNSUPPORTED_PROTOCOL
207 {"CURLE_UNSUPPORTED_PROTOCOL", CURLE_UNSUPPORTED_PROTOCOL},
208 #else
209 {"CURLE_UNSUPPORTED_PROTOCOL", -1},
210 #endif
211 #if HAVE_DECL_CURLE_FAILED_INIT
212 {"CURLE_FAILED_INIT", CURLE_FAILED_INIT},
213 #else
214 {"CURLE_FAILED_INIT", -1},
215 #endif
216 #if HAVE_DECL_CURLE_URL_MALFORMAT
217 {"CURLE_URL_MALFORMAT", CURLE_URL_MALFORMAT},
218 #else
219 {"CURLE_URL_MALFORMAT", -1},
220 #endif
221 #if HAVE_DECL_CURLE_URL_MALFORMAT_USER
222 {"CURLE_URL_MALFORMAT_USER", CURLE_URL_MALFORMAT_USER},
223 #else
224 {"CURLE_URL_MALFORMAT_USER", -1},
225 #endif
226 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_PROXY
227 {"CURLE_COULDNT_RESOLVE_PROXY", CURLE_COULDNT_RESOLVE_PROXY},
228 #else
229 {"CURLE_COULDNT_RESOLVE_PROXY", -1},
230 #endif
231 #if HAVE_DECL_CURLE_COULDNT_RESOLVE_HOST
232 {"CURLE_COULDNT_RESOLVE_HOST", CURLE_COULDNT_RESOLVE_HOST},
233 #else
234 {"CURLE_COULDNT_RESOLVE_HOST", -1},
235 #endif
236 #if HAVE_DECL_CURLE_COULDNT_CONNECT
237 {"CURLE_COULDNT_CONNECT", CURLE_COULDNT_CONNECT},
238 #else
239 {"CURLE_COULDNT_CONNECT", -1},
240 #endif
241 #if HAVE_DECL_CURLE_FTP_WEIRD_SERVER_REPLY
242 {"CURLE_FTP_WEIRD_SERVER_REPLY", CURLE_FTP_WEIRD_SERVER_REPLY},
243 #else
244 {"CURLE_FTP_WEIRD_SERVER_REPLY", -1},
245 #endif
246 #if HAVE_DECL_CURLE_FTP_ACCESS_DENIED
247 {"CURLE_FTP_ACCESS_DENIED", CURLE_FTP_ACCESS_DENIED},
248 #else
249 {"CURLE_FTP_ACCESS_DENIED", -1},
250 #endif
251 #if HAVE_DECL_CURLE_FTP_USER_PASSWORD_INCORRECT
252 {"CURLE_FTP_USER_PASSWORD_INCORRECT", CURLE_FTP_USER_PASSWORD_INCORRECT},
253 #else
254 {"CURLE_FTP_USER_PASSWORD_INCORRECT", -1},
255 #endif
256 #if HAVE_DECL_CURLE_FTP_WEIRD_PASS_REPLY
257 {"CURLE_FTP_WEIRD_PASS_REPLY", CURLE_FTP_WEIRD_PASS_REPLY},
258 #else
259 {"CURLE_FTP_WEIRD_PASS_REPLY", -1},
260 #endif
261 #if HAVE_DECL_CURLE_FTP_WEIRD_USER_REPLY
262 {"CURLE_FTP_WEIRD_USER_REPLY", CURLE_FTP_WEIRD_USER_REPLY},
263 #else
264 {"CURLE_FTP_WEIRD_USER_REPLY", -1},
265 #endif
266 #if HAVE_DECL_CURLE_FTP_WEIRD_PASV_REPLY
267 {"CURLE_FTP_WEIRD_PASV_REPLY", CURLE_FTP_WEIRD_PASV_REPLY},
268 #else
269 {"CURLE_FTP_WEIRD_PASV_REPLY", -1},
270 #endif
271 #if HAVE_DECL_CURLE_FTP_WEIRD_227_FORMAT
272 {"CURLE_FTP_WEIRD_227_FORMAT", CURLE_FTP_WEIRD_227_FORMAT},
273 #else
274 {"CURLE_FTP_WEIRD_227_FORMAT", -1},
275 #endif
276 #if HAVE_DECL_CURLE_FTP_CANT_GET_HOST
277 {"CURLE_FTP_CANT_GET_HOST", CURLE_FTP_CANT_GET_HOST},
278 #else
279 {"CURLE_FTP_CANT_GET_HOST", -1},
280 #endif
281 #if HAVE_DECL_CURLE_FTP_CANT_RECONNECT
282 {"CURLE_FTP_CANT_RECONNECT", CURLE_FTP_CANT_RECONNECT},
283 #else
284 {"CURLE_FTP_CANT_RECONNECT", -1},
285 #endif
286 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_BINARY
287 {"CURLE_FTP_COULDNT_SET_BINARY", CURLE_FTP_COULDNT_SET_BINARY},
288 #else
289 {"CURLE_FTP_COULDNT_SET_BINARY", -1},
290 #endif
291 #if HAVE_DECL_CURLE_PARTIAL_FILE
292 {"CURLE_PARTIAL_FILE", CURLE_PARTIAL_FILE},
293 #else
294 {"CURLE_PARTIAL_FILE", -1},
295 #endif
296 #if HAVE_DECL_CURLE_FTP_COULDNT_RETR_FILE
297 {"CURLE_FTP_COULDNT_RETR_FILE", CURLE_FTP_COULDNT_RETR_FILE},
298 #else
299 {"CURLE_FTP_COULDNT_RETR_FILE", -1},
300 #endif
301 #if HAVE_DECL_CURLE_FTP_WRITE_ERROR
302 {"CURLE_FTP_WRITE_ERROR", CURLE_FTP_WRITE_ERROR},
303 #else
304 {"CURLE_FTP_WRITE_ERROR", -1},
305 #endif
306 #if HAVE_DECL_CURLE_FTP_QUOTE_ERROR
307 {"CURLE_FTP_QUOTE_ERROR", CURLE_FTP_QUOTE_ERROR},
308 #else
309 {"CURLE_FTP_QUOTE_ERROR", -1},
310 #endif
311 #if HAVE_DECL_CURLE_HTTP_NOT_FOUND
312 {"CURLE_HTTP_NOT_FOUND", CURLE_HTTP_NOT_FOUND},
313 #else
314 {"CURLE_HTTP_NOT_FOUND", -1},
315 #endif
316 #if HAVE_DECL_CURLE_WRITE_ERROR
317 {"CURLE_WRITE_ERROR", CURLE_WRITE_ERROR},
318 #else
319 {"CURLE_WRITE_ERROR", -1},
320 #endif
321 #if HAVE_DECL_CURLE_MALFORMAT_USER
322 {"CURLE_MALFORMAT_USER", CURLE_MALFORMAT_USER},
323 #else
324 {"CURLE_MALFORMAT_USER", -1},
325 #endif
326 #if HAVE_DECL_CURLE_FTP_COULDNT_STOR_FILE
327 {"CURLE_FTP_COULDNT_STOR_FILE", CURLE_FTP_COULDNT_STOR_FILE},
328 #else
329 {"CURLE_FTP_COULDNT_STOR_FILE", -1},
330 #endif
331 #if HAVE_DECL_CURLE_READ_ERROR
332 {"CURLE_READ_ERROR", CURLE_READ_ERROR},
333 #else
334 {"CURLE_READ_ERROR", -1},
335 #endif
336 #if HAVE_DECL_CURLE_OUT_OF_MEMORY
337 {"CURLE_OUT_OF_MEMORY", CURLE_OUT_OF_MEMORY},
338 #else
339 {"CURLE_OUT_OF_MEMORY", -1},
340 #endif
341 #if HAVE_DECL_CURLE_OPERATION_TIMEOUTED
342 {"CURLE_OPERATION_TIMEOUTED", CURLE_OPERATION_TIMEOUTED},
343 #else
344 {"CURLE_OPERATION_TIMEOUTED", -1},
345 #endif
346 #if HAVE_DECL_CURLE_FTP_COULDNT_SET_ASCII
347 {"CURLE_FTP_COULDNT_SET_ASCII", CURLE_FTP_COULDNT_SET_ASCII},
348 #else
349 {"CURLE_FTP_COULDNT_SET_ASCII", -1},
350 #endif
351 #if HAVE_DECL_CURLE_FTP_PORT_FAILED
352 {"CURLE_FTP_PORT_FAILED", CURLE_FTP_PORT_FAILED},
353 #else
354 {"CURLE_FTP_PORT_FAILED", -1},
355 #endif
356 #if HAVE_DECL_CURLE_FTP_COULDNT_USE_REST
357 {"CURLE_FTP_COULDNT_USE_REST", CURLE_FTP_COULDNT_USE_REST},
358 #else
359 {"CURLE_FTP_COULDNT_USE_REST", -1},
360 #endif
361 #if HAVE_DECL_CURLE_FTP_COULDNT_GET_SIZE
362 {"CURLE_FTP_COULDNT_GET_SIZE", CURLE_FTP_COULDNT_GET_SIZE},
363 #else
364 {"CURLE_FTP_COULDNT_GET_SIZE", -1},
365 #endif
366 #if HAVE_DECL_CURLE_HTTP_RANGE_ERROR
367 {"CURLE_HTTP_RANGE_ERROR", CURLE_HTTP_RANGE_ERROR},
368 #else
369 {"CURLE_HTTP_RANGE_ERROR", -1},
370 #endif
371 #if HAVE_DECL_CURLE_HTTP_POST_ERROR
372 {"CURLE_HTTP_POST_ERROR", CURLE_HTTP_POST_ERROR},
373 #else
374 {"CURLE_HTTP_POST_ERROR", -1},
375 #endif
376 #if HAVE_DECL_CURLE_SSL_CONNECT_ERROR
377 {"CURLE_SSL_CONNECT_ERROR", CURLE_SSL_CONNECT_ERROR},
378 #else
379 {"CURLE_SSL_CONNECT_ERROR", -1},
380 #endif
381 #if HAVE_DECL_CURLE_FTP_BAD_DOWNLOAD_RESUME
382 {"CURLE_FTP_BAD_DOWNLOAD_RESUME", CURLE_FTP_BAD_DOWNLOAD_RESUME},
383 #else
384 {"CURLE_FTP_BAD_DOWNLOAD_RESUME", -1},
385 #endif
386 #if HAVE_DECL_CURLE_FILE_COULDNT_READ_FILE
387 {"CURLE_FILE_COULDNT_READ_FILE", CURLE_FILE_COULDNT_READ_FILE},
388 #else
389 {"CURLE_FILE_COULDNT_READ_FILE", -1},
390 #endif
391 #if HAVE_DECL_CURLE_LDAP_CANNOT_BIND
392 {"CURLE_LDAP_CANNOT_BIND", CURLE_LDAP_CANNOT_BIND},
393 #else
394 {"CURLE_LDAP_CANNOT_BIND", -1},
395 #endif
396 #if HAVE_DECL_CURLE_LDAP_SEARCH_FAILED
397 {"CURLE_LDAP_SEARCH_FAILED", CURLE_LDAP_SEARCH_FAILED},
398 #else
399 {"CURLE_LDAP_SEARCH_FAILED", -1},
400 #endif
401 #if HAVE_DECL_CURLE_LIBRARY_NOT_FOUND
402 {"CURLE_LIBRARY_NOT_FOUND", CURLE_LIBRARY_NOT_FOUND},
403 #else
404 {"CURLE_LIBRARY_NOT_FOUND", -1},
405 #endif
406 #if HAVE_DECL_CURLE_FUNCTION_NOT_FOUND
407 {"CURLE_FUNCTION_NOT_FOUND", CURLE_FUNCTION_NOT_FOUND},
408 #else
409 {"CURLE_FUNCTION_NOT_FOUND", -1},
410 #endif
411 #if HAVE_DECL_CURLE_ABORTED_BY_CALLBACK
412 {"CURLE_ABORTED_BY_CALLBACK", CURLE_ABORTED_BY_CALLBACK},
413 #else
414 {"CURLE_ABORTED_BY_CALLBACK", -1},
415 #endif
416 #if HAVE_DECL_CURLE_BAD_FUNCTION_ARGUMENT
417 {"CURLE_BAD_FUNCTION_ARGUMENT", CURLE_BAD_FUNCTION_ARGUMENT},
418 #else
419 {"CURLE_BAD_FUNCTION_ARGUMENT", -1},
420 #endif
421 #if HAVE_DECL_CURLE_BAD_CALLING_ORDER
422 {"CURLE_BAD_CALLING_ORDER", CURLE_BAD_CALLING_ORDER},
423 #else
424 {"CURLE_BAD_CALLING_ORDER", -1},
425 #endif
426 #if HAVE_DECL_CURLE_HTTP_PORT_FAILED
427 {"CURLE_HTTP_PORT_FAILED", CURLE_HTTP_PORT_FAILED},
428 #else
429 {"CURLE_HTTP_PORT_FAILED", -1},
430 #endif
431 #if HAVE_DECL_CURLE_BAD_PASSWORD_ENTERED
432 {"CURLE_BAD_PASSWORD_ENTERED", CURLE_BAD_PASSWORD_ENTERED},
433 #else
434 {"CURLE_BAD_PASSWORD_ENTERED", -1},
435 #endif
436 #if HAVE_DECL_CURLE_TOO_MANY_REDIRECTS
437 {"CURLE_TOO_MANY_REDIRECTS", CURLE_TOO_MANY_REDIRECTS},
438 #else
439 {"CURLE_TOO_MANY_REDIRECTS", -1},
440 #endif
441 #if HAVE_DECL_CURLE_UNKNOWN_TELNET_OPTION
442 {"CURLE_UNKNOWN_TELNET_OPTION", CURLE_UNKNOWN_TELNET_OPTION},
443 #else
444 {"CURLE_UNKNOWN_TELNET_OPTION", -1},
445 #endif
446 #if HAVE_DECL_CURLE_TELNET_OPTION_SYNTAX
447 {"CURLE_TELNET_OPTION_SYNTAX", CURLE_TELNET_OPTION_SYNTAX},
448 #else
449 {"CURLE_TELNET_OPTION_SYNTAX", -1},
450 #endif
451 #if HAVE_DECL_CURLE_SSL_PEER_CERTIFICATE
452 {"CURLE_SSL_PEER_CERTIFICATE", CURLE_SSL_PEER_CERTIFICATE},
453 #else
454 {"CURLE_SSL_PEER_CERTIFICATE", -1},
455 #endif
456 #if HAVE_DECL_CURLE_GOT_NOTHING
457 {"CURLE_GOT_NOTHING", CURLE_GOT_NOTHING},
458 #else
459 {"CURLE_GOT_NOTHING", -1},
460 #endif
461 #if HAVE_DECL_CURLE_SSL_ENGINE_NOT_FOUND
462 {"CURLE_SSL_ENGINE_NOT_FOUND", CURLE_SSL_ENGINE_NOTFOUND},
463 #else
464 {"CURLE_SSL_ENGINE_NOT_FOUND", -1},
465 #endif
466 #if HAVE_DECL_CURLE_SSL_ENGINE_SET_FAILED
467 {"CURLE_SSL_ENGINE_SET_FAILED", CURLE_SSL_ENGINE_SETFAILED},
468 #else
469 {"CURLE_SSL_ENGINE_SET_FAILED", -1},
470 #endif
471 #if HAVE_DECL_CURLE_SEND_ERROR
472 {"CURLE_SEND_ERROR", CURLE_SEND_ERROR},
473 #else
474 {"CURLE_SEND_ERROR", -1},
475 #endif
476 #if HAVE_DECL_CURLE_RECV_ERROR
477 {"CURLE_RECV_ERROR", CURLE_RECV_ERROR},
478 #else
479 {"CURLE_RECV_ERROR", -1},
480 #endif
481 #if HAVE_DECL_CURLE_SHARE_IN_USE
482 {"CURLE_SHARE_IN_USE", CURLE_SHARE_IN_USE},
483 #else
484 {"CURLE_SHARE_IN_USE", -1},
485 #endif
486 #if HAVE_DECL_CURLE_SSL_CERTPROBLEM
487 {"CURLE_SSL_CERTPROBLEN", CURLE_SSL_CERTPROBLEM},
488 #else
489 {"CURLE_SSL_CERTPROBLEN", -1},
490 #endif
491 #if HAVE_DECL_CURLE_SSL_CIPHER
492 {"CURLE_SSL_CIPHER", CURLE_SSL_CIPHER},
493 #else
494 {"CURLE_SSL_CIPHER", -1},
495 #endif
496 #if HAVE_DECL_CURLE_SSL_CACERT
497 {"CURLE_SSL_CACERT", CURLE_SSL_CACERT},
498 #else
499 {"CURLE_SSL_CACERT", -1},
500 #endif
501 #if HAVE_DECL_CURLE_BAD_CONTENT_ENCODING
502 {"CURLE_BAD_CONTENT_ENCODING", CURLE_BAD_CONTENT_ENCODING},
503 #else
504 {"CURLE_BAD_CONTENT_ENCODING", -1},
505 #endif
506 #if HAVE_DECL_CURLE_LDAP_INVALID_URL
507 {"CURLE_LDAP_INVALID_URL", CURLE_LDAP_INVALID_URL},
508 #else
509 {"CURLE_LDAP_INVALID_URL", -1},
510 #endif
511 #if HAVE_DECL_CURLE_FILESIZE_EXCEEDED
512 {"CURLE_FILESIZE_EXCEEDED", CURLE_FILESIZE_EXCEEDED},
513 #else
514 {"CURLE_FILESIZE_EXCEEDED", -1},
515 #endif
516 #if HAVE_DECL_CURLE_FTP_SSL_FAILED
517 {"CURLE_FTP_SSL_FAILED", CURLE_FTP_SSL_FAILED},
518 #else
519 {"CURLE_FTP_SSL_FAILED", -1},
520 #endif
521 #if HAVE_DECL_CURLE_SEND_FAIL_REWIND
522 {"CURLE_SEND_FAIL_REWIND", CURLE_SEND_FAIL_REWIND},
523 #else
524 {"CURLE_SEND_FAIL_REWIND", -1},
525 #endif
526 #if HAVE_DECL_CURLE_SSL_ENGINE_INITFAILED
527 {"CURLE_SSL_ENGINE_INITFAILED", CURLE_SSL_ENGINE_INITFAILED},
528 #else
529 {"CURLE_SSL_ENGINE_INITFAILED", -1},
530 #endif
531 #if HAVE_DECL_CURLE_LOGIN_DENIED
532 {"CURLE_LOGIN_DENIED", CURLE_LOGIN_DENIED},
533 #else
534 {"CURLE_LOGIN_DENIED", -1},
535 #endif
536 #if HAVE_DECL_CURLE_TFTP_NOTFOUND
537 {"CURLE_TFTP_NOTFOUND", CURLE_TFTP_NOTFOUND},
538 #else
539 {"CURLE_TFTP_NOTFOUND", -1},
540 #endif
541 #if HAVE_DECL_CURLE_TFTP_PERM
542 {"CURLE_TFTP_PERM", CURLE_TFTP_PERM},
543 #else
544 {"CURLE_TFTP_PERM", -1},
545 #endif
546 #if HAVE_DECL_CURLE_REMOTE_DISK_FULL
547 {"CURLE_REMOTE_DISK_FULL", CURLE_REMOTE_DISK_FULL},
548 #else
549 {"CURLE_REMOTE_DISK_FULL", -1},
550 #endif
551 #if HAVE_DECL_CURLE_TFTP_ILLEGAL
552 {"CURLE_TFTP_ILLEGAL", CURLE_TFTP_ILLEGAL},
553 #else
554 {"CURLE_TFTP_ILLEGAL", -1},
555 #endif
556 #if HAVE_DECL_CURLE_TFTP_UNKNOWNID
557 {"CURLE_TFTP_UNKNOWNID", CURLE_TFTP_UNKNOWNID},
558 #else
559 {"CURLE_TFTP_UNKNOWNID", -1},
560 #endif
561 #if HAVE_DECL_CURLE_REMOTE_FILE_EXISTS
562 {"CURLE_REMOTE_FILE_EXISTS", CURLE_REMOTE_FILE_EXISTS},
563 #else
564 {"CURLE_REMOTE_FILE_EXISTS", -1},
565 #endif
566 #if HAVE_DECL_CURLE_TFTP_NOSUCHUSER
567 {"CURLE_TFTP_NOSUCHUSER", CURLE_TFTP_NOSUCHUSER},
568 #else
569 {"CURLE_TFTP_NOSUCHUSER", -1},
570 #endif
571 #if HAVE_DECL_CURLE_CONV_FAILED
572 {"CURLE_CONV_FAILED", CURLE_CONV_FAILED},
573 #else
574 {"CURLE_CONV_FAILED", -1},
575 #endif
576 #if HAVE_DECL_CURLE_CONV_REQUIRED
577 {"CURLE_CONV_REQUIRED", CURLE_CONV_REQUIRED},
578 #else
579 {"CURLE_CONV_REQUIRED", -1},
580 #endif
581 #if HAVE_DECL_CURLE_SSL_CACERT_BADFILE
582 {"CURLE_SSL_CACERT_BADFILE", CURLE_SSL_CACERT_BADFILE},
583 #else
584 {"CURLE_SSL_CACERT_BADFILE", -1},
585 #endif
586 #if HAVE_DECL_CURLE_REMOTE_FILE_NOT_FOUND
587 {"CURLE_REMOTE_FILE_NOT_FOUND", CURLE_REMOTE_FILE_NOT_FOUND},
588 #else
589 {"CURLE_REMOTE_FILE_NOT_FOUND", -1},
590 #endif
591 #if HAVE_DECL_CURLE_SSH
592 {"CURLE_SSH", CURLE_SSH},
593 #else
594 {"CURLE_SSH", -1},
595 #endif
596 #if HAVE_DECL_CURLE_SSL_SHUTDOWN_FAILED
597 {"CURLE_SSL_SHUTDOWN_FAILED", CURLE_SSL_SHUTDOWN_FAILED},
598 #else
599 {"CURLE_SSL_SHUTDOWN_FAILED", -1},
600 #endif
601 #if HAVE_DECL_CURLE_AGAIN
602 {"CURLE_AGAIN", CURLE_AGAIN},
603 #else
604 {"CURLE_AGAIN", -1},
605 #endif
606 {"CURLE_OK", CURLE_OK},
607 {NULL, 0}
610 typedef struct CURLOptionMapping CURLOptionMapping;
612 struct CURLOptionMapping
614 void (*optionHandler)(Connection *, value);
615 char *name;
616 OcamlValue ocamlValue;
619 static char* strdup_ml(value v)
621 char* p = NULL;
622 p = malloc(caml_string_length(v)+1);
623 memcpy(p,String_val(v),caml_string_length(v)+1); // caml strings have terminating zero
624 return p;
627 static void free_curl_slist(struct curl_slist *slist)
629 if (NULL == slist)
630 return;
632 curl_slist_free_all(slist);
635 static void raiseError(Connection *conn, CURLcode code)
637 CAMLparam0();
638 CAMLlocal1(exceptionData);
639 value *exception;
640 char *errorString = "Unknown Error";
641 int i;
643 for (i = 0; errorMap[i].name != NULL; i++)
645 if (errorMap[i].error == code)
647 errorString = errorMap[i].name;
648 break;
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);
671 CAMLreturn0;
674 static void resetOcamlValues(Connection* connection)
676 int i;
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;
700 else
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;
757 return connection;
760 static Connection *newConnection(void)
762 CURL* h;
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 */
782 if (finalization)
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)
787 fin_url = "unknown";
789 fprintf(stderr,"Curl: handle %p leaked, conn %p, url %s\n", connection->connection, connection, fin_url);
790 fflush(stderr);
792 else
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);
861 #if 1
862 static void checkConnection(Connection * connection)
864 (void)connection;
866 #else
867 static void checkConnection(Connection *connection)
869 Connection *listIter;
871 listIter = connectionList.tail;
873 while (listIter != NULL)
875 if (listIter == connection)
876 return;
878 listIter = listIter->next;
881 failwith("Invalid Connection");
883 #endif
885 static Connection* findConnection(CURL* h)
887 Connection *listIter;
889 listIter = connectionList.tail;
891 while (listIter != NULL)
893 if (listIter->connection == h)
894 return listIter;
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 */
907 conn->refcount--;
908 if (0 == conn->refcount)
910 removeConnection(conn, 1);
911 free(conn);
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 = {
928 "ygrek.curl_easy",
929 op_curl_easy_finalize,
930 op_curl_easy_compare,
931 op_curl_easy_hash,
932 custom_serialize_default,
933 custom_deserialize_default,
934 #if defined(custom_compare_ext_default)
935 custom_compare_ext_default,
936 #endif
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;
943 conn->refcount++;
944 return v;
947 #define WRAP_DATA_CALLBACK(name) \
948 static size_t cb_##name(char *ptr, size_t size, size_t nmemb, void *data)\
950 size_t result;\
951 leave_blocking_section();\
952 result = cb_##name##_nolock(ptr,size,nmemb,data);\
953 enter_blocking_section();\
954 return result;\
957 static size_t cb_WRITEFUNCTION_nolock(char *ptr, size_t size, size_t nmemb, void *data)
959 CAMLparam0();
960 CAMLlocal2(result, str);
961 Connection *conn = (Connection *)data;
962 size_t i;
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 cb_READFUNCTION_nolock(void *ptr, size_t size, size_t nmemb, void *data)
980 CAMLparam0();
981 CAMLlocal1(result);
982 Connection *conn = (Connection *)data;
983 size_t length;
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);
1003 else
1005 CAMLreturnT(size_t,CURL_READFUNC_ABORT);
1009 WRAP_DATA_CALLBACK( READFUNCTION)
1011 static size_t cb_HEADERFUNCTION_nolock(char *ptr, size_t size, size_t nmemb, void *data)
1013 CAMLparam0();
1014 CAMLlocal2(result,str);
1015 Connection *conn = (Connection *)data;
1016 size_t i;
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 cb_PROGRESSFUNCTION_nolock(void *data,
1033 double dlTotal,
1034 double dlNow,
1035 double ulTotal,
1036 double ulNow)
1038 CAMLparam0();
1039 CAMLlocal1(result);
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),
1051 4, callbackData);
1053 CAMLreturnT(int, Is_exception_result(result) ? 1 : Bool_val(result));
1056 static int cb_PROGRESSFUNCTION(void *data,
1057 double dlTotal,
1058 double dlNow,
1059 double ulTotal,
1060 double ulNow)
1062 int r;
1063 leave_blocking_section();
1064 r = cb_PROGRESSFUNCTION_nolock(data,dlTotal,dlNow,ulTotal,ulNow);
1065 enter_blocking_section();
1066 return r;
1069 static int cb_DEBUGFUNCTION_nolock(CURL *debugConnection,
1070 curl_infotype infoType,
1071 char *buffer,
1072 size_t bufferLength,
1073 void *data)
1075 CAMLparam0();
1076 CAMLlocal3(camlDebugConnection, camlInfoType, camlMessage);
1077 size_t i;
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,
1092 camlInfoType,
1093 camlMessage);
1095 CAMLreturnT(int, 0);
1098 static int cb_DEBUGFUNCTION(CURL *debugConnection,
1099 curl_infotype infoType,
1100 char *buffer,
1101 size_t bufferLength,
1102 void *data)
1104 int r;
1105 leave_blocking_section();
1106 r = cb_DEBUGFUNCTION_nolock(debugConnection, infoType, buffer, bufferLength, data);
1107 enter_blocking_section();
1108 return r;
1111 static curlioerr cb_IOCTLFUNCTION_nolock(CURL *ioctl,
1112 int cmd,
1113 void *data)
1115 CAMLparam0();
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);
1127 else
1128 failwith("Invalid IOCTL Cmd!");
1130 camlConnection = caml_curl_alloc(conn);
1132 camlResult = callback2_exn(Field(conn->ocamlValues, Ocaml_IOCTLFUNCTION),
1133 camlConnection,
1134 camlCmd);
1136 if (Is_exception_result(camlResult))
1138 result = CURLIOE_FAILRESTART;
1140 else
1141 switch (Long_val(camlResult))
1143 case 0: /* CURLIOE_OK */
1144 result = CURLIOE_OK;
1145 break;
1147 case 1: /* CURLIOE_UNKNOWNCMD */
1148 result = CURLIOE_UNKNOWNCMD;
1149 break;
1151 case 2: /* CURLIOE_FAILRESTART */
1152 result = CURLIOE_FAILRESTART;
1153 break;
1155 default: /* Incorrect return value, but let's handle it */
1156 result = CURLIOE_FAILRESTART;
1157 break;
1160 CAMLreturnT(curlioerr, result);
1163 static curlioerr cb_IOCTLFUNCTION(CURL *ioctl,
1164 int cmd,
1165 void *data)
1167 curlioerr r;
1168 leave_blocking_section();
1169 r = cb_IOCTLFUNCTION_nolock(ioctl, cmd, data);
1170 enter_blocking_section();
1171 return r;
1174 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1175 static int cb_SEEKFUNCTION_nolock(void *data,
1176 curl_off_t offset,
1177 int origin)
1179 CAMLparam0();
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);
1191 else
1192 failwith("Invalid seek code");
1194 camlResult = callback2_exn(Field(conn->ocamlValues,
1195 Ocaml_SEEKFUNCTION),
1196 camlOffset,
1197 camlOrigin);
1199 int result;
1200 if (Is_exception_result(camlResult))
1201 result = CURL_SEEKFUNC_FAIL;
1202 else
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 cb_SEEKFUNCTION(void *data,
1215 curl_off_t offset,
1216 int origin)
1218 int r;
1219 leave_blocking_section();
1220 r = cb_SEEKFUNCTION_nolock(data,offset,origin);
1221 enter_blocking_section();
1222 return r;
1225 #endif
1227 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1228 static int cb_OPENSOCKETFUNCTION_nolock(void *data,
1229 curlsocktype purpose,
1230 struct curl_sockaddr *addr)
1232 CAMLparam0();
1233 CAMLlocal1(result);
1234 Connection *conn = (Connection *)data;
1235 int sock = -1;
1236 (void)purpose; /* not used */
1238 sock = socket(addr->family, addr->socktype, addr->protocol);
1240 if (-1 != sock)
1242 /* FIXME windows */
1243 result = callback_exn(Field(conn->ocamlValues, Ocaml_OPENSOCKETFUNCTION), Val_int(sock));
1244 if (Is_exception_result(result))
1246 close(sock);
1247 sock = -1;
1251 CAMLreturnT(int, (sock == -1) ? CURL_SOCKET_BAD : sock);
1254 static int cb_OPENSOCKETFUNCTION(void *data,
1255 curlsocktype purpose,
1256 struct curl_sockaddr *address)
1258 int r;
1259 leave_blocking_section();
1260 r = cb_OPENSOCKETFUNCTION_nolock(data,purpose,address);
1261 enter_blocking_section();
1262 return r;
1265 #endif
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)));
1279 break;
1281 case 1: /* CURLINIT_GLOBALSSL */
1282 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_SSL)));
1283 break;
1285 case 2: /* CURLINIT_GLOBALWIN32 */
1286 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_WIN32)));
1287 break;
1289 case 3: /* CURLINIT_GLOBALNOTHING */
1290 CAMLreturn(Val_long(curl_global_init(CURL_GLOBAL_NOTHING)));
1291 break;
1293 default:
1294 failwith("Invalid Initialization Option");
1295 break;
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)
1308 CAMLparam0();
1310 curl_global_cleanup();
1312 CAMLreturn(Val_unit);
1316 ** curl_easy_init helper function
1318 CAMLprim value helper_curl_easy_init(void)
1320 CAMLparam0();
1321 CAMLlocal1(result);
1323 result = caml_curl_alloc(newConnection());
1325 CAMLreturn(result);
1328 CAMLprim value helper_curl_easy_reset(value conn)
1330 CAMLparam1(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 #define SETOPT_FUNCTION(name) \
1345 static void handle_##name##FUNCTION(Connection *conn, value option) \
1347 CAMLparam1(option); \
1348 CURLcode result = CURLE_OK; \
1349 Store_field(conn->ocamlValues, Ocaml_##name##FUNCTION, option); \
1350 result = curl_easy_setopt(conn->connection, CURLOPT_##name##FUNCTION, cb_##name##FUNCTION); \
1351 if (result != CURLE_OK) raiseError(conn, result); \
1352 result = curl_easy_setopt(conn->connection, CURLOPT_##name##DATA, conn); \
1353 if (result != CURLE_OK) raiseError(conn, result); \
1354 CAMLreturn0; \
1357 SETOPT_FUNCTION( WRITE)
1358 SETOPT_FUNCTION( READ)
1359 SETOPT_FUNCTION( HEADER)
1360 SETOPT_FUNCTION( PROGRESS)
1361 SETOPT_FUNCTION( DEBUG)
1363 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
1364 SETOPT_FUNCTION( SEEK)
1365 #endif
1367 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
1368 SETOPT_FUNCTION( IOCTL)
1369 #endif
1371 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
1372 SETOPT_FUNCTION( OPENSOCKET)
1373 #endif
1375 static void handle_slist(Connection *conn, struct curl_slist** slist, OcamlValue caml_option, CURLoption curl_option, value option)
1377 CAMLparam1(option);
1378 CURLcode result = CURLE_OK;
1380 Store_field(conn->ocamlValues, caml_option, option);
1382 free_curl_slist(*slist);
1383 *slist = NULL;
1385 while (Val_emptylist != option)
1387 *slist = curl_slist_append(*slist, String_val(Field(option, 0)));
1389 option = Field(option, 1);
1392 result = curl_easy_setopt(conn->connection, curl_option, *slist);
1394 if (result != CURLE_OK)
1395 raiseError(conn, result);
1397 CAMLreturn0;
1400 #define SETOPT_STRING(name) \
1401 static void handle_##name(Connection *conn, value option) \
1403 CAMLparam1(option); \
1404 CURLcode result = CURLE_OK; \
1406 Store_field(conn->ocamlValues, Ocaml_##name, option); \
1408 if (conn->curl_##name != NULL) \
1409 free(conn->curl_##name); \
1411 conn->curl_##name = strdup(String_val(option)); \
1413 result = curl_easy_setopt(conn->connection, CURLOPT_##name, conn->curl_##name); \
1415 if (result != CURLE_OK) \
1416 raiseError(conn, result); \
1418 CAMLreturn0; \
1421 #define SETOPT_VAL_(func_name, curl_option, conv_val) \
1422 static void func_name(Connection *conn, value option) \
1424 CAMLparam1(option); \
1425 CURLcode result = CURLE_OK; \
1427 result = curl_easy_setopt(conn->connection, curl_option, conv_val(option)); \
1429 if (result != CURLE_OK) \
1430 raiseError(conn, result); \
1432 CAMLreturn0; \
1435 #define SETOPT_VAL(name, conv) SETOPT_VAL_(handle_##name, CURLOPT_##name, conv)
1436 #define SETOPT_BOOL(name) SETOPT_VAL(name, Bool_val)
1437 #define SETOPT_LONG(name) SETOPT_VAL(name, Long_val)
1438 #define SETOPT_INT64(name) SETOPT_VAL(name, Int64_val)
1440 #define SETOPT_SLIST(name) \
1441 static void handle_##name(Connection* conn, value option) \
1443 handle_slist(conn,&(conn->curl_##name),Ocaml_##name,CURLOPT_##name,option); \
1446 SETOPT_STRING( URL)
1447 SETOPT_LONG( INFILESIZE)
1448 SETOPT_STRING( PROXY)
1449 SETOPT_LONG( PROXYPORT)
1450 SETOPT_BOOL( HTTPPROXYTUNNEL)
1451 SETOPT_BOOL( VERBOSE)
1452 SETOPT_BOOL( HEADER)
1453 SETOPT_BOOL( NOPROGRESS)
1455 #if HAVE_DECL_CURLOPT_NOSIGNAL
1456 SETOPT_BOOL( NOSIGNAL)
1457 #endif
1459 SETOPT_BOOL( NOBODY)
1460 SETOPT_BOOL( FAILONERROR)
1461 SETOPT_BOOL( UPLOAD)
1462 SETOPT_BOOL( POST)
1463 SETOPT_BOOL( FTPLISTONLY)
1464 SETOPT_BOOL( FTPAPPEND)
1467 static void handle_NETRC(Connection *conn, value option)
1469 CAMLparam1(option);
1470 CURLcode result = CURLE_OK;
1471 long netrc;
1473 switch (Long_val(option))
1475 case 0: /* CURL_NETRC_OPTIONAL */
1476 netrc = CURL_NETRC_OPTIONAL;
1477 break;
1479 case 1:/* CURL_NETRC_IGNORED */
1480 netrc = CURL_NETRC_IGNORED;
1481 break;
1483 case 2: /* CURL_NETRC_REQUIRED */
1484 netrc = CURL_NETRC_REQUIRED;
1485 break;
1487 default:
1488 failwith("Invalid NETRC Option");
1489 break;
1492 result = curl_easy_setopt(conn->connection,
1493 CURLOPT_NETRC,
1494 netrc);
1496 if (result != CURLE_OK)
1497 raiseError(conn, result);
1499 CAMLreturn0;
1502 #if HAVE_DECL_CURLOPT_ENCODING
1503 static void handle_ENCODING(Connection *conn, value option)
1505 CAMLparam1(option);
1506 CURLcode result = CURLE_OK;
1508 switch (Long_val(option))
1510 case 0: /* CURL_ENCODING_NONE */
1511 result = curl_easy_setopt(conn->connection,
1512 CURLOPT_ENCODING,
1513 "identity");
1514 break;
1516 case 1: /* CURL_ENCODING_DEFLATE */
1517 result = curl_easy_setopt(conn->connection,
1518 CURLOPT_ENCODING,
1519 "deflate");
1520 break;
1522 case 2: /* CURL_ENCODING_GZIP */
1523 result = curl_easy_setopt(conn->connection,
1524 CURLOPT_ENCODING,
1525 "gzip");
1526 break;
1528 case 3: /* CURL_ENCODING_ANY */
1529 result = curl_easy_setopt(conn->connection,
1530 CURLOPT_ENCODING,
1531 "");
1532 break;
1534 default:
1535 failwith("Invalid Encoding Option");
1536 break;
1539 if (result != CURLE_OK)
1540 raiseError(conn, result);
1542 CAMLreturn0;
1544 #endif
1547 SETOPT_BOOL( FOLLOWLOCATION)
1548 SETOPT_BOOL( TRANSFERTEXT)
1549 SETOPT_BOOL( PUT)
1550 SETOPT_STRING( USERPWD)
1551 SETOPT_STRING( PROXYUSERPWD)
1552 SETOPT_STRING( RANGE)
1554 static void handle_ERRORBUFFER(Connection *conn, value option)
1556 CAMLparam1(option);
1557 CURLcode result = CURLE_OK;
1559 Store_field(conn->ocamlValues, Ocaml_ERRORBUFFER, option);
1561 if (conn->curl_ERRORBUFFER != NULL)
1562 free(conn->curl_ERRORBUFFER);
1564 conn->curl_ERRORBUFFER = malloc(sizeof(char) * CURL_ERROR_SIZE);
1566 result = curl_easy_setopt(conn->connection,
1567 CURLOPT_ERRORBUFFER,
1568 conn->curl_ERRORBUFFER);
1570 if (result != CURLE_OK)
1571 raiseError(conn, result);
1573 CAMLreturn0;
1576 SETOPT_LONG( TIMEOUT)
1578 static void handle_POSTFIELDS(Connection *conn, value option)
1580 CAMLparam1(option);
1581 CURLcode result = CURLE_OK;
1583 Store_field(conn->ocamlValues, Ocaml_POSTFIELDS, option);
1585 if (conn->curl_POSTFIELDS != NULL)
1586 free(conn->curl_POSTFIELDS);
1588 conn->curl_POSTFIELDS = strdup_ml(option);
1590 result = curl_easy_setopt(conn->connection,
1591 CURLOPT_POSTFIELDS,
1592 conn->curl_POSTFIELDS);
1594 if (result != CURLE_OK)
1595 raiseError(conn, result);
1597 CAMLreturn0;
1600 SETOPT_LONG( POSTFIELDSIZE)
1601 SETOPT_STRING( REFERER)
1602 SETOPT_STRING( USERAGENT)
1603 SETOPT_STRING( FTPPORT)
1604 SETOPT_LONG( LOW_SPEED_LIMIT)
1605 SETOPT_LONG( LOW_SPEED_TIME)
1606 SETOPT_LONG( RESUME_FROM)
1607 SETOPT_STRING( COOKIE)
1609 SETOPT_SLIST( HTTPHEADER)
1611 static void handle_HTTPPOST(Connection *conn, value option)
1613 CAMLparam1(option);
1614 CAMLlocal3(listIter, formItem, contentType);
1615 CURLcode result = CURLE_OK;
1617 listIter = option;
1619 Store_field(conn->ocamlValues, Ocaml_HTTPPOST, option);
1621 free_curl_slist(conn->httpPostBuffers);
1622 if (conn->httpPostFirst != NULL)
1623 curl_formfree(conn->httpPostFirst);
1625 conn->httpPostBuffers = NULL;
1626 conn->httpPostFirst = NULL;
1627 conn->httpPostLast = NULL;
1629 while (!Is_long(listIter))
1631 formItem = Field(listIter, 0);
1633 switch (Tag_val(formItem))
1635 case 0: /* CURLFORM_CONTENT */
1636 if (Wosize_val(formItem) < 3)
1638 failwith("Incorrect CURLFORM_CONTENT parameters");
1641 if (Is_long(Field(formItem, 2)) &&
1642 Long_val(Field(formItem, 2)) == 0)
1644 curl_formadd(&conn->httpPostFirst,
1645 &conn->httpPostLast,
1646 CURLFORM_COPYNAME,
1647 String_val(Field(formItem, 0)),
1648 CURLFORM_NAMELENGTH,
1649 string_length(Field(formItem, 0)),
1650 CURLFORM_COPYCONTENTS,
1651 String_val(Field(formItem, 1)),
1652 CURLFORM_CONTENTSLENGTH,
1653 string_length(Field(formItem, 1)),
1654 CURLFORM_END);
1656 else if (Is_block(Field(formItem, 2)))
1658 contentType = Field(formItem, 2);
1660 curl_formadd(&conn->httpPostFirst,
1661 &conn->httpPostLast,
1662 CURLFORM_COPYNAME,
1663 String_val(Field(formItem, 0)),
1664 CURLFORM_NAMELENGTH,
1665 string_length(Field(formItem, 0)),
1666 CURLFORM_PTRCONTENTS,
1667 String_val(Field(formItem, 1)),
1668 CURLFORM_CONTENTSLENGTH,
1669 string_length(Field(formItem, 1)),
1670 CURLFORM_CONTENTTYPE,
1671 String_val(Field(contentType, 0)),
1672 CURLFORM_END);
1674 else
1676 failwith("Incorrect CURLFORM_CONTENT parameters");
1678 break;
1680 case 1: /* CURLFORM_FILECONTENT */
1681 if (Wosize_val(formItem) < 3)
1683 failwith("Incorrect CURLFORM_FILECONTENT parameters");
1686 if (Is_long(Field(formItem, 2)) &&
1687 Long_val(Field(formItem, 2)) == 0)
1689 curl_formadd(&conn->httpPostFirst,
1690 &conn->httpPostLast,
1691 CURLFORM_COPYNAME,
1692 String_val(Field(formItem, 0)),
1693 CURLFORM_NAMELENGTH,
1694 string_length(Field(formItem, 0)),
1695 CURLFORM_FILECONTENT,
1696 String_val(Field(formItem, 1)),
1697 CURLFORM_END);
1699 else if (Is_block(Field(formItem, 2)))
1701 contentType = Field(formItem, 2);
1703 curl_formadd(&conn->httpPostFirst,
1704 &conn->httpPostLast,
1705 CURLFORM_COPYNAME,
1706 String_val(Field(formItem, 0)),
1707 CURLFORM_NAMELENGTH,
1708 string_length(Field(formItem, 0)),
1709 CURLFORM_FILECONTENT,
1710 String_val(Field(formItem, 1)),
1711 CURLFORM_CONTENTTYPE,
1712 String_val(Field(contentType, 0)),
1713 CURLFORM_END);
1715 else
1717 failwith("Incorrect CURLFORM_FILECONTENT parameters");
1719 break;
1721 case 2: /* CURLFORM_FILE */
1722 if (Wosize_val(formItem) < 3)
1724 failwith("Incorrect CURLFORM_FILE parameters");
1727 if (Is_long(Field(formItem, 2)) &&
1728 Long_val(Field(formItem, 2)) == 0)
1730 curl_formadd(&conn->httpPostFirst,
1731 &conn->httpPostLast,
1732 CURLFORM_COPYNAME,
1733 String_val(Field(formItem, 0)),
1734 CURLFORM_NAMELENGTH,
1735 string_length(Field(formItem, 0)),
1736 CURLFORM_FILE,
1737 String_val(Field(formItem, 1)),
1738 CURLFORM_END);
1740 else if (Is_block(Field(formItem, 2)))
1742 contentType = Field(formItem, 2);
1744 curl_formadd(&conn->httpPostFirst,
1745 &conn->httpPostLast,
1746 CURLFORM_COPYNAME,
1747 String_val(Field(formItem, 0)),
1748 CURLFORM_NAMELENGTH,
1749 string_length(Field(formItem, 0)),
1750 CURLFORM_FILE,
1751 String_val(Field(formItem, 1)),
1752 CURLFORM_CONTENTTYPE,
1753 String_val(Field(contentType, 0)),
1754 CURLFORM_END);
1756 else
1758 failwith("Incorrect CURLFORM_FILE parameters");
1760 break;
1762 case 3: /* CURLFORM_BUFFER */
1763 if (Wosize_val(formItem) < 4)
1765 failwith("Incorrect CURLFORM_BUFFER parameters");
1768 if (Is_long(Field(formItem, 3)) &&
1769 Long_val(Field(formItem, 3)) == 0)
1771 conn->httpPostBuffers = curl_slist_append(conn->httpPostBuffers, String_val(Field(formItem, 2)));
1773 curl_formadd(&conn->httpPostFirst,
1774 &conn->httpPostLast,
1775 CURLFORM_COPYNAME,
1776 String_val(Field(formItem, 0)),
1777 CURLFORM_NAMELENGTH,
1778 string_length(Field(formItem, 0)),
1779 CURLFORM_BUFFER,
1780 String_val(Field(formItem, 1)),
1781 CURLFORM_BUFFERPTR,
1782 conn->httpPostBuffers->data,
1783 CURLFORM_BUFFERLENGTH,
1784 string_length(Field(formItem, 2)),
1785 CURLFORM_END);
1787 else if (Is_block(Field(formItem, 3)))
1789 conn->httpPostBuffers = curl_slist_append(conn->httpPostBuffers, String_val(Field(formItem, 2)));
1791 contentType = Field(formItem, 3);
1793 curl_formadd(&conn->httpPostFirst,
1794 &conn->httpPostLast,
1795 CURLFORM_COPYNAME,
1796 String_val(Field(formItem, 0)),
1797 CURLFORM_NAMELENGTH,
1798 string_length(Field(formItem, 0)),
1799 CURLFORM_BUFFER,
1800 String_val(Field(formItem, 1)),
1801 CURLFORM_BUFFERPTR,
1802 conn->httpPostBuffers->data,
1803 CURLFORM_BUFFERLENGTH,
1804 string_length(Field(formItem, 2)),
1805 CURLFORM_CONTENTTYPE,
1806 String_val(Field(contentType, 0)),
1807 CURLFORM_END);
1809 else
1811 failwith("Incorrect CURLFORM_BUFFER parameters");
1813 break;
1816 listIter = Field(listIter, 1);
1819 result = curl_easy_setopt(conn->connection,
1820 CURLOPT_HTTPPOST,
1821 conn->httpPostFirst);
1823 if (result != CURLE_OK)
1824 raiseError(conn, result);
1826 CAMLreturn0;
1829 SETOPT_STRING( SSLCERT)
1830 SETOPT_STRING( SSLCERTTYPE)
1831 SETOPT_STRING( SSLCERTPASSWD)
1832 SETOPT_STRING( SSLKEY)
1833 SETOPT_STRING( SSLKEYTYPE)
1834 SETOPT_STRING( SSLKEYPASSWD)
1835 SETOPT_STRING( SSLENGINE)
1836 SETOPT_BOOL( SSLENGINE_DEFAULT)
1837 SETOPT_BOOL( CRLF)
1839 SETOPT_SLIST( QUOTE)
1840 SETOPT_SLIST( POSTQUOTE)
1842 SETOPT_STRING( COOKIEFILE)
1843 SETOPT_LONG( SSLVERSION)
1845 static void handle_TIMECONDITION(Connection *conn, value option)
1847 CAMLparam1(option);
1848 CURLcode result = CURLE_OK;
1849 int timecond = CURL_TIMECOND_NONE;
1851 switch (Long_val(option))
1853 case 0: timecond = CURL_TIMECOND_NONE; break;
1854 case 1: timecond = CURL_TIMECOND_IFMODSINCE; break;
1855 case 2: timecond = CURL_TIMECOND_IFUNMODSINCE; break;
1856 case 3: timecond = CURL_TIMECOND_LASTMOD; break;
1857 default:
1858 failwith("Invalid TIMECOND Option");
1859 break;
1862 result = curl_easy_setopt(conn->connection, CURLOPT_TIMECONDITION, timecond);
1864 if (result != CURLE_OK)
1865 raiseError(conn, result);
1867 CAMLreturn0;
1870 SETOPT_VAL( TIMEVALUE, Int32_val)
1871 SETOPT_STRING( CUSTOMREQUEST)
1872 SETOPT_STRING( INTERFACE)
1874 static void handle_KRB4LEVEL(Connection *conn, value option)
1876 CAMLparam1(option);
1877 CURLcode result = CURLE_OK;
1879 switch (Long_val(option))
1881 case 0: /* KRB4_NONE */
1882 result = curl_easy_setopt(conn->connection,
1883 CURLOPT_KRB4LEVEL,
1884 NULL);
1885 break;
1887 case 1: /* KRB4_CLEAR */
1888 result = curl_easy_setopt(conn->connection,
1889 CURLOPT_KRB4LEVEL,
1890 "clear");
1891 break;
1893 case 2: /* KRB4_SAFE */
1894 result = curl_easy_setopt(conn->connection,
1895 CURLOPT_KRB4LEVEL,
1896 "safe");
1897 break;
1899 case 3: /* KRB4_CONFIDENTIAL */
1900 result = curl_easy_setopt(conn->connection,
1901 CURLOPT_KRB4LEVEL,
1902 "confidential");
1903 break;
1905 case 4: /* KRB4_PRIVATE */
1906 result = curl_easy_setopt(conn->connection,
1907 CURLOPT_KRB4LEVEL,
1908 "private");
1909 break;
1911 default:
1912 failwith("Invalid KRB4 Option");
1913 break;
1916 if (result != CURLE_OK)
1917 raiseError(conn, result);
1919 CAMLreturn0;
1922 SETOPT_BOOL( SSL_VERIFYPEER)
1923 SETOPT_STRING( CAINFO)
1924 SETOPT_STRING( CAPATH)
1925 SETOPT_BOOL( FILETIME)
1926 SETOPT_LONG( MAXREDIRS)
1927 SETOPT_LONG( MAXCONNECTS)
1929 static void handle_CLOSEPOLICY(Connection *conn, value option)
1931 CAMLparam1(option);
1932 CURLcode result = CURLE_OK;
1934 switch (Long_val(option))
1936 case 0: /* CLOSEPOLICY_OLDEST */
1937 result = curl_easy_setopt(conn->connection,
1938 CURLOPT_CLOSEPOLICY,
1939 CURLCLOSEPOLICY_OLDEST);
1940 break;
1942 case 1: /* CLOSEPOLICY_LEAST_RECENTLY_USED */
1943 result = curl_easy_setopt(conn->connection,
1944 CURLOPT_CLOSEPOLICY,
1945 CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
1946 break;
1948 default:
1949 failwith("Invalid CLOSEPOLICY Option");
1950 break;
1953 if (result != CURLE_OK)
1954 raiseError(conn, result);
1956 CAMLreturn0;
1959 SETOPT_BOOL( FRESH_CONNECT)
1960 SETOPT_BOOL( FORBID_REUSE)
1961 SETOPT_STRING( RANDOM_FILE)
1962 SETOPT_STRING( EGDSOCKET)
1963 SETOPT_LONG( CONNECTTIMEOUT)
1964 SETOPT_BOOL( HTTPGET)
1966 static void handle_SSL_VERIFYHOST(Connection *conn, value option)
1968 CAMLparam1(option);
1969 CURLcode result = CURLE_OK;
1971 switch (Long_val(option))
1973 case 0: /* SSLVERIFYHOST_NONE */
1974 case 1: /* SSLVERIFYHOST_EXISTENCE */
1975 case 2: /* SSLVERIFYHOST_HOSTNAME */
1976 result = curl_easy_setopt(conn->connection,
1977 CURLOPT_SSL_VERIFYHOST,
1978 /* map EXISTENCE to HOSTNAME */
1979 Long_val(option) == 0 ? 0 : 2);
1980 break;
1982 default:
1983 failwith("Invalid SSLVERIFYHOST Option");
1984 break;
1987 if (result != CURLE_OK)
1988 raiseError(conn, result);
1990 CAMLreturn0;
1993 SETOPT_STRING( COOKIEJAR)
1994 SETOPT_STRING( SSL_CIPHER_LIST)
1996 static void handle_HTTP_VERSION(Connection *conn, value option)
1998 CAMLparam1(option);
1999 CURLcode result = CURLE_OK;
2001 switch (Long_val(option))
2003 case 0: /* HTTP_VERSION_NONE */
2004 result = curl_easy_setopt(conn->connection,
2005 CURLOPT_HTTP_VERSION,
2006 CURL_HTTP_VERSION_NONE);
2007 break;
2009 case 1: /* HTTP_VERSION_1_0 */
2010 result = curl_easy_setopt(conn->connection,
2011 CURLOPT_HTTP_VERSION,
2012 CURL_HTTP_VERSION_1_0);
2013 break;
2015 case 2: /* HTTP_VERSION_1_1 */
2016 result = curl_easy_setopt(conn->connection,
2017 CURLOPT_HTTP_VERSION,
2018 CURL_HTTP_VERSION_1_1);
2019 break;
2021 default:
2022 failwith("Invalid HTTP_VERSION Option");
2023 break;
2026 if (result != CURLE_OK)
2027 raiseError(conn, result);
2029 CAMLreturn0;
2032 SETOPT_BOOL( FTP_USE_EPSV)
2033 SETOPT_LONG( DNS_CACHE_TIMEOUT)
2034 SETOPT_BOOL( DNS_USE_GLOBAL_CACHE)
2036 #if HAVE_DECL_CURLOPT_PRIVATE
2037 SETOPT_STRING( PRIVATE)
2038 #endif
2040 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2041 SETOPT_SLIST( HTTP200ALIASES)
2042 #endif
2044 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2045 SETOPT_BOOL( UNRESTRICTED_AUTH)
2046 #endif
2048 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2049 SETOPT_BOOL( FTP_USE_EPRT)
2050 #endif
2052 #if HAVE_DECL_CURLOPT_HTTPAUTH
2053 static void handle_HTTPAUTH(Connection *conn, value option)
2055 CAMLparam1(option);
2056 CAMLlocal1(listIter);
2057 CURLcode result = CURLE_OK;
2058 long auth = CURLAUTH_NONE;
2060 listIter = option;
2062 while (!Is_long(listIter))
2064 switch (Long_val(Field(listIter, 0)))
2066 case 0: /* CURLAUTH_BASIC */
2067 auth |= CURLAUTH_BASIC;
2068 break;
2070 case 1: /* CURLAUTH_DIGEST */
2071 auth |= CURLAUTH_DIGEST;
2072 break;
2074 case 2: /* CURLAUTH_GSSNEGOTIATE */
2075 auth |= CURLAUTH_GSSNEGOTIATE;
2076 break;
2078 case 3: /* CURLAUTH_NTLM */
2079 auth |= CURLAUTH_NTLM;
2080 break;
2082 case 4: /* CURLAUTH_ANY */
2083 auth |= CURLAUTH_ANY;
2084 break;
2086 case 5: /* CURLAUTH_ANYSAFE */
2087 auth |= CURLAUTH_ANYSAFE;
2088 break;
2090 default:
2091 failwith("Invalid HTTPAUTH Value");
2092 break;
2095 listIter = Field(listIter, 1);
2098 result = curl_easy_setopt(conn->connection,
2099 CURLOPT_HTTPAUTH,
2100 auth);
2102 if (result != CURLE_OK)
2103 raiseError(conn, result);
2105 CAMLreturn0;
2107 #endif
2109 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2110 SETOPT_BOOL( FTP_CREATE_MISSING_DIRS)
2111 #endif
2113 #if HAVE_DECL_CURLOPT_PROXYAUTH
2114 static void handle_PROXYAUTH(Connection *conn, value option)
2116 CAMLparam1(option);
2117 CAMLlocal1(listIter);
2118 CURLcode result = CURLE_OK;
2119 long auth = CURLAUTH_NONE;
2121 listIter = option;
2123 while (!Is_long(listIter))
2125 switch (Long_val(Field(listIter, 0)))
2127 case 0: /* CURLAUTH_BASIC */
2128 auth |= CURLAUTH_BASIC;
2129 break;
2131 case 1: /* CURLAUTH_DIGEST */
2132 auth |= CURLAUTH_DIGEST;
2133 break;
2135 case 2: /* CURLAUTH_GSSNEGOTIATE */
2136 auth |= CURLAUTH_GSSNEGOTIATE;
2137 break;
2139 case 3: /* CURLAUTH_NTLM */
2140 auth |= CURLAUTH_NTLM;
2141 break;
2143 case 4: /* CURLAUTH_ANY */
2144 auth |= CURLAUTH_ANY;
2145 break;
2147 case 5: /* CURLAUTH_ANYSAFE */
2148 auth |= CURLAUTH_ANYSAFE;
2149 break;
2151 default:
2152 failwith("Invalid HTTPAUTH Value");
2153 break;
2156 listIter = Field(listIter, 1);
2159 result = curl_easy_setopt(conn->connection,
2160 CURLOPT_PROXYAUTH,
2161 auth);
2163 if (result != CURLE_OK)
2164 raiseError(conn, result);
2166 CAMLreturn0;
2168 #endif
2170 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2171 SETOPT_LONG( FTP_RESPONSE_TIMEOUT)
2172 #endif
2174 #if HAVE_DECL_CURLOPT_IPRESOLVE
2175 static void handle_IPRESOLVE(Connection *conn, value option)
2177 CAMLparam1(option);
2178 CURLcode result = CURLE_OK;
2180 switch (Long_val(option))
2182 case 0: /* CURL_IPRESOLVE_WHATEVER */
2183 result = curl_easy_setopt(conn->connection,
2184 CURLOPT_IPRESOLVE,
2185 CURL_IPRESOLVE_WHATEVER);
2186 break;
2188 case 1: /* CURL_IPRESOLVE_V4 */
2189 result = curl_easy_setopt(conn->connection,
2190 CURLOPT_IPRESOLVE,
2191 CURL_IPRESOLVE_V4);
2192 break;
2194 case 2: /* CURL_IPRESOLVE_V6 */
2195 result = curl_easy_setopt(conn->connection,
2196 CURLOPT_IPRESOLVE,
2197 CURL_IPRESOLVE_V6);
2198 break;
2200 default:
2201 failwith("Invalid IPRESOLVE Value");
2202 break;
2205 if (result != CURLE_OK)
2206 raiseError(conn, result);
2208 CAMLreturn0;
2210 #endif
2212 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2213 SETOPT_VAL( MAXFILESIZE, Int32_val)
2214 #endif
2216 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2217 SETOPT_INT64( INFILESIZE_LARGE)
2218 #endif
2220 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2221 SETOPT_INT64( RESUME_FROM_LARGE)
2222 #endif
2224 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2225 SETOPT_INT64( MAXFILESIZE_LARGE)
2226 #endif
2228 #if HAVE_DECL_CURLOPT_NETRC_FILE
2229 SETOPT_STRING( NETRC_FILE)
2230 #endif
2232 #if HAVE_DECL_CURLOPT_FTP_SSL
2233 static void handle_FTP_SSL(Connection *conn, value option)
2235 CAMLparam1(option);
2236 CURLcode result = CURLE_OK;
2238 switch (Long_val(option))
2240 case 0: /* CURLFTPSSL_NONE */
2241 result = curl_easy_setopt(conn->connection,
2242 CURLOPT_FTP_SSL,
2243 CURLFTPSSL_NONE);
2244 break;
2246 case 1: /* CURLFTPSSL_TRY */
2247 result = curl_easy_setopt(conn->connection,
2248 CURLOPT_FTP_SSL,
2249 CURLFTPSSL_TRY);
2250 break;
2252 case 2: /* CURLFTPSSL_CONTROL */
2253 result = curl_easy_setopt(conn->connection,
2254 CURLOPT_FTP_SSL,
2255 CURLFTPSSL_CONTROL);
2256 break;
2258 case 3: /* CURLFTPSSL_ALL */
2259 result = curl_easy_setopt(conn->connection,
2260 CURLOPT_FTP_SSL,
2261 CURLFTPSSL_ALL);
2262 break;
2264 default:
2265 failwith("Invalid FTP_SSL Value");
2266 break;
2269 if (result != CURLE_OK)
2270 raiseError(conn, result);
2272 CAMLreturn0;
2274 #endif
2276 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2277 SETOPT_INT64( POSTFIELDSIZE_LARGE)
2278 #endif
2280 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2281 /* not using SETOPT_BOOL here because of TCP_NODELAY defined in winsock.h */
2282 SETOPT_VAL_( handle_TCP_NODELAY, CURLOPT_TCP_NODELAY, Bool_val)
2283 #endif
2285 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2286 static void handle_FTPSSLAUTH(Connection *conn, value option)
2288 CAMLparam1(option);
2289 CURLcode result = CURLE_OK;
2291 switch (Long_val(option))
2293 case 0: /* CURLFTPAUTH_DEFAULT */
2294 result = curl_easy_setopt(conn->connection,
2295 CURLOPT_FTPSSLAUTH,
2296 CURLFTPAUTH_DEFAULT);
2297 break;
2299 case 1: /* CURLFTPAUTH_SSL */
2300 result = curl_easy_setopt(conn->connection,
2301 CURLOPT_FTPSSLAUTH,
2302 CURLFTPAUTH_SSL);
2303 break;
2305 case 2: /* CURLFTPAUTH_TLS */
2306 result = curl_easy_setopt(conn->connection,
2307 CURLOPT_FTPSSLAUTH,
2308 CURLFTPAUTH_TLS);
2309 break;
2311 default:
2312 failwith("Invalid FTPSSLAUTH value");
2313 break;
2316 if (result != CURLE_OK)
2317 raiseError(conn, result);
2319 CAMLreturn0;
2321 #endif
2323 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2324 SETOPT_STRING( FTP_ACCOUNT)
2325 #endif
2327 #if HAVE_DECL_CURLOPT_COOKIELIST
2328 SETOPT_STRING( COOKIELIST)
2329 #endif
2331 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2332 SETOPT_BOOL( IGNORE_CONTENT_LENGTH)
2333 #endif
2335 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2336 SETOPT_BOOL( FTP_SKIP_PASV_IP)
2337 #endif
2339 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2340 static void handle_FTP_FILEMETHOD(Connection *conn, value option)
2342 CAMLparam1(option);
2343 CURLcode result = CURLE_OK;
2345 switch (Long_val(option))
2347 case 0: /* CURLFTPMETHOD_DEFAULT */
2348 result = curl_easy_setopt(conn->connection,
2349 CURLOPT_FTP_FILEMETHOD,
2350 CURLFTPMETHOD_DEFAULT);
2351 break;
2353 case 1: /* CURLFTMETHOD_MULTICWD */
2354 result = curl_easy_setopt(conn->connection,
2355 CURLOPT_FTP_FILEMETHOD,
2356 CURLFTPMETHOD_MULTICWD);
2357 break;
2359 case 2: /* CURLFTPMETHOD_NOCWD */
2360 result = curl_easy_setopt(conn->connection,
2361 CURLOPT_FTP_FILEMETHOD,
2362 CURLFTPMETHOD_NOCWD);
2363 break;
2365 case 3: /* CURLFTPMETHOD_SINGLECWD */
2366 result = curl_easy_setopt(conn->connection,
2367 CURLOPT_FTP_FILEMETHOD,
2368 CURLFTPMETHOD_SINGLECWD);
2370 default:
2371 failwith("Invalid FTP_FILEMETHOD value");
2372 break;
2375 if (result != CURLE_OK)
2376 raiseError(conn, result);
2378 CAMLreturn0;
2380 #endif
2382 #if HAVE_DECL_CURLOPT_LOCALPORT
2383 SETOPT_LONG( LOCALPORT)
2384 #endif
2386 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2387 SETOPT_LONG( LOCALPORTRANGE)
2388 #endif
2390 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2391 SETOPT_BOOL( CONNECT_ONLY)
2392 #endif
2394 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2395 SETOPT_INT64( MAX_SEND_SPEED_LARGE)
2396 #endif
2398 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2399 SETOPT_INT64( MAX_RECV_SPEED_LARGE)
2400 #endif
2402 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2403 SETOPT_STRING( FTP_ALTERNATIVE_TO_USER)
2404 #endif
2406 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2407 SETOPT_BOOL( SSL_SESSIONID_CACHE)
2408 #endif
2410 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2411 static void handle_SSH_AUTH_TYPES(Connection *conn, value option)
2413 CAMLparam1(option);
2414 CAMLlocal1(listIter);
2415 CURLcode result = CURLE_OK;
2416 long authTypes = CURLSSH_AUTH_NONE;
2418 listIter = option;
2420 while (!Is_long(listIter))
2422 switch (Long_val(Field(listIter, 0)))
2424 case 0: /* CURLSSH_AUTH_ANY */
2425 authTypes |= CURLSSH_AUTH_ANY;
2426 break;
2428 case 1: /* CURLSSH_AUTH_PUBLICKEY */
2429 authTypes |= CURLSSH_AUTH_PUBLICKEY;
2430 break;
2432 case 2: /* CURLSSH_AUTH_PASSWORD */
2433 authTypes |= CURLSSH_AUTH_PASSWORD;
2434 break;
2436 case 3: /* CURLSSH_AUTH_HOST */
2437 authTypes |= CURLSSH_AUTH_HOST;
2438 break;
2440 case 4: /* CURLSSH_AUTH_KEYBOARD */
2441 authTypes |= CURLSSH_AUTH_KEYBOARD;
2442 break;
2444 default:
2445 failwith("Invalid CURLSSH_AUTH_TYPES Value");
2446 break;
2449 listIter = Field(listIter, 1);
2452 result = curl_easy_setopt(conn->connection,
2453 CURLOPT_SSH_AUTH_TYPES,
2454 authTypes);
2456 if (result != CURLE_OK)
2457 raiseError(conn, result);
2459 CAMLreturn0;
2461 #endif
2463 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2464 SETOPT_STRING( SSH_PUBLIC_KEYFILE)
2465 #endif
2467 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2468 SETOPT_STRING( SSH_PRIVATE_KEYFILE)
2469 #endif
2471 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2472 static void handle_FTP_SSL_CCC(Connection *conn, value option)
2474 CAMLparam1(option);
2475 CURLcode result = CURLE_OK;
2477 switch (Long_val(option))
2479 case 0: /* CURLFTPSSL_CCC_NONE */
2480 result = curl_easy_setopt(conn->connection,
2481 CURLOPT_FTP_SSL_CCC,
2482 CURLFTPSSL_CCC_NONE);
2483 break;
2485 case 1: /* CURLFTPSSL_CCC_PASSIVE */
2486 result = curl_easy_setopt(conn->connection,
2487 CURLOPT_FTP_SSL_CCC,
2488 CURLFTPSSL_CCC_PASSIVE);
2489 break;
2491 case 2: /* CURLFTPSSL_CCC_ACTIVE */
2492 result = curl_easy_setopt(conn->connection,
2493 CURLOPT_FTP_SSL_CCC,
2494 CURLFTPSSL_CCC_ACTIVE);
2495 break;
2497 default:
2498 failwith("Invalid FTPSSL_CCC value");
2499 break;
2502 if (result != CURLE_OK)
2503 raiseError(conn, result);
2505 CAMLreturn0;
2507 #endif
2509 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2510 SETOPT_LONG( TIMEOUT_MS)
2511 #endif
2513 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
2514 SETOPT_LONG( CONNECTTIMEOUT_MS)
2515 #endif
2517 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
2518 SETOPT_BOOL( HTTP_TRANSFER_DECODING)
2519 #endif
2521 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
2522 SETOPT_BOOL( HTTP_CONTENT_DECODING)
2523 #endif
2525 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
2526 SETOPT_LONG( NEW_FILE_PERMS)
2527 #endif
2529 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
2530 SETOPT_LONG( NEW_DIRECTORY_PERMS)
2531 #endif
2533 #if HAVE_DECL_CURLOPT_POST301
2534 SETOPT_BOOL( POST301)
2535 #endif
2537 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
2538 SETOPT_STRING( SSH_HOST_PUBLIC_KEY_MD5)
2539 #endif
2541 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
2542 SETOPT_STRING( COPYPOSTFIELDS)
2543 #endif
2545 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
2546 SETOPT_BOOL( PROXY_TRANSFER_MODE)
2547 #endif
2549 #if HAVE_DECL_CURLOPT_AUTOREFERER
2550 SETOPT_BOOL( AUTOREFERER)
2551 #endif
2553 #if HAVE_DECL_CURLOPT_PROXYTYPE
2554 static void handle_PROXYTYPE(Connection *conn, value option)
2556 CAMLparam1(option);
2557 CURLcode result = CURLE_OK;
2558 long proxy_type;
2560 switch (Long_val(option))
2562 case 0: proxy_type = CURLPROXY_HTTP; break;
2563 case 1: proxy_type = CURLPROXY_HTTP_1_0; break;
2564 case 2: proxy_type = CURLPROXY_SOCKS4; break;
2565 case 3: proxy_type = CURLPROXY_SOCKS5; break;
2566 case 4: proxy_type = CURLPROXY_SOCKS4A; break;
2567 case 5: proxy_type = CURLPROXY_SOCKS5_HOSTNAME; break;
2568 default:
2569 failwith("Invalid curl proxy type");
2572 result = curl_easy_setopt(conn->connection,
2573 CURLOPT_PROXYTYPE,
2574 proxy_type);
2576 if (result != CURLE_OK)
2577 raiseError(conn, result);
2579 CAMLreturn0;
2581 #endif
2583 #if HAVE_DECL_CURLOPT_PROTOCOLS || HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2585 long protoMap[] =
2587 CURLPROTO_ALL,
2588 CURLPROTO_HTTP, CURLPROTO_HTTPS, CURLPROTO_FTP, CURLPROTO_FTPS, CURLPROTO_SCP, CURLPROTO_SFTP,
2589 CURLPROTO_TELNET, CURLPROTO_LDAP, CURLPROTO_LDAPS, CURLPROTO_DICT, CURLPROTO_FILE, CURLPROTO_TFTP,
2590 /* factor out with autoconf? */
2591 #if defined(CURLPROTO_IMAP)
2592 CURLPROTO_IMAP,
2593 #else
2595 #endif
2596 #if defined(CURLPROTO_IMAPS)
2597 CURLPROTO_IMAPS,
2598 #else
2600 #endif
2601 #if defined(CURLPROTO_POP3)
2602 CURLPROTO_POP3,
2603 #else
2605 #endif
2606 #if defined(CURLPROTO_POP3S)
2607 CURLPROTO_POP3S,
2608 #else
2610 #endif
2611 #if defined(CURLPROTO_SMTP)
2612 CURLPROTO_SMTP,
2613 #else
2615 #endif
2616 #if defined(CURLPROTO_SMTPS)
2617 CURLPROTO_SMTPS,
2618 #else
2620 #endif
2621 #if defined(CURLPROTO_RTSP)
2622 CURLPROTO_RTSP,
2623 #else
2625 #endif
2626 #if defined(CURLPROTO_RTMP)
2627 CURLPROTO_RTMP,
2628 #else
2630 #endif
2631 #if defined(CURLPROTO_RTMPT)
2632 CURLPROTO_RTMPT,
2633 #else
2635 #endif
2636 #if defined(CURLPROTO_RTMPE)
2637 CURLPROTO_RTMPE,
2638 #else
2640 #endif
2641 #if defined(CURLPROTO_RTMPTE)
2642 CURLPROTO_RTMPTE,
2643 #else
2645 #endif
2646 #if defined(CURLPROTO_RTMPS)
2647 CURLPROTO_RTMPS,
2648 #else
2650 #endif
2651 #if defined(CURLPROTO_RTMPTS)
2652 CURLPROTO_RTMPTS,
2653 #else
2655 #endif
2656 #if defined(CURLPROTO_GOPHER)
2657 CURLPROTO_GOPHER,
2658 #else
2660 #endif
2663 static void handle_PROTOCOLSOPTION(CURLoption curlopt, Connection *conn, value option)
2665 CAMLparam1(option);
2666 CURLcode result = CURLE_OK;
2667 long protocols = 0;
2668 int index;
2670 while (Val_emptylist != option)
2672 index = Int_val(Field(option, 0));
2673 if ((index < 0) || ((size_t)index >= sizeof(protoMap) / sizeof(protoMap[0])))
2674 failwith("Invalid curl protocol");
2676 protocols = protocols | protoMap[index];
2678 option = Field(option, 1);
2681 result = curl_easy_setopt(conn->connection,
2682 curlopt,
2683 protocols);
2685 if (result != CURLE_OK)
2686 raiseError(conn, result);
2688 CAMLreturn0;
2690 #endif
2692 #if HAVE_DECL_CURLOPT_PROTOCOLS
2693 static void handle_PROTOCOLS(Connection *conn, value option)
2695 handle_PROTOCOLSOPTION(CURLOPT_PROTOCOLS, conn, option);
2697 #endif
2699 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
2700 static void handle_REDIR_PROTOCOLS(Connection *conn, value option)
2702 handle_PROTOCOLSOPTION(CURLOPT_REDIR_PROTOCOLS, conn, option);
2704 #endif
2706 #if HAVE_DECL_CURLOPT_RESOLVE
2707 SETOPT_SLIST( RESOLVE)
2708 #endif
2710 #if HAVE_DECL_CURLOPT_DNS_SERVERS
2711 SETOPT_STRING( DNS_SERVERS)
2712 #endif
2714 #if HAVE_DECL_CURLOPT_MAIL_FROM
2715 SETOPT_STRING( MAIL_FROM)
2716 #endif
2718 #if HAVE_DECL_CURLOPT_MAIL_RCPT
2719 SETOPT_SLIST( MAIL_RCPT)
2720 #endif
2723 ** curl_easy_setopt helper function
2726 #define MAP(name) { handle_ ## name, "CURLOPT_"#name, Ocaml_##name }
2727 #define MAP_NO(name) { NULL, "CURLOPT_"#name , Ocaml_##name }
2728 #define IMM(name) { handle_ ## name, "CURLOPT_"#name, -1 }
2729 #define IMM_NO(name) { NULL, "CURLOPT_"#name , -1 }
2731 CURLOptionMapping implementedOptionMap[] =
2733 MAP(WRITEFUNCTION),
2734 MAP(READFUNCTION),
2735 IMM(INFILESIZE),
2736 MAP(URL),
2737 MAP(PROXY),
2738 IMM(PROXYPORT),
2739 IMM(HTTPPROXYTUNNEL),
2740 IMM(VERBOSE),
2741 IMM(HEADER),
2742 IMM(NOPROGRESS),
2743 #if HAVE_DECL_CURLOPT_NOSIGNAL
2744 IMM(NOSIGNAL),
2745 #else
2746 IMM_NO(NOSIGNAL),
2747 #endif
2748 IMM(NOBODY),
2749 IMM(FAILONERROR),
2750 IMM(UPLOAD),
2751 IMM(POST),
2752 IMM(FTPLISTONLY),
2753 IMM(FTPAPPEND),
2754 IMM(NETRC),
2755 #if HAVE_DECL_CURLOPT_ENCODING
2756 IMM(ENCODING),
2757 #else
2758 IMM_NO(ENCODING),
2759 #endif
2760 IMM(FOLLOWLOCATION),
2761 IMM(TRANSFERTEXT),
2762 IMM(PUT),
2763 MAP(USERPWD),
2764 MAP(PROXYUSERPWD),
2765 MAP(RANGE),
2766 IMM(ERRORBUFFER), /* mutable buffer, as output value, do not duplicate */
2767 IMM(TIMEOUT),
2768 MAP(POSTFIELDS),
2769 IMM(POSTFIELDSIZE),
2770 MAP(REFERER),
2771 MAP(USERAGENT),
2772 MAP(FTPPORT),
2773 IMM(LOW_SPEED_LIMIT),
2774 IMM(LOW_SPEED_TIME),
2775 IMM(RESUME_FROM),
2776 MAP(COOKIE),
2777 MAP(HTTPHEADER),
2778 MAP(HTTPPOST),
2779 MAP(SSLCERT),
2780 MAP(SSLCERTTYPE),
2781 MAP(SSLCERTPASSWD),
2782 MAP(SSLKEY),
2783 MAP(SSLKEYTYPE),
2784 MAP(SSLKEYPASSWD),
2785 MAP(SSLENGINE),
2786 IMM(SSLENGINE_DEFAULT),
2787 IMM(CRLF),
2788 MAP(QUOTE),
2789 MAP(POSTQUOTE),
2790 MAP(HEADERFUNCTION),
2791 MAP(COOKIEFILE),
2792 IMM(SSLVERSION),
2793 IMM(TIMECONDITION),
2794 IMM(TIMEVALUE),
2795 MAP(CUSTOMREQUEST),
2796 MAP(INTERFACE),
2797 IMM(KRB4LEVEL),
2798 MAP(PROGRESSFUNCTION),
2799 IMM(SSL_VERIFYPEER),
2800 MAP(CAINFO),
2801 MAP(CAPATH),
2802 IMM(FILETIME),
2803 IMM(MAXREDIRS),
2804 IMM(MAXCONNECTS),
2805 IMM(CLOSEPOLICY),
2806 IMM(FRESH_CONNECT),
2807 IMM(FORBID_REUSE),
2808 MAP(RANDOM_FILE),
2809 MAP(EGDSOCKET),
2810 IMM(CONNECTTIMEOUT),
2811 IMM(HTTPGET),
2812 IMM(SSL_VERIFYHOST),
2813 MAP(COOKIEJAR),
2814 MAP(SSL_CIPHER_LIST),
2815 IMM(HTTP_VERSION),
2816 IMM(FTP_USE_EPSV),
2817 IMM(DNS_CACHE_TIMEOUT),
2818 IMM(DNS_USE_GLOBAL_CACHE),
2819 MAP(DEBUGFUNCTION),
2820 #if HAVE_DECL_CURLOPT_PRIVATE
2821 MAP(PRIVATE),
2822 #else
2823 MAP_NO(PRIVATE),
2824 #endif
2825 #if HAVE_DECL_CURLOPT_HTTP200ALIASES
2826 MAP(HTTP200ALIASES),
2827 #else
2828 MAP_NO(HTTP200ALIASES),
2829 #endif
2830 #if HAVE_DECL_CURLOPT_UNRESTRICTED_AUTH
2831 IMM(UNRESTRICTED_AUTH),
2832 #else
2833 IMM_NO(UNRESTRICTED_AUTH),
2834 #endif
2835 #if HAVE_DECL_CURLOPT_FTP_USE_EPRT
2836 IMM(FTP_USE_EPRT),
2837 #else
2838 IMM_NO(FTP_USE_EPRT),
2839 #endif
2840 #if HAVE_DECL_CURLOPT_HTTPAUTH
2841 IMM(HTTPAUTH),
2842 #else
2843 IMM_NO(HTTPAUTH),
2844 #endif
2845 #if HAVE_DECL_CURLOPT_FTP_CREATE_MISSING_DIRS
2846 IMM(FTP_CREATE_MISSING_DIRS),
2847 #else
2848 IMM_NO(FTP_CREATE_MISSING_DIRS),
2849 #endif
2850 #if HAVE_DECL_CURLOPT_PROXYAUTH
2851 IMM(PROXYAUTH),
2852 #else
2853 IMM_NO(PROXYAUTH),
2854 #endif
2855 #if HAVE_DECL_CURLOPT_FTP_RESPONSE_TIMEOUT
2856 IMM(FTP_RESPONSE_TIMEOUT),
2857 #else
2858 IMM_NO(FTP_RESPONSE_TIMEOUT),
2859 #endif
2860 #if HAVE_DECL_CURLOPT_IPRESOLVE
2861 IMM(IPRESOLVE),
2862 #else
2863 IMM_NO(IPRESOLVE),
2864 #endif
2865 #if HAVE_DECL_CURLOPT_MAXFILESIZE
2866 IMM(MAXFILESIZE),
2867 #else
2868 IMM_NO(MAXFILESIZE),
2869 #endif
2870 #if HAVE_DECL_CURLOPT_INFILESIZE_LARGE
2871 IMM(INFILESIZE_LARGE),
2872 #else
2873 IMM_NO(INFILESIZE_LARGE),
2874 #endif
2875 #if HAVE_DECL_CURLOPT_RESUME_FROM_LARGE
2876 IMM(RESUME_FROM_LARGE),
2877 #else
2878 IMM_NO(RESUME_FROM_LARGE),
2879 #endif
2880 #if HAVE_DECL_CURLOPT_MAXFILESIZE_LARGE
2881 IMM(MAXFILESIZE_LARGE),
2882 #else
2883 IMM_NO(MAXFILESIZE_LARGE),
2884 #endif
2885 #if HAVE_DECL_CURLOPT_NETRC_FILE
2886 MAP(NETRC_FILE),
2887 #else
2888 MAP_NO(NETRC_FILE),
2889 #endif
2890 #if HAVE_DECL_CURLOPT_FTP_SSL
2891 IMM(FTP_SSL),
2892 #else
2893 IMM_NO(FTP_SSL),
2894 #endif
2895 #if HAVE_DECL_CURLOPT_POSTFIELDSIZE_LARGE
2896 IMM(POSTFIELDSIZE_LARGE),
2897 #else
2898 IMM_NO(POSTFIELDSIZE_LARGE),
2899 #endif
2900 #if HAVE_DECL_CURLOPT_TCP_NODELAY
2901 IMM(TCP_NODELAY),
2902 #else
2903 IMM_NO(TCP_NODELAY),
2904 #endif
2905 #if HAVE_DECL_CURLOPT_FTPSSLAUTH
2906 IMM(FTPSSLAUTH),
2907 #else
2908 IMM_NO(FTPSSLAUTH),
2909 #endif
2910 #if HAVE_DECL_CURLOPT_IOCTLFUNCTION
2911 MAP(IOCTLFUNCTION),
2912 #else
2913 MAP_NO(IOCTLFUNCTION),
2914 #endif
2915 #if HAVE_DECL_CURLOPT_FTP_ACCOUNT
2916 MAP(FTP_ACCOUNT),
2917 #else
2918 MAP_NO(FTP_ACCOUNT),
2919 #endif
2920 #if HAVE_DECL_CURLOPT_COOKIELIST
2921 MAP(COOKIELIST),
2922 #else
2923 MAP_NO(COOKIELIST),
2924 #endif
2925 #if HAVE_DECL_CURLOPT_IGNORE_CONTENT_LENGTH
2926 IMM(IGNORE_CONTENT_LENGTH),
2927 #else
2928 IMM_NO(IGNORE_CONTENT_LENGTH),
2929 #endif
2930 #if HAVE_DECL_CURLOPT_FTP_SKIP_PASV_IP
2931 IMM(FTP_SKIP_PASV_IP),
2932 #else
2933 IMM_NO(FTP_SKIP_PASV_IP),
2934 #endif
2935 #if HAVE_DECL_CURLOPT_FTP_FILEMETHOD
2936 IMM(FTP_FILEMETHOD),
2937 #else
2938 IMM_NO(FTP_FILEMETHOD),
2939 #endif
2940 #if HAVE_DECL_CURLOPT_LOCALPORT
2941 IMM(LOCALPORT),
2942 #else
2943 IMM_NO(LOCALPORT),
2944 #endif
2945 #if HAVE_DECL_CURLOPT_LOCALPORTRANGE
2946 IMM(LOCALPORTRANGE),
2947 #else
2948 IMM_NO(LOCALPORTRANGE),
2949 #endif
2950 #if HAVE_DECL_CURLOPT_CONNECT_ONLY
2951 IMM(CONNECT_ONLY),
2952 #else
2953 IMM_NO(CONNECT_ONLY),
2954 #endif
2955 #if HAVE_DECL_CURLOPT_MAX_SEND_SPEED_LARGE
2956 IMM(MAX_SEND_SPEED_LARGE),
2957 #else
2958 IMM_NO(MAX_SEND_SPEED_LARGE),
2959 #endif
2960 #if HAVE_DECL_CURLOPT_MAX_RECV_SPEED_LARGE
2961 IMM(MAX_RECV_SPEED_LARGE),
2962 #else
2963 IMM_NO(MAX_RECV_SPEED_LARGE),
2964 #endif
2965 #if HAVE_DECL_CURLOPT_FTP_ALTERNATIVE_TO_USER
2966 MAP(FTP_ALTERNATIVE_TO_USER),
2967 #else
2968 MAP_NO(FTP_ALTERNATIVE_TO_USER),
2969 #endif
2970 #if HAVE_DECL_CURLOPT_SSL_SESSIONID_CACHE
2971 IMM(SSL_SESSIONID_CACHE),
2972 #else
2973 IMM_NO(SSL_SESSIONID_CACHE),
2974 #endif
2975 #if HAVE_DECL_CURLOPT_SSH_AUTH_TYPES
2976 IMM(SSH_AUTH_TYPES),
2977 #else
2978 IMM_NO(SSH_AUTH_TYPES),
2979 #endif
2980 #if HAVE_DECL_CURLOPT_SSH_PUBLIC_KEYFILE
2981 MAP(SSH_PUBLIC_KEYFILE),
2982 #else
2983 MAP_NO(SSH_PUBLIC_KEYFILE),
2984 #endif
2985 #if HAVE_DECL_CURLOPT_SSH_PRIVATE_KEYFILE
2986 MAP(SSH_PRIVATE_KEYFILE),
2987 #else
2988 MAP_NO(SSH_PRIVATE_KEYFILE),
2989 #endif
2990 #if HAVE_DECL_CURLOPT_FTP_SSL_CCC
2991 IMM(FTP_SSL_CCC),
2992 #else
2993 IMM_NO(FTP_SSL_CCC),
2994 #endif
2995 #if HAVE_DECL_CURLOPT_TIMEOUT_MS
2996 IMM(TIMEOUT_MS),
2997 #else
2998 IMM_NO(TIMEOUT_MS),
2999 #endif
3000 #if HAVE_DECL_CURLOPT_CONNECTTIMEOUT_MS
3001 IMM(CONNECTTIMEOUT_MS),
3002 #else
3003 IMM_NO(CONNECTTIMEOUT_MS),
3004 #endif
3005 #if HAVE_DECL_CURLOPT_HTTP_TRANSFER_DECODING
3006 IMM(HTTP_TRANSFER_DECODING),
3007 #else
3008 IMM_NO(HTTP_TRANSFER_DECODING),
3009 #endif
3010 #if HAVE_DECL_CURLOPT_HTTP_CONTENT_DECODING
3011 IMM(HTTP_CONTENT_DECODING),
3012 #else
3013 IMM_NO(HTTP_CONTENT_DECODING),
3014 #endif
3015 #if HAVE_DECL_CURLOPT_NEW_FILE_PERMS
3016 IMM(NEW_FILE_PERMS),
3017 #else
3018 IMM_NO(NEW_FILE_PERMS),
3019 #endif
3020 #if HAVE_DECL_CURLOPT_NEW_DIRECTORY_PERMS
3021 IMM(NEW_DIRECTORY_PERMS),
3022 #else
3023 IMM_NO(NEW_DIRECTORY_PERMS),
3024 #endif
3025 #if HAVE_DECL_CURLOPT_POST301
3026 IMM(POST301),
3027 #else
3028 IMM_NO(POST301),
3029 #endif
3030 #if HAVE_DECL_CURLOPT_SSH_HOST_PUBLIC_KEY_MD5
3031 MAP(SSH_HOST_PUBLIC_KEY_MD5),
3032 #else
3033 MAP_NO(SSH_HOST_PUBLIC_KEY_MD5),
3034 #endif
3035 #if HAVE_DECL_CURLOPT_COPYPOSTFIELDS
3036 MAP(COPYPOSTFIELDS),
3037 #else
3038 MAP_NO(COPYPOSTFIELDS),
3039 #endif
3040 #if HAVE_DECL_CURLOPT_PROXY_TRANSFER_MODE
3041 IMM(PROXY_TRANSFER_MODE),
3042 #else
3043 IMM_NO(PROXY_TRANSFER_MODE),
3044 #endif
3045 #if HAVE_DECL_CURLOPT_SEEKFUNCTION
3046 MAP(SEEKFUNCTION),
3047 #else
3048 MAP_NO(SEEKFUNCTION),
3049 #endif
3050 #if HAVE_DECL_CURLOPT_AUTOREFERER
3051 IMM(AUTOREFERER),
3052 #else
3053 IMM_NO(AUTOREFERER),
3054 #endif
3055 #if HAVE_DECL_CURLOPT_OPENSOCKETFUNCTION
3056 MAP(OPENSOCKETFUNCTION),
3057 #else
3058 MAP_NO(OPENSOCKETFUNCTION),
3059 #endif
3060 #if HAVE_DECL_CURLOPT_PROXYTYPE
3061 IMM(PROXYTYPE),
3062 #else
3063 IMM_NO(PROXYTYPE),
3064 #endif
3065 #if HAVE_DECL_CURLOPT_PROTOCOLS
3066 IMM(PROTOCOLS),
3067 #else
3068 IMM_NO(PROTOCOLS),
3069 #endif
3070 #if HAVE_DECL_CURLOPT_REDIR_PROTOCOLS
3071 IMM(REDIR_PROTOCOLS),
3072 #else
3073 IMM_NO(REDIR_PROTOCOLS),
3074 #endif
3075 #if HAVE_DECL_CURLOPT_RESOLVE
3076 MAP(RESOLVE),
3077 #else
3078 MAP_NO(RESOLVE),
3079 #endif
3080 #if HAVE_DECL_CURLOPT_DNS_SERVERS
3081 MAP(DNS_SERVERS),
3082 #else
3083 MAP_NO(DNS_SERVERS),
3084 #endif
3085 #if HAVE_DECL_CURLOPT_MAIL_FROM
3086 MAP(MAIL_FROM),
3087 #else
3088 MAP_NO(MAIL_FROM),
3089 #endif
3090 #if HAVE_DECL_CURLOPT_MAIL_RCPT
3091 MAP(MAIL_RCPT),
3092 #else
3093 MAP_NO(MAIL_RCPT),
3094 #endif
3097 static Connection *duplicateConnection(Connection *original)
3099 Connection *connection = NULL;
3100 CURL* h = NULL;
3101 size_t i = 0;
3102 CURLOptionMapping* this = NULL;
3104 caml_enter_blocking_section();
3105 h = curl_easy_duphandle(original->connection);
3106 caml_leave_blocking_section();
3108 connection = allocConnection(h);
3110 for (i = 0; i < sizeof(implementedOptionMap)/sizeof(CURLOptionMapping); i++)
3112 this = &implementedOptionMap[i];
3113 if (-1 == this->ocamlValue) continue;
3114 if (this->optionHandler && (Field(original->ocamlValues, this->ocamlValue) != Val_unit))
3116 this->optionHandler(connection, Field(original->ocamlValues, this->ocamlValue));
3120 return connection;
3123 CAMLprim value helper_curl_easy_setopt(value conn, value option)
3125 CAMLparam2(conn, option);
3126 CAMLlocal1(data);
3127 Connection *connection = Connection_val(conn);
3128 CURLOptionMapping* thisOption = NULL;
3129 static value* exception = NULL;
3131 checkConnection(connection);
3133 if (!Is_block(option))
3134 failwith("Not a block");
3136 if (Wosize_val(option) < 1)
3137 failwith("Insufficient data in block");
3139 data = Field(option, 0);
3141 if (Tag_val(option) < sizeof(implementedOptionMap)/sizeof(CURLOptionMapping))
3143 thisOption = &implementedOptionMap[Tag_val(option)];
3144 if (thisOption->optionHandler)
3145 thisOption->optionHandler(connection, data);
3146 else
3148 if (NULL == exception)
3150 exception = caml_named_value("Curl.NotImplemented");
3151 if (NULL == exception) caml_invalid_argument("Curl.NotImplemented");
3154 caml_raise_with_string(*exception, thisOption->name);
3157 else
3158 failwith("Invalid CURLOPT Option");
3160 CAMLreturn(Val_unit);
3164 ** curl_easy_perform helper function
3167 CAMLprim value helper_curl_easy_perform(value conn)
3169 CAMLparam1(conn);
3170 CURLcode result = CURLE_OK;
3171 Connection *connection = Connection_val(conn);
3173 checkConnection(connection);
3175 enter_blocking_section();
3176 result = curl_easy_perform(connection->connection);
3177 leave_blocking_section();
3179 if (result != CURLE_OK)
3180 raiseError(connection, result);
3182 CAMLreturn(Val_unit);
3186 ** curl_easy_cleanup helper function
3189 CAMLprim value helper_curl_easy_cleanup(value conn)
3191 CAMLparam1(conn);
3192 Connection *connection = Connection_val(conn);
3194 checkConnection(connection);
3196 removeConnection(connection, 0);
3198 CAMLreturn(Val_unit);
3202 ** curl_easy_duphandle helper function
3205 CAMLprim value helper_curl_easy_duphandle(value conn)
3207 CAMLparam1(conn);
3208 CAMLlocal1(result);
3209 Connection *connection = Connection_val(conn);
3211 checkConnection(connection);
3213 result = caml_curl_alloc(duplicateConnection(connection));
3215 CAMLreturn(result);
3219 ** curl_easy_getinfo helper function
3222 enum GetInfoResultType {
3223 StringValue, LongValue, DoubleValue, StringListValue
3226 value convertStringList(struct curl_slist *slist)
3228 CAMLparam0();
3229 CAMLlocal3(result, current, next);
3230 struct curl_slist *p = slist;
3232 result = Val_int(0);
3233 current = Val_int(0);
3234 next = Val_int(0);
3236 while (p != NULL)
3238 next = alloc_tuple(2);
3239 Store_field(next, 0, copy_string(p->data));
3240 Store_field(next, 1, Val_int(0));
3242 if (result == Val_int(0))
3243 result = next;
3245 if (current != Val_int(0))
3246 Store_field(current, 1, next);
3248 current = next;
3250 p = p->next;
3253 curl_slist_free_all(slist);
3255 CAMLreturn(result);
3258 CAMLprim value helper_curl_easy_getinfo(value conn, value option)
3260 CAMLparam2(conn, option);
3261 CAMLlocal1(result);
3262 CURLcode curlResult;
3263 Connection *connection = Connection_val(conn);
3264 enum GetInfoResultType resultType;
3265 char *strValue = NULL;
3266 double doubleValue;
3267 long longValue;
3268 struct curl_slist *stringListValue = NULL;
3270 checkConnection(connection);
3272 switch(Long_val(option))
3274 #if HAVE_DECL_CURLINFO_EFFECTIVE_URL
3275 case 0: /* CURLINFO_EFFECTIVE_URL */
3276 resultType = StringValue;
3278 curlResult = curl_easy_getinfo(connection->connection,
3279 CURLINFO_EFFECTIVE_URL,
3280 &strValue);
3281 break;
3282 #else
3283 #pragma message("libcurl does not provide CURLINFO_EFFECTIVE_URL")
3284 #endif
3286 #if HAVE_DECL_CURLINFO_RESPONSE_CODE || HAVE_DECL_CURLINFO_HTTP_CODE
3287 case 1: /* CURLINFO_HTTP_CODE */
3288 case 2: /* CURLINFO_RESPONSE_CODE */
3289 #if HAVE_DECL_CURLINFO_RESPONSE_CODE
3290 resultType = LongValue;
3292 curlResult = curl_easy_getinfo(connection->connection,
3293 CURLINFO_RESPONSE_CODE,
3294 &longValue);
3295 #else
3296 resultType = LongValue;
3298 curlResult = curl_easy_getinfo(connection->connection,
3299 CURLINFO_HTTP_CODE,
3300 &longValue);
3301 #endif
3302 break;
3303 #endif
3305 #if HAVE_DECL_CURLINFO_TOTAL_TIME
3306 case 3: /* CURLINFO_TOTAL_TIME */
3307 resultType = DoubleValue;
3309 curlResult = curl_easy_getinfo(connection->connection,
3310 CURLINFO_TOTAL_TIME,
3311 &doubleValue);
3312 break;
3313 #endif
3315 #if HAVE_DECL_CURLINFO_NAMELOOKUP_TIME
3316 case 4: /* CURLINFO_NAMELOOKUP_TIME */
3317 resultType = DoubleValue;
3319 curlResult = curl_easy_getinfo(connection->connection,
3320 CURLINFO_NAMELOOKUP_TIME,
3321 &doubleValue);
3322 break;
3323 #endif
3325 #if HAVE_DECL_CURLINFO_CONNECT_TIME
3326 case 5: /* CURLINFO_CONNECT_TIME */
3327 resultType = DoubleValue;
3329 curlResult = curl_easy_getinfo(connection->connection,
3330 CURLINFO_CONNECT_TIME,
3331 &doubleValue);
3332 break;
3333 #endif
3335 #if HAVE_DECL_CURLINFO_PRETRANSFER_TIME
3336 case 6: /* CURLINFO_PRETRANSFER_TIME */
3337 resultType = DoubleValue;
3339 curlResult = curl_easy_getinfo(connection->connection,
3340 CURLINFO_PRETRANSFER_TIME,
3341 &doubleValue);
3342 break;
3343 #endif
3345 #if HAVE_DECL_CURLINFO_SIZE_UPLOAD
3346 case 7: /* CURLINFO_SIZE_UPLOAD */
3347 resultType = DoubleValue;
3349 curlResult = curl_easy_getinfo(connection->connection,
3350 CURLINFO_SIZE_UPLOAD,
3351 &doubleValue);
3352 break;
3353 #endif
3355 #if HAVE_DECL_CURLINFO_SIZE_DOWNLOAD
3356 case 8: /* CURLINFO_SIZE_DOWNLOAD */
3357 resultType = DoubleValue;
3359 curlResult = curl_easy_getinfo(connection->connection,
3360 CURLINFO_SIZE_DOWNLOAD,
3361 &doubleValue);
3362 break;
3363 #endif
3365 #if HAVE_DECL_CURLINFO_SPEED_DOWNLOAD
3366 case 9: /* CURLINFO_SPEED_DOWNLOAD */
3367 resultType = DoubleValue;
3369 curlResult = curl_easy_getinfo(connection->connection,
3370 CURLINFO_SPEED_DOWNLOAD,
3371 &doubleValue);
3372 break;
3373 #endif
3375 #if HAVE_DECL_CURLINFO_SPEED_UPLOAD
3376 case 10: /* CURLINFO_SPEED_UPLOAD */
3377 resultType = DoubleValue;
3379 curlResult = curl_easy_getinfo(connection->connection,
3380 CURLINFO_SPEED_UPLOAD,
3381 &doubleValue);
3382 break;
3384 #endif
3386 #if HAVE_DECL_CURLINFO_HEADER_SIZE
3387 case 11: /* CURLINFO_HEADER_SIZE */
3388 resultType = LongValue;
3390 curlResult = curl_easy_getinfo(connection->connection,
3391 CURLINFO_HEADER_SIZE,
3392 &longValue);
3393 break;
3394 #endif
3396 #if HAVE_DECL_CURLINFO_REQUEST_SIZE
3397 case 12: /* CURLINFO_REQUEST_SIZE */
3398 resultType = LongValue;
3400 curlResult = curl_easy_getinfo(connection->connection,
3401 CURLINFO_REQUEST_SIZE,
3402 &longValue);
3403 break;
3404 #endif
3406 #if HAVE_DECL_CURLINFO_SSL_VERIFYRESULT
3407 case 13: /* CURLINFO_SSL_VERIFYRESULT */
3408 resultType = LongValue;
3410 curlResult = curl_easy_getinfo(connection->connection,
3411 CURLINFO_SSL_VERIFYRESULT,
3412 &longValue);
3413 break;
3414 #endif
3416 #if HAVE_DECL_CURLINFO_FILETIME
3417 case 14: /* CURLINFO_FILETIME */
3418 resultType = DoubleValue;
3420 curlResult = curl_easy_getinfo(connection->connection,
3421 CURLINFO_FILETIME,
3422 &longValue);
3424 doubleValue = longValue;
3425 break;
3426 #endif
3428 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_DOWNLOAD
3429 case 15: /* CURLINFO_CONTENT_LENGTH_DOWNLOAD */
3430 resultType = DoubleValue;
3432 curlResult = curl_easy_getinfo(connection->connection,
3433 CURLINFO_CONTENT_LENGTH_DOWNLOAD,
3434 &doubleValue);
3435 break;
3436 #endif
3438 #if HAVE_DECL_CURLINFO_CONTENT_LENGTH_UPLOAD
3439 case 16: /* CURLINFO_CONTENT_LENGTH_UPLOAD */
3440 resultType = DoubleValue;
3442 curlResult = curl_easy_getinfo(connection->connection,
3443 CURLINFO_CONTENT_LENGTH_UPLOAD,
3444 &doubleValue);
3445 break;
3446 #endif
3448 #if HAVE_DECL_CURLINFO_STARTTRANSFER_TIME
3449 case 17: /* CURLINFO_STARTTRANSFER_TIME */
3450 resultType = DoubleValue;
3452 curlResult = curl_easy_getinfo(connection->connection,
3453 CURLINFO_STARTTRANSFER_TIME,
3454 &doubleValue);
3455 break;
3456 #endif
3458 #if HAVE_DECL_CURLINFO_CONTENT_TYPE
3459 case 18: /* CURLINFO_CONTENT_TYPE */
3460 resultType = StringValue;
3462 curlResult = curl_easy_getinfo(connection->connection,
3463 CURLINFO_CONTENT_TYPE,
3464 &strValue);
3465 break;
3466 #endif
3468 #if HAVE_DECL_CURLINFO_REDIRECT_TIME
3469 case 19: /* CURLINFO_REDIRECT_TIME */
3470 resultType = DoubleValue;
3472 curlResult = curl_easy_getinfo(connection->connection,
3473 CURLINFO_REDIRECT_TIME,
3474 &doubleValue);
3475 break;
3476 #endif
3478 #if HAVE_DECL_CURLINFO_REDIRECT_COUNT
3479 case 20: /* CURLINFO_REDIRECT_COUNT */
3480 resultType = LongValue;
3482 curlResult = curl_easy_getinfo(connection->connection,
3483 CURLINFO_REDIRECT_COUNT,
3484 &longValue);
3485 break;
3486 #endif
3488 #if HAVE_DECL_CURLINFO_PRIVATE
3489 case 21: /* CURLINFO_PRIVATE */
3490 resultType = StringValue;
3492 curlResult = curl_easy_getinfo(connection->connection,
3493 CURLINFO_PRIVATE,
3494 &strValue);
3495 break;
3496 #endif
3498 #if HAVE_DECL_CURLINFO_HTTP_CONNECTCODE
3499 case 22: /* CURLINFO_HTTP_CONNECTCODE */
3500 resultType = LongValue;
3502 curlResult = curl_easy_getinfo(connection->connection,
3503 CURLINFO_HTTP_CONNECTCODE,
3504 &longValue);
3505 break;
3506 #endif
3508 #if HAVE_DECL_CURLINFO_HTTPAUTH_AVAIL
3509 case 23: /* CURLINFO_HTTPAUTH_AVAIL */
3510 resultType = LongValue;
3512 curlResult = curl_easy_getinfo(connection->connection,
3513 CURLINFO_HTTPAUTH_AVAIL,
3514 &longValue);
3515 break;
3516 #endif
3518 #if HAVE_DECL_CURLINFO_PROXYAUTH_AVAIL
3519 case 24: /* CURLINFO_PROXYAUTH_AVAIL */
3520 resultType = LongValue;
3522 curlResult = curl_easy_getinfo(connection->connection,
3523 CURLINFO_PROXYAUTH_AVAIL,
3524 &longValue);
3525 break;
3526 #endif
3528 #if HAVE_DECL_CURLINFO_OS_ERRNO
3529 case 25: /* CURLINFO_OS_ERRNO */
3530 resultType = LongValue;
3532 curlResult = curl_easy_getinfo(connection->connection,
3533 CURLINFO_OS_ERRNO,
3534 &longValue);
3535 break;
3536 #endif
3538 #if HAVE_DECL_CURLINFO_NUM_CONNECTS
3539 case 26: /* CURLINFO_NUM_CONNECTS */
3540 resultType = LongValue;
3542 curlResult = curl_easy_getinfo(connection->connection,
3543 CURLINFO_NUM_CONNECTS,
3544 &longValue);
3545 break;
3546 #endif
3548 #if HAVE_DECL_CURLINFO_SSL_ENGINES
3549 case 27: /* CURLINFO_SSL_ENGINES */
3550 resultType = StringListValue;
3552 curlResult = curl_easy_getinfo(connection->connection,
3553 CURLINFO_SSL_ENGINES,
3554 &stringListValue);
3555 break;
3556 #endif
3558 #if HAVE_DECL_CURLINFO_COOKIELIST
3559 case 28: /* CURLINFO_COOKIELIST */
3560 resultType = StringListValue;
3562 curlResult = curl_easy_getinfo(connection->connection,
3563 CURLINFO_COOKIELIST,
3564 &stringListValue);
3565 break;
3566 #endif
3568 #if HAVE_DECL_CURLINFO_LASTSOCKET
3569 case 29: /* CURLINFO_LASTSOCKET */
3570 resultType = LongValue;
3572 curlResult = curl_easy_getinfo(connection->connection,
3573 CURLINFO_LASTSOCKET,
3574 &longValue);
3575 break;
3576 #endif
3578 #if HAVE_DECL_CURLINFO_FTP_ENTRY_PATH
3579 case 30: /* CURLINFO_FTP_ENTRY_PATH */
3580 resultType = StringValue;
3582 curlResult = curl_easy_getinfo(connection->connection,
3583 CURLINFO_FTP_ENTRY_PATH,
3584 &strValue);
3585 break;
3586 #endif
3588 #if HAVE_DECL_CURLINFO_REDIRECT_URL
3589 case 31: /* CURLINFO_REDIRECT_URL */
3590 resultType = StringValue;
3592 curlResult = curl_easy_getinfo(connection->connection,
3593 CURLINFO_REDIRECT_URL,
3594 &strValue);
3595 break;
3596 #else
3597 #pragma message("libcurl does not provide CURLINFO_REDIRECT_URL")
3598 #endif
3600 #if HAVE_DECL_CURLINFO_PRIMARY_IP
3601 case 32: /* CURLINFO_PRIMARY_IP */
3602 resultType = StringValue;
3604 curlResult = curl_easy_getinfo(connection->connection,
3605 CURLINFO_PRIMARY_IP,
3606 &strValue);
3607 break;
3608 #else
3609 #pragma message("libcurl does not provide CURLINFO_PRIMARY_IP")
3610 #endif
3612 #if HAVE_DECL_CURLINFO_LOCAL_IP
3613 case 33: /* CURLINFO_LOCAL_IP */
3614 resultType = StringValue;
3616 curlResult = curl_easy_getinfo(connection->connection,
3617 CURLINFO_LOCAL_IP,
3618 &strValue);
3619 break;
3620 #else
3621 #pragma message("libcurl does not provide CURLINFO_LOCAL_IP")
3622 #endif
3624 #if HAVE_DECL_CURLINFO_LOCAL_PORT
3625 case 34: /* CURLINFO_LOCAL_PORT */
3626 resultType = LongValue;
3628 curlResult = curl_easy_getinfo(connection->connection,
3629 CURLINFO_LOCAL_PORT,
3630 &longValue);
3631 break;
3632 #else
3633 #pragma message("libcurl does not provide CURLINFO_LOCAL_PORT")
3634 #endif
3636 #if HAVE_DECL_CURLINFO_CONDITION_UNMET
3637 case 35: /* CURLINFO_CONDITION_UNMET */
3638 resultType = LongValue;
3640 curlResult = curl_easy_getinfo(connection->connection,
3641 CURLINFO_CONDITION_UNMET,
3642 &longValue);
3643 break;
3644 #else
3645 #pragma message("libcurl does not provide CURLINFO_CONDITION_UNMET")
3646 #endif
3648 default:
3649 failwith("Invalid CURLINFO Option");
3650 break;
3653 if (curlResult != CURLE_OK)
3654 raiseError(connection, curlResult);
3656 switch (resultType)
3658 case StringValue:
3659 result = alloc(1, StringValue);
3660 Store_field(result, 0, copy_string(strValue?strValue:""));
3661 break;
3663 case LongValue:
3664 result = alloc(1, LongValue);
3665 Store_field(result, 0, Val_long(longValue));
3666 break;
3668 case DoubleValue:
3669 result = alloc(1, DoubleValue);
3670 Store_field(result, 0, copy_double(doubleValue));
3671 break;
3673 case StringListValue:
3674 result = alloc(1, StringListValue);
3675 Store_field(result, 0, convertStringList(stringListValue));
3676 break;
3679 CAMLreturn(result);
3683 ** curl_escape helper function
3686 CAMLprim value helper_curl_escape(value str)
3688 CAMLparam1(str);
3689 CAMLlocal1(result);
3690 char *curlResult;
3692 curlResult = curl_escape(String_val(str), string_length(str));
3693 result = copy_string(curlResult);
3694 free(curlResult);
3696 CAMLreturn(result);
3700 ** curl_unescape helper function
3703 CAMLprim value helper_curl_unescape(value str)
3705 CAMLparam1(str);
3706 CAMLlocal1(result);
3707 char *curlResult;
3709 curlResult = curl_unescape(String_val(str), string_length(str));
3710 result = copy_string(curlResult);
3711 free(curlResult);
3713 CAMLreturn(result);
3717 ** curl_getdate helper function
3720 CAMLprim value helper_curl_getdate(value str, value now)
3722 CAMLparam2(str, now);
3723 CAMLlocal1(result);
3724 time_t curlResult;
3725 time_t curlNow;
3727 curlNow = (time_t)Double_val(now);
3728 curlResult = curl_getdate(String_val(str), &curlNow);
3729 result = copy_double((double)curlResult);
3731 CAMLreturn(result);
3735 ** curl_version helper function
3738 CAMLprim value helper_curl_version(void)
3740 CAMLparam0();
3741 CAMLlocal1(result);
3742 char *str;
3744 str = curl_version();
3745 result = copy_string(str);
3747 CAMLreturn(result);
3750 struct CURLVersionBitsMapping
3752 int code;
3753 char *name;
3756 struct CURLVersionBitsMapping versionBitsMap[] =
3758 {CURL_VERSION_IPV6, "ipv6"},
3759 {CURL_VERSION_KERBEROS4, "kerberos4"},
3760 {CURL_VERSION_SSL, "ssl"},
3761 {CURL_VERSION_LIBZ, "libz"},
3762 {CURL_VERSION_NTLM, "ntlm"},
3763 {CURL_VERSION_GSSNEGOTIATE, "gssnegotiate"},
3764 {CURL_VERSION_DEBUG, "debug"},
3765 {CURL_VERSION_CURLDEBUG, "curldebug"},
3766 {CURL_VERSION_ASYNCHDNS, "asynchdns"},
3767 {CURL_VERSION_SPNEGO, "spnego"},
3768 {CURL_VERSION_LARGEFILE, "largefile"},
3769 {CURL_VERSION_IDN, "idn"},
3770 {CURL_VERSION_SSPI, "sspi"},
3771 {CURL_VERSION_CONV, "conv"},
3772 #if HAVE_DECL_CURL_VERSION_TLSAUTH_SRP
3773 {CURL_VERSION_TLSAUTH_SRP, "srp"},
3774 #endif
3775 #if HAVE_DECL_CURL_VERSION_NTLM_WB
3776 {CURL_VERSION_NTLM_WB, "wb"},
3777 #endif
3780 CAMLprim value caml_curl_version_info(value unit)
3782 CAMLparam1(unit);
3783 CAMLlocal4(v, vlist, vnum, vfeatures);
3784 const char* const* p = NULL;
3785 size_t i = 0;
3787 curl_version_info_data* data = curl_version_info(CURLVERSION_NOW);
3788 if (NULL == data) caml_failwith("curl_version_info");
3790 vlist = Val_emptylist;
3791 for (p = data->protocols; NULL != *p; p++)
3793 vlist = Val_cons(vlist, caml_copy_string(*p));
3796 vfeatures = Val_emptylist;
3797 for (i = 0; i < sizeof(versionBitsMap)/sizeof(versionBitsMap[0]); i++)
3799 if (0 != (versionBitsMap[i].code & data->features))
3800 vfeatures = Val_cons(vfeatures, caml_copy_string(versionBitsMap[i].name));
3803 vnum = caml_alloc_tuple(3);
3804 Store_field(vnum,0,Val_int(0xFF & (data->version_num >> 16)));
3805 Store_field(vnum,1,Val_int(0xFF & (data->version_num >> 8)));
3806 Store_field(vnum,2,Val_int(0xFF & (data->version_num)));
3808 v = caml_alloc_tuple(12);
3809 Store_field(v,0,caml_copy_string(data->version));
3810 Store_field(v,1,vnum);
3811 Store_field(v,2,caml_copy_string(data->host));
3812 Store_field(v,3,vfeatures);
3813 Store_field(v,4,data->ssl_version ? Val_some(caml_copy_string(data->ssl_version)) : Val_none);
3814 Store_field(v,5,data->libz_version ? Val_some(caml_copy_string(data->libz_version)) : Val_none);
3815 Store_field(v,6,vlist);
3816 Store_field(v,7,caml_copy_string((data->age >= 1 && data->ares) ? data->ares : ""));
3817 Store_field(v,8,Val_int((data->age >= 1) ? data->ares_num : 0));
3818 Store_field(v,9,caml_copy_string((data->age >= 2 && data->libidn) ? data->libidn : ""));
3819 Store_field(v,10,Val_int((data->age >= 3) ? data->iconv_ver_num : 0));
3820 Store_field(v,11,caml_copy_string((data->age >= 3 && data->libssh_version) ? data->libssh_version : ""));
3822 CAMLreturn(v);
3825 CAMLprim value caml_curl_pause(value conn, value opts)
3827 CAMLparam2(conn, opts);
3828 CAMLlocal4(v, vlist, vnum, vfeatures);
3829 Connection *connection = Connection_val(conn);
3830 int bitmask = 0;
3831 CURLcode result;
3833 while (Val_emptylist != opts)
3835 switch (Int_val(Field(opts,0)))
3837 case 0: bitmask |= CURLPAUSE_SEND; break;
3838 case 1: bitmask |= CURLPAUSE_RECV; break;
3839 case 2: bitmask |= CURLPAUSE_ALL; break;
3840 default: caml_failwith("wrong pauseOption");
3842 opts = Field(opts,1);
3845 result = curl_easy_pause(connection->connection,bitmask);
3846 if (result != CURLE_OK)
3847 raiseError(connection, result);
3849 CAMLreturn(Val_unit);
3853 * Curl multi stack support
3855 * Exported thin wrappers for libcurl are prefixed with caml_curl_multi_.
3856 * Other exported functions are prefixed with caml_curlm_, some of them
3857 * can/should be decomposed into smaller parts.
3860 struct ml_multi_handle
3862 CURLM* handle;
3863 value values; /* callbacks */
3866 enum
3868 curlmopt_socket_function,
3869 curlmopt_timer_function,
3871 /* last, not used */
3872 multi_values_total
3875 typedef struct ml_multi_handle ml_multi_handle;
3877 #define Multi_val(v) (*(ml_multi_handle**)Data_custom_val(v))
3878 #define CURLM_val(v) (Multi_val(v)->handle)
3880 static struct custom_operations curl_multi_ops = {
3881 "ygrek.curl_multi",
3882 custom_finalize_default,
3883 custom_compare_default,
3884 custom_hash_default,
3885 custom_serialize_default,
3886 custom_deserialize_default,
3887 #if defined(custom_compare_ext_default)
3888 custom_compare_ext_default,
3889 #endif
3892 CAMLprim value caml_curl_multi_init(value unit)
3894 CAMLparam1(unit);
3895 CAMLlocal1(v);
3896 ml_multi_handle* multi = (ml_multi_handle*)caml_stat_alloc(sizeof(ml_multi_handle));
3897 CURLM* h = curl_multi_init();
3899 if (!h)
3901 caml_stat_free(multi);
3902 failwith("caml_curl_multi_init");
3905 multi->handle = h;
3906 multi->values = caml_alloc(multi_values_total, 0);
3907 caml_register_generational_global_root(&multi->values);
3909 v = caml_alloc_custom(&curl_multi_ops, sizeof(ml_multi_handle*), 0, 1);
3910 Multi_val(v) = multi;
3912 CAMLreturn(v);
3915 CAMLprim value caml_curl_multi_cleanup(value handle)
3917 CAMLparam1(handle);
3918 ml_multi_handle* h = Multi_val(handle);
3920 if (NULL == h)
3921 CAMLreturn(Val_unit);
3923 caml_remove_generational_global_root(&h->values);
3925 if (CURLM_OK != curl_multi_cleanup(h->handle))
3926 failwith("caml_curl_multi_cleanup");
3928 Multi_val(handle) = (ml_multi_handle*)NULL;
3930 CAMLreturn(Val_unit);
3933 static CURL* curlm_remove_finished(CURLM* multi_handle, CURLcode* result)
3935 int msgs_in_queue = 0;
3937 while (1)
3939 CURLMsg* msg = curl_multi_info_read(multi_handle, &msgs_in_queue);
3940 if (NULL == msg) return NULL;
3941 if (CURLMSG_DONE == msg->msg)
3943 CURL* easy_handle = msg->easy_handle;
3944 if (result) *result = msg->data.result;
3945 if (CURLM_OK != curl_multi_remove_handle(multi_handle, easy_handle))
3947 /*failwith("curlm_remove_finished");*/
3949 return easy_handle;
3954 CAMLprim value caml_curlm_remove_finished(value v_multi)
3956 CAMLparam1(v_multi);
3957 CAMLlocal2(v_easy, v_tuple);
3958 CURL* handle;
3959 CURLM* multi_handle;
3960 CURLcode result;
3961 Connection* conn = NULL;
3963 multi_handle = CURLM_val(v_multi);
3965 caml_enter_blocking_section();
3966 handle = curlm_remove_finished(multi_handle,&result);
3967 caml_leave_blocking_section();
3969 if (NULL == handle)
3971 CAMLreturn(Val_none);
3973 else
3975 conn = findConnection(handle);
3976 if (conn->curl_ERRORBUFFER != NULL)
3978 Store_field(Field(conn->ocamlValues, Ocaml_ERRORBUFFER), 0, caml_copy_string(conn->curl_ERRORBUFFER));
3980 conn->refcount--;
3981 /* NB: same handle, but different block */
3982 v_easy = caml_curl_alloc(conn);
3983 v_tuple = caml_alloc(2, 0);
3984 Store_field(v_tuple,0,v_easy);
3985 Store_field(v_tuple,1,Val_int(result)); /* CURLcode */
3986 CAMLreturn(Val_some(v_tuple));
3990 static int curlm_wait_data(CURLM* multi_handle)
3992 struct timeval timeout;
3993 CURLMcode ret;
3995 fd_set fdread;
3996 fd_set fdwrite;
3997 fd_set fdexcep;
3998 int maxfd = -1;
4000 FD_ZERO(&fdread);
4001 FD_ZERO(&fdwrite);
4002 FD_ZERO(&fdexcep);
4004 /* set a suitable timeout */
4005 timeout.tv_sec = 1;
4006 timeout.tv_usec = 0;
4008 /* get file descriptors from the transfers */
4009 ret = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
4011 if (ret == CURLM_OK && maxfd >= 0)
4013 int rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
4014 if (-1 != rc) return 0;
4016 return 1;
4019 CAMLprim value caml_curlm_wait_data(value v_multi)
4021 CAMLparam1(v_multi);
4022 int ret;
4023 CURLM* h = CURLM_val(v_multi);
4025 caml_enter_blocking_section();
4026 ret = curlm_wait_data(h);
4027 caml_leave_blocking_section();
4029 CAMLreturn(Val_bool(0 == ret));
4032 CAMLprim value caml_curl_multi_add_handle(value v_multi, value v_easy)
4034 CAMLparam2(v_multi,v_easy);
4035 CURLM* multi = CURLM_val(v_multi);
4036 Connection* conn = Connection_val(v_easy);
4038 /* prevent collection of OCaml value while the easy handle is used
4039 and may invoke callbacks registered on OCaml side */
4040 conn->refcount++;
4042 /* may invoke callbacks so need to be consistent with locks */
4043 caml_enter_blocking_section();
4044 if (CURLM_OK != curl_multi_add_handle(multi, conn->connection))
4046 conn->refcount--; /* not added, revert */
4047 caml_leave_blocking_section();
4048 failwith("caml_curl_multi_add_handle");
4050 caml_leave_blocking_section();
4052 CAMLreturn(Val_unit);
4055 CAMLprim value caml_curl_multi_remove_handle(value v_multi, value v_easy)
4057 CAMLparam2(v_multi,v_easy);
4058 CURLM* multi = CURLM_val(v_multi);
4059 Connection* conn = Connection_val(v_easy);
4061 /* may invoke callbacks so need to be consistent with locks */
4062 caml_enter_blocking_section();
4063 if (CURLM_OK != curl_multi_remove_handle(multi, conn->connection))
4065 caml_leave_blocking_section();
4066 failwith("caml_curl_multi_remove_handle");
4068 conn->refcount--;
4069 caml_leave_blocking_section();
4071 CAMLreturn(Val_unit);
4074 CAMLprim value caml_curl_multi_perform_all(value v_multi)
4076 CAMLparam1(v_multi);
4077 int still_running = 0;
4078 CURLM* h = CURLM_val(v_multi);
4080 caml_enter_blocking_section();
4081 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(h, &still_running));
4082 caml_leave_blocking_section();
4084 CAMLreturn(Val_int(still_running));
4087 CAMLprim value helper_curl_easy_strerror(value v_code)
4089 CAMLparam1(v_code);
4090 CAMLreturn(caml_copy_string(curl_easy_strerror(Int_val(v_code))));
4094 * Wrappers for the curl_multi_socket_action infrastructure
4095 * Based on curl hiperfifo.c example
4098 #ifdef _WIN32
4099 #ifndef Val_socket
4100 #define Val_socket(v) win_alloc_socket(v)
4101 #endif
4102 #ifndef Socket_val
4103 #error Socket_val not defined in unixsupport.h
4104 #endif
4105 #else /* _WIN32 */
4106 #ifndef Socket_val
4107 #define Socket_val(v) Long_val(v)
4108 #endif
4109 #ifndef Val_socket
4110 #define Val_socket(v) Val_int(v)
4111 #endif
4112 #endif /* _WIN32 */
4114 static void raise_multi_error(char const* msg)
4116 static value* exception = NULL;
4118 if (NULL == exception)
4120 exception = caml_named_value("Curl.Multi.Error");
4121 if (NULL == exception) caml_invalid_argument("Curl.Multi.Error");
4124 caml_raise_with_string(*exception, msg);
4127 static void check_mcode(CURLMcode code)
4129 char const *s = NULL;
4130 switch (code)
4132 case CURLM_OK : return;
4133 case CURLM_CALL_MULTI_PERFORM : s="CURLM_CALL_MULTI_PERFORM"; break;
4134 case CURLM_BAD_HANDLE : s="CURLM_BAD_HANDLE"; break;
4135 case CURLM_BAD_EASY_HANDLE : s="CURLM_BAD_EASY_HANDLE"; break;
4136 case CURLM_OUT_OF_MEMORY : s="CURLM_OUT_OF_MEMORY"; break;
4137 case CURLM_INTERNAL_ERROR : s="CURLM_INTERNAL_ERROR"; break;
4138 case CURLM_UNKNOWN_OPTION : s="CURLM_UNKNOWN_OPTION"; break;
4139 case CURLM_LAST : s="CURLM_LAST"; break;
4140 case CURLM_BAD_SOCKET : s="CURLM_BAD_SOCKET"; break;
4141 default : s="CURLM_unknown"; break;
4143 raise_multi_error(s);
4146 CAMLprim value caml_curl_multi_socket_action(value v_multi, value v_fd, value v_kind)
4148 CAMLparam3(v_multi, v_fd, v_kind);
4149 CURLM* h = CURLM_val(v_multi);
4150 int still_running = 0;
4151 CURLMcode rc = CURLM_OK;
4152 curl_socket_t socket;
4153 int kind = 0;
4155 if (Val_none == v_fd)
4157 socket = CURL_SOCKET_TIMEOUT;
4159 else
4161 socket = Socket_val(Field(v_fd, 0));
4164 switch (Int_val(v_kind))
4166 case 0 : break;
4167 case 1 : kind |= CURL_CSELECT_IN; break;
4168 case 2 : kind |= CURL_CSELECT_OUT; break;
4169 case 3 : kind |= CURL_CSELECT_IN | CURL_CSELECT_OUT; break;
4170 default:
4171 raise_multi_error("caml_curl_multi_socket_action");
4174 /* fprintf(stdout,"fd %u kind %u\n",socket, kind); fflush(stdout); */
4176 caml_enter_blocking_section();
4177 do {
4178 rc = curl_multi_socket_action(h, socket, kind, &still_running);
4179 } while (rc == CURLM_CALL_MULTI_PERFORM);
4180 caml_leave_blocking_section();
4182 check_mcode(rc);
4184 CAMLreturn(Val_int(still_running));
4187 CAMLprim value caml_curl_multi_socket_all(value v_multi)
4189 CAMLparam1(v_multi);
4190 int still_running = 0;
4191 CURLMcode rc = CURLM_OK;
4192 CURLM* h = CURLM_val(v_multi);
4194 caml_enter_blocking_section();
4195 do {
4196 rc = curl_multi_socket_all(h, &still_running);
4197 } while (rc == CURLM_CALL_MULTI_PERFORM);
4198 caml_leave_blocking_section();
4200 check_mcode(rc);
4202 CAMLreturn(Val_int(still_running));
4205 static int curlm_sock_cb_nolock(CURL *e, curl_socket_t sock, int what, ml_multi_handle* multi, void *sockp)
4207 CAMLparam0();
4208 CAMLlocal2(v_what,csock);
4209 (void)e;
4210 (void)sockp; /* not used */
4212 /* v_what = Val_int(what); */
4213 switch (what)
4215 case CURL_POLL_NONE : v_what = Val_int(0); break;
4216 case CURL_POLL_IN : v_what = Val_int(1); break;
4217 case CURL_POLL_OUT : v_what = Val_int(2); break;
4218 case CURL_POLL_INOUT : v_what = Val_int(3); break;
4219 case CURL_POLL_REMOVE : v_what = Val_int(4); break;
4220 default:
4221 fprintf(stderr, "curlm_sock_cb sock=%d what=%d\n", sock, what);
4222 fflush(stderr);
4223 raise_multi_error("curlm_sock_cb"); /* FIXME exception from callback */
4225 csock=Val_socket(sock);
4226 caml_callback2(Field(multi->values,curlmopt_socket_function),
4227 csock, v_what);
4229 CAMLreturn(0);
4232 static int curlm_sock_cb(CURL *e, curl_socket_t sock, int what, void *cbp, void *sockp)
4234 int ret;
4235 caml_leave_blocking_section();
4236 ret = curlm_sock_cb_nolock(e, sock, what, (ml_multi_handle*)cbp, sockp);
4237 caml_enter_blocking_section();
4238 return ret;
4241 CAMLprim value caml_curl_multi_socketfunction(value v_multi, value v_cb)
4243 CAMLparam2(v_multi, v_cb);
4244 ml_multi_handle* multi = Multi_val(v_multi);
4246 Store_field(multi->values, curlmopt_socket_function, v_cb);
4248 curl_multi_setopt(multi->handle, CURLMOPT_SOCKETFUNCTION, curlm_sock_cb);
4249 curl_multi_setopt(multi->handle, CURLMOPT_SOCKETDATA, multi);
4251 CAMLreturn(Val_unit);
4254 static void curlm_timer_cb_nolock(ml_multi_handle *multi, long timeout_ms)
4256 CAMLparam0();
4257 caml_callback(Field(multi->values,curlmopt_timer_function), Val_long(timeout_ms));
4258 CAMLreturn0;
4261 static int curlm_timer_cb(CURLM *multi, long timeout_ms, void *userp)
4263 (void)multi;
4265 caml_leave_blocking_section();
4266 curlm_timer_cb_nolock((ml_multi_handle*)userp, timeout_ms);
4267 caml_enter_blocking_section();
4268 return 0;
4271 CAMLprim value caml_curl_multi_timerfunction(value v_multi, value v_cb)
4273 CAMLparam2(v_multi, v_cb);
4274 ml_multi_handle* multi = Multi_val(v_multi);
4276 Store_field(multi->values, curlmopt_timer_function, v_cb);
4278 curl_multi_setopt(multi->handle, CURLMOPT_TIMERFUNCTION, curlm_timer_cb);
4279 curl_multi_setopt(multi->handle, CURLMOPT_TIMERDATA, multi);
4281 CAMLreturn(Val_unit);
4284 CAMLprim value caml_curl_multi_timeout(value v_multi)
4286 CAMLparam1(v_multi);
4287 long ms = 0;
4288 CURLMcode rc = CURLM_OK;
4289 ml_multi_handle* multi = Multi_val(v_multi);
4291 rc = curl_multi_timeout(multi->handle, &ms);
4293 check_mcode(rc);
4295 CAMLreturn(Val_long(ms));