PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/hwdvideos/coders/ffmpeg/libavformat/http.c

https://github.com/Shigaru/shigaru
C | 355 lines | 278 code | 38 blank | 39 comment | 89 complexity | 1852b5f4640c0710a544a74904ce2f40 MD5 | raw file
  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 "network.h"
  26. #include "os_support.h"
  27. /* XXX: POST protocol is not completely implemented because ffmpeg uses
  28. only a subset of it. */
  29. //#define DEBUG
  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 off, filesize;
  40. char location[URL_SIZE];
  41. } HTTPContext;
  42. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  43. const char *auth, int *new_location);
  44. static int http_write(URLContext *h, uint8_t *buf, int size);
  45. /* return non zero if error */
  46. static int http_open_cnx(URLContext *h)
  47. {
  48. const char *path, *proxy_path;
  49. char hostname[1024], hoststr[1024];
  50. char auth[1024];
  51. char path1[1024];
  52. char buf[1024];
  53. int port, use_proxy, err, location_changed = 0, redirects = 0;
  54. HTTPContext *s = h->priv_data;
  55. URLContext *hd = NULL;
  56. proxy_path = getenv("http_proxy");
  57. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  58. av_strstart(proxy_path, "http://", NULL);
  59. /* fill the dest addr */
  60. redo:
  61. /* needed in any case to build the host string */
  62. url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
  63. path1, sizeof(path1), s->location);
  64. if (port > 0) {
  65. snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
  66. } else {
  67. av_strlcpy(hoststr, hostname, sizeof(hoststr));
  68. }
  69. if (use_proxy) {
  70. 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. snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
  82. err = url_open(&hd, buf, URL_RDWR);
  83. if (err < 0)
  84. goto fail;
  85. s->hd = hd;
  86. if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
  87. goto fail;
  88. if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
  89. /* url moved, get next */
  90. url_close(hd);
  91. if (redirects++ >= MAX_REDIRECTS)
  92. return AVERROR(EIO);
  93. location_changed = 0;
  94. goto redo;
  95. }
  96. return 0;
  97. fail:
  98. if (hd)
  99. url_close(hd);
  100. return AVERROR(EIO);
  101. }
  102. static int http_open(URLContext *h, const char *uri, int flags)
  103. {
  104. HTTPContext *s;
  105. int ret;
  106. h->is_streamed = 1;
  107. s = av_malloc(sizeof(HTTPContext));
  108. if (!s) {
  109. return AVERROR(ENOMEM);
  110. }
  111. h->priv_data = s;
  112. s->filesize = -1;
  113. s->off = 0;
  114. av_strlcpy(s->location, uri, URL_SIZE);
  115. ret = http_open_cnx(h);
  116. if (ret != 0)
  117. av_free (s);
  118. return ret;
  119. }
  120. static int http_getc(HTTPContext *s)
  121. {
  122. int len;
  123. if (s->buf_ptr >= s->buf_end) {
  124. len = url_read(s->hd, s->buffer, BUFFER_SIZE);
  125. if (len < 0) {
  126. return AVERROR(EIO);
  127. } else if (len == 0) {
  128. return -1;
  129. } else {
  130. s->buf_ptr = s->buffer;
  131. s->buf_end = s->buffer + len;
  132. }
  133. }
  134. return *s->buf_ptr++;
  135. }
  136. static int process_line(URLContext *h, char *line, int line_count,
  137. int *new_location)
  138. {
  139. HTTPContext *s = h->priv_data;
  140. char *tag, *p;
  141. /* end of header */
  142. if (line[0] == '\0')
  143. return 0;
  144. p = line;
  145. if (line_count == 0) {
  146. while (!isspace(*p) && *p != '\0')
  147. p++;
  148. while (isspace(*p))
  149. p++;
  150. s->http_code = strtol(p, NULL, 10);
  151. #ifdef DEBUG
  152. printf("http_code=%d\n", s->http_code);
  153. #endif
  154. /* error codes are 4xx and 5xx */
  155. if (s->http_code >= 400 && s->http_code < 600)
  156. return -1;
  157. } else {
  158. while (*p != '\0' && *p != ':')
  159. p++;
  160. if (*p != ':')
  161. return 1;
  162. *p = '\0';
  163. tag = line;
  164. p++;
  165. while (isspace(*p))
  166. p++;
  167. if (!strcmp(tag, "Location")) {
  168. strcpy(s->location, p);
  169. *new_location = 1;
  170. } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
  171. s->filesize = atoll(p);
  172. } else if (!strcmp (tag, "Content-Range")) {
  173. /* "bytes $from-$to/$document_size" */
  174. const char *slash;
  175. if (!strncmp (p, "bytes ", 6)) {
  176. p += 6;
  177. s->off = atoll(p);
  178. if ((slash = strchr(p, '/')) && strlen(slash) > 0)
  179. s->filesize = atoll(slash+1);
  180. }
  181. h->is_streamed = 0; /* we _can_ in fact seek */
  182. }
  183. }
  184. return 1;
  185. }
  186. static int http_connect(URLContext *h, const char *path, const char *hoststr,
  187. const char *auth, int *new_location)
  188. {
  189. HTTPContext *s = h->priv_data;
  190. int post, err, ch;
  191. char line[1024], *q;
  192. char *auth_b64;
  193. int auth_b64_len = strlen(auth)* 4 / 3 + 12;
  194. int64_t off = s->off;
  195. /* send http header */
  196. post = h->flags & URL_WRONLY;
  197. auth_b64 = av_malloc(auth_b64_len);
  198. av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));
  199. snprintf(s->buffer, sizeof(s->buffer),
  200. "%s %s HTTP/1.1\r\n"
  201. "User-Agent: %s\r\n"
  202. "Accept: */*\r\n"
  203. "Range: bytes=%"PRId64"-\r\n"
  204. "Host: %s\r\n"
  205. "Authorization: Basic %s\r\n"
  206. "Connection: close\r\n"
  207. "\r\n",
  208. post ? "POST" : "GET",
  209. path,
  210. LIBAVFORMAT_IDENT,
  211. s->off,
  212. hoststr,
  213. auth_b64);
  214. av_freep(&auth_b64);
  215. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  216. return AVERROR(EIO);
  217. /* init input buffer */
  218. s->buf_ptr = s->buffer;
  219. s->buf_end = s->buffer;
  220. s->line_count = 0;
  221. s->off = 0;
  222. s->filesize = -1;
  223. if (post) {
  224. return 0;
  225. }
  226. /* wait for header */
  227. q = line;
  228. for(;;) {
  229. ch = http_getc(s);
  230. if (ch < 0)
  231. return AVERROR(EIO);
  232. if (ch == '\n') {
  233. /* process line */
  234. if (q > line && q[-1] == '\r')
  235. q--;
  236. *q = '\0';
  237. #ifdef DEBUG
  238. printf("header='%s'\n", line);
  239. #endif
  240. err = process_line(h, line, s->line_count, new_location);
  241. if (err < 0)
  242. return err;
  243. if (err == 0)
  244. break;
  245. s->line_count++;
  246. q = line;
  247. } else {
  248. if ((q - line) < sizeof(line) - 1)
  249. *q++ = ch;
  250. }
  251. }
  252. return (off == s->off) ? 0 : -1;
  253. }
  254. static int http_read(URLContext *h, uint8_t *buf, int size)
  255. {
  256. HTTPContext *s = h->priv_data;
  257. int len;
  258. /* read bytes from input buffer first */
  259. len = s->buf_end - s->buf_ptr;
  260. if (len > 0) {
  261. if (len > size)
  262. len = size;
  263. memcpy(buf, s->buf_ptr, len);
  264. s->buf_ptr += len;
  265. } else {
  266. len = url_read(s->hd, buf, size);
  267. }
  268. if (len > 0)
  269. s->off += len;
  270. return len;
  271. }
  272. /* used only when posting data */
  273. static int http_write(URLContext *h, uint8_t *buf, int size)
  274. {
  275. HTTPContext *s = h->priv_data;
  276. return url_write(s->hd, buf, size);
  277. }
  278. static int http_close(URLContext *h)
  279. {
  280. HTTPContext *s = h->priv_data;
  281. url_close(s->hd);
  282. av_free(s);
  283. return 0;
  284. }
  285. static int64_t http_seek(URLContext *h, int64_t off, int whence)
  286. {
  287. HTTPContext *s = h->priv_data;
  288. URLContext *old_hd = s->hd;
  289. int64_t old_off = s->off;
  290. if (whence == AVSEEK_SIZE)
  291. return s->filesize;
  292. else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
  293. return -1;
  294. /* we save the old context in case the seek fails */
  295. s->hd = NULL;
  296. if (whence == SEEK_CUR)
  297. off += s->off;
  298. else if (whence == SEEK_END)
  299. off += s->filesize;
  300. s->off = off;
  301. /* if it fails, continue on old connection */
  302. if (http_open_cnx(h) < 0) {
  303. s->hd = old_hd;
  304. s->off = old_off;
  305. return -1;
  306. }
  307. url_close(old_hd);
  308. return off;
  309. }
  310. URLProtocol http_protocol = {
  311. "http",
  312. http_open,
  313. http_read,
  314. http_write,
  315. http_seek,
  316. http_close,
  317. };