代理池,集
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
proxyPool/dump.go

98 lines
2.5 KiB

package main
import (
"bufio"
"bytes"
"compress/flate"
"compress/gzip"
"fmt"
8 years ago
"io"
"io/ioutil"
"math"
"net/http"
"strconv"
)
8 years ago
func httpDump(reqDump []byte, resp *http.Response) {
defer resp.Body.Close()
var respStatusStr string
respStatus := resp.StatusCode
respStatusHeader := int(math.Floor(float64(respStatus / 100)))
switch respStatusHeader {
case 2:
respStatusStr = Green("<--" + strconv.Itoa(respStatus))
case 3:
respStatusStr = Yellow("<--" + strconv.Itoa(respStatus))
case 4:
respStatusStr = Magenta("<--" + strconv.Itoa(respStatus))
case 5:
respStatusStr = Red("<--" + strconv.Itoa(respStatus))
}
8 years ago
fmt.Println(Green("Request:"), respStatusStr)
8 years ago
req, _ := ParseReq(reqDump)
fmt.Printf("%s %s %s\n", Blue(req.Method), req.Host+req.RequestURI, respStatusStr)
8 years ago
fmt.Printf("%s %s\n", Blue("RemoteAddr:"), req.RemoteAddr)
for headerName, headerContext := range req.Header {
fmt.Printf("%s: %s\n", Blue(headerName), headerContext)
8 years ago
}
8 years ago
if req.Method == "POST" {
fmt.Println(Green("POST Param:"))
err := req.ParseForm()
8 years ago
if err != nil {
logger.Println("parseForm error:", err)
} else {
for k, v := range req.Form {
fmt.Printf("\t%s: %s\n", Blue(k), v)
}
}
8 years ago
}
fmt.Println(Green("Response:"))
for headerName, headerContext := range resp.Header {
fmt.Printf("%s: %s\n", Blue(headerName), headerContext)
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Println("func httpDump read resp body err:", err)
} else {
acceptEncode := resp.Header["Content-Encoding"]
var respBodyBin bytes.Buffer
w := bufio.NewWriter(&respBodyBin)
w.Write(respBody)
w.Flush()
for _, compress := range acceptEncode {
switch compress {
case "gzip":
r, err := gzip.NewReader(&respBodyBin)
if err != nil {
logger.Println("gzip reader err:", err)
} else {
defer r.Close()
respBody, _ = ioutil.ReadAll(r)
}
break
case "deflate":
r := flate.NewReader(&respBodyBin)
defer r.Close()
respBody, _ = ioutil.ReadAll(r)
break
}
}
fmt.Printf("%s\n", string(respBody))
}
fmt.Printf("%s%s%s\n", Black("####################"), Cyan("END"), Black("####################"))
}
8 years ago
8 years ago
func ParseReq(b []byte) (*http.Request, error) {
// func ReadRequest(b *bufio.Reader) (req *Request, err error) { return readRequest(b, deleteHostHeader) }
fmt.Println(string(b))
fmt.Println("-----------------------")
var buf io.ReadWriter
buf = new(bytes.Buffer)
buf.Write(b)
bufr := bufio.NewReader(buf)
return http.ReadRequest(bufr)
8 years ago
}