Browse code

http: Make custom headers settable via an AVOption

Signed-off-by: Martin Storsjö <martin@martin.st>

Martin Storsjö authored on 2011/11/07 18:43:13
Showing 1 changed files
... ...
@@ -47,13 +47,14 @@ typedef struct {
47 47
     int64_t off, filesize;
48 48
     char location[MAX_URL_SIZE];
49 49
     HTTPAuthState auth_state;
50
-    unsigned char headers[BUFFER_SIZE];
50
+    char *headers;
51 51
     int willclose;          /**< Set if the server correctly handles Connection: close and will close the connection after feeding us the content. */
52 52
 } HTTPContext;
53 53
 
54 54
 #define OFFSET(x) offsetof(HTTPContext, x)
55 55
 static const AVOption options[] = {
56 56
 {"chunksize", "use chunked transfer-encoding for posts, -1 disables it, 0 enables it", OFFSET(chunksize), AV_OPT_TYPE_INT64, {.dbl = 0}, -1, 0 }, /* Default to 0, for chunked POSTs */
57
+{"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING },
57 58
 {NULL}
58 59
 };
59 60
 static const AVClass httpcontext_class = {
... ...
@@ -69,12 +70,9 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
69 69
 void ff_http_set_headers(URLContext *h, const char *headers)
70 70
 {
71 71
     HTTPContext *s = h->priv_data;
72
-    int len = strlen(headers);
73 72
 
74
-    if (len && strcmp("\r\n", headers + len - 2))
75
-        av_log(h, AV_LOG_ERROR, "No trailing CRLF found in HTTP header.\n");
76
-
77
-    av_strlcpy(s->headers, headers, sizeof(s->headers));
73
+    av_freep(&s->headers);
74
+    s->headers = av_strdup(headers);
78 75
 }
79 76
 
80 77
 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
... ...
@@ -168,6 +166,12 @@ static int http_open(URLContext *h, const char *uri, int flags)
168 168
     s->filesize = -1;
169 169
     av_strlcpy(s->location, uri, sizeof(s->location));
170 170
 
171
+    if (s->headers) {
172
+        int len = strlen(s->headers);
173
+        if (len < 2 || strcmp("\r\n", s->headers + len - 2))
174
+            av_log(h, AV_LOG_ERROR, "No trailing CRLF found in HTTP header.\n");
175
+    }
176
+
171 177
     return http_open_cnx(h);
172 178
 }
173 179
 static int http_getc(HTTPContext *s)
... ...
@@ -285,6 +289,8 @@ static int process_line(URLContext *h, char *line, int line_count,
285 285
 static inline int has_header(const char *str, const char *header)
286 286
 {
287 287
     /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
288
+    if (!str)
289
+        return 0;
288 290
     return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
289 291
 }
290 292
 
... ...
@@ -323,7 +329,8 @@ static int http_connect(URLContext *h, const char *path, const char *hoststr,
323 323
                            "Host: %s\r\n", hoststr);
324 324
 
325 325
     /* now add in custom headers */
326
-    av_strlcpy(headers+len, s->headers, sizeof(headers)-len);
326
+    if (s->headers)
327
+        av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
327 328
 
328 329
     snprintf(s->buffer, sizeof(s->buffer),
329 330
              "%s %s HTTP/1.1\r\n"