···407407 |> String.split_on_char ',' |> List.hd |> String.split_on_char ':' |> List.hd
408408 |> String.trim
409409410410-let rec http_get ?(max_redirects = 5) ?headers uri =
410410+let rec http_get ?(max_redirects = 5) ?(no_drain = false) ?headers uri =
411411 let ua = "pegasus (" ^ Env.host_endpoint ^ ")" in
412412 let headers =
413413 match headers with
···417417 Http.Header.of_list [("User-Agent", ua)]
418418 in
419419 let%lwt ans = Cohttp_lwt_unix.Client.get ~headers uri in
420420- follow_redirect ~max_redirects uri ans
420420+ follow_redirect ~max_redirects ~no_drain uri ans
421421422422-and follow_redirect ~max_redirects request_uri (response, body) =
422422+and follow_redirect ~max_redirects ~no_drain request_uri (response, body) =
423423 let status = Http.Response.status response in
424424 (* the unconsumed body would otherwise leak memory *)
425425 let%lwt () =
426426- if status <> `OK then Cohttp_lwt.Body.drain_body body else Lwt.return_unit
426426+ if status <> `OK && not no_drain then Cohttp_lwt.Body.drain_body body
427427+ else Lwt.return_unit
427428 in
428429 match status with
429430 | `Permanent_redirect | `Moved_permanently ->
+25-46
pegasus/lib/xrpc.ml
···251251 let rpc_aud = aud ^ fragment in
252252 Auth.assert_rpc_scope ctx.auth ~aud:rpc_aud ~lxm ;
253253 match%lwt Id_resolver.Did.resolve service_did with
254254- | Ok did_doc -> (
254254+ | Ok did_doc ->
255255 let scheme, host =
256256 match Id_resolver.Did.Document.get_service did_doc fragment with
257257 | Some service -> (
···286286 , Dream.header ctx.req "atproto-accept-labelers" )
287287 ; ("authorization", Some ("Bearer " ^ jwt)) ]
288288 in
289289- match Dream.method_ ctx.req with
290290- | `GET -> (
291291- let%lwt res, body =
292292- try%lwt
293293- Lwt_unix.with_timeout 15.0 (fun () -> Util.http_get uri ~headers)
294294- with Lwt_unix.Timeout ->
295295- Errors.internal_error ~msg:"proxy request timed out" ()
296296- in
297297- let res_headers =
298298- Cohttp.Response.headers res |> Cohttp.Header.to_list
299299- in
300300- match res.status with
301301- | `OK ->
302302- Dream.stream ~status:`OK ~headers:res_headers (fun stream ->
303303- Body.to_stream body |> Lwt_stream.iter_s (Dream.write stream) )
304304- | e ->
305305- let%lwt () = Body.drain_body body in
306306- Log.err (fun log ->
307307- log "error when proxying to %s: %s" (Uri.to_string uri)
308308- (Http.Status.to_string e) ) ;
309309- Errors.internal_error ~msg:"failed to proxy request" () )
310310- | `POST -> (
311311- let%lwt req_body = Dream.body ctx.req in
312312- let%lwt res, body =
313313- try%lwt
314314- Lwt_unix.with_timeout 15.0 (fun () ->
315315- Client.post uri ~headers ~body:(Body.of_string req_body) )
316316- with Lwt_unix.Timeout ->
317317- Errors.internal_error ~msg:"proxy request timed out" ()
318318- in
319319- let res_headers =
320320- Cohttp.Response.headers res |> Cohttp.Header.to_list
321321- in
322322- match res.status with
323323- | `OK ->
324324- Dream.stream ~status:`OK ~headers:res_headers (fun stream ->
325325- Body.to_stream body |> Lwt_stream.iter_s (Dream.write stream) )
326326- | e ->
327327- let%lwt () = Body.drain_body body in
328328- Log.err (fun log ->
329329- log "error when proxying to %s: %s" (Uri.to_string uri)
330330- (Http.Status.to_string e) ) ;
331331- Errors.internal_error ~msg:"failed to proxy request" () )
332332- | _ ->
333333- Errors.invalid_request "unsupported method" )
289289+ let%lwt res, body =
290290+ try%lwt
291291+ Lwt_unix.with_timeout 30.0 (fun () ->
292292+ match Dream.method_ ctx.req with
293293+ | `GET ->
294294+ Util.http_get uri ~headers ~no_drain:true
295295+ | `POST ->
296296+ let%lwt req_body = Dream.body ctx.req in
297297+ Client.post uri ~headers ~body:(Body.of_string req_body)
298298+ | _ ->
299299+ Errors.invalid_request "unsupported method" )
300300+ with Lwt_unix.Timeout ->
301301+ Errors.internal_error ~msg:"proxy request timed out" ()
302302+ in
303303+ let res_headers = Cohttp.Response.headers res |> Cohttp.Header.to_list in
304304+ if res.status <> `OK then
305305+ Log.err (fun log ->
306306+ log "error when proxying to %s: %s" (Uri.to_string uri)
307307+ (Http.Status.to_string res.status) ) ;
308308+ Dream.stream
309309+ ~status:(Dream.int_to_status (Http.Status.to_int res.status))
310310+ ~headers:res_headers
311311+ (fun stream ->
312312+ Body.to_stream body |> Lwt_stream.iter_s (Dream.write stream) )
334313 | Error e ->
335314 Log.err (fun log -> log "error when resolving destination service: %s" e) ;
336315 Errors.internal_error ~msg:"failed to resolve destination service" ()