void swoole_set_object(zval *object, void *ptr) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); zend_object_handle handle = Z_OBJ_HANDLE_P(object); #else int handle = (int) Z_OBJ_HANDLE(*object); #endif assert(handle < SWOOLE_OBJECT_MAX); if (handle >= swoole_objects.size) { uint32_t old_size = swoole_objects.size; uint32_t new_size = swoole_get_new_size(old_size, handle TSRMLS_CC); void *old_ptr = swoole_objects.array; void *new_ptr = NULL; new_ptr = realloc(old_ptr, sizeof(void*) * new_size); if (!new_ptr) { swoole_php_fatal_error(E_ERROR, "malloc(%d) failed.", (int )(new_size * sizeof(void *))); return; } bzero(new_ptr + (old_size * sizeof(void*)), (new_size - old_size) * sizeof(void*)); swoole_objects.array = new_ptr; swoole_objects.size = new_size; } swoole_objects.array[handle] = ptr; }
/* ****************************************************************************************************** Get the code of registered UDF module. * * @param aerospike_obj_p The C client's aerospike object. * @param error_p The C client's as_error to be set to the encountered error. * @param module_p The name of the UDF module to get from the * server. * @param module_len Length of UDF module name. * @param udf_code_p Code of the UDF to be populated by this * function. * @param language The Aerospike::UDF_TYPE_* constant. * @param options_p The optional policy. * * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x. ****************************************************************************************************** */ extern as_status aerospike_get_registered_udf_module_code(Aerospike_object* aerospike_obj_p, as_error* error_p, char* module_p, zval* udf_code_p, long language, zval* options_p) { uint32_t init_udf_file = 0; as_udf_file udf_file; as_policy_info info_policy; TSRMLS_FETCH_FROM_CTX(aerospike_obj_p->ts); set_policy(&aerospike_obj_p->as_ref_p->as_p->config, NULL, NULL, NULL, NULL, &info_policy, NULL, NULL, NULL, options_p, error_p TSRMLS_CC); if (AEROSPIKE_OK != (error_p->code)) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } as_udf_file_init(&udf_file); init_udf_file = 1; if (AEROSPIKE_OK != aerospike_udf_get(aerospike_obj_p->as_ref_p->as_p, error_p, &info_policy, module_p, language, &udf_file)) { DEBUG_PHP_EXT_ERROR("%s", error_p->message); goto exit; } ZVAL_STRINGL(udf_code_p, (char*) udf_file.content.bytes, udf_file.content.size, 1); exit: if (init_udf_file) { as_udf_file_destroy(&udf_file); } return error_p->code; }
static void mergeGlobal(zval * val, zval *zrequest, int type) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); zval *http_server; MAKE_STD_ZVAL(http_server); object_init_ex(http_server, swoole_http_server_class_entry_ptr); zval *http_request_ptr = zend_read_property(swoole_http_server_class_entry_ptr, http_server, ZEND_STRL("request"), 1 TSRMLS_CC); int http_request = Z_LVAL_P(http_request_ptr); switch (type) { case HTTP_GLOBAL_GET: ZEND_SET_SYMBOL(&EG(symbol_table), "_GET", val); break; case HTTP_GLOBAL_POST: ZEND_SET_SYMBOL(&EG(symbol_table), "_POST", val); break; case HTTP_GLOBAL_COOKIE: ZEND_SET_SYMBOL(&EG(symbol_table), "_COOKIE", val); break; } if(http_request & type) { zval *_request = zend_read_property(swoole_http_request_class_entry_ptr, zrequest, ZEND_STRL("request"), 1 TSRMLS_CC); zend_hash_copy(Z_ARRVAL_P(_request), Z_ARRVAL_P(val), NULL, NULL, sizeof(zval)); } }
static int php_swoole_event_onWrite(swReactor *reactor, swEvent *event) { zval *retval; zval **args[1]; php_reactor_fd *fd = event->socket->object; #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif if (!fd->cb_write) { return swReactor_onWrite(reactor, event); } args[0] = &fd->socket; if (sw_call_user_function_ex(EG(function_table), NULL, fd->cb_write, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE) { swoole_php_fatal_error(E_WARNING, "swoole_event: onWrite handler error"); SwooleG.main_reactor->del(SwooleG.main_reactor, event->fd); return SW_ERR; } if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR TSRMLS_CC); } if (retval != NULL) { sw_zval_ptr_dtor(&retval); } return SW_OK; }
/** * safe signal */ static void php_swoole_onSignal(int signo) { zval *retval; zval **args[1]; zval *callback = signal_callback[signo]; #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *zsigno; SW_MAKE_STD_ZVAL(zsigno); ZVAL_LONG(zsigno, signo); args[0] = &zsigno; if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "user_signal handler error"); } if (retval != NULL) { sw_zval_ptr_dtor(&retval); } sw_zval_ptr_dtor(&zsigno); }
static int php_swoole_event_onError(swReactor *reactor, swEvent *event) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif int error; socklen_t len = sizeof(error); if (getsockopt(event->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { swoole_php_fatal_error(E_WARNING, "swoole_event->onError[1]: getsockopt[sock=%d] failed. Error: %s[%d]", event->fd, strerror(errno), errno); } if (error != 0) { swoole_php_fatal_error(E_WARNING, "swoole_event->onError[1]: socket error. Error: %s [%d]", strerror(error), error); } efree(event->socket->object); event->socket->active = 0; SwooleG.main_reactor->del(SwooleG.main_reactor, event->fd); return SW_OK; }
int swoole_websocket_onHandshake(swListenPort *port, http_context *ctx) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif int fd = ctx->fd; int ret = websocket_handshake(port, ctx); if (ret == SW_ERR) { swServer_tcp_close(SwooleG.serv, fd, 1); } else { swoole_websocket_onOpen(ctx); } //free client data if (!ctx->end) { swoole_http_context_free(ctx TSRMLS_CC); } return SW_OK; }
static int http_request_on_body(php_http_parser *parser, const char *at, size_t length) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); http_client *client = parser->data; char *body = estrndup(at, length); if (client->request.post_form_urlencoded) { zval *post; MAKE_STD_ZVAL(post); array_init(post); zend_update_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("post"), post TSRMLS_CC); sapi_module.treat_data(PARSE_STRING, body, post TSRMLS_CC); mergeGlobal(post, client->zrequest, HTTP_GLOBAL_POST); } else { client->request.post_content = body; client->request.post_length = length; } return 0; }
static int http_request_on_body(php_http_parser *parser, const char *at, size_t length) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); http_client *client = parser->data; char *body = estrndup(at, length); if (client->request.post_form_urlencoded) { zval *post; MAKE_STD_ZVAL(post); array_init(post); zend_update_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("post"), post TSRMLS_CC); sapi_module.treat_data(PARSE_STRING, body, post TSRMLS_CC); ZEND_SET_SYMBOL(&EG(symbol_table), "_POST", post); zval *_request = zend_read_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("request"), 1 TSRMLS_CC); zend_hash_copy(Z_ARRVAL_P(_request), Z_ARRVAL_P(post), NULL, NULL, sizeof(zval)); } else { client->request.post_content = body; client->request.post_length = length; } return 0; }
int swoole_websocket_onHandshake(swoole_http_client *client) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif int fd = client->fd; int ret = websocket_handshake(client); if (ret == SW_ERR) { SwooleG.serv->factory.end(&SwooleG.serv->factory, fd); } else { swoole_websocket_onOpen(client); } //free client data if (!client->end) { swoole_http_request_free(client TSRMLS_CC); } return SW_OK; }
static void php_swoole_event_onDefer(void *_cb) { php_defer_callback *defer = _cb; #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *retval; if (sw_call_user_function_ex(EG(function_table), NULL, defer->callback, &retval, 0, NULL, 0, NULL TSRMLS_CC) == FAILURE) { swoole_php_fatal_error(E_WARNING, "swoole_event: defer handler error"); return; } if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR TSRMLS_CC); } if (retval != NULL) { sw_zval_ptr_dtor(&retval); } sw_zval_ptr_dtor(&defer->callback); efree(defer); }
static void php_swoole_onTimeout(swTimer *timer, swTimer_node *event) { swTimer_callback *callback = event->data; zval *retval = NULL; #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval **args[1]; int argc = 0; if (callback->data) { args[0] = &callback->data; argc = 1; } if (sw_call_user_function_ex(EG(function_table), NULL, callback->callback, &retval, argc, args, 0, NULL TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_timer: onTimeout handler error"); return; } if (retval) { sw_zval_ptr_dtor(&retval); } callback = event->data; if (callback) { if (callback->data) { sw_zval_ptr_dtor(&callback->data); } efree(callback); } }
static isc_callback _php_ibase_callback(ibase_event *event, /* {{{ */ unsigned short buffer_size, char *result_buf) { zval *res; /* this function is called asynchronously by the Interbase client library. */ TSRMLS_FETCH_FROM_CTX(event->thread_ctx); /** * The callback function is called when the event is first registered and when the event * is cancelled. I consider this is a bug. By clearing event->callback first and setting * it to -1 later, we make sure nothing happens if no event was actually posted. */ switch (event->state) { unsigned short i; unsigned long occurred_event[15]; zval return_value, args[2]; default: /* == DEAD */ break; case ACTIVE: /* copy the updated results into the result buffer */ memcpy(event->result_buffer, result_buf, buffer_size); res = zend_hash_index_find(&EG(regular_list), event->link_res_id); ZVAL_RES(&args[1], Z_RES_P(res)); /* find out which event occurred */ isc_event_counts(occurred_event, buffer_size, event->event_buffer, event->result_buffer); for (i = 0; i < event->event_count; ++i) { if (occurred_event[i]) { ZVAL_STRING(&args[0], event->events[i]); efree(event->events[i]); break; } } /* call the callback provided by the user */ if (SUCCESS != call_user_function(EG(function_table), NULL, &event->callback, &return_value, 2, args)) { _php_ibase_module_error("Error calling callback %s", Z_STRVAL(event->callback)); break; } if (Z_TYPE(return_value) == IS_FALSE) { event->state = DEAD; break; } case NEW: /* re-register the event */ if (isc_que_events(IB_STATUS, &event->link->handle, &event->event_id, buffer_size, event->event_buffer,(isc_callback)_php_ibase_callback, (void *)event)) { _php_ibase_error(); } event->state = ACTIVE; } return 0; }
static void http_client_free(http_client *client) { if (client->zrequest) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); http_request_free(client TSRMLS_CC); } efree(client); }
/* ****************************************************************************************************** Applies a UDF to a record at the Aerospike DB. * * @param aerospike_obj_p The C client's aerospike object. * @param as_key_p The C client's as_key that identifies the * record on which UDF will be applied. * @param error_p The C client's as_error to be set to the encountered error. * @param module_p The name of the UDF module registered * against the Aerospike DB. * @param function_p The name of the function to be applied to * the record. * @param args_pp An array of arguments for the UDF. * @param return_value_p It will contain result value of calling the * UDF. * @param options_p The optional policy. * * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x. ****************************************************************************************************** */ extern as_status aerospike_udf_apply(Aerospike_object* aerospike_obj_p, as_key* as_key_p, as_error* error_p, char* module_p, char* function_p, zval** args_pp, zval* return_value_p, zval* options_p) { as_arraylist args_list; as_arraylist* args_list_p = NULL; as_static_pool udf_pool = {0}; as_val* udf_result_p = NULL; foreach_callback_udata udf_result_callback_udata; uint32_t serializer_policy = -1; as_policy_apply apply_policy; TSRMLS_FETCH_FROM_CTX(aerospike_obj_p->ts); set_policy_udf_apply(&aerospike_obj_p->as_ref_p->as_p->config, &apply_policy, &serializer_policy, options_p, error_p TSRMLS_CC); if (AEROSPIKE_OK != (error_p->code)) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } if ((*args_pp)) { as_arraylist_inita(&args_list, zend_hash_num_elements(Z_ARRVAL_PP(args_pp))); args_list_p = &args_list; AS_LIST_PUT(NULL, args_pp, args_list_p, &udf_pool, serializer_policy, error_p TSRMLS_CC); } if (AEROSPIKE_OK != (aerospike_key_apply(aerospike_obj_p->as_ref_p->as_p, error_p, &apply_policy, as_key_p, module_p, function_p, (as_list *) args_list_p, &udf_result_p))) { DEBUG_PHP_EXT_DEBUG("%s", error_p->message); goto exit; } if (return_value_p) { udf_result_callback_udata.udata_p = return_value_p; udf_result_callback_udata.error_p = error_p; AS_DEFAULT_GET(NULL, udf_result_p, &udf_result_callback_udata); } exit: if (udf_result_p) { as_val_destroy(udf_result_p); } if (args_list_p) { as_arraylist_destroy(args_list_p); } /* clean up the as_* objects that were initialised */ aerospike_helper_free_static_pool(&udf_pool); return error_p->code; }
int swoole_websocket_onMessage(swEventData *req) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif int fd = req->info.fd; zval *zdata; SW_MAKE_STD_ZVAL(zdata); zdata = php_swoole_get_recv_data(zdata, req TSRMLS_CC); char *buf = Z_STRVAL_P(zdata); long finish = buf[0] ? 1 : 0; long opcode = buf[1] ? 1 : 0; zval *zframe; SW_MAKE_STD_ZVAL(zframe); object_init_ex(zframe, swoole_websocket_frame_class_entry_ptr); zend_update_property_long(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("fd"), fd TSRMLS_CC); zend_update_property_long(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("finish"), finish TSRMLS_CC); zend_update_property_long(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("opcode"), opcode TSRMLS_CC); zend_update_property_stringl(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("data"), buf + 2, (Z_STRLEN_P(zdata) - 2) TSRMLS_CC); swServer *serv = SwooleG.serv; zval *zserv = (zval *) serv->ptr2; zval **args[2]; args[0] = &zserv; args[1] = &zframe; zval *retval = NULL; if (sw_call_user_function_ex(EG(function_table), NULL, websocket_callbacks[WEBSOCKET_CALLBACK_onMessage], &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "onMessage handler error"); } if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR TSRMLS_CC); } if (retval) { sw_zval_ptr_dtor(&retval); } sw_zval_ptr_dtor(&zdata); sw_zval_ptr_dtor(&zframe); return SW_OK; }
static sw_inline void client_execute_callback(swClient *cli, enum client_callback_type type) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *callback = NULL; zval *retval = NULL; zval **args[1]; zval *zobject = cli->object; client_callback *cb = swoole_get_property(zobject, 0); char *callback_name; switch(type) { case SW_CLIENT_CALLBACK_onConnect: callback = cb->onConnect; callback_name = "onConnect"; break; case SW_CLIENT_CALLBACK_onError: callback = cb->onError; callback_name = "onError"; break; case SW_CLIENT_CALLBACK_onClose: callback = cb->onClose; callback_name = "onClose"; break; default: return; } if (callback == NULL || ZVAL_IS_NULL(callback)) { swoole_php_fatal_error(E_WARNING, "object have not %s callback.", callback_name); return; } args[0] = &zobject; if (sw_call_user_function_ex(EG(function_table), NULL, callback, &retval, 1, args, 0, NULL TSRMLS_CC) == FAILURE) { swoole_php_fatal_error(E_WARNING, "%s handler error.", callback_name); return; } if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR TSRMLS_CC); } if (retval) { sw_zval_ptr_dtor(&retval); } }
static void php_swoole_onTimerInterval(swTimer *timer, swTimer_node *event) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *retval = NULL; zval **args[2]; int argc = 1; zval *ztimer_id; swTimer_callback *cb = event->data; //server->addtimer if (cb == NULL && SwooleG.serv) { SwooleG.serv->onTimer(SwooleG.serv, event->interval); return; } if (cb->type == SW_TIMER_TICK) { SW_MAKE_STD_ZVAL(ztimer_id); ZVAL_LONG(ztimer_id, event->id); if (cb->data) { argc = 2; sw_zval_add_ref(&cb->data); args[1] = &cb->data; } } else { SW_MAKE_STD_ZVAL(ztimer_id); ZVAL_LONG(ztimer_id, event->interval); } args[0] = &ztimer_id; if (sw_call_user_function_ex(EG(function_table), NULL, cb->callback, &retval, argc, args, 0, NULL TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "swoole_timer: onTimerCallback handler error"); return; } if (retval != NULL) { sw_zval_ptr_dtor(&retval); } sw_zval_ptr_dtor(&ztimer_id); }
int swoole_websocket_onMessage(swEventData *req) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif int fd = req->info.fd; zval *zdata; SW_MAKE_STD_ZVAL(zdata); char frame_header[2]; php_swoole_get_recv_data(zdata, req, frame_header, 2); long finish = frame_header[0] ? 1 : 0; long opcode = frame_header[1]; zval *zframe; SW_MAKE_STD_ZVAL(zframe); object_init_ex(zframe, swoole_websocket_frame_class_entry_ptr); zend_update_property_long(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("fd"), fd TSRMLS_CC); zend_update_property_bool(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("finish"), finish TSRMLS_CC); zend_update_property_long(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("opcode"), opcode TSRMLS_CC); zend_update_property(swoole_websocket_frame_class_entry_ptr, zframe, ZEND_STRL("data"), zdata TSRMLS_CC); swServer *serv = SwooleG.serv; zval *zserv = (zval *) serv->ptr2; zval **args[2]; args[0] = &zserv; args[1] = &zframe; zval *retval = NULL; zval *zcallback = php_swoole_server_get_callback(serv, req->info.from_fd, SW_SERVER_CB_onMessage); if (sw_call_user_function_ex(EG(function_table), NULL, zcallback, &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "onMessage handler error"); } if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR TSRMLS_CC); } if (retval) { sw_zval_ptr_dtor(&retval); } sw_zval_ptr_dtor(&zdata); sw_zval_ptr_dtor(&zframe); return SW_OK; }
static int http_request_on_query_string(php_http_parser *parser, const char *at, size_t length) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); http_client *client = parser->data; char *query = estrndup(at, length); zval *get; MAKE_STD_ZVAL(get); array_init(get); zend_update_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("get"), get TSRMLS_CC); sapi_module.treat_data(PARSE_STRING, query, get TSRMLS_CC); return 0; }
void swoole_websocket_onOpen(swoole_http_client *client) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif int fd = client->fd; swConnection *conn = swWorker_get_connection(SwooleG.serv, fd); if (!conn) { swWarn("connection[%d] is closed.", fd); return; } conn->websocket_status = WEBSOCKET_STATUS_HANDSHAKE; swTrace("\n\n\n\nconn ws status:%d, fd=%d\n\n\n", conn->websocket_status, fd); if (websocket_callbacks[WEBSOCKET_CALLBACK_onOpen]) { swTrace("\n\n\n\nhandshake success\n\n\n"); zval **args[2]; swServer *serv = SwooleG.serv; zval *zserv = (zval *) serv->ptr2; zval *zrequest_object = client->request.zrequest_object; zval *retval = NULL; #ifdef __CYGWIN__ //TODO: memory error on cygwin. sw_zval_add_ref(&zrequest_object); #endif args[0] = &zserv; args[1] = &zrequest_object; if (sw_call_user_function_ex(EG(function_table), NULL, websocket_callbacks[WEBSOCKET_CALLBACK_onOpen], &retval, 2, args, 0, NULL TSRMLS_CC) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "onOpen handler error"); } if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR TSRMLS_CC); } if (retval) { sw_zval_ptr_dtor(&retval); } } }
static int websocket_handshake(swListenPort *port, http_context *ctx) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *header = ctx->request.zheader; HashTable *ht = Z_ARRVAL_P(header); zval *pData; if (sw_zend_hash_find(ht, ZEND_STRS("sec-websocket-key"), (void **) &pData) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "header no sec-websocket-key"); return SW_ERR; } convert_to_string(pData); swString_clear(swoole_http_buffer); swString_append_ptr(swoole_http_buffer, ZEND_STRL("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n")); int n; char sec_websocket_accept[128]; memcpy(sec_websocket_accept, Z_STRVAL_P(pData), Z_STRLEN_P(pData)); memcpy(sec_websocket_accept + Z_STRLEN_P(pData), SW_WEBSOCKET_GUID, sizeof(SW_WEBSOCKET_GUID) - 1); char sha1_str[20]; bzero(sha1_str, sizeof(sha1_str)); php_swoole_sha1(sec_websocket_accept, Z_STRLEN_P(pData) + sizeof(SW_WEBSOCKET_GUID) - 1, (unsigned char *) sha1_str); char encoded_str[50]; bzero(encoded_str, sizeof(encoded_str)); n = swBase64_encode((unsigned char *) sha1_str, sizeof(sha1_str), encoded_str); char _buf[128]; n = snprintf(_buf, sizeof(_buf), "Sec-WebSocket-Accept: %*s\r\n", n, encoded_str); swString_append_ptr(swoole_http_buffer, _buf, n); swString_append_ptr(swoole_http_buffer, ZEND_STRL("Sec-WebSocket-Version: "SW_WEBSOCKET_VERSION"\r\n")); if (port->websocket_subprotocol) { swString_append_ptr(swoole_http_buffer, ZEND_STRL("Sec-WebSocket-Protocol: ")); swString_append_ptr(swoole_http_buffer, port->websocket_subprotocol, port->websocket_subprotocol_length); swString_append_ptr(swoole_http_buffer, ZEND_STRL("\r\n")); } swString_append_ptr(swoole_http_buffer, ZEND_STRL("Server: "SW_WEBSOCKET_SERVER_SOFTWARE"\r\n\r\n")); swTrace("websocket header len:%ld\n%s \n", swoole_http_buffer->length, swoole_http_buffer->str); return swServer_tcp_send(SwooleG.serv, ctx->fd, swoole_http_buffer->str, swoole_http_buffer->length); }
static void http_client_coro_onConnect(swClient *cli) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *zobject = cli->object; http_client *http = swoole_get_object(zobject); if (!http->cli) { swoole_php_fatal_error(E_WARNING, "object is not instanceof swoole_http_client_coro."); return; } http_client_coro_send_http_request(zobject TSRMLS_CC); }
/** * @zobject: swoole_http_client_coro object */ static void http_client_coro_onClose(swClient *cli) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *zobject = cli->object; if (!cli->released) { http_client_free(zobject TSRMLS_CC); } #if PHP_MAJOR_VERSION < 7 sw_zval_ptr_dtor(&zobject); #endif return; }
static int websocket_handshake(http_client *client) { //HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: %s\r\nSec-WebSocket-Version: %s\r\nKeepAlive: off\r\nContent-Length: 0\r\nServer: ZWebSocket\r\n TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); zval *header = zend_read_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("header"), 1 TSRMLS_CC); HashTable *ht = Z_ARRVAL_P(header); zval **pData; if(zend_hash_find(ht, ZEND_STRS("sec-websocket-key") , (void **) &pData) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "header no sec-websocket-key"); return SW_ERR; } convert_to_string(*pData); // swTrace("key: %s len:%d\n", Z_STRVAL_PP(pData), Z_STRLEN_PP(pData)); swString *buf = swString_new(256); swString_append_ptr(buf, ZEND_STRL("HTTP/1.1 101 Switching Protocols\r\n")); swString_append_ptr(buf, ZEND_STRL("Upgrade: websocket\r\nConnection: Upgrade\r\n")); swString *shaBuf = swString_new(Z_STRLEN_PP(pData)+36); swString_append_ptr(shaBuf, Z_STRVAL_PP(pData), Z_STRLEN_PP(pData)); swString_append_ptr(shaBuf, ZEND_STRL(SW_WEBSOCKET_GUID)); char data_str[20]; // bzero(data_str, sizeof(data_str)); // swTrace("sha1 start:%s\n", shaBuf->str); sha1(shaBuf->str, (unsigned char *) data_str); char encoded_value[50]; bzero(encoded_value, sizeof(encoded_value)); // swTrace("base64_encode start:%d\n", sizeof(data_str)); swBase64_encode((unsigned char *) data_str, 20, encoded_value); // swTrace("base64_encode end:%s %d %d\n", encoded_value, encoded_len, strlen(encoded_value)); char _buf[128]; int n = 0; n = snprintf(_buf, strlen(encoded_value)+25, "Sec-WebSocket-Accept: %s\r\n", encoded_value); // efree(data_str); // efree(encoded_value); swString_free(shaBuf); // swTrace("accept value: %s\n", _buf); swString_append_ptr(buf, _buf, n); swString_append_ptr(buf, ZEND_STRL("Sec-WebSocket-Version: 13\r\n")); swString_append_ptr(buf, ZEND_STRL("Server: swoole-websocket\r\n\r\n")); swTrace("websocket header len:%zd\n%s \n", buf->length, buf->str); int ret = swServer_tcp_send(SwooleG.serv, client->fd, buf->str, buf->length); swString_free(buf); // swTrace("handshake send: %d lenght: %d\n", client->fd, ret); return ret; }
/* ****************************************************************************************************** Lists the UDF modules registered with the server. * * @param aerospike_obj_p The C client's aerospike object. * @param error_p The C client's as_error to be set to the encountered error. * @param array_of_modules_p An array of registered UDF modules * against the Aerospike DB to be populated by * this function. * @param language Optionally filters a subset of modules * matching the given type. * @options_p The optional parameters to the * Aerospike::listRegistered(). * * @return AEROSPIKE_OK if success. Otherwise AEROSPIKE_x. ****************************************************************************************************** */ extern as_status aerospike_list_registered_udf_modules(Aerospike_object* aerospike_obj_p, as_error* error_p, zval* array_of_modules_p, long language, zval* options_p) { as_udf_files udf_files; uint32_t init_udf_files = 0; uint32_t i = 0; as_policy_info info_policy; TSRMLS_FETCH_FROM_CTX(aerospike_obj_p->ts); set_policy(&aerospike_obj_p->as_ref_p->as_p->config, NULL, NULL, NULL, NULL, &info_policy, NULL, NULL, NULL, options_p, error_p TSRMLS_CC); if (AEROSPIKE_OK != (error_p->code)) { DEBUG_PHP_EXT_DEBUG("Unable to set policy"); goto exit; } as_udf_files_init(&udf_files, 0); init_udf_files = 1; if (AEROSPIKE_OK != aerospike_udf_list(aerospike_obj_p->as_ref_p->as_p, error_p, &info_policy, &udf_files)) { DEBUG_PHP_EXT_ERROR("%s", error_p->message); goto exit; } for (i = 0; i < udf_files.size; i++) { if ((language != -1) && (udf_files.entries[i].type != language)) { continue; } zval* module_p = NULL; MAKE_STD_ZVAL(module_p); array_init(module_p); add_assoc_stringl(module_p, UDF_MODULE_NAME, udf_files.entries[i].name, strlen(udf_files.entries[i].name), 1); add_assoc_long(module_p, UDF_MODULE_TYPE, (udf_files.entries[i].type)); add_next_index_zval(array_of_modules_p, module_p); } exit: if (init_udf_files) { as_udf_files_destroy(&udf_files); } return error_p->code; }
/** * @zobject: swoole_http_client object */ static void http_client_coro_onError(swClient *cli) { #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval *zdata; zval *retval = NULL; SW_MAKE_STD_ZVAL(zdata); //return false ZVAL_BOOL(zdata, 0); zval *zobject = cli->object; php_context *sw_current_context = swoole_get_property(zobject, 1); zend_update_property_long(swoole_http_client_coro_class_entry_ptr, zobject, ZEND_STRL("errCode"), SwooleG.error TSRMLS_CC); if (cli->timeout_id > 0) { php_swoole_clear_timer_coro(cli->timeout_id TSRMLS_CC); cli->timeout_id=0; } if (!cli->released) { http_client_free(zobject TSRMLS_CC); } swoole_set_object(zobject, NULL); http_client_property *hcc = swoole_get_property(zobject, 0); if(hcc->defer && hcc->defer_status != HTTP_CLIENT_STATE_DEFER_WAIT){ hcc->defer_status = HTTP_CLIENT_STATE_DEFER_DONE; hcc->defer_result = 0; goto free_zdata; } hcc->defer_status = HTTP_CLIENT_STATE_DEFER_INIT; int ret = coro_resume(sw_current_context, zdata, &retval); if (ret > 0) { goto free_zdata; } if (retval != NULL) { sw_zval_ptr_dtor(&retval); } free_zdata: sw_zval_ptr_dtor(&zdata); }
static int http_request_on_query_string(php_http_parser *parser, const char *at, size_t length) { TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); http_client *client = parser->data; char *query = estrndup(at, length); zval *get; MAKE_STD_ZVAL(get); array_init(get); zend_update_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("get"), get TSRMLS_CC); sapi_module.treat_data(PARSE_STRING, query, get TSRMLS_CC); ZEND_SET_SYMBOL(&EG(symbol_table), "_GET", get); zval *_request = zend_read_property(swoole_http_request_class_entry_ptr, client->zrequest, ZEND_STRL("request"), 1 TSRMLS_CC); zend_hash_copy(Z_ARRVAL_P(_request), Z_ARRVAL_P(get), NULL, NULL, sizeof(zval)); return 0; }
static void php_swoole_onTimeout(swTimer *timer, swTimer_node *event) { swTimer_callback *cb = event->data; zval *retval = NULL; #if PHP_MAJOR_VERSION < 7 TSRMLS_FETCH_FROM_CTX(sw_thread_ctx ? sw_thread_ctx : NULL); #endif zval **args[1]; int argc = 0; if (cb->data) { args[0] = &cb->data; argc = 1; } timer->_current_id = event->id; if (sw_call_user_function_ex(EG(function_table), NULL, cb->callback, &retval, argc, args, 0, NULL TSRMLS_CC) == FAILURE) { swoole_php_fatal_error(E_WARNING, "swoole_timer: onTimeout handler error"); return; } timer->_current_id = -1; if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR TSRMLS_CC); } if (retval) { sw_zval_ptr_dtor(&retval); } cb = event->data; if (cb) { if (cb->data) { sw_zval_ptr_dtor(&cb->data); } sw_zval_ptr_dtor(&cb->callback); efree(cb); } }
char *owsrequest_getenv(const char *name, void *thread_context) { zval **val, **ppzval; zval *cookie_result, *key; HashTable *cookies; char *string_key = NULL, *cookie_tmp; ulong num_key; int numElements, i = 0; TSRMLS_FETCH_FROM_CTX(thread_context); if (STRING_EQUAL(name, "HTTP_COOKIE")) { cookies = PG(http_globals)[TRACK_VARS_COOKIE]->value.ht; numElements = zend_hash_num_elements(cookies); MAKE_STD_ZVAL(cookie_result); ZVAL_STRING(cookie_result, "",1); for(zend_hash_internal_pointer_reset(cookies); zend_hash_has_more_elements(cookies) == SUCCESS; zend_hash_move_forward(cookies), ++i) { zend_hash_get_current_data(cookies, (void **)&ppzval); zend_hash_get_current_key(cookies, &string_key, &num_key, 1); cookie_tmp = malloc((strlen(string_key)+Z_STRLEN_PP(ppzval)+3) * sizeof(char)); sprintf(cookie_tmp, "%s=%s;",string_key,Z_STRVAL_PP(ppzval)); MAKE_STD_ZVAL(key); ZVAL_STRING(key, cookie_tmp,1); add_string_to_string(cookie_result,cookie_result, key); zval_dtor(key); free(cookie_tmp); } return Z_STRVAL_P(cookie_result); } else { zend_is_auto_global("_SERVER", sizeof("_SERVER")-1 TSRMLS_CC); if ( PG(http_globals)[TRACK_VARS_SERVER] && (zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, name, strlen(name)+1, (void **) &val) == SUCCESS) && (Z_TYPE_PP(val) == IS_STRING)) { return Z_STRVAL_PP(val); } } return NULL; }