Line data Source code
1 : #ifndef DDEBUG
2 : #define DDEBUG 0
3 : #endif
4 : #include "ddebug.h"
5 :
6 :
7 : #include <ndk.h>
8 : #include "ngx_http_set_random.h"
9 : #include <stdlib.h>
10 :
11 :
12 : ngx_int_t
13 0 : ngx_http_set_misc_set_random(ngx_http_request_t *r, ngx_str_t *res,
14 : ngx_http_variable_value_t *v)
15 : {
16 : ngx_http_variable_value_t *rand_from, *rand_to;
17 : ngx_int_t int_from, int_to, tmp, random;
18 :
19 0 : rand_from = v;
20 0 : rand_to = v + 1;
21 :
22 0 : int_from = ngx_atoi(rand_from->data, rand_from->len);
23 0 : if (int_from == NGX_ERROR) {
24 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
25 : "set_random: bad \"from\" argument: %v", rand_from);
26 0 : return NGX_ERROR;
27 : }
28 :
29 0 : int_to = ngx_atoi(rand_to->data, rand_to->len);
30 0 : if (int_to == NGX_ERROR) {
31 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
32 : "set_random: bad \"to\" argument: %v", rand_to);
33 0 : return NGX_ERROR;
34 : }
35 :
36 0 : if (int_from > int_to) {
37 0 : tmp = int_from;
38 0 : int_from = int_to;
39 0 : int_to = tmp;
40 : }
41 :
42 0 : random = rand() % (int_to - int_from + 1) + int_from;
43 :
44 0 : res->data = ngx_palloc(r->pool, NGX_INT_T_LEN);
45 0 : if (res->data == NULL) {
46 0 : return NGX_ERROR;
47 : }
48 :
49 0 : res->len = ngx_sprintf(res->data, "%i", random) - res->data;
50 :
51 : /* Set all required params */
52 0 : v->valid = 1;
53 0 : v->no_cacheable = 0;
54 0 : v->not_found = 0;
55 :
56 0 : return NGX_OK;
57 : }
|