]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/http.c
cosmetics: use one type per field in every structs
[ffmpeg] / libavformat / http.c
index da619bbd95685196a3fbb97a5d092145afcd43bf..6a509dc6aa2187561f63b8095fe221f6e97630e9 100644 (file)
@@ -24,8 +24,8 @@
 
 #include "base64.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
 
@@ -96,7 +96,7 @@ 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)
@@ -169,6 +169,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++;
@@ -206,17 +209,15 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
     HTTPContext *s = h->priv_data;
     int post, err, ch;
     char line[1024], *q;
-    char *auth_b64 = av_malloc(strlen(auth) * 4 / 3 + 12);
+    char *auth_b64;
+    int auth_b64_len = strlen(auth)* 4 / 3 + 12;
     offset_t off = s->off;
 
-    if (auth_b64 == NULL) return AVERROR(ENOMEM);
 
     /* send http header */
     post = h->flags & URL_WRONLY;
-
-    auth_b64 = av_base64_encode(auth_b64, strlen(auth) * 4 / 3 + 12,
-                                (uint8_t *)auth, strlen(auth));
-
+    auth_b64 = av_malloc(auth_b64_len);
+    av_base64_encode(auth_b64, auth_b64_len, (uint8_t *)auth, strlen(auth));
     snprintf(s->buffer, sizeof(s->buffer),
              "%s %s HTTP/1.1\r\n"
              "User-Agent: %s\r\n"
@@ -224,6 +225,7 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
              "Range: bytes=%"PRId64"-\r\n"
              "Host: %s\r\n"
              "Authorization: Basic %s\r\n"
+             "Connection: close\r\n"
              "\r\n",
              post ? "POST" : "GET",
              path,
@@ -241,8 +243,9 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
     s->buf_end = s->buffer;
     s->line_count = 0;
     s->off = 0;
+    s->filesize = -1;
     if (post) {
-        sleep(1);
+        usleep(1000*1000);
         return 0;
     }