]> git.sesse.net Git - ffmpeg/blob - libavformat/http.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavformat / http.c
1 /*
2  * HTTP protocol for ffmpeg client
3  * Copyright (c) 2000, 2001 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/avstring.h"
23 #include "avformat.h"
24 #include <unistd.h>
25 #include "internal.h"
26 #include "network.h"
27 #include "http.h"
28 #include "os_support.h"
29 #include "httpauth.h"
30 #include "url.h"
31 #include "libavutil/opt.h"
32
33 /* XXX: POST protocol is not completely implemented because ffmpeg uses
34    only a subset of it. */
35
36 /* used for protocol handling */
37 #define BUFFER_SIZE 1024
38 #define MAX_REDIRECTS 8
39
40 typedef struct {
41     const AVClass *class;
42     URLContext *hd;
43     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
44     int line_count;
45     int http_code;
46     int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
47     char *user_agent;
48     int64_t off, filesize;
49     char location[MAX_URL_SIZE];
50     HTTPAuthState auth_state;
51     HTTPAuthState proxy_auth_state;
52     char *headers;
53     int willclose;          /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
54     int chunked_post;
55 } HTTPContext;
56
57 #define OFFSET(x) offsetof(HTTPContext, x)
58 #define D AV_OPT_FLAG_DECODING_PARAM
59 #define E AV_OPT_FLAG_ENCODING_PARAM
60 #define DEC AV_OPT_FLAG_DECODING_PARAM
61 static const AVOption options[] = {
62 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, E },
63 {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
64 {"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
65 {NULL}
66 };
67 #define HTTP_CLASS(flavor)\
68 static const AVClass flavor ## _context_class = {\
69     .class_name     = #flavor,\
70     .item_name      = av_default_item_name,\
71     .option         = options,\
72     .version        = LIBAVUTIL_VERSION_INT,\
73 }
74
75 HTTP_CLASS(http);
76 HTTP_CLASS(https);
77
78 static int http_connect(URLContext *h, const char *path, const char *local_path,
79                         const char *hoststr, const char *auth,
80                         const char *proxyauth, int *new_location);
81
82 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
83 {
84     memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
85            &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
86     memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
87            &((HTTPContext*)src->priv_data)->proxy_auth_state,
88            sizeof(HTTPAuthState));
89 }
90
91 /* return non zero if error */
92 static int http_open_cnx(URLContext *h)
93 {
94     const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
95     char hostname[1024], hoststr[1024], proto[10];
96     char auth[1024], proxyauth[1024] = "";
97     char path1[1024];
98     char buf[1024], urlbuf[1024];
99     int port, use_proxy, err, location_changed = 0, redirects = 0, attempts = 0;
100     HTTPAuthType cur_auth_type, cur_proxy_auth_type;
101     HTTPContext *s = h->priv_data;
102     URLContext *hd = NULL;
103
104     proxy_path = getenv("http_proxy");
105     use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
106         av_strstart(proxy_path, "http://", NULL);
107
108     /* fill the dest addr */
109  redo:
110     /* needed in any case to build the host string */
111     av_url_split(proto, sizeof(proto), auth, sizeof(auth),
112                  hostname, sizeof(hostname), &port,
113                  path1, sizeof(path1), s->location);
114     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
115
116     if (!strcmp(proto, "https")) {
117         lower_proto = "tls";
118         use_proxy = 0;
119         if (port < 0)
120             port = 443;
121     }
122     if (port < 0)
123         port = 80;
124
125     if (path1[0] == '\0')
126         path = "/";
127     else
128         path = path1;
129     local_path = path;
130     if (use_proxy) {
131         /* Reassemble the request URL without auth string - we don't
132          * want to leak the auth to the proxy. */
133         ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
134                     path1);
135         path = urlbuf;
136         av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
137                      hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
138     }
139
140     ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
141     err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE,
142                      &h->interrupt_callback, NULL);
143     if (err < 0)
144         goto fail;
145
146     s->hd = hd;
147     cur_auth_type = s->auth_state.auth_type;
148     cur_proxy_auth_type = s->auth_state.auth_type;
149     if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
150         goto fail;
151     attempts++;
152     if (s->http_code == 401) {
153         if ((cur_auth_type == HTTP_AUTH_NONE || s->auth_state.stale) &&
154             s->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
155             ffurl_close(hd);
156             goto redo;
157         } else
158             goto fail;
159     }
160     if (s->http_code == 407) {
161         if ((cur_proxy_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
162             s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 4) {
163             ffurl_close(hd);
164             goto redo;
165         } else
166             goto fail;
167     }
168     if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
169         && location_changed == 1) {
170         /* url moved, get next */
171         ffurl_close(hd);
172         if (redirects++ >= MAX_REDIRECTS)
173             return AVERROR(EIO);
174         /* Restart the authentication process with the new target, which
175          * might use a different auth mechanism. */
176         memset(&s->auth_state, 0, sizeof(s->auth_state));
177         attempts = 0;
178         location_changed = 0;
179         goto redo;
180     }
181     return 0;
182  fail:
183     if (hd)
184         ffurl_close(hd);
185     s->hd = NULL;
186     return AVERROR(EIO);
187 }
188
189 static int http_open(URLContext *h, const char *uri, int flags)
190 {
191     HTTPContext *s = h->priv_data;
192
193     h->is_streamed = 1;
194
195     s->filesize = -1;
196     av_strlcpy(s->location, uri, sizeof(s->location));
197
198     if (s->headers) {
199         int len = strlen(s->headers);
200         if (len < 2 || strcmp("\r\n", s->headers + len - 2))
201             av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
202     }
203
204     return http_open_cnx(h);
205 }
206 static int http_getc(HTTPContext *s)
207 {
208     int len;
209     if (s->buf_ptr >= s->buf_end) {
210         len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
211         if (len < 0) {
212             return AVERROR(EIO);
213         } else if (len == 0) {
214             return -1;
215         } else {
216             s->buf_ptr = s->buffer;
217             s->buf_end = s->buffer + len;
218         }
219     }
220     return *s->buf_ptr++;
221 }
222
223 static int http_get_line(HTTPContext *s, char *line, int line_size)
224 {
225     int ch;
226     char *q;
227
228     q = line;
229     for(;;) {
230         ch = http_getc(s);
231         if (ch < 0)
232             return AVERROR(EIO);
233         if (ch == '\n') {
234             /* process line */
235             if (q > line && q[-1] == '\r')
236                 q--;
237             *q = '\0';
238
239             return 0;
240         } else {
241             if ((q - line) < line_size - 1)
242                 *q++ = ch;
243         }
244     }
245 }
246
247 static int process_line(URLContext *h, char *line, int line_count,
248                         int *new_location)
249 {
250     HTTPContext *s = h->priv_data;
251     char *tag, *p, *end;
252
253     /* end of header */
254     if (line[0] == '\0')
255         return 0;
256
257     p = line;
258     if (line_count == 0) {
259         while (!isspace(*p) && *p != '\0')
260             p++;
261         while (isspace(*p))
262             p++;
263         s->http_code = strtol(p, &end, 10);
264
265         av_dlog(NULL, "http_code=%d\n", s->http_code);
266
267         /* error codes are 4xx and 5xx, but regard 401 as a success, so we
268          * don't abort until all headers have been parsed. */
269         if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401
270             || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
271             (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
272             end += strspn(end, SPACE_CHARS);
273             av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
274                    s->http_code, end);
275             return -1;
276         }
277     } else {
278         while (*p != '\0' && *p != ':')
279             p++;
280         if (*p != ':')
281             return 1;
282
283         *p = '\0';
284         tag = line;
285         p++;
286         while (isspace(*p))
287             p++;
288         if (!av_strcasecmp(tag, "Location")) {
289             strcpy(s->location, p);
290             *new_location = 1;
291         } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
292             s->filesize = atoll(p);
293         } else if (!av_strcasecmp (tag, "Content-Range")) {
294             /* "bytes $from-$to/$document_size" */
295             const char *slash;
296             if (!strncmp (p, "bytes ", 6)) {
297                 p += 6;
298                 s->off = atoll(p);
299                 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
300                     s->filesize = atoll(slash+1);
301             }
302             h->is_streamed = 0; /* we _can_ in fact seek */
303         } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5)) {
304             h->is_streamed = 0;
305         } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
306             s->filesize = -1;
307             s->chunksize = 0;
308         } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
309             ff_http_auth_handle_header(&s->auth_state, tag, p);
310         } else if (!av_strcasecmp (tag, "Authentication-Info")) {
311             ff_http_auth_handle_header(&s->auth_state, tag, p);
312         } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) {
313             ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
314         } else if (!av_strcasecmp (tag, "Connection")) {
315             if (!strcmp(p, "close"))
316                 s->willclose = 1;
317         }
318     }
319     return 1;
320 }
321
322 static inline int has_header(const char *str, const char *header)
323 {
324     /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
325     if (!str)
326         return 0;
327     return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
328 }
329
330 static int http_connect(URLContext *h, const char *path, const char *local_path,
331                         const char *hoststr, const char *auth,
332                         const char *proxyauth, int *new_location)
333 {
334     HTTPContext *s = h->priv_data;
335     int post, err;
336     char line[1024];
337     char headers[1024] = "";
338     char *authstr = NULL, *proxyauthstr = NULL;
339     int64_t off = s->off;
340     int len = 0;
341     const char *method;
342
343
344     /* send http header */
345     post = h->flags & AVIO_FLAG_WRITE;
346     method = post ? "POST" : "GET";
347     authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
348                                            method);
349     proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
350                                                 local_path, method);
351
352     /* set default headers if needed */
353     if (!has_header(s->headers, "\r\nUser-Agent: "))
354         len += av_strlcatf(headers + len, sizeof(headers) - len,
355                            "User-Agent: %s\r\n",
356                            s->user_agent ? s->user_agent : LIBAVFORMAT_IDENT);
357     if (!has_header(s->headers, "\r\nAccept: "))
358         len += av_strlcpy(headers + len, "Accept: */*\r\n",
359                           sizeof(headers) - len);
360     if (!has_header(s->headers, "\r\nRange: ") && !post)
361         len += av_strlcatf(headers + len, sizeof(headers) - len,
362                            "Range: bytes=%"PRId64"-\r\n", s->off);
363     if (!has_header(s->headers, "\r\nConnection: "))
364         len += av_strlcpy(headers + len, "Connection: close\r\n",
365                           sizeof(headers)-len);
366     if (!has_header(s->headers, "\r\nHost: "))
367         len += av_strlcatf(headers + len, sizeof(headers) - len,
368                            "Host: %s\r\n", hoststr);
369
370     /* now add in custom headers */
371     if (s->headers)
372         av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
373
374     snprintf(s->buffer, sizeof(s->buffer),
375              "%s %s HTTP/1.1\r\n"
376              "%s"
377              "%s"
378              "%s"
379              "%s%s"
380              "\r\n",
381              method,
382              path,
383              post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
384              headers,
385              authstr ? authstr : "",
386              proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
387
388     av_freep(&authstr);
389     av_freep(&proxyauthstr);
390     if (ffurl_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
391         return AVERROR(EIO);
392
393     /* init input buffer */
394     s->buf_ptr = s->buffer;
395     s->buf_end = s->buffer;
396     s->line_count = 0;
397     s->off = 0;
398     s->filesize = -1;
399     s->willclose = 0;
400     if (post) {
401         /* Pretend that it did work. We didn't read any header yet, since
402          * we've still to send the POST data, but the code calling this
403          * function will check http_code after we return. */
404         s->http_code = 200;
405         return 0;
406     }
407     s->chunksize = -1;
408
409     /* wait for header */
410     for(;;) {
411         if (http_get_line(s, line, sizeof(line)) < 0)
412             return AVERROR(EIO);
413
414         av_dlog(NULL, "header='%s'\n", line);
415
416         err = process_line(h, line, s->line_count, new_location);
417         if (err < 0)
418             return err;
419         if (err == 0)
420             break;
421         s->line_count++;
422     }
423
424     return (off == s->off) ? 0 : -1;
425 }
426
427
428 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
429 {
430     HTTPContext *s = h->priv_data;
431     int len;
432     /* read bytes from input buffer first */
433     len = s->buf_end - s->buf_ptr;
434     if (len > 0) {
435         if (len > size)
436             len = size;
437         memcpy(buf, s->buf_ptr, len);
438         s->buf_ptr += len;
439     } else {
440         if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
441             return AVERROR_EOF;
442         len = ffurl_read(s->hd, buf, size);
443     }
444     if (len > 0) {
445         s->off += len;
446         if (s->chunksize > 0)
447             s->chunksize -= len;
448     }
449     return len;
450 }
451
452 static int http_read(URLContext *h, uint8_t *buf, int size)
453 {
454     HTTPContext *s = h->priv_data;
455
456     if (s->chunksize >= 0) {
457         if (!s->chunksize) {
458             char line[32];
459
460             for(;;) {
461                 do {
462                     if (http_get_line(s, line, sizeof(line)) < 0)
463                         return AVERROR(EIO);
464                 } while (!*line);    /* skip CR LF from last chunk */
465
466                 s->chunksize = strtoll(line, NULL, 16);
467
468                 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
469
470                 if (!s->chunksize)
471                     return 0;
472                 break;
473             }
474         }
475         size = FFMIN(size, s->chunksize);
476     }
477     return http_buf_read(h, buf, size);
478 }
479
480 /* used only when posting data */
481 static int http_write(URLContext *h, const uint8_t *buf, int size)
482 {
483     char temp[11] = "";  /* 32-bit hex + CRLF + nul */
484     int ret;
485     char crlf[] = "\r\n";
486     HTTPContext *s = h->priv_data;
487
488     if (!s->chunked_post) {
489         /* non-chunked data is sent without any special encoding */
490         return ffurl_write(s->hd, buf, size);
491     }
492
493     /* silently ignore zero-size data since chunk encoding that would
494      * signal EOF */
495     if (size > 0) {
496         /* upload data using chunked encoding */
497         snprintf(temp, sizeof(temp), "%x\r\n", size);
498
499         if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
500             (ret = ffurl_write(s->hd, buf, size)) < 0 ||
501             (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
502             return ret;
503     }
504     return size;
505 }
506
507 static int http_close(URLContext *h)
508 {
509     int ret = 0;
510     char footer[] = "0\r\n\r\n";
511     HTTPContext *s = h->priv_data;
512
513     /* signal end of chunked encoding if used */
514     if ((h->flags & AVIO_FLAG_WRITE) && s->chunked_post) {
515         ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
516         ret = ret > 0 ? 0 : ret;
517     }
518
519     if (s->hd)
520         ffurl_close(s->hd);
521     return ret;
522 }
523
524 static int64_t http_seek(URLContext *h, int64_t off, int whence)
525 {
526     HTTPContext *s = h->priv_data;
527     URLContext *old_hd = s->hd;
528     int64_t old_off = s->off;
529     uint8_t old_buf[BUFFER_SIZE];
530     int old_buf_size;
531
532     if (whence == AVSEEK_SIZE)
533         return s->filesize;
534     else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
535         return -1;
536
537     /* we save the old context in case the seek fails */
538     old_buf_size = s->buf_end - s->buf_ptr;
539     memcpy(old_buf, s->buf_ptr, old_buf_size);
540     s->hd = NULL;
541     if (whence == SEEK_CUR)
542         off += s->off;
543     else if (whence == SEEK_END)
544         off += s->filesize;
545     s->off = off;
546
547     /* if it fails, continue on old connection */
548     if (http_open_cnx(h) < 0) {
549         memcpy(s->buffer, old_buf, old_buf_size);
550         s->buf_ptr = s->buffer;
551         s->buf_end = s->buffer + old_buf_size;
552         s->hd = old_hd;
553         s->off = old_off;
554         return -1;
555     }
556     ffurl_close(old_hd);
557     return off;
558 }
559
560 static int
561 http_get_file_handle(URLContext *h)
562 {
563     HTTPContext *s = h->priv_data;
564     return ffurl_get_file_handle(s->hd);
565 }
566
567 #if CONFIG_HTTP_PROTOCOL
568 URLProtocol ff_http_protocol = {
569     .name                = "http",
570     .url_open            = http_open,
571     .url_read            = http_read,
572     .url_write           = http_write,
573     .url_seek            = http_seek,
574     .url_close           = http_close,
575     .url_get_file_handle = http_get_file_handle,
576     .priv_data_size      = sizeof(HTTPContext),
577     .priv_data_class     = &http_context_class,
578     .flags               = URL_PROTOCOL_FLAG_NETWORK,
579 };
580 #endif
581 #if CONFIG_HTTPS_PROTOCOL
582 URLProtocol ff_https_protocol = {
583     .name                = "https",
584     .url_open            = http_open,
585     .url_read            = http_read,
586     .url_write           = http_write,
587     .url_seek            = http_seek,
588     .url_close           = http_close,
589     .url_get_file_handle = http_get_file_handle,
590     .priv_data_size      = sizeof(HTTPContext),
591     .priv_data_class     = &https_context_class,
592     .flags               = URL_PROTOCOL_FLAG_NETWORK,
593 };
594 #endif
595
596 #if CONFIG_HTTPPROXY_PROTOCOL
597 static int http_proxy_close(URLContext *h)
598 {
599     HTTPContext *s = h->priv_data;
600     if (s->hd)
601         ffurl_close(s->hd);
602     return 0;
603 }
604
605 static int http_proxy_open(URLContext *h, const char *uri, int flags)
606 {
607     HTTPContext *s = h->priv_data;
608     char hostname[1024], hoststr[1024];
609     char auth[1024], pathbuf[1024], *path;
610     char line[1024], lower_url[100];
611     int port, ret = 0, attempts = 0;
612     HTTPAuthType cur_auth_type;
613     char *authstr;
614
615     h->is_streamed = 1;
616
617     av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
618                  pathbuf, sizeof(pathbuf), uri);
619     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
620     path = pathbuf;
621     if (*path == '/')
622         path++;
623
624     ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
625                 NULL);
626 redo:
627     ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
628                      &h->interrupt_callback, NULL);
629     if (ret < 0)
630         return ret;
631
632     authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
633                                            path, "CONNECT");
634     snprintf(s->buffer, sizeof(s->buffer),
635              "CONNECT %s HTTP/1.1\r\n"
636              "Host: %s\r\n"
637              "Connection: close\r\n"
638              "%s%s"
639              "\r\n",
640              path,
641              hoststr,
642              authstr ? "Proxy-" : "", authstr ? authstr : "");
643     av_freep(&authstr);
644
645     if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
646         goto fail;
647
648     s->buf_ptr = s->buffer;
649     s->buf_end = s->buffer;
650     s->line_count = 0;
651     s->filesize = -1;
652     cur_auth_type = s->proxy_auth_state.auth_type;
653
654     for (;;) {
655         int new_loc;
656         // Note: This uses buffering, potentially reading more than the
657         // HTTP header. If tunneling a protocol where the server starts
658         // the conversation, we might buffer part of that here, too.
659         // Reading that requires using the proper ffurl_read() function
660         // on this URLContext, not using the fd directly (as the tls
661         // protocol does). This shouldn't be an issue for tls though,
662         // since the client starts the conversation there, so there
663         // is no extra data that we might buffer up here.
664         if (http_get_line(s, line, sizeof(line)) < 0) {
665             ret = AVERROR(EIO);
666             goto fail;
667         }
668
669         av_dlog(h, "header='%s'\n", line);
670
671         ret = process_line(h, line, s->line_count, &new_loc);
672         if (ret < 0)
673             goto fail;
674         if (ret == 0)
675             break;
676         s->line_count++;
677     }
678     attempts++;
679     if (s->http_code == 407 &&
680         (cur_auth_type == HTTP_AUTH_NONE || s->proxy_auth_state.stale) &&
681         s->proxy_auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2) {
682         ffurl_close(s->hd);
683         s->hd = NULL;
684         goto redo;
685     }
686
687     if (s->http_code < 400)
688         return 0;
689     ret = AVERROR(EIO);
690
691 fail:
692     http_proxy_close(h);
693     return ret;
694 }
695
696 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
697 {
698     HTTPContext *s = h->priv_data;
699     return ffurl_write(s->hd, buf, size);
700 }
701
702 URLProtocol ff_httpproxy_protocol = {
703     .name                = "httpproxy",
704     .url_open            = http_proxy_open,
705     .url_read            = http_buf_read,
706     .url_write           = http_proxy_write,
707     .url_close           = http_proxy_close,
708     .url_get_file_handle = http_get_file_handle,
709     .priv_data_size      = sizeof(HTTPContext),
710     .flags               = URL_PROTOCOL_FLAG_NETWORK,
711 };
712 #endif