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.

440 lines
12KB

  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 "libavutil/base64.h"
  22. #include "libavutil/avstring.h"
  23. #include "avformat.h"
  24. #include <unistd.h>
  25. #include <strings.h>
  26. #include "network.h"
  27. #include "os_support.h"
  28. /* XXX: POST protocol is not completely implemented because ffmpeg uses
  29. only a subset of it. */
  30. /* used for protocol handling */
  31. #define BUFFER_SIZE 1024
  32. #define URL_SIZE 4096
  33. #define MAX_REDIRECTS 8
  34. typedef struct {
  35. URLContext *hd;
  36. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  37. int line_count;
  38. int http_code;
  39. int64_t chunksize; /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
  40. int64_t off, filesize;
  41. char location[URL_SIZE];
  42. } HTTPContext;
  43. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  44. const char *auth, int *new_location);
  45. static int http_write(URLContext *h, uint8_t *buf, int size);
  46. /* return non zero if error */
  47. static int http_open_cnx(URLContext *h)
  48. {
  49. const char *path, *proxy_path;
  50. char hostname[1024], hoststr[1024];
  51. char auth[1024];
  52. char path1[1024];
  53. char buf[1024];
  54. int port, use_proxy, err, location_changed = 0, redirects = 0;
  55. HTTPContext *s = h->priv_data;
  56. URLContext *hd = NULL;
  57. proxy_path = getenv("http_proxy");
  58. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  59. av_strstart(proxy_path, "http://", NULL);
  60. /* fill the dest addr */
  61. redo:
  62. /* needed in any case to build the host string */
  63. ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  64. path1, sizeof(path1), s->location);
  65. ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
  66. if (use_proxy) {
  67. ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  68. NULL, 0, proxy_path);
  69. path = s->location;
  70. } else {
  71. if (path1[0] == '\0')
  72. path = "/";
  73. else
  74. path = path1;
  75. }
  76. if (port < 0)
  77. port = 80;
  78. ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
  79. err = url_open(&hd, buf, URL_RDWR);
  80. if (err < 0)
  81. goto fail;
  82. s->hd = hd;
  83. if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
  84. goto fail;
  85. if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
  86. /* url moved, get next */
  87. url_close(hd);
  88. if (redirects++ >= MAX_REDIRECTS)
  89. return AVERROR(EIO);
  90. location_changed = 0;
  91. goto redo;
  92. }
  93. return 0;
  94. fail:
  95. if (hd)
  96. url_close(hd);
  97. return AVERROR(EIO);
  98. }
  99. static int http_open(URLContext *h, const char *uri, int flags)
  100. {
  101. HTTPContext *s;
  102. int ret;
  103. h->is_streamed = 1;
  104. s = av_malloc(sizeof(HTTPContext));
  105. if (!s) {
  106. return AVERROR(ENOMEM);
  107. }
  108. h->priv_data = s;
  109. s->filesize = -1;
  110. s->chunksize = -1;
  111. s->off = 0;
  112. av_strlcpy(s->location, uri, URL_SIZE);
  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(EIO);
  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 http_get_line(HTTPContext *s, char *line, int line_size)
  135. {
  136. int ch;
  137. char *q;
  138. q = line;
  139. for(;;) {
  140. ch = http_getc(s);
  141. if (ch < 0)
  142. return AVERROR(EIO);
  143. if (ch == '\n') {
  144. /* process line */
  145. if (q > line && q[-1] == '\r')
  146. q--;
  147. *q = '\0';
  148. return 0;
  149. } else {
  150. if ((q - line) < line_size - 1)
  151. *q++ = ch;
  152. }
  153. }
  154. }
  155. static int process_line(URLContext *h, char *line, int line_count,
  156. int *new_location)
  157. {
  158. HTTPContext *s = h->priv_data;
  159. char *tag, *p;
  160. /* end of header */
  161. if (line[0] == '\0')
  162. return 0;
  163. p = line;
  164. if (line_count == 0) {
  165. while (!isspace(*p) && *p != '\0')
  166. p++;
  167. while (isspace(*p))
  168. p++;
  169. s->http_code = strtol(p, NULL, 10);
  170. dprintf(NULL, "http_code=%d\n", s->http_code);
  171. /* error codes are 4xx and 5xx */
  172. if (s->http_code >= 400 && s->http_code < 600)
  173. return -1;
  174. } else {
  175. while (*p != '\0' && *p != ':')
  176. p++;
  177. if (*p != ':')
  178. return 1;
  179. *p = '\0';
  180. tag = line;
  181. p++;
  182. while (isspace(*p))
  183. p++;
  184. if (!strcmp(tag, "Location")) {
  185. strcpy(s->location, p);
  186. *new_location = 1;
  187. } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
  188. s->filesize = atoll(p);
  189. } else if (!strcmp (tag, "Content-Range")) {
  190. /* "bytes $from-$to/$document_size" */
  191. const char *slash;
  192. if (!strncmp (p, "bytes ", 6)) {
  193. p += 6;
  194. s->off = atoll(p);
  195. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  196. s->filesize = atoll(slash+1);
  197. }
  198. h->is_streamed = 0; /* we _can_ in fact seek */
  199. } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
  200. s->filesize = -1;
  201. s->chunksize = 0;
  202. }
  203. }
  204. return 1;
  205. }
  206. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  207. const char *auth, int *new_location)
  208. {
  209. HTTPContext *s = h->priv_data;
  210. int post, err;
  211. char line[1024];
  212. char *auth_b64;
  213. int auth_b64_len = (strlen(auth) + 2) / 3 * 4 + 1;
  214. int64_t off = s->off;
  215. /* send http header */
  216. post = h->flags & URL_WRONLY;
  217. auth_b64 = av_malloc(auth_b64_len);
  218. av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));
  219. snprintf(s->buffer, sizeof(s->buffer),
  220. "%s %s HTTP/1.1\r\n"
  221. "User-Agent: %s\r\n"
  222. "Accept: */*\r\n"
  223. "Range: bytes=%"PRId64"-\r\n"
  224. "Host: %s\r\n"
  225. "Authorization: Basic %s\r\n"
  226. "Connection: close\r\n"
  227. "%s"
  228. "\r\n",
  229. post ? "POST" : "GET",
  230. path,
  231. LIBAVFORMAT_IDENT,
  232. s->off,
  233. hoststr,
  234. auth_b64,
  235. post ? "Transfer-Encoding: chunked\r\n" : "");
  236. av_freep(&auth_b64);
  237. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  238. return AVERROR(EIO);
  239. /* init input buffer */
  240. s->buf_ptr = s->buffer;
  241. s->buf_end = s->buffer;
  242. s->line_count = 0;
  243. s->off = 0;
  244. s->filesize = -1;
  245. if (post) {
  246. /* always use chunked encoding for upload data */
  247. s->chunksize = 0;
  248. return 0;
  249. }
  250. /* wait for header */
  251. for(;;) {
  252. if (http_get_line(s, line, sizeof(line)) < 0)
  253. return AVERROR(EIO);
  254. dprintf(NULL, "header='%s'\n", line);
  255. err = process_line(h, line, s->line_count, new_location);
  256. if (err < 0)
  257. return err;
  258. if (err == 0)
  259. break;
  260. s->line_count++;
  261. }
  262. return (off == s->off) ? 0 : -1;
  263. }
  264. static int http_read(URLContext *h, uint8_t *buf, int size)
  265. {
  266. HTTPContext *s = h->priv_data;
  267. int len;
  268. if (s->chunksize >= 0) {
  269. if (!s->chunksize) {
  270. char line[32];
  271. for(;;) {
  272. do {
  273. if (http_get_line(s, line, sizeof(line)) < 0)
  274. return AVERROR(EIO);
  275. } while (!*line); /* skip CR LF from last chunk */
  276. s->chunksize = strtoll(line, NULL, 16);
  277. dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
  278. if (!s->chunksize)
  279. return 0;
  280. break;
  281. }
  282. }
  283. size = FFMIN(size, s->chunksize);
  284. }
  285. /* read bytes from input buffer first */
  286. len = s->buf_end - s->buf_ptr;
  287. if (len > 0) {
  288. if (len > size)
  289. len = size;
  290. memcpy(buf, s->buf_ptr, len);
  291. s->buf_ptr += len;
  292. } else {
  293. len = url_read(s->hd, buf, size);
  294. }
  295. if (len > 0) {
  296. s->off += len;
  297. if (s->chunksize > 0)
  298. s->chunksize -= len;
  299. }
  300. return len;
  301. }
  302. /* used only when posting data */
  303. static int http_write(URLContext *h, uint8_t *buf, int size)
  304. {
  305. char temp[11]; /* 32-bit hex + CRLF + nul */
  306. int ret;
  307. char crlf[] = "\r\n";
  308. HTTPContext *s = h->priv_data;
  309. if (s->chunksize == -1) {
  310. /* headers are sent without any special encoding */
  311. return url_write(s->hd, buf, size);
  312. }
  313. /* silently ignore zero-size data since chunk encoding that would
  314. * signal EOF */
  315. if (size > 0) {
  316. /* upload data using chunked encoding */
  317. snprintf(temp, sizeof(temp), "%x\r\n", size);
  318. if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
  319. (ret = url_write(s->hd, buf, size)) < 0 ||
  320. (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
  321. return ret;
  322. }
  323. return size;
  324. }
  325. static int http_close(URLContext *h)
  326. {
  327. int ret = 0;
  328. char footer[] = "0\r\n\r\n";
  329. HTTPContext *s = h->priv_data;
  330. /* signal end of chunked encoding if used */
  331. if ((h->flags & URL_WRONLY) && s->chunksize != -1) {
  332. ret = url_write(s->hd, footer, sizeof(footer) - 1);
  333. ret = ret > 0 ? 0 : ret;
  334. }
  335. url_close(s->hd);
  336. av_free(s);
  337. return ret;
  338. }
  339. static int64_t http_seek(URLContext *h, int64_t off, int whence)
  340. {
  341. HTTPContext *s = h->priv_data;
  342. URLContext *old_hd = s->hd;
  343. int64_t old_off = s->off;
  344. uint8_t old_buf[BUFFER_SIZE];
  345. int old_buf_size;
  346. if (whence == AVSEEK_SIZE)
  347. return s->filesize;
  348. else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
  349. return -1;
  350. /* we save the old context in case the seek fails */
  351. old_buf_size = s->buf_end - s->buf_ptr;
  352. memcpy(old_buf, s->buf_ptr, old_buf_size);
  353. s->hd = NULL;
  354. if (whence == SEEK_CUR)
  355. off += s->off;
  356. else if (whence == SEEK_END)
  357. off += s->filesize;
  358. s->off = off;
  359. /* if it fails, continue on old connection */
  360. if (http_open_cnx(h) < 0) {
  361. memcpy(s->buffer, old_buf, old_buf_size);
  362. s->buf_ptr = s->buffer;
  363. s->buf_end = s->buffer + old_buf_size;
  364. s->hd = old_hd;
  365. s->off = old_off;
  366. return -1;
  367. }
  368. url_close(old_hd);
  369. return off;
  370. }
  371. static int
  372. http_get_file_handle(URLContext *h)
  373. {
  374. HTTPContext *s = h->priv_data;
  375. return url_get_file_handle(s->hd);
  376. }
  377. URLProtocol http_protocol = {
  378. "http",
  379. http_open,
  380. http_read,
  381. http_write,
  382. http_seek,
  383. http_close,
  384. .url_get_file_handle = http_get_file_handle,
  385. };