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.

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