httpd(3tcl)
π httpd β Tcl Web Server π
okay so bestie, this is the manual page for httpd(3tcl), aka Tcl Web Server. Buckle up, it's a long one, but it's so worth it. π«Ά
πΈ NAME πΈ
httpd β a TclOO and coroutine based web server. no cap, it's literally a whole web server living inside Tcl. β¨
π SYNOPSIS π
so first you gotta pull in the whole squad of packages, then you get the methods. here's the full list, exactly as it is:
package require Tcl 8.6 9
package require uuid
package require clay
package require coroutine
package require fileutil
package require fileutil::magic::filetype
package require websocket
package require mime
package require cron
package require uri
package require Markdown
method ChannelCopy in out ?args?
method html_header ?title ? ?args?
method html_footer ?args?
method http_code_string code
method HttpHeaders sock ?debug ?
method HttpHeaders_Default
method HttpServerHeaders
method MimeParse mimetext
method Url_Decode data
method Url_PathCheck urlsuffix
method wait mode sock
variable ChannelRegister
variable reply
variable request
delegate <server>
method constructor ServerObj ?args?
method destructor ?dictargs?
method ChannelRegister ?args?
method close
method Log_Dispatched
method dispatch newsock datastate
method Dispatch
method html_header title ?args?
method html_footer ?args?
method error code ?msg ? ?errorInfo ?
method content
method EncodeStatus status
method log type ?info ?
method CoroName
method DoOutput
method FormData
method PostData length
method Session_Load
method puts line
method RequestFind field
method request subcommand ?args?
method reply subcommand ?args?
method reset
method timeOutCheck
method timestamp
variable template
variable url_patterns
method constructor args ?port auto? ?myaddr 127.0.0.1? ?string auto? ?name auto? ?doc_root ? ?reverse_dns 0? ?configuration_file ? ?protocol HTTP/1.1?
method destructor ?dictargs?
method connect sock ip port
method ServerHeaders ip http_request mimetxt
method Connect uuid sock ip
method counter which
method CheckTimeout
method debug ?args?
method dispatch data
method Dispatch_Default reply
method Dispatch_Local data
method Headers_Local varname
method Headers_Process varname
method HostName ipaddr
method log ?args?
method plugin slot ?class ?
method port_listening
method PrefixNormalize prefix
method source filename
method start
method stop
method SubObject {} db
method SubObject {} default
method template page
method TemplateSearch page
method Thread_start
method Uuid_Generate
method Validate_Connection sock ip
method reset
method content
method Dispatch
method content
method FileName
method DirectoryListing local_file
method content
method Dispatch
variable exename
method CgiExec execname script arglist
method Cgi_Executable script
method proxy_channel
method proxy_path
method ProxyRequest chana chanb
method ProxyReply chana chanb ?args?
method Dispatch
method FileName
method proxy_channel
method ProxyRequest chana chanb
method ProxyReply chana chanb ?args?
method DirectoryListing local_file
method EncodeStatus status
method scgi_info
method proxy_channel
method ProxyRequest chana chanb
method ProxyReply chana chanb ?args?
method debug ?args?
method Connect uuid sock ip
method Dispatch_Dict data
method uri {} add vhosts patterns info
method uri {} direct vhosts patterns info body
method output
method DoOutput
method close
method local_memchan command ?args?
method Connect_Local uuid sock ?args?
π¦ DESCRIPTION π¦
This module implements a web server, and it's built to be embedded right into an application. The server is object oriented, and it comes with all of the fundamentals you need for a full service website. everything. the whole vibe. π
π MINIMAL EXAMPLE π
To get a web service going you start a class of type httpd::server, hand that server one or more URIs to service, and give it httpd::reply derived classes to actually generate them. easy.
oo::class create ::reply.hello {
method content {} {
my puts "<HTML><HEAD><TITLE>IRM Dispatch Server</TITLE></HEAD><BODY>"
my puts "<h1>Hello World!</h1>"
my puts </BODY></HTML>
}
}
::httpd::server create HTTPD port 8015 myaddr 127.0.0.1 doc_root ~/htdocs
HTTPD plugin dispatch httpd::server::dispatch
HTTPD uri add * /hello [list mixin reply.hello]
The bare module does have facilities to serve files from a file system. And ooh, files that end in .tml get substituted in the style of Tclhttpd:
<!-- hello.tml -->
[my html_header {Hello World!}]
Your Server is running.
<p>
The time is now [clock format [clock seconds]]
[my html_footer]
There's a complete example of an httpd server in the /examples directory of Tcllib. It also shows how to dispatch URIs to other processes via SCGI and HTTP proxies. obsessed tbh.
cd ~/tcl/sandbox/tcllib
tclsh examples/httpd.tcl
π CLASSES π
π§ CLASS HTTPD::MIME
A metaclass for MIME handling behavior across a live socket. π
Methods
method ChannelCopy in out ?args?β copies a channel, bestie. exactly what it says on the tin.method html_header ?title ? ?args?β Returns a block of HTML. πmethod html_footer ?args?β the footer half of that. closing tags, closing energy. β¨method http_code_string codeβ turns an HTTP code into its string form.method HttpHeaders sock ?debug ?β streams the headers out over the socket.method HttpHeaders_Defaultβ the default headers. the base fit. πmethod HttpServerHeadersβ the server's own headers.method MimeParse mimetextβ 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 theerrormethod, and immediately invoke theoutputmethod to produce an error code and close the connection. so yeah, it handles its own drama. πmethod Url_Decode dataβ De-httpizes a string.method Url_PathCheck urlsuffixβ checks that url suffix, lowkey doing a vibe check on it.method wait mode sockβ waits on the socket in the given mode.
πΈ CLASS HTTPD::REPLY
ancestors: 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 does this whole routine:
- Invokes the
resetmethod for the object to populate default headers. - Invokes the
HttpHeadersmethod to stream the MIME headers out of the socket. - Invokes the
requestparse method to convert the stream of MIME headers into a dict that can be read via therequestmethod. - Stores the raw stream of MIME headers in the
rawrequestvariable of the object. - Invokes the
contentmethod for the object, generating a call to theerrormethod if an exception is raised. - Invokes the
outputmethod 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 looks like this, and honestly? it's giving range:
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β the channel registry, keeping track of the channels this object owns. πvariable replyβ A dictionary which will be 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. like, the parent. the origin story. π
Methods
method constructor ServerObj ?args?β sets the whole thing up.method destructor ?dictargs?β clean up on exit. gotta leave it tidy bestie. π§Ήmethod ChannelRegister ?args?β Registers a channel to be closed by theclosemethod.method closeβ Close channels opened by this object.method Log_Dispatchedβ Record a dispatch event. for the archives. πmethod dispatch newsock datastateβ Accept the handoff from the server object of the socketnewsockand feed it the statedatastate. Fields thedatastateare looking for in particular are:mixinβ A key/value list of slots and classes to be mixed into the object prior to invokingDispatch. β¨
httpβ a key/value list of values to populate the object's request ensemble. cute.
All the other fields? They get passed along to the clay structure of the object. no notes.
Dispatchβ the dispatcher. doin its thing.html_headertitle ?args?html_footer?args?errorcode ?msg ? ?errorInfo ?contentβ REPLACE ME: this method is the literal "meat" of your application. It writes to the result buffer via theputsmethod, and it can tweak the headers viaclay put header_reply. this is your moment bestie, the main character energy lives here πEncodeStatusstatus β formulates a standard HTTP status header from the string provided.logtype ?info ?CoroNameDoOutputβ generates the HTTP reply, streams that reply back acrosschan, and then destroys the object. it's giving final act β¨FormDataβ for GET requests, converts theQUERY_DATAheader into a key/value list. For POST requests, reads the Post data and converts that information to a key/value list forapplication/x-www-form-urlencodedposts. For multipart posts, it composites all of the MIME headers of the post into a singular key/value list, and providesMIME_*information as computed by the mime package, including theMIME_TOKEN, which can be fed back into the mime package to read out the contents. iconic.PostDatalength β streams length bytes from thechansocket, but only if the request is a POST or PUSH. Returns an empty string otherwise. periodt.Session_Loadβ manage session data.putsline β appends the value of string to the end ofreply_body, plus a trailing newline character.RequestFindfieldrequestsubcommand ?args?replysubcommand ?args?resetβ clears the contents of thereply_bodyvariable, and resets all headers in the reply structure back to the defaults for this object. fresh start fr fr π§ΌtimeOutCheckβ called from thehttp::serverobject 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 destroying the object and closing thechansocket. β°timestampβ returns the current system time in the format:
%a, %d %b %Y %T %Z
π CLASS HTTPD::SERVER π
ancestors: httpd::mime
Variables
templateurl_patterns
Methods
constructorargs ?portauto? ?myaddr127.0.0.1? ?stringauto? ?nameauto? ?doc_root ? ?reverse_dns0? ?configuration_file ? ?protocolHTTP/1.1?destructor?dictargs?connectsock ip port β replies to an open socket. This method builds a coroutine to manage the rest of the connection. The coroutine's operations are driven by theConnectmethod.ServerHeadersip http_request mimetxtConnectuuid sock ip β this method reads HTTP headers, and then consults thedispatchmethod to figure out if the request is valid, and/or what kind of reply to generate. Under normal cases, an object of class::http::replyis created, and that class'sdispatchmethod runs. This action passes control of the socket to the reply object. The reply object manages the rest of the transaction, including closing the socket. πcounterwhich β increments an internal counter.CheckTimeoutβ checks open connections for a time out event.debug?args?dispatchdata β given a key/value list of information, returns a data structure describing how the server should reply.Dispatch_Defaultreply β thedispatchmethod of last resort before returning a404 NOT FOUNDerror. The default behavior is to look for a file inDOCUMENT_ROOTwhich matches the query.Dispatch_Localdata β thedispatchmethod 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.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.Headers_Processvarname β introspect and possibly modify a data structure destined for a reply. This method is built dynamically by thepluginmethod.HostNameipaddr β convert an ip address to a host name. If theserver/reverse_dnsflag is false, this method simply returns the IP address back. Internally, this method uses thednsmodule from tcllib.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 stockhttp::serverobject.pluginslot ?class ? β incorporate behaviors from a plugin. This method dynamically rebuilds theDispatchandHeadersmethod. 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'sDispatchmethod.headersβ a script to stitch into the server'sHeadersmethod.threadβ a script to stitch into the server'sThread_startmethod.
port_listeningβ return the actual port that httpd is listening on. π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.sourcefilenamestartβ open the socket listener.stopβ shut off the socket listener, and destroy any pending replies.SubObject{}dbSubObject{}defaulttemplatepage β return a template for the string page.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.tmlor.htmlextension in the ?doc_root? directory.Thread_startβ built by thepluginmethod. Called by thestartmethod. Intended to allow plugins to spawn worker threads.Uuid_Generateβ generate a GUUID. Used to ensure every request has a unique ID. The default implementation is:
return [::clay::uuid generate]
Validate_Connectionsock ip β 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 returns0. This is intended for applications to be able to implement black lists and/or provide security based on IP address. π‘οΈ
π¦ CLASS HTTPD::SERVER::DISPATCH π¦
ancestors: httpd::server
Provide a backward compadible alias. iykyk.
πΈ CLASS HTTPD::CONTENT.REDIRECT πΈ
Methods
resetcontent
β CLASS HTTPD::CONTENT.CACHE β
Methods
Dispatch
π CLASS HTTPD::CONTENT.TEMPLATE π
Methods
content
π CLASS HTTPD::CONTENT.FILE π
Class to deliver Static content. When utilized, this class is fed a local filename by the dispatcher.
Methods
FileNameDirectoryListinglocal_filecontentDispatch
π₯ CLASS HTTPD::CONTENT.EXEC π₯
Variable
exename
Methods
CgiExecexecname script arglistCgi_Executablescript
π CLASS HTTPD::CONTENT.PROXY π
ancestors: httpd::content.exec
Return data from an proxy process.
Methods
proxy_channelproxy_pathProxyRequestchana chanbProxyReplychana chanb ?args?Dispatch
π«Ά CLASS HTTPD::CONTENT.CGI π«Ά
ancestors: httpd::content.proxy
Methods
FileNameproxy_channelProxyRequestchana chanbProxyReplychana chanb ?args?DirectoryListinglocal_file β for most CGI applications a directory list is vorboten. π«
β¨ CLASS HTTPD::PROTOCOL.SCGI β¨
Return data from an SCGI process.
Methods
EncodeStatusstatus
π
CLASS HTTPD::CONTENT.SCGI π
ancestors: httpd::content.proxy
Methods
scgi_infoproxy_channelProxyRequestchana chanbProxyReplychana chanb ?args?
π CLASS HTTPD::SERVER.SCGI π
ancestors: httpd::server
Act as an SCGI Server.
Methods
debug?args?Connectuuid sock ip
π¦ CLASS HTTPD::CONTENT.WEBSOCKET π¦
Upgrade a connection to a websocket. lowkey the coolest glow-up a connection can have π«
π§ CLASS HTTPD::PLUGIN π§
httpd plugin template.
π CLASS HTTPD::PLUGIN.DICT_DISPATCH π
A rudimentary plugin that dispatches URLs from a dict data structure.
Methods
Dispatch_Dictdata β implementation of the dispatcher.uri{}add vhosts patterns infouri{}direct vhosts patterns info body
π CLASS HTTPD::REPLY.MEMCHAN π
ancestors: httpd::reply
Methods
outputDoOutputclose
π CLASS HTTPD::PLUGIN.LOCAL_MEMCHAN π
Methods
local_memchancommand ?args?Connect_Localuuid sock ?args? β a modified connection method that passes simple GET request to an object and pulls data directly from thereply_bodydata 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. π fr fr, it's a whole situation.
π AUTHORS π
Sean Woods
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category network of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist](http://core.tcl.tk/tcllib/reportlist). Please also report any ideas for enhancements you may have for either package and/or documentation.
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar.
bestie pls don't inline your patch, the maintainers are begging π
β KEYWORDS β
TclOO, WWW, http, httpd, httpserver, services
π CATEGORY π
Networking
π COPYRIGHT π
okay bestie, final section, and it's the one where we give credit where credit is due, because we are not the kind of girl who forgets to tag people. π«Ά
- Copyright (c) 2018 Sean Woods
<yoda@etoyoc.com>
that's it. that's the whole vibe. Sean Woods wrote this thing back in 2018 and honestly? iconic behavior. the email's right there in the backticks if you ever wanna slide into his inbox with a thank-you note (do it, people love that, fr fr π ).
quick lil reminder bestie:</> a copyright line isn't decoration, it's a real thing. if you're out here copy-pasting this manpage into your own docs, keep the credit attached. stealing credit is not main character energy, it's giving villain arc. π
and that's a wrap on httpd(3tcl)! you made it all the way to the bottom, which means you now know more about Tcl HTTP servers than like 90% of the group chat. slay. go build something cute with it. π¦β¨
this page lives in tcllib, version 4.3.6, and the page itself is httpd(3tcl) β no date stamp on this footer, she's simply timeless like that. π
bye bestie!! ππΈπ
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2018 Sean Woods <yoda@etoyoc\&.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms