| ... | ... | @@ -1,5 +1,7 @@ |
| 1 | 1 | //! TODO: send connection: keep-alive and LRU cache a configurable number of |
| 2 | 2 | //! open connections to skip DNS and TLS handshake for subsequent requests. |
| 3 | //! |
| 4 | //! This API is *not* thread safe. |
| 3 | 5 | |
| 4 | 6 | const std = @import("../std.zig"); |
| 5 | 7 | const mem = std.mem; |
| ... | ... | @@ -15,6 +17,9 @@ const testing = std.testing; |
| 15 | 17 | /// managed buffer is not provided. |
| 16 | 18 | allocator: Allocator, |
| 17 | 19 | ca_bundle: std.crypto.Certificate.Bundle = .{}, |
| 20 | /// When this is `true`, the next time this client performs an HTTPS request, |
| 21 | /// it will first rescan the system for root certificates. |
| 22 | next_https_rescan_certs: bool = true, |
| 18 | 23 | |
| 19 | 24 | pub const Connection = struct { |
| 20 | 25 | stream: net.Stream, |
| ... | ... | @@ -594,6 +599,7 @@ pub const Request = struct { |
| 594 | 599 | CertificateTimeInvalid, |
| 595 | 600 | CertificateHasUnrecognizedObjectId, |
| 596 | 601 | CertificateHasInvalidBitString, |
| 602 | CertificateAuthorityBundleTooBig, |
| 597 | 603 | |
| 598 | 604 | // TODO: convert to higher level errors |
| 599 | 605 | InvalidFormat, |
| ... | ... | @@ -648,6 +654,10 @@ pub const Request = struct { |
| 648 | 654 | NetworkSubsystemFailed, |
| 649 | 655 | NotDir, |
| 650 | 656 | ReadOnlyFileSystem, |
| 657 | Unseekable, |
| 658 | MissingEndCertificateMarker, |
| 659 | InvalidPadding, |
| 660 | EndOfStream, |
| 651 | 661 | }; |
| 652 | 662 | |
| 653 | 663 | pub fn read(req: *Request, buffer: []u8) ReadError!usize { |
| ... | ... | @@ -837,10 +847,6 @@ pub fn deinit(client: *Client) void { |
| 837 | 847 | client.* = undefined; |
| 838 | 848 | } |
| 839 | 849 | |
| 840 | | pub fn rescanRootCertificates(client: *Client) !void { |
| 841 | | return client.ca_bundle.rescan(client.allocator); |
| 842 | | } |
| 843 | | |
| 844 | 850 | pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connection.Protocol) !Connection { |
| 845 | 851 | var conn: Connection = .{ |
| 846 | 852 | .stream = try net.tcpConnectToHost(client.allocator, host, port), |
| ... | ... | @@ -876,6 +882,11 @@ pub fn request(client: *Client, uri: Uri, headers: Request.Headers, options: Req |
| 876 | 882 | |
| 877 | 883 | const host = uri.host orelse return error.UriMissingHost; |
| 878 | 884 | |
| 885 | if (client.next_https_rescan_certs and protocol == .tls) { |
| 886 | try client.ca_bundle.rescan(client.allocator); |
| 887 | client.next_https_rescan_certs = false; |
| 888 | } |
| 889 | |
| 879 | 890 | var req: Request = .{ |
| 880 | 891 | .client = client, |
| 881 | 892 | .headers = headers, |