#!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>

# ===
#
# Internet.pi3
#
#	Pi3 Configuration file for Internet server.
#
#	- Serve files only (see Devel.pi3 for CGI and SSI support).
#	- Bare bones functionality.
#	- Use If-Modified-Since headers (ConditionalGet handler).
#
# ===

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

# ---
#
# Default
#
#	Handle initial phases of requests, typically before we have enough
#	information to select different execution paths
#
# ---
<Object>
	Name Default
	Class FlexibleHandlerClass
	Init ReturnCode ReturnCode="COMPLETED"
	Headers ReadRequestHeaders
	HostMap ReturnCode ReturnCode="COMPLETED"

	# Change virtual path based on error code from redirections
	Mapping MapToErrorDocument \
		IgnoreStatus="0 200" \
		DefaultMessage="/errors/unknown.htm" \
		301="/errors/301.htm" \
		304="/errors/304.htm" \
		400="/errors/400.htm" \
		401="/errors/401.htm" \
		403="/errors/403.htm" \
		404="/errors/404.htm" \
		500="/errors/500.htm" \
		501="/errors/501.htm"
#
#	Uncomment this to map user directories that are prefixed with a tilda
#	tilda (~), i.e.
#
#    /~jroy/hello.html --> /home/jroy/public_html/hello.html
#
#	Mapping UserDirectory UserDirectoryRoot="/home/" \
#		HTMLDirectory="/public_html"

	Mapping PathMapper From="/errors/" To="Fragment/" 
	Mapping PathMapper From="/icons/" To="Icons/" 

	# Document root
	Mapping PathMapper From="/" To="WebRoot/" 
	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"
	
	# ConditionalGet sends back a '304 Not Modified' under the right
	# circumstances include the presence of a client If-Modified-Since
	# header
	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"

	#
	# Most requests refer to a file, so put that first 
	#
	# Handle the request by sending a file if
	#	The Media type of the resource does not match internal/* 
	#
	Handle Condition="¬(®exp('internal/*',$c))" SendFile
	Handle Condition="&cmp($c,internal/x-directory)" ListDirectory
	Handle Condition="&cmp($c,internal/x-imagemap)" ImageMap
	Log ExpressionLogger File="Logs/access.txt" OpenMode="a" \
		Expression="$A $h - [$t] \"$r\" $s $b$M"
</Object>

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

	# see Features.pi3 or Devel.pi3 for debugging options
#	DebugLogFile "Logs/debug.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>