-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-multipart.c
122 lines (99 loc) · 2.62 KB
/
test-multipart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* sane-airscan HTTP multipart decoder test
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*/
#include "airscan.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Print error message and exit
*/
void __attribute__((noreturn))
die (const char *format, ...)
{
va_list ap;
va_start(ap, format);
vprintf(format, ap);
printf("\n");
va_end(ap);
exit(1);
}
/* The main function
*/
int
main (int argc, char **argv)
{
const char *file;
FILE *fp;
long size;
int rc;
void *data;
error err;
http_client *client;
http_uri *uri;
http_query *q;
int i, cnt;
/* Parse command-line arguments */
if (argc != 2) {
die("usage: %s file", argv[0]);
}
file = argv[1];
/* Load the file */
fp = fopen(file, "rb");
if (fp == NULL) {
die("%s: %s", file, strerror(errno));
}
rc = fseek(fp, 0, SEEK_END);
if (rc < 0) {
die("%s: %s", file, strerror(errno));
}
size = ftell(fp);
if (size < 0) {
die("%s: %s", file, strerror(errno));
}
rc = fseek(fp, 0, SEEK_SET);
if (rc < 0) {
die("%s: %s", file, strerror(errno));
}
data = mem_new(char, size);
if ((size_t) size != fread(data, 1, size, fp)) {
die("%s: read error", file);
}
fclose(fp);
/* Initialize logging */
log_init();
conf.dbg_enabled = true;
log_configure();
/* Decode the image */
client = http_client_new(NULL, NULL);
uri = http_uri_new("http://localhost", false);
q = http_query_new(client, uri, "GET", NULL, NULL);
err = http_query_test_decode_response(q, data, size);
if (err != NULL) {
die("%s", ESTRING(err));
}
cnt = http_query_get_mp_response_count(q);
if (cnt > 0) {
printf("Part Size Saved As Content-Type\n");
printf("==== ==== ============= ============\n");
for (i = 0; i < cnt; i ++) {
http_data *data = http_query_get_mp_response_data(q, i);
char name[64];
FILE *fp;
sprintf(name, "%8.8d.part", i);
printf("%3d %8d %s %s\n", i, (int) data->size, name, data->content_type);
fp = fopen(name, "wb");
if (fp == NULL) {
die("%s: %s", strerror(errno));
}
fwrite(data->bytes, 1, data->size, fp);
fclose(fp);
}
}
return 0;
}
/* vim:ts=8:sw=4:et
*/