Blame SOURCES/0008-curl-7.61.1-CVE-2018-20483.patch

b35f73
From 907da069c450ca20442839d9e95e3661a5c06b61 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Sun, 5 Aug 2018 11:51:07 +0200
b35f73
Subject: [PATCH 01/14] URL-API
b35f73
b35f73
See header file and man pages for API. All documented API details work
b35f73
and are tested in the 1560 test case.
b35f73
b35f73
Closes #2842
b35f73
b35f73
Upstream-commit: fb30ac5a2d63773c529c19259754e2b306ac2e2e
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/Makefile.inc        |    1 +
b35f73
 docs/libcurl/curl_url.3          |   61 ++
b35f73
 docs/libcurl/curl_url_cleanup.3  |   44 +
b35f73
 docs/libcurl/curl_url_dup.3      |   52 ++
b35f73
 docs/libcurl/curl_url_get.3      |  110 +++
b35f73
 docs/libcurl/curl_url_set.3      |  120 +++
b35f73
 docs/libcurl/symbols-in-versions |   30 +
b35f73
 include/curl/Makefile.am         |    4 +-
b35f73
 include/curl/curl.h              |    1 +
b35f73
 include/curl/urlapi.h            |  121 +++
b35f73
 lib/Makefile.inc                 |    5 +-
b35f73
 lib/escape.c                     |   20 +-
b35f73
 lib/escape.h                     |    3 +-
b35f73
 lib/imap.c                       |    3 +-
b35f73
 lib/transfer.c                   |  314 +------
b35f73
 lib/url.c                        |   44 +-
b35f73
 lib/url.h                        |    2 +
b35f73
 lib/{escape.h => urlapi-int.h}   |   22 +-
b35f73
 lib/urlapi.c                     | 1315 ++++++++++++++++++++++++++++++
b35f73
 tests/data/Makefile.inc          |    2 +
b35f73
 tests/data/test1560              |   28 +
b35f73
 tests/libtest/Makefile.am        |    5 +
b35f73
 tests/libtest/Makefile.inc       |    4 +
b35f73
 tests/libtest/lib1560.c          |  760 +++++++++++++++++
b35f73
 24 files changed, 2716 insertions(+), 355 deletions(-)
b35f73
 create mode 100644 docs/libcurl/curl_url.3
b35f73
 create mode 100644 docs/libcurl/curl_url_cleanup.3
b35f73
 create mode 100644 docs/libcurl/curl_url_dup.3
b35f73
 create mode 100644 docs/libcurl/curl_url_get.3
b35f73
 create mode 100644 docs/libcurl/curl_url_set.3
b35f73
 create mode 100644 include/curl/urlapi.h
b35f73
 copy lib/{escape.h => urlapi-int.h} (66%)
b35f73
 create mode 100644 lib/urlapi.c
b35f73
 create mode 100644 tests/data/test1560
b35f73
 create mode 100644 tests/libtest/lib1560.c
b35f73
b35f73
diff --git a/docs/libcurl/Makefile.inc b/docs/libcurl/Makefile.inc
b35f73
index eea48c4..955492c 100644
b35f73
--- a/docs/libcurl/Makefile.inc
b35f73
+++ b/docs/libcurl/Makefile.inc
b35f73
@@ -22,4 +22,5 @@ man_MANS = curl_easy_cleanup.3 curl_easy_getinfo.3 curl_easy_init.3      \
b35f73
   curl_mime_data.3 curl_mime_data_cb.3 curl_mime_filedata.3              \
b35f73
   curl_mime_filename.3 curl_mime_subparts.3                              \
b35f73
   curl_mime_type.3 curl_mime_headers.3 curl_mime_encoder.3 libcurl-env.3 \
b35f73
+  curl_url.3 curl_url_cleanup.3 curl_url_dup.3 curl_url_get.3 curl_url_set.3 \
b35f73
   libcurl-security.3
b35f73
diff --git a/docs/libcurl/curl_url.3 b/docs/libcurl/curl_url.3
b35f73
new file mode 100644
b35f73
index 0000000..0a56264
b35f73
--- /dev/null
b35f73
+++ b/docs/libcurl/curl_url.3
b35f73
@@ -0,0 +1,61 @@
b35f73
+.\" **************************************************************************
b35f73
+.\" *                                  _   _ ____  _
b35f73
+.\" *  Project                     ___| | | |  _ \| |
b35f73
+.\" *                             / __| | | | |_) | |
b35f73
+.\" *                            | (__| |_| |  _ <| |___
b35f73
+.\" *                             \___|\___/|_| \_\_____|
b35f73
+.\" *
b35f73
+.\" * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+.\" *
b35f73
+.\" * This software is licensed as described in the file COPYING, which
b35f73
+.\" * you should have received as part of this distribution. The terms
b35f73
+.\" * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+.\" *
b35f73
+.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+.\" * copies of the Software, and permit persons to whom the Software is
b35f73
+.\" * furnished to do so, under the terms of the COPYING file.
b35f73
+.\" *
b35f73
+.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+.\" * KIND, either express or implied.
b35f73
+.\" *
b35f73
+.\" **************************************************************************
b35f73
+.TH curl_url 3 "6 Aug 2018" "libcurl" "libcurl Manual"
b35f73
+.SH NAME
b35f73
+curl_url - returns a new CURLU handle
b35f73
+.SH SYNOPSIS
b35f73
+.B #include <curl/curl.h>
b35f73
+
b35f73
+CURLU *curl_url();
b35f73
+.SH EXPERIMENTAL
b35f73
+The URL API is considered \fBEXPERIMENTAL\fP until further notice. Please test
b35f73
+it, report bugs and help us perfect it. Once proven to be reliable, the
b35f73
+experimental label will be removed.
b35f73
+
b35f73
+While this API is marked experimental, we reserve the right to modify the API
b35f73
+slightly if we deem it necessary and it makes it notably better or easier to
b35f73
+use.
b35f73
+.SH DESCRIPTION
b35f73
+This function will allocates and returns a pointer to a fresh CURLU handle, to
b35f73
+be used for further use of the URL API.
b35f73
+.SH RETURN VALUE
b35f73
+Returns a \fBCURLU *\fP if successful, or NULL if out of memory.
b35f73
+.SH EXAMPLE
b35f73
+.nf
b35f73
+  CURLUcode rc;
b35f73
+  CURLU *url = curl_url();
b35f73
+  rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
b35f73
+  if(!rc) {
b35f73
+    char *scheme;
b35f73
+    rc = curl_url_get(url, CURLUPART_SCHEME, &scheme, 0);
b35f73
+    if(!rc) {
b35f73
+      printf("the scheme is %s\n", scheme);
b35f73
+      curl_free(scheme);
b35f73
+    }
b35f73
+    curl_url_cleanup(url);
b35f73
+  }
b35f73
+.fi
b35f73
+.SH AVAILABILITY
b35f73
+Added in curl 7.63.0
b35f73
+.SH "SEE ALSO"
b35f73
+.BR curl_url_cleanup "(3), " curl_url_get "(3), " curl_url_set "(3), "
b35f73
+.BR curl_url_dup "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_cleanup.3 b/docs/libcurl/curl_url_cleanup.3
b35f73
new file mode 100644
b35f73
index 0000000..a8158b7
b35f73
--- /dev/null
b35f73
+++ b/docs/libcurl/curl_url_cleanup.3
b35f73
@@ -0,0 +1,44 @@
b35f73
+.\" **************************************************************************
b35f73
+.\" *                                  _   _ ____  _
b35f73
+.\" *  Project                     ___| | | |  _ \| |
b35f73
+.\" *                             / __| | | | |_) | |
b35f73
+.\" *                            | (__| |_| |  _ <| |___
b35f73
+.\" *                             \___|\___/|_| \_\_____|
b35f73
+.\" *
b35f73
+.\" * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+.\" *
b35f73
+.\" * This software is licensed as described in the file COPYING, which
b35f73
+.\" * you should have received as part of this distribution. The terms
b35f73
+.\" * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+.\" *
b35f73
+.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+.\" * copies of the Software, and permit persons to whom the Software is
b35f73
+.\" * furnished to do so, under the terms of the COPYING file.
b35f73
+.\" *
b35f73
+.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+.\" * KIND, either express or implied.
b35f73
+.\" *
b35f73
+.\" **************************************************************************
b35f73
+.TH curl_url_cleanup 3 "6 Aug 2018" "libcurl" "libcurl Manual"
b35f73
+.SH NAME
b35f73
+curl_url_cleanup - free a CURLU handle
b35f73
+.SH SYNOPSIS
b35f73
+.B #include <curl/curl.h>
b35f73
+
b35f73
+void curl_url_cleanup(CURLU *handle);
b35f73
+.fi
b35f73
+.SH DESCRIPTION
b35f73
+Frees all the resources associated with the given CURLU handle!
b35f73
+.SH RETURN VALUE
b35f73
+none
b35f73
+.SH EXAMPLE
b35f73
+.nf
b35f73
+  CURLU *url = curl_url();
b35f73
+  curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
b35f73
+  curl_url_cleanup(url);
b35f73
+.fi
b35f73
+.SH AVAILABILITY
b35f73
+Added in curl 7.63.0
b35f73
+.SH "SEE ALSO"
b35f73
+.BR curl_url_dup "(3), " curl_url "(3), " curl_url_set "(3), "
b35f73
+.BR curl_url_get "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_dup.3 b/docs/libcurl/curl_url_dup.3
b35f73
new file mode 100644
b35f73
index 0000000..4815dbd
b35f73
--- /dev/null
b35f73
+++ b/docs/libcurl/curl_url_dup.3
b35f73
@@ -0,0 +1,52 @@
b35f73
+.\" **************************************************************************
b35f73
+.\" *                                  _   _ ____  _
b35f73
+.\" *  Project                     ___| | | |  _ \| |
b35f73
+.\" *                             / __| | | | |_) | |
b35f73
+.\" *                            | (__| |_| |  _ <| |___
b35f73
+.\" *                             \___|\___/|_| \_\_____|
b35f73
+.\" *
b35f73
+.\" * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+.\" *
b35f73
+.\" * This software is licensed as described in the file COPYING, which
b35f73
+.\" * you should have received as part of this distribution. The terms
b35f73
+.\" * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+.\" *
b35f73
+.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+.\" * copies of the Software, and permit persons to whom the Software is
b35f73
+.\" * furnished to do so, under the terms of the COPYING file.
b35f73
+.\" *
b35f73
+.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+.\" * KIND, either express or implied.
b35f73
+.\" *
b35f73
+.\" **************************************************************************
b35f73
+.TH curl_url_dup 3 "6 Aug 2018" "libcurl" "libcurl Manual"
b35f73
+.SH NAME
b35f73
+curl_url_dup - duplicate a CURLU handle
b35f73
+.SH SYNOPSIS
b35f73
+.B #include <curl/curl.h>
b35f73
+
b35f73
+CURLU *curl_url_dup(CURLU *inhandle);
b35f73
+.fi
b35f73
+.SH DESCRIPTION
b35f73
+Duplicates a given CURLU \fIinhandle\fP and all its contents and returns a
b35f73
+pointer to a new CURLU handle. The new handle also needs to be freed with
b35f73
+\fIcurl_url_cleanup(3)\fP.
b35f73
+.SH RETURN VALUE
b35f73
+Returns a new handle or NULL if out of memory.
b35f73
+.SH EXAMPLE
b35f73
+.nf
b35f73
+  CURLUcode rc;
b35f73
+  CURLU *url = curl_url();
b35f73
+  CURLU *url2;
b35f73
+  rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
b35f73
+  if(!rc) {
b35f73
+    url2 = curl_url_dup(url); /* clone it! */
b35f73
+    curl_url_cleanup(url2);
b35f73
+  }
b35f73
+  curl_url_cleanup(url);
b35f73
+.fi
b35f73
+.SH AVAILABILITY
b35f73
+Added in curl 7.63.0
b35f73
+.SH "SEE ALSO"
b35f73
+.BR curl_url_cleanup "(3), " curl_url "(3), " curl_url_set "(3), "
b35f73
+.BR curl_url_get "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_get.3 b/docs/libcurl/curl_url_get.3
b35f73
new file mode 100644
b35f73
index 0000000..824d496
b35f73
--- /dev/null
b35f73
+++ b/docs/libcurl/curl_url_get.3
b35f73
@@ -0,0 +1,110 @@
b35f73
+.\" **************************************************************************
b35f73
+.\" *                                  _   _ ____  _
b35f73
+.\" *  Project                     ___| | | |  _ \| |
b35f73
+.\" *                             / __| | | | |_) | |
b35f73
+.\" *                            | (__| |_| |  _ <| |___
b35f73
+.\" *                             \___|\___/|_| \_\_____|
b35f73
+.\" *
b35f73
+.\" * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+.\" *
b35f73
+.\" * This software is licensed as described in the file COPYING, which
b35f73
+.\" * you should have received as part of this distribution. The terms
b35f73
+.\" * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+.\" *
b35f73
+.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+.\" * copies of the Software, and permit persons to whom the Software is
b35f73
+.\" * furnished to do so, under the terms of the COPYING file.
b35f73
+.\" *
b35f73
+.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+.\" * KIND, either express or implied.
b35f73
+.\" *
b35f73
+.\" **************************************************************************
b35f73
+.TH curl_url_get 3 "6 Aug 2018" "libcurl" "libcurl Manual"
b35f73
+.SH NAME
b35f73
+curl_url_get - extract a part from a URL
b35f73
+.SH SYNOPSIS
b35f73
+.B #include <curl/curl.h>
b35f73
+
b35f73
+.nf
b35f73
+CURLUcode curl_url_get(CURLU *url,
b35f73
+                       CURLUPart what,
b35f73
+                       char **part,
b35f73
+                       unsigned int flags)
b35f73
+.fi
b35f73
+.SH DESCRIPTION
b35f73
+Given the \fIurl\fP handle of an already parsed URL, this function lets the
b35f73
+user extract individual pieces from it.
b35f73
+
b35f73
+The \fIwhat\fP argument should be the particular part to extract (see list
b35f73
+below) and \fIpart\fP points to a 'char *' to get updated to point to a newly
b35f73
+allocated string with the contents.
b35f73
+
b35f73
+The \fIflags\fP argument is a bitmask with individual features.
b35f73
+
b35f73
+The returned part pointer must be freed with \fIcurl_free(3)\fP after use.
b35f73
+.SH FLAGS
b35f73
+The flags argument is zero, one or more bits set in a bitmask.
b35f73
+.IP CURLU_DEFAULT_PORT
b35f73
+If the handle has no port stored, this option will make \fIcurl_url_get(3)\fP
b35f73
+return the default port for the used scheme.
b35f73
+.IP CURLU_DEFAULT_SCHEME
b35f73
+If the handle has no scheme stored, this option will make
b35f73
+\fIcurl_url_get(3)\fP return the default scheme instead of error.
b35f73
+.IP CURLU_NO_DEFAULT_PORT
b35f73
+Instructs \fIcurl_url_get(3)\fP to not return a port number if it matches the
b35f73
+default port for the scheme.
b35f73
+.IP CURLU_URLDECODE
b35f73
+Asks \fIcurl_url_get(3)\fP to URL decode the contents before returning it. It
b35f73
+will not attempt to decode the scheme, the port number or the full URL.
b35f73
+
b35f73
+The query component will also get plus-to-space convertion as a bonus when
b35f73
+this bit is set.
b35f73
+
b35f73
+Note that this URL decoding is charset unaware and you will get a zero
b35f73
+terminated string back with data that could be intended for a particular
b35f73
+encoding.
b35f73
+
b35f73
+If there's any byte values lower than 32 in the decoded string, the get
b35f73
+operation will return an error instead.
b35f73
+.SH PARTS
b35f73
+.IP CURLUPART_URL
b35f73
+When asked to return the full URL, \fIcurl_url_get(3)\fP will return a
b35f73
+normalized and possibly cleaned up version of what was previously parsed.
b35f73
+.IP CURLUPART_SCHEME
b35f73
+Scheme cannot be URL decoded on get.
b35f73
+.IP CURLUPART_USER
b35f73
+.IP CURLUPART_PASSWORD
b35f73
+.IP CURLUPART_OPTIONS
b35f73
+.IP CURLUPART_HOST
b35f73
+.IP CURLUPART_PORT
b35f73
+Port cannot be URL decoded on get.
b35f73
+.IP CURLUPART_PATH
b35f73
+.IP CURLUPART_QUERY
b35f73
+The query part will also get pluses converted to space when asked to URL
b35f73
+decode on get with the CURLU_URLDECODE bit.
b35f73
+.IP CURLUPART_FRAGMENT
b35f73
+.SH RETURN VALUE
b35f73
+Returns a CURLUcode error value, which is CURLUE_OK (0) if everything went
b35f73
+fine.
b35f73
+
b35f73
+If this function returns an error, no URL part is returned.
b35f73
+.SH EXAMPLE
b35f73
+.nf
b35f73
+  CURLUcode rc;
b35f73
+  CURLU *url = curl_url();
b35f73
+  rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
b35f73
+  if(!rc) {
b35f73
+    char *scheme;
b35f73
+    rc = curl_url_get(url, CURLUPART_SCHEME, &scheme, 0);
b35f73
+    if(!rc) {
b35f73
+      printf("the scheme is %s\n", scheme);
b35f73
+      curl_free(scheme);
b35f73
+    }
b35f73
+    curl_url_cleanup(url);
b35f73
+  }
b35f73
+.fi
b35f73
+.SH AVAILABILITY
b35f73
+Added in curl 7.63.0
b35f73
+.SH "SEE ALSO"
b35f73
+.BR curl_url_cleanup "(3), " curl_url "(3), " curl_url_set "(3), "
b35f73
+.BR curl_url_dup "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_set.3 b/docs/libcurl/curl_url_set.3
b35f73
new file mode 100644
b35f73
index 0000000..75fc0d9
b35f73
--- /dev/null
b35f73
+++ b/docs/libcurl/curl_url_set.3
b35f73
@@ -0,0 +1,120 @@
b35f73
+.\" **************************************************************************
b35f73
+.\" *                                  _   _ ____  _
b35f73
+.\" *  Project                     ___| | | |  _ \| |
b35f73
+.\" *                             / __| | | | |_) | |
b35f73
+.\" *                            | (__| |_| |  _ <| |___
b35f73
+.\" *                             \___|\___/|_| \_\_____|
b35f73
+.\" *
b35f73
+.\" * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+.\" *
b35f73
+.\" * This software is licensed as described in the file COPYING, which
b35f73
+.\" * you should have received as part of this distribution. The terms
b35f73
+.\" * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+.\" *
b35f73
+.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+.\" * copies of the Software, and permit persons to whom the Software is
b35f73
+.\" * furnished to do so, under the terms of the COPYING file.
b35f73
+.\" *
b35f73
+.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+.\" * KIND, either express or implied.
b35f73
+.\" *
b35f73
+.\" **************************************************************************
b35f73
+.TH curl_url_set 3 "6 Aug 2018" "libcurl" "libcurl Manual"
b35f73
+.SH NAME
b35f73
+curl_url_set - set a part from a URL
b35f73
+.SH SYNOPSIS
b35f73
+.B #include <curl/curl.h>
b35f73
+
b35f73
+CURLUcode curl_url_set(CURLU *url,
b35f73
+                       CURLUPart part,
b35f73
+                       const char *content,
b35f73
+                       unsigned int flags)
b35f73
+.fi
b35f73
+.SH DESCRIPTION
b35f73
+Given the \fIurl\fP handle of an already parsed URL, this function lets the
b35f73
+user set/update individual pieces of it.
b35f73
+
b35f73
+The \fIpart\fP argument should identify the particular URL part (see list
b35f73
+below) to set or change, with \fIcontent\fP pointing to a zero terminated
b35f73
+string with the new contents for that URL part. The contents should be in the
b35f73
+form and encoding they'd use in a URL: URL encoded.
b35f73
+
b35f73
+Setting a part to a NULL pointer will effectively remove that part's contents
b35f73
+from the CURLU handle.
b35f73
+
b35f73
+The \fIflags\fP argument is a bitmask with independent features.
b35f73
+.SH PARTS
b35f73
+.IP CURLUPART_URL
b35f73
+Allows the full URL of the handle to be replaced. If the handle already is
b35f73
+populated with a URL, the new URL can be relative to the previous.
b35f73
+
b35f73
+When successfully setting a new URL, relative or absolute, the handle contents
b35f73
+will be replaced with the information of the newly set URL.
b35f73
+
b35f73
+Pass a pointer to a zero terminated string to the \fIurl\fP parameter. The
b35f73
+string must point to a correctly formatted "RFC 3986+" URL or be a NULL
b35f73
+pointer.
b35f73
+.IP CURLUPART_SCHEME
b35f73
+Scheme cannot be URL decoded on set.
b35f73
+.IP CURLUPART_USER
b35f73
+.IP CURLUPART_PASSWORD
b35f73
+.IP CURLUPART_OPTIONS
b35f73
+.IP CURLUPART_HOST
b35f73
+The host name can use IDNA. The string must then be encoded as your locale
b35f73
+says or UTF-8 (when winidn is used).
b35f73
+.IP CURLUPART_PORT
b35f73
+Port cannot be URL encoded on set.
b35f73
+.IP CURLUPART_PATH
b35f73
+If a path is set in the URL without a leading slash, a slash will be inserted
b35f73
+automatically when this URL is read from the handle.
b35f73
+.IP CURLUPART_QUERY
b35f73
+The query part will also get spaces converted to pluses when asked to URL
b35f73
+encode on set with the CURLU_URLENCODE bit.
b35f73
+
b35f73
+If used in with \fICURLU_APPENDQUERY\fP, the provided part will be appended on
b35f73
+the end of the existing query - and if the previous part didn't end with an
b35f73
+ampersand (&), an ampersand will be inserted before the new appended part.
b35f73
+
b35f73
+When \fCURLU_APPENDQUERY\fP is used together with \fICURLU_URLENCODE\fP,
b35f73
+the '=' symbols will not be URL encoded.
b35f73
+
b35f73
+The question mark in the URL is not part of the actual query contents.
b35f73
+.IP CURLUPART_FRAGMENT
b35f73
+The hash sign in the URL is not part of the actual fragment contents.
b35f73
+.SH FLAGS
b35f73
+The flags argument is zero, one or more bits set in a bitmask.
b35f73
+.IP CURLU_NON_SUPPORT_SCHEME
b35f73
+If set, allows \fIcurl_url_set(3)\fP to set a non-supported scheme.
b35f73
+.IP CURLU_URLENCODE
b35f73
+When set, \fIcurl_url_set(3)\fP URL encodes the part on entry, except for
b35f73
+scheme, port and URL.
b35f73
+
b35f73
+When setting the path component with URL encoding enabled, the slash character
b35f73
+will be skipped.
b35f73
+
b35f73
+The query part gets space-to-plus conversion before the URL conversion.
b35f73
+
b35f73
+This URL encoding is charset unaware and will convert the input on a
b35f73
+byte-by-byte manner.
b35f73
+.SH RETURN VALUE
b35f73
+Returns a CURLUcode error value, which is CURLUE_OK (0) if everything went
b35f73
+fine.
b35f73
+
b35f73
+If this function returns an error, no URL part is returned.
b35f73
+.SH EXAMPLE
b35f73
+.nf
b35f73
+  CURLUcode rc;
b35f73
+  CURLU *url = curl_url();
b35f73
+  rc = curl_url_set(url, CURLUPART_URL, "https://example.com", 0);
b35f73
+  if(!rc) {
b35f73
+    char *scheme;
b35f73
+    /* change it to an FTP URL */
b35f73
+    rc = curl_url_set(url, CURLUPART_SCHEME, "ftp", 0);
b35f73
+  }
b35f73
+  curl_url_cleanup(url);
b35f73
+.fi
b35f73
+.SH AVAILABILITY
b35f73
+Added in curl 7.63.0
b35f73
+.SH "SEE ALSO"
b35f73
+.BR curl_url_cleanup "(3), " curl_url "(3), " curl_url_get "(3), "
b35f73
+.BR curl_url_dup "(3), "
b35f73
diff --git a/docs/libcurl/symbols-in-versions b/docs/libcurl/symbols-in-versions
b35f73
index 7448b4f..c797cb7 100644
b35f73
--- a/docs/libcurl/symbols-in-versions
b35f73
+++ b/docs/libcurl/symbols-in-versions
b35f73
@@ -718,6 +718,36 @@ CURLSSLSET_NO_BACKENDS          7.56.0
b35f73
 CURLSSLSET_OK                   7.56.0
b35f73
 CURLSSLSET_TOO_LATE             7.56.0
b35f73
 CURLSSLSET_UNKNOWN_BACKEND      7.56.0
b35f73
+CURLUPART_FRAGMENT              7.62.0
b35f73
+CURLUPART_HOST                  7.62.0
b35f73
+CURLUPART_OPTIONS               7.62.0
b35f73
+CURLUPART_PASSWORD              7.62.0
b35f73
+CURLUPART_PATH                  7.62.0
b35f73
+CURLUPART_PORT                  7.62.0
b35f73
+CURLUPART_QUERY                 7.62.0
b35f73
+CURLUPART_SCHEME                7.62.0
b35f73
+CURLUPART_URL                   7.62.0
b35f73
+CURLUPART_USER                  7.62.0
b35f73
+CURLUE_BAD_HANDLE               7.62.0
b35f73
+CURLUE_BAD_PARTPOINTER          7.62.0
b35f73
+CURLUE_BAD_PORT_NUMBER          7.62.0
b35f73
+CURLUE_MALFORMED_INPUT          7.62.0
b35f73
+CURLUE_NO_FRAGMENT              7.62.0
b35f73
+CURLUE_NO_HOST                  7.62.0
b35f73
+CURLUE_NO_OPTIONS               7.62.0
b35f73
+CURLUE_NO_PASSWORD              7.62.0
b35f73
+CURLUE_NO_PATH                  7.62.0
b35f73
+CURLUE_NO_PORT                  7.62.0
b35f73
+CURLUE_NO_QUERY                 7.62.0
b35f73
+CURLUE_NO_SCHEME                7.62.0
b35f73
+CURLUE_NO_USER                  7.62.0
b35f73
+CURLUE_OK                       7.62.0
b35f73
+CURLUE_OUT_OF_MEMORY            7.62.0
b35f73
+CURLUE_RELATIVE                 7.62.0
b35f73
+CURLUE_UNKNOWN_PART             7.62.0
b35f73
+CURLUE_UNSUPPORTED_SCHEME       7.62.0
b35f73
+CURLUE_URLDECODE                7.62.0
b35f73
+CURLUE_USER_NOT_ALLOWED         7.62.0
b35f73
 CURLUSESSL_ALL                  7.17.0
b35f73
 CURLUSESSL_CONTROL              7.17.0
b35f73
 CURLUSESSL_NONE                 7.17.0
b35f73
diff --git a/include/curl/Makefile.am b/include/curl/Makefile.am
b35f73
index 989d4a2..bf5f061 100644
b35f73
--- a/include/curl/Makefile.am
b35f73
+++ b/include/curl/Makefile.am
b35f73
@@ -5,7 +5,7 @@
b35f73
 #                            | (__| |_| |  _ <| |___
b35f73
 #                             \___|\___/|_| \_\_____|
b35f73
 #
b35f73
-# Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+# Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
 #
