aboutsummaryrefslogtreecommitdiff
path: root/src/dutil/uriutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/dutil/uriutil.cpp93
1 files changed, 54 insertions, 39 deletions
diff --git a/src/dutil/uriutil.cpp b/src/dutil/uriutil.cpp
index fc192b3f..7913c22e 100644
--- a/src/dutil/uriutil.cpp
+++ b/src/dutil/uriutil.cpp
@@ -3,6 +3,21 @@
3#include "precomp.h" 3#include "precomp.h"
4 4
5 5
6// Exit macros
7#define UriExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_URIUTIL, x, s, __VA_ARGS__)
8#define UriExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_URIUTIL, x, s, __VA_ARGS__)
9#define UriExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_URIUTIL, x, s, __VA_ARGS__)
10#define UriExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_URIUTIL, x, s, __VA_ARGS__)
11#define UriExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_URIUTIL, x, s, __VA_ARGS__)
12#define UriExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_URIUTIL, x, s, __VA_ARGS__)
13#define UriExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_URIUTIL, p, x, e, s, __VA_ARGS__)
14#define UriExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_URIUTIL, p, x, s, __VA_ARGS__)
15#define UriExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_URIUTIL, p, x, e, s, __VA_ARGS__)
16#define UriExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_URIUTIL, p, x, s, __VA_ARGS__)
17#define UriExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_URIUTIL, e, x, s, __VA_ARGS__)
18#define UriExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_URIUTIL, g, x, s, __VA_ARGS__)
19
20
6// 21//
7// UriCanonicalize - canonicalizes a URI. 22// UriCanonicalize - canonicalizes a URI.
8// 23//
@@ -16,11 +31,11 @@ extern "C" HRESULT DAPI UriCanonicalize(
16 31
17 if (!::InternetCanonicalizeUrlW(*psczUri, wz, &cch, ICU_DECODE)) 32 if (!::InternetCanonicalizeUrlW(*psczUri, wz, &cch, ICU_DECODE))
18 { 33 {
19 ExitWithLastError(hr, "Failed to canonicalize URI."); 34 UriExitWithLastError(hr, "Failed to canonicalize URI.");
20 } 35 }
21 36
22 hr = StrAllocString(psczUri, wz, cch); 37 hr = StrAllocString(psczUri, wz, cch);
23 ExitOnFailure(hr, "Failed copy canonicalized URI."); 38 UriExitOnFailure(hr, "Failed copy canonicalized URI.");
24 39
25LExit: 40LExit:
26 return hr; 41 return hr;
@@ -83,7 +98,7 @@ extern "C" HRESULT DAPI UriCrack(
83 98
84 if (!::InternetCrackUrlW(wzUri, 0, ICU_DECODE | ICU_ESCAPE, &components)) 99 if (!::InternetCrackUrlW(wzUri, 0, ICU_DECODE | ICU_ESCAPE, &components))
85 { 100 {
86 ExitWithLastError(hr, "Failed to crack URI."); 101 UriExitWithLastError(hr, "Failed to crack URI.");
87 } 102 }
88 103
89 if (pScheme) 104 if (pScheme)
@@ -94,7 +109,7 @@ extern "C" HRESULT DAPI UriCrack(
94 if (psczHostName) 109 if (psczHostName)
95 { 110 {
96 hr = StrAllocString(psczHostName, components.lpszHostName, components.dwHostNameLength); 111 hr = StrAllocString(psczHostName, components.lpszHostName, components.dwHostNameLength);
97 ExitOnFailure(hr, "Failed to copy host name."); 112 UriExitOnFailure(hr, "Failed to copy host name.");
98 } 113 }
99 114
100 if (pPort) 115 if (pPort)
@@ -105,25 +120,25 @@ extern "C" HRESULT DAPI UriCrack(
105 if (psczUser) 120 if (psczUser)
106 { 121 {
107 hr = StrAllocString(psczUser, components.lpszUserName, components.dwUserNameLength); 122 hr = StrAllocString(psczUser, components.lpszUserName, components.dwUserNameLength);
108 ExitOnFailure(hr, "Failed to copy user name."); 123 UriExitOnFailure(hr, "Failed to copy user name.");
109 } 124 }
110 125
111 if (psczPassword) 126 if (psczPassword)
112 { 127 {
113 hr = StrAllocString(psczPassword, components.lpszPassword, components.dwPasswordLength); 128 hr = StrAllocString(psczPassword, components.lpszPassword, components.dwPasswordLength);
114 ExitOnFailure(hr, "Failed to copy password."); 129 UriExitOnFailure(hr, "Failed to copy password.");
115 } 130 }
116 131
117 if (psczPath) 132 if (psczPath)
118 { 133 {
119 hr = StrAllocString(psczPath, components.lpszUrlPath, components.dwUrlPathLength); 134 hr = StrAllocString(psczPath, components.lpszUrlPath, components.dwUrlPathLength);
120 ExitOnFailure(hr, "Failed to copy path."); 135 UriExitOnFailure(hr, "Failed to copy path.");
121 } 136 }
122 137
123 if (psczQueryString) 138 if (psczQueryString)
124 { 139 {
125 hr = StrAllocString(psczQueryString, components.lpszExtraInfo, components.dwExtraInfoLength); 140 hr = StrAllocString(psczQueryString, components.lpszExtraInfo, components.dwExtraInfoLength);
126 ExitOnFailure(hr, "Failed to copy query string."); 141 UriExitOnFailure(hr, "Failed to copy query string.");
127 } 142 }
128 143
129LExit: 144LExit:
@@ -142,7 +157,7 @@ extern "C" HRESULT DAPI UriCrackEx(
142 HRESULT hr = S_OK; 157 HRESULT hr = S_OK;
143 158
144 hr = UriCrack(wzUri, &pUriInfo->scheme, &pUriInfo->sczHostName, &pUriInfo->port, &pUriInfo->sczUser, &pUriInfo->sczPassword, &pUriInfo->sczPath, &pUriInfo->sczQueryString); 159 hr = UriCrack(wzUri, &pUriInfo->scheme, &pUriInfo->sczHostName, &pUriInfo->port, &pUriInfo->sczUser, &pUriInfo->sczPassword, &pUriInfo->sczPath, &pUriInfo->sczQueryString);
145 ExitOnFailure(hr, "Failed to crack URI."); 160 UriExitOnFailure(hr, "Failed to crack URI.");
146 161
147LExit: 162LExit:
148 return hr; 163 return hr;
@@ -195,11 +210,11 @@ extern "C" HRESULT DAPI UriCreate(
195 210
196 if (!::InternetCreateUrlW(&components, ICU_ESCAPE, wz, &cch)) 211 if (!::InternetCreateUrlW(&components, ICU_ESCAPE, wz, &cch))
197 { 212 {
198 ExitWithLastError(hr, "Failed to create URI."); 213 UriExitWithLastError(hr, "Failed to create URI.");
199 } 214 }
200 215
201 hr = StrAllocString(psczUri, wz, cch); 216 hr = StrAllocString(psczUri, wz, cch);
202 ExitOnFailure(hr, "Failed copy created URI."); 217 UriExitOnFailure(hr, "Failed copy created URI.");
203 218
204LExit: 219LExit:
205 return hr; 220 return hr;
@@ -227,13 +242,13 @@ extern "C" HRESULT DAPI UriGetServerAndResource(
227 LPWSTR sczQueryString = NULL; 242 LPWSTR sczQueryString = NULL;
228 243
229 hr = UriCrack(wzUri, &scheme, &sczHostName, &port, &sczUser, &sczPassword, &sczPath, &sczQueryString); 244 hr = UriCrack(wzUri, &scheme, &sczHostName, &port, &sczUser, &sczPassword, &sczPath, &sczQueryString);
230 ExitOnFailure(hr, "Failed to crack URI."); 245 UriExitOnFailure(hr, "Failed to crack URI.");
231 246
232 hr = UriCreate(psczServer, scheme, sczHostName, port, sczUser, sczPassword, NULL, NULL); 247 hr = UriCreate(psczServer, scheme, sczHostName, port, sczUser, sczPassword, NULL, NULL);
233 ExitOnFailure(hr, "Failed to allocate server URI."); 248 UriExitOnFailure(hr, "Failed to allocate server URI.");
234 249
235 hr = UriCreate(psczResource, INTERNET_SCHEME_UNKNOWN, NULL, INTERNET_INVALID_PORT_NUMBER, NULL, NULL, sczPath, sczQueryString); 250 hr = UriCreate(psczResource, INTERNET_SCHEME_UNKNOWN, NULL, INTERNET_INVALID_PORT_NUMBER, NULL, NULL, sczPath, sczQueryString);
236 ExitOnFailure(hr, "Failed to allocate resource URI."); 251 UriExitOnFailure(hr, "Failed to allocate resource URI.");
237 252
238LExit: 253LExit:
239 ReleaseStr(sczQueryString); 254 ReleaseStr(sczQueryString);
@@ -265,13 +280,13 @@ extern "C" HRESULT DAPI UriFile(
265 280
266 if (!::InternetCrackUrlW(wzUri, 0, ICU_DECODE | ICU_ESCAPE, &uc)) 281 if (!::InternetCrackUrlW(wzUri, 0, ICU_DECODE | ICU_ESCAPE, &uc))
267 { 282 {
268 ExitWithLastError(hr, "Failed to crack URI."); 283 UriExitWithLastError(hr, "Failed to crack URI.");
269 } 284 }
270 285
271 // Copy only the file name. Fortunately, PathFile() understands that 286 // Copy only the file name. Fortunately, PathFile() understands that
272 // forward slashes can be directory separators like backslashes. 287 // forward slashes can be directory separators like backslashes.
273 hr = StrAllocString(psczFile, PathFile(wz), 0); 288 hr = StrAllocString(psczFile, PathFile(wz), 0);
274 ExitOnFailure(hr, "Failed to copy file name"); 289 UriExitOnFailure(hr, "Failed to copy file name");
275 290
276LExit: 291LExit:
277 return hr; 292 return hr;
@@ -367,7 +382,7 @@ extern "C" HRESULT DAPI UriRoot(
367 LPCWSTR pwcSlash = NULL; 382 LPCWSTR pwcSlash = NULL;
368 383
369 hr = UriProtocol(wzUri, &protocol); 384 hr = UriProtocol(wzUri, &protocol);
370 ExitOnFailure(hr, "Invalid URI."); 385 UriExitOnFailure(hr, "Invalid URI.");
371 386
372 switch (protocol) 387 switch (protocol)
373 { 388 {
@@ -377,7 +392,7 @@ extern "C" HRESULT DAPI UriRoot(
377 if (((L'a' <= wzUri[8] && L'z' >= wzUri[8]) || (L'A' <= wzUri[8] && L'Z' >= wzUri[8])) && L':' == wzUri[9]) 392 if (((L'a' <= wzUri[8] && L'z' >= wzUri[8]) || (L'A' <= wzUri[8] && L'Z' >= wzUri[8])) && L':' == wzUri[9])
378 { 393 {
379 hr = StrAlloc(ppwzRoot, 4); 394 hr = StrAlloc(ppwzRoot, 4);
380 ExitOnFailure(hr, "Failed to allocate string for root of URI."); 395 UriExitOnFailure(hr, "Failed to allocate string for root of URI.");
381 *ppwzRoot[0] = wzUri[8]; 396 *ppwzRoot[0] = wzUri[8];
382 *ppwzRoot[1] = L':'; 397 *ppwzRoot[1] = L':';
383 *ppwzRoot[2] = L'\\'; 398 *ppwzRoot[2] = L'\\';
@@ -386,7 +401,7 @@ extern "C" HRESULT DAPI UriRoot(
386 else 401 else
387 { 402 {
388 hr = E_INVALIDARG; 403 hr = E_INVALIDARG;
389 ExitOnFailure(hr, "Invalid file path in URI."); 404 UriExitOnFailure(hr, "Invalid file path in URI.");
390 } 405 }
391 } 406 }
392 else // UNC share 407 else // UNC share
@@ -395,23 +410,23 @@ extern "C" HRESULT DAPI UriRoot(
395 if (!pwcSlash) 410 if (!pwcSlash)
396 { 411 {
397 hr = E_INVALIDARG; 412 hr = E_INVALIDARG;
398 ExitOnFailure(hr, "Invalid server name in URI."); 413 UriExitOnFailure(hr, "Invalid server name in URI.");
399 } 414 }
400 else 415 else
401 { 416 {
402 hr = StrAllocString(ppwzRoot, L"\\\\", 64); 417 hr = StrAllocString(ppwzRoot, L"\\\\", 64);
403 ExitOnFailure(hr, "Failed to allocate string for root of URI."); 418 UriExitOnFailure(hr, "Failed to allocate string for root of URI.");
404 419
405 pwcSlash = wcschr(pwcSlash + 1, L'/'); 420 pwcSlash = wcschr(pwcSlash + 1, L'/');
406 if (pwcSlash) 421 if (pwcSlash)
407 { 422 {
408 hr = StrAllocConcat(ppwzRoot, wzUri + 8, pwcSlash - wzUri - 8); 423 hr = StrAllocConcat(ppwzRoot, wzUri + 8, pwcSlash - wzUri - 8);
409 ExitOnFailure(hr, "Failed to add server/share to root of URI."); 424 UriExitOnFailure(hr, "Failed to add server/share to root of URI.");
410 } 425 }
411 else 426 else
412 { 427 {
413 hr = StrAllocConcat(ppwzRoot, wzUri + 8, 0); 428 hr = StrAllocConcat(ppwzRoot, wzUri + 8, 0);
414 ExitOnFailure(hr, "Failed to add server/share to root of URI."); 429 UriExitOnFailure(hr, "Failed to add server/share to root of URI.");
415 } 430 }
416 431
417 // replace all slashes with backslashes to be truly UNC. 432 // replace all slashes with backslashes to be truly UNC.
@@ -431,12 +446,12 @@ extern "C" HRESULT DAPI UriRoot(
431 if (pwcSlash) 446 if (pwcSlash)
432 { 447 {
433 hr = StrAllocString(ppwzRoot, wzUri, pwcSlash - wzUri); 448 hr = StrAllocString(ppwzRoot, wzUri, pwcSlash - wzUri);
434 ExitOnFailure(hr, "Failed allocate root from URI."); 449 UriExitOnFailure(hr, "Failed allocate root from URI.");
435 } 450 }
436 else 451 else
437 { 452 {
438 hr = StrAllocString(ppwzRoot, wzUri, 0); 453 hr = StrAllocString(ppwzRoot, wzUri, 0);
439 ExitOnFailure(hr, "Failed allocate root from URI."); 454 UriExitOnFailure(hr, "Failed allocate root from URI.");
440 } 455 }
441 break; 456 break;
442 457
@@ -445,18 +460,18 @@ extern "C" HRESULT DAPI UriRoot(
445 if (pwcSlash) 460 if (pwcSlash)
446 { 461 {
447 hr = StrAllocString(ppwzRoot, wzUri, pwcSlash - wzUri); 462 hr = StrAllocString(ppwzRoot, wzUri, pwcSlash - wzUri);
448 ExitOnFailure(hr, "Failed allocate root from URI."); 463 UriExitOnFailure(hr, "Failed allocate root from URI.");
449 } 464 }
450 else 465 else
451 { 466 {
452 hr = StrAllocString(ppwzRoot, wzUri, 0); 467 hr = StrAllocString(ppwzRoot, wzUri, 0);
453 ExitOnFailure(hr, "Failed allocate root from URI."); 468 UriExitOnFailure(hr, "Failed allocate root from URI.");
454 } 469 }
455 break; 470 break;
456 471
457 default: 472 default:
458 hr = E_INVALIDARG; 473 hr = E_INVALIDARG;
459 ExitOnFailure(hr, "Unknown URI protocol."); 474 UriExitOnFailure(hr, "Unknown URI protocol.");
460 } 475 }
461 476
462 if (pProtocol) 477 if (pProtocol)
@@ -473,7 +488,7 @@ extern "C" HRESULT DAPI UriResolve(
473 __in_z LPCWSTR wzUri, 488 __in_z LPCWSTR wzUri,
474 __in_opt LPCWSTR wzBaseUri, 489 __in_opt LPCWSTR wzBaseUri,
475 __out LPWSTR* ppwzResolvedUri, 490 __out LPWSTR* ppwzResolvedUri,
476 __out_opt const URI_PROTOCOL* pResolvedProtocol 491 __out_opt URI_PROTOCOL* pResolvedProtocol
477 ) 492 )
478{ 493{
479 UNREFERENCED_PARAMETER(wzUri); 494 UNREFERENCED_PARAMETER(wzUri);
@@ -486,45 +501,45 @@ extern "C" HRESULT DAPI UriResolve(
486 URI_PROTOCOL protocol = URI_PROTOCOL_UNKNOWN; 501 URI_PROTOCOL protocol = URI_PROTOCOL_UNKNOWN;
487 502
488 hr = UriProtocol(wzUri, &protocol); 503 hr = UriProtocol(wzUri, &protocol);
489 ExitOnFailure(hr, "Failed to determine protocol for URL: %ls", wzUri); 504 UriExitOnFailure(hr, "Failed to determine protocol for URL: %ls", wzUri);
490 505
491 ExitOnNull(ppwzResolvedUri, hr, E_INVALIDARG, "Failed to resolve URI, because no method of output was provided"); 506 UriExitOnNull(ppwzResolvedUri, hr, E_INVALIDARG, "Failed to resolve URI, because no method of output was provided");
492 507
493 if (URI_PROTOCOL_UNKNOWN == protocol) 508 if (URI_PROTOCOL_UNKNOWN == protocol)
494 { 509 {
495 ExitOnNull(wzBaseUri, hr, E_INVALIDARG, "Failed to resolve URI - base URI provided was NULL"); 510 UriExitOnNull(wzBaseUri, hr, E_INVALIDARG, "Failed to resolve URI - base URI provided was NULL");
496 511
497 if (L'/' == *wzUri || L'\\' == *wzUri) 512 if (L'/' == *wzUri || L'\\' == *wzUri)
498 { 513 {
499 hr = UriRoot(wzBaseUri, ppwzResolvedUri, &protocol); 514 hr = UriRoot(wzBaseUri, ppwzResolvedUri, &protocol);
500 ExitOnFailure(hr, "Failed to get root from URI: %ls", wzBaseUri); 515 UriExitOnFailure(hr, "Failed to get root from URI: %ls", wzBaseUri);
501 516
502 hr = StrAllocConcat(ppwzResolvedUri, wzUri, 0); 517 hr = StrAllocConcat(ppwzResolvedUri, wzUri, 0);
503 ExitOnFailure(hr, "Failed to concat file to base URI."); 518 UriExitOnFailure(hr, "Failed to concat file to base URI.");
504 } 519 }
505 else 520 else
506 { 521 {
507 hr = UriProtocol(wzBaseUri, &protocol); 522 hr = UriProtocol(wzBaseUri, &protocol);
508 ExitOnFailure(hr, "Failed to get protocol of base URI: %ls", wzBaseUri); 523 UriExitOnFailure(hr, "Failed to get protocol of base URI: %ls", wzBaseUri);
509 524
510 LPCWSTR pwcFile = const_cast<LPCWSTR> (UriFile(wzBaseUri)); 525 LPCWSTR pwcFile = const_cast<LPCWSTR> (UriFile(wzBaseUri));
511 if (!pwcFile) 526 if (!pwcFile)
512 { 527 {
513 hr = E_INVALIDARG; 528 hr = E_INVALIDARG;
514 ExitOnFailure(hr, "Failed to get file from base URI: %ls", wzBaseUri); 529 UriExitOnFailure(hr, "Failed to get file from base URI: %ls", wzBaseUri);
515 } 530 }
516 531
517 hr = StrAllocString(ppwzResolvedUri, wzBaseUri, pwcFile - wzBaseUri); 532 hr = StrAllocString(ppwzResolvedUri, wzBaseUri, pwcFile - wzBaseUri);
518 ExitOnFailure(hr, "Failed to allocate string for resolved URI."); 533 UriExitOnFailure(hr, "Failed to allocate string for resolved URI.");
519 534
520 hr = StrAllocConcat(ppwzResolvedUri, wzUri, 0); 535 hr = StrAllocConcat(ppwzResolvedUri, wzUri, 0);
521 ExitOnFailure(hr, "Failed to concat file to resolved URI."); 536 UriExitOnFailure(hr, "Failed to concat file to resolved URI.");
522 } 537 }
523 } 538 }
524 else 539 else
525 { 540 {
526 hr = StrAllocString(ppwzResolvedUri, wzUri, 0); 541 hr = StrAllocString(ppwzResolvedUri, wzUri, 0);
527 ExitOnFailure(hr, "Failed to copy resolved URI."); 542 UriExitOnFailure(hr, "Failed to copy resolved URI.");
528 } 543 }
529 544
530 if (pResolvedProtocol) 545 if (pResolvedProtocol)