• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/http.c

Go to the documentation of this file.
00001 /*
00002  * HTTP protocol for ffmpeg client
00003  * Copyright (c) 2000, 2001 Fabrice Bellard
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/avstring.h"
00023 #include "avformat.h"
00024 #include <unistd.h>
00025 #include "internal.h"
00026 #include "network.h"
00027 #include "http.h"
00028 #include "os_support.h"
00029 #include "httpauth.h"
00030 #include "url.h"
00031 #include "libavutil/opt.h"
00032 
00033 /* XXX: POST protocol is not completely implemented because ffmpeg uses
00034    only a subset of it. */
00035 
00036 /* used for protocol handling */
00037 #define BUFFER_SIZE 1024
00038 #define MAX_REDIRECTS 8
00039 
00040 typedef struct {
00041     const AVClass *class;
00042     URLContext *hd;
00043     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
00044     int line_count;
00045     int http_code;
00046     int64_t chunksize;      
00047     char *user_agent;
00048     int64_t off, filesize;
00049     char location[MAX_URL_SIZE];
00050     HTTPAuthState auth_state;
00051     HTTPAuthState proxy_auth_state;
00052     char *headers;
00053     int willclose;          
00054     int chunked_post;
00055 } HTTPContext;
00056 
00057 #define OFFSET(x) offsetof(HTTPContext, x)
00058 #define D AV_OPT_FLAG_DECODING_PARAM
00059 #define E AV_OPT_FLAG_ENCODING_PARAM
00060 #define DEC AV_OPT_FLAG_DECODING_PARAM
00061 static const AVOption options[] = {
00062 {"chunked_post", "use chunked transfer-encoding for posts", OFFSET(chunked_post), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1, E },
00063 {"headers", "custom HTTP headers, can override built in default headers", OFFSET(headers), AV_OPT_TYPE_STRING, { 0 }, 0, 0, D|E },
00064 {"user-agent", "override User-Agent header", OFFSET(user_agent), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, DEC},
00065 {NULL}
00066 };
00067 #define HTTP_CLASS(flavor)\
00068 static const AVClass flavor ## _context_class = {\
00069     .class_name     = #flavor,\
00070     .item_name      = av_default_item_name,\
00071     .option         = options,\
00072     .version        = LIBAVUTIL_VERSION_INT,\
00073 }
00074 
00075 HTTP_CLASS(http);
00076 HTTP_CLASS(https);
00077 
00078 static int http_connect(URLContext *h, const char *path, const char *local_path,
00079                         const char *hoststr, const char *auth,
00080                         const char *proxyauth, int *new_location);
00081 
00082 void ff_http_init_auth_state(URLContext *dest, const URLContext *src)
00083 {
00084     memcpy(&((HTTPContext*)dest->priv_data)->auth_state,
00085            &((HTTPContext*)src->priv_data)->auth_state, sizeof(HTTPAuthState));
00086     memcpy(&((HTTPContext*)dest->priv_data)->proxy_auth_state,
00087            &((HTTPContext*)src->priv_data)->proxy_auth_state,
00088            sizeof(HTTPAuthState));
00089 }
00090 
00091 /* return non zero if error */
00092 static int http_open_cnx(URLContext *h)
00093 {
00094     const char *path, *proxy_path, *lower_proto = "tcp", *local_path;
00095     char hostname[1024], hoststr[1024], proto[10];
00096     char auth[1024], proxyauth[1024] = "";
00097     char path1[1024];
00098     char buf[1024], urlbuf[1024];
00099     int port, use_proxy, err, location_changed = 0, redirects = 0;
00100     HTTPAuthType cur_auth_type, cur_proxy_auth_type;
00101     HTTPContext *s = h->priv_data;
00102     URLContext *hd = NULL;
00103 
00104     proxy_path = getenv("http_proxy");
00105     use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
00106         av_strstart(proxy_path, "http://", NULL);
00107 
00108     /* fill the dest addr */
00109  redo:
00110     /* needed in any case to build the host string */
00111     av_url_split(proto, sizeof(proto), auth, sizeof(auth),
00112                  hostname, sizeof(hostname), &port,
00113                  path1, sizeof(path1), s->location);
00114     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
00115 
00116     if (!strcmp(proto, "https")) {
00117         lower_proto = "tls";
00118         use_proxy = 0;
00119         if (port < 0)
00120             port = 443;
00121     }
00122     if (port < 0)
00123         port = 80;
00124 
00125     if (path1[0] == '\0')
00126         path = "/";
00127     else
00128         path = path1;
00129     local_path = path;
00130     if (use_proxy) {
00131         /* Reassemble the request URL without auth string - we don't
00132          * want to leak the auth to the proxy. */
00133         ff_url_join(urlbuf, sizeof(urlbuf), proto, NULL, hostname, port, "%s",
00134                     path1);
00135         path = urlbuf;
00136         av_url_split(NULL, 0, proxyauth, sizeof(proxyauth),
00137                      hostname, sizeof(hostname), &port, NULL, 0, proxy_path);
00138     }
00139 
00140     ff_url_join(buf, sizeof(buf), lower_proto, NULL, hostname, port, NULL);
00141     err = ffurl_open(&hd, buf, AVIO_FLAG_READ_WRITE,
00142                      &h->interrupt_callback, NULL);
00143     if (err < 0)
00144         goto fail;
00145 
00146     s->hd = hd;
00147     cur_auth_type = s->auth_state.auth_type;
00148     cur_proxy_auth_type = s->auth_state.auth_type;
00149     if (http_connect(h, path, local_path, hoststr, auth, proxyauth, &location_changed) < 0)
00150         goto fail;
00151     if (s->http_code == 401) {
00152         if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
00153             ffurl_close(hd);
00154             goto redo;
00155         } else
00156             goto fail;
00157     }
00158     if (s->http_code == 407) {
00159         if (cur_proxy_auth_type == HTTP_AUTH_NONE &&
00160             s->proxy_auth_state.auth_type != HTTP_AUTH_NONE) {
00161             ffurl_close(hd);
00162             goto redo;
00163         } else
00164             goto fail;
00165     }
00166     if ((s->http_code == 301 || s->http_code == 302 || s->http_code == 303 || s->http_code == 307)
00167         && location_changed == 1) {
00168         /* url moved, get next */
00169         ffurl_close(hd);
00170         if (redirects++ >= MAX_REDIRECTS)
00171             return AVERROR(EIO);
00172         location_changed = 0;
00173         goto redo;
00174     }
00175     return 0;
00176  fail:
00177     if (hd)
00178         ffurl_close(hd);
00179     s->hd = NULL;
00180     return AVERROR(EIO);
00181 }
00182 
00183 static int http_open(URLContext *h, const char *uri, int flags)
00184 {
00185     HTTPContext *s = h->priv_data;
00186 
00187     h->is_streamed = 1;
00188 
00189     s->filesize = -1;
00190     av_strlcpy(s->location, uri, sizeof(s->location));
00191 
00192     if (s->headers) {
00193         int len = strlen(s->headers);
00194         if (len < 2 || strcmp("\r\n", s->headers + len - 2))
00195             av_log(h, AV_LOG_WARNING, "No trailing CRLF found in HTTP header.\n");
00196     }
00197 
00198     return http_open_cnx(h);
00199 }
00200 static int http_getc(HTTPContext *s)
00201 {
00202     int len;
00203     if (s->buf_ptr >= s->buf_end) {
00204         len = ffurl_read(s->hd, s->buffer, BUFFER_SIZE);
00205         if (len < 0) {
00206             return AVERROR(EIO);
00207         } else if (len == 0) {
00208             return -1;
00209         } else {
00210             s->buf_ptr = s->buffer;
00211             s->buf_end = s->buffer + len;
00212         }
00213     }
00214     return *s->buf_ptr++;
00215 }
00216 
00217 static int http_get_line(HTTPContext *s, char *line, int line_size)
00218 {
00219     int ch;
00220     char *q;
00221 
00222     q = line;
00223     for(;;) {
00224         ch = http_getc(s);
00225         if (ch < 0)
00226             return AVERROR(EIO);
00227         if (ch == '\n') {
00228             /* process line */
00229             if (q > line && q[-1] == '\r')
00230                 q--;
00231             *q = '\0';
00232 
00233             return 0;
00234         } else {
00235             if ((q - line) < line_size - 1)
00236                 *q++ = ch;
00237         }
00238     }
00239 }
00240 
00241 static int process_line(URLContext *h, char *line, int line_count,
00242                         int *new_location)
00243 {
00244     HTTPContext *s = h->priv_data;
00245     char *tag, *p, *end;
00246 
00247     /* end of header */
00248     if (line[0] == '\0')
00249         return 0;
00250 
00251     p = line;
00252     if (line_count == 0) {
00253         while (!isspace(*p) && *p != '\0')
00254             p++;
00255         while (isspace(*p))
00256             p++;
00257         s->http_code = strtol(p, &end, 10);
00258 
00259         av_dlog(NULL, "http_code=%d\n", s->http_code);
00260 
00261         /* error codes are 4xx and 5xx, but regard 401 as a success, so we
00262          * don't abort until all headers have been parsed. */
00263         if (s->http_code >= 400 && s->http_code < 600 && (s->http_code != 401
00264             || s->auth_state.auth_type != HTTP_AUTH_NONE) &&
00265             (s->http_code != 407 || s->proxy_auth_state.auth_type != HTTP_AUTH_NONE)) {
00266             end += strspn(end, SPACE_CHARS);
00267             av_log(h, AV_LOG_WARNING, "HTTP error %d %s\n",
00268                    s->http_code, end);
00269             return -1;
00270         }
00271     } else {
00272         while (*p != '\0' && *p != ':')
00273             p++;
00274         if (*p != ':')
00275             return 1;
00276 
00277         *p = '\0';
00278         tag = line;
00279         p++;
00280         while (isspace(*p))
00281             p++;
00282         if (!av_strcasecmp(tag, "Location")) {
00283             strcpy(s->location, p);
00284             *new_location = 1;
00285         } else if (!av_strcasecmp (tag, "Content-Length") && s->filesize == -1) {
00286             s->filesize = atoll(p);
00287         } else if (!av_strcasecmp (tag, "Content-Range")) {
00288             /* "bytes $from-$to/$document_size" */
00289             const char *slash;
00290             if (!strncmp (p, "bytes ", 6)) {
00291                 p += 6;
00292                 s->off = atoll(p);
00293                 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
00294                     s->filesize = atoll(slash+1);
00295             }
00296             h->is_streamed = 0; /* we _can_ in fact seek */
00297         } else if (!av_strcasecmp(tag, "Accept-Ranges") && !strncmp(p, "bytes", 5)) {
00298             h->is_streamed = 0;
00299         } else if (!av_strcasecmp (tag, "Transfer-Encoding") && !av_strncasecmp(p, "chunked", 7)) {
00300             s->filesize = -1;
00301             s->chunksize = 0;
00302         } else if (!av_strcasecmp (tag, "WWW-Authenticate")) {
00303             ff_http_auth_handle_header(&s->auth_state, tag, p);
00304         } else if (!av_strcasecmp (tag, "Authentication-Info")) {
00305             ff_http_auth_handle_header(&s->auth_state, tag, p);
00306         } else if (!av_strcasecmp (tag, "Proxy-Authenticate")) {
00307             ff_http_auth_handle_header(&s->proxy_auth_state, tag, p);
00308         } else if (!av_strcasecmp (tag, "Connection")) {
00309             if (!strcmp(p, "close"))
00310                 s->willclose = 1;
00311         }
00312     }
00313     return 1;
00314 }
00315 
00316 static inline int has_header(const char *str, const char *header)
00317 {
00318     /* header + 2 to skip over CRLF prefix. (make sure you have one!) */
00319     if (!str)
00320         return 0;
00321     return av_stristart(str, header + 2, NULL) || av_stristr(str, header);
00322 }
00323 
00324 static int http_connect(URLContext *h, const char *path, const char *local_path,
00325                         const char *hoststr, const char *auth,
00326                         const char *proxyauth, int *new_location)
00327 {
00328     HTTPContext *s = h->priv_data;
00329     int post, err;
00330     char line[1024];
00331     char headers[1024] = "";
00332     char *authstr = NULL, *proxyauthstr = NULL;
00333     int64_t off = s->off;
00334     int len = 0;
00335     const char *method;
00336 
00337 
00338     /* send http header */
00339     post = h->flags & AVIO_FLAG_WRITE;
00340     method = post ? "POST" : "GET";
00341     authstr = ff_http_auth_create_response(&s->auth_state, auth, local_path,
00342                                            method);
00343     proxyauthstr = ff_http_auth_create_response(&s->proxy_auth_state, proxyauth,
00344                                                 local_path, method);
00345 
00346     /* set default headers if needed */
00347     if (!has_header(s->headers, "\r\nUser-Agent: "))
00348         len += av_strlcatf(headers + len, sizeof(headers) - len,
00349                            "User-Agent: %s\r\n",
00350                            s->user_agent ? s->user_agent : LIBAVFORMAT_IDENT);
00351     if (!has_header(s->headers, "\r\nAccept: "))
00352         len += av_strlcpy(headers + len, "Accept: */*\r\n",
00353                           sizeof(headers) - len);
00354     if (!has_header(s->headers, "\r\nRange: ") && !post)
00355         len += av_strlcatf(headers + len, sizeof(headers) - len,
00356                            "Range: bytes=%"PRId64"-\r\n", s->off);
00357     if (!has_header(s->headers, "\r\nConnection: "))
00358         len += av_strlcpy(headers + len, "Connection: close\r\n",
00359                           sizeof(headers)-len);
00360     if (!has_header(s->headers, "\r\nHost: "))
00361         len += av_strlcatf(headers + len, sizeof(headers) - len,
00362                            "Host: %s\r\n", hoststr);
00363 
00364     /* now add in custom headers */
00365     if (s->headers)
00366         av_strlcpy(headers + len, s->headers, sizeof(headers) - len);
00367 
00368     snprintf(s->buffer, sizeof(s->buffer),
00369              "%s %s HTTP/1.1\r\n"
00370              "%s"
00371              "%s"
00372              "%s"
00373              "%s%s"
00374              "\r\n",
00375              method,
00376              path,
00377              post && s->chunked_post ? "Transfer-Encoding: chunked\r\n" : "",
00378              headers,
00379              authstr ? authstr : "",
00380              proxyauthstr ? "Proxy-" : "", proxyauthstr ? proxyauthstr : "");
00381 
00382     av_freep(&authstr);
00383     av_freep(&proxyauthstr);
00384     if (ffurl_write(s->hd, s->buffer, strlen(s->buffer)) < 0)
00385         return AVERROR(EIO);
00386 
00387     /* init input buffer */
00388     s->buf_ptr = s->buffer;
00389     s->buf_end = s->buffer;
00390     s->line_count = 0;
00391     s->off = 0;
00392     s->filesize = -1;
00393     s->willclose = 0;
00394     if (post) {
00395         /* Pretend that it did work. We didn't read any header yet, since
00396          * we've still to send the POST data, but the code calling this
00397          * function will check http_code after we return. */
00398         s->http_code = 200;
00399         return 0;
00400     }
00401     s->chunksize = -1;
00402 
00403     /* wait for header */
00404     for(;;) {
00405         if (http_get_line(s, line, sizeof(line)) < 0)
00406             return AVERROR(EIO);
00407 
00408         av_dlog(NULL, "header='%s'\n", line);
00409 
00410         err = process_line(h, line, s->line_count, new_location);
00411         if (err < 0)
00412             return err;
00413         if (err == 0)
00414             break;
00415         s->line_count++;
00416     }
00417 
00418     return (off == s->off) ? 0 : -1;
00419 }
00420 
00421 
00422 static int http_buf_read(URLContext *h, uint8_t *buf, int size)
00423 {
00424     HTTPContext *s = h->priv_data;
00425     int len;
00426     /* read bytes from input buffer first */
00427     len = s->buf_end - s->buf_ptr;
00428     if (len > 0) {
00429         if (len > size)
00430             len = size;
00431         memcpy(buf, s->buf_ptr, len);
00432         s->buf_ptr += len;
00433     } else {
00434         if (!s->willclose && s->filesize >= 0 && s->off >= s->filesize)
00435             return AVERROR_EOF;
00436         len = ffurl_read(s->hd, buf, size);
00437     }
00438     if (len > 0) {
00439         s->off += len;
00440         if (s->chunksize > 0)
00441             s->chunksize -= len;
00442     }
00443     return len;
00444 }
00445 
00446 static int http_read(URLContext *h, uint8_t *buf, int size)
00447 {
00448     HTTPContext *s = h->priv_data;
00449 
00450     if (s->chunksize >= 0) {
00451         if (!s->chunksize) {
00452             char line[32];
00453 
00454             for(;;) {
00455                 do {
00456                     if (http_get_line(s, line, sizeof(line)) < 0)
00457                         return AVERROR(EIO);
00458                 } while (!*line);    /* skip CR LF from last chunk */
00459 
00460                 s->chunksize = strtoll(line, NULL, 16);
00461 
00462                 av_dlog(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
00463 
00464                 if (!s->chunksize)
00465                     return 0;
00466                 break;
00467             }
00468         }
00469         size = FFMIN(size, s->chunksize);
00470     }
00471     return http_buf_read(h, buf, size);
00472 }
00473 
00474 /* used only when posting data */
00475 static int http_write(URLContext *h, const uint8_t *buf, int size)
00476 {
00477     char temp[11] = "";  /* 32-bit hex + CRLF + nul */
00478     int ret;
00479     char crlf[] = "\r\n";
00480     HTTPContext *s = h->priv_data;
00481 
00482     if (!s->chunked_post) {
00483         /* non-chunked data is sent without any special encoding */
00484         return ffurl_write(s->hd, buf, size);
00485     }
00486 
00487     /* silently ignore zero-size data since chunk encoding that would
00488      * signal EOF */
00489     if (size > 0) {
00490         /* upload data using chunked encoding */
00491         snprintf(temp, sizeof(temp), "%x\r\n", size);
00492 
00493         if ((ret = ffurl_write(s->hd, temp, strlen(temp))) < 0 ||
00494             (ret = ffurl_write(s->hd, buf, size)) < 0 ||
00495             (ret = ffurl_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
00496             return ret;
00497     }
00498     return size;
00499 }
00500 
00501 static int http_close(URLContext *h)
00502 {
00503     int ret = 0;
00504     char footer[] = "0\r\n\r\n";
00505     HTTPContext *s = h->priv_data;
00506 
00507     /* signal end of chunked encoding if used */
00508     if ((h->flags & AVIO_FLAG_WRITE) && s->chunked_post) {
00509         ret = ffurl_write(s->hd, footer, sizeof(footer) - 1);
00510         ret = ret > 0 ? 0 : ret;
00511     }
00512 
00513     if (s->hd)
00514         ffurl_close(s->hd);
00515     return ret;
00516 }
00517 
00518 static int64_t http_seek(URLContext *h, int64_t off, int whence)
00519 {
00520     HTTPContext *s = h->priv_data;
00521     URLContext *old_hd = s->hd;
00522     int64_t old_off = s->off;
00523     uint8_t old_buf[BUFFER_SIZE];
00524     int old_buf_size;
00525 
00526     if (whence == AVSEEK_SIZE)
00527         return s->filesize;
00528     else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
00529         return -1;
00530 
00531     /* we save the old context in case the seek fails */
00532     old_buf_size = s->buf_end - s->buf_ptr;
00533     memcpy(old_buf, s->buf_ptr, old_buf_size);
00534     s->hd = NULL;
00535     if (whence == SEEK_CUR)
00536         off += s->off;
00537     else if (whence == SEEK_END)
00538         off += s->filesize;
00539     s->off = off;
00540 
00541     /* if it fails, continue on old connection */
00542     if (http_open_cnx(h) < 0) {
00543         memcpy(s->buffer, old_buf, old_buf_size);
00544         s->buf_ptr = s->buffer;
00545         s->buf_end = s->buffer + old_buf_size;
00546         s->hd = old_hd;
00547         s->off = old_off;
00548         return -1;
00549     }
00550     ffurl_close(old_hd);
00551     return off;
00552 }
00553 
00554 static int
00555 http_get_file_handle(URLContext *h)
00556 {
00557     HTTPContext *s = h->priv_data;
00558     return ffurl_get_file_handle(s->hd);
00559 }
00560 
00561 #if CONFIG_HTTP_PROTOCOL
00562 URLProtocol ff_http_protocol = {
00563     .name                = "http",
00564     .url_open            = http_open,
00565     .url_read            = http_read,
00566     .url_write           = http_write,
00567     .url_seek            = http_seek,
00568     .url_close           = http_close,
00569     .url_get_file_handle = http_get_file_handle,
00570     .priv_data_size      = sizeof(HTTPContext),
00571     .priv_data_class     = &http_context_class,
00572     .flags               = URL_PROTOCOL_FLAG_NETWORK,
00573 };
00574 #endif
00575 #if CONFIG_HTTPS_PROTOCOL
00576 URLProtocol ff_https_protocol = {
00577     .name                = "https",
00578     .url_open            = http_open,
00579     .url_read            = http_read,
00580     .url_write           = http_write,
00581     .url_seek            = http_seek,
00582     .url_close           = http_close,
00583     .url_get_file_handle = http_get_file_handle,
00584     .priv_data_size      = sizeof(HTTPContext),
00585     .priv_data_class     = &https_context_class,
00586     .flags               = URL_PROTOCOL_FLAG_NETWORK,
00587 };
00588 #endif
00589 
00590 #if CONFIG_HTTPPROXY_PROTOCOL
00591 static int http_proxy_close(URLContext *h)
00592 {
00593     HTTPContext *s = h->priv_data;
00594     if (s->hd)
00595         ffurl_close(s->hd);
00596     return 0;
00597 }
00598 
00599 static int http_proxy_open(URLContext *h, const char *uri, int flags)
00600 {
00601     HTTPContext *s = h->priv_data;
00602     char hostname[1024], hoststr[1024];
00603     char auth[1024], pathbuf[1024], *path;
00604     char line[1024], lower_url[100];
00605     int port, ret = 0;
00606     HTTPAuthType cur_auth_type;
00607     char *authstr;
00608 
00609     h->is_streamed = 1;
00610 
00611     av_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
00612                  pathbuf, sizeof(pathbuf), uri);
00613     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
00614     path = pathbuf;
00615     if (*path == '/')
00616         path++;
00617 
00618     ff_url_join(lower_url, sizeof(lower_url), "tcp", NULL, hostname, port,
00619                 NULL);
00620 redo:
00621     ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
00622                      &h->interrupt_callback, NULL);
00623     if (ret < 0)
00624         return ret;
00625 
00626     authstr = ff_http_auth_create_response(&s->proxy_auth_state, auth,
00627                                            path, "CONNECT");
00628     snprintf(s->buffer, sizeof(s->buffer),
00629              "CONNECT %s HTTP/1.1\r\n"
00630              "Host: %s\r\n"
00631              "Connection: close\r\n"
00632              "%s%s"
00633              "\r\n",
00634              path,
00635              hoststr,
00636              authstr ? "Proxy-" : "", authstr ? authstr : "");
00637     av_freep(&authstr);
00638 
00639     if ((ret = ffurl_write(s->hd, s->buffer, strlen(s->buffer))) < 0)
00640         goto fail;
00641 
00642     s->buf_ptr = s->buffer;
00643     s->buf_end = s->buffer;
00644     s->line_count = 0;
00645     s->filesize = -1;
00646     cur_auth_type = s->proxy_auth_state.auth_type;
00647 
00648     for (;;) {
00649         int new_loc;
00650         // Note: This uses buffering, potentially reading more than the
00651         // HTTP header. If tunneling a protocol where the server starts
00652         // the conversation, we might buffer part of that here, too.
00653         // Reading that requires using the proper ffurl_read() function
00654         // on this URLContext, not using the fd directly (as the tls
00655         // protocol does). This shouldn't be an issue for tls though,
00656         // since the client starts the conversation there, so there
00657         // is no extra data that we might buffer up here.
00658         if (http_get_line(s, line, sizeof(line)) < 0) {
00659             ret = AVERROR(EIO);
00660             goto fail;
00661         }
00662 
00663         av_dlog(h, "header='%s'\n", line);
00664 
00665         ret = process_line(h, line, s->line_count, &new_loc);
00666         if (ret < 0)
00667             goto fail;
00668         if (ret == 0)
00669             break;
00670         s->line_count++;
00671     }
00672     if (s->http_code == 407 && cur_auth_type == HTTP_AUTH_NONE &&
00673         s->proxy_auth_state.auth_type != HTTP_AUTH_NONE) {
00674         ffurl_close(s->hd);
00675         s->hd = NULL;
00676         goto redo;
00677     }
00678 
00679     if (s->http_code < 400)
00680         return 0;
00681     ret = AVERROR(EIO);
00682 
00683 fail:
00684     http_proxy_close(h);
00685     return ret;
00686 }
00687 
00688 static int http_proxy_write(URLContext *h, const uint8_t *buf, int size)
00689 {
00690     HTTPContext *s = h->priv_data;
00691     return ffurl_write(s->hd, buf, size);
00692 }
00693 
00694 URLProtocol ff_httpproxy_protocol = {
00695     .name                = "httpproxy",
00696     .url_open            = http_proxy_open,
00697     .url_read            = http_buf_read,
00698     .url_write           = http_proxy_write,
00699     .url_close           = http_proxy_close,
00700     .url_get_file_handle = http_get_file_handle,
00701     .priv_data_size      = sizeof(HTTPContext),
00702     .flags               = URL_PROTOCOL_FLAG_NETWORK,
00703 };
00704 #endif
Generated on Fri Feb 1 2013 14:34:51 for FFmpeg by doxygen 1.7.1