urlmon: Fixed the scheme name parser to handle wildcard schemes.
[wine] / dlls / urlmon / uri.c
1 /*
2  * Copyright 2010 Jacek Caban for CodeWeavers
3  * Copyright 2010 Thomas Mullaly
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "urlmon_main.h"
21 #include "wine/debug.h"
22
23 #define NO_SHLWAPI_REG
24 #include "shlwapi.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
27
28 typedef struct {
29     const IUriVtbl  *lpIUriVtbl;
30     LONG ref;
31     BSTR        raw_uri;
32
33     /* Information about the canonicalized URI's buffer. */
34     WCHAR       *canon_uri;
35     DWORD       canon_size;
36     DWORD       canon_len;
37
38     INT         scheme_start;
39     DWORD       scheme_len;
40     URL_SCHEME  scheme_type;
41 } Uri;
42
43 typedef struct {
44     const IUriBuilderVtbl  *lpIUriBuilderVtbl;
45     LONG ref;
46 } UriBuilder;
47
48 typedef struct {
49     BSTR            uri;
50
51     BOOL            is_relative;
52
53     const WCHAR     *scheme;
54     DWORD           scheme_len;
55     URL_SCHEME      scheme_type;
56 } parse_data;
57
58 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
59 static const struct {
60     URL_SCHEME  scheme;
61     WCHAR       scheme_name[16];
62 } recognized_schemes[] = {
63     {URL_SCHEME_FTP,            {'f','t','p',0}},
64     {URL_SCHEME_HTTP,           {'h','t','t','p',0}},
65     {URL_SCHEME_GOPHER,         {'g','o','p','h','e','r',0}},
66     {URL_SCHEME_MAILTO,         {'m','a','i','l','t','o',0}},
67     {URL_SCHEME_NEWS,           {'n','e','w','s',0}},
68     {URL_SCHEME_NNTP,           {'n','n','t','p',0}},
69     {URL_SCHEME_TELNET,         {'t','e','l','n','e','t',0}},
70     {URL_SCHEME_WAIS,           {'w','a','i','s',0}},
71     {URL_SCHEME_FILE,           {'f','i','l','e',0}},
72     {URL_SCHEME_MK,             {'m','k',0}},
73     {URL_SCHEME_HTTPS,          {'h','t','t','p','s',0}},
74     {URL_SCHEME_SHELL,          {'s','h','e','l','l',0}},
75     {URL_SCHEME_SNEWS,          {'s','n','e','w','s',0}},
76     {URL_SCHEME_LOCAL,          {'l','o','c','a','l',0}},
77     {URL_SCHEME_JAVASCRIPT,     {'j','a','v','a','s','c','r','i','p','t',0}},
78     {URL_SCHEME_VBSCRIPT,       {'v','b','s','c','r','i','p','t',0}},
79     {URL_SCHEME_ABOUT,          {'a','b','o','u','t',0}},
80     {URL_SCHEME_RES,            {'r','e','s',0}},
81     {URL_SCHEME_MSSHELLROOTED,  {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
82     {URL_SCHEME_MSSHELLIDLIST,  {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
83     {URL_SCHEME_MSHELP,         {'h','c','p',0}},
84     {URL_SCHEME_WILDCARD,       {'*',0}}
85 };
86
87 static inline BOOL is_alpha(WCHAR val) {
88         return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
89 }
90
91 static inline BOOL is_num(WCHAR val) {
92         return (val >= '0' && val <= '9');
93 }
94
95 /* A URI is implicitly a file path if it begins with
96  * a drive letter (eg X:) or starts with "\\" (UNC path).
97  */
98 static inline BOOL is_implicit_file_path(const WCHAR *str) {
99     if(is_alpha(str[0]) && str[1] == ':')
100         return TRUE;
101     else if(str[0] == '\\' && str[1] == '\\')
102         return TRUE;
103
104     return FALSE;
105 }
106
107 /* Tries to parse the scheme name of the URI.
108  *
109  * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
110  * NOTE: Windows accepts a number as the first character of a scheme.
111  */
112 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data) {
113     const WCHAR *start = *ptr;
114
115     data->scheme = NULL;
116     data->scheme_len = 0;
117
118     while(**ptr) {
119         if(**ptr == '*' && *ptr == start) {
120             /* Might have found a wildcard scheme. If it is the next
121              * char has to be a ':' for it to be a valid URI
122              */
123             ++(*ptr);
124             break;
125         } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
126            **ptr != '-' && **ptr != '.')
127             break;
128
129         (*ptr)++;
130     }
131
132     if(*ptr == start)
133         return FALSE;
134
135     /* Schemes must end with a ':' */
136     if(**ptr != ':') {
137         *ptr = start;
138         return FALSE;
139     }
140
141     data->scheme = start;
142     data->scheme_len = *ptr - start;
143
144     ++(*ptr);
145     return TRUE;
146 }
147
148 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
149  * the deduced URL_SCHEME in data->scheme_type.
150  */
151 static BOOL parse_scheme_type(parse_data *data) {
152     /* If there's scheme data then see if it's a recognized scheme. */
153     if(data->scheme && data->scheme_len) {
154         DWORD i;
155
156         for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
157             if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
158                 /* Has to be a case insensitive compare. */
159                 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
160                     data->scheme_type = recognized_schemes[i].scheme;
161                     return TRUE;
162                 }
163             }
164         }
165
166         /* If we get here it means it's not a recognized scheme. */
167         data->scheme_type = URL_SCHEME_UNKNOWN;
168         return TRUE;
169     } else if(data->is_relative) {
170         /* Relative URI's have no scheme. */
171         data->scheme_type = URL_SCHEME_UNKNOWN;
172         return TRUE;
173     } else {
174         /* Should never reach here! what happened... */
175         FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
176         return FALSE;
177     }
178 }
179
180 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
181  * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
182  * using the flags specified in 'flags' (if any). Flags that affect how this function
183  * operates are the Uri_CREATE_ALLOW_* flags.
184  *
185  * All parsed/deduced information will be stored in 'data' when the function returns.
186  *
187  * Returns TRUE if it was able to successfully parse the information.
188  */
189 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags) {
190     static const WCHAR fileW[] = {'f','i','l','e',0};
191     static const WCHAR wildcardW[] = {'*',0};
192
193     /* First check to see if the uri could implicitly be a file path. */
194     if(is_implicit_file_path(*ptr)) {
195         if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
196             data->scheme = fileW;
197             data->scheme_len = lstrlenW(fileW);
198             TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
199         } else {
200             /* Window's does not consider anything that can implicitly be a file
201              * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
202              */
203             TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
204                     ptr, data, flags);
205             return FALSE;
206         }
207     } else if(!parse_scheme_name(ptr, data)) {
208         /* No Scheme was found, this means it could be:
209          *      a) an implicit Wildcard scheme
210          *      b) a relative URI
211          *      c) a invalid URI.
212          */
213         if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
214             data->scheme = wildcardW;
215             data->scheme_len = lstrlenW(wildcardW);
216
217             TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
218         } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
219             data->is_relative = TRUE;
220             TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
221         } else {
222             TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
223             return FALSE;
224         }
225     }
226
227     if(!data->is_relative)
228         TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
229                 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
230
231     if(!parse_scheme_type(data))
232         return FALSE;
233
234     TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
235     return TRUE;
236 }
237
238 /* Parses and validates the components of the specified by data->uri
239  * and stores the information it parses into 'data'.
240  *
241  * Returns TRUE if it successfully parsed the URI. False otherwise.
242  */
243 static BOOL parse_uri(parse_data *data, DWORD flags) {
244     const WCHAR *ptr;
245     const WCHAR **pptr;
246
247     ptr = data->uri;
248     pptr = &ptr;
249
250     TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
251
252     if(!parse_scheme(pptr, data, flags))
253         return FALSE;
254
255     TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
256     return TRUE;
257 }
258
259 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
260 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
261     uri->scheme_start = -1;
262     uri->scheme_len = 0;
263
264     if(!data->scheme) {
265         /* The only type of URI that doesn't have to have a scheme is a relative
266          * URI.
267          */
268         if(!data->is_relative) {
269             FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
270                     uri, flags, debugstr_w(data->uri));
271             return FALSE;
272         }
273     } else {
274         if(!computeOnly) {
275             DWORD i;
276             INT pos = uri->canon_len;
277
278             for(i = 0; i < data->scheme_len; ++i) {
279                 /* Scheme name must be lower case after canonicalization. */
280                 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
281             }
282
283             uri->canon_uri[i + pos] = ':';
284             uri->scheme_start = pos;
285
286             TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
287                     debugstr_wn(uri->canon_uri,  uri->scheme_len), data->scheme_len);
288         }
289
290         /* This happens in both compute only and non-compute only. */
291         uri->canon_len += data->scheme_len + 1;
292         uri->scheme_len = data->scheme_len;
293     }
294     return TRUE;
295 }
296
297 /* Compute's what the length of the URI specified by the parse_data will be
298  * after canonicalization occurs using the specified flags.
299  *
300  * This function will return a non-zero value indicating the length of the canonicalized
301  * URI, or -1 on error.
302  */
303 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
304     Uri uri;
305
306     memset(&uri, 0, sizeof(Uri));
307
308     TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
309             debugstr_w(data->uri));
310
311     if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
312         ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
313         return -1;
314     }
315
316     TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
317
318     return uri.canon_len;
319 }
320
321 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
322  * canonicalization succeededs it will store all the canonicalization information
323  * in the pointer to the Uri.
324  *
325  * To canonicalize a URI this function first computes what the length of the URI
326  * specified by the parse_data will be. Once this is done it will then perfom the actual
327  * canonicalization of the URI.
328  */
329 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
330     INT len;
331
332     uri->canon_uri = NULL;
333     len = uri->canon_size = uri->canon_len = 0;
334
335     TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
336
337     /* First try to compute the length of the URI. */
338     len = compute_canonicalized_length(data, flags);
339     if(len == -1) {
340         ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
341                 debugstr_w(data->uri));
342         return E_INVALIDARG;
343     }
344
345     uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
346     if(!uri->canon_uri)
347         return E_OUTOFMEMORY;
348
349     if(!canonicalize_scheme(data, uri, flags, FALSE)) {
350         ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
351         heap_free(uri->canon_uri);
352         return E_INVALIDARG;
353     }
354     uri->scheme_type = data->scheme_type;
355
356     uri->canon_uri[uri->canon_len] = '\0';
357     TRACE("(%p %p %x): finished canonicalizing the URI.\n", data, uri, flags);
358
359     return S_OK;
360 }
361
362 #define URI(x)         ((IUri*)  &(x)->lpIUriVtbl)
363 #define URIBUILDER(x)  ((IUriBuilder*)  &(x)->lpIUriBuilderVtbl)
364
365 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
366
367 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
368 {
369     Uri *This = URI_THIS(iface);
370
371     if(IsEqualGUID(&IID_IUnknown, riid)) {
372         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
373         *ppv = URI(This);
374     }else if(IsEqualGUID(&IID_IUri, riid)) {
375         TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
376         *ppv = URI(This);
377     }else {
378         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
379         *ppv = NULL;
380         return E_NOINTERFACE;
381     }
382
383     IUnknown_AddRef((IUnknown*)*ppv);
384     return S_OK;
385 }
386
387 static ULONG WINAPI Uri_AddRef(IUri *iface)
388 {
389     Uri *This = URI_THIS(iface);
390     LONG ref = InterlockedIncrement(&This->ref);
391
392     TRACE("(%p) ref=%d\n", This, ref);
393
394     return ref;
395 }
396
397 static ULONG WINAPI Uri_Release(IUri *iface)
398 {
399     Uri *This = URI_THIS(iface);
400     LONG ref = InterlockedDecrement(&This->ref);
401
402     TRACE("(%p) ref=%d\n", This, ref);
403
404     if(!ref) {
405         SysFreeString(This->raw_uri);
406         heap_free(This->canon_uri);
407         heap_free(This);
408     }
409
410     return ref;
411 }
412
413 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
414 {
415     Uri *This = URI_THIS(iface);
416     HRESULT hres;
417     TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
418
419     if(!pbstrProperty)
420         return E_POINTER;
421
422     if(uriProp > Uri_PROPERTY_STRING_LAST) {
423         /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
424         *pbstrProperty = SysAllocStringLen(NULL, 0);
425         if(!(*pbstrProperty))
426             return E_OUTOFMEMORY;
427
428         /* It only returns S_FALSE for the ZONE property... */
429         if(uriProp == Uri_PROPERTY_ZONE)
430             return S_FALSE;
431         else
432             return S_OK;
433     }
434
435     /* Don't have support for flags yet. */
436     if(dwFlags) {
437         FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
438         return E_NOTIMPL;
439     }
440
441     switch(uriProp) {
442     case Uri_PROPERTY_RAW_URI:
443         *pbstrProperty = SysAllocString(This->raw_uri);
444         if(!(*pbstrProperty))
445             hres = E_OUTOFMEMORY;
446         else
447             hres = S_OK;
448         break;
449     case Uri_PROPERTY_SCHEME_NAME:
450         if(This->scheme_start > -1) {
451             *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
452             hres = S_OK;
453         } else {
454             *pbstrProperty = SysAllocStringLen(NULL, 0);
455             hres = S_FALSE;
456         }
457
458         if(!(*pbstrProperty))
459             hres = E_OUTOFMEMORY;
460
461         break;
462     default:
463         FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
464         hres = E_NOTIMPL;
465     }
466
467     return hres;
468 }
469
470 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
471 {
472     Uri *This = URI_THIS(iface);
473     HRESULT hres;
474     TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
475
476     if(!pcchProperty)
477         return E_INVALIDARG;
478
479     /* Can only return a length for a property if it's a string. */
480     if(uriProp > Uri_PROPERTY_STRING_LAST)
481         return E_INVALIDARG;
482
483     /* Don't have support for flags yet. */
484     if(dwFlags) {
485         FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
486         return E_NOTIMPL;
487     }
488
489     switch(uriProp) {
490     case Uri_PROPERTY_RAW_URI:
491         *pcchProperty = SysStringLen(This->raw_uri);
492         hres = S_OK;
493         break;
494     case Uri_PROPERTY_SCHEME_NAME:
495         *pcchProperty = This->scheme_len;
496         hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
497         break;
498     default:
499         FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
500         hres = E_NOTIMPL;
501     }
502
503     return hres;
504 }
505
506 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
507 {
508     Uri *This = URI_THIS(iface);
509     HRESULT hres;
510
511     TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
512
513     if(!pcchProperty)
514         return E_INVALIDARG;
515
516     /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
517      * From what I can tell, instead of checking which URLZONE the URI belongs to it
518      * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
519      * function.
520      */
521     if(uriProp == Uri_PROPERTY_ZONE) {
522         *pcchProperty = URLZONE_INVALID;
523         return E_NOTIMPL;
524     }
525
526     if(uriProp < Uri_PROPERTY_DWORD_START) {
527         *pcchProperty = 0;
528         return E_INVALIDARG;
529     }
530
531     switch(uriProp) {
532     case Uri_PROPERTY_SCHEME:
533         *pcchProperty = This->scheme_type;
534         hres = S_OK;
535         break;
536     default:
537         FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
538         hres = E_NOTIMPL;
539     }
540
541     return hres;
542 }
543
544 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
545 {
546     Uri *This = URI_THIS(iface);
547     FIXME("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
548
549     if(!pfHasProperty)
550         return E_INVALIDARG;
551
552     return E_NOTIMPL;
553 }
554
555 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
556 {
557     Uri *This = URI_THIS(iface);
558     FIXME("(%p)->(%p)\n", This, pstrAbsoluteUri);
559
560     if(!pstrAbsoluteUri)
561         return E_POINTER;
562
563     return E_NOTIMPL;
564 }
565
566 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
567 {
568     Uri *This = URI_THIS(iface);
569     FIXME("(%p)->(%p)\n", This, pstrAuthority);
570
571     if(!pstrAuthority)
572         return E_POINTER;
573
574     return E_NOTIMPL;
575 }
576
577 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
578 {
579     Uri *This = URI_THIS(iface);
580     FIXME("(%p)->(%p)\n", This, pstrDisplayUri);
581
582     if(!pstrDisplayUri)
583         return E_POINTER;
584
585     return E_NOTIMPL;
586 }
587
588 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
589 {
590     Uri *This = URI_THIS(iface);
591     FIXME("(%p)->(%p)\n", This, pstrDomain);
592
593     if(!pstrDomain)
594         return E_POINTER;
595
596     return E_NOTIMPL;
597 }
598
599 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
600 {
601     Uri *This = URI_THIS(iface);
602     FIXME("(%p)->(%p)\n", This, pstrExtension);
603
604     if(!pstrExtension)
605         return E_POINTER;
606
607     return E_NOTIMPL;
608 }
609
610 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
611 {
612     Uri *This = URI_THIS(iface);
613     FIXME("(%p)->(%p)\n", This, pstrFragment);
614
615     if(!pstrFragment)
616         return E_POINTER;
617
618     return E_NOTIMPL;
619 }
620
621 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
622 {
623     Uri *This = URI_THIS(iface);
624     FIXME("(%p)->(%p)\n", This, pstrHost);
625
626     if(!pstrHost)
627         return E_POINTER;
628
629     return E_NOTIMPL;
630 }
631
632 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
633 {
634     Uri *This = URI_THIS(iface);
635     FIXME("(%p)->(%p)\n", This, pstrPassword);
636
637     if(!pstrPassword)
638         return E_POINTER;
639
640     return E_NOTIMPL;
641 }
642
643 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
644 {
645     Uri *This = URI_THIS(iface);
646     FIXME("(%p)->(%p)\n", This, pstrPath);
647
648     if(!pstrPath)
649         return E_POINTER;
650
651     return E_NOTIMPL;
652 }
653
654 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
655 {
656     Uri *This = URI_THIS(iface);
657     FIXME("(%p)->(%p)\n", This, pstrPathAndQuery);
658
659     if(!pstrPathAndQuery)
660         return E_POINTER;
661
662     return E_NOTIMPL;
663 }
664
665 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
666 {
667     Uri *This = URI_THIS(iface);
668     FIXME("(%p)->(%p)\n", This, pstrQuery);
669
670     if(!pstrQuery)
671         return E_POINTER;
672
673     return E_NOTIMPL;
674 }
675
676 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
677 {
678     Uri *This = URI_THIS(iface);
679     TRACE("(%p)->(%p)\n", This, pstrRawUri);
680
681     /* Just forward the call to GetPropertyBSTR. */
682     return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
683 }
684
685 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
686 {
687     Uri *This = URI_THIS(iface);
688     TRACE("(%p)->(%p)\n", This, pstrSchemeName);
689     return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
690 }
691
692 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
693 {
694     Uri *This = URI_THIS(iface);
695     FIXME("(%p)->(%p)\n", This, pstrUserInfo);
696
697     if(!pstrUserInfo)
698         return E_POINTER;
699
700     return E_NOTIMPL;
701 }
702
703 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
704 {
705     Uri *This = URI_THIS(iface);
706     FIXME("(%p)->(%p)\n", This, pstrUserName);
707
708     if(!pstrUserName)
709         return E_POINTER;
710
711     return E_NOTIMPL;
712 }
713
714 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
715 {
716     Uri *This = URI_THIS(iface);
717     FIXME("(%p)->(%p)\n", This, pdwHostType);
718
719     if(!pdwHostType)
720         return E_INVALIDARG;
721
722     return E_NOTIMPL;
723 }
724
725 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
726 {
727     Uri *This = URI_THIS(iface);
728     FIXME("(%p)->(%p)\n", This, pdwPort);
729
730     if(!pdwPort)
731         return E_INVALIDARG;
732
733     return E_NOTIMPL;
734 }
735
736 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
737 {
738     Uri *This = URI_THIS(iface);
739     TRACE("(%p)->(%p)\n", This, pdwScheme);
740     return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
741 }
742
743 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
744 {
745     Uri *This = URI_THIS(iface);
746     FIXME("(%p)->(%p)\n", This, pdwZone);
747
748     if(!pdwZone)
749         return E_INVALIDARG;
750
751     /* Microsoft doesn't seem to have this implemented yet... See
752      * the comment in Uri_GetPropertyDWORD for more about this.
753      */
754     *pdwZone = URLZONE_INVALID;
755     return E_NOTIMPL;
756 }
757
758 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
759 {
760     Uri *This = URI_THIS(iface);
761     FIXME("(%p)->(%p)\n", This, pdwProperties);
762
763     if(!pdwProperties)
764         return E_INVALIDARG;
765
766     return E_NOTIMPL;
767 }
768
769 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
770 {
771     Uri *This = URI_THIS(iface);
772     TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
773
774     if(!pfEqual)
775         return E_POINTER;
776
777     if(!pUri) {
778         *pfEqual = FALSE;
779
780         /* For some reason Windows returns S_OK here... */
781         return S_OK;
782     }
783
784     FIXME("(%p)->(%p %p)\n", This, pUri, pfEqual);
785     return E_NOTIMPL;
786 }
787
788 #undef URI_THIS
789
790 static const IUriVtbl UriVtbl = {
791     Uri_QueryInterface,
792     Uri_AddRef,
793     Uri_Release,
794     Uri_GetPropertyBSTR,
795     Uri_GetPropertyLength,
796     Uri_GetPropertyDWORD,
797     Uri_HasProperty,
798     Uri_GetAbsoluteUri,
799     Uri_GetAuthority,
800     Uri_GetDisplayUri,
801     Uri_GetDomain,
802     Uri_GetExtension,
803     Uri_GetFragment,
804     Uri_GetHost,
805     Uri_GetPassword,
806     Uri_GetPath,
807     Uri_GetPathAndQuery,
808     Uri_GetQuery,
809     Uri_GetRawUri,
810     Uri_GetSchemeName,
811     Uri_GetUserInfo,
812     Uri_GetUserName,
813     Uri_GetHostType,
814     Uri_GetPort,
815     Uri_GetScheme,
816     Uri_GetZone,
817     Uri_GetProperties,
818     Uri_IsEqual
819 };
820
821 /***********************************************************************
822  *           CreateUri (urlmon.@)
823  */
824 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
825 {
826     Uri *ret;
827     HRESULT hr;
828     parse_data data;
829
830     TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
831
832     if(!ppURI)
833         return E_INVALIDARG;
834
835     if(!pwzURI) {
836         *ppURI = NULL;
837         return E_INVALIDARG;
838     }
839
840     ret = heap_alloc(sizeof(Uri));
841     if(!ret)
842         return E_OUTOFMEMORY;
843
844     ret->lpIUriVtbl = &UriVtbl;
845     ret->ref = 1;
846
847     /* Create a copy of pwzURI and store it as the raw_uri. */
848     ret->raw_uri = SysAllocString(pwzURI);
849     if(!ret->raw_uri) {
850         heap_free(ret);
851         return E_OUTOFMEMORY;
852     }
853
854     memset(&data, 0, sizeof(parse_data));
855     data.uri = ret->raw_uri;
856
857     /* Validate and parse the URI into it's components. */
858     if(!parse_uri(&data, dwFlags)) {
859         /* Encountered an unsupported or invalid URI */
860         SysFreeString(ret->raw_uri);
861         heap_free(ret);
862         *ppURI = NULL;
863         return E_INVALIDARG;
864     }
865
866     /* Canonicalize the URI. */
867     hr = canonicalize_uri(&data, ret, dwFlags);
868     if(FAILED(hr)) {
869         SysFreeString(ret->raw_uri);
870         heap_free(ret);
871         *ppURI = NULL;
872         return hr;
873     }
874
875     *ppURI = URI(ret);
876     return S_OK;
877 }
878
879 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
880
881 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
882 {
883     UriBuilder *This = URIBUILDER_THIS(iface);
884
885     if(IsEqualGUID(&IID_IUnknown, riid)) {
886         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
887         *ppv = URIBUILDER(This);
888     }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
889         TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
890         *ppv = URIBUILDER(This);
891     }else {
892         TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
893         *ppv = NULL;
894         return E_NOINTERFACE;
895     }
896
897     IUnknown_AddRef((IUnknown*)*ppv);
898     return S_OK;
899 }
900
901 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
902 {
903     UriBuilder *This = URIBUILDER_THIS(iface);
904     LONG ref = InterlockedIncrement(&This->ref);
905
906     TRACE("(%p) ref=%d\n", This, ref);
907
908     return ref;
909 }
910
911 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
912 {
913     UriBuilder *This = URIBUILDER_THIS(iface);
914     LONG ref = InterlockedDecrement(&This->ref);
915
916     TRACE("(%p) ref=%d\n", This, ref);
917
918     if(!ref)
919         heap_free(This);
920
921     return ref;
922 }
923
924 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
925                                                  DWORD        dwAllowEncodingPropertyMask,
926                                                  DWORD_PTR    dwReserved,
927                                                  IUri       **ppIUri)
928 {
929     UriBuilder *This = URIBUILDER_THIS(iface);
930     FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
931     return E_NOTIMPL;
932 }
933
934 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
935                                            DWORD        dwCreateFlags,
936                                            DWORD        dwAllowEncodingPropertyMask,
937                                            DWORD_PTR    dwReserved,
938                                            IUri       **ppIUri)
939 {
940     UriBuilder *This = URIBUILDER_THIS(iface);
941     FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
942     return E_NOTIMPL;
943 }
944
945 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
946                                          DWORD        dwCreateFlags,
947                                          DWORD        dwUriBuilderFlags,
948                                          DWORD        dwAllowEncodingPropertyMask,
949                                          DWORD_PTR    dwReserved,
950                                          IUri       **ppIUri)
951 {
952     UriBuilder *This = URIBUILDER_THIS(iface);
953     FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
954         dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
955     return E_NOTIMPL;
956 }
957
958 static HRESULT WINAPI  UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
959 {
960     UriBuilder *This = URIBUILDER_THIS(iface);
961     FIXME("(%p)->(%p)\n", This, ppIUri);
962     return E_NOTIMPL;
963 }
964
965 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
966 {
967     UriBuilder *This = URIBUILDER_THIS(iface);
968     FIXME("(%p)->(%p)\n", This, pIUri);
969     return E_NOTIMPL;
970 }
971
972 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
973 {
974     UriBuilder *This = URIBUILDER_THIS(iface);
975     FIXME("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
976     return E_NOTIMPL;
977 }
978
979 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
980 {
981     UriBuilder *This = URIBUILDER_THIS(iface);
982     FIXME("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
983     return E_NOTIMPL;
984 }
985
986 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
987 {
988     UriBuilder *This = URIBUILDER_THIS(iface);
989     FIXME("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
990     return E_NOTIMPL;
991 }
992
993 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
994 {
995     UriBuilder *This = URIBUILDER_THIS(iface);
996     FIXME("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
997     return E_NOTIMPL;
998 }
999
1000 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
1001 {
1002     UriBuilder *This = URIBUILDER_THIS(iface);
1003     FIXME("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
1004     return E_NOTIMPL;
1005 }
1006
1007 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
1008 {
1009     UriBuilder *This = URIBUILDER_THIS(iface);
1010     FIXME("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
1011     return E_NOTIMPL;
1012 }
1013
1014 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
1015 {
1016     UriBuilder *This = URIBUILDER_THIS(iface);
1017     FIXME("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
1018     return E_NOTIMPL;
1019 }
1020
1021 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
1022 {
1023     UriBuilder *This = URIBUILDER_THIS(iface);
1024     FIXME("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
1025     return E_NOTIMPL;
1026 }
1027
1028 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
1029 {
1030     UriBuilder *This = URIBUILDER_THIS(iface);
1031     FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
1032     return E_NOTIMPL;
1033 }
1034
1035 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
1036 {
1037     UriBuilder *This = URIBUILDER_THIS(iface);
1038     FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
1039     return E_NOTIMPL;
1040 }
1041
1042 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
1043 {
1044     UriBuilder *This = URIBUILDER_THIS(iface);
1045     FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
1046     return E_NOTIMPL;
1047 }
1048
1049 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
1050 {
1051     UriBuilder *This = URIBUILDER_THIS(iface);
1052     FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
1053     return E_NOTIMPL;
1054 }
1055
1056 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
1057 {
1058     UriBuilder *This = URIBUILDER_THIS(iface);
1059     FIXME("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
1060     return E_NOTIMPL;
1061 }
1062
1063 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
1064 {
1065     UriBuilder *This = URIBUILDER_THIS(iface);
1066     FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
1067     return E_NOTIMPL;
1068 }
1069
1070 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
1071 {
1072     UriBuilder *This = URIBUILDER_THIS(iface);
1073     FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
1074     return E_NOTIMPL;
1075 }
1076
1077 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
1078 {
1079     UriBuilder *This = URIBUILDER_THIS(iface);
1080     FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
1081     return E_NOTIMPL;
1082 }
1083
1084 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
1085 {
1086     UriBuilder *This = URIBUILDER_THIS(iface);
1087     FIXME("(%p)->(0x%08x)\n", This, dwPropertyMask);
1088     return E_NOTIMPL;
1089 }
1090
1091 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
1092 {
1093     UriBuilder *This = URIBUILDER_THIS(iface);
1094     FIXME("(%p)->(%p)\n", This, pfModified);
1095     return E_NOTIMPL;
1096 }
1097
1098 #undef URIBUILDER_THIS
1099
1100 static const IUriBuilderVtbl UriBuilderVtbl = {
1101     UriBuilder_QueryInterface,
1102     UriBuilder_AddRef,
1103     UriBuilder_Release,
1104     UriBuilder_CreateUriSimple,
1105     UriBuilder_CreateUri,
1106     UriBuilder_CreateUriWithFlags,
1107     UriBuilder_GetIUri,
1108     UriBuilder_SetIUri,
1109     UriBuilder_GetFragment,
1110     UriBuilder_GetHost,
1111     UriBuilder_GetPassword,
1112     UriBuilder_GetPath,
1113     UriBuilder_GetPort,
1114     UriBuilder_GetQuery,
1115     UriBuilder_GetSchemeName,
1116     UriBuilder_GetUserName,
1117     UriBuilder_SetFragment,
1118     UriBuilder_SetHost,
1119     UriBuilder_SetPassword,
1120     UriBuilder_SetPath,
1121     UriBuilder_SetPort,
1122     UriBuilder_SetQuery,
1123     UriBuilder_SetSchemeName,
1124     UriBuilder_SetUserName,
1125     UriBuilder_RemoveProperties,
1126     UriBuilder_HasBeenModified,
1127 };
1128
1129 /***********************************************************************
1130  *           CreateIUriBuilder (urlmon.@)
1131  */
1132 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
1133 {
1134     UriBuilder *ret;
1135
1136     TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
1137
1138     ret = heap_alloc(sizeof(UriBuilder));
1139     if(!ret)
1140         return E_OUTOFMEMORY;
1141
1142     ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
1143     ret->ref = 1;
1144
1145     *ppIUriBuilder = URIBUILDER(ret);
1146     return S_OK;
1147 }