You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
10KB

  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. #include "libavutil/base64.h"
  22. #include "libavutil/avstring.h"
  23. #include "avformat.h"
  24. #include <unistd.h>
  25. #include <strings.h>
  26. #include "network.h"
  27. #include "os_support.h"
  28. /* XXX: POST protocol is not completely implemented because ffmpeg uses
  29. only a subset of it. */
  30. /* used for protocol handling */
  31. #define BUFFER_SIZE 1024
  32. #define URL_SIZE 4096
  33. #define MAX_REDIRECTS 8
  34. typedef struct {
  35. URLContext *hd;
  36. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  37. int line_count;
  38. int http_code;
  39. int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
  40. int64_t off, filesize;
  41. char location[URL_SIZE];
  42. } HTTPContext;
  43. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  44. const char *auth, int *new_location);
  45. static int http_write(URLContext *h, uint8_t *buf, int size);
  46. /* return non zero if error */
  47. static int http_open_cnx(URLContext *h)
  48. {
  49. const char *path, *proxy_path;
  50. char hostname[1024], hoststr[1024];
  51. char auth[1024];
  52. char path1[1024];
  53. char buf[1024];
  54. int port, use_proxy, err, location_changed = 0, redirects = 0;
  55. HTTPContext *s = h->priv_data;
  56. URLContext *hd = NULL;
  57. proxy_path = getenv("http_proxy");
  58. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  59. av_strstart(proxy_path, "http://", NULL);
  60. /* fill the dest addr */
  61. redo:
  62. /* needed in any case to build the host string */
  63. url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  64. path1, sizeof(path1), s->location);
  65. if (port > 0) {
  66. snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
  67. } else {
  68. av_strlcpy(hoststr, hostname, sizeof(hoststr));
  69. }
  70. if (use_proxy) {
  71. url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  72. NULL, 0, proxy_path);
  73. path = s->location;
  74. } else {
  75. if (path1[0] == '\0')
  76. path = "/";
  77. else
  78. path = path1;
  79. }
  80. if (port < 0)
  81. port = 80;
  82. snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
  83. err = url_open(&hd, buf, URL_RDWR);
  84. if (err < 0)
  85. goto fail;
  86. s->hd = hd;
  87. if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
  88. goto fail;
  89. if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
  90. /* url moved, get next */
  91. url_close(hd);
  92. if (redirects++ >= MAX_REDIRECTS)
  93. return AVERROR(EIO);
  94. location_changed = 0;
  95. goto redo;
  96. }
  97. return 0;
  98. fail:
  99. if (hd)
  100. url_close(hd);
  101. return AVERROR(EIO);
  102. }
  103. static int http_open(URLContext *h, const char *uri, int flags)
  104. {
  105. HTTPContext *s;
  106. int ret;
  107. h->is_streamed = 1;
  108. s = av_malloc(sizeof(HTTPContext));
  109. if (!s) {
  110. return AVERROR(ENOMEM);
  111. }
  112. h->priv_data = s;
  113. s->filesize = -1;
  114. s->chunksize = -1;
  115. s->off = 0;
  116. av_strlcpy(s->location, uri, URL_SIZE);
  117. ret = http_open_cnx(h);
  118. if (ret != 0)
  119. av_free (s);
  120. return ret;
  121. }
  122. static int http_getc(HTTPContext *s)
  123. {
  124. int len;
  125. if (s->buf_ptr >= s->buf_end) {
  126. len = url_read(s->hd, s->buffer, BUFFER_SIZE);
  127. if (len < 0) {
  128. return AVERROR(EIO);
  129. } else if (len == 0) {
  130. return -1;
  131. } else {
  132. s->buf_ptr = s->buffer;
  133. s->buf_end = s->buffer + len;
  134. }
  135. }
  136. return *s->buf_ptr++;
  137. }
  138. static int http_get_line(HTTPContext *s, char *line, int line_size)
  139. {
  140. int ch;
  141. char *q;
  142. q = line;
  143. for(;;) {
  144. ch = http_getc(s);
  145. if (ch < 0)
  146. return AVERROR(EIO);
  147. if (ch == '\n') {
  148. /* process line */
  149. if (q > line && q[-1] == '\r')
  150. q--;
  151. *q = '\0';
  152. return 0;
  153. } else {
  154. if ((q - line) < line_size - 1)
  155. *q++ = ch;
  156. }
  157. }
  158. }
  159. static int process_line(URLContext *h, char *line, int line_count,
  160. int *new_location)
  161. {
  162. HTTPContext *s = h->priv_data;
  163. char *tag, *p;
  164. /* end of header */
  165. if (line[0] == '\0')
  166. return 0;
  167. p = line;
  168. if (line_count == 0) {
  169. while (!isspace(*p) && *p != '\0')
  170. p++;
  171. while (isspace(*p))
  172. p++;
  173. s->http_code = strtol(p, NULL, 10);
  174. dprintf(NULL, "http_code=%d\n", s->http_code);
  175. /* error codes are 4xx and 5xx */
  176. if (s->http_code >= 400 && s->http_code < 600)
  177. return -1;
  178. } else {
  179. while (*p != '\0' && *p != ':')
  180. p++;
  181. if (*p != ':')
  182. return 1;
  183. *p = '\0';
  184. tag = line;
  185. p++;
  186. while (isspace(*p))
  187. p++;
  188. if (!strcmp(tag, "Location")) {
  189. strcpy(s->location, p);
  190. *new_location = 1;
  191. } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
  192. s->filesize = atoll(p);
  193. } else if (!strcmp (tag, "Content-Range")) {
  194. /* "bytes $from-$to/$document_size" */
  195. const char *slash;
  196. if (!strncmp (p, "bytes ", 6)) {
  197. p += 6;
  198. s->off = atoll(p);
  199. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  200. s->filesize = atoll(slash+1);
  201. }
  202. h->is_streamed = 0; /* we _can_ in fact seek */
  203. } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
  204. s->filesize = -1;
  205. s->chunksize = 0;
  206. }
  207. }
  208. return 1;
  209. }
  210. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  211. const char *auth, int *new_location)
  212. {
  213. HTTPContext *s = h->priv_data;
  214. int post, err;
  215. char line[1024];
  216. char *auth_b64;
  217. int auth_b64_len = (strlen(auth) + 2) / 3 * 4 + 1;
  218. int64_t off = s->off;
  219. /* send http header */
  220. post = h->flags & URL_WRONLY;
  221. auth_b64 = av_malloc(auth_b64_len);
  222. av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));
  223. snprintf(s->buffer, sizeof(s->buffer),
  224. "%s %s HTTP/1.1\r\n"
  225. "User-Agent: %s\r\n"
  226. "Accept: */*\r\n"
  227. "Range: bytes=%"PRId64"-\r\n"
  228. "Host: %s\r\n"
  229. "Authorization: Basic %s\r\n"
  230. "Connection: close\r\n"
  231. "\r\n",
  232. post ? "POST" : "GET",
  233. path,
  234. LIBAVFORMAT_IDENT,
  235. s->off,
  236. hoststr,
  237. auth_b64);
  238. av_freep(&auth_b64);
  239. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  240. return AVERROR(EIO);
  241. /* init input buffer */
  242. s->buf_ptr = s->buffer;
  243. s->buf_end = s->buffer;
  244. s->line_count = 0;
  245. s->off = 0;
  246. s->filesize = -1;
  247. if (post) {
  248. return 0;
  249. }
  250. /* wait for header */
  251. for(;;) {
  252. if (http_get_line(s, line, sizeof(line)) < 0)
  253. return AVERROR(EIO);
  254. dprintf(NULL, "header='%s'\n", line);
  255. err = process_line(h, line, s->line_count, new_location);
  256. if (err < 0)
  257. return err;
  258. if (err == 0)
  259. break;
  260. s->line_count++;
  261. }
  262. return (off == s->off) ? 0 : -1;
  263. }
  264. static int http_read(URLContext *h, uint8_t *buf, int size)
  265. {
  266. HTTPContext *s = h->priv_data;
  267. int len;
  268. if (s->chunksize >= 0) {
  269. if (!s->chunksize) {
  270. char line[32];
  271. for(;;) {
  272. do {
  273. if (http_get_line(s, line, sizeof(line)) < 0)
  274. return AVERROR(EIO);
  275. } while (!*line); /* skip CR LF from last chunk */
  276. s->chunksize = strtoll(line, NULL, 16);
  277. dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
  278. if (!s->chunksize)
  279. return 0;
  280. break;
  281. }
  282. }
  283. size = FFMIN(size, s->chunksize);
  284. }
  285. /* read bytes from input buffer first */
  286. len = s->buf_end - s->buf_ptr;
  287. if (len > 0) {
  288. if (len > size)
  289. len = size;
  290. memcpy(buf, s->buf_ptr, len);
  291. s->buf_ptr += len;
  292. } else {
  293. len = url_read(s->hd, buf, size);
  294. }
  295. if (len > 0) {
  296. s->off += len;
  297. if (s->chunksize > 0)
  298. s->chunksize -= len;
  299. }
  300. return len;
  301. }
  302. /* used only when posting data */
  303. static int http_write(URLContext *h, uint8_t *buf, int size)
  304. {
  305. HTTPContext *s = h->priv_data;
  306. return url_write(s->hd, buf, size);
  307. }
  308. static int http_close(URLContext *h)
  309. {
  310. HTTPContext *s = h->priv_data;
  311. url_close(s->hd);
  312. av_free(s);
  313. return 0;
  314. }
  315. static int64_t http_seek(URLContext *h, int64_t off, int whence)
  316. {
  317. HTTPContext *s = h->priv_data;
  318. URLContext *old_hd = s->hd;
  319. int64_t old_off = s->off;
  320. if (whence == AVSEEK_SIZE)
  321. return s->filesize;
  322. else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
  323. return -1;
  324. /* we save the old context in case the seek fails */
  325. s->hd = NULL;
  326. if (whence == SEEK_CUR)
  327. off += s->off;
  328. else if (whence == SEEK_END)
  329. off += s->filesize;
  330. s->off = off;
  331. /* if it fails, continue on old connection */
  332. if (http_open_cnx(h) < 0) {
  333. s->hd = old_hd;
  334. s->off = old_off;
  335. return -1;
  336. }
  337. url_close(old_hd);
  338. return off;
  339. }
  340. static int
  341. http_get_file_handle(URLContext *h)
  342. {
  343. HTTPContext *s = h->priv_data;
  344. return url_get_file_handle(s->hd);
  345. }
  346. URLProtocol http_protocol = {
  347. "http",
  348. http_open,
  349. http_read,
  350. http_write,
  351. http_seek,
  352. http_close,
  353. .url_get_file_handle = http_get_file_handle,
  354. };