forked from nulab/nginx-too-many-requests-retryafter-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx-1.6.x-too-many-requests-retryafter.patch
131 lines (115 loc) · 5.66 KB
/
nginx-1.6.x-too-many-requests-retryafter.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
diff -Bbu -ur nginx-1.6.0/src/http/modules/ngx_http_limit_req_module.c nginx-1.6.0-retry-after-pached/src/http/modules/ngx_http_limit_req_module.c
--- nginx-1.6.0/src/http/modules/ngx_http_limit_req_module.c 2014-04-24 21:52:25.000000000 +0900
+++ nginx-1.6.0-retry-after-pached/src/http/modules/ngx_http_limit_req_module.c 2014-05-13 23:25:50.000000000 +0900
@@ -167,6 +167,9 @@
ngx_http_limit_req_ctx_t *ctx;
ngx_http_limit_req_conf_t *lrcf;
ngx_http_limit_req_limit_t *limit, *limits;
+ ngx_table_elt_t *h;
+ ngx_uint_t rate;
+ u_char *ra;
if (r->main->limit_req_set) {
return NGX_DECLINED;
@@ -176,6 +179,7 @@
limits = lrcf->limits.elts;
excess = 0;
+ rate = 0;
rc = NGX_DECLINED;
@@ -223,6 +227,7 @@
n, rc, excess / 1000, excess % 1000);
if (rc != NGX_AGAIN) {
+ rate = ctx->rate;
break;
}
}
@@ -240,6 +245,17 @@
"limiting requests, excess: %ui.%03ui by zone \"%V\"",
excess / 1000, excess % 1000,
&limit->shm_zone->shm.name);
+
+ if ( rate > 0 && (ra = ngx_palloc(r->pool, sizeof("1000"))) != NULL){
+ h = ngx_list_push(&r->headers_out.headers);
+ if ( h != NULL ){
+ h->hash = 1;
+ ngx_str_set(&h->key, "Retry-After");
+ ngx_sprintf(ra, "%ui", (rate >= 1000) ? 1 : 1000/rate);
+ h->value.len = ngx_strlen(ra);
+ h->value.data = ra;
+ }
+ }
}
while (n--) {
@@ -722,7 +738,7 @@
NGX_LOG_INFO : conf->limit_log_level + 1;
ngx_conf_merge_uint_value(conf->status_code, prev->status_code,
- NGX_HTTP_SERVICE_UNAVAILABLE);
+ NGX_HTTP_TOO_MANY_REQUESTS);
return NGX_CONF_OK;
}
diff -Bbu -ur nginx-1.6.0/src/http/ngx_http_header_filter_module.c nginx-1.6.0-retry-after-pached/src/http/ngx_http_header_filter_module.c
--- nginx-1.6.0/src/http/ngx_http_header_filter_module.c 2014-04-24 21:52:26.000000000 +0900
+++ nginx-1.6.0-retry-after-pached/src/http/ngx_http_header_filter_module.c 2014-05-13 23:25:25.000000000 +0900
@@ -96,16 +96,23 @@
ngx_string("415 Unsupported Media Type"),
ngx_string("416 Requested Range Not Satisfiable"),
- /* ngx_null_string, */ /* "417 Expectation Failed" */
- /* ngx_null_string, */ /* "418 unused" */
- /* ngx_null_string, */ /* "419 unused" */
- /* ngx_null_string, */ /* "420 unused" */
- /* ngx_null_string, */ /* "421 unused" */
- /* ngx_null_string, */ /* "422 Unprocessable Entity" */
- /* ngx_null_string, */ /* "423 Locked" */
- /* ngx_null_string, */ /* "424 Failed Dependency" */
+ ngx_null_string, /* "417 Expectation Failed" */
+ ngx_null_string, /* "418 I'm a teapot" */
+ ngx_null_string, /* "419 unused" */
+ ngx_null_string, /* "420 unused" */
+ ngx_null_string, /* "421 unused" */
+ ngx_null_string, /* "422 Unprocessable Entity" */
+ ngx_null_string, /* "423 Locked" */
+ ngx_null_string, /* "424 Failed Dependency" */
+ ngx_null_string, /* "425 No code" */
+ ngx_null_string, /* "426 Upgrade Required" */
+ ngx_null_string, /* "427 unused" */
+ ngx_null_string, /* "428 Precondition Required" */
+ ngx_string("429 Too Many Requests"),
+ ngx_null_string, /* "430 unused" */
+ ngx_null_string, /* "431 Request Header Fields Too Large" */
-#define NGX_HTTP_LAST_4XX 417
+#define NGX_HTTP_LAST_4XX 432
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)
ngx_string("500 Internal Server Error"),
diff -Bbu -ur nginx-1.6.0/src/http/ngx_http_request.h nginx-1.6.0-retry-after-pached/src/http/ngx_http_request.h
--- nginx-1.6.0/src/http/ngx_http_request.h 2014-04-24 21:52:26.000000000 +0900
+++ nginx-1.6.0-retry-after-pached/src/http/ngx_http_request.h 2014-05-13 23:25:25.000000000 +0900
@@ -95,6 +95,7 @@
#define NGX_HTTP_UNSUPPORTED_MEDIA_TYPE 415
#define NGX_HTTP_RANGE_NOT_SATISFIABLE 416
+#define NGX_HTTP_TOO_MANY_REQUESTS 429
/* Our own HTTP codes */
diff -Bbu -ur nginx-1.6.0/src/http/ngx_http_special_response.c nginx-1.6.0-retry-after-pached/src/http/ngx_http_special_response.c
--- nginx-1.6.0/src/http/ngx_http_special_response.c 2014-04-24 21:52:26.000000000 +0900
+++ nginx-1.6.0-retry-after-pached/src/http/ngx_http_special_response.c 2014-05-13 23:25:25.000000000 +0900
@@ -334,8 +334,23 @@
ngx_string(ngx_http_error_414_page),
ngx_string(ngx_http_error_415_page),
ngx_string(ngx_http_error_416_page),
+ ngx_null_string, /* 417 */
+ ngx_null_string, /* 418 */
+ ngx_null_string, /* 419 */
+ ngx_null_string, /* 429 */
+ ngx_null_string, /* 421 */
+ ngx_null_string, /* 422 */
+ ngx_null_string, /* 423 */
+ ngx_null_string, /* 424 */
+ ngx_null_string, /* 425 */
+ ngx_null_string, /* 426 */
+ ngx_null_string, /* 427 */
+ ngx_null_string, /* 428 */
+ ngx_null_string, /* 429 */
+ ngx_null_string, /* 430 */
+ ngx_null_string, /* 431 */
-#define NGX_HTTP_LAST_4XX 417
+#define NGX_HTTP_LAST_4XX 432
#define NGX_HTTP_OFF_5XX (NGX_HTTP_LAST_4XX - 400 + NGX_HTTP_OFF_4XX)
ngx_string(ngx_http_error_494_page), /* 494, request header too large */