http: Eliminate allocation in cloneURL; add RemoteAddr to origRequest

This commit is contained in:
Matthew Holt 2019-11-05 16:28:33 -07:00
parent 97d918df3e
commit 6e95477224
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5

View File

@ -116,7 +116,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log = logger.Error log = logger.Error
} }
log("received request", log("handled request",
zap.String("common_log", repl.ReplaceAll(CommonLogFormat, "-")), zap.String("common_log", repl.ReplaceAll(CommonLogFormat, "-")),
zap.Duration("latency", latency), zap.Duration("latency", latency),
zap.Int("size", wrec.Size()), zap.Int("size", wrec.Size()),
@ -392,14 +392,15 @@ func errLogValues(err error) (status int, msg string, fields []zapcore.Field) {
// originalRequest returns a partial, shallow copy of // originalRequest returns a partial, shallow copy of
// req, including: req.Method, deep copy of req.URL // req, including: req.Method, deep copy of req.URL
// (into the urlCopy parameter, which should be on the // (into the urlCopy parameter, which should be on the
// stack), and req.RequestURI. Notably, headers are not // stack), req.RequestURI, and req.RemoteAddr. Notably,
// copied. This function is designed to be very fast // headers are not copied. This function is designed to
// and efficient, and useful primarly for read-only // be very fast and efficient, and useful primarly for
// logging purposes. // read-only/logging purposes.
func originalRequest(req *http.Request, urlCopy *url.URL) http.Request { func originalRequest(req *http.Request, urlCopy *url.URL) http.Request {
urlCopy = cloneURL(req.URL) cloneURL(req.URL, urlCopy)
return http.Request{ return http.Request{
Method: req.Method, Method: req.Method,
RemoteAddr: req.RemoteAddr,
RequestURI: req.RequestURI, RequestURI: req.RequestURI,
URL: urlCopy, URL: urlCopy,
} }
@ -407,14 +408,13 @@ func originalRequest(req *http.Request, urlCopy *url.URL) http.Request {
// cloneURL makes a copy of r.URL and returns a // cloneURL makes a copy of r.URL and returns a
// new value that doesn't reference the original. // new value that doesn't reference the original.
func cloneURL(u *url.URL) *url.URL { func cloneURL(from, to *url.URL) {
urlCopy := *u *to = *from
if u.User != nil { if from.User != nil {
userInfo := new(url.Userinfo) userInfo := new(url.Userinfo)
*userInfo = *u.User *userInfo = *from.User
urlCopy.User = userInfo to.User = userInfo
} }
return &urlCopy
} }
const ( const (