- Synchronize up to trunk's revision r57864.
[reactos.git] / dll / win32 / urlmon / uri.c
index 2aee38a..f12f826 100644 (file)
 #define NO_SHLWAPI_REG
 #include "shlwapi.h"
 
+#include "strsafe.h"
+
 #define UINT_MAX 0xffffffff
 #define USHORT_MAX 0xffff
 
+#define URI_DISPLAY_NO_ABSOLUTE_URI         0x1
+#define URI_DISPLAY_NO_DEFAULT_PORT_AUTH    0x2
+
 #define ALLOW_NULL_TERM_SCHEME          0x01
 #define ALLOW_NULL_TERM_USER_NAME       0x02
 #define ALLOW_NULL_TERM_PASSWORD        0x04
 #define SKIP_IP_FUTURE_CHECK            0x10
 #define IGNORE_PORT_DELIMITER           0x20
 
+#define RAW_URI_FORCE_PORT_DISP     0x1
+#define RAW_URI_CONVERT_TO_DOS_PATH 0x2
+
+#define COMBINE_URI_FORCE_FLAG_USE  0x1
+
 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
 
 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
 
 typedef struct {
-    const IUriVtbl  *lpIUriVtbl;
+    IUri                IUri_iface;
+    IUriBuilderFactory  IUriBuilderFactory_iface;
+
     LONG ref;
 
     BSTR            raw_uri;
@@ -47,7 +59,7 @@ typedef struct {
     WCHAR           *canon_uri;
     DWORD           canon_size;
     DWORD           canon_len;
-    BOOL            display_absolute;
+    BOOL            display_modifiers;
     DWORD           create_flags;
 
     INT             scheme_start;
@@ -62,7 +74,8 @@ typedef struct {
     DWORD           host_len;
     Uri_HOST_TYPE   host_type;
 
-    USHORT          port;
+    INT             port_offset;
+    DWORD           port;
     BOOL            has_port;
 
     INT             authority_start;
@@ -82,7 +95,7 @@ typedef struct {
 } Uri;
 
 typedef struct {
-    const IUriBuilderVtbl  *lpIUriBuilderVtbl;
+    IUriBuilder IUriBuilder_iface;
     LONG ref;
 
     Uri *uri;
@@ -245,11 +258,11 @@ static Uri *get_uri_obj(IUri *uri)
 }
 
 static inline BOOL is_alpha(WCHAR val) {
-       return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
+    return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
 }
 
 static inline BOOL is_num(WCHAR val) {
-       return (val >= '0' && val <= '9');
+    return (val >= '0' && val <= '9');
 }
 
 static inline BOOL is_drive_path(const WCHAR *str) {
@@ -337,6 +350,22 @@ static inline BOOL is_path_delim(WCHAR val) {
     return (!val || val == '#' || val == '?');
 }
 
+static inline BOOL is_slash(WCHAR c)
+{
+    return c == '/' || c == '\\';
+}
+
+static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
+    DWORD i;
+
+    for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
+        if(default_ports[i].scheme == scheme && default_ports[i].port)
+            return TRUE;
+    }
+
+    return FALSE;
+}
+
 /* List of schemes types Windows seems to expect to be hierarchical. */
 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
     return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
@@ -568,21 +597,6 @@ static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
     dest[2] = hexDigits[val & 0xf];
 }
 
-/* Scans the range of characters [str, end] and returns the last occurrence
- * of 'ch' or returns NULL.
- */
-static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
-    const WCHAR *ptr = end;
-
-    while(ptr >= str) {
-        if(*ptr == ch)
-            return ptr;
-        --ptr;
-    }
-
-    return NULL;
-}
-
 /* Attempts to parse the domain name from the host.
  *
  * This function also includes the Top-level Domain (TLD) name
@@ -590,10 +604,10 @@ static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
  * a valid domain name it will assign 'domain_start' the offset
  * into 'host' where the domain name starts.
  *
- * It's implied that if a domain name its range is implied to be
+ * It's implied that if there is a domain name its range is:
  * [host+domain_start, host+host_len).
  */
-static void find_domain_name(const WCHAR *host, DWORD host_len,
+void find_domain_name(const WCHAR *host, DWORD host_len,
                              INT *domain_start) {
     const WCHAR *last_tld, *sec_last_tld, *end;
 
@@ -607,12 +621,12 @@ static void find_domain_name(const WCHAR *host, DWORD host_len,
     if(host_len < 4)
         return;
 
-    last_tld = str_last_of(host, end, '.');
+    last_tld = memrchrW(host, '.', host_len);
     if(!last_tld)
         /* http://hostname -> has no domain name. */
         return;
 
-    sec_last_tld = str_last_of(host, last_tld-1, '.');
+    sec_last_tld = memrchrW(host, '.', last_tld-host);
     if(!sec_last_tld) {
         /* If the '.' is at the beginning of the host there
          * has to be at least 3 characters in the TLD for it
@@ -663,7 +677,7 @@ static void find_domain_name(const WCHAR *host, DWORD host_len,
         if(last_tld - (sec_last_tld+1) == 3) {
             for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
                 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
-                    const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
+                    const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
 
                     if(!domain)
                         *domain_start = 0;
@@ -681,7 +695,7 @@ static void find_domain_name(const WCHAR *host, DWORD host_len,
              * part of the TLD.
              *  Ex: www.google.fo.uk -> google.fo.uk as the domain name.
              */
-            const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
+            const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
 
             if(!domain)
                 *domain_start = 0;
@@ -703,8 +717,6 @@ static void find_domain_name(const WCHAR *host, DWORD host_len,
 /* Removes the dot segments from a hierarchical URIs path component. This
  * function performs the removal in place.
  *
- * This is a modified version of Qt's QUrl function "removeDotsFromPath".
- *
  * This function returns the new length of the path string.
  */
 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
@@ -714,48 +726,46 @@ static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
     DWORD len;
 
     while(in < end) {
-        /* A.  if the input buffer begins with a prefix of "/./" or "/.",
-         *     where "." is a complete path segment, then replace that
-         *     prefix with "/" in the input buffer; otherwise,
+        /* Move the first path segment in the input buffer to the end of
+         * the output buffer, and any subsequent characters up to, including
+         * the next "/" character (if any) or the end of the input buffer.
          */
-        if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
-            in += 2;
-            continue;
-        } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
-            *out++ = '/';
-            in += 2;
+        while(in < end && !is_slash(*in))
+            *out++ = *in++;
+        if(in == end)
             break;
-        }
+        *out++ = *in++;
 
-        /* B.  if the input buffer begins with a prefix of "/../" or "/..",
-         *     where ".." is a complete path segment, then replace that
-         *     prefix with "/" in the input buffer and remove the last
-         *     segment and its preceding "/" (if any) from the output
-         *     buffer; otherwise,
-         */
-        if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
-            while(out > path && *(--out) != '/');
+        while(in < end) {
+            if(*in != '.')
+                break;
+
+            /* Handle ending "/." */
+            if(in + 1 == end) {
+                ++in;
+                break;
+            }
+
+            /* Handle "/./" */
+            if(is_slash(in[1])) {
+                in += 2;
+                continue;
+            }
 
-            in += 3;
-            continue;
-        } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
-            while(out > path && *(--out) != '/');
+            /* If we don't have "/../" or ending "/.." */
+            if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
+                break;
 
-            if(*out == '/')
+            /* Find the slash preceding out pointer and move out pointer to it */
+            if(out > path+1 && is_slash(*--out))
+                --out;
+            while(out > path && !is_slash(*(--out)));
+            if(is_slash(*out))
                 ++out;
-
-            in += 3;
-            break;
+            in += 2;
+            if(in != end)
+                ++in;
         }
-
-        /* C.  move the first path segment in the input buffer to the end of
-         *     the output buffer, including the initial "/" character (if
-         *     any) and any subsequent characters up to, but not including,
-         *     the next "/" character or the end of the input buffer.
-         */
-        *out++ = *in++;
-        while(in < end && *in != '/')
-            *out++ = *in++;
     }
 
     len = out - path;
@@ -785,12 +795,12 @@ static INT find_file_extension(const WCHAR *path, DWORD path_len) {
  *
  * NOTES:
  *  Windows will expand an elision if the elision only represents 1 h16
- *  component of the URI.
+ *  component of the address.
  *
  *  Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
  *
  *  If the IPv6 address contains an IPv4 address, the IPv4 address is also
- *  considered for being included as part of an elision if all it's components
+ *  considered for being included as part of an elision if all its components
  *  are zeros.
  *
  *  Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
@@ -933,6 +943,19 @@ static DWORD ui2ipv4(WCHAR *dest, UINT address) {
     return ret;
 }
 
+static DWORD ui2str(WCHAR *dest, UINT value) {
+    static const WCHAR formatW[] = {'%','u',0};
+    DWORD ret = 0;
+
+    if(!dest) {
+        WCHAR tmp[11];
+        ret = sprintfW(tmp, formatW, value);
+    } else
+        ret = sprintfW(dest, formatW, value);
+
+    return ret;
+}
+
 /* Converts an h16 component (from an IPv6 address) into it's
  * numerical value.
  *
@@ -950,7 +973,7 @@ static USHORT h16tous(h16 component) {
     return ret;
 }
 
-/* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
+/* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
  *
  * This function assumes that the ipv6_address has already been validated.
  */
@@ -961,7 +984,7 @@ static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
     for(i = 0; i < address->h16_count; ++i) {
         if(address->elision) {
             if(address->components[i].str > address->elision && !already_passed_elision) {
-                /* Means we just passed the elision and need to add it's values to
+                /* Means we just passed the elision and need to add its values to
                  * 'number' before we do anything else.
                  */
                 DWORD j = 0;
@@ -979,7 +1002,6 @@ static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
     if(!already_passed_elision && address->elision) {
         for(i = 0; i < address->elision_size; i+=2)
             number[cur_component++] = 0;
-        already_passed_elision = TRUE;
     }
 
     if(address->ipv4) {
@@ -1316,19 +1338,13 @@ static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWO
 }
 
 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
-    const WCHAR *start = *ptr;
-
-    if(**ptr != ':')
-        return TRUE;
-
-    ++(*ptr);
     data->password = *ptr;
 
     while(**ptr != '@') {
         if(**ptr == '%') {
             if(!check_pct_encoded(ptr)) {
                 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
-                    *ptr = start;
+                    *ptr = data->password;
                     data->password = NULL;
                     return FALSE;
                 }
@@ -1337,7 +1353,7 @@ static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWO
         } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
             break;
         else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
-            *ptr = start;
+            *ptr = data->password;
             data->password = NULL;
             return FALSE;
         }
@@ -1376,12 +1392,15 @@ static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
         return;
     }
 
-    if(!parse_password(ptr, data, flags, 0)) {
-        *ptr = start;
-        data->username = NULL;
-        data->username_len = 0;
-        TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
-        return;
+    if(**ptr == ':') {
+        ++(*ptr);
+        if(!parse_password(ptr, data, flags, 0)) {
+            *ptr = start;
+            data->username = NULL;
+            data->username_len = 0;
+            TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
+            return;
+        }
     }
 
     if(**ptr != '@') {
@@ -1524,8 +1543,11 @@ static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags)
 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
     const BOOL has_start_bracket = **ptr == '[';
     const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
+    const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
     BOOL inside_brackets = has_start_bracket;
-    BOOL ignore_col = extras & IGNORE_PORT_DELIMITER;
+
+    /* res URIs don't have ports. */
+    BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
 
     /* We have to be careful with file schemes. */
     if(data->scheme_type == URL_SCHEME_FILE) {
@@ -1547,7 +1569,11 @@ static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWO
 
     data->host = *ptr;
 
-    while(!is_auth_delim(**ptr, known_scheme)) {
+    /* For res URIs, everything before the first '/' is
+     * considered the host.
+     */
+    while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
+          (is_res && **ptr && **ptr != '/')) {
         if(**ptr == ':' && !ignore_col) {
             /* We can ignore ':' if were inside brackets.*/
             if(!inside_brackets) {
@@ -1571,7 +1597,7 @@ static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWO
                     break;
                 }
             }
-        } else if(**ptr == '%' && known_scheme) {
+        } else if(**ptr == '%' && (known_scheme && !is_res)) {
             /* Has to be a legit % encoded value. */
             if(!check_pct_encoded(ptr)) {
                 *ptr = data->host;
@@ -1579,6 +1605,10 @@ static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWO
                 return FALSE;
             } else
                 continue;
+        } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
+            *ptr = data->host;
+            data->host = NULL;
+            return FALSE;
         } else if(**ptr == ']')
             inside_brackets = FALSE;
         else if(**ptr == '[')
@@ -1603,7 +1633,7 @@ static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWO
         data->host_len = *ptr - data->host;
 
     /* If the host is empty, then it's an unknown host type. */
-    if(data->host_len == 0)
+    if(data->host_len == 0 || is_res)
         data->host_type = Uri_HOST_UNKNOWN;
     else
         data->host_type = Uri_HOST_DNS;
@@ -1773,7 +1803,7 @@ static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags)
 
     TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
         ptr, data, flags, debugstr_wn(start, *ptr-start),
-        *ptr-start);
+        (int)(*ptr-start));
     return TRUE;
 }
 
@@ -1815,7 +1845,7 @@ static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
     data->host_type = Uri_HOST_UNKNOWN;
 
     TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
-        debugstr_wn(start, *ptr-start), *ptr-start);
+          debugstr_wn(start, *ptr-start), (int)(*ptr-start));
 
     return TRUE;
 }