b35f73
 # This software is licensed as described in the file COPYING, which
b35f73
 # you should have received as part of this distribution. The terms
b35f73
@@ -21,7 +21,7 @@
b35f73
 ###########################################################################
b35f73
 pkginclude_HEADERS = \
b35f73
 	curl.h curlver.h easy.h mprintf.h stdcheaders.h multi.h \
b35f73
-	typecheck-gcc.h system.h
b35f73
+	typecheck-gcc.h system.h urlapi.h
b35f73
 
b35f73
 pkgincludedir= $(includedir)/curl
b35f73
 
b35f73
diff --git a/include/curl/curl.h b/include/curl/curl.h
b35f73
index 067b34d..8f473e2 100644
b35f73
--- a/include/curl/curl.h
b35f73
+++ b/include/curl/curl.h
b35f73
@@ -2779,6 +2779,7 @@ CURL_EXTERN CURLcode curl_easy_pause(CURL *handle, int bitmask);
b35f73
   stuff before they can be included! */
b35f73
 #include "easy.h" /* nothing in curl is fun without the easy stuff */
b35f73
 #include "multi.h"
b35f73
+#include "urlapi.h"
b35f73
 
b35f73
 /* the typechecker doesn't work in C++ (yet) */
b35f73
 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && \
b35f73
diff --git a/include/curl/urlapi.h b/include/curl/urlapi.h
b35f73
new file mode 100644
b35f73
index 0000000..b16cfce
b35f73
--- /dev/null
b35f73
+++ b/include/curl/urlapi.h
b35f73
@@ -0,0 +1,121 @@
b35f73
+#ifndef __CURL_URLAPI_H
b35f73
+#define __CURL_URLAPI_H
b35f73
+/***************************************************************************
b35f73
+ *                                  _   _ ____  _
b35f73
+ *  Project                     ___| | | |  _ \| |
b35f73
+ *                             / __| | | | |_) | |
b35f73
+ *                            | (__| |_| |  _ <| |___
b35f73
+ *                             \___|\___/|_| \_\_____|
b35f73
+ *
b35f73
+ * Copyright (C) 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ *
b35f73
+ * This software is licensed as described in the file COPYING, which
b35f73
+ * you should have received as part of this distribution. The terms
b35f73
+ * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+ *
b35f73
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+ * copies of the Software, and permit persons to whom the Software is
b35f73
+ * furnished to do so, under the terms of the COPYING file.
b35f73
+ *
b35f73
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+ * KIND, either express or implied.
b35f73
+ *
b35f73
+ ***************************************************************************/
b35f73
+
b35f73
+#ifdef  __cplusplus
b35f73
+extern "C" {
b35f73
+#endif
b35f73
+
b35f73
+/* the error codes for the URL API */
b35f73
+typedef enum {
b35f73
+  CURLUE_OK,
b35f73
+  CURLUE_BAD_HANDLE,          /* 1 */
b35f73
+  CURLUE_BAD_PARTPOINTER,     /* 2 */
b35f73
+  CURLUE_MALFORMED_INPUT,     /* 3 */
b35f73
+  CURLUE_BAD_PORT_NUMBER,     /* 4 */
b35f73
+  CURLUE_UNSUPPORTED_SCHEME,  /* 5 */
b35f73
+  CURLUE_URLDECODE,           /* 6 */
b35f73
+  CURLUE_RELATIVE,            /* 7 */
b35f73
+  CURLUE_USER_NOT_ALLOWED,    /* 8 */
b35f73
+  CURLUE_UNKNOWN_PART,        /* 9 */
b35f73
+  CURLUE_NO_SCHEME,           /* 10 */
b35f73
+  CURLUE_NO_USER,             /* 11 */
b35f73
+  CURLUE_NO_PASSWORD,         /* 12 */
b35f73
+  CURLUE_NO_OPTIONS,          /* 13 */
b35f73
+  CURLUE_NO_HOST,             /* 14 */
b35f73
+  CURLUE_NO_PORT,             /* 15 */
b35f73
+  CURLUE_NO_PATH,             /* 16 */
b35f73
+  CURLUE_NO_QUERY,            /* 17 */
b35f73
+  CURLUE_NO_FRAGMENT,         /* 18 */
b35f73
+  CURLUE_OUT_OF_MEMORY        /* 19 */
b35f73
+} CURLUcode;
b35f73
+
b35f73
+typedef enum {
b35f73
+  CURLUPART_URL,
b35f73
+  CURLUPART_SCHEME,
b35f73
+  CURLUPART_USER,
b35f73
+  CURLUPART_PASSWORD,
b35f73
+  CURLUPART_OPTIONS,
b35f73
+  CURLUPART_HOST,
b35f73
+  CURLUPART_PORT,
b35f73
+  CURLUPART_PATH,
b35f73
+  CURLUPART_QUERY,
b35f73
+  CURLUPART_FRAGMENT
b35f73
+} CURLUPart;
b35f73
+
b35f73
+#define CURLU_DEFAULT_PORT (1<<0)       /* return default port number */
b35f73
+#define CURLU_NO_DEFAULT_PORT (1<<1)    /* act as if no port number was set,
b35f73
+                                           if the port number matches the
b35f73
+                                           default for the scheme */
b35f73
+#define CURLU_DEFAULT_SCHEME (1<<2)     /* return default scheme if
b35f73
+                                           missing */
b35f73
+#define CURLU_NON_SUPPORT_SCHEME (1<<3) /* allow non-supported scheme */
b35f73
+#define CURLU_PATH_AS_IS (1<<4)         /* leave dot sequences */
b35f73
+#define CURLU_DISALLOW_USER (1<<5)      /* no user+password allowed */
b35f73
+#define CURLU_URLDECODE (1<<6)          /* URL decode on get */
b35f73
+#define CURLU_URLENCODE (1<<7)          /* URL encode on set */
b35f73
+#define CURLU_APPENDQUERY (1<<8)        /* append a form style part */
b35f73
+
b35f73
+typedef struct Curl_URL CURLU;
b35f73
+
b35f73
+/*
b35f73
+ * curl_url() creates a new CURLU handle and returns a pointer to it.
b35f73
+ * Must be freed with curl_url_cleanup().
b35f73
+ */
b35f73
+CURL_EXTERN CURLU *curl_url(void);
b35f73
+
b35f73
+/*
b35f73
+ * curl_url_cleanup() frees the CURLU handle and related resources used for
b35f73
+ * the URL parsing. It will not free strings previously returned with the URL
b35f73
+ * API.
b35f73
+ */
b35f73
+CURL_EXTERN void curl_url_cleanup(CURLU *handle);
b35f73
+
b35f73
+/*
b35f73
+ * curl_url_dup() duplicates a CURLU handle and returns a new copy. The new
b35f73
+ * handle must also be freed with curl_url_cleanup().
b35f73
+ */
b35f73
+CURL_EXTERN CURLU *curl_url_dup(CURLU *in);
b35f73
+
b35f73
+/*
b35f73
+ * curl_url_get() extracts a specific part of the URL from a CURLU
b35f73
+ * handle. Returns error code. The returned pointer MUST be freed with
b35f73
+ * curl_free() afterwards.
b35f73
+ */
b35f73
+CURL_EXTERN CURLUcode curl_url_get(CURLU *handle, CURLUPart what,
b35f73
+                                   char **part, unsigned int flags);
b35f73
+
b35f73
+/*
b35f73
+ * curl_url_set() sets a specific part of the URL in a CURLU handle. Returns
b35f73
+ * error code. The passed in string will be copied. Passing a NULL instead of
b35f73
+ * a part string, clears that part.
b35f73
+ */
b35f73
+CURL_EXTERN CURLUcode curl_url_set(CURLU *handle, CURLUPart what,
b35f73
+                                   const char *part, unsigned int flags);
b35f73
+
b35f73
+
b35f73
+#ifdef __cplusplus
b35f73
+} /* end of extern "C" */
b35f73
+#endif
b35f73
+
b35f73
+#endif
b35f73
diff --git a/lib/Makefile.inc b/lib/Makefile.inc
b35f73
index 76ca6d0..1ff82e1 100644
b35f73
--- a/lib/Makefile.inc
b35f73
+++ b/lib/Makefile.inc
b35f73
@@ -54,7 +54,8 @@ LIB_CFILES = file.c timeval.c base64.c hostip.c progress.c formdata.c   \
b35f73
   http_ntlm.c curl_ntlm_wb.c curl_ntlm_core.c curl_sasl.c rand.c        \
b35f73
   curl_multibyte.c hostcheck.c conncache.c pipeline.c dotdot.c          \
b35f73
   x509asn1.c http2.c smb.c curl_endian.c curl_des.c system_win32.c      \
b35f73
-  mime.c sha256.c setopt.c curl_path.c curl_ctype.c curl_range.c psl.c
b35f73
+  mime.c sha256.c setopt.c curl_path.c curl_ctype.c curl_range.c psl.c  \
b35f73
+  urlapi.c
b35f73
 
b35f73
 LIB_HFILES = arpa_telnet.h netrc.h file.h timeval.h hostip.h progress.h \
b35f73
   formdata.h cookie.h http.h sendf.h ftp.h url.h dict.h if2ip.h         \
b35f73
@@ -74,7 +75,7 @@ LIB_HFILES = arpa_telnet.h netrc.h file.h timeval.h hostip.h progress.h \
b35f73
   curl_setup_once.h multihandle.h setup-vms.h pipeline.h dotdot.h       \
b35f73
   x509asn1.h http2.h sigpipe.h smb.h curl_endian.h curl_des.h           \
b35f73
   curl_printf.h system_win32.h rand.h mime.h curl_sha256.h setopt.h     \
b35f73
-  curl_path.h curl_ctype.h curl_range.h psl.h
b35f73
+  curl_path.h curl_ctype.h curl_range.h psl.h urlapi-int.h
b35f73
 
b35f73
 LIB_RCFILES = libcurl.rc
b35f73
 
