2017-11-22 17:20:03 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
2018-07-11 08:08:03 +00:00
|
|
|
// SerializableHttpRequest serializable HTTP request
|
2017-11-22 17:20:03 +00:00
|
|
|
type SerializableHttpRequest struct {
|
|
|
|
Method string
|
|
|
|
URL *url.URL
|
|
|
|
Proto string // "HTTP/1.0"
|
|
|
|
ProtoMajor int // 1
|
|
|
|
ProtoMinor int // 0
|
|
|
|
Header http.Header
|
|
|
|
ContentLength int64
|
|
|
|
TransferEncoding []string
|
|
|
|
Host string
|
|
|
|
Form url.Values
|
|
|
|
PostForm url.Values
|
|
|
|
MultipartForm *multipart.Form
|
|
|
|
Trailer http.Header
|
|
|
|
RemoteAddr string
|
|
|
|
RequestURI string
|
|
|
|
TLS *tls.ConnectionState
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:08:03 +00:00
|
|
|
// Clone clone a request
|
2017-11-22 17:20:03 +00:00
|
|
|
func Clone(r *http.Request) *SerializableHttpRequest {
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rc := new(SerializableHttpRequest)
|
|
|
|
rc.Method = r.Method
|
|
|
|
rc.URL = r.URL
|
|
|
|
rc.Proto = r.Proto
|
|
|
|
rc.ProtoMajor = r.ProtoMajor
|
|
|
|
rc.ProtoMinor = r.ProtoMinor
|
|
|
|
rc.Header = r.Header
|
|
|
|
rc.ContentLength = r.ContentLength
|
|
|
|
rc.Host = r.Host
|
|
|
|
rc.RemoteAddr = r.RemoteAddr
|
|
|
|
rc.RequestURI = r.RequestURI
|
|
|
|
return rc
|
|
|
|
}
|
|
|
|
|
2018-07-11 08:08:03 +00:00
|
|
|
// ToJson serializes to JSON
|
2017-11-22 17:20:03 +00:00
|
|
|
func (s *SerializableHttpRequest) ToJson() string {
|
2018-07-11 08:08:03 +00:00
|
|
|
jsonVal, err := json.Marshal(s)
|
|
|
|
if err != nil || jsonVal == nil {
|
|
|
|
return fmt.Sprintf("Error marshalling SerializableHttpRequest to json: %s", err)
|
2017-11-22 17:20:03 +00:00
|
|
|
}
|
2018-07-11 08:08:03 +00:00
|
|
|
return string(jsonVal)
|
2017-11-22 17:20:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 08:08:03 +00:00
|
|
|
// DumpHttpRequest dump a HTTP request to JSON
|
2017-11-22 17:20:03 +00:00
|
|
|
func DumpHttpRequest(req *http.Request) string {
|
2018-07-11 08:08:03 +00:00
|
|
|
return Clone(req).ToJson()
|
2017-11-22 17:20:03 +00:00
|
|
|
}
|