nwg(3) | AFNIX Module | nwg(3) |
NAME¶
nwg - standard network working group moduleSTANDARD NETWORK WORKING GROUP MODULE¶
The Standard Network Working Group module is an original implemtation of the recommendations proposed by the NWG and currently found in the form of Request for Comments (RFC). Most of the objects are used with networking application, with the most common one beeing the Universal Resource Identifier (URI) object. The uri classconst uri (afnix:www:Uri "http://www.afnix.org")
const uri (afnix:www:Uri "http://www.afnix.org/") println (uri:get-scheme) # http println (uri:get-authority) # www.afnix.org println (uri:get-path) # /
const uri (afnix:www:Uri "http://www.afnix.org?name=hello&value=world") println (uri:get-query) # name=hello&value=world
# create a query string object const qs (afnix:nwg:UriQuery (uri:get-query)) # get the name value qs:get-value "name"
# check the cgi protocol assert "CGI/1.1" ( afnix:sys:get-env "GATEWAY_INTERFACE")
# check the cgi protocol assert "CGI/.1" ( afnix:sys:get-env "GATEWAY_INTERFACE") # initialize the query string const query (afnix:sys:get-env "QUERY_STRING") # get the request method const rqm (afnix:sys:get-env "REQUEST_METHOD") # check for a post request and update the query string if (== rqm "POST") { # create a buffer from the content length const len ( Integer (afnix:sys:get-env "CONTENT_LENGTH")) # get the standard input stream and read content const is (interp:get-input-stream) const buf (is:read len) # set the query string query:= (buf:to-string) }
const query ( afnix:www:UriQuery "name=hello&value=world") query:length # 2 query:get-value "name" # hello query:get-value "value" # world
# normalize a uri name trans unm "http://www.afnix.org" assert unm ( afnix:nwg:normalize-uri-name unm) assert unm ( afnix:nwg:normalize-uri-name "www.afnix.org") assert unm ( afnix:nwg:normalize-uri-name "//www.afnix.org")
# check a media type assert true (afnix:nwg:mime-value-p "text/plain") # check the mime extension predicate assert true (afnix:nwg:mime-extension-p "txt") # check the extension to mime assert "text/plain" ( afnix:nwg:extension-to-mime "txt")
#create a valid response const hr (afnix:nwg:HttpResponse 200)
# create a client response by stream const hr (afnix:nwg:HttpResponse is)
# check that a response is valid if (hr:status-ok-p) { # create a http stream const rs (afnix:nwg:HttpStream ht is) # read the response stream while (rs:eos-p) (rs:read) }
# create a cookie with name/value const cookie (afnix:nwg:Cookie "cartid" "123456789")
STANDARD NETWORK WORKING GROUP REFERENCE¶
Uriuri-p
Inheritance
Object
Constructors
Uri (none)
The Uri constructor creates an empty uri object.
Uri (String)
The Uri constructor create a uri object by value. The string argument is the uri
to parse at the object construction.
Uri (String String Integer)
The Uri constructor create a uri object by scheme host and port. The first
argument is the uri scheme. The second argument is the uri host name. The
third argument is the uri port. The uri base name can be reconstructed from
this information.
Methods
parse -> none (String)
The parse method reset the uri object, parse the string argument and fill the
uri object with the result.
get-scheme -> String (none)
The get-scheme method returns the scheme of the parsed uri object.
get-authority -> String (none)
The get-authority method returns the authority part of the parsed uri.
get-path -> String (none)
The get-path method returns the path of the parsed uri.
get-path-target -> String (none)
The get-path-target method returns the path target of the parsed uri. The path
target is the last element of the uri path.
get-query -> String (none)
The get-query method returns the complete query string of the parsed uri. Note
that characters are not escaped when getting the string.
get-fragment -> String (none)
The get-fragment method returns the complete query string of the parsed
uri.
get-base -> String (none)
The get-base method returns the combined uri scheme and authority.
get-rname -> String (none)
The get-rname method returns the reference uri name with the combined uri
scheme, authority and path all percent encoded.
get-hname -> String (none)
The get-hname method returns the combined uri scheme, authority and path.
get-aname -> String (none)
The get-aname method returns the almost combined uri name with the scheme,
authority, path and query.
add-path -> Uri (String)
The add-path method adds a path to the calling uri and returns a new uri with
the new path added to the old one.
get-href -> Uri (String)
The get-href method returns a new uri by eventually combining the string
argument. If the string argument correspond to an uri, the corresponding uri
is built. Otherwise, the string argument is considered as a path to be added
to the current uri in order to build a new uri.
get-system-path -> String (none)
The get-system-path method returns the system path representation of the uri
path. This function works only if the scheme if a file scheme.
get-path-encoded -> String (none)
The get-path-encoded method returns the uri in the encoded form. Normally the
get-path removes the percent-encoded characters which might not be appropriate
with some protocol such like the http protocol. The get-path-encoded returns
the original path. Note that getting the path with getpath and doing a percent
coding might result in a different result since the internal representation
uses normalized string.
get-host -> String (none)
The get-host method returns the authority or path host name if any can be found
with respect to the scheme. With a ftp, http or https scheme, the host is
extracted from the authority. With a mailto scheme, the host is extracted from
the path.
get-port -> Integer (none)
The get-port method returns the authority port if any can be found with respect
to the scheme.
UriQuery
uri-query-p
Inheritance
Plist
Constructors
UriQuery (none)
The UriQuery constructor creates an empty uri query object.
UriQuery (String)
The UriQuery constructor create a uri object by value. The string argument is
the uri query string to parse at the object construction. The query string is
the one obtained from the get-query method of the Uri class.
Methods
parse -> none (String)
The parse method reset the uri query object, parses the string argument and fill
the property list object with the result.
get-query -> String (none)
The get-query method returns the original query string.
Functions
mime-extension-p -> Boolean (String)
The mime-extension-p predicates returns true if a media type extension - mime
extension - is defined. Most of the time, media type extension can be seen as
a file extension.
mime-value-p -> Boolean (String)
The mime-value-p predicates returns true if a media type - mime value - is
defined.
extension-to-mime -> String (String
[Boolean])
The extension-to-mime function converts a media type extension into a media
type. In the first form, without a second argument, if the media type
extension does not exist, an exception is raised. In the second form, with the
second argument set to true, if the media type extension does not exist, the
default media type is returned. If the flag is set to false, an exception is
raised like the first form.
normalize-uri-name -> String
(String)
The normalize-uri-name function normalizes the string argument by adding a uri
scheme if missing in the original string. If the function detects that the
name starts with a host name, the "http" scheme is added. If the
function detects that the string starts with a path, the "file"
scheme is added. otherwise, the name argument is left untouched.
system-uri-name -> String (String)
The system-uri-name function normalizes the string argument by prioritizing the
system name. The function attempts to find a file that match the string
argument and eventually build a uri file scheme. If the file is not fond, the
normalization process occurs with the normalize-uri-name function.
HttpProto
http-proto-p
Inheritance
Object
Methods
reset -> none (none)
The reset method resets the http protocol object by clearing the protocol
version and header.
parse -> none (none)
The parse method parse the input stream bound to the http protocol. In order to
operate, an input stream must be associated with the protocol object or an
exception is raised. After a stream has been parsed, the protocol version and
the header are set.
write -> none
(none|OutputStream|Buffer)
The write method formats and writes the http protocol object to an output stream
or a buffer. Without argument, the default output stream is used. With an
argument, an output stream or a buffer object can be used.
header-length -> Integer (none)
The header-length method returns the number of properties in the header.
header-exists-p -> Boolean (String)
The header-exists-p predicate returns true if the property exists in the header.
The string argument is the property name.
header-set -> none (String Literal)
The header-set method sets a new property to the http header. The first argument
is the property name. The second argument is a literal object which is
internally converted to a string.
header-get -> Property (Integer)
The header-get method returns a property object by index.
header-map -> String (String)
The header-map method returns a property value by name. The string argument is
the property name.
header-find -> Property (String)
The header-find method returns a property object by name. The string argument is
the property name. If the property is not found, the nil object is
returned.
header-lookup -> Property (String)
The header-lookup method returns a property object by name. The string argument
is the property name. If the property is not found, an exception is
raised.
header-plist -> Plist (none)
The header-plist method returns the header in the form of a property list.
content-length-p -> Boolean (none)
The content-length-p predicate returns true if the content length is defined in
the protocol header.
get-content-length -> Integer (none)
The get-content-length method returns the content length defined in the protocol
header. If the content length is not defined in the header, the null value is
returned.
media-type-p -> Boolean (none)
The media-type-p predicate returns true if the content type is defined in the
protocol header.
get-media-type -> String (none)
The get-media-type method returns the media type defined in the protocol header.
If the media type is not defined in the header, the default media type is
returned.
encoding-mode-p -> Boolean (none)
The encoding-mode-p predicate returns true if the encoding mode is defined in
the protocol header.
get-encoding-mode -> String (none)
The get-encoding-mode method returns the protocol encoding mode. If the encoding
mode is not defined in the protocol header, the default encoding mode is
returned.
HttpRequest
http-request-p
Inheritance
HttpProto
Constructors
HttpRequest (none)
The HttpRequest constructor creates a default http request. By default, the
request object is built with the GET method and the request uri set to the
root value.
HttpRequest (String)
The HttpRequest constructor creates a http request object with a specific
command. By default, the request uri is set to root, except for the OPTIONS
method
HttpRequest (Uri)
The HttpRequest constructor creates a http request object with a uri. The
default request method is GET.
HttpRequest (InputStream)
The HttpRequest constructor creates a http request object with a specific input
stream. At construction, the request header is cleared and the input stream is
bound to the object.
HttpRequest (String String)
The HttpRequest constructor creates a http request object with a specific method
and a uri name. The first string argument is the request method to use. The
second string argument is the uri attached to the command. Note that the term
uri should be understood as a request uri.
HttpRequest (String Uri)
The HttpRequest constructor creates a http request object with a specific method
and a uri. The first string argument is the request method to use. The second
argument is the uri attached to the method.
Methods
set-method -> none (String)
The set-method method sets the request method. This method does not check that
the command is a valid HTTP method and thus leaves plenty of room for server
development. As a matter of fact, RFC 2616 does not prohibit the existence of
such extension.
get-method -> String (none)
The get-method method returns the request method string.
set-uri -> none (String)
The set-uri method sets the request uri. The argument string does not have to be
a valid uri string since some commands might accept special string such like
"*" to indicate all applicable uri.
get-uri -> String (none)
The get-uri method returns the request uri string.
HttpResponse
http-response-p
Inheritance
HttpProto
Constructors
HttpResponse (none)
The HttpResponse constructor creates a default http response object. The
response is marked valid with a default text/plain media type.
HttpResponse (Integer)
The HttpResponse constructor creates a http response object with a status code.
The response code is associated with the default text/plain media type.
HttpResponse (InputStream)
The HttpResponse constructor creates a http response object with a specific
input stream. At construction, the response header is cleared and the input
stream is bound to the object.
HttpResponse (Integer String)
The HttpResponse constructor creates a http response object with a status code
and a media type. The first argument is the status code. The second argument
is the associated media type.
Methods
set-status-code -> none (Integer)
The set-status-code method sets the response status code.
get-status-code -> Integer (none)
The get-status-code method returns the response status code.
map-status-code -> String (none)
The map-status-code method returns a string representation of the response
status code.
status-ok-p -> Boolean (none)
The status-ok-p predicate returns true if the response status code is valid (aka
status 200).
status-error-p -> Boolean (none)
The status-error-p predicate returns true if the response status code is an
error code.
location-p -> Boolean (none)
The location-p predicate returns true is the response status code indicates that
a request should be made at another location. The location can be found with
the get-location method.
get-location -> String (none)
The get-location method returns the location uri found in the response header.
This method is equivalent to a header query.
set-location -> none (String)
The set-location method set the redirect location in the response header. The
string argument is the location uri.
set-cookie -> none (Cookie)
The set-cookie method sets a cookie object to the http header. The cookie
version is properly handled by the method.
Cookie
cookie-p
Inheritance
Object
Constructors
Cookie (String String)
The Cookie constructor creates a cookie with a name value pair. The first
argument is the cookie name. The second argument is the cookie value.
Cookie (String String Integer)
The Cookie constructor creates a cookie with a name value pair and a maximum
age. The first argument is the cookie name. The second argument is the cookie
value. The third argument is the cookie maximum age.
Methods
get-version -> Integer (none)
The get-version method returns the cookie version.
set-version -> none (Integer)
The set-version method sets the cookie version. The version number can only be 0
or 1.
get-name -> String (none)
The get-name method returns the cookie name. This is the name store on the http
client.
set-name -> none (String)
The set-name method sets the cookie name. This is the name store on the http
client.
get-value -> String (none)
The get-value method returns the cookie value. This is the value stored on the
http client bounded by the cookie name.
set-value -> none (String)
The set-value method sets the cookie value. This is the value store on the http
client bounded by the cookie name.
get-maximum-age -> Integer (none)
The get-maximum-age method returns the cookie maximum age. The default value is
-1, that is, no maximum age is set and the cookie is valid only for the http
client session.
set-maximum-age -> none (Integer)
The set-maximum-age method sets the cookie maximum age. A negative value is
reset to -1. A 0 value tells the http client to discard the cookie. A positive
value tells the http client to store the cookie for the remaining
seconds.
get-path -> String (none)
The get-path method returns the cookie path value. The path determines for which
http request the cookie is valid.
set-path -> none (String)
The set-path method sets the cookie path value. The path determines for which
http request the cookie is valid.
get-domain -> String (none)
The get-domain method returns the cookie domain value.
set-domain -> none (String)
The set-domain method sets the cookie domain value. It is string recommended to
use the originator domain name since many http client can reject cookie those
domain name does not match the originator name.
get-port -> Integer (none)
The get-port method returns the cookie port number.
set-port -> none (Integer)
The set-port method sets the cookie port number. This value is not used with a
cookie version 0.
get-comment -> String (none)
The get-comment method returns the cookie comment value.
set-comment -> none (String)
The set-comment method sets the cookie comment value.
get-comment-url -> String (none)
The get-comment-url method returns the cookie comment url value.
set-comment-url -> none (String)
The set-comment-url method sets the cookie comment url value. This value is not
used with cookie version 0.
get-discard -> Boolean (none)
The get-discard method returns the cookie discard flag.
set-discard -> none (Boolean)
The set-discard method sets the cookie discard flag. The discard flag the tells
the user agent to destroy the cookie when it terminates. This value is not
used with cookie version 0.
get-secure -> Boolean (none)
The get-secure method returns the cookie secure flag.
set-secure -> none (Boolean)
The set-secure method sets the cookie secure flag. When a cookie is secured, it
is only returned by the http client if a connection has been secured (i.e use
https).
to-string -> String (none)
The to-string method returns a string formatted for the http reply header.
Normally this method should not be called since the set-cookie method of the
httpReply takes care of such thing.
Session
session-p
Inheritance
Serial
Constructors
Session (String)
The Session constructor creates a session by name. The string argument is the
session name.
Session (String String)
The Session constructor creates a session with a name and a user. The first
argument is the session name. The second argument is the user name.
Session (String String String)
The Session constructor creates a session with a name, a user and a path. The
first argument is the session name. The second argument is the session user
name. The third argument is the session path.
Methods
expire-p -> Boolean (none)
The expire-p predicate returns true if the session has expired.
get-name -> String (none)
The get-name method returns the session name.
set-hash-id -> none (String)
The set-hash-id method sets the session hash identifier. The session hash id
must be unique and secured enough so that the session name cannot be derived
from it.
get-hash-id -> String (none)
The get-hash-id method returns the session hash identifier.
set-user -> none (String)
The set-user method sets the session user name.
get-user -> String (none)
The get-user method returns the session user name.
set-path -> none (String)
The set-path method sets the session path.
get-path -> String (none)
The get-path method returns the session path.
set-maximum-age -> none (Integer)
The set-max-age method sets the session maximum age. The maximum age is an
integer in seconds relative to the current time.
mark-expired -> none (none)
The mark-expired method marks the session as expired. This is done by forcing
the expiration time to the creation time.q
get-valid-time -> Integer (none)
The get-valid-time method returns the remaining valid session time.
get-expire-time -> Integer (none)
The get-expire-time method returns the session expiration time in seconds. The
expiration time is an absolute time.
set-expire-time -> none (Integer)
The set-expire-time method sets the session expiration time. The expiration time
is an absolute time in seconds.
get-creation-time -> Integer (none)
The get-creation-time method returns the session creation time. The creation
time is an absolute time in seconds.
get-modification-time -> Integer
(none)
The get-modification-time method returns the session creation time. The
modification time is an absolute time in seconds.
2012-03-26 | AFNIX |