httpcore.nim 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2016 Dominik Picheta
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Contains functionality shared between the ``httpclient`` and
  10. ## ``asynchttpserver`` modules.
  11. import tables, strutils, parseutils
  12. type
  13. HttpHeaders* = ref object
  14. table*: TableRef[string, seq[string]]
  15. HttpHeaderValues* = distinct seq[string]
  16. # The range starts at '0' so that we don't have to explicitly initialise
  17. # it. See: http://irclogs.nim-lang.org/19-09-2016.html#19:48:27 for context.
  18. HttpCode* = distinct range[0 .. 599]
  19. HttpVersion* = enum
  20. HttpVer11,
  21. HttpVer10
  22. HttpMethod* = enum ## the requested HttpMethod
  23. HttpHead, ## Asks for the response identical to the one that would
  24. ## correspond to a GET request, but without the response
  25. ## body.
  26. HttpGet, ## Retrieves the specified resource.
  27. HttpPost, ## Submits data to be processed to the identified
  28. ## resource. The data is included in the body of the
  29. ## request.
  30. HttpPut, ## Uploads a representation of the specified resource.
  31. HttpDelete, ## Deletes the specified resource.
  32. HttpTrace, ## Echoes back the received request, so that a client
  33. ## can see what intermediate servers are adding or
  34. ## changing in the request.
  35. HttpOptions, ## Returns the HTTP methods that the server supports
  36. ## for specified address.
  37. HttpConnect, ## Converts the request connection to a transparent
  38. ## TCP/IP tunnel, usually used for proxies.
  39. HttpPatch ## Applies partial modifications to a resource.
  40. const
  41. Http100* = HttpCode(100)
  42. Http101* = HttpCode(101)
  43. Http200* = HttpCode(200)
  44. Http201* = HttpCode(201)
  45. Http202* = HttpCode(202)
  46. Http203* = HttpCode(203)
  47. Http204* = HttpCode(204)
  48. Http205* = HttpCode(205)
  49. Http206* = HttpCode(206)
  50. Http300* = HttpCode(300)
  51. Http301* = HttpCode(301)
  52. Http302* = HttpCode(302)
  53. Http303* = HttpCode(303)
  54. Http304* = HttpCode(304)
  55. Http305* = HttpCode(305)
  56. Http307* = HttpCode(307)
  57. Http400* = HttpCode(400)
  58. Http401* = HttpCode(401)
  59. Http403* = HttpCode(403)
  60. Http404* = HttpCode(404)
  61. Http405* = HttpCode(405)
  62. Http406* = HttpCode(406)
  63. Http407* = HttpCode(407)
  64. Http408* = HttpCode(408)
  65. Http409* = HttpCode(409)
  66. Http410* = HttpCode(410)
  67. Http411* = HttpCode(411)
  68. Http412* = HttpCode(412)
  69. Http413* = HttpCode(413)
  70. Http414* = HttpCode(414)
  71. Http415* = HttpCode(415)
  72. Http416* = HttpCode(416)
  73. Http417* = HttpCode(417)
  74. Http418* = HttpCode(418)
  75. Http421* = HttpCode(421)
  76. Http422* = HttpCode(422)
  77. Http426* = HttpCode(426)
  78. Http428* = HttpCode(428)
  79. Http429* = HttpCode(429)
  80. Http431* = HttpCode(431)
  81. Http451* = HttpCode(451)
  82. Http500* = HttpCode(500)
  83. Http501* = HttpCode(501)
  84. Http502* = HttpCode(502)
  85. Http503* = HttpCode(503)
  86. Http504* = HttpCode(504)
  87. Http505* = HttpCode(505)
  88. const headerLimit* = 10_000
  89. proc newHttpHeaders*(): HttpHeaders =
  90. new result
  91. result.table = newTable[string, seq[string]]()
  92. proc newHttpHeaders*(keyValuePairs:
  93. openarray[tuple[key: string, val: string]]): HttpHeaders =
  94. var pairs: seq[tuple[key: string, val: seq[string]]] = @[]
  95. for pair in keyValuePairs:
  96. pairs.add((pair.key.toLowerAscii(), @[pair.val]))
  97. new result
  98. result.table = newTable[string, seq[string]](pairs)
  99. proc `$`*(headers: HttpHeaders): string =
  100. return $headers.table
  101. proc clear*(headers: HttpHeaders) =
  102. headers.table.clear()
  103. proc `[]`*(headers: HttpHeaders, key: string): HttpHeaderValues =
  104. ## Returns the values associated with the given ``key``. If the returned
  105. ## values are passed to a procedure expecting a ``string``, the first
  106. ## value is automatically picked. If there are
  107. ## no values associated with the key, an exception is raised.
  108. ##
  109. ## To access multiple values of a key, use the overloaded ``[]`` below or
  110. ## to get all of them access the ``table`` field directly.
  111. return headers.table[key.toLowerAscii].HttpHeaderValues
  112. converter toString*(values: HttpHeaderValues): string =
  113. return seq[string](values)[0]
  114. proc `[]`*(headers: HttpHeaders, key: string, i: int): string =
  115. ## Returns the ``i``'th value associated with the given key. If there are
  116. ## no values associated with the key or the ``i``'th value doesn't exist,
  117. ## an exception is raised.
  118. return headers.table[key.toLowerAscii][i]
  119. proc `[]=`*(headers: HttpHeaders, key, value: string) =
  120. ## Sets the header entries associated with ``key`` to the specified value.
  121. ## Replaces any existing values.
  122. headers.table[key.toLowerAscii] = @[value]
  123. proc `[]=`*(headers: HttpHeaders, key: string, value: seq[string]) =
  124. ## Sets the header entries associated with ``key`` to the specified list of
  125. ## values.
  126. ## Replaces any existing values.
  127. headers.table[key.toLowerAscii] = value
  128. proc add*(headers: HttpHeaders, key, value: string) =
  129. ## Adds the specified value to the specified key. Appends to any existing
  130. ## values associated with the key.
  131. if not headers.table.hasKey(key.toLowerAscii):
  132. headers.table[key.toLowerAscii] = @[value]
  133. else:
  134. headers.table[key.toLowerAscii].add(value)
  135. proc del*(headers: HttpHeaders, key: string) =
  136. ## Delete the header entries associated with ``key``
  137. headers.table.del(key.toLowerAscii)
  138. iterator pairs*(headers: HttpHeaders): tuple[key, value: string] =
  139. ## Yields each key, value pair.
  140. for k, v in headers.table:
  141. for value in v:
  142. yield (k, value)
  143. proc contains*(values: HttpHeaderValues, value: string): bool =
  144. ## Determines if ``value`` is one of the values inside ``values``. Comparison
  145. ## is performed without case sensitivity.
  146. for val in seq[string](values):
  147. if val.toLowerAscii == value.toLowerAscii: return true
  148. proc hasKey*(headers: HttpHeaders, key: string): bool =
  149. return headers.table.hasKey(key.toLowerAscii())
  150. proc getOrDefault*(headers: HttpHeaders, key: string,
  151. default = @[""].HttpHeaderValues): HttpHeaderValues =
  152. ## Returns the values associated with the given ``key``. If there are no
  153. ## values associated with the key, then ``default`` is returned.
  154. if headers.hasKey(key):
  155. return headers[key]
  156. else:
  157. return default
  158. proc len*(headers: HttpHeaders): int = return headers.table.len
  159. proc parseList(line: string, list: var seq[string], start: int): int =
  160. var i = 0
  161. var current = ""
  162. while start+i < line.len and line[start + i] notin {'\c', '\l'}:
  163. i += line.skipWhitespace(start + i)
  164. i += line.parseUntil(current, {'\c', '\l', ','}, start + i)
  165. list.add(current)
  166. if start+i < line.len and line[start + i] == ',':
  167. i.inc # Skip ,
  168. current.setLen(0)
  169. proc parseHeader*(line: string): tuple[key: string, value: seq[string]] =
  170. ## Parses a single raw header HTTP line into key value pairs.
  171. ##
  172. ## Used by ``asynchttpserver`` and ``httpclient`` internally and should not
  173. ## be used by you.
  174. result.value = @[]
  175. var i = 0
  176. i = line.parseUntil(result.key, ':')
  177. inc(i) # skip :
  178. if i < len(line):
  179. i += parseList(line, result.value, i)
  180. elif result.key.len > 0:
  181. result.value = @[""]
  182. else:
  183. result.value = @[]
  184. proc `==`*(protocol: tuple[orig: string, major, minor: int],
  185. ver: HttpVersion): bool =
  186. let major =
  187. case ver
  188. of HttpVer11, HttpVer10: 1
  189. let minor =
  190. case ver
  191. of HttpVer11: 1
  192. of HttpVer10: 0
  193. result = protocol.major == major and protocol.minor == minor
  194. proc contains*(methods: set[HttpMethod], x: string): bool =
  195. return parseEnum[HttpMethod](x) in methods
  196. proc `$`*(code: HttpCode): string =
  197. ## Converts the specified ``HttpCode`` into a HTTP status.
  198. ##
  199. ## For example:
  200. ##
  201. ## .. code-block:: nim
  202. ## doAssert($Http404 == "404 Not Found")
  203. case code.int
  204. of 100: "100 Continue"
  205. of 101: "101 Switching Protocols"
  206. of 200: "200 OK"
  207. of 201: "201 Created"
  208. of 202: "202 Accepted"
  209. of 203: "203 Non-Authoritative Information"
  210. of 204: "204 No Content"
  211. of 205: "205 Reset Content"
  212. of 206: "206 Partial Content"
  213. of 300: "300 Multiple Choices"
  214. of 301: "301 Moved Permanently"
  215. of 302: "302 Found"
  216. of 303: "303 See Other"
  217. of 304: "304 Not Modified"
  218. of 305: "305 Use Proxy"
  219. of 307: "307 Temporary Redirect"
  220. of 400: "400 Bad Request"
  221. of 401: "401 Unauthorized"
  222. of 403: "403 Forbidden"
  223. of 404: "404 Not Found"
  224. of 405: "405 Method Not Allowed"
  225. of 406: "406 Not Acceptable"
  226. of 407: "407 Proxy Authentication Required"
  227. of 408: "408 Request Timeout"
  228. of 409: "409 Conflict"
  229. of 410: "410 Gone"
  230. of 411: "411 Length Required"
  231. of 412: "412 Precondition Failed"
  232. of 413: "413 Request Entity Too Large"
  233. of 414: "414 Request-URI Too Long"
  234. of 415: "415 Unsupported Media Type"
  235. of 416: "416 Requested Range Not Satisfiable"
  236. of 417: "417 Expectation Failed"
  237. of 418: "418 I'm a teapot"
  238. of 421: "421 Misdirected Request"
  239. of 422: "422 Unprocessable Entity"
  240. of 426: "426 Upgrade Required"
  241. of 428: "428 Precondition Required"
  242. of 429: "429 Too Many Requests"
  243. of 431: "431 Request Header Fields Too Large"
  244. of 451: "451 Unavailable For Legal Reasons"
  245. of 500: "500 Internal Server Error"
  246. of 501: "501 Not Implemented"
  247. of 502: "502 Bad Gateway"
  248. of 503: "503 Service Unavailable"
  249. of 504: "504 Gateway Timeout"
  250. of 505: "505 HTTP Version Not Supported"
  251. else: $(int(code))
  252. proc `==`*(a, b: HttpCode): bool {.borrow.}
  253. proc `==`*(rawCode: string, code: HttpCode): bool =
  254. return cmpIgnoreCase(rawCode, $code) == 0
  255. proc is2xx*(code: HttpCode): bool =
  256. ## Determines whether ``code`` is a 2xx HTTP status code.
  257. return code.int in {200 .. 299}
  258. proc is3xx*(code: HttpCode): bool =
  259. ## Determines whether ``code`` is a 3xx HTTP status code.
  260. return code.int in {300 .. 399}
  261. proc is4xx*(code: HttpCode): bool =
  262. ## Determines whether ``code`` is a 4xx HTTP status code.
  263. return code.int in {400 .. 499}
  264. proc is5xx*(code: HttpCode): bool =
  265. ## Determines whether ``code`` is a 5xx HTTP status code.
  266. return code.int in {500 .. 599}
  267. proc `$`*(httpMethod: HttpMethod): string =
  268. return (system.`$`(httpMethod))[4 .. ^1].toUpperAscii()
  269. when isMainModule:
  270. var test = newHttpHeaders()
  271. test["Connection"] = @["Upgrade", "Close"]
  272. doAssert test["Connection", 0] == "Upgrade"
  273. doAssert test["Connection", 1] == "Close"
  274. test.add("Connection", "Test")
  275. doAssert test["Connection", 2] == "Test"
  276. doAssert "upgrade" in test["Connection"]
  277. # Bug #5344.
  278. doAssert parseHeader("foobar: ") == ("foobar", @[""])
  279. let (key, value) = parseHeader("foobar: ")
  280. test = newHttpHeaders()
  281. test[key] = value
  282. doAssert test["foobar"] == ""
  283. doAssert parseHeader("foobar:") == ("foobar", @[""])