Files
cmp-go-snippet/cmpSnipper/cmpSnippetHttpCli.go
2024-01-26 07:11:35 +12:00

197 lines
3.5 KiB
Go

package cmpSnipper
import (
// "crypto/md5"
"net/http"
// "os"
"io"
// "fmt"
"bufio"
// "io/ioutil"
"bytes"
"strings"
// "golang.org/x/net/html"
// "net/url"
"encoding/json"
"compress/gzip"
"compress/flate"
"github.com/andybalholm/brotli"
)
func (app *App) HttpGet(url string) (map[string]interface{}) {
ret := map[string]interface{}{
"Error" : nil ,
"RequestUrl" : url ,
"StatusCode" : 0 ,
"ContentRaw" : nil ,
"ContentJson" : nil ,
"Header" : nil ,
"Content-Encoding" : "" ,
"Content-Type" : "" ,
"Content-Length" : "" ,
}
app.Log(url)
cli := &http.Client{}
ifcRawHead, err := app.ConfGetIfc("httpTest", "head")
if err != nil {
app.Log(err)
ret["Error"] = err
return ret
}
mapIfcHead := ifcRawHead.(map[string]interface{})
mapStrHead := make(map[string][]string)
for key, val := range mapIfcHead {
mapStrHead[key] = []string{ val.(string) }
}
req, err := http.NewRequest("GET", url, nil)
req.Header = mapStrHead
res, err := cli.Do(req)
if err != nil {
app.Log(err)
ret["Error"] = err
return ret
}
defer res.Body.Close()
if err != nil {
app.Log(err)
ret["Error"] = err
return ret
}
ret["StatusCode" ] = res.StatusCode
ret["Content-Length"] = res.ContentLength
ret["Header" ] = res.Header
// app.Log("Response status: %s", res.Status)
// app.Log("Header: %+v", res.Header)
// app.Log("TransferEncoding: %+v", res.TransferEncoding)
// app.Log("ContentLength: %+v", res.ContentLength)
var tmpl []string
tmpl = res.Header["Content-Encoding"]
if len(tmpl) > 0 {
ret["Content-Encoding"] = tmpl[0]
}
tmpl = res.Header["Content-Type"]
if len(tmpl) > 0 {
tmpl = strings.Split(tmpl[0], ";")
if len(tmpl) > 0 {
ret["Content-Type"] = tmpl[0]
}
}
// Content-Disposition: attachment; name="fieldName"; filename="myfile.txt"
out := new(bytes.Buffer)
var encReader io.Reader
switch ret["Content-Encoding"] {
case "":
encReader = bufio.NewReader(res.Body)
case "br":
encReader = brotli.NewReader(res.Body)
case "gzip":
encReader, err = gzip.NewReader(res.Body)
case "deflate":
encReader = flate.NewReader(res.Body)
default:
app.Log("Unknown Content-Encoding %s", ret["Content-Encoding"])
encReader = bufio.NewReader(res.Body)
}
if err != nil {
app.Log(err)
ret["Error"] = err
return ret
}
size, err := out.ReadFrom(encReader)
if err != nil {
app.Log(err)
ret["Error"] = err
return ret
}
app.Log("Read %s", size)
var contRaw []byte = out.Bytes()
ret["ContentRaw"] = contRaw
if ret["Content-Type"] == "application/json" {
var contJson interface{}
err = json.Unmarshal(contRaw, &contJson)
if err != nil {
app.Log(err)
ret["Error"] = err
return ret
}
ret["ContentJson"] = contJson
}
return ret
}
func (app *App) CmpHttpTestSnip(parm any) (any, error) {
var err error
cli, err := app.SnipGetCli(parm)
if err != nil {
return map[string]interface{}{
"status": "error",
"error" : "Not client found",
}, err
}
url, err := app.ConfGetStr("httpTest", "url")
if err != nil {
return map[string]interface{}{
"status": "error",
"error" : "Not test URL",
}, err
}
go func() {
var a map[string]interface{}
a = app.HttpGet(url)
app.Log(a)
app.HttpWebSockSendTo(cli, "cmpHttpTest", map[string]interface{}{
"status" : "response",
"response": a,
})
}()
return map[string]interface{}{
"status" : "request",
"request": url,
}, nil
}