7 #include <netinet/in.h>
22 #define IPPORT_HTTP 80
25 #define IPPORT_HTTPS 443
27 #ifndef IPPORT_PGPKEYSERVER
28 #define IPPORT_PGPKEYSERVER 11371
41 #define URLDBG(_f, _m, _x) if ((_url_debug | (_f)) & (_m)) fprintf _x
43 #define URLDBGIO(_f, _x) URLDBG((_f), RPMURL_DEBUG_IO, _x)
44 #define URLDBGREFS(_f, _x) URLDBG((_f), RPMURL_DEBUG_REFS, _x)
62 URLDBGREFS(0, (stderr,
"--> url %p ++ %d %s at %s:%u\n", u, u->
nrefs, msg, file, line));
70 if ((u =
xmalloc(
sizeof(*u))) == NULL)
72 memset(u, 0,
sizeof(*u));
92 URLDBGREFS(0, (stderr,
"--> url %p -- %d %s at %s:%u\n", u, u->
nrefs, msg, file, line));
112 fprintf(stderr,
_(
"warning: u %p ctrl %p nrefs != 0 (%s %s)\n"),
132 fprintf(stderr,
_(
"warning: u %p data %p nrefs != 0 (%s %s)\n"),
157 if (_url_cache[i] == NULL)
continue;
158 _url_cache[i] =
urlFree(_url_cache[i],
"_url_cache");
161 _(
"warning: _url_cache[%d] %p nrefs(%d) != 1 (%s %s)\n"),
162 i, _url_cache[i], _url_cache[i]->nrefs,
163 (_url_cache[i]->host ? _url_cache[i]->host :
""),
164 (_url_cache[i]->scheme ? _url_cache[i]->scheme :
""));
167 _url_cache =
_free(_url_cache);
172 static int urlStrcmp(
const char * str1,
const char * str2)
177 return strcmp(str1, str2);
202 if (_url_cache == NULL || (ou = _url_cache[i]) == NULL) {
223 if (i == _url_count) {
226 _url_cache =
xrealloc(_url_cache,
sizeof(*_url_cache) * _url_count);
229 _url_cache[ucx] =
urlLink(u,
"_url_cache (miss)");
230 u =
urlFree(u,
"urlSplit (urlFind miss)");
233 u =
urlFree(u,
"urlSplit (urlFind hit)");
239 u =
urlLink(_url_cache[ucx],
"_url_cache");
242 u =
urlFree(u,
"_url_cache (urlFind)");
252 if (mustAsk || (u->
user != NULL && u->
password == NULL)) {
253 const char * host = (u->
host ? u->
host :
"");
254 const char * user = (u->
user ? u->
user :
"");
256 prompt =
alloca(strlen(host) + strlen(user) + 256);
257 sprintf(prompt,
_(
"Password for %s@%s: "), user, host);
267 const char *proxy =
rpmExpand(
"%{_ftpproxy}", NULL);
268 if (proxy && *proxy !=
'%') {
270 const char * host = (u->
host ? u->
host :
"");
271 const char *uu = (u->
user ? u->
user :
"anonymous");
272 char *nu =
xmalloc(strlen(uu) +
sizeof(
"@") + strlen(host));
277 proxy =
_free(proxy);
281 const char *proxy =
rpmExpand(
"%{_ftpport}", NULL);
282 if (proxy && *proxy !=
'%') {
284 int port = strtol(proxy, &end, 0);
285 if (!(end && *end ==
'\0')) {
286 fprintf(stderr,
_(
"error: %sport must be a number\n"),
292 proxy =
_free(proxy);
300 const char *proxy =
rpmExpand(
"%{_httpproxy}", NULL);
301 if (proxy && *proxy !=
'%')
303 proxy =
_free(proxy);
307 const char *proxy =
rpmExpand(
"%{_httpport}", NULL);
308 if (proxy && *proxy !=
'%') {
310 int port = strtol(proxy, &end, 0);
311 if (!(end && *end ==
'\0')) {
312 fprintf(stderr,
_(
"error: %sport must be a number\n"),
318 proxy =
_free(proxy);
374 url +=
sizeof(
"ftp://") - 1;
375 path = strchr(url,
'/');
376 if (path == NULL) path = url + strlen(url);
379 url +=
sizeof(
"file://") - 1;
380 path = strchr(url,
'/');
381 if (path == NULL) path = url + strlen(url);
384 url +=
sizeof(
"hkp://") - 1;
385 path = strchr(url,
'/');
386 if (path == NULL) path = url + strlen(url);
389 url +=
sizeof(
"http://") - 1;
390 path = strchr(url,
'/');
391 if (path == NULL) path = url + strlen(url);
394 url +=
sizeof(
"https://") - 1;
395 path = strchr(url,
'/');
396 if (path == NULL) path = url + strlen(url);
399 if (path == NULL) path =
"";
426 char *s, *se, *f, *fe;
430 if ((u =
urlNew(
"urlSplit")) == NULL)
433 if ((se = s = myurl =
xstrdup(url)) == NULL) {
434 u =
urlFree(u,
"urlSplit (error #1)");
443 while (*se && *se !=
'/') se++;
445 if (*se && (se != s) && se[-1] ==
':' && se[0] ==
'/' && se[1] ==
'/') {
460 while (*fe && *fe !=
'@') fe++;
466 while (fe > f && *fe !=
':') fe--;
477 if (strchr(fe,
'[') && strchr(fe,
']'))
483 while (*fe && *fe !=
':') fe++;
490 if (!(end && *end ==
'\0')) {
492 myurl =
_free(myurl);
493 u =
urlFree(u,
"urlSplit (error #3)");
501 struct servent *serv;
504 serv = getservbyname(u->
scheme,
"tcp");
507 u->
port = ntohs(serv->s_port);
518 myurl =
_free(myurl);
535 const char * sfuPath = NULL;
536 int urlType =
urlPath(url, &sfuPath);
538 if (*sfuPath ==
'\0')
541 sfd =
Fopen(url,
"r");
542 if (sfd == NULL ||
Ferror(sfd)) {
549 if ((dest = strrchr(sfuPath,
'/')) != NULL)
559 tfd =
Fopen(dest,
"w");
561 fprintf(stderr,
"*** urlGetFile sfd %p %s tfd %p %s\n", sfd, url, (tfd ? tfd : NULL), dest);
562 if (tfd == NULL ||
Ferror(tfd)) {