[PATCH] http Transfer-Encoding chunked

Peter Holik peter
Mon May 25 16:18:42 CEST 2009


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

diff --git a/libavformat/http.c b/libavformat/http.c
index d904e7a..d4db856 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -41,6 +41,8 @@ typedef struct {
     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
     int line_count;
     int http_code;
+    int chunked;
+    int chunk_size;
     int64_t off, filesize;
     char location[URL_SIZE];
 } HTTPContext;
@@ -200,6 +202,10 @@ 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") && !strncmp(p, "chunked", 7)) {
+            s->filesize = -1;
+            s->chunk_size = 0;
+            s->chunked = 1;
         }
     }
     return 1;
@@ -286,6 +292,37 @@ static int http_read(URLContext *h, uint8_t *buf, int size)
     HTTPContext *s = h->priv_data;
     int len;
 
+    if (s->chunked) {
+        if (!s->chunk_size) {
+            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->chunk_size = (int) strtol(line, NULL, 16);
+
+                    if (s->chunk_size == 0)
+                        return 0;
+                    break;
+                } else
+                    if ((q - line) < sizeof(line) - 1)
+                        *q++ = ch;
+            }
+        }
+        if (s->chunk_size < size)
+            size = s->chunk_size;
+    }
     /* read bytes from input buffer first */
     len = s->buf_end - s->buf_ptr;
     if (len > 0) {
@@ -296,8 +333,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->chunked)
+            s->chunk_size -= len;
+    }
     return len;
 }
 
-- 
1.6.3.1

------=_20090525162838_62736--





More information about the ffmpeg-devel mailing list