~rbdr/ngx_http_office_hours_filter_module

fa2b280d41f583da217a5bdccea67e87cdadffb6 — Ben Beltran 4 years ago af4b488
Use astyle instead of indent
5 files changed, 133 insertions(+), 99 deletions(-)

M .gitignore
D .indent.pro
M Makefile
M README.md
M ngx_http_office_hours_filter_module.c
M .gitignore => .gitignore +1 -1
@@ 1,1 1,1 @@
*.BAK
*.orig

D .indent.pro => .indent.pro +0 -21
@@ 1,21 0,0 @@
-nbad
-bap
-nbc
-br
-c33
-cd33
-ncdb
-ce
-ci4
-cli0
-d0
-di1
-nfc1
-i4
-ip0
-l75
-lp
-npcs
-npsl
-nsc
-nsob

M Makefile => Makefile +1 -1
@@ 2,4 2,4 @@ setup_hooks:
	ln -s ../../scripts/git-hooks/pre-commit .git/hooks/pre-commit

format:
	indent *.c
	astyle --style=kr *.c

M README.md => README.md +20 -1
@@ 4,7 4,7 @@ A victory for the server's labor rights: An nginx module that allows you to serv

## Using the directive

The `office_hours` directive expects a list of time ranges sepaarated by
The `office_hours` directive expects a list of time ranges separated by
spaces. The first range will be used as the default, and the rest will
be read from right to left, ending with *sunday*



@@ 61,6 61,21 @@ Uninstall nginx.
I'm still not sure! I'm learning how to build this nginx module, so I'll
figure it out as I go and add it here.

* [astyle][astyle]: Used to format the code
* An `nginx` [source distribution][nginx] to compile the code.

## Building the module

You can build this module as a dynamic module. From the `nginx` source
directory run:

```
./configure --add-dynamic-module=/path/to/ngx_http_office_hours_filter_module
make
```

For more information check the [nginx docs][nginx-module-docs]

## Installing git hooks

This repo contains a pre-commit git hook so indent will run before every


@@ 72,3 87,7 @@ commit. Run `make setup_hooks` to install it.
* Add support to respect the time zone of the visitor and not just the
  server
* Add support for double shifts in the same day

[astyle]: http://astyle.sourceforge.net
[nginx]: https://nginx.org/en/download.html
[nginx-module-docs]: https://www.nginx.com/resources/wiki/extending/

M ngx_http_office_hours_filter_module.c => ngx_http_office_hours_filter_module.c +111 -75
@@ 16,102 16,133 @@
#include <ngx_core.h>
#include <ngx_http.h>

// Module Config Definitions
/*
 * Declarations
 */

/* Main Configuration Structure */

typedef struct {
    ngx_array_t *office_hours;
} ngx_http_office_hours_conf_t;

/* Lifecycle Functions For Module Context */

static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf);
static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child);
static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
        void *parent, void *child);
static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf);

static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf);
/* Configuration Handler */

static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
                                   void *conf);

/* Body Filter Storage */

static ngx_http_output_body_filter_pt ngx_http_next_body_filter;

/*
 * Module Definitions
 */

//Module Directive Definitions
/* Module Directives */

static ngx_command_t ngx_http_office_hours_commands[] = {
    {
	ngx_string("office_hours"),
	NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_1MORE,
	ngx_http_office_hours,
	NGX_HTTP_LOC_CONF_OFFSET,
	offsetof(ngx_http_office_hours_conf_t, office_hours),
	NULL
        ngx_string("office_hours"),
        NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_1MORE,
        ngx_http_office_hours,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_office_hours_conf_t, office_hours),
        NULL
    },

    ngx_null_command
};

//Define the module context

/* Module Context */

static ngx_http_module_t ngx_http_office_hours_filter_module_ctx = {
    NULL, //Preconfiguration
    ngx_http_office_hours_init, //Postconfiguration
    NULL,			/* Preconfiguration */
    ngx_http_office_hours_init,	/* Postconfiguration */

    NULL, //Create main configuration
    NULL, //Initialize main configuration
    NULL, //Create server configuration
    NULL, //Merge server configuration
    NULL,			/* Create main configuration */
    NULL,			/* Initialize main configuration */

    ngx_http_office_hours_create_conf, //Create location configuration
    ngx_http_office_hours_merge_conf // Merge location configuration
    NULL,			/* Create server configuration */
    NULL,			/* Merge server configuration */

    ngx_http_office_hours_create_conf,	/* Create location configuration */
    ngx_http_office_hours_merge_conf	/* Merge location configuration */
};

//Define the module

/* Module Definition */

ngx_module_t ngx_http_office_hours_filter_module = {
    NGX_MODULE_V1, //Module Version
	& ngx_http_office_hours_filter_module_ctx, //Module context
	ngx_http_office_hours_commands, //Module commands
	NGX_HTTP_MODULE, //Module Type
	NULL, //Initialize Master
	NULL, //Initialize Module
	NULL, //Initialize Process
	NULL, //Initialize Thread
	NULL, //Exit Thread
	NULL, //Exit Process
	NULL, //Exit Master
	NGX_MODULE_V1_PADDING
    NGX_MODULE_V1,		//Module Version
    &ngx_http_office_hours_filter_module_ctx,	//Module context
    ngx_http_office_hours_commands,	//Module commands
    NGX_HTTP_MODULE,		//Module Type
    NULL,			//Initialize Master
    NULL,			//Initialize Module
    NULL,			//Initialize Process
    NULL,			//Initialize Thread
    NULL,			//Exit Thread
    NULL,			//Exit Process
    NULL,			//Exit Master
    NGX_MODULE_V1_PADDING
};


