LCOV - code coverage report
Current view: top level - libavformat - url.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 36 76 47.4 %
Date: 2017-12-14 01:15:32 Functions: 1 3 33.3 %

          Line data    Source code
       1             : /*
       2             :  * URL utility functions
       3             :  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : 
      23             : #include "avformat.h"
      24             : #include "config.h"
      25             : #include "url.h"
      26             : #if CONFIG_NETWORK
      27             : #include "network.h"
      28             : #endif
      29             : #include "libavutil/avstring.h"
      30             : 
      31             : /**
      32             :  * @file
      33             :  * URL utility functions.
      34             :  */
      35             : 
      36           0 : int ff_url_join(char *str, int size, const char *proto,
      37             :                 const char *authorization, const char *hostname,
      38             :                 int port, const char *fmt, ...)
      39             : {
      40             : #if CONFIG_NETWORK
      41           0 :     struct addrinfo hints = { 0 }, *ai;
      42             : #endif
      43             : 
      44           0 :     str[0] = '\0';
      45           0 :     if (proto)
      46           0 :         av_strlcatf(str, size, "%s://", proto);
      47           0 :     if (authorization && authorization[0])
      48           0 :         av_strlcatf(str, size, "%s@", authorization);
      49             : #if CONFIG_NETWORK && defined(AF_INET6)
      50             :     /* Determine if hostname is a numerical IPv6 address,
      51             :      * properly escape it within [] in that case. */
      52           0 :     hints.ai_flags = AI_NUMERICHOST;
      53           0 :     if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
      54           0 :         if (ai->ai_family == AF_INET6) {
      55           0 :             av_strlcat(str, "[", size);
      56           0 :             av_strlcat(str, hostname, size);
      57           0 :             av_strlcat(str, "]", size);
      58             :         } else {
      59           0 :             av_strlcat(str, hostname, size);
      60             :         }
      61           0 :         freeaddrinfo(ai);
      62             :     } else
      63             : #endif
      64             :         /* Not an IPv6 address, just output the plain string. */
      65           0 :         av_strlcat(str, hostname, size);
      66             : 
      67           0 :     if (port >= 0)
      68           0 :         av_strlcatf(str, size, ":%d", port);
      69           0 :     if (fmt) {
      70             :         va_list vl;
      71           0 :         size_t len = strlen(str);
      72             : 
      73           0 :         va_start(vl, fmt);
      74           0 :         vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
      75           0 :         va_end(vl);
      76             :     }
      77           0 :     return strlen(str);
      78             : }
      79             : 
      80         111 : void ff_make_absolute_url(char *buf, int size, const char *base,
      81             :                           const char *rel)
      82             : {
      83             :     char *sep, *path_query;
      84             :     /* Absolute path, relative to the current server */
      85         111 :     if (base && strstr(base, "://") && rel[0] == '/') {
      86           6 :         if (base != buf)
      87           3 :             av_strlcpy(buf, base, size);
      88           6 :         sep = strstr(buf, "://");
      89           6 :         if (sep) {
      90             :             /* Take scheme from base url */
      91           6 :             if (rel[1] == '/') {
      92           2 :                 sep[1] = '\0';
      93             :             } else {
      94             :                 /* Take scheme and host from base url */
      95           4 :                 sep += 3;
      96           4 :                 sep = strchr(sep, '/');
      97           4 :                 if (sep)
      98           4 :                     *sep = '\0';
      99             :             }
     100             :         }
     101           6 :         av_strlcat(buf, rel, size);
     102           6 :         return;
     103             :     }
     104             :     /* If rel actually is an absolute url, just copy it */
     105         105 :     if (!base || strstr(rel, "://") || rel[0] == '/') {
     106           9 :         av_strlcpy(buf, rel, size);
     107           9 :         return;
     108             :     }
     109          96 :     if (base != buf)
     110          89 :         av_strlcpy(buf, base, size);
     111             : 
     112             :     /* Strip off any query string from base */
     113          96 :     path_query = strchr(buf, '?');
     114          96 :     if (path_query)
     115           2 :         *path_query = '\0';
     116             : 
     117             :     /* Is relative path just a new query part? */
     118          96 :     if (rel[0] == '?') {
     119           2 :         av_strlcat(buf, rel, size);
     120           2 :         return;
     121             :     }
     122             : 
     123             :     /* Remove the file name from the base url */
     124          94 :     sep = strrchr(buf, '/');
     125          94 :     if (sep)
     126          94 :         sep[1] = '\0';
     127             :     else
     128           0 :         buf[0] = '\0';
     129         264 :     while (av_strstart(rel, "../", NULL) && sep) {
     130             :         /* Remove the path delimiter at the end */
     131          76 :         sep[0] = '\0';
     132          76 :         sep = strrchr(buf, '/');
     133             :         /* If the next directory name to pop off is "..", break here */
     134          76 :         if (!strcmp(sep ? &sep[1] : buf, "..")) {
     135             :             /* Readd the slash we just removed */
     136           0 :             av_strlcat(buf, "/", size);
     137           0 :             break;
     138             :         }
     139             :         /* Cut off the directory name */
     140          76 :         if (sep)
     141          76 :             sep[1] = '\0';
     142             :         else
     143           0 :             buf[0] = '\0';
     144          76 :         rel += 3;
     145             :     }
     146          94 :     av_strlcat(buf, rel, size);
     147             : }
     148             : 
     149           0 : AVIODirEntry *ff_alloc_dir_entry(void)
     150             : {
     151           0 :     AVIODirEntry *entry = av_mallocz(sizeof(AVIODirEntry));
     152           0 :     if (entry) {
     153           0 :         entry->type = AVIO_ENTRY_UNKNOWN;
     154           0 :         entry->size = -1;
     155           0 :         entry->modification_timestamp = -1;
     156           0 :         entry->access_timestamp = -1;
     157           0 :         entry->status_change_timestamp = -1;
     158           0 :         entry->user_id = -1;
     159           0 :         entry->group_id = -1;
     160           0 :         entry->filemode = -1;
     161             :     }
     162           0 :     return entry;
     163             : }

Generated by: LCOV version 1.13