package cmpSnipper import ( // "crypto/md5" "net/http" "net/url" "crypto/tls" // "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) HttpPost(url string, head map[string]string, cookie []http.Cookie, data url.Values) (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("%s", url) var tr *http.Transport = &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, } if !app.HttpCliSslVerify { tr.TLSClientConfig.InsecureSkipVerify = true } cli := &http.Client{Transport: tr} 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) } } // app.Log(head) for key, val := range head { mapStrHead[key] = []string{ val } } req, err := http.NewRequest( "POST", url, strings.NewReader(data.Encode()), ) if err != nil { app.Log(err) ret["Error"] = err return ret } req.Header = mapStrHead req.Header.Add("Content-Type", "application/x-www-form-urlencoded") for _, coo := range cookie { req.AddCookie(&coo) } 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 ret["Cookies" ] = res.Cookies() if res.StatusCode != 200 { app.Log("StatusCode != 200") app.Log(req) app.Log(req.Header) app.Log(mapStrHead) } // 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 } if app.HttpCliDebug > 1 { 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) HttpGet(url string, head map[string]string, cookie []http.Cookie) (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("%s", url) var tr *http.Transport = &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, } if !app.HttpCliSslVerify { tr.TLSClientConfig.InsecureSkipVerify = true } cli := &http.Client{Transport: tr} 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) } } // app.Log(head) for key, val := range head { mapStrHead[key] = []string{ val } } req, err := http.NewRequest("GET", url, nil) req.Header = mapStrHead for _, coo := range cookie { req.AddCookie(&coo) } 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 ret["Cookies" ] = res.Cookies() if res.StatusCode != 200 { app.Log("StatusCode != 200") app.Log(req) app.Log(req.Header) app.Log(mapStrHead) } // 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 } if app.HttpCliDebug > 1 { 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, nil, nil) app.Log(a) app.HttpWebSockSendTo(cli, "cmpHttpTest", map[string]interface{}{ "status" : "response", "response": a, }) }() return map[string]interface{}{ "status" : "request", "request": url, }, nil }