int32_t ppb_host_resolver_resolve(PP_Resource host_resolver, const char *host, uint16_t port, const struct PP_HostResolver_Private_Hint *hint, struct PP_CompletionCallback callback) { struct pp_host_resolver_s *hr = pp_resource_acquire(host_resolver, PP_RESOURCE_HOST_RESOLVER); if (!hr) { trace_error("%s, bad resource\n", __func__); return PP_ERROR_BADRESOURCE; } hr->host = nullsafe_strdup(host); struct async_network_task_s *task = async_network_task_create(); task->type = ASYNC_NETWORK_HOST_RESOLVE; task->resource = host_resolver; task->host = nullsafe_strdup(host); task->port = port; task->callback = callback; task->callback_ml = ppb_message_loop_get_current(); pp_resource_release(host_resolver); async_network_task_push(task); return PP_OK_COMPLETIONPENDING; }
int32_t ppb_tcp_socket_connect(PP_Resource tcp_socket, const char *host, uint16_t port, struct PP_CompletionCallback callback) { struct pp_tcp_socket_s *ts = pp_resource_acquire(tcp_socket, PP_RESOURCE_TCP_SOCKET); if (!ts) { trace_error("%s, bad resource\n", __func__); return PP_ERROR_BADRESOURCE; } struct async_network_task_s *task = async_network_task_create(); task->type = ASYNC_NETWORK_TCP_CONNECT; task->resource = tcp_socket; task->sock = ts->sock; task->host = nullsafe_strdup(host); task->port = port; task->callback = callback; pp_resource_release(tcp_socket); async_network_task_push(task); return PP_OK_COMPLETIONPENDING; }
int32_t ppb_url_loader_follow_redirect(PP_Resource loader, struct PP_CompletionCallback callback) { struct pp_url_loader_s *ul = pp_resource_acquire(loader, PP_RESOURCE_URL_LOADER); if (!ul) { trace_error("%s, bad resource\n", __func__); return PP_ERROR_BADRESOURCE; } char *new_url = nullsafe_strdup(ul->redirect_url); free_and_nullify(ul, url); free_and_nullify(ul, redirect_url); free_and_nullify(ul, status_line); free_and_nullify(ul, headers); free_and_nullify(ul, request_headers); if (ul->fd >= 0) { close(ul->fd); ul->fd = -1; } // abort further handling of the NPStream if (ul->np_stream) { ul->np_stream->pdata = NULL; ul->np_stream = NULL; } ul->fd = open_temporary_file(); ul->url = new_url; ul->read_pos = 0; ul->method = PP_METHOD_GET; ul->ccb = callback; struct url_loader_open_param_s *p = g_slice_alloc(sizeof(*p)); p->url = ul->url; p->loader = loader; p->instance_id = ul->instance->id; p->method = ul->method; p->request_headers = ul->request_headers; p->custom_referrer_url = ul->custom_referrer_url; p->custom_content_transfer_encoding = ul->custom_content_transfer_encoding; p->custom_user_agent = ul->custom_user_agent; p->target = NULL; p->post_len = 0; p->post_data = NULL; p->m_loop = ppb_message_loop_get_current(); p->depth = ppb_message_loop_get_depth(p->m_loop) + 1; ppb_message_loop_post_work(p->m_loop, PP_MakeCCB(_url_loader_open_comt, p), 0); ppb_message_loop_run_nested(p->m_loop); pp_resource_release(loader); int retval = p->retval; g_slice_free1(sizeof(*p), p); if (retval != NPERR_NO_ERROR) return PP_ERROR_FAILED; if (callback.func == NULL) { int done = 0; while (!done) { ul = pp_resource_acquire(loader, PP_RESOURCE_URL_LOADER); if (ul) { done = ul->finished_loading; pp_resource_release(loader); } else { break; } printf("waitin'\n"); usleep(10000); } return PP_OK; } return PP_OK_COMPLETIONPENDING; }
int32_t ppb_url_loader_open_target(PP_Resource loader, PP_Resource request_info, struct PP_CompletionCallback callback, const char *target) { struct pp_url_loader_s *ul = pp_resource_acquire(loader, PP_RESOURCE_URL_LOADER); if (!ul) { trace_error("%s, bad resource\n", __func__); return PP_ERROR_BADRESOURCE; } struct pp_url_request_info_s *ri = pp_resource_acquire(request_info, PP_RESOURCE_URL_REQUEST_INFO); if (!ri) { trace_error("%s, bad resource\n", __func__); pp_resource_release(loader); return PP_ERROR_BADRESOURCE; } struct PP_Var full_url; if (ri->is_immediate_javascript) { full_url = ppb_var_var_from_utf8_z(ri->url); } else { struct PP_Var rel_url = ppb_var_var_from_utf8_z(ri->url); full_url = ppb_url_util_dev_resolve_relative_to_document(ul->instance->id, rel_url, NULL); ppb_var_release(rel_url); } ul->url = nullsafe_strdup(ppb_var_var_to_utf8(full_url, NULL)); ul->method = ri->method; ul->read_pos = 0; ul->request_headers = nullsafe_strdup(ri->headers); ul->follow_redirects = ri->follow_redirects; ul->stream_to_file = ri->stream_to_file; ul->record_download_progress = ri->record_download_progress; ul->record_upload_progress = ri->record_upload_progress; ul->custom_referrer_url = nullsafe_strdup(ri->custom_referrer_url); ul->allow_cross_origin_requests = ri->allow_cross_origin_requests; ul->allow_credentials = ri->allow_credentials; ul->custom_content_transfer_encoding = nullsafe_strdup(ri->custom_content_transfer_encoding); ul->custom_user_agent = nullsafe_strdup(ri->custom_user_agent); ul->target = nullsafe_strdup(target); #define TRIM_NEWLINE(s) s = trim_nl(s) TRIM_NEWLINE(ul->request_headers); TRIM_NEWLINE(ul->custom_referrer_url); TRIM_NEWLINE(ul->custom_content_transfer_encoding); TRIM_NEWLINE(ul->custom_user_agent); ul->post_len = ri->post_len; if (ri->post_len > 0) { ul->post_data = malloc(ri->post_len); memcpy(ul->post_data, ri->post_data, ri->post_len); } ul->fd = open_temporary_file(); ul->ccb = callback; ppb_var_release(full_url); pp_resource_release(request_info); struct url_loader_open_param_s *p = g_slice_alloc(sizeof(*p)); p->url = ul->url; p->loader = loader; p->instance_id = ul->instance->id; p->method = ul->method; p->request_headers = ul->request_headers; p->custom_referrer_url = ul->custom_referrer_url; p->custom_content_transfer_encoding = ul->custom_content_transfer_encoding; p->custom_user_agent = ul->custom_user_agent; p->target = ul->target; p->post_len = ul->post_len; p->post_data = ul->post_data; p->m_loop = ppb_message_loop_get_current(); p->depth = ppb_message_loop_get_depth(p->m_loop) + 1; ppb_core_add_ref_resource(loader); // add ref to ensure data in ul remain accessible pp_resource_release(loader); ppb_message_loop_post_work(p->m_loop, PP_MakeCCB(_url_loader_open_comt, p), 0); ppb_message_loop_run_nested(p->m_loop); ppb_core_release_resource(loader); int retval = p->retval; g_slice_free1(sizeof(*p), p); if (retval != NPERR_NO_ERROR) return PP_ERROR_FAILED; if (callback.func == NULL) { int done = 0; while (!done) { ul = pp_resource_acquire(loader, PP_RESOURCE_URL_LOADER); if (ul) { done = ul->finished_loading; pp_resource_release(loader); } else { break; } printf("waitin'\n"); usleep(10000); } return PP_OK; } return PP_OK_COMPLETIONPENDING; }