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.

353 lines
8.8KB

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