Function open [src]

Open a connection to the host specified by uri and prepare to send a HTTP request. uri must remain alive during the entire request. The caller is responsible for calling deinit() on the Request. This function is threadsafe. Asserts that "\r\n" does not occur in any header name or value.

Prototype

pub fn open( client: *Client, method: http.Method, uri: Uri, options: RequestOptions, ) RequestError!Request

Parameters

client: *Clientmethod: http.Methoduri: Urioptions: RequestOptions

Possible Errors

CertificateBundleLoadFailure
ConnectionRefused ConnectTcpError
ConnectionResetByPeer ConnectTcpError
ConnectionTimedOut ConnectTcpError
HostLacksNetworkAddresses ConnectTcpError
InvalidCharacter ParseIntError

The input was empty or contained an invalid character

InvalidContentLength SendError
NameServerFailure ConnectTcpError
NetworkUnreachable ConnectTcpError
OutOfMemory Error
Overflow ParseIntError

The result cannot fit in the type specified

TemporaryNameServerFailure ConnectTcpError
TlsInitializationFailed ConnectTcpError
UnexpectedConnectFailure ConnectTcpError
UnexpectedWriteFailure WriteError
UnknownHostName ConnectTcpError
UnsupportedTransferEncoding SendError
UnsupportedUriScheme ConnectErrorPartial
UriMissingHost

Source

pub fn open( client: *Client, method: http.Method, uri: Uri, options: RequestOptions, ) RequestError!Request { if (std.debug.runtime_safety) { for (options.extra_headers) |header| { assert(header.name.len != 0); assert(std.mem.indexOfScalar(u8, header.name, ':') == null); assert(std.mem.indexOfPosLinear(u8, header.name, 0, "\r\n") == null); assert(std.mem.indexOfPosLinear(u8, header.value, 0, "\r\n") == null); } for (options.privileged_headers) |header| { assert(header.name.len != 0); assert(std.mem.indexOfPosLinear(u8, header.name, 0, "\r\n") == null); assert(std.mem.indexOfPosLinear(u8, header.value, 0, "\r\n") == null); } } var server_header: std.heap.FixedBufferAllocator = .init(options.server_header_buffer); const protocol, const valid_uri = try validateUri(uri, server_header.allocator()); if (protocol == .tls and @atomicLoad(bool, &client.next_https_rescan_certs, .acquire)) { if (disable_tls) unreachable; client.ca_bundle_mutex.lock(); defer client.ca_bundle_mutex.unlock(); if (client.next_https_rescan_certs) { client.ca_bundle.rescan(client.allocator) catch return error.CertificateBundleLoadFailure; @atomicStore(bool, &client.next_https_rescan_certs, false, .release); } } const conn = options.connection orelse try client.connect(valid_uri.host.?.raw, uriPort(valid_uri, protocol), protocol); var req: Request = .{ .uri = valid_uri, .client = client, .connection = conn, .keep_alive = options.keep_alive, .method = method, .version = options.version, .transfer_encoding = .none, .redirect_behavior = options.redirect_behavior, .handle_continue = options.handle_continue, .response = .{ .version = undefined, .status = undefined, .reason = undefined, .keep_alive = undefined, .parser = .init(server_header.buffer[server_header.end_index..]), }, .headers = options.headers, .extra_headers = options.extra_headers, .privileged_headers = options.privileged_headers, }; errdefer req.deinit(); return req; }