Function send [src]

Send the HTTP request headers to the server.

Prototype

pub fn send(req: *Request) SendError!void

Parameters

req: *Request

Possible Errors

ConnectionResetByPeer WriteError
InvalidContentLength
UnexpectedWriteFailure WriteError
UnsupportedTransferEncoding

Source

pub fn send(req: *Request) SendError!void { if (!req.method.requestHasBody() and req.transfer_encoding != .none) return error.UnsupportedTransferEncoding; const connection = req.connection.?; const w = connection.writer(); try req.method.write(w); try w.writeByte(' '); if (req.method == .CONNECT) { try req.uri.writeToStream(.{ .authority = true }, w); } else { try req.uri.writeToStream(.{ .scheme = connection.proxied, .authentication = connection.proxied, .authority = connection.proxied, .path = true, .query = true, }, w); } try w.writeByte(' '); try w.writeAll(@tagName(req.version)); try w.writeAll("\r\n"); if (try emitOverridableHeader("host: ", req.headers.host, w)) { try w.writeAll("host: "); try req.uri.writeToStream(.{ .authority = true }, w); try w.writeAll("\r\n"); } if (try emitOverridableHeader("authorization: ", req.headers.authorization, w)) { if (req.uri.user != null or req.uri.password != null) { try w.writeAll("authorization: "); const authorization = try connection.allocWriteBuffer( @intCast(basic_authorization.valueLengthFromUri(req.uri)), ); assert(basic_authorization.value(req.uri, authorization).len == authorization.len); try w.writeAll("\r\n"); } } if (try emitOverridableHeader("user-agent: ", req.headers.user_agent, w)) { try w.writeAll("user-agent: zig/"); try w.writeAll(builtin.zig_version_string); try w.writeAll(" (std.http)\r\n"); } if (try emitOverridableHeader("connection: ", req.headers.connection, w)) { if (req.keep_alive) { try w.writeAll("connection: keep-alive\r\n"); } else { try w.writeAll("connection: close\r\n"); } } if (try emitOverridableHeader("accept-encoding: ", req.headers.accept_encoding, w)) { // https://github.com/ziglang/zig/issues/18937 //try w.writeAll("accept-encoding: gzip, deflate, zstd\r\n"); try w.writeAll("accept-encoding: gzip, deflate\r\n"); } switch (req.transfer_encoding) { .chunked => try w.writeAll("transfer-encoding: chunked\r\n"), .content_length => |len| try w.print("content-length: {d}\r\n", .{len}), .none => {}, } if (try emitOverridableHeader("content-type: ", req.headers.content_type, w)) { // The default is to omit content-type if not provided because // "application/octet-stream" is redundant. } for (req.extra_headers) |header| { assert(header.name.len != 0); try w.writeAll(header.name); try w.writeAll(": "); try w.writeAll(header.value); try w.writeAll("\r\n"); } if (connection.proxied) proxy: { const proxy = switch (connection.protocol) { .plain => req.client.http_proxy, .tls => req.client.https_proxy, } orelse break :proxy; const authorization = proxy.authorization orelse break :proxy; try w.writeAll("proxy-authorization: "); try w.writeAll(authorization); try w.writeAll("\r\n"); } try w.writeAll("\r\n"); try connection.flush(); }