forked from openresty/rds-json-nginx-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_http_rds_utils.h
202 lines (138 loc) · 4.97 KB
/
ngx_http_rds_utils.h
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright (C) agentzh
*/
#ifndef NGX_HTTP_RDS_UTILS_H
#define NGX_HTTP_RDS_UTILS_H
static ngx_inline ngx_int_t
ngx_http_rds_parse_header(ngx_http_request_t *r, ngx_buf_t *b,
ngx_http_rds_header_t *header)
{
ssize_t rest;
rest = sizeof(uint8_t) /* endian type */
+ sizeof(uint32_t) /* format version */
+ sizeof(uint8_t) /* result type */
+ sizeof(uint16_t) /* standard error code */
+ sizeof(uint16_t) /* driver-specific error code */
+ sizeof(uint16_t) /* driver-specific errstr len */
+ 0 /* driver-specific errstr data */
+ sizeof(uint64_t) /* affected rows */
+ sizeof(uint64_t) /* insert id */
+ sizeof(uint16_t) /* column count */
;
if (b->last - b->pos < rest) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds: header is incomplete in the buf");
return NGX_ERROR;
}
/* check endian type */
if (*(uint8_t *) b->pos !=
#if (NGX_HAVE_LITTLE_ENDIAN)
0
#else /* big endian */
1
#endif
)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds: endian type in the header differ");
return NGX_ERROR;
}
b->pos += sizeof(uint8_t);
/* check RDS format version number */
if (*(uint32_t *) b->pos != (uint32_t) resty_dbd_stream_version) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds: RDS format version differ");
return NGX_ERROR;
}
dd("RDS format version: %d", (int) *(uint32_t *) b->pos);
b->pos += sizeof(uint32_t);
/* check RDS result type */
if (*b->pos != 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds: RDS result type must be 0 for now");
return NGX_ERROR;
}
b->pos++;
/* save the standard error code */
header->std_errcode = *(uint16_t *) b->pos;
b->pos += sizeof(uint16_t);
/* save the driver-specific error code */
header->drv_errcode = *(uint16_t *) b->pos;
b->pos += sizeof(uint16_t);
/* save the error string length */
header->errstr.len = *(uint16_t *) b->pos;
b->pos += sizeof(uint16_t);
dd("errstr len: %d", (int) header->errstr.len);
/* check the rest data's size */
rest = header->errstr.len
+ sizeof(uint64_t) /* affected rows */
+ sizeof(uint64_t) /* insert id */
+ sizeof(uint16_t) /* column count */
;
if (b->last - b->pos < rest) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds: header is incomplete in the buf");
return NGX_ERROR;
}
/* save the error string data */
header->errstr.data = b->pos;
b->pos += header->errstr.len;
/* save affected rows */
header->affected_rows = *(uint64_t *) b->pos;
b->pos += sizeof(uint64_t);
/* save insert id */
header->insert_id = *(uint64_t *)b->pos;
b->pos += sizeof(uint64_t);
/* save column count */
header->col_count = *(uint16_t *) b->pos;
b->pos += sizeof(uint16_t);
dd("saved column count: %d", (int) header->col_count);
return NGX_OK;
}
static ngx_inline ngx_int_t
ngx_http_rds_parse_col(ngx_http_request_t *r, ngx_buf_t *b,
ngx_http_rds_column_t *col)
{
ssize_t rest;
rest = sizeof(uint16_t) /* std col type */
+ sizeof(uint16_t) /* driver col type */
+ sizeof(uint16_t) /* col name str len */
;
if (b->last - b->pos < rest) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds: column spec is incomplete in the buf");
return NGX_ERROR;
}
/* save standard column type */
col->std_type = *(uint16_t *) b->pos;
b->pos += sizeof(uint16_t);
/* save driver-specific column type */
col->drv_type = *(uint16_t *) b->pos;
b->pos += sizeof(uint16_t);
/* read column name string length */
col->name.len = *(uint16_t *) b->pos;
b->pos += sizeof(uint16_t);
if (col->name.len == 0) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds_json: column name empty");
return NGX_ERROR;
}
rest = col->name.len;
if (b->last - b->pos < rest) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"rds: column name string is incomplete in the buf");
return NGX_ERROR;
}
/* save the column name string data */
col->name.data = ngx_palloc(r->pool, col->name.len);
if (col->name.data == NULL) {
return NGX_ERROR;
}
ngx_memcpy(col->name.data, b->pos, col->name.len);
b->pos += col->name.len;
dd("saved column name \"%.*s\" (len %d, offset %d)",
(int) col->name.len, col->name.data,
(int) col->name.len, (int) (b->pos - b->start));
return NGX_OK;
}
#endif /* NGX_HTTP_RDS_UTILS_H */