Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to have Server_connection pass the fd to the request_hander? #139

Closed
aantron opened this issue Sep 5, 2024 · 7 comments
Closed

Comments

@aantron
Copy link
Contributor

aantron commented Sep 5, 2024

This would help with ocaml/ocaml.org#2676 (comment). I don't see a way to pass the fd at the moment, and it doesn't seem to be available through Reqd.t or Request.t.

@anmonteiro
Copy link
Owner

anmonteiro commented Sep 5, 2024

it's fairly easy to do this, though it's unsafe.

here's a simplified version with eio:

let request_handler (_fd: _ Eio.Net.stream_socket) (_ : Eio.Net.Sockaddr.stream) { Gluten.reqd= _; _ } =
  assert false
;;

(* ... *)

let handler fd =
  Server.create_connection_handler
    ~request_handler:(request_handler fd)
    ~error_handler

(* ... *)

while true do
  Eio.Net.accept_fork socket ~sw ~on_error:log_connection_error (fun client_sock client_addr ->
    handler client_sock client_addr client_sock)
done;

Eio.Net.accept* (or Lwt_io.establish_server_with_client_socket) has the fd reference in the callback somewhere, all you need to do is eta-expand to use it.

@aantron
Copy link
Contributor Author

aantron commented Sep 5, 2024

Thank you. On a basic level, I have it working. However, is the expansion still safe w.r.t. side effects for SSL?

      let request_handler = wrap_handler true error_handler handler in
      let error_handler = Error_handler.httpaf error_handler in
      fun client_address unix_socket tls_endpoint ->
        Httpaf_lwt_unix.Server.SSL.create_connection_handler
          ?config:None
          ~request_handler:(request_handler unix_socket)
          ~error_handler
          client_address
          tls_endpoint

and likewise for ocaml-h2?

@anmonteiro
Copy link
Owner

However, is the expansion still safe w.r.t. side effects for SSL?

I believe you can rely on this, since the connection establishment happens at accept, i.e. before the callback is invoked.

@aantron
Copy link
Contributor Author

aantron commented Sep 5, 2024

Alright, thank you! This is probably the most questionable part of Dream at this point, together with the Obj.magic in aantron/dream#345 for getting actual fd numbers, but if it works... it works :)

@aantron aantron closed this as completed Sep 5, 2024
@anmonteiro
Copy link
Owner

but if it works... it works :)

I don't know if "it works". Referring to my initial response:

it's fairly easy to do this, though it's unsafe.

it depends what you do with the socket. httpun / gluten / etc guarantee the correct lifecycle for the connection (and consequently, the socket) – that's not the case if you're accessing it directly.

@aantron
Copy link
Contributor Author

aantron commented Sep 5, 2024

Dumping the fd number on Unix.

@anmonteiro
Copy link
Owner

I understood your intent, but I wanted to leave it written down for the next person looking for this answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants