Module ringo/utils/http
Functions
- BufferFactory(data, encoding)
- TempFileFactory(data, encoding)
- getMimeParameter(headerValue, paramName)
- isFileUpload(contentType)
- isUrlEncoded(contentType)
- mergeParameter(params, name, value)
- parseFileUpload(request, params, encoding, streamFactory)
- parseParameters(input, params, encoding)
- setCookie(key, value, days, options)
- urlEncode(object)
Class Headers
Instance Methods
BufferFactory (data, encoding)
A stream factory that stores file upload in a memory buffer. This function is not meant to be called directly but to be passed as streamFactory argument to parseFileUpload().
The buffer is stored in the value
property of the parameter's data object.
Parameters
Object | data | |
String | encoding |
Headers (headers)
Returns an object for use as a HTTP header collection. The returned object provides methods for setting, getting, and deleting its properties in a case-insensitive and case-preserving way.
This function can be used as mixin for an existing JavaScript object or as a constructor.
Parameters
Object | headers | an existing JS object. If undefined, a new object is created |
Headers.prototype. add (name, value)
Add a header with the given name and value.
Parameters
String | name | the header name |
String | value | the header value |
Headers.prototype. contains (name)
Queries whether a header with the given name is set
Parameters
String | name | the header name |
Returns
Boolean | true if a header with this name is set |
Headers.prototype. get (name)
Get the value of the header with the given name
Parameters
String | name | the header name |
Returns
the header value |
Headers.prototype. set (name, value)
Set the header with the given name to the given value.
Parameters
String | name | the header name |
String | value | the header value |
Headers.prototype. toString ()
Returns a string representation of the headers in MIME format.
Returns
String | a string representation of the headers |
Headers.prototype. unset (name)
Unsets any cookies with the given name
Parameters
String | name | the header name |
ResponseFilter (body, filter)
A utility class for implementing JSGI response filters. Each part of the response is first passed to the filter function. If the filter function returns a value, that value is passed on to the JSGI response stream.
Parameters
Object | body | a JSGI response body |
Function | filter | a filter function |
ResponseFilter.prototype. forEach (fn)
forEach function called by the JSGI connector.
Parameters
Function | fn | the response handler callback function |
TempFileFactory (data, encoding)
A stream factory that stores file uploads in temporary files. This function is not meant to be called directly but to be passed as streamFactory argument to parseFileUpload().
The name of the temporary file is stored in the tempfile
property
of the parameter's data object.
Parameters
Object | data | |
String | encoding |
getMimeParameter (headerValue, paramName)
Get a parameter from a MIME header value. For example, calling this function with "Content-Type: text/plain; charset=UTF-8" and "charset" will return "UTF-8".
Parameters
String | headerValue | a header value |
String | paramName | a MIME parameter name |
isFileUpload (contentType)
Find out whether the content type denotes a format this module can parse.
Parameters
String | contentType | a HTTP request Content-Type header |
Returns
true if the content type can be parsed as form data by this module |
isUrlEncoded (contentType)
Find out whether the content type denotes a format this module can parse.
Parameters
String | contentType | a HTTP request Content-Type header |
Returns
true if the content type can be parsed as form data by this module |
mergeParameter (params, name, value)
Adds a value to a parameter object using a square bracket property syntax.
For example, parameter foo[bar][][baz]=hello
will result in
object structure {foo: {bar: [{baz : "hello"}]}}
.
Parameters
Object | params | the top level parameter object |
String | name | the parameter name |
String | value | the parameter value |
parseFileUpload (request, params, encoding, streamFactory)
Parses a multipart MIME input stream. Parses a multipart MIME input stream.
Parameters
Object | request | the JSGI request object |
Object | params | the parameter object to parse into. If not defined a new object is created and returned. |
string | encoding | optional encoding to apply to non-file parameters. Defaults to "UTF-8". |
function | streamFactory | factory function to create streams for mime parts |
Returns
Object | the parsed parameter object |
parseParameters (input, params, encoding)
Parse a string or binary object representing a query string or post data into a JavaScript object structure using the specified encoding.
Parameters
Binary|String | input | a Binary object or string containing the URL-encoded parameters |
Object | params | optional parameter object to parse into. If undefined a new object is created and returned. |
String | encoding | a valid encoding name, defaults to UTF-8 |
Returns
the parsed parameter object |
setCookie (key, value, days, options)
Creates value for the Set-Cookie header for creating a cookie with the given name, value, and attributes.
All arguments except for key and value are optional. The days argument specifies the number of days until the cookie expires. To delete a cookie immediately, set the days argument to 0. If days is undefined or negative, the cookie is set for the current browser session.
Example
setCookie("username", "michi");
setCookie("password", "strenggeheim", 10,
{path: "/mypath", domain: ".mydomain.org"});
Parameters
String | key | the cookie name |
String | value | the cookie value |
Number | days | optional the number of days to keep the cookie. If this is undefined or -1, the cookie is set for the current session. If this is 0, the cookie will be deleted immediately. |
Object | options | optional options argument which may contain the following properties:
|
Returns
String | the Set-Cookie header value |
urlEncode (object)
Encode an object's properties into an URL encoded string.
Parameters
Object | object | an object |
Returns
String | a string containing the URL encoded properties of the object |