// Recieved data to write from ClientRequest._write void iotjs_https_data_to_write(iotjs_https_t* https_data, iotjs_string_t read_chunk, const iotjs_jval_t* callback, const iotjs_jval_t* onwrite) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_https_t, https_data); if (_this->to_destroy_read_onwrite) { _this->to_destroy_read_onwrite = false; iotjs_string_destroy(&(_this->read_chunk)); iotjs_jval_destroy(&(_this->read_onwrite)); iotjs_jval_destroy(&(_this->read_callback)); } _this->read_chunk = read_chunk; _this->data_to_read = true; _this->read_callback = iotjs_jval_create_copied(callback); _this->read_onwrite = iotjs_jval_create_copied(onwrite); _this->to_destroy_read_onwrite = true; if (_this->request_done) { iotjs_https_call_read_onwrite_async(https_data); } else if (_this->is_stream_writable) { curl_easy_pause(_this->curl_easy_handle, CURLPAUSE_CONT); uv_timer_stop(&(_this->timeout)); uv_timer_start(&(_this->timeout), iotjs_https_uv_timeout_callback, 1, 0); } }
void iotjs_jhandler_initialize(iotjs_jhandler_t* jhandler, const jerry_value_t jfunc, const jerry_value_t jthis, const jerry_value_t jargv[], const uint16_t jargc) { IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_jhandler_t, jhandler); _this->jfunc = iotjs_jval_create_raw(jfunc); _this->jthis = iotjs_jval_create_raw(jthis); _this->jret = iotjs_jval_create_copied(iotjs_jval_get_undefined()); #ifdef NDEBUG _this->jargv = (iotjs_jval_t*)jargv; #else if (jargc > 0) { unsigned buffer_size = sizeof(iotjs_jval_t) * jargc; _this->jargv = (iotjs_jval_t*)iotjs_buffer_allocate(buffer_size); for (int i = 0; i < jargc; ++i) { _this->jargv[i] = iotjs_jval_create_raw(jargv[i]); } } else { _this->jargv = NULL; } _this->finished = false; #endif _this->jargc = jargc; }
void iotjs_jargs_replace(iotjs_jargs_t* jargs, uint16_t index, const iotjs_jval_t* x) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jargs_t, jargs); IOTJS_ASSERT(index < _this->argc); iotjs_jval_destroy(&_this->argv[index]); _this->argv[index] = iotjs_jval_create_copied(x); }
void iotjs_jhandler_throw(iotjs_jhandler_t* jhandler, const iotjs_jval_t* err) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jhandler_t, jhandler); #ifndef NDEBUG IOTJS_ASSERT(_this->finished == false); #endif iotjs_jval_destroy(&_this->jret); _this->jret = iotjs_jval_create_copied(err); jerry_value_set_error_flag(&_this->jret.unsafe.value); #ifndef NDEBUG _this->finished = true; #endif }
void iotjs_jhandler_return_jval(iotjs_jhandler_t* jhandler, const iotjs_jval_t* ret) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jhandler_t, jhandler); #ifndef NDEBUG IOTJS_ASSERT(_this->finished == false); #endif iotjs_jval_destroy(&_this->jret); _this->jret = iotjs_jval_create_copied(ret); #ifndef NDEBUG _this->finished = true; #endif }
iotjs_jval_t iotjs_jval_get_property_by_index(const iotjs_jval_t* jarr, uint32_t idx) { const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jarr); IOTJS_ASSERT(iotjs_jval_is_object(jarr)); jerry_value_t res = jerry_get_property_by_index(_this->value, idx); if (jerry_value_has_error_flag(res)) { jerry_release_value(res); return iotjs_jval_create_copied(iotjs_jval_get_undefined()); } return iotjs_jval_create_raw(res); }
iotjs_jval_t iotjs_jval_get_property(const iotjs_jval_t* jobj, const char* name) { const IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jval_t, jobj); IOTJS_ASSERT(iotjs_jval_is_object(jobj)); jerry_value_t prop_name = jerry_create_string((const jerry_char_t*)(name)); jerry_value_t res = jerry_get_property(_this->value, prop_name); jerry_release_value(prop_name); if (jerry_value_has_error_flag(res)) { jerry_release_value(res); return iotjs_jval_create_copied(iotjs_jval_get_undefined()); } return iotjs_jval_create_raw(res); }
void iotjs_jargs_append_jval(iotjs_jargs_t* jargs, const iotjs_jval_t* x) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_jargs_t, jargs); IOTJS_ASSERT(_this->argc < _this->capacity); _this->argv[_this->argc++] = iotjs_jval_create_copied(x); }
//-------------Constructor------------ iotjs_https_t* iotjs_https_create(const char* URL, const char* method, const char* ca, const char* cert, const char* key, const bool reject_unauthorized, const iotjs_jval_t* jthis) { iotjs_https_t* https_data = IOTJS_ALLOC(iotjs_https_t); IOTJS_VALIDATED_STRUCT_CONSTRUCTOR(iotjs_https_t, https_data); // Original Request Details _this->URL = URL; _this->header_list = NULL; if (strcmp(method, STRING_GET) == 0) _this->method = HTTPS_GET; else if (strcmp(method, STRING_POST) == 0) _this->method = HTTPS_POST; else if (strcmp(method, STRING_PUT) == 0) _this->method = HTTPS_PUT; else if (strcmp(method, STRING_DELETE) == 0) _this->method = HTTPS_DELETE; else if (strcmp(method, STRING_HEAD) == 0) _this->method = HTTPS_HEAD; else if (strcmp(method, STRING_CONNECT) == 0) _this->method = HTTPS_CONNECT; else if (strcmp(method, STRING_OPTIONS) == 0) _this->method = HTTPS_OPTIONS; else if (strcmp(method, STRING_TRACE) == 0) _this->method = HTTPS_TRACE; else { IOTJS_ASSERT(0); } // TLS certs stuff _this->ca = ca; _this->cert = cert; _this->key = key; _this->reject_unauthorized = reject_unauthorized; // Content Length stuff _this->content_length = -1; // Handles _this->loop = iotjs_environment_loop(iotjs_environment_get()); _this->jthis_native = iotjs_jval_create_copied(jthis); iotjs_jval_set_object_native_handle(&(_this->jthis_native), (uintptr_t)https_data, &https_native_info); _this->curl_multi_handle = curl_multi_init(); _this->curl_easy_handle = curl_easy_init(); _this->timeout.data = (void*)https_data; uv_timer_init(_this->loop, &(_this->timeout)); _this->request_done = false; _this->closing_handles = 3; _this->poll_data = NULL; // Timeout stuff _this->timeout_ms = -1; _this->last_bytes_num = -1; _this->last_bytes_time = 0; _this->socket_timeout.data = (void*)https_data; uv_timer_init(_this->loop, &(_this->socket_timeout)); // ReadData stuff _this->cur_read_index = 0; _this->is_stream_writable = false; _this->stream_ended = false; _this->data_to_read = false; _this->to_destroy_read_onwrite = false; _this->async_read_onwrite.data = (void*)https_data; uv_timer_init(_this->loop, &(_this->async_read_onwrite)); // No Need to read data for following types of requests if (_this->method == HTTPS_GET || _this->method == HTTPS_DELETE || _this->method == HTTPS_HEAD || _this->method == HTTPS_OPTIONS || _this->method == HTTPS_TRACE) _this->stream_ended = true; return https_data; }