type

WebUtil

src @Js
class WebUtil : Obj

WebUtil encapsulates several useful utility web methods. Also see MimeType and its utility methods.

methods

fromQuotedStr

Decode a HTTP quoted string according to RFC 2616 Section 2.2.

headersToCharset

Given a set of HTTP headers map Content-Type to its charset or default to UTF-8.

isToken

Return if the specified string is a valid HTTP token production which is any ASCII character which is not a control char or a separator.

isTokenChar

Return if given char unicode point is allowable within the HTTP token production.

jsMain

Generate the method invocation code used to boostrap into JavaScript from a webpage.

makeChunkedInStream

Wrap the given input stream to read bytes using a HTTP chunked transfer encoding.

makeChunkedOutStream

Wrap the given output stream to write bytes using a HTTP chunked transfer encoding.

makeContentInStream

Given a set of headers, wrap the specified input stream to read the content body

makeContentOutStream

Given a set of headers, wrap the specified output stream to write the content body

makeFixedInStream

Wrap the given input stream to read a fixed number of bytes.

makeFixedOutStream

Wrap the given output stream to write a fixed number of bytes.

parseHeaders

Parse a series of HTTP headers according to RFC 2616 section 4.2.

parseList

Parse a list of comma separated tokens.

parseMultiPart

Parse a multipart/form-data input stream.

parseQVals

Given an HTTP header that uses q values, return a map of name/q-value pairs.

toQuotedStr

Return the specified string as a HTTP quoted string according to RFC 2616 Section 2.2.

Slot Details

fromQuotedStr

src static Str fromQuotedStr(Str s)

Decode a HTTP quoted string according to RFC 2616 Section 2.2. The given string must be wrapped in quotes. See toQuotedStr.

headersToCharset

src static Charset headersToCharset(Str:Str headers)

Given a set of HTTP headers map Content-Type to its charset or default to UTF-8.

isToken

src static Bool isToken(Str s)

Return if the specified string is a valid HTTP token production which is any ASCII character which is not a control char or a separator. The separators characters are:

"(" | ")" | "<" | ">" | "@" |
"," | ";" | ":" | "\" | <"> |
"/" | "[" | "]" | "?" | "=" |
"{" | "}" | SP | HT

isTokenChar

src static Bool isTokenChar(Int c)

Return if given char unicode point is allowable within the HTTP token production. See isToken.

jsMain

src @Deprecated { msg="use WebOutStream.initJs" }
static Void jsMain(OutStream out, Str main, [Str:Str]? env := null)

Generate the method invocation code used to boostrap into JavaScript from a webpage. This must be called inside the <head> tag for the page. The main method will be invoked using the onLoad DOM event.

The main argument can be either a type or method. If no method is specified, main is used. If the method is not static, a new instance of type is created:

"foo::Instance"     =>  Instance().main()
"foo::Instance.bar" =>  Instance().bar()
"foo::Static"       =>  Static.main()
"foo::Static.bar"   =>  Static.bar()

If env is specified, then vars will be added to and available from Env.vars on client-side.

makeChunkedInStream

src static InStream makeChunkedInStream(InStream in)

Wrap the given input stream to read bytes using a HTTP chunked transfer encoding. The wrapped streams provides a contiguous stream of bytes until the last chunk is read. Closing the wrapper stream does not close the underlying stream.

makeChunkedOutStream

src static OutStream makeChunkedOutStream(OutStream out)

Wrap the given output stream to write bytes using a HTTP chunked transfer encoding. Closing the wrapper stream terminates the chunking, but does not close the underlying stream.

makeContentInStream

src static InStream makeContentInStream(Str:Str headers, InStream in)

Given a set of headers, wrap the specified input stream to read the content body:

  1. If Content-Encoding is gzip then wrap via Zip.gzipInStream
  2. If Content-Length then makeFixedInStream
  3. If Transfer-Encoding is chunked then makeChunkedInStream
  4. If Content-Type assume non-pipelined connection and return in directly

If a stream is returned, then it is automatically configured with the correct content encoding based on the Content-Type.

makeContentOutStream

src static OutStream? makeContentOutStream(Str:Str headers, OutStream out)

Given a set of headers, wrap the specified output stream to write the content body:

  1. If Content-Length then makeFixedOutStream
  2. If Content-Type then set Transfer-Encoding header to chunked and return makeChunkedOutStream
  3. Assume no content and return null

If a stream is returned, then it is automatically configured with the correct content encoding based on the Content-Type.

makeFixedInStream

src static InStream makeFixedInStream(InStream in, Int fixed)

Wrap the given input stream to read a fixed number of bytes. Once fixed bytes have been read from the underlying input stream, the wrapped stream will return end-of-stream. Closing the wrapper stream does not close the underlying stream.

makeFixedOutStream

src static OutStream makeFixedOutStream(OutStream out, Int fixed)

Wrap the given output stream to write a fixed number of bytes. Once fixed bytes have been written, attempting to further bytes will throw IOErr. Closing the wrapper stream does not close the underlying stream.

parseHeaders

src static Str:Str parseHeaders(InStream in)

Parse a series of HTTP headers according to RFC 2616 section 4.2. The final CRLF which terminates headers is consumed with the stream positioned immediately following. The headers are returned as a case insensitive map. Throw ParseErr if headers are malformed.

parseList

src static Str[] parseList(Str s)

Parse a list of comma separated tokens. Any leading or trailing whitespace is trimmed from the list of tokens.

parseMultiPart

src static Void parseMultiPart(InStream in, Str boundary, |Str:Str,InStream| cb)

Parse a multipart/form-data input stream. For each part in the stream call the given callback function with the part's headers and an input stream used to read the part's body. Each callback must completely drain the input stream to prepare for the next part. Also see WebReq.parseMultiPartForm.

parseQVals

src static Str:Float parseQVals(Str s)

Given an HTTP header that uses q values, return a map of name/q-value pairs. This map has a def value of 0.

Example:

compress,gzip              =>  ["compress":1f, "gzip":1f]
compress;q=0.5,gzip;q=0.0  =>  ["compress":0.5f, "gzip":0.0f]

toQuotedStr

src static Str toQuotedStr(Str s)

Return the specified string as a HTTP quoted string according to RFC 2616 Section 2.2. The result is wrapped in quotes. Throw ArgErr if any character is outside of the ASCII range of 0x20 to 0x7e. The quote char itself is backslash escaped. See fromQuotedStr.