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.

356 lines
9.1KB

  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 "avformat.h"
  22. #include <unistd.h>
  23. #include "network.h"
  24. #include "base64.h"
  25. #include "avstring.h"
  26. /* XXX: POST protocol is not completely implemented because ffmpeg uses
  27. only a subset of it. */
  28. //#define DEBUG
  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. offset_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 process_line(URLContext *h, char *line, int line_count,
  136. int *new_location)
  137. {
  138. HTTPContext *s = h->priv_data;
  139. char *tag, *p;
  140. /* end of header */
  141. if (line[0] == '\0')
  142. return 0;
  143. p = line;
  144. if (line_count == 0) {
  145. while (!isspace(*p) && *p != '\0')
  146. p++;
  147. while (isspace(*p))
  148. p++;
  149. s->http_code = strtol(p, NULL, 10);
  150. #ifdef DEBUG
  151. printf("http_code=%d\n", s->http_code);
  152. #endif
  153. /* error codes are 4xx and 5xx */
  154. if (s->http_code >= 400 && s->http_code < 600)
  155. return -1;
  156. } else {
  157. while (*p != '\0' && *p != ':')
  158. p++;
  159. if (*p != ':')
  160. return 1;
  161. *p = '\0';
  162. tag = line;
  163. p++;
  164. while (isspace(*p))
  165. p++;
  166. if (!strcmp(tag, "Location")) {
  167. strcpy(s->location, p);
  168. *new_location = 1;
  169. } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
  170. s->filesize = atoll(p);
  171. } else if (!strcmp (tag, "Content-Range")) {
  172. /* "bytes $from-$to/$document_size" */
  173. const char *slash;
  174. if (!strncmp (p, "bytes ", 6)) {
  175. p += 6;
  176. s->off = atoll(p);
  177. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  178. s->filesize = atoll(slash+1);
  179. }
  180. h->is_streamed = 0; /* we _can_ in fact seek */
  181. }
  182. }
  183. return 1;
  184. }
  185. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  186. const char *auth, int *new_location)
  187. {
  188. HTTPContext *s = h->priv_data;
  189. int post, err, ch;
  190. char line[1024], *q;
  191. char *auth_b64;
  192. int auth_b64_len = strlen(auth)* 4 / 3 + 12;
  193. offset_t off = s->off;
  194. /* send http header */
  195. post = h->flags & URL_WRONLY;
  196. auth_b64 = av_malloc(auth_b64_len);
  197. av_base64_encode(auth_b64, auth_b64_len, (uint8_t *)auth, strlen(auth));
  198. snprintf(s->buffer, sizeof(s->buffer),
  199. "%s %s HTTP/1.1\r\n"
  200. "User-Agent: %s\r\n"
  201. "Accept: */*\r\n"
  202. "Range: bytes=%"PRId64"-\r\n"
  203. "Host: %s\r\n"
  204. "Authorization: Basic %s\r\n"
  205. "Connection: close\r\n"
  206. "\r\n",
  207. post ? "POST" : "GET",
  208. path,
  209. LIBAVFORMAT_IDENT,
  210. s->off,
  211. hoststr,
  212. auth_b64);
  213. av_freep(&auth_b64);
  214. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  215. return AVERROR(EIO);
  216. /* init input buffer */
  217. s->buf_ptr = s->buffer;
  218. s->buf_end = s->buffer;
  219. s->line_count = 0;
  220. s->off = 0;
  221. s->filesize = -1;
  222. if (post) {
  223. usleep(1000*1000);
  224. return 0;
  225. }
  226. /* wait for header */
  227. q = line;
  228. for(;;) {
  229. ch = http_getc(s);
  230. if (ch < 0)
  231. return AVERROR(EIO);
  232. if (ch == '\n') {
  233. /* process line */
  234. if (q > line && q[-1] == '\r')
  235. q--;
  236. *q = '\0';
  237. #ifdef DEBUG
  238. printf("header='%s'\n", line);
  239. #endif
  240. err = process_line(h, line, s->line_count, new_location);
  241. if (err < 0)
  242. return err;
  243. if (err == 0)
  244. break;
  245. s->line_count++;
  246. q = line;
  247. } else {
  248. if ((q - line) < sizeof(line) - 1)
  249. *q++ = ch;
  250. }
  251. }
  252. return (off == s->off) ? 0 : -1;
  253. }
  254. static int http_read(URLContext *h, uint8_t *buf, int size)
  255. {
  256. HTTPContext *s = h->priv_data;
  257. int len;
  258. /* read bytes from input buffer first */
  259. len = s->buf_end - s->buf_ptr;
  260. if (len > 0) {
  261. if (len > size)
  262. len = size;
  263. memcpy(buf, s->buf_ptr, len);
  264. s->buf_ptr += len;
  265. } else {
  266. len = url_read(s->hd, buf, size);
  267. }
  268. if (len > 0)
  269. s->off += len;
  270. return len;
  271. }
  272. /* used only when posting data */
  273. static int http_write(URLContext *h, uint8_t *buf, int size)
  274. {
  275. HTTPContext *s = h->priv_data;
  276. return url_write(s->hd, buf, size);
  277. }
  278. static int http_close(URLContext *h)
  279. {
  280. HTTPContext *s = h->priv_data;
  281. url_close(s->hd);
  282. av_free(s);
  283. return 0;
  284. }
  285. static offset_t http_seek(URLContext *h, offset_t off, int whence)
  286. {
  287. HTTPContext *s = h->priv_data;
  288. URLContext *old_hd = s->hd;
  289. offset_t old_off = s->off;
  290. if (whence == AVSEEK_SIZE)
  291. return s->filesize;
  292. else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
  293. return -1;
  294. /* we save the old context in case the seek fails */
  295. s->hd = NULL;
  296. if (whence == SEEK_CUR)
  297. off += s->off;
  298. else if (whence == SEEK_END)
  299. off += s->filesize;
  300. s->off = off;
  301. /* if it fails, continue on old connection */
  302. if (http_open_cnx(h) < 0) {
  303. s->hd = old_hd;
  304. s->off = old_off;
  305. return -1;
  306. }
  307. url_close(old_hd);
  308. return off;
  309. }
  310. URLProtocol http_protocol = {
  311. "http",
  312. http_open,
  313. http_read,
  314. http_write,
  315. http_seek,
  316. http_close,
  317. };