<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.ethernut.de/nutwiki/index.php?action=history&amp;feed=atom&amp;title=Documents%2FuHTTP_Lib_SSI</id>
	<title>Documents/uHTTP Lib SSI - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.ethernut.de/nutwiki/index.php?action=history&amp;feed=atom&amp;title=Documents%2FuHTTP_Lib_SSI"/>
	<link rel="alternate" type="text/html" href="https://www.ethernut.de/nutwiki/index.php?title=Documents/uHTTP_Lib_SSI&amp;action=history"/>
	<updated>2026-11-22T13:26:18Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://www.ethernut.de/nutwiki/index.php?title=Documents/uHTTP_Lib_SSI&amp;diff=366&amp;oldid=prev</id>
		<title>Harald: Created page with &quot;&lt;div id=&quot;content&quot;&gt;  = MicroHTTP Library: Using Server Side Includes =  On this page I will explain how to create dynamic web content with the uhttplib.html|MicroHTTP library...&quot;</title>
		<link rel="alternate" type="text/html" href="https://www.ethernut.de/nutwiki/index.php?title=Documents/uHTTP_Lib_SSI&amp;diff=366&amp;oldid=prev"/>
		<updated>2017-07-13T08:31:51Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;lt;div id=&amp;quot;content&amp;quot;&amp;gt;  = MicroHTTP Library: Using Server Side Includes =  On this page I will explain how to create dynamic web content with the uhttplib.html|MicroHTTP library...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;div id=&amp;quot;content&amp;quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= MicroHTTP Library: Using Server Side Includes =&lt;br /&gt;
