<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- Copyright (C) 2003 Red Hat, Inc. -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- This material may be distributed only subject to the terms -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- and conditions set forth in the Open Publication License, v1.0 -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- or later (the latest version is presently available at -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- http://www.opencontent.org/openpub/). -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- Distribution of the work or derivative of the work in any -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- standard (paper) book form is prohibited unless prior -->
|
<!-- permission is obtained from the copyright holder. -->
|
<!-- permission is obtained from the copyright holder. -->
|
<HTML
|
<HTML
|
><HEAD
|
><HEAD
|
><TITLE
|
><TITLE
|
>Server Organization</TITLE
|
>Server Organization</TITLE
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
><meta name="MSSmartTagsPreventParsing" content="TRUE">
|
<META
|
<META
|
NAME="GENERATOR"
|
NAME="GENERATOR"
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
|
"><LINK
|
"><LINK
|
REL="HOME"
|
REL="HOME"
|
TITLE="eCos Reference Manual"
|
TITLE="eCos Reference Manual"
|
HREF="ecos-ref.html"><LINK
|
HREF="ecos-ref.html"><LINK
|
REL="UP"
|
REL="UP"
|
TITLE="Embedded HTTP Server"
|
TITLE="Embedded HTTP Server"
|
HREF="net-httpd-chapter.html"><LINK
|
HREF="net-httpd-chapter.html"><LINK
|
REL="PREVIOUS"
|
REL="PREVIOUS"
|
TITLE="Embedded HTTP Server"
|
TITLE="Embedded HTTP Server"
|
HREF="net-httpd-chapter.html"><LINK
|
HREF="net-httpd-chapter.html"><LINK
|
REL="NEXT"
|
REL="NEXT"
|
TITLE="Server Configuration"
|
TITLE="Server Configuration"
|
HREF="net-httpd-configuration.html"></HEAD
|
HREF="net-httpd-configuration.html"></HEAD
|
><BODY
|
><BODY
|
CLASS="SECT1"
|
CLASS="SECT1"
|
BGCOLOR="#FFFFFF"
|
BGCOLOR="#FFFFFF"
|
TEXT="#000000"
|
TEXT="#000000"
|
LINK="#0000FF"
|
LINK="#0000FF"
|
VLINK="#840084"
|
VLINK="#840084"
|
ALINK="#0000FF"
|
ALINK="#0000FF"
|
><DIV
|
><DIV
|
CLASS="NAVHEADER"
|
CLASS="NAVHEADER"
|
><TABLE
|
><TABLE
|
SUMMARY="Header navigation table"
|
SUMMARY="Header navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TH
|
><TH
|
COLSPAN="3"
|
COLSPAN="3"
|
ALIGN="center"
|
ALIGN="center"
|
>eCos Reference Manual</TH
|
>eCos Reference Manual</TH
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="net-httpd-chapter.html"
|
HREF="net-httpd-chapter.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="80%"
|
WIDTH="80%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
>Chapter 48. Embedded HTTP Server</TD
|
>Chapter 48. Embedded HTTP Server</TD
|
><TD
|
><TD
|
WIDTH="10%"
|
WIDTH="10%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="bottom"
|
VALIGN="bottom"
|
><A
|
><A
|
HREF="net-httpd-configuration.html"
|
HREF="net-httpd-configuration.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"></DIV
|
WIDTH="100%"></DIV
|
><DIV
|
><DIV
|
CLASS="SECT1"
|
CLASS="SECT1"
|
><H1
|
><H1
|
CLASS="SECT1"
|
CLASS="SECT1"
|
><A
|
><A
|
NAME="NET-HTTPD-ORGANIZATION">Server Organization</H1
|
NAME="NET-HTTPD-ORGANIZATION">Server Organization</H1
|
><P
|
><P
|
>The server consists of one or more threads running in parallel to any
|
>The server consists of one or more threads running in parallel to any
|
application threads and which serve web pages to clients. Apart from
|
application threads and which serve web pages to clients. Apart from
|
defining content, the application does not need to do anything to
|
defining content, the application does not need to do anything to
|
start the HTTP server.</P
|
start the HTTP server.</P
|
><P
|
><P
|
>The HTTP server is started by a static constructor. This simply
|
>The HTTP server is started by a static constructor. This simply
|
creates an initial thread and sets it running. Since this is called
|
creates an initial thread and sets it running. Since this is called
|
before the scheduler is started, nothing will happen until the
|
before the scheduler is started, nothing will happen until the
|
application calls <TT
|
application calls <TT
|
CLASS="FUNCTION"
|
CLASS="FUNCTION"
|
>cyg_scheduler_start()</TT
|
>cyg_scheduler_start()</TT
|
>.</P
|
>.</P
|
><P
|
><P
|
>When the thread gets to run it first optionally delays for some period
|
>When the thread gets to run it first optionally delays for some period
|
of time. This is to allow the application to perform any
|
of time. This is to allow the application to perform any
|
initialization free of any interference from the HTTP server. When the
|
initialization free of any interference from the HTTP server. When the
|
thread does finally run it creates a socket, binds it to the HTTP
|
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
|
server port, and puts it into listen mode. It will then create any
|
additional HTTPD server threads that have been configured before
|
additional HTTPD server threads that have been configured before
|
becoming a server thread itself.</P
|
becoming a server thread itself.</P
|
><P
|
><P
|
>Each HTTPD server thread simply waits for a connection to be made to
|
>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
|
the server port. When the connection is made it reads the HTTP request
|
and extracts the filename being accessed. If the request also contains
|
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
|
form data, this is also preserved. The filename is then looked up in a
|
table.</P
|
table.</P
|
><P
|
><P
|
>Each table entry contains a filename pattern string, a
|
>Each table entry contains a filename pattern string, a
|
pointer to a handler function, and a user defined argument for the
|
pointer to a handler function, and a user defined argument for the
|
function. Table entries are defined using the same link-time table
|
function. Table entries are defined using the same link-time table
|
building mechanism used to generate device tables. This is all handled
|
building mechanism used to generate device tables. This is all handled
|
by the <TT
|
by the <TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>CYG_HTTPD_TABLE_ENTRY()</TT
|
>CYG_HTTPD_TABLE_ENTRY()</TT
|
> macro which has the
|
> macro which has the
|
following format:</P
|
following format:</P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="PROGRAMLISTING"
|
CLASS="PROGRAMLISTING"
|
> #include <cyg/httpd/httpd.h>
|
> #include <cyg/httpd/httpd.h>
|
|
|
CYG_HTTPD_TABLE_ENTRY( __name, __pattern, __handler, __arg ) </PRE
|
CYG_HTTPD_TABLE_ENTRY( __name, __pattern, __handler, __arg ) </PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
>The <TT
|
>The <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>__name</I
|
>__name</I
|
></TT
|
></TT
|
> argument is a variable name for the
|
> argument is a variable name for the
|
table entry since C does not allow us to define anonymous data
|
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
|
structures. This name should be chosen so that it is unique and does
|
not pollute the name space. The <TT
|
not pollute the name space. The <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>__pattern</I
|
>__pattern</I
|
></TT
|
></TT
|
>
|
>
|
argument is the match pattern. The <TT
|
argument is the match pattern. The <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>__handler</I
|
>__handler</I
|
></TT
|
></TT
|
>
|
>
|
argument is a pointer to the handler function and
|
argument is a pointer to the handler function and
|
<TT
|
<TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>__arg</I
|
>__arg</I
|
></TT
|
></TT
|
> the user defined value.</P
|
> the user defined value.</P
|
><P
|
><P
|
>The link-time table building means that several different pieces of
|
>The link-time table building means that several different pieces of
|
code can define server table entries, and so long as the patterns do
|
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
|
not clash they can be totally oblivious of each other. However, note
|
also that this mechanism does not guarantee the order in which entries
|
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
|
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
|
could vary from one build to the next. So any tricky pattern matching
|
that relies on this may not always work.</P
|
that relies on this may not always work.</P
|
><P
|
><P
|
>A request filename matches an entry in the table if either it exactly
|
>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
|
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
|
it matches everything up to that point. So for example the pattern
|
"/monitor/threads.html" will only match that exact filename,
|
"/monitor/threads.html" will only match that exact filename,
|
but the pattern "/monitor/thread-*" will match
|
but the pattern "/monitor/thread-*" will match
|
"/monitor/thread-0040.html",
|
"/monitor/thread-0040.html",
|
"/monitor/thread-0100.html" and any other filename starting
|
"/monitor/thread-0100.html" and any other filename starting
|
with "/monitor/thread-".</P
|
with "/monitor/thread-".</P
|
><P
|
><P
|
>When a pattern is matched, the hander function is called. It has the
|
>When a pattern is matched, the hander function is called. It has the
|
following prototype:</P
|
following prototype:</P
|
><TABLE
|
><TABLE
|
BORDER="5"
|
BORDER="5"
|
BGCOLOR="#E0E0F0"
|
BGCOLOR="#E0E0F0"
|
WIDTH="70%"
|
WIDTH="70%"
|
><TR
|
><TR
|
><TD
|
><TD
|
><PRE
|
><PRE
|
CLASS="PROGRAMLISTING"
|
CLASS="PROGRAMLISTING"
|
>cyg_bool cyg_httpd_handler(FILE *client,
|
>cyg_bool cyg_httpd_handler(FILE *client,
|
char *filename,
|
char *filename,
|
char *formdata,
|
char *formdata,
|
void *arg);</PRE
|
void *arg);</PRE
|
></TD
|
></TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
><P
|
><P
|
>The <TT
|
>The <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>client</I
|
>client</I
|
></TT
|
></TT
|
> argument is the TCP connection to
|
> argument is the TCP connection to
|
the client: anything output through this stream will be returned to
|
the client: anything output through this stream will be returned to
|
the browser. The <TT
|
the browser. The <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>filename</I
|
>filename</I
|
></TT
|
></TT
|
> argument is the
|
> argument is the
|
filename from the HTTP request and the <TT
|
filename from the HTTP request and the <TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>formdata</I
|
>formdata</I
|
></TT
|
></TT
|
>
|
>
|
argument is any form response data, or NULL if none was sent. The
|
argument is any form response data, or NULL if none was sent. The
|
<TT
|
<TT
|
CLASS="PARAMETER"
|
CLASS="PARAMETER"
|
><I
|
><I
|
>arg</I
|
>arg</I
|
></TT
|
></TT
|
> argument is the user defined value from the
|
> argument is the user defined value from the
|
table entry.</P
|
table entry.</P
|
><P
|
><P
|
>The handler is entirely responsible for generating the response to the
|
>The handler is entirely responsible for generating the response to the
|
client, both HTTP header and content. If the handler decides that it
|
client, both HTTP header and content. If the handler decides that it
|
does not want to generate a response it can return
|
does not want to generate a response it can return
|
<TT
|
<TT
|
CLASS="LITERAL"
|
CLASS="LITERAL"
|
>false</TT
|
>false</TT
|
>, in which case the table scan is resumed for
|
>, in which case the table scan is resumed for
|
another match. If no match is found, or no handler returns true, then
|
another match. If no match is found, or no handler returns true, then
|
a default response page is generated indicating that the requested
|
a default response page is generated indicating that the requested
|
page cannot be found.</P
|
page cannot be found.</P
|
><P
|
><P
|
>Finally, the server thread closes the connection to the client and
|
>Finally, the server thread closes the connection to the client and
|
loops back to accept a new connection.</P
|
loops back to accept a new connection.</P
|
></DIV
|
></DIV
|
><DIV
|
><DIV
|
CLASS="NAVFOOTER"
|
CLASS="NAVFOOTER"
|
><HR
|
><HR
|
ALIGN="LEFT"
|
ALIGN="LEFT"
|
WIDTH="100%"><TABLE
|
WIDTH="100%"><TABLE
|
SUMMARY="Footer navigation table"
|
SUMMARY="Footer navigation table"
|
WIDTH="100%"
|
WIDTH="100%"
|
BORDER="0"
|
BORDER="0"
|
CELLPADDING="0"
|
CELLPADDING="0"
|
CELLSPACING="0"
|
CELLSPACING="0"
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="net-httpd-chapter.html"
|
HREF="net-httpd-chapter.html"
|
ACCESSKEY="P"
|
ACCESSKEY="P"
|
>Prev</A
|
>Prev</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="ecos-ref.html"
|
HREF="ecos-ref.html"
|
ACCESSKEY="H"
|
ACCESSKEY="H"
|
>Home</A
|
>Home</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="net-httpd-configuration.html"
|
HREF="net-httpd-configuration.html"
|
ACCESSKEY="N"
|
ACCESSKEY="N"
|
>Next</A
|
>Next</A
|
></TD
|
></TD
|
></TR
|
></TR
|
><TR
|
><TR
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="left"
|
ALIGN="left"
|
VALIGN="top"
|
VALIGN="top"
|
>Embedded HTTP Server</TD
|
>Embedded HTTP Server</TD
|
><TD
|
><TD
|
WIDTH="34%"
|
WIDTH="34%"
|
ALIGN="center"
|
ALIGN="center"
|
VALIGN="top"
|
VALIGN="top"
|
><A
|
><A
|
HREF="net-httpd-chapter.html"
|
HREF="net-httpd-chapter.html"
|
ACCESSKEY="U"
|
ACCESSKEY="U"
|
>Up</A
|
>Up</A
|
></TD
|
></TD
|
><TD
|
><TD
|
WIDTH="33%"
|
WIDTH="33%"
|
ALIGN="right"
|
ALIGN="right"
|
VALIGN="top"
|
VALIGN="top"
|
>Server Configuration</TD
|
>Server Configuration</TD
|
></TR
|
></TR
|
></TABLE
|
></TABLE
|
></DIV
|
></DIV
|
></BODY
|
></BODY
|
></HTML
|
></HTML
|
|
|