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.

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