Skip to content

Commit

Permalink
lib: monkey: upgrade http stack
Browse files Browse the repository at this point in the history
  0ef0dee mk_server: http: fix api to handle request_ends
  87a188c mk_server: http: add support for ETag header (fix #139)
  94cfa12 mk_server: http: fix trace messages (socket number)

Signed-off-by: Eduardo Silva <eduardo@treasure-data.com>
  • Loading branch information
edsiper committed Jun 30, 2015
1 parent 0eff596 commit 0bca1e0
Show file tree
Hide file tree
Showing 17 changed files with 376 additions and 153 deletions.
2 changes: 1 addition & 1 deletion lib/monkey/include/monkey/mk_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pthread_key_t MK_EXPORT _mkp_data;

#ifdef TRACE
#define PLUGIN_TRACE(...) \
mk_api->trace("", \
mk_api->trace("plug", \
MK_TRACE_PLUGIN, \
__FUNCTION__, \
__FILE__, \
Expand Down
2 changes: 2 additions & 0 deletions lib/monkey/include/monkey/mk_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ struct mk_http_session
int close_now; /* Close the session ASAP */

struct mk_channel *channel;
struct mk_sched_conn *conn;

unsigned int body_size;
unsigned int body_length;
Expand Down Expand Up @@ -198,6 +199,7 @@ void mk_http_request_init(struct mk_http_session *session,
struct mk_http_request *request);
struct mk_http_header *mk_http_header_get(int name, struct mk_http_request *req,
const char *key, unsigned int len);
int mk_http_request_end(struct mk_http_session *cs);

#define mk_http_session_get(conn) \
(struct mk_http_session *) \
Expand Down
12 changes: 12 additions & 0 deletions lib/monkey/include/monkey/mk_http_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <monkey/mk_stream.h>

#define MK_HEADER_ETAG_SIZE 32

struct response_headers
{
int status;
Expand Down Expand Up @@ -55,6 +57,9 @@ struct response_headers
mk_ptr_t content_encoding;
char *location;

int etag_len;
char etag_buf[MK_HEADER_ETAG_SIZE];

/*
* This field allow plugins to add their own response
* headers
Expand Down Expand Up @@ -140,6 +145,13 @@ struct mk_http_request
*/
int stage30_blocked;

/*
* If the connection is being managed by a plugin (e.g: CGI), associate the
* plugin reference to the stage30_handler field. This is useful to handle
* protocol exception and notify the handlers about it.
*/
void *stage30_handler;

/* Static file information */
struct file_info file_info;

Expand Down
36 changes: 20 additions & 16 deletions lib/monkey/include/monkey/mk_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ struct plugin_api
void (*_error) (int, const char *, ...) PRINTF_WARNINGS(2,3);

/* HTTP request function */
int (*http_session_end) (struct mk_http_session *session);
int (*http_request_end) (struct mk_http_session *cs, int close);
int (*http_request_error) (int, struct mk_http_session *, struct mk_http_request *);

/* memory functions */
Expand Down Expand Up @@ -243,20 +243,7 @@ struct mk_plugin_event
struct mk_plugin *handler; /* plugin owner/handler */
};

struct mk_plugin_stage {
int (*stage10) (int);
int (*stage20) (struct mk_http_session *, struct mk_http_request *);
int (*stage30) (struct mk_plugin *, struct mk_http_session *,
struct mk_http_request *);
int (*stage40) (struct mk_http_session *, struct mk_http_request *);
int (*stage50) (int);

/* Just a reference to the parent plugin */
struct mk_plugin *plugin;

/* Only used when doing direct mapping from config->stageN_handler; */
struct mk_list _head;
};
struct mk_plugin_stage;

/* Info: used to register a plugin */
struct mk_plugin {
Expand Down Expand Up @@ -291,6 +278,23 @@ struct mk_plugin {
int load_type;
};

struct mk_plugin_stage {
int (*stage10) (int);
int (*stage20) (struct mk_http_session *, struct mk_http_request *);
int (*stage30) (struct mk_plugin *, struct mk_http_session *,
struct mk_http_request *);
int (*stage30_hangup) (struct mk_plugin *, struct mk_http_session *,
struct mk_http_request *);
int (*stage40) (struct mk_http_session *, struct mk_http_request *);
int (*stage50) (int);

/* Just a reference to the parent plugin */
struct mk_plugin *plugin;

/* Only used when doing direct mapping from config->stageN_handler; */
struct mk_list _head;
};


void mk_plugin_api_init();
void mk_plugin_load_all();
Expand Down Expand Up @@ -321,7 +325,7 @@ int mk_plugin_event_socket_change_mode(int socket, int mode, unsigned int behavi
struct mk_plugin *mk_plugin_load(int type, const char *shortname,
void *data);
void *mk_plugin_load_symbol(void *handler, const char *symbol);
int mk_plugin_http_session_end(struct mk_http_session *cs);
int mk_plugin_http_request_end(struct mk_http_session *cs, int close);

/* Register functions */
struct plugin *mk_plugin_register(struct plugin *p);
Expand Down
4 changes: 3 additions & 1 deletion lib/monkey/include/monkey/mk_plugin_stage.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ static inline int mk_plugin_stage_run_20(struct mk_http_session *cs,
}

static inline int mk_plugin_stage_run_30(struct mk_http_session *cs,
struct mk_http_request *sr)
struct mk_http_request *sr,
struct mk_plugin **handler)
{
int ret;
struct mk_list *head;
Expand All @@ -77,6 +78,7 @@ static inline int mk_plugin_stage_run_30(struct mk_http_session *cs,
return ret;
case MK_PLUGIN_RET_CLOSE_CONX:
case MK_PLUGIN_RET_CONTINUE:
*handler = stage->plugin;
return ret;
default:
mk_err("Plugin returns invalid value %i", ret);
Expand Down
104 changes: 89 additions & 15 deletions lib/monkey/man/monkey.1
Original file line number Diff line number Diff line change
@@ -1,30 +1,108 @@
.TH Monkey 1 "Feb 5, 2011"
.TH Monkey 1 "Jul 01, 2015"
.\" Please update the above date whenever this man page is modified.
.SH NAME
monkey \- Monkey HTTP Server
.SH SYNOPSIS
.B monkey [options]
.SH DESCRIPTION
\fBmonkey\fP is a very small, fast and scalable web server for Linux.
This manual page only lists the command line arguments. For further and more detailed information regarding the program configuration see the Monkey manual, found at http://monkey-project.com/documentation .
\fBmonkey\fP is fast and scalable web server for Linux, OSX and FreeBSD.
This manual page only lists the command line arguments available. For further and
more detailed information regarding the program configuration see the Monkey manual,
found at http://monkey-project.com/documentation .

.SH OPTIONS
This command will launch the Monkey webserver. It accepts the
following options:
.TP 8
.B \-h, --help
Prints a brief help message and Perform a sanity check on the configuration file. The server will not run.

.B \-c, --configdir=DIR
Allows to specify an optional configuration directory where the required
configuration files by Monkey are located. It's expected that under this directory
exists the files: monkey.conf, monkey.mime, plugins.load and the plugins/ directory.
.TP 8
.B \-v, --version
Prints Monkey's version and exit

.B \-s, --serverconf=FILE
Overrides the default path of the 'monkey.conf' configuration file.
.TP 8
.B \-D, --daemon

.B \-d, --daemon
Launches the server as a daemon (background process). The default behaviour is to
stay attached to the controlling terminal.
.TP 8
.B \-c, --configdir directory
Specifies the configuration files directory

.B \-I, --pid-file=FILE
Overrides the default path for the 'pid' file.
.TP 8

.B \-p, --port=PORT
Specify a single listener TCP port, this option overrides the content of the
configuration file.
.TP 8

.B \-o, --one-shot=DIR
The one-shot mode and directory path associated, makes to serve that directory
content as the document root of the default virtual host.
.TP 8

.B \-t, --transport=TRANSPORT
Specify a transport layer plugin. This option overrides the configuration file
and allows just one plugin, either 'liana' for plain sockets or 'tls' for SSL/TLS
support (just if 'tls' plugin was compiled and available).
.TP 8

.B \-w, --workers=N
Specify the number of worker threads that Monkey will spawn upon start. Each worker
thread is capable to handle several connections, is suggested to spawn a worker per
CPU core available. By default Monkey set this value to zero (--workers=0) which
let's Monkey core to detect the number of CPU core available and spawn one worker
per core.
.TP 8

.B \-m, --mimes-conf-file=FILE
Specify a configuration file that holds the mime types available.
.TP 8

.B \-l, --plugins-load-conf-file=FILE
Specify a configuration file that holds the list of dynamic plugins to load.
.TP 8

.B \-S, --sites-conf-dir=DIR
Specify the directory path where the files that defines the virtual hosts are
available.
.TP 8

.B \-P, --plugins-conf-dir=DIR
Specify the directory path where the plugins can locate their configuration files.
.TP 8

.B \-b, --balancing-mode
This option enable the OLD balancing mode when receiving connections. It means that
for every TCP connection that arrives, the Monkey Scheduler will assign the
connection to the least-busy worker thread.

The default and new mechanism is based on the shared TCP sockets implementation,
which let the OS Kernel to decide to which worker thread assign the new connection.
.TP 8

.B \-T, --allow-shared-sockets
When using shared TCP sockets (no --balancing-mode), multiple instances of Monkey
can be started. Monkey will detect if the TCP port is in use by another process,
despite the address can be shared, Monkey will notify this and abort. This option
let Monkey know that despites the address is shared and in-use, continue anyways.
.TP 8

.B \-b, --build
Print build information and exit. It prints to the standard output the configure
options, default paths, built-in plugins and others.
.TP 8

.B \-v, --version
Prints Monkey's version and exit.
.TP 8

.B \-h, --help
Prints this help.
.TP 8

.SH SIGNALS
The following signals are supported by Monkey:
Expand All @@ -43,11 +121,7 @@ The following signals are supported by Monkey:
.SS Bug reports
In general, send bug reports to the bug report mailing list at <monkey@lists.monkey-project.com>. You are requested to use a descriptive subject for your email message, perhaps parts of the error message.

.SH "SEE ALSO"
Monkey can be run either with this command or
\&\fIbanana\fR\|(1).

.SH AUTHOR
Eduardo Silva <edsiper@gmail.com> and the rest of the Monkey Project team.
Eduardo Silva <eduardo@monkey.io> and the rest of the Monkey Project team.
.PP
This manpage is maintained by the Monkey HTTP Daemon Project team.
4 changes: 3 additions & 1 deletion lib/monkey/mk_core/mk_event_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ static inline int _mk_event_del(struct mk_event_ctx *ctx, struct mk_event *event
MK_TRACE("[FD %i] Epoll, remove from QUEUE_FD=%i, ret=%i",
event->fd, ctx->efd, ret);
if (ret < 0) {
mk_libc_error("epoll_ctl");
#ifdef TRACE
mk_libc_warn("epoll_ctl");
#endif
}

return ret;
Expand Down
5 changes: 5 additions & 0 deletions lib/monkey/mk_server/mk_header.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ int mk_header_prepare(struct mk_http_session *cs,
}
}

/* E-Tag */
if (sh->etag_len > 0) {
mk_iov_add(iov, sh->etag_buf, sh->etag_len, MK_FALSE);
}

/* Content-Encoding */
if (sh->content_encoding.len > 0) {
mk_iov_add(iov, mk_header_content_encoding.data,
Expand Down

0 comments on commit 0bca1e0

Please sign in to comment.