Documentation
¶
Overview ¶
Package cgi is a pure-Go (no cgo) reimplementation of the deterministic escaping and query-parsing surface of Ruby's CGI utility methods, byte-for-byte compatible with MRI 4.0.5 (the cgi/escape default + the cgi gem's CGI.parse).
It is the CGI backend for go-embedded-ruby, but is a standalone, reusable module with no dependency on the Ruby runtime — a sibling of go-ruby-yaml, go-ruby-regexp and go-ruby-erb.
Only the pure-compute surface lives here: URL/form escaping (Escape / Unescape), the URI-component variants (EscapeURIComponent / UnescapeURIComponent), the HTML-entity helpers (EscapeHTML / UnescapeHTML, with numeric &#NN; / &#xHH; decoding), the element helpers (EscapeElement / UnescapeElement), and query parsing (ParseQuery). The request/response/session machinery that needs a live server is out of scope.
Index ¶
- func Escape(s string) string
- func EscapeElement(s string, elements ...string) string
- func EscapeHTML(s string) string
- func EscapeURIComponent(s string) string
- func ParseQuery(query string) map[string][]string
- func Unescape(s string) string
- func UnescapeElement(s string, elements ...string) string
- func UnescapeHTML(s string) string
- func UnescapeURIComponent(s string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Escape ¶
Escape implements Ruby's CGI.escape: application/x-www-form-urlencoded encoding. Every byte outside the unreserved set is percent-encoded, and a space becomes '+'.
CGI.escape("a b&c") # => "a+b%26c"
func EscapeElement ¶
EscapeElement implements Ruby's CGI.escapeElement(string, *elements). It HTML-escapes (per EscapeHTML) only the start and end tags of the named elements, leaving the rest of the string untouched. Element names are matched case-insensitively at a word boundary, so EscapeElement("<A><AB>", "A") only touches the "<A>". When no elements are given the string is returned verbatim.
EscapeElement(`<BR><A HREF="url"></A>`, "A") // => `<BR><A HREF="url"></A>`
func EscapeHTML ¶
EscapeHTML implements Ruby's CGI.escapeHTML: it replaces '&', '<', '>', '"' and a single quote with their HTML entities (the single quote becoming "'").
It mirrors the table-driven native path of MRI's cgi/escape C extension: a single pass over the bytes copies each verbatim run in bulk and splices in the entity for each escapable byte, straight into one output buffer. Inputs with nothing to escape return the original string with no allocation at all.
CGI.escapeHTML(`<a href="x">&'`) # => `<a href="x">&'`
func EscapeURIComponent ¶
EscapeURIComponent implements Ruby's CGI.escapeURIComponent (added in the 3.5/4.0 era). It is like Escape but encodes a space as "%20" rather than '+'.
CGI.escapeURIComponent("a b") # => "a%20b"
func ParseQuery ¶
ParseQuery implements Ruby's CGI.parse(query). The query string is split on '&' and ';'; each pair is split on its first '='; key and value are Unescape-d (form decoding, so '+' is a space and "%XX" is decoded). Repeated keys accumulate their values in order. A pair with no '=' (e.g. "k") yields an empty value slice for that key. Ruby's CGI does NOT special-case "[]" in keys — the brackets are part of the key name verbatim.
ParseQuery("a=1&b=2&a=3") // => map[string][]string{"a":{"1","3"}, "b":{"2"}}
ParseQuery("x[]=1&x[]=2") // => map[string][]string{"x[]":{"1","2"}}
ParseQuery("k") // => map[string][]string{"k":{}}
func Unescape ¶
Unescape implements Ruby's CGI.unescape: it decodes "%XX" escapes and turns '+' into a space. Malformed escapes (a '%' not followed by two hex digits) are left exactly as-is — CGI.unescape never raises.
CGI.unescape("a+b%26c") # => "a b&c"
CGI.unescape("%zz%2") # => "%zz%2"
func UnescapeElement ¶
UnescapeElement implements Ruby's CGI.unescapeElement(string, *elements). It reverses EscapeElement: it HTML-unescapes (per UnescapeHTML) only the escaped start and end tags of the named elements, leaving the rest untouched. When no elements are given the string is returned verbatim.
UnescapeElement(EscapeHTML(`<BR><A HREF="url"></A>`), "A") // => `<BR><A HREF="url"></A>`
It faithfully reproduces Ruby's regexp
/<\/?(?:E…)\b(?>[^&]+|&(?![gl]t;)\w+;)*(?:>)?/im
by scanning for a tag head, then consuming the tag body (runs of non-'&' text or entity references other than >/<), then an optional closing >, and HTML-unescaping exactly that span.
func UnescapeHTML ¶
UnescapeHTML implements Ruby's CGI.unescapeHTML. It decodes the five named entities (amp, lt, gt, quot, apos), decimal numeric entities ("&#NN;") and hexadecimal numeric entities ("&#xHH;" / "&#XHH;"). A numeric code point is emitted as its raw UTF-8 byte sequence using Ruby's permissive encoder (so surrogate code points yield their three-byte form, as MRI does). Anything not recognised — an unknown name, an empty or overflowing number, a '&' without a terminating ';' — is left exactly as it appears.
Like MRI, the numeric decoding assumes a Unicode (UTF-8) receiver: it always produces the UTF-8 bytes of the code point.
The scan mirrors MRI's C decoder: it jumps '&' to '&' with IndexByte, copies each verbatim run in bulk, and decodes entities straight into the output buffer with no per-entity allocation (named entities resolve to a constant string, numeric ones write their UTF-8 bytes in place).
CGI.unescapeHTML("&AB") # => "&AB"
func UnescapeURIComponent ¶
UnescapeURIComponent implements Ruby's CGI.unescapeURIComponent. It is like Unescape but does NOT turn '+' into a space (only "%XX" is decoded).
CGI.unescapeURIComponent("a%20b+c") # => "a b+c"
Types ¶
This section is empty.
