-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirrorer.nim
34 lines (26 loc) · 929 Bytes
/
mirrorer.nim
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 httpclient
import cgi
import parsecfg
import strutils
import strformat
var config = loadConfig("mirrorer.ini")
var uri = split(getRequestURI(), '/', 3)
if uri.len != 4:
writeContentType()
echo "Malformed URL"
quit(1)
var target = uri[2]
var url = uri[3]
var site = config.getSectionValue(target, "url", "")
var rSite = &"""{config.getSectionValue("", "protocol", "http")}://{getHttpHost()}/{uri[1]}/{uri[2]}"""
var auth = config.getSectionValue(target, "auth", "")
if site.len == 0:
writeContentType()
echo &"Site '{target}' not found in config"
quit(1)
var headers = newHttpHeaders({"User-Agent": getHttpUserAgent(), "Authorization": &"Basic {auth}"})
var client = newHttpClient(headers=headers)
var get = client.get(site & url)
var contentType = get.headers["Content-Type"]
write(stdout, &"Content-type: {contentType}\n\n")
write(stdout, get.body.replace(site.strip(chars={'/'}), rSite))