authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-22 17:48:03-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-10-22 17:48:03-04:00
logb82459fa435c366c6af0fee96c3d9b95c24078f9
tree771d71234a09b362ae716f53fb6482cdcdf27eb7
parent33483407a26a49db60bea039b40931cc77b10453
parent93e1f8c8e583b3140bc1985e8b346fd7aca8cf6b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #17407 from truemedian/http-ng

std.http: more proxy support, buffer writes, tls toggle

12 files changed, 769 insertions(+), 371 deletions(-)

lib/std/Uri.zig+79-33
......@@ -208,24 +208,45 @@ pub fn parseWithoutScheme(text: []const u8) ParseError!Uri {
208208 return uri;
209209}
210210
211pub fn format(
211pub const WriteToStreamOptions = struct {
212 /// When true, include the scheme part of the URI.
213 scheme: bool = false,
214
215 /// When true, include the user and password part of the URI. Ignored if `authority` is false.
216 authentication: bool = false,
217
218 /// When true, include the authority part of the URI.
219 authority: bool = false,
220
221 /// When true, include the path part of the URI.
222 path: bool = false,
223
224 /// When true, include the query part of the URI. Ignored when `path` is false.
225 query: bool = false,
226
227 /// When true, include the fragment part of the URI. Ignored when `path` is false.
228 fragment: bool = false,
229
230 /// When true, do not escape any part of the URI.
231 raw: bool = false,
232};
233
234pub fn writeToStream(
212235 uri: Uri,
213 comptime fmt: []const u8,
214 options: std.fmt.FormatOptions,
236 options: WriteToStreamOptions,
215237 writer: anytype,
216238) @TypeOf(writer).Error!void {
217 _ = options;
218
219 const needs_absolute = comptime std.mem.indexOf(u8, fmt, "+") != null;
220 const needs_path = comptime std.mem.indexOf(u8, fmt, "/") != null or fmt.len == 0;
221 const raw_uri = comptime std.mem.indexOf(u8, fmt, "r") != null;
222 const needs_fragment = comptime std.mem.indexOf(u8, fmt, "#") != null;
223
224 if (needs_absolute) {
239 if (options.scheme) {
225240 try writer.writeAll(uri.scheme);
226241 try writer.writeAll(":");
227 if (uri.host) |host| {
242
243 if (options.authority and uri.host != null) {
228244 try writer.writeAll("//");
245 }
246 }
247
248 if (options.authority) {
249 if (options.authentication and uri.host != null) {
229250 if (uri.user) |user| {
230251 try writer.writeAll(user);
231252 if (uri.password) |password| {
......@@ -234,7 +255,9 @@ pub fn format(
234255 }
235256 try writer.writeAll("@");
236257 }
258 }
237259
260 if (uri.host) |host| {
238261 try writer.writeAll(host);
239262
240263 if (uri.port) |port| {
......@@ -244,39 +267,62 @@ pub fn format(
244267 }
245268 }
246269
247 if (needs_path) {
270 if (options.path) {
248271 if (uri.path.len == 0) {
249272 try writer.writeAll("/");
273 } else if (options.raw) {
274 try writer.writeAll(uri.path);
250275 } else {
251 if (raw_uri) {
252 try writer.writeAll(uri.path);
253 } else {
254 try Uri.writeEscapedPath(writer, uri.path);
255 }
276 try writeEscapedPath(writer, uri.path);
256277 }
257278
258 if (uri.query) |q| {
279 if (options.query) if (uri.query) |q| {
259280 try writer.writeAll("?");
260 if (raw_uri) {
281 if (options.raw) {
261282 try writer.writeAll(q);
262283 } else {
263 try Uri.writeEscapedQuery(writer, q);
284 try writeEscapedQuery(writer, q);
264285 }
265 }
286 };
266287
267 if (needs_fragment) {
268 if (uri.fragment) |f| {
269 try writer.writeAll("#");
270 if (raw_uri) {
271 try writer.writeAll(f);
272 } else {
273 try Uri.writeEscapedQuery(writer, f);
274 }
288 if (options.fragment) if (uri.fragment) |f| {
289 try writer.writeAll("#");
290 if (options.raw) {
291 try writer.writeAll(f);
292 } else {
293 try writeEscapedQuery(writer, f);
275294 }
276 }
295 };
277296 }
278297}
279298
299pub fn format(
300 uri: Uri,
301 comptime fmt: []const u8,
302 options: std.fmt.FormatOptions,
303 writer: anytype,
304) @TypeOf(writer).Error!void {
305 _ = options;
306
307 const scheme = comptime std.mem.indexOf(u8, fmt, ":") != null or fmt.len == 0;
308 const authentication = comptime std.mem.indexOf(u8, fmt, "@") != null or fmt.len == 0;
309 const authority = comptime std.mem.indexOf(u8, fmt, "+") != null or fmt.len == 0;
310 const path = comptime std.mem.indexOf(u8, fmt, "/") != null or fmt.len == 0;
311 const query = comptime std.mem.indexOf(u8, fmt, "?") != null or fmt.len == 0;
312 const fragment = comptime std.mem.indexOf(u8, fmt, "#") != null or fmt.len == 0;
313 const raw = comptime std.mem.indexOf(u8, fmt, "r") != null or fmt.len == 0;
314
315 return writeToStream(uri, .{
316 .scheme = scheme,
317 .authentication = authentication,
318 .authority = authority,
319 .path = path,
320 .query = query,
321 .fragment = fragment,
322 .raw = raw,
323 }, writer);
324}
325
280326/// Parses the URI or returns an error.
281327/// The return value will contain unescaped strings pointing into the
282328/// original `text`. Each component that is provided, will be non-`null`.
......@@ -711,7 +757,7 @@ test "URI query escaping" {
711757 const parsed = try Uri.parse(address);
712758
713759 // format the URI to escape it
714 const formatted_uri = try std.fmt.allocPrint(std.testing.allocator, "{}", .{parsed});
760 const formatted_uri = try std.fmt.allocPrint(std.testing.allocator, "{/?}", .{parsed});
715761 defer std.testing.allocator.free(formatted_uri);
716762 try std.testing.expectEqualStrings("/?response-content-type=application%2Foctet-stream", formatted_uri);
717763}
......@@ -729,6 +775,6 @@ test "format" {
729775 };
730776 var buf = std.ArrayList(u8).init(std.testing.allocator);
731777 defer buf.deinit();
732 try uri.format("+/", .{}, buf.writer());
778 try uri.format(":/?#", .{}, buf.writer());
733779 try std.testing.expectEqualSlices(u8, "file:/foo/bar/baz", buf.items);
734780}
lib/std/crypto/tls/Client.zig+1-1
......@@ -881,7 +881,7 @@ pub fn readAll(c: *Client, stream: anytype, buffer: []u8) !usize {
881881/// The `iovecs` parameter is mutable because this function needs to mutate the fields in
882882/// order to handle partial reads from the underlying stream layer.
883883pub fn readv(c: *Client, stream: anytype, iovecs: []std.os.iovec) !usize {
884 return readvAtLeast(c, stream, iovecs);
884 return readvAtLeast(c, stream, iovecs, 1);
885885}
886886
887887/// Receives TLS-encrypted data from `stream`, which must conform to `StreamInterface`.
lib/std/http.zig+5-1
......@@ -35,7 +35,8 @@ pub const Method = enum(u64) { // TODO: should be u192 or u256, but neither is s
3535 /// Asserts that `s` is 24 or fewer bytes.
3636 pub fn parse(s: []const u8) u64 {
3737 var x: u64 = 0;
38 @memcpy(std.mem.asBytes(&x)[0..s.len], s);
38 const len = @min(s.len, @sizeOf(@TypeOf(x)));
39 @memcpy(std.mem.asBytes(&x)[0..len], s[0..len]);
3940 return x;
4041 }
4142
......@@ -289,14 +290,17 @@ pub const Status = enum(u10) {
289290
290291pub const TransferEncoding = enum {
291292 chunked,
293 none,
292294 // compression is intentionally omitted here, as std.http.Client stores it as content-encoding
293295};
294296
295297pub const ContentEncoding = enum {
296298 identity,
297299 compress,
300 @"x-compress",
298301 deflate,
299302 gzip,
303 @"x-gzip",
300304 zstd,
301305};
302306
lib/std/http/Client.zig+540-222
......@@ -13,12 +13,16 @@ const assert = std.debug.assert;
1313const Client = @This();
1414const proto = @import("protocol.zig");
1515
16pub const default_connection_pool_size = 32;
17pub const connection_pool_size = std.options.http_connection_pool_size;
16pub const disable_tls = std.options.http_disable_tls;
1817
18/// Allocator used for all allocations made by the client.
19///
20/// This allocator must be thread-safe.
1921allocator: Allocator,
20ca_bundle: std.crypto.Certificate.Bundle = .{},
22
23ca_bundle: if (disable_tls) void else std.crypto.Certificate.Bundle = if (disable_tls) {} else .{},
2124ca_bundle_mutex: std.Thread.Mutex = .{},
25
2226/// When this is `true`, the next time this client performs an HTTPS request,
2327/// it will first rescan the system for root certificates.
2428next_https_rescan_certs: bool = true,
......@@ -26,7 +30,11 @@ next_https_rescan_certs: bool = true,
2630/// The pool of connections that can be reused (and currently in use).
2731connection_pool: ConnectionPool = .{},
2832
29proxy: ?HttpProxy = null,
33/// This is the proxy that will handle http:// connections. It *must not* be modified when the client has any active connections.
34http_proxy: ?Proxy = null,
35
36/// This is the proxy that will handle https:// connections. It *must not* be modified when the client has any active connections.
37https_proxy: ?Proxy = null,
3038
3139/// A set of linked lists of connections that can be reused.
3240pub const ConnectionPool = struct {
......@@ -34,7 +42,7 @@ pub const ConnectionPool = struct {
3442 pub const Criteria = struct {
3543 host: []const u8,
3644 port: u16,
37 is_tls: bool,
45 protocol: Connection.Protocol,
3846 };
3947
4048 const Queue = std.DoublyLinkedList(Connection);
......@@ -46,22 +54,24 @@ pub const ConnectionPool = struct {
4654 /// Open connections that are not currently in use.
4755 free: Queue = .{},
4856 free_len: usize = 0,
49 free_size: usize = connection_pool_size,
57 free_size: usize = 32,
5058
5159 /// Finds and acquires a connection from the connection pool matching the criteria. This function is threadsafe.
5260 /// If no connection is found, null is returned.
53 pub fn findConnection(pool: *ConnectionPool, criteria: Criteria) ?*Node {
61 pub fn findConnection(pool: *ConnectionPool, criteria: Criteria) ?*Connection {
5462 pool.mutex.lock();
5563 defer pool.mutex.unlock();
5664
5765 var next = pool.free.last;
5866 while (next) |node| : (next = node.prev) {
59 if ((node.data.protocol == .tls) != criteria.is_tls) continue;
67 if (node.data.protocol != criteria.protocol) continue;
6068 if (node.data.port != criteria.port) continue;
61 if (!mem.eql(u8, node.data.host, criteria.host)) continue;
69
70 // Domain names are case-insensitive (RFC 5890, Section 2.3.2.4)
71 if (!std.ascii.eqlIgnoreCase(node.data.host, criteria.host)) continue;
6272
6373 pool.acquireUnsafe(node);
64 return node;
74 return &node.data;
6575 }
6676
6777 return null;
......@@ -85,23 +95,28 @@ pub const ConnectionPool = struct {
8595
8696 /// Tries to release a connection back to the connection pool. This function is threadsafe.
8797 /// If the connection is marked as closing, it will be closed instead.
88 pub fn release(pool: *ConnectionPool, client: *Client, node: *Node) void {
98 ///
99 /// The allocator must be the owner of all nodes in this pool.
100 /// The allocator must be the owner of all resources associated with the connection.
101 pub fn release(pool: *ConnectionPool, allocator: Allocator, connection: *Connection) void {
89102 pool.mutex.lock();
90103 defer pool.mutex.unlock();
91104
105 const node = @fieldParentPtr(Node, "data", connection);
106
92107 pool.used.remove(node);
93108
94 if (node.data.closing) {
95 node.data.deinit(client);
96 return client.allocator.destroy(node);
109 if (node.data.closing or pool.free_size == 0) {
110 node.data.close(allocator);
111 return allocator.destroy(node);
97112 }
98113
99114 if (pool.free_len >= pool.free_size) {
100115 const popped = pool.free.popFirst() orelse unreachable;
101116 pool.free_len -= 1;
102117
103 popped.data.deinit(client);
104 client.allocator.destroy(popped);
118 popped.data.close(allocator);
119 allocator.destroy(popped);
105120 }
106121
107122 if (node.data.proxied) {
......@@ -121,23 +136,43 @@ pub const ConnectionPool = struct {
121136 pool.used.append(node);
122137 }
123138
124 pub fn deinit(pool: *ConnectionPool, client: *Client) void {
139 /// Resizes the connection pool. This function is threadsafe.
140 ///
141 /// If the new size is smaller than the current size, then idle connections will be closed until the pool is the new size.
142 pub fn resize(pool: *ConnectionPool, allocator: Allocator, new_size: usize) void {
143 pool.mutex.lock();
144 defer pool.mutex.unlock();
145
146 var next = pool.free.first;
147 _ = next;
148 while (pool.free_len > new_size) {
149 const popped = pool.free.popFirst() orelse unreachable;
150 pool.free_len -= 1;
151
152 popped.data.close(allocator);
153 allocator.destroy(popped);
154 }
155
156 pool.free_size = new_size;
157 }
158
159 pub fn deinit(pool: *ConnectionPool, allocator: Allocator) void {
125160 pool.mutex.lock();
126161
127162 var next = pool.free.first;
128163 while (next) |node| {
129 defer client.allocator.destroy(node);
164 defer allocator.destroy(node);
130165 next = node.next;
131166
132 node.data.deinit(client);
167 node.data.close(allocator);
133168 }
134169
135170 next = pool.used.first;
136171 while (next) |node| {
137 defer client.allocator.destroy(node);
172 defer allocator.destroy(node);
138173 next = node.next;
139174
140 node.data.deinit(client);
175 node.data.close(allocator);
141176 }
142177
143178 pool.* = undefined;
......@@ -147,11 +182,13 @@ pub const ConnectionPool = struct {
147182/// An interface to either a plain or TLS connection.
148183pub const Connection = struct {
149184 pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
185 const BufferSize = std.math.IntFittingRange(0, buffer_size);
186
150187 pub const Protocol = enum { plain, tls };
151188
152189 stream: net.Stream,
153190 /// undefined unless protocol is tls.
154 tls_client: *std.crypto.tls.Client,
191 tls_client: if (!disable_tls) *std.crypto.tls.Client else void,
155192
156193 protocol: Protocol,
157194 host: []u8,
......@@ -160,16 +197,15 @@ pub const Connection = struct {
160197 proxied: bool = false,
161198 closing: bool = false,
162199
163 read_start: u16 = 0,
164 read_end: u16 = 0,
200 read_start: BufferSize = 0,
201 read_end: BufferSize = 0,
202 write_end: BufferSize = 0,
165203 read_buf: [buffer_size]u8 = undefined,
204 write_buf: [buffer_size]u8 = undefined,
166205
167 pub fn rawReadAtLeast(conn: *Connection, buffer: []u8, len: usize) ReadError!usize {
168 return switch (conn.protocol) {
169 .plain => conn.stream.readAtLeast(buffer, len),
170 .tls => conn.tls_client.readAtLeast(conn.stream, buffer, len),
171 } catch |err| {
172 // TODO: https://github.com/ziglang/zig/issues/2473
206 pub fn readvDirectTls(conn: *Connection, buffers: []std.os.iovec) ReadError!usize {
207 return conn.tls_client.readv(conn.stream, buffers) catch |err| {
208 // https://github.com/ziglang/zig/issues/2473
173209 if (mem.startsWith(u8, @errorName(err), "TlsAlert")) return error.TlsAlert;
174210
175211 switch (err) {
......@@ -181,61 +217,69 @@ pub const Connection = struct {
181217 };
182218 }
183219
220 pub fn readvDirect(conn: *Connection, buffers: []std.os.iovec) ReadError!usize {
221 if (conn.protocol == .tls) {
222 if (disable_tls) unreachable;
223
224 return conn.readvDirectTls(buffers);
225 }
226
227 return conn.stream.readv(buffers) catch |err| switch (err) {
228 error.ConnectionTimedOut => return error.ConnectionTimedOut,
229 error.ConnectionResetByPeer, error.BrokenPipe => return error.ConnectionResetByPeer,
230 else => return error.UnexpectedReadFailure,
231 };
232 }
233
184234 pub fn fill(conn: *Connection) ReadError!void {
185235 if (conn.read_end != conn.read_start) return;
186236
187 const nread = try conn.rawReadAtLeast(conn.read_buf[0..], 1);
237 var iovecs = [1]std.os.iovec{
238 .{ .iov_base = &conn.read_buf, .iov_len = conn.read_buf.len },
239 };
240 const nread = try conn.readvDirect(&iovecs);
188241 if (nread == 0) return error.EndOfStream;
189242 conn.read_start = 0;
190 conn.read_end = @as(u16, @intCast(nread));
243 conn.read_end = @intCast(nread);
191244 }
192245
193246 pub fn peek(conn: *Connection) []const u8 {
194247 return conn.read_buf[conn.read_start..conn.read_end];
195248 }
196249
197 pub fn drop(conn: *Connection, num: u16) void {
250 pub fn drop(conn: *Connection, num: BufferSize) void {
198251 conn.read_start += num;
199252 }
200253
201 pub fn readAtLeast(conn: *Connection, buffer: []u8, len: usize) ReadError!usize {
202 assert(len <= buffer.len);
203
204 var out_index: u16 = 0;
205 while (out_index < len) {
206 const available_read = conn.read_end - conn.read_start;
207 const available_buffer = buffer.len - out_index;
208
209 if (available_read > available_buffer) { // partially read buffered data
210 @memcpy(buffer[out_index..], conn.read_buf[conn.read_start..conn.read_end][0..available_buffer]);
211 out_index += @as(u16, @intCast(available_buffer));
212 conn.read_start += @as(u16, @intCast(available_buffer));
254 pub fn read(conn: *Connection, buffer: []u8) ReadError!usize {
255 const available_read = conn.read_end - conn.read_start;
256 const available_buffer = buffer.len;
213257
214 break;
215 } else if (available_read > 0) { // fully read buffered data
216 @memcpy(buffer[out_index..][0..available_read], conn.read_buf[conn.read_start..conn.read_end]);
217 out_index += available_read;
218 conn.read_start += available_read;
258 if (available_read > available_buffer) { // partially read buffered data
259 @memcpy(buffer[0..available_buffer], conn.read_buf[conn.read_start..conn.read_end][0..available_buffer]);
260 conn.read_start += @intCast(available_buffer);
219261
220 if (out_index >= len) break;
221 }
262 return available_buffer;
263 } else if (available_read > 0) { // fully read buffered data
264 @memcpy(buffer[0..available_read], conn.read_buf[conn.read_start..conn.read_end]);
265 conn.read_start += available_read;
222266
223 const leftover_buffer = available_buffer - available_read;
224 const leftover_len = len - out_index;
267 return available_read;
268 }
225269
226 if (leftover_buffer > conn.read_buf.len) {
227 // skip the buffer if the output is large enough
228 return conn.rawReadAtLeast(buffer[out_index..], leftover_len);
229 }
270 var iovecs = [2]std.os.iovec{
271 .{ .iov_base = buffer.ptr, .iov_len = buffer.len },
272 .{ .iov_base = &conn.read_buf, .iov_len = conn.read_buf.len },
273 };
274 const nread = try conn.readvDirect(&iovecs);
230275
231 try conn.fill();
276 if (nread > buffer.len) {
277 conn.read_start = 0;
278 conn.read_end = @intCast(nread - buffer.len);
279 return buffer.len;
232280 }
233281
234 return out_index;
235 }
236
237 pub fn read(conn: *Connection, buffer: []u8) ReadError!usize {
238 return conn.readAtLeast(buffer, 1);
282 return nread;
239283 }
240284
241285 pub const ReadError = error{
......@@ -253,26 +297,49 @@ pub const Connection = struct {
253297 return Reader{ .context = conn };
254298 }
255299
256 pub fn writeAll(conn: *Connection, buffer: []const u8) !void {
257 return switch (conn.protocol) {
258 .plain => conn.stream.writeAll(buffer),
259 .tls => conn.tls_client.writeAll(conn.stream, buffer),
260 } catch |err| switch (err) {
300 pub fn writeAllDirectTls(conn: *Connection, buffer: []const u8) WriteError!void {
301 return conn.tls_client.writeAll(conn.stream, buffer) catch |err| switch (err) {
261302 error.BrokenPipe, error.ConnectionResetByPeer => return error.ConnectionResetByPeer,
262303 else => return error.UnexpectedWriteFailure,
263304 };
264305 }
265306
266 pub fn write(conn: *Connection, buffer: []const u8) !usize {
267 return switch (conn.protocol) {
268 .plain => conn.stream.write(buffer),
269 .tls => conn.tls_client.write(conn.stream, buffer),
270 } catch |err| switch (err) {
307 pub fn writeAllDirect(conn: *Connection, buffer: []const u8) WriteError!void {
308 if (conn.protocol == .tls) {
309 if (disable_tls) unreachable;
310
311 return conn.writeAllDirectTls(buffer);
312 }
313
314 return conn.stream.writeAll(buffer) catch |err| switch (err) {
271315 error.BrokenPipe, error.ConnectionResetByPeer => return error.ConnectionResetByPeer,
272316 else => return error.UnexpectedWriteFailure,
273317 };
274318 }
275319
320 pub fn write(conn: *Connection, buffer: []const u8) WriteError!usize {
321 if (conn.write_end + buffer.len > conn.write_buf.len) {
322 try conn.flush();
323
324 if (buffer.len > conn.write_buf.len) {
325 try conn.writeAllDirect(buffer);
326 return buffer.len;
327 }
328 }
329
330 @memcpy(conn.write_buf[conn.write_end..][0..buffer.len], buffer);
331 conn.write_end += @intCast(buffer.len);
332
333 return buffer.len;
334 }
335
336 pub fn flush(conn: *Connection) WriteError!void {
337 if (conn.write_end == 0) return;
338
339 try conn.writeAllDirect(conn.write_buf[0..conn.write_end]);
340 conn.write_end = 0;
341 }
342
276343 pub const WriteError = error{
277344 ConnectionResetByPeer,
278345 UnexpectedWriteFailure,
......@@ -284,19 +351,17 @@ pub const Connection = struct {
284351 return Writer{ .context = conn };
285352 }
286353
287 pub fn close(conn: *Connection, client: *const Client) void {
354 pub fn close(conn: *Connection, allocator: Allocator) void {
288355 if (conn.protocol == .tls) {
356 if (disable_tls) unreachable;
357
289358 // try to cleanly close the TLS connection, for any server that cares.
290359 _ = conn.tls_client.writeEnd(conn.stream, "", true) catch {};
291 client.allocator.destroy(conn.tls_client);
360 allocator.destroy(conn.tls_client);
292361 }
293362
294363 conn.stream.close();
295 }
296
297 pub fn deinit(conn: *Connection, client: *const Client) void {
298 conn.close(client);
299 client.allocator.free(conn.host);
364 allocator.free(conn.host);
300365 }
301366};
302367
......@@ -331,7 +396,7 @@ pub const Response = struct {
331396 };
332397
333398 pub fn parse(res: *Response, bytes: []const u8, trailing: bool) ParseError!void {
334 var it = mem.tokenizeAny(u8, bytes[0 .. bytes.len - 4], "\r\n");
399 var it = mem.tokenizeAny(u8, bytes, "\r\n");
335400
336401 const first_line = it.next() orelse return error.HttpHeadersInvalid;
337402 if (first_line.len < 12)
......@@ -350,6 +415,8 @@ pub const Response = struct {
350415 res.status = status;
351416 res.reason = reason;
352417
418 res.headers.clearRetainingCapacity();
419
353420 while (it.next()) |line| {
354421 if (line.len == 0) return error.HttpHeadersInvalid;
355422 switch (line[0]) {
......@@ -365,46 +432,42 @@ pub const Response = struct {
365432
366433 if (trailing) continue;
367434
368 if (std.ascii.eqlIgnoreCase(header_name, "content-length")) {
369 const content_length = std.fmt.parseInt(u64, header_value, 10) catch return error.InvalidContentLength;
370
371 if (res.content_length != null and res.content_length != content_length) return error.HttpHeadersInvalid;
372
373 res.content_length = content_length;
374 } else if (std.ascii.eqlIgnoreCase(header_name, "transfer-encoding")) {
435 if (std.ascii.eqlIgnoreCase(header_name, "transfer-encoding")) {
375436 // Transfer-Encoding: second, first
376437 // Transfer-Encoding: deflate, chunked
377438 var iter = mem.splitBackwardsScalar(u8, header_value, ',');
378439
379 if (iter.next()) |first| {
380 const trimmed = mem.trim(u8, first, " ");
440 const first = iter.first();
441 const trimmed_first = mem.trim(u8, first, " ");
381442
382 if (std.meta.stringToEnum(http.TransferEncoding, trimmed)) |te| {
383 if (res.transfer_encoding != null) return error.HttpHeadersInvalid;
384 res.transfer_encoding = te;
385 } else if (std.meta.stringToEnum(http.ContentEncoding, trimmed)) |ce| {
386 if (res.transfer_compression != null) return error.HttpHeadersInvalid;
387 res.transfer_compression = ce;
388 } else {
389 return error.HttpTransferEncodingUnsupported;
390 }
391 }
443 var next: ?[]const u8 = first;
444 if (std.meta.stringToEnum(http.TransferEncoding, trimmed_first)) |transfer| {
445 if (res.transfer_encoding != .none) return error.HttpHeadersInvalid; // we already have a transfer encoding
446 res.transfer_encoding = transfer;
392447
393 if (iter.next()) |second| {
394 if (res.transfer_compression != null) return error.HttpTransferEncodingUnsupported;
448 next = iter.next();
449 }
395450
396 const trimmed = mem.trim(u8, second, " ");
451 if (next) |second| {
452 const trimmed_second = mem.trim(u8, second, " ");
397453
398 if (std.meta.stringToEnum(http.ContentEncoding, trimmed)) |ce| {
399 res.transfer_compression = ce;
454 if (std.meta.stringToEnum(http.ContentEncoding, trimmed_second)) |transfer| {
455 if (res.transfer_compression != .identity) return error.HttpHeadersInvalid; // double compression is not supported
456 res.transfer_compression = transfer;
400457 } else {
401458 return error.HttpTransferEncodingUnsupported;
402459 }
403460 }
404461
405462 if (iter.next()) |_| return error.HttpTransferEncodingUnsupported;
463 } else if (std.ascii.eqlIgnoreCase(header_name, "content-length")) {
464 const content_length = std.fmt.parseInt(u64, header_value, 10) catch return error.InvalidContentLength;
465
466 if (res.content_length != null and res.content_length != content_length) return error.HttpHeadersInvalid;
467
468 res.content_length = content_length;
406469 } else if (std.ascii.eqlIgnoreCase(header_name, "content-encoding")) {
407 if (res.transfer_compression != null) return error.HttpHeadersInvalid;
470 if (res.transfer_compression != .identity) return error.HttpHeadersInvalid;
408471
409472 const trimmed = mem.trim(u8, header_value, " ");
410473
......@@ -440,13 +503,21 @@ pub const Response = struct {
440503 status: http.Status,
441504 reason: []const u8,
442505
506 /// If present, the number of bytes in the response body.
443507 content_length: ?u64 = null,
444 transfer_encoding: ?http.TransferEncoding = null,
445 transfer_compression: ?http.ContentEncoding = null,
446508
509 /// If present, the transfer encoding of the response body, otherwise none.
510 transfer_encoding: http.TransferEncoding = .none,
511
512 /// If present, the compression of the response body, otherwise identity (no compression).
513 transfer_compression: http.ContentEncoding = .identity,
514
515 /// The headers received from the server.
447516 headers: http.Headers,
448517 parser: proto.HeadersParser,
449518 compression: Compression = .none,
519
520 /// Whether the response body should be skipped. Any data read from the response body will be discarded.
450521 skip: bool = false,
451522};
452523
......@@ -457,15 +528,18 @@ pub const Request = struct {
457528 uri: Uri,
458529 client: *Client,
459530 /// is null when this connection is released
460 connection: ?*ConnectionPool.Node,
531 connection: ?*Connection,
461532
462533 method: http.Method,
463534 version: http.Version = .@"HTTP/1.1",
464535 headers: http.Headers,
536
537 /// The transfer encoding of the request body.
465538 transfer_encoding: RequestTransfer = .none,
466539
467540 redirects_left: u32,
468541 handle_redirects: bool,
542 handle_continue: bool,
469543
470544 response: Response,
471545
......@@ -491,9 +565,9 @@ pub const Request = struct {
491565 if (req.connection) |connection| {
492566 if (!req.response.parser.done) {
493567 // If the response wasn't fully read, then we need to close the connection.
494 connection.data.closing = true;
568 connection.closing = true;
495569 }
496 req.client.connection_pool.release(req.client, connection);
570 req.client.connection_pool.release(req.client.allocator, connection);
497571 }
498572
499573 req.arena.deinit();
......@@ -512,7 +586,7 @@ pub const Request = struct {
512586 .zstd => |*zstd| zstd.deinit(),
513587 }
514588
515 req.client.connection_pool.release(req.client, req.connection.?);
589 req.client.connection_pool.release(req.client.allocator, req.connection.?);
516590 req.connection = null;
517591
518592 const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUrlScheme;
......@@ -539,42 +613,33 @@ pub const Request = struct {
539613 };
540614 }
541615
542 pub const StartError = Connection.WriteError || error{ InvalidContentLength, UnsupportedTransferEncoding };
616 pub const SendError = Connection.WriteError || error{ InvalidContentLength, UnsupportedTransferEncoding };
543617
544 pub const StartOptions = struct {
545 /// Specifies that the uri should be used as is
618 pub const SendOptions = struct {
619 /// Specifies that the uri should be used as is. You guarantee that the uri is already escaped.
546620 raw_uri: bool = false,
547621 };
548622
549 /// Send the request to the server.
550 pub fn start(req: *Request, options: StartOptions) StartError!void {
623 /// Send the HTTP request headers to the server.
624 pub fn send(req: *Request, options: SendOptions) SendError!void {
551625 if (!req.method.requestHasBody() and req.transfer_encoding != .none) return error.UnsupportedTransferEncoding;
552626
553 var buffered = std.io.bufferedWriter(req.connection.?.data.writer());
554 const w = buffered.writer();
627 const w = req.connection.?.writer();
555628
556629 try req.method.write(w);
557630 try w.writeByte(' ');
558631
559632 if (req.method == .CONNECT) {
560 try w.writeAll(req.uri.host.?);
561 try w.writeByte(':');
562 try w.print("{}", .{req.uri.port.?});
633 try req.uri.writeToStream(.{ .authority = true }, w);
563634 } else {
564 if (req.connection.?.data.proxied) {
565 // proxied connections require the full uri
566 if (options.raw_uri) {
567 try w.print("{+/r}", .{req.uri});
568 } else {
569 try w.print("{+/}", .{req.uri});
570 }
571 } else {
572 if (options.raw_uri) {
573 try w.print("{/r}", .{req.uri});
574 } else {
575 try w.print("{/}", .{req.uri});
576 }
577 }
635 try req.uri.writeToStream(.{
636 .scheme = req.connection.?.proxied,
637 .authentication = req.connection.?.proxied,
638 .authority = req.connection.?.proxied,
639 .path = true,
640 .query = true,
641 .raw = options.raw_uri,
642 }, w);
578643 }
579644 try w.writeByte(' ');
580645 try w.writeAll(@tagName(req.version));
......@@ -582,7 +647,7 @@ pub const Request = struct {
582647
583648 if (!req.headers.contains("host")) {
584649 try w.writeAll("Host: ");
585 try w.writeAll(req.uri.host.?);
650 try req.uri.writeToStream(.{ .authority = true }, w);
586651 try w.writeAll("\r\n");
587652 }
588653
......@@ -614,17 +679,17 @@ pub const Request = struct {
614679 .none => {},
615680 }
616681 } else {
617 if (has_content_length) {
618 const content_length = std.fmt.parseInt(u64, req.headers.getFirstValue("content-length").?, 10) catch return error.InvalidContentLength;
619
620 req.transfer_encoding = .{ .content_length = content_length };
621 } else if (has_transfer_encoding) {
682 if (has_transfer_encoding) {
622683 const transfer_encoding = req.headers.getFirstValue("transfer-encoding").?;
623684 if (std.mem.eql(u8, transfer_encoding, "chunked")) {
624685 req.transfer_encoding = .chunked;
625686 } else {
626687 return error.UnsupportedTransferEncoding;
627688 }
689 } else if (has_content_length) {
690 const content_length = std.fmt.parseInt(u64, req.headers.getFirstValue("content-length").?, 10) catch return error.InvalidContentLength;
691
692 req.transfer_encoding = .{ .content_length = content_length };
628693 } else {
629694 req.transfer_encoding = .none;
630695 }
......@@ -639,9 +704,27 @@ pub const Request = struct {
639704 try w.writeAll("\r\n");
640705 }
641706
707 if (req.connection.?.proxied) {
708 const proxy_headers: ?http.Headers = switch (req.connection.?.protocol) {
709 .plain => if (req.client.http_proxy) |proxy| proxy.headers else null,
710 .tls => if (req.client.https_proxy) |proxy| proxy.headers else null,
711 };
712
713 if (proxy_headers) |headers| {
714 for (headers.list.items) |entry| {
715 if (entry.value.len == 0) continue;
716
717 try w.writeAll(entry.name);
718 try w.writeAll(": ");
719 try w.writeAll(entry.value);
720 try w.writeAll("\r\n");
721 }
722 }
723 }
724
642725 try w.writeAll("\r\n");
643726
644 try buffered.flush();
727 try req.connection.?.flush();
645728 }
646729
647730 const TransferReadError = Connection.ReadError || proto.HeadersParser.ReadError;
......@@ -657,7 +740,7 @@ pub const Request = struct {
657740
658741 var index: usize = 0;
659742 while (index == 0) {
660 const amt = try req.response.parser.read(&req.connection.?.data, buf[index..], req.response.skip);
743 const amt = try req.response.parser.read(req.connection.?, buf[index..], req.response.skip);
661744 if (amt == 0 and req.response.parser.done) break;
662745 index += amt;
663746 }
......@@ -665,20 +748,22 @@ pub const Request = struct {
665748 return index;
666749 }
667750
668 pub const WaitError = RequestError || StartError || TransferReadError || proto.HeadersParser.CheckCompleteHeadError || Response.ParseError || Uri.ParseError || error{ TooManyHttpRedirects, RedirectRequiresResend, HttpRedirectMissingLocation, CompressionInitializationFailed, CompressionNotSupported };
751 pub const WaitError = RequestError || SendError || TransferReadError || proto.HeadersParser.CheckCompleteHeadError || Response.ParseError || Uri.ParseError || error{ TooManyHttpRedirects, RedirectRequiresResend, HttpRedirectMissingLocation, CompressionInitializationFailed, CompressionNotSupported };
669752
670753 /// Waits for a response from the server and parses any headers that are sent.
671754 /// This function will block until the final response is received.
672755 ///
673756 /// If `handle_redirects` is true and the request has no payload, then this function will automatically follow
674757 /// redirects. If a request payload is present, then this function will error with error.RedirectRequiresResend.
758 ///
759 /// Must be called after `start` and, if any data was written to the request body, then also after `finish`.
675760 pub fn wait(req: *Request) WaitError!void {
676761 while (true) { // handle redirects
677762 while (true) { // read headers
678 try req.connection.?.data.fill();
763 try req.connection.?.fill();
679764
680 const nchecked = try req.response.parser.checkCompleteHead(req.client.allocator, req.connection.?.data.peek());
681 req.connection.?.data.drop(@as(u16, @intCast(nchecked)));
765 const nchecked = try req.response.parser.checkCompleteHead(req.client.allocator, req.connection.?.peek());
766 req.connection.?.drop(@intCast(nchecked));
682767
683768 if (req.response.parser.state.isContent()) break;
684769 }
......@@ -688,12 +773,16 @@ pub const Request = struct {
688773 if (req.response.status == .@"continue") {
689774 req.response.parser.done = true; // we're done parsing the continue response, reset to prepare for the real response
690775 req.response.parser.reset();
776
777 if (req.handle_continue)
778 continue;
779
691780 break;
692781 }
693782
694783 // we're switching protocols, so this connection is no longer doing http
695784 if (req.response.status == .switching_protocols or (req.method == .CONNECT and req.response.status == .ok)) {
696 req.connection.?.data.closing = false;
785 req.connection.?.closing = false;
697786 req.response.parser.done = true;
698787 }
699788
......@@ -704,13 +793,14 @@ pub const Request = struct {
704793 const res_connection = req.response.headers.getFirstValue("connection");
705794 const res_keepalive = res_connection != null and !std.ascii.eqlIgnoreCase("close", res_connection.?);
706795 if (res_keepalive and (req_keepalive or req_connection == null)) {
707 req.connection.?.data.closing = false;
796 req.connection.?.closing = false;
708797 } else {
709 req.connection.?.data.closing = true;
798 req.connection.?.closing = true;
710799 }
711800
712 if (req.response.transfer_encoding) |te| {
713 switch (te) {
801 if (req.response.transfer_encoding != .none) {
802 switch (req.response.transfer_encoding) {
803 .none => unreachable,
714804 .chunked => {
715805 req.response.parser.next_chunk_length = 0;
716806 req.response.parser.state = .chunk_head_size;
......@@ -774,23 +864,23 @@ pub const Request = struct {
774864
775865 try req.redirect(resolved_url);
776866
777 try req.start(.{});
867 try req.send(.{});
778868 } else {
779869 req.response.skip = false;
780870 if (!req.response.parser.done) {
781 if (req.response.transfer_compression) |tc| switch (tc) {
871 switch (req.response.transfer_compression) {
782872 .identity => req.response.compression = .none,
783 .compress => return error.CompressionNotSupported,
873 .compress, .@"x-compress" => return error.CompressionNotSupported,
784874 .deflate => req.response.compression = .{
785875 .deflate = std.compress.zlib.decompressStream(req.client.allocator, req.transferReader()) catch return error.CompressionInitializationFailed,
786876 },
787 .gzip => req.response.compression = .{
877 .gzip, .@"x-gzip" => req.response.compression = .{
788878 .gzip = std.compress.gzip.decompress(req.client.allocator, req.transferReader()) catch return error.CompressionInitializationFailed,
789879 },
790880 .zstd => req.response.compression = .{
791881 .zstd = std.compress.zstd.decompressStream(req.client.allocator, req.transferReader()),
792882 },
793 };
883 }
794884 }
795885
796886 break;
......@@ -806,7 +896,7 @@ pub const Request = struct {
806896 return .{ .context = req };
807897 }
808898
809 /// Reads data from the response body. Must be called after `do`.
899 /// Reads data from the response body. Must be called after `wait`.
810900 pub fn read(req: *Request, buffer: []u8) ReadError!usize {
811901 const out_index = switch (req.response.compression) {
812902 .deflate => |*deflate| deflate.read(buffer) catch return error.DecompressionFailure,
......@@ -819,15 +909,13 @@ pub const Request = struct {
819909 const has_trail = !req.response.parser.state.isContent();
820910
821911 while (!req.response.parser.state.isContent()) { // read trailing headers
822 try req.connection.?.data.fill();
912 try req.connection.?.fill();
823913
824 const nchecked = try req.response.parser.checkCompleteHead(req.client.allocator, req.connection.?.data.peek());
825 req.connection.?.data.drop(@as(u16, @intCast(nchecked)));
914 const nchecked = try req.response.parser.checkCompleteHead(req.client.allocator, req.connection.?.peek());
915 req.connection.?.drop(@intCast(nchecked));
826916 }
827917
828918 if (has_trail) {
829 req.response.headers.clearRetainingCapacity();
830
831919 // The response headers before the trailers are already guaranteed to be valid, so they will always be parsed again and cannot return an error.
832920 // This will *only* fail for a malformed trailer.
833921 req.response.parse(req.response.parser.header_bytes.items, true) catch return error.InvalidTrailers;
......@@ -837,7 +925,7 @@ pub const Request = struct {
837925 return out_index;
838926 }
839927
840 /// Reads data from the response body. Must be called after `do`.
928 /// Reads data from the response body. Must be called after `wait`.
841929 pub fn readAll(req: *Request, buffer: []u8) !usize {
842930 var index: usize = 0;
843931 while (index < buffer.len) {
......@@ -856,20 +944,21 @@ pub const Request = struct {
856944 return .{ .context = req };
857945 }
858946
859 /// Write `bytes` to the server. The `transfer_encoding` request header determines how data will be sent.
947 /// Write `bytes` to the server. The `transfer_encoding` field determines how data will be sent.
948 /// Must be called after `start` and before `finish`.
860949 pub fn write(req: *Request, bytes: []const u8) WriteError!usize {
861950 switch (req.transfer_encoding) {
862951 .chunked => {
863 try req.connection.?.data.writer().print("{x}\r\n", .{bytes.len});
864 try req.connection.?.data.writeAll(bytes);
865 try req.connection.?.data.writeAll("\r\n");
952 try req.connection.?.writer().print("{x}\r\n", .{bytes.len});
953 try req.connection.?.writer().writeAll(bytes);
954 try req.connection.?.writer().writeAll("\r\n");
866955
867956 return bytes.len;
868957 },
869958 .content_length => |*len| {
870959 if (len.* < bytes.len) return error.MessageTooLong;
871960
872 const amt = try req.connection.?.data.write(bytes);
961 const amt = try req.connection.?.write(bytes);
873962 len.* -= amt;
874963 return amt;
875964 },
......@@ -877,6 +966,8 @@ pub const Request = struct {
877966 }
878967 }
879968
969 /// Write `bytes` to the server. The `transfer_encoding` field determines how data will be sent.
970 /// Must be called after `start` and before `finish`.
880971 pub fn writeAll(req: *Request, bytes: []const u8) WriteError!void {
881972 var index: usize = 0;
882973 while (index < bytes.len) {
......@@ -887,50 +978,169 @@ pub const Request = struct {
887978 pub const FinishError = WriteError || error{MessageNotCompleted};
888979
889980 /// Finish the body of a request. This notifies the server that you have no more data to send.
981 /// Must be called after `start`.
890982 pub fn finish(req: *Request) FinishError!void {
891983 switch (req.transfer_encoding) {
892 .chunked => try req.connection.?.data.writeAll("0\r\n\r\n"),
984 .chunked => try req.connection.?.writer().writeAll("0\r\n\r\n"),
893985 .content_length => |len| if (len != 0) return error.MessageNotCompleted,
894986 .none => {},
895987 }
988
989 try req.connection.?.flush();
896990 }
897991};
898992
899pub const HttpProxy = struct {
900 pub const ProxyAuthentication = union(enum) {
901 basic: []const u8,
902 custom: []const u8,
903 };
993pub const Proxy = struct {
994 allocator: Allocator,
995 headers: http.Headers,
904996
905997 protocol: Connection.Protocol,
906998 host: []const u8,
907 port: ?u16 = null,
999 port: u16,
9081000
909 /// The value for the Proxy-Authorization header.
910 auth: ?ProxyAuthentication = null,
1001 supports_connect: bool = true,
9111002};
9121003
9131004/// Release all associated resources with the client.
914/// TODO: currently leaks all request allocated data
1005///
1006/// All pending requests must be de-initialized and all active connections released
1007/// before calling this function.
9151008pub fn deinit(client: *Client) void {
916 client.connection_pool.deinit(client);
1009 assert(client.connection_pool.used.first == null); // There are still active requests.
1010
1011 client.connection_pool.deinit(client.allocator);
1012
1013 if (client.http_proxy) |*proxy| {
1014 proxy.allocator.free(proxy.host);
1015 proxy.headers.deinit();
1016 }
1017
1018 if (client.https_proxy) |*proxy| {
1019 proxy.allocator.free(proxy.host);
1020 proxy.headers.deinit();
1021 }
1022
1023 if (!disable_tls)
1024 client.ca_bundle.deinit(client.allocator);
9171025
918 client.ca_bundle.deinit(client.allocator);
9191026 client.* = undefined;
9201027}
9211028
922pub const ConnectUnproxiedError = Allocator.Error || error{ ConnectionRefused, NetworkUnreachable, ConnectionTimedOut, ConnectionResetByPeer, TemporaryNameServerFailure, NameServerFailure, UnknownHostName, HostLacksNetworkAddresses, UnexpectedConnectFailure, TlsInitializationFailed };
1029/// Uses the *_proxy environment variable to set any unset proxies for the client.
1030/// This function *must not* be called when the client has any active connections.
1031pub fn loadDefaultProxies(client: *Client) !void {
1032 // Prevent any new connections from being created.
1033 client.connection_pool.mutex.lock();
1034 defer client.connection_pool.mutex.unlock();
1035
1036 assert(client.connection_pool.used.first == null); // There are still active requests.
1037
1038 if (client.http_proxy == null) http: {
1039 const content: []const u8 = if (std.process.hasEnvVarConstant("http_proxy"))
1040 try std.process.getEnvVarOwned(client.allocator, "http_proxy")
1041 else if (std.process.hasEnvVarConstant("HTTP_PROXY"))
1042 try std.process.getEnvVarOwned(client.allocator, "HTTP_PROXY")
1043 else if (std.process.hasEnvVarConstant("all_proxy"))
1044 try std.process.getEnvVarOwned(client.allocator, "all_proxy")
1045 else if (std.process.hasEnvVarConstant("ALL_PROXY"))
1046 try std.process.getEnvVarOwned(client.allocator, "ALL_PROXY")
1047 else
1048 break :http;
1049 defer client.allocator.free(content);
1050
1051 const uri = try Uri.parse(content);
1052
1053 const protocol = protocol_map.get(uri.scheme) orelse break :http; // Unknown scheme, ignore
1054 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :http; // Missing host, ignore
1055 client.http_proxy = .{
1056 .allocator = client.allocator,
1057 .headers = .{ .allocator = client.allocator },
1058
1059 .protocol = protocol,
1060 .host = host,
1061 .port = uri.port orelse switch (protocol) {
1062 .plain => 80,
1063 .tls => 443,
1064 },
1065 };
1066
1067 if (uri.user != null and uri.password != null) {
1068 const prefix_len = "Basic ".len;
1069
1070 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
1071 defer client.allocator.free(unencoded);
1072
1073 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len);
1074 defer client.allocator.free(buffer);
1075
1076 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);
1077 @memcpy(buffer[0..prefix_len], "Basic ");
1078
1079 try client.http_proxy.?.headers.append("proxy-authorization", result);
1080 }
1081 }
1082
1083 if (client.https_proxy == null) https: {
1084 const content: []const u8 = if (std.process.hasEnvVarConstant("https_proxy"))
1085 try std.process.getEnvVarOwned(client.allocator, "https_proxy")
1086 else if (std.process.hasEnvVarConstant("HTTPS_PROXY"))
1087 try std.process.getEnvVarOwned(client.allocator, "HTTPS_PROXY")
1088 else if (std.process.hasEnvVarConstant("all_proxy"))
1089 try std.process.getEnvVarOwned(client.allocator, "all_proxy")
1090 else if (std.process.hasEnvVarConstant("ALL_PROXY"))
1091 try std.process.getEnvVarOwned(client.allocator, "ALL_PROXY")
1092 else
1093 break :https;
1094 defer client.allocator.free(content);
1095
1096 const uri = try Uri.parse(content);
1097
1098 const protocol = protocol_map.get(uri.scheme) orelse break :https; // Unknown scheme, ignore
1099 const host = if (uri.host) |host| try client.allocator.dupe(u8, host) else break :https; // Missing host, ignore
1100 client.http_proxy = .{
1101 .allocator = client.allocator,
1102 .headers = .{ .allocator = client.allocator },
1103
1104 .protocol = protocol,
1105 .host = host,
1106 .port = uri.port orelse switch (protocol) {
1107 .plain => 80,
1108 .tls => 443,
1109 },
1110 };
1111
1112 if (uri.user != null and uri.password != null) {
1113 const prefix_len = "Basic ".len;
1114
1115 const unencoded = try std.fmt.allocPrint(client.allocator, "{s}:{s}", .{ uri.user.?, uri.password.? });
1116 defer client.allocator.free(unencoded);
1117
1118 const buffer = try client.allocator.alloc(u8, std.base64.standard.Encoder.calcSize(unencoded.len) + prefix_len);
1119 defer client.allocator.free(buffer);
1120
1121 const result = std.base64.standard.Encoder.encode(buffer[prefix_len..], unencoded);
1122 @memcpy(buffer[0..prefix_len], "Basic ");
1123
1124 try client.https_proxy.?.headers.append("proxy-authorization", result);
1125 }
1126 }
1127}
1128
1129pub const ConnectTcpError = Allocator.Error || error{ ConnectionRefused, NetworkUnreachable, ConnectionTimedOut, ConnectionResetByPeer, TemporaryNameServerFailure, NameServerFailure, UnknownHostName, HostLacksNetworkAddresses, UnexpectedConnectFailure, TlsInitializationFailed };
9231130
9241131/// Connect to `host:port` using the specified protocol. This will reuse a connection if one is already open.
9251132/// This function is threadsafe.
926pub fn connectUnproxied(client: *Client, host: []const u8, port: u16, protocol: Connection.Protocol) ConnectUnproxiedError!*ConnectionPool.Node {
1133pub fn connectTcp(client: *Client, host: []const u8, port: u16, protocol: Connection.Protocol) ConnectTcpError!*Connection {
9271134 if (client.connection_pool.findConnection(.{
9281135 .host = host,
9291136 .port = port,
930 .is_tls = protocol == .tls,
1137 .protocol = protocol,
9311138 })) |node|
9321139 return node;
9331140
1141 if (disable_tls and protocol == .tls)
1142 return error.TlsInitializationFailed;
1143
9341144 const conn = try client.allocator.create(ConnectionPool.Node);
9351145 errdefer client.allocator.destroy(conn);
9361146 conn.* = .{ .data = undefined };
......@@ -951,40 +1161,41 @@ pub fn connectUnproxied(client: *Client, host: []const u8, port: u16, protocol:
9511161 conn.data = .{
9521162 .stream = stream,
9531163 .tls_client = undefined,
954 .protocol = protocol,
9551164
1165 .protocol = protocol,
9561166 .host = try client.allocator.dupe(u8, host),
9571167 .port = port,
9581168 };
9591169 errdefer client.allocator.free(conn.data.host);
9601170
961 switch (protocol) {
962 .plain => {},
963 .tls => {
964 conn.data.tls_client = try client.allocator.create(std.crypto.tls.Client);
965 errdefer client.allocator.destroy(conn.data.tls_client);
1171 if (protocol == .tls) {
1172 if (disable_tls) unreachable;
9661173
967 conn.data.tls_client.* = std.crypto.tls.Client.init(stream, client.ca_bundle, host) catch return error.TlsInitializationFailed;
968 // This is appropriate for HTTPS because the HTTP headers contain
969 // the content length which is used to detect truncation attacks.
970 conn.data.tls_client.allow_truncation_attacks = true;
971 },
1174 conn.data.tls_client = try client.allocator.create(std.crypto.tls.Client);
1175 errdefer client.allocator.destroy(conn.data.tls_client);
1176
1177 conn.data.tls_client.* = std.crypto.tls.Client.init(stream, client.ca_bundle, host) catch return error.TlsInitializationFailed;
1178 // This is appropriate for HTTPS because the HTTP headers contain
1179 // the content length which is used to detect truncation attacks.
1180 conn.data.tls_client.allow_truncation_attacks = true;
9721181 }
9731182
9741183 client.connection_pool.addUsed(conn);
9751184
976 return conn;
1185 return &conn.data;
9771186}
9781187
9791188pub const ConnectUnixError = Allocator.Error || std.os.SocketError || error{ NameTooLong, Unsupported } || std.os.ConnectError;
9801189
981pub fn connectUnix(client: *Client, path: []const u8) ConnectUnixError!*ConnectionPool.Node {
1190/// Connect to `path` as a unix domain socket. This will reuse a connection if one is already open.
1191/// This function is threadsafe.
1192pub fn connectUnix(client: *Client, path: []const u8) ConnectUnixError!*Connection {
9821193 if (!net.has_unix_sockets) return error.Unsupported;
9831194
9841195 if (client.connection_pool.findConnection(.{
9851196 .host = path,
9861197 .port = 0,
987 .is_tls = false,
1198 .protocol = .plain,
9881199 })) |node|
9891200 return node;
9901201
......@@ -1007,37 +1218,130 @@ pub fn connectUnix(client: *Client, path: []const u8) ConnectUnixError!*Connecti
10071218
10081219 client.connection_pool.addUsed(conn);
10091220
1010 return conn;
1221 return &conn.data;
10111222}
10121223
1013// Prevents a dependency loop in request()
1014const ConnectErrorPartial = ConnectUnproxiedError || error{ UnsupportedUrlScheme, ConnectionRefused };
1015pub const ConnectError = ConnectErrorPartial || RequestError;
1224/// Connect to `tunnel_host:tunnel_port` using the specified proxy with HTTP CONNECT. This will reuse a connection if one is already open.
1225/// This function is threadsafe.
1226pub fn connectTunnel(
1227 client: *Client,
1228 proxy: *Proxy,
1229 tunnel_host: []const u8,
1230 tunnel_port: u16,
1231) !*Connection {
1232 if (!proxy.supports_connect) return error.TunnelNotSupported;
10161233
1017pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connection.Protocol) ConnectError!*ConnectionPool.Node {
10181234 if (client.connection_pool.findConnection(.{
1019 .host = host,
1020 .port = port,
1021 .is_tls = protocol == .tls,
1235 .host = tunnel_host,
1236 .port = tunnel_port,
1237 .protocol = proxy.protocol,
10221238 })) |node|
10231239 return node;
10241240
1025 if (client.proxy) |proxy| {
1026 const proxy_port: u16 = proxy.port orelse switch (proxy.protocol) {
1027 .plain => 80,
1028 .tls => 443,
1241 var maybe_valid = false;
1242 (tunnel: {
1243 const conn = try client.connectTcp(proxy.host, proxy.port, proxy.protocol);
1244 errdefer {
1245 conn.closing = true;
1246 client.connection_pool.release(client.allocator, conn);
1247 }
1248
1249 const uri = Uri{
1250 .scheme = "http",
1251 .user = null,
1252 .password = null,
1253 .host = tunnel_host,
1254 .port = tunnel_port,
1255 .path = "",
1256 .query = null,
1257 .fragment = null,
1258 };
1259
1260 // we can use a small buffer here because a CONNECT response should be very small
1261 var buffer: [8096]u8 = undefined;
1262
1263 var req = client.open(.CONNECT, uri, proxy.headers, .{
1264 .handle_redirects = false,
1265 .connection = conn,
1266 .header_strategy = .{ .static = &buffer },
1267 }) catch |err| {
1268 std.log.debug("err {}", .{err});
1269 break :tunnel err;
10291270 };
1271 defer req.deinit();
1272
1273 req.send(.{ .raw_uri = true }) catch |err| break :tunnel err;
1274 req.wait() catch |err| break :tunnel err;
1275
1276 if (req.response.status.class() == .server_error) {
1277 maybe_valid = true;
1278 break :tunnel error.ServerError;
1279 }
1280
1281 if (req.response.status != .ok) break :tunnel error.ConnectionRefused;
10301282
1031 const conn = try client.connectUnproxied(proxy.host, proxy_port, proxy.protocol);
1032 conn.data.proxied = true;
1283 // this connection is now a tunnel, so we can't use it for anything else, it will only be released when the client is de-initialized.
1284 req.connection = null;
1285
1286 client.allocator.free(conn.host);
1287 conn.host = try client.allocator.dupe(u8, tunnel_host);
1288 errdefer client.allocator.free(conn.host);
1289
1290 conn.port = tunnel_port;
1291 conn.closing = false;
10331292
10341293 return conn;
1035 } else {
1036 return client.connectUnproxied(host, port, protocol);
1294 }) catch {
1295 // something went wrong with the tunnel
1296 proxy.supports_connect = maybe_valid;
1297 return error.TunnelNotSupported;
1298 };
1299}
1300
1301// Prevents a dependency loop in request()
1302const ConnectErrorPartial = ConnectTcpError || error{ UnsupportedUrlScheme, ConnectionRefused };
1303pub const ConnectError = ConnectErrorPartial || RequestError;
1304
1305/// Connect to `host:port` using the specified protocol. This will reuse a connection if one is already open.
1306///
1307/// If a proxy is configured for the client, then the proxy will be used to connect to the host.
1308///
1309/// This function is threadsafe.
1310pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connection.Protocol) ConnectError!*Connection {
1311 // pointer required so that `supports_connect` can be updated if a CONNECT fails
1312 const potential_proxy: ?*Proxy = switch (protocol) {
1313 .plain => if (client.http_proxy) |*proxy_info| proxy_info else null,
1314 .tls => if (client.https_proxy) |*proxy_info| proxy_info else null,
1315 };
1316
1317 if (potential_proxy) |proxy| {
1318 // don't attempt to proxy the proxy thru itself.
1319 if (std.mem.eql(u8, proxy.host, host) and proxy.port == port and proxy.protocol == protocol) {
1320 return client.connectTcp(host, port, protocol);
1321 }
1322
1323 if (proxy.supports_connect) tunnel: {
1324 return connectTunnel(client, proxy, host, port) catch |err| switch (err) {
1325 error.TunnelNotSupported => break :tunnel,
1326 else => |e| return e,
1327 };
1328 }
1329
1330 // fall back to using the proxy as a normal http proxy
1331 const conn = try client.connectTcp(proxy.host, proxy.port, proxy.protocol);
1332 errdefer {
1333 conn.closing = true;
1334 client.connection_pool.release(conn);
1335 }
1336
1337 conn.proxied = true;
1338 return conn;
10371339 }
1340
1341 return client.connectTcp(host, port, protocol);
10381342}
10391343
1040pub const RequestError = ConnectUnproxiedError || ConnectErrorPartial || Request.StartError || std.fmt.ParseIntError || Connection.WriteError || error{
1344pub const RequestError = ConnectTcpError || ConnectErrorPartial || Request.SendError || std.fmt.ParseIntError || Connection.WriteError || error{
10411345 UnsupportedUrlScheme,
10421346 UriMissingHost,
10431347
......@@ -1048,12 +1352,20 @@ pub const RequestError = ConnectUnproxiedError || ConnectErrorPartial || Request
10481352pub const RequestOptions = struct {
10491353 version: http.Version = .@"HTTP/1.1",
10501354
1355 /// Automatically ignore 100 Continue responses. This assumes you don't care, and will have sent the body before you
1356 /// wait for the response.
1357 ///
1358 /// If this is not the case AND you know the server will send a 100 Continue, set this to false and wait for a
1359 /// response before sending the body. If you wait AND the server does not send a 100 Continue before you finish the
1360 /// request, then the request *will* deadlock.
1361 handle_continue: bool = true,
1362
10511363 handle_redirects: bool = true,
10521364 max_redirects: u32 = 3,
10531365 header_strategy: StorageStrategy = .{ .dynamic = 16 * 1024 },
10541366
10551367 /// Must be an already acquired connection.
1056 connection: ?*ConnectionPool.Node = null,
1368 connection: ?*Connection = null,
10571369
10581370 pub const StorageStrategy = union(enum) {
10591371 /// In this case, the client's Allocator will be used to store the
......@@ -1076,14 +1388,14 @@ pub const protocol_map = std.ComptimeStringMap(Connection.Protocol, .{
10761388 .{ "wss", .tls },
10771389});
10781390
1079/// Form and send a http request to a server.
1391/// Open a connection to the host specified by `uri` and prepare to send a HTTP request.
10801392///
10811393/// `uri` must remain alive during the entire request.
10821394/// `headers` is cloned and may be freed after this function returns.
10831395///
10841396/// The caller is responsible for calling `deinit()` on the `Request`.
10851397/// This function is threadsafe.
1086pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Headers, options: RequestOptions) RequestError!Request {
1398pub fn open(client: *Client, method: http.Method, uri: Uri, headers: http.Headers, options: RequestOptions) RequestError!Request {
10871399 const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUrlScheme;
10881400
10891401 const port: u16 = uri.port orelse switch (protocol) {
......@@ -1094,6 +1406,8 @@ pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Hea
10941406 const host = uri.host orelse return error.UriMissingHost;
10951407
10961408 if (protocol == .tls and @atomicLoad(bool, &client.next_https_rescan_certs, .Acquire)) {
1409 if (disable_tls) unreachable;
1410
10971411 client.ca_bundle_mutex.lock();
10981412 defer client.ca_bundle_mutex.unlock();
10991413
......@@ -1114,6 +1428,7 @@ pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Hea
11141428 .version = options.version,
11151429 .redirects_left = options.max_redirects,
11161430 .handle_redirects = options.handle_redirects,
1431 .handle_continue = options.handle_continue,
11171432 .response = .{
11181433 .status = undefined,
11191434 .reason = undefined,
......@@ -1178,6 +1493,9 @@ pub const FetchResult = struct {
11781493 }
11791494};
11801495
1496/// Perform a one-shot HTTP request with the provided options.
1497///
1498/// This function is threadsafe.
11811499pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !FetchResult {
11821500 const has_transfer_encoding = options.headers.contains("transfer-encoding");
11831501 const has_content_length = options.headers.contains("content-length");
......@@ -1189,7 +1507,7 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc
11891507 .uri => |u| u,
11901508 };
11911509
1192 var req = try request(client, options.method, uri, options.headers, .{
1510 var req = try open(client, options.method, uri, options.headers, .{
11931511 .header_strategy = options.header_strategy,
11941512 .handle_redirects = options.payload == .none,
11951513 });
......@@ -1206,7 +1524,7 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc
12061524 .none => {},
12071525 }
12081526
1209 try req.start(.{ .raw_uri = options.raw_uri });
1527 try req.send(.{ .raw_uri = options.raw_uri });
12101528
12111529 switch (options.payload) {
12121530 .string => |str| try req.writeAll(str),
lib/std/http/Headers.zig+7-4
......@@ -14,15 +14,18 @@ pub const CaseInsensitiveStringContext = struct {
1414 pub fn hash(self: @This(), s: []const u8) u64 {
1515 _ = self;
1616 var buf: [64]u8 = undefined;
17 var i: u8 = 0;
17 var i: usize = 0;
1818
1919 var h = std.hash.Wyhash.init(0);
20 while (i < s.len) : (i += 64) {
21 const left = @min(64, s.len - i);
22 const ret = ascii.lowerString(buf[0..], s[i..][0..left]);
20 while (i + 64 < s.len) : (i += 64) {
21 const ret = ascii.lowerString(buf[0..], s[i..][0..64]);
2322 h.update(ret);
2423 }
2524
25 const left = @min(64, s.len - i);
26 const ret = ascii.lowerString(buf[0..], s[i..][0..left]);
27 h.update(ret);
28
2629 return h.final();
2730 }
2831
lib/std/http/Server.zig+44-33
......@@ -14,7 +14,7 @@ allocator: Allocator,
1414
1515socket: net.StreamServer,
1616
17/// An interface to either a plain or TLS connection.
17/// An interface to a plain connection.
1818pub const Connection = struct {
1919 pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
2020 pub const Protocol = enum { plain };
......@@ -178,7 +178,7 @@ pub const Request = struct {
178178 };
179179
180180 pub fn parse(req: *Request, bytes: []const u8) ParseError!void {
181 var it = mem.tokenizeAny(u8, bytes[0 .. bytes.len - 4], "\r\n");
181 var it = mem.tokenizeAny(u8, bytes, "\r\n");
182182
183183 const first_line = it.next() orelse return error.HttpHeadersInvalid;
184184 if (first_line.len < 10)
......@@ -228,27 +228,23 @@ pub const Request = struct {
228228 // Transfer-Encoding: deflate, chunked
229229 var iter = mem.splitBackwardsScalar(u8, header_value, ',');
230230
231 if (iter.next()) |first| {
232 const trimmed = mem.trim(u8, first, " ");
231 const first = iter.first();
232 const trimmed_first = mem.trim(u8, first, " ");
233233
234 if (std.meta.stringToEnum(http.TransferEncoding, trimmed)) |te| {
235 if (req.transfer_encoding != null) return error.HttpHeadersInvalid;
236 req.transfer_encoding = te;
237 } else if (std.meta.stringToEnum(http.ContentEncoding, trimmed)) |ce| {
238 if (req.transfer_compression != null) return error.HttpHeadersInvalid;
239 req.transfer_compression = ce;
240 } else {
241 return error.HttpTransferEncodingUnsupported;
242 }
243 }
234 var next: ?[]const u8 = first;
235 if (std.meta.stringToEnum(http.TransferEncoding, trimmed_first)) |transfer| {
236 if (req.transfer_encoding != .none) return error.HttpHeadersInvalid; // we already have a transfer encoding
237 req.transfer_encoding = transfer;
244238
245 if (iter.next()) |second| {
246 if (req.transfer_compression != null) return error.HttpTransferEncodingUnsupported;
239 next = iter.next();
240 }
247241
248 const trimmed = mem.trim(u8, second, " ");
242 if (next) |second| {
243 const trimmed_second = mem.trim(u8, second, " ");
249244
250 if (std.meta.stringToEnum(http.ContentEncoding, trimmed)) |ce| {
251 req.transfer_compression = ce;
245 if (std.meta.stringToEnum(http.ContentEncoding, trimmed_second)) |transfer| {
246 if (req.transfer_compression != .identity) return error.HttpHeadersInvalid; // double compression is not supported
247 req.transfer_compression = transfer;
252248 } else {
253249 return error.HttpTransferEncodingUnsupported;
254250 }
......@@ -256,7 +252,7 @@ pub const Request = struct {
256252
257253 if (iter.next()) |_| return error.HttpTransferEncodingUnsupported;
258254 } else if (std.ascii.eqlIgnoreCase(header_name, "content-encoding")) {
259 if (req.transfer_compression != null) return error.HttpHeadersInvalid;
255 if (req.transfer_compression != .identity) return error.HttpHeadersInvalid;
260256
261257 const trimmed = mem.trim(u8, header_value, " ");
262258
......@@ -277,9 +273,14 @@ pub const Request = struct {
277273 target: []const u8,
278274 version: http.Version,
279275
276 /// The length of the request body, if known.
280277 content_length: ?u64 = null,
281 transfer_encoding: ?http.TransferEncoding = null,
282 transfer_compression: ?http.ContentEncoding = null,
278
279 /// The transfer encoding of the request body, or .none if not present.
280 transfer_encoding: http.TransferEncoding = .none,
281
282 /// The compression of the request body, or .identity (no compression) if not present.
283 transfer_compression: http.ContentEncoding = .identity,
283284
284285 headers: http.Headers,
285286 parser: proto.HeadersParser,
......@@ -315,6 +316,7 @@ pub const Response = struct {
315316 finished,
316317 };
317318
319 /// Free all resources associated with this response.
318320 pub fn deinit(res: *Response) void {
319321 res.connection.close();
320322
......@@ -390,10 +392,10 @@ pub const Response = struct {
390392 }
391393 }
392394
393 pub const DoError = Connection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };
395 pub const SendError = Connection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };
394396
395 /// Send the response headers.
396 pub fn do(res: *Response) DoError!void {
397 /// Send the HTTP response headers to the client.
398 pub fn send(res: *Response) SendError!void {
397399 switch (res.state) {
398400 .waited => res.state = .responded,
399401 .first, .start, .responded, .finished => unreachable,
......@@ -511,8 +513,9 @@ pub const Response = struct {
511513 res.request.headers = .{ .allocator = res.allocator, .owned = true };
512514 try res.request.parse(res.request.parser.header_bytes.items);
513515
514 if (res.request.transfer_encoding) |te| {
515 switch (te) {
516 if (res.request.transfer_encoding != .none) {
517 switch (res.request.transfer_encoding) {
518 .none => unreachable,
516519 .chunked => {
517520 res.request.parser.next_chunk_length = 0;
518521 res.request.parser.state = .chunk_head_size;
......@@ -527,19 +530,19 @@ pub const Response = struct {
527530 }
528531
529532 if (!res.request.parser.done) {
530 if (res.request.transfer_compression) |tc| switch (tc) {
533 switch (res.request.transfer_compression) {
531534 .identity => res.request.compression = .none,
532 .compress => return error.CompressionNotSupported,
535 .compress, .@"x-compress" => return error.CompressionNotSupported,
533536 .deflate => res.request.compression = .{
534537 .deflate = std.compress.zlib.decompressStream(res.allocator, res.transferReader()) catch return error.CompressionInitializationFailed,
535538 },
536 .gzip => res.request.compression = .{
539 .gzip, .@"x-gzip" => res.request.compression = .{
537540 .gzip = std.compress.gzip.decompress(res.allocator, res.transferReader()) catch return error.CompressionInitializationFailed,
538541 },
539542 .zstd => res.request.compression = .{
540543 .zstd = std.compress.zstd.decompressStream(res.allocator, res.transferReader()),
541544 },
542 };
545 }
543546 }
544547 }
545548
......@@ -551,6 +554,7 @@ pub const Response = struct {
551554 return .{ .context = res };
552555 }
553556
557 /// Reads data from the response body. Must be called after `wait`.
554558 pub fn read(res: *Response, buffer: []u8) ReadError!usize {
555559 switch (res.state) {
556560 .waited, .responded, .finished => {},
......@@ -586,6 +590,7 @@ pub const Response = struct {
586590 return out_index;
587591 }
588592
593 /// Reads data from the response body. Must be called after `wait`.
589594 pub fn readAll(res: *Response, buffer: []u8) !usize {
590595 var index: usize = 0;
591596 while (index < buffer.len) {
......@@ -605,6 +610,7 @@ pub const Response = struct {
605610 }
606611
607612 /// Write `bytes` to the server. The `transfer_encoding` request header determines how data will be sent.
613 /// Must be called after `start` and before `finish`.
608614 pub fn write(res: *Response, bytes: []const u8) WriteError!usize {
609615 switch (res.state) {
610616 .responded => {},
......@@ -630,6 +636,8 @@ pub const Response = struct {
630636 }
631637 }
632638
639 /// Write `bytes` to the server. The `transfer_encoding` request header determines how data will be sent.
640 /// Must be called after `start` and before `finish`.
633641 pub fn writeAll(req: *Response, bytes: []const u8) WriteError!void {
634642 var index: usize = 0;
635643 while (index < bytes.len) {
......@@ -640,6 +648,7 @@ pub const Response = struct {
640648 pub const FinishError = WriteError || error{MessageNotCompleted};
641649
642650 /// Finish the body of a request. This notifies the server that you have no more data to send.
651 /// Must be called after `start`.
643652 pub fn finish(res: *Response) FinishError!void {
644653 switch (res.state) {
645654 .responded => res.state = .finished,
......@@ -654,6 +663,7 @@ pub const Response = struct {
654663 }
655664};
656665
666/// Create a new HTTP server.
657667pub fn init(allocator: Allocator, options: net.StreamServer.Options) Server {
658668 return .{
659669 .allocator = allocator,
......@@ -661,6 +671,7 @@ pub fn init(allocator: Allocator, options: net.StreamServer.Options) Server {
661671 };
662672}
663673
674/// Free all resources associated with this server.
664675pub fn deinit(server: *Server) void {
665676 server.socket.deinit();
666677}
......@@ -756,13 +767,13 @@ test "HTTP server handles a chunked transfer coding request" {
756767 defer _ = res.reset();
757768 try res.wait();
758769
759 try expect(res.request.transfer_encoding.? == .chunked);
770 try expect(res.request.transfer_encoding == .chunked);
760771
761772 const server_body: []const u8 = "message from server!\n";
762773 res.transfer_encoding = .{ .content_length = server_body.len };
763774 try res.headers.append("content-type", "text/plain");
764775 try res.headers.append("connection", "close");
765 try res.do();
776 try res.send();
766777
767778 var buf: [128]u8 = undefined;
768779 const n = try res.readAll(&buf);
lib/std/http/protocol.zig+3-3
......@@ -529,7 +529,7 @@ pub const HeadersParser = struct {
529529 try conn.fill();
530530
531531 const nread = @min(conn.peek().len, data_avail);
532 conn.drop(@as(u16, @intCast(nread)));
532 conn.drop(@intCast(nread));
533533 r.next_chunk_length -= nread;
534534
535535 if (r.next_chunk_length == 0) r.done = true;
......@@ -553,7 +553,7 @@ pub const HeadersParser = struct {
553553 try conn.fill();
554554
555555 const i = r.findChunkedLen(conn.peek());
556 conn.drop(@as(u16, @intCast(i)));
556 conn.drop(@intCast(i));
557557
558558 switch (r.state) {
559559 .invalid => return error.HttpChunkInvalid,
......@@ -582,7 +582,7 @@ pub const HeadersParser = struct {
582582 try conn.fill();
583583
584584 const nread = @min(conn.peek().len, data_avail);
585 conn.drop(@as(u16, @intCast(nread)));
585 conn.drop(@intCast(nread));
586586 r.next_chunk_length -= nread;
587587 } else if (out_avail > 0) {
588588 const can_read: usize = @intCast(@min(data_avail, out_avail));
lib/std/std.zig+8-3
......@@ -283,10 +283,15 @@ pub const options = struct {
283283 else
284284 false;
285285
286 pub const http_connection_pool_size = if (@hasDecl(options_override, "http_connection_pool_size"))
287 options_override.http_connection_pool_size
286 /// By default, std.http.Client will support HTTPS connections. Set this option to `true` to
287 /// disable TLS support.
288 ///
289 /// This will likely reduce the size of the binary, but it will also make it impossible to
290 /// make a HTTPS connection.
291 pub const http_disable_tls = if (@hasDecl(options_override, "http_disable_tls"))
292 options_override.http_disable_tls
288293 else
289 http.Client.default_connection_pool_size;
294 false;
290295
291296 pub const side_channels_mitigations: crypto.SideChannelsMitigations = if (@hasDecl(options_override, "side_channels_mitigations"))
292297 options_override.side_channels_mitigations
src/Package/Fetch.zig+2-2
......@@ -826,7 +826,7 @@ fn initResource(f: *Fetch, uri: std.Uri) RunError!Resource {
826826 var h = std.http.Headers{ .allocator = gpa };
827827 defer h.deinit();
828828
829 var req = http_client.request(.GET, uri, h, .{}) catch |err| {
829 var req = http_client.open(.GET, uri, h, .{}) catch |err| {
830830 return f.fail(f.location_tok, try eb.printString(
831831 "unable to connect to server: {s}",
832832 .{@errorName(err)},
......@@ -834,7 +834,7 @@ fn initResource(f: *Fetch, uri: std.Uri) RunError!Resource {
834834 };
835835 errdefer req.deinit(); // releases more than memory
836836
837 req.start(.{}) catch |err| {
837 req.send(.{}) catch |err| {
838838 return f.fail(f.location_tok, try eb.printString(
839839 "HTTP request failed: {s}",
840840 .{@errorName(err)},
src/Package/Fetch/git.zig+6-6
......@@ -518,11 +518,11 @@ pub const Session = struct {
518518 defer headers.deinit();
519519 try headers.append("Git-Protocol", "version=2");
520520
521 var request = try session.transport.request(.GET, info_refs_uri, headers, .{
521 var request = try session.transport.open(.GET, info_refs_uri, headers, .{
522522 .max_redirects = 3,
523523 });
524524 errdefer request.deinit();
525 try request.start(.{});
525 try request.send(.{});
526526 try request.finish();
527527
528528 try request.wait();
......@@ -641,12 +641,12 @@ pub const Session = struct {
641641 }
642642 try Packet.write(.flush, body_writer);
643643
644 var request = try session.transport.request(.POST, upload_pack_uri, headers, .{
644 var request = try session.transport.open(.POST, upload_pack_uri, headers, .{
645645 .handle_redirects = false,
646646 });
647647 errdefer request.deinit();
648648 request.transfer_encoding = .{ .content_length = body.items.len };
649 try request.start(.{});
649 try request.send(.{});
650650 try request.writeAll(body.items);
651651 try request.finish();
652652
......@@ -740,12 +740,12 @@ pub const Session = struct {
740740 try Packet.write(.{ .data = "done\n" }, body_writer);
741741 try Packet.write(.flush, body_writer);
742742
743 var request = try session.transport.request(.POST, upload_pack_uri, headers, .{
743 var request = try session.transport.open(.POST, upload_pack_uri, headers, .{
744744 .handle_redirects = false,
745745 });
746746 errdefer request.deinit();
747747 request.transfer_encoding = .{ .content_length = body.items.len };
748 try request.start(.{});
748 try request.send(.{});
749749 try request.writeAll(body.items);
750750 try request.finish();
751751
src/main.zig+4
......@@ -5128,6 +5128,8 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
51285128 var http_client: std.http.Client = .{ .allocator = gpa };
51295129 defer http_client.deinit();
51305130
5131 try http_client.loadDefaultProxies();
5132
51315133 var progress: std.Progress = .{ .dont_print_on_dumb = true };
51325134 const root_prog_node = progress.start("Fetch Packages", 0);
51335135 defer root_prog_node.end();
......@@ -7039,6 +7041,8 @@ fn cmdFetch(
70397041 var http_client: std.http.Client = .{ .allocator = gpa };
70407042 defer http_client.deinit();
70417043
7044 try http_client.loadDefaultProxies();
7045
70427046 var progress: std.Progress = .{ .dont_print_on_dumb = true };
70437047 const root_prog_node = progress.start("Fetch", 0);
70447048 defer root_prog_node.end();
test/standalone/http.zig+70-63
......@@ -7,6 +7,10 @@ const Client = http.Client;
77const mem = std.mem;
88const testing = std.testing;
99
10pub const std_options = struct {
11 pub const http_disable_tls = true;
12};
13
1014const max_header_size = 8192;
1115
1216var gpa_server = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = 12 }){};
......@@ -25,11 +29,11 @@ fn handleRequest(res: *Server.Response) !void {
2529 if (res.request.headers.contains("expect")) {
2630 if (mem.eql(u8, res.request.headers.getFirstValue("expect").?, "100-continue")) {
2731 res.status = .@"continue";
28 try res.do();
32 try res.send();
2933 res.status = .ok;
3034 } else {
3135 res.status = .expectation_failed;
32 try res.do();
36 try res.send();
3337 return;
3438 }
3539 }
......@@ -50,7 +54,7 @@ fn handleRequest(res: *Server.Response) !void {
5054
5155 try res.headers.append("content-type", "text/plain");
5256
53 try res.do();
57 try res.send();
5458 if (res.request.method != .HEAD) {
5559 try res.writeAll("Hello, ");
5660 try res.writeAll("World!\n");
......@@ -61,7 +65,7 @@ fn handleRequest(res: *Server.Response) !void {
6165 } else if (mem.startsWith(u8, res.request.target, "/large")) {
6266 res.transfer_encoding = .{ .content_length = 14 * 1024 + 14 * 10 };
6367
64 try res.do();
68 try res.send();
6569
6670 var i: u32 = 0;
6771 while (i < 5) : (i += 1) {
......@@ -88,14 +92,14 @@ fn handleRequest(res: *Server.Response) !void {
8892 try testing.expectEqualStrings("14", res.request.headers.getFirstValue("content-length").?);
8993 }
9094
91 try res.do();
95 try res.send();
9296 try res.writeAll("Hello, ");
9397 try res.writeAll("World!\n");
9498 try res.finish();
9599 } else if (mem.eql(u8, res.request.target, "/trailer")) {
96100 res.transfer_encoding = .chunked;
97101
98 try res.do();
102 try res.send();
99103 try res.writeAll("Hello, ");
100104 try res.writeAll("World!\n");
101105 // try res.finish();
......@@ -106,7 +110,7 @@ fn handleRequest(res: *Server.Response) !void {
106110 res.status = .found;
107111 try res.headers.append("location", "../../get");
108112
109 try res.do();
113 try res.send();
110114 try res.writeAll("Hello, ");
111115 try res.writeAll("Redirected!\n");
112116 try res.finish();
......@@ -116,7 +120,7 @@ fn handleRequest(res: *Server.Response) !void {
116120 res.status = .found;
117121 try res.headers.append("location", "/redirect/1");
118122
119 try res.do();
123 try res.send();
120124 try res.writeAll("Hello, ");
121125 try res.writeAll("Redirected!\n");
122126 try res.finish();
......@@ -129,7 +133,7 @@ fn handleRequest(res: *Server.Response) !void {
129133 res.status = .found;
130134 try res.headers.append("location", location);
131135
132 try res.do();
136 try res.send();
133137 try res.writeAll("Hello, ");
134138 try res.writeAll("Redirected!\n");
135139 try res.finish();
......@@ -139,7 +143,7 @@ fn handleRequest(res: *Server.Response) !void {
139143 res.status = .found;
140144 try res.headers.append("location", "/redirect/3");
141145
142 try res.do();
146 try res.send();
143147 try res.writeAll("Hello, ");
144148 try res.writeAll("Redirected!\n");
145149 try res.finish();
......@@ -150,11 +154,11 @@ fn handleRequest(res: *Server.Response) !void {
150154
151155 res.status = .found;
152156 try res.headers.append("location", location);
153 try res.do();
157 try res.send();
154158 try res.finish();
155159 } else {
156160 res.status = .not_found;
157 try res.do();
161 try res.send();
158162 }
159163}
160164
......@@ -226,8 +230,11 @@ pub fn main() !void {
226230 const server_thread = try std.Thread.spawn(.{}, serverThread, .{&server});
227231
228232 var client = Client{ .allocator = calloc };
233 errdefer client.deinit();
229234 // defer client.deinit(); handled below
230235
236 try client.loadDefaultProxies();
237
231238 { // read content-length response
232239 var h = http.Headers{ .allocator = calloc };
233240 defer h.deinit();
......@@ -237,10 +244,10 @@ pub fn main() !void {
237244 const uri = try std.Uri.parse(location);
238245
239246 log.info("{s}", .{location});
240 var req = try client.request(.GET, uri, h, .{});
247 var req = try client.open(.GET, uri, h, .{});
241248 defer req.deinit();
242249
243 try req.start(.{});
250 try req.send(.{});
244251 try req.wait();
245252
246253 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -251,7 +258,7 @@ pub fn main() !void {
251258 }
252259
253260 // connection has been kept alive
254 try testing.expect(client.connection_pool.free_len == 1);
261 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
255262
256263 { // read large content-length response
257264 var h = http.Headers{ .allocator = calloc };
......@@ -262,10 +269,10 @@ pub fn main() !void {
262269 const uri = try std.Uri.parse(location);
263270
264271 log.info("{s}", .{location});
265 var req = try client.request(.GET, uri, h, .{});
272 var req = try client.open(.GET, uri, h, .{});
266273 defer req.deinit();
267274
268 try req.start(.{});
275 try req.send(.{});
269276 try req.wait();
270277
271278 const body = try req.reader().readAllAlloc(calloc, 8192 * 1024);
......@@ -275,7 +282,7 @@ pub fn main() !void {
275282 }
276283
277284 // connection has been kept alive
278 try testing.expect(client.connection_pool.free_len == 1);
285 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
279286
280287 { // send head request and not read chunked
281288 var h = http.Headers{ .allocator = calloc };
......@@ -286,10 +293,10 @@ pub fn main() !void {
286293 const uri = try std.Uri.parse(location);
287294
288295 log.info("{s}", .{location});
289 var req = try client.request(.HEAD, uri, h, .{});
296 var req = try client.open(.HEAD, uri, h, .{});
290297 defer req.deinit();
291298
292 try req.start(.{});
299 try req.send(.{});
293300 try req.wait();
294301
295302 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -301,7 +308,7 @@ pub fn main() !void {
301308 }
302309
303310 // connection has been kept alive
304 try testing.expect(client.connection_pool.free_len == 1);
311 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
305312
306313 { // read chunked response
307314 var h = http.Headers{ .allocator = calloc };
......@@ -312,10 +319,10 @@ pub fn main() !void {
312319 const uri = try std.Uri.parse(location);
313320
314321 log.info("{s}", .{location});
315 var req = try client.request(.GET, uri, h, .{});
322 var req = try client.open(.GET, uri, h, .{});
316323 defer req.deinit();
317324
318 try req.start(.{});
325 try req.send(.{});
319326 try req.wait();
320327
321328 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -326,7 +333,7 @@ pub fn main() !void {
326333 }
327334
328335 // connection has been kept alive
329 try testing.expect(client.connection_pool.free_len == 1);
336 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
330337
331338 { // send head request and not read chunked
332339 var h = http.Headers{ .allocator = calloc };
......@@ -337,10 +344,10 @@ pub fn main() !void {
337344 const uri = try std.Uri.parse(location);
338345
339346 log.info("{s}", .{location});
340 var req = try client.request(.HEAD, uri, h, .{});
347 var req = try client.open(.HEAD, uri, h, .{});
341348 defer req.deinit();
342349
343 try req.start(.{});
350 try req.send(.{});
344351 try req.wait();
345352
346353 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -352,7 +359,7 @@ pub fn main() !void {
352359 }
353360
354361 // connection has been kept alive
355 try testing.expect(client.connection_pool.free_len == 1);
362 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
356363
357364 { // check trailing headers
358365 var h = http.Headers{ .allocator = calloc };
......@@ -363,10 +370,10 @@ pub fn main() !void {
363370 const uri = try std.Uri.parse(location);
364371
365372 log.info("{s}", .{location});
366 var req = try client.request(.GET, uri, h, .{});
373 var req = try client.open(.GET, uri, h, .{});
367374 defer req.deinit();
368375
369 try req.start(.{});
376 try req.send(.{});
370377 try req.wait();
371378
372379 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -377,7 +384,7 @@ pub fn main() !void {
377384 }
378385
379386 // connection has been kept alive
380 try testing.expect(client.connection_pool.free_len == 1);
387 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
381388
382389 { // send content-length request
383390 var h = http.Headers{ .allocator = calloc };
......@@ -390,12 +397,12 @@ pub fn main() !void {
390397 const uri = try std.Uri.parse(location);
391398
392399 log.info("{s}", .{location});
393 var req = try client.request(.POST, uri, h, .{});
400 var req = try client.open(.POST, uri, h, .{});
394401 defer req.deinit();
395402
396403 req.transfer_encoding = .{ .content_length = 14 };
397404
398 try req.start(.{});
405 try req.send(.{});
399406 try req.writeAll("Hello, ");
400407 try req.writeAll("World!\n");
401408 try req.finish();
......@@ -409,7 +416,7 @@ pub fn main() !void {
409416 }
410417
411418 // connection has been kept alive
412 try testing.expect(client.connection_pool.free_len == 1);
419 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
413420
414421 { // read content-length response with connection close
415422 var h = http.Headers{ .allocator = calloc };
......@@ -422,10 +429,10 @@ pub fn main() !void {
422429 const uri = try std.Uri.parse(location);
423430
424431 log.info("{s}", .{location});
425 var req = try client.request(.GET, uri, h, .{});
432 var req = try client.open(.GET, uri, h, .{});
426433 defer req.deinit();
427434
428 try req.start(.{});
435 try req.send(.{});
429436 try req.wait();
430437
431438 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -449,12 +456,12 @@ pub fn main() !void {
449456 const uri = try std.Uri.parse(location);
450457
451458 log.info("{s}", .{location});
452 var req = try client.request(.POST, uri, h, .{});
459 var req = try client.open(.POST, uri, h, .{});
453460 defer req.deinit();
454461
455462 req.transfer_encoding = .chunked;
456463
457 try req.start(.{});
464 try req.send(.{});
458465 try req.writeAll("Hello, ");
459466 try req.writeAll("World!\n");
460467 try req.finish();
......@@ -468,7 +475,7 @@ pub fn main() !void {
468475 }
469476
470477 // connection has been kept alive
471 try testing.expect(client.connection_pool.free_len == 1);
478 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
472479
473480 { // relative redirect
474481 var h = http.Headers{ .allocator = calloc };
......@@ -479,10 +486,10 @@ pub fn main() !void {
479486 const uri = try std.Uri.parse(location);
480487
481488 log.info("{s}", .{location});
482 var req = try client.request(.GET, uri, h, .{});
489 var req = try client.open(.GET, uri, h, .{});
483490 defer req.deinit();
484491
485 try req.start(.{});
492 try req.send(.{});
486493 try req.wait();
487494
488495 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -492,7 +499,7 @@ pub fn main() !void {
492499 }
493500
494501 // connection has been kept alive
495 try testing.expect(client.connection_pool.free_len == 1);
502 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
496503
497504 { // redirect from root
498505 var h = http.Headers{ .allocator = calloc };
......@@ -503,10 +510,10 @@ pub fn main() !void {
503510 const uri = try std.Uri.parse(location);
504511
505512 log.info("{s}", .{location});
506 var req = try client.request(.GET, uri, h, .{});
513 var req = try client.open(.GET, uri, h, .{});
507514 defer req.deinit();
508515
509 try req.start(.{});
516 try req.send(.{});
510517 try req.wait();
511518
512519 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -516,7 +523,7 @@ pub fn main() !void {
516523 }
517524
518525 // connection has been kept alive
519 try testing.expect(client.connection_pool.free_len == 1);
526 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
520527
521528 { // absolute redirect
522529 var h = http.Headers{ .allocator = calloc };
......@@ -527,10 +534,10 @@ pub fn main() !void {
527534 const uri = try std.Uri.parse(location);
528535
529536 log.info("{s}", .{location});
530 var req = try client.request(.GET, uri, h, .{});
537 var req = try client.open(.GET, uri, h, .{});
531538 defer req.deinit();
532539
533 try req.start(.{});
540 try req.send(.{});
534541 try req.wait();
535542
536543 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -540,7 +547,7 @@ pub fn main() !void {
540547 }
541548
542549 // connection has been kept alive
543 try testing.expect(client.connection_pool.free_len == 1);
550 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
544551
545552 { // too many redirects
546553 var h = http.Headers{ .allocator = calloc };
......@@ -551,10 +558,10 @@ pub fn main() !void {
551558 const uri = try std.Uri.parse(location);
552559
553560 log.info("{s}", .{location});
554 var req = try client.request(.GET, uri, h, .{});
561 var req = try client.open(.GET, uri, h, .{});
555562 defer req.deinit();
556563
557 try req.start(.{});
564 try req.send(.{});
558565 req.wait() catch |err| switch (err) {
559566 error.TooManyHttpRedirects => {},
560567 else => return err,
......@@ -562,7 +569,7 @@ pub fn main() !void {
562569 }
563570
564571 // connection has been kept alive
565 try testing.expect(client.connection_pool.free_len == 1);
572 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
566573
567574 { // check client without segfault by connection error after redirection
568575 var h = http.Headers{ .allocator = calloc };
......@@ -573,17 +580,20 @@ pub fn main() !void {
573580 const uri = try std.Uri.parse(location);
574581
575582 log.info("{s}", .{location});
576 var req = try client.request(.GET, uri, h, .{});
583 var req = try client.open(.GET, uri, h, .{});
577584 defer req.deinit();
578585
579 try req.start(.{});
586 try req.send(.{});
580587 const result = req.wait();
581588
582 try testing.expectError(error.ConnectionRefused, result); // expects not segfault but the regular error
589 // a proxy without an upstream is likely to return a 5xx status.
590 if (client.http_proxy == null) {
591 try testing.expectError(error.ConnectionRefused, result); // expects not segfault but the regular error
592 }
583593 }
584594
585595 // connection has been kept alive
586 try testing.expect(client.connection_pool.free_len == 1);
596 try testing.expect(client.http_proxy != null or client.connection_pool.free_len == 1);
587597
588598 { // Client.fetch()
589599 var h = http.Headers{ .allocator = calloc };
......@@ -618,15 +628,12 @@ pub fn main() !void {
618628 const uri = try std.Uri.parse(location);
619629
620630 log.info("{s}", .{location});
621 var req = try client.request(.POST, uri, h, .{});
631 var req = try client.open(.POST, uri, h, .{});
622632 defer req.deinit();
623633
624634 req.transfer_encoding = .chunked;
625635
626 try req.start(.{});
627 try req.wait();
628 try testing.expectEqual(http.Status.@"continue", req.response.status);
629
636 try req.send(.{});
630637 try req.writeAll("Hello, ");
631638 try req.writeAll("World!\n");
632639 try req.finish();
......@@ -652,12 +659,12 @@ pub fn main() !void {
652659 const uri = try std.Uri.parse(location);
653660
654661 log.info("{s}", .{location});
655 var req = try client.request(.POST, uri, h, .{});
662 var req = try client.open(.POST, uri, h, .{});
656663 defer req.deinit();
657664
658665 req.transfer_encoding = .chunked;
659666
660 try req.start(.{});
667 try req.send(.{});
661668 try req.wait();
662669 try testing.expectEqual(http.Status.expectation_failed, req.response.status);
663670 }
......@@ -672,9 +679,9 @@ pub fn main() !void {
672679 defer calloc.free(requests);
673680
674681 for (0..total_connections) |i| {
675 var req = try client.request(.GET, uri, .{ .allocator = calloc }, .{});
682 var req = try client.open(.GET, uri, .{ .allocator = calloc }, .{});
676683 req.response.parser.done = true;
677 req.connection.?.data.closing = false;
684 req.connection.?.closing = false;
678685 requests[i] = req;
679686 }
680687