tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

533 lignes · 12709 octetsBrut · Historique
1/// URLs (RFC 3986) with a host, as HTTP clients need them: parse one,
2/// resolve a link against it, write it back, and the request target to
3/// send.
4///
5/// url.parse "https://Example.com/a/b?x=1"
6/// // Ok(Url("https", "example.com", None, "/a/b", Some("x=1"), None))
7/// url.resolve page "../c#top" // Ok: https://example.com/c#top
8/// url.request_target page // "/a/b?x=1"
9///
10/// A URL here always has a scheme and a host (`mailto:` and `urn:` are
11/// refused). The scheme and the host are lowercased, an IPv6 host keeps
12/// its brackets (`[::1]`), and the path has its dot segments removed.
13/// Refused: a user name or password (`user@host`), a host that is not
14/// ASCII (there is no IDNA: write the `xn--` form), a host with a
15/// character outside `A-Z a-z 0-9 - . _ ~`, a port above 65535, and a
16/// control character anywhere.
17
18import std/bytes
19import std/list
20import std/option
21import std/result
22import std/string
23
24/// An absolute URL. `port` is `None` when the URL names none (see
25/// [`port_or_default`]); `query` and `fragment` are `None` when absent and
26/// `Some("")` after a bare `?` or `#`. The path is `""` or starts with `/`.
27@derive Eq Debug
28pub type Url
29{ Url(
30 scheme: String
31 , host: String
32 , port: Option<Int>
33 , path: String
34 , query: Option<String>
35 , fragment: Option<String>
36 )
37}
38
39impl Show<Url>
40{
41 fn show :: url
42 {
43 to_string url
44 }
45}
46
47/// A reference split into its five parts (RFC 3986, Appendix B).
48type Parts
49{ Parts(
50 scheme: Option<String>
51 , authority: Option<String>
52 , path: String
53 , query: Option<String>
54 , fragment: Option<String>
55 )
56}
57
58// ---- parsing -------------------------------------------------------------------
59
60/// The absolute URL `text` holds, or why it is not one.
61pub fn parse :: text: String -> Result<Url, String>
62{
63 use parts <- result.try (split text)
64 absolute parts
65}
66
67fn absolute :: parts: Parts -> Result<Url, String>
68{
69 match (parts.scheme, parts.authority)
70 { (None, _) -> Error("not an absolute URL: it has no scheme")
71 , (_, None) -> Error("a URL needs a host: //host after the scheme")
72 , (Some(scheme), Some(authority)) ->
73 {
74 use (host, port) <- result.try (authority_of authority)
75 Ok( Url( scheme
76 , host
77 , port
78 , remove_dot_segments parts.path
79 , parts.query
80 , parts.fragment
81 )
82 )
83 }
84 }
85}
86
87fn split :: text: String -> Result<Parts, String>
88{
89 let data = bytes.from_string text
90 match control_at data 0
91 { Some(at) -> Error("a control character at byte {at}")
92 , None ->
93 {
94 let colon = until data 0 [58, 47, 63, 35]
95 if colon > 0 && bytes.read_u8 data colon == 58
96 {
97 let scheme = text_of data 0 colon
98 if is_scheme scheme
99 {
100 Ok(after_scheme data (colon + 1) (Some(string.to_lower scheme)))
101 }
102 else
103 {
104 Error("a scheme can only have letters, digits, + - and .")
105 }
106 }
107 else
108 {
109 Ok(after_scheme data 0 None)
110 }
111 }
112 }
113}
114
115fn after_scheme :: data: Bytes at: Int scheme: Option<String> -> Parts
116{
117 if at_text data at "//"
118 {
119 let end = until data (at + 2) [47, 63, 35]
120 after_authority data end scheme (Some(text_of data (at + 2) end))
121 }
122 else
123 {
124 after_authority data at scheme None
125 }
126}
127
128fn after_authority ::
129 data: Bytes
130 start: Int
131 scheme: Option<String>
132 authority: Option<String>
133 -> Parts
134{
135 let n = bytes.length data
136 let path_end = until data start [63, 35]
137 let hash = until data path_end [35]
138 let query = if path_end < hash
139 {
140 Some(text_of data (path_end + 1) hash)
141 }
142 else
143 {
144 None
145 }
146 let fragment = if hash < n { Some(text_of data (hash + 1) n) } else { None }
147 Parts(scheme, authority, text_of data start path_end, query, fragment)
148}
149
150fn is_scheme :: text: String -> Bool
151{
152 let data = bytes.from_string text
153 is_alpha (bytes.read_u8 data 0)
154 && all_from
155 data
156 0
157 fn b { is_alpha b || is_digit b || b == 43 || b == 45 || b == 46 }
158}
159
160/// The host (lowercased) and the port of an authority.
161fn authority_of :: text: String -> Result<(String, Option<Int>), String>
162{
163 let data = bytes.from_string text
164 if until data 0 [64] < bytes.length data
165 {
166 Error("a user name or password in a URL (user@host) is refused")
167 }
168 else if bytes.read_u8 data 0 == 91
169 {
170 let close = until data 0 [93]
171 if close == bytes.length data
172 {
173 Error("an IPv6 host without its closing ]")
174 }
175 else
176 {
177 use host <- result.try (ipv6_host data close)
178 use port <- result.try (port_after data (close + 1))
179 Ok((host, port))
180 }
181 }
182 else
183 {
184 let colon = until data 0 [58]
185 use host <- result.try (name_host data colon)
186 use port <- result.try (port_after data colon)
187 Ok((host, port))
188 }
189}
190
191fn ipv6_host :: data: Bytes close: Int -> Result<String, String>
192{
193 let inside = bytes.slice data 1 close
194 if bytes.length inside > 0 && until inside 0 [58] < bytes.length inside
195 && all_from inside 0 fn b { is_hex b || b == 58 || b == 46 }
196 {
197 Ok(string.to_lower (text_of data 0 (close + 1)))
198 }
199 else
200 {
201 Error("a bad IPv6 host")
202 }
203}
204
205fn name_host :: data: Bytes end: Int -> Result<String, String>
206{
207 let host = bytes.slice data 0 end
208 if end == 0
209 {
210 Error("a URL needs a host")
211 }
212 else if !all_from host 0 fn b { b < 128 }
213 {
214 Error("a host that is not ASCII (no IDNA here): write its xn-- form")
215 }
216 else if !all_from host 0 is_unreserved
217 {
218 Error("a host can only have letters, digits, - . _ and ~")
219 }
220 else
221 {
222 Ok(string.to_lower (text_of data 0 end))
223 }
224}
225
226/// The port after the host, which ends at `at`: none, or `:` and digits.
227fn port_after :: data: Bytes at: Int -> Result<Option<Int>, String>
228{
229 let n = bytes.length data
230 if at == n || at + 1 == n && bytes.read_u8 data at == 58
231 {
232 Ok(None)
233 }
234 else if bytes.read_u8 data at != 58
235 {
236 Error("a host followed by something other than :port")
237 }
238 else
239 {
240 let digits = bytes.slice data (at + 1) n
241 if bytes.length digits <= 5 && all_from digits 0 is_digit
242 {
243 let port =
244 result.unwrap_or (string.to_int (bytes.to_string_lossy digits)) 65536
245 if port <= 65535
246 {
247 Ok(Some(port))
248 }
249 else
250 {
251 Error("a port above 65535")
252 }
253 }
254 else
255 {
256 Error("a bad port")
257 }
258 }
259}
260
261// ---- resolving -----------------------------------------------------------------
262
263/// The URL `reference` (a link: absolute, `//host/x`, `/x`, `x`, `../x`,
264/// `?q`, `#f`) points to from `base` (RFC 3986, §5.2). A reference with a
265/// scheme must itself be a URL with a host.
266pub fn resolve :: base: Url reference: String -> Result<Url, String>
267{
268 use r <- result.try (split reference)
269 match (r.scheme, r.authority)
270 { (Some(_), _) -> absolute r
271 , (None, Some(authority)) ->
272 {
273 use (host, port) <- result.try (authority_of authority)
274 Ok( Url( base.scheme
275 , host
276 , port
277 , remove_dot_segments r.path
278 , r.query
279 , r.fragment
280 )
281 )
282 }
283 , (None, None) -> Ok(relative base r)
284 }
285}
286
287fn relative :: base: Url r: Parts -> Url
288{
289 if r.path == ""
290 {
291 Url(..base, query: option.or r.query base.query, fragment: r.fragment)
292 }
293 else
294 {
295 let path = if string.starts_with r.path "/"
296 {
297 r.path
298 }
299 else
300 {
301 merge base.path r.path
302 }
303 Url( ..base
304 , path: remove_dot_segments path
305 , query: r.query
306 , fragment: r.fragment
307 )
308 }
309}
310
311/// RFC 3986, §5.2.3: the base path up to its last `/`, then `path`.
312fn merge :: base: String path: String -> String
313{
314 if base == ""
315 {
316 "/" <> path
317 }
318 else
319 {
320 let data = bytes.from_string base
321 text_of data 0 (last_slash data (bytes.length data - 1) + 1) <> path
322 }
323}
324
325fn last_slash :: data: Bytes at: Int -> Int
326{
327 if at < 0 || bytes.read_u8 data at == 47
328 {
329 at
330 }
331 else
332 {
333 last_slash data (at - 1)
334 }
335}
336
337/// RFC 3986, §5.2.4: `.` and `..` segments interpreted and removed.
338fn remove_dot_segments :: path: String -> String
339{
340 string.concat (list.reverse (removed (bytes.from_string path) 0 []))
341}
342
343/// The output so far is `out`, its last segment first.
344fn removed :: data: Bytes at: Int out: List<String> -> List<String>
345{
346 if at >= bytes.length data
347 {
348 out
349 }
350 else if at_text data at "../"
351 {
352 removed data (at + 3) out
353 }
354 else if at_text data at "./"
355 {
356 removed data (at + 2) out
357 }
358 else if at_text data at "/./"
359 {
360 removed data (at + 2) out
361 }
362 else if rest_is data at "/."
363 {
364 ["/", ..out]
365 }
366 else if at_text data at "/../"
367 {
368 removed data (at + 3) (list.drop out 1)
369 }
370 else if rest_is data at "/.."
371 {
372 ["/", ..list.drop out 1]
373 }
374 else if rest_is data at "." || rest_is data at ".."
375 {
376 out
377 }
378 else
379 {
380 let end = until data (at + 1) [47]
381 removed data end [text_of data at end, ..out]
382 }
383}
384
385// ---- writing -------------------------------------------------------------------
386
387/// The URL as text: `parse (to_string url) == Ok(url)` for a parsed URL.
388pub fn to_string :: url: Url -> String
389{
390 let port = match url.port { Some(p) -> ":" <> show p, None -> "" }
391 let query = match url.query { Some(q) -> "?" <> q, None -> "" }
392 let fragment = match url.fragment { Some(f) -> "#" <> f, None -> "" }
393 string.concat [url.scheme, "://", url.host, port, url.path, query, fragment]
394}
395
396/// The URL's port, else its scheme's: 80 for `http` and `ws`, 443 for
397/// `https` and `wss`, `None` for another scheme.
398pub fn port_or_default :: url: Url -> Option<Int>
399{
400 match url.port
401 { Some(p) -> Some(p)
402 , None -> match url.scheme
403 { "http" | "ws" -> Some(80), "https" | "wss" -> Some(443), _ -> None }
404 }
405}
406
407/// What an HTTP request line names: the path (`/` when empty) and the
408/// query, without the fragment. A byte that may not appear there as it
409/// is (a space, `"`, `<`, a byte of a non-ASCII character, a `%` not
410/// followed by two hex digits) is percent-encoded; `%XX` escapes already
411/// written are kept.
412pub fn request_target :: url: Url -> String
413{
414 let path = if url.path == "" { "/" } else { url.path }
415 let query = match url.query { Some(q) -> "?" <> q, None -> "" }
416 let data = bytes.from_string (path <> query)
417 bytes.build_string (encoded data 0 (bytes.builder ()))
418}
419
420fn encoded :: data: Bytes at: Int out: bytes.Builder -> bytes.Builder
421{
422 let b = bytes.read_u8 data at
423 if b < 0
424 {
425 out
426 }
427 else if is_target b
428 || b == 37 && is_hex (bytes.read_u8 data (at + 1))
429 && is_hex (bytes.read_u8 data (at + 2))
430 {
431 encoded data (at + 1) (bytes.append_byte out b)
432 }
433 else
434 {
435 let digits = "0123456789ABCDEF"
436 let hex =
437 string.slice digits (b / 16) (b / 16 + 1)
438 <> string.slice digits (b % 16) (b % 16 + 1)
439 encoded data (at + 1) (bytes.append_string out ("%" <> hex))
440 }
441}
442
443/// Bytes a request target may hold as they are: unreserved, sub-delims,
444/// `:`, `@`, `/` and `?` (RFC 3986's pchar and query).
445fn is_target :: b: Int -> Bool
446{
447 is_unreserved b || b == 33 || b == 36 || b >= 38 && b <= 44 || b == 59
448 || b == 61
449 || b == 58
450 || b == 64
451 || b == 47
452 || b == 63
453}
454
455// ---- bytes ---------------------------------------------------------------------
456
457/// The first index at or after `at` whose byte is one of `stops`, else
458/// the length.
459fn until :: data: Bytes at: Int stops: List<Int> -> Int
460{
461 let b = bytes.read_u8 data at
462 if b < 0
463 {
464 bytes.length data
465 }
466 else if list.any stops fn s { s == b }
467 {
468 at
469 }
470 else
471 {
472 until data (at + 1) stops
473 }
474}
475
476fn control_at :: data: Bytes at: Int -> Option<Int>
477{
478 let b = bytes.read_u8 data at
479 if b < 0
480 {
481 None
482 }
483 else if b < 32 || b == 127
484 {
485 Some(at)
486 }
487 else
488 {
489 control_at data (at + 1)
490 }
491}
492
493fn all_from :: data: Bytes at: Int ok: fn(Int) -> Bool -> Bool
494{
495 let b = bytes.read_u8 data at
496 b < 0 || ok b && all_from data (at + 1) ok
497}
498
499fn text_of :: data: Bytes from: Int to: Int -> String
500{
501 bytes.to_string_lossy (bytes.slice data from to)
502}
503
504fn at_text :: data: Bytes at: Int text: String -> Bool
505{
506 let wanted = bytes.from_string text
507 bytes.slice data at (at + bytes.length wanted) == wanted
508}
509
510fn rest_is :: data: Bytes at: Int text: String -> Bool
511{
512 at + string.byte_length text == bytes.length data && at_text data at text
513}
514
515fn is_alpha :: b: Int -> Bool
516{
517 b >= 65 && b <= 90 || b >= 97 && b <= 122
518}
519
520fn is_digit :: b: Int -> Bool
521{
522 b >= 48 && b <= 57
523}
524
525fn is_hex :: b: Int -> Bool
526{
527 is_digit b || b >= 65 && b <= 70 || b >= 97 && b <= 102
528}
529
530fn is_unreserved :: b: Int -> Bool
531{
532 is_alpha b || is_digit b || b == 45 || b == 46 || b == 95 || b == 126
533}