header.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package httpbinding
  2. import (
  3. "encoding/base64"
  4. "math"
  5. "math/big"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. )
  10. // Headers is used to encode header keys using a provided prefix
  11. type Headers struct {
  12. header http.Header
  13. prefix string
  14. }
  15. // AddHeader returns a HeaderValue used to append values to prefix+key
  16. func (h Headers) AddHeader(key string) HeaderValue {
  17. return h.newHeaderValue(key, true)
  18. }
  19. // SetHeader returns a HeaderValue used to set the value of prefix+key
  20. func (h Headers) SetHeader(key string) HeaderValue {
  21. return h.newHeaderValue(key, false)
  22. }
  23. func (h Headers) newHeaderValue(key string, append bool) HeaderValue {
  24. return newHeaderValue(h.header, h.prefix+strings.TrimSpace(key), append)
  25. }
  26. // HeaderValue is used to encode values to an HTTP header
  27. type HeaderValue struct {
  28. header http.Header
  29. key string
  30. append bool
  31. }
  32. func newHeaderValue(header http.Header, key string, append bool) HeaderValue {
  33. return HeaderValue{header: header, key: strings.TrimSpace(key), append: append}
  34. }
  35. func (h HeaderValue) modifyHeader(value string) {
  36. if h.append {
  37. h.header[h.key] = append(h.header[h.key], value)
  38. } else {
  39. h.header[h.key] = append(h.header[h.key][:0], value)
  40. }
  41. }
  42. // String encodes the value v as the header string value
  43. func (h HeaderValue) String(v string) {
  44. h.modifyHeader(v)
  45. }
  46. // Byte encodes the value v as a query string value
  47. func (h HeaderValue) Byte(v int8) {
  48. h.Long(int64(v))
  49. }
  50. // Short encodes the value v as a query string value
  51. func (h HeaderValue) Short(v int16) {
  52. h.Long(int64(v))
  53. }
  54. // Integer encodes the value v as the header string value
  55. func (h HeaderValue) Integer(v int32) {
  56. h.Long(int64(v))
  57. }
  58. // Long encodes the value v as the header string value
  59. func (h HeaderValue) Long(v int64) {
  60. h.modifyHeader(strconv.FormatInt(v, 10))
  61. }
  62. // Boolean encodes the value v as a query string value
  63. func (h HeaderValue) Boolean(v bool) {
  64. h.modifyHeader(strconv.FormatBool(v))
  65. }
  66. // Float encodes the value v as a query string value
  67. func (h HeaderValue) Float(v float32) {
  68. h.float(float64(v), 32)
  69. }
  70. // Double encodes the value v as a query string value
  71. func (h HeaderValue) Double(v float64) {
  72. h.float(v, 64)
  73. }
  74. func (h HeaderValue) float(v float64, bitSize int) {
  75. switch {
  76. case math.IsNaN(v):
  77. h.String(floatNaN)
  78. case math.IsInf(v, 1):
  79. h.String(floatInfinity)
  80. case math.IsInf(v, -1):
  81. h.String(floatNegInfinity)
  82. default:
  83. h.modifyHeader(strconv.FormatFloat(v, 'f', -1, bitSize))
  84. }
  85. }
  86. // BigInteger encodes the value v as a query string value
  87. func (h HeaderValue) BigInteger(v *big.Int) {
  88. h.modifyHeader(v.String())
  89. }
  90. // BigDecimal encodes the value v as a query string value
  91. func (h HeaderValue) BigDecimal(v *big.Float) {
  92. if i, accuracy := v.Int64(); accuracy == big.Exact {
  93. h.Long(i)
  94. return
  95. }
  96. h.modifyHeader(v.Text('e', -1))
  97. }
  98. // Blob encodes the value v as a base64 header string value
  99. func (h HeaderValue) Blob(v []byte) {
  100. encodeToString := base64.StdEncoding.EncodeToString(v)
  101. h.modifyHeader(encodeToString)
  102. }