@@ -356,120 +356,6 @@ let http_request_lwt_exn = Http_lwt.http_request_exn
356
356
let http_query_lwt = Http_lwt. http_query
357
357
let http_submit_lwt = Http_lwt. http_submit
358
358
359
- let http_get_io_exn ?(setup =ignore) ?max_size ?(check =(fun h -> Curl. get_httpcode h = 200 )) url out =
360
- let inner = ref None in
361
- try
362
- with_curl_cache begin fun h ->
363
- Curl. set_url h url;
364
- curl_default_setup h;
365
- setup h;
366
- let read_size = ref 0 in
367
- Curl. set_writefunction h begin fun s ->
368
- try
369
- match check h with
370
- | false -> 0
371
- | true ->
372
- IO. nwrite_string out s;
373
- let l = String. length s in
374
- read_size += l;
375
- match max_size with
376
- | Some max_size when ! read_size > max_size -> Exn. fail " received too much data (%db) when max is %db" ! read_size max_size
377
- | _ -> l
378
- with exn -> inner := Some exn ; 0 end;
379
- let result = Curl. perform h in
380
- IO. flush out;
381
- result
382
- end
383
- with
384
- | exn -> raise (Option. default exn ! inner)
385
-
386
- let http_get_io url ?(verbose =true ) ?setup ?max_size out =
387
- try
388
- http_get_io_exn url ?setup ?max_size out
389
- with
390
- | Curl. CurlException (Curl. CURLE_WRITE_ERROR,_ ,_ ) -> ()
391
- | exn -> if verbose then Log. main #warn ~exn " http_get_io(%s)" url else ()
392
-
393
- let http_get ?verbose ?setup ?max_size url = wrapped (IO. output_string () ) IO. close_out (http_get_io ?verbose ?setup ?max_size url)
394
-
395
- let http_get_io_lwt ?body ?timeout ?(setup =ignore) ?(check =(fun h -> Curl. get_httpcode h = 200 )) url out =
396
- let inner_error = ref `None in
397
- let error code = sprintf " curl (%d) %s" (Curl. errno code) (Curl. strerror code) in
398
- let inner_error_msg () =
399
- match ! inner_error with
400
- | `None -> error Curl. CURLE_WRITE_ERROR
401
- | `Write exn -> sprintf " write error : %s" @@ Exn. to_string exn
402
- | `Http code -> sprintf " http : %d" code
403
- in
404
- try % lwt
405
- Http_lwt. with_curl_cache begin fun h ->
406
- Curl. set_url h url;
407
- curl_default_setup h;
408
- Option. may (Curl. set_timeout h) timeout;
409
- Option. may (fun (ct , body ) ->
410
- let open Curl in
411
- set_post h true ;
412
- set_httpheader h [" Content-Type: " ^ ct];
413
- set_postfields h body;
414
- set_postfieldsize h (String. length body)
415
- ) body;
416
- setup h;
417
- Curl. set_writefunction h begin fun s ->
418
- try
419
- match check h with
420
- | false -> inner_error := `Http (Curl. get_httpcode h); 0
421
- | true -> IO. nwrite_string out s; String. length s
422
- with exn -> inner_error := `Write exn ; 0
423
- end;
424
- match % lwt Curl_lwt. perform h with
425
- | Curl. CURLE_OK when not @@ check h -> `Error (sprintf " http: %d" (Curl. get_httpcode h)) |> Lwt. return
426
- | Curl. CURLE_OK -> IO. flush out; `Ok (Curl. get_sizedownload h) |> Lwt. return
427
- | Curl. CURLE_WRITE_ERROR -> `Error (inner_error_msg () ) |> Lwt. return
428
- | code -> `Error (error code) |> Lwt. return
429
- end
430
- with
431
- | exn -> Exn_lwt. fail ~exn " http_get_io_lwt (%s)" (inner_error_msg () )
432
-
433
- (* NOTE don't forget to set http_1_0=true when sending requests to a Httpev-based server *)
434
- (* deprecated! use http_request or http_query instead *)
435
- let http_do ?ua ?timeout ?(verbose =false ) ?(setup =ignore) ?(http_1_0 =false ) (action :http_action_old ) url =
436
- let open Curl in
437
- let post ?req h ct body =
438
- set_post h true ;
439
- begin match req with None -> () | Some s -> set_customrequest h s end ;
440
- set_postfields h body;
441
- set_postfieldsize h (String. length body);
442
- set_httpheader h [" Content-Type: " ^ ct];
443
- in
444
- let setup h =
445
- begin match action with
446
- | `GET -> ()
447
- | `DELETE -> set_customrequest h " DELETE"
448
- | `POST (ct ,body ) -> post h ct body
449
- | `PUT (ct ,body ) -> post ~req: " PUT" h ct body
450
- | `POST_FORM args -> post h " application/x-www-form-urlencoded" (make_url_args args)
451
- | `CUSTOM (req ,ct ,body ) -> post ~req h ct body
452
- end ;
453
- if http_1_0 then set_httpversion h HTTP_VERSION_1_0 ;
454
- Option. may (set_timeout h) timeout;
455
- Option. may (set_useragent h) ua;
456
- let () = setup h in
457
- ()
458
- in
459
- if verbose then begin
460
- let log_verb req ct body = log #info " %s %s %s %s" req url ct (Stre. shorten 64 body) in
461
- match action with
462
- | `GET -> log #info " GET %s" url
463
- | `DELETE -> log #info " DELETE %s" url
464
- | `POST (ct ,body ) -> log_verb " POST" ct body
465
- | `PUT (ct ,body ) -> log_verb " PUT" ct body
466
- | `POST_FORM l -> log #info " POST %s %s" url (String. concat " " @@ List. map (fun (k ,v ) -> sprintf " %s=%S" k (Stre. shorten 64 v)) l)
467
- | `CUSTOM (req ,ct ,body ) -> log_verb req ct body
468
- end ;
469
- match http_gets ~setup url with
470
- | `Ok (code , s ) when code / 100 = 2 -> `Ok s
471
- | r -> `Error (show_result r)
472
-
473
359
(* http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html *)
474
360
let string_of_http_code = function
475
361
| 100 -> " Continue"
0 commit comments