diff options
author | rumpf_a@web.de <> | 2010-01-31 14:33:33 +0100 |
---|---|---|
committer | rumpf_a@web.de <> | 2010-01-31 14:33:33 +0100 |
commit | e22c73fd893447cc59dd854e9680626ed7da01b2 (patch) | |
tree | f287376afd1cad6e2aa573053e0e8f3e586262eb /lib/devel | |
parent | 262a47887d2578c2eb0c2196ea3191b6a088209f (diff) | |
download | Nim-e22c73fd893447cc59dd854e9680626ed7da01b2.tar.gz |
parseutils added
Diffstat (limited to 'lib/devel')
-rwxr-xr-x | lib/devel/httpclient.nim | 78 | ||||
-rw-r--r-- | lib/devel/parseutils.nim | 63 |
2 files changed, 40 insertions, 101 deletions
diff --git a/lib/devel/httpclient.nim b/lib/devel/httpclient.nim index fb9359630..d600fcb59 100755 --- a/lib/devel/httpclient.nim +++ b/lib/devel/httpclient.nim @@ -11,7 +11,7 @@ ## webpages/other data. # neuer Code: -import sockets, strutils, parseurl, pegs, os, parseutils +import sockets, strutils, parseurl, pegs, parseutils type TResponse* = tuple[ @@ -19,9 +19,9 @@ type 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 + EInvalidProtocol* = object of EBase ## exception that is raised when server + ## does not conform to the implemented + ## protocol EHttpRequestErr* = object of EBase ## Thrown in the ``getContent`` proc, ## when the server returns an error @@ -35,7 +35,7 @@ template newException(exceptn, message: expr): expr = e proc httpError(msg: string) = - var e: ref EInvalidHttp + var e: ref EInvalidProtocol new(e) e.msg = msg raise e @@ -54,42 +54,44 @@ proc getHeaderValue*(headers: seq[THeader], name: string): string = return headers[i].hvalue return "" +proc parseChunks(data: var string, start: int, s: TSocket): string = + # 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) + 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) + while missing > 0: + var bytesRead = s.recv(addr(result[L]), missing) + inc(L, bytesRead) + dec(missing, bytesRead) + + # next chunk: + data = s.recv() + i = 0 + # skip trailing CR-LF: + while data[i] in {'\C', '\L'}: inc(i) + if data[i] == '\0': data.add(s.recv()) + 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) - + result = parseChunks(data, start, s) else: result = copy(data, start) # -REGION- Content-Length diff --git a/lib/devel/parseutils.nim b/lib/devel/parseutils.nim deleted file mode 100644 index 4c5152167..000000000 --- a/lib/devel/parseutils.nim +++ /dev/null @@ -1,63 +0,0 @@ -# -# -# Nimrod's Runtime Library -# (c) Copyright 2010 Andreas Rumpf -# -# See the file "copying.txt", included in this -# distribution, for details about the copyright. -# - -## Helpers for parsing. - -import strutils - -proc parseHex*(s: string, number: var int, start = 0): int = - ## parses a hexadecimal number and stores its value in ``number``. Returns - ## the number of the parsed characters or 0 in case of an error. - var i = start - var foundDigit = false - if s[i] == '0' and (s[i+1] == 'x' or s[i+1] == 'X'): inc(i, 2) - elif s[i] == '#': inc(i) - while true: - case s[i] - of '_': nil - of '0'..'9': - number = number shl 4 or (ord(s[i]) - ord('0')) - foundDigit = true - of 'a'..'f': - number = number shl 4 or (ord(s[i]) - ord('a') + 10) - foundDigit = true - of 'A'..'F': - number = number shl 4 or (ord(s[i]) - ord('A') + 10) - foundDigit = true - else: break - inc(i) - if foundDigit: result = i-start - -proc parseIdent*(s: string, ident: var string, start = 0): int = - ## parses an identifier and stores it in ``ident``. Returns - ## the number of the parsed characters or 0 in case of an error. - var i = start - if s[i] in IdentStartChars: - inc(i) - while s[i] in IdentChars: inc(i) - ident = copy(s, start, i-1) - result = i-start - -proc skipWhitespace*(s: string, start = 0): int {.inline.} = - while s[start+result] in Whitespace: inc(result) - -proc skip*(s, token: string, start = 0): int = - while result < token.len and s[result+start] == token[result]: inc(result) - if result != token.len: result = 0 - -proc skipIgnoreCase*(s, token: string, start = 0): int = - while result < token.len and - toLower(s[result+start]) == toLower(token[result]): inc(result) - if result != token.len: result = 0 - -proc parseBiggestInt*(s: string, number: var biggestInt, start = 0): int = - assert(false) # to implement - -proc parseBiggestFloat*(s: string, number: var biggestFloat, start = 0): int = - assert(false) # to implement |