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.

476 lines
13KB

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