HttpUtils.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. require_once(XASSET_PATH . 'common/config/HttpHeaders.php');
  3. class HttpUtils
  4. {
  5. private static $PERCENT_ENCODED_STRINGS;
  6. public static function __init()
  7. {
  8. HttpUtils::$PERCENT_ENCODED_STRINGS = array();
  9. for ($i = 0; $i < 256; ++$i) {
  10. HttpUtils::$PERCENT_ENCODED_STRINGS[$i] = sprintf("%%%02X", $i);
  11. }
  12. foreach (range('a', 'z') as $ch) {
  13. HttpUtils::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
  14. }
  15. foreach (range('A', 'Z') as $ch) {
  16. HttpUtils::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
  17. }
  18. foreach (range('0', '9') as $ch) {
  19. HttpUtils::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
  20. }
  21. HttpUtils::$PERCENT_ENCODED_STRINGS[ord('-')] = '-';
  22. HttpUtils::$PERCENT_ENCODED_STRINGS[ord('.')] = '.';
  23. HttpUtils::$PERCENT_ENCODED_STRINGS[ord('_')] = '_';
  24. HttpUtils::$PERCENT_ENCODED_STRINGS[ord('~')] = '~';
  25. }
  26. /**
  27. * Normalize a string for use in url path. The algorithm is:
  28. * <p>
  29. *
  30. * <ol>
  31. * <li>Normalize the string</li>
  32. * <li>replace all "%2F" with "/"</li>
  33. * <li>replace all "//" with "/%2F"</li>
  34. * </ol>
  35. *
  36. * <p>
  37. * Bos object key can contain arbitrary characters, which may result double
  38. * slash in the url path. Apache Http client will replace "//" in the path
  39. * with a single '/', which makes the object key incorrect. Thus we replace
  40. * "//" with "/%2F" here.
  41. *
  42. * @param $path string the path string to normalize.
  43. * @return string the normalized path string.
  44. * @see #normalize(string)
  45. */
  46. public static function urlEncodeExceptSlash($path)
  47. {
  48. return str_replace("%2F", "/", HttpUtils::urlEncode($path));
  49. }
  50. /**
  51. * Normalize a string for use in BCE web service APIs. The normalization
  52. * algorithm is:
  53. * <p>
  54. * <ol>
  55. * <li>Convert the string into a UTF-8 byte array.</li>
  56. * <li>Encode all octets into percent-encoding, except all URI unreserved
  57. * characters per the RFC 3986.</li>
  58. * </ol>
  59. *
  60. * <p>
  61. * All letters used in the percent-encoding are in uppercase.
  62. *
  63. * @param $value string the string to normalize.
  64. * @return string the normalized string.
  65. */
  66. public static function urlEncode($value)
  67. {
  68. $result = '';
  69. for ($i = 0; $i < strlen($value); ++$i) {
  70. $result .= HttpUtils::$PERCENT_ENCODED_STRINGS[ord($value[$i])];
  71. }
  72. return $result;
  73. }
  74. /**
  75. * @param $parameters array
  76. * @param $forSignature bool
  77. * @return string
  78. */
  79. public static function getCanonicalQueryString(array $parameters, $forSignature)
  80. {
  81. if (count($parameters) == 0) {
  82. return '';
  83. }
  84. $parameterStrings = array();
  85. foreach ($parameters as $k => $v) {
  86. if ($forSignature
  87. && strcasecmp(HttpHeaders::AUTHORIZATION, $k) == 0) {
  88. continue;
  89. }
  90. if (!isset($k)) {
  91. throw new \InvalidArgumentException(
  92. "parameter key should not be null"
  93. );
  94. }
  95. if (isset($v)) {
  96. $parameterStrings[] = HttpUtils::urlEncode($k)
  97. . '=' . HttpUtils::urlEncode((string) $v);
  98. } else {
  99. if ($forSignature) {
  100. $parameterStrings[] = HttpUtils::urlEncode($k) . '=';
  101. } else {
  102. $parameterStrings[] = HttpUtils::urlEncode($k);
  103. }
  104. }
  105. }
  106. sort($parameterStrings);
  107. return implode('&', $parameterStrings);
  108. }
  109. }
  110. HttpUtils::__init();