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

📄 dirindex.cpp

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

 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/HTTP/DirIndex.cpp,v $
 * $Date: 2004/07/04 19:20:56 $
 *
 Description:
	Directory Handler. Flexible generation of directory indexes.

\*____________________________________________________________________________*/
//$SourceTop:$

#include <ctype.h>
#include <stdio.h>
#include <fstream.h>

#include "DeQuote.h"
#include "PIStrStr.h"
#include "StrToken.h"
#include "SwapStr.h"

#include "Pi3Expr.h"
#include "HandBase.h"
#include "HTTPCore.h"
#include "HTTPUtil.h"

/*____________________________________________________________________________*\
 *
 Description:
	Documentation.
\*____________________________________________________________________________*/
#if 0
	/*
	** HTML documentation for this handler
	*/
/*___+++HTMLDOC_BEGIN+++___*/
Name:
	DirectoryIndex

Description:
	Generate directory indexes based on flexible criteria and send the
	output directly to the remote client. HTML directory indexes are
	optional sortable and can tag files, which are newer than a threshold
	date.

	The directory listing sent to the user is composed of the following
	document in order
<LIST>
<ITEM>An optional header, specified by 'HeaderPattern'.
<ITEM>An optional header file from the directory, specified by 'HeaderFile'.
<ITEM>An optional list start pattern, specified by 'ListTop'.
<ITEM>Every applicable file in the directory, output format is specified
by 'FilePattern'.
<ITEM>An optional list end pattern, specified by 'ListBottom'.
<ITEM>An optional footer file, specified by 'FooterFile'.
<ITEM>An optional footer pattern, specified by 'FooterPattern'.
</LIST>

Options:
<H5>Overview</H5>
<TABLE BORDER=1>
<TH>Option
<TH>Default
<TH>Values
<TH>Short Description
<TH>Example(s)

<TR>
<TD>HeaderPattern
<TD>-
<TD>A Pi3Expression
<TD>Output first
<TD>HeaderPattern="<HTML><TITLE>Index of %p</TITLE>"

<TR>
<TD>HeaderFile
<TD>-
<TD>A filename
<TD>File sent before listing
<TD>HeaderFile="HEADER"

<TR>
<TR>
<TD>FooterFile
<TD>-
<TD>A filename
<TD>File send after listing
<TD>FooterFile="FOOTER"

<TR>
<TD>ListTop
<TD>-
<TD>A Pi3Expression
<TD>Output before file iteration
<TD>ListTop="<H2>Directory listing of %p</H2>"

<TR>
<TD>FilePattern
<TD>+
<TD>A Pi3Expression
<TD>Determines output generated for every file
<TD>FilePattern="&abbrev(%n,32,...)&align(%s,10,right)"

<TR>
<TD>ListBottom
<TD>-
<TD>A Pi3Expression
<TD>Output after file iteration
<TD>ListBottom="<HR>"

<TR>
<TD>FooterPattern
<TD>-
<TD>A Pi3Expression
<TD>Output last
<TD>FooterPattern="</HTML>"

<TR>
<TD>DescriptionFile
<TD>-
<TD>&lt;filename&gt;
<TD>File with descriptions of files
<TD>DescriptionFile=".desc"

<TR>
<TD>MIMEIcon
<TD>-
<TD>"MIME icon_relative_path"
<TD>Maps a MIME specification to an icons path
<TD>MIMEIcon="text/* /icons/text.gif"; MIMEIcon="* /icons/unknown.gif"

<TR>
<TD>Include
<TD>-
<TD>Regular expression
<TD>A file pattern to include
<TD>Include="*"

<TR>
<TD>Exclude
<TD>-
<TD>Regular expression
<TD>A file pattern to exclude
<TD>Exclude="."

<TR>
<TD>HTTPEquiv
<TD>-
<TD>&lt;rfc822-variable&gt;: &lt;content&gt;
<TD>An rfc822 response header to send
<TD>HTTPEquiv="Content-Type: text/plain"

<TR>
<TD>LastModifiedFormat
<TD>-
<TD>"%a %b %d %Y %T GMT"
<TD>Format for file last modified date
<TD>LastModifedFormat="%a %b %d %Y %T GMT"

<TR>
<TD>SwapFileName
<TD>-
<TD>"file1 file2"
<TD>Replace any file1 name with file2
<TD>SwapFileName=".. Parent Directory"

<TR>
<TD>NewPattern
<TD>-
<TD>A wellformed HTML fragment
<TD>Replaces the %m parameter
<TD>NewPattern "&lt;IMG SRC="/icons/new.gif"&gt;"

<TR>
<TD>NewThreshold
<TD>-
<TD><number>
<TD>Threshold for new files in days
<TD>NewThreshold 365

<TR>
<TD>Options
<TD>-
<TD>Options flags as described below
<TD>Options which effect behaviour
<TD>Options "Name | AltName | Size etc."

</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>
	HeaderPattern
</H5>
Specifies the pattern that will be output first. See the directive
'FilePattern' for a description of parameters.

<H5>
	FooterPattern
</H5>
Specifies the pattern that will be output last. See the directive
'FilePattern' for a description of parameters.

<H5>
	HeaderFile
</H5>
Specifies the name of a file that will be sent before the directory listing
and ListTop pattern but after the HeaderPattern pattern.
Typically this file would also be excluded
from the directory list by naming it in an 'Exclude' directive.

<H5>
	FooterFile
</H5>
Specified the name of a file that will be sent after the directory
index and ListBottom pattern, but before the FooterPattern pattern.
Typically this file would also be excluded
from the directory listing by naming it in an 'Exclude' directive.

<H5>
	ListTop
