authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-16 18:50:38-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
logf9dff2fcf1d5c6df1d6b935c4eb536793148a9d9
treec48b4e45e64d6cf72e802815491ffaa1607c4fb1
parent78192637fba21c1cc6fca364be716795e4e44020

std.http: fields at the top of the struct

Perhaps the language should enforce this.

2 files changed, 77 insertions(+), 77 deletions(-)

lib/std/http/Client.zig+62-62
......@@ -44,6 +44,14 @@ https_proxy: ?*Proxy = null,
4444
4545/// A set of linked lists of connections that can be reused.
4646pub const ConnectionPool = struct {
47 mutex: std.Thread.Mutex = .{},
48 /// Open connections that are currently in use.
49 used: Queue = .{},
50 /// Open connections that are not currently in use.
51 free: Queue = .{},
52 free_len: usize = 0,
53 free_size: usize = 32,
54
4755 /// The criteria for a connection to be considered a match.
4856 pub const Criteria = struct {
4957 host: []const u8,
......@@ -54,14 +62,6 @@ pub const ConnectionPool = struct {
5462 const Queue = std.DoublyLinkedList(Connection);
5563 pub const Node = Queue.Node;
5664
57 mutex: std.Thread.Mutex = .{},
58 /// Open connections that are currently in use.
59 used: Queue = .{},
60 /// Open connections that are not currently in use.
61 free: Queue = .{},
62 free_len: usize = 0,
63 free_size: usize = 32,
64
6565 /// Finds and acquires a connection from the connection pool matching the criteria. This function is threadsafe.
6666 /// If no connection is found, null is returned.
6767 pub fn findConnection(pool: *ConnectionPool, criteria: Criteria) ?*Connection {
......@@ -190,11 +190,6 @@ pub const ConnectionPool = struct {
190190
191191/// An interface to either a plain or TLS connection.
192192pub const Connection = struct {
193 pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
194 const BufferSize = std.math.IntFittingRange(0, buffer_size);
195
196 pub const Protocol = enum { plain, tls };
197
198193 stream: net.Stream,
199194 /// undefined unless protocol is tls.
200195 tls_client: if (!disable_tls) *std.crypto.tls.Client else void,
......@@ -220,6 +215,11 @@ pub const Connection = struct {
220215 read_buf: [buffer_size]u8 = undefined,
221216 write_buf: [buffer_size]u8 = undefined,
222217
218 pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
219 const BufferSize = std.math.IntFittingRange(0, buffer_size);
220
221 pub const Protocol = enum { plain, tls };
222
223223 pub fn readvDirectTls(conn: *Connection, buffers: []std.os.iovec) ReadError!usize {
224224 return conn.tls_client.readv(conn.stream, buffers) catch |err| {
225225 // https://github.com/ziglang/zig/issues/2473
......@@ -419,6 +419,35 @@ pub const Compression = union(enum) {
419419
420420/// A HTTP response originating from a server.
421421pub const Response = struct {
422 version: http.Version,
423 status: http.Status,
424 reason: []const u8,
425
426 /// Points into the user-provided `server_header_buffer`.
427 location: ?[]const u8 = null,
428 /// Points into the user-provided `server_header_buffer`.
429 content_type: ?[]const u8 = null,
430 /// Points into the user-provided `server_header_buffer`.
431 content_disposition: ?[]const u8 = null,
432
433 keep_alive: bool = false,
434
435 /// If present, the number of bytes in the response body.
436 content_length: ?u64 = null,
437
438 /// If present, the transfer encoding of the response body, otherwise none.
439 transfer_encoding: http.TransferEncoding = .none,
440
441 /// If present, the compression of the response body, otherwise identity (no compression).
442 transfer_compression: http.ContentEncoding = .identity,
443
444 parser: proto.HeadersParser,
445 compression: Compression = .none,
446
447 /// Whether the response body should be skipped. Any data read from the
448 /// response body will be discarded.
449 skip: bool = false,
450
422451 pub const ParseError = error{
423452 HttpHeadersInvalid,
424453 HttpHeaderContinuationsUnsupported,
......@@ -542,35 +571,6 @@ pub const Response = struct {
542571 pub fn iterateHeaders(r: Response) proto.HeaderIterator {
543572 return proto.HeaderIterator.init(r.parser.get());
544573 }
545
546 version: http.Version,
547 status: http.Status,
548 reason: []const u8,
549
550 /// Points into the user-provided `server_header_buffer`.
551 location: ?[]const u8 = null,
552 /// Points into the user-provided `server_header_buffer`.
553 content_type: ?[]const u8 = null,
554 /// Points into the user-provided `server_header_buffer`.
555 content_disposition: ?[]const u8 = null,
556
557 keep_alive: bool = false,
558
559 /// If present, the number of bytes in the response body.
560 content_length: ?u64 = null,
561
562 /// If present, the transfer encoding of the response body, otherwise none.
563 transfer_encoding: http.TransferEncoding = .none,
564
565 /// If present, the compression of the response body, otherwise identity (no compression).
566 transfer_compression: http.ContentEncoding = .identity,
567
568 parser: proto.HeadersParser,
569 compression: Compression = .none,
570
571 /// Whether the response body should be skipped. Any data read from the
572 /// response body will be discarded.
573 skip: bool = false,
574574};
575575
576576/// A HTTP request that has been sent.
......@@ -1558,6 +1558,26 @@ pub fn open(
15581558}
15591559
15601560pub const FetchOptions = struct {
1561 server_header_buffer: ?[]u8 = null,
1562 response_strategy: ResponseStrategy = .{ .storage = .{ .dynamic = 16 * 1024 * 1024 } },
1563 redirect_behavior: ?Request.RedirectBehavior = null,
1564
1565 location: Location,
1566 method: http.Method = .GET,
1567 payload: Payload = .none,
1568 raw_uri: bool = false,
1569
1570 /// Standard headers that have default, but overridable, behavior.
1571 headers: Request.Headers = .{},
1572 /// These headers are kept including when following a redirect to a
1573 /// different domain.
1574 /// Externally-owned; must outlive the Request.
1575 extra_headers: []const http.Header = &.{},
1576 /// These headers are stripped when following a redirect to a different
1577 /// domain.
1578 /// Externally-owned; must outlive the Request.
1579 privileged_headers: []const http.Header = &.{},
1580
15611581 pub const Location = union(enum) {
15621582 url: []const u8,
15631583 uri: Uri,
......@@ -1587,26 +1607,6 @@ pub const FetchOptions = struct {
15871607 /// cannot be returned from `read()`.
15881608 static: []u8,
15891609 };
1590
1591 server_header_buffer: ?[]u8 = null,
1592 response_strategy: ResponseStrategy = .{ .storage = .{ .dynamic = 16 * 1024 * 1024 } },
1593 redirect_behavior: ?Request.RedirectBehavior = null,
1594
1595 location: Location,
1596 method: http.Method = .GET,
1597 payload: Payload = .none,
1598 raw_uri: bool = false,
1599
1600 /// Standard headers that have default, but overridable, behavior.
1601 headers: Request.Headers = .{},
1602 /// These headers are kept including when following a redirect to a
1603 /// different domain.
1604 /// Externally-owned; must outlive the Request.
1605 extra_headers: []const http.Header = &.{},
1606 /// These headers are stripped when following a redirect to a different
1607 /// domain.
1608 /// Externally-owned; must outlive the Request.
1609 privileged_headers: []const http.Header = &.{},
16101610};
16111611
16121612pub const FetchResult = struct {
lib/std/http/Server.zig+15-15
......@@ -21,9 +21,6 @@ socket: net.StreamServer,
2121
2222/// An interface to a plain connection.
2323pub const Connection = struct {
24 pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
25 pub const Protocol = enum { plain };
26
2724 stream: net.Stream,
2825 protocol: Protocol,
2926
......@@ -33,6 +30,9 @@ pub const Connection = struct {
3330 read_start: u16 = 0,
3431 read_end: u16 = 0,
3532
33 pub const buffer_size = std.crypto.tls.max_ciphertext_record_len;
34 pub const Protocol = enum { plain };
35
3636 pub fn rawReadAtLeast(conn: *Connection, buffer: []u8, len: usize) ReadError!usize {
3737 return switch (conn.protocol) {
3838 .plain => conn.stream.readAtLeast(buffer, len),
......@@ -174,6 +174,18 @@ pub const Compression = union(enum) {
174174
175175/// A HTTP request originating from a client.
176176pub const Request = struct {
177 method: http.Method,
178 target: []const u8,
179 version: http.Version,
180 expect: ?[]const u8 = null,
181 content_type: ?[]const u8 = null,
182 content_length: ?u64 = null,
183 transfer_encoding: http.TransferEncoding = .none,
184 transfer_compression: http.ContentEncoding = .identity,
185 keep_alive: bool = false,
186 parser: proto.HeadersParser,
187 compression: Compression = .none,
188
177189 pub const ParseError = Allocator.Error || error{
178190 UnknownHttpMethod,
179191 HttpHeadersInvalid,
......@@ -284,18 +296,6 @@ pub const Request = struct {
284296 inline fn int64(array: *const [8]u8) u64 {
285297 return @bitCast(array.*);
286298 }
287
288 method: http.Method,
289 target: []const u8,
290 version: http.Version,
291 expect: ?[]const u8 = null,
292 content_type: ?[]const u8 = null,
293 content_length: ?u64 = null,
294 transfer_encoding: http.TransferEncoding = .none,
295 transfer_compression: http.ContentEncoding = .identity,
296 keep_alive: bool = false,
297 parser: proto.HeadersParser,
298 compression: Compression = .none,
299299};
300300
301301/// A HTTP response waiting to be sent.