cgi
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>") Imports
Types
CgiError = object of IOError
- exception that is raised if a CGI error occurs Source Edit
RequestMethod = enum methodNone, ## no REQUEST_METHOD environment variable methodPost, ## query uses the POST method methodGet ## query uses the GET method
- the used request method Source Edit
Procs
proc xmlEncode(s: string): string {...}{.raises: [], tags: [].}- 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.
-
proc cgiError(msg: string) {...}{.noreturn, raises: [CgiError], tags: [].}- raises an ECgi exception with message
msg. Source Edit proc readData(allowedMethods: set[RequestMethod] = {methodNone, methodPost, methodGet}): StringTableRef {...}{.raises: [CgiError, ValueError, IOError], tags: [ReadEnvEffect, ReadIOEffect].}- Read CGI data. If the client does not use a method listed in the
allowedMethodsset, anECgiexception is raised. Source Edit proc readData(data: string): StringTableRef {...}{.raises: [CgiError], tags: [].}- Read CGI data from a string. Source Edit
proc validateData(data: StringTableRef; validKeys: varargs[string]) {...}{. raises: [CgiError], tags: [].}- validates data; raises
ECgiif this fails. This checks that each variable name of the CGIdataoccurs in thevalidKeysarray. Source Edit proc getContentLength(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
CONTENT_LENGTHenvironment variable Source Edit proc getContentType(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
CONTENT_TYPEenvironment variable Source Edit proc getDocumentRoot(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
DOCUMENT_ROOTenvironment variable Source Edit proc getGatewayInterface(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
GATEWAY_INTERFACEenvironment variable Source Edit proc getHttpAccept(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_ACCEPTenvironment variable Source Edit proc getHttpAcceptCharset(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_ACCEPT_CHARSETenvironment variable Source Edit proc getHttpAcceptEncoding(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_ACCEPT_ENCODINGenvironment variable Source Edit proc getHttpAcceptLanguage(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_ACCEPT_LANGUAGEenvironment variable Source Edit proc getHttpConnection(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_CONNECTIONenvironment variable Source Edit proc getHttpCookie(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_COOKIEenvironment variable Source Edit proc getHttpHost(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_HOSTenvironment variable Source Edit proc getHttpReferer(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_REFERERenvironment variable Source Edit proc getHttpUserAgent(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
HTTP_USER_AGENTenvironment variable Source Edit proc getPathInfo(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
PATH_INFOenvironment variable Source Edit proc getPathTranslated(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
PATH_TRANSLATEDenvironment variable Source Edit proc getQueryString(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
QUERY_STRINGenvironment variable Source Edit proc getRemoteAddr(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
REMOTE_ADDRenvironment variable Source Edit proc getRemoteHost(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
REMOTE_HOSTenvironment variable Source Edit proc getRemoteIdent(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
REMOTE_IDENTenvironment variable Source Edit proc getRemotePort(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
REMOTE_PORTenvironment variable Source Edit proc getRemoteUser(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
REMOTE_USERenvironment variable Source Edit proc getRequestMethod(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
REQUEST_METHODenvironment variable Source Edit proc getRequestURI(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
REQUEST_URIenvironment variable Source Edit proc getScriptFilename(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SCRIPT_FILENAMEenvironment variable Source Edit proc getScriptName(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SCRIPT_NAMEenvironment variable Source Edit proc getServerAddr(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SERVER_ADDRenvironment variable Source Edit proc getServerAdmin(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SERVER_ADMINenvironment variable Source Edit proc getServerName(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SERVER_NAMEenvironment variable Source Edit proc getServerPort(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SERVER_PORTenvironment variable Source Edit proc getServerProtocol(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SERVER_PROTOCOLenvironment variable Source Edit proc getServerSignature(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SERVER_SIGNATUREenvironment variable Source Edit proc getServerSoftware(): string {...}{.raises: [], tags: [ReadEnvEffect].}- returns contents of the
SERVER_SOFTWAREenvironment variable Source Edit proc setTestData(keysvalues: varargs[string]) {...}{.raises: [OSError], tags: [WriteEnvEffect].}- fills the appropriate environment variables to test your CGI application. This can only simulate the 'GET' request method.
keysvaluesshould provide embedded (name, value)-pairs. Example:setTestData("name", "Hanz", "password", "12345")Source Edit proc writeContentType() {...}{.raises: [IOError], tags: [WriteIOEffect].}- call this before starting to send your HTML data to
stdout. This implements this part of the CGI protocol:write(stdout, "Content-type: text/html\n\n")
Source Edit proc writeErrorMessage(data: string) {...}{.raises: [IOError], tags: [WriteIOEffect].}- Tries to reset browser state and writes
datato stdout in <plaintext> tag. Source Edit proc setStackTraceStdout() {...}{.raises: [], tags: [].}- Makes Nim output stacktraces to stdout, instead of server log. Source Edit
proc setCookie(name, value: string) {...}{.raises: [IOError], tags: [WriteIOEffect].}- Sets a cookie. Source Edit
proc getCookie(name: string): TaintedString {...}{.raises: [], tags: [ReadEnvEffect].}- Gets a cookie. If no cookie of
nameexists, "" is returned. Source Edit proc existsCookie(name: string): bool {...}{.raises: [], tags: [ReadEnvEffect].}- Checks if a cookie of
nameexists. Source Edit
Iterators
iterator decodeData(data: string): tuple[key, value: TaintedString] {...}{. raises: [CgiError], tags: [].}- Reads and decodes CGI data and yields the (name, value) pairs the data consists of. Source Edit
iterator decodeData(allowedMethods: set[RequestMethod] = {methodNone, methodPost, methodGet}): tuple[key, value: TaintedString] {...}{. raises: [CgiError, ValueError, IOError], tags: [ReadEnvEffect, ReadIOEffect].}- 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
allowedMethodsset, anECgiexception is raised. Source Edit
Exports
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/cgi.html