public void DecodeUrl_WithEmptyString_ReturnMsg() { //Arrange string inputUrl = null; //Act string result = _encoder.Decode(inputUrl); //Assert Assert.AreEqual("Please fill in url to decode!", result); }
/// <summary> /// Decrypts an salted string /// </summary> /// <param name="encryptedString"></param> public string Decrypt(string encryptedString) { var returnValue = TypeExtension.DefaultString; var itemToDecrypt = TypeExtension.DefaultString; try { itemToDecrypt = encryptedString; if (this.EncodeForURL) { itemToDecrypt = UrlEncoder.Decode(encryptedString); } TripleDES des = CreateDes(); ICryptoTransform decryptor = des.CreateDecryptor(); var encryptedByte = Convert.FromBase64String(itemToDecrypt); var decryptedByte = decryptor.TransformFinalBlock(encryptedByte, 0, encryptedByte.Length); var decryptedSaltedString = Encoding.Unicode.GetString(decryptedByte); // Final decryption and return returnValue = decryptedSaltedString.Remove(decryptedSaltedString.Length - this.Salt.Length); } catch { returnValue = TypeExtension.DefaultString; } return(returnValue); }
public void Must_perform_decoding() { var systemUnderTest = new UrlEncoder(); string decodedUrl = systemUnderTest.Decode("%3fTest%3d1%26Test%3d2%0d"); Assert.That(decodedUrl, Is.EqualTo("?Test=1&Test=2\r")); }
public void ThrowWhenDesinationIsSmaller() { var input = GetBytes("/test%20test"); var destination = new Span <byte>(new byte[input.Length - 1]); Assert.Throws <ArgumentException>(() => { UrlEncoder.Decode(input, destination); }); }
public void ThrowWhenDesinationIsSmaller() { var input = GetBytes("/test%20test"); var destination = new Span <byte>(new byte[input.Length - 1]); try { UrlEncoder.Decode(input, destination); Assert.True(false); } catch (Exception ex) { Assert.True(ex is ArgumentException); } }
protected override void TestCore(string raw, string expected) { var input = GetBytes(raw); var destination = new Span <byte>(new byte[input.Length]); var len = UrlEncoder.Decode(input, destination); Assert.True(len <= input.Length); var unescaped = destination.Slice(0, len); Assert.False(unescaped.ReferenceEquals(input.Slice(0, len))); var outputDecoded = Encoding.UTF8.GetString(unescaped.ToArray()); Assert.Equal(expected, outputDecoded); }
/// <summary> /// Checks the query for the return url, and defaults to 'default.asp' if none /// is specified, and then redirects to this url. /// </summary> private void Redirect() { // Default return url is homepage string returnUrl = "~/Default.aspx"; // Get the return url from the querystring if (Request.QueryString.Get("returnUrl") != null) { returnUrl = UrlEncoder.Decode(Request.QueryString.Get("returnUrl")); } // No point sending the user back to the login page after login! if ((Path.GetFileName(returnUrl) ?? string.Empty).ToLower() == "login.aspx") { returnUrl = "~/Default.aspx"; } Response.Redirect(returnUrl, false); }
/// <summary> /// Decrypts an salted String /// </summary> /// <param name="originalString"></param> public string Decrypt(string originalString) { var returnValue = TypeExtension.DefaultString; byte[] encryptedByte; try { originalString = this.EncodeForURL == true?UrlEncoder.Decode(originalString) : originalString; encryptedByte = Convert.FromBase64String(originalString); originalString = Encoding.Unicode.GetString(encryptedByte, 0, encryptedByte.Length); returnValue = this.ShiftString(originalString, Arithmetic.Multiply(this.Key.TryParseInt16(), -1).ToShort()); } catch (Exception ex) { returnValue = ex.Message; returnValue = TypeExtension.DefaultString; } return(returnValue); }
private void OnOriginFormTarget(HttpMethod method, HttpVersion version, Span <byte> target, Span <byte> path, Span <byte> query, Span <byte> customMethod, bool pathEncoded) { Debug.Assert(target[0] == ByteForwardSlash, "Should only be called when path starts with /"); _requestTargetForm = HttpRequestTarget.OriginForm; // URIs are always encoded/escaped to ASCII https://tools.ietf.org/html/rfc3986#page-11 // Multibyte Internationalized Resource Identifiers (IRIs) are first converted to utf8; // then encoded/escaped to ASCII https://www.ietf.org/rfc/rfc3987.txt "Mapping of IRIs to URIs" string requestUrlPath = null; string rawTarget = null; try { // Read raw target before mutating memory. rawTarget = target.GetAsciiStringNonNullCharacters(); if (pathEncoded) { // URI was encoded, unescape and then parse as UTF-8 // Disabling warning temporary #pragma warning disable 618 var pathLength = UrlEncoder.Decode(path, path); #pragma warning restore 618 // Removing dot segments must be done after unescaping. From RFC 3986: // // URI producing applications should percent-encode data octets that // correspond to characters in the reserved set unless these characters // are specifically allowed by the URI scheme to represent data in that // component. If a reserved character is found in a URI component and // no delimiting role is known for that character, then it must be // interpreted as representing the data octet corresponding to that // character's encoding in US-ASCII. // // https://tools.ietf.org/html/rfc3986#section-2.2 pathLength = PathNormalizer.RemoveDotSegments(path.Slice(0, pathLength)); requestUrlPath = GetUtf8String(path.Slice(0, pathLength)); } else { var pathLength = PathNormalizer.RemoveDotSegments(path); if (path.Length == pathLength && query.Length == 0) { // If no decoding was required, no dot segments were removed and // there is no query, the request path is the same as the raw target requestUrlPath = rawTarget; } else { requestUrlPath = path.Slice(0, pathLength).GetAsciiStringNonNullCharacters(); } } } catch (InvalidOperationException) { ThrowRequestTargetRejected(target); } QueryString = query.GetAsciiStringNonNullCharacters(); RawTarget = rawTarget; Path = requestUrlPath; }
/// <summary> /// Decodes to URL friendly /// </summary> /// <param name="originalString"></param> /// <returns></returns> public static string Decode(string originalString) { return(UrlEncoder.Decode(originalString)); }