http.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "reflect"
  23. )
  24. type HTTPPluginOptions struct {
  25. Name string
  26. Addr string
  27. Path string
  28. Ops []string
  29. }
  30. type httpPlugin struct {
  31. options HTTPPluginOptions
  32. url string
  33. client *http.Client
  34. }
  35. func NewHTTPPluginOptions(options HTTPPluginOptions) Plugin {
  36. return &httpPlugin{
  37. options: options,
  38. url: fmt.Sprintf("http://%s%s", options.Addr, options.Path),
  39. client: &http.Client{},
  40. }
  41. }
  42. func (p *httpPlugin) Name() string {
  43. return p.options.Name
  44. }
  45. func (p *httpPlugin) IsSupport(op string) bool {
  46. for _, v := range p.options.Ops {
  47. if v == op {
  48. return true
  49. }
  50. }
  51. return false
  52. }
  53. func (p *httpPlugin) Handle(ctx context.Context, op string, content interface{}) (*Response, interface{}, error) {
  54. r := &Request{
  55. Version: APIVersion,
  56. Op: op,
  57. Content: content,
  58. }
  59. var res Response
  60. res.Content = reflect.New(reflect.TypeOf(content)).Interface()
  61. if err := p.do(ctx, r, &res); err != nil {
  62. return nil, nil, err
  63. }
  64. return &res, res.Content, nil
  65. }
  66. func (p *httpPlugin) do(ctx context.Context, r *Request, res *Response) error {
  67. buf, err := json.Marshal(r)
  68. if err != nil {
  69. return err
  70. }
  71. req, err := http.NewRequest("POST", p.url, bytes.NewReader(buf))
  72. if err != nil {
  73. return err
  74. }
  75. req = req.WithContext(ctx)
  76. req.Header.Set("X-Frp-Reqid", GetReqidFromContext(ctx))
  77. resp, err := p.client.Do(req)
  78. if err != nil {
  79. return err
  80. }
  81. defer resp.Body.Close()
  82. if resp.StatusCode != http.StatusOK {
  83. return fmt.Errorf("do http request error code: %d", resp.StatusCode)
  84. }
  85. buf, err = ioutil.ReadAll(resp.Body)
  86. if err != nil {
  87. return err
  88. }
  89. if err = json.Unmarshal(buf, res); err != nil {
  90. return err
  91. }
  92. return nil
  93. }