@@ -2338,6 +2368,9 @@ static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags,
  *      it isn't an unknown scheme type.
  *
  *  4)  If it's a file scheme and the host is "localhost" it's removed.
+ *
+ *  5)  If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
+ *      then the UNC path characters are added before the host name.
  */
 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
                                   DWORD flags, BOOL computeOnly) {
@@ -2346,8 +2379,6 @@ static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
     const WCHAR *ptr;
     const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
 
-    uri->host_start = uri->canon_len;
-
     if(data->scheme_type == URL_SCHEME_FILE &&
        data->host_len == lstrlenW(localhostW)) {
         if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
@@ -2358,6 +2389,17 @@ static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
         }
     }
 
+    if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
+        if(!computeOnly) {
+            uri->canon_uri[uri->canon_len] = '\\';
+            uri->canon_uri[uri->canon_len+1] = '\\';
+        }
+        uri->canon_len += 2;
+        uri->authority_start = uri->canon_len;
+    }
+
+    uri->host_start = uri->canon_len;
+
     for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
         if(*ptr == '%' && known_scheme) {
             WCHAR val = decode_pct_val(ptr);
@@ -2751,7 +2793,7 @@ static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOO
     USHORT default_port = 0;
     DWORD i;
 
-    uri->has_port = FALSE;
+    uri->port_offset = -1;
 
     /* Check if the scheme has a default port. */
     for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
@@ -2762,8 +2804,7 @@ static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOO
         }
     }
 
-    if(data->port || has_default_port)
-        uri->has_port = TRUE;
+    uri->has_port = data->has_port || has_default_port;
 
     /* Possible cases:
      *  1)  Has a port which is the default port.
@@ -2771,38 +2812,44 @@ static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOO
      *  3)  Doesn't have a port, but, scheme has a default port.
      *  4)  No port.
      */
-    if(has_default_port && data->port && data->port_value == default_port) {
+    if(has_default_port && data->has_port && data->port_value == default_port) {
         /* If it's the default port and this flag isn't set, don't do anything. */
         if(flags & Uri_CREATE_NO_CANONICALIZE) {
-            /* Copy the original port over. */
-            if(!computeOnly) {
+            uri->port_offset = uri->canon_len-uri->authority_start;
+            if(!computeOnly)
                 uri->canon_uri[uri->canon_len] = ':';
-                memcpy(uri->canon_uri+uri->canon_len+1, data->port, data->port_len*sizeof(WCHAR));
+            ++uri->canon_len;
+
+            if(data->port) {
+                /* Copy the original port over. */
+                if(!computeOnly)
+                    memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
+                uri->canon_len += data->port_len;
+            } else {
+                if(!computeOnly)
+                    uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
+                else
+                    uri->canon_len += ui2str(NULL, data->port_value);
             }
-            uri->canon_len += data->port_len+1;
         }
 
         uri->port = default_port;
-    } else if(data->port) {
+    } else if(data->has_port) {
+        uri->port_offset = uri->canon_len-uri->authority_start;
         if(!computeOnly)
             uri->canon_uri[uri->canon_len] = ':';
         ++uri->canon_len;
 
-        if(flags & Uri_CREATE_NO_CANONICALIZE) {
+        if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
             /* Copy the original over without changes. */
             if(!computeOnly)
                 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
             uri->canon_len += data->port_len;
         } else {
-            const WCHAR formatW[] = {'%','u',0};
-            INT len = 0;
             if(!computeOnly)
-                len = sprintfW(uri->canon_uri+uri->canon_len, formatW, data->port_value);
-            else {
-                WCHAR tmp[6];
-                len = sprintfW(tmp, formatW, data->port_value);
-            }
-            uri->canon_len += len;
+                uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
+            else
+                uri->canon_len += ui2str(NULL, data->port_value);
         }
 
         uri->port = data->port_value;
@@ -2826,7 +2873,7 @@ static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags
     if(!canonicalize_port(data, uri, flags, computeOnly))
         return FALSE;
 
-    if(uri->host_start != -1)
+    if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
         uri->authority_len = uri->canon_len - uri->authority_start;
     else
         uri->authority_start = -1;
@@ -2859,6 +2906,7 @@ static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags
  * NOTES:
  *      file://c:/test%20test   -> file:///c:/test%2520test
  *      file://c:/test%3Etest   -> file:///c:/test%253Etest
+ * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
  *      file:///c:/test%20test  -> file:///c:/test%20test
  *      file:///c:/test%test    -> file:///c:/test%25test
  */
@@ -2867,6 +2915,7 @@ static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
     const WCHAR *ptr;
     const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
     const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
+    const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
 
     BOOL escape_pct = FALSE;
 
@@ -2907,13 +2956,22 @@ static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
         }
     }
 
+    if(!is_file && *(data->path) && *(data->path) != '/') {
+        /* Prepend a '/' to the path if it doesn't have one. */
+        if(!computeOnly)
+            uri->canon_uri[uri->canon_len] = '/';
+        ++uri->canon_len;
+    }
+
     for(; ptr < data->path+data->path_len; ++ptr) {
-        if(*ptr == '%') {
+        BOOL do_default_action = TRUE;
+
+        if(*ptr == '%' && !is_res) {
             const WCHAR *tmp = ptr;
             WCHAR val;
 
             /* Check if the % represents a valid encoded char, or if it needs encoded. */
-            BOOL force_encode = !check_pct_encoded(&tmp) && is_file;
+            BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
             val = decode_pct_val(ptr);
 
             if(force_encode || escape_pct) {
@@ -2921,49 +2979,43 @@ static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
                 if(!computeOnly)
                     pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
                 uri->canon_len += 3;
+                do_default_action = FALSE;
             } else if((is_unreserved(val) && known_scheme) ||
-                      (is_file && (is_unreserved(val) || is_reserved(val)))) {
+                      (is_file && (is_unreserved(val) || is_reserved(val) ||
+                      (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
                 if(!computeOnly)
                     uri->canon_uri[uri->canon_len] = val;
                 ++uri->canon_len;
 
                 ptr += 2;
                 continue;
-            } else {
-                if(!computeOnly)
-                    uri->canon_uri[uri->canon_len] = *ptr;
-                ++uri->canon_len;
             }
         } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
             /* Convert the '/' back to a '\\'. */
             if(!computeOnly)
                 uri->canon_uri[uri->canon_len] = '\\';
             ++uri->canon_len;
+            do_default_action = FALSE;
         } else if(*ptr == '\\' && known_scheme) {
-            if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
-                /* Don't convert the '\\' to a '/'. */
-                if(!computeOnly)
-                    uri->canon_uri[uri->canon_len] = *ptr;
-                ++uri->canon_len;
-            } else {
+            if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
+                /* Convert '\\' into a '/'. */
                 if(!computeOnly)
                     uri->canon_uri[uri->canon_len] = '/';
                 ++uri->canon_len;
+                do_default_action = FALSE;
             }
-        } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
+        } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
                   (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
-            if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
-                /* Don't escape the character. */
-                if(!computeOnly)
-                    uri->canon_uri[uri->canon_len] = *ptr;
-                ++uri->canon_len;
-            } else {
+            if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
                 /* Escape the forbidden character. */
                 if(!computeOnly)
                     pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
                 uri->canon_len += 3;
+                do_default_action = FALSE;
             }
-        } else {
+        }
+
+        if(do_default_action) {
             if(!computeOnly)
                 uri->canon_uri[uri->canon_len] = *ptr;
             ++uri->canon_len;
@@ -3041,6 +3093,8 @@ static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD fla
     }
 
     for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
+        BOOL do_default_action = TRUE;
+
         if(*ptr == '%' && known_scheme) {
             WCHAR val = decode_pct_val(ptr);
 
@@ -3051,57 +3105,48 @@ static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD fla
 
                 ptr += 2;
                 continue;
-            } else {
-                if(!computeOnly)
-                    uri->canon_uri[uri->canon_len] = *ptr;
-                ++uri->canon_len;
             }
         } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
             if(!computeOnly)
                 uri->canon_uri[uri->canon_len] = '\\';
             ++uri->canon_len;
-        } else if(*ptr == '\\' && is_file) {
-            if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
+            do_default_action = FALSE;
+        } else if(*ptr == '\\') {
+            if(is_file && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
                 /* Convert to a '/'. */
                 if(!computeOnly)
                     uri->canon_uri[uri->canon_len] = '/';
                 ++uri->canon_len;
-            } else {
-                /* Just copy it over. */
-                if(!computeOnly)
-                    uri->canon_uri[uri->canon_len] = *ptr;
-                ++uri->canon_len;
+                do_default_action = FALSE;
             }
         } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
                   !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
