You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.7 KiB
Go

4 years ago
package flow
import (
4 years ago
"encoding/json"
4 years ago
"net/http"
"net/url"
4 years ago
_log "github.com/sirupsen/logrus"
4 years ago
)
4 years ago
var log = _log.WithField("at", "flow")
4 years ago
type Request struct {
Method string
URL *url.URL
Proto string
Header http.Header
Body []byte
4 years ago
raw *http.Request
}
4 years ago
func (req *Request) MarshalJSON() ([]byte, error) {
r := make(map[string]interface{})
r["method"] = req.Method
r["url"] = req.URL.String()
r["proto"] = req.Proto
r["header"] = req.Header
r["body"] = req.Body
return json.Marshal(r)
}
4 years ago
func NewRequest(req *http.Request) *Request {
return &Request{
Method: req.Method,
URL: req.URL,
Proto: req.Proto,
Header: req.Header,
raw: req,
}
}
func (r *Request) Raw() *http.Request {
return r.raw
4 years ago
}
type Response struct {
4 years ago
StatusCode int `json:"statusCode"`
Header http.Header `json:"header"`
Body []byte `json:"body"`
4 years ago
decodedBody []byte
decoded bool // decoded reports whether the response was sent compressed but was decoded to decodedBody.
decodedErr error
4 years ago
}
type Flow struct {
*Request
*Response
// https://docs.mitmproxy.org/stable/overview-features/#streaming
// 如果为 true则不缓冲 Request.Body 和 Response.Body且不进入之后的 Addon.Request 和 Addon.Response
Stream bool
done chan struct{}
4 years ago
State map[string]interface{} // Can add value by addon
}
func (f *Flow) MarshalJSON() ([]byte, error) {
j := make(map[string]interface{})
j["request"] = f.Request
j["response"] = f.Response
return json.Marshal(j)
4 years ago
}
func NewFlow() *Flow {
4 years ago
return &Flow{done: make(chan struct{}), State: make(map[string]interface{})}
4 years ago
}
func (f *Flow) Done() <-chan struct{} {
return f.done
}
func (f *Flow) Finish() {
close(f.done)
}