[PATCH] http Transfer-Encoding chunked

Peter Holik peter
Tue May 26 09:48:09 CEST 2009


Signed-off-by: Peter Holik <peter at holik.at>
---
 libavformat/http.c |   44 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 43 insertions(+), 1 deletions(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index d904e7a..298f3eb 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -23,6 +23,7 @@
 #include "libavutil/avstring.h"
 #include "avformat.h"
 #include <unistd.h>
+#include <strings.h>
 #include "network.h"
 #include "os_support.h"
 
@@ -41,6 +42,7 @@ typedef struct {
     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
     int line_count;
     int http_code;
+    int64_t chunksize;      /**< Used if "Transfer-Encoding: chunked" otherwise -1. */
     int64_t off, filesize;
     char location[URL_SIZE];
 } HTTPContext;
@@ -126,6 +128,7 @@ static int http_open(URLContext *h, const char *uri, int flags)
     }
     h->priv_data = s;
     s->filesize = -1;
+    s->chunksize = -1;
     s->off = 0;
     av_strlcpy(s->location, uri, URL_SIZE);
 
@@ -200,6 +203,9 @@ static int process_line(URLContext *h, char *line, int line_count,
                     s->filesize = atoll(slash+1);
             }
             h->is_streamed = 0; /* we _can_ in fact seek */
+        } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
+            s->filesize = -1;
+            s->chunksize = 0;
         }
     }
     return 1;
@@ -286,6 +292,39 @@ static int http_read(URLContext *h, uint8_t *buf, int size)
     HTTPContext *s = h->priv_data;
     int len;
 
+    if (s->chunksize >= 0) {
+        if (!s->chunksize) {
+            int ch;
+            char line[32], *q;
+
+            q = line;
+            for(;;) {
+                ch = http_getc(s);
+                if (ch < 0)
+                    return 0;
+                if (ch == '\n') {
+                    /* process chunk size */
+                    if (q > line && q[-1] == '\r')
+                         q--;
+                    *q = '\0';
+                    /* skip CR LF from last chunk */
+                    if (!(*line)) continue;
+
+                    s->chunksize = strtoll(line, NULL, 16);
+#ifdef DEBUG
+            printf("chunksize='%"PRId64"'\n", s->chunksize);
+#endif
+                    if (!s->chunksize)
+                        return 0;
+                    break;
+                } else
+                    if ((q - line) < sizeof(line) - 1)
+                        *q++ = ch;
+            }
+        }
+        if (s->chunksize < size)
+            size = s->chunksize;
+    }
     /* read bytes from input buffer first */
     len = s->buf_end - s->buf_ptr;
     if (len > 0) {
@@ -296,8 +335,11 @@ static int http_read(URLContext *h, uint8_t *buf, int size)
     } else {
         len = url_read(s->hd, buf, size);
     }
-    if (len > 0)
+    if (len > 0) {
         s->off += len;
+        if (s->chunksize > 0)
+            s->chunksize -= len;
+    }
     return len;
 }
 
-- 
1.6.3.1

------=_20090526095211_13984--





More information about the ffmpeg-devel mailing list