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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Developer Poll
- Dart: a New Web Programming Experience
- What's the tweeting protocol?
- New Products








1 hour 25 min ago
3 hours 2 min ago
5 hours 10 sec ago
5 hours 17 min ago
5 hours 47 min ago
5 hours 47 min ago
5 hours 48 min ago
8 hours 49 min ago
17 hours 15 min ago
17 hours 21 min ago