Commit 02334735 02334735e86f1cfda6a2e07cfea9283e4fc88011 by Sergey Poznyakoff

New interface for composing URLs from parts.

* include/mailutils/url.h (mu_url_sget_name)
(mu_url_aget_name,mu_url_get_name)
(mu_url_invalidate,mu_url_create_null)
(mu_url_set_user,mu_url_set_path,mu_url_set_scheme)
(mu_url_set_host,mu_url_set_port,mu_url_set_service)
(mu_url_set_auth,mu_url_set_secret,mu_url_add_param)
(mu_url_clear_param,mu_url_add_query,mu_url_clear_query): New
prototypes.

* libmailutils/tests/url-comp.at: New test case.
* libmailutils/tests/url-comp.c: New test program.
* libmailutils/tests/.gitignore: Update.
* libmailutils/tests/Makefile.am: Add new files.
* libmailutils/tests/testsuite.at: Add new tests.

* libmailutils/url/Makefile.am: Add new files.
* libmailutils/url/add-param.c: New file.
* libmailutils/url/add-query.c: New file.
* libmailutils/url/clr-param.c: New file.
* libmailutils/url/clr-query.c: New file.
* libmailutils/url/null.c: New file.
* libmailutils/url/set-auth.c: New file.
* libmailutils/url/set-host.c: New file.
* libmailutils/url/set-path.c: New file.
* libmailutils/url/set-port.c: New file.
* libmailutils/url/set-scheme.c: New file.
* libmailutils/url/set-secret.c: New file.
* libmailutils/url/set-service.c: New file.
* libmailutils/url/set-user.c: New file.
* libmailutils/url/urlinv.c: New file.

* libmailutils/url/create.c (mu_url_create_hint): Return EINVAL if
purl is NULL.
* libmailutils/url/scheme.c (mu_url_set_scheme): Remove. See set-scheme.c
* libmailutils/url/urlstr.c (mu_url_sget_name)
(mu_url_aget_name,mu_url_get_name): New functions.
(mu_url_to_string): Rewrite using the above.

