httpcore

Contains functionality shared between the httpclient and asynchttpserver modules.

Unstable API.

Imports

tables, strutils, parseutils

Types

HttpHeaders = ref object
  table*: TableRef[string, seq[string]]
  isTitleCase: bool
Source Edit
HttpHeaderValues = distinct seq[string]
Source Edit
HttpCode = distinct range[0 .. 599]
Source Edit
HttpVersion = enum
  HttpVer11, HttpVer10
Source Edit
HttpMethod = enum
  HttpHead, ## Asks for the response identical to the one that would
             ## correspond to a GET request, but without the response
             ## body.
  HttpGet,                  ## Retrieves the specified resource.
  HttpPost,                 ## Submits data to be processed to the identified
                             ## resource. The data is included in the body of the
                             ## request.
  HttpPut,                  ## Uploads a representation of the specified resource.
  HttpDelete,               ## Deletes the specified resource.
  HttpTrace,                ## Echoes back the received request, so that a client
                             ## can see what intermediate servers are adding or
                             ## changing in the request.
  HttpOptions,              ## Returns the HTTP methods that the server supports
                             ## for specified address.
  HttpConnect,              ## Converts the request connection to a transparent
                             ## TCP/IP tunnel, usually used for proxies.
  HttpPatch                  ## Applies partial modifications to a resource.
the requested HttpMethod Source Edit

Consts

Http100 = 100
Source Edit
Http101 = 101
Source Edit
Http200 = 200
Source Edit
Http201 = 201
Source Edit
Http202 = 202
Source Edit
Http203 = 203
Source Edit
Http204 = 204
Source Edit
Http205 = 205
Source Edit
Http206 = 206
Source Edit
Http300 = 300
Source Edit
Http301 = 301
Source Edit
Http302 = 302
Source Edit
Http303 = 303
Source Edit
Http304 = 304
Source Edit
Http305 = 305
Source Edit
Http307 = 307
Source Edit
Http308 = 308
Source Edit
Http400 = 400
Source Edit
Http401 = 401
Source Edit
Http403 = 403
Source Edit
Http404 = 404
Source Edit
Http405 = 405
Source Edit
Http406 = 406
Source Edit
Http407 = 407
Source Edit
Http408 = 408
Source Edit
Http409 = 409
Source Edit
Http410 = 410
Source Edit
Http411 = 411
Source Edit
Http412 = 412
Source Edit
Http413 = 413
Source Edit
Http414 = 414
Source Edit
Http415 = 415
Source Edit
Http416 = 416
Source Edit
Http417 = 417
Source Edit
Http418 = 418
Source Edit
Http421 = 421
Source Edit
Http422 = 422
Source Edit
Http426 = 426
Source Edit
Http428 = 428
Source Edit
Http429 = 429
Source Edit
Http431 = 431
Source Edit
Http451 = 451
Source Edit
Http500 = 500
Source Edit
Http501 = 501
Source Edit
Http502 = 502
Source Edit
Http503 = 503
Source Edit
Http504 = 504
Source Edit
Http505 = 505
Source Edit
httpNewLine = "\c\n"
Source Edit
headerLimit = 10000
Source Edit

Procs

proc clear(headers: HttpHeaders) {...}{.inline, raises: [], tags: [].}
Source Edit
proc `[]=`(headers: HttpHeaders; key, value: string) {...}{.raises: [], tags: [].}
Sets the header entries associated with key to the specified value. Replaces any existing values. Source Edit
proc `[]=`(headers: HttpHeaders; key: string; value: seq[string]) {...}{.raises: [],
    tags: [].}
Sets the header entries associated with key to the specified list of values. Replaces any existing values. If value is empty, deletes the header entries associated with key. Source Edit
proc add(headers: HttpHeaders; key, value: string) {...}{.raises: [KeyError],
    tags: [].}
Adds the specified value to the specified key. Appends to any existing values associated with the key. Source Edit
proc del(headers: HttpHeaders; key: string) {...}{.raises: [], tags: [].}
Deletes the header entries associated with key Source Edit
proc `==`(rawCode: string; code: HttpCode): bool {...}{.
    deprecated: "Deprecated since v1.2; use rawCode == $code instead",
    raises: [], tags: [].}
