Open pettno opened 11 years ago
This is a somewhat complicated issue. Here are my thoughts, both short and long term.
Short term: I've pushed an update that adds an accessor for the HTTP request object that WebSocket++ builds when it receives requests. This object is of a type defined by the HTTP policy set in the endpoint's config parameter.
When using the reference HTTP parser policy included with WebSocket++ the procedure for getting the raw request is now: con->get_request()->raw()
. Note this will only include the headers. As noted in #181 the reference HTTP parser does not parse request bodies and the library itself is not set up to read request bytes beyond the headers.
Long term: Supporting HTTP pass through to another server is something that probably would benefit from better internal support. WebSocket++ core already supports forward HTTP proxies on the client role. Reverse proxy support on the server role would allow trivial forwarding of un-upgraded HTTP connections to other servers while not blocking the endpoint the way an http handler based solution would. This would also get around the need to manually close the socket in the handler (which the library is not expecting) and the issue of not being able to deal with request bodies.
I think this would be a better option long term than adding more HTTP circuitry to allow handlers to read bodies and close sockets, etc. Thoughts?
The short term solution is part of what we need for now. To access the request body, we would also need something like the data in m_buf
and "size" in m_buf_cursor
? This feels more or less like hacking into the library to steal data from its internals, though.
Your long term solution is indeed what we would prefer. This would allow us to listen on port 80 by the websocket serving application and forward the rest to the web server. IMHO: It would be a great feature to be able to add handling of websockets in front of (and transparent to) any type of "regular" web server like this.
This might be a duplicate of #181.
Full support for this feature is still in the works. In the meantime some smaller improvements to the library have been made to make manually implementing it a little easier. Specifically access to HTTP request bodies and the ability to cleanly terminate the connection after an http handler without writing an http response.
See #181 for further discussion.
void on_http( websocketpp::connection_hdl hdl )
server::connection_ptr con = endpoint.get_con_from_hdl( hdl );
tcp::socket & server_socket = con->get_socket();
// write what you need to to server_socket here
con->terminate(websocketpp::error::make_error_code(websocketpp::error::http_connection_ended));
}
We have an application which:
This enables us to serve both websockets and http on the same port.
Here is some working incomplete code:
Suggestions or feature requests for WebSocket++ (if it is not possible to solve differently):
This issue is related to #152 and #164.