⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dispatch.cpp

📁 mini http server,可以集成嵌入到程序中,实现简单的web功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*____________________________________________________________________________*\
 *

 Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.

 These sources, libraries and applications are
 FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
 as long as the following conditions are adhered to.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer. 

 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in
    the documentation and/or other materials provided with the
    distribution.

 3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 OF THE POSSIBILITY OF SUCH DAMAGE.

 *____________________________________________________________________________*|
 *
 * $Source: /cvsroot/pi3web/Pi3Web_200/Source/Pi3API/Dispatch.cpp,v $
 * $Date: 2003/05/13 18:42:09 $
 *
 Description:
	HTTP Dispatch class. Dispatch requests through other objects.

	TODO:
		- this file could do with a rewrite

\*____________________________________________________________________________*/
//$SourceTop:$

#include <assert.h>
#include "DblList.h"
#include "PIString.h"
#include "StrToken.h"
#include "PICompat.h"

#include "Pi2API.h"
#include "PIHTTP.h"
#include "HTTPCore.h"
#include "DblList.h"

/*____________________________________________________________________________*\
 *
 Description:
\*____________________________________________________________________________*/
#define DISPATCHERDB_NAME					"Dispatcher DB"
#define HANDLERSDB_NAME						"Handler DB"
#define KEY_CONF_KEEPOPEN					"KeepOpen"
#define KEY_CONF_KEEPOPENCOUNT				"KeepOpenCount"
#define KEY_CONF_KEEPOPENTIMEOUT			"KeepOpenTimeout"
#define KEY_CONF_DEFAULTHOST				"DefaultHost"
#define VALUE_OFF							"Off"

#define CONFIG_ERR(this, msg)   \
	{ PILog_addMessage( PIObject_getDB(this), \
	PIObject_getConfigurationDB(this), PILOG_ERROR, (msg) ); }
/*
#define D { cerr << __FILE__ << ", " << __LINE__ << ": " << endl; }
*/
#define D

