You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
1.6 KiB

  1. package cos
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "fmt"
  7. "net/http"
  8. )
  9. // 计算 md5 或 sha1 时的分块大小
  10. const calDigestBlockSize = 1024 * 1024 * 10
  11. func calMD5Digest(msg []byte) []byte {
  12. // TODO: 分块计算,减少内存消耗
  13. m := md5.New()
  14. m.Write(msg)
  15. return m.Sum(nil)
  16. }
  17. func calSHA1Digest(msg []byte) []byte {
  18. // TODO: 分块计算,减少内存消耗
  19. m := sha1.New()
  20. m.Write(msg)
  21. return m.Sum(nil)
  22. }
  23. // cloneRequest returns a clone of the provided *http.Request. The clone is a
  24. // shallow copy of the struct and its Header map.
  25. func cloneRequest(r *http.Request) *http.Request {
  26. // shallow copy of the struct
  27. r2 := new(http.Request)
  28. *r2 = *r
  29. // deep copy of the Header
  30. r2.Header = make(http.Header, len(r.Header))
  31. for k, s := range r.Header {
  32. r2.Header[k] = append([]string(nil), s...)
  33. }
  34. return r2
  35. }
  36. // encodeURIComponent like same function in javascript
  37. //
  38. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
  39. //
  40. // http://www.ecma-international.org/ecma-262/6.0/#sec-uri-syntax-and-semantics
  41. func encodeURIComponent(s string) string {
  42. var b bytes.Buffer
  43. written := 0
  44. for i, n := 0, len(s); i < n; i++ {
  45. c := s[i]
  46. switch c {
  47. case '-', '_', '.', '!', '~', '*', '\'', '(', ')':
  48. continue
  49. default:
  50. // Unreserved according to RFC 3986 sec 2.3
  51. if 'a' <= c && c <= 'z' {
  52. continue
  53. }
  54. if 'A' <= c && c <= 'Z' {
  55. continue
  56. }
  57. if '0' <= c && c <= '9' {
  58. continue
  59. }
  60. }
  61. b.WriteString(s[written:i])
  62. fmt.Fprintf(&b, "%%%02x", c)
  63. written = i + 1
  64. }
  65. if written == 0 {
  66. return s
  67. }
  68. b.WriteString(s[written:])
  69. return b.String()
  70. }