authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-01 16:38:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-06 22:42:42-07:00
log98ee9360555730328621aa8b9b9170a4e2b0df7b
tree45b19ca078a8600129b4b2a78d0b8f7227854533
parent8f06754a06996e1114e5ada9644fa36da4908642

http fixes


7 files changed, 298 insertions(+), 245 deletions(-)

lib/std/Io/Writer.zig+77-19
......@@ -191,29 +191,87 @@ pub fn writeSplatHeader(
191191 data: []const []const u8,
192192 splat: usize,
193193) Error!usize {
194 const new_end = w.end + header.len;
195 if (new_end <= w.buffer.len) {
196 @memcpy(w.buffer[w.end..][0..header.len], header);
197 w.end = new_end;
198 return header.len + try writeSplat(w, data, splat);
199 }
200 var vecs: [8][]const u8 = undefined; // Arbitrarily chosen size.
201 var i: usize = 1;
202 vecs[0] = header;
203 for (data[0 .. data.len - 1]) |buf| {
204 if (buf.len == 0) continue;
205 vecs[i] = buf;
206 i += 1;
207 if (vecs.len - i == 0) break;
194 return writeSplatHeaderLimit(w, header, data, splat, .unlimited);
195}
196
197/// Equivalent to `writeSplatHeader` but writes at most `limit` bytes.
198pub fn writeSplatHeaderLimit(
199 w: *Writer,
200 header: []const u8,
201 data: []const []const u8,
202 splat: usize,
203 limit: Limit,
204) Error!usize {
205 var remaining = @intFromEnum(limit);
206 {
207 const copy_len = @min(header.len, w.buffer.len - w.end, remaining);
208 if (header.len - copy_len != 0) return writeSplatHeaderLimitFinish(w, header, data, splat, remaining);
209 @memcpy(w.buffer[w.end..][0..copy_len], header[0..copy_len]);
210 w.end += copy_len;
211 remaining -= copy_len;
212 }
213 for (data[0 .. data.len - 1], 0..) |buf, i| {
214 const copy_len = @min(buf.len, w.buffer.len - w.end, remaining);
215 if (buf.len - copy_len != 0) return @intFromEnum(limit) - remaining +
216 try writeSplatHeaderLimitFinish(w, &.{}, data[i..], splat, remaining);
217 @memcpy(w.buffer[w.end..][0..copy_len], buf[0..copy_len]);
218 w.end += copy_len;
219 remaining -= copy_len;
208220 }
209221 const pattern = data[data.len - 1];
210 const new_splat = s: {
211 if (pattern.len == 0 or vecs.len - i == 0) break :s 1;
222 const splat_n = pattern.len * splat;
223 if (splat_n > @min(w.buffer.len - w.end, remaining)) {
224 const buffered_n = @intFromEnum(limit) - remaining;
225 const written = try writeSplatHeaderLimitFinish(w, &.{}, data[data.len - 1 ..][0..1], splat, remaining);
226 return buffered_n + written;
227 }
228
229 for (0..splat) |_| {
230 @memcpy(w.buffer[w.end..][0..pattern.len], pattern);
231 w.end += pattern.len;
232 }
233
234 remaining -= splat_n;
235 return @intFromEnum(limit) - remaining;
236}
237
238fn writeSplatHeaderLimitFinish(
239 w: *Writer,
240 header: []const u8,
241 data: []const []const u8,
242 splat: usize,
243 limit: usize,
244) Error!usize {
245 var remaining = limit;
246 var vecs: [8][]const u8 = undefined;
247 var i: usize = 0;
248 v: {
249 if (header.len != 0) {
250 const copy_len = @min(header.len, remaining);
251 vecs[i] = header[0..copy_len];
252 i += 1;
253 remaining -= copy_len;
254 if (remaining == 0) break :v;
255 }
256 for (data[0 .. data.len - 1]) |buf| if (buf.len != 0) {
257 const copy_len = @min(header.len, remaining);
258 vecs[i] = buf;
259 i += 1;
260 remaining -= copy_len;
261 if (remaining == 0) break :v;
262 if (vecs.len - i == 0) break :v;
263 };
264 const pattern = data[data.len - 1];
265 if (splat == 1) {
266 vecs[i] = pattern[0..@min(remaining, pattern.len)];
267 i += 1;
268 break :v;
269 }
212270 vecs[i] = pattern;
213271 i += 1;
214 break :s splat;
215 };
216 return w.vtable.drain(w, vecs[0..i], new_splat);
272 return w.vtable.drain(w, (&vecs)[0..i], @min(remaining / pattern.len, splat));
273 }
274 return w.vtable.drain(w, (&vecs)[0..i], 1);
217275}
218276
219277test "writeSplatHeader splatting avoids buffer aliasing temptation" {
lib/std/Uri.zig+90-146
......@@ -4,6 +4,8 @@
44const std = @import("std.zig");
55const testing = std.testing;
66const Uri = @This();
7const Allocator = std.mem.Allocator;
8const Writer = std.Io.Writer;
79
810scheme: []const u8,
911user: ?Component = null,
......@@ -14,6 +16,32 @@ path: Component = Component.empty,
1416query: ?Component = null,
1517fragment: ?Component = null,
1618
19pub const host_name_max = 255;
20
21/// Returned value may point into `buffer` or be the original string.
22///
23/// Suggested buffer length: `host_name_max`.
24///
25/// See also:
26/// * `getHostAlloc`
27pub fn getHost(uri: Uri, buffer: []u8) error{ UriMissingHost, UriHostTooLong }![]const u8 {
28 const component = uri.host orelse return error.UriMissingHost;
29 return component.toRaw(buffer) catch |err| switch (err) {
30 error.NoSpaceLeft => return error.UriHostTooLong,
31 };
32}
33
34/// Returned value may point into `buffer` or be the original string.
35///
36/// See also:
37/// * `getHost`
38pub fn getHostAlloc(uri: Uri, arena: Allocator) error{ UriMissingHost, UriHostTooLong, OutOfMemory }![]const u8 {
39 const component = uri.host orelse return error.UriMissingHost;
40 const result = try component.toRawMaybeAlloc(arena);
41 if (result.len > host_name_max) return error.UriHostTooLong;
42 return result;
43}
44
1745pub const Component = union(enum) {
1846 /// Invalid characters in this component must be percent encoded
1947 /// before being printed as part of a URI.
......@@ -30,11 +58,19 @@ pub const Component = union(enum) {
3058 };
3159 }
3260
61 /// Returned value may point into `buffer` or be the original string.
62 pub fn toRaw(component: Component, buffer: []u8) error{NoSpaceLeft}![]const u8 {
63 return switch (component) {
64 .raw => |raw| raw,
65 .percent_encoded => |percent_encoded| if (std.mem.indexOfScalar(u8, percent_encoded, '%')) |_|
66 try std.fmt.bufPrint(buffer, "{f}", .{std.fmt.alt(component, .formatRaw)})
67 else
68 percent_encoded,
69 };
70 }
71
3372 /// Allocates the result with `arena` only if needed, so the result should not be freed.
34 pub fn toRawMaybeAlloc(
35 component: Component,
36 arena: std.mem.Allocator,
37 ) std.mem.Allocator.Error![]const u8 {
73 pub fn toRawMaybeAlloc(component: Component, arena: Allocator) Allocator.Error![]const u8 {
3874 return switch (component) {
3975 .raw => |raw| raw,
4076 .percent_encoded => |percent_encoded| if (std.mem.indexOfScalar(u8, percent_encoded, '%')) |_|
......@@ -44,7 +80,7 @@ pub const Component = union(enum) {
4480 };
4581 }
4682
47 pub fn formatRaw(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
83 pub fn formatRaw(component: Component, w: *Writer) Writer.Error!void {
4884 switch (component) {
4985 .raw => |raw| try w.writeAll(raw),
5086 .percent_encoded => |percent_encoded| {
......@@ -67,56 +103,56 @@ pub const Component = union(enum) {
67103 }
68104 }
69105
70 pub fn formatEscaped(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
106 pub fn formatEscaped(component: Component, w: *Writer) Writer.Error!void {
71107 switch (component) {
72108 .raw => |raw| try percentEncode(w, raw, isUnreserved),
73109 .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded),
74110 }
75111 }
76112
77 pub fn formatUser(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
113 pub fn formatUser(component: Component, w: *Writer) Writer.Error!void {
78114 switch (component) {
79115 .raw => |raw| try percentEncode(w, raw, isUserChar),
80116 .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded),
81117 }
82118 }
83119
84 pub fn formatPassword(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
120 pub fn formatPassword(component: Component, w: *Writer) Writer.Error!void {
85121 switch (component) {
86122 .raw => |raw| try percentEncode(w, raw, isPasswordChar),
87123 .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded),
88124 }
89125 }
90126
91 pub fn formatHost(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
127 pub fn formatHost(component: Component, w: *Writer) Writer.Error!void {
92128 switch (component) {
93129 .raw => |raw| try percentEncode(w, raw, isHostChar),
94130 .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded),
95131 }
96132 }
97133
98 pub fn formatPath(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
134 pub fn formatPath(component: Component, w: *Writer) Writer.Error!void {
99135 switch (component) {
100136 .raw => |raw| try percentEncode(w, raw, isPathChar),
101137 .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded),
102138 }
103139 }
104140
105 pub fn formatQuery(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
141 pub fn formatQuery(component: Component, w: *Writer) Writer.Error!void {
106142 switch (component) {
107143 .raw => |raw| try percentEncode(w, raw, isQueryChar),
108144 .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded),
109145 }
110146 }
111147
112 pub fn formatFragment(component: Component, w: *std.io.Writer) std.io.Writer.Error!void {
148 pub fn formatFragment(component: Component, w: *Writer) Writer.Error!void {
113149 switch (component) {
114150 .raw => |raw| try percentEncode(w, raw, isFragmentChar),
115151 .percent_encoded => |percent_encoded| try w.writeAll(percent_encoded),
116152 }
117153 }
118154
119 pub fn percentEncode(w: *std.io.Writer, raw: []const u8, comptime isValidChar: fn (u8) bool) std.io.Writer.Error!void {
155 pub fn percentEncode(w: *Writer, raw: []const u8, comptime isValidChar: fn (u8) bool) Writer.Error!void {
120156 var start: usize = 0;
121157 for (raw, 0..) |char, index| {
122158 if (isValidChar(char)) continue;
......@@ -165,17 +201,15 @@ pub const ParseError = error{ UnexpectedCharacter, InvalidFormat, InvalidPort };
165201/// The return value will contain strings pointing into the original `text`.
166202/// Each component that is provided, will be non-`null`.
167203pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri {
168 var reader = SliceReader{ .slice = text };
169
170204 var uri: Uri = .{ .scheme = scheme, .path = undefined };
205 var i: usize = 0;
171206
172 if (reader.peekPrefix("//")) a: { // authority part
173 std.debug.assert(reader.get().? == '/');
174 std.debug.assert(reader.get().? == '/');
175
176 const authority = reader.readUntil(isAuthoritySeparator);
207 if (std.mem.startsWith(u8, text, "//")) a: {
208 i = std.mem.indexOfAnyPos(u8, text, 2, &authority_sep) orelse text.len;
209 const authority = text[2..i];
177210 if (authority.len == 0) {
178 if (reader.peekPrefix("/")) break :a else return error.InvalidFormat;
211 if (!std.mem.startsWith(u8, text[2..], "/")) return error.InvalidFormat;
212 break :a;
179213 }
180214
181215 var start_of_host: usize = 0;
......@@ -225,26 +259,28 @@ pub fn parseAfterScheme(scheme: []const u8, text: []const u8) ParseError!Uri {
225259 uri.host = .{ .percent_encoded = authority[start_of_host..end_of_host] };
226260 }
227261
228 uri.path = .{ .percent_encoded = reader.readUntil(isPathSeparator) };
262 const path_start = i;
263 i = std.mem.indexOfAnyPos(u8, text, path_start, &path_sep) orelse text.len;
264 uri.path = .{ .percent_encoded = text[path_start..i] };
229265
230 if ((reader.peek() orelse 0) == '?') { // query part
231 std.debug.assert(reader.get().? == '?');
232 uri.query = .{ .percent_encoded = reader.readUntil(isQuerySeparator) };
266 if (std.mem.startsWith(u8, text[i..], "?")) {
267 const query_start = i + 1;
268 i = std.mem.indexOfScalarPos(u8, text, query_start, '#') orelse text.len;
269 uri.query = .{ .percent_encoded = text[query_start..i] };
233270 }
234271
235 if ((reader.peek() orelse 0) == '#') { // fragment part
236 std.debug.assert(reader.get().? == '#');
237 uri.fragment = .{ .percent_encoded = reader.readUntilEof() };
272 if (std.mem.startsWith(u8, text[i..], "#")) {
273 uri.fragment = .{ .percent_encoded = text[i + 1 ..] };
238274 }
239275
240276 return uri;
241277}
242278
243pub fn format(uri: *const Uri, writer: *std.io.Writer) std.io.Writer.Error!void {
279pub fn format(uri: *const Uri, writer: *Writer) Writer.Error!void {
244280 return writeToStream(uri, writer, .all);
245281}
246282
247pub fn writeToStream(uri: *const Uri, writer: *std.io.Writer, flags: Format.Flags) std.io.Writer.Error!void {
283pub fn writeToStream(uri: *const Uri, writer: *Writer, flags: Format.Flags) Writer.Error!void {
248284 if (flags.scheme) {
249285 try writer.print("{s}:", .{uri.scheme});
250286 if (flags.authority and uri.host != null) {
......@@ -318,7 +354,7 @@ pub const Format = struct {
318354 };
319355 };
320356
321 pub fn default(f: Format, writer: *std.io.Writer) std.io.Writer.Error!void {
357 pub fn default(f: Format, writer: *Writer) Writer.Error!void {
322358 return writeToStream(f.uri, writer, f.flags);
323359 }
324360};
......@@ -327,41 +363,33 @@ pub fn fmt(uri: *const Uri, flags: Format.Flags) std.fmt.Formatter(Format, Forma
327363 return .{ .data = .{ .uri = uri, .flags = flags } };
328364}
329365
330/// Parses the URI or returns an error.
331/// The return value will contain strings pointing into the
332/// original `text`. Each component that is provided, will be non-`null`.
366/// The return value will contain strings pointing into the original `text`.
367/// Each component that is provided will be non-`null`.
333368pub fn parse(text: []const u8) ParseError!Uri {
334 var reader: SliceReader = .{ .slice = text };
335 const scheme = reader.readWhile(isSchemeChar);
336
337 // after the scheme, a ':' must appear
338 if (reader.get()) |c| {
339 if (c != ':')
340 return error.UnexpectedCharacter;
341 } else {
342 return error.InvalidFormat;
343 }
344
345 return parseAfterScheme(scheme, reader.readUntilEof());
369 const end = for (text, 0..) |byte, i| {
370 if (!isSchemeChar(byte)) break i;
371 } else text.len;
372 // After the scheme, a ':' must appear.
373 if (end >= text.len) return error.InvalidFormat;
374 if (text[end] != ':') return error.UnexpectedCharacter;
375 return parseAfterScheme(text[0..end], text[end + 1 ..]);
346376}
347377
348378pub const ResolveInPlaceError = ParseError || error{NoSpaceLeft};
349379
350380/// Resolves a URI against a base URI, conforming to RFC 3986, Section 5.
351/// Copies `new` to the beginning of `aux_buf.*`, allowing the slices to overlap,
352/// then parses `new` as a URI, and then resolves the path in place.
381///
382/// Assumes new location is already copied to the beginning of `aux_buf.*`.
383/// Parses that new location as a URI, and then resolves the path in place.
384///
353385/// If a merge needs to take place, the newly constructed path will be stored
354/// in `aux_buf.*` just after the copied `new`, and `aux_buf.*` will be modified
355/// to only contain the remaining unused space.
356pub fn resolve_inplace(base: Uri, new: []const u8, aux_buf: *[]u8) ResolveInPlaceError!Uri {
357 std.mem.copyForwards(u8, aux_buf.*, new);
358 // At this point, new is an invalid pointer.
359 const new_mut = aux_buf.*[0..new.len];
360 aux_buf.* = aux_buf.*[new.len..];
361
362 const new_parsed = parse(new_mut) catch |err|
363 (parseAfterScheme("", new_mut) catch return err);
364 // As you can see above, `new_mut` is not a const pointer.
386/// in `aux_buf.*` just after the copied location, and `aux_buf.*` will be
387/// modified to only contain the remaining unused space.
388pub fn resolveInPlace(base: Uri, new_len: usize, aux_buf: *[]u8) ResolveInPlaceError!Uri {
389 const new = aux_buf.*[0..new_len];
390 const new_parsed = parse(new) catch |err| (parseAfterScheme("", new) catch return err);
391 aux_buf.* = aux_buf.*[new_len..];
392 // As you can see above, `new` is not a const pointer.
365393 const new_path: []u8 = @constCast(new_parsed.path.percent_encoded);
366394
367395 if (new_parsed.scheme.len > 0) return .{
......@@ -461,7 +489,7 @@ test remove_dot_segments {
461489
462490/// 5.2.3. Merge Paths
463491fn merge_paths(base: Component, new: []u8, aux_buf: *[]u8) error{NoSpaceLeft}!Component {
464 var aux: std.io.Writer = .fixed(aux_buf.*);
492 var aux: Writer = .fixed(aux_buf.*);
465493 if (!base.isEmpty()) {
466494 base.formatPath(&aux) catch return error.NoSpaceLeft;
467495 aux.end = std.mem.lastIndexOfScalar(u8, aux.buffered(), '/') orelse return remove_dot_segments(new);
......@@ -472,59 +500,6 @@ fn merge_paths(base: Component, new: []u8, aux_buf: *[]u8) error{NoSpaceLeft}!Co
472500 return merged_path;
473501}
474502
475const SliceReader = struct {
476 const Self = @This();
477
478 slice: []const u8,
479 offset: usize = 0,
480
481 fn get(self: *Self) ?u8 {
482 if (self.offset >= self.slice.len)
483 return null;
484 const c = self.slice[self.offset];
485 self.offset += 1;
486 return c;
487 }
488
489 fn peek(self: Self) ?u8 {
490 if (self.offset >= self.slice.len)
491 return null;
492 return self.slice[self.offset];
493 }
494
495 fn readWhile(self: *Self, comptime predicate: fn (u8) bool) []const u8 {
496 const start = self.offset;
497 var end = start;
498 while (end < self.slice.len and predicate(self.slice[end])) {
499 end += 1;
500 }
501 self.offset = end;
502 return self.slice[start..end];
503 }
504
505 fn readUntil(self: *Self, comptime predicate: fn (u8) bool) []const u8 {
506 const start = self.offset;
507 var end = start;
508 while (end < self.slice.len and !predicate(self.slice[end])) {
509 end += 1;
510 }
511 self.offset = end;
512 return self.slice[start..end];
513 }
514
515 fn readUntilEof(self: *Self) []const u8 {
516 const start = self.offset;
517 self.offset = self.slice.len;
518 return self.slice[start..];
519 }
520
521 fn peekPrefix(self: Self, prefix: []const u8) bool {
522 if (self.offset + prefix.len > self.slice.len)
523 return false;
524 return std.mem.eql(u8, self.slice[self.offset..][0..prefix.len], prefix);
525 }
526};
527
528503/// scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
529504fn isSchemeChar(c: u8) bool {
530505 return switch (c) {
......@@ -533,19 +508,6 @@ fn isSchemeChar(c: u8) bool {
533508 };
534509}
535510
536/// reserved = gen-delims / sub-delims
537fn isReserved(c: u8) bool {
538 return isGenLimit(c) or isSubLimit(c);
539}
540
541/// gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"
542fn isGenLimit(c: u8) bool {
543 return switch (c) {
544 ':', ',', '?', '#', '[', ']', '@' => true,
545 else => false,
546 };
547}
548
549511/// sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
550512/// / "*" / "+" / "," / ";" / "="
551513fn isSubLimit(c: u8) bool {
......@@ -585,26 +547,8 @@ fn isQueryChar(c: u8) bool {
585547
586548const isFragmentChar = isQueryChar;
587549
588fn isAuthoritySeparator(c: u8) bool {
589 return switch (c) {
590 '/', '?', '#' => true,
591 else => false,
592 };
593}
594
595fn isPathSeparator(c: u8) bool {
596 return switch (c) {
597 '?', '#' => true,
598 else => false,
599 };
600}
601
602fn isQuerySeparator(c: u8) bool {
603 return switch (c) {
604 '#' => true,
605 else => false,
606 };
607}
550const authority_sep: [3]u8 = .{ '/', '?', '#' };
551const path_sep: [2]u8 = .{ '?', '#' };
608552
609553test "basic" {
610554 const parsed = try parse("https://ziglang.org/download");
lib/std/crypto/tls/Client.zig+3-1
......@@ -328,7 +328,9 @@ pub fn init(input: *Reader, output: *Writer, options: Options) InitError!Client
328328 var cleartext_bufs: [2][tls.max_ciphertext_inner_record_len]u8 = undefined;
329329 fragment: while (true) {
330330 // Ensure the input buffer pointer is stable in this scope.
331 input.rebaseCapacity(tls.max_ciphertext_record_len);
331 input.rebase(tls.max_ciphertext_record_len) catch |err| switch (err) {
332 error.EndOfStream => {}, // We have assurance the remainder of stream can be buffered.
333 };
332334 const record_header = input.peek(tls.record_header_len) catch |err| switch (err) {
333335 error.EndOfStream => return error.TlsConnectionTruncated,
334336 error.ReadFailed => return error.ReadFailed,
lib/std/http.zig+63-19
......@@ -343,9 +343,6 @@ pub const Reader = struct {
343343 /// read from `in`.
344344 trailers: []const u8 = &.{},
345345 body_err: ?BodyError = null,
346 /// Determines at which point `error.HttpHeadersOversize` occurs, as well
347 /// as the minimum buffer capacity of `in`.
348 max_head_len: usize,
349346
350347 pub const RemainingChunkLen = enum(u64) {
351348 head = 0,
......@@ -397,27 +394,34 @@ pub const Reader = struct {
397394 ReadFailed,
398395 };
399396
400 /// Buffers the entire head.
401 pub fn receiveHead(reader: *Reader) HeadError!void {
397 /// Buffers the entire head inside `in`.
398 ///
399 /// The resulting memory is invalidated by any subsequent consumption of
400 /// the input stream.
401 pub fn receiveHead(reader: *Reader) HeadError![]const u8 {
402402 reader.trailers = &.{};
403403 const in = reader.in;
404 try in.rebase(reader.max_head_len);
405404 var hp: HeadParser = .{};
406 var head_end: usize = 0;
405 var head_len: usize = 0;
407406 while (true) {
408 if (head_end >= in.buffer.len) return error.HttpHeadersOversize;
409 in.fillMore() catch |err| switch (err) {
410 error.EndOfStream => switch (head_end) {
411 0 => return error.HttpConnectionClosing,
412 else => return error.HttpRequestTruncated,
413 },
414 error.ReadFailed => return error.ReadFailed,
415 };
416 head_end += hp.feed(in.buffered()[head_end..]);
407 if (in.buffer.len - head_len == 0) return error.HttpHeadersOversize;
408 const remaining = in.buffered()[head_len..];
409 if (remaining.len == 0) {
410 in.fillMore() catch |err| switch (err) {
411 error.EndOfStream => switch (head_len) {
412 0 => return error.HttpConnectionClosing,
413 else => return error.HttpRequestTruncated,
414 },
415 error.ReadFailed => return error.ReadFailed,
416 };
417 continue;
418 }
419 head_len += hp.feed(remaining);
417420 if (hp.state == .finished) {
418 reader.head_buffer = in.steal(head_end);
419421 reader.state = .received_head;
420 return;
422 const head_buffer = in.buffered()[0..head_len];
423 in.toss(head_len);
424 return head_buffer;
421425 }
422426 }
423427 }
......@@ -786,7 +790,7 @@ pub const BodyWriter = struct {
786790 };
787791
788792 pub fn isEliding(w: *const BodyWriter) bool {
789 return w.writer.vtable.drain == Writer.discardingDrain;
793 return w.writer.vtable.drain == elidingDrain;
790794 }
791795
792796 /// Sends all buffered data across `BodyWriter.http_protocol_output`.
......@@ -930,6 +934,46 @@ pub const BodyWriter = struct {
930934 return w.consume(n);
931935 }
932936
937 pub fn elidingDrain(w: *Writer, data: []const []const u8, splat: usize) Error!usize {
938 const bw: *BodyWriter = @fieldParentPtr("writer", w);
939 const slice = data[0 .. data.len - 1];
940 const pattern = data[slice.len];
941 var written: usize = pattern.len * splat;
942 for (slice) |bytes| written += bytes.len;
943 switch (bw.state) {
944 .content_length => |*len| len.* -= written + w.end,
945 else => {},
946 }
947 w.end = 0;
948 return written;
949 }
950
951 pub fn elidingSendFile(w: *Writer, file_reader: *File.Reader, limit: std.Io.Limit) Writer.FileError!usize {
952 const bw: *BodyWriter = @fieldParentPtr("writer", w);
953 if (File.Handle == void) return error.Unimplemented;
954 if (builtin.zig_backend == .stage2_aarch64) return error.Unimplemented;
955 switch (bw.state) {
956 .content_length => |*len| len.* -= w.end,
957 else => {},
958 }
959 w.end = 0;
960 if (limit == .nothing) return 0;
961 if (file_reader.getSize()) |size| {
962 const n = limit.minInt64(size - file_reader.pos);
963 if (n == 0) return error.EndOfStream;
964 file_reader.seekBy(@intCast(n)) catch return error.Unimplemented;
965 switch (bw.state) {
966 .content_length => |*len| len.* -= n,
967 else => {},
968 }
969 return n;
970 } else |_| {
971 // Error is observable on `file_reader` instance, and it is better to
972 // treat the file as a pipe.
973 return error.Unimplemented;
974 }
975 }
976
933977 /// Returns `null` if size cannot be computed without making any syscalls.
934978 pub fn noneSendFile(w: *Writer, file_reader: *File.Reader, limit: std.Io.Limit) Writer.FileError!usize {
935979 const bw: *BodyWriter = @fieldParentPtr("writer", w);
lib/std/http/Client.zig+15-14
......@@ -821,7 +821,6 @@ pub const Request = struct {
821821
822822 /// Returns the request's `Connection` back to the pool of the `Client`.
823823 pub fn deinit(r: *Request) void {
824 r.reader.restituteHeadBuffer();
825824 if (r.connection) |connection| {
826825 connection.closing = connection.closing or switch (r.reader.state) {
827826 .ready => false,
......@@ -908,13 +907,13 @@ pub const Request = struct {
908907 const connection = r.connection.?;
909908 const w = connection.writer();
910909
911 try r.method.write(w);
910 try r.method.format(w);
912911 try w.writeByte(' ');
913912
914913 if (r.method == .CONNECT) {
915 try uri.writeToStream(.{ .authority = true }, w);
914 try uri.writeToStream(w, .{ .authority = true });
916915 } else {
917 try uri.writeToStream(.{
916 try uri.writeToStream(w, .{
918917 .scheme = connection.proxied,
919918 .authentication = connection.proxied,
920919 .authority = connection.proxied,
......@@ -928,7 +927,7 @@ pub const Request = struct {
928927
929928 if (try emitOverridableHeader("host: ", r.headers.host, w)) {
930929 try w.writeAll("host: ");
931 try uri.writeToStream(.{ .authority = true }, w);
930 try uri.writeToStream(w, .{ .authority = true });
932931 try w.writeAll("\r\n");
933932 }
934933
......@@ -1046,10 +1045,10 @@ pub const Request = struct {
10461045 pub fn receiveHead(r: *Request, redirect_buffer: []u8) ReceiveHeadError!Response {
10471046 var aux_buf = redirect_buffer;
10481047 while (true) {
1049 try r.reader.receiveHead();
1048 const head_buffer = try r.reader.receiveHead();
10501049 const response: Response = .{
10511050 .request = r,
1052 .head = Response.Head.parse(r.reader.head_buffer) catch return error.HttpHeadersInvalid,
1051 .head = Response.Head.parse(head_buffer) catch return error.HttpHeadersInvalid,
10531052 };
10541053 const head = &response.head;
10551054
......@@ -1121,7 +1120,6 @@ pub const Request = struct {
11211120 _ = reader.discardRemaining() catch |err| switch (err) {
11221121 error.ReadFailed => return r.reader.body_err.?,
11231122 };
1124 r.reader.restituteHeadBuffer();
11251123 }
11261124 const new_uri = r.uri.resolveInPlace(location.len, aux_buf) catch |err| switch (err) {
11271125 error.UnexpectedCharacter => return error.HttpRedirectLocationInvalid,
......@@ -1302,12 +1300,13 @@ pub const basic_authorization = struct {
13021300 }
13031301
13041302 pub fn write(uri: Uri, out: *Writer) Writer.Error!void {
1305 var buf: [max_user_len + ":".len + max_password_len]u8 = undefined;
1303 var buf: [max_user_len + 1 + max_password_len]u8 = undefined;
13061304 var w: Writer = .fixed(&buf);
1307 w.print("{fuser}:{fpassword}", .{
1308 uri.user orelse Uri.Component.empty,
1309 uri.password orelse Uri.Component.empty,
1310 }) catch unreachable;
1305 const user: Uri.Component = uri.user orelse .empty;
1306 const password: Uri.Component = uri.user orelse .empty;
1307 user.formatUser(&w) catch unreachable;
1308 w.writeByte(':') catch unreachable;
1309 password.formatPassword(&w) catch unreachable;
13111310 try out.print("Basic {b64}", .{w.buffered()});
13121311 }
13131312};
......@@ -1697,6 +1696,7 @@ pub const FetchError = Uri.ParseError || RequestError || Request.ReceiveHeadErro
16971696 StreamTooLong,
16981697 /// TODO provide optional diagnostics when this occurs or break into more error codes
16991698 WriteFailed,
1699 UnsupportedCompressionMethod,
17001700};
17011701
17021702/// Perform a one-shot HTTP request with the provided options.
......@@ -1748,7 +1748,8 @@ pub fn fetch(client: *Client, options: FetchOptions) FetchError!FetchResult {
17481748 const decompress_buffer: []u8 = switch (response.head.content_encoding) {
17491749 .identity => &.{},
17501750 .zstd => options.decompress_buffer orelse try client.allocator.alloc(u8, std.compress.zstd.default_window_len),
1751 else => options.decompress_buffer orelse try client.allocator.alloc(u8, 8 * 1024),
1751 .deflate, .gzip => options.decompress_buffer orelse try client.allocator.alloc(u8, std.compress.flate.max_window_len),
1752 .compress => return error.UnsupportedCompressionMethod,
17521753 };
17531754 defer if (options.decompress_buffer == null) client.allocator.free(decompress_buffer);
17541755
lib/std/http/Server.zig+21-19
......@@ -6,7 +6,7 @@ const mem = std.mem;
66const Uri = std.Uri;
77const assert = std.debug.assert;
88const testing = std.testing;
9const Writer = std.io.Writer;
9const Writer = std.Io.Writer;
1010
1111const Server = @This();
1212
......@@ -21,7 +21,7 @@ reader: http.Reader,
2121/// header, otherwise `receiveHead` returns `error.HttpHeadersOversize`.
2222///
2323/// The returned `Server` is ready for `receiveHead` to be called.
24pub fn init(in: *std.io.Reader, out: *Writer) Server {
24pub fn init(in: *std.Io.Reader, out: *Writer) Server {
2525 return .{
2626 .reader = .{
2727 .in = in,
......@@ -33,25 +33,22 @@ pub fn init(in: *std.io.Reader, out: *Writer) Server {
3333 };
3434}
3535
36pub fn deinit(s: *Server) void {
37 s.reader.restituteHeadBuffer();
38}
39
4036pub const ReceiveHeadError = http.Reader.HeadError || error{
4137 /// Client sent headers that did not conform to the HTTP protocol.
4238 ///
43 /// To find out more detailed diagnostics, `http.Reader.head_buffer` can be
39 /// To find out more detailed diagnostics, `Request.head_buffer` can be
4440 /// passed directly to `Request.Head.parse`.
4541 HttpHeadersInvalid,
4642};
4743
4844pub fn receiveHead(s: *Server) ReceiveHeadError!Request {
49 try s.reader.receiveHead();
45 const head_buffer = try s.reader.receiveHead();
5046 return .{
5147 .server = s,
48 .head_buffer = head_buffer,
5249 // No need to track the returned error here since users can repeat the
5350 // parse with the header buffer to get detailed diagnostics.
54 .head = Request.Head.parse(s.reader.head_buffer) catch return error.HttpHeadersInvalid,
51 .head = Request.Head.parse(head_buffer) catch return error.HttpHeadersInvalid,
5552 };
5653}
5754
......@@ -60,6 +57,7 @@ pub const Request = struct {
6057 /// Pointers in this struct are invalidated with the next call to
6158 /// `receiveHead`.
6259 head: Head,
60 head_buffer: []const u8,
6361 respond_err: ?RespondError = null,
6462
6563 pub const RespondError = error{
......@@ -229,7 +227,7 @@ pub const Request = struct {
229227
230228 pub fn iterateHeaders(r: *Request) http.HeaderIterator {
231229 assert(r.server.reader.state == .received_head);
232 return http.HeaderIterator.init(r.server.reader.head_buffer);
230 return http.HeaderIterator.init(r.head_buffer);
233231 }
234232
235233 test iterateHeaders {
......@@ -244,7 +242,6 @@ pub const Request = struct {
244242 .reader = .{
245243 .in = undefined,
246244 .state = .received_head,
247 .head_buffer = @constCast(request_bytes),
248245 .interface = undefined,
249246 },
250247 .out = undefined,
......@@ -253,6 +250,7 @@ pub const Request = struct {
253250 var request: Request = .{
254251 .server = &server,
255252 .head = undefined,
253 .head_buffer = @constCast(request_bytes),
256254 };
257255
258256 var it = request.iterateHeaders();
......@@ -435,10 +433,8 @@ pub const Request = struct {
435433
436434 for (o.extra_headers) |header| {
437435 assert(header.name.len != 0);
438 try out.writeAll(header.name);
439 try out.writeAll(": ");
440 try out.writeAll(header.value);
441 try out.writeAll("\r\n");
436 var bufs: [4][]const u8 = .{ header.name, ": ", header.value, "\r\n" };
437 try out.writeVecAll(&bufs);
442438 }
443439
444440 try out.writeAll("\r\n");
......@@ -453,7 +449,13 @@ pub const Request = struct {
453449 return if (elide_body) .{
454450 .http_protocol_output = request.server.out,
455451 .state = state,
456 .writer = .discarding(buffer),
452 .writer = .{
453 .buffer = buffer,
454 .vtable = &.{
455 .drain = http.BodyWriter.elidingDrain,
456 .sendFile = http.BodyWriter.elidingSendFile,
457 },
458 },
457459 } else .{
458460 .http_protocol_output = request.server.out,
459461 .state = state,
......@@ -564,7 +566,7 @@ pub const Request = struct {
564566 ///
565567 /// See `readerExpectNone` for an infallible alternative that cannot write
566568 /// to the server output stream.
567 pub fn readerExpectContinue(request: *Request, buffer: []u8) ExpectContinueError!*std.io.Reader {
569 pub fn readerExpectContinue(request: *Request, buffer: []u8) ExpectContinueError!*std.Io.Reader {
568570 const flush = request.head.expect != null;
569571 try writeExpectContinue(request);
570572 if (flush) try request.server.out.flush();
......@@ -576,7 +578,7 @@ pub const Request = struct {
576578 /// this function.
577579 ///
578580 /// Asserts that this function is only called once.
579 pub fn readerExpectNone(request: *Request, buffer: []u8) *std.io.Reader {
581 pub fn readerExpectNone(request: *Request, buffer: []u8) *std.Io.Reader {
580582 assert(request.server.reader.state == .received_head);
581583 assert(request.head.expect == null);
582584 if (!request.head.method.requestHasBody()) return .ending;
......@@ -640,7 +642,7 @@ pub const Request = struct {
640642/// See https://tools.ietf.org/html/rfc6455
641643pub const WebSocket = struct {
642644 key: []const u8,
643 input: *std.io.Reader,
645 input: *std.Io.Reader,
644646 output: *Writer,
645647
646648 pub const Header0 = packed struct(u8) {
lib/std/http/test.zig+29-27
......@@ -65,7 +65,7 @@ test "trailers" {
6565 try req.sendBodiless();
6666 var response = try req.receiveHead(&.{});
6767
68 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
68 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
6969 defer gpa.free(body);
7070
7171 try expectEqualStrings("Hello, World!\n", body);
......@@ -183,7 +183,11 @@ test "echo content server" {
183183 if (request.head.expect) |expect_header_value| {
184184 if (mem.eql(u8, expect_header_value, "garbage")) {
185185 try expectError(error.HttpExpectationFailed, request.readerExpectContinue(&.{}));
186 try request.respond("", .{ .keep_alive = false });
186 request.head.expect = null;
187 try request.respond("", .{
188 .keep_alive = false,
189 .status = .expectation_failed,
190 });
187191 continue;
188192 }
189193 }
......@@ -204,7 +208,7 @@ test "echo content server" {
204208 // request.head.target,
205209 //});
206210
207 const body = try (try request.readerExpectContinue(&.{})).allocRemaining(std.testing.allocator, .limited(8192));
211 const body = try (try request.readerExpectContinue(&.{})).allocRemaining(std.testing.allocator, .unlimited);
208212 defer std.testing.allocator.free(body);
209213
210214 try expect(mem.startsWith(u8, request.head.target, "/echo-content"));
......@@ -273,7 +277,6 @@ test "Server.Request.respondStreaming non-chunked, unknown content-length" {
273277 for (0..500) |i| {
274278 try w.print("{d}, ah ha ha!\n", .{i});
275279 }
276 try expectEqual(7390, w.count);
277280 try w.flush();
278281 try response.end();
279282 try expectEqual(.closing, server.reader.state);
......@@ -291,7 +294,7 @@ test "Server.Request.respondStreaming non-chunked, unknown content-length" {
291294
292295 var tiny_buffer: [1]u8 = undefined; // allows allocRemaining to detect limit exceeded
293296 var stream_reader = stream.reader(&tiny_buffer);
294 const response = try stream_reader.interface().allocRemaining(gpa, .limited(8192));
297 const response = try stream_reader.interface().allocRemaining(gpa, .unlimited);
295298 defer gpa.free(response);
296299
297300 var expected_response = std.ArrayList(u8).init(gpa);
......@@ -362,7 +365,7 @@ test "receiving arbitrary http headers from the client" {
362365
363366 var tiny_buffer: [1]u8 = undefined; // allows allocRemaining to detect limit exceeded
364367 var stream_reader = stream.reader(&tiny_buffer);
365 const response = try stream_reader.interface().allocRemaining(gpa, .limited(8192));
368 const response = try stream_reader.interface().allocRemaining(gpa, .unlimited);
366369 defer gpa.free(response);
367370
368371 var expected_response = std.ArrayList(u8).init(gpa);
......@@ -408,12 +411,10 @@ test "general client/server API coverage" {
408411 fn handleRequest(request: *http.Server.Request, listen_port: u16) !void {
409412 const log = std.log.scoped(.server);
410413
411 log.info("{f} {s} {s}", .{
412 request.head.method, @tagName(request.head.version), request.head.target,
413 });
414 log.info("{f} {t} {s}", .{ request.head.method, request.head.version, request.head.target });
414415
415416 const gpa = std.testing.allocator;
416 const body = try (try request.readerExpectContinue(&.{})).allocRemaining(gpa, .limited(8192));
417 const body = try (try request.readerExpectContinue(&.{})).allocRemaining(gpa, .unlimited);
417418 defer gpa.free(body);
418419
419420 if (mem.startsWith(u8, request.head.target, "/get")) {
......@@ -447,7 +448,8 @@ test "general client/server API coverage" {
447448 try w.writeAll("Hello, World!\n");
448449 }
449450
450 try w.writeAll("Hello, World!\n" ** 1024);
451 var vec: [1][]const u8 = .{"Hello, World!\n"};
452 try w.writeSplatAll(&vec, 1024);
451453
452454 i = 0;
453455 while (i < 5) : (i += 1) {
......@@ -556,7 +558,7 @@ test "general client/server API coverage" {
556558 try req.sendBodiless();
557559 var response = try req.receiveHead(&redirect_buffer);
558560
559 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
561 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
560562 defer gpa.free(body);
561563
562564 try expectEqualStrings("Hello, World!\n", body);
......@@ -579,7 +581,7 @@ test "general client/server API coverage" {
579581 try req.sendBodiless();
580582 var response = try req.receiveHead(&redirect_buffer);
581583
582 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192 * 1024));
584 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
583585 defer gpa.free(body);
584586
585587 try expectEqual(@as(usize, 14 * 1024 + 14 * 10), body.len);
......@@ -601,7 +603,7 @@ test "general client/server API coverage" {
601603 try req.sendBodiless();
602604 var response = try req.receiveHead(&redirect_buffer);
603605
604 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
606 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
605607 defer gpa.free(body);
606608
607609 try expectEqualStrings("", body);
......@@ -625,7 +627,7 @@ test "general client/server API coverage" {
625627 try req.sendBodiless();
626628 var response = try req.receiveHead(&redirect_buffer);
627629
628 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
630 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
629631 defer gpa.free(body);
630632
631633 try expectEqualStrings("Hello, World!\n", body);
......@@ -648,7 +650,7 @@ test "general client/server API coverage" {
648650 try req.sendBodiless();
649651 var response = try req.receiveHead(&redirect_buffer);
650652
651 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
653 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
652654 defer gpa.free(body);
653655
654656 try expectEqualStrings("", body);
......@@ -674,7 +676,7 @@ test "general client/server API coverage" {
674676 try req.sendBodiless();
675677 var response = try req.receiveHead(&redirect_buffer);
676678
677 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
679 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
678680 defer gpa.free(body);
679681
680682 try expectEqualStrings("Hello, World!\n", body);
......@@ -703,7 +705,7 @@ test "general client/server API coverage" {
703705
704706 try std.testing.expectEqual(.ok, response.head.status);
705707
706 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
708 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
707709 defer gpa.free(body);
708710
709711 try expectEqualStrings("", body);
......@@ -740,7 +742,7 @@ test "general client/server API coverage" {
740742 try req.sendBodiless();
741743 var response = try req.receiveHead(&redirect_buffer);
742744
743 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
745 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
744746 defer gpa.free(body);
745747
746748 try expectEqualStrings("Hello, World!\n", body);
......@@ -762,7 +764,7 @@ test "general client/server API coverage" {
762764 try req.sendBodiless();
763765 var response = try req.receiveHead(&redirect_buffer);
764766
765 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
767 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
766768 defer gpa.free(body);
767769
768770 try expectEqualStrings("Hello, World!\n", body);
......@@ -784,7 +786,7 @@ test "general client/server API coverage" {
784786 try req.sendBodiless();
785787 var response = try req.receiveHead(&redirect_buffer);
786788
787 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
789 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
788790 defer gpa.free(body);
789791
790792 try expectEqualStrings("Hello, World!\n", body);
......@@ -825,7 +827,7 @@ test "general client/server API coverage" {
825827 try req.sendBodiless();
826828 var response = try req.receiveHead(&redirect_buffer);
827829
828 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
830 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
829831 defer gpa.free(body);
830832
831833 try expectEqualStrings("Encoded redirect successful!\n", body);
......@@ -915,7 +917,7 @@ test "Server streams both reading and writing" {
915917 try body_writer.writer.writeAll("fish");
916918 try body_writer.end();
917919
918 const body = try response.reader(&.{}).allocRemaining(std.testing.allocator, .limited(8192));
920 const body = try response.reader(&.{}).allocRemaining(std.testing.allocator, .unlimited);
919921 defer std.testing.allocator.free(body);
920922
921923 try expectEqualStrings("ONE FISH", body);
......@@ -947,7 +949,7 @@ fn echoTests(client: *http.Client, port: u16) !void {
947949
948950 var response = try req.receiveHead(&redirect_buffer);
949951
950 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
952 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
951953 defer gpa.free(body);
952954
953955 try expectEqualStrings("Hello, World!\n", body);
......@@ -980,7 +982,7 @@ fn echoTests(client: *http.Client, port: u16) !void {
980982
981983 var response = try req.receiveHead(&redirect_buffer);
982984
983 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
985 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
984986 defer gpa.free(body);
985987
986988 try expectEqualStrings("Hello, World!\n", body);
......@@ -1034,7 +1036,7 @@ fn echoTests(client: *http.Client, port: u16) !void {
10341036 var response = try req.receiveHead(&redirect_buffer);
10351037 try expectEqual(.ok, response.head.status);
10361038
1037 const body = try response.reader(&.{}).allocRemaining(gpa, .limited(8192));
1039 const body = try response.reader(&.{}).allocRemaining(gpa, .unlimited);
10381040 defer gpa.free(body);
10391041
10401042 try expectEqualStrings("Hello, World!\n", body);
......@@ -1175,7 +1177,7 @@ test "redirect to different connection" {
11751177 var response = try req.receiveHead(&redirect_buffer);
11761178 var reader = response.reader(&.{});
11771179
1178 const body = try reader.allocRemaining(gpa, .limited(8192));
1180 const body = try reader.allocRemaining(gpa, .unlimited);
11791181 defer gpa.free(body);
11801182
11811183 try expectEqualStrings("good job, you pass", body);