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

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