Skip to content
Fixedmark

Networking · 11 min read

Updated

HTTP CONNECT Proxy Explained, Byte by Byte

Your client opens TCP to the proxy and sends `CONNECT host:443 HTTP/1.1` with a `Proxy-Authorization` header. The proxy dials the destination, replies `200`, and from then on copies bytes both ways. Your client runs TLS with the destination inside that pipe, so the proxy sees the host name, the port, and byte counts, but never the request or response.

On this page

Two jobs an HTTP proxy can do

An HTTP proxy works in one of two modes, and the destination's scheme decides which one your client uses.

For an http:// URL, the client sends the whole request to the proxy with the full URL in the request line, such as GET http://example.com/ HTTP/1.1. RFC 9112 calls this the absolute-form. The proxy reads the request, makes it again on your behalf, and hands back the response. It sees everything, because there is nothing to hide it from.

For an https:// URL, the client cannot hand the proxy a request, because the request has to be encrypted for the destination and only the destination holds the key. So the client asks for a tunnel instead. RFC 9110 defines CONNECT as a request that the recipient "establish a tunnel to the destination origin server" and then "restrict its behavior to blind forwarding of data, in both directions, until the tunnel is closed."

The proxy does one HTTP exchange to agree on where to connect, then becomes a pipe. Your client and the destination run TLS through it.

curl uses CONNECT for every https:// URL you send through an HTTP proxy. You do not need a flag for it. The curl book says tunneling "is exactly what's done every time you use HTTPS through the HTTP proxy." The -p (--proxytunnel) flag forces a tunnel for other schemes too, such as plain http:// or FTP.

The CONNECT request, line by line

To see the bytes, run a tiny proxy locally and print what arrives. The script at the end of this post does that. Here is exactly what curl 8.21.0 sent to it for curl --proxy http://demo:s3cret@127.0.0.1:38471 https://example.com/, with \r\n shown as line breaks.

What the proxy received
CONNECT example.com:443 HTTP/1.1
Host: example.com:443
Proxy-Authorization: Basic ZGVtbzpzM2NyZXQ=
User-Agent: curl/8.21.0
Proxy-Connection: Keep-Alive
  • CONNECT example.com:443 HTTP/1.1 is the request line. The target is only a host and a port, which RFC 9112 calls the authority-form. It is used for CONNECT and nothing else. There is no path, because the proxy is not being asked for a resource.
  • The port is mandatory. RFC 9110 says "There is no default port" for CONNECT, and a proxy must reject an empty or invalid port, usually with 400. Clients fill in 443 from the https:// scheme.
  • Host: example.com:443 repeats the authority. HTTP/1.1 requires a Host header on every request, CONNECT included.
  • Proxy-Authorization: Basic ZGVtbzpzM2NyZXQ= carries the credentials. The value is base64 of demo:s3cret. That is an encoding, not encryption. The next section covers it.
  • User-Agent is informational. Proxies may log it.
  • Proxy-Connection: Keep-Alive is a legacy header that curl still sends. RFC 9112's appendix on persistent connections says it proved unworkable and that "clients are encouraged not to send the Proxy-Connection header field in any requests."
  • An empty line ends the request. A CONNECT request has no body. Any bytes after the empty line belong to the tunnel, not to the proxy.

There is no path, no API key, no cookie, and no body. Those travel later, inside TLS.

Proxy-Authorization and the 407 challenge

A proxy that wants credentials and gets none answers 407 Proxy Authentication Required. RFC 9110 section 15.5.8 says the proxy must include a Proxy-Authenticate header naming the scheme it accepts. Here is the same request without a password.

Terminal
$ curl -v --proxy http://127.0.0.1:38471 https://example.com/
*   Trying 127.0.0.1:38471...
* CONNECT: no ALPN negotiated
* Establishing HTTP proxy tunnel to example.com:443
> CONNECT example.com:443 HTTP/1.1
> Host: example.com:443
> User-Agent: curl/8.21.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 407 Proxy Authentication Required
< Proxy-Authenticate: Basic realm="proxy"
< Content-Length: 0
<
* CONNECT tunnel failed, response 407
* closing connection #0

