?? httpd.sgml
字號(hào):
<!-- =============================================================== -->
<!-- -->
<!-- httpd.sgml -->
<!-- -->
<!-- eCos HTTP Server -->
<!-- -->
<!-- =============================================================== -->
<!-- ####COPYRIGHTBEGIN#### -->
<!-- -->
<!-- =============================================================== -->
<!-- Copyright (C) 2002 Nick Garnett -->
<!-- This material may be distributed only subject to the terms -->
<!-- and conditions set forth in the Open Publication License, v1.0 -->
<!-- or later (the latest version is presently available at -->
<!-- http://www.opencontent.org/openpub/) -->
<!-- Distribution of the work or derivative of the work in any -->
<!-- standard (paper) book form is prohibited unless prior -->
<!-- permission obtained from the copyright holder -->
<!-- =============================================================== -->
<!-- -->
<!-- ####COPYRIGHTEND#### -->
<!-- =============================================================== -->
<!-- #####DESCRIPTIONBEGIN#### -->
<!-- -->
<!-- ####DESCRIPTIONEND#### -->
<!-- =============================================================== -->
<part id="net-httpd">
<title>Embedded HTTP Server</title>
<chapter id="net-httpd-chapter">
<title>Embedded HTTP Server</title>
<sect1 id="net-httpd-intro">
<title>Intrduction</title>
<para>
The <emphasis>eCos</emphasis> HTTPD package provides a simple HTTP
server for use with applications in eCos. This server is specifically
aimed at the remote control and monitoring requirements of embedded
applications. For this reason the emphasis is on dynamically generated
content, simple forms handling and a basic CGI interface. It is
<emphasis>not</emphasis> intended to be a general purpose server for
delivering arbitrary web content. For these purposes a port of the
GoAhead web server is available from <ulink
url="www.goahead.com">www.goahead.com</ulink>.
</para>
</sect1>
<sect1 id="net-httpd-organization">
<title>Server Organization</title>
<para>
The server consists of one or more threads running in parallel to any
application threads and which serve web pages to clients. Apart from
defining content, the application does not need to do anything to
start the HTTP server.
</para>
<para>
The HTTP server is started by a static constructor. This simply
creates an initial thread and sets it running. Since this is called
before the scheduler is started, nothing will happen until the
application calls <function>cyg_scheduler_start()</function>.
</para>
<para>
When the thread gets to run it first optionally delays for some period
of time. This is to allow the application to perform any
initialization free of any interference from the HTTP server. When the
thread does finally run it creates a socket, binds it to the HTTP
server port, and puts it into listen mode. It will then create any
additional HTTPD server threads that have been configured before
becoming a server thread itself.
</para>
<para>
Each HTTPD server thread simply waits for a connection to be made to
the server port. When the connection is made it reads the HTTP request
and extracts the filename being accessed. If the request also contains
form data, this is also preserved. The filename is then looked up in a
table.
</para>
<para>
Each table entry contains a filename pattern string, a
pointer to a handler function, and a user defined argument for the
function. Table entries are defined using the same link-time table
building mechanism used to generate device tables. This is all handled
by the <literal>CYG_HTTPD_TABLE_ENTRY()</literal> macro which has the
following format:
</para>
<programlisting width=72>
#include <cyg/httpd/httpd.h>
CYG_HTTPD_TABLE_ENTRY( __name, __pattern, __handler, __arg )
</programlisting>
<para>
The <parameter>__name</parameter> argument is a variable name for the
table entry since C does not allow us to define anonymous data
structures. This name should be chosen so that it is unique and does
not pollute the name space. The <parameter>__pattern</parameter>
argument is the match pattern. The <parameter>__handler</parameter>
argument is a pointer to the handler function and
<parameter>__arg</parameter> the user defined value.
</para>
<para>
The link-time table building means that several different pieces of
code can define server table entries, and so long as the patterns do
not clash they can be totally oblivious of each other. However, note
also that this mechanism does not guarantee the order in which entries
appear, this depends on the order of object files in the link, which
could vary from one build to the next. So any tricky pattern matching
that relies on this may not always work.
</para>
<para>
A request filename matches an entry in the table if either it exactly
matches the pattern string, or if the pattern ends in an asterisk, and
it matches everything up to that point. So for example the pattern
"/monitor/threads.html" will only match that exact filename,
but the pattern "/monitor/thread-*" will match
"/monitor/thread-0040.html",
"/monitor/thread-0100.html" and any other filename starting
with "/monitor/thread-".
</para>
<para>
When a pattern is matched, the hander function is called. It has the
following prototype:
</para>
<programlisting width=72>
cyg_bool cyg_httpd_handler(FILE *client,
char *filename,
char *formdata,
void *arg);
</programlisting>
<para>
The <parameter>client</parameter> argument is the TCP connection to
the client: anything output through this stream will be returned to
the browser. The <parameter>filename</parameter> argument is the
filename from the HTTP request and the <parameter>formdata</parameter>
argument is any form response data, or NULL if none was sent. The
<parameter>arg</parameter> argument is the user defined value from the
table entry.
</para>
<para>
The handler is entirely responsible for generating the response to the
client, both HTTP header and content. If the handler decides that it
does not want to generate a response it can return
<literal>false</literal>, in which case the table scan is resumed for
another match. If no match is found, or no handler returns true, then
a default response page is generated indicating that the requested
page cannot be found.
</para>
<para>
Finally, the server thread closes the connection to the client and
loops back to accept a new connection.
</para>
</sect1>
<!-- =============================================================== -->
<sect1 id="net-httpd-configuration">
<title>Server Configuration</title>
<para>
The HTTP server has a number of configuration options:
</para>
<sect2>
<title><literal>CYGNUM_HTTPD_SERVER_PORT</literal></title>
<para>
This option defines the TCP port that the server will listen on. It
defaults to the standard HTTP port number 80. It may be changed to a
different number if, for example, another HTTP server is using the
main HTTP port.
</para>
</sect2>
<sect2>
<title><literal>CYGDAT_HTTPD_SERVER_ID</literal></title>
<para>
This is the string that is reported to the client in the
"Server:" field of the HTTP header.
</para>
</sect2>
<sect2>
<title><literal>CYGNUM_HTTPD_THREAD_COUNT</literal></title>
<para>
The HTTP server can be configured to use more than one thread to
service HTTP requests. If you expect to serve complex pages with many
images or other components that are fetched separately, or if any
pages may take a long time to send, then it may be useful to increase
the number of server threads. For most uses, however, the connection
queuing in the TCP/IP stack and the speed with which each page is
generated, means that a single thread is usually adequate.
</para>
</sect2>
<sect2>
<title><literal>CYGNUM_HTTPD_THREAD_PRIORITY</literal></title>
<para>
The HTTP server threads can be run at any priority. The exact priority
depends on the importance of the server relative to the rest of the
system. The default is to put them in the middle of the priority range
to provide reasonable response without impacting genuine high priority
threads.
</para>
</sect2>
<sect2>
<title><literal>CYGNUM_HTTPD_THREAD_STACK_SIZE</literal></title>
<para>
This is the amount of stack to be allocated for each of the HTTPD
threads. The actual stack size allocated will be this value plus the
values of <literal>CYGNUM_HAL_STACK_SIZE_MINIMUM</literal> and
<literal>CYGNUM_HTTPD_SERVER_BUFFER_SIZE</literal>.
</para>
</sect2>
<sect2>
<title><literal>CYGNUM_HTTPD_SERVER_BUFFER_SIZE</literal></title>
<para>
This defines the size of the buffer used to receive the first line of
each HTTP request. If you expect to use particularly long URLs or have
very complex forms, this should be increased.
</para>
</sect2>
<sect2>
<title><literal>CYGNUM_HTTPD_SERVER_DELAY</literal></title>
<para>
This defines the number of system clock ticks that the HTTP server
will wait before initializing itself and spawning any extra server
threads. This is to give the application a chance to initialize
properly without any interference from the HTTPD.
</para>
</sect2>
</sect1>
<!-- =============================================================== -->
<sect1 id="net-httpd-html">
<title>Support Functions and Macros</title>
<para>
The emphasis of this server is on dynamically generated content,
rather than fetching it from a filesystem. To do this the handler
functions make calls to <function>fprintf()</function> and
<function>fputs()</function>. Such handler functions would end up a
mass of print calls, with the actual structure of the HTML page hidden
in the format strings and arguments, making maintenance and debugging
very difficult. Such an approach would also result in the definition
of many, often only slightly different, format strings, leading to
unnecessary bloat.
</para>
<para>
In an effort to expose the structure of the HTML in the structure of
the C code, and to maximize the sharing of string constants, the
<filename>cyg/httpd/httpd.h</filename> header file defines a set of
helper functions and macros. Most of these are wrappers for predefined
print calls on the <parameter>client</parameter> stream passed to the
hander function. For examples of their use, see the System Monitor
example.
</para>
<note>
<para>
All arguments to macros are pointers to strings, unless otherwise
stated. In general, wherever a function or macro has an
<parameter>attr</parameter> or <parameter>__attr</parameter>
parameter, then the contents of this string will be inserted into the
tag being defined as HTML attributes. If it is a NULL or empty string
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -