httpd.sgml
来自「eCos操作系统源码」· SGML 代码 · 共 551 行 · 第 1/2 页
SGML
551 行
<!-- =============================================================== --><!-- --><!-- 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 HTTPserver for use with applications in eCos. This server is specificallyaimed at the remote control and monitoring requirements of embeddedapplications. For this reason the emphasis is on dynamically generatedcontent, simple forms handling and a basic CGI interface. It is<emphasis>not</emphasis> intended to be a general purpose server fordelivering arbitrary web content. For these purposes a port of theGoAhead web server is available from <ulinkurl="www.goahead.com">www.goahead.com</ulink>.</para><para>This server is also capable of serving content using IPv6 whenthe eCos configuration contains IPv6.</para></sect1><sect1 id="net-httpd-organization"><title>Server Organization</title><para>The server consists of one or more threads running in parallel to anyapplication threads and which serve web pages to clients. Apart fromdefining content, the application does not need to do anything tostart the HTTP server.</para><para>The HTTP server is, by default, started by a static constructor. Thissimply creates an initial thread and sets it running. Since this iscalled before the scheduler is started, nothing will happen until theapplication calls <function>cyg_scheduler_start()</function>. Theserver thread can also be started explicitly by the application, seethe <literal>CYGNUM_HTTPD_SERVER_AUTO_START</literal> option fordetails.</para><para>When the thread gets to run it first optionally delays for some periodof time. This is to allow the application to perform anyinitialization free of any interference from the HTTP server. When thethread does finally run it creates a socket, binds it to the HTTPserver port, and puts it into listen mode. It will then create anyadditional HTTPD server threads that have been configured beforebecoming a server thread itself.</para><para>Each HTTPD server thread simply waits for a connection to be made tothe server port. When the connection is made it reads the HTTP requestand extracts the filename being accessed. If the request also containsform data, this is also preserved. The filename is then looked up in atable.</para><para>Each table entry contains a filename pattern string, apointer to a handler function, and a user defined argument for thefunction. Table entries are defined using the same link-time tablebuilding mechanism used to generate device tables. This is all handledby the <literal>CYG_HTTPD_TABLE_ENTRY()</literal> macro which has thefollowing 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 thetable entry since C does not allow us to define anonymous datastructures. This name should be chosen so that it is unique and doesnot 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 ofcode can define server table entries, and so long as the patterns donot clash they can be totally oblivious of each other. However, notealso that this mechanism does not guarantee the order in which entriesappear, this depends on the order of object files in the link, whichcould vary from one build to the next. So any tricky pattern matchingthat relies on this may not always work.</para><para>A request filename matches an entry in the table if either it exactlymatches the pattern string, or if the pattern ends in an asterisk, andit 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 startingwith "/monitor/thread-".</para><para>When a pattern is matched, the hander function is called. It has thefollowing 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 tothe client: anything output through this stream will be returned tothe browser. The <parameter>filename</parameter> argument is thefilename 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 thetable entry.</para><para>The handler is entirely responsible for generating the response to theclient, both HTTP header and content. If the handler decides that itdoes not want to generate a response it can return<literal>false</literal>, in which case the table scan is resumed foranother match. If no match is found, or no handler returns true, thena default response page is generated indicating that the requestedpage cannot be found.</para><para>Finally, the server thread closes the connection to the client andloops 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. Itdefaults to the standard HTTP port number 80. It may be changed to adifferent number if, for example, another HTTP server is using themain 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 toservice HTTP requests. If you expect to serve complex pages with manyimages or other components that are fetched separately, or if anypages may take a long time to send, then it may be useful to increasethe number of server threads. For most uses, however, the connectionqueuing in the TCP/IP stack and the speed with which each page isgenerated, 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 prioritydepends on the importance of the server relative to the rest of thesystem. The default is to put them in the middle of the priority rangeto provide reasonable response without impacting genuine high prioritythreads.</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 HTTPDthreads. The actual stack size allocated will be this value plus thevalues 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 ofeach HTTP request. If you expect to use particularly long URLs or havevery complex forms, this should be increased.</para></sect2><sect2><title><literal>CYGNUM_HTTPD_SERVER_AUTO_START</literal></title><para>This option causes the HTTP Daemon to be started automatically duringsystem initialization. If this option is not set then the applicationmust start the daemon explicitly by calling<function>cyg_httpd_startup()</function>. This option is set bydefault.</para></sect2><sect2><title><literal>CYGNUM_HTTPD_SERVER_DELAY</literal></title><para>This defines the number of system clock ticks that the HTTP serverwill wait before initializing itself and spawning any extra serverthreads. This is to give the application a chance to initializeproperly 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 handlerfunctions make calls to <function>fprintf()</function> and<function>fputs()</function>. Such handler functions would end up amass of print calls, with the actual structure of the HTML page hiddenin the format strings and arguments, making maintenance and debuggingvery difficult. Such an approach would also result in the definitionof many, often only slightly different, format strings, leading tounnecessary bloat.</para><para>In an effort to expose the structure of the HTML in the structure ofthe C code, and to maximize the sharing of string constants, the<filename>cyg/httpd/httpd.h</filename> header file defines a set ofhelper functions and macros. Most of these are wrappers for predefinedprint calls on the <parameter>client</parameter> stream passed to thehander function. For examples of their use, see the System Monitorexample.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?