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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#
#
# Nimrod's Runtime Library
# (c) Copyright 2010 Dominik Picheta, Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module implements a simple HTTP client that can be used to retrieve
## webpages/other data.
# neuer Code:
import sockets, strutils, parseurl, pegs, os, parseutils
type
TResponse* = tuple[
version: string, status: string, headers: seq[THeader],
body: string]
THeader* = tuple[htype: string, hvalue: string]
EInvalidHttp* = object of EBase ## exception that is raised when server does
## not conform to the implemented HTTP
## protocol
EHttpRequestErr* = object of EBase ## Thrown in the ``getContent`` proc,
## when the server returns an error
template newException(exceptn, message: expr): expr =
block: # open a new scope
var
e: ref exceptn
new(e)
e.msg = message
e
proc httpError(msg: string) =
var e: ref EInvalidHttp
new(e)
e.msg = msg
raise e
proc fileError(msg: string) =
var e: ref EIO
new(e)
e.msg = msg
raise e
proc getHeaderValue*(headers: seq[THeader], name: string): string =
## Retrieves a header by ``name``, from ``headers``.
## Returns "" if a header is not found
for i in low(headers)..high(headers):
if cmpIgnoreCase(headers[i].htype, name) == 0:
return headers[i].hvalue
return ""
proc parseBody(data: var string, start: int, s: TSocket,
headers: seq[THeader]): string =
if getHeaderValue(headers, "Transfer-Encoding") == "chunked":
# get chunks:
var i = start
result = ""
while true:
var chunkSize = 0
var j = parseHex(data, chunkSize, i)
if j <= 0: break
inc(i, j)
while data[i] notin {'\C', '\L', '\0'}: inc(i)
if data[i] == '\C': inc(i)
if data[i] == '\L': inc(i)
echo "ChunkSize: ", chunkSize
if chunkSize <= 0: break
var x = copy(data, i, i+chunkSize-1)
var size = x.len
result.add(x)
if size < chunkSize:
# read in the rest:
var missing = chunkSize - size
var L = result.len
setLen(result, L + missing)
discard s.recv(addr(result[L]), missing)
# next chunk:
data = s.recv()
echo data
i = 0
# skip trailing CR-LF:
while data[i] in {'\C', '\L'}: inc(i)
else:
result = copy(data, start)
# -REGION- Content-Length
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.3
var contentLengthHeader = getHeaderValue(headers, "Content-Length")
if contentLengthHeader != "":
var length = contentLengthHeader.parseint()
while result.len() < length: result.add(s.recv())
else:
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.4 TODO
# -REGION- Connection: Close
# (http://tools.ietf.org/html/rfc2616#section-4.4) NR.5
if getHeaderValue(headers, "Connection") == "close":
while True:
var moreData = recv(s)
if moreData.len == 0: break
result.add(moreData)
proc parseResponse(s: TSocket): TResponse =
var data = s.recv()
var i = 0
# Parse the version
# Parses the first line of the headers
# ``HTTP/1.1`` 200 OK
var matches: array[0..1, string]
var L = data.matchLen(peg"\i 'HTTP/' {'1.1'/'1.0'} \s+ {(!\n .)*}\n",
matches, i)
if L < 0: httpError("invalid HTTP header")
result.version = matches[0]
result.status = matches[1]
inc(i, L)
# Parse the headers
# Everything after the first line leading up to the body
# htype: hvalue
result.headers = @[]
while true:
var key = ""
while data[i] != ':':
if data[i] == '\0': httpError("invalid HTTP header, ':' expected")
key.add(data[i])
inc(i)
inc(i) # skip ':'
if data[i] == ' ': inc(i) # skip if the character is a space
var val = ""
while data[i] notin {'\C', '\L', '\0'}:
val.add(data[i])
inc(i)
result.headers.add((key, val))
if data[i] == '\C': inc(i)
if data[i] == '\L': inc(i)
else: httpError("invalid HTTP header, CR-LF expected")
if data[i] == '\C': inc(i)
if data[i] == '\L':
inc(i)
break
result.body = parseBody(data, i, s, result.headers)
proc request*(url: string): TResponse =
var r = parse(url)
var headers: string
if r.path != "":
headers = "GET " & r.path & " HTTP/1.1\c\L"
else:
headers = "GET / HTTP/1.1\c\L"
add(headers, "Host: " & r.hostname & "\c\L\c\L")
var s = socket()
s.connect(r.hostname, TPort(80))
s.send(headers)
result = parseResponse(s)
s.close()
proc redirection(status: string): bool =
const redirectionNRs = ["301", "302", "303", "307"]
for i in items(redirectionNRs):
if status.startsWith(i):
return True
proc get*(url: string, maxRedirects = 5): TResponse =
## low-level proc similar to ``request`` which handles redirection
result = request(url)
for i in 1..maxRedirects:
if result.status.redirection():
var locationHeader = getHeaderValue(result.headers, "Location")
if locationHeader == "": httpError("location header expected")
result = request(locationHeader)
proc getContent*(url: string): string =
## GET's the body and returns it as a string
## Raises exceptions for the status codes ``4xx`` and ``5xx``
var r = get(url)
if r.status[0] in {'4','5'}:
raise newException(EHTTPRequestErr, r.status)
else:
return r.body
proc downloadFile*(url: string, outputFilename: string) =
var f: TFile
if open(f, outputFilename, fmWrite):
f.write(getContent(url))
f.close()
else:
fileError("Unable to open file")
when isMainModule:
downloadFile("http://www.google.com", "GoogleTest.html")
|