This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
forked from couchbase/php-ext-couchbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
designdoc.c
344 lines (302 loc) · 9.22 KB
/
designdoc.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright 2012 Couchbase, Inc. |
+----------------------------------------------------------------------+
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| https://www.apache.org/licenses/LICENSE-2.0 |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| implied. See the License for the specific language governing |
| permissions and limitations under the License. |
+----------------------------------------------------------------------+
*/
#include "internal.h"
struct http_ctx {
lcb_error_t error;
lcb_http_status_t status;
char *payload;
};
static void http_callback(lcb_http_request_t request,
lcb_t instance,
const void *cookie,
lcb_error_t error,
const lcb_http_resp_t *resp)
{
struct http_ctx *ctx = (void *)cookie;
ctx->error = error;
ctx->payload = NULL;
if (resp->version != 0) {
/* @todo add an error code I may use */
ctx->error = LCB_NOT_SUPPORTED;
} else {
ctx->status = resp->v.v0.status;
if (resp->v.v0.nbytes != 0) {
ctx->payload = emalloc(resp->v.v0.nbytes + 1);
if (ctx->payload == NULL) {
ctx->error = LCB_CLIENT_ENOMEM;
} else {
memcpy(ctx->payload, resp->v.v0.bytes, resp->v.v0.nbytes);
ctx->payload[resp->v.v0.nbytes] = '\0';
}
}
}
}
PHP_COUCHBASE_LOCAL
void php_couchbase_delget_design_doc_impl(INTERNAL_FUNCTION_PARAMETERS, int oo, int rem)
{
char *path;
char *name = NULL;
long nname = 0;
php_couchbase_res *couchbase_res;
int argflags;
lcb_t instance;
lcb_error_t rc;
struct http_ctx ctx;
lcb_http_cmd_t cmd;
lcb_http_complete_callback old;
int len;
if (oo) {
argflags = PHP_COUCHBASE_ARG_F_OO;
} else {
argflags = PHP_COUCHBASE_ARG_F_FUNCTIONAL;
}
PHP_COUCHBASE_GET_PARAMS(couchbase_res, argflags, "s", &name, &nname);
if (!nname) {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_illegal_key_exception,
"You have to specify the name of the design doc");
return;
}
memset(&ctx, 0, sizeof(ctx));
memset(&cmd, 0, sizeof(cmd));
instance = couchbase_res->handle;
path = ecalloc(1, 80 + nname);
len = sprintf(path, "/_design/%*s", (int)nname, name);
cmd.v.v0.path = path;
cmd.v.v0.npath = len;
if (rem) {
cmd.v.v0.method = LCB_HTTP_METHOD_DELETE;
} else {
cmd.v.v0.method = LCB_HTTP_METHOD_GET;
}
cmd.v.v0.content_type = "application/json";
old = lcb_set_http_complete_callback(instance, http_callback);
lcb_behavior_set_syncmode(instance, LCB_SYNCHRONOUS);
rc = lcb_make_http_request(instance, &ctx, LCB_HTTP_TYPE_VIEW, &cmd, NULL);
lcb_behavior_set_syncmode(instance, LCB_ASYNCHRONOUS);
old = lcb_set_http_complete_callback(instance, old);
efree(path);
if (rc == LCB_SUCCESS) {
rc = ctx.error;
}
couchbase_res->rc = rc;
if (rc != LCB_SUCCESS) {
/* An error occured occurred on libcouchbase level */
efree(ctx.payload);
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_lcb_exception,
"Failed to retrieve design doc: %s",
lcb_strerror(instance, rc));
return;
}
switch (ctx.status) {
case LCB_HTTP_STATUS_OK:
if (rem) {
efree(ctx.payload);
RETURN_TRUE;
} else {
RETURN_STRING(ctx.payload, 0);
}
/* not reached */
case LCB_HTTP_STATUS_NOT_FOUND:
efree(ctx.payload);
RETURN_FALSE;
case LCB_HTTP_STATUS_UNAUTHORIZED:
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_auth_exception, "Incorrect credentials");
break;
default:
if (ctx.payload == NULL) {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_server_exception,
"{\"errors\":{\"http response\": %d }}",
(int)ctx.status);
} else {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_server_exception,
ctx.payload);
}
}
efree(ctx.payload);
}
PHP_COUCHBASE_LOCAL
void php_couchbase_set_design_doc_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
{
char *path;
char *name = NULL;
long nname = 0;
char *doc = NULL;
long ndoc = 0;
php_couchbase_res *couchbase_res;
int argflags;
lcb_t instance;
lcb_error_t rc;
struct http_ctx ctx;
lcb_http_cmd_t cmd;
lcb_http_complete_callback old;
int len;
if (oo) {
argflags = PHP_COUCHBASE_ARG_F_OO;
} else {
argflags = PHP_COUCHBASE_ARG_F_FUNCTIONAL;
}
PHP_COUCHBASE_GET_PARAMS(couchbase_res, argflags, "ss", &name, &nname,
&doc, &ndoc);
if (!nname) {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_illegal_key_exception,
"You have to specify the name of the design doc");
return;
}
if (!doc) {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_exception,
"You can't store an empty document");
return;
}
memset(&ctx, 0, sizeof(ctx));
memset(&cmd, 0, sizeof(cmd));
instance = couchbase_res->handle;
path = ecalloc(1, 80 + nname);
len = sprintf(path, "/_design/%*s", (int)nname, name);
cmd.v.v0.path = path;
cmd.v.v0.npath = len;
cmd.v.v0.body = doc;
cmd.v.v0.nbody = ndoc;
cmd.v.v0.method = LCB_HTTP_METHOD_PUT;
cmd.v.v0.content_type = "application/json";
old = lcb_set_http_complete_callback(instance, http_callback);
lcb_behavior_set_syncmode(instance, LCB_SYNCHRONOUS);
rc = lcb_make_http_request(instance, &ctx, LCB_HTTP_TYPE_VIEW, &cmd, NULL);
lcb_behavior_set_syncmode(instance, LCB_ASYNCHRONOUS);
old = lcb_set_http_complete_callback(instance, old);
efree(path);
if (rc == LCB_SUCCESS) {
rc = ctx.error;
}
couchbase_res->rc = rc;
if (rc != LCB_SUCCESS) {
/* An error occured occurred on libcouchbase level */
efree(ctx.payload);
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_lcb_exception,
"Failed to store design doc: %s",
lcb_strerror(instance, rc));
return;
}
switch (ctx.status) {
case LCB_HTTP_STATUS_OK:
case LCB_HTTP_STATUS_CREATED:
case LCB_HTTP_STATUS_ACCEPTED:
efree(ctx.payload);
RETURN_TRUE;
case LCB_HTTP_STATUS_UNAUTHORIZED:
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_auth_exception, "Incorrect credentials");
break;
default:
if (ctx.payload == NULL) {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_server_exception,
"{\"errors\":{\"http response\": %d }}",
(int)ctx.status);
} else {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_server_exception,
ctx.payload);
}
}
efree(ctx.payload);
}
PHP_COUCHBASE_LOCAL
void php_couchbase_list_design_docs_impl(INTERNAL_FUNCTION_PARAMETERS, int oo)
{
php_couchbase_res *couchbase_res;
int argflags;
lcb_t instance;
lcb_error_t rc;
struct http_ctx ctx;
lcb_http_cmd_t cmd;
lcb_http_complete_callback old;
int len;
char *path;
if (oo) {
argflags = PHP_COUCHBASE_ARG_F_OO;
} else {
argflags = PHP_COUCHBASE_ARG_F_FUNCTIONAL;
}
PHP_COUCHBASE_GET_PARAMS(couchbase_res, argflags, "");
instance = couchbase_res->handle;
memset(&ctx, 0, sizeof(ctx));
memset(&cmd, 0, sizeof(cmd));
path = ecalloc(strlen(couchbase_res->bucket) + 80, 1);
len = sprintf(path, "/pools/default/buckets/%s/ddocs",
couchbase_res->bucket);
cmd.v.v0.path = path;
cmd.v.v0.npath = len;
cmd.v.v0.method = LCB_HTTP_METHOD_GET;
cmd.v.v0.content_type = "application/json";
old = lcb_set_http_complete_callback(instance, http_callback);
lcb_behavior_set_syncmode(instance, LCB_SYNCHRONOUS);
rc = lcb_make_http_request(instance, &ctx, LCB_HTTP_TYPE_MANAGEMENT,
&cmd, NULL);
lcb_behavior_set_syncmode(instance, LCB_ASYNCHRONOUS);
old = lcb_set_http_complete_callback(instance, old);
efree(path);
if (rc == LCB_SUCCESS) {
rc = ctx.error;
}
couchbase_res->rc = rc;
if (rc != LCB_SUCCESS) {
/* An error occured occurred on libcouchbase level */
efree(ctx.payload);
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_lcb_exception,
"Failed to retrieve design doc: %s",
lcb_strerror(instance, rc));
return;
}
switch (ctx.status) {
case LCB_HTTP_STATUS_OK:
RETURN_STRING(ctx.payload, 0);
case LCB_HTTP_STATUS_UNAUTHORIZED:
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_auth_exception, "Incorrect credentials");
break;
default:
if (ctx.payload == NULL) {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_server_exception,
"{\"errors\":{\"http response\": %d }}",
(int)ctx.status);
} else {
couchbase_report_error(INTERNAL_FUNCTION_PARAM_PASSTHRU, oo,
cb_server_exception,
ctx.payload);
}
}
efree(ctx.payload);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet expandtab sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/