2009-11-17 Ralf Corsépius <ralf.corsepius@rtems.org>

* mghttpd/mongoose.c, mghttpd/mongoose.h, mghttpd/mongoose.1:
	Import from svn http://mongoose.googlecode.com/svn/trunk (r477).
This commit is contained in:
Ralf Corsepius
2009-11-17 16:53:48 +00:00
parent f70c86eaff
commit b45febaa2c
4 changed files with 5164 additions and 0 deletions

View File

@@ -1,3 +1,8 @@
2009-11-17 Ralf Corsépius <ralf.corsepius@rtems.org>
* mghttpd/mongoose.c, mghttpd/mongoose.h, mghttpd/mongoose.1:
Import from svn http://mongoose.googlecode.com/svn/trunk (r477).
2009-11-13 Ralf Corsépius <ralf.corsepius@rtems.org> 2009-11-13 Ralf Corsépius <ralf.corsepius@rtems.org>
* libmisc/capture/capture-cli.c: Cast pointer to intptr_t to * libmisc/capture/capture-cli.c: Cast pointer to intptr_t to

181
cpukit/mghttpd/mongoose.1 Normal file
View File

@@ -0,0 +1,181 @@
.\" Process this file with
.\" groff -man -Tascii mongoose.1
.\" $Id$
.Dd Dec 1, 2008
.Dt mongoose 1
.Sh NAME
.Nm mongoose
.Nd lightweight web server
.Sh SYNOPSIS
.Nm
.Op Ar options
.Op Ar config_file
.Nm
.Fl A Ar htpasswd_file domain_name user_name password
.Sh DESCRIPTION
.Nm
is small, fast and easy to use web server with CGI, SSL, Digest Authorization
support.
.Pp
.Nm
does not detach from terminal, and uses current working directory
as the web root, unless
.Fl root
option is specified.
.Pp
It is possible to specify multiple ports to listen on. For example, to
make
.Nm
listen on HTTP port 80 and HTTPS port 443, one should start it as
.Dq mongoose -ssl_cert cert.pem -ports 80,443s .
.Pp
Options may be specified in any order, with one exception: if SSL listening
port is specified in the -ports option, then -ssl_cert option must be set
before -ports option.
.Pp
Unlike other web servers,
.Nm
does not expect CGI scripts to be put in a special directory. CGI scripts may
be anywhere. CGI files are recognized by the file extension.
.Pp
SSI files are also recognized by extension. Unknown SSI directives are silently
ignored. Currently, two SSI directives supported, "include" and "exec". For the
"include" directive, included file name can be specified in three different
ways. Below is the summary of supported SSI directives:
.Bl -bullet
.It
<!--#exec "shell command"--> Execute shell command.
.It
<!--#include "path"--> File path must be relative to the current document.
.It
<!--#include virtual="path"--> File path must be relative to the document root.
.It
<!--#include file="path"--> File path must be the absolute path.
.El
.Pp
.Nm
can use the configuration file. By default, it is "mongoose.conf", and if it
is present in the same directory where
.Nm
lives, the command line options are read from it. Alternatively, the
configuration file may be specified as a last argument. The format of the
configuration file is exactly the same as for the command line options, the
only difference is that the command line options must be specified on
separate lines, and leading dashes for option names must be omitted.
Lines beginning with '#' are regarded as comments and ignored.
.Pp
.Sh OPTIONS
.Bl -tag -width indent
.It Fl A Ar htpasswd_file domain_name user_name password
Add/edit user's password in the passwords file. Deleting users can be done
with any text editor. Functionality similar to Apache's
.Ic htdigest
utility.
.It Fl access_log Ar file
Access log file. Default: not set, no logging is done.
.It Fl acl Ar (+|-)x.x.x.x[/x],...
Specify access control list (ACL). ACL is a comma separated list
of IP subnets, each subnet is prepended by '-' or '+' sign. Plus means allow,
minus means deny. If subnet mask is
omitted, like "-1.2.3.4", then it means single IP address. Mask may vary
from 0 to 32 inclusive. On each request, full list is traversed, and
last match wins. Default: not set, allow all.
.It Fl admin_uri Ar uri
If set,
.Nm
creates special administrative URI where options may be changed at runtime.
This URI probably wants to be password-protected, look at
.Fl protect
option, and in the EXAMPLES section on how to do it. Default: not set.
.It Fl aliases Ar list
This options gives an ability to serve the directories outside web root
by sort of symbolic linking to certain URI. The
.Ar list
must be comma-separated list of URI=PATH pairs, like this:
"/etc/=/my_etc,/tmp=/my_tmp". Default: not set.
.It Fl auth_PUT Ar file
PUT and DELETE passwords file. This must be specified if PUT or
DELETE methods are used. Default: not set.
.It Fl auth_gpass Ar file
Location of global passwords file. When set, per-directory .htpasswd files are
ignored, and all accessed must be authorised against global passwords file.
Default: not set.
.It Fl auth_realm Ar domain_name
Authorization realm. Default: "mydomain.com".
.It Fl cgi_env Ar list
Pass environment variables to the CGI script in addition to standard ones.
The list must be comma-separated list of X=Y pairs, like this:
"VARIABLE1=VALUE1,VARIABLE2=VALUE2". Default: not set.
.It Fl cgi_ext Ar list
Comma-separated list of CGI extensions. All files having these extensions
are treated as CGI scripts. Default: "cgi,pl,php"
.It Fl cgi_interp Ar file
Force
.Ar file
to be a CGI interpreter for all CGI scripts. By default this option is not
set, and
.Nm
decides which interpreter to use by looking at the first line of CGI script.
.It Fl dir_list Ar yes|no
Enable/disable directory listing. Default: "1" (enabled).
.It Fl error_log Ar file
Error log file. Default: not set, no errors are logged.
.It Fl idle_time Ar num_seconds
Number of seconds worker thread waits for some work before exit. Default: 10
.It Fl max_threads Ar number
Maximum number of worker threads to start. Default: 100
.It Fl mime_types Ar list
Additional to builtin mime types, in form
"extension1=type1,extension2=type2,...". Extension must include dot.
.It Fl ports Ar port_list
Comma-separated list of ports to listen on. If the port is SSL, a letter 's'
must be appeneded, for example, "-ports 80,443s" will open port 80 and port 443,
and connections on port 443 will be SSL-ed. It is possible to specify an
IP address to bind to. In this case, an IP address and a colon must be
prepended to the port number, for example, "-ports 127.0.0.1:8080". Note that
if SSL listening port is requested, then
.Fl ssl_cert
option must specified BEFORE
.Fl ports
option. Default: 8080
.It Fl protect Ar list
Comma separated list of URI=PATH pairs, specifying that given URIs
must the protected with respected password files. Default: not set.
.It Fl root Ar directory
Location of the WWW root directory. Default: working directory from which
.Nm
has been started.
.It Fl ssi_ext Ar list
Comma separated list of SSI extensions. Default: "shtml,shtm".
.It Fl ssl_cert Ar pem_file
Location of SSL certificate file. Default: not set.
.It Fl uid Ar login
Switch to given user after startup. Default: not set.
.El
.Pp
.Sh EMBEDDING
.Nm
was designed to be embeddable into C/C++ applications. Since the
source code is contained in single C file, it is fairly easy to embed it,
and to follow the updates. Please refer to http://code.google.com/p/mongoose
for details.
.Pp
.Sh EXAMPLES
.Bl -tag -width indent
.It Nm Fl root Ar /var/www Fl ssl_cert Ar /etc/cert.pem Fl ports Ar 8080,8043s Fl aliases Ar /aa=/tmp,/bb=/etc
Start listening on port 8080 for HTTP, and 8043 for HTTPS connections.
Use /etc/cert.pem as SSL certificate file. Web root is /var/www. In addition,
map directory /tmp to URI /aa, directory /etc to URI /bb.
.It Nm Fl acl Ar -0.0.0.0/0,+10.0.0.0/8,+1.2.3.4
Deny connections from everywhere, allow only IP address 1.2.3.4 and
all IP addresses from 10.0.0.0/8 subnet to connect.
.It Nm Fl admin_uri Ar /ctl Fl protect Ar /ctl=/tmp/passwords.txt
Create an administrative URI "/ctl" where
options may be changed at runtime, and protect that URI with authorization.
.El
.Pp
.Sh COPYRIGHT
.Nm
is licensed under the terms of the MIT license.
.Sh AUTHOR
.An Sergey Lyubka Aq valenok@gmail.com .

