CLASSHTTPD::MIME
A metaclass for MIME handling behavior across a live socket
Methods
method ChannelCopyinout ?args?
method html_header ?title ? ?args?
Returns a block of HTML
method html_footer ?args?
method http_code_stringcode
method HttpHeaderssock ?debug ?
method HttpHeaders_Default
method HttpServerHeaders
method MimeParsemimetext
Converts a block of mime encoded text to a key/value list. If an exception is encountered, the
method will generate its own call to the error method, and immediately invoke the output method to
produce an error code and close the connection.
method Url_Decodedata
De-httpizes a string.
method Url_PathCheckurlsuffix
method waitmodesockCLASSHTTPD::REPLYancestors: httpd::mime
A class which shephards a request through the process of generating a reply. The socket associated with
the reply is available at all times as the chan variable. The process of generating a reply begins with
an httpd::server generating a http::class object, mixing in a set of behaviors and then invoking the
reply object's dispatch method. In normal operations the dispatch method:
[1] Invokes the reset method for the object to populate default headers.
[2] Invokes the HttpHeaders method to stream the MIME headers out of the socket
[3] Invokes the requestparse method to convert the stream of MIME headers into a dict that can be
read via the request method.
[4] Stores the raw stream of MIME headers in the rawrequest variable of the object.
[5] Invokes the content method for the object, generating an call to the error method if an exception
is raised.
[6] Invokes the output method for the object
Developers have the option of streaming output to a buffer via the puts method of the reply, or simply
populating the reply_body variable of the object. The information returned by the content method is not
interpreted in any way. If an exception is thrown (via the error command in Tcl, for example) the caller
will auto-generate a 500 {Internal Error} message. A typical implementation of content look like:
clay::define ::test::content.file {
superclass ::httpd::content.file
# Return a file
# Note: this is using the content.file mixin which looks for the reply_file variable
# and will auto-compute the Content-Type
method content {} {
my reset
set doc_root [my request get DOCUMENT_ROOT]
my variable reply_file
set reply_file [file join $doc_root index.html]
}
}
clay::define ::test::content.time {
# return the current system time
method content {} {
my variable reply_body
my reply set Content-Type text/plain
set reply_body [clock seconds]
}
}
clay::define ::test::content.echo {
method content {} {
my variable reply_body
my reply set Content-Type [my request get CONTENT_TYPE]
set reply_body [my PostData [my request get CONTENT_LENGTH]]
}
}
clay::define ::test::content.form_handler {
method content {} {
set form [my FormData]
my reply set Content-Type {text/html; charset=UTF-8}
my puts [my html_header {My Dynamic Page}]
my puts "<BODY>"
my puts "You Sent<p>"
my puts "<TABLE>"
foreach {f v} $form {
my puts "<TR><TH>$f</TH><TD><verbatim>$v</verbatim></TD>"
}
my puts "</TABLE><p>"
my puts "Send some info:<p>"
my puts "<FORM action=/[my request get REQUEST_PATH] method POST>"
my puts "<TABLE>"
foreach field {name rank serial_number} {
set line "<TR><TH>$field</TH><TD><input name=\"$field\" "
if {[dict exists $form $field]} {
append line " value=\"[dict get $form $field]\"""
}
append line " /></TD></TR>"
my puts $line
}
my puts "</TABLE>"
my puts [my html footer]
}
}
Variable
variable ChannelRegister
variable reply
A dictionary which will converted into the MIME headers of the reply
variable request
A dictionary containing the SCGI transformed HTTP headers for the request
Delegate
delegate <server>
The server object which spawned this reply
Methods
method constructorServerObj ?args?
method destructor ?dictargs?
clean up on exit
method ChannelRegister ?args?
Registers a channel to be closed by the close method
method close
Close channels opened by this object
method Log_Dispatched
Record a dispatch event
method dispatchnewsockdatastate
Accept the handoff from the server object of the socket newsock and feed it the state datastate.
Fields the datastate are looking for in particular are:
* mixin - A key/value list of slots and classes to be mixed into the object prior to invoking
Dispatch.
* http - A key/value list of values to populate the object's request ensemble
All other fields are passed along to the clay structure of the object.
method Dispatch
method html_headertitle ?args?
method html_footer ?args?
method errorcode ?msg ? ?errorInfo ?
method content
REPLACE ME: This method is the "meat" of your application. It writes to the result buffer via the
"puts" method and can tweak the headers via "clay put header_reply"
method EncodeStatusstatus
Formulate a standard HTTP status header from he string provided.
method logtype ?info ?
method CoroName
method DoOutput
Generates the the HTTP reply, streams that reply back across chan, and destroys the object.
method FormData
For GET requests, converts the QUERY_DATA header into a key/value list. For POST requests, reads
the Post data and converts that information to a key/value list for application/x-www-form-
urlencoded posts. For multipart posts, it composites all of the MIME headers of the post to a
singular key/value list, and provides MIME_* information as computed by the mime package,
including the MIME_TOKEN, which can be fed back into the mime package to read out the contents.
method PostDatalength
Stream length bytes from the chan socket, but only of the request is a POST or PUSH. Returns an
empty string otherwise.
method Session_Load
Manage session data
method putsline
Appends the value of string to the end of reply_body, as well as a trailing newline character.
method RequestFindfield
method requestsubcommand ?args?
method replysubcommand ?args?
method reset
Clear the contents of the reply_body variable, and reset all headers in the reply structure back
to the defaults for this object.
method timeOutCheck
Called from the http::server object which spawned this reply. Checks to see if too much time has
elapsed while waiting for data or generating a reply, and issues a timeout error to the request if
it has, as well as destroy the object and close the chan socket.
method timestamp
Return the current system time in the format:
%a, %d %b %Y %T %Z
CLASSHTTPD::SERVERancestors: httpd::mimeVariable
variable template
variable url_patternsMethods
method constructorargs ?portauto? ?myaddr127.0.0.1? ?stringauto? ?nameauto? ?doc_root ? ?reverse_dns0? ?configuration_file ? ?protocolHTTP/1.1?
method destructor ?dictargs?
method connectsockipport
Reply to an open socket. This method builds a coroutine to manage the remainder of the connection.
The coroutine's operations are driven by the Connect method.
method ServerHeadersiphttp_requestmimetxt
method Connectuuidsockip
This method reads HTTP headers, and then consults the dispatch method to determine if the request
is valid, and/or what kind of reply to generate. Under normal cases, an object of class
::http::reply is created, and that class's dispatch method. This action passes control of the
socket to the reply object. The reply object manages the rest of the transaction, including
closing the socket.
method counterwhich
Increment an internal counter.
method CheckTimeout
Check open connections for a time out event.
method debug ?args?
method dispatchdata
Given a key/value list of information, return a data structure describing how the server should
reply.
method Dispatch_Defaultreply
Method dispatch method of last resort before returning a 404 NOT FOUND error. The default
behavior is to look for a file in DOCUMENT_ROOT which matches the query.
method Dispatch_Localdata
Method dispatch method invoked prior to invoking methods implemented by plugins. If this method
returns a non-empty dictionary, that structure will be passed to the reply. The default is an
empty implementation.
method Headers_Localvarname
Introspect and possibly modify a data structure destined for a reply. This method is invoked
before invoking Header methods implemented by plugins. The default implementation is empty.
method Headers_Processvarname
Introspect and possibly modify a data structure destined for a reply. This method is built
dynamically by the plugin method.
method HostNameipaddr
Convert an ip address to a host name. If the server/ reverse_dns flag is false, this method simply
returns the IP address back. Internally, this method uses the dns module from tcllib.
method log ?args?
Log an event. The input for args is free form. This method is intended to be replaced by the user,
and is a noop for a stock http::server object.
method pluginslot ?class ?
Incorporate behaviors from a plugin. This method dynamically rebuilds the Dispatch and Headers
method. For every plugin, the server looks for the following entries in clayplugin/:
load - A script to invoke in the server's namespace during the plugin method invokation.
dispatch - A script to stitch into the server's Dispatch method.
headers - A script to stitch into the server's Headers method.
thread - A script to stitch into the server's Thread_start method.
method port_listening
Return the actual port that httpd is listening on.
method PrefixNormalizeprefix
For the stock version, trim trailing /'s and *'s from a prefix. This method can be replaced by the
end user to perform any other transformations needed for the application.
method sourcefilename
method start
Open the socket listener.
method stop
Shut off the socket listener, and destroy any pending replies.
method SubObject{}db
method SubObject{}default
method templatepage
Return a template for the string page
method TemplateSearchpage
Perform a search for the template that best matches page. This can include local file searches,
in-memory structures, or even database lookups. The stock implementation simply looks for files
with a .tml or .html extension in the ?doc_root? directory.
method Thread_start
Built by the plugin method. Called by the start method. Intended to allow plugins to spawn worker
threads.
method Uuid_Generate
Generate a GUUID. Used to ensure every request has a unique ID. The default implementation is:
return [::clay::uuid generate]
method Validate_Connectionsockip
Given a socket and an ip address, return true if this connection should be terminated, or false if
it should be allowed to continue. The stock implementation always returns 0. This is intended for
applications to be able to implement black lists and/or provide security based on IP address.
CLASSHTTPD::SERVER::DISPATCHancestors: httpd::server
Provide a backward compadible alias
CLASSHTTPD::CONTENT.REDIRECTMethods
method reset
method contentCLASSHTTPD::CONTENT.CACHEMethods
method DispatchCLASSHTTPD::CONTENT.TEMPLATEMethods
method contentCLASSHTTPD::CONTENT.FILE
Class to deliver Static content When utilized, this class is fed a local filename by the dispatcher
Methods
method FileName
method DirectoryListinglocal_file
method content
method DispatchCLASSHTTPD::CONTENT.EXECVariable
variable exenameMethods
method CgiExecexecnamescriptarglist
method Cgi_ExecutablescriptCLASSHTTPD::CONTENT.PROXYancestors: httpd::content.exec
Return data from an proxy process
Methods
method proxy_channel
method proxy_path
method ProxyRequestchanachanb
method ProxyReplychanachanb ?args?
method DispatchCLASSHTTPD::CONTENT.CGIancestors: httpd::content.proxyMethods
method FileName
method proxy_channel
method ProxyRequestchanachanb
method ProxyReplychanachanb ?args?
method DirectoryListinglocal_file
For most CGI applications a directory list is vorboten
CLASSHTTPD::PROTOCOL.SCGI
Return data from an SCGI process
Methods
method EncodeStatusstatusCLASSHTTPD::CONTENT.SCGIancestors: httpd::content.proxyMethods
method scgi_info
method proxy_channel
method ProxyRequestchanachanb
method ProxyReplychanachanb ?args?
CLASSHTTPD::SERVER.SCGIancestors: httpd::server
Act as an SCGI Server
Methods
method debug ?args?
method ConnectuuidsockipCLASSHTTPD::CONTENT.WEBSOCKET
Upgrade a connection to a websocket
CLASSHTTPD::PLUGIN
httpd plugin template
CLASSHTTPD::PLUGIN.DICT_DISPATCH
A rudimentary plugin that dispatches URLs from a dict data structure
Methods
method Dispatch_Dictdata
Implementation of the dispatcher
method uri{}addvhostspatternsinfo
method uri{}directvhostspatternsinfobodyCLASSHTTPD::REPLY.MEMCHANancestors: httpd::replyMethods
method output
method DoOutput
method closeCLASSHTTPD::PLUGIN.LOCAL_MEMCHANMethods
method local_memchancommand ?args?
method Connect_Localuuidsock ?args?
A modified connection method that passes simple GET request to an object and pulls data directly
from the reply_body data variable in the object Needed because memchan is bidirectional, and we
can't seem to communicate that the server is one side of the link and the reply is another