43#if !HAVE_WINSOCK2_H && HAVE_SYS_TIME_H
48#define SHUT_WRITE SD_SEND
51#define SHUT_WRITE SHUT_WR
52#define SOCKLEN socklen_t
56#define TIMEOUT_MS 5000
57#define TIMEOUT_US (TIMEOUT_MS * 1000LL)
58#define MARK "TUNNEL_PREFIX_BYTES_0123456789_ABCDEFGHIJ"
59#define GET_HEADER "HTTP/1.1 200 OK\r\nContent-Length: 1150\r\nConnection: close\r\n\r\n"
62static const char head_reply[] =
63 "HTTP/1.1 206 Partial Content\r\n"
64 "Content-Length: 1150\r\n"
65 "Content-Range: bytes 0-1149/1150\r\n"
66 "Accept-Ranges: bytes\r\n"
67 "Connection: close\r\n\r\n";
69static const char lie_reply[] = GET_HEADER;
70static const char connect_reply[] =
"HTTP/1.1 200 OK\r\nconnection: close\r\n\r\n";
74static const char close_reply[] =
75 "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n";
76static const char close_body_reply[] =
77 "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nABC";
82static const char chunk_short_reply[] =
83 "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
84 "Accept-Ranges: bytes\r\nConnection: close\r\n\r\n"
86static const char chunk_open_reply[] =
87 "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
88 "Accept-Ranges: bytes\r\nConnection: close\r\n\r\n"
90static const char chunk_trailer_reply[] =
91 "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
92 "Accept-Ranges: bytes\r\nConnection: close\r\n\r\n"
94static const char chunk_done_reply[] =
95 "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n"
96 "Accept-Ranges: bytes\r\nConnection: close\r\n\r\n"
97 "3\r\nABC\r\n0\r\n\r\n";
101static const char curl_head_reply[] =
102 "HTTP/1.1 200 OK\r\nContent-Length: 1150\r\nConnection: close\r\n\r\n";
103static const char curl_head_gzip_reply[] =
104 "HTTP/1.1 200 OK\r\nContent-Length: 1150\r\n"
105 "Content-Encoding: gzip\r\nConnection: close\r\n\r\n";
106static const char curl_head_pad_reply[] =
107 "HTTP/1.1 200 OK\r\n"
108 "Content-Length: 001150\r\n"
109 "Connection: close\r\n\r\n";
110static const char curl_head_zero_reply[] =
111 "HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
115static const char curl_lc_head_reply[] =
116 "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nABC";
121static const char coalesced_reply[] =
"HTTP/1.1 200 OK\r\n\r\n" MARK;
123static char body_reply[
sizeof(GET_HEADER) - 1 + BODY_SIZE];
125enum { GROUP_HTTP = 1, GROUP_PROXY = 2, GROUP_LIBCURL = 4 };
126enum { PRINT_NONE, PRINT_ENDS, PRINT_TEXT };
139typedef struct TestServer {
140 int fd, port, reply_len, failed;
146static void set_timeouts(
int fd)
149 DWORD ms = TIMEOUT_MS;
150 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (
const char *)&ms,
sizeof(ms));
151 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (
const char *)&ms,
sizeof(ms));
153 struct timeval tv = { TIMEOUT_MS / 1000, 0 };
154 setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
155 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv));
159static int send_all(
int fd,
const char *buf,
int len)
161 for (
int sent = 0; sent <
len;) {
172static void *server_main(
void *
arg)
176 char req[4096] = { 0 };
185 if (
w < 0 || (
c = accept(
s->fd,
NULL,
NULL)) < 0) {
191 while (n < (
int)
sizeof(req) - 1 && !strstr(req,
"\r\n\r\n")) {
192 int r = recv(
c, req + n,
sizeof(req) - 1 - n, 0);
200 if (!strstr(req,
"\r\n\r\n") || send_all(
c,
s->reply,
s->reply_len) < 0)
202 shutdown(
c, SHUT_WRITE);
207static int server_start(TestServer *
s,
const char *reply,
int reply_len)
209 struct sockaddr_in addr = { .sin_family = AF_INET };
210 SOCKLEN alen =
sizeof(addr);
213 memset(
s, 0,
sizeof(*
s));
216 s->reply_len = reply_len;
223 setsockopt(
s->fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on));
225 if (bind(
s->fd, (
const struct sockaddr *)&addr,
sizeof(addr)) < 0 ||
226 listen(
s->fd, 1) < 0 ||
227 getsockname(
s->fd, (
struct sockaddr *)&addr, &alen) < 0)
229 s->port = ntohs(addr.sin_port);
240static void server_stop(TestServer *
s)
248static int check_deadline(
void *opaque)
253static void format_status(
char *
dst,
size_t dst_len,
int64_t val)
256 snprintf(
dst, dst_len,
"EOF");
258 snprintf(
dst, dst_len,
"EIO");
260 snprintf(
dst, dst_len,
"ENOSYS");
262 snprintf(
dst, dst_len,
"%"PRId64,
val);
272 char url[256], open_s[32], size_s[32], read_s[32];
274 int open_ret, read_ret = 0;
276 if (server_start(&srv, tc->reply, tc->reply_len) < 0) {
277 printf(
"%s: server_start failed\n", tc->
name);
281 snprintf(url,
sizeof(url), tc->url_fmt, srv.port);
282 if (tc->group != GROUP_PROXY)
284 if (tc->group == GROUP_LIBCURL)
293 "http,httpproxy,tcp,libcurl",
NULL,
NULL);
303 format_status(open_s,
sizeof(open_s), open_ret);
304 format_status(size_s,
sizeof(size_s), size_ret);
305 format_status(read_s,
sizeof(read_s), read_ret);
306 printf(
"%s: open=%s size=%s read=%s server_failed=%d\n",
307 tc->
name, open_s, size_s, read_s, srv.failed);
309 if (read_ret > 0 && tc->payload_print == PRINT_ENDS)
310 printf(
"body ends: %c %c\n", buf[0], buf[read_ret - 1]);
311 else if (read_ret > 0 && tc->payload_print == PRINT_TEXT)
312 printf(
"residue: %.*s\n", read_ret, buf);
316 {
"head", GROUP_HTTP,
"http://127.0.0.1:%d/x",
317 "HEAD", head_reply,
sizeof(head_reply) - 1, PRINT_NONE },
318 {
"get_body", GROUP_HTTP,
"http://127.0.0.1:%d/x",
319 NULL, body_reply,
sizeof(body_reply), PRINT_ENDS },
320 {
"get_lie", GROUP_HTTP,
"http://127.0.0.1:%d/x",
321 NULL, lie_reply,
sizeof(lie_reply) - 1, PRINT_NONE },
322 {
"get_close", GROUP_HTTP,
"http://127.0.0.1:%d/x",
323 NULL, close_reply,
sizeof(close_reply) - 1, PRINT_NONE },
324 {
"get_close_body", GROUP_HTTP,
"http://127.0.0.1:%d/x",
325 NULL, close_body_reply,
sizeof(close_body_reply) - 1, PRINT_TEXT },
326 {
"chunk_short", GROUP_HTTP,
"http://127.0.0.1:%d/x",
327 NULL, chunk_short_reply,
sizeof(chunk_short_reply) - 1, PRINT_NONE },
328 {
"chunk_open", GROUP_HTTP,
"http://127.0.0.1:%d/x",
329 NULL, chunk_open_reply,
sizeof(chunk_open_reply) - 1, PRINT_NONE },
330 {
"chunk_trailer", GROUP_HTTP,
"http://127.0.0.1:%d/x",
331 NULL, chunk_trailer_reply,
sizeof(chunk_trailer_reply) - 1, PRINT_NONE },
332 {
"chunk_trailer_mr0", GROUP_HTTP,
"http://127.0.0.1:%d/x",
333 NULL, chunk_trailer_reply,
sizeof(chunk_trailer_reply) - 1, PRINT_NONE, 1 },
334 {
"chunk_done", GROUP_HTTP,
"http://127.0.0.1:%d/x",
335 NULL, chunk_done_reply,
sizeof(chunk_done_reply) - 1, PRINT_TEXT },
336 {
"chunk_done_mr0", GROUP_HTTP,
"http://127.0.0.1:%d/x",
337 NULL, chunk_done_reply,
sizeof(chunk_done_reply) - 1, PRINT_TEXT, 1 },
338 {
"proxy_close", GROUP_PROXY,
"httpproxy://127.0.0.1:%d/example.com:443",
339 NULL, connect_reply,
sizeof(connect_reply) - 1, PRINT_NONE },
340 {
"proxy_residue", GROUP_PROXY,
"httpproxy://127.0.0.1:%d/example.com:443",
341 NULL, coalesced_reply,
sizeof(coalesced_reply) - 1, PRINT_TEXT },
342 {
"curl_head", GROUP_LIBCURL,
"http://127.0.0.1:%d/x",
343 "HEAD", curl_head_reply,
sizeof(curl_head_reply) - 1, PRINT_NONE },
344 {
"curl_head_gzip", GROUP_LIBCURL,
"http://127.0.0.1:%d/x",
345 "HEAD", curl_head_gzip_reply,
sizeof(curl_head_gzip_reply) - 1, PRINT_NONE },
346 {
"curl_head_pad", GROUP_LIBCURL,
"http://127.0.0.1:%d/x",
347 "HEAD", curl_head_pad_reply,
sizeof(curl_head_pad_reply) - 1, PRINT_NONE },
348 {
"curl_head_zero", GROUP_LIBCURL,
"http://127.0.0.1:%d/x",
349 "HEAD", curl_head_zero_reply,
sizeof(curl_head_zero_reply) - 1, PRINT_NONE },
350 {
"curl_lc_head", GROUP_LIBCURL,
"http://127.0.0.1:%d/x",
351 "head", curl_lc_head_reply,
sizeof(curl_lc_head_reply) - 1, PRINT_TEXT },
354int main(
int argc,
char **argv)
356 const char *group = argc > 1 ? argv[1] :
"all";
357 int mask = !strcmp(group,
"all") ? (GROUP_HTTP | GROUP_PROXY) :
358 !strcmp(group,
"http") ? GROUP_HTTP :
359 !strcmp(group,
"proxy") ? GROUP_PROXY :
360 !strcmp(group,
"libcurl") ? GROUP_LIBCURL : 0;
363 fprintf(stderr,
"Unknown test group: %s\n", group);
367 memcpy(body_reply, GET_HEADER,
sizeof(GET_HEADER) - 1);
368 memset(body_reply +
sizeof(GET_HEADER) - 1,
'X', BODY_SIZE);
383 printf(
"SKIP no pthreads, loopback server unavailable\n");
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static double val(void *priv, double ch)
static AVDictionary * opts
static int run_case(int channels, int bps, int64_t bit_rate)
int ffurl_open_whitelist(URLContext **puc, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options, const char *whitelist, const char *blacklist, URLContext *parent)
Create an URLContext for accessing to the resource indicated by url, and open it.
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
int ffurl_read_complete(URLContext *h, unsigned char *buf, int size)
Read as many bytes as possible (up to size), calling the read function multiple times if necessary.
#define AVIO_FLAG_READ
read-only
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
#define i(width, name, range_min, range_max)
__device__ int printf(const char *,...)
const AVIOInterruptCB int_cb
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
#define AVERROR_EOF
End of file.
static const uint16_t mask[17]
Utility Preprocessor macros.
int ff_socket(int af, int type, int proto, void *logctx)
int ff_network_wait_fd_wakeup(int fd, int write, int timeout_ms, FFNetWakeup *w)
Like ff_network_wait_fd(), but also returns as soon as the wakeup channel is signaled,...
int ff_network_wakeup_open(FFNetWakeup *w)
Create a wakeup channel.
int ff_network_wakeup_signal(FFNetWakeup *w)
Wake the thread blocked in ff_network_wait_fd_wakeup() on this channel, or the next one that waits on...
void ff_network_wakeup_close(FFNetWakeup *w)
Close the channel.
int ff_network_init(void)
Initialize the network subsystem.
void ff_network_close(void)
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
#define FF_ARRAY_ELEMS(a)
Callback for checking whether to abort blocking functions.
static const TestCase cases[]
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
unbuffered private I/O API
static int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
Change the position that will be used by the next read/write operation on the resource accessed by h.