7 #include <netinet/in.h>
30 #define IPPORT_HTTP 80
33 #define IPPORT_HTTPS 443
35 #ifndef IPPORT_PGPKEYSERVER
36 #define IPPORT_PGPKEYSERVER 11371
39 #define IPPORT_MONGO 27017
47 #define URL_IS_POSTGRES
48 #define URL_IS_SQLSERVER
73 #define URLDBG(_f, _m, _x) if ((_url_debug | (_f)) & (_m)) fprintf _x
75 #define URLDBGIO(_f, _x) URLDBG((_f), RPMURL_DEBUG_IO, _x)
76 #define URLDBGREFS(_f, _x) URLDBG((_f), RPMURL_DEBUG_REFS, _x)
106 fprintf(stderr,
_(
"warning: u %p ctrl %p nrefs != 0 (%s %s)\n"),
126 fprintf(stderr,
_(
"warning: u %p data %p nrefs != 0 (%s %s)\n"),
166 if (_urlPool == NULL) {
167 _urlPool =
rpmioNewPool(
"u",
sizeof(*u), -1, _url_debug,
172 memset(((
char *)u)+
sizeof(u->
_item), 0,
sizeof(*u)-
sizeof(u->
_item));
207 if (_url_cache[i] == NULL)
continue;
208 _url_cache[
i] =
urlFree(_url_cache[i],
"_url_cache");
209 if (_url_cache[i] == NULL)
213 _(
"warning: _url_cache[%d] %p nrefs(%ld) != 1 (%s %s)\n"),
214 i, _url_cache[i],
yarnPeekLock(_url_cache[i]->_item.use),
215 (_url_cache[i]->
host ? _url_cache[i]->
host :
""),
220 _url_cache =
_free(_url_cache);
224 static int urlStrcmp(
const char * str1,
const char * str2)
229 return strcmp(str1, str2);
253 if (_url_cache == NULL || (ou = _url_cache[i]) == NULL) {
274 if (i == _url_count) {
280 _url_cache[ucx] =
urlLink(u,
"_url_cache (miss)");
281 u =
urlFree(u,
"urlSplit (urlFind miss)");
284 u =
urlFree(u,
"urlSplit (urlFind hit)");
290 u =
urlLink(_url_cache[ucx],
"_url_cache");
293 u =
urlFree(u,
"_url_cache (urlFind)");
304 if (mustAsk || (u->
user != NULL && u->
password == NULL)) {
308 prompt = (
char *)
alloca(strlen(host) + strlen(user) + 256);
309 sprintf(prompt,
_(
"Password for %s@%s: "), user, host);
319 const char *proxy =
rpmExpand(
"%{?_ftpproxy}", NULL);
320 if (proxy && *proxy !=
'\0') {
323 const char *uu = (u->
user ? u->
user :
"anonymous");
324 char *nu = (
char *)
xmalloc(strlen(uu) +
sizeof(
"@") + strlen(host));
329 proxy =
_free(proxy);
333 const char *proxy =
rpmExpand(
"%{?_ftpport}", NULL);
334 if (proxy && *proxy !=
'\0') {
336 int port = strtol(proxy, &end, 0);
337 if (!(end && *end ==
'\0')) {
338 fprintf(stderr,
_(
"error: %sport must be a number\n"),
340 proxy =
_free(proxy);
345 proxy =
_free(proxy);
353 const char *proxy =
rpmExpand(
"%{?_httpproxy}", NULL);
354 if (proxy && *proxy !=
'\0')
356 proxy =
_free(proxy);
360 const char *proxy =
rpmExpand(
"%{?_httpport}", NULL);
361 if (proxy && *proxy !=
'\0') {
363 int port = strtol(proxy, &end, 0);
364 if (!(end && *end ==
'\0')) {
365 fprintf(stderr,
_(
"error: %sport must be a number\n"),
371 proxy =
_free(proxy);
414 if (url && *url && *url !=
'/')
432 static const char empty[] =
"";
433 const char *path = (url ? url : empty);
436 if (*path !=
'\0' && *path !=
'/') {
441 if ((path = strchr(url+us->
len,
'/')) == NULL)
460 size_t nb = strlen(url);
461 char * t = (
char *)
xmalloc(nb + 1 + 1);
462 const char * nurl = t;
480 char *s, *se, *f, *fe;
484 if ((u =
urlNew(
"urlSplit")) == NULL)
488 if ((se = strrchr(myurl,
'#')) != NULL) {
492 if ((se = strrchr(myurl,
'?')) != NULL) {
503 while (*se && *se !=
'/') se++;
505 if (*se && (se != s) && se[-1] ==
':' && se[0] ==
'/' && se[1] ==
'/') {
520 while (*fe && *fe !=
'@') fe++;
525 while (fe > f && *fe !=
':') fe--;
535 if (strchr(fe,
'[') && strchr(fe,
']')) {
541 while (*fe && *fe !=
':') fe++;
548 if (!(end && *end ==
'\0')) {
550 myurl =
_free(myurl);
551 u =
urlFree(u,
"urlSplit (error #3)");
559 struct servent *serv;
562 serv = getservbyname(u->
scheme,
"tcp");
565 u->
port = (int) ntohs(serv->s_port);
580 #define URL_IS_SQLITE
582 #define URL_IS_POSTGRES
587 myurl =
_free(myurl);
603 const char * sfuPath = NULL;
607 if (*sfuPath ==
'\0')
611 if ((dest = strrchr(sfuPath,
'/')) != NULL)
621 result =
rpmExpand(
"%{__urlgetfile ", url,
" ", dest,
"}", NULL);
622 if (result != NULL && strcmp(result,
"OK") == 0)
628 result =
_free(result);
633 sfd =
Fopen(url,
"r.ufdio");
634 if (sfd == NULL ||
Ferror(sfd)) {
641 tfd =
Fopen(dest,
"w");
643 fprintf(stderr,
"*** urlGetFile sfd %p %s tfd %p %s\n", sfd, url, (tfd ? tfd : NULL), dest);
644 if (tfd == NULL ||
Ferror(tfd)) {
int(* urlNotify)(const urlinfo u, unsigned status)
static void * fdGetFp(FD_t fd)
enum urltype_e urltype
Supported URL types.
static const char * urlStrdup(const char *url)
Copy a URL, adding extra byte for the pesky trailing '/'.
void yarnPossess(yarnLock bolt)
struct urlinfo_s * urlinfo
long yarnPeekLock(yarnLock bolt)
char * xstrdup(const char *str)
FD_t Fopen(const char *path, const char *_fmode)
fopen(3) clone.
urlinfo urlNew(const char *msg)
Create a URL control structure instance.
#define RPMURL_SERVER_HASRANGE
rpmioItem rpmioLinkPoolItem(rpmioItem item, const char *msg, const char *fn, unsigned ln)
Increment a pool item refcount.
void * rpmioFreePoolItem(rpmioItem item, const char *msg, const char *fn, unsigned ln)
Free a pool item.
#define RPMURL_IOBUF_SIZE
static void fdPush(FD_t fd, FDIO_t io, void *fp, int fdno)
static void urlFind(urlinfo *uret, int mustAsk)
static void rpmlog(int code, const char *fmt,...)
static struct urlstring urlstrings[]
#define IPPORT_PGPKEYSERVER
const char * Fstrerror(FD_t fd)
strerror(3) clone.
void * xcalloc(size_t nmemb, size_t size)
fdio_close_function_t close
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
int davFree(urlinfo u)
Free persistent neon session state.
urltype urlIsURL(const char *url)
Return type of URL.
The FD_t File Handle data structure.
const char const char * user
urltype urlType(void *_u)
char * rpmExpand(const char *arg,...)
Return (malloc'ed) concatenated macro expansion(s).
static int fdFileno(void *cookie)
int Fclose(FD_t fd)
fclose(3) clone.
Cumulative statistics for an operation.
void yarnRelease(yarnLock bolt)
int ufdGetFile(FD_t sfd, FD_t tfd)
static urlinfo urlGetPool(rpmioPool pool)
int Ferror(FD_t fd)
ferror(3) clone.
int urlSplit(const char *url, urlinfo *uret)
Parse URL string into a control structure.
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
char * stpcpy(char *dest, const char *src)
int urlGetFile(const char *url, const char *dest)
Copy data from URL to local file.
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
static int urlStrcmp(const char *str1, const char *str2)
int(* notify)(const urlinfo u, unsigned status)
urltype urlPath(const char *url, const char **pathp)
Return path component of URL.
urlinfo urlFree(urlinfo u, const char *msg)
Dereference a URL control structure instance.
int rpmExpandNumeric(const char *arg)
Return macro expansion as a numeric value.
void urlFreeCache(void)
Free cached URL control structures.
urlinfo XurlNew(const char *msg, const char *fn, unsigned ln)
static void urlFini(void *_u)
char *(* Getpass)(const char *prompt)
Return a password.
int Unlink(const char *path)
unlink(2) clone.
urlinfo urlLink(urlinfo u, const char *msg)
Reference a URL control structure instance.