• 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 <strings.h>
00026 #include "internal.h"
00027 #include "network.h"
00028 #include "os_support.h"
00029 #include "httpauth.h"
00030 
00031 /* XXX: POST protocol is not completely implemented because ffmpeg uses
00032    only a subset of it. */
00033 
00034 /* used for protocol handling */
00035 #define BUFFER_SIZE 1024
00036 #define URL_SIZE    4096
00037 #define MAX_REDIRECTS 8
00038 
00039 typedef struct {
00040     URLContext *hd;
00041     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end;
00042     int line_count;
00043     int http_code;
00044     int64_t chunksize;      
00045     int64_t off, filesize;
00046     char location[URL_SIZE];
00047     HTTPAuthState auth_state;
00048 } HTTPContext;
00049 
00050 static int http_connect(URLContext *h, const char *path, const char *hoststr,
00051                         const char *auth, int *new_location);
00052 static int http_write(URLContext *h, uint8_t *buf, int size);
00053 
00054 
00055 /* return non zero if error */
00056 static int http_open_cnx(URLContext *h)
00057 {
00058     const char *path, *proxy_path;
00059     char hostname[1024], hoststr[1024];
00060     char auth[1024];
00061     char path1[1024];
00062     char buf[1024];
00063     int port, use_proxy, err, location_changed = 0, redirects = 0;
00064     HTTPAuthType cur_auth_type;
00065     HTTPContext *s = h->priv_data;
00066     URLContext *hd = NULL;
00067 
00068     proxy_path = getenv("http_proxy");
00069     use_proxy = (proxy_path != NULL) && !getenv("no_proxy") &&
00070         av_strstart(proxy_path, "http://", NULL);
00071 
00072     /* fill the dest addr */
00073  redo:
00074     /* needed in any case to build the host string */
00075     ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
00076                  path1, sizeof(path1), s->location);
00077     ff_url_join(hoststr, sizeof(hoststr), NULL, NULL, hostname, port, NULL);
00078 
00079     if (use_proxy) {
00080         ff_url_split(NULL, 0, auth, sizeof(auth), hostname, sizeof(hostname), &port,
00081                      NULL, 0, proxy_path);
00082         path = s->location;
00083     } else {
00084         if (path1[0] == '\0')
00085             path = "/";
00086         else
00087             path = path1;
00088     }
00089     if (port < 0)
00090         port = 80;
00091 
00092     ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);
00093     err = url_open(&hd, buf, URL_RDWR);
00094     if (err < 0)
00095         goto fail;
00096 
00097     s->hd = hd;
00098     cur_auth_type = s->auth_state.auth_type;
00099     if (http_connect(h, path, hoststr, auth, &location_changed) < 0)
00100         goto fail;
00101     if (s->http_code == 401) {
00102         if (cur_auth_type == HTTP_AUTH_NONE && s->auth_state.auth_type != HTTP_AUTH_NONE) {
00103             url_close(hd);
00104             goto redo;
00105         } else
00106             goto fail;
00107     }
00108     if ((s->http_code == 302 || s->http_code == 303) && location_changed == 1) {
00109         /* url moved, get next */
00110         url_close(hd);
00111         if (redirects++ >= MAX_REDIRECTS)
00112             return AVERROR(EIO);
00113         location_changed = 0;
00114         goto redo;
00115     }
00116     return 0;
00117  fail:
00118     if (hd)
00119         url_close(hd);
00120     return AVERROR(EIO);
00121 }
00122 
00123 static int http_open(URLContext *h, const char *uri, int flags)
00124 {
00125     HTTPContext *s;
00126     int ret;
00127 
00128     h->is_streamed = 1;
00129 
00130     s = av_malloc(sizeof(HTTPContext));
00131     if (!s) {
00132         return AVERROR(ENOMEM);
00133     }
00134     h->priv_data = s;
00135     s->filesize = -1;
00136     s->chunksize = -1;
00137     s->off = 0;
00138     memset(&s->auth_state, 0, sizeof(s->auth_state));
00139     av_strlcpy(s->location, uri, URL_SIZE);
00140 
00141     ret = http_open_cnx(h);
00142     if (ret != 0)
00143         av_free (s);
00144     return ret;
00145 }
00146 static int http_getc(HTTPContext *s)
00147 {
00148     int len;
00149     if (s->buf_ptr >= s->buf_end) {
00150         len = url_read(s->hd, s->buffer, BUFFER_SIZE);
00151         if (len < 0) {
00152             return AVERROR(EIO);
00153         } else if (len == 0) {
00154             return -1;
00155         } else {
00156             s->buf_ptr = s->buffer;
00157             s->buf_end = s->buffer + len;
00158         }
00159     }
00160     return *s->buf_ptr++;
00161 }
00162 
00163 static int http_get_line(HTTPContext *s, char *line, int line_size)
00164 {
00165     int ch;
00166     char *q;
00167 
00168     q = line;
00169     for(;;) {
00170         ch = http_getc(s);
00171         if (ch < 0)
00172             return AVERROR(EIO);
00173         if (ch == '\n') {
00174             /* process line */
00175             if (q > line && q[-1] == '\r')
00176                 q--;
00177             *q = '\0';
00178 
00179             return 0;
00180         } else {
00181             if ((q - line) < line_size - 1)
00182                 *q++ = ch;
00183         }
00184     }
00185 }
00186 
00187 static int process_line(URLContext *h, char *line, int line_count,
00188                         int *new_location)
00189 {
00190     HTTPContext *s = h->priv_data;
00191     char *tag, *p;
00192 
00193     /* end of header */
00194     if (line[0] == '\0')
00195         return 0;
00196 
00197     p = line;
00198     if (line_count == 0) {
00199         while (!isspace(*p) && *p != '\0')
00200             p++;
00201         while (isspace(*p))
00202             p++;
00203         s->http_code = strtol(p, NULL, 10);
00204 
00205         dprintf(NULL, "http_code=%d\n", s->http_code);
00206 
00207         /* error codes are 4xx and 5xx, but regard 401 as a success, so we
00208          * don't abort until all headers have been parsed. */
00209         if (s->http_code >= 400 && s->http_code < 600 && s->http_code != 401)
00210             return -1;
00211     } else {
00212         while (*p != '\0' && *p != ':')
00213             p++;
00214         if (*p != ':')
00215             return 1;
00216 
00217         *p = '\0';
00218         tag = line;
00219         p++;
00220         while (isspace(*p))
00221             p++;
00222         if (!strcmp(tag, "Location")) {
00223             strcpy(s->location, p);
00224             *new_location = 1;
00225         } else if (!strcmp (tag, "Content-Length") && s->filesize == -1) {
00226             s->filesize = atoll(p);
00227         } else if (!strcmp (tag, "Content-Range")) {
00228             /* "bytes $from-$to/$document_size" */
00229             const char *slash;
00230             if (!strncmp (p, "bytes ", 6)) {
00231                 p += 6;
00232                 s->off = atoll(p);
00233                 if ((slash = strchr(p, '/')) && strlen(slash) > 0)
00234                     s->filesize = atoll(slash+1);
00235             }
00236             h->is_streamed = 0; /* we _can_ in fact seek */
00237         } else if (!strcmp (tag, "Transfer-Encoding") && !strncasecmp(p, "chunked", 7)) {
00238             s->filesize = -1;
00239             s->chunksize = 0;
00240         } else if (!strcmp (tag, "WWW-Authenticate")) {
00241             ff_http_auth_handle_header(&s->auth_state, tag, p);
00242         } else if (!strcmp (tag, "Authentication-Info")) {
00243             ff_http_auth_handle_header(&s->auth_state, tag, p);
00244         }
00245     }
00246     return 1;
00247 }
00248 
00249 static int http_connect(URLContext *h, const char *path, const char *hoststr,
00250                         const char *auth, int *new_location)
00251 {
00252     HTTPContext *s = h->priv_data;
00253     int post, err;
00254     char line[1024];
00255     char *authstr = NULL;
00256     int64_t off = s->off;
00257 
00258 
00259     /* send http header */
00260     post = h->flags & URL_WRONLY;
00261     authstr = ff_http_auth_create_response(&s->auth_state, auth, path,
00262                                         post ? "POST" : "GET");
00263     snprintf(s->buffer, sizeof(s->buffer),
00264              "%s %s HTTP/1.1\r\n"
00265              "User-Agent: %s\r\n"
00266              "Accept: */*\r\n"
00267              "Range: bytes=%"PRId64"-\r\n"
00268              "Host: %s\r\n"
00269              "%s"
00270              "Connection: close\r\n"
00271              "%s"
00272              "\r\n",
00273              post ? "POST" : "GET",
00274              path,
00275              LIBAVFORMAT_IDENT,
00276              s->off,
00277              hoststr,
00278              authstr ? authstr : "",
00279              post ? "Transfer-Encoding: chunked\r\n" : "");
00280 
00281     av_freep(&authstr);
00282     if (http_write(h, s->buffer, strlen(s->buffer)) < 0)
00283         return AVERROR(EIO);
00284 
00285     /* init input buffer */
00286     s->buf_ptr = s->buffer;
00287     s->buf_end = s->buffer;
00288     s->line_count = 0;
00289     s->off = 0;
00290     s->filesize = -1;
00291     if (post) {
00292         /* always use chunked encoding for upload data */
00293         s->chunksize = 0;
00294         return 0;
00295     }
00296 
00297     /* wait for header */
00298     for(;;) {
00299         if (http_get_line(s, line, sizeof(line)) < 0)
00300             return AVERROR(EIO);
00301 
00302         dprintf(NULL, "header='%s'\n", line);
00303 
00304         err = process_line(h, line, s->line_count, new_location);
00305         if (err < 0)
00306             return err;
00307         if (err == 0)
00308             break;
00309         s->line_count++;
00310     }
00311 
00312     return (off == s->off) ? 0 : -1;
00313 }
00314 
00315 
00316 static int http_read(URLContext *h, uint8_t *buf, int size)
00317 {
00318     HTTPContext *s = h->priv_data;
00319     int len;
00320 
00321     if (s->chunksize >= 0) {
00322         if (!s->chunksize) {
00323             char line[32];
00324 
00325             for(;;) {
00326                 do {
00327                     if (http_get_line(s, line, sizeof(line)) < 0)
00328                         return AVERROR(EIO);
00329                 } while (!*line);    /* skip CR LF from last chunk */
00330 
00331                 s->chunksize = strtoll(line, NULL, 16);
00332 
00333                 dprintf(NULL, "Chunked encoding data size: %"PRId64"'\n", s->chunksize);
00334 
00335                 if (!s->chunksize)
00336                     return 0;
00337                 break;
00338             }
00339         }
00340         size = FFMIN(size, s->chunksize);
00341     }
00342     /* read bytes from input buffer first */
00343     len = s->buf_end - s->buf_ptr;
00344     if (len > 0) {
00345         if (len > size)
00346             len = size;
00347         memcpy(buf, s->buf_ptr, len);
00348         s->buf_ptr += len;
00349     } else {
00350         len = url_read(s->hd, buf, size);
00351     }
00352     if (len > 0) {
00353         s->off += len;
00354         if (s->chunksize > 0)
00355             s->chunksize -= len;
00356     }
00357     return len;
00358 }
00359 
00360 /* used only when posting data */
00361 static int http_write(URLContext *h, uint8_t *buf, int size)
00362 {
00363     char temp[11];  /* 32-bit hex + CRLF + nul */
00364     int ret;
00365     char crlf[] = "\r\n";
00366     HTTPContext *s = h->priv_data;
00367 
00368     if (s->chunksize == -1) {
00369         /* headers are sent without any special encoding */
00370         return url_write(s->hd, buf, size);
00371     }
00372 
00373     /* silently ignore zero-size data since chunk encoding that would
00374      * signal EOF */
00375     if (size > 0) {
00376         /* upload data using chunked encoding */
00377         snprintf(temp, sizeof(temp), "%x\r\n", size);
00378 
00379         if ((ret = url_write(s->hd, temp, strlen(temp))) < 0 ||
00380             (ret = url_write(s->hd, buf, size)) < 0 ||
00381             (ret = url_write(s->hd, crlf, sizeof(crlf) - 1)) < 0)
00382             return ret;
00383     }
00384     return size;
00385 }
00386 
00387 static int http_close(URLContext *h)
00388 {
00389     int ret = 0;
00390     char footer[] = "0\r\n\r\n";
00391     HTTPContext *s = h->priv_data;
00392 
00393     /* signal end of chunked encoding if used */
00394     if ((h->flags & URL_WRONLY) && s->chunksize != -1) {
00395         ret = url_write(s->hd, footer, sizeof(footer) - 1);
00396         ret = ret > 0 ? 0 : ret;
00397     }
00398 
00399     url_close(s->hd);
00400     av_free(s);
00401     return ret;
00402 }
00403 
00404 static int64_t http_seek(URLContext *h, int64_t off, int whence)
00405 {
00406     HTTPContext *s = h->priv_data;
00407     URLContext *old_hd = s->hd;
00408     int64_t old_off = s->off;
00409     uint8_t old_buf[BUFFER_SIZE];
00410     int old_buf_size;
00411 
00412     if (whence == AVSEEK_SIZE)
00413         return s->filesize;
00414     else if ((s->filesize == -1 && whence == SEEK_END) || h->is_streamed)
00415         return -1;
00416 
00417     /* we save the old context in case the seek fails */
00418     old_buf_size = s->buf_end - s->buf_ptr;
00419     memcpy(old_buf, s->buf_ptr, old_buf_size);
00420     s->hd = NULL;
00421     if (whence == SEEK_CUR)
00422         off += s->off;
00423     else if (whence == SEEK_END)
00424         off += s->filesize;
00425     s->off = off;
00426 
00427     /* if it fails, continue on old connection */
00428     if (http_open_cnx(h) < 0) {
00429         memcpy(s->buffer, old_buf, old_buf_size);
00430         s->buf_ptr = s->buffer;
00431         s->buf_end = s->buffer + old_buf_size;
00432         s->hd = old_hd;
00433         s->off = old_off;
00434         return -1;
00435     }
00436     url_close(old_hd);
00437     return off;
00438 }
00439 
00440 static int
00441 http_get_file_handle(URLContext *h)
00442 {
00443     HTTPContext *s = h->priv_data;
00444     return url_get_file_handle(s->hd);
00445 }
00446 
00447 URLProtocol http_protocol = {
00448     "http",
00449     http_open,
00450     http_read,
00451     http_write,
00452     http_seek,
00453     http_close,
00454     .url_get_file_handle = http_get_file_handle,
00455 };

Generated on Fri Sep 16 2011 17:17:48 for FFmpeg by  doxygen 1.7.1