/// <summary> /// Is allowed? /// </summary> /// <param name="flags"></param> /// <param name="ch"></param> /// <returns></returns> private static bool IsAllowedChar(EscapeEncodingFlag flags, char ch) { bool isUriString = (flags & EscapeEncodingFlag.UriString) == EscapeEncodingFlag.UriString; bool isLegacyRfc2396 = (flags & EscapeEncodingFlag.LegacyRfc2396) == EscapeEncodingFlag.LegacyRfc2396; if (isUriString) { if (!isLegacyRfc2396 && RFC3986UnreservedMarks.IndexOf(ch) >= 0) { return(true); } if (isLegacyRfc2396 && RFC2396UnreservedMarks.IndexOf(ch) >= 0) { return(true); } } else { if (!isLegacyRfc2396 && RFC3986ReservedMarks.IndexOf(ch) >= 0) { return(true); } if (isLegacyRfc2396 && RFC2396ReservedMarks.IndexOf(ch) >= 0) { return(true); } } return(false); }
/// <summary> /// Escape unicode string data for use in http-requests /// </summary> /// <param name="source">unicode string-data to be encoded</param> /// <param name="target">target for the encoded result</param> /// <param name="flags"><see cref="EscapeEncodingFlag"/>s for how to perform the encoding</param> public static void EscapeDataEncode(string source, StringBuilder target, EscapeEncodingFlag flags) { if (string.IsNullOrEmpty(source)) { return; } bool isUriString = (flags & EscapeEncodingFlag.UriString) == EscapeEncodingFlag.UriString; bool isLegacyRfc2396 = (flags & EscapeEncodingFlag.LegacyRfc2396) == EscapeEncodingFlag.LegacyRfc2396; bool isLowerCaseHex = (flags & EscapeEncodingFlag.LowerCaseHex) == EscapeEncodingFlag.LowerCaseHex; bool isSpaceAsPlus = (flags & EscapeEncodingFlag.SpaceAsPlus) == EscapeEncodingFlag.SpaceAsPlus; bool isNLogLegacy = (flags & EscapeEncodingFlag.NLogLegacy) == EscapeEncodingFlag.NLogLegacy; char[] charArray = null; byte[] byteArray = null; char[] hexChars = isLowerCaseHex ? hexLowerChars : hexUpperChars; for (int i = 0; i < source.Length; ++i) { char ch = source[i]; target.Append(ch); if (ch >= 'a' && ch <= 'z') { continue; } if (ch >= 'A' && ch <= 'Z') { continue; } if (ch >= '0' && ch <= '9') { continue; } if (isSpaceAsPlus && ch == ' ') { target[target.Length - 1] = '+'; continue; } if (isUriString) { if (!isLegacyRfc2396 && RFC3986UnreservedMarks.IndexOf(ch) >= 0) { continue; } if (isLegacyRfc2396 && RFC2396UnreservedMarks.IndexOf(ch) >= 0) { continue; } } else { if (!isLegacyRfc2396 && RFC3986ReservedMarks.IndexOf(ch) >= 0) { continue; } if (isLegacyRfc2396 && RFC2396ReservedMarks.IndexOf(ch) >= 0) { continue; } } if (isNLogLegacy) { if (ch < 256) { target[target.Length - 1] = '%'; target.Append(hexChars[(ch >> 4) & 0xF]); target.Append(hexChars[(ch >> 0) & 0xF]); } else { target[target.Length - 1] = '%'; target.Append('u'); target.Append(hexChars[(ch >> 12) & 0xF]); target.Append(hexChars[(ch >> 8) & 0xF]); target.Append(hexChars[(ch >> 4) & 0xF]); target.Append(hexChars[(ch >> 0) & 0xF]); } continue; } if (charArray == null) { charArray = new char[1]; } charArray[0] = ch; if (byteArray == null) { byteArray = new byte[8]; } // Convert the wide-char into utf8-bytes, and then escape int byteCount = Encoding.UTF8.GetBytes(charArray, 0, 1, byteArray, 0); for (int j = 0; j < byteCount; ++j) { byte byteCh = byteArray[j]; if (j == 0) { target[target.Length - 1] = '%'; } else { target.Append('%'); } target.Append(hexChars[(byteCh & 0xf0) >> 4]); target.Append(hexChars[byteCh & 0xf]); } } }