Monkey Plugins

5.3. Monkey Plugins

A plugin for Monkey is a shared library written in C which is loaded on startup per instruction of the plugins.load configuration file. This plugin is composed by mandatory and optional functions that work like hooks being called from Monkey core.

Every Monkey hook function is prefixed with _mkp_.

5.3.1. Mandatory hooks

5.3.1.1. _mkp_init()

Function called when registering the plugin, it also allows to set basic configuration.

int _mkp_init(struct plugin_api **api, char *confdir)

Return Value: On success, the plugin returns 0. On error, -1 is returned and the plugin is unregistered by Monkey.

5.3.1.2. _mkp_exit()

Function called before to unload the plugin, the plugin must free the used memory resources.

void _mkp_exit()

Return Value: No return value.

5.3.2. Core Plugin

The Core plugins interface, allow plugins to define some data in the process context or thread context depending of it needs when Monkey is starting up.

5.3.2.1. _mkp_core_prctx()

This function is invoked when Monkey is still in the process context, the server loop has not yet started

void _mkp_core_prctx()

Plugin definition: MK_PLUGIN_CORE_PRCTX

5.3.2.2. _mkp_core_thctx()

This function is invoked when Monkey goes into the thread context. When each worker thread is started this function is invoked. Server loop has not yet started

void _mkp_core_thctx()

Plugin definition: MK_PLUGIN_CORE_THCTX

5.3.3. Stage Plugin

Every single HTTP request is placed in a cycle of stages. The request pass around five stages and each one provides a hook function which is invoked by Monkey, so you can program your own routines and program specific actions when the stage is being called.

5.3.3.1. Return values

MK_PLUGIN_RET_CONTINUE: The plugin will not take any action and the request can continue be processed normally.

MK_PLUGIN_RET_NOT_ME: The plugin will not own the request.

MK_PLUGIN_RET_END: The plugin has finished to work the request.

MK_PLUGIN_RET_CLOSE_CNX: The client connection must be closed.

5.3.3.2. _mkp_stage_10()

This is the first stage of the cycle, it means that the HTTP remote connection has been just accept()ed and not yet assigned to a working thread. At this level you just have the remote socket file descriptor.

int _mkp_stage_10(int sockfd)

Plugin definition: MK_PLUGIN_STAGE_10

Return values: MK_PLUGIN_RET_CONTINUE or MK_PLUGIN_RET_CLOSE_CNX.

5.3.3.3. _mkp_stage_20()

On this stage the HTTP Request stream has been just received and parsed. If you have pipelined request, this function will be invoked as many pipelined request exist.

int _mkp_stage_20(struct client_request *cr, struct request *sr)

Plugin definition: MK_PLUGIN_STAGE_20

Return values: MK_PLUGIN_RET_CONTINUE or MK_PLUGIN_RET_CLOSE_CNX.

5.3.3.4. _mkp_stage_30()

The plugin acts like an Object handler, it will take care of the request, it decides what to do with the request.

int _mkp_stage_30(struct plugin *p, struct client_request *cr, struct request *sr)

Plugin definition: MK_PLUGIN_STAGE_30

Return values: MK_PLUGIN_RET_NOT_ME, MK_PLUGIN_RET_CONTINUE, MK_PLUGIN_RET_END or MK_PLUGIN_RET_CLOSE_CNX.

5.3.3.5. _mkp_stage_40()

The request has ended, the content has been served.

int _mkp_stage_40(struct client_request *cr, struct request *sr)

Plugin definition: MK_PLUGIN_STAGE_40

Return values: MK_PLUGIN_RET_NOT_ME or MK_PLUGIN_RET_CONTINUE.

5.3.3.6. _mkp_stage_50()

Client connection has been closed by Monkey core.

int _mkp_stage_50(int sockfd)

Plugin definition: MK_PLUGIN_STAGE_50

Return values: MK_PLUGIN_RET_NOT_ME or MK_PLUGIN_RET_CONTINUE.

5.3.4. Networking Plugin

A Networking plugin provide the networking functions for the core of Monkey, it basically provides a mechanism to replace the I/O and IP related functions. Just one networking plugin can exists for I/O and IP.

5.3.4.1. I/O

Input and Output provides functions to work with basic socket operations

Plugin definition: MK_PLUGIN_NETWORK_IO

5.3.4.1.1. _mkp_network_io_accept()

Accept socket conection

int _mkp_network_io_accept(int server_fd, struct sockaddr_in sockaddr)

5.3.4.1.2. _mkp_network_io_accept()

Accept socket conection

int _mkp_network_io_accept(int server_fd, struct sockaddr_in sockaddr)

5.3.4.1.3. _mkp_network_io_read()

Read from socket

int _mkp_network_io_accept(int server_fd, struct sockaddr_in sockaddr)

5.3.4.1.4. _mkp_network_io_write()

Write to socket

int _mkp_network_io_write(int sockfd, const void *buf, size_t count)

5.3.4.1.5. _mkp_network_io_writev()

Write iov struct to socket

int _mkp_network_io_writev(int sockfd, struct mk_iov *mk_io)

5.3.4.1.6. _mkp_network_io_close()

Close socket

int _mkp_network_io_close(int sockfd)

5.3.4.1.7. _mkp_network_io_connect

Connect to remote socket server

int _mkp_network_io_connect(const char *host, int port)

5.3.4.1.8. _mkp_network_io_send_file()

Send file to socket

int _mkp_network_io_send_file(int sockfd, int filefd, off_t *file_offset, size_t file_count)

5.3.5. Optional functions

You should use these optional functions depending of your own needs, as they are not mandatory you are able to extend and define the desired behaviors.