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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* testlibwebvi.c: unittest for webvi C bindings
*
* Copyright (c) 2010-2012 Antti Ajanki <antti.ajanki@iki.fi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <sys/select.h>
#include <errno.h>
#include "libwebvi.h"
#define WVTREFERENCE "wvt:///www.youtube.com/videopage.xsl?srcurl=http%3A//www.youtube.com/watch%3Fv%3Dk5LmKNYTqvk"
#define CHECK_WEBVI_CALL(err, funcname) \
if (err != WEBVIERR_OK) { \
fprintf(stderr, "%s FAILED: %s\n", funcname, webvi_strerror(ctx, err)); \
returncode = 127; \
goto cleanup; \
}
struct download_data {
long bytes_downloaded;
WebviCtx ctx;
WebviHandle handle;
};
ssize_t file_callback(const char *buf, size_t len, void *data) {
struct download_data *dldata = (struct download_data *)data;
if (dldata->bytes_downloaded == 0) {
char *url, *title, *contentType;
long contentLength;
if (webvi_get_info(dldata->ctx, dldata->handle, WEBVIINFO_URL, &url) != WEBVIERR_OK) {
fprintf(stderr, "webvi_get_info FAILED\n");
return -1;
}
if (url) {
printf("File URL: %s\n", url);
free(url);
}
if (webvi_get_info(dldata->ctx, dldata->handle, WEBVIINFO_STREAM_TITLE, &title) != WEBVIERR_OK) {
fprintf(stderr, "webvi_get_info FAILED\n");
return -1;
}
if (title) {
printf("Title: %s\n", title);
free(title);
}
if (webvi_get_info(dldata->ctx, dldata->handle, WEBVIINFO_CONTENT_TYPE, &contentType) != WEBVIERR_OK) {
fprintf(stderr, "webvi_get_info FAILED\n");
return -1;
}
if (contentType) {
printf("Content type: %s\n", contentType);
free(contentType);
}
if (webvi_get_info(dldata->ctx, dldata->handle, WEBVIINFO_CONTENT_LENGTH, &contentLength) != WEBVIERR_OK) {
fprintf(stderr, "webvi_get_info FAILED\n");
return -1;
}
printf("Content length: %ld\n", contentLength);
}
dldata->bytes_downloaded += len;
printf("\r%ld", dldata->bytes_downloaded);
return len;
}
int main(int argc, const char* argv[]) {
int returncode = 0;
WebviCtx ctx = 0;
WebviHandle handle = -1;
fd_set readfd, writefd, excfd;
int maxfd, fd, s, msg_remaining;
struct timeval timeout;
long running;
WebviMsg *donemsg;
int done;
struct download_data callback_data;
printf("Testing %s\n", webvi_version());
if (webvi_global_init() != 0) {
fprintf(stderr, "webvi_global_init FAILED\n");
return 127;
}
ctx = webvi_initialize_context();
if (ctx == 0) {
fprintf(stderr, "webvi_initialize_context FAILED\n");
returncode = 127;
goto cleanup;
}
CHECK_WEBVI_CALL(webvi_set_config(ctx, WEBVI_CONFIG_TEMPLATE_PATH, "../../templates"),
"webvi_set_config(WEBVI_CONFIG_TEMPLATE_PATH)");
handle = webvi_new_request(ctx, WVTREFERENCE, WEBVIREQ_FILE);
if (handle == -1) {
fprintf(stderr, "webvi_new_request FAILED\n");
returncode = 127;
goto cleanup;
}
callback_data.bytes_downloaded = 0;
callback_data.ctx = ctx;
callback_data.handle = handle;
CHECK_WEBVI_CALL(webvi_set_opt(ctx, handle, WEBVIOPT_WRITEDATA, &callback_data),
"webvi_set_opt(WEBVIOPT_WRITEDATA)");
CHECK_WEBVI_CALL(webvi_set_opt(ctx, handle, WEBVIOPT_WRITEFUNC, file_callback),
"webvi_set_opt(WEBVIOPT_WRITEFUNC)");
CHECK_WEBVI_CALL(webvi_start_handle(ctx, handle),
"webvi_start_handle");
done = 0;
do {
FD_ZERO(&readfd);
FD_ZERO(&writefd);
FD_ZERO(&excfd);
CHECK_WEBVI_CALL(webvi_fdset(ctx, &readfd, &writefd, &excfd, &maxfd),
"webvi_fdset");
timeout.tv_sec = 1;
timeout.tv_usec = 0;
s = select(maxfd+1, &readfd, &writefd, NULL, &timeout);
if (s < 0) {
if (errno == EINTR)
continue;
perror("select FAILED");
returncode = 127;
goto cleanup;
} if (s == 0) {
CHECK_WEBVI_CALL(webvi_perform(ctx, 0, WEBVI_SELECT_TIMEOUT, &running),
"webvi_perform");
} else {
for (fd=0; fd<=maxfd; fd++) {
if (FD_ISSET(fd, &readfd)) {
CHECK_WEBVI_CALL(webvi_perform(ctx, fd, WEBVI_SELECT_READ, &running),
"webvi_perform");
}
if (FD_ISSET(fd, &writefd)) {
CHECK_WEBVI_CALL(webvi_perform(ctx, fd, WEBVI_SELECT_WRITE, &running),
"webvi_perform");
}
}
}
do {
donemsg = webvi_get_message(ctx, &msg_remaining);
if (donemsg && donemsg->msg == WEBVIMSG_DONE && donemsg->handle == handle) {
done = 1;
}
} while (msg_remaining > 0);
} while (!done);
if (donemsg->status_code != 0) {
printf("Error: Unexpected status code %d\n", donemsg->status_code);
returncode = 127;
} else {
printf("\nRead %ld bytes.\n"
"Test successful.\n", callback_data.bytes_downloaded);
}
cleanup:
if (ctx != 0) {
if (handle != -1)
webvi_delete_handle(ctx, handle);
webvi_cleanup_context(ctx);
}
webvi_cleanup(1);
return returncode;
}
|