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.

357 lines
8.9KB

  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. #ifndef __BEOS__
  27. # include <arpa/inet.h>
  28. #else
  29. # include "barpainet.h"
  30. #endif
  31. #include <netdb.h>
  32. #include "base64.h"
  33. /* XXX: POST protocol is not completly implemented because ffmpeg use
  34. only a subset of it */
  35. //#define DEBUG
  36. /* used for protocol handling */
  37. #define BUFFER_SIZE 1024
  38. #define URL_SIZE 4096
  39. #define MAX_REDIRECTS 8
  40. typedef struct {
  41. URLContext *hd;
  42. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  43. int line_count;
  44. int http_code;
  45. offset_t off, filesize;
  46. char location[URL_SIZE];
  47. } HTTPContext;
  48. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  49. const char *auth, int *new_location);
  50. static int http_write(URLContext *h, uint8_t *buf, int size);
  51. /* return non zero if error */
  52. static int http_open_cnx(URLContext *h)
  53. {
  54. const char *path, *proxy_path;
  55. char hostname[1024], hoststr[1024];
  56. char auth[1024];
  57. char path1[1024];
  58. char buf[1024];
  59. int port, use_proxy, err, location_changed = 0, redirects = 0;
  60. HTTPContext *s = h->priv_data;
  61. URLContext *hd = NULL;
  62. proxy_path = getenv("http_proxy");
  63. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  64. strstart(proxy_path, "http://", NULL);
  65. /* fill the dest addr */
  66. redo:
  67. /* needed in any case to build the host string */
  68. url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  69. path1, sizeof(path1), s->location);
  70. if (port > 0) {
  71. snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
  72. } else {
  73. pstrcpy(hoststr, sizeof(hoststr), hostname);
  74. }
  75. if (use_proxy) {
  76. url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  77. NULL, 0, proxy_path);
  78. path = s->location;
  79. } else {
  80. if (path1[0] == '\0')
  81. path = "/";
  82. else
  83. path = path1;
  84. }
  85. if (port < 0)
  86. port = 80;
  87. snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
  88. err = url_open(&hd, buf, URL_RDWR);
  89. if (err < 0)
  90. goto fail;
  91. s->hd = hd;
  92. if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
  93. goto fail;
  94. if (s->http_code == 303 && location_changed == 1) {
  95. /* url moved, get next */
  96. url_close(hd);
  97. if (redirects++ >= MAX_REDIRECTS)
  98. return AVERROR_IO;
  99. location_changed = 0;
  100. goto redo;
  101. }
  102. return 0;
  103. fail:
  104. if (hd)
  105. url_close(hd);
  106. return AVERROR_IO;
  107. }
  108. static int http_open(URLContext *h, const char *uri, int flags)
  109. {
  110. HTTPContext *s;
  111. int ret;
  112. h->is_streamed = 1;
  113. s = av_malloc(sizeof(HTTPContext));
  114. if (!s) {
  115. return -ENOMEM;
  116. }
  117. h->priv_data = s;
  118. s->filesize = -1;
  119. s->off = 0;
  120. pstrcpy (s->location, URL_SIZE, uri);
  121. ret = http_open_cnx(h);
  122. if (ret != 0)
  123. av_free (s);
  124. return ret;
  125. }
  126. static int http_getc(HTTPContext *s)
  127. {
  128. int len;
  129. if (s->buf_ptr >= s->buf_end) {
  130. len = url_read(s->hd, s->buffer, BUFFER_SIZE);
  131. if (len < 0) {
  132. return AVERROR_IO;
  133. } else if (len == 0) {
  134. return -1;
  135. } else {
  136. s->buf_ptr = s->buffer;
  137. s->buf_end = s->buffer + len;
  138. }
  139. }
  140. return *s->buf_ptr++;
  141. }
  142. static int process_line(URLContext *h, char *line, int line_count,
  143. int *new_location)
  144. {
  145. HTTPContext *s = h->priv_data;
  146. char *tag, *p;
  147. /* end of header */
  148. if (line[0] == '\0')
  149. return 0;
  150. p = line;
  151. if (line_count == 0) {
  152. while (!isspace(*p) && *p != '\0')
  153. p++;
  154. while (isspace(*p))
  155. p++;
  156. s->http_code = strtol(p, NULL, 10);
  157. #ifdef DEBUG
  158. printf("http_code=%d\n", s->http_code);
  159. #endif
  160. } else {
  161. while (*p != '\0' && *p != ':')
  162. p++;
  163. if (*p != ':')
  164. return 1;
  165. *p = '\0';
  166. tag = line;
  167. p++;
  168. while (isspace(*p))
  169. p++;
  170. if (!strcmp(tag, "Location")) {
  171. strcpy(s->location, p);
  172. *new_location = 1;
  173. } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
  174. s->filesize = atoll(p);
  175. } else if (!strcmp (tag, "Content-Range")) {
  176. /* "bytes $from-$to/$document_size" */
  177. const char *slash;
  178. if (!strncmp (p, "bytes ", 6)) {
  179. p += 6;
  180. s->off = atoll(p);
  181. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  182. s->filesize = atoll(slash+1);
  183. }
  184. h->is_streamed = 0; /* we _can_ in fact seek */
  185. }
  186. }
  187. return 1;
  188. }
  189. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  190. const char *auth, int *new_location)
  191. {
  192. HTTPContext *s = h->priv_data;
  193. int post, err, ch;
  194. char line[1024], *q;
  195. char *auth_b64;
  196. offset_t off = s->off;
  197. /* send http header */
  198. post = h->flags & URL_WRONLY;
  199. auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
  200. snprintf(s->buffer, sizeof(s->buffer),
  201. "%s %s HTTP/1.1\r\n"
  202. "User-Agent: %s\r\n"
  203. "Accept: */*\r\n"
  204. "Range: bytes=%llu-\r\n"
  205. "Host: %s\r\n"
  206. "Authorization: Basic %s\r\n"
  207. "\r\n",
  208. post ? "POST" : "GET",
  209. path,
  210. LIBAVFORMAT_IDENT,
  211. s->off,
  212. hoststr,
  213. auth_b64);
  214. av_freep(&auth_b64);
  215. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  216. return AVERROR_IO;
  217. /* init input buffer */
  218. s->buf_ptr = s->buffer;
  219. s->buf_end = s->buffer;
  220. s->line_count = 0;
  221. s->off = 0;
  222. if (post) {
  223. sleep(1);
  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_IO;
  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. };