-            if(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
-                /* Forbidden characters aren't percent encoded for file schemes
-                 * with USE_DOS_PATH set.
-                 */
-                if(!computeOnly)
-                    uri->canon_uri[uri->canon_len] = *ptr;
-                ++uri->canon_len;
-            } else if(data->scheme_type == URL_SCHEME_MK && *ptr == '\\') {
-                /* MK URIs don't get '\\' percent encoded. */
-                if(!computeOnly)
-                    uri->canon_uri[uri->canon_len] = *ptr;
-                ++uri->canon_len;
-            } else {
+            if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
                 if(!computeOnly)
                     pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
                 uri->canon_len += 3;
+                do_default_action = FALSE;
             }
-        } else {
+        }
+
+        if(do_default_action) {
             if(!computeOnly)
                 uri->canon_uri[uri->canon_len] = *ptr;
             ++uri->canon_len;
         }
     }
 
+    if(data->scheme_type == URL_SCHEME_MK && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
+        DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
+                                            uri->canon_len - uri->path_start);
+        uri->canon_len = uri->path_start + new_len;
+    }
+
     uri->path_len = uri->canon_len - uri->path_start;
 
-    TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
-        debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
+    if(!computeOnly)
+        TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
+            debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
     return TRUE;
 }
 
@@ -3112,11 +3157,15 @@ static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD fla
  * URI is opaque it canonicalizes the path of the URI.
  */
 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
