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.

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