comparison ngx_http_delay_body_filter_module.c @ 0:a386f95c5ae9

Initial module skeleton. Mostly based on catch body filter. Does not work yet.
author Maxim Dounin <mdounin@mdounin.ru>
date Tue, 17 Aug 2021 22:51:56 +0300
parents
children 7c2d64d9c656
comparison
equal deleted inserted replaced
-1:000000000000 0:a386f95c5ae9
1
2 /*
3 * Copyright (C) Maxim Dounin
4 */
5
6 #include <ngx_config.h>
7 #include <ngx_core.h>
8 #include <ngx_http.h>
9
10
11 typedef struct {
12 ngx_msec_t delay;
13 } ngx_http_delay_body_conf_t;
14
15
16 static void *ngx_http_delay_body_create_conf(ngx_conf_t *cf);
17 static char *ngx_http_delay_body_merge_conf(ngx_conf_t *cf, void *parent,
18 void *child);
19 static ngx_int_t ngx_http_delay_body_init(ngx_conf_t *cf);
20
21
22 static ngx_command_t ngx_http_delay_body_commands[] = {
23
24 { ngx_string("delay_body"),
25 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
26 ngx_conf_set_msec_slot,
27 NGX_HTTP_LOC_CONF_OFFSET,
28 offsetof(ngx_http_delay_body_conf_t, delay),
29 NULL },
30
31 ngx_null_command
32 };
33
34
35 static ngx_http_module_t ngx_http_delay_body_module_ctx = {
36 NULL, /* preconfiguration */
37 ngx_http_delay_body_init, /* postconfiguration */
38
39 NULL, /* create main configuration */
40 NULL, /* init main configuration */
41
42 NULL, /* create server configuration */
43 NULL, /* merge server configuration */
44
45 ngx_http_delay_body_create_conf, /* create location configuration */
46 ngx_http_delay_body_merge_conf /* merge location configuration */
47 };
48
49
50 ngx_module_t ngx_http_delay_body_filter_module = {
51 NGX_MODULE_V1,
52 &ngx_http_delay_body_module_ctx, /* module context */
53 ngx_http_delay_body_commands, /* module directives */
54 NGX_HTTP_MODULE, /* module type */
55 NULL, /* init master */
56 NULL, /* init module */
57 NULL, /* init process */
58 NULL, /* init thread */
59 NULL, /* exit thread */
60 NULL, /* exit process */
61 NULL, /* exit master */
62 NGX_MODULE_V1_PADDING
63 };
64
65
66 static ngx_http_request_body_filter_pt ngx_http_next_request_body_filter;
67
68
69 static ngx_int_t
70 ngx_http_delay_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
71 {
72 ngx_chain_t *cl;
73 ngx_http_delay_body_conf_t *conf;
74
75 conf = ngx_http_get_module_loc_conf(r, ngx_http_delay_body_filter_module);
76
77 if (!conf->delay) {
78 return ngx_http_next_request_body_filter(r, in);
79 }
80
81 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
82 "delay request body filter");
83
84 /* TODO: delay */
85
86 for (cl = in; cl; cl = cl->next) {
87
88 if (cl->buf->last_buf) {
89 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
90 "delay body: last buf");
91 }
92
93 }
94
95 return ngx_http_next_request_body_filter(r, in);
96 }
97
98
99 static void *
100 ngx_http_delay_body_create_conf(ngx_conf_t *cf)
101 {
102 ngx_http_delay_body_conf_t *conf;
103
104 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_delay_body_conf_t));
105 if (conf == NULL) {
106 return NULL;
107 }
108
109 conf->delay = NGX_CONF_UNSET_MSEC;
110
111 return conf;
112 }
113
114
115 static char *
116 ngx_http_delay_body_merge_conf(ngx_conf_t *cf, void *parent, void *child)
117 {
118 ngx_http_delay_body_conf_t *prev = parent;
119 ngx_http_delay_body_conf_t *conf = child;
120
121 ngx_conf_merge_msec_value(conf->delay, prev->delay, 0);
122
123 return NGX_CONF_OK;
124 }
125
126
127 static ngx_int_t
128 ngx_http_delay_body_init(ngx_conf_t *cf)
129 {
130 ngx_http_next_request_body_filter = ngx_http_top_request_body_filter;
131 ngx_http_top_request_body_filter = ngx_http_delay_body_filter;
132
133 return NGX_OK;
134 }