FFmpeg
Loading...
Searching...
No Matches
http_nobody.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Jun Zhao
3 *
4 * Regression tests for HTTP / httpproxy premature-end handling (issue #26071),
5 * unknown-length clean EOF and libcurl HEAD.
6 * Expected output lives in tests/ref/fate/http-head-no-body,
7 * tests/ref/fate/httpproxy-tunnel-eof and tests/ref/fate/http-curl-head.
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <inttypes.h>
27#include <stdio.h>
28#include <string.h>
29
30#include "config.h"
31
32#if HAVE_PTHREADS
33#include <pthread.h>
34
35#include "libavutil/dict.h"
36#include "libavutil/error.h"
37#include "libavutil/macros.h"
38#include "libavutil/time.h"
39#include "libavformat/avio.h"
40#include "libavformat/network.h"
41#include "libavformat/url.h"
42
43#if !HAVE_WINSOCK2_H && HAVE_SYS_TIME_H
44#include <sys/time.h>
45#endif
46
47#if HAVE_WINSOCK2_H
48#define SHUT_WRITE SD_SEND
49#define SOCKLEN int
50#else
51#define SHUT_WRITE SHUT_WR
52#define SOCKLEN socklen_t
53#endif
54
55#define BODY_SIZE 1150
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"
60
61/* HEAD reply declaring a resource length while legitimately sending no body. */
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";
68
69static const char lie_reply[] = GET_HEADER;
70static const char connect_reply[] = "HTTP/1.1 200 OK\r\nconnection: close\r\n\r\n";
71
72/* Unknown-length replies ending in a clean close: no Content-Length, so
73 * filesize stays UINT64_MAX. These must yield EOF, not a premature-end EIO. */
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";
78
79/* Chunked replies. Accept-Ranges makes the stream seekable, which is the
80 * path that used to report a clean close of an unfinished chunk as EOF.
81 * A finished message still ends at its terminating 0 chunk. */
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"
85 "5\r\nABC";
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"
89 "3\r\nABC\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"
93 "3\r\nABC\r\n0\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";
98
99/* HEAD reply for the libcurl protocol: a body-less response with a declared
100 * resource size, used to verify -method HEAD is no longer dropped. */
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";
112/* Lowercase "head" is a distinct method token, so it must carry its body.
113 * Omit Content-Length so AVSEEK_SIZE stays ENOSYS across libcurl versions
114 * that populate CURLINFO_CONTENT_LENGTH_DOWNLOAD_T at different points. */
115static const char curl_lc_head_reply[] =
116 "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nABC";
117
118/* CONNECT 200 header and initial tunnel payload sent together in an attempt
119 * to exercise forwarding of bytes buffered during header parsing, while still
120 * verifying full payload delivery across short transfers. */
121static const char coalesced_reply[] = "HTTP/1.1 200 OK\r\n\r\n" MARK;
122
123static char body_reply[sizeof(GET_HEADER) - 1 + BODY_SIZE];
124
125enum { GROUP_HTTP = 1, GROUP_PROXY = 2, GROUP_LIBCURL = 4 };
126enum { PRINT_NONE, PRINT_ENDS, PRINT_TEXT };
127
128typedef struct TestCase {
129 const char *name;
130 int group;
131 const char *url_fmt;
132 const char *method;
133 const char *reply;
134 int reply_len;
135 int payload_print;
136 int no_persist;
137} TestCase;
138
139typedef struct TestServer {
140 int fd, port, reply_len, failed;
141 const char *reply;
142 FFNetWakeup wakeup;
143 pthread_t thread;
144} TestServer;
145
146static void set_timeouts(int fd)
147{
148#if HAVE_WINSOCK2_H
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));
152#else
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));
156#endif
157}
158
159static int send_all(int fd, const char *buf, int len)
160{
161 for (int sent = 0; sent < len;) {
162 int r = send(fd, buf + sent, len - sent, MSG_NOSIGNAL);
163 if (r < 0 && ff_neterrno() == AVERROR(EINTR))
164 continue;
165 if (r <= 0)
166 return -1;
167 sent += r;
168 }
169 return 0;
170}
171
172static void *server_main(void *arg)
173{
174 TestServer *s = arg;
175 int64_t deadline = av_gettime_relative() + TIMEOUT_US;
176 char req[4096] = { 0 };
177 int c, w, n = 0;
178
179 do {
180 int64_t rem_ms = (deadline - av_gettime_relative() + 999) / 1000;
181 w = rem_ms > 0 ? ff_network_wait_fd_wakeup(s->fd, 0, (int)rem_ms, &s->wakeup)
182 : AVERROR(ETIMEDOUT);
183 } while (w == AVERROR(EINTR));
184
185 if (w < 0 || (c = accept(s->fd, NULL, NULL)) < 0) {
186 s->failed = 1;
187 return NULL;
188 }
189 set_timeouts(c);
190
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);
193 if (r < 0 && ff_neterrno() == AVERROR(EINTR))
194 continue;
195 if (r <= 0)
196 break;
197 req[n += r] = 0;
198 }
199
200 if (!strstr(req, "\r\n\r\n") || send_all(c, s->reply, s->reply_len) < 0)
201 s->failed = 1;
202 shutdown(c, SHUT_WRITE);
203 closesocket(c);
204 return NULL;
205}
206
207static int server_start(TestServer *s, const char *reply, int reply_len)
208{
209 struct sockaddr_in addr = { .sin_family = AF_INET };
210 SOCKLEN alen = sizeof(addr);
211 int on = 1;
212
213 memset(s, 0, sizeof(*s));
214 s->fd = -1;
215 s->reply = reply;
216 s->reply_len = reply_len;
217 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
218
219 if (ff_network_wakeup_open(&s->wakeup) < 0)
220 return -1;
221 if ((s->fd = ff_socket(AF_INET, SOCK_STREAM, 0, NULL)) < 0)
222 goto fail;
223 setsockopt(s->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
224
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)
228 goto fail;
229 s->port = ntohs(addr.sin_port);
230
231 if (!pthread_create(&s->thread, NULL, server_main, s))
232 return 0;
233fail:
234 if (s->fd >= 0)
235 closesocket(s->fd);
236 ff_network_wakeup_close(&s->wakeup);
237 return -1;
238}
239
240static void server_stop(TestServer *s)
241{
242 ff_network_wakeup_signal(&s->wakeup);
243 pthread_join(s->thread, NULL);
244 closesocket(s->fd);
245 ff_network_wakeup_close(&s->wakeup);
246}
247
248static int check_deadline(void *opaque)
249{
250 return av_gettime_relative() > *(const int64_t *)opaque;
251}
252
253static void format_status(char *dst, size_t dst_len, int64_t val)
254{
255 if (val == AVERROR_EOF)
256 snprintf(dst, dst_len, "EOF");
257 else if (val == AVERROR(EIO))
258 snprintf(dst, dst_len, "EIO");
259 else if (val == AVERROR(ENOSYS))
260 snprintf(dst, dst_len, "ENOSYS");
261 else
262 snprintf(dst, dst_len, "%"PRId64, val);
263}
264
265static void run_case(const TestCase *tc)
266{
267 TestServer srv;
268 URLContext *uc = NULL;
270 int64_t deadline = av_gettime_relative() + TIMEOUT_US, size_ret = 0;
271 AVIOInterruptCB int_cb = { check_deadline, &deadline };
272 char url[256], open_s[32], size_s[32], read_s[32];
273 uint8_t buf[4096];
274 int open_ret, read_ret = 0;
275
276 if (server_start(&srv, tc->reply, tc->reply_len) < 0) {
277 printf("%s: server_start failed\n", tc->name);
278 return;
279 }
280
281 snprintf(url, sizeof(url), tc->url_fmt, srv.port);
282 if (tc->group != GROUP_PROXY)
283 av_dict_set(&opts, "http_proxy", "", 0);
284 if (tc->group == GROUP_LIBCURL)
285 av_dict_set(&opts, "prefer_libcurl", "1", 0);
286 av_dict_set_int(&opts, "rw_timeout", TIMEOUT_US, 0);
287 if (tc->method)
288 av_dict_set(&opts, "method", tc->method, 0);
289 if (tc->no_persist)
290 av_dict_set(&opts, "multiple_requests", "0", 0);
291
292 open_ret = ffurl_open_whitelist(&uc, url, AVIO_FLAG_READ, &int_cb, &opts,
293 "http,httpproxy,tcp,libcurl", NULL, NULL);
295 if (open_ret >= 0) {
296 size_ret = ffurl_seek(uc, 0, AVSEEK_SIZE);
297 read_ret = ffurl_read_complete(uc, buf, sizeof(buf));
298 ffurl_closep(&uc);
299 }
300
301 server_stop(&srv);
302
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);
308
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);
313}
314
315static const TestCase cases[] = {
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 },
352};
353
354int main(int argc, char **argv)
355{
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;
361
362 if (!mask) {
363 fprintf(stderr, "Unknown test group: %s\n", group);
364 return 1;
365 }
366
367 memcpy(body_reply, GET_HEADER, sizeof(GET_HEADER) - 1);
368 memset(body_reply + sizeof(GET_HEADER) - 1, 'X', BODY_SIZE);
369
370 if (ff_network_init() < 0)
371 return 1;
372
373 for (size_t i = 0; i < FF_ARRAY_ELEMS(cases); i++)
374 if (cases[i].group & mask)
375 run_case(&cases[i]);
376
378 return 0;
379}
380#else
381int main(void)
382{
383 printf("SKIP no pthreads, loopback server unavailable\n");
384 return 0;
385}
386#endif
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static double val(void *priv, double ch)
Definition aeval.c:77
static AVDictionary * opts
static int run_case(int channels, int bps, int64_t bit_rate)
Definition auenc.c:29
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.
Definition avio.c:467
int ffurl_closep(URLContext **hh)
Close the resource accessed by the URLContext h, and free the memory used by it.
Definition avio.c:664
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.
Definition avio.c:633
Buffered I/O operations.
#define AVIO_FLAG_READ
read-only
Definition avio.h:630
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition avio.h:481
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
__device__ int printf(const char *,...)
Public dictionary API.
error code definitions
const AVIOInterruptCB int_cb
Definition ffmpeg.c:322
#define fail
Definition test.h:479
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:236
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition dict.c:87
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.
Definition dict.c:180
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
int main(void)
#define r
Definition input.c:42
const char * arg
Definition jacosubdec.c:66
uint8_t w
Definition llvidencdsp.c:39
static const uint16_t mask[17]
Definition lzw.c:38
Utility Preprocessor macros.
int ff_socket(int af, int type, int proto, void *logctx)
Definition network.c:306
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,...
Definition network.c:194
int ff_network_wakeup_open(FFNetWakeup *w)
Create a wakeup channel.
Definition network.c:106
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...
Definition network.c:162
void ff_network_wakeup_close(FFNetWakeup *w)
Close the channel.
Definition network.c:185
int ff_network_init(void)
Initialize the network subsystem.
Definition network.c:66
void ff_network_close(void)
Definition network.c:239
#define MSG_NOSIGNAL
Definition network.h:196
#define ff_neterrno()
Definition network.h:69
#define INADDR_LOOPBACK
Definition network.h:292
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition os2threads.h:94
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition os2threads.h:80
#define FF_ARRAY_ELEMS(a)
Callback for checking whether to abort blocking functions.
Definition avio.h:59
const char * name
static const TestCase cases[]
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
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.
Definition url.h:225
int len
static double c[64]