#!Pi3

# ===
#
# Top level configuration file
#
#	** IMPORTANT **
#	If you do nothing else then do this:
#

#... ###### Customize server settings

# Skip below to the definition of 'MainVirtualHostInformation', change
# the settings for Hostname (*Mandatory* redirects will not work without
# this!) and Administrator (optional).
#
# ===

# Include  standard IO and server stuff
include ../Conf/IO.cnf
include ../Conf/Server.cnf
include ../Conf/Pi3API.cnf

# ---
#
# IO Object for main listening socket.
#
# ---
<Object>
    Name ServerIOObject
    Class TCPIPIOClass
    Type Passive

# A hostname must be specified for the Pi3Web HTTP server. The hostname
# will be the used by the server to identify itself in URLs generated
# for redirects and will also be used to lookup the IP address that this
# server will listen on.
	BindHost localhost

# A TCP port must be specified for this HTTP server. The TCP port
# is the port that the HTTP will listen for requests on. This is
# typically 80 for HTTP servers. 
#
# Note that UNIX operating systems normally require that the server
# initially run as user 'root' to bind on ports between 0 and 1024
	BindPort 80

# Timeout accepting connections. -1 specifies no timeout.
	AcceptTimeout -1

# Specify timeout for sending data. -1 specifies no timeout. 

	RecvTimeout 60

# Specify timeout for receiving data. -1 specifies no timeout. 
	SendTimeout 45
	PeerIPVariable STRING:RemoteAddr
	PeerHostNameVariable STRING:RemoteHost
	LocalAddressVariable STRING:LocalAddr

# Flags
#
#   OwnDB - Object adds its own DB, rather than use the parents
#               this is necessary to avoid collision of values placed in
#               the DB between different connection. 
#
#   SetPeerAddress - IP address of remote peer with be put
#               into the specified variable.
#
#   DNSReverseLookup - Use reverse DNS to get the hostname of the 
#               peer and place it in the specified variable.
#
#   SetLocalAddress - Put local address in the specified variable.
	Flags OwnDB|SetPeerAddress|DNSReverseLookup|SetLocalAddress

# Pointer to file with lockfile name, to avoid collisions on accept
# with multiple processes
	AcceptLockFile  "../Logs/lockfile.txt"
</Object>

# ---
#
# Main virtual host object.
#
# ---
<Object>
	Name MainVirtualHostInformation
	Class VirtualHostClass

# Administrator for the primary virtual host
	Administrator webmaster@localhost

# Hostname that this server uses in redirects.
	HostName localhost

# Port that the server uses in redirects.
# Specify it only if it is not the standard port for the protocol in
# use, i.e. 80 for HTTP and 443 for HTTPS
#	ServerPort 80

</Object>

# ---
#
#    Thread dispatch object
#
#   This is an instance of MultiThreadedIOServerClass with appropriate
#   parameters
#
# ---
<Object>
	Name ThreadPoolDispatcher
	Class MultiThreadedIOServerClass

# The IOObject is the object that will be used for the listening socket
# it can be a real IOObject (such as TCP/IP), as test IO object which 
# feeds stub data into the server or a filter IO object which encrpts
# or logs data passed through it.
#
#    Example test IO object:
# 	IOObject TestIOObject
#
#    Log IO to file:
#	IOObject SnoopIO IOObject="ServerIOObject" OutFile="./snoop.dbg"
#
	IOObject ServerIOObject

# Dispatch accepted IO connection to the HTTPLogicObject
	LogicObject HTTPLogicObject

# Number of threads to use 
	MaxThreads 10

# Number of requests to exit after
	ExitAfter 1000

</Object>

# ---
#
# Main Pi3 object. 
#
# This is an instance of the UNIXDaemonClass to fork the server into
# one monitor process and 'NumberOfProcesses' child processes. 
#
# The monitor process automatically starts a new child processes whenever
# a child process exits.
#
# ---
<Object>
	Name Main
	Class UNIXDaemonClass

# Actual class to accept new IO requests and dispatch them
	ServerObject ThreadPoolDispatcher
	PIDFile "../Logs/pid.log"
	RLimitCpu Maximum

