http.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package plugin
  15. import (
  16. "bytes"
  17. "context"
  18. "encoding/json"
  19. "fmt"
  20. "io/ioutil"
  21. "net/http"
  22. "net/url"
  23. "reflect"
  24. )
  25. type HTTPPluginOptions struct {
  26. Name string
  27. Addr string
  28. Path string
  29. Ops []string
  30. }
  31. type httpPlugin struct {
  32. options HTTPPluginOptions
  33. url string
  34. client *http.Client
  35. }
  36. func NewHTTPPluginOptions(options HTTPPluginOptions) Plugin {
  37. return &httpPlugin{
  38. options: options,
  39. url: fmt.Sprintf("http://%s%s", options.Addr, options.Path),
  40. client: &http.Client{},
  41. }
  42. }
  43. func (p *httpPlugin) Name() string {
  44. return p.options.Name
  45. }
  46. func (p *httpPlugin) IsSupport(op string) bool {
  47. for _, v := range p.options.Ops {
  48. if v == op {
  49. return true
  50. }
  51. }
  52. return false
  53. }
  54. func (p *httpPlugin) Handle(ctx context.Context, op string, content interface{}) (*Response, interface{}, error) {
  55. r := &Request{
  56. Version: APIVersion,
  57. Op: op,
  58. Content: content,
  59. }
  60. var res Response
  61. res.Content = reflect.New(reflect.TypeOf(content)).Interface()
  62. if err := p.do(ctx, r, &res); err != nil {
  63. return nil, nil, err
  64. }
  65. return &res, res.Content, nil
  66. }
  67. func (p *httpPlugin) do(ctx context.Context, r *Request, res *Response) error {
  68. buf, err := json.Marshal(r)
  69. if err != nil {
  70. return err
  71. }
  72. v := url.Values{}
  73. v.Set("version", r.Version)
  74. v.Set("op", r.Op)
  75. req, err := http.NewRequest("POST", p.url+"?"+v.Encode(), bytes.NewReader(buf))
  76. if err != nil {
  77. return err
  78. }
  79. req = req.WithContext(ctx)
  80. req.Header.Set("X-Frp-Reqid", GetReqidFromContext(ctx))
  81. req.Header.Set("Content-Type", "application/json")
  82. resp, err := p.client.Do(req)
  83. if err != nil {
  84. return err
  85. }
  86. defer resp.Body.Close()
  87. if resp.StatusCode != http.StatusOK {
  88. return fmt.Errorf("do http request error code: %d", resp.StatusCode)
  89. }
  90. buf, err = ioutil.ReadAll(resp.Body)
  91. if err != nil {
  92. return err
  93. }
  94. if err = json.Unmarshal(buf, res); err != nil {
  95. return err
  96. }
  97. return nil
  98. }