Skip to content

Commit 546bc7f

Browse files
committed
feat: cancellation backwards
1 parent 40570ba commit 546bc7f

File tree

9 files changed

+145
-134
lines changed

9 files changed

+145
-134
lines changed

src/Std/Internal/Http/Data/Body.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ instance : ForIn ContextAsync Body Chunk where
9090
| .stream stream' => ByteStream.forIn' stream' acc step
9191

9292
/--
93-
Collect all data from the body into a single `ByteArray`. This reads the entire body content into memory,
94-
so use with caution for large bodies as it may consume significant memory.
93+
Collect all data from the body into a single `ByteArray`. This reads the entire body content into memory
94+
and consumes significant memory for large bodies.
9595
-/
9696
def collectByteArray (body : Body) : Async ByteArray := do
9797
let mut result := .empty
9898
for x in body do result := result ++ x.data
9999
return result
100100

101101
/--
102-
Collect all data from the body into a single `String`. This reads the entire body content into memory,
103-
so use with caution for large bodies as it may consume significant memory. If it is a valid UTF-8 string
104-
then it will return `some` otherwise `none`.
102+
Collect all data from the body into a single `String`. This reads the entire body content into memory
103+
and consumes significant memory for large bodies. Returns `some` if the data is valid UTF-8, otherwise
104+
`none`.
105105
-/
106106
def collectString (body : Body) : Async (Option String) := do
107107
let mut result := .empty

src/Std/Internal/Http/Data/Body/ByteStream.lean

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public section
2020
2121
A `ByteStream` represents an asynchronous channel for streaming byte data in chunks. It provides an
2222
interface for producers and consumers to exchange byte arrays with optional chunk metadata (extensions),
23-
aking it suitable for HTTP chunked transfer encoding and other streaming scenarios.
23+
making it suitable for HTTP chunked transfer encoding and other streaming scenarios.
2424
-/
2525

2626
namespace Std.Http.Body
@@ -54,8 +54,8 @@ def empty : Async ByteStream :=
5454
emptyWithCapacity
5555