* libmailutils/base/Makefile.am: Add fscompose.c
* libmailutils/base/fscompose.c: New file.
1 parent e9a329bf
......@@ -80,6 +80,10 @@ int mu_url_has_flag (mu_url_t, int);
void mu_url_destroy (mu_url_t *);
int mu_url_sget_name (const mu_url_t, const char **);
int mu_url_aget_name (const mu_url_t, char **);
int mu_url_get_name (const mu_url_t, char *, size_t, size_t *);
int mu_url_sget_scheme (const mu_url_t, const char **);
int mu_url_aget_scheme (const mu_url_t, char **);
int mu_url_get_scheme (const mu_url_t, char *, size_t, size_t *);
......@@ -120,8 +124,6 @@ int mu_url_aget_param (const mu_url_t url, const char *param, char **val);
int mu_url_expand_path (mu_url_t url);
const char *mu_url_to_string (const mu_url_t);
int mu_url_set_scheme (mu_url_t url, const char *scheme);
int mu_url_is_scheme (mu_url_t, const char *scheme);
int mu_url_is_same_scheme (mu_url_t, mu_url_t);
......@@ -133,6 +135,22 @@ int mu_url_is_same_port (mu_url_t, mu_url_t);
int mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *wcn);
int mu_url_decode (mu_url_t url);
int mu_url_invalidate (mu_url_t url);
int mu_url_create_null (mu_url_t *purl);
int mu_url_set_user (mu_url_t url, const char *user);
int mu_url_set_path (mu_url_t url, const char *path);
int mu_url_set_scheme (mu_url_t url, const char *scheme);
int mu_url_set_host (mu_url_t url, const char *host);
int mu_url_set_port (mu_url_t url, unsigned port);
int mu_url_set_service (mu_url_t url, const char *str);
int mu_url_set_auth (mu_url_t url, const char *auth);
int mu_url_set_secret (mu_url_t url, mu_secret_t secret);
int mu_url_add_param (mu_url_t url, size_t pc, const char **pv);
int mu_url_clear_param (mu_url_t url);
int mu_url_add_query (mu_url_t url, size_t pc, const char **pv);
int mu_url_clear_query (mu_url_t url);
#ifdef __cplusplus
}
......
......@@ -31,6 +31,7 @@ libbase_la_SOURCES = \
fgetpwent.c\
filename.c\
freeitem.c\
fscompose.c\
getcwd.c\
getmaxfd.c\
getpass.c\
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/util.h>
#include <mailutils/nls.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
int
mu_file_safety_compose (int *res, const char *name, int defval)
{
int negate = 0;
int val;
if (*name == '-')
{
negate = 1;
name++;
}
else if (*name == '+')
name++;
if (strcmp (name, "none") == 0)
{
*res = negate ? MU_FILE_SAFETY_ALL : MU_FILE_SAFETY_NONE;
return 0;
}
else if (strcmp (name, "all") == 0)
{
*res = negate ? MU_FILE_SAFETY_NONE : MU_FILE_SAFETY_ALL;
return 0;
}
else if (strcmp (name, "default") == 0)
val = defval;
else if (mu_file_safety_name_to_code (name, &val))
return MU_ERR_NOENT;
if (negate)
*res &= ~val;
else
*res |= val;
return 0;
}
......@@ -15,6 +15,7 @@ fsaf
listop
mailcap
prop
url-comp
url-parse
wicket
wsp
......
......@@ -51,6 +51,7 @@ noinst_PROGRAMS = \
mailcap\
prop\
tempfile\
url-comp\
url-parse\
wicket\
wsp
......@@ -80,6 +81,7 @@ TESTSUITE_AT = \
prop.at\
testsuite.at\
url.at\
url-comp.at\
wicket.at\
wordsplit.at
......
......@@ -58,6 +58,7 @@ m4_include([list.at])
m4_include([address.at])
m4_include([wordsplit.at])
m4_include([url.at])
m4_include([url-comp.at])
m4_include([mailcap.at])
m4_include([wicket.at])
m4_include([prop.at])
......
# This file is part of GNU Mailutils. -*- Autotest -*-
# Copyright (C) 2011 Free Software Foundation, Inc.
#
# GNU Mailutils is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or (at
# your option) any later version.
#
# GNU Mailutils is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
AT_BANNER(URL Composition)
dnl ------------------------------------------------------------
1 2 3 4
dnl URLCOMP(KW, ARGS, [STDOUT = `'], [STDERR = `'])
dnl
m4_pushdef([URLCOMP],[
AT_SETUP([$2 => $3])
AT_KEYWORDS([url-comp url-comp-$1])
AT_CHECK([url-comp $2],
[m4_if([$4],,0,1)],
[m4_if([$3],,,[$3
])],
m4_if([$4],,,[url-comp: $4
]))
AT_CLEANUP
])
URLCOMP(0, [], [], [URL missing required parts])
URLCOMP(1, [path=foo], [foo])
URLCOMP(2, [scheme=file], [file://])
URLCOMP(3, [scheme=file path=foo], [file://foo])
URLCOMP(4, [scheme=smtp host=localhost port=587], [smtp://localhost:587])
URLCOMP(5,
[scheme=smtp host=localhost port=587 param=strip-domain param=domain=example.com],
[smtp://localhost:587;strip-domain;domain=example.com])
URLCOMP(6, [scheme=imap user=gray host=localhost path=INBOX],
[imap://gray@localhost/INBOX])
URLCOMP(7,
[scheme=imap user=gray pass=test host=localhost path=INBOX],
[imap://gray:***@localhost/INBOX])
URLCOMP(8,
[scheme=pop user=gray pass=test auth=+APOP host=localhost path=INBOX],
[pop://gray:***;AUTH=+APOP@localhost/INBOX])
URLCOMP(9,
[scheme=prog path=/bin/mailman query=request query=list],
[prog:///bin/mailman?request&list])
URLCOMP(10,
[scheme=prog path=/bin/mailman query=request query=list param=test],
[prog:///bin/mailman;test?request&list])
URLCOMP(11,
[url=imap://gray@localhost/INBOX path=],
[imap://gray@localhost])
URLCOMP(12,
[url=imap://gray@localhost/INBOX path= user=],
[imap://localhost])
URLCOMP(13,
[url='prog:///bin/mailman;test?request&list' query= param=],
[prog:///bin/mailman])
URLCOMP(14,
[url=smtp://gray@localhost/test host=],
[],
[URL missing required parts])
m4_popdef([URLCOMP])
\ No newline at end of file
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/url.h>
#include <mailutils/secret.h>
#include <mailutils/kwd.h>
static const char *
strval (const char *str)
{
return str[0] ? str : NULL;
}
void
usage (FILE *str, int code)
{
fprintf (str, "usage: %s [url=URL] [OPTIONS]\n", mu_program_name);
exit (code);
}
int
main (int argc, char **argv)
{
int i = 1, rc;
mu_url_t url;
const char *arg;
mu_set_program_name (argv[0]);
if (argc > 1)
{
if (strcmp (argv[1], "help") == 0 ||
strcmp (argv[1], "--help") == 0 ||
strcmp (argv[1], "-h") == 0)
usage (stdout, 0);
if (strncmp (argv[1], "url=", 4) == 0)
{
MU_ASSERT (mu_url_create (&url, argv[1] + 4));
i = 2;
}
}
if (!url)
{
MU_ASSERT (mu_url_create_null (&url));
i = 1;
}
for (; i < argc; i++)
{
if (strncmp (argv[i], "scheme=", 7) == 0)
{
MU_ASSERT (mu_url_set_scheme (url, strval (argv[i] + 7)));
}
else if (strncmp (argv[i], "user=", 5) == 0)
{
MU_ASSERT (mu_url_set_user (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "path=", 5) == 0)
{
MU_ASSERT (mu_url_set_path (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "host=", 5) == 0)
{
MU_ASSERT (mu_url_set_host (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "port=", 5) == 0)
{
MU_ASSERT (mu_url_set_port (url, atoi (argv[i] + 5)));
}
else if (strncmp (argv[i], "service=", 8) == 0)
{
MU_ASSERT (mu_url_set_service (url, strval (argv[i] + 8)));
}
else if (strncmp (argv[i], "auth=", 5) == 0)
{
MU_ASSERT (mu_url_set_auth (url, strval (argv[i] + 5)));
}
else if (strncmp (argv[i], "pass=", 5) == 0)
{
mu_secret_t secret;
arg = strval (argv[i] + 5);
if (arg)
{
MU_ASSERT (mu_secret_create (&secret, arg, strlen (arg)));
}
else
secret = NULL;
MU_ASSERT (mu_url_set_secret (url, secret));
}
else if (strncmp (argv[i], "param=", 6) == 0)
{
arg = strval (argv[i] + 6);
if (arg)
MU_ASSERT (mu_url_add_param (url, 1, (const char **)&arg));
else
MU_ASSERT (mu_url_clear_param (url));
}
else if (strncmp (argv[i], "query=", 6) == 0)
{
arg = strval (argv[i] + 6);
if (arg)
MU_ASSERT (mu_url_add_query (url, 1, (const char **)&arg));
else
MU_ASSERT (mu_url_clear_query (url));
}
else
{
mu_error ("unrecognized argument: %s", argv[i]);
return 1;
}
}
rc = mu_url_sget_name (url, &arg);
if (rc)
{
mu_error ("%s", mu_strerror (rc));
return 1;
}
printf ("%s\n", arg);
return 0;
}
......@@ -19,6 +19,10 @@ noinst_LTLIBRARIES = liburl.la
liburl_la_SOURCES = \
accessor.h\
add-param.c\
clr-param.c\
add-query.c\
clr-query.c\
copy.c\
create.c\
decode.c\
......@@ -36,9 +40,19 @@ liburl_la_SOURCES = \
get-secret.c\
get-user.c\
match.c\
null.c\
port.c\
scheme.c\
set-auth.c\
set-host.c\
set-path.c\
set-port.c\
set-scheme.c\
set-secret.c\
set-service.c\
set-user.c\
uplevel.c\
urlinv.c\
urlstr.c
INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_add_param (mu_url_t url, size_t pc, const char **pv)
{
char **fv;
int i, j;
if (!url)
return EINVAL;
if (!pc || !pv)
return 0;
fv = realloc (url->fvpairs,
sizeof (url->fvpairs[0]) * (url->fvcount + pc + 1));
if (!fv)
return ENOMEM;
url->fvpairs = fv;
for (i = url->fvcount, j = 0; j < pc; i++, j++)
{
fv[i] = strdup (pv[j]);
if (!fv[i])
{
/* Restore the status quo */
for (; j; j--)
free (fv[--i]);
if (url->fvcount)
fv[url->fvcount] = NULL;
else
{
free (url->fvpairs);
url->fvpairs = NULL;
url->fvcount = 0;
}
return ENOMEM;
}
}
fv[i] = NULL;
url->fvcount = i;
url->flags |= MU_URL_PARAM;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_add_query (mu_url_t url, size_t pc, const char **pv)
{
char **fv;
int i, j;
if (!url)
return EINVAL;
if (!pc || !pv)
return 0;
fv = realloc (url->qargv,
sizeof (url->qargv[0]) * (url->qargc + pc + 1));
if (!fv)
return ENOMEM;
url->qargv = fv;
for (i = url->qargc, j = 0; j < pc; i++, j++)
{
fv[i] = strdup (pv[j]);
if (!fv[i])
{
/* Restore the status quo */
for (; j; j--)
free (fv[--i]);
if (url->qargc)
fv[url->qargc] = NULL;
else
{
free (url->qargv);
url->qargv = NULL;
url->qargc = 0;
}
return ENOMEM;
}
}
fv[i] = NULL;
url->qargc = i;
url->flags |= MU_URL_QUERY;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_clear_param (mu_url_t url)
{
int i;
if (!url)
return EINVAL;
for (i = 0; i < url->fvcount; i++)
free (url->fvpairs[i]);
free (url->fvpairs);
url->fvpairs = NULL;
url->fvcount = 0;
url->flags &= ~MU_URL_PARAM;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_clear_query (mu_url_t url)
{
int i;
if (!url)
return EINVAL;
for (i = 0; i < url->qargc; i++)
free (url->qargv[i]);
free (url->qargv);
url->qargv = NULL;
url->qargc = 0;
url->flags &= ~MU_URL_QUERY;
mu_url_invalidate (url);
return 0;
}
......@@ -532,7 +532,11 @@ mu_url_create_hint (mu_url_t *purl, const char *str, int flags,
{
int rc;
struct mu_url_ctx ctx;
mu_url_t url = calloc (1, sizeof (*url));
mu_url_t url;
if (!purl)
return EINVAL;
url = calloc (1, sizeof (*url));
if (url == NULL)
return ENOMEM;
url->name = strdup (str);
......
......@@ -27,7 +27,7 @@ mu_url_get_flags (mu_url_t url, int *pf)
{
if (!url || !pf)
return EINVAL;
*pf = url->flags;
*pf = url->flags;
return 0;
}
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010, 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_create_null (mu_url_t *purl)
{
mu_url_t url;
if (!purl)
return EINVAL;
url = calloc (1, sizeof (*url));
if (url == NULL)
return ENOMEM;
*purl = url;
return 0;
}
......@@ -31,19 +31,6 @@
#include <mailutils/sys/url.h>
int
mu_url_set_scheme (mu_url_t url, const char *scheme)
{
char *p;
if (!url || !scheme)
return EINVAL;
p = realloc (url->scheme, strlen (scheme) + 1);
if (!p)
return ENOMEM;
strcpy (url->scheme, scheme);
return 0;
}
int
mu_url_is_scheme (mu_url_t url, const char *scheme)
{
if (url && scheme && url->scheme
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_set_auth (mu_url_t url, const char *auth)
{
char *copy;
if (!url)
return EINVAL;
if (auth)
{
copy = strdup (auth);
if (!copy)
return ENOMEM;
url->flags |= MU_URL_AUTH;
}
else
{
url->flags &= ~MU_URL_AUTH;
copy = NULL;
}
free (url->auth);
url->auth = copy;
url->_get_auth = NULL;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_set_host (mu_url_t url, const char *host)
{
char *copy;
if (!url)
return EINVAL;
if (host)
{
size_t len;
int flag = MU_URL_HOST;
len = strlen (host);
if (len == 0)
return EINVAL;
if (host[0] == '[' && host[len-1] == ']')
{
flag |= MU_URL_IPV6;
host++;
len -= 2;
}
copy = malloc (len + 1);
if (!copy)
return ENOMEM;
memcpy (copy, host, len);
copy[len] = 0;
url->flags |= flag;
}
else
{
url->flags &= ~(MU_URL_HOST|MU_URL_IPV6);
copy = NULL;
}
url->_get_host = NULL;
free (url->host);
url->host = copy;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_set_path (mu_url_t url, const char *path)
{
char *copy;
if (!url)
return EINVAL;
if (path)
{
copy = strdup (path);
if (!copy)
return ENOMEM;
url->flags |= MU_URL_PATH;
}
else
{
url->flags &= ~MU_URL_PATH;
copy = NULL;
}
free (url->path);
url->path = copy;
url->_get_path = NULL;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_set_port (mu_url_t url, unsigned port)
{
char *copy;
if (!url)
return EINVAL;
if (port)
{
char nbuf[128];
snprintf (nbuf, sizeof nbuf, "%u", port);
copy = strdup (nbuf);
if (!copy)
return ENOMEM;
url->flags |= MU_URL_PORT;
}
else
{
copy = NULL;
url->flags &= ~MU_URL_PORT;
}
url->_get_port = NULL;
url->_get_portstr = NULL;
free (url->portstr);
url->port = port;
url->portstr = copy;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_set_scheme (mu_url_t url, const char *scheme)
{
char *copy;
if (!url)
return EINVAL;
if (scheme)
{
copy = strdup (scheme);
if (!copy)
return ENOMEM;
url->flags |= MU_URL_SCHEME;
}
else
{
url->flags &= ~MU_URL_SCHEME;
copy = NULL;
}
free (url->scheme);
url->scheme = copy;
url->_get_scheme = NULL;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
#include <mailutils/secret.h>
int
mu_url_set_secret (mu_url_t url, mu_secret_t secret)
{
if (!url)
return EINVAL;
if (secret)
{
url->flags |= MU_URL_SECRET;
}
else
{
url->flags &= ~MU_URL_SECRET;
}
mu_secret_destroy (&url->secret);
url->secret = secret;
url->_get_secret = NULL;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010, 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_set_service (mu_url_t url, const char *str)
{
unsigned port;
char *copy;
if (!url)
return EINVAL;
if (str)
{
unsigned long n;
char *p;
n = strtoul (str, &p, 10);
if (*p)
{
/* FIXME: 1. This assumes MU_URL_PARSE_PORTSRV. */
/* FIXME: 2. Another proto? */
struct servent *sp = getservbyname (str, "tcp");
if (!sp)
return MU_ERR_TCP_NO_PORT; /*FIXME: Error code?*/
port = ntohs (sp->s_port);
}
else if (n > USHRT_MAX)
return ERANGE;
copy = strdup (str);
if (!copy)
return ENOMEM;
url->flags |= MU_URL_PORT;
}
else
{
copy = NULL;
port = 0;
url->flags &= ~MU_URL_PORT;
}
url->_get_port = NULL;
url->_get_portstr = NULL;
free (url->portstr);
url->port = port;
url->portstr = copy;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mailutils/types.h>
#include <mailutils/errno.h>
#include <mailutils/sys/url.h>
#include <mailutils/url.h>
int
mu_url_set_user (mu_url_t url, const char *user)
{
char *copy;
if (!url)
return EINVAL;
if (user)
{
copy = strdup (user);
if (!copy)
return ENOMEM;
url->flags |= MU_URL_USER;
}
else
{
url->flags &= ~MU_URL_USER;
copy = NULL;
}
free (url->user);
url->user = copy;
url->_get_user = NULL;
mu_url_invalidate (url);
return 0;
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <mailutils/sys/url.h>
int
mu_url_invalidate (mu_url_t url)
{
if (!url)
return EINVAL;
free (url->name);
url->name = NULL;
return 0;
}
......@@ -19,13 +19,160 @@
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/sys/url.h>
#include <mailutils/errno.h>
#include <mailutils/util.h>
#include <mailutils/opool.h>
#define AUTH_PFX ";AUTH="
static int
url_reconstruct_to_pool (mu_url_t url, mu_opool_t pool)
{
if (url->flags & MU_URL_SCHEME)
{
int i;
mu_opool_append (pool, url->scheme, strlen (url->scheme));
mu_opool_append (pool, "://", 3);
if (url->flags & MU_URL_USER)
mu_opool_append (pool, url->user, strlen (url->user));
if (url->flags & MU_URL_SECRET)
mu_opool_append (pool, ":***", 4); /* FIXME: How about MU_URL_PARSE_HIDEPASS? */
if (url->flags & MU_URL_AUTH)
{
mu_opool_append (pool, AUTH_PFX, sizeof AUTH_PFX - 1);
mu_opool_append (pool, url->auth, strlen (url->auth));
}
if (url->flags & MU_URL_HOST)
{
if (url->flags & (MU_URL_USER|MU_URL_SECRET|MU_URL_AUTH))
mu_opool_append_char (pool, '@');
mu_opool_append (pool, url->host, strlen (url->host));
if (url->flags & MU_URL_PORT)
{
mu_opool_append_char (pool, ':');
mu_opool_append (pool, url->portstr, strlen (url->portstr));
}
}
else if (url->flags & (MU_URL_USER|MU_URL_SECRET|MU_URL_AUTH))
return MU_ERR_URL_MISS_PARTS;
if (url->flags & MU_URL_PATH)
{
if (url->flags & MU_URL_HOST)
mu_opool_append_char (pool, '/');
mu_opool_append (pool, url->path, strlen (url->path));
}
if (url->flags & MU_URL_PARAM)
{
for (i = 0; i < url->fvcount; i++)
{
mu_opool_append_char (pool, ';');
mu_opool_append (pool, url->fvpairs[i],
strlen (url->fvpairs[i]));
}
}
if (url->flags & MU_URL_QUERY)
{
mu_opool_append_char (pool, '?');
mu_opool_append (pool, url->qargv[0],
strlen (url->qargv[0]));
for (i = 1; i < url->qargc; i++)
{
mu_opool_append_char (pool, '&');
mu_opool_append (pool, url->qargv[i],
strlen (url->qargv[i]));
}
}
return 0;
}
else if (url->flags == MU_URL_PATH)
{
mu_opool_append (pool, url->path, strlen (url->path));
return 0;
}
return MU_ERR_URL_MISS_PARTS;
}
int
mu_url_sget_name (const mu_url_t url, const char **retptr)
{
if (!url)
return EINVAL;
if (!url->name)
{
mu_opool_t pool;
int rc;
char *ptr, *newname;
size_t size;
rc = mu_opool_create (&pool, 0);
if (rc)
return rc;
rc = url_reconstruct_to_pool (url, pool);
if (rc)
{
mu_opool_destroy (&pool);
return rc;
}
ptr = mu_opool_finish (pool, &size);
newname = realloc (url->name, size + 1);
if (!newname)
{
mu_opool_destroy (&pool);
return ENOMEM;
}
memcpy (newname, ptr, size);
newname[size] = 0;
url->name = newname;
mu_opool_destroy (&pool);
}
if (retptr)
*retptr = url->name;
return 0;
}
int
mu_url_aget_name (const mu_url_t url, char **ret)
{
char *s;
const char *ptr;
int rc = mu_url_sget_name (url, &ptr);
if (rc)
return rc;
s = strdup (ptr);
if (!s)
return errno;
*ret = s;
return 0;
}
int
mu_url_get_name (const mu_url_t url, char *buf, size_t size, size_t *n)
{
size_t i;
const char *ptr;
int rc = mu_url_sget_name (url, &ptr);
if (rc)
return rc;
i = mu_cpystr (buf, ptr, size);
if (n)
*n = i;
return 0;
}
const char *
mu_url_to_string (const mu_url_t url)
{
if (url == NULL || url->name == NULL)
const char *ptr;
if (mu_url_sget_name (url, &ptr))
return "";
return url->name;
return ptr;
}
......