move dump to get info
This commit is contained in:
parent
e6f222c497
commit
bdaa96ef56
21
capture.go
21
capture.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -22,12 +23,9 @@ type CaptureList struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Capture struct {
|
type Capture struct {
|
||||||
ID int `json:"id"`
|
ID int
|
||||||
Path string `json:"path"`
|
Req *http.Request
|
||||||
Method string `json:"method"`
|
Res *http.Response
|
||||||
Status int `json:"status"`
|
|
||||||
Request string `json:"request"`
|
|
||||||
Response string `json:"response"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CaptureMetadata struct {
|
type CaptureMetadata struct {
|
||||||
|
@ -37,12 +35,17 @@ type CaptureMetadata struct {
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CaptureDump struct {
|
||||||
|
Request string `json:"request"`
|
||||||
|
Response string `json:"response"`
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Capture) Metadata() CaptureMetadata {
|
func (c *Capture) Metadata() CaptureMetadata {
|
||||||
return CaptureMetadata{
|
return CaptureMetadata{
|
||||||
ID: c.ID,
|
ID: c.ID,
|
||||||
Path: c.Path,
|
Path: c.Req.URL.Path,
|
||||||
Method: c.Method,
|
Method: c.Req.Method,
|
||||||
Status: c.Status,
|
Status: c.Res.StatusCode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
38
main.go
38
main.go
|
@ -82,7 +82,7 @@ func NewDashboardItemInfoHandler(repo CaptureRepository) http.Handler {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rw.Header().Add("Content-Type", "application/json")
|
rw.Header().Add("Content-Type", "application/json")
|
||||||
json.NewEncoder(rw).Encode(capture)
|
json.NewEncoder(rw).Encode(dump(capture))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,34 +109,26 @@ func NewPlugin(next http.Handler) http.Handler {
|
||||||
|
|
||||||
func NewRecorder(repo CaptureRepository, next http.Handler) http.Handler {
|
func NewRecorder(repo CaptureRepository, next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
reqDump, err := dumpRequest(req)
|
|
||||||
if err != nil {
|
// save req body for later
|
||||||
fmt.Printf("could not dump request: %v\n", err)
|
var reqBody []byte
|
||||||
}
|
req.Body, reqBody = drain(req.Body)
|
||||||
|
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
|
||||||
next.ServeHTTP(rec, req)
|
next.ServeHTTP(rec, req)
|
||||||
|
|
||||||
|
// respond
|
||||||
for k, v := range rec.HeaderMap {
|
for k, v := range rec.HeaderMap {
|
||||||
rw.Header()[k] = v
|
rw.Header()[k] = v
|
||||||
}
|
}
|
||||||
rw.WriteHeader(rec.Code)
|
rw.WriteHeader(rec.Code)
|
||||||
rw.Write(rec.Body.Bytes())
|
rw.Write(rec.Body.Bytes())
|
||||||
|
|
||||||
|
// record req and res
|
||||||
|
req.Body = ioutil.NopCloser(bytes.NewReader(reqBody))
|
||||||
res := rec.Result()
|
res := rec.Result()
|
||||||
resDump, err := dumpResponse(res)
|
repo.Insert(Capture{Req: req, Res: res})
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("could not dump response: %v\n", err)
|
|
||||||
}
|
|
||||||
capture := Capture{
|
|
||||||
Path: req.URL.Path,
|
|
||||||
Method: req.Method,
|
|
||||||
Status: res.StatusCode,
|
|
||||||
Request: string(reqDump),
|
|
||||||
Response: string(resDump),
|
|
||||||
}
|
|
||||||
repo.Insert(capture)
|
|
||||||
emitToDashboard(repo.FindAll())
|
emitToDashboard(repo.FindAll())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -155,6 +147,18 @@ func NewProxyHandler(URL string) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dump(c *Capture) CaptureDump {
|
||||||
|
reqDump, err := dumpRequest(c.Req)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("could not dump request: %v\n", err)
|
||||||
|
}
|
||||||
|
resDump, err := dumpResponse(c.Res)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("could not dump response: %v\n", err)
|
||||||
|
}
|
||||||
|
return CaptureDump{Request: string(reqDump), Response: string(resDump)}
|
||||||
|
}
|
||||||
|
|
||||||
func dumpRequest(req *http.Request) ([]byte, error) {
|
func dumpRequest(req *http.Request) ([]byte, error) {
|
||||||
if req.Header.Get("Content-Encoding") == "gzip" {
|
if req.Header.Get("Content-Encoding") == "gzip" {
|
||||||
var reqBody []byte
|
var reqBody []byte
|
||||||
|
|
Loading…
Reference in a new issue