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; }
ne_decompress *ne_decompress_reader(ne_request *req, ne_accept_response acpt, ne_block_reader rdr, void *userdata) { ne_add_response_body_reader(req, acpt, rdr, userdata); /* an arbitrary return value: don't confuse them by returning NULL. */ return (ne_decompress *)req; }
static void install_content_reader( ne_request *req, void *userdata, const ne_status *status ) { const char *enc = NULL; struct transfer_context *writeCtx = userdata; (void) status; if( !writeCtx ) { DEBUG_WEBDAV("Error: install_content_reader called without valid write context!"); return; } enc = ne_get_response_header( req, "Content-Encoding" ); if( status && status->code != 200 ) { DEBUG_WEBDAV("Content encoding ist <%s> with status %d", enc ? enc : "empty", status ? status->code : -1 ); } if( enc && c_streq( enc, "gzip" )) { writeCtx->decompress = ne_decompress_reader( req, ne_accept_2xx, compress_reader, /* reader callback */ (void*) writeCtx ); /* userdata */ } else { ne_add_response_body_reader( req, ne_accept_2xx, uncompress_reader, (void*) writeCtx ); writeCtx->decompress = NULL; } }
void get(const StringSlice& url, WriteTarget out) { static int inited = ne_sock_init(); if (inited != 0) { throw Exception("ne_sock_init()"); } CString cstr(url); ne_uri uri = {}; if (ne_uri_parse(cstr.data(), &uri)) { throw Exception("ne_uri_parse()"); } if (uri.port == 0) { uri.port = ne_uri_defaultport(uri.scheme); } unique_ptr<ne_uri, decltype(&ne_uri_free)> uri_free(&uri, ne_uri_free); unique_ptr<ne_session, decltype(&ne_session_destroy)> sess( ne_session_create(uri.scheme, uri.host, uri.port), ne_session_destroy); unique_ptr<ne_request, decltype(&ne_request_destroy)> req( ne_request_create(sess.get(), "GET", uri.path), ne_request_destroy); ne_userdata userdata = {out}; ne_add_response_body_reader(req.get(), accept, reader, &userdata); auto err = ne_request_dispatch(req.get()); if (err != NE_OK) { throw Exception("ne_request_dispatch()"); } auto* st = ne_get_status(req.get()); if (st->code != 200) { throw Exception(st->code); } }
WebGrep::IssuedRequest Client::issueRequest(const char* method, const char* path, bool withLock) { std::shared_ptr<std::lock_guard<std::mutex>> lk; if (withLock) { lk = std::make_shared<std::lock_guard<std::mutex>>(ctx->mu); } ctx->response.clear(); auto rq = ne_request_create(ctx->sess, method, path); ne_add_response_body_reader(rq, ne_accept_always, httpResponseReader, (void*)ctx.get()); IssuedRequest out; out.ctx = ctx; out.req = std::shared_ptr<ne_request>(rq, [out](ne_request* ptr){ne_request_destroy(ptr);} ); return out; }
ne_decompress *ne_decompress_reader(ne_request *req, ne_accept_response acpt, ne_block_reader rdr, void *userdata) { ne_decompress *ctx = ne_calloc(sizeof *ctx); ne_add_request_header(req, "Accept-Encoding", "gzip"); ne_add_response_body_reader(req, gz_acceptor, gz_reader, ctx); ctx->reader = rdr; ctx->userdata = userdata; ctx->session = ne_get_session(req); ctx->request = req; ctx->acceptor = acpt; ne__reqhook_pre_send(req, gz_pre_send, ctx); return ctx; }
/* Attach READER as a response reader for the request REQ, with the * acceptance function ACCPT. The response body data will be decompressed, * if compressed, before being passed to READER. USERDATA will be passed as * the first argument to the acceptance and reader callbacks. */ static void attach_ne_body_reader(svn_ra_neon__request_t *req, ne_accept_response accpt, ne_block_reader reader, void *userdata) { if (req->sess->compression) { ne_decompress *decompress = ne_decompress_reader(req->ne_req, accpt, reader, userdata); apr_pool_cleanup_register(req->pool, decompress, compressed_body_reader_cleanup, apr_pool_cleanup_null); } else ne_add_response_body_reader(req->ne_req, accpt, reader, userdata); }
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; }
GHashTable * rawx_client_get_statistics(rawx_session_t * session, const gchar *url, GError ** err) { int rc; gchar str_addr[64]; gsize str_addr_size; GHashTable *parsed = NULL; GHashTable *result = NULL; GByteArray *buffer = NULL; ne_request *request = NULL; if (!session || !url) { GSETERROR(err, "Invalid parameter"); return NULL; } ne_set_connect_timeout(session->neon_session, session->timeout.cnx / 1000); ne_set_read_timeout(session->neon_session, session->timeout.req / 1000); request = ne_request_create(session->neon_session, "GET", url); if (!request) { GSETERROR(err, "neon request creation error"); return NULL; } buffer = g_byte_array_new(); ne_add_response_body_reader(request, ne_accept_2xx, body_reader, buffer); switch (rc = ne_request_dispatch(request)) { case NE_OK: if (ne_get_status(request)->klass != 2) { GSETERROR(err, "RAWX returned an error"); goto exit; } else if (!(parsed = body_parser(buffer, err))) { GSETERROR(err, "No statistics from the RAWX server"); goto exit; } break; case NE_ERROR: case NE_TIMEOUT: case NE_CONNECT: case NE_AUTH: str_addr_size = addr_info_to_string(&(session->addr), str_addr, sizeof(str_addr)); GSETERROR(err, "cannot download the stats from [%.*s]' (%s)", (int)str_addr_size, str_addr, ne_get_error(session->neon_session)); goto exit; default: GSETERROR(err, "Unexpected return code from the neon library : %d", rc); goto exit; } result = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); g_hash_table_foreach(parsed, _convert_string_to_double, result); exit: if (buffer != NULL) g_byte_array_free(buffer, TRUE); if (request != NULL) ne_request_destroy(request); if (parsed != NULL) g_hash_table_destroy(parsed); return result; }
gboolean rawx_client_get_directory_data(rawx_session_t * session, hash_sha256_t chunk_id, struct content_textinfo_s *content, struct chunk_textinfo_s *chunk, GError ** error) { int rc; gchar str_addr[64]; gsize str_addr_size; gchar str_req[2048]; gchar str_chunk_id[(sizeof(hash_sha256_t) * 2) + 1]; GHashTable *result = NULL; GByteArray *buffer = NULL; ne_request *request = NULL; if (!session) { GSETERROR(error, "Invalid parameter"); return FALSE; } memset(str_chunk_id, '\0', sizeof(str_chunk_id)); oio_str_bin2hex(chunk_id, sizeof(hash_sha256_t), str_chunk_id, sizeof(str_chunk_id)); memset(str_req, '\0', sizeof(str_req)); snprintf(str_req, sizeof(str_req) - 1, "%s/%s", RAWX_REQ_GET_DIRINFO, str_chunk_id); ne_set_connect_timeout(session->neon_session, session->timeout.cnx / 1000); ne_set_read_timeout(session->neon_session, session->timeout.req / 1000); request = ne_request_create(session->neon_session, "HEAD", str_req); if (!request) { GSETERROR(error, "neon request creation error"); return FALSE; } buffer = g_byte_array_new(); ne_add_response_body_reader(request, ne_accept_2xx, body_reader, buffer); switch (rc = ne_request_dispatch(request)) { case NE_OK: if (ne_get_status(request)->klass != 2) { GSETERROR(error, "RAWX returned an error %d : %s", ne_get_status(request)->code, ne_get_status(request)->reason_phrase); goto error; } else if (!(result = header_parser(request))) { GSETERROR(error, "No attr from the RAWX server"); goto error; } break; case NE_ERROR: case NE_TIMEOUT: case NE_CONNECT: case NE_AUTH: str_addr_size = addr_info_to_string(&(session->addr), str_addr, sizeof(str_addr)); GSETERROR(error, "cannot download the data from [%.*s]' (%s)", (int)str_addr_size, str_addr, ne_get_error(session->neon_session)); goto error; default: GSETERROR(error, "Unexpected return code from the neon library : %d", rc); goto error; } g_byte_array_free(buffer, TRUE); ne_request_destroy(request); /* Fill the textinfo structs */ parse_chunkinfo_from_rawx(result, content, chunk); g_hash_table_destroy(result); return TRUE; error: g_byte_array_free(buffer, TRUE); ne_request_destroy(request); return FALSE; }