annotate http.c @ 1881:a7abdfdb5ed7 libavformat

handle http error codes
author alex
date Tue, 06 Mar 2007 13:38:41 +0000
parents c704f9d730bf
children 663139d56669
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
1 /*
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
2 * HTTP protocol for ffmpeg client
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
4 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
5 * This file is part of FFmpeg.
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
6 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
11 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
15 * Lesser General Public License for more details.
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
16 *
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
20 */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
21 #include "avformat.h"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
22 #include <unistd.h>
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
23 #include "network.h"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
24
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
25 #include "base64.h"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
26
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
27 /* XXX: POST protocol is not completly implemented because ffmpeg use
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
28 only a subset of it */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
29
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
30 //#define DEBUG
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
31
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
32 /* used for protocol handling */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
33 #define BUFFER_SIZE 1024
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
34 #define URL_SIZE 4096
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
35 #define MAX_REDIRECTS 8
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
36
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
37 typedef struct {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
38 URLContext *hd;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
39 unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
40 int line_count;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
41 int http_code;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
42 offset_t off, filesize;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
43 char location[URL_SIZE];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
44 } HTTPContext;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
45
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
46 static int http_connect(URLContext *h, const char *path, const char *hoststr,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
47 const char *auth, int *new_location);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
48 static int http_write(URLContext *h, uint8_t *buf, int size);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
49
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
50
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
51 /* return non zero if error */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
52 static int http_open_cnx(URLContext *h)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
53 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
54 const char *path, *proxy_path;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
55 char hostname[1024], hoststr[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
56 char auth[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
57 char path1[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
58 char buf[1024];
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
59 int port, use_proxy, err, location_changed = 0, redirects = 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
60 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
61 URLContext *hd = NULL;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
62
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
63 proxy_path = getenv("http_proxy");
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
64 use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
65 strstart(proxy_path, "http://", NULL);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
66
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
67 /* fill the dest addr */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
68 redo:
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
69 /* needed in any case to build the host string */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
70 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
71 path1, sizeof(path1), s->location);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
72 if (port > 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
73 snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
74 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
75 pstrcpy(hoststr, sizeof(hoststr), hostname);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
76 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
77
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
78 if (use_proxy) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
79 url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
80 NULL, 0, proxy_path);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
81 path = s->location;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
82 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
83 if (path1[0] == '\0')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
84 path = "/";
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
85 else
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
86 path = path1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
87 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
88 if (port < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
89 port = 80;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
90
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
91 snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
92 err = url_open(&hd, buf, URL_RDWR);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
93 if (err < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
94 goto fail;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
95
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
96 s->hd = hd;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
97 if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
98 goto fail;
1852
467a411b5d26 make HTTP MOVED messages also work in ffmpeg (302 is essentially the same as 303).
gpoirier
parents: 1822
diff changeset
99 if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
100 /* url moved, get next */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
101 url_close(hd);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
102 if (redirects++ >= MAX_REDIRECTS)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
103 return AVERROR_IO;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
104 location_changed = 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
105 goto redo;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
106 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
107 return 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
108 fail:
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
109 if (hd)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
110 url_close(hd);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
111 return AVERROR_IO;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
112 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
113
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
114 static int http_open(URLContext *h, const char *uri, int flags)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
115 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
116 HTTPContext *s;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
117 int ret;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
118
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
119 h->is_streamed = 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
120
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
121 s = av_malloc(sizeof(HTTPContext));
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
122 if (!s) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
123 return AVERROR(ENOMEM);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
124 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
125 h->priv_data = s;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
126 s->filesize = -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
127 s->off = 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
128 pstrcpy (s->location, URL_SIZE, uri);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
129
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
130 ret = http_open_cnx(h);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
131 if (ret != 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
132 av_free (s);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
133 return ret;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
134 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
135 static int http_getc(HTTPContext *s)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
136 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
137 int len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
138 if (s->buf_ptr >= s->buf_end) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
139 len = url_read(s->hd, s->buffer, BUFFER_SIZE);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
140 if (len < 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
141 return AVERROR_IO;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
142 } else if (len == 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
143 return -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
144 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
145 s->buf_ptr = s->buffer;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
146 s->buf_end = s->buffer + len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
147 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
148 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
149 return *s->buf_ptr++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
150 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
151
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
152 static int process_line(URLContext *h, char *line, int line_count,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
153 int *new_location)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
154 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
155 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
156 char *tag, *p;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
157
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
158 /* end of header */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
159 if (line[0] == '\0')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
160 return 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
161
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
162 p = line;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
163 if (line_count == 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
164 while (!isspace(*p) && *p != '\0')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
165 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
166 while (isspace(*p))
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
167 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
168 s->http_code = strtol(p, NULL, 10);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
169 #ifdef DEBUG
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
170 printf("http_code=%d\n", s->http_code);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
171 #endif
1881
a7abdfdb5ed7 handle http error codes
alex
parents: 1853
diff changeset
172 /* error codes are 4xx and 5xx */
a7abdfdb5ed7 handle http error codes
alex
parents: 1853
diff changeset
173 if (s->http_code >= 400 && s->http_code < 600)
a7abdfdb5ed7 handle http error codes
alex
parents: 1853
diff changeset
174 return -1;
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
175 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
176 while (*p != '\0' && *p != ':')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
177 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
178 if (*p != ':')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
179 return 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
180
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
181 *p = '\0';
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
182 tag = line;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
183 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
184 while (isspace(*p))
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
185 p++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
186 if (!strcmp(tag, "Location")) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
187 strcpy(s->location, p);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
188 *new_location = 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
189 } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
190 s->filesize = atoll(p);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
191 } else if (!strcmp (tag, "Content-Range")) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
192 /* "bytes $from-$to/$document_size" */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
193 const char *slash;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
194 if (!strncmp (p, "bytes ", 6)) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
195 p += 6;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
196 s->off = atoll(p);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
197 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
198 s->filesize = atoll(slash+1);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
199 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
200 h->is_streamed = 0; /* we _can_ in fact seek */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
201 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
202 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
203 return 1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
204 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
205
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
206 static int http_connect(URLContext *h, const char *path, const char *hoststr,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
207 const char *auth, int *new_location)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
208 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
209 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
210 int post, err, ch;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
211 char line[1024], *q;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
212 char *auth_b64;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
213 offset_t off = s->off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
214
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
215
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
216 /* send http header */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
217 post = h->flags & URL_WRONLY;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
218
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
219 auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
220 snprintf(s->buffer, sizeof(s->buffer),
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
221 "%s %s HTTP/1.1\r\n"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
222 "User-Agent: %s\r\n"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
223 "Accept: */*\r\n"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
224 "Range: bytes=%"PRId64"-\r\n"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
225 "Host: %s\r\n"
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
226 "Authorization: Basic %s\r\n"
1853
c704f9d730bf Close connection on EOF
gpoirier
parents: 1852
diff changeset
227 "Connection: close\r\n"
1822
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
228 "\r\n",
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
229 post ? "POST" : "GET",
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
230 path,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
231 LIBAVFORMAT_IDENT,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
232 s->off,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
233 hoststr,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
234 auth_b64);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
235
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
236 av_freep(&auth_b64);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
237 if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
238 return AVERROR_IO;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
239
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
240 /* init input buffer */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
241 s->buf_ptr = s->buffer;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
242 s->buf_end = s->buffer;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
243 s->line_count = 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
244 s->off = 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
245 if (post) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
246 sleep(1);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
247 return 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
248 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
249
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
250 /* wait for header */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
251 q = line;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
252 for(;;) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
253 ch = http_getc(s);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
254 if (ch < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
255 return AVERROR_IO;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
256 if (ch == '\n') {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
257 /* process line */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
258 if (q > line && q[-1] == '\r')
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
259 q--;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
260 *q = '\0';
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
261 #ifdef DEBUG
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
262 printf("header='%s'\n", line);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
263 #endif
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
264 err = process_line(h, line, s->line_count, new_location);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
265 if (err < 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
266 return err;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
267 if (err == 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
268 break;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
269 s->line_count++;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
270 q = line;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
271 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
272 if ((q - line) < sizeof(line) - 1)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
273 *q++ = ch;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
274 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
275 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
276
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
277 return (off == s->off) ? 0 : -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
278 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
279
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
280
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
281 static int http_read(URLContext *h, uint8_t *buf, int size)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
282 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
283 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
284 int len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
285
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
286 /* read bytes from input buffer first */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
287 len = s->buf_end - s->buf_ptr;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
288 if (len > 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
289 if (len > size)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
290 len = size;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
291 memcpy(buf, s->buf_ptr, len);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
292 s->buf_ptr += len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
293 } else {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
294 len = url_read(s->hd, buf, size);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
295 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
296 if (len > 0)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
297 s->off += len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
298 return len;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
299 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
300
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
301 /* used only when posting data */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
302 static int http_write(URLContext *h, uint8_t *buf, int size)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
303 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
304 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
305 return url_write(s->hd, buf, size);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
306 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
307
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
308 static int http_close(URLContext *h)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
309 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
310 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
311 url_close(s->hd);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
312 av_free(s);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
313 return 0;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
314 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
315
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
316 static offset_t http_seek(URLContext *h, offset_t off, int whence)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
317 {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
318 HTTPContext *s = h->priv_data;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
319 URLContext *old_hd = s->hd;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
320 offset_t old_off = s->off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
321
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
322 if (whence == AVSEEK_SIZE)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
323 return s->filesize;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
324 else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
325 return -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
326
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
327 /* we save the old context in case the seek fails */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
328 s->hd = NULL;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
329 if (whence == SEEK_CUR)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
330 off += s->off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
331 else if (whence == SEEK_END)
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
332 off += s->filesize;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
333 s->off = off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
334
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
335 /* if it fails, continue on old connection */
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
336 if (http_open_cnx(h) < 0) {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
337 s->hd = old_hd;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
338 s->off = old_off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
339 return -1;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
340 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
341 url_close(old_hd);
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
342 return off;
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
343 }
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
344
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
345 URLProtocol http_protocol = {
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
346 "http",
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
347 http_open,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
348 http_read,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
349 http_write,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
350 http_seek,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
351 http_close,
2526fc1e568b Reverting stray commit part II, r8156 had the base64 export patch mixed with the nutdec patch
lu_zero
parents:
diff changeset
352 };