Skip to content

Commit 9882dba

Browse files
committed
feat: request type has request target
1 parent ad7fa87 commit 9882dba

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ prelude
99
public import Std.Internal.Http.Data.Method
1010
public import Std.Internal.Http.Data.Version
1111
public import Std.Internal.Http.Data.Headers
12+
public import Std.Internal.Http.Data.URI
1213

1314
public section
1415

@@ -40,7 +41,7 @@ structure Request.Head where
4041
/--
4142
The request target/URI indicating the resource being requested
4243
-/
43-
uri : String := ""
44+
uri : RequestTarget := .asteriskForm
4445

4546
/--
4647
Collection of HTTP headers for the request (Content-Type, Authorization, etc.)
@@ -88,7 +89,7 @@ instance : Encode .v11 Head where
8889
encode buffer req :=
8990
let buffer := Encode.encode (v := .v11) buffer req.method
9091
let buffer := buffer.writeChar ' '
91-
let buffer := buffer.writeString req.uri
92+
let buffer := Encode.encode (v := .v11) buffer req.uri
9293
let buffer := buffer.writeChar ' '
9394
let buffer := Encode.encode (v := .v11) buffer req.version
9495
let buffer := buffer.writeString "\r\n"
@@ -124,9 +125,16 @@ def version (builder : Builder) (version : Version) : Builder :=
124125
/--
125126
Sets the request target/URI for the request being built
126127
-/
127-
def uri (builder : Builder) (uri : String) : Builder :=
128+
def uri (builder : Builder) (uri : RequestTarget) : Builder :=
128129
{ builder with head := { builder.head with uri := uri } }
129130

131+
/--
132+
Sets the request target/URI for the request being built
133+
-/
134+
def uri! (builder : Builder) (uri : String) : Builder :=
135+
let uri := RequestTarget.parse! uri
136+
{ builder with head := { builder.head with uri } }
137+
130138
/--
131139
Sets the headers for the request being built
132140
-/
@@ -166,39 +174,39 @@ end Builder
166174
/--
167175
Creates a new HTTP GET Request with the specified URI
168176
-/
169-
def get (uri : String) : Builder :=
177+
def get (uri : RequestTarget) : Builder :=
170178
new
171179
|>.method .get
172180
|>.uri uri
173181

174182
/--
175183
Creates a new HTTP POST Request builder with the specified URI.
176184
-/
177-
def post (uri : String) : Builder :=
185+
def post (uri : RequestTarget) : Builder :=
178186
new
179187
|>.method .post
180188
|>.uri uri
181189

182190
/--
183191
Creates a new HTTP PUT Request builder with the specified URI.
184192
-/
185-
def put (uri : String) : Builder :=
193+
def put (uri : RequestTarget) : Builder :=
186194
new
187195
|>.method .put
188196
|>.uri uri
189197

190198
/--
191199
Creates a new HTTP DELETE Request builder with the specified URI
192200
-/
193-
def delete (uri : String) : Builder :=
201+
def delete (uri : RequestTarget) : Builder :=
194202
new
195203
|>.method .delete
196204
|>.uri uri
197205

198206
/--
199207
Creates a new HTTP PATCH Request builder with the specified URI
200208
-/
201-
def patch (uri : String) : Builder :=
209+
def patch (uri : RequestTarget) : Builder :=
202210
new
203211
|>.method .patch
204212
|>.uri uri
@@ -207,33 +215,33 @@ def patch (uri : String) : Builder :=
207215
Creates a new HTTP HEAD Request builder with the specified URI.
208216
Named `head'` to avoid conflict with the `head` field accessor.
209217
-/
210-
def head' (uri : String) : Builder :=
218+
def head' (uri : RequestTarget) : Builder :=
211219
new
212220
|>.method .head
213221
|>.uri uri
214222

215223
/--
216224
Creates a new HTTP OPTIONS Request builder with the specified URI.
217-
Use `Request.options (String.asteriskForm)` for server-wide OPTIONS.
225+
Use `Request.options (RequestTarget.asteriskForm)` for server-wide OPTIONS.
218226
-/
219-
def options (uri : String) : Builder :=
227+
def options (uri : RequestTarget) : Builder :=
220228
new
221229
|>.method .options
222230
|>.uri uri
223231

224232
/--
225233
Creates a new HTTP CONNECT Request builder with the specified URI.
226-
Typically used with `String.authorityForm` for tunneling.
234+
Typically used with `RequestTarget.authorityForm` for tunneling.
227235
-/
228-
def connect (uri : String) : Builder :=
236+
def connect (uri : RequestTarget) : Builder :=
229237
new
230238
|>.method .connect
231239
|>.uri uri
232240

233241
/--
234242
Creates a new HTTP TRACE Request builder with the specified URI
235243
-/
236-
def trace (uri : String) : Builder :=
244+
def trace (uri : RequestTarget) : Builder :=
237245
new
238246
|>.method .trace
239247
|>.uri uri

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,4 +726,7 @@ instance : ToString RequestTarget where
726726
| .authorityForm auth => toString auth
727727
| .asteriskForm => "*"
728728

729+
instance : Encode .v11 RequestTarget where
730+
encode buffer target := buffer.writeString (toString target)
731+
729732
end Std.Http.RequestTarget

tests/lean/run/async_http_encode.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ info: "X-Custom-Header: value\x0d\n"
132132
info: "GET /path HTTP/1.1\x0d\n\x0d\n"
133133
-/
134134
#guard_msgs in
135-
#eval encodeStr ({ method := .get, version := .v11, uri := "/path" } : Request.Head)
135+
#eval encodeStr ({ method := .get, version := .v11, uri := .parse! "/path" } : Request.Head)
136136

137137
/--
138138
info: "POST /submit HTTP/1.1\x0d\n\x0d\n"
139139
-/
140140
#guard_msgs in
141-
#eval encodeStr ({ method := .post, version := .v11, uri := "/submit" } : Request.Head)
141+
#eval encodeStr ({ method := .post, version := .v11, uri := .parse! "/submit" } : Request.Head)
142142

143143
/--
144144
info: "PUT /resource HTTP/2.0\x0d\nContent-Type: application/json\x0d\n\x0d\n"
@@ -147,7 +147,7 @@ info: "PUT /resource HTTP/2.0\x0d\nContent-Type: application/json\x0d\n\x0d\n"
147147
#eval encodeStr ({
148148
method := .put
149149
version := .v20
150-
uri := "/resource"
150+
uri := .parse! "/resource"
151151
headers := Headers.empty.insert! "content-type" "application/json"
152152
} : Request.Head)
153153

0 commit comments

Comments
 (0)