&lt;br /&gt;
On this page I will explain how to create dynamic web content with the [[uhttplib.html|MicroHTTP library&amp;#039;s]] SSI capabilities.&lt;br /&gt;
&lt;br /&gt;
== Enabling SSI ==&lt;br /&gt;
&lt;br /&gt;
To enable SSI, the application must register a handler for it. The easiest way to do this is to enable default SSI support in the Configurator. The application simply needs to call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;int MediaTypeInitDefaults(void);&amp;lt;/pre&amp;gt;&lt;br /&gt;
to activate all configured handlers. For SSI, it will register the internal default handler&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;int HttpSsiHandler(HTTPD_SESSION *hs, const MEDIA_TYPE_ENTRY *mt, const char *ext);&amp;lt;/pre&amp;gt;&lt;br /&gt;
When the browser requests a file with extension &amp;#039;&amp;#039;shtml&amp;#039;&amp;#039;, the web server will redirect the request to the SSI handler. The handler parses the file for SSI elements and replaces them according to the SSI syntax rules.&lt;br /&gt;
&lt;br /&gt;
At the time of this writing, the following SSI elements are recognized by the server:&lt;br /&gt;
&lt;br /&gt;
* echo&lt;br /&gt;
* exec&lt;br /&gt;
* include&lt;br /&gt;
&lt;br /&gt;
Support for &amp;#039;&amp;#039;if-then-else&amp;#039;&amp;#039; is not provided, probably the most missing feature. If implemented once, we would also miss the &amp;#039;&amp;#039;set&amp;#039;&amp;#039; command.&lt;br /&gt;
&lt;br /&gt;
CGI scripts had been discussed on [[uhttplib_forms.html|the previous page]]. Including external files is straight forward and a simple way to add common parts like menus to all your webpages. Retrieving the value of variables will be explained now.&lt;br /&gt;
&lt;br /&gt;
== Predefined Variables ==&lt;br /&gt;
&lt;br /&gt;
The library offers a number of predefined SSI variables, for example the current date and time. The following HTTP snippet displays the local time as well as the Greenwich Mean Time.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;&amp;amp;lt;p&amp;amp;gt;Local time &amp;amp;lt;!--#echo var=&amp;amp;quot;DATE_LOCAL&amp;amp;quot; --&amp;amp;gt;&amp;amp;lt;/p&amp;amp;gt;&lt;br /&gt;
&amp;amp;lt;p&amp;amp;gt;GMT time &amp;amp;lt;!--#echo var=&amp;amp;quot;DATE_GMT&amp;amp;quot; --&amp;amp;gt;&amp;amp;lt;/p&amp;amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
To enable these variables, applications must call&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;int EnvInitDefaults(void);&lt;br /&gt;
HTTP_SSI_VARHANDLER HttpRegisterSsiVarHandler(HTTP_SSI_VARHANDLER handler);&amp;lt;/pre&amp;gt;&lt;br /&gt;
The first call adds all predefined variables to the environment and the second call is used to register a handler. MicroHTTP offers a default handler named &amp;#039;&amp;#039;EnvHandler&amp;#039;&amp;#039;, which can be registered with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;HttpRegisterSsiVarHandler(EnvHandler);&amp;lt;/pre&amp;gt;&lt;br /&gt;
The library supports the following variables:&lt;br /&gt;
&lt;br /&gt;
* SERVER_PORT&lt;br /&gt;
* SERVER_NAME&lt;br /&gt;
* SERVER_ADDR&lt;br /&gt;
* SCRIPT_NAME&lt;br /&gt;
* SCRIPT_FILENAME&lt;br /&gt;
* REQUEST_URI&lt;br /&gt;
* REQUEST_METHOD&lt;br /&gt;
* REMOTE_PORT&lt;br /&gt;
* REMOTE_ADDR&lt;br /&gt;
* QUERY_STRING_UNESCAPED&lt;br /&gt;
* QUERY_STRING&lt;br /&gt;
* HTTP_USER_AGENT&lt;br /&gt;
* HTTP_REFERER&lt;br /&gt;
* HTTP_HOST&lt;br /&gt;
* HTTP_COOKIE&lt;br /&gt;
* HTTP_CONNECTION&lt;br /&gt;
* HTTP_ACCEPT_ENCODING&lt;br /&gt;
* DOCUMENT_ROOT&lt;br /&gt;
* DOCUMENT_NAME&lt;br /&gt;
* DATE_LOCAL&lt;br /&gt;
* DATE_GMT&lt;br /&gt;
&lt;br /&gt;
== User Defined Variables ==&lt;br /&gt;
&lt;br /&gt;
The following HTML snippet should display the application&amp;#039;s name and version:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;&amp;amp;lt;p&amp;amp;gt;&amp;amp;lt;!--#echo var=&amp;amp;quot;APPNAME&amp;amp;quot; --&amp;amp;gt; &amp;amp;lt;!--#echo var=&amp;amp;quot;APPVERSION&amp;amp;quot; --&amp;amp;gt;&amp;amp;lt;/p&amp;amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
The default SSI variable handler, that was used for our predefined variables above, also allows to add user defined variables with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;int EnvRegisterVariable(char *name, HTTP_ENVVAR_HANDLER handler, int item);&amp;lt;/pre&amp;gt;&lt;br /&gt;
* &amp;#039;&amp;#039;name&amp;#039;&amp;#039; points to the name of the variable.&lt;br /&gt;
* &amp;#039;&amp;#039;handler&amp;#039;&amp;#039; points to the handler for this variable.&lt;br /&gt;
* &amp;#039;&amp;#039;item&amp;#039;&amp;#039; is an index starting from 1. It will be passed to the handler.&lt;br /&gt;
&lt;br /&gt;
Here is the complete initialization part:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;#define APPVAR_NAME 1&lt;br /&gt;
#define APPVAR_VERSION 2&lt;br /&gt;
&lt;br /&gt;
StreamInit();&lt;br /&gt;
MediaTypeInitDefaults();&lt;br /&gt;
EnvRegisterVariable(&amp;amp;quot;APPNAME&amp;amp;quot;, AppVarHandler, APPVAR_NAME);&lt;br /&gt;
EnvRegisterVariable(&amp;amp;quot;APPVERSION&amp;amp;quot;, AppVarHandler, APPVAR_VERSION);&lt;br /&gt;
HttpRegisterSsiVarHandler(EnvHandler);&lt;br /&gt;
StreamClientAccept(HttpdClientHandler, NULL);&amp;lt;/pre&amp;gt;&lt;br /&gt;
We can create a single handler for all our user defined variables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;coding&amp;quot;&amp;gt;const char *AppVarHandler(HTTPD_SESSION *hs, int item)&lt;br /&gt;
{&lt;br /&gt;
    static char value[128];&lt;br /&gt;
&lt;br /&gt;
    value[0] = &amp;#039;\0&amp;#039;;&lt;br /&gt;
    switch (item) {&lt;br /&gt;
    case APPVAR_NAME:&lt;br /&gt;
        strcpy(value, &amp;amp;quot;SSI Sample&amp;amp;quot;);&lt;br /&gt;
        break;&lt;br /&gt;
    case APPVAR_VERSION:&lt;br /&gt;
        sprintf(value, &amp;amp;quot;%d.%d&amp;amp;quot;, MAJOR_VERSION, MINOR_VERSION);&lt;br /&gt;
        break;&lt;br /&gt;
    }&lt;br /&gt;
    return value;&lt;br /&gt;
}&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note, that the application must make sure, that each variable, which is sharing the same handler, must have a unique index.&lt;br /&gt;
&lt;br /&gt;
== Next Step ==&lt;br /&gt;
&lt;br /&gt;
CGIs and server side includes are most useful to create a web interface to remotely configure your embedded device. Probably, you do not want to grant access to everyone and need some kind of [[uhttplib_auth.html|authentication]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;/div&gt;</summary>
		<author><name>Harald</name></author>
	</entry>
</feed>