-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathngx_http_uuid4.c
153 lines (119 loc) · 4.19 KB
/
ngx_http_uuid4.c
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
/* (C) 2015 Cybozu. All rights reserved. */
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include "mt19937/mt64.h"
static char *ngx_http_uuid4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_command_t ngx_http_uuid4_commands[] = {
{ ngx_string("uuid4"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_uuid4,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_uuid4_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_uuid4_module = {
NGX_MODULE_V1,
&ngx_http_uuid4_module_ctx, /* module context */
ngx_http_uuid4_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static int mt_initialized = 0;
static ngx_int_t
initialize_mt(ngx_http_request_t *r)
{
static const size_t SEED_LENGTH = 312;
unsigned long long seed[SEED_LENGTH];
size_t n;
FILE *f;
f = fopen("/dev/urandom", "r");
if (f == NULL) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to open /dev/urandom");
return NGX_ERROR;
}
n = fread(seed, sizeof(unsigned long long), SEED_LENGTH, f);
if (n < SEED_LENGTH) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"failed to read /dev/urandom");
fclose(f);
return NGX_ERROR;
}
fclose(f);
init_by_array64(seed, SEED_LENGTH);
return NGX_OK;
}
static ngx_int_t
ngx_http_uuid4_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v,
uintptr_t data)
{
static const size_t UUID_STR_LENGTH = 36;
if (!mt_initialized) {
if (initialize_mt(r) != NGX_OK)
return NGX_ERROR;
mt_initialized = 1;
}
uint64_t upper = (uint64_t)genrand64_int64();
uint64_t lower = (uint64_t)genrand64_int64();
upper &= ~((1ULL << 12) | (1ULL << 13) | (1ULL << 15));
upper |= (1ULL << 14);
lower &= ~(1ULL << 62);
lower |= (1ULL << 63);
v->len = UUID_STR_LENGTH;
v->data = ngx_palloc(r->pool, UUID_STR_LENGTH);
if (v->data == NULL) {
*v = ngx_http_variable_null_value;
return NGX_OK;
}
ngx_snprintf(v->data, UUID_STR_LENGTH, "%08uxL-%04uxL-%04uxL-%04uxL-%012uxL",
upper >> 32, (upper >> 16) & 0xFFFFULL, upper & 0xFFFFULL,
lower >> 48, lower & 0xFFFFFFFFFFFFULL);
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
return NGX_OK;
}
static char *
ngx_http_uuid4(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_str_t *value;
ngx_http_variable_t *v;
ngx_int_t index;
value = cf->args->elts;
if (value[1].data[0] != '$') {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid variable name \"%V\"", &value[1]);
return NGX_CONF_ERROR;
}
value[1].len--;
value[1].data++;
v = ngx_http_add_variable(cf, &value[1], NGX_HTTP_VAR_CHANGEABLE);
if (v == NULL) {
return NGX_CONF_ERROR;
}
index = ngx_http_get_variable_index(cf, &value[1]);
if (index == NGX_ERROR) {
return NGX_CONF_ERROR;
}
v->get_handler = ngx_http_uuid4_variable;
return NGX_CONF_OK;
}