5656
/--
57-
Tries to receive a chunk from the stream. Returns `some` with a chunk when data is available, or `none`
58-
hen the stream is closed or no data is available.
57+
Attempts to receive a chunk from the stream. Returns `some` with a chunk when data is available, or `none`
58+
when the stream is closed or no data is available.
5959
-/
6060
def tryRecv (stream : ByteStream) : Async (Option Chunk) := do
6161
stream.channel.tryRecv
@@ -89,17 +89,17 @@ def write (stream : ByteStream) (data : ByteArray) (extensions : Array (String
8989
if data.isEmpty then
9090
return
9191

92-
let chunk : Chunk := { data := data, extensions := extensions }
92+
let chunk := { data := data, extensions := extensions }
9393
let task ← stream.channel.send chunk
94-
let task : AsyncTask _ := task.map (Except.mapError (fun x => userError (toString x)))
94+
let task := task.map (Except.mapError (fun x => userError (toString x)))
9595
Async.ofAsyncTask task
9696

9797
/--
9898
Writes a complete chunk with extensions to the stream.
9999
-/
100100
def writeChunk (stream : ByteStream) (chunk : Chunk) : Async Unit := do
101101
let task ← stream.channel.send chunk
102-
let task : AsyncTask _ := task.map (Except.mapError (fun x => userError (toString x)))
102+
let task := task.map (Except.mapError (fun x => userError (toString x)))
103103
Async.ofAsyncTask task
104104

105105
/--
@@ -112,7 +112,7 @@ def getKnownSize (stream : ByteStream) : Async (Option Body.Length) := do
112112

113113
/--
114114
Sets the known size of the stream.
115-
This is typically used when the total expected size is known ahead of time.
115+
Use this when the total expected size is known ahead of time.
116116
-/
117117
def setKnownSize (stream : ByteStream) (size : Option Body.Length) : Async Unit := do
118118
stream.knownSize.atomically do

src/Std/Internal/Http/Data/Body/Length.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public section
1313
/-!
1414
# Length
1515
16-
This module defines the `Length` structure, that represents the Content-Length or Transfer-Encoding
16+
This module defines the `Length` type, that represents the Content-Length or Transfer-Encoding
1717
of an HTTP Request or response.
1818
-/
1919

src/Std/Internal/Http/Data/Header/Map.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ deriving Repr, Inhabited
4444
namespace Headers
4545

4646
/--
47-
Tries to retrieve the `HeaderValue` for the given key.
47+
Retrieves the `HeaderValue` for the given key.
4848
Returns `none` if the header is absent.
4949
-/
5050
@[inline]
@@ -53,15 +53,15 @@ def get (headers : Headers) (name : HeaderName) : Option HeaderValue :=
5353
|>.map (.joinCommaSep)
5454

5555
/--
56-
Tries to retrieve the `HeaderValue` for the given key.
56+
Retrieves all `HeaderValue` entries for the given key.
5757
Returns `none` if the header is absent.
5858
-/
5959
@[inline]
6060
def getAll? (headers : Headers) (name : HeaderName) : Option (Array HeaderValue) :=
6161
headers.data.get? name
6262

6363
/--
64-
Tries to check if the entry is present in the `Headers`
64+
Checks if the entry is present in the `Headers`.
6565
-/
6666
@[inline]
6767
def hasEntry (headers : Headers) (name : HeaderName) (value : String) : Bool :=
@@ -70,7 +70,7 @@ def hasEntry (headers : Headers) (name : HeaderName) (value : String) : Bool :=
7070
|>.isSome
7171

7272
/--
73-
Tries to retrieve the last header value for the given key.
73+
Retrieves the last header value for the given key.
7474
Returns `none` if the header is absent.
7575
-/
7676
@[inline]
@@ -80,7 +80,7 @@ def getLast? (headers : Headers) (name : HeaderName) : Option HeaderValue :=
8080

8181

8282
/--
83-
Like `get?`, but returns an empty HashSet if absent.
83+
Like `get?`, but returns a default value if absent.
8484
-/
8585
@[inline]
8686
def getD (headers : Headers) (name : HeaderName) (d : HeaderValue) : HeaderValue :=

src/Std/Internal/Http/Data/Header/Name.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ def append (l : HeaderValue) (r : HeaderValue) : HeaderValue :=
105105
⟨l.value ++ r.value, ?_⟩
106106
where finally
107107
unfold isValidHeaderValue
108-
rw [String.toList_append]
109-
rw [List.all_append]
110-
rw [Bool.and_eq_true]
108+
rw [String.toList_append, List.all_append, Bool.and_eq_true]
111109
constructor
112110
· exact l.validHeaderValue
113111
· exact r.validHeaderValue

src/Std/Internal/Http/Data/URI.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ namespace Std.Http.RequestTarget
2424
set_option linter.all true
2525

2626
/--
27-
Attempt to parse a `RequestTarget` from the given string.
27+
Attempts to parse a `RequestTarget` from the given string.
2828
-/
2929
@[inline]
3030
def parse? (string : String) : Option RequestTarget :=
3131
(Parser.parseRequestTarget <* Std.Internal.Parsec.eof).run string.toUTF8 |>.toOption
3232

3333

3434
/--
35-
Parse a `RequestTarget` from the given string. Panics if parsing fails. Use `parse?`
35+
Parses a `RequestTarget` from the given string. Panics if parsing fails. Use `parse?`
3636
if you need a safe option-returning version.
3737
-/
3838
@[inline]

src/Std/Internal/Http/Data/URI/Basic.lean

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ URI scheme (e.g., "http", "https", "ftp").
2020
abbrev Scheme := String
2121

2222
/--
23-
User information component that usually contains the username and password.
23+
User information component containing the username and password.
2424
-/
2525
abbrev UserInfo := String
2626

@@ -29,7 +29,7 @@ Host component of a URI, supporting domain names and IP addresses.
2929
-/
3030
inductive Host
3131
/--
32-
A registered name (typically a domain name).
32+
A registered name (e.g., a domain name).
3333
-/
3434
| name (name : String)
3535

@@ -58,7 +58,7 @@ instance : Repr Host where
5858
| Host.ipv6 a => repr "ipv6" (toString a)
5959

6060
/--
61-
TCP number port.
61+
TCP port number.
6262
-/
6363
abbrev Port := UInt16
6464

@@ -70,7 +70,7 @@ on the network.
7070
-/
7171
structure Authority where
7272
/--
73-
Optional user information like user and password.
73+
Optional user information such as username and password.
7474
-/
7575
userInfo: Option UserInfo := none
7676

@@ -162,12 +162,12 @@ inductive RequestTarget where
162162
| absoluteForm (uri : URI)
163163

164164
/--
165-
Request target using the authority-form, typically for CONNECT requests.
165+
Request target using the authority-form (used for CONNECT requests).
166166
-/
167167
| authorityForm (authority : URI.Authority)
168168

169169
/--
170-
Asterisk-form request target, typically used with OPTIONS requests.
170+
Asterisk-form request target (used with OPTIONS requests).
171171
-/
172172
| asteriskForm
173173
deriving Inhabited, Repr
@@ -223,8 +223,8 @@ private def byteToHex (b : UInt8) : ByteArray :=
223223
ByteArray.mk #['%'.toUInt8, hi, lo]
224224

225225
/--
226-
Encodes a string as a URI component by percent-encoding all characters
227-
--/
226+
Encodes a string as a URI component by percent-encoding all characters.
227+
-/
228228
def encodeURIComponent (s : String) : ByteArray :=
229229
s.toUTF8.foldl (init := ByteArray.emptyWithCapacity s.utf8ByteSize) fun acc c =>
230230
if isUnreserved c then acc.push c else acc.append (byteToHex c)

src/Std/Internal/Http/Protocol/H1.lean

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,10 @@ def setHeaders (messageHead : Message.Head dir.swap) (machine : Machine dir) : M
336336
/-- Put some data inside the input of the machine. -/
337337
@[inline]
338338
def feed (machine : Machine ty) (data : ByteArray) : Machine ty :=
339-
{ machine with reader := machine.reader.feed data }
339+
if machine.isReaderClosed then
340+
machine
341+
else
342+
{ machine with reader := machine.reader.feed data }
340343

341344
/-- Signal that reader is not going to receive any more messages. -/
342345
@[inline]

0 commit comments

Comments
 (0)