2

I am writing an apache module. I need to recieve arbitrary data from the client after authentication. In principle my solution requires me to write a loop that allows the program to read and write from a socket — or the apache underlying mechanism for socket IO.

I know this might be the XY problem, but I can't think of another way to do this. My program is ready to start bidirectional communication but I have searched the web and I have failed to find something useful.

I also inspected the request_rec structure and all the relevant structures of some of it's fields and I didn't find anything that looks interesting or relevant for this problem.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Just to make sure I'm understanding you correctly... you're writing an HTTP server extension, but you want to deviate from the normal HTTP request/response pattern of exchanging data. Is that right? – nephtes Jun 08 '16 at 19:36
  • @nephtes It's exactly what I want. You put it in the right words actually. Is this possible? Do you know how to do it? – Iharob Al Asimi Jun 08 '16 at 19:59
  • I haven't dealt with Apache httpd internals for a long time. That being said... what you want is actually very similar to Apache's [WebSocket proxy module](https://httpd.apache.org/docs/2.4/mod/mod_proxy_wstunnel.html), so it should be possible. – nephtes Jun 08 '16 at 20:27
  • In particular, a request handler can access the underlying socket with `ap_get_conn_socket(r->connection)`. Getting the module to behave in a way that won't put the request in a nonsensical state will be trickier. However, The source of mod_wstunnel is [here](http://svn.apache.org/repos/asf/httpd/httpd/trunk/modules/proxy/mod_proxy_wstunnel.c); that should help guide you. Good luck! – nephtes Jun 08 '16 at 20:27
  • @nepthes please, post the last comment as an answer (*or both combined*). And I will accept it. The `ap_get_conn_socket()` appears to be precisely what I need. I only have to deal with the apache not interfering with the request anymore. – Iharob Al Asimi Jun 08 '16 at 20:45
  • Done. Glad to be of service! – nephtes Jun 08 '16 at 20:49

1 Answers1

1

What you want is actually very similar to Apache's WebSocket proxy module, so it should be possible.

In particular, a request handler can access the underlying socket with ap_get_conn_socket(r->connection). Getting the module to behave in a way that won't put the request in a nonsensical state will be trickier. However, the source of mod_wstunnel is here; that should help guide you. Good luck!

nephtes
  • 1,319
  • 9
  • 19
  • I can't believe how helpful your `ap_get_conn_socket()` suggestion was. After that just use `ap_send_interim_response()`, `ap_finalize_request_protocol()` and `ap_set_keepalive()` and I am doing exactly what I intended. Thanks. – Iharob Al Asimi Jun 11 '16 at 22:06