4725
cpukit/mghttpd/mongoose.c Normal file

File diff suppressed because it is too large Load Diff

253
cpukit/mghttpd/mongoose.h Normal file
View File

@@ -0,0 +1,253 @@
/*
* Copyright (c) 2004-2009 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* $Id$
*/
#ifndef MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_HEADER_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
struct mg_context; /* Handle for the HTTP service itself */
struct mg_connection; /* Handle for the individual connection */
/*
* This structure contains full information about the HTTP request.
* It is passed to the user-specified callback function as a parameter.
*/
struct mg_request_info {
char *request_method; /* "GET", "POST", etc */
char *uri; /* Normalized URI */
char *http_version; /* E.g. "1.0", "1.1" */
char *query_string; /* \0 - terminated */
char *post_data; /* POST data buffer */
char *remote_user; /* Authenticated user */
long remote_ip; /* Client's IP address */
int remote_port; /* Client's port */
int post_data_len; /* POST buffer length */
int status_code; /* HTTP status code */
int num_headers; /* Number of headers */
struct mg_header {
char *name; /* HTTP header name */
char *value; /* HTTP header value */
} http_headers[64]; /* Maximum 64 headers */
};
/*
* User-defined callback function prototype for URI handling, error handling,
* or logging server messages.
*/
typedef void (*mg_callback_t)(struct mg_connection *,
const struct mg_request_info *info, void *user_data);
/*
* Start the web server.
* This must be the first function called by the application.
* It creates a serving thread, and returns a context structure that
* can be used to alter the configuration, and stop the server.
*/
struct mg_context *mg_start(void);
/*
* Stop the web server.
* Must be called last, when an application wants to stop the web server and
* release all associated resources. This function blocks until all Mongoose
* threads are stopped. Context pointer becomes invalid.
*/
void mg_stop(struct mg_context *);
/*
* Return current value of a particular option.
*/
const char *mg_get_option(const struct mg_context *, const char *option_name);
/*
* Set a value for a particular option.
* Mongoose makes an internal copy of the option value string, which must be
* valid nul-terminated ASCII or UTF-8 string. It is safe to change any option
* at any time. The order of setting various options is also irrelevant with
* one exception: if "ports" option contains SSL listening ports, a "ssl_cert"
* option must be set BEFORE the "ports" option.
* Return value:
* -1 if option is unknown
* 0 if mg_set_option() failed
* 1 if mg_set_option() succeeded
*/
int mg_set_option(struct mg_context *, const char *opt_name, const char *value);
/*
* Add, edit or delete the entry in the passwords file.
* This function allows an application to manipulate .htpasswd files on the
* fly by adding, deleting and changing user records. This is one of the two
* ways of implementing authentication on the server side. For another,
* cookie-based way please refer to the examples/authentication.c in the
* source tree.
* If password is not NULL, entry is added (or modified if already exists).
* If password is NULL, entry is deleted. Return:
* 1 on success
* 0 on error
*/
int mg_modify_passwords_file(struct mg_context *ctx, const char *file_name,
const char *user_name, const char *password);
/*
* Register URI handler.
* It is possible to handle many URIs if using * in the uri_regex, which
* matches zero or more characters. user_data pointer will be passed to the
* handler as a third parameter. If func is NULL, then the previously installed
* handler for this uri_regex is removed.
*/
void mg_set_uri_callback(struct mg_context *ctx, const char *uri_regex,
mg_callback_t func, void *user_data);
/*
* Register HTTP error handler.
* An application may use that function if it wants to customize the error
* page that user gets on the browser (for example, 404 File Not Found message).
* It is possible to specify a error handler for all errors by passing 0 as
* error_code. That '0' error handler must be set last, if more specific error
* handlers are also used. The actual error code value can be taken from
* the request info structure that is passed to the callback.
*/
void mg_set_error_callback(struct mg_context *ctx, int error_code,
mg_callback_t func, void *user_data);
/*
* Register authorization handler.
* This function provides a mechanism to implement custom authorization,
* for example cookie based (look at examples/authorization.c).
* The callback function must analyze the request, and make its own judgement
* on wether it should be authorized or not. After the decision is made, a
* callback must call mg_authorize() if the request is authorized.
*/
void mg_set_auth_callback(struct mg_context *ctx, const char *uri_regex,
mg_callback_t func, void *user_data);
/*
* Register log handler.
* By default, Mongoose logs all error messages to stderr. If "error_log"
* option is specified, the errors are written in the specified file. However,
* if an application registers its own log handler, Mongoose will not log
* anything but call the handler function, passing an error message as
* "user_data" callback argument.
*/
void mg_set_log_callback(struct mg_context *ctx, mg_callback_t func);
/*
* Register SSL password handler.
* This is needed only if SSL certificate asks for a password. Instead of
* prompting for a password on a console a specified function will be called.
*/
typedef int (*mg_spcb_t)(char *buf, int num, int w, void *key);
void mg_set_ssl_password_callback(struct mg_context *ctx, mg_spcb_t func);
/*
* Send data to the browser.
* Return number of bytes sent. If the number of bytes sent is less then
* requested or equals to -1, network error occured, usually meaning the
* remote side has closed the connection.
*/
int mg_write(struct mg_connection *, const void *buf, int len);
/*
* Send data to the browser using printf() semantics.
* Works exactly like mg_write(), but allows to do message formatting.
* Note that mg_printf() uses internal buffer of size MAX_REQUEST_SIZE
* (8 Kb by default) as temporary message storage for formatting. Do not
* print data that is bigger than that, otherwise it will be truncated.
* Return number of bytes sent.
*/
int mg_printf(struct mg_connection *, const char *fmt, ...);
/*
* Read data from the remote or local end.
*/
int mg_read(struct mg_connection *, int local, void *buf, int len);
/*
* Get the value of particular HTTP header.
* This is a helper function. It traverses request_info->http_headers array,
* and if the header is present in the array, returns its value. If it is
* not present, NULL is returned.
*/
const char *mg_get_header(const struct mg_connection *, const char *hdr_name);
/*
* Authorize the request.
* See the documentation for mg_set_auth_callback() function.
*/
void mg_authorize(struct mg_connection *);
/*
* Get a value of particular form variable.
* Both query string (whatever comes after '?' in the URL) and a POST buffer
* are scanned. If a variable is specified in both query string and POST
* buffer, POST buffer wins. Return value:
* NULL if the variable is not found
* non-NULL if found. NOTE: this returned value is dynamically allocated
* and is subject to mg_free() when no longer needed. It is
* an application's responsibility to mg_free() the variable.
*/
char *mg_get_var(const struct mg_connection *, const char *var_name);
/*
* Free up memory returned by mg_get_var().
*/
void mg_free(char *var);
/*
* Return Mongoose version.
*/
const char *mg_version(void);
/*
* Print command line usage string.
*/
void mg_show_usage_string(FILE *fp);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* MONGOOSE_HEADER_INCLUDED */