#if 0
	/*
	** HTML documentation for this object
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	Dispatcher

Description:
	The dispatcher is the logic object, which is loaded once by the server object and executed
	for each connection. This object will handle basic HTTP configuration stuff at startup time
	like MIME-mappings and general functionality per physical connection like Keep-Alive behaviour.
	The main functionality is to distribute the request to the several logical handlers, which are
	configured for the dispatcher.

Options:

<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>AddMIMEEntry
<TD>-
<TD>"&lt;ext&gt;SP&lt;MIME-type&gt;"
<TD>To register mime entry by server
<TD>AddMIMEEntry "exe	application/octet-stream"

<TR>
<TD>KeepOpen
<TD>-
<TD>"On","Off"
<TD>Control, if a physical connection is reused by multiple logical requests
<TD>KeepOpen "On"

<TR>
<TD>KeepOpenCount
<TD>-
<TD>nnnn
<TD>Number of reuses of physical connection
<TD>KeepOpenCount 100

<TR>
<TD>KeepOpenTimeout
<TD>-
<TD>n seconds or -1 (infinite) 
<TD>Timout for physical connection between logical requests
<TD>KeepOpenTimeout 5
		
<TR>
<TD>ServerRoot
<TD>-
<TD>absolute or relative path
<TD>Root directory of the server
<TD>ServerRoot ./../

<TR>
<TD>ServerStamp
<TD>-
<TD>Any string
<TD>Server name stamp used in headers
<TD>ServerStamp Pi3Web/2.0.0

<TR>
<TD>DefaultHost
<TD>-
<TD>An configured virtual host object
<TD>Logical host used per default
<TD>DefaultHost MainVirtualHostInformation

<TR>
<TD>MIMEFile
<TD>-
<TD>Valid name of an existing file
<TD>A file containing mime entries
<TD>MIMEFile "Fragment/Mime.typ"

<TR>
<TD>ErrorLogFile
<TD>-
<TD>A valid file name
<TD>A file to write error logs
<TD>ErrorLogFile "Logs/error.txt"

<TR>
<TD>DebugLogFile
<TD>-
<TD>A valid file name
<TD>A file to write debug logs
<TD>DebugLogFile "Logs/debug.txt"

<TR>
<TD>DebugBeforeHandler
<TD>-
<TD>A Pi3Expression
<TD>An expression to log before a handler is called
<TD>DebugBeforeHandler "[$P:$k] &align(,$X)&align(,$X) |--&gt; $T -------- &align($N,12) &align($R,12) $n \"$r\" $s"

<TR>
<TD>DebugAfterHandler
<TD>-
<TD>A Pi3Expression
<TD>An expression to log after a handler is called
<TD>DebugAfterHandler "[$P:$k] &align(,$X)&align(,$X) |&lt;-- $T &align($D,8) &align($N,12) &align($R,12) $n \"$r\" $s"

</TABLE>
<STRONG>-</STRONG> in the <IT>default</IT> indicates no default<BR>
<STRONG>+</STRONG> in the <IT>default</IT> indicates the field is mandatory<BR>

<H4>Description of Options</H4>
<H5>AddMIMEEntry</H5>
The respective MIME type will be registered in the server. If a resource with the same extension
is retrieved by a request, the <i>Content-Type</i> header will be set to the appropriate MIME-type.

<H5>KeepOpen</H5>
This flag determines, if a physical connection could be reused through multiple logical connections
(HTTP request/response pairs). This could significant increase the performance. The default value
is "On". But it also depends on the headers sent by the client if actual the <i>Keep-Alive</i>
protocol is used.

<H5>KeepOpenCount</H5>
The optional value determines the maximum number of connection reuses. This is useful, because otherwise
the server would not fall through to the process and thread handling routines, which would kill a process
after a number of requests. This number is configured per server process, because Pi3Web logic may
have and some OS libraries actual have significant memory leaks (Solaris). The default value is 100.

<H5>KeepOpenTimeout</H5>
The optional value determines the timeout value between the requests of a reused physical connection.
This timeout value should be selected shorter than the general IO receive timeout configured with the
TCPIOObject. The default timeout value is 5 seconds.

<H5>ServerRoot</H5>
The relative or absolute path specifies the root directory of the server. All relative paths used
in mappings will be in relation to this directory. However, it is not possible to look above the
ServerRoot with "special" URLs like "/..". If a relative path is given as ServerRoot, the resulting
path is relative to the startup directory (normaly ./Pi3Web/bin),

<H5>ServerStamp</H5>
The string you set as ServerStamp is sent back to the client in the respective HTTP response header.
Several companies watch to the internet and the server activities. Their statisitcs are based on
the ServerStamps they get back from the web hosts.

<H5>DefaultHost</H5>
The value must point to a valid virtual host object. This object is used per default, if no other
mapping occurs during request phase <i>HOSTMAP</i>.

<H5>MIMEFile</H5>
This is an alternative way to configure the MIME-type mapping of the Pi3Web server. The file must
contain one MIME-Mapping per line specified in the format

<PRE>
# Comment
application/octet-stream    bin dms lha lzh exe class
application/pdf             pdf
</PRE>

<H5>ErrorLogFile</H5>
The optional specified file will be used to write server error log information. If the file does
not exist it will be created. If the file exists it will be overwritten. By omitting this option
the error log will be switched off.

<H5>DebugLogFile</H5>
The optional specified file will be used to write server debug log information. If the file does
not exist it will be created. If the file exists it will be overwritten. By omitting this option
the debug log will be switched off. Because debug log is very expensive it should not be used with
a production server. It is intended to use for development, performance optimization and
troubleshooting purposes only.
<p>The format of the log entries is specified by the options <i>DebugBeforeHandler</i> and
<i>DebugAfterHandler</i>. Per handler call 2 debug entries will be written before and after
the handler is executed. If nested handlers are called, the corresponding debug log lines are
indented.

<H5>DebugBeforeHandler</H5>
This Pi3Expression specifies the line which is logged before a certain handler is called.

<H5>DebugAfterHandler</H5>
This Pi3Expression specifies the line which is logged before a certain handler is called.

Phase:
	All phases

Returns:
	The execute function of the Dispatcher object will return either PIAPI_COMPLETED, PIAPI_ABORT
	or PIAPI_ERROR according to the status returned by the logical extensions (handlers).

Note:
Example:
	<PRE>

	&lt;Object&gt;
		Name HTTPLogicObject
		Class HTTPDispatcherClass

		Handlers Start Scripts Default

		AddMIMEEntry "xls	application/msexcel"
		AddMIMEEntry "xlt	application/msexcel"
		AddMIMEEntry "doc	application/msword"
		AddMIMEEntry "bin	application/octet-stream"
		AddMIMEEntry "dms	application/octet-stream"
		AddMIMEEntry "lha	application/octet-stream"
		AddMIMEEntry "lzh	application/octet-stream"
		AddMIMEEntry "exe	application/octet-stream"
		.
		.
		.
		AddMIMEEntry "imap	internal/x-imagemap"
		DefaultMIMEType "application/octet-stream"

		KeepOpen "On"
		KeepOpenCount 100
		KeepOpenTimeout 5

		ServerRoot ./../
		ServerStamp Pi3Web/2.0.0
		DefaultHost MainVirtualHostInformation
#		MIMEFile "Fragment/Mime.typ"
		ErrorLogFile "Logs/error.txt"
#		DebugLogFile "Logs/debug.txt"
		DebugBeforeHandler "[$P:$k] &align(,$X)&align(,$X) |--&gt; $T \
-------- &align($N,12) &align($R,12) $n \"$r\" $s"
		DebugAfterHandler "[$P:$k] &align(,$X)&align(,$X) |&lt;-- $T \
&align($D,8) &align($N,12) &align($R,12) $n \"$r\" $s"
	&lt;/Object&gt;

	</PRE>

/*___+++HTMLDOC_END+++___*/
#endif


/*____________________________________________________________________________*\
 *
 Class:
 Description:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -