http.c 12 KB

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