</H5>
Determines the output generated before iterating through files. See 
'FilePattern' for a description of parameters.

<H5>
	FilePattern
</H5>
Determines the output generated for every file which meets the criteria
specified by 'Include' and 'Exclude'. This is a Pi3Expression with
the following parameters specific to directory indexing:
<CENTER>
<TABLE BORDER=1>
<TH>Pi3Expression parameter
<TH>Evaluates to

<TR><TD>%a<TD>Alternate file name
<TR><TD>%b<TD>Empty if file is not a directory
<TR><TD>%c<TD>Media (MIME) type
<TR><TD>%d<TD>Description of the file if available
<TR><TD>%l<TD>File last modified date
<TR><TD>%m<TD>Mark file conditional as new
<TR><TD>%n<TD>File/directory name
<TR><TD>%i<TD>Icon
<TR><TD>%p<TD>Directory (virtual path)
<TR><TD>%r<TD>Relative path
<TR><TD>%s<TD>Size in decimal
<TR><TD>%S<TD>Size of all regular files in decimal
<TR><TD>%N<TD>Number of all files and directories

</TABLE>
</CENTER>

<H5>
	ListBottom
</H5>
Determines the output generated after iterating through files. See
'FilePattern' for a description of parameters.

<H5>
	MIMEIcon
</H5>
Specify a mapping from a MIME type to an icons relative path. The MIME type
is considered the first part of the specified string up the the first
whitespace. The MIME pattern is a simple regular expression which matches
wildcards (*). 
This directive
can be repeated multiple times to denote different MIME mappings. Typically
the pattern '*' would be mapped to a default icon.

<H5>
	DescriptionFile
</H5>
Specifies a file with descriptions of other files. Each line in the
file is of the form
<PRE>
	filename|its description
</PRE>
Whitespace will not be stripped out of the filename or description.
Typically this file would also be excluded from the directory list by
naming it in an 'Exclude' directive.
If present a files description can be accessed via parameter %d.

<H5>
	Include
</H5>
Specifies the file patterns which will included in the directory listing. The
pattern is a simple regular expression (allows wildcards (*)). The directory
indexer first allows file then excludes a subset of them with the 
'Exclude' directive. The include directive may be repeated multiple times.

<H5>
	Exclude
</H5>
Specifies the file patterns which will exclude from the directory listing. The
pattern is a simple regular expression (allows wildcards (*)). The directory
indexer first allows files using 'Include' patterns  then excludes a subset of
them. The exclude directive may be repeated multiple times.

<H5>
	LastModifiedFormat
</H5>
Specifies the format (according to the C library strftime() function) for
the last modified date of the file. The directive enables parameter
%l to contain this format.

<H5>
	HTTPEquiv
</H5>
Specifies an HTTP header to be sent to the remote client as part of the
response. This directive may be used to override the default response
media of 'text/html'. This pattern may be repeated multiple times.

<H5>
	SwapFileName
</H5>
If the current file matches the specified filename the replacement filename
will be subtituted. SwapFileName takes a string as argument, the pattern up
to the first whitespace character is considered the 'old' filename, subsequent
characters, excluding leading whitespace are considered the 'new' filename.

<H5>
	NewPattern
</H5>
The pattern must be a wellformed HTML fragment. It replaces the %m parameter
if the option "NewTag" is set and the file is newer than the specified
NewTreshold.

<H5>
	NewThreshold
</H5>
This parameter specifies the number of days, within a file or directory is
considered as new and tagged accordingly.

<H5>
	Options
</H5>
Specifies options which effect the behaviour of directory indexing, many
available facilities are optional for performance reasons, the table below
lists the available option flags with thier meanings.
<CENTER>
<TABLE BORDER=1>
<TH>Option
<TH>Meaning
<TR>

<TD>Name
<TD>Lookup the name of the file or directory and put it in parameter %n

<TR>
<TD>AltName
<TD>Lookup the alternate name of the file or directory and put it in 
parameter %a.

<TR>
<TD>Icon
<TD>Evaluate the appropriate icon file for the file based on its
extension and MIME type and make it available through parameter %i

<TR>
<TD>MIMEType
<TD>Record the MIME type of the current file in parameter %c.

<TR>
<TD>RelPath
<TD>Record the relative path (path component of URL) to the file in parameter
%r.

<TR>
<TD>Size
<TD>Record the filesize of the current file in parameter %s in full decimal.
If the current file is a directory '-' is placed in %s.
If more than one of 'Size', 'AbbrevSize' or 'FormatSize' are used the
results are undefined.

<TR>
<TD>AbbrevSize
<TD>The filesize is abbreviated and placed in parameter %s. The abbreviated
file size has the following form '<1K', '3M', '2K', etc.
If more than one of 'Size', 'AbbrevSize' or 'FormatSize' are used the
results are undefined.

<TR>
<TD>FormatSize
<TD>The filesize is formatted with the comma (',') character seperating
thousands. The formatted size of place in parameter $s.
If more than one of 'Size', 'AbbrevSize' or 'FormatSize' are used the
results are undefined.

<TR>
<TD>SortDesc
<TD>The sort order is alphabetical descending. If this option is omitted
the sort order is ascending by default.

<TR>
<TD>SortCase
<TD>The sort order is case sensitive. If this option is omitted
the sort order is case insensitive by default.

<TR>
<TD>SortType
<TD>The sort order is determined by the file extension. Directories are
on the top of the sort order.

<TR>
<TD>SortName
<TD>The sort order is determined by the name of the file or directory.

<TR>
<TD>SortSize
<TD>The sort order is determined by the filesize.

<TR>

⌨️ 快捷键说明

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