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.

319 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 <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <ctype.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <netdb.h>
  30. #include "avformat.h"
  31. /* XXX: POST protocol is not completly implemented because ffmpeg use
  32. only a subset of it */
  33. //#define DEBUG
  34. /* used for protocol handling */
  35. #define BUFFER_SIZE 1024
  36. #define URL_SIZE 4096
  37. typedef struct {
  38. int fd;
  39. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  40. int line_count;
  41. int http_code;
  42. char location[URL_SIZE];
  43. } HTTPContext;
  44. static int http_connect(URLContext *h, const char *path);
  45. static int http_write(URLContext *h, UINT8 *buf, int size);
  46. /* return non zero if error */
  47. static int http_open(URLContext *h, const char *uri, int flags)
  48. {
  49. struct sockaddr_in dest_addr;
  50. const char *p, *path, *proxy_path;
  51. char hostname[1024], *q;
  52. int port, fd = -1, use_proxy;
  53. struct hostent *hp;
  54. HTTPContext *s;
  55. h->is_streamed = 1;
  56. s = malloc(sizeof(HTTPContext));
  57. if (!s) {
  58. return -ENOMEM;
  59. }
  60. h->priv_data = s;
  61. proxy_path = getenv("http_proxy");
  62. use_proxy = (proxy_path != NULL) && !getenv("no_proxy");
  63. /* fill the dest addr */
  64. redo:
  65. if (use_proxy) {
  66. p = proxy_path;
  67. } else {
  68. p = uri;
  69. }
  70. if (!strstart(p, "http://", &p))
  71. goto fail;
  72. q = hostname;
  73. while (*p != ':' && *p != '/' && *p != '\0') {
  74. if ((q - hostname) < sizeof(hostname) - 1)
  75. *q++ = *p;
  76. p++;
  77. }
  78. *q = '\0';
  79. port = 80;
  80. if (*p == ':') {
  81. p++;
  82. port = strtoul(p, (char **)&p, 10);
  83. }
  84. if (port <= 0)
  85. goto fail;
  86. if (use_proxy) {
  87. path = uri;
  88. } else {
  89. if (*p == '\0')
  90. path = "/";
  91. else
  92. path = p;
  93. }
  94. dest_addr.sin_family = AF_INET;
  95. dest_addr.sin_port = htons(port);
  96. if ((inet_aton(hostname, &dest_addr.sin_addr)) == 0) {
  97. hp = gethostbyname(hostname);
  98. if (!hp)
  99. goto fail;
  100. memcpy (&dest_addr.sin_addr, hp->h_addr, sizeof(dest_addr.sin_addr));
  101. }
  102. fd = socket(PF_INET, SOCK_STREAM, 0);
  103. if (fd < 0)
  104. goto fail;
  105. if (connect(fd, (struct sockaddr *)&dest_addr,
  106. sizeof(dest_addr)) < 0)
  107. goto fail;
  108. s->fd = fd;
  109. if (http_connect(h, path) < 0)
  110. goto fail;
  111. if (s->http_code == 303 && s->location[0] != '\0') {
  112. /* url moved, get next */
  113. uri = s->location;
  114. goto redo;
  115. }
  116. return 0;
  117. fail:
  118. if (fd >= 0)
  119. close(fd);
  120. free(s);
  121. return -EIO;
  122. }
  123. static int http_getc(HTTPContext *s)
  124. {
  125. int len;
  126. if (s->buf_ptr >= s->buf_end) {
  127. redo:
  128. len = read(s->fd, s->buffer, BUFFER_SIZE);
  129. if (len < 0) {
  130. if (errno == EAGAIN || errno == EINTR)
  131. goto redo;
  132. return -EIO;
  133. } else if (len == 0) {
  134. return -1;
  135. } else {
  136. s->buf_ptr = s->buffer;
  137. s->buf_end = s->buffer + len;
  138. }
  139. }
  140. return *s->buf_ptr++;
  141. }
  142. static int process_line(HTTPContext *s, char *line, int line_count)
  143. {
  144. char *tag, *p;
  145. /* end of header */
  146. if (line[0] == '\0')
  147. return 0;
  148. p = line;
  149. if (line_count == 0) {
  150. while (!isspace(*p) && *p != '\0')
  151. p++;
  152. while (isspace(*p))
  153. p++;
  154. s->http_code = strtol(p, NULL, 10);
  155. #ifdef DEBUG
  156. printf("http_code=%d\n", s->http_code);
  157. #endif
  158. } else {
  159. while (*p != '\0' && *p != ':')
  160. p++;
  161. if (*p != ':')
  162. return 1;
  163. *p = '\0';
  164. tag = line;
  165. p++;
  166. while (isspace(*p))
  167. p++;
  168. if (!strcmp(tag, "Location")) {
  169. strcpy(s->location, p);
  170. }
  171. }
  172. return 1;
  173. }
  174. static int http_connect(URLContext *h, const char *path)
  175. {
  176. HTTPContext *s = h->priv_data;
  177. int post, err, ch;
  178. char line[1024], *q;
  179. /* send http header */
  180. post = h->flags & URL_WRONLY;
  181. snprintf(s->buffer, sizeof(s->buffer),
  182. "%s %s HTTP/1.0\n"
  183. "User-Agent: FFmpeg %s\n"
  184. "Accept: */*\n"
  185. "\n",
  186. post ? "POST" : "GET",
  187. path,
  188. FFMPEG_VERSION
  189. );
  190. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  191. return -EIO;
  192. /* init input buffer */
  193. s->buf_ptr = s->buffer;
  194. s->buf_end = s->buffer;
  195. s->line_count = 0;
  196. s->location[0] = '\0';
  197. if (post)
  198. return 0;
  199. /* wait for header */
  200. q = line;
  201. for(;;) {
  202. ch = http_getc(s);
  203. if (ch < 0)
  204. return -EIO;
  205. if (ch == '\n') {
  206. /* process line */
  207. if (q > line && q[-1] == '\r')
  208. q--;
  209. *q = '\0';
  210. #ifdef DEBUG
  211. printf("header='%s'\n", line);
  212. #endif
  213. err = process_line(s, line, s->line_count);
  214. if (err < 0)
  215. return err;
  216. if (err == 0)
  217. return 0;
  218. s->line_count++;
  219. q = line;
  220. } else {
  221. if ((q - line) < sizeof(line) - 1)
  222. *q++ = ch;
  223. }
  224. }
  225. }
  226. static int http_read(URLContext *h, UINT8 *buf, int size)
  227. {
  228. HTTPContext *s = h->priv_data;
  229. int size1, len;
  230. size1 = size;
  231. while (size > 0) {
  232. /* read bytes from input buffer first */
  233. len = s->buf_end - s->buf_ptr;
  234. if (len > 0) {
  235. if (len > size)
  236. len = size;
  237. memcpy(buf, s->buf_ptr, len);
  238. s->buf_ptr += len;
  239. } else {
  240. len = read (s->fd, buf, size);
  241. if (len < 0) {
  242. if (errno != EINTR && errno != EAGAIN)
  243. return -errno;
  244. else
  245. continue;
  246. } else if (len == 0) {
  247. break;
  248. }
  249. }
  250. size -= len;
  251. buf += len;
  252. }
  253. return size1 - size;
  254. }
  255. /* used only when posting data */
  256. static int http_write(URLContext *h, UINT8 *buf, int size)
  257. {
  258. HTTPContext *s = h->priv_data;
  259. int ret, size1;
  260. size1 = size;
  261. while (size > 0) {
  262. ret = write (s->fd, buf, size);
  263. if (ret < 0 && errno != EINTR && errno != EAGAIN)
  264. return -errno;
  265. size -= ret;
  266. buf += ret;
  267. }
  268. return size1 - size;
  269. }
  270. static int http_close(URLContext *h)
  271. {
  272. HTTPContext *s = h->priv_data;
  273. close(s->fd);
  274. return 0;
  275. }
  276. URLProtocol http_protocol = {
  277. "http",
  278. http_open,
  279. http_read,
  280. http_write,
  281. NULL, /* seek */
  282. http_close,
  283. };