Deprecated: Deprecated since v1.2; use rawCode == $code instead
Compare the string form of the status code with a HttpCode
Note: According to HTTP/1.1 specification, the reason phrase is
optional and should be ignored by the client, making this proc only suitable for comparing the HttpCode against the string form of itself.
Source Edit

Funcs

func newHttpHeaders(titleCase = false): HttpHeaders {...}{.raises: [], tags: [].}
Returns a new HttpHeaders object. if titleCase is set to true, headers are passed to the server in title case (e.g. "Content-Length") Source Edit
func newHttpHeaders(keyValuePairs: openArray[tuple[key: string, val: string]];
                    titleCase = false): HttpHeaders {...}{.raises: [KeyError],
    tags: [].}
Returns a new HttpHeaders object from an array. if titleCase is set to true, headers are passed to the server in title case (e.g. "Content-Length") Source Edit
func `$`(headers: HttpHeaders): string {...}{.inline, raises: [], tags: [].}
Source Edit
func `[]`(headers: HttpHeaders; key: string): HttpHeaderValues {...}{.
    raises: [KeyError], tags: [].}

Returns the values associated with the given key. If the returned values are passed to a procedure expecting a string, the first value is automatically picked. If there are no values associated with the key, an exception is raised.

To access multiple values of a key, use the overloaded [] below or to get all of them access the table field directly.

Source Edit
func `[]`(headers: HttpHeaders; key: string; i: int): string {...}{.
    raises: [KeyError], tags: [].}
Returns the i'th value associated with the given key. If there are no values associated with the key or the i'th value doesn't exist, an exception is raised. Source Edit
func contains(values: HttpHeaderValues; value: string): bool {...}{.raises: [],
    tags: [].}
Determines if value is one of the values inside values. Comparison is performed without case sensitivity. Source Edit
func hasKey(headers: HttpHeaders; key: string): bool {...}{.raises: [], tags: [].}
Source Edit
func getOrDefault(headers: HttpHeaders; key: string;
                  default = @[""].HttpHeaderValues): HttpHeaderValues {...}{.
    raises: [KeyError], tags: [].}
Returns the values associated with the given key. If there are no values associated with the key, then default is returned. Source Edit
func len(headers: HttpHeaders): int {...}{.inline, raises: [], tags: [].}
Source Edit
func parseHeader(line: string): tuple[key: string, value: seq[string]] {...}{.
    raises: [], tags: [].}

Parses a single raw header HTTP line into key value pairs.

Used by asynchttpserver and httpclient internally and should not be used by you.

Source Edit
func `==`(protocol: tuple[orig: string, major, minor: int]; ver: HttpVersion): bool {...}{.
    raises: [], tags: [].}
Source Edit
func contains(methods: set[HttpMethod]; x: string): bool {...}{.raises: [ValueError],
    tags: [].}
Source Edit
func `$`(code: HttpCode): string {...}{.raises: [], tags: [].}
Converts the specified HttpCode into a HTTP status.

Example:

doAssert($Http404 == "404 Not Found")
Source Edit
func `==`(a, b: HttpCode): bool {...}{.borrow.}
Source Edit
func is2xx(code: HttpCode): bool {...}{.inline, raises: [], tags: [].}
Determines whether code is a 2xx HTTP status code. Source Edit
func is3xx(code: HttpCode): bool {...}{.inline, raises: [], tags: [].}
Determines whether code is a 3xx HTTP status code. Source Edit
func is4xx(code: HttpCode): bool {...}{.inline, raises: [], tags: [].}
Determines whether code is a 4xx HTTP status code. Source Edit
func is5xx(code: HttpCode): bool {...}{.inline, raises: [], tags: [].}
Determines whether code is a 5xx HTTP status code. Source Edit
func `$`(httpMethod: HttpMethod): string {...}{.raises: [], tags: [].}
Source Edit

Iterators

iterator pairs(headers: HttpHeaders): tuple[key, value: string] {...}{.raises: [],
    tags: [].}
Yields each key, value pair. Source Edit

Converters

converter toString(values: HttpHeaderValues): string {...}{.raises: [], tags: [].}
Source Edit

© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/httpcore.html