2 * URL utility functions
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/avstring.h"
34 * URL utility functions.
37 int ff_url_join(char *str, int size, const char *proto,
38 const char *authorization, const char *hostname,
39 int port, const char *fmt, ...)
42 struct addrinfo hints = { 0 }, *ai;
47 av_strlcatf(str, size, "%s://", proto);
48 if (authorization && authorization[0])
49 av_strlcatf(str, size, "%s@", authorization);
50 #if CONFIG_NETWORK && defined(AF_INET6)
51 /* Determine if hostname is a numerical IPv6 address,
52 * properly escape it within [] in that case. */
53 hints.ai_flags = AI_NUMERICHOST;
54 if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
55 if (ai->ai_family == AF_INET6) {
56 av_strlcat(str, "[", size);
57 av_strlcat(str, hostname, size);
58 av_strlcat(str, "]", size);
60 av_strlcat(str, hostname, size);
65 /* Not an IPv6 address, just output the plain string. */
66 av_strlcat(str, hostname, size);
69 av_strlcatf(str, size, ":%d", port);
72 size_t len = strlen(str);
75 vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
81 static void trim_double_dot_url(char *buf, const char *rel, int size)
84 const char *root = rel;
85 char tmp_path[MAX_URL_SIZE] = {0, };
89 /* Get the path root of the url which start by "://" */
90 if (p && (sep = strstr(p, "://"))) {
92 root = strchr(sep, '/');
97 /* set new current position if the root node is changed */
99 while (p && (node = strstr(p, ".."))) {
100 av_strlcat(tmp_path, p, node - p + strlen(tmp_path));
102 sep = strrchr(tmp_path, '/');
109 if (!av_stristart(p, "/", NULL) && root != rel)
110 av_strlcat(tmp_path, "/", size);
112 av_strlcat(tmp_path, p, size);
113 /* start set buf after temp path process. */
114 av_strlcpy(buf, rel, root - rel + 1);
116 if (!av_stristart(tmp_path, "/", NULL) && root != rel)
117 av_strlcat(buf, "/", size);
119 av_strlcat(buf, tmp_path, size);
122 void ff_make_absolute_url(char *buf, int size, const char *base,
125 char *sep, *path_query;
127 char tmp_path[MAX_URL_SIZE];
129 memset(tmp_path, 0, sizeof(tmp_path));
130 /* Absolute path, relative to the current server */
131 if (base && strstr(base, "://") && rel[0] == '/') {
133 av_strlcpy(buf, base, size);
134 sep = strstr(buf, "://");
136 /* Take scheme from base url */
140 /* Take scheme and host from base url */
142 sep = strchr(sep, '/');
147 av_strlcat(buf, rel, size);
148 trim_double_dot_url(tmp_path, buf, size);
149 memset(buf, 0, size);
150 av_strlcpy(buf, tmp_path, size);
153 /* If rel actually is an absolute url, just copy it */
154 if (!base || strstr(rel, "://") || rel[0] == '/') {
155 memset(buf, 0, size);
156 trim_double_dot_url(buf, rel, size);
160 av_strlcpy(buf, base, size);
162 /* Strip off any query string from base */
163 path_query = strchr(buf, '?');
167 /* Is relative path just a new query part? */
169 av_strlcat(buf, rel, size);
170 trim_double_dot_url(tmp_path, buf, size);
171 memset(buf, 0, size);
172 av_strlcpy(buf, tmp_path, size);
177 /* Get the path root of the url which start by "://" */
178 if (p && strstr(p, "://")) {
179 sep = strstr(p, "://");
182 root = strchr(sep, '/');
188 /* Remove the file name from the base url */
189 sep = strrchr(buf, '/');
190 if (sep && sep <= root)
197 while (av_strstart(rel, "..", NULL) && sep) {
198 /* Remove the path delimiter at the end */
201 sep = strrchr(buf, '/');
204 /* If the next directory name to pop off is "..", break here */
205 if (!strcmp(sep ? &sep[1] : buf, "..")) {
206 /* Readd the slash we just removed */
207 av_strlcat(buf, "/", size);
210 /* Cut off the directory name */
217 av_strlcat(buf, rel, size);
218 trim_double_dot_url(tmp_path, buf, size);
219 memset(buf, 0, size);
220 av_strlcpy(buf, tmp_path, size);
223 AVIODirEntry *ff_alloc_dir_entry(void)
225 AVIODirEntry *entry = av_mallocz(sizeof(AVIODirEntry));
227 entry->type = AVIO_ENTRY_UNKNOWN;
229 entry->modification_timestamp = -1;
230 entry->access_timestamp = -1;
231 entry->status_change_timestamp = -1;
233 entry->group_id = -1;
234 entry->filemode = -1;