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.

355 lines
9.0KB

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