authorgravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-10-07 20:27:39-05:00
committergravatar for truemedian@gmail.comNameless <truemedian@gmail.com> 2023-10-21 20:52:59-05:00
log363d0ee5e13f4ac3a93d246121edfd00ef9fd97b
treecf0f1561e448ac231b15c21feabace4a41489582
parent544ed34d99f59a1b487341eaaa9610be44629924
signaturelock-open Commit is signed but in an unrecognized format.

std.http: rename start->send and request->open to be more inline with operation


5 files changed, 72 insertions(+), 72 deletions(-)

lib/std/http/Client.zig+16-16
......@@ -598,15 +598,15 @@ pub const Request = struct {
598598 };
599599 }
600600
601 pub const StartError = Connection.WriteError || error{ InvalidContentLength, UnsupportedTransferEncoding };
601 pub const SendError = Connection.WriteError || error{ InvalidContentLength, UnsupportedTransferEncoding };
602602
603 pub const StartOptions = struct {
604 /// Specifies that the uri should be used as is
603 pub const SendOptions = struct {
604 /// Specifies that the uri should be used as is. You guarantee that the uri is already escaped.
605605 raw_uri: bool = false,
606606 };
607607
608608 /// Send the HTTP request headers to the server.
609 pub fn start(req: *Request, options: StartOptions) StartError!void {
609 pub fn send(req: *Request, options: SendOptions) SendError!void {
610610 if (!req.method.requestHasBody() and req.transfer_encoding != .none) return error.UnsupportedTransferEncoding;
611611
612612 const w = req.connection.?.writer();
......@@ -733,14 +733,14 @@ pub const Request = struct {
733733 return index;
734734 }
735735
736 pub const WaitError = RequestError || StartError || TransferReadError || proto.HeadersParser.CheckCompleteHeadError || Response.ParseError || Uri.ParseError || error{ TooManyHttpRedirects, RedirectRequiresResend, HttpRedirectMissingLocation, CompressionInitializationFailed, CompressionNotSupported };
736 pub const WaitError = RequestError || SendError || TransferReadError || proto.HeadersParser.CheckCompleteHeadError || Response.ParseError || Uri.ParseError || error{ TooManyHttpRedirects, RedirectRequiresResend, HttpRedirectMissingLocation, CompressionInitializationFailed, CompressionNotSupported };
737737
738738 /// Waits for a response from the server and parses any headers that are sent.
739739 /// This function will block until the final response is received.
740740 ///
741741 /// If `handle_redirects` is true and the request has no payload, then this function will automatically follow
742742 /// redirects. If a request payload is present, then this function will error with error.RedirectRequiresResend.
743 ///
743 ///
744744 /// Must be called after `start` and, if any data was written to the request body, then also after `finish`.
745745 pub fn wait(req: *Request) WaitError!void {
746746 while (true) { // handle redirects
......@@ -845,7 +845,7 @@ pub const Request = struct {
845845
846846 try req.redirect(resolved_url);
847847
848 try req.start(.{});
848 try req.send(.{});
849849 } else {
850850 req.response.skip = false;
851851 if (!req.response.parser.done) {
......@@ -1223,7 +1223,7 @@ pub fn connectTunnel(
12231223 // we can use a small buffer here because a CONNECT response should be very small
12241224 var buffer: [8096]u8 = undefined;
12251225
1226 var req = client.request(.CONNECT, uri, proxy.headers, .{
1226 var req = client.open(.CONNECT, uri, proxy.headers, .{
12271227 .handle_redirects = false,
12281228 .connection = conn,
12291229 .header_strategy = .{ .static = buffer[0..] },
......@@ -1233,7 +1233,7 @@ pub fn connectTunnel(
12331233 };
12341234 defer req.deinit();
12351235
1236 req.start(.{ .raw_uri = true }) catch |err| break :tunnel err;
1236 req.send(.{ .raw_uri = true }) catch |err| break :tunnel err;
12371237 req.wait() catch |err| break :tunnel err;
12381238
12391239 if (req.response.status.class() == .server_error) {
......@@ -1266,9 +1266,9 @@ const ConnectErrorPartial = ConnectTcpError || error{ UnsupportedUrlScheme, Conn
12661266pub const ConnectError = ConnectErrorPartial || RequestError;
12671267
12681268/// Connect to `host:port` using the specified protocol. This will reuse a connection if one is already open.
1269///
1269///
12701270/// If a proxy is configured for the client, then the proxy will be used to connect to the host.
1271///
1271///
12721272/// This function is threadsafe.
12731273pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connection.Protocol) ConnectError!*Connection {
12741274 // pointer required so that `supports_connect` can be updated if a CONNECT fails
......@@ -1304,7 +1304,7 @@ pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connectio
13041304 return client.connectTcp(host, port, protocol);
13051305}
13061306
1307pub const RequestError = ConnectTcpError || ConnectErrorPartial || Request.StartError || std.fmt.ParseIntError || Connection.WriteError || error{
1307pub const RequestError = ConnectTcpError || ConnectErrorPartial || Request.SendError || std.fmt.ParseIntError || Connection.WriteError || error{
13081308 UnsupportedUrlScheme,
13091309 UriMissingHost,
13101310
......@@ -1350,7 +1350,7 @@ pub const protocol_map = std.ComptimeStringMap(Connection.Protocol, .{
13501350///
13511351/// The caller is responsible for calling `deinit()` on the `Request`.
13521352/// This function is threadsafe.
1353pub fn request(client: *Client, method: http.Method, uri: Uri, headers: http.Headers, options: RequestOptions) RequestError!Request {
1353pub fn open(client: *Client, method: http.Method, uri: Uri, headers: http.Headers, options: RequestOptions) RequestError!Request {
13541354 const protocol = protocol_map.get(uri.scheme) orelse return error.UnsupportedUrlScheme;
13551355
13561356 const port: u16 = uri.port orelse switch (protocol) {
......@@ -1446,7 +1446,7 @@ pub const FetchResult = struct {
14461446};
14471447
14481448/// Perform a one-shot HTTP request with the provided options.
1449///
1449///
14501450/// This function is threadsafe.
14511451pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !FetchResult {
14521452 const has_transfer_encoding = options.headers.contains("transfer-encoding");
......@@ -1459,7 +1459,7 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc
14591459 .uri => |u| u,
14601460 };
14611461
1462 var req = try request(client, options.method, uri, options.headers, .{
1462 var req = try open(client, options.method, uri, options.headers, .{
14631463 .header_strategy = options.header_strategy,
14641464 .handle_redirects = options.payload == .none,
14651465 });
......@@ -1476,7 +1476,7 @@ pub fn fetch(client: *Client, allocator: Allocator, options: FetchOptions) !Fetc
14761476 .none => {},
14771477 }
14781478
1479 try req.start(.{ .raw_uri = options.raw_uri });
1479 try req.send(.{ .raw_uri = options.raw_uri });
14801480
14811481 switch (options.payload) {
14821482 .string => |str| try req.writeAll(str),
lib/std/http/Server.zig+3-3
......@@ -392,10 +392,10 @@ pub const Response = struct {
392392 }
393393 }
394394
395 pub const StartError = Connection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };
395 pub const SendError = Connection.WriteError || error{ UnsupportedTransferEncoding, InvalidContentLength };
396396
397397 /// Send the HTTP response headers to the client.
398 pub fn start(res: *Response) StartError!void {
398 pub fn send(res: *Response) SendError!void {
399399 switch (res.state) {
400400 .waited => res.state = .responded,
401401 .first, .start, .responded, .finished => unreachable,
......@@ -771,7 +771,7 @@ test "HTTP server handles a chunked transfer coding request" {
771771 res.transfer_encoding = .{ .content_length = server_body.len };
772772 try res.headers.append("content-type", "text/plain");
773773 try res.headers.append("connection", "close");
774 try res.do();
774 try res.send();
775775
776776 var buf: [128]u8 = undefined;
777777 const n = try res.readAll(&buf);
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
test/standalone/http.zig+45-45
......@@ -29,11 +29,11 @@ fn handleRequest(res: *Server.Response) !void {
2929 if (res.request.headers.contains("expect")) {
3030 if (mem.eql(u8, res.request.headers.getFirstValue("expect").?, "100-continue")) {
3131 res.status = .@"continue";
32 try res.start();
32 try res.send();
3333 res.status = .ok;
3434 } else {
3535 res.status = .expectation_failed;
36 try res.start();
36 try res.send();
3737 return;
3838 }
3939 }
......@@ -54,7 +54,7 @@ fn handleRequest(res: *Server.Response) !void {
5454
5555 try res.headers.append("content-type", "text/plain");
5656
57 try res.start();
57 try res.send();
5858 if (res.request.method != .HEAD) {
5959 try res.writeAll("Hello, ");
6060 try res.writeAll("World!\n");
......@@ -65,7 +65,7 @@ fn handleRequest(res: *Server.Response) !void {
6565 } else if (mem.startsWith(u8, res.request.target, "/large")) {
6666 res.transfer_encoding = .{ .content_length = 14 * 1024 + 14 * 10 };
6767
68 try res.start();
68 try res.send();
6969
7070 var i: u32 = 0;
7171 while (i < 5) : (i += 1) {
......@@ -92,14 +92,14 @@ fn handleRequest(res: *Server.Response) !void {
9292 try testing.expectEqualStrings("14", res.request.headers.getFirstValue("content-length").?);
9393 }
9494
95 try res.start();
95 try res.send();
9696 try res.writeAll("Hello, ");
9797 try res.writeAll("World!\n");
9898 try res.finish();
9999 } else if (mem.eql(u8, res.request.target, "/trailer")) {
100100 res.transfer_encoding = .chunked;
101101
102 try res.start();
102 try res.send();
103103 try res.writeAll("Hello, ");
104104 try res.writeAll("World!\n");
105105 // try res.finish();
......@@ -110,7 +110,7 @@ fn handleRequest(res: *Server.Response) !void {
110110 res.status = .found;
111111 try res.headers.append("location", "../../get");
112112
113 try res.start();
113 try res.send();
114114 try res.writeAll("Hello, ");
115115 try res.writeAll("Redirected!\n");
116116 try res.finish();
......@@ -120,7 +120,7 @@ fn handleRequest(res: *Server.Response) !void {
120120 res.status = .found;
121121 try res.headers.append("location", "/redirect/1");
122122
123 try res.start();
123 try res.send();
124124 try res.writeAll("Hello, ");
125125 try res.writeAll("Redirected!\n");
126126 try res.finish();
......@@ -133,7 +133,7 @@ fn handleRequest(res: *Server.Response) !void {
133133 res.status = .found;
134134 try res.headers.append("location", location);
135135
136 try res.start();
136 try res.send();
137137 try res.writeAll("Hello, ");
138138 try res.writeAll("Redirected!\n");
139139 try res.finish();
......@@ -143,7 +143,7 @@ fn handleRequest(res: *Server.Response) !void {
143143 res.status = .found;
144144 try res.headers.append("location", "/redirect/3");
145145
146 try res.start();
146 try res.send();
147147 try res.writeAll("Hello, ");
148148 try res.writeAll("Redirected!\n");
149149 try res.finish();
......@@ -154,11 +154,11 @@ fn handleRequest(res: *Server.Response) !void {
154154
155155 res.status = .found;
156156 try res.headers.append("location", location);
157 try res.start();
157 try res.send();
158158 try res.finish();
159159 } else {
160160 res.status = .not_found;
161 try res.start();
161 try res.send();
162162 }
163163}
164164
......@@ -244,10 +244,10 @@ pub fn main() !void {
244244 const uri = try std.Uri.parse(location);
245245
246246 log.info("{s}", .{location});
247 var req = try client.request(.GET, uri, h, .{});
247 var req = try client.open(.GET, uri, h, .{});
248248 defer req.deinit();
249249
250 try req.start(.{});
250 try req.send(.{});
251251 try req.wait();
252252
253253 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -269,10 +269,10 @@ pub fn main() !void {
269269 const uri = try std.Uri.parse(location);
270270
271271 log.info("{s}", .{location});
272 var req = try client.request(.GET, uri, h, .{});
272 var req = try client.open(.GET, uri, h, .{});
273273 defer req.deinit();
274274
275 try req.start(.{});
275 try req.send(.{});
276276 try req.wait();
277277
278278 const body = try req.reader().readAllAlloc(calloc, 8192 * 1024);
......@@ -293,10 +293,10 @@ pub fn main() !void {
293293 const uri = try std.Uri.parse(location);
294294
295295 log.info("{s}", .{location});
296 var req = try client.request(.HEAD, uri, h, .{});
296 var req = try client.open(.HEAD, uri, h, .{});
297297 defer req.deinit();
298298
299 try req.start(.{});
299 try req.send(.{});
300300 try req.wait();
301301
302302 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -319,10 +319,10 @@ pub fn main() !void {
319319 const uri = try std.Uri.parse(location);
320320
321321 log.info("{s}", .{location});
322 var req = try client.request(.GET, uri, h, .{});
322 var req = try client.open(.GET, uri, h, .{});
323323 defer req.deinit();
324324
325 try req.start(.{});
325 try req.send(.{});
326326 try req.wait();
327327
328328 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -344,10 +344,10 @@ pub fn main() !void {
344344 const uri = try std.Uri.parse(location);
345345
346346 log.info("{s}", .{location});
347 var req = try client.request(.HEAD, uri, h, .{});
347 var req = try client.open(.HEAD, uri, h, .{});
348348 defer req.deinit();
349349
350 try req.start(.{});
350 try req.send(.{});
351351 try req.wait();
352352
353353 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -370,10 +370,10 @@ pub fn main() !void {
370370 const uri = try std.Uri.parse(location);
371371
372372 log.info("{s}", .{location});
373 var req = try client.request(.GET, uri, h, .{});
373 var req = try client.open(.GET, uri, h, .{});
374374 defer req.deinit();
375375
376 try req.start(.{});
376 try req.send(.{});
377377 try req.wait();
378378
379379 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -397,12 +397,12 @@ pub fn main() !void {
397397 const uri = try std.Uri.parse(location);
398398
399399 log.info("{s}", .{location});
400 var req = try client.request(.POST, uri, h, .{});
400 var req = try client.open(.POST, uri, h, .{});
401401 defer req.deinit();
402402
403403 req.transfer_encoding = .{ .content_length = 14 };
404404
405 try req.start(.{});
405 try req.send(.{});
406406 try req.writeAll("Hello, ");
407407 try req.writeAll("World!\n");
408408 try req.finish();
......@@ -429,10 +429,10 @@ pub fn main() !void {
429429 const uri = try std.Uri.parse(location);
430430
431431 log.info("{s}", .{location});
432 var req = try client.request(.GET, uri, h, .{});
432 var req = try client.open(.GET, uri, h, .{});
433433 defer req.deinit();
434434
435 try req.start(.{});
435 try req.send(.{});
436436 try req.wait();
437437
438438 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -456,12 +456,12 @@ pub fn main() !void {
456456 const uri = try std.Uri.parse(location);
457457
458458 log.info("{s}", .{location});
459 var req = try client.request(.POST, uri, h, .{});
459 var req = try client.open(.POST, uri, h, .{});
460460 defer req.deinit();
461461
462462 req.transfer_encoding = .chunked;
463463
464 try req.start(.{});
464 try req.send(.{});
465465 try req.writeAll("Hello, ");
466466 try req.writeAll("World!\n");
467467 try req.finish();
......@@ -486,10 +486,10 @@ pub fn main() !void {
486486 const uri = try std.Uri.parse(location);
487487
488488 log.info("{s}", .{location});
489 var req = try client.request(.GET, uri, h, .{});
489 var req = try client.open(.GET, uri, h, .{});
490490 defer req.deinit();
491491
492 try req.start(.{});
492 try req.send(.{});
493493 try req.wait();
494494
495495 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -510,10 +510,10 @@ pub fn main() !void {
510510 const uri = try std.Uri.parse(location);
511511
512512 log.info("{s}", .{location});
513 var req = try client.request(.GET, uri, h, .{});
513 var req = try client.open(.GET, uri, h, .{});
514514 defer req.deinit();
515515
516 try req.start(.{});
516 try req.send(.{});
517517 try req.wait();
518518
519519 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -534,10 +534,10 @@ pub fn main() !void {
534534 const uri = try std.Uri.parse(location);
535535
536536 log.info("{s}", .{location});
537 var req = try client.request(.GET, uri, h, .{});
537 var req = try client.open(.GET, uri, h, .{});
538538 defer req.deinit();
539539
540 try req.start(.{});
540 try req.send(.{});
541541 try req.wait();
542542
543543 const body = try req.reader().readAllAlloc(calloc, 8192);
......@@ -558,10 +558,10 @@ pub fn main() !void {
558558 const uri = try std.Uri.parse(location);
559559
560560 log.info("{s}", .{location});
561 var req = try client.request(.GET, uri, h, .{});
561 var req = try client.open(.GET, uri, h, .{});
562562 defer req.deinit();
563563
564 try req.start(.{});
564 try req.send(.{});
565565 req.wait() catch |err| switch (err) {
566566 error.TooManyHttpRedirects => {},
567567 else => return err,
......@@ -580,10 +580,10 @@ pub fn main() !void {
580580 const uri = try std.Uri.parse(location);
581581
582582 log.info("{s}", .{location});
583 var req = try client.request(.GET, uri, h, .{});
583 var req = try client.open(.GET, uri, h, .{});
584584 defer req.deinit();
585585
586 try req.start(.{});
586 try req.send(.{});
587587 const result = req.wait();
588588
589589 // a proxy without an upstream is likely to return a 5xx status.
......@@ -628,12 +628,12 @@ pub fn main() !void {
628628 const uri = try std.Uri.parse(location);
629629
630630 log.info("{s}", .{location});
631 var req = try client.request(.POST, uri, h, .{});
631 var req = try client.open(.POST, uri, h, .{});
632632 defer req.deinit();
633633
634634 req.transfer_encoding = .chunked;
635635
636 try req.start(.{});
636 try req.send(.{});
637637 try req.wait();
638638 try testing.expectEqual(http.Status.@"continue", req.response.status);
639639
......@@ -662,12 +662,12 @@ pub fn main() !void {
662662 const uri = try std.Uri.parse(location);
663663
664664 log.info("{s}", .{location});
665 var req = try client.request(.POST, uri, h, .{});
665 var req = try client.open(.POST, uri, h, .{});
666666 defer req.deinit();
667667
668668 req.transfer_encoding = .chunked;
669669
670 try req.start(.{});
670 try req.send(.{});
671671 try req.wait();
672672 try testing.expectEqual(http.Status.expectation_failed, req.response.status);
673673 }
......@@ -682,7 +682,7 @@ pub fn main() !void {
682682 defer calloc.free(requests);
683683
684684 for (0..total_connections) |i| {
685 var req = try client.request(.GET, uri, .{ .allocator = calloc }, .{});
685 var req = try client.open(.GET, uri, .{ .allocator = calloc }, .{});
686686 req.response.parser.done = true;
687687 req.connection.?.closing = false;
688688 requests[i] = req;