twhttpd Documentation

Copyright 2002 by Sam Ng

Version 1.01


Part C: Specific Configuration

Part C.2 Decision Tree

A decision tree is some if-elseif-else statements with some set statements or http return code.

All server configuration consists of at least one if-elseif-else statement and return code.

An example of a decision tree is as follows

if ( $path == /cgi-bin* )
{
    return 200;
}
else
{
    return 400;
}

Below are valid operators, variables and functions you can use to build your own decision tree.

Operators

== test equal, can be used to test for string type and numeric type
!= not equal, can be used to test for string type and numeric type
>, <, >=, <= greater than, greater than or equal to, less than, less than or equal to, can be only be used to test for numeric type only
=~ The very powerful regular expression check, can only be used to test for string type variable and a regular expression (regex).

Variables

You can ONLY "test" these variable, you can not "set" these variable.

Option Date Type Description
$method String The HTTP request method, currently only support GET, POST, CONNECT
Note: This is a case sensitive string.
$host String The HTTP request host, either from HTTP "Host" header or URL.
$path String The HTTP request path, excluding query string if any
$ext String The extension part of the path, empty string if no extension

NOTE: For file name like this_file.tar.gz, $ext is "tar.gz"

$user_agent Strnig The User-Agent HTTP field, NULL if not presented
$referer String The Referer HTTP field, NULL if not presented
$query String The Query string of the request, NULL if not presented.
$cookie String The Cookie field, NULL if not presented.
$auth Auth_String The HTTP www-authentication
$proxy_auth Auth_String The HTTP proxy-authentication
$client_ip IP Address  The client (broswer) IP.
$local_ip IP Address The local server IP
$port Number The server port number
$post_len Number The post data length, equals to -1 if no post data

Functions

is_cgi() Check if this request is a CGI.

Basically this means $query_len > 0, or $method == POST

strlen(String_type) Return the string length for a given "string type" variable

E.g. if ( strlen($query) > 30)
       {
           return 400;
        }

htpasswd(FILE, $auth), or

htpasswd(FILE, $proxy_auth)

Check if the $auth or $proxy_auth is a valid login according to the FILE. 

NOTE:

  • If you check for $proxy_auth, you have to return 407 "Proxy Not Authorized" when not authorized. 
  • If you check for $auth, return 401 "Not Authorized" when not authorized.

Return/Set Statement

Below are valid variables you can "set" inside the if-then-else execution block.

$forward = IP[:Port] $forward can be set in Specific Config Option as default value, but it can also be set/modified inside a decision tree.

This is pariticual usually for virtual host setting, the destination web server may be determined from the $host string.

E.g. if ( $host = "www.host.com" )
       {
           $forward = 192.168.1.1;
           return 200;
       }
       elsif ( $host = "www2.host.com" )
       {
           $forward = 192.168.2.1;
           return 200;
       }
       else
       {
            return 401;
       }

$forward_proxy = "enable|disable" $forward_proxy can also be set in Specific Config Option.
$location = [string] This is ONLY useful if you want twhttpd to do the redirection for you, and you HAVE TO return "301" or "302" to make it effective.

e.g. redirect mail.abc.com to webmail.abc.com
if ($host == "mail.abc.com")
{
    $location = "http://webmail.abc.com/";
    return 301;
}

return = [numeric] The decision tree ends when it reach any return statement.

The return value 200 means this access is allowed by THIS PROGRAM only, the request will then be forward to the destination server for further process, and if the request file is not found in the destination web server, the final return code may still be 404.

Return value other than 200 are send to the client directly, the request would not be send to the destination web server.

NOTE: there is no "$" in front of the keyword "return"

Click HERE is see all the supported return codes

For more details about decision tree, see How Decision Tree works.

Constants

Data Type Syntax Example
IP Address IP-IP or 
IP/Bit 
192.168.1.1-192.168.1.254
210.12.34.54/26
Number Any numbers, usually 9 digits max 123
String Non-quoted string or quoted "string"
Note you have to quote the string if it contains any meta-characters, if un-sure, quote the string.
this_is_a_string
"this is another sting*"
Regex Regular Expression
"/" quoted string, /string/
Default is case INSENSITIVE, if you want case sensitive search, use /string/s
Only used when testing a string type variable with ~=
See example config 2 for more details
/(www\.|webmail\.)?host\.com/
meaning: www.host.com, webmail.host.com or host.com

/$\/cgi\-bin\/.*/s
meaning: anything starting with "/cgi-bin/", case sensitive

Auth_String This is basically an internal string, you can only call  this through htpasswd() N/A

 


[Sam Ng Home] [twhttpd Home]

Last Modified: 2002-02-12