-    uri->display_absolute = TRUE;
-
-    if(!data->is_opaque) {
-        /* "//" is only added for non-wildcard scheme types. */
-        if(data->scheme_type != URL_SCHEME_WILDCARD) {
+    if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
+        /* "//" is only added for non-wildcard scheme types.
+         *
+         * A "//" is only added to a relative URI if it has a
+         * host or port component (this only happens if a IUriBuilder
+         * is generating an IUri).
+         */
+        if((data->is_relative && (data->host || data->has_port)) ||
+           (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
             if(!computeOnly) {
                 INT pos = uri->canon_len;
 
@@ -3129,10 +3178,13 @@ static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags,
         if(!canonicalize_authority(data, uri, flags, computeOnly))
             return FALSE;
 
-        /* TODO: Canonicalize the path of the URI. */
-        if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
-            return FALSE;
-
+        if(data->is_relative && (data->password || data->username)) {
+            if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
+                return FALSE;
+        } else {
+            if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
+                return FALSE;
+        }
     } else {
         /* Opaque URI's don't have an authority. */
         uri->userinfo_start = uri->userinfo_split = -1;
@@ -3144,6 +3196,7 @@ static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags,
         uri->authority_start = -1;
         uri->authority_len = 0;
         uri->domain_offset = -1;
+        uri->port_offset = -1;
 
         if(is_hierarchical_scheme(data->scheme_type)) {
             DWORD i;
@@ -3151,7 +3204,7 @@ static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags,
             /* Absolute URIs aren't displayed for known scheme types
              * which should be hierarchical URIs.
              */
-            uri->display_absolute = FALSE;
+            uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
 
             /* Windows also sets the port for these (if they have one). */
             for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
@@ -3375,7 +3428,7 @@ static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
     INT len;
 
     uri->canon_uri = NULL;
-    len = uri->canon_size = uri->canon_len = 0;
+    uri->canon_size = uri->canon_len = 0;
 
     TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
 
@@ -3394,14 +3447,12 @@ static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
     uri->canon_size = len;
     if(!canonicalize_scheme(data, uri, flags, FALSE)) {
         ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
-        heap_free(uri->canon_uri);
         return E_INVALIDARG;
     }
     uri->scheme_type = data->scheme_type;
 
     if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
         ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
-        heap_free(uri->canon_uri);
         return E_INVALIDARG;
     }
 
@@ -3510,12 +3561,9 @@ static HRESULT set_builder_component(LPWSTR *component, DWORD *component_len, LP
     return S_OK;
 }
 
-#define URI(x)         ((IUri*)  &(x)->lpIUriVtbl)
-#define URIBUILDER(x)  ((IUriBuilder*)  &(x)->lpIUriBuilderVtbl)
-
 static void reset_builder(UriBuilder *builder) {
     if(builder->uri)
-        IUri_Release(URI(builder->uri));
+        IUri_Release(&builder->uri->IUri_iface);
     builder->uri = NULL;
 
     heap_free(builder->fragment);
@@ -3720,8 +3768,11 @@ static void setup_port(const UriBuilder *builder, parse_data *data, DWORD flags)
 
 static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD flags) {
     const WCHAR *ptr = NULL;
+    const WCHAR *component;
     const WCHAR **pptr;
     DWORD expected_len;
+    BOOL check_len = TRUE;
+    BOOL valid = FALSE;
 
     if(builder->path) {
         ptr = builder->path;
@@ -3730,29 +3781,30 @@ static HRESULT validate_path(const UriBuilder *builder, parse_data *data, DWORD
               builder->uri && builder->uri->path_start > -1) {
         ptr = builder->uri->canon_uri+builder->uri->path_start;
         expected_len = builder->uri->path_len;
+    } else {
+        static const WCHAR nullW[] = {0};
+        ptr = nullW;
+        check_len = FALSE;
     }
 
-    if(ptr) {
-        BOOL valid = FALSE;
-        const WCHAR *component = ptr;
-        pptr = &ptr;
-
-        /* How the path is validated depends on what type of
-         * URI it is.
-         */
-        valid = data->is_opaque ?
-            parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
+    component = ptr;
+    pptr = &ptr;
 
-        if(!valid || expected_len != data->path_len) {
-            TRACE("(%p %p %x): Invalid path componet %s.\n", builder, data, flags,
-                debugstr_wn(component, expected_len));
-            return INET_E_INVALID_URL;
-        }
+    /* How the path is validated depends on what type of
+     * URI it is.
+     */
+    valid = data->is_opaque ?
+        parse_path_opaque(pptr, data, flags) : parse_path_hierarchical(pptr, data, flags);
 
-        TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
-            debugstr_wn(data->path, data->path_len), data->path_len);
+    if(!valid || (check_len && expected_len != data->path_len)) {
+        TRACE("(%p %p %x): Invalid path component %s.\n", builder, data, flags,
+            debugstr_wn(component, check_len ? expected_len : -1) );
+        return INET_E_INVALID_URL;
     }
 
+    TRACE("(%p %p %x): Valid path component %s len=%d.\n", builder, data, flags,
+        debugstr_wn(data->path, data->path_len), data->path_len);
+
     return S_OK;
 }
 
@@ -3805,7 +3857,7 @@ static HRESULT validate_fragment(const UriBuilder *builder, parse_data *data, DW
         const WCHAR *component = ptr;
         pptr = &ptr;
 
-        if(parse_query(pptr, data, flags) && expected_len == data->fragment_len)
+        if(parse_fragment(pptr, data, flags) && expected_len == data->fragment_len)
             TRACE("(%p %p %x): Valid fragment component %s len=%d.\n", builder, data, flags,
                 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
         else {
@@ -3847,11 +3899,14 @@ static HRESULT validate_components(const UriBuilder *builder, parse_data *data,
     if(FAILED(hr))
         return hr;
 
-    /* The URI is opaque if it doesn't have an authority component. */
-    data->is_opaque = !data->username && !data->password && !data->host;
-
     setup_port(builder, data, flags);
 
+    /* The URI is opaque if it doesn't have an authority component. */
+    if(!data->is_relative)
+        data->is_opaque = !data->username && !data->password && !data->host && !data->has_port;
+    else
+        data->is_opaque = !data->host && !data->has_port;
+
     hr = validate_path(builder, data, flags);
     if(FAILED(hr))
         return hr;
@@ -3869,104 +3924,258 @@ static HRESULT validate_components(const UriBuilder *builder, parse_data *data,
     return S_OK;
 }
 
-static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
-                         DWORD use_orig_flags, DWORD encoding_mask)
+static void convert_to_dos_path(const WCHAR *path, DWORD path_len,
+                                WCHAR *output, DWORD *output_len)
 {
-    HRESULT hr;
-    parse_data data;
+    const WCHAR *ptr = path;
 
-    if(!uri)
-        return E_POINTER;
+    if(path_len > 3 && *ptr == '/' && is_drive_path(path+1))
+        /* Skip over the leading / before the drive path. */
+        ++ptr;
 
-    if(encoding_mask && (!builder->uri || builder->modified_props)) {
-        *uri = NULL;
-        return E_NOTIMPL;
+    for(; ptr < path+path_len; ++ptr) {
+        if(*ptr == '/') {
+            if(output)
+                *output++ = '\\';
+            (*output_len)++;
+        } else {
+            if(output)
+                *output++ = *ptr;
+            (*output_len)++;
+        }
     }
+}
 
-    /* Decide what flags should be used when creating the Uri. */
-    if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
-        create_flags = builder->uri->create_flags;
-    else {
-        if(has_invalid_flag_combination(create_flags)) {
-            *uri = NULL;
-            return E_INVALIDARG;
+/* Generates a raw uri string using the parse_data. */
+static DWORD generate_raw_uri(const parse_data *data, BSTR uri, DWORD flags) {
+    DWORD length = 0;
+
+    if(data->scheme) {
+        if(uri) {
+            memcpy(uri, data->scheme, data->scheme_len*sizeof(WCHAR));
+            uri[data->scheme_len] = ':';
         }
+        length += data->scheme_len+1;
+    }
 
-        /* Set the default flags if they don't cause a conflict. */
-        apply_default_flags(&create_flags);
+    if(!data->is_opaque) {
+        /* For the "//" which appears before the authority component. */
+        if(uri) {
+            uri[length] = '/';
+            uri[length+1] = '/';
+        }
+        length += 2;
+
+        /* Check if we need to add the "\\" before the host name
+         * of a UNC server name in a DOS path.
+         */
+        if(flags & RAW_URI_CONVERT_TO_DOS_PATH &&
+           data->scheme_type == URL_SCHEME_FILE && data->host) {
+            if(uri) {
+                uri[length] = '\\';
+                uri[length+1] = '\\';
+            }
+            length += 2;
+        }
     }
 
-    /* Return the base IUri if no changes have been made and the create_flags match. */
-    if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
-        *uri = URI(builder->uri);
-        IUri_AddRef(*uri);
-        return S_OK;
+    if(data->username) {
+        if(uri)
+            memcpy(uri+length, data->username, data->username_len*sizeof(WCHAR));
+        length += data->username_len;
     }
 
-    hr = validate_components(builder, &data, create_flags);
-    if(FAILED(hr)) {
-        *uri = NULL;
-        return hr;
+    if(data->password) {
+        if(uri) {
+            uri[length] = ':';
+            memcpy(uri+length+1, data->password, data->password_len*sizeof(WCHAR));
+        }
+        length += data->password_len+1;
     }
 
-    return E_NOTIMPL;
-}
+    if(data->password || data->username) {
+        if(uri)
+            uri[length] = '@';
+        ++length;
+    }
 
-#define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
+    if(data->host) {
+        /* IPv6 addresses get the brackets added around them if they don't already
+         * have them.
+         */
+        const BOOL add_brackets = data->host_type == Uri_HOST_IPV6 && *(data->host) != '[';
+        if(add_brackets) {
+            if(uri)
+                uri[length] = '[';
+            ++length;
+        }
 
-static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
-{
-    Uri *This = URI_THIS(iface);
+        if(uri)
+            memcpy(uri+length, data->host, data->host_len*sizeof(WCHAR));
+        length += data->host_len;
 
-    if(IsEqualGUID(&IID_IUnknown, riid)) {
-        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
-        *ppv = URI(This);
-    }else if(IsEqualGUID(&IID_IUri, riid)) {
-        TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
-        *ppv = URI(This);
-    }else if(IsEqualGUID(&IID_IUriObj, riid)) {
-        TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
-        *ppv = This;
-        return S_OK;
-    }else {
-        TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
-        *ppv = NULL;
-        return E_NOINTERFACE;
+        if(add_brackets) {
+            if(uri)
+                uri[length] = ']';
+            length++;
+        }
     }
 
-    IUnknown_AddRef((IUnknown*)*ppv);
-    return S_OK;
-}
+    if(data->has_port) {
+        /* The port isn't included in the raw uri if it's the default
+         * port for the scheme type.
+         */
+        DWORD i;
+        BOOL is_default = FALSE;
 
-static ULONG WINAPI Uri_AddRef(IUri *iface)
-{
-    Uri *This = URI_THIS(iface);
-    LONG ref = InterlockedIncrement(&This->ref);
+        for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
+            if(data->scheme_type == default_ports[i].scheme &&
+               data->port_value == default_ports[i].port)
+                is_default = TRUE;
+        }
 
-    TRACE("(%p) ref=%d\n", This, ref);
+        if(!is_default || flags & RAW_URI_FORCE_PORT_DISP) {
+            if(uri)
+                uri[length] = ':';
+            ++length;
 
-    return ref;
-}
+            if(uri)
+                length += ui2str(uri+length, data->port_value);
+            else
+                length += ui2str(NULL, data->port_value);
+        }
+    }
 
-static ULONG WINAPI Uri_Release(IUri *iface)
-{
-    Uri *This = URI_THIS(iface);
+    /* Check if a '/' should be added before the path for hierarchical URIs. */
+    if(!data->is_opaque && data->path && *(data->path) != '/') {
+        if(uri)
+            uri[length] = '/';
+        ++length;
+    }
+
+    if(data->path) {
+        if(!data->is_opaque && data->scheme_type == URL_SCHEME_FILE &&
+           flags & RAW_URI_CONVERT_TO_DOS_PATH) {
+            DWORD len = 0;
+
+            if(uri)
+                convert_to_dos_path(data->path, data->path_len, uri+length, &len);
+            else
+                convert_to_dos_path(data->path, data->path_len, NULL, &len);
+
+            length += len;
+        } else {
+            if(uri)
+                memcpy(uri+length, data->path, data->path_len*sizeof(WCHAR));
+            length += data->path_len;
+        }
+    }
+
+    if(data->query) {
+        if(uri)
+            memcpy(uri+length, data->query, data->query_len*sizeof(WCHAR));
+        length += data->query_len;
+    }
+
+    if(data->fragment) {
+        if(uri)
+            memcpy(uri+length, data->fragment, data->fragment_len*sizeof(WCHAR));
+        length += data->fragment_len;
+    }
+
+    if(uri)
+        TRACE("(%p %p): Generated raw uri=%s len=%d\n", data, uri, debugstr_wn(uri, length), length);
+    else
+        TRACE("(%p %p): Computed raw uri len=%d\n", data, uri, length);
+
+    return length;
+}
+
+static HRESULT generate_uri(const UriBuilder *builder, const parse_data *data, Uri *uri, DWORD flags) {
+    HRESULT hr;
+    DWORD length = generate_raw_uri(data, NULL, 0);
+    uri->raw_uri = SysAllocStringLen(NULL, length);
+    if(!uri->raw_uri)
+        return E_OUTOFMEMORY;
+
+    generate_raw_uri(data, uri->raw_uri, 0);
+
+    hr = canonicalize_uri(data, uri, flags);
+    if(FAILED(hr)) {
+        if(hr == E_INVALIDARG)
+            return INET_E_INVALID_URL;
+        return hr;
+    }
+
+    uri->create_flags = flags;
+    return S_OK;
+}
+
+static inline Uri* impl_from_IUri(IUri *iface)
+{
+    return CONTAINING_RECORD(iface, Uri, IUri_iface);
+}
+
+static inline void destory_uri_obj(Uri *This)
+{
+    SysFreeString(This->raw_uri);
+    heap_free(This->canon_uri);
+    heap_free(This);
+}
+
+static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
+{
+    Uri *This = impl_from_IUri(iface);
+
+    if(IsEqualGUID(&IID_IUnknown, riid)) {
+        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
+        *ppv = &This->IUri_iface;
+    }else if(IsEqualGUID(&IID_IUri, riid)) {
+        TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
+        *ppv = &This->IUri_iface;
+    }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
+        TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, riid);
+        *ppv = &This->IUriBuilderFactory_iface;
+    }else if(IsEqualGUID(&IID_IUriObj, riid)) {
+        TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv);
+        *ppv = This;
+        return S_OK;
+    }else {
+        TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
+        *ppv = NULL;
+        return E_NOINTERFACE;
+    }
+
+    IUnknown_AddRef((IUnknown*)*ppv);
+    return S_OK;
+}
+
+static ULONG WINAPI Uri_AddRef(IUri *iface)
+{
+    Uri *This = impl_from_IUri(iface);
+    LONG ref = InterlockedIncrement(&This->ref);
+
+    TRACE("(%p) ref=%d\n", This, ref);
+
+    return ref;
+}
+
+static ULONG WINAPI Uri_Release(IUri *iface)
+{
+    Uri *This = impl_from_IUri(iface);
     LONG ref = InterlockedDecrement(&This->ref);
 
     TRACE("(%p) ref=%d\n", This, ref);
 
-    if(!ref) {
-        SysFreeString(This->raw_uri);
-        heap_free(This->canon_uri);
-        heap_free(This);
-    }
+    if(!ref)
+        destory_uri_obj(This);
 
     return ref;
 }
 
 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
 {
-    Uri *This = URI_THIS(iface);
+    Uri *This = impl_from_IUri(iface);
     HRESULT hres;
     TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
 
@@ -3994,7 +4203,7 @@ static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BST
 
     switch(uriProp) {
     case Uri_PROPERTY_ABSOLUTE_URI:
-        if(!This->display_absolute) {
+        if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
             *pbstrProperty = SysAllocStringLen(NULL, 0);
             hres = S_FALSE;
         } else {
@@ -4036,7 +4245,12 @@ static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BST
         break;
     case Uri_PROPERTY_AUTHORITY:
         if(This->authority_start > -1) {
-            *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
+            if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) &&
+               This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH)
+                /* Don't include the port in the authority component. */
+                *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset);
+            else
+                *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
             hres = S_OK;
         } else {
             *pbstrProperty = SysAllocStringLen(NULL, 0);
@@ -4251,7 +4465,7 @@ static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BST
 
 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
 {
-    Uri *This = URI_THIS(iface);
+    Uri *This = impl_from_IUri(iface);
     HRESULT hres;
     TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
 
@@ -4270,7 +4484,7 @@ static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, D
 
     switch(uriProp) {
     case Uri_PROPERTY_ABSOLUTE_URI:
-        if(!This->display_absolute) {
+        if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) {
             *pcchProperty = 0;
             hres = S_FALSE;
         } else {
@@ -4292,7 +4506,13 @@ static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, D
 
         break;
     case Uri_PROPERTY_AUTHORITY:
-        *pcchProperty = This->authority_len;
+        if(This->port_offset > -1 &&
+           This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH &&
+           is_default_port(This->scheme_type, This->port))
+            /* Only count up until the port in the authority. */
+            *pcchProperty = This->port_offset;
+        else
+            *pcchProperty = This->authority_len;
         hres = (This->authority_start > -1) ? S_OK : S_FALSE;
         break;
     case Uri_PROPERTY_DISPLAY_URI:
@@ -4379,7 +4599,7 @@ static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, D
 
 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
 {
-    Uri *This = URI_THIS(iface);
+    Uri *This = impl_from_IUri(iface);
     HRESULT hres;
 
     TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
@@ -4431,7 +4651,7 @@ static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DW
 
 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
 {
-    Uri *This = URI_THIS(iface);
+    Uri *This = impl_from_IUri(iface);
     TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
 
     if(!pfHasProperty)
@@ -4439,7 +4659,7 @@ static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *p
 
     switch(uriProp) {
     case Uri_PROPERTY_ABSOLUTE_URI:
-        *pfHasProperty = This->display_absolute;
+        *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI);
         break;
     case Uri_PROPERTY_AUTHORITY:
         *pfHasProperty = This->authority_start > -1;
@@ -4509,121 +4729,120 @@ static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *p
 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
 {
     TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
 }
 
 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
 {
     TRACE("(%p)->(%p)\n", iface, pstrAuthority);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
 }
 
 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
 {
     TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
 }
 
 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
 {
     TRACE("(%p)->(%p)\n", iface, pstrDomain);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
 }
 
 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
 {
     TRACE("(%p)->(%p)\n", iface, pstrExtension);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
 }
 
 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
 {
     TRACE("(%p)->(%p)\n", iface, pstrFragment);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
 }
 
 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
 {
     TRACE("(%p)->(%p)\n", iface, pstrHost);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
 }
 
 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
 {
     TRACE("(%p)->(%p)\n", iface, pstrPassword);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
 }
 
 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
 {
     TRACE("(%p)->(%p)\n", iface, pstrPath);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
 }
 
 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
 {
     TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
 }
 
 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
 {
     TRACE("(%p)->(%p)\n", iface, pstrQuery);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
 }
 
 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
 {
     TRACE("(%p)->(%p)\n", iface, pstrRawUri);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
 }
 
 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
 {
     TRACE("(%p)->(%p)\n", iface, pstrSchemeName);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
 }
 
 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
 {
     TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
 }
 
 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
 {
     TRACE("(%p)->(%p)\n", iface, pstrUserName);
-    return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
+    return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
 }
 
 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
 {
     TRACE("(%p)->(%p)\n", iface, pdwHostType);
-    return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
+    return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
 }
 
 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
 {
     TRACE("(%p)->(%p)\n", iface, pdwPort);
-    return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
+    return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
 }
 
 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
 {
-    Uri *This = URI_THIS(iface);
-    TRACE("(%p)->(%p)\n", This, pdwScheme);
-    return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
+    TRACE("(%p)->(%p)\n", iface, pdwScheme);
+    return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
 }
 
 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
 {
     TRACE("(%p)->(%p)\n", iface, pdwZone);
-    return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
+    return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
 }
 
 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
 {
-    Uri *This = URI_THIS(iface);
+    Uri *This = impl_from_IUri(iface);
     TRACE("(%p)->(%p)\n", This, pdwProperties);
 
     if(!pdwProperties)
@@ -4632,7 +4851,7 @@ static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
     /* All URIs have these. */
     *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
 
-    if(This->display_absolute)
+    if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI))
         *pdwProperties |= Uri_HAS_ABSOLUTE_URI;
 
     if(This->scheme_start > -1)
@@ -4671,7 +4890,7 @@ static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
 
 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
 {
-    Uri *This = URI_THIS(iface);
+    Uri *This = impl_from_IUri(iface);
     Uri *other;
 
     TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
@@ -4698,8 +4917,6 @@ static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
     return S_OK;
 }
 
-#undef URI_THIS
-
 static const IUriVtbl UriVtbl = {
     Uri_QueryInterface,
     Uri_AddRef,
@@ -4731,6 +4948,114 @@ static const IUriVtbl UriVtbl = {
     Uri_IsEqual
 };
 
+static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface)
+{
+    return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface);
+}
+
+static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv)
+{
+    Uri *This = impl_from_IUriBuilderFactory(iface);
+
+    if(IsEqualGUID(&IID_IUnknown, riid)) {
+        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
+        *ppv = &This->IUriBuilderFactory_iface;
+    }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) {
+        TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv);
+        *ppv = &This->IUriBuilderFactory_iface;
+    }else if(IsEqualGUID(&IID_IUri, riid)) {
+        TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
+        *ppv = &This->IUri_iface;
+    }else {
+        TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
+        *ppv = NULL;
+        return E_NOINTERFACE;
+    }
+
+    IUnknown_AddRef((IUnknown*)*ppv);
+    return S_OK;
+}
+
+static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface)
+{
+    Uri *This = impl_from_IUriBuilderFactory(iface);
+    LONG ref = InterlockedIncrement(&This->ref);
+
+    TRACE("(%p) ref=%d\n", This, ref);
+
+    return ref;
+}
+
+static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface)
+{
+    Uri *This = impl_from_IUriBuilderFactory(iface);
+    LONG ref = InterlockedDecrement(&This->ref);
+
+    TRACE("(%p) ref=%d\n", This, ref);
+
+    if(!ref)
+        destory_uri_obj(This);
+
+    return ref;
+}
+
+static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface,
+                                                          DWORD dwFlags,
+                                                          DWORD_PTR dwReserved,
+                                                          IUriBuilder **ppIUriBuilder)
+{
+    Uri *This = impl_from_IUriBuilderFactory(iface);
+    TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
+
+    if(!ppIUriBuilder)
+        return E_POINTER;
+
+    if(dwFlags || dwReserved) {
+        *ppIUriBuilder = NULL;
+        return E_INVALIDARG;
+    }
+
+    return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder);
+}
+
+static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface,
+                                                                     DWORD dwFlags,
+                                                                     DWORD_PTR dwReserved,
+                                                                     IUriBuilder **ppIUriBuilder)
+{
+    Uri *This = impl_from_IUriBuilderFactory(iface);
+    TRACE("(%p)->(%08x %08x %p)\n", This, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
+
+    if(!ppIUriBuilder)
+        return E_POINTER;
+
+    if(dwFlags || dwReserved) {
+        *ppIUriBuilder = NULL;
+        return E_INVALIDARG;
+    }
+
+    return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder);
+}
+
+static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = {
+    UriBuilderFactory_QueryInterface,
+    UriBuilderFactory_AddRef,
+    UriBuilderFactory_Release,
+    UriBuilderFactory_CreateIUriBuilder,
+    UriBuilderFactory_CreateInitializedIUriBuilder
+};
+
+static Uri* create_uri_obj(void) {
+    Uri *ret = heap_alloc_zero(sizeof(Uri));
+    if(ret) {
+        ret->IUri_iface.lpVtbl = &UriVtbl;
+        ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl;
+        ret->ref = 1;
+    }
+
+    return ret;
+}
+
 /***********************************************************************
  *           CreateUri (urlmon.@)
  *
@@ -4786,12 +5111,11 @@ HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IU
     if(dwFlags & ~supported_flags)
         FIXME("Ignoring unsupported flag(s) %x\n", dwFlags & ~supported_flags);
 
-    ret = heap_alloc(sizeof(Uri));
-    if(!ret)
+    ret = create_uri_obj();
+    if(!ret) {
+        *ppURI = NULL;
         return E_OUTOFMEMORY;
-
-    ret->lpIUriVtbl = &UriVtbl;
-    ret->ref = 1;
+    }
 
     /* Explicitly set the default flags if it doesn't cause a flag conflict. */
     apply_default_flags(&dwFlags);
@@ -4813,8 +5137,7 @@ HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IU
     /* Validate and parse the URI into it's components. */
     if(!parse_uri(&data, dwFlags)) {
         /* Encountered an unsupported or invalid URI */
-        SysFreeString(ret->raw_uri);
-        heap_free(ret);
+        IUri_Release(&ret->IUri_iface);
         *ppURI = NULL;
         return E_INVALIDARG;
     }
@@ -4822,15 +5145,14 @@ HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IU
     /* Canonicalize the URI. */
     hr = canonicalize_uri(&data, ret, dwFlags);
     if(FAILED(hr)) {
-        SysFreeString(ret->raw_uri);
-        heap_free(ret);
+        IUri_Release(&ret->IUri_iface);
         *ppURI = NULL;
         return hr;
     }
 
     ret->create_flags = dwFlags;
 
-    *ppURI = URI(ret);
+    *ppURI = &ret->IUri_iface;
     return S_OK;
 }
 
@@ -4908,18 +5230,79 @@ HRESULT WINAPI CreateUriWithFragment(LPCWSTR pwzURI, LPCWSTR pwzFragment, DWORD
     return hres;
 }
 
-#define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
+static HRESULT build_uri(const UriBuilder *builder, IUri **uri, DWORD create_flags,
+                         DWORD use_orig_flags, DWORD encoding_mask)
+{
+    HRESULT hr;
+    parse_data data;
+    Uri *ret;
+
+    if(!uri)
+        return E_POINTER;
+
+    if(encoding_mask && (!builder->uri || builder->modified_props)) {
+        *uri = NULL;
+        return E_NOTIMPL;
+    }
+
+    /* Decide what flags should be used when creating the Uri. */
+    if((use_orig_flags & UriBuilder_USE_ORIGINAL_FLAGS) && builder->uri)
+        create_flags = builder->uri->create_flags;
+    else {
+        if(has_invalid_flag_combination(create_flags)) {
+            *uri = NULL;
+            return E_INVALIDARG;
+        }
+
+        /* Set the default flags if they don't cause a conflict. */
+        apply_default_flags(&create_flags);
+    }
+
+    /* Return the base IUri if no changes have been made and the create_flags match. */
+    if(builder->uri && !builder->modified_props && builder->uri->create_flags == create_flags) {
+        *uri = &builder->uri->IUri_iface;
+        IUri_AddRef(*uri);
+        return S_OK;
+    }
+
+    hr = validate_components(builder, &data, create_flags);
+    if(FAILED(hr)) {
+        *uri = NULL;
+        return hr;
+    }
+
+    ret = create_uri_obj();
+    if(!ret) {
+        *uri = NULL;
+        return E_OUTOFMEMORY;
+    }
+
+    hr = generate_uri(builder, &data, ret, create_flags);
+    if(FAILED(hr)) {
+        IUri_Release(&ret->IUri_iface);
+        *uri = NULL;
+        return hr;
+    }
+
+    *uri = &ret->IUri_iface;
+    return S_OK;
+}
+
+static inline UriBuilder* impl_from_IUriBuilder(IUriBuilder *iface)
+{
+    return CONTAINING_RECORD(iface, UriBuilder, IUriBuilder_iface);
+}
 
 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
 
     if(IsEqualGUID(&IID_IUnknown, riid)) {
         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
-        *ppv = URIBUILDER(This);
+        *ppv = &This->IUriBuilder_iface;
     }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
-        TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
-        *ppv = URIBUILDER(This);
+        TRACE("(%p)->(IID_IUriBuilder %p)\n", This, ppv);
+        *ppv = &This->IUriBuilder_iface;
     }else {
         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
         *ppv = NULL;
@@ -4932,7 +5315,7 @@ static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid,
 
 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     LONG ref = InterlockedIncrement(&This->ref);
 
     TRACE("(%p) ref=%d\n", This, ref);
@@ -4942,13 +5325,13 @@ static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
 
 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     LONG ref = InterlockedDecrement(&This->ref);
 
     TRACE("(%p) ref=%d\n", This, ref);
 
     if(!ref) {
-        if(This->uri) IUri_Release(URI(This->uri));
+        if(This->uri) IUri_Release(&This->uri->IUri_iface);
         heap_free(This->fragment);
         heap_free(This->host);
         heap_free(This->password);
@@ -4967,7 +5350,7 @@ static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
                                                  DWORD_PTR    dwReserved,
                                                  IUri       **ppIUri)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     HRESULT hr;
     TRACE("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
 
@@ -4983,7 +5366,7 @@ static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
                                            DWORD_PTR    dwReserved,
                                            IUri       **ppIUri)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     HRESULT hr;
     TRACE("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
 
@@ -5004,7 +5387,7 @@ static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
                                          DWORD_PTR    dwReserved,
                                          IUri       **ppIUri)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     HRESULT hr;
     TRACE("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
         dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
@@ -5018,14 +5401,14 @@ static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
 
 static HRESULT WINAPI  UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p)\n", This, ppIUri);
 
     if(!ppIUri)
         return E_POINTER;
 
     if(This->uri) {
-        IUri *uri = URI(This->uri);
+        IUri *uri = &This->uri->IUri_iface;
         IUri_AddRef(uri);
         *ppIUri = uri;
     } else
@@ -5036,7 +5419,7 @@ static HRESULT WINAPI  UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
 
 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p)\n", This, pIUri);
 
     if(pIUri) {
@@ -5068,7 +5451,7 @@ static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
 
 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
 
     if(!This->uri || This->uri->fragment_start == -1 || This->modified_props & Uri_HAS_FRAGMENT)
@@ -5080,7 +5463,7 @@ static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFrag
 
 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
 
     if(!This->uri || This->uri->host_start == -1 || This->modified_props & Uri_HAS_HOST)
@@ -5098,7 +5481,7 @@ static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LP
 
 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
 
     if(!This->uri || This->uri->userinfo_split == -1 || This->modified_props & Uri_HAS_PASSWORD)
@@ -5112,7 +5495,7 @@ static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPass
 
 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
 
     if(!This->uri || This->uri->path_start == -1 || This->modified_props & Uri_HAS_PATH)
@@ -5124,7 +5507,7 @@ static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LP
 
 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
 
     if(!pfHasPort) {
@@ -5145,7 +5528,7 @@ static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DW
 
 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
 
     if(!This->uri || This->uri->query_start == -1 || This->modified_props & Uri_HAS_QUERY)
@@ -5157,7 +5540,7 @@ static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery,
 
 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
 
     if(!This->uri || This->uri->scheme_start == -1 || This->modified_props & Uri_HAS_SCHEME_NAME)
@@ -5169,7 +5552,7 @@ static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSc
 
 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
 
     if(!This->uri || This->uri->userinfo_start == -1 || This->uri->userinfo_split == 0 ||
@@ -5191,7 +5574,7 @@ static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUser
 
 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
     return set_builder_component(&This->fragment, &This->fragment_len, pwzNewValue, '#',
                                  &This->modified_props, Uri_HAS_FRAGMENT);
@@ -5199,7 +5582,7 @@ static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewV
 
 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
 
     /* Host name can't be set to NULL. */
@@ -5212,7 +5595,7 @@ static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue
 
 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
     return set_builder_component(&This->password, &This->password_len, pwzNewValue, 0,
                                  &This->modified_props, Uri_HAS_PASSWORD);
@@ -5220,7 +5603,7 @@ static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewV
 
 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
     return set_builder_component(&This->path, &This->path_len, pwzNewValue, 0,
                                  &This->modified_props, Uri_HAS_PATH);
@@ -5228,7 +5611,7 @@ static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue
 
 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
 
     This->has_port = fHasPort;
@@ -5239,7 +5622,7 @@ static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWOR
 
 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
     return set_builder_component(&This->query, &This->query_len, pwzNewValue, '?',
                                  &This->modified_props, Uri_HAS_QUERY);
@@ -5247,7 +5630,7 @@ static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValu
 
 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
 
     /* Only set the scheme name if it's not NULL or empty. */
@@ -5260,7 +5643,7 @@ static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNe
 
 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
     return set_builder_component(&This->username, &This->username_len, pwzNewValue, 0,
                                  &This->modified_props, Uri_HAS_USER_NAME);
@@ -5272,7 +5655,7 @@ static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPr
                                  Uri_HAS_PASSWORD|Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY|Uri_HAS_QUERY|
                                  Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
 
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(0x%08x)\n", This, dwPropertyMask);
 
     if(dwPropertyMask & ~accepted_flags)
@@ -5308,7 +5691,7 @@ static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPr
 
 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
 {
-    UriBuilder *This = URIBUILDER_THIS(iface);
+    UriBuilder *This = impl_from_IUriBuilder(iface);
     TRACE("(%p)->(%p)\n", This, pfModified);
 
     if(!pfModified)
@@ -5318,8 +5701,6 @@ static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfMod
     return S_OK;
 }
 
-#undef URIBUILDER_THIS
-
 static const IUriBuilderVtbl UriBuilderVtbl = {
     UriBuilder_QueryInterface,
     UriBuilder_AddRef,
@@ -5365,7 +5746,7 @@ HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserve
     if(!ret)
         return E_OUTOFMEMORY;
 
-    ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
+    ret->IUriBuilder_iface.lpVtbl = &UriBuilderVtbl;
     ret->ref = 1;
 
     if(pIUri) {
@@ -5388,6 +5769,920 @@ HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserve
         }
     }
 
-    *ppIUriBuilder = URIBUILDER(ret);
+    *ppIUriBuilder = &ret->IUriBuilder_iface;
+    return S_OK;
+}
+
+/* Merges the base path with the relative path and stores the resulting path
+ * and path len in 'result' and 'result_len'.
+ */
+static HRESULT merge_paths(parse_data *data, const WCHAR *base, DWORD base_len, const WCHAR *relative,
+                           DWORD relative_len, WCHAR **result, DWORD *result_len, DWORD flags)
+{
+    const WCHAR *end = NULL;
+    DWORD base_copy_len = 0;
+    WCHAR *ptr;
+
+    if(base_len) {
+        /* Find the characters the will be copied over from
+         * the base path.
+         */
+        end = memrchrW(base, '/', base_len);
+        if(!end && data->scheme_type == URL_SCHEME_FILE)
+            /* Try looking for a '\\'. */
+            end = memrchrW(base, '\\', base_len);
+    }
+
+    if(end) {
+        base_copy_len = (end+1)-base;
+        *result = heap_alloc((base_copy_len+relative_len+1)*sizeof(WCHAR));
+    } else
+        *result = heap_alloc((relative_len+1)*sizeof(WCHAR));
+
+    if(!(*result)) {
+        *result_len = 0;
+        return E_OUTOFMEMORY;
+    }
+
+    ptr = *result;
+    if(end) {
+        memcpy(ptr, base, base_copy_len*sizeof(WCHAR));
+        ptr += base_copy_len;
+    }
+
+    memcpy(ptr, relative, relative_len*sizeof(WCHAR));
+    ptr += relative_len;
+    *ptr = '\0';
+
+    *result_len = (ptr-*result);
     return S_OK;
 }
+
+static HRESULT combine_uri(Uri *base, Uri *relative, DWORD flags, IUri **result, DWORD extras) {
+    Uri *ret;
+    HRESULT hr;
+    parse_data data;
+    DWORD create_flags = 0, len = 0;
+
+    memset(&data, 0, sizeof(parse_data));
+
+    /* Base case is when the relative Uri has a scheme name,
+     * if it does, then 'result' will contain the same data
+     * as the relative Uri.
+     */
+    if(relative->scheme_start > -1) {
+        data.uri = SysAllocString(relative->raw_uri);
+        if(!data.uri) {
+            *result = NULL;
+            return E_OUTOFMEMORY;
+        }
+
+        parse_uri(&data, 0);
+
+        ret = create_uri_obj();
+        if(!ret) {
+            *result = NULL;
+            return E_OUTOFMEMORY;
+        }
+
+        if(extras & COMBINE_URI_FORCE_FLAG_USE) {
+            if(flags & URL_DONT_SIMPLIFY)
+                create_flags |= Uri_CREATE_NO_CANONICALIZE;
+            if(flags & URL_DONT_UNESCAPE_EXTRA_INFO)
+                create_flags |= Uri_CREATE_NO_DECODE_EXTRA_INFO;
+        }
+
+        ret->raw_uri = data.uri;
+        hr = canonicalize_uri(&data, ret, create_flags);
+        if(FAILED(hr)) {
+            IUri_Release(&ret->IUri_iface);
+            *result = NULL;
+            return hr;
+        }
+
+        apply_default_flags(&create_flags);
+        ret->create_flags = create_flags;
+
+        *result = &ret->IUri_iface;
+    } else {
+        WCHAR *path = NULL;
+        DWORD raw_flags = 0;
+
+        if(base->scheme_start > -1) {
+            data.scheme = base->canon_uri+base->scheme_start;
+            data.scheme_len = base->scheme_len;
+            data.scheme_type = base->scheme_type;
+        } else {
+            data.is_relative = TRUE;
+            data.scheme_type = URL_SCHEME_UNKNOWN;
+            create_flags |= Uri_CREATE_ALLOW_RELATIVE;
+        }
+
+        if(base->authority_start > -1) {
+            if(base->userinfo_start > -1 && base->userinfo_split != 0) {
+                data.username = base->canon_uri+base->userinfo_start;
+                data.username_len = (base->userinfo_split > -1) ? base->userinfo_split : base->userinfo_len;
+            }
+
+            if(base->userinfo_split > -1) {
+                data.password = base->canon_uri+base->userinfo_start+base->userinfo_split+1;
+                data.password_len = base->userinfo_len-base->userinfo_split-1;
+            }
+
+            if(base->host_start > -1) {
+                data.host = base->canon_uri+base->host_start;
+                data.host_len = base->host_len;
+                data.host_type = base->host_type;
+            }
+
+            if(base->has_port) {
+                data.has_port = TRUE;
+                data.port_value = base->port;
+            }
+        } else if(base->scheme_type != URL_SCHEME_FILE)
+            data.is_opaque = TRUE;
+
+        if(relative->path_start == -1 || !relative->path_len) {
+            if(base->path_start > -1) {
+                data.path = base->canon_uri+base->path_start;
+                data.path_len = base->path_len;
+            } else if((base->path_start == -1 || !base->path_len) && !data.is_opaque) {
+                /* Just set the path as a '/' if the base didn't have
+                 * one and if it's an hierarchical URI.
+                 */
+                static const WCHAR slashW[] = {'/',0};
+                data.path = slashW;
+                data.path_len = 1;
+            }
+
+            if(relative->query_start > -1) {
+                data.query = relative->canon_uri+relative->query_start;
+                data.query_len = relative->query_len;
+            } else if(base->query_start > -1) {
+                data.query = base->canon_uri+base->query_start;
+                data.query_len = base->query_len;
+            }
+        } else {
+            const WCHAR *ptr, **pptr;
+            DWORD path_offset = 0, path_len = 0;
+
+            /* There's two possibilities on what will happen to the path component
+             * of the result IUri. First, if the relative path begins with a '/'
+             * then the resulting path will just be the relative path. Second, if
+             * relative path doesn't begin with a '/' then the base path and relative
+             * path are merged together.
+             */
+            if(relative->path_len && *(relative->canon_uri+relative->path_start) == '/') {
+                WCHAR *tmp = NULL;
+                BOOL copy_drive_path = FALSE;
+
+                /* If the relative IUri's path starts with a '/', then we
+                 * don't use the base IUri's path. Unless the base IUri
+                 * is a file URI, in which case it uses the drive path of
+                 * the base IUri (if it has any) in the new path.
+                 */
+                if(base->scheme_type == URL_SCHEME_FILE) {
+                    if(base->path_len > 3 && *(base->canon_uri+base->path_start) == '/' &&
+                       is_drive_path(base->canon_uri+base->path_start+1)) {
+                        path_len += 3;
+                        copy_drive_path = TRUE;
+                    }
+                }
+
+                path_len += relative->path_len;
+
+                path = heap_alloc((path_len+1)*sizeof(WCHAR));
+                if(!path) {
+                    *result = NULL;
+                    return E_OUTOFMEMORY;
+                }
+
+                tmp = path;
+
+                /* Copy the base paths, drive path over. */
+                if(copy_drive_path) {
+                    memcpy(tmp, base->canon_uri+base->path_start, 3*sizeof(WCHAR));
+                    tmp += 3;
+                }
+
+                memcpy(tmp, relative->canon_uri+relative->path_start, relative->path_len*sizeof(WCHAR));
+                path[path_len] = '\0';
+            } else {
+                /* Merge the base path with the relative path. */
+                hr = merge_paths(&data, base->canon_uri+base->path_start, base->path_len,
+                                 relative->canon_uri+relative->path_start, relative->path_len,
+                                 &path, &path_len, flags);
+                if(FAILED(hr)) {
+                    *result = NULL;
+                    return hr;
+                }
+
+                /* If the resulting IUri is a file URI, the drive path isn't
+                 * reduced out when the dot segments are removed.
+                 */
+                if(path_len >= 3 && data.scheme_type == URL_SCHEME_FILE && !data.host) {
+                    if(*path == '/' && is_drive_path(path+1))
+                        path_offset = 2;
+                    else if(is_drive_path(path))
+                        path_offset = 1;
+                }
+            }
+
+            /* Check if the dot segments need to be removed from the path. */
+            if(!(flags & URL_DONT_SIMPLIFY) && !data.is_opaque) {
+                DWORD offset = (path_offset > 0) ? path_offset+1 : 0;
+                DWORD new_len = remove_dot_segments(path+offset,path_len-offset);
+
+                if(new_len != path_len) {
+                    WCHAR *tmp = heap_realloc(path, (offset+new_len+1)*sizeof(WCHAR));
+                    if(!tmp) {
+                        heap_free(path);
+                        *result = NULL;
+                        return E_OUTOFMEMORY;
+                    }
+
+                    tmp[new_len+offset] = '\0';
+                    path = tmp;
+                    path_len = new_len+offset;
+                }
+            }
+
+            if(relative->query_start > -1) {
+                data.query = relative->canon_uri+relative->query_start;
+                data.query_len = relative->query_len;
+            }
+
+            /* Make sure the path component is valid. */
+            ptr = path;
+            pptr = &ptr;
+            if((data.is_opaque && !parse_path_opaque(pptr, &data, 0)) ||
+               (!data.is_opaque && !parse_path_hierarchical(pptr, &data, 0))) {
+                heap_free(path);
+                *result = NULL;
+                return E_INVALIDARG;
+            }
+        }
+
+        if(relative->fragment_start > -1) {
+            data.fragment = relative->canon_uri+relative->fragment_start;
+            data.fragment_len = relative->fragment_len;
+        }
+
+        if(flags & URL_DONT_SIMPLIFY)
+            raw_flags |= RAW_URI_FORCE_PORT_DISP;
+        if(flags & URL_FILE_USE_PATHURL)
+            raw_flags |= RAW_URI_CONVERT_TO_DOS_PATH;
+
+        len = generate_raw_uri(&data, data.uri, raw_flags);
+        data.uri = SysAllocStringLen(NULL, len);
+        if(!data.uri) {
+            heap_free(path);
+            *result = NULL;
+            return E_OUTOFMEMORY;
+        }
+
+        generate_raw_uri(&data, data.uri, raw_flags);
+
+        ret = create_uri_obj();
+        if(!ret) {
+            SysFreeString(data.uri);
+            heap_free(path);
+            *result = NULL;
+            return E_OUTOFMEMORY;
+        }
+
+        if(flags & URL_DONT_SIMPLIFY)
+            create_flags |= Uri_CREATE_NO_CANONICALIZE;
+        if(flags & URL_FILE_USE_PATHURL)
+            create_flags |= Uri_CREATE_FILE_USE_DOS_PATH;
+
+        ret->raw_uri = data.uri;
+        hr = canonicalize_uri(&data, ret, create_flags);
+        if(FAILED(hr)) {
+            IUri_Release(&ret->IUri_iface);
+            *result = NULL;
+            return hr;
+        }
+
+        if(flags & URL_DONT_SIMPLIFY)
+            ret->display_modifiers |= URI_DISPLAY_NO_DEFAULT_PORT_AUTH;
+
+        apply_default_flags(&create_flags);
+        ret->create_flags = create_flags;
+        *result = &ret->IUri_iface;
+
+        heap_free(path);
+    }
+
+    return S_OK;
+}
+
+/***********************************************************************
+ *           CoInternetCombineIUri (urlmon.@)
+ */
+HRESULT WINAPI CoInternetCombineIUri(IUri *pBaseUri, IUri *pRelativeUri, DWORD dwCombineFlags,
+                                     IUri **ppCombinedUri, DWORD_PTR dwReserved)
+{
+    HRESULT hr;
+    IInternetProtocolInfo *info;
+    Uri *relative, *base;
+    TRACE("(%p %p %x %p %x)\n", pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
+
+    if(!ppCombinedUri)
+        return E_INVALIDARG;
+
+    if(!pBaseUri || !pRelativeUri) {
+        *ppCombinedUri = NULL;
+        return E_INVALIDARG;
+    }
+
+    relative = get_uri_obj(pRelativeUri);
+    base = get_uri_obj(pBaseUri);
+    if(!relative || !base) {
+        *ppCombinedUri = NULL;
+        FIXME("(%p %p %x %p %x) Unknown IUri types not supported yet.\n",
+            pBaseUri, pRelativeUri, dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
+        return E_NOTIMPL;
+    }
+
+    info = get_protocol_info(base->canon_uri);
+    if(info) {
+        WCHAR result[INTERNET_MAX_URL_LENGTH+1];
+        DWORD result_len = 0;
+
+        hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, relative->canon_uri, dwCombineFlags,
+                                              result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
+        IInternetProtocolInfo_Release(info);
+        if(SUCCEEDED(hr)) {
+            hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
+            if(SUCCEEDED(hr))
+                return hr;
+        }
+    }
+
+    return combine_uri(base, relative, dwCombineFlags, ppCombinedUri, 0);
+}
+
+/***********************************************************************
+ *           CoInternetCombineUrlEx (urlmon.@)
+ */
+HRESULT WINAPI CoInternetCombineUrlEx(IUri *pBaseUri, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags,
+                                      IUri **ppCombinedUri, DWORD_PTR dwReserved)
+{
+    IUri *relative;
+    Uri *base;
+    HRESULT hr;
+    IInternetProtocolInfo *info;
+
+    TRACE("(%p %s %x %p %x) stub\n", pBaseUri, debugstr_w(pwzRelativeUrl), dwCombineFlags,
+        ppCombinedUri, (DWORD)dwReserved);
+
+    if(!ppCombinedUri)
+        return E_POINTER;
+
+    if(!pwzRelativeUrl) {
+        *ppCombinedUri = NULL;
+        return E_UNEXPECTED;
+    }
+
+    if(!pBaseUri) {
+        *ppCombinedUri = NULL;
+        return E_INVALIDARG;
+    }
+
+    base = get_uri_obj(pBaseUri);
+    if(!base) {
+        *ppCombinedUri = NULL;
+        FIXME("(%p %s %x %p %x) Unknown IUri's not supported yet.\n", pBaseUri, debugstr_w(pwzRelativeUrl),
+            dwCombineFlags, ppCombinedUri, (DWORD)dwReserved);
+        return E_NOTIMPL;
+    }
+
+    info = get_protocol_info(base->canon_uri);
+    if(info) {
+        WCHAR result[INTERNET_MAX_URL_LENGTH+1];
+        DWORD result_len = 0;
+
+        hr = IInternetProtocolInfo_CombineUrl(info, base->canon_uri, pwzRelativeUrl, dwCombineFlags,
+                                              result, INTERNET_MAX_URL_LENGTH+1, &result_len, 0);
+        IInternetProtocolInfo_Release(info);
+        if(SUCCEEDED(hr)) {
+            hr = CreateUri(result, Uri_CREATE_ALLOW_RELATIVE, 0, ppCombinedUri);
+            if(SUCCEEDED(hr))
+                return hr;
+        }
+    }
+
+    hr = CreateUri(pwzRelativeUrl, Uri_CREATE_ALLOW_RELATIVE, 0, &relative);
+    if(FAILED(hr)) {
+        *ppCombinedUri = NULL;
+        return hr;
+    }
+
+    hr = combine_uri(base, get_uri_obj(relative), dwCombineFlags, ppCombinedUri, COMBINE_URI_FORCE_FLAG_USE);
+
+    IUri_Release(relative);
+    return hr;
+}
+
+static HRESULT parse_canonicalize(const Uri *uri, DWORD flags, LPWSTR output,
+                                  DWORD output_len, DWORD *result_len)
+{
+    const WCHAR *ptr = NULL;
+    WCHAR *path = NULL;
+    const WCHAR **pptr;
+    WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
+    DWORD len = 0;
+    BOOL reduce_path;
+
+    /* URL_UNESCAPE only has effect if none of the URL_ESCAPE flags are set. */
+    const BOOL allow_unescape = !(flags & URL_ESCAPE_UNSAFE) &&
+                                !(flags & URL_ESCAPE_SPACES_ONLY) &&
+                                !(flags & URL_ESCAPE_PERCENT);
+
+
+    /* Check if the dot segments need to be removed from the
+     * path component.
+     */
+    if(uri->scheme_start > -1 && uri->path_start > -1) {
+        ptr = uri->canon_uri+uri->scheme_start+uri->scheme_len+1;
+        pptr = &ptr;
+    }
+    reduce_path = !(flags & URL_NO_META) &&
+                  !(flags & URL_DONT_SIMPLIFY) &&
+                  ptr && check_hierarchical(pptr);
+
+    for(ptr = uri->canon_uri; ptr < uri->canon_uri+uri->canon_len; ++ptr) {
+        BOOL do_default_action = TRUE;
+
+        /* Keep track of the path if we need to remove dot segments from
+         * it later.
+         */
+        if(reduce_path && !path && ptr == uri->canon_uri+uri->path_start)
+            path = buffer+len;
+
+        /* Check if it's time to reduce the path. */
+        if(reduce_path && ptr == uri->canon_uri+uri->path_start+uri->path_len) {
+            DWORD current_path_len = (buffer+len) - path;
+            DWORD new_path_len = remove_dot_segments(path, current_path_len);
+
+            /* Update the current length. */
+            len -= (current_path_len-new_path_len);
+            reduce_path = FALSE;
+        }
+
+        if(*ptr == '%') {
+            const WCHAR decoded = decode_pct_val(ptr);
+            if(decoded) {
+                if(allow_unescape && (flags & URL_UNESCAPE)) {
+                    buffer[len++] = decoded;
+                    ptr += 2;
+                    do_default_action = FALSE;
+                }
+            }
+
+            /* See if %'s needed to encoded. */
+            if(do_default_action && (flags & URL_ESCAPE_PERCENT)) {
+                pct_encode_val(*ptr, buffer+len);
+                len += 3;
+                do_default_action = FALSE;
+            }
+        } else if(*ptr == ' ') {
+            if((flags & URL_ESCAPE_SPACES_ONLY) &&
+               !(flags & URL_ESCAPE_UNSAFE)) {
+                pct_encode_val(*ptr, buffer+len);
+                len += 3;
+                do_default_action = FALSE;
+            }
+        } else if(!is_reserved(*ptr) && !is_unreserved(*ptr)) {
+            if(flags & URL_ESCAPE_UNSAFE) {
+                pct_encode_val(*ptr, buffer+len);
+                len += 3;
+                do_default_action = FALSE;
+            }
+        }
+
+        if(do_default_action)
+            buffer[len++] = *ptr;
+    }
+
+    /* Sometimes the path is the very last component of the IUri, so
+     * see if the dot segments need to be reduced now.
+     */
+    if(reduce_path && path) {
+        DWORD current_path_len = (buffer+len) - path;
+        DWORD new_path_len = remove_dot_segments(path, current_path_len);
+
+        /* Update the current length. */
+        len -= (current_path_len-new_path_len);
+    }
+
+    buffer[len++] = 0;
+
+    /* The null terminator isn't included the length. */
+    *result_len = len-1;
+    if(len > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+    else
+        memcpy(output, buffer, len*sizeof(WCHAR));
+
+    return S_OK;
+}
+
+static HRESULT parse_friendly(IUri *uri, LPWSTR output, DWORD output_len,
+                              DWORD *result_len)
+{
+    HRESULT hr;
+    DWORD display_len;
+    BSTR display;
+
+    hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DISPLAY_URI, &display_len, 0);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    *result_len = display_len;
+    if(display_len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    hr = IUri_GetDisplayUri(uri, &display);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    memcpy(output, display, (display_len+1)*sizeof(WCHAR));
+    SysFreeString(display);
+    return S_OK;
+}
+
+static HRESULT parse_rootdocument(const Uri *uri, LPWSTR output, DWORD output_len,
+                                  DWORD *result_len)
+{
+    static const WCHAR colon_slashesW[] = {':','/','/'};
+
+    WCHAR *ptr;
+    DWORD len = 0;
+
+    /* Windows only returns the root document if the URI has an authority
+     * and it's not an unknown scheme type or a file scheme type.
+     */
+    if(uri->authority_start == -1 ||
+       uri->scheme_type == URL_SCHEME_UNKNOWN ||
+       uri->scheme_type == URL_SCHEME_FILE) {
+        *result_len = 0;
+        if(!output_len)
+            return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+        output[0] = 0;
+        return S_OK;
+    }
+
+    len = uri->scheme_len+uri->authority_len;
+    /* For the "://" and '/' which will be added. */
+    len += 4;
+
+    if(len+1 > output_len) {
+        *result_len = len;
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+    }
+
+    ptr = output;
+    memcpy(ptr, uri->canon_uri+uri->scheme_start, uri->scheme_len*sizeof(WCHAR));
+
+    /* Add the "://". */
+    ptr += uri->scheme_len;
+    memcpy(ptr, colon_slashesW, sizeof(colon_slashesW));
+
+    /* Add the authority. */
+    ptr += sizeof(colon_slashesW)/sizeof(WCHAR);
+    memcpy(ptr, uri->canon_uri+uri->authority_start, uri->authority_len*sizeof(WCHAR));
+
+    /* Add the '/' after the authority. */
+    ptr += uri->authority_len;
+    *ptr = '/';
+    ptr[1] = 0;
+
+    *result_len = len;
+    return S_OK;
+}
+
+static HRESULT parse_document(const Uri *uri, LPWSTR output, DWORD output_len,
+                              DWORD *result_len)
+{
+    DWORD len = 0;
+
+    /* It has to be a known scheme type, but, it can't be a file
+     * scheme. It also has to hierarchical.
+     */
+    if(uri->scheme_type == URL_SCHEME_UNKNOWN ||
+       uri->scheme_type == URL_SCHEME_FILE ||
+       uri->authority_start == -1) {
+        *result_len = 0;
+        if(output_len < 1)
+            return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+        output[0] = 0;
+        return S_OK;
+    }
+
+    if(uri->fragment_start > -1)
+        len = uri->fragment_start;
+    else
+        len = uri->canon_len;
+
+    *result_len = len;
+    if(len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    memcpy(output, uri->canon_uri, len*sizeof(WCHAR));
+    output[len] = 0;
+    return S_OK;
+}
+
+static HRESULT parse_path_from_url(const Uri *uri, LPWSTR output, DWORD output_len,
+                                   DWORD *result_len)
+{
+    const WCHAR *path_ptr;
+    WCHAR buffer[INTERNET_MAX_URL_LENGTH+1];
+    WCHAR *ptr;
+
+    if(uri->scheme_type != URL_SCHEME_FILE) {
+        *result_len = 0;
+        if(output_len > 0)
+            output[0] = 0;
+        return E_INVALIDARG;
+    }
+
+    ptr = buffer;
+    if(uri->host_start > -1) {
+        static const WCHAR slash_slashW[] = {'\\','\\'};
+
+        memcpy(ptr, slash_slashW, sizeof(slash_slashW));
+        ptr += sizeof(slash_slashW)/sizeof(WCHAR);
+        memcpy(ptr, uri->canon_uri+uri->host_start, uri->host_len*sizeof(WCHAR));
+        ptr += uri->host_len;
+    }
+
+    path_ptr = uri->canon_uri+uri->path_start;
+    if(uri->path_len > 3 && *path_ptr == '/' && is_drive_path(path_ptr+1))
+        /* Skip past the '/' in front of the drive path. */
+        ++path_ptr;
+
+    for(; path_ptr < uri->canon_uri+uri->path_start+uri->path_len; ++path_ptr, ++ptr) {
+        BOOL do_default_action = TRUE;
+
+        if(*path_ptr == '%') {
+            const WCHAR decoded = decode_pct_val(path_ptr);
+            if(decoded) {
+                *ptr = decoded;
+                path_ptr += 2;
+                do_default_action = FALSE;
+            }
+        } else if(*path_ptr == '/') {
+            *ptr = '\\';
+            do_default_action = FALSE;
+        }
+
+        if(do_default_action)
+            *ptr = *path_ptr;
+    }
+
+    *ptr = 0;
+
+    *result_len = ptr-buffer;
+    if(*result_len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    memcpy(output, buffer, (*result_len+1)*sizeof(WCHAR));
+    return S_OK;
+}
+
+static HRESULT parse_url_from_path(IUri *uri, LPWSTR output, DWORD output_len,
+                                   DWORD *result_len)
+{
+    HRESULT hr;
+    BSTR received;
+    DWORD len = 0;
+
+    hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_ABSOLUTE_URI, &len, 0);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    *result_len = len;
+    if(len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    hr = IUri_GetAbsoluteUri(uri, &received);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    memcpy(output, received, (len+1)*sizeof(WCHAR));
+    SysFreeString(received);
+
+    return S_OK;
+}
+
+static HRESULT parse_schema(IUri *uri, LPWSTR output, DWORD output_len,
+                            DWORD *result_len)
+{
+    HRESULT hr;
+    DWORD len;
+    BSTR received;
+
+    hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_SCHEME_NAME, &len, 0);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    *result_len = len;
+    if(len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    hr = IUri_GetSchemeName(uri, &received);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    memcpy(output, received, (len+1)*sizeof(WCHAR));
+    SysFreeString(received);
+
+    return S_OK;
+}
+
+static HRESULT parse_site(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
+{
+    HRESULT hr;
+    DWORD len;
+    BSTR received;
+
+    hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_HOST, &len, 0);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    *result_len = len;
+    if(len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    hr = IUri_GetHost(uri, &received);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    memcpy(output, received, (len+1)*sizeof(WCHAR));
+    SysFreeString(received);
+
+    return S_OK;
+}
+
+static HRESULT parse_domain(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
+{
+    HRESULT hr;
+    DWORD len;
+    BSTR received;
+
+    hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_DOMAIN, &len, 0);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    *result_len = len;
+    if(len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    hr = IUri_GetDomain(uri, &received);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    memcpy(output, received, (len+1)*sizeof(WCHAR));
+    SysFreeString(received);
+
+    return S_OK;
+}
+
+static HRESULT parse_anchor(IUri *uri, LPWSTR output, DWORD output_len, DWORD *result_len)
+{
+    HRESULT hr;
+    DWORD len;
+    BSTR received;
+
+    hr = IUri_GetPropertyLength(uri, Uri_PROPERTY_FRAGMENT, &len, 0);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    *result_len = len;
+    if(len+1 > output_len)
+        return STRSAFE_E_INSUFFICIENT_BUFFER;
+
+    hr = IUri_GetFragment(uri, &received);
+    if(FAILED(hr)) {
+        *result_len = 0;
+        return hr;
+    }
+
+    memcpy(output, received, (len+1)*sizeof(WCHAR));
+    SysFreeString(received);
+
+    return S_OK;
+}
+
+/***********************************************************************
+ *           CoInternetParseIUri (urlmon.@)
+ */
+HRESULT WINAPI CoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, DWORD dwFlags,
+                                   LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult,
+                                   DWORD_PTR dwReserved)
+{
+    HRESULT hr;
+    Uri *uri;
+    IInternetProtocolInfo *info;
+
+    TRACE("(%p %d %x %p %d %p %x)\n", pIUri, ParseAction, dwFlags, pwzResult,
+        cchResult, pcchResult, (DWORD)dwReserved);
+
+    if(!pcchResult)
+        return E_POINTER;
+
+    if(!pwzResult || !pIUri) {
+        *pcchResult = 0;
+        return E_INVALIDARG;
+    }
+
+    if(!(uri = get_uri_obj(pIUri))) {
+        *pcchResult = 0;
+        FIXME("(%p %d %x %p %d %p %x) Unknown IUri's not supported for this action.\n",
+            pIUri, ParseAction, dwFlags, pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
+        return E_NOTIMPL;
+    }
+
+    info = get_protocol_info(uri->canon_uri);
+    if(info) {
+        hr = IInternetProtocolInfo_ParseUrl(info, uri->canon_uri, ParseAction, dwFlags,
+                                            pwzResult, cchResult, pcchResult, 0);
+        IInternetProtocolInfo_Release(info);
+        if(SUCCEEDED(hr)) return hr;
+    }
+
+    switch(ParseAction) {
+    case PARSE_CANONICALIZE:
+        hr = parse_canonicalize(uri, dwFlags, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_FRIENDLY:
+        hr = parse_friendly(pIUri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_ROOTDOCUMENT:
+        hr = parse_rootdocument(uri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_DOCUMENT:
+        hr = parse_document(uri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_PATH_FROM_URL:
+        hr = parse_path_from_url(uri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_URL_FROM_PATH:
+        hr = parse_url_from_path(pIUri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_SCHEMA:
+        hr = parse_schema(pIUri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_SITE:
+        hr = parse_site(pIUri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_DOMAIN:
+        hr = parse_domain(pIUri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_LOCATION:
+    case PARSE_ANCHOR:
+        hr = parse_anchor(pIUri, pwzResult, cchResult, pcchResult);
+        break;
+    case PARSE_SECURITY_URL:
+    case PARSE_MIME:
+    case PARSE_SERVER:
+    case PARSE_SECURITY_DOMAIN:
+        *pcchResult = 0;
+        hr = E_FAIL;
+        break;
+    default:
+        *pcchResult = 0;
+        hr = E_NOTIMPL;
+        FIXME("(%p %d %x %p %d %p %x) Partial stub.\n", pIUri, ParseAction, dwFlags,
+            pwzResult, cchResult, pcchResult, (DWORD)dwReserved);
+    }
+
+    return hr;
+}