static GByteArray* _download_to_gba(ne_session *session, const gchar *path_url, GError **error) { GByteArray *gba; ne_request *http_request; DEBUG("About to download [%s] into a memory buffer", path_url); gba = g_byte_array_new(); http_request = _build_request(session, path_url); ne_add_response_body_reader(http_request, ne_accept_2xx, read_to_gba, gba); switch (ne_request_dispatch(http_request)) { case NE_OK: if (ne_get_status(http_request)->klass != 2) { GSETERROR (error, "Failed to download '%s': %s", path_url, ne_get_error(session)); g_byte_array_free(gba, TRUE); gba = NULL; } break; case NE_AUTH: case NE_CONNECT: case NE_TIMEOUT: case NE_ERROR: default: GSETERROR(error,"Failed download '%s': %s", path_url, ne_get_error(session)); g_byte_array_free(gba, TRUE); gba = NULL; break; } ne_request_destroy(http_request); return gba; }
pplx::task<http::http_response> http_client_impl::send_http_request(const ::utility::string_t& method, const ::utility::string_t& request_uri, const ::utility::string_t accept, ::web::json::value object) { http::uri_builder bldr; bldr.set_path(request_uri); auto request = _build_request(method, bldr, accept, object); return m_client->request(request); }
static gboolean _download_to_file(ne_session *session, const gchar *path_url, const gchar *path_local, GError **error) { gchar *dirname, path_tmp[2048]; gboolean rc = FALSE; int rc_dispatch; FILE *stream_out; ne_request *http_request; g_snprintf(path_tmp, sizeof(path_tmp), "%s.%d.%ld", path_local, getpid(), time(0)); DEBUG("About to download [%s] into [%s]", path_url, path_tmp); /*create the destination*/ dirname = g_path_get_dirname(path_tmp); if (!dirname) { GSETERROR(error,"Failed to extract the dirname of '%s'", path_tmp); return FALSE; } if (-1 == g_mkdir_with_parents(dirname,0755)) { g_free(dirname); GSETERROR(error,"Failed to create the dirname of '%s' : %s", path_tmp, strerror(errno)); return FALSE; } g_free(dirname); /*open the destination*/ stream_out = fopen(path_tmp,"w"); if (!stream_out) { GSETERROR(error,"Failed to open '%s' in write mode : %s", path_local, strerror(errno)); return FALSE; } http_request = _build_request(session, path_url); ne_add_response_body_reader(http_request, ne_accept_2xx, read_to_stream, stream_out); switch (rc_dispatch = ne_request_dispatch(http_request)) { case NE_OK: if (ne_get_status(http_request)->klass != 2) { GSETERROR (error, "Failed to download '%s': %s", path_url, ne_get_error(session)); goto label_error; } break; case NE_AUTH: case NE_CONNECT: case NE_TIMEOUT: case NE_ERROR: GSETERROR(error,"Failed download '%s' (rc=%d) : %s", path_url, rc_dispatch, ne_get_error(session)); goto label_error; } if (-1 == g_rename(path_tmp, path_local)) { GSETERROR(error,"Failed to commit the temporary download file '%s' : %s", path_tmp, strerror(errno)); goto label_error; } g_chmod(path_local,0644); DEBUG("Download of '%s' succeeded", path_url); rc = TRUE; label_error: ne_request_destroy(http_request); fclose(stream_out); if (g_file_test(path_tmp, G_FILE_TEST_IS_REGULAR)) g_remove(path_tmp); return rc; }