From ec6ad739addcd33d829cd1bac3ea63d2f11d2936 Mon Sep 17 00:00:00 2001
From: Josh Allmann <joshua.allmann@gmail.com>
Date: Sat, 5 Jun 2010 14:42:04 -0700
Subject: [PATCH 2/5] Added in capability to write custom HTTP headers.

---
 libavformat/http.c |   45 +++++++++++++++++++++++++++++++++++----------
 libavformat/http.h |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 10 deletions(-)
 create mode 100644 libavformat/http.h

diff --git a/libavformat/http.c b/libavformat/http.c
index 68ad2ce..d3861ab 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -25,6 +25,7 @@
 #include <strings.h>
 #include "internal.h"
 #include "network.h"
+#include "http.h"
 #include "os_support.h"
 #include "httpauth.h"
 
@@ -46,12 +47,23 @@ typedef struct {
     char location[URL_SIZE];
     HTTPAuthState auth_state;
     int init;
+    unsigned char headers[BUFFER_SIZE];
 } HTTPContext;
 
 static int http_connect(URLContext *h, const char *path, const char *hoststr,
                         const char *auth, int *new_location);
 static int http_write(URLContext *h, const uint8_t *buf, int size);
 
+void ff_http_set_headers(URLContext *h, const char *headers)
+{
+    HTTPContext *s = h->priv_data;
+    int len = strlen(headers);
+
+    if (len && strcmp("\r\n", headers+len-2))
+        av_log(NULL, AV_LOG_ERROR, "No trailing CRLF found in HTTP header.\n");
+
+    snprintf(s->headers, sizeof(s->headers), "%s", headers);
+}
 
 /* return non zero if error */
 static int http_open_cnx(URLContext *h)
@@ -137,6 +149,7 @@ static int http_open(URLContext *h, const char *uri, int flags)
     s->chunksize = -1;
     s->off = 0;
     s->init = 0;
+    *s->headers = '\0';
     memset(&s->auth_state, 0, sizeof(s->auth_state));
     av_strlcpy(s->location, uri, URL_SIZE);
 
@@ -251,31 +264,43 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
     HTTPContext *s = h->priv_data;
     int post, err;
     char line[1024];
+    char headers[1024];
     char *authstr = NULL;
     int64_t off = s->off;
+    int len = 0;
 
 
     /* send http header */
     post = h->flags & URL_WRONLY;
     authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
                                         post ? "POST" : "GET");
+
+     /* set default headers if needed */
+     if(!strstr(s->headers, "\r\nUser-Agent:"))
+        len += snprintf(headers+len, sizeof(headers)-len, "User-Agent: %s\r\n", LIBAVFORMAT_IDENT);
+    if(!strstr(s->headers, "\r\nAccept:"))
+        len = snprintf(headers, sizeof(headers), "Accept: */*\r\n");
+    if(!strstr(s->headers, "\r\nRange:"))
+        len += snprintf(headers+len, sizeof(headers)-len, "Range: bytes=%"PRId64"\r\n", s->off);
+    if(!strstr(s->headers, "\r\nConnection:"))
+        len += snprintf(headers+len, sizeof(headers)-len, "Connection: close\r\n");
+    if(!strstr(s->headers, "\r\nHost:"))
+        len += snprintf(headers+len, sizeof(headers)-len, "Host: %s\r\n", hoststr);
+
+    /* now add in custom headers */
+    snprintf(headers+len, sizeof(headers)-len, "%s", s->headers);
+
     snprintf(s->buffer, sizeof(s->buffer),
              "%s %s HTTP/1.1\r\n"
-             "User-Agent: %s\r\n"
-             "Accept: */*\r\n"
-             "Range: bytes=%"PRId64"-\r\n"
-             "Host: %s\r\n"
              "%s"
-             "Connection: close\r\n"
+             "%s"
              "%s"
              "\r\n",
              post ? "POST" : "GET",
              path,
-             LIBAVFORMAT_IDENT,
-             s->off,
-             hoststr,
-             authstr ? authstr : "",
-             post ? "Transfer-Encoding: chunked\r\n" : "");
+             post ? "Transfer-Encoding: chunked\r\n" : "",
+             headers,
+             authstr ? authstr : "");
 
     av_freep(&authstr);
     if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
diff --git a/libavformat/http.h b/libavformat/http.h
new file mode 100644
index 0000000..2cb1f2c
--- /dev/null
+++ b/libavformat/http.h
@@ -0,0 +1,50 @@
+/*
+ * HTTP definitions
+ * Copyright (c) 2010 Josh Allmann
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#ifndef AVFORMAT_HTTP_H
+#define AVFORMAT_HTTP_H
+
+/**
+ * Sets custom HTTP headers. Note this overwrites all default options.
+ * A trailing CRLF ("\r\n") is required for custom headers.
+ * Passing in an empty header string ("\0") will reset to defaults.
+ *
+ * The following headers will always be set, no matter what:
+ *  -HTTP verb, path, HTTP version
+ *  -User-Agent
+ *  -Transfer-Encoding (if applicable)
+ *  - Authorization (if applicable)
+ *
+ * The following headers are set by default, and will be overwritten if
+ * custom headers are set. Be sure to re-specify them if needed.
+ *  -Accept
+ *  -Range
+ *  -Host
+ *  -Connection
+ *
+ * If seeking is needed with custom headers, Range will need
+ * to be manually specified before each request.
+ *
+ * @param h URL context for this HTTP connection
+ * @param is_chunked 0 to disable chunking, nonzero otherwise.
+ */
+void ff_http_set_headers(URLContext *h, const char *headers);
+
+#endif /* AVFORMAT_HTTP_H */
-- 
1.7.0.4

