Line data Source code
1 :
2 : /*
3 : * Copyright (C) agentzh
4 : */
5 :
6 :
7 : #ifndef NGX_HTTP_RDS_UTILS_H
8 : #define NGX_HTTP_RDS_UTILS_H
9 :
10 :
11 : static ngx_inline ngx_int_t
12 0 : ngx_http_rds_parse_header(ngx_http_request_t *r, ngx_buf_t *b,
13 : ngx_http_rds_header_t *header)
14 : {
15 : ssize_t rest;
16 :
17 0 : rest = sizeof(uint8_t) /* endian type */
18 : + sizeof(uint32_t) /* format version */
19 : + sizeof(uint8_t) /* result type */
20 :
21 : + sizeof(uint16_t) /* standard error code */
22 : + sizeof(uint16_t) /* driver-specific error code */
23 :
24 : + sizeof(uint16_t) /* driver-specific errstr len */
25 : + 0 /* driver-specific errstr data */
26 : + sizeof(uint64_t) /* affected rows */
27 : + sizeof(uint64_t) /* insert id */
28 : + sizeof(uint16_t) /* column count */
29 : ;
30 :
31 0 : if (b->last - b->pos < rest) {
32 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
33 : "rds: header is incomplete in the buf");
34 0 : return NGX_ERROR;
35 : }
36 :
37 : /* check endian type */
38 :
39 0 : if (*(uint8_t *) b->pos !=
40 : #if (NGX_HAVE_LITTLE_ENDIAN)
41 : 0
42 : #else /* big endian */
43 : 1
44 : #endif
45 : )
46 : {
47 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
48 : "rds: endian type in the header differ");
49 0 : return NGX_ERROR;
50 : }
51 :
52 0 : b->pos += sizeof(uint8_t);
53 :
54 : /* check RDS format version number */
55 :
56 0 : if (*(uint32_t *) b->pos != (uint32_t) resty_dbd_stream_version) {
57 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
58 : "rds: RDS format version differ");
59 0 : return NGX_ERROR;
60 : }
61 :
62 : dd("RDS format version: %d", (int) *(uint32_t *) b->pos);
63 :
64 0 : b->pos += sizeof(uint32_t);
65 :
66 : /* check RDS result type */
67 :
68 0 : if (*b->pos != 0) {
69 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
70 : "rds: RDS result type must be 0 for now");
71 0 : return NGX_ERROR;
72 : }
73 :
74 0 : b->pos++;
75 :
76 : /* save the standard error code */
77 :
78 0 : header->std_errcode = *(uint16_t *) b->pos;
79 :
80 0 : b->pos += sizeof(uint16_t);
81 :
82 : /* save the driver-specific error code */
83 :
84 0 : header->drv_errcode = *(uint16_t *) b->pos;
85 :
86 0 : b->pos += sizeof(uint16_t);
87 :
88 : /* save the error string length */
89 :
90 0 : header->errstr.len = *(uint16_t *) b->pos;
91 :
92 0 : b->pos += sizeof(uint16_t);
93 :
94 : dd("errstr len: %d", (int) header->errstr.len);
95 :
96 : /* check the rest data's size */
97 :
98 0 : rest = header->errstr.len
99 : + sizeof(uint64_t) /* affected rows */
100 : + sizeof(uint64_t) /* insert id */
101 0 : + sizeof(uint16_t) /* column count */
102 : ;
103 :
104 0 : if (b->last - b->pos < rest) {
105 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
106 : "rds: header is incomplete in the buf");
107 0 : return NGX_ERROR;
108 : }
109 :
110 : /* save the error string data */
111 :
112 0 : header->errstr.data = b->pos;
113 :
114 0 : b->pos += header->errstr.len;
115 :
116 : /* save affected rows */
117 :
118 0 : header->affected_rows = *(uint64_t *) b->pos;
119 :
120 0 : b->pos += sizeof(uint64_t);
121 :
122 : /* save insert id */
123 :
124 0 : header->insert_id = *(uint64_t *)b->pos;
125 :
126 0 : b->pos += sizeof(uint64_t);
127 :
128 : /* save column count */
129 :
130 0 : header->col_count = *(uint16_t *) b->pos;
131 :
132 0 : b->pos += sizeof(uint16_t);
133 :
134 : dd("saved column count: %d", (int) header->col_count);
135 :
136 0 : return NGX_OK;
137 : }
138 :
139 :
140 : static ngx_inline ngx_int_t
141 0 : ngx_http_rds_parse_col(ngx_http_request_t *r, ngx_buf_t *b,
142 : ngx_http_rds_column_t *col)
143 : {
144 : ssize_t rest;
145 :
146 0 : rest = sizeof(uint16_t) /* std col type */
147 : + sizeof(uint16_t) /* driver col type */
148 : + sizeof(uint16_t) /* col name str len */
149 : ;
150 :
151 0 : if (b->last - b->pos < rest) {
152 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
153 : "rds: column spec is incomplete in the buf");
154 0 : return NGX_ERROR;
155 : }
156 :
157 : /* save standard column type */
158 0 : col->std_type = *(uint16_t *) b->pos;
159 0 : b->pos += sizeof(uint16_t);
160 :
161 : /* save driver-specific column type */
162 0 : col->drv_type = *(uint16_t *) b->pos;
163 0 : b->pos += sizeof(uint16_t);
164 :
165 : /* read column name string length */
166 :
167 0 : col->name.len = *(uint16_t *) b->pos;
168 0 : b->pos += sizeof(uint16_t);
169 :
170 0 : if (col->name.len == 0) {
171 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
172 : "rds_json: column name empty");
173 0 : return NGX_ERROR;
174 : }
175 :
176 0 : rest = col->name.len;
177 :
178 0 : if (b->last - b->pos < rest) {
179 0 : ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
180 : "rds: column name string is incomplete in the buf");
181 0 : return NGX_ERROR;
182 : }
183 :
184 : /* save the column name string data */
185 :
186 0 : col->name.data = ngx_palloc(r->pool, col->name.len);
187 0 : if (col->name.data == NULL) {
188 0 : return NGX_ERROR;
189 : }
190 :
191 0 : ngx_memcpy(col->name.data, b->pos, col->name.len);
192 0 : b->pos += col->name.len;
193 :
194 : dd("saved column name \"%.*s\" (len %d, offset %d)",
195 : (int) col->name.len, col->name.data,
196 : (int) col->name.len, (int) (b->pos - b->start));
197 :
198 0 : return NGX_OK;
199 : }
200 :
201 :
202 : #endif /* NGX_HTTP_RDS_UTILS_H */
|