1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
12 >Server Organization</TITLE
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="Embedded HTTP Server"
23 HREF="net-httpd-chapter.html"><LINK
25 TITLE="Embedded HTTP Server"
26 HREF="net-httpd-chapter.html"><LINK
28 TITLE="Server Configuration"
29 HREF="net-httpd-configuration.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="net-httpd-chapter.html"
65 >Chapter 48. Embedded HTTP Server</TD
71 HREF="net-httpd-configuration.html"
85 NAME="NET-HTTPD-ORGANIZATION">Server Organization</H1
87 >The server consists of one or more threads running in parallel to any
88 application threads and which serve web pages to clients. Apart from
89 defining content, the application does not need to do anything to
90 start the HTTP server.</P
92 >The HTTP server is started by a static constructor. This simply
93 creates an initial thread and sets it running. Since this is called
94 before the scheduler is started, nothing will happen until the
97 >cyg_scheduler_start()</TT
100 >When the thread gets to run it first optionally delays for some period
101 of time. This is to allow the application to perform any
102 initialization free of any interference from the HTTP server. When the
103 thread does finally run it creates a socket, binds it to the HTTP
104 server port, and puts it into listen mode. It will then create any
105 additional HTTPD server threads that have been configured before
106 becoming a server thread itself.</P
108 >Each HTTPD server thread simply waits for a connection to be made to
109 the server port. When the connection is made it reads the HTTP request
110 and extracts the filename being accessed. If the request also contains
111 form data, this is also preserved. The filename is then looked up in a
114 >Each table entry contains a filename pattern string, a
115 pointer to a handler function, and a user defined argument for the
116 function. Table entries are defined using the same link-time table
117 building mechanism used to generate device tables. This is all handled
120 >CYG_HTTPD_TABLE_ENTRY()</TT
121 > macro which has the
130 CLASS="PROGRAMLISTING"
131 > #include <cyg/httpd/httpd.h>
133 CYG_HTTPD_TABLE_ENTRY( __name, __pattern, __handler, __arg ) </PRE
143 > argument is a variable name for the
144 table entry since C does not allow us to define anonymous data
145 structures. This name should be chosen so that it is unique and does
146 not pollute the name space. The <TT
152 argument is the match pattern. The <TT
158 argument is a pointer to the handler function and
164 > the user defined value.</P
166 >The link-time table building means that several different pieces of
167 code can define server table entries, and so long as the patterns do
168 not clash they can be totally oblivious of each other. However, note
169 also that this mechanism does not guarantee the order in which entries
170 appear, this depends on the order of object files in the link, which
171 could vary from one build to the next. So any tricky pattern matching
172 that relies on this may not always work.</P
174 >A request filename matches an entry in the table if either it exactly
175 matches the pattern string, or if the pattern ends in an asterisk, and
176 it matches everything up to that point. So for example the pattern
177 "/monitor/threads.html" will only match that exact filename,
178 but the pattern "/monitor/thread-*" will match
179 "/monitor/thread-0040.html",
180 "/monitor/thread-0100.html" and any other filename starting
181 with "/monitor/thread-".</P
183 >When a pattern is matched, the hander function is called. It has the
184 following prototype:</P
192 CLASS="PROGRAMLISTING"
193 >cyg_bool cyg_httpd_handler(FILE *client,
206 > argument is the TCP connection to
207 the client: anything output through this stream will be returned to
214 filename from the HTTP request and the <TT
220 argument is any form response data, or NULL if none was sent. The
226 > argument is the user defined value from the
229 >The handler is entirely responsible for generating the response to the
230 client, both HTTP header and content. If the handler decides that it
231 does not want to generate a response it can return
235 >, in which case the table scan is resumed for
236 another match. If no match is found, or no handler returns true, then
237 a default response page is generated indicating that the requested
238 page cannot be found.</P
240 >Finally, the server thread closes the connection to the client and
241 loops back to accept a new connection.</P
248 SUMMARY="Footer navigation table"
259 HREF="net-httpd-chapter.html"
277 HREF="net-httpd-configuration.html"
287 >Embedded HTTP Server</TD
293 HREF="net-httpd-chapter.html"
301 >Server Configuration</TD