b35f73
diff --git a/lib/escape.c b/lib/escape.c
b35f73
index 10774f0..afd3899 100644
b35f73
--- a/lib/escape.c
b35f73
+++ b/lib/escape.c
b35f73
@@ -5,7 +5,7 @@
b35f73
  *                            | (__| |_| |  _ <| |___
b35f73
  *                             \___|\___/|_| \_\_____|
b35f73
  *
b35f73
- * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
  *
b35f73
  * This software is licensed as described in the file COPYING, which
b35f73
  * you should have received as part of this distribution. The terms
b35f73
@@ -41,7 +41,7 @@
b35f73
    its behavior is altered by the current locale.
b35f73
    See https://tools.ietf.org/html/rfc3986#section-2.3
b35f73
 */
b35f73
-static bool Curl_isunreserved(unsigned char in)
b35f73
+bool Curl_isunreserved(unsigned char in)
b35f73
 {
b35f73
   switch(in) {
b35f73
     case '0': case '1': case '2': case '3': case '4':
b35f73
@@ -141,6 +141,8 @@ char *curl_easy_escape(struct Curl_easy *data, const char *string,
b35f73
  * Returns a pointer to a malloced string in *ostring with length given in
b35f73
  * *olen. If length == 0, the length is assumed to be strlen(string).
b35f73
  *
b35f73
+ * 'data' can be set to NULL but then this function can't convert network
b35f73
+ * data to host for non-ascii.
b35f73
  */
b35f73
 CURLcode Curl_urldecode(struct Curl_easy *data,
b35f73
                         const char *string, size_t length,
b35f73
@@ -151,7 +153,7 @@ CURLcode Curl_urldecode(struct Curl_easy *data,
b35f73
   char *ns = malloc(alloc);
b35f73
   size_t strindex = 0;
b35f73
   unsigned long hex;
b35f73
-  CURLcode result;
b35f73
+  CURLcode result = CURLE_OK;
b35f73
 
b35f73
   if(!ns)
b35f73
     return CURLE_OUT_OF_MEMORY;
b35f73
@@ -171,11 +173,13 @@ CURLcode Curl_urldecode(struct Curl_easy *data,
b35f73
 
b35f73
       in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
b35f73
 
b35f73
-      result = Curl_convert_from_network(data, (char *)&in, 1);
b35f73
-      if(result) {
b35f73
-        /* Curl_convert_from_network calls failf if unsuccessful */
b35f73
-        free(ns);
b35f73
-        return result;
b35f73
+      if(data) {
b35f73
+        result = Curl_convert_from_network(data, (char *)&in, 1);
b35f73
+        if(result) {
b35f73
+          /* Curl_convert_from_network calls failf if unsuccessful */
b35f73
+          free(ns);
b35f73
+          return result;
b35f73
+        }
b35f73
       }
b35f73
 
b35f73
       string += 2;
b35f73
diff --git a/lib/escape.h b/lib/escape.h
b35f73
index 638666f..666f1ad 100644
b35f73
--- a/lib/escape.h
b35f73
+++ b/lib/escape.h
b35f73
@@ -7,7 +7,7 @@
b35f73
  *                            | (__| |_| |  _ <| |___
b35f73
  *                             \___|\___/|_| \_\_____|
b35f73
  *
b35f73
- * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
  *
b35f73
  * This software is licensed as described in the file COPYING, which
b35f73
  * you should have received as part of this distribution. The terms
b35f73
@@ -24,6 +24,7 @@
b35f73
 /* Escape and unescape URL encoding in strings. The functions return a new
b35f73
  * allocated string or NULL if an error occurred.  */
b35f73
 
b35f73
+bool Curl_isunreserved(unsigned char in);
b35f73
 CURLcode Curl_urldecode(struct Curl_easy *data,
b35f73
                         const char *string, size_t length,
b35f73
                         char **ostring, size_t *olen,
b35f73
diff --git a/lib/imap.c b/lib/imap.c
b35f73
index 942fe7d..28962c1 100644
b35f73
--- a/lib/imap.c
b35f73
+++ b/lib/imap.c
b35f73
@@ -159,7 +159,8 @@ const struct Curl_handler Curl_handler_imaps = {
b35f73
   ZERO_NULL,                        /* connection_check */
b35f73
   PORT_IMAPS,                       /* defport */
b35f73
   CURLPROTO_IMAPS,                  /* protocol */
b35f73
-  PROTOPT_CLOSEACTION | PROTOPT_SSL /* flags */
b35f73
+  PROTOPT_CLOSEACTION | PROTOPT_SSL | /* flags */
b35f73
+  PROTOPT_URLOPTIONS
b35f73
 };
b35f73
 #endif
b35f73
 
b35f73
diff --git a/lib/transfer.c b/lib/transfer.c
b35f73
index 7159d5c..ecd1063 100644
b35f73
--- a/lib/transfer.c
b35f73
+++ b/lib/transfer.c
b35f73
@@ -75,6 +75,7 @@
b35f73
 #include "http2.h"
b35f73
 #include "mime.h"
b35f73
 #include "strcase.h"
b35f73
+#include "urlapi-int.h"
b35f73
 
b35f73
 /* The last 3 #include files should be in this order */
b35f73
 #include "curl_printf.h"
b35f73
@@ -1453,311 +1454,6 @@ CURLcode Curl_posttransfer(struct Curl_easy *data)
b35f73
   return CURLE_OK;
b35f73
 }
b35f73
 
b35f73
-#ifndef CURL_DISABLE_HTTP
b35f73
-/*
b35f73
- * Find the separator at the end of the host name, or the '?' in cases like
b35f73
- * http://www.url.com?id=2380
b35f73
- */
b35f73
-static const char *find_host_sep(const char *url)
b35f73
-{
b35f73
-  const char *sep;
b35f73
-  const char *query;
b35f73
-
b35f73
-  /* Find the start of the hostname */
b35f73
-  sep = strstr(url, "//");
b35f73
-  if(!sep)
b35f73
-    sep = url;
b35f73
-  else
b35f73
-    sep += 2;
b35f73
-
b35f73
-  query = strchr(sep, '?');
b35f73
-  sep = strchr(sep, '/');
b35f73
-
b35f73
-  if(!sep)
b35f73
-    sep = url + strlen(url);
b35f73
-
b35f73
-  if(!query)
b35f73
-    query = url + strlen(url);
b35f73
-
b35f73
-  return sep < query ? sep : query;
b35f73
-}
b35f73
-
b35f73
-/*
b35f73
- * Decide in an encoding-independent manner whether a character in an
b35f73
- * URL must be escaped. The same criterion must be used in strlen_url()
b35f73
- * and strcpy_url().
b35f73
- */
b35f73
-static bool urlchar_needs_escaping(int c)
b35f73
-{
b35f73
-    return !(ISCNTRL(c) || ISSPACE(c) || ISGRAPH(c));
b35f73
-}
b35f73
-
b35f73
-/*
b35f73
- * strlen_url() returns the length of the given URL if the spaces within the
b35f73
- * URL were properly URL encoded.
b35f73
- * URL encoding should be skipped for host names, otherwise IDN resolution
b35f73
- * will fail.
b35f73
- */
b35f73
-static size_t strlen_url(const char *url, bool relative)
b35f73
-{
b35f73
-  const unsigned char *ptr;
b35f73
-  size_t newlen = 0;
b35f73
-  bool left = TRUE; /* left side of the ? */
b35f73
-  const unsigned char *host_sep = (const unsigned char *) url;
b35f73
-
b35f73
-  if(!relative)
b35f73
-    host_sep = (const unsigned char *) find_host_sep(url);
b35f73
-
b35f73
-  for(ptr = (unsigned char *)url; *ptr; ptr++) {
b35f73
-
b35f73
-    if(ptr < host_sep) {
b35f73
-      ++newlen;
b35f73
-      continue;
b35f73
-    }
b35f73
-
b35f73
-    switch(*ptr) {
b35f73
-    case '?':
b35f73
-      left = FALSE;
b35f73
-      /* FALLTHROUGH */
b35f73
-    default:
b35f73
-      if(urlchar_needs_escaping(*ptr))
b35f73
-        newlen += 2;
b35f73
-      newlen++;
b35f73
-      break;
b35f73
-    case ' ':
b35f73
-      if(left)
b35f73
-        newlen += 3;
b35f73
-      else
b35f73
-        newlen++;
b35f73
-      break;
b35f73
-    }
b35f73
-  }
b35f73
-  return newlen;
b35f73
-}
b35f73
-
b35f73
-/* strcpy_url() copies a url to a output buffer and URL-encodes the spaces in
b35f73
- * the source URL accordingly.
b35f73
- * URL encoding should be skipped for host names, otherwise IDN resolution
b35f73
- * will fail.
b35f73
- */
b35f73
-static void strcpy_url(char *output, const char *url, bool relative)
b35f73
-{
b35f73
-  /* we must add this with whitespace-replacing */
b35f73
-  bool left = TRUE;
b35f73
-  const unsigned char *iptr;
b35f73
-  char *optr = output;
b35f73
-  const unsigned char *host_sep = (const unsigned char *) url;
b35f73
-
b35f73
-  if(!relative)
b35f73
-    host_sep = (const unsigned char *) find_host_sep(url);
b35f73
-
b35f73
-  for(iptr = (unsigned char *)url;    /* read from here */
b35f73
-      *iptr;         /* until zero byte */
b35f73
-      iptr++) {
b35f73
-
b35f73
-    if(iptr < host_sep) {
b35f73
-      *optr++ = *iptr;
b35f73
-      continue;
b35f73
-    }
b35f73
-
b35f73
-    switch(*iptr) {
b35f73
-    case '?':
b35f73
-      left = FALSE;
b35f73
-      /* FALLTHROUGH */
b35f73
-    default:
b35f73
-      if(urlchar_needs_escaping(*iptr)) {
b35f73
-        snprintf(optr, 4, "%%%02x", *iptr);
b35f73
-        optr += 3;
b35f73
-      }
b35f73
-      else
b35f73
-        *optr++=*iptr;
b35f73
-      break;
b35f73
-    case ' ':
b35f73
-      if(left) {
b35f73
-        *optr++='%'; /* add a '%' */
b35f73
-        *optr++='2'; /* add a '2' */
b35f73
-        *optr++='0'; /* add a '0' */
b35f73
-      }
b35f73
-      else
b35f73
-        *optr++='+'; /* add a '+' here */
b35f73
-      break;
b35f73
-    }
b35f73
-  }
b35f73
-  *optr = 0; /* zero terminate output buffer */
b35f73
-
b35f73
-}
b35f73
-
b35f73
-/*
b35f73
- * Returns true if the given URL is absolute (as opposed to relative)
b35f73
- */
b35f73
-static bool is_absolute_url(const char *url)
b35f73
-{
b35f73
-  char prot[16]; /* URL protocol string storage */
b35f73
-  char letter;   /* used for a silly sscanf */
b35f73
-
b35f73
-  return (2 == sscanf(url, "%15[^?&/:]://%c", prot, &letter)) ? TRUE : FALSE;
b35f73
-}
b35f73
-
b35f73
-/*
b35f73
- * Concatenate a relative URL to a base URL making it absolute.
b35f73
- * URL-encodes any spaces.
b35f73
- * The returned pointer must be freed by the caller unless NULL
b35f73
- * (returns NULL on out of memory).
b35f73
- */
b35f73
-static char *concat_url(const char *base, const char *relurl)
b35f73
-{
b35f73
-  /***
b35f73
-   TRY to append this new path to the old URL
b35f73
-   to the right of the host part. Oh crap, this is doomed to cause
b35f73
-   problems in the future...
b35f73
-  */
b35f73
-  char *newest;
b35f73
-  char *protsep;
b35f73
-  char *pathsep;
b35f73
-  size_t newlen;
b35f73
-  bool host_changed = FALSE;
b35f73
-
b35f73
-  const char *useurl = relurl;
b35f73
-  size_t urllen;
b35f73
-
b35f73
-  /* we must make our own copy of the URL to play with, as it may
b35f73
-     point to read-only data */
b35f73
-  char *url_clone = strdup(base);
b35f73
-
b35f73
-  if(!url_clone)
b35f73
-    return NULL; /* skip out of this NOW */
b35f73
-
b35f73
-  /* protsep points to the start of the host name */
b35f73
-  protsep = strstr(url_clone, "//");
b35f73
-  if(!protsep)
b35f73
-    protsep = url_clone;
b35f73
-  else
b35f73
-    protsep += 2; /* pass the slashes */
b35f73
-
b35f73
-  if('/' != relurl[0]) {
b35f73
-    int level = 0;
b35f73
-
b35f73
-    /* First we need to find out if there's a ?-letter in the URL,
b35f73
-       and cut it and the right-side of that off */
b35f73
-    pathsep = strchr(protsep, '?');
b35f73
-    if(pathsep)
b35f73
-      *pathsep = 0;
b35f73
-
b35f73
-    /* we have a relative path to append to the last slash if there's one
b35f73
-       available, or if the new URL is just a query string (starts with a
b35f73
-       '?')  we append the new one at the end of the entire currently worked
b35f73
-       out URL */
b35f73
-    if(useurl[0] != '?') {
b35f73
-      pathsep = strrchr(protsep, '/');
b35f73
-      if(pathsep)
b35f73
-        *pathsep = 0;
b35f73
-    }
b35f73
-
b35f73
-    /* Check if there's any slash after the host name, and if so, remember
b35f73
-       that position instead */
b35f73
-    pathsep = strchr(protsep, '/');
b35f73
-    if(pathsep)
b35f73
-      protsep = pathsep + 1;
b35f73
-    else
b35f73
-      protsep = NULL;
b35f73
-
b35f73
-    /* now deal with one "./" or any amount of "../" in the newurl
b35f73
-       and act accordingly */
b35f73
-
b35f73
-    if((useurl[0] == '.') && (useurl[1] == '/'))
b35f73
-      useurl += 2; /* just skip the "./" */
b35f73
-
b35f73
-    while((useurl[0] == '.') &&
b35f73
-          (useurl[1] == '.') &&
b35f73
-          (useurl[2] == '/')) {
b35f73
-      level++;
b35f73
-      useurl += 3; /* pass the "../" */
b35f73
-    }
b35f73
-
b35f73
-    if(protsep) {
b35f73
-      while(level--) {
b35f73
-        /* cut off one more level from the right of the original URL */
b35f73
-        pathsep = strrchr(protsep, '/');
b35f73
-        if(pathsep)
b35f73
-          *pathsep = 0;
b35f73
-        else {
b35f73
-          *protsep = 0;
b35f73
-          break;
b35f73
-        }
b35f73
-      }
b35f73
-    }
b35f73
-  }
b35f73
-  else {
b35f73
-    /* We got a new absolute path for this server */
b35f73
-
b35f73
-    if((relurl[0] == '/') && (relurl[1] == '/')) {
b35f73
-      /* the new URL starts with //, just keep the protocol part from the
b35f73
-         original one */
b35f73
-      *protsep = 0;
b35f73
-      useurl = &relurl[2]; /* we keep the slashes from the original, so we
b35f73
-                              skip the new ones */
b35f73
-      host_changed = TRUE;
b35f73
-    }
b35f73
-    else {
b35f73
-      /* cut off the original URL from the first slash, or deal with URLs
b35f73
-         without slash */
b35f73
-      pathsep = strchr(protsep, '/');
b35f73
-      if(pathsep) {
b35f73
-        /* When people use badly formatted URLs, such as
b35f73
-           "http://www.url.com?dir=/home/daniel" we must not use the first
b35f73
-           slash, if there's a ?-letter before it! */
b35f73
-        char *sep = strchr(protsep, '?');
b35f73
-        if(sep && (sep < pathsep))
b35f73
-          pathsep = sep;
b35f73
-        *pathsep = 0;
b35f73
-      }
b35f73
-      else {
b35f73
-        /* There was no slash. Now, since we might be operating on a badly
b35f73
-           formatted URL, such as "http://www.url.com?id=2380" which doesn't
b35f73
-           use a slash separator as it is supposed to, we need to check for a
b35f73
-           ?-letter as well! */
b35f73
-        pathsep = strchr(protsep, '?');
b35f73
-        if(pathsep)
b35f73
-          *pathsep = 0;
b35f73
-      }
b35f73
-    }
b35f73
-  }
b35f73
-
b35f73
-  /* If the new part contains a space, this is a mighty stupid redirect
b35f73
-     but we still make an effort to do "right". To the left of a '?'
b35f73
-     letter we replace each space with %20 while it is replaced with '+'
b35f73
-     on the right side of the '?' letter.
b35f73
-  */
b35f73
-  newlen = strlen_url(useurl, !host_changed);
b35f73
-
b35f73
-  urllen = strlen(url_clone);
b35f73
-
b35f73
-  newest = malloc(urllen + 1 + /* possible slash */
b35f73
-                  newlen + 1 /* zero byte */);
b35f73
-
b35f73
-  if(!newest) {
b35f73
-    free(url_clone); /* don't leak this */
b35f73
-    return NULL;
b35f73
-  }
b35f73
-
b35f73
-  /* copy over the root url part */
b35f73
-  memcpy(newest, url_clone, urllen);
b35f73
-
b35f73
-  /* check if we need to append a slash */
b35f73
-  if(('/' == useurl[0]) || (protsep && !*protsep) || ('?' == useurl[0]))
b35f73
-    ;
b35f73
-  else
b35f73
-    newest[urllen++]='/';
b35f73
-
b35f73
-  /* then append the new piece on the right side */
b35f73
-  strcpy_url(&newest[urllen], useurl, !host_changed);
b35f73
-
b35f73
-  free(url_clone);
b35f73
-
b35f73
-  return newest;
b35f73
-}
b35f73
-#endif /* CURL_DISABLE_HTTP */
b35f73
-
b35f73
 /*
b35f73
  * Curl_follow() handles the URL redirect magic. Pass in the 'newurl' string
b35f73
  * as given by the remote server and set up the new URL to request.
b35f73
@@ -1809,12 +1505,12 @@ CURLcode Curl_follow(struct Curl_easy *data,
b35f73
     }
b35f73
   }
b35f73
 
b35f73
-  if(!is_absolute_url(newurl)) {
b35f73
+  if(!Curl_is_absolute_url(newurl, NULL, 8)) {
b35f73
     /***
b35f73
      *DANG* this is an RFC 2068 violation. The URL is supposed
b35f73
      to be absolute and this doesn't seem to be that!
b35f73
      */
b35f73
-    char *absolute = concat_url(data->change.url, newurl);
b35f73
+    char *absolute = Curl_concat_url(data->change.url, newurl);
b35f73
     if(!absolute)
b35f73
       return CURLE_OUT_OF_MEMORY;
b35f73
     newurl = absolute;
b35f73
@@ -1823,7 +1519,7 @@ CURLcode Curl_follow(struct Curl_easy *data,
b35f73
     /* The new URL MAY contain space or high byte values, that means a mighty
b35f73
        stupid redirect URL but we still make an effort to do "right". */
b35f73
     char *newest;
b35f73
-    size_t newlen = strlen_url(newurl, FALSE);
b35f73
+    size_t newlen = Curl_strlen_url(newurl, FALSE);
b35f73
 
b35f73
     /* This is an absolute URL, don't allow the custom port number */
b35f73
     disallowport = TRUE;
b35f73
@@ -1832,7 +1528,7 @@ CURLcode Curl_follow(struct Curl_easy *data,
b35f73
     if(!newest)
b35f73
       return CURLE_OUT_OF_MEMORY;
b35f73
 
b35f73
-    strcpy_url(newest, newurl, FALSE); /* create a space-free URL */
b35f73
+    Curl_strcpy_url(newest, newurl, FALSE); /* create a space-free URL */
b35f73
     newurl = newest; /* use this instead now */
b35f73
 
b35f73
   }
b35f73
diff --git a/lib/url.c b/lib/url.c
b35f73
index dcc1ecc..4f75f11 100644
b35f73
--- a/lib/url.c
b35f73
+++ b/lib/url.c
b35f73
@@ -1939,30 +1939,37 @@ static struct connectdata *allocate_conn(struct Curl_easy *data)
b35f73
   return NULL;
b35f73
 }
b35f73
 
b35f73
-static CURLcode findprotocol(struct Curl_easy *data,
b35f73
-                             struct connectdata *conn,
b35f73
-                             const char *protostr)
b35f73
+/* returns the handdler if the given scheme is built-in */
b35f73
+const struct Curl_handler *Curl_builtin_scheme(const char *scheme)
b35f73
 {
b35f73
   const struct Curl_handler * const *pp;
b35f73
   const struct Curl_handler *p;
b35f73
-
b35f73
-  /* Scan protocol handler table and match against 'protostr' to set a few
b35f73
-     variables based on the URL. Now that the handler may be changed later
b35f73
-     when the protocol specific setup function is called. */
b35f73
-  for(pp = protocols; (p = *pp) != NULL; pp++) {
b35f73
-    if(strcasecompare(p->scheme, protostr)) {
b35f73
+  /* Scan protocol handler table and match against 'scheme'. The handler may
b35f73
+     be changed later when the protocol specific setup function is called. */
b35f73
+  for(pp = protocols; (p = *pp) != NULL; pp++)
b35f73
+    if(strcasecompare(p->scheme, scheme))
b35f73
       /* Protocol found in table. Check if allowed */
b35f73
-      if(!(data->set.allowed_protocols & p->protocol))
b35f73
-        /* nope, get out */
b35f73
-        break;
b35f73
+      return p;
b35f73
+  return NULL; /* not found */
b35f73
+}
b35f73
 
b35f73
-      /* it is allowed for "normal" request, now do an extra check if this is
b35f73
-         the result of a redirect */
b35f73
-      if(data->state.this_is_a_follow &&
b35f73
-         !(data->set.redir_protocols & p->protocol))
b35f73
-        /* nope, get out */
b35f73
-        break;
b35f73
 
b35f73
+static CURLcode findprotocol(struct Curl_easy *data,
b35f73
+                             struct connectdata *conn,
b35f73
+                             const char *protostr)
b35f73
+{
b35f73
+  const struct Curl_handler *p = Curl_builtin_scheme(protostr);
b35f73
+
b35f73
+  if(p && /* Protocol found in table. Check if allowed */
b35f73
+     (data->set.allowed_protocols & p->protocol)) {
b35f73
+
b35f73
+    /* it is allowed for "normal" request, now do an extra check if this is
b35f73
+       the result of a redirect */
b35f73
+    if(data->state.this_is_a_follow &&
b35f73
+       !(data->set.redir_protocols & p->protocol))
b35f73
+      /* nope, get out */
b35f73
+      ;
b35f73
+    else {
b35f73
       /* Perform setup complement if some. */
b35f73
       conn->handler = conn->given = p;
b35f73
 
b35f73
@@ -1971,7 +1978,6 @@ static CURLcode findprotocol(struct Curl_easy *data,
b35f73
     }
b35f73
   }
b35f73
 
b35f73
-
b35f73
   /* The protocol was not found in the table, but we don't have to assign it
b35f73
      to anything since it is already assigned to a dummy-struct in the
b35f73
      create_conn() function when the connectdata struct is allocated. */
b35f73
diff --git a/lib/url.h b/lib/url.h
b35f73
index ef3ebf0..0034f82 100644
b35f73
--- a/lib/url.h
b35f73
+++ b/lib/url.h
b35f73
@@ -69,6 +69,8 @@ void Curl_getoff_all_pipelines(struct Curl_easy *data,
b35f73
 
b35f73
 void Curl_close_connections(struct Curl_easy *data);
b35f73
 
b35f73
+const struct Curl_handler *Curl_builtin_scheme(const char *scheme);
b35f73
+
b35f73
 #define CURL_DEFAULT_PROXY_PORT 1080 /* default proxy port unless specified */
b35f73
 #define CURL_DEFAULT_HTTPS_PROXY_PORT 443 /* default https proxy port unless
b35f73
                                              specified */
b35f73
diff --git a/lib/escape.h b/lib/urlapi-int.h
b35f73
similarity index 66%
b35f73
copy from lib/escape.h
b35f73
copy to lib/urlapi-int.h
b35f73
index 638666f..7ac09fd 100644
b35f73
--- a/lib/escape.h
b35f73
+++ b/lib/urlapi-int.h
b35f73
@@ -1,5 +1,5 @@
b35f73
-#ifndef HEADER_CURL_ESCAPE_H
b35f73
-#define HEADER_CURL_ESCAPE_H
b35f73
+#ifndef HEADER_CURL_URLAPI_INT_H
b35f73
+#define HEADER_CURL_URLAPI_INT_H
b35f73
 /***************************************************************************
b35f73
  *                                  _   _ ____  _
b35f73
  *  Project                     ___| | | |  _ \| |
b35f73
@@ -7,7 +7,7 @@
b35f73
  *                            | (__| |_| |  _ <| |___
b35f73
  *                             \___|\___/|_| \_\_____|
b35f73
  *
b35f73
- * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
  *
b35f73
  * This software is licensed as described in the file COPYING, which
b35f73
  * you should have received as part of this distribution. The terms
b35f73
@@ -21,13 +21,9 @@
b35f73
  * KIND, either express or implied.
b35f73
  *
b35f73
  ***************************************************************************/
b35f73
-/* Escape and unescape URL encoding in strings. The functions return a new
b35f73
- * allocated string or NULL if an error occurred.  */
b35f73
-
b35f73
-CURLcode Curl_urldecode(struct Curl_easy *data,
b35f73
-                        const char *string, size_t length,
b35f73
-                        char **ostring, size_t *olen,
b35f73
-                        bool reject_crlf);
b35f73
-
b35f73
-#endif /* HEADER_CURL_ESCAPE_H */
b35f73
-
b35f73
+#include "curl_setup.h"
b35f73
+bool Curl_is_absolute_url(const char *url, char *scheme, size_t buflen);
b35f73
+char *Curl_concat_url(const char *base, const char *relurl);
b35f73
+size_t Curl_strlen_url(const char *url, bool relative);
b35f73
+void Curl_strcpy_url(char *output, const char *url, bool relative);
b35f73
+#endif
b35f73
diff --git a/lib/urlapi.c b/lib/urlapi.c
b35f73
new file mode 100644
b35f73
index 0000000..8287861
b35f73
--- /dev/null
b35f73
+++ b/lib/urlapi.c
b35f73
@@ -0,0 +1,1315 @@
b35f73
+/***************************************************************************
b35f73
+ *                                  _   _ ____  _
b35f73
+ *  Project                     ___| | | |  _ \| |
b35f73
+ *                             / __| | | | |_) | |
b35f73
+ *                            | (__| |_| |  _ <| |___
b35f73
+ *                             \___|\___/|_| \_\_____|
b35f73
+ *
b35f73
+ * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ *
b35f73
+ * This software is licensed as described in the file COPYING, which
b35f73
+ * you should have received as part of this distribution. The terms
b35f73
+ * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+ *
b35f73
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+ * copies of the Software, and permit persons to whom the Software is
b35f73
+ * furnished to do so, under the terms of the COPYING file.
b35f73
+ *
b35f73
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+ * KIND, either express or implied.
b35f73
+ *
b35f73
+ ***************************************************************************/
b35f73
+
b35f73
+#include "curl_setup.h"
b35f73
+
b35f73
+#include "urldata.h"
b35f73
+#include "urlapi-int.h"
b35f73
+#include "strcase.h"
b35f73
+#include "dotdot.h"
b35f73
+#include "url.h"
b35f73
+#include "escape.h"
b35f73
+#include "curl_ctype.h"
b35f73
+
b35f73
+/* The last 3 #include files should be in this order */
b35f73
+#include "curl_printf.h"
b35f73
+#include "curl_memory.h"
b35f73
+#include "memdebug.h"
b35f73
+
b35f73
+/* Internal representation of CURLU. Point to URL-encoded strings. */
b35f73
+struct Curl_URL {
b35f73
+  char *scheme;
b35f73
+  char *user;
b35f73
+  char *password;
b35f73
+  char *options; /* IMAP only? */
b35f73
+  char *host;
b35f73
+  char *port;
b35f73
+  char *path;
b35f73
+  char *query;
b35f73
+  char *fragment;
b35f73
+
b35f73
+  char *scratch; /* temporary scratch area */
b35f73
+  long portnum; /* the numerical version */
b35f73
+};
b35f73
+
b35f73
+#define DEFAULT_SCHEME "https"
b35f73
+
b35f73
+/* scheme is not URL encoded, the longest libcurl supported ones are 6
b35f73
+   letters */
b35f73
+#define MAX_SCHEME_LEN 8
b35f73
+
b35f73
+static void free_urlhandle(struct Curl_URL *u)
b35f73
+{
b35f73
+  free(u->scheme);
b35f73
+  free(u->user);
b35f73
+  free(u->password);
b35f73
+  free(u->options);
b35f73
+  free(u->host);
b35f73
+  free(u->port);
b35f73
+  free(u->path);
b35f73
+  free(u->query);
b35f73
+  free(u->fragment);
b35f73
+  free(u->scratch);
b35f73
+}
b35f73
+
b35f73
+/* move the full contents of one handle onto another and
b35f73
+   free the original */
b35f73
+static void mv_urlhandle(struct Curl_URL *from,
b35f73
+                         struct Curl_URL *to)
b35f73
+{
b35f73
+  free_urlhandle(to);
b35f73
+  *to = *from;
b35f73
+  free(from);
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ * Find the separator at the end of the host name, or the '?' in cases like
b35f73
+ * http://www.url.com?id=2380
b35f73
+ */
b35f73
+static const char *find_host_sep(const char *url)
b35f73
+{
b35f73
+  const char *sep;
b35f73
+  const char *query;
b35f73
+
b35f73
+  /* Find the start of the hostname */
b35f73
+  sep = strstr(url, "//");
b35f73
+  if(!sep)
b35f73
+    sep = url;
b35f73
+  else
b35f73
+    sep += 2;
b35f73
+
b35f73
+  query = strchr(sep, '?');
b35f73
+  sep = strchr(sep, '/');
b35f73
+
b35f73
+  if(!sep)
b35f73
+    sep = url + strlen(url);
b35f73
+
b35f73
+  if(!query)
b35f73
+    query = url + strlen(url);
b35f73
+
b35f73
+  return sep < query ? sep : query;
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ * Decide in an encoding-independent manner whether a character in an
b35f73
+ * URL must be escaped. The same criterion must be used in strlen_url()
b35f73
+ * and strcpy_url().
b35f73
+ */
b35f73
+static bool urlchar_needs_escaping(int c)
b35f73
+{
b35f73
+    return !(ISCNTRL(c) || ISSPACE(c) || ISGRAPH(c));
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ * strlen_url() returns the length of the given URL if the spaces within the
b35f73
+ * URL were properly URL encoded.
b35f73
+ * URL encoding should be skipped for host names, otherwise IDN resolution
b35f73
+ * will fail.
b35f73
+ */
b35f73
+size_t Curl_strlen_url(const char *url, bool relative)
b35f73
+{
b35f73
+  const unsigned char *ptr;
b35f73
+  size_t newlen = 0;
b35f73
+  bool left = TRUE; /* left side of the ? */
b35f73
+  const unsigned char *host_sep = (const unsigned char *) url;
b35f73
+
b35f73
+  if(!relative)
b35f73
+    host_sep = (const unsigned char *) find_host_sep(url);
b35f73
+
b35f73
+  for(ptr = (unsigned char *)url; *ptr; ptr++) {
b35f73
+
b35f73
+    if(ptr < host_sep) {
b35f73
+      ++newlen;
b35f73
+      continue;
b35f73
+    }
b35f73
+
b35f73
+    switch(*ptr) {
b35f73
+    case '?':
b35f73
+      left = FALSE;
b35f73
+      /* FALLTHROUGH */
b35f73
+    default:
b35f73
+      if(urlchar_needs_escaping(*ptr))
b35f73
+        newlen += 2;
b35f73
+      newlen++;
b35f73
+      break;
b35f73
+    case ' ':
b35f73
+      if(left)
b35f73
+        newlen += 3;
b35f73
+      else
b35f73
+        newlen++;
b35f73
+      break;
b35f73
+    }
b35f73
+  }
b35f73
+  return newlen;
b35f73
+}
b35f73
+
b35f73
+/* strcpy_url() copies a url to a output buffer and URL-encodes the spaces in
b35f73
+ * the source URL accordingly.
b35f73
+ * URL encoding should be skipped for host names, otherwise IDN resolution
b35f73
+ * will fail.
b35f73
+ */
b35f73
+void Curl_strcpy_url(char *output, const char *url, bool relative)
b35f73
+{
b35f73
+  /* we must add this with whitespace-replacing */
b35f73
+  bool left = TRUE;
b35f73
+  const unsigned char *iptr;
b35f73
+  char *optr = output;
b35f73
+  const unsigned char *host_sep = (const unsigned char *) url;
b35f73
+
b35f73
+  if(!relative)
b35f73
+    host_sep = (const unsigned char *) find_host_sep(url);
b35f73
+
b35f73
+  for(iptr = (unsigned char *)url;    /* read from here */
b35f73
+      *iptr;         /* until zero byte */
b35f73
+      iptr++) {
b35f73
+
b35f73
+    if(iptr < host_sep) {
b35f73
+      *optr++ = *iptr;
b35f73
+      continue;
b35f73
+    }
b35f73
+
b35f73
+    switch(*iptr) {
b35f73
+    case '?':
b35f73
+      left = FALSE;
b35f73
+      /* FALLTHROUGH */
b35f73
+    default:
b35f73
+      if(urlchar_needs_escaping(*iptr)) {
b35f73
+        snprintf(optr, 4, "%%%02x", *iptr);
b35f73
+        optr += 3;
b35f73
+      }
b35f73
+      else
b35f73
+        *optr++=*iptr;
b35f73
+      break;
b35f73
+    case ' ':
b35f73
+      if(left) {
b35f73
+        *optr++='%'; /* add a '%' */
b35f73
+        *optr++='2'; /* add a '2' */
b35f73
+        *optr++='0'; /* add a '0' */
b35f73
+      }
b35f73
+      else
b35f73
+        *optr++='+'; /* add a '+' here */
b35f73
+      break;
b35f73
+    }
b35f73
+  }
b35f73
+  *optr = 0; /* zero terminate output buffer */
b35f73
+
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ * Returns true if the given URL is absolute (as opposed to relative) within
b35f73
+ * the buffer size. Returns the scheme in the buffer if TRUE and 'buf' is
b35f73
+ * non-NULL.
b35f73
+ */
b35f73
+bool Curl_is_absolute_url(const char *url, char *buf, size_t buflen)
b35f73
+{
b35f73
+  size_t i;
b35f73
+  for(i = 0; i < buflen && url[i]; ++i) {
b35f73
+    char s = url[i];
b35f73
+    if(s == ':') {
b35f73
+      if(buf)
b35f73
+        buf[i] = 0;
b35f73
+      return TRUE;
b35f73
+    }
b35f73
+    /* RFC 3986 3.1 explains:
b35f73
+      scheme      = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
b35f73
+    */
b35f73
+    else if(ISALNUM(s) || (s == '+') || (s == '-') || (s == '.') ) {
b35f73
+      if(buf)
b35f73
+        buf[i] = (char)TOLOWER(s);
b35f73
+    }
b35f73
+    else
b35f73
+      break;
b35f73
+  }
b35f73
+  return FALSE;
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ * Concatenate a relative URL to a base URL making it absolute.
b35f73
+ * URL-encodes any spaces.
b35f73
+ * The returned pointer must be freed by the caller unless NULL
b35f73
+ * (returns NULL on out of memory).
b35f73
+ */
b35f73
+char *Curl_concat_url(const char *base, const char *relurl)
b35f73
+{
b35f73
+  /***
b35f73
+   TRY to append this new path to the old URL
b35f73
+   to the right of the host part. Oh crap, this is doomed to cause
b35f73
+   problems in the future...
b35f73
+  */
b35f73
+  char *newest;
b35f73
+  char *protsep;
b35f73
+  char *pathsep;
b35f73
+  size_t newlen;
b35f73
+  bool host_changed = FALSE;
b35f73
+
b35f73
+  const char *useurl = relurl;
b35f73
+  size_t urllen;
b35f73
+
b35f73
+  /* we must make our own copy of the URL to play with, as it may
b35f73
+     point to read-only data */
b35f73
+  char *url_clone = strdup(base);
b35f73
+
b35f73
+  if(!url_clone)
b35f73
+    return NULL; /* skip out of this NOW */
b35f73
+
b35f73
+  /* protsep points to the start of the host name */
b35f73
+  protsep = strstr(url_clone, "//");
b35f73
+  if(!protsep)
b35f73
+    protsep = url_clone;
b35f73
+  else
b35f73
+    protsep += 2; /* pass the slashes */
b35f73
+
b35f73
+  if('/' != relurl[0]) {
b35f73
+    int level = 0;
b35f73
+
b35f73
+    /* First we need to find out if there's a ?-letter in the URL,
b35f73
+       and cut it and the right-side of that off */
b35f73
+    pathsep = strchr(protsep, '?');
b35f73
+    if(pathsep)
b35f73
+      *pathsep = 0;
b35f73
+
b35f73
+    /* we have a relative path to append to the last slash if there's one
b35f73
+       available, or if the new URL is just a query string (starts with a
b35f73
+       '?')  we append the new one at the end of the entire currently worked
b35f73
+       out URL */
b35f73
+    if(useurl[0] != '?') {
b35f73
+      pathsep = strrchr(protsep, '/');
b35f73
+      if(pathsep)
b35f73
+        *pathsep = 0;
b35f73
+    }
b35f73
+
b35f73
+    /* Check if there's any slash after the host name, and if so, remember
b35f73
+       that position instead */
b35f73
+    pathsep = strchr(protsep, '/');
b35f73
+    if(pathsep)
b35f73
+      protsep = pathsep + 1;
b35f73
+    else
b35f73
+      protsep = NULL;
b35f73
+
b35f73
+    /* now deal with one "./" or any amount of "../" in the newurl
b35f73
+       and act accordingly */
b35f73
+
b35f73
+    if((useurl[0] == '.') && (useurl[1] == '/'))
b35f73
+      useurl += 2; /* just skip the "./" */
b35f73
+
b35f73
+    while((useurl[0] == '.') &&
b35f73
+          (useurl[1] == '.') &&
b35f73
+          (useurl[2] == '/')) {
b35f73
+      level++;
b35f73
+      useurl += 3; /* pass the "../" */
b35f73
+    }
b35f73
+
b35f73
+    if(protsep) {
b35f73
+      while(level--) {
b35f73
+        /* cut off one more level from the right of the original URL */
b35f73
+        pathsep = strrchr(protsep, '/');
b35f73
+        if(pathsep)
b35f73
+          *pathsep = 0;
b35f73
+        else {
b35f73
+          *protsep = 0;
b35f73
+          break;
b35f73
+        }
b35f73
+      }
b35f73
+    }
b35f73
+  }
b35f73
+  else {
b35f73
+    /* We got a new absolute path for this server */
b35f73
+
b35f73
+    if((relurl[0] == '/') && (relurl[1] == '/')) {
b35f73
+      /* the new URL starts with //, just keep the protocol part from the
b35f73
+         original one */
b35f73
+      *protsep = 0;
b35f73
+      useurl = &relurl[2]; /* we keep the slashes from the original, so we
b35f73
+                              skip the new ones */
b35f73
+      host_changed = TRUE;
b35f73
+    }
b35f73
+    else {
b35f73
+      /* cut off the original URL from the first slash, or deal with URLs
b35f73
+         without slash */
b35f73
+      pathsep = strchr(protsep, '/');
b35f73
+      if(pathsep) {
b35f73
+        /* When people use badly formatted URLs, such as
b35f73
+           "http://www.url.com?dir=/home/daniel" we must not use the first
b35f73
+           slash, if there's a ?-letter before it! */
b35f73
+        char *sep = strchr(protsep, '?');
b35f73
+        if(sep && (sep < pathsep))
b35f73
+          pathsep = sep;
b35f73
+        *pathsep = 0;
b35f73
+      }
b35f73
+      else {
b35f73
+        /* There was no slash. Now, since we might be operating on a badly
b35f73
+           formatted URL, such as "http://www.url.com?id=2380" which doesn't
b35f73
+           use a slash separator as it is supposed to, we need to check for a
b35f73
+           ?-letter as well! */
b35f73
+        pathsep = strchr(protsep, '?');
b35f73
+        if(pathsep)
b35f73
+          *pathsep = 0;
b35f73
+      }
b35f73
+    }
b35f73
+  }
b35f73
+
b35f73
+  /* If the new part contains a space, this is a mighty stupid redirect
b35f73
+     but we still make an effort to do "right". To the left of a '?'
b35f73
+     letter we replace each space with %20 while it is replaced with '+'
b35f73
+     on the right side of the '?' letter.
b35f73
+  */
b35f73
+  newlen = Curl_strlen_url(useurl, !host_changed);
b35f73
+
b35f73
+  urllen = strlen(url_clone);
b35f73
+
b35f73
+  newest = malloc(urllen + 1 + /* possible slash */
b35f73
+                  newlen + 1 /* zero byte */);
b35f73
+
b35f73
+  if(!newest) {
b35f73
+    free(url_clone); /* don't leak this */
b35f73
+    return NULL;
b35f73
+  }
b35f73
+
b35f73
+  /* copy over the root url part */
b35f73
+  memcpy(newest, url_clone, urllen);
b35f73
+
b35f73
+  /* check if we need to append a slash */
b35f73
+  if(('/' == useurl[0]) || (protsep && !*protsep) || ('?' == useurl[0]))
b35f73
+    ;
b35f73
+  else
b35f73
+    newest[urllen++]='/';
b35f73
+
b35f73
+  /* then append the new piece on the right side */
b35f73
+  Curl_strcpy_url(&newest[urllen], useurl, !host_changed);
b35f73
+
b35f73
+  free(url_clone);
b35f73
+
b35f73
+  return newest;
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ * parse_hostname_login()
b35f73
+ *
b35f73
+ * Parse the login details (user name, password and options) from the URL and
b35f73
+ * strip them out of the host name
b35f73
+ *
b35f73
+ */
b35f73
+static CURLUcode parse_hostname_login(struct Curl_URL *u,
b35f73
+                                      const struct Curl_handler *h,
b35f73
+                                      char **hostname,
b35f73
+                                      unsigned int flags)
b35f73
+{
b35f73
+  CURLUcode result = CURLUE_OK;
b35f73
+  CURLcode ccode;
b35f73
+  char *userp = NULL;
b35f73
+  char *passwdp = NULL;
b35f73
+  char *optionsp = NULL;
b35f73
+
b35f73
+  /* At this point, we're hoping all the other special cases have
b35f73
+   * been taken care of, so conn->host.name is at most
b35f73
+   *    [user[:password][;options]]@]hostname
b35f73
+   *
b35f73
+   * We need somewhere to put the embedded details, so do that first.
b35f73
+   */
b35f73
+
b35f73
+  char *ptr = strchr(*hostname, '@');
b35f73
+  char *login = *hostname;
b35f73
+
b35f73
+  if(!ptr)
b35f73
+    goto out;
b35f73
+
b35f73
+  /* We will now try to extract the
b35f73
+   * possible login information in a string like:
b35f73
+   * ftp://user:password@ftp.my.site:8021/README */
b35f73
+  *hostname = ++ptr;
b35f73
+
b35f73
+  /* We could use the login information in the URL so extract it. Only parse
b35f73
+     options if the handler says we should. */
b35f73
+  ccode = Curl_parse_login_details(login, ptr - login - 1,
b35f73
+                                   &userp, &passwdp,
b35f73
+                                   h->flags & PROTOPT_URLOPTIONS ?
b35f73
+                                   &optionsp:NULL);
b35f73
+  if(ccode) {
b35f73
+    result = CURLUE_MALFORMED_INPUT;
b35f73
+    goto out;
b35f73
+  }
b35f73
+
b35f73
+  if(userp) {
b35f73
+    if(flags & CURLU_DISALLOW_USER) {
b35f73
+      /* Option DISALLOW_USER is set and url contains username. */
b35f73
+      result = CURLUE_USER_NOT_ALLOWED;
b35f73
+      goto out;
b35f73
+    }
b35f73
+
b35f73
+    u->user = userp;
b35f73
+  }
b35f73
+
b35f73
+  if(passwdp)
b35f73
+    u->password = passwdp;
b35f73
+
b35f73
+  if(optionsp)
b35f73
+    u->options = optionsp;
b35f73
+
b35f73
+  return CURLUE_OK;
b35f73
+  out:
b35f73
+
b35f73
+  free(userp);
b35f73
+  free(passwdp);
b35f73
+  free(optionsp);
b35f73
+
b35f73
+  return result;
b35f73
+}
b35f73
+
b35f73
+static CURLUcode parse_port(struct Curl_URL *u, char *hostname)
b35f73
+{
b35f73
+  char *portptr;
b35f73
+  char endbracket;
b35f73
+  int len;
b35f73
+
b35f73
+  if((1 == sscanf(hostname, "[%*45[0123456789abcdefABCDEF:.]%c%n",
b35f73
+                  &endbracket, &len)) &&
b35f73
+     (']' == endbracket)) {
b35f73
+    /* this is a RFC2732-style specified IP-address */
b35f73
+    portptr = &hostname[len];
b35f73
+    if (*portptr != ':')
b35f73
+      return CURLUE_MALFORMED_INPUT;
b35f73
+  }
b35f73
+  else
b35f73
+    portptr = strchr(hostname, ':');
b35f73
+
b35f73
+  if(portptr) {
b35f73
+    char *rest;
b35f73
+    long port;
b35f73
+    char portbuf[7];
b35f73
+
b35f73
+    if(!ISDIGIT(portptr[1]))
b35f73
+      return CURLUE_BAD_PORT_NUMBER;
b35f73
+
b35f73
+    port = strtol(portptr + 1, &rest, 10);  /* Port number must be decimal */
b35f73
+
b35f73
+    if((port <= 0) || (port > 0xffff))
b35f73
+      /* Single unix standard says port numbers are 16 bits long, but we don't
b35f73
+         treat port zero as OK. */
b35f73
+      return CURLUE_BAD_PORT_NUMBER;
b35f73
+
b35f73
+    if(rest[0])
b35f73
+      return CURLUE_BAD_PORT_NUMBER;
b35f73
+
b35f73
+    if(rest != &portptr[1]) {
b35f73
+      *portptr++ = '\0'; /* cut off the name there */
b35f73
+      *rest = 0;
b35f73
+      /* generate a new to get rid of leading zeroes etc */
b35f73
+      snprintf(portbuf, sizeof(portbuf), "%ld", port);
b35f73
+      u->portnum = port;
b35f73
+      u->port = strdup(portbuf);
b35f73
+      if(!u->port)
b35f73
+        return CURLUE_OUT_OF_MEMORY;
b35f73
+    }
b35f73
+    else {
b35f73
+      /* Browser behavior adaptation. If there's a colon with no digits after,
b35f73
+         just cut off the name there which makes us ignore the colon and just
b35f73
+         use the default port. Firefox and Chrome both do that. */
b35f73
+      *portptr = '\0';
b35f73
+    }
b35f73
+  }
b35f73
+
b35f73
+  return CURLUE_OK;
b35f73
+}
b35f73
+
b35f73
+/* scan for byte values < 31 or 127 */
b35f73
+static CURLUcode junkscan(char *part)
b35f73
+{
b35f73
+  char badbytes[]={
b35f73
+    /* */ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
b35f73
+    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
b35f73
+    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
b35f73
+    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
b35f73
+    0x7f,
b35f73
+    0x00 /* zero terminate */
b35f73
+  };
b35f73
+  if(part) {
b35f73
+    size_t n = strlen(part);
b35f73
+    size_t nfine = strcspn(part, badbytes);
b35f73
+    if(nfine != n)
b35f73
+      /* since we don't know which part is scanned, return a generic error
b35f73
+         code */
b35f73
+      return CURLUE_MALFORMED_INPUT;
b35f73
+  }
b35f73
+  return CURLUE_OK;
b35f73
+}
b35f73
+
b35f73
+static CURLUcode hostname_check(char *hostname, unsigned int flags)
b35f73
+{
b35f73
+  const char *l; /* accepted characters */
b35f73
+  size_t len;
b35f73
+  size_t hlen = strlen(hostname);
b35f73
+  (void)flags;
b35f73
+
b35f73
+  if(hostname[0] == '[') {
b35f73
+    hostname++;
b35f73
+    l = "0123456789abcdefABCDEF::.";
b35f73
+    hlen -= 2;
b35f73
+  }
b35f73
+  else /* % for URL escaped letters */
b35f73
+    l = "0123456789abcdefghijklimnopqrstuvwxyz-_.ABCDEFGHIJKLIMNOPQRSTUVWXYZ%";
b35f73
+
b35f73
+  len = strspn(hostname, l);
b35f73
+  if(hlen != len)
b35f73
+    /* hostname with bad content */
b35f73
+    return CURLUE_MALFORMED_INPUT;
b35f73
+
b35f73
+  return CURLUE_OK;
b35f73
+}
b35f73
+
b35f73
+#define HOSTNAME_END(x) (((x) == '/') || ((x) == '?') || ((x) == '#'))
b35f73
+
b35f73
+static CURLUcode seturl(const char *url, CURLU *u, unsigned int flags)
b35f73
+{
b35f73
+  char *path;
b35f73
+  bool path_alloced = FALSE;
b35f73
+  char *hostname;
b35f73
+  char *query = NULL;
b35f73
+  char *fragment = NULL;
b35f73
+  CURLUcode result;
b35f73
+  bool url_has_scheme = FALSE;
b35f73
+  char schemebuf[MAX_SCHEME_LEN];
b35f73
+  char *schemep;
b35f73
+  size_t schemelen = 0;
b35f73
+  size_t urllen;
b35f73
+  const struct Curl_handler *h = NULL;
b35f73
+
b35f73
+  if(!url)
b35f73
+    return CURLUE_MALFORMED_INPUT;
b35f73
+
b35f73
+  /*************************************************************
b35f73
+   * Parse the URL.
b35f73
+   ************************************************************/
b35f73
+  /* allocate scratch area */
b35f73
+  urllen = strlen(url);
b35f73
+  path = u->scratch = malloc(urllen * 2 + 2);
b35f73
+  if(!path)
b35f73
+    return CURLUE_OUT_OF_MEMORY;
b35f73
+
b35f73
+  hostname = &path[urllen + 1];
b35f73
+  hostname[0] = 0;
b35f73
+
b35f73
+  /* MSDOS/Windows style drive prefix, eg c: in c:foo */
b35f73
+#define STARTS_WITH_DRIVE_PREFIX(str) \
b35f73
+  ((('a' <= str[0] && str[0] <= 'z') || \
b35f73
+    ('A' <= str[0] && str[0] <= 'Z')) && \
b35f73
+   (str[1] == ':'))
b35f73
+
b35f73
+  /* MSDOS/Windows style drive prefix, optionally with
b35f73
+   * a '|' instead of ':', followed by a slash or NUL */
b35f73
+#define STARTS_WITH_URL_DRIVE_PREFIX(str) \
b35f73
+  ((('a' <= (str)[0] && (str)[0] <= 'z') || \
b35f73
+    ('A' <= (str)[0] && (str)[0] <= 'Z')) && \
b35f73
+   ((str)[1] == ':' || (str)[1] == '|') && \
b35f73
+   ((str)[2] == '/' || (str)[2] == '\\' || (str)[2] == 0))
b35f73
+
b35f73
+  if(Curl_is_absolute_url(url, schemebuf, sizeof(schemebuf))) {
b35f73
+    url_has_scheme = TRUE;
b35f73
+    schemelen = strlen(schemebuf);
b35f73
+  }
b35f73
+
b35f73
+  /* handle the file: scheme */
b35f73
+  if(url_has_scheme && strcasecompare(schemebuf, "file")) {
b35f73
+    /* path has been allocated large anough to hold this */
b35f73
+    strcpy(path, &url[5]);
b35f73
+
b35f73
+    hostname = NULL; /* no host for file: URLs */
b35f73
+    u->scheme = strdup("file");
b35f73
+    if(!u->scheme)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+
b35f73
+    /* Extra handling URLs with an authority component (i.e. that start with
b35f73
+     * "file://")
b35f73
+     *
b35f73
+     * We allow omitted hostname (e.g. file:/<path>) -- valid according to
b35f73
+     * RFC 8089, but not the (current) WHAT-WG URL spec.
b35f73
+     */
b35f73
+    if(path[0] == '/' && path[1] == '/') {
b35f73
+      /* swallow the two slashes */
b35f73
+      char *ptr = &path[2];
b35f73
+
b35f73
+      /*
b35f73
+       * According to RFC 8089, a file: URL can be reliably dereferenced if:
b35f73
+       *
b35f73
+       *  o it has no/blank hostname, or
b35f73
+       *
b35f73
+       *  o the hostname matches "localhost" (case-insensitively), or
b35f73
+       *
b35f73
+       *  o the hostname is a FQDN that resolves to this machine.
b35f73
+       *
b35f73
+       * For brevity, we only consider URLs with empty, "localhost", or
b35f73
+       * "127.0.0.1" hostnames as local.
b35f73
+       *
b35f73
+       * Additionally, there is an exception for URLs with a Windows drive
b35f73
+       * letter in the authority (which was accidentally omitted from RFC 8089
b35f73
+       * Appendix E, but believe me, it was meant to be there. --MK)
b35f73
+       */
b35f73
+      if(ptr[0] != '/' && !STARTS_WITH_URL_DRIVE_PREFIX(ptr)) {
b35f73
+        /* the URL includes a host name, it must match "localhost" or
b35f73
+           "127.0.0.1" to be valid */
b35f73
+        if(!checkprefix("localhost/", ptr) &&
b35f73
+           !checkprefix("127.0.0.1/", ptr)) {
b35f73
+          /* Invalid file://hostname/, expected localhost or 127.0.0.1 or
b35f73
+             none */
b35f73
+          return CURLUE_MALFORMED_INPUT;
b35f73
+        }
b35f73
+        ptr += 9; /* now points to the slash after the host */
b35f73
+      }
b35f73
+
b35f73
+      path = ptr;
b35f73
+    }
b35f73
+
b35f73
+#if !defined(MSDOS) && !defined(WIN32) && !defined(__CYGWIN__)
b35f73
+    /* Don't allow Windows drive letters when not in Windows.
b35f73
+     * This catches both "file:/c:" and "file:c:" */
b35f73
+    if(('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) ||
b35f73
+       STARTS_WITH_URL_DRIVE_PREFIX(path)) {
b35f73
+      /* File drive letters are only accepted in MSDOS/Windows */
b35f73
+      return CURLUE_MALFORMED_INPUT;
b35f73
+    }
b35f73
+#else
b35f73
+    /* If the path starts with a slash and a drive letter, ditch the slash */
b35f73
+    if('/' == path[0] && STARTS_WITH_URL_DRIVE_PREFIX(&path[1])) {
b35f73
+      /* This cannot be done with strcpy, as the memory chunks overlap! */
b35f73
+      memmove(path, &path[1], strlen(&path[1]) + 1);
b35f73
+    }
b35f73
+#endif
b35f73
+
b35f73
+  }
b35f73
+  else {
b35f73
+    /* clear path */
b35f73
+    const char *p;
b35f73
+    const char *hostp;
b35f73
+    size_t len;
b35f73
+    path[0] = 0;
b35f73
+
b35f73
+    if(url_has_scheme) {
b35f73
+      int i = 0;
b35f73
+      p = &url[schemelen + 1];
b35f73
+      while(p && (*p == '/') && (i < 4)) {
b35f73
+        p++;
b35f73
+        i++;
b35f73
+      }
b35f73
+      if((i < 1) || (i>3))
b35f73
+        /* less than one or more than three slashes */
b35f73
+        return CURLUE_MALFORMED_INPUT;
b35f73
+
b35f73
+      schemep = schemebuf;
b35f73
+      if(!Curl_builtin_scheme(schemep) &&
b35f73
+         !(flags & CURLU_NON_SUPPORT_SCHEME))
b35f73
+        return CURLUE_UNSUPPORTED_SCHEME;
b35f73
+
b35f73
+      if(junkscan(schemep))
b35f73
+        return CURLUE_MALFORMED_INPUT;
b35f73
+    }
b35f73
+    else {
b35f73
+      /* no scheme! */
b35f73
+
b35f73
+      if(!(flags & CURLU_DEFAULT_SCHEME))
b35f73
+        return CURLUE_MALFORMED_INPUT;
b35f73
+      schemep = (char *) DEFAULT_SCHEME;
b35f73
+
b35f73
+      /*
b35f73
+       * The URL was badly formatted, let's try without scheme specified.
b35f73
+       */
b35f73
+      p = url;
b35f73
+    }
b35f73
+    hostp = p; /* host name starts here */
b35f73
+
b35f73
+    while(*p && !HOSTNAME_END(*p)) /* find end of host name */
b35f73
+      p++;
b35f73
+
b35f73
+    len = p - hostp;
b35f73
+    if(!len)
b35f73
+      return CURLUE_MALFORMED_INPUT;
b35f73
+
b35f73
+    memcpy(hostname, hostp, len);
b35f73
+    hostname[len] = 0;
b35f73
+
b35f73
+    len = strlen(p);
b35f73
+    memcpy(path, p, len);
b35f73
+    path[len] = 0;
b35f73
+
b35f73
+    u->scheme = strdup(schemep);
b35f73
+    if(!u->scheme)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+  }
b35f73
+
b35f73
+  /* if this is a known scheme, get some details */
b35f73
+  h = Curl_builtin_scheme(u->scheme);
b35f73
+
b35f73
+  if(junkscan(path))
b35f73
+    return CURLUE_MALFORMED_INPUT;
b35f73
+
b35f73
+  query = strchr(path, '?');
b35f73
+  if(query)
b35f73
+    *query++ = 0;
b35f73
+
b35f73
+  fragment = strchr(query?query:path, '#');
b35f73
+  if(fragment)
b35f73
+    *fragment++ = 0;
b35f73
+
b35f73
+  if(!path[0])
b35f73
+    /* if there's no path set, unset */
b35f73
+    path = NULL;
b35f73
+  else if(!(flags & CURLU_PATH_AS_IS)) {
b35f73
+    /* sanitise paths and remove ../ and ./ sequences according to RFC3986 */
b35f73
+    char *newp = Curl_dedotdotify(path);
b35f73
+    if(!newp)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+
b35f73
+    if(strcmp(newp, path)) {
b35f73
+      /* if we got a new version */
b35f73
+      path = newp;
b35f73
+      path_alloced = TRUE;
b35f73
+    }
b35f73
+    else
b35f73
+      free(newp);
b35f73
+  }
b35f73
+  if(path) {
b35f73
+    u->path = path_alloced?path:strdup(path);
b35f73
+    if(!u->path)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+  }
b35f73
+
b35f73
+  if(hostname) {
b35f73
+    /*
b35f73
+     * Parse the login details and strip them out of the host name.
b35f73
+     */
b35f73
+    if(junkscan(hostname))
b35f73
+      return CURLUE_MALFORMED_INPUT;
b35f73
+
b35f73
+    result = parse_hostname_login(u, h, &hostname, flags);
b35f73
+    if(result)
b35f73
+      return result;
b35f73
+
b35f73
+    result = parse_port(u, hostname);
b35f73
+    if(result)
b35f73
+      return result;
b35f73
+
b35f73
+    result = hostname_check(hostname, flags);
b35f73
+    if(result)
b35f73
+      return result;
b35f73
+
b35f73
+    u->host = strdup(hostname);
b35f73
+    if(!u->host)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+  }
b35f73
+
b35f73
+  if(query && query[0]) {
b35f73
+    u->query = strdup(query);
b35f73
+    if(!u->query)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+  }
b35f73
+  if(fragment && fragment[0]) {
b35f73
+    u->fragment = strdup(fragment);
b35f73
+    if(!u->fragment)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+  }
b35f73
+
b35f73
+  free(u->scratch);
b35f73
+  u->scratch = NULL;
b35f73
+
b35f73
+  return CURLUE_OK;
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ * Parse the URL and set the relevant members of the Curl_URL struct.
b35f73
+ */
b35f73
+static CURLUcode parseurl(const char *url, CURLU *u, unsigned int flags)
b35f73
+{
b35f73
+  CURLUcode result = seturl(url, u, flags);
b35f73
+  if(result) {
b35f73
+    free_urlhandle(u);
b35f73
+    memset(u, 0, sizeof(struct Curl_URL));
b35f73
+  }
b35f73
+  return result;
b35f73
+}
b35f73
+
b35f73
+/*
b35f73
+ */
b35f73
+CURLU *curl_url(void)
b35f73
+{
b35f73
+  return calloc(sizeof(struct Curl_URL), 1);
b35f73
+}
b35f73
+
b35f73
+void curl_url_cleanup(CURLU *u)
b35f73
+{
b35f73
+  if(u) {
b35f73
+    free_urlhandle(u);
b35f73
+    free(u);
b35f73
+  }
b35f73
+}
b35f73
+
b35f73
+#define DUP(dest, src, name)         \
b35f73
+  if(src->name) {                    \
b35f73
+    dest->name = strdup(src->name);  \
b35f73
+    if(!dest->name)                  \
b35f73
+      goto fail;                     \
b35f73
+  }
b35f73
+
b35f73
+CURLU *curl_url_dup(CURLU *in)
b35f73
+{
b35f73
+  struct Curl_URL *u = calloc(sizeof(struct Curl_URL), 1);
b35f73
+  if(u) {
b35f73
+    DUP(u, in, scheme);
b35f73
+    DUP(u, in, user);
b35f73
+    DUP(u, in, password);
b35f73
+    DUP(u, in, options);
b35f73
+    DUP(u, in, host);
b35f73
+    DUP(u, in, port);
b35f73
+    DUP(u, in, path);
b35f73
+    DUP(u, in, query);
b35f73
+    DUP(u, in, fragment);
b35f73
+    u->portnum = in->portnum;
b35f73
+  }
b35f73
+  return u;
b35f73
+  fail:
b35f73
+  curl_url_cleanup(u);
b35f73
+  return NULL;
b35f73
+}
b35f73
+
b35f73
+CURLUcode curl_url_get(CURLU *u, CURLUPart what,
b35f73
+                       char **part, unsigned int flags)
b35f73
+{
b35f73
+  char *ptr;
b35f73
+  CURLUcode ifmissing = CURLUE_UNKNOWN_PART;
b35f73
+  char portbuf[7];
b35f73
+  bool urldecode = (flags & CURLU_URLDECODE)?1:0;
b35f73
+  bool plusdecode = FALSE;
b35f73
+  (void)flags;
b35f73
+  if(!u)
b35f73
+    return CURLUE_BAD_HANDLE;
b35f73
+  if(!part)
b35f73
+    return CURLUE_BAD_PARTPOINTER;
b35f73
+  *part = NULL;
b35f73
+
b35f73
+  switch(what) {
b35f73
+  case CURLUPART_SCHEME:
b35f73
+    ptr = u->scheme;
b35f73
+    ifmissing = CURLUE_NO_SCHEME;
b35f73
+    urldecode = FALSE; /* never for schemes */
b35f73
+    break;
b35f73
+  case CURLUPART_USER:
b35f73
+    ptr = u->user;
b35f73
+    ifmissing = CURLUE_NO_USER;
b35f73
+    break;
b35f73
+  case CURLUPART_PASSWORD:
b35f73
+    ptr = u->password;
b35f73
+    ifmissing = CURLUE_NO_PASSWORD;
b35f73
+    break;
b35f73
+  case CURLUPART_OPTIONS:
b35f73
+    ptr = u->options;
b35f73
+    ifmissing = CURLUE_NO_OPTIONS;
b35f73
+    break;
b35f73
+  case CURLUPART_HOST:
b35f73
+    ptr = u->host;
b35f73
+    ifmissing = CURLUE_NO_HOST;
b35f73
+    break;
b35f73
+  case CURLUPART_PORT:
b35f73
+    ptr = u->port;
b35f73
+    ifmissing = CURLUE_NO_PORT;
b35f73
+    urldecode = FALSE; /* never for port */
b35f73
+    if(!ptr && (flags & CURLU_DEFAULT_PORT) && u->scheme) {
b35f73
+      /* there's no stored port number, but asked to deliver
b35f73
+         a default one for the scheme */
b35f73
+      const struct Curl_handler *h =
b35f73
+        Curl_builtin_scheme(u->scheme);
b35f73
+      if(h) {
b35f73
+        snprintf(portbuf, sizeof(portbuf), "%ld", h->defport);
b35f73
+        ptr = portbuf;
b35f73
+      }
b35f73
+    }
b35f73
+    else if(ptr && u->scheme) {
b35f73
+      /* there is a stored port number, but ask to inhibit if
b35f73
+         it matches the default one for the scheme */
b35f73
+      const struct Curl_handler *h =
b35f73
+        Curl_builtin_scheme(u->scheme);
b35f73
+      if(h && (h->defport == u->portnum) &&
b35f73
+         (flags & CURLU_NO_DEFAULT_PORT))
b35f73
+        ptr = NULL;
b35f73
+    }
b35f73
+    break;
b35f73
+  case CURLUPART_PATH:
b35f73
+    ptr = u->path;
b35f73
+    if(!ptr) {
b35f73
+      ptr = u->path = strdup("/");
b35f73
+      if(!u->path)
b35f73
+        return CURLUE_OUT_OF_MEMORY;
b35f73
+    }
b35f73
+    break;
b35f73
+  case CURLUPART_QUERY:
b35f73
+    ptr = u->query;
b35f73
+    ifmissing = CURLUE_NO_QUERY;
b35f73
+    plusdecode = urldecode;
b35f73
+    break;
b35f73
+  case CURLUPART_FRAGMENT:
b35f73
+    ptr = u->fragment;
b35f73
+    ifmissing = CURLUE_NO_FRAGMENT;
b35f73
+    break;
b35f73
+  case CURLUPART_URL: {
b35f73
+    char *url;
b35f73
+    char *scheme;
b35f73
+    char *options = u->options;
b35f73
+    char *port = u->port;
b35f73
+    urldecode = FALSE; /* not for the whole thing */
b35f73
+    if(u->scheme && strcasecompare("file", u->scheme)) {
b35f73
+      url = aprintf("file://%s%s%s",
b35f73
+                    u->path,
b35f73
+                    u->fragment? "#": "",
b35f73
+                    u->fragment? u->fragment : "");
b35f73
+    }
b35f73
+    else if(!u->host)
b35f73
+      return CURLUE_NO_HOST;
b35f73
+    else {
b35f73
+      const struct Curl_handler *h = NULL;
b35f73
+      if(u->scheme)
b35f73
+        scheme = u->scheme;
b35f73
+      else if(flags & CURLU_DEFAULT_SCHEME)
b35f73
+        scheme = (char *) DEFAULT_SCHEME;
b35f73
+      else
b35f73
+        return CURLUE_NO_SCHEME;
b35f73
+
b35f73
+      if(scheme) {
b35f73
+        h = Curl_builtin_scheme(scheme);
b35f73
+        if(!port && (flags & CURLU_DEFAULT_PORT)) {
b35f73
+          /* there's no stored port number, but asked to deliver
b35f73
+             a default one for the scheme */
b35f73
+          if(h) {
b35f73
+            snprintf(portbuf, sizeof(portbuf), "%ld", h->defport);
b35f73
+            port = portbuf;
b35f73
+          }
b35f73
+        }
b35f73
+        else if(port) {
b35f73
+          /* there is a stored port number, but asked to inhibit if it matches
b35f73
+             the default one for the scheme */
b35f73
+          if(h && (h->defport == u->portnum) &&
b35f73
+             (flags & CURLU_NO_DEFAULT_PORT))
b35f73
+            port = NULL;
b35f73
+        }
b35f73
+      }
b35f73
+      if(h && !(h->flags & PROTOPT_URLOPTIONS))
b35f73
+        options = NULL;
b35f73
+
b35f73
+      url = aprintf("%s://%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
b35f73
+                    scheme,
b35f73
+                    u->user ? u->user : "",
b35f73
+                    u->password ? ":": "",
b35f73
+                    u->password ? u->password : "",
b35f73
+                    options ? ";" : "",
b35f73
+                    options ? options : "",
b35f73
+                    (u->user || u->password || options) ? "@": "",
b35f73
+                    u->host,
b35f73
+                    port ? ":": "",
b35f73
+                    port ? port : "",
b35f73
+                    (u->path && (u->path[0] != '/')) ? "/": "",
b35f73
+                    u->path ? u->path : "/",
b35f73
+                    u->query? "?": "",
b35f73
+                    u->query? u->query : "",
b35f73
+                    u->fragment? "#": "",
b35f73
+                    u->fragment? u->fragment : "");
b35f73
+    }
b35f73
+    if(!url)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+    *part = url;
b35f73
+    return CURLUE_OK;
b35f73
+    break;
b35f73
+  }
b35f73
+  default:
b35f73
+    ptr = NULL;
b35f73
+  }
b35f73
+  if(ptr) {
b35f73
+    *part = strdup(ptr);
b35f73
+    if(!*part)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+    if(plusdecode) {
b35f73
+      /* convert + to space */
b35f73
+      char *plus;
b35f73
+      for(plus = *part; *plus; ++plus) {
b35f73
+        if(*plus == '+')
b35f73
+          *plus = ' ';
b35f73
+      }
b35f73
+    }
b35f73
+    if(urldecode) {
b35f73
+      char *decoded;
b35f73
+      size_t dlen;
b35f73
+      CURLcode res = Curl_urldecode(NULL, *part, 0, &decoded, &dlen, TRUE);
b35f73
+      free(*part);
b35f73
+      if(res) {
b35f73
+        *part = NULL;
b35f73
+        return CURLUE_URLDECODE;
b35f73
+      }
b35f73
+      *part = decoded;
b35f73
+    }
b35f73
+    return CURLUE_OK;
b35f73
+  }
b35f73
+  else
b35f73
+    return ifmissing;
b35f73
+}
b35f73
+
b35f73
+CURLUcode curl_url_set(CURLU *u, CURLUPart what,
b35f73
+                       const char *part, unsigned int flags)
b35f73
+{
b35f73
+  char **storep = NULL;
b35f73
+  long port = 0;
b35f73
+  bool urlencode = (flags & CURLU_URLENCODE)? 1 : 0;
b35f73
+  bool plusencode = FALSE;
b35f73
+  bool urlskipslash = FALSE;
b35f73
+  bool appendquery = FALSE;
b35f73
+
b35f73
+  if(!u)
b35f73
+    return CURLUE_BAD_HANDLE;
b35f73
+  if(!part) {
b35f73
+    /* setting a part to NULL clears it */
b35f73
+    switch(what) {
b35f73
+    case CURLUPART_URL:
b35f73
+      break;
b35f73
+    case CURLUPART_SCHEME:
b35f73
+      storep = &u->scheme;
b35f73
+      break;
b35f73
+    case CURLUPART_USER:
b35f73
+      storep = &u->user;
b35f73
+      break;
b35f73
+    case CURLUPART_PASSWORD:
b35f73
+      storep = &u->password;
b35f73
+      break;
b35f73
+    case CURLUPART_OPTIONS:
b35f73
+      storep = &u->options;
b35f73
+      break;
b35f73
+    case CURLUPART_HOST:
b35f73
+      storep = &u->host;
b35f73
+      break;
b35f73
+    case CURLUPART_PORT:
b35f73
+      storep = &u->port;
b35f73
+      break;
b35f73
+    case CURLUPART_PATH:
b35f73
+      storep = &u->path;
b35f73
+      break;
b35f73
+    case CURLUPART_QUERY:
b35f73
+      storep = &u->query;
b35f73
+      break;
b35f73
+    case CURLUPART_FRAGMENT:
b35f73
+      storep = &u->fragment;
b35f73
+      break;
b35f73
+    default:
b35f73
+      return CURLUE_UNKNOWN_PART;
b35f73
+    }
b35f73
+    if(storep && *storep) {
b35f73
+      free(*storep);
b35f73
+      *storep = NULL;
b35f73
+    }
b35f73
+    return CURLUE_OK;
b35f73
+  }
b35f73
+
b35f73
+  switch(what) {
b35f73
+  case CURLUPART_SCHEME:
b35f73
+    if(!(flags & CURLU_NON_SUPPORT_SCHEME) &&
b35f73
+       /* verify that it is a fine scheme */
b35f73
+       !Curl_builtin_scheme(part))
b35f73
+      return CURLUE_UNSUPPORTED_SCHEME;
b35f73
+    storep = &u->scheme;
b35f73
+    urlencode = FALSE; /* never */
b35f73
+    break;
b35f73
+  case CURLUPART_USER:
b35f73
+    storep = &u->user;
b35f73
+    break;
b35f73
+  case CURLUPART_PASSWORD:
b35f73
+    storep = &u->password;
b35f73
+    break;
b35f73
+  case CURLUPART_OPTIONS:
b35f73
+    storep = &u->options;
b35f73
+    break;
b35f73
+  case CURLUPART_HOST:
b35f73
+    storep = &u->host;
b35f73
+    break;
b35f73
+  case CURLUPART_PORT:
b35f73
+    urlencode = FALSE; /* never */
b35f73
+    port = strtol(part, NULL, 10);  /* Port number must be decimal */
b35f73
+    if((port <= 0) || (port > 0xffff))
b35f73
+      return CURLUE_BAD_PORT_NUMBER;
b35f73
+    storep = &u->port;
b35f73
+    break;
b35f73
+  case CURLUPART_PATH:
b35f73
+    urlskipslash = TRUE;
b35f73
+    storep = &u->path;
b35f73
+    break;
b35f73
+  case CURLUPART_QUERY:
b35f73
+    plusencode = urlencode;
b35f73
+    appendquery = (flags & CURLU_APPENDQUERY)?1:0;
b35f73
+    storep = &u->query;
b35f73
+    break;
b35f73
+  case CURLUPART_FRAGMENT:
b35f73
+    storep = &u->fragment;
b35f73
+    break;
b35f73
+  case CURLUPART_URL: {
b35f73
+    /*
b35f73
+     * Allow a new URL to replace the existing (if any) contents.
b35f73
+     *
b35f73
+     * If the existing contents is enough for a URL, allow a relative URL to
b35f73
+     * replace it.
b35f73
+     */
b35f73
+    CURLUcode result;
b35f73
+    char *oldurl;
b35f73
+    char *redired_url;
b35f73
+    CURLU *handle2;
b35f73
+
b35f73
+    if(Curl_is_absolute_url(part, NULL, MAX_SCHEME_LEN)) {
b35f73
+      handle2 = curl_url();
b35f73
+      if(!handle2)
b35f73
+        return CURLUE_OUT_OF_MEMORY;
b35f73
+      result = parseurl(part, handle2, flags);
b35f73
+      if(!result)
b35f73
+        mv_urlhandle(handle2, u);
b35f73
+      else
b35f73
+        curl_url_cleanup(handle2);
b35f73
+      return result;
b35f73
+    }
b35f73
+    /* extract the full "old" URL to do the redirect on */
b35f73
+    result = curl_url_get(u, CURLUPART_URL, &oldurl, flags);
b35f73
+    if(result) {
b35f73
+      /* couldn't get the old URL, just use the new! */
b35f73
+      handle2 = curl_url();
b35f73
+      if(!handle2)
b35f73
+        return CURLUE_OUT_OF_MEMORY;
b35f73
+      result = parseurl(part, handle2, flags);
b35f73
+      if(!result)
b35f73
+        mv_urlhandle(handle2, u);
b35f73
+      else
b35f73
+        curl_url_cleanup(handle2);
b35f73
+      return result;
b35f73
+    }
b35f73
+
b35f73
+    /* apply the relative part to create a new URL */
b35f73
+    redired_url = Curl_concat_url(oldurl, part);
b35f73
+    free(oldurl);
b35f73
+    if(!redired_url)
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+
b35f73
+    /* now parse the new URL */
b35f73
+    handle2 = curl_url();
b35f73
+    if(!handle2) {
b35f73
+      free(redired_url);
b35f73
+      return CURLUE_OUT_OF_MEMORY;
b35f73
+    }
b35f73
+    result = parseurl(redired_url, handle2, flags);
b35f73
+    free(redired_url);
b35f73
+    if(!result)
b35f73
+      mv_urlhandle(handle2, u);
b35f73
+    else
b35f73
+      curl_url_cleanup(handle2);
b35f73
+    return result;
b35f73
+  }
b35f73
+  default:
b35f73
+    return CURLUE_UNKNOWN_PART;
b35f73
+  }
b35f73
+  if(storep) {
b35f73
+    const char *newp = part;
b35f73
+    size_t nalloc = strlen(part);
b35f73
+
b35f73
+    if(urlencode) {
b35f73
+      const char *i;
b35f73
+      char *o;
b35f73
+      bool free_part = FALSE;
b35f73
+      char *enc = malloc(nalloc * 3 + 1); /* for worst case! */
b35f73
+      if(!enc)
b35f73
+        return CURLUE_OUT_OF_MEMORY;
b35f73
+      if(plusencode) {
b35f73
+        /* space to plus */
b35f73
+        i = part;
b35f73
+        for(o = enc; *i; ++o, ++i)
b35f73
+          *o = (*i == ' ') ? '+' : *i;
b35f73
+        *o = 0; /* zero terminate */
b35f73
+        part = strdup(enc);
b35f73
+        if(!part) {
b35f73
+          free(enc);
b35f73
+          return CURLUE_OUT_OF_MEMORY;
b35f73
+        }
b35f73
+        free_part = TRUE;
b35f73
+      }
b35f73
+      for(i = part, o = enc; *i; i++) {
b35f73
+        if(Curl_isunreserved(*i) ||
b35f73
+           ((*i == '/') && urlskipslash) ||
b35f73
+           ((*i == '=') && appendquery) ||
b35f73
+           ((*i == '+') && plusencode)) {
b35f73
+          *o = *i;
b35f73
+          o++;
b35f73
+        }
b35f73
+        else {
b35f73
+          snprintf(o, 4, "%%%02x", *i);
b35f73
+          o += 3;
b35f73
+        }
b35f73
+      }
b35f73
+      *o = 0; /* zero terminate */
b35f73
+      newp = enc;
b35f73
+      if(free_part)
b35f73
+        free((char *)part);
b35f73
+    }
b35f73
+    else {
b35f73
+      char *p;
b35f73
+      newp = strdup(part);
b35f73
+      if(!newp)
b35f73
+        return CURLUE_OUT_OF_MEMORY;
b35f73
+      p = (char *)newp;
b35f73
+      while(*p) {
b35f73
+        /* make sure percent encoded are lower case */
b35f73
+        if((*p == '%') && ISXDIGIT(p[1]) && ISXDIGIT(p[2]) &&
b35f73
+           (ISUPPER(p[1]) || ISUPPER(p[2]))) {
b35f73
+          p[1] = (char)TOLOWER(p[1]);
b35f73
+          p[2] = (char)TOLOWER(p[2]);
b35f73
+          p += 3;
b35f73
+        }
b35f73
+        else
b35f73
+          p++;
b35f73
+      }
b35f73
+    }
b35f73
+
b35f73
+    if(appendquery) {
b35f73
+      /* Append the string onto the old query. Add a '&' separator if none is
b35f73
+         present at the end of the exsting query already */
b35f73
+      size_t querylen = u->query ? strlen(u->query) : 0;
b35f73
+      bool addamperand = querylen && (u->query[querylen -1] != '&';;
b35f73
+      if(querylen) {
b35f73
+        size_t newplen = strlen(newp);
b35f73
+        char *p = malloc(querylen + addamperand + newplen + 1);
b35f73
+        if(!p) {
b35f73
+          free((char *)newp);
b35f73
+          return CURLUE_OUT_OF_MEMORY;
b35f73
+        }
b35f73
+        strcpy(p, u->query); /* original query */
b35f73
+        if(addamperand)
b35f73
+          p[querylen] = '&'; /* ampersand */
b35f73
+        strcpy(&p[querylen + addamperand], newp); /* new suffix */
b35f73
+        free((char *)newp);
b35f73
+        free(*storep);
b35f73
+        *storep = p;
b35f73
+        return CURLUE_OK;
b35f73
+      }
b35f73
+    }
b35f73
+
b35f73
+    free(*storep);
b35f73
+    *storep = (char *)newp;
b35f73
+  }
b35f73
+  /* set after the string, to make it not assigned if the allocation above
b35f73
+     fails */
b35f73
+  if(port)
b35f73
+    u->portnum = port;
b35f73
+  return CURLUE_OK;
b35f73
+}
b35f73
diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc
b35f73
index aa5fff0..0f6ac44 100644
b35f73
--- a/tests/data/Makefile.inc
b35f73
+++ b/tests/data/Makefile.inc
b35f73
@@ -178,6 +178,8 @@ test1533 test1534 test1535 test1536 test1537 test1538 \
b35f73
 test1540 \
b35f73
 test1550 test1551 test1552 test1553 test1554 test1555 test1556 test1557 \
b35f73
 \
b35f73
+test1560 \
b35f73
+\
b35f73
 test1590 \
b35f73
 test1600 test1601 test1602 test1603 test1604 test1605 test1606 test1607 \
b35f73
 test1608 test1609 \
b35f73
diff --git a/tests/data/test1560 b/tests/data/test1560
b35f73
new file mode 100644
b35f73
index 0000000..720df03
b35f73
--- /dev/null
b35f73
+++ b/tests/data/test1560
b35f73
@@ -0,0 +1,28 @@
b35f73
+<testcase>
b35f73
+<info>
b35f73
+<keywords>
b35f73
+unittest
b35f73
+URL API
b35f73
+</keywords>
b35f73
+</info>
b35f73
+
b35f73
+#
b35f73
+# Client-side
b35f73
+<client>
b35f73
+<server>
b35f73
+none
b35f73
+</server>
b35f73
+<features>
b35f73
+file
b35f73
+https
b35f73
+http
b35f73
+</features>
b35f73
+ <name>
b35f73
+URL API
b35f73
+ </name>
b35f73
+<tool>
b35f73
+lib1560
b35f73
+</tool>
b35f73
+</client>
b35f73
+
b35f73
+</testcase>
b35f73
diff --git a/tests/libtest/Makefile.am b/tests/libtest/Makefile.am
b35f73
index d14f37d..dc97e32 100644
b35f73
--- a/tests/libtest/Makefile.am
b35f73
+++ b/tests/libtest/Makefile.am
b35f73
@@ -133,3 +133,8 @@ lib1521.c: $(top_srcdir)/tests/libtest/mk-lib1521.pl $(top_srcdir)/include/curl/
b35f73
 
b35f73
 checksrc:
b35f73
 	@PERL@ $(top_srcdir)/lib/checksrc.pl $(srcdir)/*.c
b35f73
+
b35f73
+if CURLDEBUG
b35f73
+# for debug builds, we scan the sources on all regular make invokes
b35f73
+all-local: checksrc
b35f73
+endif
b35f73
diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc
b35f73
index 238ef97..7a3cd16 100644
b35f73
--- a/tests/libtest/Makefile.inc
b35f73
+++ b/tests/libtest/Makefile.inc
b35f73
@@ -30,6 +30,7 @@ noinst_PROGRAMS = chkhostname libauthretry libntlmconnect                \
b35f73
  lib1534 lib1535 lib1536 lib1537 lib1538 \
b35f73
  lib1540 \
b35f73
  lib1550 lib1551 lib1552 lib1553 lib1554 lib1555 lib1556 lib1557 \
b35f73
+ lib1560 \
b35f73
  lib1900 \
b35f73
  lib2033
b35f73
 
b35f73
@@ -507,6 +508,9 @@ lib1557_SOURCES = lib1557.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
b35f73
 lib1557_LDADD = $(TESTUTIL_LIBS)
b35f73
 lib1557_CPPFLAGS = $(AM_CPPFLAGS) -DLIB1557
b35f73
 
b35f73
+lib1560_SOURCES = lib1560.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
b35f73
+lib1560_LDADD = $(TESTUTIL_LIBS)
b35f73
+
b35f73
 lib1900_SOURCES = lib1900.c $(SUPPORTFILES) $(TESTUTIL) $(WARNLESS)
b35f73
 lib1900_LDADD = $(TESTUTIL_LIBS)
b35f73
 lib1900_CPPFLAGS = $(AM_CPPFLAGS)
b35f73
diff --git a/tests/libtest/lib1560.c b/tests/libtest/lib1560.c
b35f73
new file mode 100644
b35f73
index 0000000..669ea9a
b35f73
--- /dev/null
b35f73
+++ b/tests/libtest/lib1560.c
b35f73
@@ -0,0 +1,760 @@
b35f73
+/***************************************************************************
b35f73
+ *                                  _   _ ____  _
b35f73
+ *  Project                     ___| | | |  _ \| |
b35f73
+ *                             / __| | | | |_) | |
b35f73
+ *                            | (__| |_| |  _ <| |___
b35f73
+ *                             \___|\___/|_| \_\_____|
b35f73
+ *
b35f73
+ * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ *
b35f73
+ * This software is licensed as described in the file COPYING, which
b35f73
+ * you should have received as part of this distribution. The terms
b35f73
+ * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+ *
b35f73
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+ * copies of the Software, and permit persons to whom the Software is
b35f73
+ * furnished to do so, under the terms of the COPYING file.
b35f73
+ *
b35f73
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+ * KIND, either express or implied.
b35f73
+ *
b35f73
+ ***************************************************************************/
b35f73
+
b35f73
+/*
b35f73
+ * Note:
b35f73
+ *
b35f73
+ * Since the URL parser by default only accepts schemes that *this instance*
b35f73
+ * of libcurl supports, make sure that the test1560 file lists all the schemes
b35f73
+ * that this test will assume to be present!
b35f73
+ */
b35f73
+
b35f73
+#include "test.h"
b35f73
+
b35f73
+#include "testutil.h"
b35f73
+#include "warnless.h"
b35f73
+#include "memdebug.h" /* LAST include file */
b35f73
+
b35f73
+struct part {
b35f73
+  CURLUPart part;
b35f73
+  const char *name;
b35f73
+};
b35f73
+
b35f73
+
b35f73
+static int checkparts(CURLU *u, const char *in, const char *wanted,
b35f73
+                      unsigned int getflags)
b35f73
+{
b35f73
+  int i;
b35f73
+  CURLUcode rc;
b35f73
+  char buf[256];
b35f73
+  char *bufp = &buf[0];
b35f73
+  size_t len = sizeof(buf);
b35f73
+  struct part parts[] = {
b35f73
+    {CURLUPART_SCHEME, "scheme"},
b35f73
+    {CURLUPART_USER, "user"},
b35f73
+    {CURLUPART_PASSWORD, "password"},
b35f73
+    {CURLUPART_OPTIONS, "options"},
b35f73
+    {CURLUPART_HOST, "host"},
b35f73
+    {CURLUPART_PORT, "port"},
b35f73
+    {CURLUPART_PATH, "path"},
b35f73
+    {CURLUPART_QUERY, "query"},
b35f73
+    {CURLUPART_FRAGMENT, "fragment"},
b35f73
+    {0, NULL}
b35f73
+  };
b35f73
+  buf[0] = 0;
b35f73
+
b35f73
+  for(i = 0; parts[i].name; i++) {
b35f73
+    char *p = NULL;
b35f73
+    size_t n;
b35f73
+    rc = curl_url_get(u, parts[i].part, &p, getflags);
b35f73
+    if(!rc && p) {
b35f73
+      snprintf(bufp, len, "%s%s", buf[0]?" | ":"", p);
b35f73
+    }
b35f73
+    else
b35f73
+      snprintf(bufp, len, "%s[%d]", buf[0]?" | ":"", (int)rc);
b35f73
+
b35f73
+    n = strlen(bufp);
b35f73
+    bufp += n;
b35f73
+    len -= n;
b35f73
+    curl_free(p);
b35f73
+  }
b35f73
+  if(strcmp(buf, wanted)) {
b35f73
+    fprintf(stderr, "in: %s\nwanted: %s\ngot:    %s\n", in, wanted, buf);
b35f73
+    return 1;
b35f73
+  }
b35f73
+  return 0;
b35f73
+}
b35f73
+
b35f73
+struct redircase {
b35f73
+  const char *in;
b35f73
+  const char *set;
b35f73
+  const char *out;
b35f73
+  unsigned int urlflags;
b35f73
+  unsigned int setflags;
b35f73
+  CURLUcode ucode;
b35f73
+};
b35f73
+
b35f73
+struct setcase {
b35f73
+  const char *in;
b35f73
+  const char *set;
b35f73
+  const char *out;
b35f73
+  unsigned int urlflags;
b35f73
+  unsigned int setflags;
b35f73
+  CURLUcode ucode;
b35f73
+};
b35f73
+
b35f73
+struct testcase {
b35f73
+  const char *in;
b35f73
+  const char *out;
b35f73
+  unsigned int urlflags;
b35f73
+  unsigned int getflags;
b35f73
+  CURLUcode ucode;
b35f73
+};
b35f73
+
b35f73
+struct urltestcase {
b35f73
+  const char *in;
b35f73
+  const char *out;
b35f73
+  unsigned int urlflags; /* pass to curl_url() */
b35f73
+  unsigned int getflags; /* pass to curl_url_get() */
b35f73
+  CURLUcode ucode;
b35f73
+};
b35f73
+
b35f73
+struct querycase {
b35f73
+  const char *in;
b35f73
+  const char *q;
b35f73
+  const char *out;
b35f73
+  unsigned int urlflags; /* pass to curl_url() */
b35f73
+  unsigned int qflags; /* pass to curl_url_get() */
b35f73
+  CURLUcode ucode;
b35f73
+};
b35f73
+
b35f73
+static struct testcase get_parts_list[] ={
b35f73
+  {"https://127.0.0.1:443",
b35f73
+   "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [17] | [18]",
b35f73
+   0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
b35f73
+  {"http://%3a:%3a@ex%0ample/%3f+?+%3f+%23#+%23%3f%g7",
b35f73
+   "http | : | : | [13] | [6] | [15] | /?+ |  ? # | +#?%g7",
b35f73
+   0, CURLU_URLDECODE, CURLUE_OK},
b35f73
+  {"http://%3a:%3a@ex%0ample/%3f?%3f%35#%35%3f%g7",
b35f73
+   "http | %3a | %3a | [13] | ex%0ample | [15] | /%3f | %3f%35 | %35%3f%g7",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://HO0_-st%41/",
b35f73
+   "http | [11] | [12] | [13] | HO0_-st%41 | [15] | / | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file://hello.html",
b35f73
+   "",
b35f73
+   0, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"http://HO0_-st/",
b35f73
+   "http | [11] | [12] | [13] | HO0_-st | [15] | / | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"imap://user:pass;option@server/path",
b35f73
+   "imap | user | pass | option | server | [15] | /path | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://user:pass;option@server/path",
b35f73
+   "http | user | pass;option | [13] | server | [15] | /path | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file:/hello.html",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file://127.0.0.1/hello.html",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file:////hello.html",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | //hello.html | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file:///hello.html",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [17] | [18]",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"https://127.0.0.1",
b35f73
+   "https | [11] | [12] | [13] | 127.0.0.1 | 443 | / | [17] | [18]",
b35f73
+   0, CURLU_DEFAULT_PORT, CURLUE_OK},
b35f73
+  {"https://127.0.0.1",
b35f73
+   "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [17] | [18]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"https://[::1]:1234",
b35f73
+   "https | [11] | [12] | [13] | [::1] | 1234 | / | [17] | [18]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"https://127abc.com",
b35f73
+   "https | [11] | [12] | [13] | 127abc.com | [15] | / | [17] | [18]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"https:// example.com?check",
b35f73
+   "",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"https://e x a m p l e.com?check",
b35f73
+   "",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"https://example.com?check",
b35f73
+   "https | [11] | [12] | [13] | example.com | [15] | / | check | [18]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"https://example.com:65536",
b35f73
+   "",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
+  {"https://example.com:0#moo",
b35f73
+   "",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
+  {"https://example.com:01#moo",
b35f73
+   "https | [11] | [12] | [13] | example.com | 1 | / | "
b35f73
+   "[17] | moo",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"https://example.com:1#moo",
b35f73
+   "https | [11] | [12] | [13] | example.com | 1 | / | "
b35f73
+   "[17] | moo",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://example.com#moo",
b35f73
+   "http | [11] | [12] | [13] | example.com | [15] | / | "
b35f73
+   "[17] | moo",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://example.com",
b35f73
+   "http | [11] | [12] | [13] | example.com | [15] | / | "
b35f73
+   "[17] | [18]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://example.com/path/html",
b35f73
+   "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
b35f73
+   "[17] | [18]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://example.com/path/html?query=name",
b35f73
+   "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
b35f73
+   "query=name | [18]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://example.com/path/html?query=name#anchor",
b35f73
+   "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
b35f73
+   "query=name | anchor",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://example.com:1234/path/html?query=name#anchor",
b35f73
+   "http | [11] | [12] | [13] | example.com | 1234 | /path/html | "
b35f73
+   "query=name | anchor",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http:///user:password@example.com:1234/path/html?query=name#anchor",
b35f73
+   "http | user | password | [13] | example.com | 1234 | /path/html | "
b35f73
+   "query=name | anchor",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"https://user:password@example.com:1234/path/html?query=name#anchor",
b35f73
+   "https | user | password | [13] | example.com | 1234 | /path/html | "
b35f73
+   "query=name | anchor",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://user:password@example.com:1234/path/html?query=name#anchor",
b35f73
+   "http | user | password | [13] | example.com | 1234 | /path/html | "
b35f73
+   "query=name | anchor",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http:/user:password@example.com:1234/path/html?query=name#anchor",
b35f73
+   "http | user | password | [13] | example.com | 1234 | /path/html | "
b35f73
+   "query=name | anchor",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http:////user:password@example.com:1234/path/html?query=name#anchor",
b35f73
+   "",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {NULL, NULL, 0, 0, CURLUE_OK},
b35f73
+};
b35f73
+
b35f73
+static struct urltestcase get_url_list[] = {
b35f73
+  {"HTTP://test/", "http://test/", 0, 0, CURLUE_OK},
b35f73
+  {"http://HO0_-st..~./", "", 0, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"http:/@example.com: 123/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
+  {"http:/@example.com:123 /", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
+  {"http:/@example.com:123a/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
+  {"http://host/file\r", "", 0, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"http://host/file\n\x03", "", 0, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"htt\x02://host/file", "",
b35f73
+   CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {" http://host/file", "", 0, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  /* here the password ends at the semicolon and options is 'word' */
b35f73
+  {"imap://user:pass;word@host/file",
b35f73
+   "imap://user:pass;word@host/file",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  /* here the password has the semicolon */
b35f73
+  {"http://user:pass;word@host/file",
b35f73
+   "http://user:pass;word@host/file",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file:///file.txt#moo",
b35f73
+   "file:///file.txt#moo",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file:////file.txt",
b35f73
+   "file:////file.txt",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"file:///file.txt",
b35f73
+   "file:///file.txt",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com/hello/../here",
b35f73
+   "http://example.com/hello/../here",
b35f73
+   CURLU_PATH_AS_IS, 0, CURLUE_OK},
b35f73
+  {"http://example.com/hello/../here",
b35f73
+   "http://example.com/here",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com:80",
b35f73
+   "http://example.com/",
b35f73
+   0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
b35f73
+  {"tp://example.com/path/html",
b35f73
+   "",
b35f73
+   0, 0, CURLUE_UNSUPPORTED_SCHEME},
b35f73
+  {"http://hello:fool@example.com",
b35f73
+   "",
b35f73
+   CURLU_DISALLOW_USER, 0, CURLUE_USER_NOT_ALLOWED},
b35f73
+  {"http:/@example.com:123",
b35f73
+   "http://example.com:123/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http:/:password@example.com",
b35f73
+   "http://:password@example.com/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://user@example.com?#",
b35f73
+   "http://user@example.com/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://user@example.com?",
b35f73
+   "http://user@example.com/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://user@example.com#anchor",
b35f73
+   "http://user@example.com/#anchor",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"example.com/path/html",
b35f73
+   "https://example.com/path/html",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"example.com/path/html",
b35f73
+   "",
b35f73
+   0, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"http://user:password@example.com:1234/path/html?query=name#anchor",
b35f73
+   "http://user:password@example.com:1234/path/html?query=name#anchor",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com:1234/path/html?query=name#anchor",
b35f73
+   "http://example.com:1234/path/html?query=name#anchor",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com/path/html?query=name#anchor",
b35f73
+   "http://example.com/path/html?query=name#anchor",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com/path/html?query=name",
b35f73
+   "http://example.com/path/html?query=name",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com/path/html",
b35f73
+   "http://example.com/path/html",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"tp://example.com/path/html",
b35f73
+   "tp://example.com/path/html",
b35f73
+   CURLU_NON_SUPPORT_SCHEME, 0, CURLUE_OK},
b35f73
+  {NULL, NULL, 0, 0, 0}
b35f73
+};
b35f73
+
b35f73
+static int checkurl(const char *url, const char *out)
b35f73
+{
b35f73
+  if(strcmp(out, url)) {
b35f73
+    fprintf(stderr, "Wanted: %s\nGot   : %s\n",
b35f73
+            out, url);
b35f73
+    return 1;
b35f73
+  }
b35f73
+  return 0;
b35f73
+}
b35f73
+
b35f73
+/* !checksrc! disable SPACEBEFORECOMMA 1 */
b35f73
+static struct setcase set_parts_list[] = {
b35f73
+  {"https://host/",
b35f73
+   "path=%4A%4B%4C,",
b35f73
+   "https://host/%4a%4b%4c",
b35f73
+   0, 0, CURLUE_NO_HOST},
b35f73
+  {"https://host/mooo?q#f",
b35f73
+   "path=NULL,query=NULL,fragment=NULL,",
b35f73
+   "https://host/",
b35f73
+   0, 0, CURLUE_NO_HOST},
b35f73
+  {"https://user:secret@host/",
b35f73
+   "user=NULL,password=NULL,",
b35f73
+   "https://host/",
b35f73
+   0, 0, CURLUE_NO_HOST},
b35f73
+  {NULL,
b35f73
+   "scheme=https,user=   @:,host=foobar,",
b35f73
+   "https://%20%20%20%40%3a@foobar/",
b35f73
+   0, CURLU_URLENCODE, CURLUE_OK},
b35f73
+  {NULL,
b35f73
+   "scheme=https,host=  ,path= ,user= ,password= ,query= ,fragment= ,",
b35f73
+   "https://%20:%20@%20%20/%20?+#%20",
b35f73
+   0, CURLU_URLENCODE, CURLUE_OK},
b35f73
+  {NULL,
b35f73
+   "scheme=https,host=foobar,path=/this /path /is /here,",
b35f73
+   "https://foobar/this%20/path%20/is%20/here",
b35f73
+   0, CURLU_URLENCODE, CURLUE_OK},
b35f73
+  {"imap://user:secret;opt@host/",
b35f73
+   "options=updated,scheme=imaps,password=p4ssw0rd,",
b35f73
+   "imaps://user:p4ssw0rd;updated@host/",
b35f73
+   0, 0, CURLUE_NO_HOST},
b35f73
+  {"imap://user:secret;optit@host/",
b35f73
+   "scheme=https,",
b35f73
+   "https://user:secret@host/",
b35f73
+   0, 0, CURLUE_NO_HOST},
b35f73
+  {"file:///file#anchor",
b35f73
+   "scheme=https,host=example,",
b35f73
+   "https://example/file#anchor",
b35f73
+   0, 0, CURLUE_NO_HOST},
b35f73
+  {NULL, /* start fresh! */
b35f73
+   "scheme=file,host=127.0.0.1,path=/no,user=anonymous,",
b35f73
+   "file:///no",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {NULL, /* start fresh! */
b35f73
+   "scheme=ftp,host=127.0.0.1,path=/no,user=anonymous,",
b35f73
+   "ftp://anonymous@127.0.0.1/no",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {NULL, /* start fresh! */
b35f73
+   "scheme=https,host=example.com,",
b35f73
+   "https://example.com/",
b35f73
+   0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK},
b35f73
+  {"http://user:foo@example.com/path?query#frag",
b35f73
+   "fragment=changed,",
b35f73
+   "http://user:foo@example.com/path?query#changed",
b35f73
+   0, CURLU_NON_SUPPORT_SCHEME, CURLUE_OK},
b35f73
+  {"http://example.com/",
b35f73
+   "scheme=foo,", /* not accepted */
b35f73
+   "http://example.com/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com/",
b35f73
+   "scheme=https,path=/hello,fragment=snippet,",
b35f73
+   "https://example.com/hello#snippet",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com:80",
b35f73
+   "user=foo,port=1922,",
b35f73
+   "http://foo@example.com:1922/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com:80",
b35f73
+   "user=foo,password=bar,",
b35f73
+   "http://foo:bar@example.com:80/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com:80",
b35f73
+   "user=foo,",
b35f73
+   "http://foo@example.com:80/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com",
b35f73
+   "host=www.example.com,",
b35f73
+   "http://www.example.com/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"http://example.com:80",
b35f73
+   "scheme=ftp,",
b35f73
+   "ftp://example.com:80/",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {NULL, NULL, NULL, 0, 0, 0}
b35f73
+};
b35f73
+
b35f73
+static CURLUPart part2id(char *part)
b35f73
+{
b35f73
+  if(!strcmp("url", part))
b35f73
+    return CURLUPART_URL;
b35f73
+  if(!strcmp("scheme", part))
b35f73
+    return CURLUPART_SCHEME;
b35f73
+  if(!strcmp("user", part))
b35f73
+    return CURLUPART_USER;
b35f73
+  if(!strcmp("password", part))
b35f73
+    return CURLUPART_PASSWORD;
b35f73
+  if(!strcmp("options", part))
b35f73
+    return CURLUPART_OPTIONS;
b35f73
+  if(!strcmp("host", part))
b35f73
+    return CURLUPART_HOST;
b35f73
+  if(!strcmp("port", part))
b35f73
+    return CURLUPART_PORT;
b35f73
+  if(!strcmp("path", part))
b35f73
+    return CURLUPART_PATH;
b35f73
+  if(!strcmp("query", part))
b35f73
+    return CURLUPART_QUERY;
b35f73
+  if(!strcmp("fragment", part))
b35f73
+    return CURLUPART_FRAGMENT;
b35f73
+  return 9999; /* bad input => bad output */
b35f73
+}
b35f73
+
b35f73
+static void updateurl(CURLU *u, const char *cmd, unsigned int setflags)
b35f73
+{
b35f73
+  const char *p = cmd;
b35f73
+
b35f73
+  /* make sure the last command ends with a comma too! */
b35f73
+  while(p) {
b35f73
+    char *e = strchr(p, ',');
b35f73
+    if(e) {
b35f73
+      size_t n = e-p;
b35f73
+      char buf[80];
b35f73
+      char part[80];
b35f73
+      char value[80];
b35f73
+      memcpy(buf, p, n);
b35f73
+      buf[n] = 0;
b35f73
+      if(2 == sscanf(buf, "%79[^=]=%79[^,]", part, value)) {
b35f73
+        CURLUPart what = part2id(part);
b35f73
+#if 0
b35f73
+        /* for debugging this */
b35f73
+        fprintf(stderr, "%s = %s [%d]\n", part, value, (int)what);
b35f73
+#endif
b35f73
+        if(!strcmp("NULL", value))
b35f73
+          curl_url_set(u, what, NULL, setflags);
b35f73
+        else
b35f73
+          curl_url_set(u, what, value, setflags);
b35f73
+      }
b35f73
+      p = e + 1;
b35f73
+      continue;
b35f73
+    }
b35f73
+    break;
b35f73
+  }
b35f73
+
b35f73
+}
b35f73
+
b35f73
+static struct redircase set_url_list[] = {
b35f73
+  {"file://localhost/path?query#frag",
b35f73
+   "foo#another",
b35f73
+   "file:///foo#another",
b35f73
+   0, 0, 0},
b35f73
+  {"http://example.com/path?query#frag",
b35f73
+   "https://two.example.com/bradnew",
b35f73
+   "https://two.example.com/bradnew",
b35f73
+   0, 0, 0},
b35f73
+  {"http://example.com/path?query#frag",
b35f73
+   "../../newpage#foo",
b35f73
+   "http://example.com/newpage#foo",
b35f73
+   0, 0, 0},
b35f73
+  {"http://user:foo@example.com/path?query#frag",
b35f73
+   "../../newpage",
b35f73
+   "http://user:foo@example.com/newpage",
b35f73
+   0, 0, 0},
b35f73
+  {"http://user:foo@example.com/path?query#frag",
b35f73
+   "../newpage",
b35f73
+   "http://user:foo@example.com/newpage",
b35f73
+   0, 0, 0},
b35f73
+  {NULL, NULL, NULL, 0, 0, 0}
b35f73
+};
b35f73
+
b35f73
+static int set_url(void)
b35f73
+{
b35f73
+  int i;
b35f73
+  CURLUcode rc;
b35f73
+  CURLU *urlp;
b35f73
+  int error = 0;
b35f73
+
b35f73
+  for(i = 0; set_url_list[i].in && !error; i++) {
b35f73
+    char *url = NULL;
b35f73
+    urlp = curl_url();
b35f73
+    if(!urlp)
b35f73
+      break;
b35f73
+    rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].in,
b35f73
+                      set_url_list[i].urlflags);
b35f73
+    if(!rc) {
b35f73
+      rc = curl_url_set(urlp, CURLUPART_URL, set_url_list[i].set,
b35f73
+                        set_url_list[i].setflags);
b35f73
+      if(rc) {
b35f73
+        fprintf(stderr, "%s:%d Set URL %s returned %d\n",
b35f73
+                __FILE__, __LINE__, set_url_list[i].set,
b35f73
+                (int)rc);
b35f73
+        error++;
b35f73
+      }
b35f73
+      else {
b35f73
+        rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
b35f73
+        if(rc) {
b35f73
+          fprintf(stderr, "%s:%d Get URL returned %d\n",
b35f73
+                  __FILE__, __LINE__, (int)rc);
b35f73
+          error++;
b35f73
+        }
b35f73
+        else {
b35f73
+          if(checkurl(url, set_url_list[i].out)) {
b35f73
+            error++;
b35f73
+          }
b35f73
+        }
b35f73
+      }
b35f73
+      curl_free(url);
b35f73
+    }
b35f73
+    else if(rc != set_url_list[i].ucode) {
b35f73
+      fprintf(stderr, "Set URL\nin: %s\nreturned %d (expected %d)\n",
b35f73
+              set_url_list[i].in, (int)rc, set_url_list[i].ucode);
b35f73
+      error++;
b35f73
+    }
b35f73
+    curl_url_cleanup(urlp);
b35f73
+  }
b35f73
+  return error;
b35f73
+}
b35f73
+
b35f73
+static int set_parts(void)
b35f73
+{
b35f73
+  int i;
b35f73
+  CURLUcode rc;
b35f73
+  int error = 0;
b35f73
+
b35f73
+  for(i = 0; set_parts_list[i].set && !error; i++) {
b35f73
+    char *url = NULL;
b35f73
+    CURLU *urlp = curl_url();
b35f73
+    if(!urlp) {
b35f73
+      error++;
b35f73
+      break;
b35f73
+    }
b35f73
+    if(set_parts_list[i].in)
b35f73
+      rc = curl_url_set(urlp, CURLUPART_URL, set_parts_list[i].in,
b35f73
+                        set_parts_list[i].urlflags);
b35f73
+    else
b35f73
+      rc = CURLUE_OK;
b35f73
+    if(!rc) {
b35f73
+      updateurl(urlp, set_parts_list[i].set, set_parts_list[i].setflags);
b35f73
+      rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
b35f73
+
b35f73
+      if(rc) {
b35f73
+        fprintf(stderr, "%s:%d Get URL returned %d\n",
b35f73
+                __FILE__, __LINE__, (int)rc);
b35f73
+        error++;
b35f73
+      }
b35f73
+      else if(checkurl(url, set_parts_list[i].out)) {
b35f73
+        error++;
b35f73
+      }
b35f73
+    }
b35f73
+    else if(rc != set_parts_list[i].ucode) {
b35f73
+      fprintf(stderr, "Set parts\nin: %s\nreturned %d (expected %d)\n",
b35f73
+              set_parts_list[i].in, (int)rc, set_parts_list[i].ucode);
b35f73
+      error++;
b35f73
+    }
b35f73
+    curl_free(url);
b35f73
+    curl_url_cleanup(urlp);
b35f73
+  }
b35f73
+  return error;
b35f73
+}
b35f73
+
b35f73
+static int get_url(void)
b35f73
+{
b35f73
+  int i;
b35f73
+  CURLUcode rc;
b35f73
+  int error = 0;
b35f73
+  for(i = 0; get_url_list[i].in && !error; i++) {
b35f73
+    char *url = NULL;
b35f73
+    CURLU *urlp = curl_url();
b35f73
+    if(!urlp) {
b35f73
+      error++;
b35f73
+      break;
b35f73
+    }
b35f73
+    rc = curl_url_set(urlp, CURLUPART_URL, get_url_list[i].in,
b35f73
+                      get_url_list[i].urlflags);
b35f73
+    if(!rc) {
b35f73
+      rc = curl_url_get(urlp, CURLUPART_URL, &url, get_url_list[i].getflags);
b35f73
+
b35f73
+      if(rc) {
b35f73
+        fprintf(stderr, "%s:%d returned %d\n",
b35f73
+                __FILE__, __LINE__, (int)rc);
b35f73
+        error++;
b35f73
+      }
b35f73
+      else {
b35f73
+        if(checkurl(url, get_url_list[i].out)) {
b35f73
+          error++;
b35f73
+        }
b35f73
+      }
b35f73
+    }
b35f73
+    else if(rc != get_url_list[i].ucode) {
b35f73
+      fprintf(stderr, "Get URL\nin: %s\nreturned %d (expected %d)\n",
b35f73
+              get_url_list[i].in, (int)rc, get_url_list[i].ucode);
b35f73
+      error++;
b35f73
+    }
b35f73
+    curl_free(url);
b35f73
+    curl_url_cleanup(urlp);
b35f73
+  }
b35f73
+  return error;
b35f73
+}
b35f73
+
b35f73
+static int get_parts(void)
b35f73
+{
b35f73
+  int i;
b35f73
+  CURLUcode rc;
b35f73
+  CURLU *urlp;
b35f73
+  int error = 0;
b35f73
+  for(i = 0; get_parts_list[i].in && !error; i++) {
b35f73
+    urlp = curl_url();
b35f73
+    if(!urlp) {
b35f73
+      error++;
b35f73
+      break;
b35f73
+    }
b35f73
+    rc = curl_url_set(urlp, CURLUPART_URL,
b35f73
+                      get_parts_list[i].in,
b35f73
+                      get_parts_list[i].urlflags);
b35f73
+    if(rc != get_parts_list[i].ucode) {
b35f73
+      fprintf(stderr, "Get parts\nin: %s\nreturned %d (expected %d)\n",
b35f73
+              get_parts_list[i].in, (int)rc, get_parts_list[i].ucode);
b35f73
+      error++;
b35f73
+    }
b35f73
+    else if(get_parts_list[i].ucode) {
b35f73
+      /* the expected error happened */
b35f73
+    }
b35f73
+    else if(checkparts(urlp, get_parts_list[i].in, get_parts_list[i].out,
b35f73
+                       get_parts_list[i].getflags))
b35f73
+      error++;
b35f73
+    curl_url_cleanup(urlp);
b35f73
+  }
b35f73
+  return error;
b35f73
+}
b35f73
+
b35f73
+static struct querycase append_list[] = {
b35f73
+  {"HTTP://test/?s", "name=joe\x02", "http://test/?s&name=joe%02",
b35f73
+   0, CURLU_URLENCODE, CURLUE_OK},
b35f73
+  {"HTTP://test/?size=2#f", "name=joe=", "http://test/?size=2&name=joe=#f",
b35f73
+   0, CURLU_URLENCODE, CURLUE_OK},
b35f73
+  {"HTTP://test/?size=2#f", "name=joe doe",
b35f73
+   "http://test/?size=2&name=joe+doe#f",
b35f73
+   0, CURLU_URLENCODE, CURLUE_OK},
b35f73
+  {"HTTP://test/", "name=joe", "http://test/?name=joe", 0, 0, CURLUE_OK},
b35f73
+  {"HTTP://test/?size=2", "name=joe", "http://test/?size=2&name=joe",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"HTTP://test/?size=2&", "name=joe", "http://test/?size=2&name=joe",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {"HTTP://test/?size=2#f", "name=joe", "http://test/?size=2&name=joe#f",
b35f73
+   0, 0, CURLUE_OK},
b35f73
+  {NULL, NULL, NULL, 0, 0, 0}
b35f73
+};
b35f73
+
b35f73
+static int append(void)
b35f73
+{
b35f73
+  int i;
b35f73
+  CURLUcode rc;
b35f73
+  CURLU *urlp;
b35f73
+  int error = 0;
b35f73
+  for(i = 0; append_list[i].in && !error; i++) {
b35f73
+    urlp = curl_url();
b35f73
+    if(!urlp) {
b35f73
+      error++;
b35f73
+      break;
b35f73
+    }
b35f73
+    rc = curl_url_set(urlp, CURLUPART_URL,
b35f73
+                      append_list[i].in,
b35f73
+                      append_list[i].urlflags);
b35f73
+    if(rc)
b35f73
+      error++;
b35f73
+    else
b35f73
+      rc = curl_url_set(urlp, CURLUPART_QUERY,
b35f73
+                        append_list[i].q,
b35f73
+                        append_list[i].qflags | CURLU_APPENDQUERY);
b35f73
+    if(error)
b35f73
+      ;
b35f73
+    else if(rc != append_list[i].ucode) {
b35f73
+      fprintf(stderr, "Append\nin: %s\nreturned %d (expected %d)\n",
b35f73
+              append_list[i].in, (int)rc, append_list[i].ucode);
b35f73
+      error++;
b35f73
+    }
b35f73
+    else if(append_list[i].ucode) {
b35f73
+      /* the expected error happened */
b35f73
+    }
b35f73
+    else {
b35f73
+      char *url;
b35f73
+      rc = curl_url_get(urlp, CURLUPART_URL, &url, 0);
b35f73
+      if(rc) {
b35f73
+        fprintf(stderr, "%s:%d Get URL returned %d\n",
b35f73
+                __FILE__, __LINE__, (int)rc);
b35f73
+        error++;
b35f73
+      }
b35f73
+      else {
b35f73
+        if(checkurl(url, append_list[i].out)) {
b35f73
+          error++;
b35f73
+        }
b35f73
+        curl_free(url);
b35f73
+      }
b35f73
+    }
b35f73
+    curl_url_cleanup(urlp);
b35f73
+  }
b35f73
+  return error;
b35f73
+}
b35f73
+
b35f73
+int test(char *URL)
b35f73
+{
b35f73
+  (void)URL; /* not used */
b35f73
+
b35f73
+  if(append())
b35f73
+    return 5;
b35f73
+
b35f73
+  if(set_url())
b35f73
+    return 1;
b35f73
+
b35f73
+  if(set_parts())
b35f73
+    return 2;
b35f73
+
b35f73
+  if(get_url())
b35f73
+    return 3;
b35f73
+
b35f73
+  if(get_parts())
b35f73
+    return 4;
b35f73
+
b35f73
+  printf("success\n");
b35f73
+  return 0;
b35f73
+}
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 581a3b902b949f090776c5295a8aa0786edba773 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Sat, 8 Sep 2018 16:02:25 +0200
b35f73
Subject: [PATCH 02/14] curl_url-docs: fix AVAILABILITY as Added in curl 7.62.0
b35f73
b35f73
Upstream-commit: 890eea5aade0fc4ee167e83948d53351c11dd1ae
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/curl_url.3         | 2 +-
b35f73
 docs/libcurl/curl_url_cleanup.3 | 2 +-
b35f73
 docs/libcurl/curl_url_dup.3     | 2 +-
b35f73
 docs/libcurl/curl_url_get.3     | 2 +-
b35f73
 docs/libcurl/curl_url_set.3     | 2 +-
b35f73
 5 files changed, 5 insertions(+), 5 deletions(-)
b35f73
b35f73
diff --git a/docs/libcurl/curl_url.3 b/docs/libcurl/curl_url.3
b35f73
index 0a56264..a14c45b 100644
b35f73
--- a/docs/libcurl/curl_url.3
b35f73
+++ b/docs/libcurl/curl_url.3
b35f73
@@ -55,7 +55,7 @@ Returns a \fBCURLU *\fP if successful, or NULL if out of memory.
b35f73
   }
b35f73
 .fi
b35f73
 .SH AVAILABILITY
b35f73
-Added in curl 7.63.0
b35f73
+Added in curl 7.62.0
b35f73
 .SH "SEE ALSO"
b35f73
 .BR curl_url_cleanup "(3), " curl_url_get "(3), " curl_url_set "(3), "
b35f73
 .BR curl_url_dup "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_cleanup.3 b/docs/libcurl/curl_url_cleanup.3
b35f73
index a8158b7..4d095a9 100644
b35f73
--- a/docs/libcurl/curl_url_cleanup.3
b35f73
+++ b/docs/libcurl/curl_url_cleanup.3
b35f73
@@ -38,7 +38,7 @@ none
b35f73
   curl_url_cleanup(url);
b35f73
 .fi
b35f73
 .SH AVAILABILITY
b35f73
-Added in curl 7.63.0
b35f73
+Added in curl 7.62.0
b35f73
 .SH "SEE ALSO"
b35f73
 .BR curl_url_dup "(3), " curl_url "(3), " curl_url_set "(3), "
b35f73
 .BR curl_url_get "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_dup.3 b/docs/libcurl/curl_url_dup.3
b35f73
index 4815dbd..c0259e0 100644
b35f73
--- a/docs/libcurl/curl_url_dup.3
b35f73
+++ b/docs/libcurl/curl_url_dup.3
b35f73
@@ -46,7 +46,7 @@ Returns a new handle or NULL if out of memory.
b35f73
   curl_url_cleanup(url);
b35f73
 .fi
b35f73
 .SH AVAILABILITY
b35f73
-Added in curl 7.63.0
b35f73
+Added in curl 7.62.0
b35f73
 .SH "SEE ALSO"
b35f73
 .BR curl_url_cleanup "(3), " curl_url "(3), " curl_url_set "(3), "
b35f73
 .BR curl_url_get "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_get.3 b/docs/libcurl/curl_url_get.3
b35f73
index 824d496..b1313ea 100644
b35f73
--- a/docs/libcurl/curl_url_get.3
b35f73
+++ b/docs/libcurl/curl_url_get.3
b35f73
@@ -104,7 +104,7 @@ If this function returns an error, no URL part is returned.
b35f73
   }
b35f73
 .fi
b35f73
 .SH AVAILABILITY
b35f73
-Added in curl 7.63.0
b35f73
+Added in curl 7.62.0
b35f73
 .SH "SEE ALSO"
b35f73
 .BR curl_url_cleanup "(3), " curl_url "(3), " curl_url_set "(3), "
b35f73
 .BR curl_url_dup "(3), "
b35f73
diff --git a/docs/libcurl/curl_url_set.3 b/docs/libcurl/curl_url_set.3
b35f73
index 75fc0d9..79272e8 100644
b35f73
--- a/docs/libcurl/curl_url_set.3
b35f73
+++ b/docs/libcurl/curl_url_set.3
b35f73
@@ -114,7 +114,7 @@ If this function returns an error, no URL part is returned.
b35f73
   curl_url_cleanup(url);
b35f73
 .fi
b35f73
 .SH AVAILABILITY
b35f73
-Added in curl 7.63.0
b35f73
+Added in curl 7.62.0
b35f73
 .SH "SEE ALSO"
b35f73
 .BR curl_url_cleanup "(3), " curl_url "(3), " curl_url_get "(3), "
b35f73
 .BR curl_url_dup "(3), "
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 9c33cac88a9d94557ba48df7c290afc950895bc4 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Sat, 8 Sep 2018 19:39:57 +0200
b35f73
Subject: [PATCH 03/14] curl_url_set.3: correct description
b35f73
b35f73
Upstream-commit: 8b85a3cac516a302a8ce3911cf8b9a229b62a59d
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/curl_url_set.3 | 2 +-
b35f73
 1 file changed, 1 insertion(+), 1 deletion(-)
b35f73
b35f73
diff --git a/docs/libcurl/curl_url_set.3 b/docs/libcurl/curl_url_set.3
b35f73
index 79272e8..0d6e9aa 100644
b35f73
--- a/docs/libcurl/curl_url_set.3
b35f73
+++ b/docs/libcurl/curl_url_set.3
b35f73
@@ -21,7 +21,7 @@
b35f73
 .\" **************************************************************************
b35f73
 .TH curl_url_set 3 "6 Aug 2018" "libcurl" "libcurl Manual"
b35f73
 .SH NAME
b35f73
-curl_url_set - set a part from a URL
b35f73
+curl_url_set - set a URL part
b35f73
 .SH SYNOPSIS
b35f73
 .B #include <curl/curl.h>
b35f73
 
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From dc2c1d978ec78a5f278d194e1b258015e8bfd664 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Sat, 8 Sep 2018 22:57:36 +0200
b35f73
Subject: [PATCH 04/14] urlapi: avoid derefencing a possible NULL pointer
b35f73
b35f73
Coverity CID 1439134
b35f73
b35f73
Upstream-commit: 01dedc99fc8d386fe955421ab05a1c4094c9190b
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 lib/urlapi.c | 4 ++--
b35f73
 1 file changed, 2 insertions(+), 2 deletions(-)
b35f73
b35f73
diff --git a/lib/urlapi.c b/lib/urlapi.c
b35f73
index 8287861..3183598 100644
b35f73
--- a/lib/urlapi.c
b35f73
+++ b/lib/urlapi.c
b35f73
@@ -438,10 +438,10 @@ static CURLUcode parse_hostname_login(struct Curl_URL *u,
b35f73
   *hostname = ++ptr;
b35f73
 
b35f73
   /* We could use the login information in the URL so extract it. Only parse
b35f73
-     options if the handler says we should. */
b35f73
+     options if the handler says we should. Note that 'h' might be NULL! */
b35f73
   ccode = Curl_parse_login_details(login, ptr - login - 1,
b35f73
                                    &userp, &passwdp,
b35f73
-                                   h->flags & PROTOPT_URLOPTIONS ?
b35f73
+                                   (h && (h->flags & PROTOPT_URLOPTIONS)) ?
b35f73
                                    &optionsp:NULL);
b35f73
   if(ccode) {
b35f73
     result = CURLUE_MALFORMED_INPUT;
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 6684d372c20609afd21f21399deda6deedea911e Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Gustafsson <daniel@yesql.se>
b35f73
Date: Sat, 8 Sep 2018 23:05:21 +0200
b35f73
Subject: [PATCH 05/14] url.c: fix comment typo and indentation
b35f73
b35f73
Closes #2960
b35f73
b35f73
Upstream-commit: 6e4b8c5073c3985cef98656c3b375981d25a8898
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 lib/url.c | 4 ++--
b35f73
 1 file changed, 2 insertions(+), 2 deletions(-)
b35f73
b35f73
diff --git a/lib/url.c b/lib/url.c
b35f73
index 4f75f11..dcc6cc8 100644
b35f73
--- a/lib/url.c
b35f73
+++ b/lib/url.c
b35f73
@@ -1939,7 +1939,7 @@ static struct connectdata *allocate_conn(struct Curl_easy *data)
b35f73
   return NULL;
b35f73
 }
b35f73
 
b35f73
-/* returns the handdler if the given scheme is built-in */
b35f73
+/* returns the handler if the given scheme is built-in */
b35f73
 const struct Curl_handler *Curl_builtin_scheme(const char *scheme)
b35f73
 {
b35f73
   const struct Curl_handler * const *pp;
b35f73
@@ -2245,7 +2245,7 @@ static CURLcode parseurlandfillconn(struct Curl_easy *data,
b35f73
        the host-name part */
b35f73
     memmove(path + hostlen + 1, path, pathlen + 1);
b35f73
 
b35f73
-     /* now copy the trailing host part in front of the existing path */
b35f73
+    /* now copy the trailing host part in front of the existing path */
b35f73
     memcpy(path + 1, query, hostlen);
b35f73
 
b35f73
     path[0]='/'; /* prepend the missing slash */
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 0f8d6ab26abd00459d1364a69d7771a6b3a58ce3 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Mon, 10 Sep 2018 10:09:18 +0200
b35f73
Subject: [PATCH 06/14] libcurl-url.3: overview man page for the URL API
b35f73
b35f73
Closes #2967
b35f73
b35f73
Upstream-commit: 11e8a43f853b9bf050db58f073e6f2411821ce60
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/Makefile.inc  |   1 +
b35f73
 docs/libcurl/libcurl-url.3 | 137 +++++++++++++++++++++++++++++++++++++
b35f73
 2 files changed, 138 insertions(+)
b35f73
 create mode 100644 docs/libcurl/libcurl-url.3
b35f73
b35f73
diff --git a/docs/libcurl/Makefile.inc b/docs/libcurl/Makefile.inc
b35f73
index 955492c..97cb50c 100644
b35f73
--- a/docs/libcurl/Makefile.inc
b35f73
+++ b/docs/libcurl/Makefile.inc
b35f73
@@ -23,4 +23,5 @@ man_MANS = curl_easy_cleanup.3 curl_easy_getinfo.3 curl_easy_init.3      \
b35f73
   curl_mime_filename.3 curl_mime_subparts.3                              \
b35f73
   curl_mime_type.3 curl_mime_headers.3 curl_mime_encoder.3 libcurl-env.3 \
b35f73
   curl_url.3 curl_url_cleanup.3 curl_url_dup.3 curl_url_get.3 curl_url_set.3 \
b35f73
+  libcurl-url.3 \
b35f73
   libcurl-security.3
b35f73
diff --git a/docs/libcurl/libcurl-url.3 b/docs/libcurl/libcurl-url.3
b35f73
new file mode 100644
b35f73
index 0000000..4ad0a15
b35f73
--- /dev/null
b35f73
+++ b/docs/libcurl/libcurl-url.3
b35f73
@@ -0,0 +1,137 @@
b35f73
+.\" **************************************************************************
b35f73
+.\" *                                  _   _ ____  _
b35f73
+.\" *  Project                     ___| | | |  _ \| |
b35f73
+.\" *                             / __| | | | |_) | |
b35f73
+.\" *                            | (__| |_| |  _ <| |___
b35f73
+.\" *                             \___|\___/|_| \_\_____|
b35f73
+.\" *
b35f73
+.\" * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+.\" *
b35f73
+.\" * This software is licensed as described in the file COPYING, which
b35f73
+.\" * you should have received as part of this distribution. The terms
b35f73
+.\" * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+.\" *
b35f73
+.\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+.\" * copies of the Software, and permit persons to whom the Software is
b35f73
+.\" * furnished to do so, under the terms of the COPYING file.
b35f73
+.\" *
b35f73
+.\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+.\" * KIND, either express or implied.
b35f73
+.\" *
b35f73
+.\" **************************************************************************
b35f73
+.TH libcurl 3 "10 Sep 2018" "libcurl" "libcurl url interface"
b35f73
+.SH NAME
b35f73
+libcurl-url \- URL interface overview
b35f73
+.SH DESCRIPTION
b35f73
+The URL interface provides a set of functions for parsing and generating URLs.
b35f73
+.SH INCLUDE
b35f73
+You still only include <curl/curl.h> in your code. Note that the URL API was
b35f73
+introduced in 7.62.0.
b35f73
+.SH CREATE
b35f73
+Create a handle that holds URL info and resources with \fIcurl_url(3)\fP:
b35f73
+
b35f73
+  CURLU *h = curl_url();
b35f73
+.SH CLEANUP
b35f73
+When done with it, clean it up with \fIcurl_url_cleanup(3)\fP:
b35f73
+
b35f73
+  curl_url_cleanup(h);
b35f73
+.SH DUPLICATE
b35f73
+When you need a copy of a handle, just duplicate it with \fIcurl_url_dup(3)\fP:
b35f73
+
b35f73
+  CURLU *nh = curl_url_dup(h);
b35f73
+.SH PARSING
b35f73
+By "setting" a URL to the handle with \fIcurl_url_set(3)\fP, the URL is parsed
b35f73
+and stored in the handle. If the URL is not syntactically correct it will
b35f73
+return an error instead.
b35f73
+
b35f73
+.nf
b35f73
+  rc = curl_url_set(h, CURLUPART_URL,
b35f73
+                    "https://example.com:449/foo/bar?name=moo", 0);
b35f73
+.fi
b35f73
+
b35f73
+The zero in the fourth argument is a bitmask for changing specific features.
b35f73
+
b35f73
+If successful, this stores the URL in its individual parts within the handle.
b35f73
+.SH REDIRECT
b35f73
+When a handle already contains info about a URL, setting a relative URL will
b35f73
+make it "redirect" to adapt to it.
b35f73
+
b35f73
+  rc = curl_url_set(h, CURLUPART_URL, "../test?another", 0);
b35f73
+.SH "GET URL"
b35f73
+The `CURLU` handle represents a URL and you can easily extract that with
b35f73
+\fIcurl_url_get(3)\fP:
b35f73
+
b35f73
+  char *url;
b35f73
+  rc = curl_url_get(h, CURLUPART_URL, &url, 0);
b35f73
+  curl_free(url);
b35f73
+
b35f73
+The zero in the fourth argument is a bitmask for changing specific features.
b35f73
+.SH "GET PARTS"
b35f73
+When a URL has been parsed or parts have been set, you can extract those
b35f73
+pieces from the handle at any time.
b35f73
+
b35f73
+.nf
b35f73
+  rc = curl_url_get(h, CURLUPART_HOST, &host, 0);
b35f73
+  rc = curl_url_get(h, CURLUPART_SCHEME, &scheme, 0);
b35f73
+  rc = curl_url_get(h, CURLUPART_USER, &user, 0);
b35f73
+  rc = curl_url_get(h, CURLUPART_PASSWORD, &password, 0);
b35f73
+  rc = curl_url_get(h, CURLUPART_PORT, &port, 0);
b35f73
+  rc = curl_url_get(h, CURLUPART_PATH, &path, 0);
b35f73
+  rc = curl_url_get(h, CURLUPART_QUERY, &query, 0);
b35f73
+  rc = curl_url_get(h, CURLUPART_FRAGMENT, &fragment, 0);
b35f73
+.fi
b35f73
+
b35f73
+Extracted parts are not URL decoded unless the user also asks for it with the
b35f73
+CURLU_URLDECODE flag set in the fourth bitmask argument.
b35f73
+
b35f73
+Remember to free the returned string with \fIcurl_free(3)\fP when you're done
b35f73
+with it!
b35f73
+.SH "SET PARTS"
b35f73
+A user set individual URL parts, either after having parsed a full URL or
b35f73
+instead of parsing such.
b35f73
+
b35f73
+.nf
b35f73
+  rc = curl_url_set(urlp, CURLUPART_HOST, "www.example.com", 0);
b35f73
+  rc = curl_url_set(urlp, CURLUPART_SCHEME, "https", 0);
b35f73
+  rc = curl_url_set(urlp, CURLUPART_USER, "john", 0);
b35f73
+  rc = curl_url_set(urlp, CURLUPART_PASSWORD, "doe", 0);
b35f73
+  rc = curl_url_set(urlp, CURLUPART_PORT, "443", 0);
b35f73
+  rc = curl_url_set(urlp, CURLUPART_PATH, "/index.html", 0);
b35f73
+  rc = curl_url_set(urlp, CURLUPART_QUERY, "name=john", 0);
b35f73
+  rc = curl_url_set(urlp, CURLUPART_FRAGMENT, "anchor", 0);
b35f73
+.fi
b35f73
+
b35f73
+Set parts are not URL encoded unless the user asks for it with the
b35f73
+`CURLU_URLENCODE` flag.
b35f73
+.SH "APPENDQUERY"
b35f73
+An application can append a string to the right end of the query part with the
b35f73
+`CURLU_APPENDQUERY` flag to \fIcurl_url_set(3)\fP.
b35f73
+
b35f73
+Imagine a handle that holds the URL `https://example.com/?shoes=2`. An
b35f73
+application can then add the string `hat=1` to the query part like this:
b35f73
+
b35f73
+.nf
b35f73
+  rc = curl_url_set(urlp, CURLUPART_QUERY, "hat=1", CURLU_APPENDQUERY);
b35f73
+.fi
b35f73
+
b35f73
+It will even notice the lack of an ampersand (`&`) separator so it will inject
b35f73
+one too, and the handle's full URL will then equal
b35f73
+`https://example.com/?shoes=2&hat=1`.
b35f73
+
b35f73
+The appended string can of course also get URL encoded on add, and if asked to
b35f73
+URL encode, the encoding process will skip the '=' character. For example,
b35f73
+append `candy=N&N` to what we already have, and URL encode it to deal with the
b35f73
+ampersand in the data:
b35f73
+
b35f73
+.nf
b35f73
+  rc = curl_url_set(urlp, CURLUPART_QUERY, "candy=N&N",
b35f73
+                    CURLU_APPENDQUERY | CURLU_URLENCODE);
b35f73
+.fi
b35f73
+
b35f73
+Now the URL looks like
b35f73
+.nf
b35f73
+  https://example.com/?shoes=2&hat=1&candy=N%26N`
b35f73
+.fi
b35f73
+.SH "SEE ALSO"
b35f73
+.BR curl_url "(3), " curl_url_cleanup "(3), " curl_url_get "(3), "
b35f73
+.BR curl_url_dup "(3), " curl_url_set "(3), " CURLOPT_URL "(3), "
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 4c235b460cf40f8ce0c6ad06b44ecb4dddc128e4 Mon Sep 17 00:00:00 2001
b35f73
From: Dave Reisner <dreisner@archlinux.org>
b35f73
Date: Mon, 10 Sep 2018 09:38:46 -0400
b35f73
Subject: [PATCH 07/14] curl_url_set.3: fix typo in reference to
b35f73
 CURLU_APPENDQUERY
b35f73
b35f73
Upstream-commit: 04110573801feb2f278e2f774087a0525d5e8d0a
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/curl_url_set.3 | 2 +-
b35f73
 1 file changed, 1 insertion(+), 1 deletion(-)
b35f73
b35f73
diff --git a/docs/libcurl/curl_url_set.3 b/docs/libcurl/curl_url_set.3
b35f73
index 0d6e9aa..b2b273f 100644
b35f73
--- a/docs/libcurl/curl_url_set.3
b35f73
+++ b/docs/libcurl/curl_url_set.3
b35f73
@@ -75,7 +75,7 @@ If used in with \fICURLU_APPENDQUERY\fP, the provided part will be appended on
b35f73
 the end of the existing query - and if the previous part didn't end with an
b35f73
 ampersand (&), an ampersand will be inserted before the new appended part.
b35f73
 
b35f73
-When \fCURLU_APPENDQUERY\fP is used together with \fICURLU_URLENCODE\fP,
b35f73
+When \fICURLU_APPENDQUERY\fP is used together with \fICURLU_URLENCODE\fP,
b35f73
 the '=' symbols will not be URL encoded.
b35f73
 
b35f73
 The question mark in the URL is not part of the actual query contents.
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From fb07ea0cf9c612b2fad6a113b1d40aa7896fe43a Mon Sep 17 00:00:00 2001
b35f73
From: Dave Reisner <dreisner@archlinux.org>
b35f73
Date: Mon, 10 Sep 2018 09:39:33 -0400
b35f73
Subject: [PATCH 08/14] curl_url_set.3: properly escape \n in example code
b35f73
b35f73
This yields
b35f73
b35f73
  "the scheme is %s\n"
b35f73
b35f73
instead of
b35f73
b35f73
  "the scheme is %s0
b35f73
b35f73
Closes #2970
b35f73
b35f73
Upstream-commit: c1e5980f6672a2bd2d26894f093b435f2deb04e0
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/curl_url_get.3 | 2 +-
b35f73
 1 file changed, 1 insertion(+), 1 deletion(-)
b35f73
b35f73
diff --git a/docs/libcurl/curl_url_get.3 b/docs/libcurl/curl_url_get.3
b35f73
index b1313ea..53f7954 100644
b35f73
--- a/docs/libcurl/curl_url_get.3
b35f73
+++ b/docs/libcurl/curl_url_get.3
b35f73
@@ -97,7 +97,7 @@ If this function returns an error, no URL part is returned.
b35f73
     char *scheme;
b35f73
     rc = curl_url_get(url, CURLUPART_SCHEME, &scheme, 0);
b35f73
     if(!rc) {
b35f73
-      printf("the scheme is %s\n", scheme);
b35f73
+      printf("the scheme is %s\\n", scheme);
b35f73
       curl_free(scheme);
b35f73
     }
b35f73
     curl_url_cleanup(url);
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 376ae7de5a5a5f5b5513e6055700d010f21d4da3 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Gustafsson <daniel@yesql.se>
b35f73
Date: Wed, 19 Sep 2018 13:44:10 +0200
b35f73
Subject: [PATCH 09/14] urlapi: don't set value which is never read
b35f73
b35f73
In the CURLUPART_URL case, there is no codepath which invokes url
b35f73
decoding so remove the assignment of the urldecode variable. This
b35f73
fixes the deadstore bug-report from clang static analysis.
b35f73
b35f73
Closes #3015
b35f73
Reviewed-by: Daniel Stenberg <daniel@haxx.se>
b35f73
b35f73
Upstream-commit: 522e647cc52c45ebdb58d57f242204f9a72c45dd
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 lib/urlapi.c | 1 -
b35f73
 1 file changed, 1 deletion(-)
b35f73
b35f73
diff --git a/lib/urlapi.c b/lib/urlapi.c
b35f73
index 3183598..127f390 100644
b35f73
--- a/lib/urlapi.c
b35f73
+++ b/lib/urlapi.c
b35f73
@@ -970,7 +970,6 @@ CURLUcode curl_url_get(CURLU *u, CURLUPart what,
b35f73
     char *scheme;
b35f73
     char *options = u->options;
b35f73
     char *port = u->port;
b35f73
-    urldecode = FALSE; /* not for the whole thing */
b35f73
     if(u->scheme && strcasecompare("file", u->scheme)) {
b35f73
       url = aprintf("file://%s%s%s",
b35f73
                     u->path,
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 26dd137f3ca894e6402a98889d3b182f608d3c7f Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Wed, 19 Sep 2018 10:17:03 +0200
b35f73
Subject: [PATCH 10/14] urlapi: add CURLU_GUESS_SCHEME and fix hostname
b35f73
 acceptance
b35f73
b35f73
In order for this API to fully work for libcurl itself, it now offers a
b35f73
CURLU_GUESS_SCHEME flag that makes it "guess" scheme based on the host
b35f73
name prefix just like libcurl always did. If there's no known prefix, it
b35f73
will guess "http://".
b35f73
b35f73
Separately, it relaxes the check of the host name so that IDN host names
b35f73
can be passed in as well.
b35f73
b35f73
Both these changes are necessary for libcurl itself to use this API.
b35f73
b35f73
Assisted-by: Daniel Gustafsson
b35f73
Closes #3018
b35f73
b35f73
Upstream-commit: 9307c219ad4741db860b864c860ac2f8bf9fad9d
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/curl_url_set.3 | 10 ++++++++
b35f73
 include/curl/urlapi.h       |  1 +
b35f73
 lib/urlapi.c                | 48 ++++++++++++++++++++++++++++---------
b35f73
 tests/data/test1560         |  6 +++++
b35f73
 tests/libtest/lib1560.c     | 26 +++++++++++++++++++-
b35f73
 5 files changed, 79 insertions(+), 12 deletions(-)
b35f73
b35f73
diff --git a/docs/libcurl/curl_url_set.3 b/docs/libcurl/curl_url_set.3
b35f73
index b2b273f..95b76bd 100644
b35f73
--- a/docs/libcurl/curl_url_set.3
b35f73
+++ b/docs/libcurl/curl_url_set.3
b35f73
@@ -96,6 +96,16 @@ The query part gets space-to-plus conversion before the URL conversion.
b35f73
 
b35f73
 This URL encoding is charset unaware and will convert the input on a
b35f73
 byte-by-byte manner.
b35f73
+.IP CURLU_DEFAULT_SCHEME
b35f73
+If set, will make libcurl allow the URL to be set without a scheme and then
b35f73
+sets that to the default scheme: HTTPS. Overrides the \fICURLU_GUESS_SCHEME\fP
b35f73
+option if both are set.
b35f73
+.IP CURLU_GUESS_SCHEME
b35f73
+If set, will make libcurl allow the URL to be set without a scheme and it
b35f73
+instead "guesses" which scheme that was intended based on the host name.  If
b35f73
+the outermost sub-domain name matches DICT, FTP, IMAP, LDAP, POP3 or SMTP then
b35f73
+that scheme will be used, otherwise it picks HTTP. Conflicts with the
b35f73
+\fICURLU_DEFAULT_SCHEME\fP option which takes precendence if both are set.
b35f73
 .SH RETURN VALUE
b35f73
 Returns a CURLUcode error value, which is CURLUE_OK (0) if everything went
b35f73
 fine.
b35f73
diff --git a/include/curl/urlapi.h b/include/curl/urlapi.h
b35f73
index b16cfce..319de35 100644
b35f73
--- a/include/curl/urlapi.h
b35f73
+++ b/include/curl/urlapi.h
b35f73
@@ -75,6 +75,7 @@ typedef enum {
b35f73
 #define CURLU_URLDECODE (1<<6)          /* URL decode on get */
b35f73
 #define CURLU_URLENCODE (1<<7)          /* URL encode on set */
b35f73
 #define CURLU_APPENDQUERY (1<<8)        /* append a form style part */
b35f73
+#define CURLU_GUESS_SCHEME (1<<9)       /* legacy curl-style guessing */
b35f73
 
b35f73
 typedef struct Curl_URL CURLU;
b35f73
 
b35f73
diff --git a/lib/urlapi.c b/lib/urlapi.c
b35f73
index 127f390..45f1e14 100644
b35f73
--- a/lib/urlapi.c
b35f73
+++ b/lib/urlapi.c
b35f73
@@ -554,7 +554,7 @@ static CURLUcode junkscan(char *part)
b35f73
 
b35f73
 static CURLUcode hostname_check(char *hostname, unsigned int flags)
b35f73
 {
b35f73
-  const char *l; /* accepted characters */
b35f73
+  const char *l = NULL; /* accepted characters */
b35f73
   size_t len;
b35f73
   size_t hlen = strlen(hostname);
b35f73
   (void)flags;
b35f73
@@ -564,14 +564,21 @@ static CURLUcode hostname_check(char *hostname, unsigned int flags)
b35f73
     l = "0123456789abcdefABCDEF::.";
b35f73
     hlen -= 2;
b35f73
   }
b35f73
-  else /* % for URL escaped letters */
b35f73
-    l = "0123456789abcdefghijklimnopqrstuvwxyz-_.ABCDEFGHIJKLIMNOPQRSTUVWXYZ%";
b35f73
-
b35f73
-  len = strspn(hostname, l);
b35f73
-  if(hlen != len)
b35f73
-    /* hostname with bad content */
b35f73
-    return CURLUE_MALFORMED_INPUT;
b35f73
 
b35f73
+  if(l) {
b35f73
+    /* only valid letters are ok */
b35f73
+    len = strspn(hostname, l);
b35f73
+    if(hlen != len)
b35f73
+      /* hostname with bad content */
b35f73
+      return CURLUE_MALFORMED_INPUT;
b35f73
+  }
b35f73
+  else {
b35f73
+    /* letters from the second string is not ok */
b35f73
+    len = strcspn(hostname, " ");
b35f73
+    if(hlen != len)
b35f73
+      /* hostname with bad content */
b35f73
+      return CURLUE_MALFORMED_INPUT;
b35f73
+  }
b35f73
   return CURLUE_OK;
b35f73
 }
b35f73
 
b35f73
@@ -587,7 +594,7 @@ static CURLUcode seturl(const char *url, CURLU *u, unsigned int flags)
b35f73
   CURLUcode result;
b35f73
   bool url_has_scheme = FALSE;
b35f73
   char schemebuf[MAX_SCHEME_LEN];
b35f73
-  char *schemep;
b35f73
+  char *schemep = NULL;
b35f73
   size_t schemelen = 0;
b35f73
   size_t urllen;
b35f73
   const struct Curl_handler *h = NULL;
b35f73
@@ -723,9 +730,10 @@ static CURLUcode seturl(const char *url, CURLU *u, unsigned int flags)
b35f73
     else {
b35f73
       /* no scheme! */
b35f73
 
b35f73
-      if(!(flags & CURLU_DEFAULT_SCHEME))
b35f73
+      if(!(flags & (CURLU_DEFAULT_SCHEME|CURLU_GUESS_SCHEME)))
b35f73
         return CURLUE_MALFORMED_INPUT;
b35f73
-      schemep = (char *) DEFAULT_SCHEME;
b35f73
+      if(flags & CURLU_DEFAULT_SCHEME)
b35f73
+        schemep = (char *) DEFAULT_SCHEME;
b35f73
 
b35f73
       /*
b35f73
        * The URL was badly formatted, let's try without scheme specified.
b35f73
@@ -744,6 +752,24 @@ static CURLUcode seturl(const char *url, CURLU *u, unsigned int flags)
b35f73
     memcpy(hostname, hostp, len);
b35f73
     hostname[len] = 0;
b35f73
 
b35f73
+    if((flags & CURLU_GUESS_SCHEME) && !schemep) {
b35f73
+      /* legacy curl-style guess based on host name */
b35f73
+      if(checkprefix("ftp.", hostname))
b35f73
+        schemep = (char *)"ftp";
b35f73
+      else if(checkprefix("dict.", hostname))
b35f73
+        schemep = (char *)"dict";
b35f73
+      else if(checkprefix("ldap.", hostname))
b35f73
+        schemep = (char *)"ldap";
b35f73
+      else if(checkprefix("imap.", hostname))
b35f73
+        schemep = (char *)"imap";
b35f73
+      else if(checkprefix("smtp.", hostname))
b35f73
+        schemep = (char *)"smtp";
b35f73
+      else if(checkprefix("pop3.", hostname))
b35f73
+        schemep = (char *)"pop3";
b35f73
+      else
b35f73
+        schemep = (char *)"http";
b35f73
+    }
b35f73
+
b35f73
     len = strlen(p);
b35f73
     memcpy(path, p, len);
b35f73
     path[len] = 0;
b35f73
diff --git a/tests/data/test1560 b/tests/data/test1560
b35f73
index 720df03..4b6c97a 100644
b35f73
--- a/tests/data/test1560
b35f73
+++ b/tests/data/test1560
b35f73
@@ -16,6 +16,12 @@ none
b35f73
 file
b35f73
 https
b35f73
 http
b35f73
+pop3
b35f73
+smtp
b35f73
+imap
b35f73
+ldap
b35f73
+dict
b35f73
+ftp
b35f73
 </features>
b35f73
  <name>
b35f73
 URL API
b35f73
diff --git a/tests/libtest/lib1560.c b/tests/libtest/lib1560.c
b35f73
index 669ea9a..30fb582 100644
b35f73
--- a/tests/libtest/lib1560.c
b35f73
+++ b/tests/libtest/lib1560.c
b35f73
@@ -246,8 +246,32 @@ static struct testcase get_parts_list[] ={
b35f73
 };
b35f73
 
b35f73
 static struct urltestcase get_url_list[] = {
b35f73
+  {"smtp.example.com/path/html",
b35f73
+   "smtp://smtp.example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
+  {"https.example.com/path/html",
b35f73
+   "http://https.example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
+  {"dict.example.com/path/html",
b35f73
+   "dict://dict.example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
+  {"pop3.example.com/path/html",
b35f73
+   "pop3://pop3.example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
+  {"ldap.example.com/path/html",
b35f73
+   "ldap://ldap.example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
+  {"imap.example.com/path/html",
b35f73
+   "imap://imap.example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
+  {"ftp.example.com/path/html",
b35f73
+   "ftp://ftp.example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
+  {"example.com/path/html",
b35f73
+   "http://example.com/path/html",
b35f73
+   CURLU_GUESS_SCHEME, 0, CURLUE_OK},
b35f73
   {"HTTP://test/", "http://test/", 0, 0, CURLUE_OK},
b35f73
-  {"http://HO0_-st..~./", "", 0, 0, CURLUE_MALFORMED_INPUT},
b35f73
+  {"http://HO0_-st..~./", "http://HO0_-st..~./", 0, 0, CURLUE_OK},
b35f73
   {"http:/@example.com: 123/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
   {"http:/@example.com:123 /", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
   {"http:/@example.com:123a/", "", 0, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 4e335817d4ac0ee5596363004bfcaaad15bc6127 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Wed, 19 Sep 2018 11:28:40 +0200
b35f73
Subject: [PATCH 11/14] urlapi: document the error codes, remove two unused
b35f73
 ones
b35f73
b35f73
Assisted-by: Daniel Gustafsson
b35f73
Closes #3019
b35f73
b35f73
Upstream-commit: 5c73093edb3bd527db9c8abdee53d0f18e6a4cc1
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 docs/libcurl/libcurl-errors.3    | 37 ++++++++++++++++++++++++++++-
b35f73
 docs/libcurl/symbols-in-versions |  2 --
b35f73
 include/curl/urlapi.h            |  8 +++----
b35f73
 tests/libtest/lib1560.c          | 40 ++++++++++++++++----------------
b35f73
 4 files changed, 59 insertions(+), 28 deletions(-)
b35f73
b35f73
diff --git a/docs/libcurl/libcurl-errors.3 b/docs/libcurl/libcurl-errors.3
b35f73
index 30c57b3..411a272 100644
b35f73
--- a/docs/libcurl/libcurl-errors.3
b35f73
+++ b/docs/libcurl/libcurl-errors.3
b35f73
@@ -5,7 +5,7 @@
b35f73
 .\" *                            | (__| |_| |  _ <| |___
b35f73
 .\" *                             \___|\___/|_| \_\_____|
b35f73
 .\" *
b35f73
-.\" * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+.\" * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
 .\" *
b35f73
 .\" * This software is licensed as described in the file COPYING, which
b35f73
 .\" * you should have received as part of this distribution. The terms
b35f73
@@ -307,6 +307,41 @@ Not enough memory was available.
b35f73
 .IP "CURLSHE_NOT_BUILT_IN (5)"
b35f73
 The requested sharing could not be done because the library you use don't have
b35f73
 that particular feature enabled. (Added in 7.23.0)
b35f73
+.SH "CURLUcode"
b35f73
+.IP "CURLUE_BAD_HANDLE (1)"
b35f73
+An argument that should be a CURLU pointer was passed in as a NULL.
b35f73
+.IP "CURLUE_BAD_PARTPOINTER (2)"
b35f73
+A NULL pointer was passed to the 'part' argument of \fIcurl_url_get(3)\fP.
b35f73
+.IP "CURLUE_MALFORMED_INPUT (3)"
b35f73
+A malformed input was passed to a URL API function.
b35f73
+.IP "CURLUE_BAD_PORT_NUMBER (4)"
b35f73
+The port number was not a decimal number between 0 and 65535.
b35f73
+.IP "CURLUE_UNSUPPORTED_SCHEME (5)"
b35f73
+This libcurl build doesn't support the given URL scheme.
b35f73
+.IP "CURLUE_URLDECODE (6)"
b35f73
+URL decode error, most likely because of rubbish in the input.
b35f73
+.IP "CURLUE_OUT_OF_MEMORY (7)"
b35f73
+A memory function failed.
b35f73
+.IP "CURLUE_USER_NOT_ALLOWED (8)"
b35f73
+Credentials was passed in the URL when prohibited.
b35f73
+.IP "CURLUE_UNKNOWN_PART (9)"
b35f73
+An unknown part ID was passed to a URL API function.
b35f73
+.IP "CURLUE_NO_SCHEME (10)"
b35f73
+There is no scheme part in the URL.
b35f73
+.IP "CURLUE_NO_USER (11)"
b35f73
+There is no user part in the URL.
b35f73
+.IP "CURLUE_NO_PASSWORD (12)"
b35f73
+There is no password part in the URL.
b35f73
+.IP "CURLUE_NO_OPTIONS (13)"
b35f73
+There is no options part in the URL.
b35f73
+.IP "CURLUE_NO_HOST (14)"
b35f73
+There is no host part in the URL.
b35f73
+.IP "CURLUE_NO_PORT (15)"
b35f73
+There is no port part in the URL.
b35f73
+.IP "CURLUE_NO_QUERY (16)"
b35f73
+There is no query part in the URL.
b35f73
+.IP "CURLUE_NO_FRAGMENT (17)"
b35f73
+There is no fragment part in the URL.
b35f73
 .SH "SEE ALSO"
b35f73
 .BR curl_easy_strerror "(3), " curl_multi_strerror "(3), "
b35f73
 .BR curl_share_strerror "(3), " CURLOPT_ERRORBUFFER "(3), "
b35f73
diff --git a/docs/libcurl/symbols-in-versions b/docs/libcurl/symbols-in-versions
b35f73
index c797cb7..3b3861f 100644
b35f73
--- a/docs/libcurl/symbols-in-versions
b35f73
+++ b/docs/libcurl/symbols-in-versions
b35f73
@@ -736,14 +736,12 @@ CURLUE_NO_FRAGMENT              7.62.0
b35f73
 CURLUE_NO_HOST                  7.62.0
b35f73
 CURLUE_NO_OPTIONS               7.62.0
b35f73
 CURLUE_NO_PASSWORD              7.62.0
b35f73
-CURLUE_NO_PATH                  7.62.0
b35f73
 CURLUE_NO_PORT                  7.62.0
b35f73
 CURLUE_NO_QUERY                 7.62.0
b35f73
 CURLUE_NO_SCHEME                7.62.0
b35f73
 CURLUE_NO_USER                  7.62.0
b35f73
 CURLUE_OK                       7.62.0
b35f73
 CURLUE_OUT_OF_MEMORY            7.62.0
b35f73
-CURLUE_RELATIVE                 7.62.0
b35f73
 CURLUE_UNKNOWN_PART             7.62.0
b35f73
 CURLUE_UNSUPPORTED_SCHEME       7.62.0
b35f73
 CURLUE_URLDECODE                7.62.0
b35f73
diff --git a/include/curl/urlapi.h b/include/curl/urlapi.h
b35f73
index 319de35..90dd56c 100644
b35f73
--- a/include/curl/urlapi.h
b35f73
+++ b/include/curl/urlapi.h
b35f73
@@ -35,7 +35,7 @@ typedef enum {
b35f73
   CURLUE_BAD_PORT_NUMBER,     /* 4 */
b35f73
   CURLUE_UNSUPPORTED_SCHEME,  /* 5 */
b35f73
   CURLUE_URLDECODE,           /* 6 */
b35f73
-  CURLUE_RELATIVE,            /* 7 */
b35f73
+  CURLUE_OUT_OF_MEMORY,       /* 7 */
b35f73
   CURLUE_USER_NOT_ALLOWED,    /* 8 */
b35f73
   CURLUE_UNKNOWN_PART,        /* 9 */
b35f73
   CURLUE_NO_SCHEME,           /* 10 */
b35f73
@@ -44,10 +44,8 @@ typedef enum {
b35f73
   CURLUE_NO_OPTIONS,          /* 13 */
b35f73
   CURLUE_NO_HOST,             /* 14 */
b35f73
   CURLUE_NO_PORT,             /* 15 */
b35f73
-  CURLUE_NO_PATH,             /* 16 */
b35f73
-  CURLUE_NO_QUERY,            /* 17 */
b35f73
-  CURLUE_NO_FRAGMENT,         /* 18 */
b35f73
-  CURLUE_OUT_OF_MEMORY        /* 19 */
b35f73
+  CURLUE_NO_QUERY,            /* 16 */
b35f73
+  CURLUE_NO_FRAGMENT          /* 17 */
b35f73
 } CURLUcode;
b35f73
 
b35f73
 typedef enum {
b35f73
diff --git a/tests/libtest/lib1560.c b/tests/libtest/lib1560.c
b35f73
index 30fb582..224cb88 100644
b35f73
--- a/tests/libtest/lib1560.c
b35f73
+++ b/tests/libtest/lib1560.c
b35f73
@@ -129,7 +129,7 @@ struct querycase {
b35f73
 
b35f73
 static struct testcase get_parts_list[] ={
b35f73
   {"https://127.0.0.1:443",
b35f73
-   "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [17] | [18]",
b35f73
+   "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
b35f73
    0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
b35f73
   {"http://%3a:%3a@ex%0ample/%3f+?+%3f+%23#+%23%3f%g7",
b35f73
    "http | : | : | [13] | [6] | [15] | /?+ |  ? # | +#?%g7",
b35f73
@@ -138,43 +138,43 @@ static struct testcase get_parts_list[] ={
b35f73
    "http | %3a | %3a | [13] | ex%0ample | [15] | /%3f | %3f%35 | %35%3f%g7",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"http://HO0_-st%41/",
b35f73
-   "http | [11] | [12] | [13] | HO0_-st%41 | [15] | / | [17] | [18]",
b35f73
+   "http | [11] | [12] | [13] | HO0_-st%41 | [15] | / | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"file://hello.html",
b35f73
    "",
b35f73
    0, 0, CURLUE_MALFORMED_INPUT},
b35f73
   {"http://HO0_-st/",
b35f73
-   "http | [11] | [12] | [13] | HO0_-st | [15] | / | [17] | [18]",
b35f73
+   "http | [11] | [12] | [13] | HO0_-st | [15] | / | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"imap://user:pass;option@server/path",
b35f73
-   "imap | user | pass | option | server | [15] | /path | [17] | [18]",
b35f73
+   "imap | user | pass | option | server | [15] | /path | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"http://user:pass;option@server/path",
b35f73
-   "http | user | pass;option | [13] | server | [15] | /path | [17] | [18]",
b35f73
+   "http | user | pass;option | [13] | server | [15] | /path | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"file:/hello.html",
b35f73
-   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [17] | [18]",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"file://127.0.0.1/hello.html",
b35f73
-   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [17] | [18]",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"file:////hello.html",
b35f73
-   "file | [11] | [12] | [13] | [14] | [15] | //hello.html | [17] | [18]",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | //hello.html | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"file:///hello.html",
b35f73
-   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [17] | [18]",
b35f73
+   "file | [11] | [12] | [13] | [14] | [15] | /hello.html | [16] | [17]",
b35f73
    0, 0, CURLUE_OK},
b35f73
   {"https://127.0.0.1",
b35f73
-   "https | [11] | [12] | [13] | 127.0.0.1 | 443 | / | [17] | [18]",
b35f73
+   "https | [11] | [12] | [13] | 127.0.0.1 | 443 | / | [16] | [17]",
b35f73
    0, CURLU_DEFAULT_PORT, CURLUE_OK},
b35f73
   {"https://127.0.0.1",
b35f73
-   "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [17] | [18]",
b35f73
+   "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"https://[::1]:1234",
b35f73
-   "https | [11] | [12] | [13] | [::1] | 1234 | / | [17] | [18]",
b35f73
+   "https | [11] | [12] | [13] | [::1] | 1234 | / | [16] | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"https://127abc.com",
b35f73
-   "https | [11] | [12] | [13] | 127abc.com | [15] | / | [17] | [18]",
b35f73
+   "https | [11] | [12] | [13] | 127abc.com | [15] | / | [16] | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"https:// example.com?check",
b35f73
    "",
b35f73
@@ -183,7 +183,7 @@ static struct testcase get_parts_list[] ={
b35f73
    "",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_MALFORMED_INPUT},
b35f73
   {"https://example.com?check",
b35f73
-   "https | [11] | [12] | [13] | example.com | [15] | / | check | [18]",
b35f73
+   "https | [11] | [12] | [13] | example.com | [15] | / | check | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"https://example.com:65536",
b35f73
    "",
b35f73
@@ -193,27 +193,27 @@ static struct testcase get_parts_list[] ={
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_BAD_PORT_NUMBER},
b35f73
   {"https://example.com:01#moo",
b35f73
    "https | [11] | [12] | [13] | example.com | 1 | / | "
b35f73
-   "[17] | moo",
b35f73
+   "[16] | moo",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"https://example.com:1#moo",
b35f73
    "https | [11] | [12] | [13] | example.com | 1 | / | "
b35f73
-   "[17] | moo",
b35f73
+   "[16] | moo",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"http://example.com#moo",
b35f73
    "http | [11] | [12] | [13] | example.com | [15] | / | "
b35f73
-   "[17] | moo",
b35f73
+   "[16] | moo",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"http://example.com",
b35f73
    "http | [11] | [12] | [13] | example.com | [15] | / | "
b35f73
-   "[17] | [18]",
b35f73
+   "[16] | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"http://example.com/path/html",
b35f73
    "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
b35f73
-   "[17] | [18]",
b35f73
+   "[16] | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"http://example.com/path/html?query=name",
b35f73
    "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
b35f73
-   "query=name | [18]",
b35f73
+   "query=name | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"http://example.com/path/html?query=name#anchor",
b35f73
    "http | [11] | [12] | [13] | example.com | [15] | /path/html | "
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 88dfdac2fc1b34a321a323868ea06116c72fe6d2 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Fri, 21 Sep 2018 08:17:39 +0200
b35f73
Subject: [PATCH 12/14] urlapi: fix support for address scope in IPv6 numerical
b35f73
 addresses
b35f73
b35f73
Closes #3024
b35f73
b35f73
Upstream-commit: 2097cd515289581df5dfb6eeb5942d083a871fa4
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 lib/urlapi-int.h        | 4 ++++
b35f73
 lib/urlapi.c            | 8 ++------
b35f73
 tests/libtest/lib1560.c | 3 +++
b35f73
 3 files changed, 9 insertions(+), 6 deletions(-)
b35f73
b35f73
diff --git a/lib/urlapi-int.h b/lib/urlapi-int.h
b35f73
index 7ac09fd..a5bb8ea 100644
b35f73
--- a/lib/urlapi-int.h
b35f73
+++ b/lib/urlapi-int.h
b35f73
@@ -22,6 +22,10 @@
b35f73
  *
b35f73
  ***************************************************************************/
b35f73
 #include "curl_setup.h"
b35f73
+/* scheme is not URL encoded, the longest libcurl supported ones are 6
b35f73
+   letters */
b35f73
+#define MAX_SCHEME_LEN 8
b35f73
+
b35f73
 bool Curl_is_absolute_url(const char *url, char *scheme, size_t buflen);
b35f73
 char *Curl_concat_url(const char *base, const char *relurl);
b35f73
 size_t Curl_strlen_url(const char *url, bool relative);
b35f73
diff --git a/lib/urlapi.c b/lib/urlapi.c
b35f73
index 45f1e14..a12112e 100644
b35f73
--- a/lib/urlapi.c
b35f73
+++ b/lib/urlapi.c
b35f73
@@ -53,10 +53,6 @@ struct Curl_URL {
b35f73
 
b35f73
 #define DEFAULT_SCHEME "https"
b35f73
 
b35f73
-/* scheme is not URL encoded, the longest libcurl supported ones are 6
b35f73
-   letters */
b35f73
-#define MAX_SCHEME_LEN 8
b35f73
-
b35f73
 static void free_urlhandle(struct Curl_URL *u)
b35f73
 {
b35f73
   free(u->scheme);
b35f73
@@ -480,7 +476,7 @@ static CURLUcode parse_port(struct Curl_URL *u, char *hostname)
b35f73
   char endbracket;
b35f73
   int len;
b35f73
 
b35f73
-  if((1 == sscanf(hostname, "[%*45[0123456789abcdefABCDEF:.]%c%n",
b35f73
+  if((1 == sscanf(hostname, "[%*45[0123456789abcdefABCDEF:.%%]%c%n",
b35f73
                   &endbracket, &len)) &&
b35f73
      (']' == endbracket)) {
b35f73
     /* this is a RFC2732-style specified IP-address */
b35f73
@@ -561,7 +557,7 @@ static CURLUcode hostname_check(char *hostname, unsigned int flags)
b35f73
 
b35f73
   if(hostname[0] == '[') {
b35f73
     hostname++;
b35f73
-    l = "0123456789abcdefABCDEF::.";
b35f73
+    l = "0123456789abcdefABCDEF::.%";
b35f73
     hlen -= 2;
b35f73
   }
b35f73
 
b35f73
diff --git a/tests/libtest/lib1560.c b/tests/libtest/lib1560.c
b35f73
index 224cb88..7a5be81 100644
b35f73
--- a/tests/libtest/lib1560.c
b35f73
+++ b/tests/libtest/lib1560.c
b35f73
@@ -128,6 +128,9 @@ struct querycase {
b35f73
 };
b35f73
 
b35f73
 static struct testcase get_parts_list[] ={
b35f73
+  {"https://[::1%252]:1234",
b35f73
+   "https | [11] | [12] | [13] | [::1%252] | 1234 | / | [16] | [17]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"https://127.0.0.1:443",
b35f73
    "https | [11] | [12] | [13] | 127.0.0.1 | [15] | / | [16] | [17]",
b35f73
    0, CURLU_NO_DEFAULT_PORT, CURLUE_OK},
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 6c9f3f4bc604ba06a4f43807ace9189503a5e9fc Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Fri, 2 Nov 2018 15:11:16 +0100
b35f73
Subject: [PATCH 13/14] URL: fix IPv6 numeral address parser
b35f73
b35f73
Regression from 46e164069d1a52. Extended test 1560 to verify.
b35f73
b35f73
Reported-by: tpaukrt on github
b35f73
Fixes #3218
b35f73
Closes #3219
b35f73
b35f73
Upstream-commit: b28094833a971870fd8c07960b3b12bf6fbbaad3
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 lib/urlapi.c            | 8 ++++++--
b35f73
 tests/libtest/lib1560.c | 9 +++++++++
b35f73
 2 files changed, 15 insertions(+), 2 deletions(-)
b35f73
b35f73
diff --git a/lib/urlapi.c b/lib/urlapi.c
b35f73
index a12112e..8626052 100644
b35f73
--- a/lib/urlapi.c
b35f73
+++ b/lib/urlapi.c
b35f73
@@ -481,8 +481,12 @@ static CURLUcode parse_port(struct Curl_URL *u, char *hostname)
b35f73
      (']' == endbracket)) {
b35f73
     /* this is a RFC2732-style specified IP-address */
b35f73
     portptr = &hostname[len];
b35f73
-    if (*portptr != ':')
b35f73
-      return CURLUE_MALFORMED_INPUT;
b35f73
+    if(*portptr) {
b35f73
+      if(*portptr != ':')
b35f73
+        return CURLUE_MALFORMED_INPUT;
b35f73
+    }
b35f73
+    else
b35f73
+      portptr = NULL;
b35f73
   }
b35f73
   else
b35f73
     portptr = strchr(hostname, ':');
b35f73
diff --git a/tests/libtest/lib1560.c b/tests/libtest/lib1560.c
b35f73
index 7a5be81..483035c 100644
b35f73
--- a/tests/libtest/lib1560.c
b35f73
+++ b/tests/libtest/lib1560.c
b35f73
@@ -128,6 +128,15 @@ struct querycase {
b35f73
 };
b35f73
 
b35f73
 static struct testcase get_parts_list[] ={
b35f73
+  {"http://[fd00:a41::50]:8080",
b35f73
+   "http | [11] | [12] | [13] | [fd00:a41::50] | 8080 | / | [16] | [17]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://[fd00:a41::50]/",
b35f73
+   "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
+  {"http://[fd00:a41::50]",
b35f73
+   "http | [11] | [12] | [13] | [fd00:a41::50] | [15] | / | [16] | [17]",
b35f73
+   CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
   {"https://[::1%252]:1234",
b35f73
    "https | [11] | [12] | [13] | [::1%252] | 1234 | / | [16] | [17]",
b35f73
    CURLU_DEFAULT_SCHEME, 0, CURLUE_OK},
b35f73
-- 
b35f73
2.17.2
b35f73
b35f73
b35f73
From 9fa7298750c1d66331dc55a202277b131868c048 Mon Sep 17 00:00:00 2001
b35f73
From: Daniel Stenberg <daniel@haxx.se>
b35f73
Date: Wed, 2 Jan 2019 20:18:27 +0100
b35f73
Subject: [PATCH 14/14] xattr: strip credentials from any URL that is stored
b35f73
b35f73
Both user and password are cleared uncondtitionally.
b35f73
b35f73
Added unit test 1621 to verify.
b35f73
b35f73
Fixes #3423
b35f73
Closes #3433
b35f73
b35f73
Upstream-commit: 98e6629154044e4ab1ee7cff8351c7ebcb131e88
b35f73
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
b35f73
---
b35f73
 src/tool_xattr.c        | 63 +++++++++++++++++++++++++----
b35f73
 tests/data/Makefile.inc |  2 +-
b35f73
 tests/data/test1621     | 27 +++++++++++++
b35f73
 tests/unit/Makefile.inc |  6 ++-
b35f73
 tests/unit/unit1621.c   | 89 +++++++++++++++++++++++++++++++++++++++++
b35f73
 5 files changed, 177 insertions(+), 10 deletions(-)
b35f73
 create mode 100644 tests/data/test1621
b35f73
 create mode 100644 tests/unit/unit1621.c
b35f73
b35f73
diff --git a/src/tool_xattr.c b/src/tool_xattr.c
b35f73
index 92b99db..730381b 100644
b35f73
--- a/src/tool_xattr.c
b35f73
+++ b/src/tool_xattr.c
b35f73
@@ -5,7 +5,7 @@
b35f73
  *                            | (__| |_| |  _ <| |___
b35f73
  *                             \___|\___/|_| \_\_____|
b35f73
  *
b35f73
- * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
  *
b35f73
  * This software is licensed as described in the file COPYING, which
b35f73
  * you should have received as part of this distribution. The terms
b35f73
@@ -49,6 +49,46 @@ static const struct xattr_mapping {
b35f73
   { NULL,                  CURLINFO_NONE } /* last element, abort loop here */
b35f73
 };
b35f73
 
b35f73
+/* returns TRUE if a new URL is returned, that then needs to be freed */
b35f73
+/* @unittest: 1621 */
b35f73
+#ifdef UNITTESTS
b35f73
+bool stripcredentials(char **url);
b35f73
+#else
b35f73
+static
b35f73
+#endif
b35f73
+bool stripcredentials(char **url)
b35f73
+{
b35f73
+  CURLU *u;
b35f73
+  CURLUcode uc;
b35f73
+  char *nurl;
b35f73
+  u = curl_url();
b35f73
+  if(u) {
b35f73
+    uc = curl_url_set(u, CURLUPART_URL, *url, 0);
b35f73
+    if(uc)
b35f73
+      goto error;
b35f73
+
b35f73
+    uc = curl_url_set(u, CURLUPART_USER, NULL, 0);
b35f73
+    if(uc)
b35f73
+      goto error;
b35f73
+
b35f73
+    uc = curl_url_set(u, CURLUPART_PASSWORD, NULL, 0);
b35f73
+    if(uc)
b35f73
+      goto error;
b35f73
+
b35f73
+    uc = curl_url_get(u, CURLUPART_URL, &nurl, 0);
b35f73
+    if(uc)
b35f73
+      goto error;
b35f73
+
b35f73
+    curl_url_cleanup(u);
b35f73
+
b35f73
+    *url = nurl;
b35f73
+    return TRUE;
b35f73
+  }
b35f73
+  error:
b35f73
+  curl_url_cleanup(u);
b35f73
+  return FALSE;
b35f73
+}
b35f73
+
b35f73
 /* store metadata from the curl request alongside the downloaded
b35f73
  * file using extended attributes
b35f73
  */
b35f73
@@ -62,17 +102,24 @@ int fwrite_xattr(CURL *curl, int fd)
b35f73
     char *value = NULL;
b35f73
     CURLcode result = curl_easy_getinfo(curl, mappings[i].info, &value);
b35f73
     if(!result && value) {
b35f73
+      bool freeptr = FALSE;
b35f73
+      if(CURLINFO_EFFECTIVE_URL == mappings[i].info)
b35f73
+        freeptr = stripcredentials(&value);
b35f73
+      if(value) {
b35f73
 #ifdef HAVE_FSETXATTR_6
b35f73
-      err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
b35f73
+        err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0, 0);
b35f73
 #elif defined(HAVE_FSETXATTR_5)
b35f73
-      err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
b35f73
+        err = fsetxattr(fd, mappings[i].attr, value, strlen(value), 0);
b35f73
 #elif defined(__FreeBSD_version)
b35f73
-      err = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, mappings[i].attr, value,
b35f73
-                           strlen(value));
b35f73
-      /* FreeBSD's extattr_set_fd returns the length of the extended attribute
b35f73
-       */
b35f73
-      err = err < 0 ? err : 0;
b35f73
+        err = extattr_set_fd(fd, EXTATTR_NAMESPACE_USER, mappings[i].attr,
b35f73
+                             value, strlen(value));
b35f73
+        /* FreeBSD's extattr_set_fd returns the length of the extended
b35f73
+           attribute */
b35f73
+        err = err < 0 ? err : 0;
b35f73
 #endif
b35f73
+        if(freeptr)
b35f73
+          curl_free(value);
b35f73
+      }
b35f73
     }
b35f73
     i++;
b35f73
   }
b35f73
diff --git a/tests/data/Makefile.inc b/tests/data/Makefile.inc
b35f73
index dd38f89..6172b77 100644
b35f73
--- a/tests/data/Makefile.inc
b35f73
+++ b/tests/data/Makefile.inc
b35f73
@@ -182,7 +182,7 @@ test1560 \
b35f73
 \
b35f73
 test1590 \
b35f73
 test1600 test1601 test1602 test1603 test1604 test1605 test1606 test1607 \
b35f73
-test1608 test1609 \
b35f73
+test1608 test1609 test1621 \
b35f73
 \
b35f73
 test1700 test1701 test1702 \
b35f73
 \
b35f73
diff --git a/tests/data/test1621 b/tests/data/test1621
b35f73
new file mode 100644
b35f73
index 0000000..1117d1b
b35f73
--- /dev/null
b35f73
+++ b/tests/data/test1621
b35f73
@@ -0,0 +1,27 @@
b35f73
+<testcase>
b35f73
+<info>
b35f73
+<keywords>
b35f73
+unittest
b35f73
+stripcredentials
b35f73
+</keywords>
b35f73
+</info>
b35f73
+
b35f73
+#
b35f73
+# Client-side
b35f73
+<client>
b35f73
+<server>
b35f73
+none
b35f73
+</server>
b35f73
+<features>
b35f73
+unittest
b35f73
+https
b35f73
+</features>
b35f73
+ <name>
b35f73
+unit tests for stripcredentials from URL
b35f73
+ </name>
b35f73
+<tool>
b35f73
+unit1621
b35f73
+</tool>
b35f73
+</client>
b35f73
+
b35f73
+</testcase>
b35f73
diff --git a/tests/unit/Makefile.inc b/tests/unit/Makefile.inc
b35f73
index 8b1a607..82eaec7 100644
b35f73
--- a/tests/unit/Makefile.inc
b35f73
+++ b/tests/unit/Makefile.inc
b35f73
@@ -10,7 +10,7 @@ UNITPROGS = unit1300 unit1301 unit1302 unit1303 unit1304 unit1305 unit1307	\
b35f73
  unit1330 unit1394 unit1395 unit1396 unit1397 unit1398	\
b35f73
  unit1399	\
b35f73
  unit1600 unit1601 unit1602 unit1603 unit1604 unit1605 unit1606 unit1607 \
b35f73
- unit1608 unit1609
b35f73
+ unit1608 unit1609 unit1621
b35f73
 
b35f73
 unit1300_SOURCES = unit1300.c $(UNITFILES)
b35f73
 unit1300_CPPFLAGS = $(AM_CPPFLAGS)
b35f73
@@ -95,3 +95,7 @@ unit1608_CPPFLAGS = $(AM_CPPFLAGS)
b35f73
 
b35f73
 unit1609_SOURCES = unit1609.c $(UNITFILES)
b35f73
 unit1609_CPPFLAGS = $(AM_CPPFLAGS)
b35f73
+ 
b35f73
+unit1621_SOURCES = unit1621.c $(UNITFILES)
b35f73
+unit1621_CPPFLAGS = $(AM_CPPFLAGS)
b35f73
+unit1621_LDADD = $(top_builddir)/src/libcurltool.la $(top_builddir)/lib/libcurl.la
b35f73
diff --git a/tests/unit/unit1621.c b/tests/unit/unit1621.c
b35f73
new file mode 100644
b35f73
index 0000000..6e07b6e
b35f73
--- /dev/null
b35f73
+++ b/tests/unit/unit1621.c
b35f73
@@ -0,0 +1,89 @@
b35f73
+/***************************************************************************
b35f73
+ *                                  _   _ ____  _
b35f73
+ *  Project                     ___| | | |  _ \| |
b35f73
+ *                             / __| | | | |_) | |
b35f73
+ *                            | (__| |_| |  _ <| |___
b35f73
+ *                             \___|\___/|_| \_\_____|
b35f73
+ *
b35f73
+ * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
b35f73
+ *
b35f73
+ * This software is licensed as described in the file COPYING, which
b35f73
+ * you should have received as part of this distribution. The terms
b35f73
+ * are also available at https://curl.haxx.se/docs/copyright.html.
b35f73
+ *
b35f73
+ * You may opt to use, copy, modify, merge, publish, distribute and/or sell
b35f73
+ * copies of the Software, and permit persons to whom the Software is
b35f73
+ * furnished to do so, under the terms of the COPYING file.
b35f73
+ *
b35f73
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
b35f73
+ * KIND, either express or implied.
b35f73
+ *
b35f73
+ ***************************************************************************/
b35f73
+#include "curlcheck.h"
b35f73
+
b35f73
+#include "urldata.h"
b35f73
+#include "url.h"
b35f73
+
b35f73
+#include "memdebug.h" /* LAST include file */
b35f73
+
b35f73
+static CURLcode unit_setup(void)
b35f73
+{
b35f73
+  return CURLE_OK;
b35f73
+}
b35f73
+
b35f73
+static void unit_stop(void)
b35f73
+{
b35f73
+}
b35f73
+
b35f73
+#ifdef __MINGW32__
b35f73
+UNITTEST_START
b35f73
+{
b35f73
+  return 0;
b35f73
+}
b35f73
+UNITTEST_STOP
b35f73
+#else
b35f73
+
b35f73
+bool stripcredentials(char **url);
b35f73
+
b35f73
+struct checkthis {
b35f73
+  const char *input;
b35f73
+  const char *output;
b35f73
+};
b35f73
+
b35f73
+static struct checkthis tests[] = {
b35f73
+  { "ninja://foo@example.com", "ninja://foo@example.com" },
b35f73
+  { "https://foo@example.com", "https://example.com/" },
b35f73
+  { "https://localhost:45", "https://localhost:45/" },
b35f73
+  { "https://foo@localhost:45", "https://localhost:45/" },
b35f73
+  { "http://daniel:password@localhost", "http://localhost/" },
b35f73
+  { "http://daniel@localhost", "http://localhost/" },
b35f73
+  { "http://localhost/", "http://localhost/" },
b35f73
+  { NULL, NULL } /* end marker */
b35f73
+};
b35f73
+
b35f73
+UNITTEST_START
b35f73
+{
b35f73
+  bool cleanup;
b35f73
+  char *url;
b35f73
+  int i;
b35f73
+  int rc = 0;
b35f73
+
b35f73
+  for(i = 0; tests[i].input; i++) {
b35f73
+    url = (char *)tests[i].input;
b35f73
+    cleanup = stripcredentials(&url;;
b35f73
+    printf("Test %u got input \"%s\", output: \"%s\"\n",
b35f73
+           i, tests[i].input, url);
b35f73
+
b35f73
+    if(strcmp(tests[i].output, url)) {
b35f73
+      fprintf(stderr, "Test %u got input \"%s\", expected output \"%s\"\n"
b35f73
+              " Actual output: \"%s\"\n", i, tests[i].input, tests[i].output,
b35f73
+              url);
b35f73
+      rc++;
b35f73
+    }
b35f73
+    if(cleanup)
b35f73
+      curl_free(url);
b35f73
+  }
b35f73
+  return rc;
b35f73
+}
b35f73
+UNITTEST_STOP
b35f73
+#endif
b35f73
-- 
b35f73
2.17.2
b35f73