@marcel91200 / caddy Mirror · read-only / commits / 6e954772
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

http: Eliminate allocation in cloneURL; add RemoteAddr to origRequest

Matthew Holt committed Nov 5, 2019 at 16:28 UTC 6e95477224d5fe0856c4fba0f71afe1d7289ed74
1 file changed +12 -12
modules/caddyhttp/server.go
+12 -12
@@ -116,7 +116,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
116 log = logger.Error
117 }
118
119 - log("received request",
119 + log("handled request",
120 zap.String("common_log", repl.ReplaceAll(CommonLogFormat, "-")),
121 zap.Duration("latency", latency),
122 zap.Int("size", wrec.Size()),
@@ -392,14 +392,15 @@ func errLogValues(err error) (status int, msg string, fields []zapcore.Field) {
392 // originalRequest returns a partial, shallow copy of
393 // req, including: req.Method, deep copy of req.URL
394 // (into the urlCopy parameter, which should be on the
395 -// stack), and req.RequestURI. Notably, headers are not
396 -// copied. This function is designed to be very fast
397 -// and efficient, and useful primarly for read-only
398 -// logging purposes.
395 +// stack), req.RequestURI, and req.RemoteAddr. Notably,
396 +// headers are not copied. This function is designed to
397 +// be very fast and efficient, and useful primarly for
398 +// read-only/logging purposes.
399 func originalRequest(req *http.Request, urlCopy *url.URL) http.Request {
400 - urlCopy = cloneURL(req.URL)
400 + cloneURL(req.URL, urlCopy)
401 return http.Request{
402 Method: req.Method,
403 + RemoteAddr: req.RemoteAddr,
404 RequestURI: req.RequestURI,
405 URL: urlCopy,
406 }
@@ -407,14 +408,13 @@ func originalRequest(req *http.Request, urlCopy *url.URL) http.Request {
408
409 // cloneURL makes a copy of r.URL and returns a
410 // new value that doesn't reference the original.
410 -func cloneURL(u *url.URL) *url.URL {
411 - urlCopy := *u
412 - if u.User != nil {
411 +func cloneURL(from, to *url.URL) {
412 + *to = *from
413 + if from.User != nil {
414 userInfo := new(url.Userinfo)
414 - *userInfo = *u.User
415 - urlCopy.User = userInfo
415 + *userInfo = *from.User
416 + to.User = userInfo
417 }
417 - return &urlCopy
418 }
419
420 const (