# User that child processes will run as. Server must run as root to 
# be able to change to the specified user. A user id number can be
# specified with #number. Server which start with root privileges should
# change to a user id with lesser privileges for security reasons.
#   User nobody

# Group that child processes will run as. Server must run as root to
# be able to change to a different group. A group id number can be
# specified with #number.
#   Group #-1

# generate a server unique lockfile name and write it into
# Logs/lockfile.txt
	LockFileName "../Logs/lockfile.txt"

# Message to write just before the server is ready to start accepting
# connections.
# default
	Message "Pi3Web HTTP Server Started"

# Specify the number of processes. 

	NumberOfProcesses 8

</Object>


# ===
#
# Personal.pi3
#
#	Small web server for a desktop platform. Set the number of
#	processes low (2 or so) and number of threads about 5 or 6.
#
#	- Serves up documents one document root.
#	- Server up server-side includes, image maps
#		and standard CGI
#	- This configuration only writes an error logfile,
#		not an access logfile
#
# ===

#
# Load library definitions for classes used here.
#
include ../Conf/HTTP.cnf
include ../Conf/Plugins.cnf

# ---
#
# CGIMapper
#
#	Instance of PathMapperClass to Map CGI URLs
#
#	CGI Scripts set PATH_INFO. 
#
# ---
<Object>
	Name CGIMapper
	Class PathMapperClass
	CaseSensitive "No"
	#
	# Subdirectories below the mapping directory should be put in path info
	#
	PathInfo "Yes"
	#
	# Set the 'ObjectMap' variable in the response DB if this map is
	# successful
	#
	Action "&dbreplace(response,string,ObjectMap,Scripts)"
</Object>

# ---
#
# AccessLogger
#
#	An instance of ExpressionLoggerClass configured to generate an 
#	logfile
#
# ---
<Object>
	Name AccessLogger
	Class ExpressionLoggerClass

	#
	# Append
	#
	OpenMode "a"
	#
	# Line to log 
	Expression "$A $h - [$t] \"$r\" $s $b$M"
</Object>

# ---
#
# Start
#
#	Handle initial phases of requests, typically before we have enough
#	information to select different execution paths
#
# ---
<Object>
	Name Start
	Class FlexibleHandlerClass
	Init ReturnCode ReturnCode=COMPLETED
	Headers ReadRequestHeaders
	HostMap ReturnCode ReturnCode="COMPLETED"
	Mapping MapToErrorDocument \
		IgnoreStatus="0 200" \
		DefaultMessage="/errors/unknown.htm" \
		301="/errors/301.ssi" \
		304="/errors/304.htm" \
		400="/errors/400.htm" \
		401="/errors/401.ssi" \
		403="/errors/403.ssi" \
		404="/errors/404.ssi" \
		500="/errors/500.ssi" \
		501="/errors/501.htm"
	Mapping CGIMapper From="/cgi-bin/" To="cgi-bin/"
	Mapping PathMapper From="/errors/" To="Fragment/" 
	Mapping PathMapper From="/icons/" To="Icons/"
	
	# Document root 
	Mapping PathMapper From="/" To="WebRoot/"
</Object>

