Tux - Perl extension for Tux webserver
package Tux::Sample::Template; use strict; use Tux; use Tux::Constants qw/:event/; my $html_content; BEGIN{ $html_content = << 'EOT'; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> <html> <head> <title>tux_perl Template</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Quanta Plus"> </head> <body> <h1>tux_perl Template</h1> <hr> Congratulation! Your tux_perl is working now! </body> </html> EOT } sub handler{ my($r)=@_; SWITCH: { ($r->event==0) && do{ $r->event(1); $r->http_status(200); return $r->tux_print_header('Content-Type' => 'text/html'); }; ($r->event==1) && do{ $r->event(2); return $r->tux_print($html_content); }; ($r->event==2) && do{ $r->event(TUX_EVENT_FINISH_REQ); return $r->tux_print_http_chunk_eof; }; } return 0; } 1;
Tux is a VERY FAST http server embedded in Linux kernel. tux_perl is an user space Perl interpreter for Tux. tux_perl is event driven. It can generate dynamic web content quite fast.
tux_perl run in user space as a module of Tux. It inherits the event driven structure of normal Tux module. Minimizing the overhead of process scheduling, it's speed is quite fast.
Because of th event driven structure, there are some differences between developing tux_perl driven modules and traditional CGIs or web scripts.
tux_perl reads the config file for the options and handlers. Usually the config file will be PREFIX/etc/tux_perl.conf.
imp_lib /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/auto/Tux/tux_perl_imp.so init_log_file /var/log/tux_perl_init.log runtime_log_file /var/log/tux_perl_runtime.log perl_lib_path /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi <perl_module> name Static lib Tux::Sample::Static handler Tux::Sample::Static::handler </perl_module> <perl_module> name Template lib Tux::Sample::Template handler Tux::Sample::Template::handler </perl_module>
There are tow dynamic libraries for tux_perl. tux_perl.so is loaded bu Tux directly, tux_perl_imp.so is loaded by tux_perl.so. The config file of Tux should be set to load tux_perl.so, and the location of tux_perl_imp.so should be specified in tux_perl.conf with imp_lib option.
Usually this option is already set by Makefile.
Because Tux module run in different privileges during the initial time and runtime, two log files are supported. init_log_file will log the information during initial time. Comment out this options with '#' will disable initial time log. This log will be written with root privileges.
runtime_log_file will log the information during runtime. Comment out this options with '#' will disable runtime log. This log will be written with privileges specified in the config file of Tux.
Notice, because Tux chroot to the DOCROOT before enter user space module. This path is related to the DOCROOT.
Append new Perl library path. Multi paths can be specified with one perl_lib_path option for every path.
The handlers are specified in the <perl_module<gt> sections, one section per handler.
name
The name of the handler, which is used in the URL. The URL reads
http://host/path/tux_perl.tux?name
lib
The package name of the handler module. tux_perl will look in the Perl lib path for the module. Please append the path with perl_lib_path if it's not included by default.
handler
The method name of the handler.
To avoid the overhead of process scheduling, non-blocking event driven structure is deployed in Tux. The request are processed when it's READY, and rescheduled when it returned from the handle or called the tux() system call to send result or read other resources.
The code of tux_perl driven module has to be split into several pieces by the tux() system call, because when the tux() system call returned, the original request may be rescheduled and another one may be returned. The module should not continue operating on this request, but exit and kernel will schedule the requests correctly.
All the methods of Tux perl lib which uses tux() system call are prefixed by tux_. Modules should exit after calling these methods:
return $r->tux_print($html_content);
The tux() system call and the parameter user_req_t are wrapped as a Tux object of Perl. A parameter of class Tux is passed to the handler when it is called.
The tux method call the tux() system call. The request will be passed as the second argument. Refer to the tux(2) man page and Tux::Constants for the description of $action. Be sure to use Tux::Constants package when calling this method.
use Tux::Constants qw/:action/;
Get the major version of Tux.
Get the minor version of Tux.
Get the patch version of Tux.
Get the http version of the request, 0 for HTTP/1.0, 1 for HTTP/1.1 .
Get the method of the request, one of METHOD_NONE, METHOD_GET, METHOD_HEAD, METHOD_POST, or METHOD_PUT.
Socket file descriptor; writing to this will send data to the connected client associated with this request. Do not read from this socket file descriptor; you could potentially confuse the HTTP engine.
Private, per-request state for use in tux modules. The system will preserve this value as long as a request is active. Passing an argument will set the event.
Thread index; see discussion of TUX_ACTION_STARTTHREAD in tux(2).
Set the error status as an integer for error reporting. Must be set before tux_print_header() or http_header().
Used by the tux daemon to determine which loadable module to associate with a req.
The IP address of the host to which sock is connected.
Set to an address for a buffer of at least $r->objectlen size into which to read an object from the URL cache with the TUX_ACTION_READ_OBJECT action. TUX_ACTION_READ_OBJECT must not be called unless $r->objectlen >= 0, and TUX implicitly relies on $r->object_addr being at least $r->objectlen in size.
Without argument, object_addr returns the object as a string. If only parameter $addr is set, the object_addr will be set to $addr and objectlen will be set to length of $addr. If parameter $length is also set, objectlen will be set to $length.
The size of a file that satisfies the current request and which is currently living in the URL cache. This is set if a request returns after TUX_ACTION_GET_OBJECT. A module should make sure that the buffer at $r->object_addr is at least $r->objectlen in size before calling TUX_ACTION_READ_OBJECT.
Passing an argument will set the objectlen.
Specifies the name of a URL to get with the TUX_ACTION_GET_OBJECT action. If the URL is not immediately available (that is, is not in the URL cache), the request is queued and the tux subsystem may go on to other ready requests while waiting.
Passing an argument will set the objectname.
The full query string sent from the client. Passing an argument will set the query.
If cookies are in the request header, cookies is the string in which the cookies are passed to the module. Passing an argument will set the cookies.
The Content-Type header value for the request. Passing an argument will set the content-type.
The User-Agent header value for the request. Passing an argument will set the User-Agent.
The Accept header value for the request. Passing an argument will set the Accept.
The Accept-Charset header value for the request. Passing an argument will set the Accept-Charset.
The Accept-Encoding header value for the request. Passing an argument will set the Accept-Encoding.
The Accept-Language header value for the request. Passing an argument will set the Accept-Language.
The Cache-Control header value for the request. Passing an argument will set the Cache-Control.
The If-Modified-Since header value for the request. Passing an argument will set the If-Modified-Since.
The Negotiate header value for the request. Passing an argument will set the Negotiate.
The Pragma header value for the request. Passing an argument will set the Pragma.
The Referer header value for the request. Passing an argument will set the Referer.
For POST requests, the incoming data is placed in post_data.
Returns the current date/time.
The KeepAlive header value for the request. Passing an argument will set the KeepAlive.
Generate HTTP response header with inputed header names and values. Transfer-Encoding header will be set to chunked by default. HTTP version will be detected automatic.
chunked
Generate a chunk of content. tux_perl support HTTP/1.1 keep alive function with Transfer-Encoding of chunked.
Generate an EOF symbol for chunk. It's necessary at the end of a HTTP/1.1 response.
Print the strings as the body of HTTP response. Strings will be wrapped into a chunk first. Don't send HTTP header with it.
Generate the HTTP response header and print it. Refer to the http_header function.
Print the EOF symbol for chunk.
Tux chroot to the DOCROOT specified at it's config file before calling the user space module. So, the environments of initial time and run time will be different. To resolve the problem, folders can be link to the DOCROOT folder with mount --bind.
link
mount --bind /usr /var/www/html/usr
If your module loaded other perl libraries dynamically, /usr/lib is required to link.
None by default.
tux(2), Tux::Constants(3)
More information about tux_perl can be found at
http://tux-perl.sourceforge.net http://sourceforge.net/projects/tux-perl
Yale Huang, <yale@sdf-eu.org>
Copyright 2002 by Yale Huang
This library is released under the GPL; you can redistribute it and/or modify it under the term of GPL.
1 POD Error
The following errors were encountered while parsing the POD:
You can't have =items (as at line 283) unless the first thing after the =over is an =item
To install Tux, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tux
CPAN shell
perl -MCPAN -e shell install Tux
For more information on module installation, please visit the detailed CPAN module installation guide.