Without -v, curl prints one line, curl: (7) CONNECT tunnel failed, response 407 on 8.21.0. The exit code has changed between curl versions, so match on the message. The toy proxy never dialed example.com. A 407 always comes from the proxy, never from the API you are calling.

  • It is for the proxy only. RFC 9110 says Proxy-Authorization "applies only to the next inbound proxy that demanded authentication." The proxy consumes it. Your destination never sees your proxy credentials, and the proxy never sees the API key you send to the destination.
  • Basic is cleartext. RFC 7617 states that Basic "is not a secure method of user authentication" because the user ID and password travel as cleartext. Anyone who can read the hop between you and the proxy can decode the value with base64 -d.
  • Special characters need URL-encoding when you put credentials in a proxy URL. The curl man page says curl URL-decodes them, so an @ in a password becomes %40.

The usual fix for cleartext Basic is to wrap the proxy hop in TLS. That is what an https:// proxy URL does, covered further down.

The 200 reply and the switch to tunnel mode

When the credentials check out, the proxy opens a TCP connection to example.com:443. If that works, it writes a status line and an empty line back to the client.

The proxy's reply
HTTP/1.1 200 Connection Established

RFC 9110 is precise about what happens next. Any 2xx response means the proxy "will switch to tunnel mode immediately after the response header section." Every byte after that empty line comes from the destination. A proxy must not send Content-Length or Transfer-Encoding on a successful CONNECT reply, and a client must ignore them if it does. Any non-2xx status means no tunnel exists yet.

The reason phrase does not matter. RFC 9112 says a client "SHOULD ignore the reason-phrase content", so Connection Established, Connection established, and OK all work the same.

The tunnel ends when either side closes. The proxy forwards any data still in flight from the side that closed, then closes both connections. That is why a destination that rejects your IP often shows up as a reset right after a successful CONNECT. The proxy did its part, and the far end hung up.

Because CONNECT can reach any port, the RFC also warns proxy operators. A tunnel to port 25 could relay spam, so proxies "SHOULD restrict its use to a limited set of known ports or a configurable list of safe request targets." The curl book notes that most HTTP proxies allow only port 443 "and perhaps a few others." If a CONNECT to port 5432 or 22 fails with 403 from the proxy, this policy is the likely cause.

A full curl -v walk-through

Here is the authenticated run against the local test proxy. The output is real and trimmed: certificate chain details and some response headers are cut. Lines starting with > are sent, < are received, and * is curl's own commentary.