# ---
#
# Default
#
#	Handle requests that other handlers have passed up
#
# ---
<Object>
	Name Default
	Class FlexibleHandlerClass
	CheckPath DirectoryExists IndexFile="index.html" IndexFile="index.htm"
	CheckPath RefuseFileByMask AllowFileMask="EF" RefuseStatus=404
	CheckPath RefuseFileByMask AllowFileMask="F" RefuseStatus=403
	CheckPath ReturnCode ReturnCode=COMPLETED
	CheckAuth ReturnCode ReturnCode=COMPLETED
	CheckAccess AccessByFile RequirePermissions="R"

	#
	# Cause a '501 Not Implemented' Message for requests that
	# are not 'GET' or 'HEAD', i.e.
	#	if !( Method=='GET' || Method=='HEAD' ) 
	#	then
	#		set status = 501
	#	endif
	#
	CheckType Condition="¬(&or(&cmpi($m,GET),&cmpi($m,HEAD)))" \
		StatusCode StatusCode="501"
	CheckType ConditionalGet
	CheckType CheckForDirectory SetType="internal/x-directory"

	#
	# Otherwise: Set the media type based on the file extension
	#
	CheckType MIMETypeByExtension 

	#
	# Return 'COMPLETED' so an internal error will not be raised
	#
	CheckType ReturnCode ReturnCode="COMPLETED"

	Handle Condition="&cmp($c,internal/x-directory)" ListDirectory
	Handle Condition="&cmp($c,internal/x-server-parsed-html)" SendSSI \
		Exec=Yes ExecFileFixup="\
/* exec file="" maps to Standard CGI (Scripts) */\
&dbreplace(response,string,ObjectMap,Scripts)\
&dbreplace(response,rfc822,Content-Type,internal/x-cgi)"
	Handle Condition="&cmp($c,internal/x-imagemap)" ImageMap
	Handle SendFile
	Log AccessLogger File="Logs/access.txt"
	Log ReturnCode ReturnCode=COMPLETED
	Destroy DeleteTemporaryFiles
</Object>

# ---
# 
# Scripts
#
#	Handler sequences for resources which have mapped to CGI
#
# ---
<Object>
	Name Scripts
	Class FlexibleHandlerClass
	Condition "&cmp(&dblookup(response,string,ObjectMap),Scripts)"
	CheckPath RefuseFileByMask AllowFileMask="EFD" RefuseStatus=404
	CheckPath RefuseFileByMask AllowFileMask="F" RefuseStatus=403
	CheckPath ReturnCode ReturnCode=COMPLETED
	CheckAccess AccessByFile RequirePermissions="X"
	CheckType ReturnCode ReturnCode=COMPLETED
	Handle StandardCGI
</Object>

# ---
#
# HTTPLogicObject
#
# 	HTTP Logic object, dispatches requests to all other handlers
#
# ---
<Object>
	Name HTTPLogicObject
	Class HTTPDispatcherClass
	Handlers Start Scripts Default
	KeepOpen On
	ServerRoot ./../
	ServerStamp Pi3Web/0.9b4
	DefaultHost MainVirtualHostInformation
	MIMEFile "Fragment/Mime.typ"
	DefaultMIMEType "application/octet-stream"
	ErrorLogFile "Logs/error.txt"
</Object>

# ---
#
# Directory listing with HTML tables
#
# ---
<Object>
	Name ListDirectory
	Class DirectoryIndexClass
	HeaderPattern "<HTML><TITLE>Index of %p</TITLE>\
<BODY BACKGROUND="/icons/Pi3Tile.gif"\
BGCOLOR="#FFFFFF">\
<H2>Index of %p</H2>\n"
	ListTop "<CENTER><TABLE CELLSPACING=0 CELLPADDING=0 WIDTH=100'%'>\
<TR>\
<TH NOWARP ALIGN=LEFT>\
<IMG SRC="/icons/blank.gif" ALIGN=MIDDLE BORDER=NONE ALT="">\
<TH NOWRAP ALIGN=LEFT>Name<BR><HR SIZE=1 NOSHADE>\
<TH NOWRAP ALIGN=LEFT>Size<BR><HR SIZE=1 NOSHADE>\
<TH NOWRAP ALIGN=LEFT>Last Modified<BR><HR SIZE=1 NOSHADE>\
<TH NOWRAP ALIGN=LEFT>Description<BR><HR SIZE=1 NOSHADE>\n"
	FilePattern "<TR>\
<TD NOWRAP ALIGN=LEFT VALIGN=TOP>\
<A HREF="%r"><IMG SRC="%i" BORDER=NONE ALIGN=MIDDLE ALT=""></A>\
<TD NOWRAP ALIGN=LEFT VALIGN=TOP><A HREF="%r">&abbrev(%n,20,...)</A>\
<TD NOWRAP ALIGN=LEFT VALIGN=TOP>%s<TD>%l\
<TD NOWRAP ALIGN=LEFT VALIGN=TOP>&if(%d,%d,<I>%c</I>)\n"
	ListBottom "</TABLE></CENTER>\n"
	LastModifiedFormat "%d-%b-%y %H:%M"
	Include "*"
	MIMEIcon "text/html /icons/layout.gif"
	MIMEIcon "text/x-code /icons/c.gif"
	MIMEIcon "text/x-perl /icons/p.gif"
	MIMEIcon "text/* /icons/text.gif"
	MIMEIcon "internal/x-directory /icons/folder.gif"
	MIMEIcon "audio/* /icons/sound2.gif"
	MIMEIcon "image/* /icons/image2.gif"
	MIMEIcon "application/x-uuencoded /icons/uuencoded.gif"
	MIMEIcon "application/x-tar /icons/compressed.gif"
	MIMEIcon "application/x-gzip /icons/compressed.gif"
	MIMEIcon "application/x-sh /icons/script.gif"
	MIMEIcon "application/* /icons/binary.gif"
	MIMEIcon "x-world/x-vrml /icons/burst.gif"
	MIMEIcon "*/* /icons/unknown.gif"
	DescriptionFile ".desc"
	SwapFileName ".. Parent Directory"
	FooterPattern "<HR SIZE=1 NOSHADE>\
Contact the hostmaster <A HREF="mailto:$a"<I>$a</I></A>.\
</BODY></HTML>"
	HeaderFile "HEADER"
	FooterFile "README"
	Exclude "."
	Exclude "~*"
	Exclude ".desc"
	Exclude "README"
	Exclude "HEADER"
	Options " Name | AbbrevSize | RelPath | Icon | MIMEType "
