This module implements helper procs for CGI applications. Example:
import strtabs, cgi # Fill the values when debugging: when debug: setTestData("name", "Klaus", "password", "123456") # read the data into `myData` var myData = readData() # check that the data's variable names are "name" or "password" validateData(myData, "name", "password") # start generating content: writeContentType() # generate content: write(stdout, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n") write(stdout, "<html><head><title>Test</title></head><body>\n") writeLine(stdout, "your name: " & myData["name"]) writeLine(stdout, "your password: " & myData["password"]) writeLine(stdout, "</body></html>")
CgiError = object of IOError
RequestMethod = enum methodNone, ## no REQUEST_METHOD environment variable methodPost, ## query uses the POST method methodGet ## query uses the GET method
proc encodeUrl(s: string): string {.raises: [], tags: [].}
{'A'..'Z', 'a'..'z', '0'..'9', '_'}
are carried over to the result, a space is converted to '+'
and every other character is encoded as '%xx'
where xx
denotes its hexadecimal value. proc decodeUrl(s: string): string {.raises: [], tags: [].}
'+'
is converted to a space, '%xx'
(where xx
denotes a hexadecimal value) is converted to the character with ordinal number xx
, and and every other character is carried over. proc xmlEncode(s: string): string {.raises: [], tags: [].}
"
is replaced by "
<
is replaced by <
>
is replaced by >
&
is replaced by &
proc cgiError(msg: string) {.noreturn, raises: [CgiError], tags: [].}
proc readData(allowedMethods: set[RequestMethod] = {methodNone, methodPost, methodGet}): StringTableRef {. raises: [CgiError, OverflowError, ValueError, IOError], tags: [ReadEnvEffect, ReadIOEffect].}
proc validateData(data: StringTableRef; validKeys: varargs[string]) {. raises: [CgiError], tags: [].}
proc getContentLength(): string {.raises: [], tags: [ReadEnvEffect].}
CONTENT_LENGTH
environment variable proc getContentType(): string {.raises: [], tags: [ReadEnvEffect].}
CONTENT_TYPE
environment variable proc getDocumentRoot(): string {.raises: [], tags: [ReadEnvEffect].}
DOCUMENT_ROOT
environment variable proc getGatewayInterface(): string {.raises: [], tags: [ReadEnvEffect].}
GATEWAY_INTERFACE
environment variable proc getHttpAccept(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_ACCEPT
environment variable proc getHttpAcceptCharset(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_ACCEPT_CHARSET
environment variable proc getHttpAcceptEncoding(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_ACCEPT_ENCODING
environment variable proc getHttpAcceptLanguage(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_ACCEPT_LANGUAGE
environment variable proc getHttpConnection(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_CONNECTION
environment variable proc getHttpCookie(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_COOKIE
environment variable proc getHttpHost(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_HOST
environment variable proc getHttpReferer(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_REFERER
environment variable proc getHttpUserAgent(): string {.raises: [], tags: [ReadEnvEffect].}
HTTP_USER_AGENT
environment variable proc getPathInfo(): string {.raises: [], tags: [ReadEnvEffect].}
PATH_INFO
environment variable proc getPathTranslated(): string {.raises: [], tags: [ReadEnvEffect].}
PATH_TRANSLATED
environment variable proc getQueryString(): string {.raises: [], tags: [ReadEnvEffect].}
QUERY_STRING
environment variable proc getRemoteAddr(): string {.raises: [], tags: [ReadEnvEffect].}
REMOTE_ADDR
environment variable proc getRemoteHost(): string {.raises: [], tags: [ReadEnvEffect].}
REMOTE_HOST
environment variable proc getRemoteIdent(): string {.raises: [], tags: [ReadEnvEffect].}
REMOTE_IDENT
environment variable proc getRemotePort(): string {.raises: [], tags: [ReadEnvEffect].}
REMOTE_PORT
environment variable proc getRemoteUser(): string {.raises: [], tags: [ReadEnvEffect].}
REMOTE_USER
environment variable proc getRequestMethod(): string {.raises: [], tags: [ReadEnvEffect].}
REQUEST_METHOD
environment variable proc getRequestURI(): string {.raises: [], tags: [ReadEnvEffect].}
REQUEST_URI
environment variable proc getScriptFilename(): string {.raises: [], tags: [ReadEnvEffect].}
SCRIPT_FILENAME
environment variable proc getScriptName(): string {.raises: [], tags: [ReadEnvEffect].}
SCRIPT_NAME
environment variable proc getServerAddr(): string {.raises: [], tags: [ReadEnvEffect].}
SERVER_ADDR
environment variable proc getServerAdmin(): string {.raises: [], tags: [ReadEnvEffect].}
SERVER_ADMIN
environment variable proc getServerName(): string {.raises: [], tags: [ReadEnvEffect].}
SERVER_NAME
environment variable proc getServerPort(): string {.raises: [], tags: [ReadEnvEffect].}
SERVER_PORT
environment variable proc getServerProtocol(): string {.raises: [], tags: [ReadEnvEffect].}
SERVER_PROTOCOL
environment variable proc getServerSignature(): string {.raises: [], tags: [ReadEnvEffect].}
SERVER_SIGNATURE
environment variable proc getServerSoftware(): string {.raises: [], tags: [ReadEnvEffect].}
SERVER_SOFTWARE
environment variable proc setTestData(keysvalues: varargs[string]) {.raises: [OSError], tags: [WriteEnvEffect].}
setTestData("name", "Hanz", "password", "12345")
proc writeContentType() {.raises: [IOError], tags: [WriteIOEffect].}
write(stdout, "Content-type: text/html\n\n")
proc writeErrorMessage(data: string) {.raises: [IOError], tags: [WriteIOEffect].}
proc setStackTraceStdout() {.raises: [], tags: [].}
proc setStackTraceNewLine() {.deprecated, raises: [], tags: [].}
proc setCookie(name, value: string) {.raises: [IOError], tags: [WriteIOEffect].}
proc getCookie(name: string): TaintedString {.raises: [], tags: [ReadEnvEffect].}
proc existsCookie(name: string): bool {.raises: [], tags: [ReadEnvEffect].}
iterator decodeData(data: string): tuple[key, value: TaintedString] {. raises: [CgiError], tags: [].}
iterator decodeData(allowedMethods: set[RequestMethod] = {methodNone, methodPost, methodGet}): tuple[key, value: TaintedString] {. raises: [CgiError, OverflowError, ValueError, IOError], tags: [ReadEnvEffect, ReadIOEffect].}
© 2006–2017 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/cgi.html