Source Edit

This module implements helper procs for CGI applications. Example:

  1. import std/[strtabs, cgi]
  2. # Fill the values when debugging:
  3. when debug:
  4. setTestData("name", "Klaus", "password", "123456")
  5. # read the data into `myData`
  6. var myData = readData()
  7. # check that the data's variable names are "name" or "password"
  8. validateData(myData, "name", "password")
  9. # start generating content:
  10. writeContentType()
  11. # generate content:
  12. write(stdout, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n")
  13. write(stdout, "<html><head><title>Test</title></head><body>\n")
  14. writeLine(stdout, "your name: " & myData["name"])
  15. writeLine(stdout, "your password: " & myData["password"])
  16. writeLine(stdout, "</body></html>")

Imports

strutils, os, strtabs, cookies, uri

Types

  1. CgiError = object of IOError

Exception that is raised if a CGI error occurs. Source Edit

  1. RequestMethod = enum
  2. methodNone, ## no REQUEST_METHOD environment variable
  3. methodPost, ## query uses the POST method
  4. methodGet ## query uses the GET method

The used request method. Source Edit

Procs

  1. proc cgiError(msg: string) {.noreturn, ...raises: [CgiError], tags: [], forbids: [].}

Raises a CgiError exception with message msg. Source Edit

  1. proc existsCookie(name: string): bool {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Checks if a cookie of name exists. Source Edit

  1. proc getContentLength(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the CONTENT_LENGTH environment variable. Source Edit

  1. proc getContentType(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the CONTENT_TYPE environment variable. Source Edit

  1. proc getCookie(name: string): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Gets a cookie. If no cookie of name exists, “” is returned. Source Edit

  1. proc getDocumentRoot(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the DOCUMENT_ROOT environment variable. Source Edit

  1. proc getGatewayInterface(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the GATEWAY_INTERFACE environment variable. Source Edit

  1. proc getHttpAccept(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the HTTP_ACCEPT environment variable. Source Edit

  1. proc getHttpAcceptCharset(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the HTTP_ACCEPT_CHARSET environment variable. Source Edit

  1. proc getHttpAcceptEncoding(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the HTTP_ACCEPT_ENCODING environment variable. Source Edit

  1. proc getHttpAcceptLanguage(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the HTTP_ACCEPT_LANGUAGE environment variable. Source Edit

  1. proc getHttpConnection(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the HTTP_CONNECTION environment variable. Source Edit

  1. proc getHttpCookie(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the HTTP_COOKIE environment variable. Source Edit

  1. proc getHttpHost(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the HTTP_HOST environment variable. Source Edit

  1. proc getHttpReferer(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the HTTP_REFERER environment variable. Source Edit

  1. proc getHttpUserAgent(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the HTTP_USER_AGENT environment variable. Source Edit

  1. proc getPathInfo(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the PATH_INFO environment variable. Source Edit

  1. proc getPathTranslated(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the PATH_TRANSLATED environment variable. Source Edit

  1. proc getQueryString(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the QUERY_STRING environment variable. Source Edit

  1. proc getRemoteAddr(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the REMOTE_ADDR environment variable. Source Edit

  1. proc getRemoteHost(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the REMOTE_HOST environment variable. Source Edit

  1. proc getRemoteIdent(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the REMOTE_IDENT environment variable. Source Edit

  1. proc getRemotePort(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the REMOTE_PORT environment variable. Source Edit

  1. proc getRemoteUser(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the REMOTE_USER environment variable. Source Edit

  1. proc getRequestMethod(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the REQUEST_METHOD environment variable. Source Edit

  1. proc getRequestURI(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the REQUEST_URI environment variable. Source Edit

  1. proc getScriptFilename(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the SCRIPT_FILENAME environment variable. Source Edit

  1. proc getScriptName(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the SCRIPT_NAME environment variable. Source Edit

  1. proc getServerAddr(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the SERVER_ADDR environment variable. Source Edit

  1. proc getServerAdmin(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the SERVER_ADMIN environment variable. Source Edit

  1. proc getServerName(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the SERVER_NAME environment variable. Source Edit

  1. proc getServerPort(): string {....raises: [], tags: [ReadEnvEffect], forbids: [].}

Returns contents of the SERVER_PORT environment variable. Source Edit

  1. proc getServerProtocol(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the SERVER_PROTOCOL environment variable. Source Edit

  1. proc getServerSignature(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the SERVER_SIGNATURE environment variable. Source Edit

  1. proc getServerSoftware(): string {....raises: [], tags: [ReadEnvEffect],
  2. forbids: [].}

Returns contents of the SERVER_SOFTWARE environment variable. Source Edit

  1. proc readData(allowedMethods: set[RequestMethod] = {methodNone, methodPost,
  2. methodGet}): StringTableRef {....raises: [CgiError, ValueError, IOError],
  3. tags: [ReadEnvEffect, ReadIOEffect],
  4. forbids: [].}

Reads CGI data. If the client does not use a method listed in the allowedMethods set, a CgiError exception is raised. Source Edit

  1. proc readData(data: string): StringTableRef {....raises: [], tags: [], forbids: [].}

Reads CGI data from a string. Source Edit

  1. proc setCookie(name, value: string) {....raises: [IOError], tags: [WriteIOEffect],
  2. forbids: [].}

Sets a cookie. Source Edit

  1. proc setStackTraceStdout() {....raises: [], tags: [], forbids: [].}

Makes Nim output stacktraces to stdout, instead of server log. Source Edit

  1. proc setTestData(keysvalues: varargs[string]) {....raises: [OSError],
  2. tags: [WriteEnvEffect], forbids: [].}

Fills the appropriate environment variables to test your CGI application. This can only simulate the ‘GET’ request method. keysvalues should provide embedded (name, value)-pairs. Example:

  1. setTestData("name", "Hanz", "password", "12345")

Source Edit

  1. proc validateData(data: StringTableRef; validKeys: varargs[string]) {.
  2. ...raises: [CgiError], tags: [], forbids: [].}

Validates data; raises CgiError if this fails. This checks that each variable name of the CGI data occurs in the validKeys array. Source Edit

  1. proc writeContentType() {....raises: [IOError], tags: [WriteIOEffect], forbids: [].}

Calls this before starting to send your HTML data to stdout. This implements this part of the CGI protocol:

  1. write(stdout, "Content-type: text/html\n\n")

Source Edit

  1. proc writeErrorMessage(data: string) {....raises: [IOError], tags: [WriteIOEffect],
  2. forbids: [].}

Tries to reset browser state and writes data to stdout in <plaintext> tag. Source Edit

  1. proc xmlEncode(s: string): string {....raises: [], tags: [], forbids: [].}

Encodes a value to be XML safe:

  • “ is replaced by "
  • < is replaced by <
  • > is replaced by >
  • & is replaced by &
  • every other character is carried over.

Source Edit

Iterators

  1. iterator decodeData(allowedMethods: set[RequestMethod] = {methodNone,
  2. methodPost, methodGet}): tuple[key, value: string] {.
  3. ...raises: [CgiError, ValueError, IOError],
  4. tags: [ReadEnvEffect, ReadIOEffect], forbids: [].}

Reads and decodes CGI data and yields the (name, value) pairs the data consists of. If the client does not use a method listed in the allowedMethods set, a CgiError exception is raised. Source Edit

  1. iterator decodeData(data: string): tuple[key, value: string] {....raises: [],
  2. tags: [], forbids: [].}

Reads and decodes CGI data and yields the (name, value) pairs the data consists of. Source Edit

Exports

encodeUrl, decodeUrl