HttpEncoder.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //------------------------------------------------------------------------------
  2. // based on https://github.com/Microsoft/referencesource/blob/master/System.Web/Util/HttpEncoder.cs
  3. // and https://github.com/Microsoft/referencesource/blob/master/System.Web/Util/HttpEncoderUtility.cs
  4. //
  5. // <copyright file="HttpEncoder.cs" company="Microsoft">
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. // </copyright>
  8. // <copyright file="HttpEncoderUtility.cs" company="Microsoft">
  9. // Copyright (c) Microsoft Corporation. All rights reserved.
  10. // </copyright>
  11. //------------------------------------------------------------------------------
  12. /*
  13. * Copyright (c) 2009 Microsoft Corporation
  14. */
  15. namespace PTMedicalInsurance.APIGATEWAY_SDK
  16. {
  17. using System;
  18. using System.Text;
  19. public partial class Signer
  20. {
  21. private static char IntToHex(int n)
  22. {
  23. if (n <= 9)
  24. return (char)(n + (int)'0');
  25. else
  26. return (char)(n - 10 + (int)'A');
  27. }
  28. private static bool IsUrlSafeChar(char ch)
  29. {
  30. if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9')
  31. return true;
  32. switch (ch)
  33. {
  34. case '-':
  35. case '_':
  36. case '.':
  37. case '~':
  38. return true;
  39. }
  40. return false;
  41. }
  42. private static byte[] UrlEncode(byte[] bytes, int offset, int count)
  43. {
  44. int cUnsafe = 0;
  45. // count them first
  46. for (int i = 0; i < count; i++)
  47. {
  48. char ch = (char)bytes[offset + i];
  49. if (!IsUrlSafeChar(ch))
  50. cUnsafe++;
  51. }
  52. // nothing to expand?
  53. if (cUnsafe == 0)
  54. {
  55. // DevDiv 912606: respect "offset" and "count"
  56. if (0 == offset && bytes.Length == count)
  57. {
  58. return bytes;
  59. }
  60. else
  61. {
  62. var subarray = new byte[count];
  63. Buffer.BlockCopy(bytes, offset, subarray, 0, count);
  64. return subarray;
  65. }
  66. }
  67. // expand not 'safe' characters into %XX, spaces to +s
  68. byte[] expandedBytes = new byte[count + cUnsafe * 2];
  69. int pos = 0;
  70. for (int i = 0; i < count; i++)
  71. {
  72. byte b = bytes[offset + i];
  73. char ch = (char)b;
  74. if (IsUrlSafeChar(ch))
  75. {
  76. expandedBytes[pos++] = b;
  77. }
  78. else
  79. {
  80. expandedBytes[pos++] = (byte)'%';
  81. expandedBytes[pos++] = (byte)IntToHex((b >> 4) & 0xf);
  82. expandedBytes[pos++] = (byte)IntToHex(b & 0x0f);
  83. }
  84. }
  85. return expandedBytes;
  86. }
  87. private static string UrlEncode(string value)
  88. {
  89. if (value == null)
  90. return null;
  91. byte[] bytes = Encoding.UTF8.GetBytes(value);
  92. return Encoding.UTF8.GetString(UrlEncode(bytes, 0, bytes.Length));
  93. }
  94. }
  95. }