http urls monitor.

context.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package linker
  2. import (
  3. "bytes"
  4. "context"
  5. "strings"
  6. "time"
  7. "github.com/wpajqz/linker/codec"
  8. )
  9. type (
  10. Context interface {
  11. Set(key string, value interface{})
  12. Get(key string) interface{}
  13. MustGet(key string) interface{}
  14. GetString(key string) (s string)
  15. GetBool(key string) (b bool)
  16. GetInt(key string) (i int)
  17. GetInt64(key string) (i64 int64)
  18. GetFloat64(key string) (f64 float64)
  19. GetTime(key string) (t time.Time)
  20. GetDuration(key string) (d time.Duration)
  21. GetStringSlice(key string) (ss []string)
  22. GetStringMap(key string) (sm map[string]interface{})
  23. ParseParam(data interface{}) error
  24. Success(body interface{})
  25. Error(code int, message string)
  26. Write(operator string, body interface{}) (int, error)
  27. SetRequestProperty(key, value string)
  28. GetRequestProperty(key string) string
  29. SetResponseProperty(key, value string)
  30. GetResponseProperty(key string) string
  31. LocalAddr() string
  32. RemoteAddr() string
  33. }
  34. common struct {
  35. config Config
  36. operateType uint32
  37. sequence int64
  38. body []byte
  39. Context context.Context
  40. Request, Response struct {
  41. Header, Body []byte
  42. }
  43. }
  44. )
  45. // Set is used to store a new key/value pair exclusively for this context.
  46. func (dc *common) Set(key string, value interface{}) {
  47. dc.Context = context.WithValue(dc.Context, key, value)
  48. }
  49. // Get returns the value for the given key
  50. func (dc *common) Get(key string) interface{} {
  51. return dc.Context.Value(key)
  52. }
  53. // Get returns the value for the given key
  54. func (dc *common) MustGet(key string) interface{} {
  55. v := dc.Context.Value(key)
  56. if v != nil {
  57. return v
  58. }
  59. panic("Key \"" + key + "\" does not exist")
  60. }
  61. // GetString returns the value associated with the key as a string.
  62. func (dc *common) GetString(key string) (s string) {
  63. v := dc.Context.Value(key)
  64. if v != nil {
  65. s, _ = v.(string)
  66. }
  67. return
  68. }
  69. // GetBool returns the value associated with the key as a boolean.
  70. func (dc *common) GetBool(key string) (b bool) {
  71. v := dc.Context.Value(key)
  72. if v != nil {
  73. b, _ = v.(bool)
  74. }
  75. return
  76. }
  77. // GetInt returns the value associated with the key as an integer.
  78. func (dc *common) GetInt(key string) (i int) {
  79. v := dc.Context.Value(key)
  80. if v != nil {
  81. i, _ = v.(int)
  82. }
  83. return
  84. }
  85. // GetInt64 returns the value associated with the key as an integer.
  86. func (dc *common) GetInt64(key string) (i64 int64) {
  87. v := dc.Context.Value(key)
  88. if v != nil {
  89. i64, _ = v.(int64)
  90. }
  91. return
  92. }
  93. // GetFloat64 returns the value associated with the key as a float64.
  94. func (dc *common) GetFloat64(key string) (f64 float64) {
  95. v := dc.Context.Value(key)
  96. if v != nil {
  97. f64, _ = v.(float64)
  98. }
  99. return
  100. }
  101. // GetTime returns the value associated with the key as time.
  102. func (dc *common) GetTime(key string) (t time.Time) {
  103. v := dc.Context.Value(key)
  104. if v != nil {
  105. t, _ = v.(time.Time)
  106. }
  107. return
  108. }
  109. // GetDuration returns the value associated with the key as a duration.
  110. func (dc *common) GetDuration(key string) (d time.Duration) {
  111. v := dc.Context.Value(key)
  112. if v != nil {
  113. d, _ = v.(time.Duration)
  114. }
  115. return
  116. }
  117. // GetStringSlice returns the value associated with the key as a slice of strings.
  118. func (dc *common) GetStringSlice(key string) (ss []string) {
  119. v := dc.Context.Value(key)
  120. if v != nil {
  121. ss, _ = v.([]string)
  122. }
  123. return
  124. }
  125. // GetStringMap returns the value associated with the key as a map of interfaces.
  126. func (dc *common) GetStringMap(key string) (sm map[string]interface{}) {
  127. v := dc.Context.Value(key)
  128. if v != nil {
  129. sm, _ = v.(map[string]interface{})
  130. }
  131. return
  132. }
  133. func (dc *common) ParseParam(data interface{}) error {
  134. r, err := codec.NewCoder(dc.config.ContentType)
  135. if err != nil {
  136. return err
  137. }
  138. return r.Decoder(dc.body, data)
  139. }
  140. func (dc *common) SetRequestProperty(key, value string) {
  141. v := dc.GetRequestProperty(key)
  142. if v != "" {
  143. dc.Request.Header = bytes.Trim(dc.Request.Header, key+"="+value+";")
  144. }
  145. dc.Request.Header = append(dc.Request.Header, []byte(key+"="+value+";")...)
  146. }
  147. func (dc *common) GetRequestProperty(key string) string {
  148. values := strings.Split(string(dc.Request.Header), ";")
  149. for _, value := range values {
  150. kv := strings.Split(value, "=")
  151. if kv[0] == key {
  152. return kv[1]
  153. }
  154. }
  155. return ""
  156. }
  157. func (dc *common) SetResponseProperty(key, value string) {
  158. v := dc.GetResponseProperty(key)
  159. if v != "" {
  160. dc.Response.Header = bytes.Trim(dc.Response.Header, key+"="+value+";")
  161. }
  162. dc.Response.Header = append(dc.Response.Header, []byte(key+"="+value+";")...)
  163. }
  164. func (dc *common) GetResponseProperty(key string) string {
  165. values := strings.Split(string(dc.Response.Header), ";")
  166. for _, value := range values {
  167. kv := strings.Split(value, "=")
  168. if kv[0] == key {
  169. return kv[1]
  170. }
  171. }
  172. return ""
  173. }