blob: c8fdea8b4682c2c909b2119cab6383791e5fd949 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import std/strutils, protocols/http, formats/[html, uri], gui/terminal
let url = "https://example.org:443/index.html"
# let url = paramStr(1)
proc request*(uri: string) =
# This is probably the best place to implement scheme-specific stuff
let url = parseURL(uri)
case url.scheme:
of "http", "https":
let response = httpRequest(url)
render(parseHTML(response.body))
# Exercise: view-source
of "view-source":
# We must parse the url again without the view-source: prefix
let url = uri.split(':', maxsplit=1)[1]
let response = httpRequest(parseURL(url))
renderSource(parseHTML((response.body)))
# Exercise: file:// scheme
of "file":
discard
# Exercise: data scheme
of "data":
discard
else:
raise newException(Exception, "Not a valid scheme: " & url.scheme)
when isMainModule:
request(url)
# HTTP/1.1
# Compression
# Redirects
# Caching
|