From 7942643fb619cde8efbf9a7380d6c073d64ed9ae Mon Sep 17 00:00:00 2001
From: Josh Allmann <joshua.allmann@gmail.com>
Date: Thu, 3 Jun 2010 15:12:11 -0700
Subject: [PATCH 3/6] Added support for disabling chunked transfer encoding.

---
 libavformat/http.c |   14 +++++++++++---
 libavformat/http.h |    8 ++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/libavformat/http.c b/libavformat/http.c
index 175555d..9be921b 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -48,6 +48,7 @@ typedef struct {
     HTTPAuthState auth_state;
     int init;
     unsigned char headers[BUFFER_SIZE];
+    int is_chunked;
 } HTTPContext;
 
 static int http_connect(URLContext *h, const char *path, const char *hoststr,
@@ -60,6 +61,11 @@ void ff_http_set_headers(URLContext *h, const char *headers)
     snprintf(s->headers, sizeof(s->headers), "%s", headers);
 }
 
+void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked)
+{
+    ((HTTPContext*)h->priv_data)->is_chunked = is_chunked;
+}
+
 /* return non zero if error */
 static int http_open_cnx(URLContext *h)
 {
@@ -142,6 +148,7 @@ static int http_open(URLContext *h, const char *uri, int flags)
     h->priv_data = s;
     s->filesize = -1;
     s->chunksize = -1;
+    s->is_chunked = 1;
     s->off = 0;
     s->init = 0;
     *s->headers = '\0';
@@ -288,7 +295,7 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
              post ? "POST" : "GET",
              path,
              LIBAVFORMAT_IDENT,
-             post ? "Transfer-Encoding: chunked\r\n" : "",
+             post && s->is_chunked ? "Transfer-Encoding: chunked\r\n" : "",
              s->headers,
              authstr ? authstr : "");
 
@@ -391,7 +398,7 @@ static int http_read(URLContext *h, uint8_t *buf, int size)
 /* used only when posting data */
 static int http_write(URLContext *h, const uint8_t *buf, int size)
 {
-    char temp[11];  /* 32-bit hex + CRLF + nul */
+    char temp[11] = "";  /* 32-bit hex + CRLF + nul */
     int ret;
     char crlf[] = "\r\n";
     HTTPContext *s = h->priv_data;
@@ -413,11 +420,12 @@ static int http_write(URLContext *h, const uint8_t *buf, int size)
      * signal EOF */
     if (size > 0) {
         /* upload data using chunked encoding */
+        if(s->is_chunked)
         snprintf(temp, sizeof(temp), "%x\r\n", size);
 
         if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
             (ret = url_write(s->hd, buf, size)) < 0 ||
-            (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
+            (s->is_chunked && (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0))
             return ret;
     }
     return size;
diff --git a/libavformat/http.h b/libavformat/http.h
index 7393ae0..e754c7e 100644
--- a/libavformat/http.h
+++ b/libavformat/http.h
@@ -42,4 +42,12 @@
  */
 void ff_http_set_headers(URLContext *h, const char *headers);
 
+/**
+ * Enables or disables chunked transfer encoding. (default is enabled)
+ *
+ * @param h URL context for this HTTP connection
+ * @param is_chunked 0 to disable chunking, nonzero otherwise.
+ */
+void ff_http_set_chunked_transfer_encoding(URLContext *h, int is_chunked);
+
 #endif /* AVFORMAT_HTTP_H */
-- 
1.7.0.4

