authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 22:33:02-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-11 15:39:48-08:00
logc50f65304f1fd9819fc1e7c0144d5329b6863b9c
tree47ffa1ccd72ef33d72e8fc280dd411683f06f988
parentda6d79c47c2d655ae30bed8bcef48134de8d157a

std.http.Client: support the Reader interface


1 files changed, 106 insertions(+), 3 deletions(-)

lib/std/http/Client.zig+106-3
......@@ -524,11 +524,110 @@ pub const Request = struct {
524524 req.* = undefined;
525525 }
526526
527 pub const Reader = std.io.Reader(*Request, ReadError, read);
528
529 pub fn reader(req: *Request) Reader {
530 return .{ .context = req };
531 }
532
527533 pub fn readAll(req: *Request, buffer: []u8) !usize {
528534 return readAtLeast(req, buffer, buffer.len);
529535 }
530536
531 pub fn read(req: *Request, buffer: []u8) !usize {
537 pub const ReadError = net.Stream.ReadError || error{
538 // From HTTP protocol
539 HttpHeadersInvalid,
540 HttpHeadersExceededSizeLimit,
541 HttpRedirectMissingLocation,
542 HttpTransferEncodingUnsupported,
543 HttpContentLengthUnknown,
544 TooManyHttpRedirects,
545 ShortHttpStatusLine,
546 BadHttpVersion,
547 HttpHeaderContinuationsUnsupported,
548 UnsupportedUrlScheme,
549 UriMissingHost,
550 UnknownHostName,
551
552 // Network problems
553 NetworkUnreachable,
554 HostLacksNetworkAddresses,
555 TemporaryNameServerFailure,
556 NameServerFailure,
557 ProtocolFamilyNotAvailable,
558 ProtocolNotSupported,
559
560 // System resource problems
561 ProcessFdQuotaExceeded,
562 SystemFdQuotaExceeded,
563 OutOfMemory,
564
565 // TLS problems
566 InsufficientEntropy,
567 TlsConnectionTruncated,
568 TlsRecordOverflow,
569 TlsDecodeError,
570 TlsAlert,
571 TlsBadRecordMac,
572 TlsBadLength,
573 TlsIllegalParameter,
574 TlsUnexpectedMessage,
575 TlsDecryptFailure,
576 CertificateFieldHasInvalidLength,
577 CertificateHostMismatch,
578 CertificatePublicKeyInvalid,
579 CertificateExpired,
580 CertificateFieldHasWrongDataType,
581 CertificateIssuerMismatch,
582 CertificateNotYetValid,
583 CertificateSignatureAlgorithmMismatch,
584 CertificateSignatureAlgorithmUnsupported,
585 CertificateSignatureInvalid,
586 CertificateSignatureInvalidLength,
587 CertificateSignatureNamedCurveUnsupported,
588 CertificateSignatureUnsupportedBitCount,
589 TlsCertificateNotVerified,
590 TlsBadSignatureScheme,
591 TlsBadRsaSignatureBitCount,
592 TlsDecryptError,
593 UnsupportedCertificateVersion,
594 CertificateTimeInvalid,
595 CertificateHasUnrecognizedObjectId,
596 CertificateHasInvalidBitString,
597
598 // TODO: convert to higher level errors
599 InvalidFormat,
600 InvalidPort,
601 UnexpectedCharacter,
602 Overflow,
603 InvalidCharacter,
604 AddressFamilyNotSupported,
605 AddressInUse,
606 AddressNotAvailable,
607 ConnectionPending,
608 ConnectionRefused,
609 FileNotFound,
610 PermissionDenied,
611 ServiceUnavailable,
612 SocketTypeNotSupported,
613 FileTooBig,
614 LockViolation,
615 NoSpaceLeft,
616 NotOpenForWriting,
617 InvalidEncoding,
618 IdentityElement,
619 NonCanonical,
620 SignatureVerificationFailed,
621 MessageTooLong,
622 NegativeIntoUnsigned,
623 TargetTooSmall,
624 BufferTooSmall,
625 InvalidSignature,
626 NotSquare,
627 DiskQuota,
628 };
629
630 pub fn read(req: *Request, buffer: []u8) ReadError!usize {
532631 return readAtLeast(req, buffer, 1);
533632 }
534633
......@@ -709,11 +808,15 @@ pub const Request = struct {
709808 }
710809};
711810
712pub fn deinit(client: *Client, gpa: Allocator) void {
713 client.ca_bundle.deinit(gpa);
811pub fn deinit(client: *Client) void {
812 client.ca_bundle.deinit(client.allocator);
714813 client.* = undefined;
715814}
716815
816pub fn rescanRootCertificates(client: *Client) !void {
817 return client.ca_bundle.rescan(client.allocator);
818}
819
717820pub fn connect(client: *Client, host: []const u8, port: u16, protocol: Connection.Protocol) !Connection {
718821 var conn: Connection = .{
719822 .stream = try net.tcpConnectToHost(client.allocator, host, port),