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.

280 lines
6.5KB

  1. /*
  2. * HTTP protocol for ffmpeg client
  3. * Copyright (c) 2000, 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <unistd.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #ifndef __BEOS__
  25. # include <arpa/inet.h>
  26. #else
  27. # include "barpainet.h"
  28. #endif
  29. #include <netdb.h>
  30. /* XXX: POST protocol is not completly implemented because ffmpeg use
  31. only a subset of it */
  32. //#define DEBUG
  33. /* used for protocol handling */
  34. #define BUFFER_SIZE 1024
  35. #define URL_SIZE 4096
  36. typedef struct {
  37. URLContext *hd;
  38. unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
  39. int line_count;
  40. int http_code;
  41. char location[URL_SIZE];
  42. } HTTPContext;
  43. static int http_connect(URLContext *h, const char *path, const char *hoststr);
  44. static int http_write(URLContext *h, uint8_t *buf, int size);
  45. /* return non zero if error */
  46. static int http_open(URLContext *h, const char *uri, int flags)
  47. {
  48. const char *path, *proxy_path;
  49. char hostname[1024], hoststr[1024];
  50. char path1[1024];
  51. char buf[1024];
  52. int port, use_proxy, err;
  53. HTTPContext *s;
  54. URLContext *hd = NULL;
  55. h->is_streamed = 1;
  56. s = av_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. strstart(proxy_path, "http://", NULL);
  64. /* fill the dest addr */
  65. redo:
  66. /* needed in any case to build the host string */
  67. url_split(NULL, 0, hostname, sizeof(hostname), &port,
  68. path1, sizeof(path1), uri);
  69. if (port > 0) {
  70. snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
  71. } else {
  72. pstrcpy(hoststr, sizeof(hoststr), hostname);
  73. }
  74. if (use_proxy) {
  75. url_split(NULL, 0, hostname, sizeof(hostname), &port,
  76. NULL, 0, proxy_path);
  77. path = uri;
  78. } else {
  79. if (path1[0] == '\0')
  80. path = "/";
  81. else
  82. path = path1;
  83. }
  84. if (port < 0)
  85. port = 80;
  86. snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
  87. err = url_open(&hd, buf, URL_RDWR);
  88. if (err < 0)
  89. goto fail;
  90. s->hd = hd;
  91. if (http_connect(h, path, hoststr) < 0)
  92. goto fail;
  93. if (s->http_code == 303 && s->location[0] != '\0') {
  94. /* url moved, get next */
  95. uri = s->location;
  96. url_close(hd);
  97. goto redo;
  98. }
  99. return 0;
  100. fail:
  101. if (hd)
  102. url_close(hd);
  103. av_free(s);
  104. return -EIO;
  105. }
  106. static int http_getc(HTTPContext *s)
  107. {
  108. int len;
  109. if (s->buf_ptr >= s->buf_end) {
  110. len = url_read(s->hd, s->buffer, BUFFER_SIZE);
  111. if (len < 0) {
  112. return -EIO;
  113. } else if (len == 0) {
  114. return -1;
  115. } else {
  116. s->buf_ptr = s->buffer;
  117. s->buf_end = s->buffer + len;
  118. }
  119. }
  120. return *s->buf_ptr++;
  121. }
  122. static int process_line(HTTPContext *s, char *line, int line_count)
  123. {
  124. char *tag, *p;
  125. /* end of header */
  126. if (line[0] == '\0')
  127. return 0;
  128. p = line;
  129. if (line_count == 0) {
  130. while (!isspace(*p) && *p != '\0')
  131. p++;
  132. while (isspace(*p))
  133. p++;
  134. s->http_code = strtol(p, NULL, 10);
  135. #ifdef DEBUG
  136. printf("http_code=%d\n", s->http_code);
  137. #endif
  138. } else {
  139. while (*p != '\0' && *p != ':')
  140. p++;
  141. if (*p != ':')
  142. return 1;
  143. *p = '\0';
  144. tag = line;
  145. p++;
  146. while (isspace(*p))
  147. p++;
  148. if (!strcmp(tag, "Location")) {
  149. strcpy(s->location, p);
  150. }
  151. }
  152. return 1;
  153. }
  154. static int http_connect(URLContext *h, const char *path, const char *hoststr)
  155. {
  156. HTTPContext *s = h->priv_data;
  157. int post, err, ch;
  158. char line[1024], *q;
  159. /* send http header */
  160. post = h->flags & URL_WRONLY;
  161. snprintf(s->buffer, sizeof(s->buffer),
  162. "%s %s HTTP/1.0\r\n"
  163. "User-Agent: %s\r\n"
  164. "Accept: */*\r\n"
  165. "Host: %s\r\n"
  166. "\r\n",
  167. post ? "POST" : "GET",
  168. path,
  169. LIBAVFORMAT_IDENT,
  170. hoststr);
  171. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  172. return -EIO;
  173. /* init input buffer */
  174. s->buf_ptr = s->buffer;
  175. s->buf_end = s->buffer;
  176. s->line_count = 0;
  177. s->location[0] = '\0';
  178. if (post) {
  179. sleep(1);
  180. return 0;
  181. }
  182. /* wait for header */
  183. q = line;
  184. for(;;) {
  185. ch = http_getc(s);
  186. if (ch < 0)
  187. return -EIO;
  188. if (ch == '\n') {
  189. /* process line */
  190. if (q > line && q[-1] == '\r')
  191. q--;
  192. *q = '\0';
  193. #ifdef DEBUG
  194. printf("header='%s'\n", line);
  195. #endif
  196. err = process_line(s, line, s->line_count);
  197. if (err < 0)
  198. return err;
  199. if (err == 0)
  200. return 0;
  201. s->line_count++;
  202. q = line;
  203. } else {
  204. if ((q - line) < sizeof(line) - 1)
  205. *q++ = ch;
  206. }
  207. }
  208. }
  209. static int http_read(URLContext *h, uint8_t *buf, int size)
  210. {
  211. HTTPContext *s = h->priv_data;
  212. int len;
  213. /* read bytes from input buffer first */
  214. len = s->buf_end - s->buf_ptr;
  215. if (len > 0) {
  216. if (len > size)
  217. len = size;
  218. memcpy(buf, s->buf_ptr, len);
  219. s->buf_ptr += len;
  220. } else {
  221. len = url_read(s->hd, buf, size);
  222. }
  223. return len;
  224. }
  225. /* used only when posting data */
  226. static int http_write(URLContext *h, uint8_t *buf, int size)
  227. {
  228. HTTPContext *s = h->priv_data;
  229. return url_write(s->hd, buf, size);
  230. }
  231. static int http_close(URLContext *h)
  232. {
  233. HTTPContext *s = h->priv_data;
  234. url_close(s->hd);
  235. av_free(s);
  236. return 0;
  237. }
  238. URLProtocol http_protocol = {
  239. "http",
  240. http_open,
  241. http_read,
  242. http_write,
  243. NULL, /* seek */
  244. http_close,
  245. };