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, '/');
95 /* set new current position if the root node is changed */
97 while (p && (node = strstr(p, ".."))) {
98 av_strlcat(tmp_path, p, node - p + strlen(tmp_path));
100 sep = strrchr(tmp_path, '/');
107 if (!av_stristart(p, "/", NULL) && root != rel)
108 av_strlcat(tmp_path, "/", size);
110 av_strlcat(tmp_path, p, size);
111 /* start set buf after temp path process. */
112 av_strlcpy(buf, rel, root - rel + 1);
114 if (!av_stristart(tmp_path, "/", NULL) && root != rel)
115 av_strlcat(buf, "/", size);
117 av_strlcat(buf, tmp_path, size);
120 void ff_make_absolute_url(char *buf, int size, const char *base,
123 char *sep, *path_query;
125 char tmp_path[MAX_URL_SIZE];
127 memset(tmp_path, 0, sizeof(tmp_path));
128 /* Absolute path, relative to the current server */
129 if (base && strstr(base, "://") && rel[0] == '/') {
131 av_strlcpy(buf, base, size);
132 sep = strstr(buf, "://");
134 /* Take scheme from base url */
138 /* Take scheme and host from base url */
140 sep = strchr(sep, '/');
145 av_strlcat(buf, rel, size);
146 trim_double_dot_url(tmp_path, buf, size);
147 memset(buf, 0, size);
148 av_strlcpy(buf, tmp_path, size);
151 /* If rel actually is an absolute url, just copy it */
152 if (!base || strstr(rel, "://") || rel[0] == '/') {
153 trim_double_dot_url(buf, rel, size);
157 av_strlcpy(buf, base, size);
159 /* Strip off any query string from base */
160 path_query = strchr(buf, '?');
164 /* Is relative path just a new query part? */
166 av_strlcat(buf, rel, size);
167 trim_double_dot_url(tmp_path, buf, size);
168 memset(buf, 0, size);
169 av_strlcpy(buf, tmp_path, size);
174 /* Get the path root of the url which start by "://" */
175 if (p && strstr(p, "://")) {
176 sep = strstr(p, "://");
179 root = strchr(sep, '/');
183 /* Remove the file name from the base url */
184 sep = strrchr(buf, '/');
185 if (sep && sep <= root)
192 while (av_strstart(rel, "..", NULL) && sep) {
193 /* Remove the path delimiter at the end */
196 sep = strrchr(buf, '/');
199 /* If the next directory name to pop off is "..", break here */
200 if (!strcmp(sep ? &sep[1] : buf, "..")) {
201 /* Readd the slash we just removed */
202 av_strlcat(buf, "/", size);
205 /* Cut off the directory name */
212 av_strlcat(buf, rel, size);
213 trim_double_dot_url(tmp_path, buf, size);
214 memset(buf, 0, size);
215 av_strlcpy(buf, tmp_path, size);
218 AVIODirEntry *ff_alloc_dir_entry(void)
220 AVIODirEntry *entry = av_mallocz(sizeof(AVIODirEntry));
222 entry->type = AVIO_ENTRY_UNKNOWN;
224 entry->modification_timestamp = -1;
225 entry->access_timestamp = -1;
226 entry->status_change_timestamp = -1;
228 entry->group_id = -1;
229 entry->filemode = -1;