Table of Contents

Class HttpProxyServer

Namespace
VpnHood.Net.Proxies.HttpProxyServers
Assembly
VpnHood.Net.Proxies.dll

Represents an HTTP proxy server that opens CONNECT tunnels and forwards plain HTTP requests, with optional Basic authentication.

public sealed class HttpProxyServer : TcpProxyServerBase, IDisposable
Inheritance
HttpProxyServer
Implements
Inherited Members

Examples

using var server = new HttpProxyServer(new HttpProxyServerOptions {
    ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0), // 0: the system picks a free port
    Username = "user", // set a user name to require Basic authentication
    Password = "pass"
});

server.Start();
var proxyEndPoint = server.ListenerEndPoint;

Remarks

A request with an absolute http: URI, such as GET http://example.com/ HTTP/1.1, goes to its origin with the client's header lines as they came, minus the hop-by-hop ones (RFC 9110, section 7.6.1), and with Connection: close, so each such request takes a client connection of its own. A request whose URI is not absolute gets 400 Bad Request; one with another scheme, such as https:, gets 501 Not Implemented, as HTTPS goes through CONNECT; and one whose origin cannot be reached gets 502 Bad Gateway, or 504 Gateway Timeout when the connection attempt runs out of time. A request rejected while it is parsed may get 400 Bad Request or be closed without an answer.

Constructors

HttpProxyServer(HttpProxyServerOptions, ILogger<HttpProxyServer>?)

Represents an HTTP proxy server that opens CONNECT tunnels and forwards plain HTTP requests, with optional Basic authentication.

public HttpProxyServer(HttpProxyServerOptions options, ILogger<HttpProxyServer>? logger = null)

Parameters

options HttpProxyServerOptions

The server's settings. ServerCertificate is ignored.

logger ILogger<HttpProxyServer>

The logger for the server's events, or null for none.

Examples

using var server = new HttpProxyServer(new HttpProxyServerOptions {
    ListenEndPoint = new IPEndPoint(IPAddress.Loopback, 0), // 0: the system picks a free port
    Username = "user", // set a user name to require Basic authentication
    Password = "pass"
});

server.Start();
var proxyEndPoint = server.ListenerEndPoint;

Remarks

A request with an absolute http: URI, such as GET http://example.com/ HTTP/1.1, goes to its origin with the client's header lines as they came, minus the hop-by-hop ones (RFC 9110, section 7.6.1), and with Connection: close, so each such request takes a client connection of its own. A request whose URI is not absolute gets 400 Bad Request; one with another scheme, such as https:, gets 501 Not Implemented, as HTTPS goes through CONNECT; and one whose origin cannot be reached gets 502 Bad Gateway, or 504 Gateway Timeout when the connection attempt runs out of time. A request rejected while it is parsed may get 400 Bad Request or be closed without an answer.

Exceptions

ArgumentNullException

ListenEndPoint is null.

ArgumentOutOfRangeException

Backlog or MaxConnections is zero or negative.

Methods

HandleClientAsync(TcpClient, CancellationToken)

Serves one client connection.

protected override Task HandleClientAsync(TcpClient client, CancellationToken serverCancellationToken)

Parameters

client TcpClient

The accepted connection.

serverCancellationToken CancellationToken

Returns

Task

A task that represents the asynchronous operation. It completes when the connection is done.

Remarks

It runs on its own task for each accepted connection. When the returned task completes, the base class disposes client, and it logs any exception the task throws.

Exceptions

ArgumentNullException

ListenEndPoint is null.

ArgumentOutOfRangeException

Backlog or MaxConnections is zero or negative.