Trouble Sending HTTP Requests to Port 443 (HTTPS) using Sockets.jl

Trying to send HTTP requests using the Sockets.jl package. It works on port 80 say for “example.com” but any attempt to contact a server at port 443 (HTTPS) I’m met with a - 400 Bad Request - The plain HTTP request was sent to HTTPS port. I’m kind a network security n00b but I suspect there’s some SSL handshake that needs to happen under the hood and I don’t see how to inspect that, if that’s even the problem. Example code below with subsequent error…

using Sockets

conn = connect("git-scm.com",443)

@async while isopen(conn)
    write(stdout, read(conn, Char))
end

query = "GET / HTTP/1.1
Host: git-scm.com
Accept: text/html

"
write(conn,query)
julia> HTTP/1.1 400 Bad Request
Server: cloudflare
Date: Thu, 27 Apr 2023 14:27:43 GMT
Content-Type: text/html
Content-Length: 253
Xonnection: close
CF-RAY: -
Connection: Keep-Alive

<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>cloudflare</center>
</body>
</html>

Have you tried OpenSSL.jl or MbedTLS.jl ?

I’ve been looking at MBedTLS.jl while making this post, but are we saying that the Sockets.jl package can’t handle this use case on its own? No problem if so, but I wanted to probe the community to see if that’s the case and to understand a solution otherwise if it can be handled natively.

Thank you for the response btw! Hope to hear more from you and others!

Writing a native TLS encryption would be too much work. For now using and building over already battle tested available packages from other languages is a safe bet.

I’d recommend using HTTP.jl. The Sockets stdlib is just a literal socket library for TCP/UDP/other sockets. HTTP/S is a protocol, it per se is unrelated to sockets.

1 Like

If one needs to use allocation free persistent communication, HTTP.jl would not be much of a help there, one would need to use Sockets, unsafe_write (with this modification) to avoid allocation in the hot loop.

Ok, but for the simple case of “use HTTPS to connect to a webserver”, HTTP.jl is perfectly fine.

1 Like