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.

376 lines
9.4KB

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