Terminal (trimmed)
$ curl -v --proxy http://demo:s3cret@127.0.0.1:38471 https://example.com/ -o /dev/null
*   Trying 127.0.0.1:38471...
* CONNECT: no ALPN negotiated
* Proxy auth using Basic with user 'demo'
* Establishing HTTP proxy tunnel to example.com:443
> CONNECT example.com:443 HTTP/1.1
> Host: example.com:443
> Proxy-Authorization: Basic ZGVtbzpzM2NyZXQ=
> User-Agent: curl/8.21.0
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection Established
<
* CONNECT phase completed for HTTP proxy
* CONNECT tunnel established, response 200
* ALPN: curl offers h2,http/1.1
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [1568 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [1210 bytes data]
* TLSv1.3 (IN), TLS handshake, Certificate (11):
{ [3686 bytes data]
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519MLKEM768 / id-ecPublicKey
* ALPN: server accepted h2
* Server certificate:
*   subject: CN=example.com
*   issuer: C=US; O=SSL Corporation; CN=Cloudflare TLS Issuing ECC CA 3
* SSL certificate verified via OpenSSL.
* using HTTP/2
> GET / HTTP/2
> Host: example.com
> User-Agent: curl/8.21.0
> Accept: */*
>
< HTTP/2 200
< content-type: text/html; charset=utf-8
< server: cloudflare
  1. 1
    TCP to the proxyTrying 127.0.0.1:38471 is a plain TCP connection to the proxy, not to example.com. Your client never opens a socket to the destination.
  2. 2
    CONNECT and credentialscurl sends the request from the previous sections. Proxy auth using Basic with user 'demo' confirms it picked up the credentials from the proxy URL.
  3. 3
    The tunnel opens200 Connection Established and CONNECT tunnel established, response 200 mark the switch. Everything after this line passes through the proxy unchanged.
  4. 4
    TLS with the destinationThe ClientHello, ServerHello, and certificate are exchanged with example.com through the tunnel. subject: CN=example.com and the public CA issuer prove curl is talking TLS to the real site, not to the proxy.
  5. 5
    The real requestGET / HTTP/2 is sent inside TLS. The proxy forwards these bytes but cannot read them. Note that ALPN picked HTTP/2 with the destination even though the CONNECT itself was HTTP/1.1. The two layers negotiate separately.

When you debug a failing call, find the line after CONNECT. A non-200 there means the proxy refused. A 200 there followed by a 403 from the API means the proxy worked and the destination said no. The proxy error codes guide goes through each case.

What the proxy can and cannot see

The toy proxy printed the first six bytes it relayed in each direction during the run above.

Toy proxy log
client->dest first bytes: 16 03 01 06 20 01
dest->client first bytes: 16 03 03 04 ba 02
client->dest: 1927 bytes
dest->client: 6465 bytes

Those are TLS record headers, as laid out in RFC 8446 section 5.1. 16 is the handshake content type (22). 03 01 is the legacy record version, which TLS 1.3 allows on the first ClientHello. 06 20 is the length, 1568 bytes, which matches curl's [1568 bytes data]. The last 01 is the ClientHello message type. On the way back, 04 ba is 1210 bytes and 02 is the ServerHello, again matching curl. After the handshake, every record is encrypted.

What a CONNECT proxy sees for an HTTPS request
Visible to the proxyHidden from the proxy
Destination host and port, from the CONNECT lineURL path and query string
Your proxy credentials and User-AgentRequest and response headers, including API keys and cookies
The TLS server name (SNI) in the ClientHello, unless the client uses Encrypted Client HelloRequest and response bodies
Your IP, the time, the connection length, and bytes each wayThe HTTP status code the API returned

The SNI line is worth checking. A version of the toy proxy that searched the first client chunk for the string example.com found it. The ClientHello carries the server name in the clear so the destination can choose a certificate. The post on TLS passthrough and zero-knowledge proxies covers SNI, Encrypted Client Hello, and timing in more depth.

The tunnel itself does not encrypt anything. Privacy comes from the TLS your client runs inside it. Running curl -p --proxy ... http://example.com/ opens a CONNECT tunnel to port 80, and the toy proxy logged 47 45 54 20 2f 20 as the first bytes, which is ASCII for GET / . A plain HTTP request inside a tunnel is still plain HTTP.

One case breaks the guarantee. Some corporate proxies terminate TLS themselves and present a certificate signed by a company CA that is installed on your machine. Your client then has TLS with the proxy, and the proxy has a second TLS session with the destination. You can spot it in curl -v. The certificate issuer is the company's CA, not a public one. If the issuer is the public CA you expect, nothing in the middle terminated your TLS.

Do the handshake yourself

Writing the client by hand makes the layering obvious. This Python script opens a socket to the proxy, sends CONNECT, reads the reply up to the empty line, and only then starts TLS on the same socket. It uses only the standard library and was run against the toy proxy below.

connect_client.py
import base64
import socket
import ssl

PROXY = ("127.0.0.1", 38471)
HOST, PORT = "example.com", 443
creds = base64.b64encode(b"demo:s3cret").decode()

# 1. Plain TCP to the proxy.
sock = socket.create_connection(PROXY, timeout=10)

# 2. The CONNECT request: authority-form target, Host, credentials.
sock.sendall(
    f"CONNECT {HOST}:{PORT} HTTP/1.1\r\n"
    f"Host: {HOST}:{PORT}\r\n"
    f"Proxy-Authorization: Basic {creds}\r\n"
    "\r\n".encode()
)

# 3. Read the reply head one byte at a time, so no tunnel bytes are consumed.
head = b""
while b"\r\n\r\n" not in head:
    chunk = sock.recv(1)
    if not chunk:
        raise ConnectionError("proxy closed the connection")
    head += chunk
status_line = head.split(b"\r\n", 1)[0].decode()
print("proxy said:", status_line)
if status_line.split(" ")[1] != "200":
    raise SystemExit("tunnel refused")

# 4. The socket is now a pipe to example.com:443. Start TLS on it.
ctx = ssl.create_default_context()
tls = ctx.wrap_socket(sock, server_hostname=HOST)
print("TLS:", tls.version(), "peer:", tls.getpeercert()["subject"])

# 5. An ordinary HTTPS request, encrypted end to end.
tls.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
print(tls.recv(200).decode(errors="replace").split("\r\n")[0])
tls.close()
Output
proxy said: HTTP/1.1 200 Connection Established
TLS: TLSv1.3 peer: ((('commonName', 'example.com'),),)
HTTP/1.1 200 OK

In step 4, wrap_socket runs on a socket whose other end is the proxy, yet the certificate it verifies belongs to example.com.

OpenSSL can do the same from the shell. openssl s_client has a -proxy option that issues an HTTP CONNECT before the handshake, with -proxy_user and -proxy_pass for Basic auth. Its docs repeat the warning that the credentials go out in reversible base64 before any TLS is set up.

Terminal
echo | openssl s_client -brief \
  -proxy 127.0.0.1:38471 -proxy_user demo -proxy_pass pass:s3cret \
  -connect example.com:443 -servername example.com

And here is the toy proxy that produced every capture in this post. It handles CONNECT only, checks one hard-coded password, and copies bytes. Run it with python3 -u toy_proxy.py so the log prints straight away. It has no timeouts, port limits, or error handling, so keep it on localhost.

toy_proxy.py
# A CONNECT-only proxy for learning. Not for production.
import asyncio
import base64

PORT = 38471
EXPECTED = "Basic " + base64.b64encode(b"demo:s3cret").decode()


async def pipe(reader, writer, label):
    total = 0
    try:
        while data := await reader.read(65536):
            if total == 0:
                print(f"{label} first bytes: {data[:6].hex(' ')}")
            total += len(data)
            writer.write(data)
            await writer.drain()
    finally:
        writer.close()
        print(f"{label}: {total} bytes")


async def handle(client_r, client_w):
    head = await client_r.readuntil(b"\r\n\r\n")
    print("received:", head)
    request_line, *header_lines = head.decode("latin-1").split("\r\n")
    method, target, _version = request_line.split(" ")
    headers = {
        k.strip().lower(): v.strip()
        for k, v in (h.split(":", 1) for h in header_lines if ":" in h)
    }
    if method != "CONNECT":
        client_w.write(b"HTTP/1.1 405 Method Not Allowed\r\nContent-Length: 0\r\n\r\n")
        client_w.close()
        return
    if headers.get("proxy-authorization") != EXPECTED:
        client_w.write(
            b"HTTP/1.1 407 Proxy Authentication Required\r\n"
            b'Proxy-Authenticate: Basic realm="proxy"\r\n'
            b"Content-Length: 0\r\n\r\n"
        )
        client_w.close()
        return
    host, port = target.rsplit(":", 1)
    dest_r, dest_w = await asyncio.open_connection(host, int(port))
    client_w.write(b"HTTP/1.1 200 Connection Established\r\n\r\n")
    await client_w.drain()
    # From here on the proxy only copies bytes.
    await asyncio.gather(
        pipe(client_r, dest_w, "client->dest"),
        pipe(dest_r, client_w, "dest->client"),
    )


async def main():
    server = await asyncio.start_server(handle, "127.0.0.1", PORT)
    async with server:
        await server.serve_forever()


asyncio.run(main())

CONNECT over TLS and over HTTP/2

Everything above used http:// in the proxy URL, so the CONNECT request and its Basic credentials crossed the first hop in the clear. Only the destination traffic inside the tunnel was encrypted.

An https:// proxy URL adds a TLS session to the proxy first. The client verifies the proxy's certificate, then sends the same CONNECT request inside that session, then runs a second TLS session with the destination inside the tunnel. The proxy can still read the CONNECT line and the credentials, because it terminates the outer TLS. A network observer between you and the proxy cannot. The curl man page lists which TLS backends support https:// proxies, and options such as --proxy-cacert and --proxy-insecure apply to the proxy's certificate separately from the destination's. Client support varies by library, which the post on HTTPS proxy endpoints compares.

HTTP/2 changes the framing but not the idea. RFC 9113 section 8.5 says an HTTP/2 CONNECT "establishes a tunnel over a single HTTP/2 stream to a remote host, rather than converting the entire connection to a tunnel." The request sets :method to CONNECT and :authority to the host and port, and omits :scheme and :path. Tunnel bytes then travel in DATA frames. One TLS connection to a proxy can carry many tunnels at once. curl asks for this with --proxy-http2, added in 8.1.0, and only for HTTPS proxies.

CONNECT also carries any TCP protocol, not only TLS. A Postgres or SSH session can run through it if the proxy allows the port. In practice most HTTP clients only use CONNECT for https:// URLs, and database drivers rarely speak HTTP proxies at all. That is why SOCKS5 is the usual choice for databases. See SOCKS5 vs socks5h for how it differs.

Doing this with Fixedmark

Fixedmark, which is in early access, is a CONNECT proxy whose job is to give your outbound calls a fixed pair of IPs that a partner can allowlist. The mechanics are the ones in this post. The concepts page describes the details.

  • The proxy URL uses https:// on port 443, in the form https://APP_ID:TOKEN@mum.egress.fixedmark.com:443. Your client opens TLS to the proxy first, so the Proxy-Authorization header and token are encrypted on that hop.
  • For an HTTPS destination, the proxy checks the token and its destination allowlist, connects from your static IP, replies 200 Connection Established, and copies bytes. It reads the TLS SNI for connection logs and nothing else. The security page covers what is and is not stored.
  • A connection log row records the destination, the SNI when sent, bytes each way, the egress IP, and the result.
  • A plain HTTP proxy on port 8080 exists for clients that cannot open TLS to a proxy. On that port the token crosses the network in cleartext, exactly like the Basic header in the captures above.

To see the handshake against Fixedmark, run the same curl -v with your proxy URL and read the line after CONNECT. The troubleshooting guide lists what each reply means, and verify your egress IP confirms the call left from your pair.

Terminal
curl -v --proxy "$FIXEDMARK_PROXY_URL" https://api.partner.example/v1/orders

Sources

Checked November 2026. Confirm current details on the vendor's site before you decide. Prices and limits change.

Blog

Frequently asked questions

Does an HTTP CONNECT proxy decrypt HTTPS traffic?

Not on its own. After the 200 reply it copies bytes, and your client runs TLS with the destination inside the tunnel. Only a proxy that presents its own certificate, which your machine must trust, can read the traffic. Check the issuer in `curl -v` to tell the difference.

Is the Proxy-Authorization header sent to the website I am calling?

No. RFC 9110 says it applies only to the proxy that asked for authentication, and the proxy consumes it. The destination sees only the headers your client sends inside the TLS tunnel.

What is the difference between 407 and 401?

A 407 comes from the proxy and means your proxy credentials are missing or wrong. A 401 comes from the destination, through the tunnel, and means the API rejected its own credentials. With curl -v, a 407 appears before `CONNECT tunnel established` and a 401 appears after the TLS handshake.

Why does curl send CONNECT when my proxy URL starts with http://?

The proxy URL's scheme describes the hop to the proxy. The destination URL's scheme decides the method. An `https://` destination always gets a CONNECT tunnel, whether the proxy hop is plain HTTP or TLS.

Can I send a database connection through a CONNECT proxy?

The protocol allows any TCP traffic if the proxy permits the port. Most database drivers have no HTTP proxy support, though, and many proxies only allow port 443. SOCKS5 or a local port-forwarding tunnel is the usual route for databases.

Make it fixed.

Fixedmark is in early access. Join the list to get dedicated static IPs when your region opens.