//Main Body Filter

static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
/*
 * Main Body Filter
 * If the current time is within office hours, it goes to the next
 * handler. Otherwise it returns 403 and the office hour listing.
 */

static ngx_int_t
 ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in)
ngx_http_office_hours_body_filter(ngx_http_request_t * r, ngx_chain_t * in)
{

    ngx_http_office_hours_conf_t *conf;
    ngx_uint_t i;
    ngx_str_t *hours;

    conf = ngx_http_get_module_loc_conf(r, ngx_http_office_hours_filter_module);
    conf =
        ngx_http_get_module_loc_conf(r,
                                     ngx_http_office_hours_filter_module);

    if (conf->office_hours == NULL) {

	ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Within office hours");

	return ngx_http_next_body_filter(r, in);
        ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
                      "Within office hours");
        return ngx_http_next_body_filter(r, in);
    }
    ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "Outside office hours");

    ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
                  "Outside office hours");

    hours = conf->office_hours->elts;

    for (i = 0; i < conf->office_hours->nelts; ++i) {
	ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, (const char *)hours[i].data);
        ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0,
                      (const char *) hours[i].data);
    }

    r->keepalive = 0;
    return NGX_HTTP_FORBIDDEN;
}

//Read Arguments from config file
/*
 * Callback for `office_hours ` directive
 * Reads the configuration loaded from the config file(cf)
 * And writes it to the right place in the module configuration(conf)
 */

static char *
 ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd, void *conf)
static char *ngx_http_office_hours(ngx_conf_t * cf, ngx_command_t * cmd,
                                   void *conf)
{

    char *conf_structure = conf;


@@ 121,41 152,41 @@ static char *
    ngx_str_t *value;
    ngx_uint_t i;

    //Gets the array from the config structure using the defined
	// offset, and if the
	pointer is unset it creates a new one.
	    //
	    //(The first element is the directive itself, so we should be
	       // offset by 1)
	    office_hours = (ngx_array_t **) (conf_structure + cmd->offset);
    /* Gets the array from the config structure using the defined
     * offset, and if the pointer is unset it creates a new one.
     * (The first element is the directive itself, so we should be
     * offset by 1)
     */
    office_hours = (ngx_array_t **) (conf_structure + cmd->offset);

    if (*office_hours == NGX_CONF_UNSET_PTR) {
	*office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1,
					 sizeof(ngx_str_t));
        *office_hours = ngx_array_create(cf->pool, cf->args->nelts - 1,
                                         sizeof(ngx_str_t));

	if (*office_hours == NULL) {
	    return NGX_CONF_ERROR;
	}
        if (*office_hours == NULL) {
            return NGX_CONF_ERROR;
        }
    }
    value = cf->args->elts;

    for (i = 1; i < cf->args->nelts; ++i) {

	hours = ngx_array_push(*office_hours);
	if (hours == NULL) {
	    return NGX_CONF_ERROR;
	}
	*hours = value[i];
        hours = ngx_array_push(*office_hours);
        if (hours == NULL) {
            return NGX_CONF_ERROR;
        }
        *hours = value[i];
    }

    return NGX_CONF_OK;
}


//Initialize the Configuration Object
/*
 * Config Creator
 * Initializes the configuration structure
 */

static void *
 ngx_http_office_hours_create_conf(ngx_conf_t * cf)
static void *ngx_http_office_hours_create_conf(ngx_conf_t * cf)
{

    ngx_http_office_hours_conf_t *conf;


@@ 163,17 194,21 @@ static void *
    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_office_hours_conf_t));

    if (conf == NULL) {
	return NULL;
        return NULL;
    }
    conf->office_hours = NGX_CONF_UNSET_PTR;

    return conf;
}

//Merge Config Values
/*
 * Merge Config Values
 * Sets the defaults for the configuration and merges
 * with other configurations
 */

static char *
 ngx_http_office_hours_merge_conf(ngx_conf_t * cf, void *parent, void *child)
static char *ngx_http_office_hours_merge_conf(ngx_conf_t * cf,
        void *parent, void *child)
{

    ngx_http_office_hours_conf_t *prev = parent;


@@ 184,11 219,12 @@ static char *
    return NGX_CONF_OK;
}

//Postconfig Initialization Handler
// Sets the request filter at the top of the chain
/*
 * Postconfig Initialization Handler
 * Sets the request filter at the top of the chain
 */

static ngx_int_t
 ngx_http_office_hours_init(ngx_conf_t * cf)
static ngx_int_t ngx_http_office_hours_init(ngx_conf_t * cf)
{

    ngx_http_next_body_filter = ngx_http_top_body_filter;