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.

291 lines
6.8KB

  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 <ctype.h>
  22. #include <sys/types.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #ifndef __BEOS__
  26. # include <arpa/inet.h>
  27. #else
  28. # include "barpainet.h"
  29. #endif
  30. #include <netdb.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. URLContext *hd;
  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, const char *hoststr);
  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. const char *path, *proxy_path;
  50. char hostname[1024], hoststr[1024];
  51. char path1[1024];
  52. char buf[1024];
  53. int port, use_proxy, err;
  54. HTTPContext *s;
  55. URLContext *hd = NULL;
  56. h->is_streamed = 1;
  57. s = av_malloc(sizeof(HTTPContext));
  58. if (!s) {
  59. return -ENOMEM;
  60. }
  61. h->priv_data = s;
  62. proxy_path = getenv("http_proxy");
  63. use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
  64. strstart(proxy_path, "http://", NULL);
  65. /* fill the dest addr */
  66. redo:
  67. /* needed in any case to build the host string */
  68. url_split(NULL, 0, hostname, sizeof(hostname), &port,
  69. path1, sizeof(path1), uri);
  70. if (port > 0) {
  71. snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
  72. } else {
  73. pstrcpy(hoststr, sizeof(hoststr), hostname);
  74. }
  75. if (use_proxy) {
  76. url_split(NULL, 0, hostname, sizeof(hostname), &port,
  77. NULL, 0, proxy_path);
  78. path = uri;
  79. } else {
  80. if (path1[0] == '\0')
  81. path = "/";
  82. else
  83. path = path1;
  84. }
  85. if (port < 0)
  86. port = 80;
  87. snprintf(buf, sizeof(buf), "tcp://%s:%d", hostname, port);
  88. err = url_open(&hd, buf, URL_RDWR);
  89. if (err < 0)
  90. goto fail;
  91. s->hd = hd;
  92. if (http_connect(h, path, hoststr) < 0)
  93. goto fail;
  94. if (s->http_code == 303 && s->location[0] != '\0') {
  95. /* url moved, get next */
  96. uri = s->location;
  97. url_close(hd);
  98. goto redo;
  99. }
  100. return 0;
  101. fail:
  102. if (hd)
  103. url_close(hd);
  104. av_free(s);
  105. return -EIO;
  106. }
  107. static int http_getc(HTTPContext *s)
  108. {
  109. int len;
  110. if (s->buf_ptr >= s->buf_end) {
  111. len = url_read(s->hd, s->buffer, BUFFER_SIZE);
  112. if (len < 0) {
  113. return -EIO;
  114. } else if (len == 0) {
  115. return -1;
  116. } else {
  117. s->buf_ptr = s->buffer;
  118. s->buf_end = s->buffer + len;
  119. }
  120. }
  121. return *s->buf_ptr++;
  122. }
  123. static int process_line(HTTPContext *s, char *line, int line_count)
  124. {
  125. char *tag, *p;
  126. /* end of header */
  127. if (line[0] == '\0')
  128. return 0;
  129. p = line;
  130. if (line_count == 0) {
  131. while (!isspace(*p) && *p != '\0')
  132. p++;
  133. while (isspace(*p))
  134. p++;
  135. s->http_code = strtol(p, NULL, 10);
  136. #ifdef DEBUG
  137. printf("http_code=%d\n", s->http_code);
  138. #endif
  139. } else {
  140. while (*p != '\0' && *p != ':')
  141. p++;
  142. if (*p != ':')
  143. return 1;
  144. *p = '\0';
  145. tag = line;
  146. p++;
  147. while (isspace(*p))
  148. p++;
  149. if (!strcmp(tag, "Location")) {
  150. strcpy(s->location, p);
  151. }
  152. }
  153. return 1;
  154. }
  155. static int http_connect(URLContext *h, const char *path, const char *hoststr)
  156. {
  157. HTTPContext *s = h->priv_data;
  158. int post, err, ch;
  159. char line[1024], *q;
  160. /* send http header */
  161. post = h->flags & URL_WRONLY;
  162. snprintf(s->buffer, sizeof(s->buffer),
  163. "%s %s HTTP/1.0\n"
  164. "User-Agent: FFmpeg %s\n"
  165. "Accept: */*\n"
  166. "Host: %s\n"
  167. "\n",
  168. post ? "POST" : "GET",
  169. path,
  170. FFMPEG_VERSION,
  171. hoststr);
  172. if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
  173. return -EIO;
  174. /* init input buffer */
  175. s->buf_ptr = s->buffer;
  176. s->buf_end = s->buffer;
  177. s->line_count = 0;
  178. s->location[0] = '\0';
  179. if (post) {
  180. sleep(1);
  181. return 0;
  182. }
  183. /* wait for header */
  184. q = line;
  185. for(;;) {
  186. ch = http_getc(s);
  187. if (ch < 0)
  188. return -EIO;
  189. if (ch == '\n') {
  190. /* process line */
  191. if (q > line && q[-1] == '\r')
  192. q--;
  193. *q = '\0';
  194. #ifdef DEBUG
  195. printf("header='%s'\n", line);
  196. #endif
  197. err = process_line(s, line, s->line_count);
  198. if (err < 0)
  199. return err;
  200. if (err == 0)
  201. return 0;
  202. s->line_count++;
  203. q = line;
  204. } else {
  205. if ((q - line) < sizeof(line) - 1)
  206. *q++ = ch;
  207. }
  208. }
  209. }
  210. static int http_read(URLContext *h, UINT8 *buf, int size)
  211. {
  212. HTTPContext *s = h->priv_data;
  213. int size1, len;
  214. size1 = size;
  215. while (size > 0) {
  216. /* read bytes from input buffer first */
  217. len = s->buf_end - s->buf_ptr;
  218. if (len > 0) {
  219. if (len > size)
  220. len = size;
  221. memcpy(buf, s->buf_ptr, len);
  222. s->buf_ptr += len;
  223. } else {
  224. len = url_read (s->hd, buf, size);
  225. if (len < 0) {
  226. return len;
  227. } else if (len == 0) {
  228. break;
  229. }
  230. }
  231. size -= len;
  232. buf += len;
  233. }
  234. return size1 - size;
  235. }
  236. /* used only when posting data */
  237. static int http_write(URLContext *h, UINT8 *buf, int size)
  238. {
  239. HTTPContext *s = h->priv_data;
  240. return url_write(s->hd, buf, size);
  241. }
  242. static int http_close(URLContext *h)
  243. {
  244. HTTPContext *s = h->priv_data;
  245. url_close(s->hd);
  246. av_free(s);
  247. return 0;
  248. }
  249. URLProtocol http_protocol = {
  250. "http",
  251. http_open,
  252. http_read,
  253. http_write,
  254. NULL, /* seek */
  255. http_close,
  256. };