HTTP and TLS

96 views
Skip to first unread message

Trey Weaver

unread,
May 19, 2021, 8:44:05 PM5/19/21
to WebSocket++
I have a websocket server that I need to connect to via TLS/HTTPS.  Once connected I need to do send a HTTP request and wait for a response.  If that is successful then send and receive JSON messages.

Can I open a standard websocket and when connected then send the HTTP request/response manually by doing a normal send/receive.

Any better way?  Any sample code?

Thanks,

Jacob Burckhardt

unread,
May 19, 2021, 9:08:38 PM5/19/21
to Trey Weaver, WebSocket++
Does the server already exist?  Do you need to write only the client?  If so, then it sounds like you do not need WebSocket functionality.  You only need HTTP functionality.  Therefore I suggest you use an HTTPS client library like curl.

Do you plan to put the JSON in an HTTPS message body?  If so then the above plan should work.


From: webso...@googlegroups.com <webso...@googlegroups.com> on behalf of Trey Weaver <treyw...@gmail.com>
Sent: Wednesday, May 19, 2021 5:44 PM
To: WebSocket++ <webso...@googlegroups.com>
Subject: HTTP and TLS
 
--
You received this message because you are subscribed to the Google Groups "WebSocket++" group.
To unsubscribe from this group and stop receiving emails from it, send an email to websocketpp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/websocketpp/94bc9442-7b54-4dc8-aa13-ab10ac475173n%40googlegroups.com.

Trey Weaver

unread,
May 19, 2021, 9:20:35 PM5/19/21
to Jacob Burckhardt, WebSocket++
Yes, the server does exist.  The client is on an embedded product and I am only writing the client.  The server uses web sockets with TLS.  Once the “socket” is created it may stay in use for a long time, sending 100’s of json messages back and forth.

So basically I need to set up a TLS-web-socket, send http header, receive http header and then read and write json message to/from socket.

Jacob Burckhardt

unread,
May 19, 2021, 9:25:59 PM5/19/21
to Trey Weaver, WebSocket++
Does the JSON need to be in HTTP messages or WebSocket messages?


Sent: Wednesday, May 19, 2021 6:20 PM
To: Jacob Burckhardt <ja...@teamconnectusa.com>
Cc: WebSocket++ <webso...@googlegroups.com>
Subject: Re: HTTP and TLS
 

Trey Weaver

unread,
May 19, 2021, 9:27:39 PM5/19/21
to Jacob Burckhardt, WebSocket++
Just WebSocket message.

Jacob Burckhardt

unread,
May 19, 2021, 9:57:24 PM5/19/21
to Trey Weaver, WebSocket++
You said "Once the “socket” is created it may stay in use for a long time".  Do you mean that the same socket may send both an HTTP and many WebSocket messages containing JSON?  Will it work if you use different sockets?  I.e. make one connection and send HTTP and then close that connection.  Then make another connection and send WebSocket messages containing JSON.  Is it ok that those are two different connections?  If so then you could use curl for the HTTP connection and websocket++ for the JSON.

It is OK if both connections go to the same server since a single web server can support both HTTP and WebSocket connections using an upgrade as described here:

   The opening handshake is intended to be compatible with HTTP-based
   server-side software and intermediaries, so that a single port can be
   used by both HTTP clients talking to that server and WebSocket
   clients talking to that server.  To this end, the WebSocket client's
   handshake is an HTTP Upgrade request:
If the HTTP and WebSocket messages are required to be on the same connection, then I'm not sure if the WebSocket protocol allows that.  Maybe it does since the second message on the connection will be a WebSocket message which the server might consider to be an upgrade as described above.  

As for whether you can use WebSocket++ to do this, I don't know.  Websocket++ can be used to create a server that receives both HTTP and WebSocket client connections but I don't know if WebSocket++ can send HTTP messages.  

Maybe you could instead send an HTTP message using the standard TCP API (not websocket++) and then give the socket file descriptor you used to send that HTTP message to the WebSocket++ libary and then maybe WebSocket++ could take over that file descriptor and start sending WebSocket messages.  But I don't recall seeing a way to do this in the WebSocket++ documentation.  I think it would be possible if you write your own transport for WebSocket++ and there are some examples of alternate transports in the WebSocket++ release, but I think that would be difficult.


From: Trey Weaver <treyw...@gmail.com>
Sent: Wednesday, May 19, 2021 6:27 PM

Trey Weaver

unread,
May 19, 2021, 10:28:56 PM5/19/21
to Jacob Burckhardt, WebSocket++
Yes,  the same socket.  The socket has to be the same one that is set up by TLS.

Here is the code for the header I am trying to send.  This is written in “go" and meant to work with Gorilla web socket library.  Notice that it tells the server to upgrade to “websocket”.  According to the developers of the server this is “basic” normal operation.

req := &http.Request{
Method: "GET",
URL: u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Host: u.Host,
}
req = req.WithContext(ctx)
// Set the request headers using the capitalization for names and values in
// RFC examples. Although the capitalization shouldn't matter, there are
// servers that depend on it. The Header.Set method is not used because the
// method canonicalizes the header names.
req.Header["Upgrade"] = []string{"websocket"}
req.Header["Connection"] = []string{"Upgrade"}
req.Header["Sec-WebSocket-Key"] = []string{challengeKey}
req.Header["Sec-WebSocket-Version"] = []string{"13"}


On May 19, 2021, at 9:57 PM, Jacob Burckhardt <ja...@teamconnectusa.com> wrote:

You said "Once the “socket” is created it may stay in use for a long time".  Do you mean that the same socket may send both an HTTP and many WebSocket messages containing JSON?  Will it work if you use different sockets?  I.e. make one connection and send HTTP and then close that connection.  Then make another connection and send WebSocket messages containing JSON.  Is it ok that those are two different connections?  If so then you could use curl for the HTTP connection and websocket++ for the JSON.

It is OK if both connections go to the same server since a single web server can support both HTTP and WebSocket connections using an upgrade as described here:

   The opening handshake is intended to be compatible with HTTP-based
   server-side software and intermediaries, so that a single port can be
   used by both HTTP clients talking to that server and WebSocket
   clients talking to that server.  To this end, the WebSocket client's
   handshake is an HTTP Upgrade request:
If the HTTP and WebSocket messages are required to be on the same connection, then I'm not sure if the WebSocket protocol allows that.  Maybe it does since the second message on the connection will be a WebSocket message which the server might consider to be an upgrade as described above.  

As for whether you can use WebSocket++ to do this, I don't know.  Websocket++ can be used to create a server thatreceives both HTTP and WebSocket client connections but I don't know if WebSocket++ can send HTTP messages.  

Jacob Burckhardt

unread,
May 20, 2021, 1:43:09 PM5/20/21
to Trey Weaver, WebSocket++
I think WebSocket++ already does the equivalent of the code you show below.  You said you needed to send an HTTP message followed by WebSocket messages.  The WebSocket handshake was designed to look like an HTTP message.  So when you saw people saying you need to send a message that that message looked like HTTP maybe you thought it was HTTP but maybe it was really just a WebSocket handshake.  Notice that the message at this link looks similar to what the code you show would generate:


WebSocket++ does that by default.

So I think WebSocket++ can do what you need without a separate HTTP library like curl.


From: Trey Weaver <treyw...@gmail.com>
Sent: Wednesday, May 19, 2021 7:28 PM
Reply all
Reply to author
Forward
0 new messages