Merge branch 'master' of ssh://git.rtems.org/data/git/rtems

This commit is contained in:
Joel Sherrill
2012-07-14 14:28:39 -05:00
13 changed files with 4373 additions and 4333 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ include_mghttpddir = $(includedir)/mghttpd
project_lib_LIBRARIES = libmghttpd.a
libmghttpd_a_CPPFLAGS = $(AM_CPPFLAGS)
# libmghttpd_a_CPPFLAGS += -DHAVE_MD5
libmghttpd_a_CPPFLAGS += -DNO_SSI -DNO_SSL -DNO_CGI
libmghttpd_a_CPPFLAGS += -DNO_SSL -DNO_POPEN -DNO_CGI
libmghttpd_a_SOURCES = mongoose.c mongoose.h
include_mghttpd_HEADERS = mongoose.h
+124 -121
View File
@@ -1,176 +1,179 @@
.\" Process this file with
.\" groff -man -Tascii mongoose.1
.Dd Dec 1, 2008
.\" $Id: mongoose.1,v 1.12 2008/11/29 15:32:42 drozd Exp $
.Dd Aug 31, 2010
.Dt mongoose 1
.Sh NAME
.Nm mongoose
.Nd lightweight web server
.Sh SYNOPSIS
.Nm
.Op Ar options
.Op Ar config_file
.Op Ar OPTIONS
.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.
is small, fast and easy to use web server with CGI, SSL, MD5 authorization,
and basic SSI support.
.Pp
.Nm
does not detach from terminal, and uses current working directory
as the web root, unless
.Fl root
.Fl r
option is specified.
.Pp
It is possible to specify multiple ports to listen on. For example, to
make
It is possible to specify multiple ports to listen on. For example, to make
mongoose listen on HTTP port 80 and HTTPS port 443, one should start it as:
.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.
.Fl s Ar cert.pem Fl p Ar 80,443s
.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.
does not require CGI scripts be put in a special directory. CGI scripts can
be anywhere. CGI (and SSI) files are recognized by the file name pattern.
.Nm
uses shell-like glob patterns with the following syntax:
.Bl -tag -compact -width indent
.It **
Matches everything
.It *
Matches everything but slash character, '/'
.It ?
Matches any character
.It $
Matches the end of the string
.It |
Matches if pattern on the left side or the right side matches. Pattern on the
left side is matched first
.El
All other characters in the pattern match themselves.
.Pp
If no arguments are given,
.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.
searches for a configuration file called "mongoose.conf" in the same directory
where mongoose binary is located. Alternatively, a file name could be
specified in the command line. Format of the configuration file is the same
as for the command line options except that each option must be specified
on a separate line, leading dashes for option names must be omitted.
Lines beginning with '#' and empty lines are 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
with any text editor. Functionality is 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],...
.It Fl C Ar cgi_pattern
All files that fully match cgi_pattern are treated as CGI.
Default pattern allows CGI files be
anywhere. To restrict CGIs to certain directory, use e.g. "-C /cgi-bin/**.cgi".
Default: "**.cgi$|**.pl$|**.php$"
.It Fl E Ar cgi_environment
Extra environment variables to be passed 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: ""
.It Fl G Ar put_delete_passwords_file
PUT and DELETE passwords file. This must be specified if PUT or
DELETE methods are used. Default: ""
.It Fl I Ar cgi_interpreter
Use
.Ar cgi_interpreter
as a CGI interpreter for all CGI scripts regardless script extension.
Mongoose decides which interpreter to use by looking at
the first line of a CGI script. Default: "".
.It Fl M Ar max_request_size
Maximum HTTP request size in bytes. Default: "16384"
.It Fl P Ar protect_uri
Comma separated list of URI=PATH pairs, specifying that given URIs
must be protected with respected password files. Default: ""
.It Fl R Ar authentication_domain
Authorization realm. Default: "mydomain.com"
.It Fl S Ar ssi_pattern
All files that fully match ssi_pattern are treated as SSI.
Unknown SSI directives are silently ignored. Currently, two SSI directives
are supported, "include" and "exec". Default: "**.shtml$|**.shtm$"
.It Fl a Ar access_log_file
Access log file. Default: "", no logging is done.
.It Fl d Ar enable_directory_listing
Enable/disable directory listing. Default: "yes"
.It Fl e Ar error_log_file
Error log file. Default: "", no errors are logged.
.It Fl g Ar global_passwords_file
Location of a global passwords file. If set, per-directory .htpasswd files are
ignored, and all requests must be authorised against that file. Default: ""
.It Fl i Ar index_files
Comma-separated list of files to be treated as directory index files.
Default: "index.html,index.htm,index.cgi"
.It Fl l Ar access_control_list
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
last match wins. Default setting is to allow all. For example, to allow only
192.168/16 subnet to connect, run "mongoose -0.0.0.0/0,+192.168/16".
Default: ""
.It Fl m Ar extra_mime_types
Extra mime types to recognize, in form
"extension1=type1,extension2=type2,...". Extension must include dot.
.It Fl ports Ar port_list
Example: "mongoose -m .cpp=plain/text,.java=plain/text". Default: ""
.It Fl p Ar listening_ports
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,
must be appeneded, for example, "-p 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.
prepended to the port number. For example, to bind to a loopback interface
on port 80 and to all interfaces on HTTPS port 443, use
"mongoose -p 127.0.0.1:80,443s". Default: "8080"
.It Fl r Ar document_root
Location of the WWW root directory. Default: "."
.It Fl s Ar ssl_certificate
Location of SSL certificate file. Default: ""
.It Fl t Ar num_threads
Number of worker threads to start. Default: "10"
.It Fl u Ar run_as_user
Switch to given user's credentials after startup. Default: ""
.It Fl w Ar url_rewrite_patterns
Comma-separated list of URL rewrites in the form of
"pattern=substitution,..." If the "pattern" matches some prefix
of the requested URL, then matched prefix gets substituted with "substitution".
For example, "-w /config=/etc,**.doc|**.rtf=/path/to/cgi-bin/handle_doc.cgi"
will serve all URLs that start with "/config" from the "/etc" directory, and
call handle_doc.cgi script for .doc and .rtf file requests. If some pattern
matches, no further matching/substitution is performed
(first matching pattern wins). Use full paths in substitutions. Default: ""
.It Fl x Ar thread_stack_size
Use the given amount of stack for each thread. Default: ""
.It Fl y Ar thread_priority
Use the given priority for all posix threads. If this option is used without the
thread_policy option, the systems default scheduling policy will be used for the
threads instead of inheriting the policy of the creating thread. Default: ""
.It Fl z Ar thread_policy
Select the posix scheduling policy for the threads. Possible Values are 's' for
sporadic (not on all systems available), 'r' for round robin, 'f' for fifo or
'o' for other scheduling strategie. If this option is used without the
thread_priority option, the systems default priority will be used for the
threads instead of inheriting the priority of the creating thread. Default: ""
.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
source code is contained in single C file, it is fairly easy to embed it
and 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
.It Nm Fl r Ar /var/www Fl s Ar /etc/cert.pem Fl p Ar 8080,8043s
Start serving files from /var/www. Listen on port 8080 for HTTP, and 8043
for HTTPS connections. Use /etc/cert.pem as SSL certificate file.
.It Nm Fl l 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.
.It Nm Fl w Ar **=/usr/bin/script.cgi
Invoke /usr/bin/script.cgi for every incoming request, regardless of the URL.
.El
.Pp
.Sh COPYRIGHT
+3582 -4002
View File
File diff suppressed because it is too large Load Diff
+196 -209
View File
@@ -1,251 +1,238 @@
/*
* 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.
*/
// Copyright (c) 2004-2011 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.
#ifndef MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_HEADER_INCLUDED
#define MONGOOSE_HEADER_INCLUDED
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#endif // __cplusplus
struct mg_context; /* Handle for the HTTP service itself */
struct mg_connection; /* Handle for the individual connection */
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.
*/
// This structure contains information about the HTTP request.
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 */
void *user_data; // User-defined pointer passed to mg_start()
char *request_method; // "GET", "POST", etc
char *uri; // URL-decoded URI
char *http_version; // E.g. "1.0", "1.1"
char *query_string; // URL part after '?' (not including '?') or NULL
char *remote_user; // Authenticated user, or NULL if no auth used
char *log_message; // Mongoose error log message, MG_EVENT_LOG only
long remote_ip; // Client's IP address
int remote_port; // Client's port
int status_code; // HTTP reply status code, e.g. 200
int is_ssl; // 1 if SSL-ed, 0 if not
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
};
// Various events on which user-defined function is called by Mongoose.
enum mg_event {
MG_NEW_REQUEST, // New HTTP request has arrived from the client
MG_HTTP_ERROR, // HTTP error must be returned to the client
MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
MG_INIT_SSL, // Mongoose initializes SSL. Instead of mg_connection *,
// SSL context is passed to the callback function.
MG_REQUEST_COMPLETE // Mongoose has finished handling the request
};
/*
* 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);
// Prototype for the user-defined function. Mongoose calls this function
// on every MG_* event.
//
// Parameters:
// event: which event has been triggered.
// conn: opaque connection handler. Could be used to read, write data to the
// client, etc. See functions below that have "mg_connection *" arg.
// request_info: Information about HTTP request.
//
// Return:
// If handler returns non-NULL, that means that handler has processed the
// request by sending appropriate HTTP reply to the client. Mongoose treats
// the request as served.
// If handler returns NULL, that means that handler has not processed
// the request. Handler must not send any data to the client in this case.
// Mongoose proceeds with request handling as if nothing happened.
typedef void * (*mg_callback_t)(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info);
/*
* 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);
// Start web server.
//
// Parameters:
// callback: user defined event handling function or NULL.
// options: NULL terminated list of option_name, option_value pairs that
// specify Mongoose configuration parameters.
//
// Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
// processing is required for these, signal handlers must be set up
// after calling mg_start().
//
//
// Example:
// const char *options[] = {
// "document_root", "/var/www",
// "listening_ports", "80,443s",
// NULL
// };
// struct mg_context *ctx = mg_start(&my_func, NULL, options);
//
// Please refer to http://code.google.com/p/mongoose/wiki/MongooseManual
// for the list of valid option and their possible values.
//
// Return:
// web server context, or NULL on error.
struct mg_context *mg_start(mg_callback_t callback, void *user_data,
const char **options);
/*
* 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.
*/
// 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);
// Get the value of particular configuration parameter.
// The value returned is read-only. Mongoose does not allow changing
// configuration at run time.
// If given parameter name is not valid, NULL is returned. For valid
// names, return value is guaranteed to be non-NULL. If parameter is not
// set, zero-length string is returned.
const char *mg_get_option(const struct mg_context *ctx, const char *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);
// Return array of strings that represent valid configuration options.
// For each option, a short name, long name, and default value is returned.
// Array is NULL terminated.
const char **mg_get_valid_option_names(void);
/*
* 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);
// 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
// several ways of implementing authentication on the server side. For another,
// cookie-based way please refer to the examples/chat.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(const char *passwords_file_name,
const char *domain,
const char *user,
const char *password);
// Send data to the client.
int mg_write(struct mg_connection *, const void *buf, size_t len);
/*
* 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);
// 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 BUFSIZ defined in <stdio.h>
// (8 KiB on most Linux systems) as temporary message storage for formatting.
// Do not print data that is bigger than that, otherwise it will be truncated.
int mg_printf(struct mg_connection *, const char *fmt, ...)
#ifdef __GNUC__
__attribute__((format(printf, 2, 3)))
#endif
;
/*
* 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);
// Send contents of the entire file together with HTTP headers.
void mg_send_file(struct mg_connection *conn, const char *path);
/*
* 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);
// Read data from the remote end, return number of bytes read.
int mg_read(struct mg_connection *, void *buf, size_t len);
/*
* 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);
// 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 *name);
/*
* 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);
// Get a value of particular form variable.
//
// Parameters:
// data: pointer to form-uri-encoded buffer. This could be either POST data,
// or request_info.query_string.
// data_len: length of the encoded data.
// var_name: variable name to decode from the buffer
// buf: destination buffer for the decoded variable
// buf_len: length of the destination buffer
//
// Return:
// On success, length of the decoded variable.
// On error, -1 (variable not found, or destination buffer is too small).
//
// Destination buffer is guaranteed to be '\0' - terminated. In case of
// failure, dst[0] == '\0'.
int mg_get_var(const char *data, size_t data_len,
const char *var_name, char *buf, size_t buf_len);
// Fetch value of certain cookie variable into the destination buffer.
//
// Destination buffer is guaranteed to be '\0' - terminated. In case of
// failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
// parameter. This function returns only first occurrence.
//
// Return:
// On success, value length.
// On error, 0 (either "Cookie:" header is not present at all, or the
// requested parameter is not found, or destination buffer is too small
// to hold the value).
int mg_get_cookie(const struct mg_connection *,
const char *cookie_name, char *buf, size_t buf_len);
/*
* 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.
*/
// Return Mongoose version.
const char *mg_version(void);
/*
* Print command line usage string.
*/
void mg_show_usage_string(FILE *fp);
// MD5 hash given strings.
// Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
// asciiz strings. When function returns, buf will contain human-readable
// MD5 hash. Example:
// char buf[33];
// mg_md5(buf, "aa", "bb", NULL);
void mg_md5(char *buf, ...);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif // __cplusplus
#endif /* MONGOOSE_HEADER_INCLUDED */
#endif // MONGOOSE_HEADER_INCLUDED
+1
View File
@@ -20,6 +20,7 @@ SUBDIRS += bspcmdline01 cpuuse devfs01 devfs02 devfs03 devfs04 \
mouse01
if NETTESTS
SUBDIRS += mghttpd01
SUBDIRS += ftp01
SUBDIRS += syscall01
endif
+1
View File
@@ -41,6 +41,7 @@ AM_CONDITIONAL(NETTESTS,test "$rtems_cv_RTEMS_NETWORKING" = "yes")
# Explicitly list all Makefiles here
AC_CONFIG_FILES([Makefile
mghttpd01/Makefile
block15/Makefile
block14/Makefile
block13/Makefile
+31
View File
@@ -0,0 +1,31 @@
rtems_tests_PROGRAMS = mghttpd01
mghttpd01_SOURCES = init.c init_fs.c init_fs.h \
test-http-client.c test-http-client.h
mghttpd01_LDADD = -lmghttpd
BUILT_SOURCES = init_fs.c init_fs.h
dist_rtems_tests_DATA = mghttpd01.scn mghttpd01.doc
include $(RTEMS_ROOT)/make/custom/@RTEMS_BSP@.cfg
include $(top_srcdir)/../automake/compile.am
include $(top_srcdir)/../automake/leaf.am
AM_CPPFLAGS += -I$(top_srcdir)/../support/include
LINK_OBJS = $(mghttpd01_OBJECTS) $(mghttpd01_LDADD)
LINK_LIBS = $(mghttpd01_LDLIBS)
mghttpd01$(EXEEXT): $(mghttpd01_OBJECTS) $(mghttpd01_DEPENDENCIES)
@rm -f mghttpd01$(EXEEXT)
$(make-exe)
init_fs.c:
$(BIN2C) -C $(srcdir)/init_fs.tar init_fs
CLEANFILES += init_fs.c
init_fs.h:
$(BIN2C) -H $(srcdir)/init_fs.tar init_fs
CLEANFILES += init_fs.h
include $(top_srcdir)/../automake/local.am
+240
View File
@@ -0,0 +1,240 @@
/*
* Copyright (c) 2012 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Obere Lagerstr. 30
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <rtems.h>
#include <tmacros.h>
#include <rtems/rtems_bsdnet.h>
#include <stdio.h>
#include <mghttpd/mongoose.h>
#include <rtems/imfs.h>
#include <rtems/error.h>
#include "init_fs.h"
#include "test-http-client.h"
#define TARFILE_START init_fs_tar
#define TARFILE_SIZE init_fs_tar_size
#define CBACKTEST_URI "/callbacktest.txt"
#define CBACKTEST_TXT "HTTP/1.1 200 OK\r\n" \
"Content-Type: text/plain\r\n" \
"Content-Length: 47\r\n" \
"\r\n" \
"This is a message from the callback function.\r\n"
#define INDEX_HTML "HTTP/1.1 200 OK\r\n" \
"Date: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \
"Last-Modified: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n" \
"Etag: \"21dae501.a2\"\r\n" \
"Content-Type: text/html\r\n" \
"Content-Length: 162\r\n" \
"Connection: close\r\n" \
"Accept-Ranges: bytes\r\n" \
"\r\n" \
"<html>\r\n" \
"<head>\r\n" \
"<title>Second Instance</title>\r\n" \
"</head>\r\n" \
"\r\n" \
"<body>\r\n" \
"<h1>Second Instance</h1>\r\n" \
"A test page for the Mongoose web server on RTEMS.\r\n" \
"</body>\r\n" \
"</html>\r\n"
#define DATE_TAG "Date: "
#define LASTMOD_TAG "Last-Modified: "
#define TIMESTAMP_SIZE (sizeof("Fri, 01 Jan 1988 00:00:26 GMT") - 1)
#define BUFFERSIZE 1024
static void test_tarfs_load(void)
{
rtems_status_code sc;
printf("Loading tarfs image ... ");
sc = rtems_tarfs_load("/",(void *)TARFILE_START, TARFILE_SIZE);
if (sc != RTEMS_SUCCESSFUL) {
printf ("error: untar failed: %s\n", rtems_status_text (sc));
rtems_test_exit(1);
}
printf ("successful\n");
}
typedef struct {
char *string;
int size;
} printctx;
static void *callback(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info)
{
if (event == MG_NEW_REQUEST) {
int cbacktest = strncmp(request_info->uri, CBACKTEST_URI, sizeof(CBACKTEST_URI));
if (cbacktest == 0)
{
mg_write(conn, CBACKTEST_TXT, sizeof(CBACKTEST_TXT));
/* Mark as processed */
return "";
}
}
return NULL;
}
static void test_mg_index_html(void)
{
httpc_context httpc_ctx;
char *buffer = malloc(BUFFERSIZE);
char *workpos = buffer;
bool brv = false;
int rv = 0;
rtems_test_assert(buffer != NULL);
puts("=== Get the index.html from second Mongoose instance:");
httpc_init_context(&httpc_ctx);
brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 8080);
rtems_test_assert(brv);
brv = httpc_send_request(&httpc_ctx, "GET /index.html", buffer, BUFFERSIZE);
rtems_test_assert(brv);
brv = httpc_close_connection(&httpc_ctx);
rtems_test_assert(brv);
puts(buffer);
/* remove timestamps from html-header */
workpos = strstr(buffer, DATE_TAG);
rtems_test_assert(workpos != NULL);
workpos += sizeof(DATE_TAG) - 1;
memset(workpos, 'x', TIMESTAMP_SIZE);
workpos = strstr(buffer, LASTMOD_TAG);
rtems_test_assert(workpos != NULL);
workpos += sizeof(LASTMOD_TAG) - 1;
memset(workpos, 'x', TIMESTAMP_SIZE);
rv = strcmp(buffer, INDEX_HTML);
rtems_test_assert(rv == 0);
puts("=== OK");
free(buffer);
}
static void test_mg_callback(void)
{
httpc_context httpc_ctx;
char *buffer = malloc(BUFFERSIZE);
bool brv = false;
int rv = 0;
rtems_test_assert(buffer != NULL);
puts("=== Get a page generated from a callback function from" \
" first Mongoose instance:");
httpc_init_context(&httpc_ctx);
brv = httpc_open_connection(&httpc_ctx, "127.0.0.1", 80);
rtems_test_assert(brv);
brv = httpc_send_request(&httpc_ctx, "GET " CBACKTEST_URI, buffer, BUFFERSIZE);
rtems_test_assert(brv);
brv = httpc_close_connection(&httpc_ctx);
rtems_test_assert(brv);
puts(buffer);
rv = strcmp(buffer, CBACKTEST_TXT);
rtems_test_assert(rv == 0);
puts("=== OK");
free(buffer);
}
static void test_mongoose(void)
{
rtems_status_code sc = RTEMS_SUCCESSFUL;
const char *options[] = {
"listening_ports", "80",
"document_root", "/www/",
"num_threads", "1",
"max_request_size", "2048",
"thread_stack_size", "16384",
"thread_priority", "250",
"thread_policy", "o",
NULL};
const char *options2[] = {
"listening_ports", "8080",
"document_root", "/www2/",
"num_threads", "1",
"thread_stack_size", "16384",
"max_request_size", "2048",
NULL};
struct mg_context *mg1 = mg_start(&callback, NULL, options);
struct mg_context *mg2 = mg_start(NULL, NULL, options2);
test_mg_index_html();
test_mg_callback();
mg_stop(mg1);
mg_stop(mg2);
}
static void Init(rtems_task_argument arg)
{
int rv = 0;
puts("\n\n*** TEST MGHTTPD 01 ***");
rv = rtems_bsdnet_initialize_network();
rtems_test_assert(rv == 0);
test_tarfs_load();
test_mongoose();
puts("*** END OF TEST MGHTTPD 01 ***");
rtems_test_exit(0);
}
#define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
#define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
#define CONFIGURE_USE_IMFS_AS_BASE_FILESYSTEM
#define CONFIGURE_FILESYSTEM_IMFS
#define CONFIGURE_LIBIO_MAXIMUM_FILE_DESCRIPTORS 16
#define CONFIGURE_UNLIMITED_OBJECTS
#define CONFIGURE_UNIFIED_WORK_AREAS
#define CONFIGURE_RTEMS_INIT_TASKS_TABLE
#define CONFIGURE_INIT_TASK_STACK_SIZE (16 * 1024)
#define CONFIGURE_INIT
#include <rtems/confdefs.h>
Binary file not shown.
@@ -0,0 +1,11 @@
This file describes the directives and concepts tested by this test set.
test set name: mghttpd01
directives:
TBD
concepts:
- Ensure that the Mongoose HTTP server works with a basic setup
@@ -0,0 +1,33 @@
*** TEST MGHTTPD 01 ***
Loading tarfs image ... successful
=== Get the index.html from second Mongoose instance:
HTTP/1.1 200 OK
Date: Fri, 01 Jan 1988 00:00:01 GMT
Last-Modified: Fri, 01 Jan 1988 00:00:01 GMT
Etag: "21dae501.a2"
Content-Type: text/html
Content-Length: 162
Connection: close
Accept-Ranges: bytes
<html>
<head>
<title>Second Instance</title>
</head>
<body>
<h1>Second Instance</h1>
A test page for the Mongoose web server on RTEMS.
</body>
</html>
=== OK
=== Get a page generated from a callback function from first Mongoose instance:
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 47
This is a message from the callback function.
=== OK
*** END OF TEST MGHTTPD 01 ***
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2012 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Obere Lagerstr. 30
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#include "test-http-client.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
void httpc_init_context(
httpc_context *ctx
)
{
ctx->socket = -1;
ctx->fd = NULL;
}
bool httpc_open_connection(
httpc_context *ctx,
char *targethost,
int targetport
)
{
struct sockaddr_in addr;
struct hostent *server;
struct servent *service;
ctx->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ctx->socket < 0) { return false; }
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(targetport);
server = gethostbyname(targethost);
if(server == NULL) { return false; }
memcpy(&addr.sin_addr.s_addr, server->h_addr, (size_t) server->h_length);
if(connect(ctx->socket, (struct sockaddr *)&addr, sizeof(addr)) != 0)
{
return false;
}
ctx->fd = fdopen(ctx->socket,"rw");
if(ctx->fd == NULL) { return false; }
return true;
}
bool httpc_close_connection(
httpc_context *ctx
)
{
if(close(ctx->socket) != 0)
{
return false;
}
return true;
}
bool httpc_send_request(
httpc_context *ctx,
char *request,
char *response,
int responsesize
)
{
int size = strlen(request);
char lineend[] = " HTTP/1.1\r\n\r\n";
write(ctx->socket, request, size);
write(ctx->socket, lineend, sizeof(lineend));
size = read(ctx->socket, response, responsesize-1);
response[size] = '\0';
return true;
}
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2012 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Obere Lagerstr. 30
* 82178 Puchheim
* Germany
* <rtems@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.com/license/LICENSE.
*/
#ifndef TEST_WEB_CLIENT_H
#define TEST_WEB_CLIENT_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
typedef struct
{
int socket;
FILE *fd;
}
httpc_context;
void httpc_init_context(
httpc_context *ctx
);
bool httpc_open_connection(
httpc_context *ctx,
char *targethost,
int targetport
);
bool httpc_close_connection(
httpc_context *ctx
);
bool httpc_send_request(
httpc_context *ctx,
char *request,
char *response,
int responsesize
);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* TEST_WEB_CLIENT_H */