Beispiel #1
0
func convertPayload(in interface{}, out interface{}) error {
	var buf []byte
	if err := codec.NewEncoderBytes(&buf, payloadHandler).Encode(in); err != nil {
		return err
	}
	if err := codec.NewDecoderBytes(buf, payloadHandler).Decode(out); err != nil {
		return err
	}
	return nil
}
Beispiel #2
0
// UnpackProxyRequest unpacks a HTTPRequest from a serialized cocaine form
func UnpackProxyRequest(raw []byte) (*http.Request, error) {
	var v struct {
		Method  string
		URI     string
		Version string
		Headers Headers
		Body    []byte
	}

	if err := codec.NewDecoderBytes(raw, hHTTPReq).Decode(&v); err != nil {
		return nil, err
	}

	req, err := http.NewRequest(v.Method, v.URI, bytes.NewBuffer(v.Body))
	if err != nil {
		return nil, err
	}

	req.Header = HeadersCocaineToHTTP(v.Headers)
	req.Host = req.Header.Get("Host")

	if xRealIP := req.Header.Get("X-Real-IP"); xRealIP != "" {
		req.RemoteAddr = xRealIP
	}

	// If body is compressed it will be decompressed
	// Inspired by https://github.com/golang/go/blob/master/src/net/http/transport.go#L883
	hasBody := req != nil && req.Method != "HEAD" && req.ContentLength != 0
	if hasBody && req.Header.Get("Content-Encoding") == "gzip" {
		req.Header.Del("Content-Encoding")
		req.Header.Del("Content-Length")
		req.ContentLength = -1
		req.Body = &gzipReader{body: req.Body}
	}

	return req, nil
}