Verified Commit 1a8cd62a authored by Karel Koci's avatar Karel Koci 🤘

download: add function that overtakes buffer

This adds instance free function that instead of deallocating output
buffer passes it to caller. This is handy if you want to preserve
downloaded data but no longer need download instance or downloader as a
whole.
parent 1f04e171
......@@ -360,3 +360,15 @@ void download_i_free(struct download_i *inst) {
}
free(inst);
}
void download_i_collect_data(struct download_i *inst, uint8_t **data, size_t *size) {
if (inst->out_t == DOWN_OUT_T_BUFFER) {
*data = inst->out.buff->data;
*size = inst->out.buff->size;
inst->out.buff->data = NULL;
} else {
*data = NULL;
*size = 0;
}
download_i_free(inst);
}
......@@ -106,7 +106,7 @@ void download_opts_def(struct download_opts *opts) __attribute__((nonnull));
// Register given URL to be downloaded to file.
// url: URL data are downloaded from
// output_path: Path where data are going to be stored (written to)
// opts: Download options
// opts: Download options (does not have to exist during instance existence)
// Returns download instance
struct download_i *download_file(struct downloader *downloader, const char *url,
const char *output_path, const struct download_opts *opts)
......@@ -117,8 +117,8 @@ struct download_i *download_file(struct downloader *downloader, const char *url,
// url: URL data are downloaded from
// output_template: Template for path where data are going to be stored (written
// to). Passed string has to end with XXXXXX and is modified to contain used
// path.
// opts: Download options
// path. This string should be freed only after download instance is freed.
// opts: Download options (does not have to exist during instance existence)
// Returns download instance
struct download_i *download_temp_file(struct downloader *downloader,
const char *url, char *output_template, const struct download_opts *opts)
......@@ -126,7 +126,7 @@ struct download_i *download_temp_file(struct downloader *downloader,
// Register given URL to be downloaded to internal buffer.
// url: URL data are downloaded from
// opts: Download options
// opts: Download options (does not have to exist during instance existence)
// Returns download instance
struct download_i *download_data(struct downloader *downloader, const char *url,
const struct download_opts *opts) __attribute__((nonnull(1, 2, 3)));
......@@ -134,4 +134,11 @@ struct download_i *download_data(struct downloader *downloader, const char *url,
// Free download instance
void download_i_free(struct download_i*) __attribute__((nonnull));
// This is same as download_i_free but where download_i_free just frees downloaded
// buffer, this passes it to caller. Instance is freed the same way as in case of
// download_i_free but data buffer has to be freed later by caller.
// In other words this overtakes allocated buffer and frees rest of instance.
// This can be called only on instance that was created by download_data.
void download_i_collect_data(struct download_i*, uint8_t **data, size_t *size);
#endif
......@@ -297,6 +297,27 @@ START_TEST(cert_invalid) {
}
END_TEST
// Test that we are able to overtake buffer
START_TEST(collect_data) {
struct downloader *d = downloader_new(1);
ck_assert_ptr_null(downloader_run(d));
struct download_opts opts;
download_opts_def(&opts);
struct download_i *inst = download_data(d, HTTP_SMALL, &opts);
ck_assert_ptr_null(downloader_run(d));
uint8_t *data;
size_t size;
download_i_collect_data(inst, &data, &size);
ck_assert_uint_eq(SMALL_SIZE, size);
ck_assert_str_eq(SMALL_CONTENT, (char *)data);
downloader_free(d);
}
END_TEST
Suite *gen_test_suite(void) {
Suite *result = suite_create("Download");
......@@ -312,6 +333,7 @@ Suite *gen_test_suite(void) {
tcase_add_test(down, invalid_continue);
tcase_add_test(down, cert_pinning);
tcase_add_test(down, cert_invalid);
tcase_add_test(down, collect_data);
suite_add_tcase(result, down);
return result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment