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.

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