]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/http.c
Typo
[ffmpeg] / libavformat / http.c
index f150f6684cf182d60e8bf4b534498eb4b9f44e2c..1d542061bdc388ffe474afa500f289aa39df528d 100644 (file)
  */
 #include "avformat.h"
 #include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#ifndef __BEOS__
-# include <arpa/inet.h>
-#else
-# include "barpainet.h"
-#endif
-#include <netdb.h>
+#include "network.h"
+#include "os_support.h"
 
 #include "base64.h"
+#include "avstring.h"
 
-/* XXX: POST protocol is not completly implemented because ffmpeg use
-   only a subset of it */
+/* XXX: POST protocol is not completely implemented because ffmpeg uses
+   only a subset of it. */
 
 //#define DEBUG
 
@@ -70,7 +64,7 @@ static int http_open_cnx(URLContext *h)
 
     proxy_path = getenv("http_proxy");
     use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
-        strstart(proxy_path, "http://", NULL);
+        av_strstart(proxy_path, "http://", NULL);
 
     /* fill the dest addr */
  redo:
@@ -80,7 +74,7 @@ static int http_open_cnx(URLContext *h)
     if (port > 0) {
         snprintf(hoststr, sizeof(hoststr), "%s:%d", hostname, port);
     } else {
-        pstrcpy(hoststr, sizeof(hoststr), hostname);
+        av_strlcpy(hoststr, hostname, sizeof(hoststr));
     }
 
     if (use_proxy) {
@@ -104,11 +98,11 @@ static int http_open_cnx(URLContext *h)
     s->hd = hd;
     if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
         goto fail;
-    if (s->http_code == 303 && location_changed == 1) {
+    if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
         /* url moved, get next */
         url_close(hd);
         if (redirects++ >= MAX_REDIRECTS)
-            return AVERROR_IO;
+            return AVERROR(EIO);
         location_changed = 0;
         goto redo;
     }
@@ -116,7 +110,7 @@ static int http_open_cnx(URLContext *h)
  fail:
     if (hd)
         url_close(hd);
-    return AVERROR_IO;
+    return AVERROR(EIO);
 }
 
 static int http_open(URLContext *h, const char *uri, int flags)
@@ -128,12 +122,12 @@ static int http_open(URLContext *h, const char *uri, int flags)
 
     s = av_malloc(sizeof(HTTPContext));
     if (!s) {
-        return -ENOMEM;
+        return AVERROR(ENOMEM);
     }
     h->priv_data = s;
     s->filesize = -1;
     s->off = 0;
-    pstrcpy (s->location, URL_SIZE, uri);
+    av_strlcpy(s->location, uri, URL_SIZE);
 
     ret = http_open_cnx(h);
     if (ret != 0)
@@ -146,7 +140,7 @@ static int http_getc(HTTPContext *s)
     if (s->buf_ptr >= s->buf_end) {
         len = url_read(s->hd, s->buffer, BUFFER_SIZE);
         if (len < 0) {
-            return AVERROR_IO;
+            return AVERROR(EIO);
         } else if (len == 0) {
             return -1;
         } else {
@@ -177,6 +171,9 @@ static int process_line(URLContext *h, char *line, int line_count,
 #ifdef DEBUG
         printf("http_code=%d\n", s->http_code);
 #endif
+        /* error codes are 4xx and 5xx */
+        if (s->http_code >= 400 && s->http_code < 600)
+            return -1;
     } else {
         while (*p != '\0' && *p != ':')
             p++;
@@ -215,20 +212,22 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
     int post, err, ch;
     char line[1024], *q;
     char *auth_b64;
+    int auth_b64_len = strlen(auth)* 4 / 3 + 12;
     offset_t off = s->off;
 
 
     /* send http header */
     post = h->flags & URL_WRONLY;
-
-    auth_b64 = av_base64_encode((uint8_t *)auth, strlen(auth));
+    auth_b64 = av_malloc(auth_b64_len);
+    av_base64_encode(auth_b64, auth_b64_len, auth, strlen(auth));
     snprintf(s->buffer, sizeof(s->buffer),
              "%s %s HTTP/1.1\r\n"
              "User-Agent: %s\r\n"
              "Accept: */*\r\n"
-             "Range: bytes=%llu-\r\n"
+             "Range: bytes=%"PRId64"-\r\n"
              "Host: %s\r\n"
              "Authorization: Basic %s\r\n"
+             "Connection: close\r\n"
              "\r\n",
              post ? "POST" : "GET",
              path,
@@ -239,15 +238,15 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
 
     av_freep(&auth_b64);
     if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
-        return AVERROR_IO;
+        return AVERROR(EIO);
 
     /* init input buffer */
     s->buf_ptr = s->buffer;
     s->buf_end = s->buffer;
     s->line_count = 0;
     s->off = 0;
+    s->filesize = -1;
     if (post) {
-        sleep(1);
         return 0;
     }
 
@@ -256,7 +255,7 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
     for(;;) {
         ch = http_getc(s);
         if (ch < 0)
-            return AVERROR_IO;
+            return AVERROR(EIO);
         if (ch == '\n') {
             /* process line */
             if (q > line && q[-1] == '\r')