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.

317 lines
7.3KB

  1. /*
  2. * HTTP protocol for ffmpeg client
  3. * Copyright (c) 2000, 2001 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include "avformat.h"
  20. #include <unistd.h>
  21. #include <ctype.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <arpa/inet.h>
  26. #include <netdb.h>
  27. /* XXX: POST protocol is not completly implemented because ffmpeg use
  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. typedef struct {
  34. int fd;
  35. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  36. int line_count;
  37. int http_code;
  38. char location[URL_SIZE];
  39. } HTTPContext;
  40. static int http_connect(URLContext *h, const char *path);
  41. static int http_write(URLContext *h, UINT8 *buf, int size);
  42. /* return non zero if error */
  43. static int http_open(URLContext *h, const char *uri, int flags)
  44. {
  45. struct sockaddr_in dest_addr;
  46. const char *p, *path, *proxy_path;
  47. char hostname[1024], *q;
  48. int port, fd = -1, use_proxy;
  49. struct hostent *hp;
  50. HTTPContext *s;
  51. h->is_streamed = 1;
  52. s = av_malloc(sizeof(HTTPContext));
  53. if (!s) {
  54. return -ENOMEM;
  55. }
  56. h->priv_data = s;
  57. proxy_path = getenv("http_proxy");
  58. use_proxy = (proxy_path != NULL) && !getenv("no_proxy");
  59. /* fill the dest addr */
  60. redo:
  61. if (use_proxy) {
  62. p = proxy_path;
  63. } else {
  64. p = uri;
  65. }
  66. if (!strstart(p, "http://", &p))
  67. goto fail;
  68. q = hostname;
  69. while (*p != ':' && *p != '/' && *p != '\0') {
  70. if ((q - hostname) < sizeof(hostname) - 1)
  71. *q++ = *p;
  72. p++;
  73. }
  74. *q = '\0';
  75. port = 80;
  76. if (*p == ':') {
  77. p++;
  78. port = strtoul(p, (char **)&p, 10);
  79. }
  80. if (port <= 0)
  81. goto fail;
  82. if (use_proxy) {
  83. path = uri;
  84. } else {
  85. if (*p == '\0')
  86. path = "/";
  87. else
  88. path = p;
  89. }
  90. dest_addr.sin_family = AF_INET;
  91. dest_addr.sin_port = htons(port);
  92. if ((inet_aton(hostname, &dest_addr.sin_addr)) == 0) {
  93. hp = gethostbyname(hostname);
  94. if (!hp)
  95. goto fail;
  96. memcpy (&dest_addr.sin_addr, hp->h_addr, sizeof(dest_addr.sin_addr));
  97. }
  98. fd = socket(PF_INET, SOCK_STREAM, 0);
  99. if (fd < 0)
  100. goto fail;
  101. if (connect(fd, (struct sockaddr *)&dest_addr,
  102. sizeof(dest_addr)) < 0)
  103. goto fail;
  104. s->fd = fd;
  105. if (http_connect(h, path) < 0)
  106. goto fail;
  107. if (s->http_code == 303 && s->location[0] != '\0') {
  108. /* url moved, get next */
  109. uri = s->location;
  110. goto redo;
  111. }
  112. return 0;
  113. fail:
  114. if (fd >= 0)
  115. close(fd);
  116. av_free(s);
  117. return -EIO;
  118. }
  119. static int http_getc(HTTPContext *s)
  120. {
  121. int len;
  122. if (s->buf_ptr >= s->buf_end) {
  123. redo:
  124. len = read(s->fd, s->buffer, BUFFER_SIZE);
  125. if (len < 0) {
  126. if (errno == EAGAIN || errno == EINTR)
  127. goto redo;
  128. return -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 process_line(HTTPContext *s, char *line, int line_count)
  139. {
  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. } else {
  155. while (*p != '\0' && *p != ':')
  156. p++;
  157. if (*p != ':')
  158. return 1;
  159. *p = '\0';
  160. tag = line;
  161. p++;
  162. while (isspace(*p))
  163. p++;
  164. if (!strcmp(tag, "Location")) {
  165. strcpy(s->location, p);
  166. }
  167. }
  168. return 1;
  169. }
  170. static int http_connect(URLContext *h, const char *path)
  171. {
  172. HTTPContext *s = h->priv_data;
  173. int post, err, ch;
  174. char line[1024], *q;
  175. /* send http header */
  176. post = h->flags & URL_WRONLY;
  177. snprintf(s->buffer, sizeof(s->buffer),
  178. "%s %s HTTP/1.0\n"
  179. "User-Agent: FFmpeg %s\n"
  180. "Accept: */*\n"
  181. "\n",
  182. post ? "POST" : "GET",
  183. path,
  184. FFMPEG_VERSION
  185. );
  186. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  187. return -EIO;
  188. /* init input buffer */
  189. s->buf_ptr = s->buffer;
  190. s->buf_end = s->buffer;
  191. s->line_count = 0;
  192. s->location[0] = '\0';
  193. if (post) {
  194. sleep(1);
  195. return 0;
  196. }
  197. /* wait for header */
  198. q = line;
  199. for(;;) {
  200. ch = http_getc(s);
  201. if (ch < 0)
  202. return -EIO;
  203. if (ch == '\n') {
  204. /* process line */
  205. if (q > line && q[-1] == '\r')
  206. q--;
  207. *q = '\0';
  208. #ifdef DEBUG
  209. printf("header='%s'\n", line);
  210. #endif
  211. err = process_line(s, line, s->line_count);
  212. if (err < 0)
  213. return err;
  214. if (err == 0)
  215. return 0;
  216. s->line_count++;
  217. q = line;
  218. } else {
  219. if ((q - line) < sizeof(line) - 1)
  220. *q++ = ch;
  221. }
  222. }
  223. }
  224. static int http_read(URLContext *h, UINT8 *buf, int size)
  225. {
  226. HTTPContext *s = h->priv_data;
  227. int size1, len;
  228. size1 = size;
  229. while (size > 0) {
  230. /* read bytes from input buffer first */
  231. len = s->buf_end - s->buf_ptr;
  232. if (len > 0) {
  233. if (len > size)
  234. len = size;
  235. memcpy(buf, s->buf_ptr, len);
  236. s->buf_ptr += len;
  237. } else {
  238. len = read (s->fd, buf, size);
  239. if (len < 0) {
  240. if (errno != EINTR && errno != EAGAIN)
  241. return -errno;
  242. else
  243. continue;
  244. } else if (len == 0) {
  245. break;
  246. }
  247. }
  248. size -= len;
  249. buf += len;
  250. }
  251. return size1 - size;
  252. }
  253. /* used only when posting data */
  254. static int http_write(URLContext *h, UINT8 *buf, int size)
  255. {
  256. HTTPContext *s = h->priv_data;
  257. int ret, size1;
  258. size1 = size;
  259. while (size > 0) {
  260. ret = write (s->fd, buf, size);
  261. if (ret < 0 && errno != EINTR && errno != EAGAIN)
  262. return -errno;
  263. size -= ret;
  264. buf += ret;
  265. }
  266. return size1 - size;
  267. }
  268. static int http_close(URLContext *h)
  269. {
  270. HTTPContext *s = h->priv_data;
  271. close(s->fd);
  272. return 0;
  273. }
  274. URLProtocol http_protocol = {
  275. "http",
  276. http_open,
  277. http_read,
  278. http_write,
  279. NULL, /* seek */
  280. http_close,
  281. };