http.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "regexp"
  11. "strings"
  12. "time"
  13. "github.com/astaxie/bat/httplib"
  14. )
  15. var defaultSetting = httplib.BeegoHttpSettings{
  16. ShowDebug: true,
  17. UserAgent: "bat/" + version,
  18. ConnectTimeout: 60 * time.Second,
  19. ReadWriteTimeout: 60 * time.Second,
  20. Gzip: true,
  21. DumpBody: true,
  22. }
  23. func getHTTP(method string, url string, args []string) (r *httplib.BeegoHttpRequest) {
  24. r = httplib.NewBeegoRequest(url, method)
  25. r.Setting(defaultSetting)
  26. r.Header("Accept-Encoding", "gzip, deflate")
  27. if *isjson {
  28. r.Header("Accept", "application/json")
  29. } else if form || method == "GET" {
  30. r.Header("Accept", "*/*")
  31. } else {
  32. r.Header("Accept", "application/json")
  33. }
  34. for i := range args {
  35. // Headers
  36. strs := strings.Split(args[i], ":")
  37. if len(strs) >= 2 {
  38. if strs[0] == "Host" {
  39. r.SetHost(strings.Join(strs[1:], ":"))
  40. }
  41. r.Header(strs[0], strings.Join(strs[1:], ":"))
  42. continue
  43. }
  44. // files
  45. strs = strings.SplitN(args[i], "@", 2)
  46. if !*isjson && len(strs) == 2 {
  47. if !form {
  48. log.Fatal("file upload only support in forms style: -f=true")
  49. }
  50. r.PostFile(strs[0], strs[1])
  51. continue
  52. }
  53. // Json raws
  54. strs = strings.SplitN(args[i], ":=", 2)
  55. if len(strs) == 2 {
  56. if strings.HasPrefix(strs[1], "@") {
  57. f, err := os.Open(strings.TrimLeft(strs[1], "@"))
  58. if err != nil {
  59. log.Fatal("Read File", strings.TrimLeft(strs[1], "@"), err)
  60. }
  61. content, err := ioutil.ReadAll(f)
  62. if err != nil {
  63. log.Fatal("ReadAll from File", strings.TrimLeft(strs[1], "@"), err)
  64. }
  65. var j interface{}
  66. err = json.Unmarshal(content, &j)
  67. if err != nil {
  68. log.Fatal("Read from File", strings.TrimLeft(strs[1], "@"), "Unmarshal", err)
  69. }
  70. jsonmap[strs[0]] = j
  71. continue
  72. }
  73. jsonmap[strs[0]] = toRealType(strs[1])
  74. continue
  75. }
  76. // Params
  77. strs = strings.SplitN(args[i], "=", 2)
  78. if len(strs) == 2 {
  79. if strings.HasPrefix(strs[1], "@") {
  80. f, err := os.Open(strings.TrimLeft(strs[1], "@"))
  81. if err != nil {
  82. log.Fatal("Read File", strings.TrimLeft(strs[1], "@"), err)
  83. }
  84. content, err := ioutil.ReadAll(f)
  85. if err != nil {
  86. log.Fatal("ReadAll from File", strings.TrimLeft(strs[1], "@"), err)
  87. }
  88. strs[1] = string(content)
  89. }
  90. if form || method == "GET" {
  91. r.Param(strs[0], strs[1])
  92. } else {
  93. jsonmap[strs[0]] = strs[1]
  94. }
  95. continue
  96. }
  97. }
  98. if !form && len(jsonmap) > 0 {
  99. r.JsonBody(jsonmap)
  100. }
  101. return
  102. }
  103. func formatResponseBody(res *http.Response, httpreq *httplib.BeegoHttpRequest, pretty bool) string {
  104. body, err := httpreq.Bytes()
  105. if err != nil {
  106. log.Fatalln("can't get the url", err)
  107. }
  108. fmt.Println("")
  109. match, err := regexp.MatchString(contentJsonRegex, res.Header.Get("Content-Type"))
  110. if err != nil {
  111. log.Fatalln("failed to compile regex", err)
  112. }
  113. if pretty && match {
  114. var output bytes.Buffer
  115. err := json.Indent(&output, body, "", " ")
  116. if err != nil {
  117. log.Fatal("Response Json Indent: ", err)
  118. }
  119. return output.String()
  120. }
  121. return string(body)
  122. }