3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 #include <ext/spl/spl_exceptions.h>
22 #include <zend_exceptions.h>
24 #include <grpc/support/alloc.h>
26 #include "call_credentials.h"
27 #include "completion_queue.h"
30 #include "byte_buffer.h"
32 zend_class_entry *grpc_ce_call;
33 PHP_GRPC_DECLARE_OBJECT_HANDLER(call_ce_handlers)
35 /* Frees and destroys an instance of wrapped_grpc_call */
36 PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call)
37 if (p->owned && p->wrapped != NULL) {
38 grpc_call_unref(p->wrapped);
40 PHP_GRPC_FREE_WRAPPED_FUNC_END()
42 /* Initializes an instance of wrapped_grpc_call to be associated with an
43 * object of a class specified by class_type */
44 php_grpc_zend_object create_wrapped_grpc_call(zend_class_entry *class_type
46 PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_call);
47 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
48 object_properties_init(&intern->std, class_type);
49 PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_call, call_ce_handlers);
52 /* Creates and returns a PHP array object with the data in a
53 * grpc_metadata_array. Returns NULL on failure */
54 zval *grpc_parse_metadata_array(grpc_metadata_array
55 *metadata_array TSRMLS_DC) {
56 int count = metadata_array->count;
57 grpc_metadata *elements = metadata_array->metadata;
59 PHP_GRPC_MAKE_STD_ZVAL(array);
62 HashTable *array_hash;
69 array_hash = Z_ARRVAL_P(array);
71 for (i = 0; i < count; i++) {
73 key_len = GRPC_SLICE_LENGTH(elem->key);
74 str_key = ecalloc(key_len + 1, sizeof(char));
75 memcpy(str_key, GRPC_SLICE_START_PTR(elem->key), key_len);
76 str_val = ecalloc(GRPC_SLICE_LENGTH(elem->value) + 1, sizeof(char));
77 memcpy(str_val, GRPC_SLICE_START_PTR(elem->value),
78 GRPC_SLICE_LENGTH(elem->value));
79 if (php_grpc_zend_hash_find(array_hash, str_key, key_len, (void **)&data)
81 if (Z_TYPE_P(data) != IS_ARRAY) {
82 zend_throw_exception(zend_exception_get_default(TSRMLS_C),
83 "Metadata hash somehow contains wrong types.",
87 PHP_GRPC_FREE_STD_ZVAL(array);
90 php_grpc_add_next_index_stringl(data, str_val,
91 GRPC_SLICE_LENGTH(elem->value),
94 PHP_GRPC_MAKE_STD_ZVAL(inner_array);
95 array_init(inner_array);
96 php_grpc_add_next_index_stringl(inner_array, str_val,
97 GRPC_SLICE_LENGTH(elem->value), false);
98 add_assoc_zval(array, str_key, inner_array);
99 PHP_GRPC_FREE_STD_ZVAL(inner_array);
102 #if PHP_MAJOR_VERSION >= 7
109 /* Populates a grpc_metadata_array with the data in a PHP array object.
110 Returns true on success and false on failure */
111 bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
112 HashTable *array_hash;
113 HashTable *inner_array_hash;
116 grpc_metadata_array_init(metadata);
118 metadata->metadata = NULL;
119 if (Z_TYPE_P(array) != IS_ARRAY) {
122 array_hash = Z_ARRVAL_P(array);
126 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type,
128 if (key_type != HASH_KEY_IS_STRING || key == NULL) {
131 if (Z_TYPE_P(inner_array) != IS_ARRAY) {
134 inner_array_hash = Z_ARRVAL_P(inner_array);
135 metadata->capacity += zend_hash_num_elements(inner_array_hash);
136 PHP_GRPC_HASH_FOREACH_END()
138 metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
142 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key1, key_type1,
144 if (key_type1 != HASH_KEY_IS_STRING) {
147 if (!grpc_header_key_is_legal(grpc_slice_from_static_string(key1))) {
150 inner_array_hash = Z_ARRVAL_P(inner_array);
151 PHP_GRPC_HASH_FOREACH_VAL_START(inner_array_hash, value)
152 if (Z_TYPE_P(value) != IS_STRING) {
155 metadata->metadata[metadata->count].key =
156 grpc_slice_from_copied_string(key1);
157 metadata->metadata[metadata->count].value =
158 grpc_slice_from_copied_buffer(Z_STRVAL_P(value), Z_STRLEN_P(value));
159 metadata->count += 1;
160 PHP_GRPC_HASH_FOREACH_END()
161 PHP_GRPC_HASH_FOREACH_END()
165 void grpc_php_metadata_array_destroy_including_entries(
166 grpc_metadata_array* array) {
168 if (array->metadata) {
169 for (i = 0; i < array->count; i++) {
170 grpc_slice_unref(array->metadata[i].key);
171 grpc_slice_unref(array->metadata[i].value);
174 grpc_metadata_array_destroy(array);
177 /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
178 struct should be destroyed at the end of the object's lifecycle */
179 zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
181 PHP_GRPC_MAKE_STD_ZVAL(call_object);
182 object_init_ex(call_object, grpc_ce_call);
183 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
185 call->wrapped = wrapped;
191 * Constructs a new instance of the Call class.
192 * @param Channel $channel_obj The channel to associate the call with.
193 * Must not be closed.
194 * @param string $method The method to call
195 * @param Timeval $deadline_obj The deadline for completing the call
196 * @param string $host_override The host is set by user (optional)
198 PHP_METHOD(Call, __construct) {
201 php_grpc_int method_len;
203 char *host_override = NULL;
204 php_grpc_int host_override_len = 0;
205 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
208 /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */
209 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", &channel_obj,
210 grpc_ce_channel, &method, &method_len,
211 &deadline_obj, grpc_ce_timeval, &host_override,
212 &host_override_len) == FAILURE) {
213 zend_throw_exception(spl_ce_InvalidArgumentException,
214 "Call expects a Channel, a String, a Timeval and "
215 "an optional String", 1 TSRMLS_CC);
218 wrapped_grpc_channel *channel =
219 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, channel_obj);
220 if (channel->wrapper == NULL) {
221 zend_throw_exception(spl_ce_InvalidArgumentException,
222 "Call cannot be constructed from a closed Channel",
226 gpr_mu_lock(&channel->wrapper->mu);
227 if (channel->wrapper == NULL || channel->wrapper->wrapped == NULL) {
228 zend_throw_exception(spl_ce_InvalidArgumentException,
229 "Call cannot be constructed from a closed Channel",
231 gpr_mu_unlock(&channel->wrapper->mu);
234 add_property_zval(getThis(), "channel", channel_obj);
235 wrapped_grpc_timeval *deadline =
236 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, deadline_obj);
237 grpc_slice method_slice = grpc_slice_from_copied_string(method);
238 grpc_slice host_slice = host_override != NULL ?
239 grpc_slice_from_copied_string(host_override) : grpc_empty_slice();
241 grpc_channel_create_call(channel->wrapper->wrapped, NULL,
242 GRPC_PROPAGATE_DEFAULTS,
243 completion_queue, method_slice,
244 host_override != NULL ? &host_slice : NULL,
245 deadline->wrapped, NULL);
246 grpc_slice_unref(method_slice);
247 grpc_slice_unref(host_slice);
249 call->channel = channel;
250 gpr_mu_unlock(&channel->wrapper->mu);
254 * Start a batch of RPC actions.
255 * @param array $array Array of actions to take
256 * @return object Object with results of all actions
258 PHP_METHOD(Call, startBatch) {
260 PHP_GRPC_MAKE_STD_ZVAL(result);
262 php_grpc_ulong index;
268 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
271 // startBatch in gRPC PHP server doesn't have channel in it.
272 if (call->channel->wrapper == NULL ||
273 call->channel->wrapper->wrapped == NULL) {
274 zend_throw_exception(spl_ce_RuntimeException,
275 "startBatch Error. Channel is closed",
283 HashTable *array_hash;
284 HashTable *status_hash;
285 HashTable *message_hash;
287 grpc_metadata_array metadata;
288 grpc_metadata_array trailing_metadata;
289 grpc_metadata_array recv_metadata;
290 grpc_metadata_array recv_trailing_metadata;
291 grpc_status_code status;
292 grpc_slice recv_status_details = grpc_empty_slice();
293 grpc_slice send_status_details = grpc_empty_slice();
294 grpc_byte_buffer *message;
296 grpc_call_error error;
298 #if PHP_MAJOR_VERSION < 7
302 zend_string* zmessage = NULL;
303 #endif // PHP_MAJOR_VERSION < 7
305 grpc_metadata_array_init(&metadata);
306 grpc_metadata_array_init(&trailing_metadata);
307 grpc_metadata_array_init(&recv_metadata);
308 grpc_metadata_array_init(&recv_trailing_metadata);
309 memset(ops, 0, sizeof(ops));
312 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
314 zend_throw_exception(spl_ce_InvalidArgumentException,
315 "start_batch expects an array", 1 TSRMLS_CC);
319 array_hash = Z_ARRVAL_P(array);
323 PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index,
325 if (key_type != HASH_KEY_IS_LONG || key != NULL) {
326 zend_throw_exception(spl_ce_InvalidArgumentException,
327 "batch keys must be integers", 1 TSRMLS_CC);
331 ops[op_num].op = (grpc_op_type)index;
332 ops[op_num].flags = 0;
333 ops[op_num].reserved = NULL;
336 case GRPC_OP_SEND_INITIAL_METADATA:
337 if (!create_metadata_array(value, &metadata)) {
338 zend_throw_exception(spl_ce_InvalidArgumentException,
339 "Bad metadata value given", 1 TSRMLS_CC);
342 ops[op_num].data.send_initial_metadata.count = metadata.count;
343 ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
345 case GRPC_OP_SEND_MESSAGE:
346 if (Z_TYPE_P(value) != IS_ARRAY) {
347 zend_throw_exception(spl_ce_InvalidArgumentException,
348 "Expected an array for send message",
352 message_hash = Z_ARRVAL_P(value);
353 if (php_grpc_zend_hash_find(message_hash, "flags", sizeof("flags"),
354 (void **)&message_flags) == SUCCESS) {
355 if (Z_TYPE_P(message_flags) != IS_LONG) {
356 zend_throw_exception(spl_ce_InvalidArgumentException,
357 "Expected an int for message flags",
360 ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
362 if (php_grpc_zend_hash_find(message_hash, "message", sizeof("message"),
363 (void **)&message_value) != SUCCESS ||
364 Z_TYPE_P(message_value) != IS_STRING) {
365 zend_throw_exception(spl_ce_InvalidArgumentException,
366 "Expected a string for send message",
370 ops[op_num].data.send_message.send_message =
371 string_to_byte_buffer(Z_STRVAL_P(message_value),
372 Z_STRLEN_P(message_value));
374 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
376 case GRPC_OP_SEND_STATUS_FROM_SERVER:
377 status_hash = Z_ARRVAL_P(value);
378 if (php_grpc_zend_hash_find(status_hash, "metadata", sizeof("metadata"),
379 (void **)&inner_value) == SUCCESS) {
380 if (!create_metadata_array(inner_value, &trailing_metadata)) {
381 zend_throw_exception(spl_ce_InvalidArgumentException,
382 "Bad trailing metadata value given",
386 ops[op_num].data.send_status_from_server.trailing_metadata =
387 trailing_metadata.metadata;
388 ops[op_num].data.send_status_from_server.trailing_metadata_count =
389 trailing_metadata.count;
391 if (php_grpc_zend_hash_find(status_hash, "code", sizeof("code"),
392 (void**)&inner_value) == SUCCESS) {
393 if (Z_TYPE_P(inner_value) != IS_LONG) {
394 zend_throw_exception(spl_ce_InvalidArgumentException,
395 "Status code must be an integer",
399 ops[op_num].data.send_status_from_server.status =
400 Z_LVAL_P(inner_value);
402 zend_throw_exception(spl_ce_InvalidArgumentException,
403 "Integer status code is required",
407 if (php_grpc_zend_hash_find(status_hash, "details", sizeof("details"),
408 (void**)&inner_value) == SUCCESS) {
409 if (Z_TYPE_P(inner_value) != IS_STRING) {
410 zend_throw_exception(spl_ce_InvalidArgumentException,
411 "Status details must be a string",
415 send_status_details = grpc_slice_from_copied_string(
416 Z_STRVAL_P(inner_value));
417 ops[op_num].data.send_status_from_server.status_details =
418 &send_status_details;
420 zend_throw_exception(spl_ce_InvalidArgumentException,
421 "String status details is required",
426 case GRPC_OP_RECV_INITIAL_METADATA:
427 ops[op_num].data.recv_initial_metadata.recv_initial_metadata =
430 case GRPC_OP_RECV_MESSAGE:
431 ops[op_num].data.recv_message.recv_message = &message;
433 case GRPC_OP_RECV_STATUS_ON_CLIENT:
434 ops[op_num].data.recv_status_on_client.trailing_metadata =
435 &recv_trailing_metadata;
436 ops[op_num].data.recv_status_on_client.status = &status;
437 ops[op_num].data.recv_status_on_client.status_details =
438 &recv_status_details;
440 case GRPC_OP_RECV_CLOSE_ON_SERVER:
441 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
444 zend_throw_exception(spl_ce_InvalidArgumentException,
445 "Unrecognized key in batch", 1 TSRMLS_CC);
449 PHP_GRPC_HASH_FOREACH_END()
451 error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
453 if (error != GRPC_CALL_OK) {
454 zend_throw_exception(spl_ce_LogicException,
455 "start_batch was called incorrectly",
456 (long)error TSRMLS_CC);
459 grpc_completion_queue_pluck(completion_queue, call->wrapped,
460 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
461 #if PHP_MAJOR_VERSION >= 7
464 for (int i = 0; i < op_num; i++) {
466 case GRPC_OP_SEND_INITIAL_METADATA:
467 add_property_bool(result, "send_metadata", true);
469 case GRPC_OP_SEND_MESSAGE:
470 add_property_bool(result, "send_message", true);
472 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
473 add_property_bool(result, "send_close", true);
475 case GRPC_OP_SEND_STATUS_FROM_SERVER:
476 add_property_bool(result, "send_status", true);
478 case GRPC_OP_RECV_INITIAL_METADATA:
479 #if PHP_MAJOR_VERSION < 7
480 array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC);
481 add_property_zval(result, "metadata", array);
483 recv_md = grpc_parse_metadata_array(&recv_metadata);
484 add_property_zval(result, "metadata", recv_md);
485 zval_ptr_dtor(recv_md);
486 PHP_GRPC_FREE_STD_ZVAL(recv_md);
488 PHP_GRPC_DELREF(array);
490 case GRPC_OP_RECV_MESSAGE:
491 #if PHP_MAJOR_VERSION < 7
492 byte_buffer_to_string(message, &message_str, &message_len);
494 zmessage = byte_buffer_to_zend_string(message);
495 #endif // PHP_MAJOR_VERSION < 7
497 #if PHP_MAJOR_VERSION < 7
498 if (message_str == NULL) {
500 if (zmessage == NULL) {
501 #endif // PHP_MAJOR_VERSION < 7
502 add_property_null(result, "message");
504 #if PHP_MAJOR_VERSION < 7
505 php_grpc_add_property_stringl(result, "message", message_str,
509 ZVAL_NEW_STR(&zmessage_val, zmessage);
510 add_property_zval(result, "message", &zmessage_val);
511 zval_ptr_dtor(&zmessage_val);
512 #endif // PHP_MAJOR_VERSION < 7
515 case GRPC_OP_RECV_STATUS_ON_CLIENT:
516 PHP_GRPC_MAKE_STD_ZVAL(recv_status);
517 object_init(recv_status);
518 #if PHP_MAJOR_VERSION < 7
519 array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC);
520 add_property_zval(recv_status, "metadata", array);
522 recv_md = grpc_parse_metadata_array(&recv_trailing_metadata);
523 add_property_zval(recv_status, "metadata", recv_md);
524 zval_ptr_dtor(recv_md);
525 PHP_GRPC_FREE_STD_ZVAL(recv_md);
527 PHP_GRPC_DELREF(array);
528 add_property_long(recv_status, "code", status);
529 char *status_details_text = grpc_slice_to_c_string(recv_status_details);
530 php_grpc_add_property_string(recv_status, "details", status_details_text,
532 gpr_free(status_details_text);
533 add_property_zval(result, "status", recv_status);
534 #if PHP_MAJOR_VERSION >= 7
535 zval_ptr_dtor(recv_status);
537 PHP_GRPC_DELREF(recv_status);
538 PHP_GRPC_FREE_STD_ZVAL(recv_status);
540 case GRPC_OP_RECV_CLOSE_ON_SERVER:
541 add_property_bool(result, "cancelled", cancelled);
549 grpc_php_metadata_array_destroy_including_entries(&metadata);
550 grpc_php_metadata_array_destroy_including_entries(&trailing_metadata);
551 grpc_metadata_array_destroy(&recv_metadata);
552 grpc_metadata_array_destroy(&recv_trailing_metadata);
553 grpc_slice_unref(recv_status_details);
554 grpc_slice_unref(send_status_details);
555 for (int i = 0; i < op_num; i++) {
556 if (ops[i].op == GRPC_OP_SEND_MESSAGE) {
557 grpc_byte_buffer_destroy(ops[i].data.send_message.send_message);
559 if (ops[i].op == GRPC_OP_RECV_MESSAGE) {
560 grpc_byte_buffer_destroy(message);
561 #if PHP_MAJOR_VERSION < 7
562 PHP_GRPC_FREE_STD_ZVAL(message_str);
563 #endif // PHP_MAJOR_VERSION < 7
566 RETURN_DESTROY_ZVAL(result);
570 * Get the endpoint this call/stream is connected to
571 * @return string The URI of the endpoint
573 PHP_METHOD(Call, getPeer) {
574 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
576 char *peer = grpc_call_get_peer(call->wrapped);
577 PHP_GRPC_RETVAL_STRING(peer, 1);
582 * Cancel the call. This will cause the call to end with STATUS_CANCELLED
583 * if it has not already ended with another status.
586 PHP_METHOD(Call, cancel) {
587 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
589 grpc_call_cancel(call->wrapped, NULL);
593 * Set the CallCredentials for this call.
594 * @param CallCredentials $creds_obj The CallCredentials object
595 * @return int The error code
597 PHP_METHOD(Call, setCredentials) {
600 /* "O" == 1 Object */
601 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj,
602 grpc_ce_call_credentials) == FAILURE) {
603 zend_throw_exception(spl_ce_InvalidArgumentException,
604 "setCredentials expects 1 CallCredentials",
609 wrapped_grpc_call_credentials *creds =
610 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, creds_obj);
611 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
614 grpc_call_error error = GRPC_CALL_ERROR;
615 error = grpc_call_set_credentials(call->wrapped, creds->wrapped);
619 ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 3)
620 ZEND_ARG_INFO(0, channel)
621 ZEND_ARG_INFO(0, method)
622 ZEND_ARG_INFO(0, deadline)
623 ZEND_ARG_INFO(0, host_override)
626 ZEND_BEGIN_ARG_INFO_EX(arginfo_startBatch, 0, 0, 1)
627 ZEND_ARG_INFO(0, ops)
630 ZEND_BEGIN_ARG_INFO_EX(arginfo_getPeer, 0, 0, 0)
633 ZEND_BEGIN_ARG_INFO_EX(arginfo_cancel, 0, 0, 0)
636 ZEND_BEGIN_ARG_INFO_EX(arginfo_setCredentials, 0, 0, 1)
637 ZEND_ARG_INFO(0, credentials)
640 static zend_function_entry call_methods[] = {
641 PHP_ME(Call, __construct, arginfo_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
642 PHP_ME(Call, startBatch, arginfo_startBatch, ZEND_ACC_PUBLIC)
643 PHP_ME(Call, getPeer, arginfo_getPeer, ZEND_ACC_PUBLIC)
644 PHP_ME(Call, cancel, arginfo_cancel, ZEND_ACC_PUBLIC)
645 PHP_ME(Call, setCredentials, arginfo_setCredentials, ZEND_ACC_PUBLIC)
649 void grpc_init_call(TSRMLS_D) {
651 INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
652 ce.create_object = create_wrapped_grpc_call;
653 grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
654 PHP_GRPC_INIT_HANDLER(wrapped_grpc_call, call_ce_handlers);