mike/helpers/context

Search:
Group by:
Source   Edit  

Consts

maxReadAllBytes {.intdefine.} = 10000000
Max size in bytes before streaming the file to the client Source   Edit  

Procs

func allowsBody(ctx: Context): bool {.inline, ...raises: [], tags: [], forbids: [].}
Returns true if the request allows a body to be sent Source   Edit  
proc beenModified(ctx: Context; modDate: DateTime = now()): bool {.
    ...raises: [TimeParseError, KeyError], tags: [TimeEffect], forbids: [].}
Returns true if modDate is newer than If-Modified-Since in the request. Source   Edit  
proc closed(ctx: Context): bool {.inline, ...raises: [], tags: [], forbids: [].}
Returns true if the client has disconnected from the server Source   Edit  
proc requestRange(ctx: Context): tuple[start, finish: Option[int]] {.
    ...raises: [KeyError, ValueError], tags: [], forbids: [].}
Returns start and end positions for a range request. Range requests are still valid if either start or finish don't exist. But if both don't exist then the request is invalid. This currently only supports single range requests Source   Edit  
proc send(ctx: Context; body: sink string; code: HttpCode;
          extraHeaders: HttpHeaders = nil) {....raises: [ValueError],
    tags: [UseResponse], forbids: [].}
Responds to a context and overwrites the status code. If responding to a HEAD or OPTIONS request then the body isn't send (But the Content-Length is set) Source   Edit  
proc send(ctx: Context; body: sink string; extraHeaders: HttpHeaders = nil) {.
    ...raises: [ValueError], tags: [UseResponse], forbids: [].}
Responds to a context with body and does not overwrite the current status code Source   Edit  
proc send(ctx: Context; code: HttpCode; extraHeaders: HttpHeaders = nil) {.
    ...raises: [ValueError], tags: [UseResponse], forbids: [].}
Responds with just a status code. Ignores the current response body Source   Edit  
proc send(ctx: Context; json: sink JsonNode; code = Http200;
          extraHeaders: HttpHeaders = nil) {....raises: [ValueError],
    tags: [UseResponse], forbids: [].}
Responds with JSON to the client. Automatically sets the Content-Type header to "application/json" Source   Edit  
proc send(ctx: Context; prob: ProblemResponse; extraHeaders: HttpHeaders = nil) {.
    ...raises: [ValueError], tags: [UseResponse], forbids: [].}
Sends a problem response back. Automatically sets the response code to the one specifed in prob Source   Edit  
proc send[T: object | ref object | array | seq | set](ctx: Context; obj: sink T;
    code = Http200; extraHeaders: HttpHeaders = nil) {.inline.}
Responds to a context in json format with obj T. Automatically sets the Content-Type header to "application/json" Source   Edit  
proc sendChunk(ctx: Context; data: sink string) {....raises: [], tags: [],
    forbids: [].}
Sends a chunk to the client. Send an empty chunk when are finished Source   Edit  
proc sendCompressed(ctx: Context; body: sink string;
                    compression: CompressedDataFormat; code = Http200;
                    extraHeaders: HttpHeaders = nil) {.
    ...raises: [ValueError, ZippyError], tags: [UseResponse], forbids: [].}
Sends body but compresses it with compression. Currently only dfGzip and dfDeflate are supported. Compresses even if the client doesn't say they support the compression Source   Edit  
proc sendCompressed(ctx: Context; body: sink string; code = Http200;
                    extraHeaders: HttpHeaders = nil) {.
    ...raises: [KeyError, ValueError, ZippyError], tags: [UseResponse], forbids: [].}
Sends body and trys to compress it. Checks Accept-Encoding header to see what it can compress with. Doesn't compress if nothing in Accept-Encoding is implemented or the header is missing Source   Edit  
proc sendEvent(ctx: Context; event, data: string) {....raises: [], tags: [],
    forbids: [].}
Sends an event with associated data. event isn't sent if it's empty (Body is still sent though) Source   Edit  
proc sendFile(ctx: Context; filename: string; dir = ".";
              headers: HttpHeaders = nil; downloadName = ""; charset = "utf-8";
              bufsize = 4096; allowRanges = false): owned(Future[void]) {.
    ...stackTrace: false, raises: [Exception, NotFoundError, OSError,
                                ForbiddenError, TimeParseError, KeyError,
                                ValueError, RangeNotSatisfiableError,
                                ZippyError, IOError],
    tags: [RootEffect, ReadDirEffect, UseResponse, TimeEffect, ReadIOEffect],
    forbids: [].}
Responds to a context with a file.
  • allowRanges: Whether to support range requests. Only use this if there is little processing before sending the file
  • dir: Base directory. Does not allow accessing files from outside of this folder
Source   Edit  
proc sendPartial(ctx: Context; data: sink string) {.inline, ...raises: [],
    tags: [], forbids: [].}
Sends some data directly to the client. You must have called startStreaming or send first So that the client has recieved headers and status code. Source   Edit  
proc setContentType(ctx: Context; fileName: string) {....raises: [],
    tags: [UseResponse], forbids: [].}
Sets the content type to be for fileName e.g. "index.html" will set "Content-Type" header to "text/html" Source   Edit  
proc startChunking(ctx: Context) {....raises: [ValueError], tags: [UseResponse],
                                   forbids: [].}
Allows you to start sending chunks back to the client. Use this if streaming a large response to the client Source   Edit  
proc startSSE(ctx: Context; retry = 3000) {....raises: [ValueError],
    tags: [UseResponse], forbids: [].}
Allows you to start sending server sent events to the client.
  • retry: How quickly in milliseconds the client should try and reconnect
Source   Edit  
proc startStreaming(ctx: Context; contentLength = none(int)) {.inline,
    ...raises: [ValueError], tags: [UseResponse], forbids: [].}
Sends everything but the body to the client. Allows you to start streaming content to the client. Source   Edit  
proc stopSSE(ctx: Context) {....raises: [], tags: [], forbids: [].}
Tells the client that the events have stopped Source   Edit  
proc supportedCompression(ctx: Context): Option[CompressedDataFormat] {.
    ...raises: [KeyError], tags: [], forbids: [].}
Returns the compression that is supported by the context. If it doesn't support any compression then none is returned Source   Edit