Mongoose: an Embeddable Web Server in C
The mg_get_header() function is used to retrieve a named header from the mg_request_info's mg_header array. Cookies are set in a header before the start of the document content:
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Set-Cookie: UUID=MGOOSE;\r\n"
"Set-Cookie: LOGIN=;\r\n"
"Content-Type: text/html\r\n\r\n"
...
This code would set the UUID cookie and clear the LOGIN cookie on the browser. Retrieving a particular cookie requires pulling the cookie header and parsing it for the desired cookie name/value pair:
static
char *getCookieParam(const char *cookie, char *param)
{
char *start = NULL;
char *end = NULL;
char *value = NULL;
int length;
if ( (cookie!=NULL) &&
((start=strstr(cookie, param)) != NULL) )
{
if ( (end=strstr(start, "; ")) != NULL )
length = end-start;
else
length = strlen(start);
value = malloc(length+1);
memset(value, 0, length+1);
strncpy(value, start, length);
}
return value;
}
This function will retrieve both the name of the cookie and its value, if any, as NAME=VALUE. The returned character string must be freed by the caller, however.
When Mongoose encounters a URI without a registered callback, it attempts to open the specified file and send it back to the client. Static HTML files can be written and stored in a document root configured with the root option. To allow retrieving directory listings of directories under the document root, set the dir_list option to yes. This option defaults to no. The directory list setting is a global configuration, so either no directory listings are allowed, or all of them can be seen.
Note:
Options that allow yes/no or true/false values are tested against the case-insensitive string values of “1”, “yes”, “true” or “ja”. Any other value is interpreted as no or false.
Listing 2. Because the dir_list option does not match a true value, it will disable directory listings.
void mongooseMgrInit()
{
...
mg_set_option(ctx, "dir", documentRoot);
mg_set_option(ctx, "dir_list", "0");
...
}
Mongoose provides access and error logging. The files are appended on restart of the server. Mongoose provides two functions available from the API that are documented in the Mongoose API page on the Web site: mg_set_error_callback() and mg_set_log_callback. These callbacks have a slightly different configuration from URI callbacks:
void mongooseMgrInit()
{
...
mg_set_error_callback(ctx, 404, show404, NULL)
mg_set_log_callback(ctx, logger)
...
}
The error callback sets callbacks for error codes from 0 to 1000. These map to HTTP error codes, such as 404 when a requested URI does not exist. When this callback function is called, the function can print a custom error page. The log callback is called any time the Mongoose server library wants to log something.
The source code for the sample server implemented using mongoose can be found at ftp.linuxjournal.com/pub/lj/listings/issue192/10680.tgz. It includes a single page with an image and CSS.
The Mongoose Project is stable and in use by a number of developers; however, the Google forums for it are littered with spam. Don't let this inconvenience prevent you from utilizing what is a well-designed and implemented Web server library.
This introduction to Mongoose covers the basics for creating a lightweight embedded Web server without covering the full breadth of Mongoose features, such as CGI or SSL. The ease of use of this library should make it plain that these extended features will require little additional knowledge of Mongoose and free developers to build custom Web servers.
Resources
Mongoose: code.google.com/p/mongoose
Example Source: ftp.linuxjournal.com/pub/lj/listings/issue192/10680.tgz
Michael J. Hammel is a Principal Software Engineer for Colorado Engineering, Inc. (CEI), in Colorado Springs, Colorado, with more than 20 years of software development and management experience. He has written more than 100 articles for numerous on-line and print magazines and is the author of three books on The GIMP, the premier open-source graphics editing package.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Web Development News
Developer Poll
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- Web & UI Developer (JavaScript & j Query)
- UX Designer
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)








2 hours 6 min ago
7 hours 52 min ago
8 hours 9 min ago
10 hours 2 min ago
11 hours 56 min ago
18 hours 50 min ago
19 hours 6 min ago
20 hours 57 min ago
1 day 2 hours ago
1 day 7 hours ago