Commit graph

172 commits

Author SHA1 Message Date
Andrew Kelley
646a911c19 std.http.Client: update from old std.Url to new std.Uri 2023-01-06 18:05:37 -07:00
Andrew Kelley
3806091a10 std.http.Client: fix handling of \r\n before next chunk size 2023-01-06 17:53:06 -07:00
Andrew Kelley
a7a933d7ee std.http.Client: support transfer-encoding: chunked
closes #14204

In order to add tests for this I need to implement an HTTP server in the
standard library (#910) so that's probably the next thing I'll do.
2023-01-05 19:57:00 -07:00
Andrew Kelley
3055ab7f86 std.http.Client: fail header parsing under more conditions
* when HTTP header continuations are used
 * when content-type or location header occurs more than once
2023-01-05 13:39:17 -07:00
Andrew Kelley
ba1e53f116 avoid triggering LLVM bug on MIPS
See #13782
2023-01-05 00:03:59 -07:00
Andrew Kelley
8248fdbbdb std.http.Client: support HTTP redirects
* std.http.Status.Class: add a "nonstandard" enum tag. Instead of
   having `class` return an optional value, it can potentially return
   nonstandard.
 * extract out std.http.Client.Connection from std.http.Client.Request
   - this code abstracts over plain/TLS only
   - this is the type that will potentially be stored in a client's LRU
     connection map
 * introduce two-staged HTTP header parsing
   - API users can rely on a heap-allocated buffer with a maximum limit,
     which defaults to 16 KB, or they can provide a static buffer that
     is borrowed by the Request instance.
   - The entire HTTP header is buffered because there are strings in
     there and they must be accessed later, such as with the case of
     HTTP redirects.
   - When buffering the HTTP header, the parser only looks for the
     \r\n\r\n pattern. Further validation is done later.
   - After the full HTTP header is buffered, it is parsed into
     components such as Content-Length and Location.
 * HTTP redirects are handled, with a maximum redirect count option that
   defaults to 3.
   - Connection: close is always used for now; implementing keep-alive
     connections and an LRU connection pool in std.http.Client is a task
     for another day.

see #2007
2023-01-04 18:37:53 -07:00
Andrew Kelley
5d9429579d std.http.Headers.Parser: parse version and status 2023-01-04 18:37:53 -07:00
Andrew Kelley
2cdc0a8b50 std.http.Client: do not heap allocate for requests 2023-01-04 18:37:53 -07:00
Andrew Kelley
7178451d62 std.crypto.tls.Client: make close_notify optional
Although RFC 8446 states:

> Each party MUST send a "close_notify" alert before closing its write
> side of the connection

In practice many servers do not do this. Also in practice, truncation
attacks are thwarted at the application layer by comparing the amount of
bytes received with the amount expected via the HTTP headers.
2023-01-02 18:27:38 -07:00
Andrew Kelley
3127bd79fb std.http.Client: don't send TLS close_notify
It appears to be implemented incorrectly in the wild and causes the read
connection to be closed even though that is a direct violation of RFC
8446 Section 6.1.

The writeEnd function variants are still there, ready to be used.
2023-01-02 16:57:16 -07:00
Andrew Kelley
611a1fdd6d std.crypto.tls: add API for sending close_notify
This commit adds `writeEnd` and `writeAllEnd` in order to send data and
also notify the server that there will be no more data written.

Unfortunately, it seems most TLS implementations in the wild get this
wrong and immediately close the socket when they see a close_notify,
rather than only ending the data stream on the application layer.
2023-01-02 16:57:16 -07:00
Andrew Kelley
2d090f61be add std.http.Headers
This is a streaming HTTP header parser. All it currently does is detect
the end of headers. This will be a non-allocating parser where one can
bring supply their own buffer if they want to handle custom headers.

This commit also improves std.http.Client to not return the HTTP headers
with the read functions.
2023-01-02 16:57:16 -07:00
Andrew Kelley
940d368e7e std.crypto.tls.Client: fix the read function
The read function has been renamed to readAdvanced since it has slightly
different semantics than typical read functions, specifically regarding
the end-of-file. A higher level read function is implemented on top.

Now, API users may pass small buffers to the read function and
everything will work fine. This is done by re-decrypting the same
ciphertext record with each call to read() until the record is finished
being transmitted.

If the buffer supplied to read() is large enough, then any given
ciphertext record will only be decrypted once, since it decrypts
directly to the read() buffer and therefore does not need any memcpy. On
the other hand, if the buffer supplied to read() is small, then the
ciphertext is decrypted into a stack buffer, a subset is copied to the
read() buffer, and then the entire ciphertext record is saved for the
next call to read().
2023-01-02 16:57:16 -07:00
Andrew Kelley
477864dca5 std.crypto.tls.Client: fix truncation attack vulnerability 2023-01-02 16:57:16 -07:00
Andrew Kelley
5b8b5f2505 add url parsing to the std lib 2023-01-02 16:57:15 -07:00
Andrew Kelley
29475b4518 std.crypto.tls: validate previous certificate 2023-01-02 16:57:15 -07:00
Andrew Kelley
bbc074252c introduce std.crypto.CertificateBundle
for reading root certificate authority bundles from standard
installation locations on the file system. So far only Linux logic is
added.
2023-01-02 16:57:15 -07:00
Andrew Kelley
93ab8be8d8 extract std.crypto.tls.Client into separate namespace 2023-01-02 16:57:15 -07:00
Andrew Kelley
b97fc43baa std.crypto.Tls: client is working against some servers 2023-01-02 16:57:15 -07:00
Andrew Kelley
40a85506b2 std.crypto.Tls: add read/write methods 2023-01-02 16:57:15 -07:00
Andrew Kelley
d2f5d0b199 std.crypto.Tls: parse the ServerHello handshake 2023-01-02 16:57:15 -07:00
Andrew Kelley
ba44513c2f std.http reorg; introduce std.crypto.Tls
TLS is capable of sending a Client Hello
2023-01-02 16:57:15 -07:00