forked from cfsego/limit_upload_rate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
for-nginx.patch
169 lines (152 loc) · 5.81 KB
/
for-nginx.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
diff -BurN nginx-1.2.5/src/http/ngx_http.c nginx-1.2.5-new/src/http/ngx_http.c
--- nginx-1.2.5/src/http/ngx_http.c 2012-08-07 00:03:56.000000000 +0800
+++ nginx-1.2.5-new/src/http/ngx_http.c 2012-12-07 16:26:42.000000000 +0800
@@ -66,12 +66,18 @@
ngx_http_conf_addr_t *addr);
#endif
+static ngx_int_t ngx_http_dummy_input_body_filter(ngx_http_request_t *r,
+ ngx_buf_t *buf);
+
ngx_uint_t ngx_http_max_module;
ngx_int_t (*ngx_http_top_header_filter) (ngx_http_request_t *r);
ngx_int_t (*ngx_http_top_body_filter) (ngx_http_request_t *r, ngx_chain_t *ch);
+ngx_int_t (*ngx_http_top_input_body_filter) (ngx_http_request_t *r,
+ ngx_buf_t *buf);
+
ngx_str_t ngx_http_html_default_types[] = {
ngx_string("text/html"),
@@ -218,6 +224,10 @@
pcf = *cf;
cf->ctx = ctx;
+ /* init input body filter pointer */
+
+ ngx_http_top_input_body_filter = ngx_http_dummy_input_body_filter;
+
for (m = 0; ngx_modules[m]; m++) {
if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
continue;
@@ -2083,3 +2093,12 @@
return NGX_OK;
}
+
+
+static ngx_int_t
+ngx_http_dummy_input_body_filter(ngx_http_request_t *r, ngx_buf_t *buf)
+{
+ ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "http dummy input body filter");
+ return NGX_OK;
+}
diff -BurN nginx-1.2.5/src/http/ngx_http.h nginx-1.2.5-new/src/http/ngx_http.h
--- nginx-1.2.5/src/http/ngx_http.h 2012-01-18 23:07:43.000000000 +0800
+++ nginx-1.2.5-new/src/http/ngx_http.h 2012-12-07 16:11:59.000000000 +0800
@@ -158,4 +158,7 @@
extern ngx_http_output_body_filter_pt ngx_http_top_body_filter;
+extern ngx_http_input_body_filter_pt ngx_http_top_input_body_filter;
+
+
#endif /* _NGX_HTTP_H_INCLUDED_ */
diff -BurN nginx-1.2.5/src/http/ngx_http_core_module.h nginx-1.2.5-new/src/http/ngx_http_core_module.h
--- nginx-1.2.5/src/http/ngx_http_core_module.h 2012-06-04 19:58:12.000000000 +0800
+++ nginx-1.2.5-new/src/http/ngx_http_core_module.h 2012-12-07 16:25:09.000000000 +0800
@@ -505,6 +505,8 @@
typedef ngx_int_t (*ngx_http_output_body_filter_pt)
(ngx_http_request_t *r, ngx_chain_t *chain);
+typedef ngx_int_t (*ngx_http_input_body_filter_pt)
+ (ngx_http_request_t *r, ngx_buf_t *buf);
ngx_int_t ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *chain);
ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *chain);
diff -BurN nginx-1.2.5/src/http/ngx_http_request_body.c nginx-1.2.5-new/src/http/ngx_http_request_body.c
--- nginx-1.2.5/src/http/ngx_http_request_body.c 2012-04-13 03:35:41.000000000 +0800
+++ nginx-1.2.5-new/src/http/ngx_http_request_body.c 2012-12-07 16:11:11.000000000 +0800
@@ -31,7 +30,8 @@
{
size_t preread;
ssize_t size;
- ngx_buf_t *b;
+ ngx_buf_t *b, buf;
+ ngx_int_t rc;
ngx_chain_t *cl, **next;
ngx_temp_file_t *tf;
ngx_http_request_body_t *rb;
@@ -128,6 +128,15 @@
b->last = r->header_in->last;
b->end = r->header_in->end;
+ ngx_memzero(&buf, sizeof(ngx_buf_t));
+ buf.memory = 1;
+ buf.start = r->header_in->pos;
+ buf.pos = r->header_in->pos;
+ buf.last = (off_t) preread >= r->headers_in.content_length_n
+ ? r->header_in->pos + (size_t) r->headers_in.content_length_n
+ : r->header_in->last;
+ buf.end = r->header_in->end;
+
rb->bufs = ngx_alloc_chain_link(r->pool);
if (rb->bufs == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -138,6 +147,27 @@
rb->buf = b;
+ rc = ngx_http_top_input_body_filter(r, &buf);
+ if (rc != NGX_OK) {
+ if (rc > NGX_OK && rc < NGX_HTTP_SPECIAL_RESPONSE) {
+ ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
+ "input filter: return code 1xx or 2xx "
+ "will cause trouble and is converted to 500");
+ }
+
+ /**
+ * NGX_OK: success and continue;
+ * NGX_ERROR: failed and exit;
+ * NGX_AGAIN: not ready and retry later.
+ */
+
+ if (rc < NGX_HTTP_SPECIAL_RESPONSE && rc != NGX_AGAIN) {
+ rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ return rc;
+ }
+
if ((off_t) preread >= r->headers_in.content_length_n) {
/* the whole request body was pre-read */
@@ -263,7 +293,8 @@
{
size_t size;
ssize_t n;
- ngx_buf_t *b;
+ ngx_buf_t *b, buf;
+ ngx_int_t rc;
ngx_connection_t *c;
ngx_http_request_body_t *rb;
ngx_http_core_loc_conf_t *clcf;
@@ -311,10 +342,32 @@
return NGX_HTTP_BAD_REQUEST;
}
+ ngx_memzero(&buf, sizeof(ngx_buf_t));
+ buf.memory = 1;
+ buf.start = rb->buf->last;
+ buf.pos = rb->buf->last;
+ buf.last = buf.start + n;
+ buf.end = buf.last;
+
rb->buf->last += n;
rb->rest -= n;
r->request_length += n;
+ rc = ngx_http_top_input_body_filter(r, &buf);
+ if (rc != NGX_OK) {
+ if (rc > NGX_OK && rc < NGX_HTTP_SPECIAL_RESPONSE) {
+ ngx_log_error(NGX_LOG_ERR, c->log, 0,
+ "input filter: return code 1xx or 2xx "
+ "will cause trouble and is converted to 500");
+ }
+
+ if (rc < NGX_HTTP_SPECIAL_RESPONSE && rc != NGX_AGAIN) {
+ rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
+ }
+
+ return rc;
+ }
+
if (rb->rest == 0) {
break;
}