@@ -110,15 +110,14 @@ static int append_view_option(php_couchbase_res *res,
110
110
111
111
112
112
static void extract_view_options (php_couchbase_res * couchbase_res ,
113
- zval * options ,
113
+ zval * options , char * * payload ,
114
114
smart_str * uri TSRMLS_DC )
115
115
{
116
116
smart_str_appendc (uri , '?' );
117
117
118
- for (
119
- zend_hash_internal_pointer_reset (Z_ARRVAL_P (options ));
120
- zend_hash_has_more_elements (Z_ARRVAL_P (options )) == SUCCESS ;
121
- zend_hash_move_forward (Z_ARRVAL_P (options ))) {
118
+ for (zend_hash_internal_pointer_reset (Z_ARRVAL_P (options ));
119
+ zend_hash_has_more_elements (Z_ARRVAL_P (options )) == SUCCESS ;
120
+ zend_hash_move_forward (Z_ARRVAL_P (options ))) {
122
121
123
122
char * key ;
124
123
uint klen ;
@@ -137,17 +136,44 @@ static void extract_view_options(php_couchbase_res *couchbase_res,
137
136
continue ;
138
137
}
139
138
140
- if (FAILURE ==
141
- zend_hash_get_current_data (
142
- Z_ARRVAL_P (options ), (void * * )& ppzval )) {
139
+ if (zend_hash_get_current_data (Z_ARRVAL_P (options ), (void * * )& ppzval ) == FAILURE ) {
143
140
144
141
php_error_docref (NULL TSRMLS_CC , E_ERROR ,
145
142
"Couldn't get value for %*s" , klen , key );
146
143
continue ;
147
144
}
148
145
149
- /* Yes! The length *includes* the NUL byte */
150
- append_view_option (couchbase_res , uri , key , klen - 1 , * ppzval TSRMLS_CC );
146
+ if (strcasecmp ("keys" , key ) == 0 ) {
147
+ smart_str buf = {0 };
148
+ char * body ;
149
+ lcb_size_t nbody ;
150
+
151
+ pcbc_json_encode (& buf , * ppzval TSRMLS_CC );
152
+ if (buf .len == 0 ) {
153
+ php_error_docref (NULL TSRMLS_CC , E_ERROR ,
154
+ "Failed to encode key array" );
155
+ return ;
156
+ }
157
+
158
+ nbody = buf .len + 15 ;
159
+ body = calloc (nbody , 1 );
160
+
161
+ if (body == NULL ) {
162
+ php_error_docref (NULL TSRMLS_CC , E_ERROR ,
163
+ "Failed to allocate memory" );
164
+ smart_str_free (& buf );
165
+ return ;
166
+ }
167
+
168
+ sprintf (body , "{ \"keys\" : " );
169
+ strncat (body , buf .c , buf .len );
170
+ strcat (body , " }" );
171
+ * payload = body ;
172
+ smart_str_free (& buf );
173
+ } else {
174
+ /* Yes! The length *includes* the NUL byte */
175
+ append_view_option (couchbase_res , uri , key , klen - 1 , * ppzval TSRMLS_CC );
176
+ }
151
177
}
152
178
153
179
/* trim the last '&' from the uri */
@@ -259,6 +285,7 @@ void php_couchbase_view_impl(INTERNAL_FUNCTION_PARAMETERS, int oo, int uri_only)
259
285
lcb_http_cmd_t cmd = {0 };
260
286
lcb_timer_t timer ;
261
287
struct tc_cookie tcc ;
288
+ char * body = NULL ;
262
289
long view_timeout = INI_INT (PCBC_INIENT_VIEW_TIMEOUT );
263
290
264
291
int argflags = oo ? PHP_COUCHBASE_ARG_F_OO : PHP_COUCHBASE_ARG_F_FUNCTIONAL ;
@@ -274,7 +301,12 @@ void php_couchbase_view_impl(INTERNAL_FUNCTION_PARAMETERS, int oo, int uri_only)
274
301
view_name , view_name_len );
275
302
276
303
if (options && memchr (uri .c , '?' , uri .len ) == NULL ) {
277
- extract_view_options (couchbase_res , options , & uri TSRMLS_CC );
304
+ extract_view_options (couchbase_res , options , & body , & uri TSRMLS_CC );
305
+ }
306
+
307
+ /* Strip off the '?' if it is at the end */
308
+ if (uri .len > 0 && uri .c [uri .len ] == '\0' && uri .c [uri .len - 1 ] == '?' ) {
309
+ -- uri .len ;
278
310
}
279
311
280
312
if (uri_only ) {
@@ -289,18 +321,24 @@ void php_couchbase_view_impl(INTERNAL_FUNCTION_PARAMETERS, int oo, int uri_only)
289
321
}
290
322
291
323
ctx .res = couchbase_res ;
292
-
293
324
memset (& cmd , 0 , sizeof (cmd ));
294
325
cmd .v .v0 .path = uri .c ;
295
326
cmd .v .v0 .npath = uri .len ;
296
- cmd .v .v0 .method = LCB_HTTP_METHOD_GET ;
327
+ cmd .v .v0 .body = body ;
328
+ if (body ) {
329
+ cmd .v .v0 .nbody = strlen (body );
330
+ cmd .v .v0 .method = LCB_HTTP_METHOD_POST ;
331
+ } else {
332
+ cmd .v .v0 .method = LCB_HTTP_METHOD_GET ;
333
+ }
297
334
cmd .v .v0 .content_type = "application/json" ;
298
335
299
336
retval = lcb_make_http_request (couchbase_res -> handle ,
300
337
(const void * )& ctx ,
301
338
LCB_HTTP_TYPE_VIEW ,
302
339
& cmd , & tcc .request );
303
340
smart_str_free (& uri );
341
+ free (body );
304
342
305
343
if (retval != LCB_SUCCESS ) {
306
344
couchbase_res -> rc = retval ;
0 commit comments