Tcl Library Source Code

NAME
Login

tool - A TclOO and coroutine based web server

Table Of Contents

SYNOPSIS

package require Tcl 8.6
package require httpd ?4.1.1?
package require sha1
package require dicttool
package require oo::meta
package require oo::dialect
package require tool
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

[constructor ?port ?port?? ?myaddr ?ipaddr? all? ?server_string ?string?? ?server_name ?string??](#1)

method add_uri pattern dict
method connect sock ip port
method Connect uuid sock ip
method counter which
method CheckTimeout
method dispatch header_dict
method log args
method port_listening
method PrefixNormalize prefix
method start
method stop
method template page
method TemplateSearch page
method Validate_Connection sock ip
method ENSEMBLE::add field element
method ENSEMBLE::dump
method ENSEMBLE::get field
method ENSEMBLE::reset
method ENSEMBLE::remove field element
method ENSEMBLE::replace keyvaluelist
method ENSEMBLE::reset
method ENSEMBLE::set field value
method http_info::netstring
method request::parse string
method reply::output
method close
method HttpHeaders sock ?debug?
method dispatch newsock datastate
method error code ?message? ?errorInfo?
method content
method EncodeStatus status
method FormData
method MimeParse mimetext
method DoOutput
method PostData length
method puts string
method reset
method timeOutCheck
method timestamp
method TransferComplete args
method Url_Decode string
method cgi_info
option path
option prefix
method proxy_info
method scgi_info

DESCRIPTION

This module implements a web server, suitable for embedding in an application. The server is object oriented, and contains all of the fundamentals needed for a full service website.

Minimal Example

Starting a web service requires starting a class of type httpd::server, and providing that server with one or more URIs to service, and httpd::reply derived classes to generate them.

tool::define ::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>
  }
}
::docserver::server create HTTPD port 8015 myaddr 127.0.0.1
HTTPD add_uri /* [list mixin reply.hello]

Class ::httpd::server

This class is the root object of the webserver. It is responsible for opening the socket and providing the initial connection negotiation.

Class ::httpd::reply

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 request parse 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

Reply Method Ensembles

The http::reply class and its derivatives maintain several variables as dictionaries internally. Access to these dictionaries is managed through a dedicated ensemble. The ensemble implements most of the same behaviors as the dict command. Each ensemble implements the following methods above, beyond, or modifying standard dicts:

Reply Method Ensemble: http_info

Manages HTTP headers passed in by the server. Ensemble Methods:

Reply Method Ensemble: request

Managed data from MIME headers of the request.

Reply Method Ensemble: reply

Manage the headers sent in the reply.

Reply Methods

Class ::httpd::content

The httpd module includes several ready to use implementations of content mixins for common use cases. Options are passed in to the add_uri method of the server.

Class ::httpd::content.cgi

An implementation to relay requests to process which will accept post data streamed in vie stdin, and sent a reply streamed to stdout.

Class ::httpd::content.file

An implementation to deliver files from the local file system.

Class ::httpd::content.proxy

An implementation to relay requests to another HTTP server, and relay the results back across the request channel.

Class ::httpd::content.scgi

An implementation to relay requests to a server listening on a socket expecting SCGI encoded requests, and relay the results back across the request channel.

Class ::httpd::content.websocket

A placeholder for a future implementation to manage requests that can expect to be promoted to a Websocket. Currently it is an empty class.

SCGI Server Functions

The HTTP module also provides an SCGI server implementation, as well as an HTTP implementation. To use the SCGI functions, create an object of the http::server.scgi class instead of the http::server class.

Class ::httpd::reply.scgi

An modified http::reply implementation that understands how to deal with netstring encoded headers.

Class ::httpd::server.scgi

A modified http::server which is tailored to replying to request according to the SCGI standard instead of the HTTP standard.

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. 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.

KEYWORDS

TclOO, WWW, http, httpd, httpserver, services

CATEGORY

Networking

COPYRIGHT

Copyright © 2018 Sean Woods [email protected]