void CFTLNetTester::test_UrlEscape() { HRESULT hr = E_FAIL; //char unsafe;=> %hex(asc(unsafe)) //MSDN中的例子 CString strSourceURL = TEXT("test/t%e<s t.asp?url=/{ex% ample</abc.asp?frame=true#fr%agment"); typedef std::pair<DWORD, CString> UrlEscapeCheckValueType; UrlEscapeCheckValueType checkValues[] = { std::make_pair(URL_ESCAPE_SPACES_ONLY, TEXT("test/t%e<s%20t.asp?url=/{ex%%20ample</abc.asp?frame=true#fr%agment")), std::make_pair(URL_ESCAPE_SPACES_ONLY | URL_DONT_ESCAPE_EXTRA_INFO, TEXT("test/t%e<s%20t.asp?url=/{ex% ample</abc.asp?frame=true#fr%agment")), std::make_pair(URL_ESCAPE_PERCENT , TEXT("test/t%25e%3Cs%20t.asp?url=/{ex% ample</abc.asp?frame=true#fr%agment")), std::make_pair(URL_ESCAPE_SEGMENT_ONLY, TEXT("test%2Ft%e%3Cs%20t.asp%3Furl=%2F%7Bex%%20ample%3C%2Fabc.asp%3Fframe=true%23fr%agment")), }; DWORD dwBufferSize = INTERNET_MAX_URL_LENGTH; FTL::CFMemAllocator<TCHAR> szTargetURL(dwBufferSize); for (int i = 0; i < _countof(checkValues); i++) { COM_VERIFY(UrlEscape(strSourceURL, (TCHAR*)szTargetURL, &dwBufferSize, checkValues[i].first)); CPPUNIT_ASSERT(checkValues[i].second.Compare((TCHAR*)szTargetURL) == 0); //重新初始化Buffer大小 dwBufferSize = INTERNET_MAX_URL_LENGTH; } //中文 }
FCIMPLEND #ifndef FEATURE_CORECLR FCIMPL1(Object*, AssemblyNameNative::EscapeCodeBase, StringObject* filenameUNSAFE) { FCALL_CONTRACT; STRINGREF rv = NULL; STRINGREF filename = (STRINGREF) filenameUNSAFE; HELPER_METHOD_FRAME_BEGIN_RET_1(filename); LPWSTR pCodeBase = NULL; DWORD dwCodeBase = 0; CQuickBytes qb; if (filename != NULL) { WCHAR* pString; int iString; filename->RefInterpretGetStringValuesDangerousForGC(&pString, &iString); dwCodeBase = (DWORD) iString; pCodeBase = (LPWSTR) qb.AllocThrows((++dwCodeBase) * sizeof(WCHAR)); memcpy(pCodeBase, pString, dwCodeBase*sizeof(WCHAR)); } if(pCodeBase) { CQuickBytes qb2; DWORD dwEscaped = 1; DWORD flags = 0; if (RunningOnWin7()) flags |= URL_ESCAPE_AS_UTF8; UrlEscape(pCodeBase, (LPWSTR) qb2.Ptr(), &dwEscaped, flags); LPWSTR result = (LPWSTR)qb2.AllocThrows((++dwEscaped) * sizeof(WCHAR)); HRESULT hr = UrlEscape(pCodeBase, result, &dwEscaped, flags); if (SUCCEEDED(hr)) rv = StringObject::NewString(result); else COMPlusThrowHR(hr); } HELPER_METHOD_FRAME_END(); return OBJECTREFToObject(rv); }
std::string Url::encode( const std::string &unescaped ) { #if defined( CINDER_COCOA ) cocoa::SafeCfString unescapedStr = cocoa::createSafeCfString( unescaped ); CFStringRef escaped = ::CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, unescapedStr.get(), NULL, NULL, kCFStringEncodingUTF8 ); std::string result = cocoa::convertCfString( escaped ); ::CFRelease( escaped ); return result; #elif defined( CINDER_MSW ) wchar_t buffer[4096]; DWORD bufferSize = 4096; std::wstring wideUnescaped = toUtf16( unescaped ); UrlEscape( wideUnescaped.c_str(), buffer, &bufferSize, 0 ); return toUtf8( std::wstring( buffer ) ); #endif }
String ArchInternetWindows::urlEncode(const String& url) { TCHAR buffer[1024]; DWORD bufferSize = sizeof(buffer); if (UrlEscape(url.c_str(), buffer, &bufferSize, URL_ESCAPE_UNSAFE) != S_OK) { throw XArch(new XArchEvalWindows()); } String result(buffer); // the win32 url encoding funcitons are pretty useless (to us) and only // escape "unsafe" chars, but not + or =, so we need to replace these // manually (and probably many other chars). synergy::string::findReplaceAll(result, "+", "%2B"); synergy::string::findReplaceAll(result, "=", "%3D"); return result; }
std::string Url::encode( const std::string &unescaped ) { #if defined( CINDER_COCOA ) cocoa::SafeCfString unescapedStr = cocoa::createSafeCfString( unescaped ); CFStringRef escaped = ::CFURLCreateStringByAddingPercentEscapes( kCFAllocatorDefault, unescapedStr.get(), NULL, NULL, kCFStringEncodingUTF8 ); std::string result = cocoa::convertCfString( escaped ); ::CFRelease( escaped ); return result; #elif defined( CINDER_MSW ) char16_t buffer[4096]; DWORD bufferSize = 4096; std::u16string wideUnescaped = toUtf16( unescaped ); UrlEscape( (wchar_t*)wideUnescaped.c_str(), (wchar_t*)buffer, &bufferSize, 0 ); return toUtf8( buffer ); #elif defined( CINDER_WINRT ) std::wstring urlStr = msw::toWideString( unescaped ); auto uri = ref new Windows::Foundation::Uri(ref new Platform::String(urlStr.c_str())); return msw::toUtf8String( std::wstring( uri->AbsoluteCanonicalUri->Data())); #endif }