📄 net-httpd-html.html
字号:
<!-- Copyright (C) 2003 Red Hat, Inc. --><!-- 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 is obtained from the copyright holder. --><HTML><HEAD><TITLE>Support Functions and Macros</TITLE><meta name="MSSmartTagsPreventParsing" content="TRUE"><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="eCos Reference Manual"HREF="ecos-ref.html"><LINKREL="UP"TITLE="Embedded HTTP Server"HREF="net-httpd-chapter.html"><LINKREL="PREVIOUS"TITLE="Server Configuration"HREF="net-httpd-configuration.html"><LINKREL="NEXT"TITLE="System Monitor"HREF="net-httpd-monitor.html"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">eCos Reference Manual</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="net-httpd-configuration.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 48. Embedded HTTP Server</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="net-httpd-monitor.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="NET-HTTPD-HTML">Support Functions and Macros</H1><P>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 <TTCLASS="FUNCTION">fprintf()</TT> and<TTCLASS="FUNCTION">fputs()</TT>. 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.</P><P>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<TTCLASS="FILENAME">cyg/httpd/httpd.h</TT> header file defines a set ofhelper functions and macros. Most of these are wrappers for predefinedprint calls on the <TTCLASS="PARAMETER"><I>client</I></TT> stream passed to thehander function. For examples of their use, see the System Monitorexample.</P><DIVCLASS="NOTE"><BLOCKQUOTECLASS="NOTE"><P><B>Note: </B>All arguments to macros are pointers to strings, unless otherwisestated. In general, wherever a function or macro has an<TTCLASS="PARAMETER"><I>attr</I></TT> or <TTCLASS="PARAMETER"><I>__attr</I></TT>parameter, then the contents of this string will be inserted into thetag being defined as HTML attributes. If it is a NULL or empty stringit will be ignored.</P></BLOCKQUOTE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN15305">HTTP Support</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">void cyg_http_start( FILE *client, char *content_type, int content_length );void cyg_http_finish( FILE *client );#define html_begin(__client)#define html_end( __client )</PRE></TD></TR></TABLE><P>The function <TTCLASS="FUNCTION">cyg_http_start()</TT> generates a simpleHTTP response header containing the value of<TTCLASS="LITERAL">CYGDAT_HTTPD_SERVER_ID</TT> in the "Server" field, and thevalues of <TTCLASS="PARAMETER"><I>content_type</I></TT> and<TTCLASS="PARAMETER"><I>content_length</I></TT> in the "Content-type"and "Content-length" field respectively. The function<TTCLASS="FUNCTION">cyg_http_finish()</TT> just adds an extra newline tothe end of the output and then flushes it to force the data out to theclient.</P><P>The macro <TTCLASS="LITERAL">html_begin()</TT> generates an HTTP headerwith a "text/html" content type followed by an opening"<html>" tag. <TTCLASS="LITERAL">html_end()</TT> generatesa closing "</html>" tag and calls<TTCLASS="FUNCTION">cyg_http_finish()</TT>.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN15318">General HTML Support</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">void cyg_html_tag_begin( FILE *client, char *tag, char *attr );void cyg_html_tag_end( FILE *client, char *tag );#define html_tag_begin( __client, __tag, __attr )#define html_tag_end( __client, __tag )#define html_head( __client, __title, __meta )#define html_body_begin( __client, __attr )#define html_body_end( __client )#define html_heading( __client, __level, __heading )#define html_para_begin( __client, __attr )#define html_url( __client, __text, __link )#define html_image( __client, __source, __alt, __attr )</PRE></TD></TR></TABLE><P>The function <TTCLASS="FUNCTION">cyg_html_tag_begin()</TT> generates anopening tag with the given name. The function<TTCLASS="FUNCTION">cyg_html_tag_end()</TT> generates a closing tag withthe given name. The macros <TTCLASS="LITERAL">html_tag_begin()</TT> and<TTCLASS="LITERAL">html_tag_end</TT> are just wrappers for these functions.</P><P>The macro <TTCLASS="LITERAL">html_head()</TT> generates an HTML headersection with <TTCLASS="PARAMETER"><I>__title</I></TT> as the title. The<TTCLASS="PARAMETER"><I>__meta</I></TT> argument defines any meta tags that willbe inserted into the header. <TTCLASS="LITERAL">html_body_begin()</TT> and<TTCLASS="LITERAL">html_body_end</TT> generate HTML body begin and endtags.</P><P><TTCLASS="LITERAL">html_heading()</TT> generates a complete HTML headerwhere <TTCLASS="PARAMETER"><I>__level</I></TT> is a numerical level, between 1and 6, and <TTCLASS="PARAMETER"><I>__heading</I></TT> is the headingtext. <TTCLASS="LITERAL">html_para_begin()</TT> generates a paragraphbreak.</P><P><TTCLASS="LITERAL">html_url()</TT> inserts a URL where<TTCLASS="PARAMETER"><I>__text</I></TT> is the displayed text and<TTCLASS="PARAMETER"><I>__link</I></TT> is the URL of the linkedpage. <TTCLASS="LITERAL">html_image()</TT> inserts an image tag where<TTCLASS="PARAMETER"><I>__source</I></TT> is the URL of the image to beincluded and <TTCLASS="PARAMETER"><I>__alt</I></TT> is the alternative text forwhen the image is not displayed.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN15344">Table Support</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">#define html_table_begin( __client, __attr )#define html_table_end( __client )#define html_table_header( __client, __content, __attr ) #define html_table_row_begin( __client, __attr ) #define html_table_row_end( __client ) #define html_table_data_begin( __client, __attr ) #define html_table_data_end( __client ) </PRE></TD></TR></TABLE><P><TTCLASS="LITERAL">html_table_begin()</TT> starts a table and<TTCLASS="LITERAL">html_table_end()</TT> endit. <TTCLASS="LITERAL">html_table_header()</TT> generates a simple tablecolumn header containg the string <TTCLASS="PARAMETER"><I>__content</I></TT>. </P><P><TTCLASS="LITERAL">html_table_row_begin()</TT> and<TTCLASS="LITERAL">html_table_row_end()</TT> begin and end a table row,and similarly <TTCLASS="LITERAL">html_table_data_begin()</TT> and<TTCLASS="LITERAL">html_table_data_end()</TT> begin and end a tableentry. </P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN15357">Forms Support</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING">#define html_form_begin( __client, __url, __attr ) #define html_form_end( __client ) #define html_form_input( __client, __type, __name, __value, __attr ) #define html_form_input_radio( __client, __name, __value, __checked )#define html_form_input_checkbox( __client, __name, __value, __checked )#define html_form_input_hidden( __client, __name, __value ) #define html_form_select_begin( __client, __name, __attr ) #define html_form_option( __client, __value, __label, __selected ) #define html_form_select_end( __client ) void cyg_formdata_parse( char *data, char *list[], int size );char *cyg_formlist_find( char *list[], char *name );</PRE></TD></TR></TABLE><P><TTCLASS="LITERAL">html_form_begin()</TT> begins a form, the<TTCLASS="PARAMETER"><I>__url</I></TT> argument is the value for the<TTCLASS="LITERAL">action</TT>attribute. <TTCLASS="LITERAL">html_form_end()</TT> ends the form.</P><P><TTCLASS="LITERAL">html_form_input()</TT> defines a general form inputelement with the given type, name andvalue. <TTCLASS="LITERAL">html_form_input_radio</TT> creates a radio buttonwith the given name and value; the <TTCLASS="PARAMETER"><I>__checked</I></TT>argument is a boolean expression that is used to determine whether the<TTCLASS="LITERAL">checked</TT> attribute is added to the tag. Similarly<TTCLASS="LITERAL">html_form_input_checkbox()</TT> defines a checkboxelement. <TTCLASS="LITERAL">html_form_input_hidden()</TT> defines a hiddenform element with the given name and value.</P><P><TTCLASS="LITERAL">html_form_select_begin()</TT> begins a multiple choicemenu with the given name. <TTCLASS="LITERAL">html_form_select_end()</TT>end it. <TTCLASS="LITERAL">html_form_option()</TT> defines a menu entrywith the given value and label; the <TTCLASS="PARAMETER"><I>__selected</I></TT>argument is a boolean expression controlling whether the<TTCLASS="LITERAL">selected</TT> attribute is added to the tag.</P><P><TTCLASS="FUNCTION">cyg_formdata_parse()</TT> converts a form responsestring into an <TTCLASS="LITERAL">NULL</TT>-terminated array of"name=value" entries. The <TTCLASS="PARAMETER"><I>data</I></TT>argument is the string as passed to the handler function; note thatthis string is not copied and will be updated in place to form thelist entries. <TTCLASS="PARAMETER"><I>list</I></TT> is a pointer to an array ofcharacter pointers, and is <TTCLASS="PARAMETER"><I>size</I></TT> elements long.<TTCLASS="FUNCTION">cyg_formlist_find()</TT> searches a list generated by<TTCLASS="FUNCTION">cyg_formdata_parse()</TT> and returns a pointer to thevalue part of the string whose name part matches<TTCLASS="PARAMETER"><I>name</I></TT>; if there is no match it will return<TTCLASS="LITERAL">NULL</TT>.</P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN15388">Predefined Handlers</H2><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING"> int cyg_httpd_send_html( FILE *client, char *filename, char *request, void *arg );typedef struct{ char *content_type; cyg_uint32 content_length; cyg_uint8 *data;} cyg_httpd_data;#define CYG_HTTPD_DATA( __name, __type, __length, __data )int cyg_httpd_send_data( FILE *client, char *filename, char *request, void *arg ); </PRE></TD></TR></TABLE><P>The HTTP server defines a couple of predefined handers to make iteasier to deliver simple, static content.</P><P><TTCLASS="FUNCTION">cyg_httpd_send_html()</TT> takes a<TTCLASS="LITERAL">NULL</TT>-terminated string as the argument and sends itto the client with an HTTP header indicating that it is HTML. Thefollowing is an example of its use:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING"> char cyg_html_message[] = "<head><title>Welcome</title></head>\n" "<body><h2>Welcome to my Web Page</h2></body>\n"CYG_HTTPD_TABLE_ENTRY( cyg_html_message_entry, "/message.html", cyg_httpd_send_html, cyg_html_message ); </PRE></TD></TR></TABLE><P><TTCLASS="FUNCTION">cyg_httpd_send_data()</TT> Sends arbitrary data to theclient. The argument is a pointer to a <SPANCLASS="TYPE">cyg_httpd_data</SPAN>structure that defines the content type and length of the data, and apointer to the data itself. The <TTCLASS="LITERAL">CYG_HTTPD_DATA()</TT>macro automates the definition of the structure. Here is a typicalexample of its use:</P><TABLEBORDER="5"BGCOLOR="#E0E0F0"WIDTH="70%"><TR><TD><PRECLASS="PROGRAMLISTING"> static cyg_uint8 ecos_logo_gif[] = { ...};CYG_HTTPD_DATA( cyg_monitor_ecos_logo_data, "image/gif", sizeof(ecos_logo_gif), ecos_logo_gif );CYG_HTTPD_TABLE_ENTRY( cyg_monitor_ecos_logo, "/monitor/ecos.gif", cyg_httpd_send_data, &cyg_monitor_ecos_logo_data ); </PRE></TD></TR></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="net-httpd-configuration.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ecos-ref.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="net-httpd-monitor.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Server Configuration</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="net-httpd-chapter.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">System Monitor</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -