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.

349 lines
8.7KB

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