authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-06 18:05:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-06 18:05:37-07:00
log646a911c19d6f42949acafbbbf7621d48e967c38
tree197a36b17514984ec6683d81e7178cc2e1a0f2a0
parente7c109329d2ed05f6a2ca14066d6b19c2ee76500

std.http.Client: update from old std.Url to new std.Uri


1 files changed, 12 insertions(+), 9 deletions(-)

lib/std/http/Client.zig+12-9
...@@ -7,7 +7,7 @@ const assert = std.debug.assert;...@@ -7,7 +7,7 @@ const assert = std.debug.assert;
7const http = std.http;7const http = std.http;
8const net = std.net;8const net = std.net;
9const Client = @This();9const Client = @This();
10const Url = std.Url;10const Uri = std.Uri;
11const Allocator = std.mem.Allocator;11const Allocator = std.mem.Allocator;
12const testing = std.testing;12const testing = std.testing;
1313
...@@ -569,7 +569,7 @@ pub const Request = struct {...@@ -569,7 +569,7 @@ pub const Request = struct {
569 if (req.redirects_left == 0) return error.TooManyHttpRedirects;569 if (req.redirects_left == 0) return error.TooManyHttpRedirects;
570 const location = req.response.headers.location orelse570 const location = req.response.headers.location orelse
571 return error.HttpRedirectMissingLocation;571 return error.HttpRedirectMissingLocation;
572 const new_url = try std.Url.parse(location);572 const new_url = try std.Uri.parse(location);
573 const new_req = try req.client.request(new_url, req.headers, .{573 const new_req = try req.client.request(new_url, req.headers, .{
574 .max_redirects = req.redirects_left - 1,574 .max_redirects = req.redirects_left - 1,
575 .header_strategy = if (req.response.header_bytes_owned) .{575 .header_strategy = if (req.response.header_bytes_owned) .{
...@@ -734,23 +734,26 @@ pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connectio...@@ -734,23 +734,26 @@ pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connectio
734 return conn;734 return conn;
735}735}
736736
737pub fn request(client: *Client, url: Url, headers: Request.Headers, options: Request.Options) !Request {737pub fn request(client: *Client, uri: Uri, headers: Request.Headers, options: Request.Options) !Request {
738 const protocol: Connection.Protocol = if (mem.eql(u8, url.scheme, "http"))738 const scheme = uri.scheme orelse return error.UnsupportedUrlScheme;
739 const protocol: Connection.Protocol = if (mem.eql(u8, scheme, "http"))
739 .plain740 .plain
740 else if (mem.eql(u8, url.scheme, "https"))741 else if (mem.eql(u8, scheme, "https"))
741 .tls742 .tls
742 else743 else
743 return error.UnsupportedUrlScheme;744 return error.UnsupportedUrlScheme;
744745
745 const port: u16 = url.port orelse switch (protocol) {746 const port: u16 = uri.port orelse switch (protocol) {
746 .plain => 80,747 .plain => 80,
747 .tls => 443,748 .tls => 443,
748 };749 };
749750
751 const host = uri.host orelse return error.UriMissingHost;
752
750 var req: Request = .{753 var req: Request = .{
751 .client = client,754 .client = client,
752 .headers = headers,755 .headers = headers,
753 .connection = try client.connect(url.host, port, protocol),756 .connection = try client.connect(host, port, protocol),
754 .redirects_left = options.max_redirects,757 .redirects_left = options.max_redirects,
755 .response = switch (options.header_strategy) {758 .response = switch (options.header_strategy) {
756 .dynamic => |max| Request.Response.initDynamic(max),759 .dynamic => |max| Request.Response.initDynamic(max),
...@@ -762,11 +765,11 @@ pub fn request(client: *Client, url: Url, headers: Request.Headers, options: Req...@@ -762,11 +765,11 @@ pub fn request(client: *Client, url: Url, headers: Request.Headers, options: Req
762 var h = try std.BoundedArray(u8, 1000).init(0);765 var h = try std.BoundedArray(u8, 1000).init(0);
763 try h.appendSlice(@tagName(headers.method));766 try h.appendSlice(@tagName(headers.method));
764 try h.appendSlice(" ");767 try h.appendSlice(" ");
765 try h.appendSlice(url.path);768 try h.appendSlice(uri.path);
766 try h.appendSlice(" ");769 try h.appendSlice(" ");
767 try h.appendSlice(@tagName(headers.version));770 try h.appendSlice(@tagName(headers.version));
768 try h.appendSlice("\r\nHost: ");771 try h.appendSlice("\r\nHost: ");
769 try h.appendSlice(url.host);772 try h.appendSlice(host);
770 try h.appendSlice("\r\nConnection: close\r\n\r\n");773 try h.appendSlice("\r\nConnection: close\r\n\r\n");
771774
772 const header_bytes = h.slice();775 const header_bytes = h.slice();