Table of Contents

Class HttpConnection

Namespace
AppleTvControlLibrary.Mrp.AirPlay.Http
Assembly
AppleTvControlLibrary.Mrp.dll

A TCP-backed HTTP/RTSP request/response connection used for the AirPlay 2 control channel. Supports optional send/receive processors so HAP encryption can be layered on transparently once pair-verify has completed.

public sealed class HttpConnection : IDisposable
Inheritance
HttpConnection
Implements
Inherited Members

Remarks

Only the client-side subset needed to drive AirPlay 2 session setup is ported; the server-side pieces of pyatv's support/http.py (used for local file serving) are out of scope here.

Properties

LocalIp

Gets the local IP address of the connection.

public string LocalIp { get; }

Property Value

string

ReceiveProcessor

Gets or sets the processor applied to data as it is received (e.g. HAP decryption).

public Func<byte[], byte[]>? ReceiveProcessor { get; set; }

Property Value

Func<byte[], byte[]>

RemoteIp

Gets the remote IP address of the connection.

public string RemoteIp { get; }

Property Value

string

SendProcessor

Gets or sets the processor applied to data before it is sent (e.g. HAP encryption).

public Func<byte[], byte[]>? SendProcessor { get; set; }

Property Value

Func<byte[], byte[]>

Methods

ConnectAsync(string, int, CancellationToken)

Open a new connection to a remote host.

public static Task<HttpConnection> ConnectAsync(string address, int port, CancellationToken cancellationToken = default)

Parameters

address string

The remote address.

port int

The remote port.

cancellationToken CancellationToken

A token used to cancel the connection attempt.

Returns

Task<HttpConnection>

A connected HttpConnection.

Dispose()

Closes the underlying socket and stops the read loop.

public void Dispose()

GetAsync(string, bool, CancellationToken)

Send a GET request and wait for the response.

public Task<HttpResponse> GetAsync(string path, bool allowError = false, CancellationToken cancellationToken = default)

Parameters

path string

The request path.

allowError bool

If true, non-2xx responses are returned rather than throwing.

cancellationToken CancellationToken

A token used to cancel the exchange.

Returns

Task<HttpResponse>

PostAsync(string, IReadOnlyDictionary<string, string>?, byte[]?, bool, CancellationToken)

Send a POST request and wait for the response.

public Task<HttpResponse> PostAsync(string path, IReadOnlyDictionary<string, string>? headers = null, byte[]? body = null, bool allowError = false, CancellationToken cancellationToken = default)

Parameters

path string

The request path.

headers IReadOnlyDictionary<string, string>

Additional request headers.

body byte[]

The request body.

allowError bool

If true, non-2xx responses are returned rather than throwing.

cancellationToken CancellationToken

A token used to cancel the exchange.

Returns

Task<HttpResponse>

SendAndReceiveAsync(string, string, string, string, string?, IReadOnlyDictionary<string, string>?, byte[]?, bool, TimeSpan?, CancellationToken)

Send a request of the given method and wait for the matching response.

public Task<HttpResponse> SendAndReceiveAsync(string method, string uri, string protocol = "HTTP/1.1", string userAgent = "AppleTvControlLibrary", string? contentType = null, IReadOnlyDictionary<string, string>? headers = null, byte[]? body = null, bool allowError = false, TimeSpan? timeout = null, CancellationToken cancellationToken = default)

Parameters

method string

The request method.

uri string

The request path/URI.

protocol string

The protocol/version string, e.g. "HTTP/1.1" or "RTSP/1.0".

userAgent string

The value of the User-Agent header.

contentType string

An optional Content-Type header value.

headers IReadOnlyDictionary<string, string>

Additional request headers.

body byte[]

An optional request body.

allowError bool

If true, non-2xx responses are returned rather than throwing.

timeout TimeSpan?

How long to wait for a response before failing.

cancellationToken CancellationToken

A token used to cancel the exchange.

Returns

Task<HttpResponse>

The response to the request.