</Object>

# ---
#
# CGIFileIOObject
# 	
#	IOObject used for communication between web browser and
#	server.
#
# ---
<Object>
	Name CGIFileIOObject
	Class PipeIOClass
	ReadTimeout -1
	WriteTimeout 10
	NoYield "Read"
</Object>

# ---
#
# StandardCGI
#
# Configuration for Standard CGI
#
# ---
<Object>
	Name StandardCGI
	Class CGIClass
	
	# Specify the IO object use to communicate with the CGI program
	FileIOObject CGIFileIOObject

	# Snoop IO objects can be instantiated in the IO chain with a line
	# like this. They log all IO trafic. This is useful for debugging.
#	FileIOObject SnoopIOObject IOObject="CGIFileIOObject" OutFile="cgi.dbg" 

	DefaultCommandLine "%p%q"
	CommandLineByExt .bat="cmd.exe /c %p%q"
	CommandLineByExt .cmd="cmd.exe /c %p%q"
	CommandLineByExt .pl="perl %p%q"
	IncludeParentsEnvironment Yes
	EnvironmentSize 8192
	
	# These are the environment variables that are sent to the
	# CGI program
	Variable "AUTH_TYPE=$x"
	Variable "CONTENT_LENGTH=$Y"
	Variable "CONTENT_TYPE=$C"
	Variable "HTTPS=$g"
	Variable "&if($K,HTTPS_KEYSIZE=$K,)"
	Variable "&if($K,HTTPS_SECRETKEYSIZE=$G,)"
	Variable "GATEWAY_INTERFACE=CGI/1.1"
	Variable "PATH_INFO=$I"
	Variable "PATH_TRANSLATED=$Z"
	Variable "QUERY_STRING=$q"
	Variable "REMOTE_ADDR=$A"
	Variable "REMOTE_HOST=$h"
	Variable "REMOTE_IDENT=$i"
	Variable "REMOTE_USER=$u"
	Variable "REQUEST_METHOD=$m"
	Variable "SCRIPT_NAME=$z"
	Variable "SERVER_NAME=$v"
	Variable "SERVER_PORT=&if($p,$p,&if($K,443,80))"
	Variable "SERVER_PROTOCOL=$H"
	Variable "SERVER_SOFTWARE=$S"
	ExtraHeaders Yes
	ExtraHeadersPrefix "HTTP_"
	ExtraHeadersIgnore "Content-Type Content-Length"
	SendCRLF Yes
</Object>