netkit/http/reader

This module provides an abstraction of read operations related to HTTP.

Overview

A server reads the incoming request from a client, and a client reads the returned response from a server.

HttpReader is a base object for read operations, ServerRequest and ClientResponse inherit from it. ServerRequest represents a incoming request from a client, and ClientResponse represents a returned response from a server.

Types

HttpReader = ref object of RootObj
  conn: HttpConnection
  lock: AsyncLock
  header*: HttpHeader
  metadata: HttpMetadata
  onEnd: proc () {...}{.gcsafe, closure.}
  contentLen: Natural
  chunked: bool
  readable: bool
An abstraction of read operations related to HTTP.   Source Edit
ServerRequest = ref object of HttpReader
Represents a incoming request from a client.   Source Edit
ClientResponse = ref object of HttpReader
Represents a returned response from a server.   Source Edit

Procs

proc newServerRequest(conn: HttpConnection; onEnd: proc () {...}{.gcsafe, closure.}): ServerRequest {...}{.
    raises: [], tags: [].}
Creates a new ServerRequest.   Source Edit
proc newClientResponse(conn: HttpConnection; onEnd: proc () {...}{.gcsafe, closure.}): ClientResponse {...}{.
    raises: [], tags: [].}
Creates a new ClientResponse.   Source Edit
proc reqMethod(req: ServerRequest): HttpMethod {...}{.inline, raises: [], tags: [].}
Returns the request method.   Source Edit
proc url(req: ServerRequest): string {...}{.inline, raises: [], tags: [].}
Returns the url.   Source Edit
proc status(res: ClientResponse): HttpCode {...}{.inline, raises: [], tags: [].}
Returns the status code.   Source Edit
proc version(reader: HttpReader): HttpVersion {...}{.inline, raises: [], tags: [].}
Returns the HTTP version.   Source Edit
proc fields(reader: HttpReader): HeaderFields {...}{.inline, raises: [], tags: [].}
Returns the header fields.   Source Edit
proc metadata(reader: HttpReader): HttpMetadata {...}{.inline, raises: [], tags: [].}
Returns the metadata.   Source Edit
proc ended(reader: HttpReader): bool {...}{.inline, raises: [], tags: [].}
Returns true if the underlying connection has been disconnected or no more data can be read.   Source Edit
proc normalizeSpecificFields(reader: HttpReader) {...}{.
    raises: [KeyError, HttpError, ValueError], tags: [].}
Normalizes a few special header fields.   Source Edit
proc read(reader: HttpReader; buf: pointer;
         size: range[int(LimitChunkDataLen) .. high(int)]): Future[Natural] {...}{.
    raises: [Exception, ValueError, FutureError], tags: [RootEffect].}

Reads up to size bytes, storing the results in the buf.

The return value is the number of bytes actually read. This might be less than size. A value of zero indicates EOF, i.e. no more data can be read.

If a system error occurs during reading, an OsError will be raised. If the connection is disconnected before successful reading, a ReadAbortedError will be raised.

  Source Edit
proc read(reader: HttpReader): Future[string] {...}{.
    raises: [Exception, ValueError, FutureError], tags: [RootEffect].}

Reads up to size bytes, storing the results as a string.

If the return value is "", that indicates eof, i.e. at the end of the request.

If a system error occurs during reading, an OsError will be raised. If the connection is disconnected before successful reading, a ReadAbortedError will be raised.

  Source Edit
proc readAll(reader: HttpReader): Future[string] {...}{.
    raises: [Exception, ValueError, FutureError], tags: [RootEffect].}

Reads all bytes, storing the results as a string.

If a system error occurs during reading, an OsError will be raised. If the connection is disconnected before successful reading, a ReadAbortedError will be raised.

  Source Edit
proc readDiscard(reader: HttpReader): Future[void] {...}{.
    raises: [Exception, FutureError], tags: [RootEffect].}

Reads all bytes, discarding the results.

If the return future is failed, OsError or ReadAbortedError may be raised.

  Source Edit