2015-02-05 21:56:50 +00:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-02-05 21:56:50 +00:00
|
|
|
"errors"
|
2015-03-25 18:11:46 +00:00
|
|
|
"net"
|
2015-02-05 21:56:50 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-10-25 11:00:01 +00:00
|
|
|
"github.com/google/uuid"
|
2015-02-05 21:56:50 +00:00
|
|
|
"github.com/gorilla/mux"
|
2017-06-23 19:45:04 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2015-02-05 21:56:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Common errors used with this package.
|
|
|
|
var (
|
2015-04-16 02:18:40 +00:00
|
|
|
ErrNoRequestContext = errors.New("no http request in context")
|
|
|
|
ErrNoResponseWriterContext = errors.New("no http response in context")
|
2015-02-05 21:56:50 +00:00
|
|
|
)
|
|
|
|
|
2015-03-25 20:44:16 +00:00
|
|
|
func parseIP(ipStr string) net.IP {
|
|
|
|
ip := net.ParseIP(ipStr)
|
|
|
|
if ip == nil {
|
|
|
|
log.Warnf("invalid remote IP address: %q", ipStr)
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
2015-03-25 18:11:46 +00:00
|
|
|
// RemoteAddr extracts the remote address of the request, taking into
|
|
|
|
// account proxy headers.
|
2015-03-24 23:46:08 +00:00
|
|
|
func RemoteAddr(r *http.Request) string {
|
2015-03-25 18:11:46 +00:00
|
|
|
if prior := r.Header.Get("X-Forwarded-For"); prior != "" {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
remoteAddr, _, _ := strings.Cut(prior, ",")
|
|
|
|
remoteAddr = strings.Trim(remoteAddr, " ")
|
|
|
|
if parseIP(remoteAddr) != nil {
|
|
|
|
return remoteAddr
|
2015-03-24 23:46:08 +00:00
|
|
|
}
|
2015-03-25 18:11:46 +00:00
|
|
|
}
|
|
|
|
// X-Real-Ip is less supported, but worth checking in the
|
|
|
|
// absence of X-Forwarded-For
|
|
|
|
if realIP := r.Header.Get("X-Real-Ip"); realIP != "" {
|
2015-03-25 20:44:16 +00:00
|
|
|
if parseIP(realIP) != nil {
|
2015-03-25 18:11:46 +00:00
|
|
|
return realIP
|
2015-03-24 23:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 18:11:46 +00:00
|
|
|
return r.RemoteAddr
|
2015-03-24 23:46:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-01 16:46:35 +00:00
|
|
|
// RemoteIP extracts the remote IP of the request, taking into
|
|
|
|
// account proxy headers.
|
|
|
|
func RemoteIP(r *http.Request) string {
|
|
|
|
addr := RemoteAddr(r)
|
|
|
|
|
|
|
|
// Try parsing it as "IP:port"
|
|
|
|
if ip, _, err := net.SplitHostPort(addr); err == nil {
|
|
|
|
return ip
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
2015-02-05 21:56:50 +00:00
|
|
|
// WithRequest places the request on the context. The context of the request
|
|
|
|
// is assigned a unique id, available at "http.request.id". The request itself
|
|
|
|
// is available at "http.request". Other common attributes are available under
|
|
|
|
// the prefix "http.request.". If a request is already present on the context,
|
|
|
|
// this method will panic.
|
2017-08-11 22:31:16 +00:00
|
|
|
func WithRequest(ctx context.Context, r *http.Request) context.Context {
|
2015-02-05 21:56:50 +00:00
|
|
|
if ctx.Value("http.request") != nil {
|
|
|
|
// NOTE(stevvooe): This needs to be considered a programming error. It
|
|
|
|
// is unlikely that we'd want to have more than one request in
|
|
|
|
// context.
|
|
|
|
panic("only one request per context")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &httpRequestContext{
|
|
|
|
Context: ctx,
|
|
|
|
startedAt: time.Now(),
|
2023-10-25 11:00:01 +00:00
|
|
|
id: uuid.NewString(),
|
2015-02-05 21:56:50 +00:00
|
|
|
r: r,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequest returns the http request in the given context. Returns
|
|
|
|
// ErrNoRequestContext if the context does not have an http request associated
|
|
|
|
// with it.
|
2017-08-11 22:31:16 +00:00
|
|
|
func GetRequest(ctx context.Context) (*http.Request, error) {
|
2015-02-05 21:56:50 +00:00
|
|
|
if r, ok := ctx.Value("http.request").(*http.Request); r != nil && ok {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
return nil, ErrNoRequestContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequestID attempts to resolve the current request id, if possible. An
|
|
|
|
// error is return if it is not available on the context.
|
2017-08-11 22:31:16 +00:00
|
|
|
func GetRequestID(ctx context.Context) string {
|
2015-02-05 21:56:50 +00:00
|
|
|
return GetStringValue(ctx, "http.request.id")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithResponseWriter returns a new context and response writer that makes
|
|
|
|
// interesting response statistics available within the context.
|
2017-08-11 22:31:16 +00:00
|
|
|
func WithResponseWriter(ctx context.Context, w http.ResponseWriter) (context.Context, http.ResponseWriter) {
|
2016-07-18 20:27:12 +00:00
|
|
|
irw := instrumentedResponseWriter{
|
|
|
|
ResponseWriter: w,
|
|
|
|
Context: ctx,
|
|
|
|
}
|
2015-08-06 22:14:44 +00:00
|
|
|
return &irw, &irw
|
2015-02-05 21:56:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-16 02:18:40 +00:00
|
|
|
// GetResponseWriter returns the http.ResponseWriter from the provided
|
|
|
|
// context. If not present, ErrNoResponseWriterContext is returned. The
|
|
|
|
// returned instance provides instrumentation in the context.
|
2017-08-11 22:31:16 +00:00
|
|
|
func GetResponseWriter(ctx context.Context) (http.ResponseWriter, error) {
|
2015-04-16 02:18:40 +00:00
|
|
|
v := ctx.Value("http.response")
|
|
|
|
|
|
|
|
rw, ok := v.(http.ResponseWriter)
|
|
|
|
if !ok || rw == nil {
|
|
|
|
return nil, ErrNoResponseWriterContext
|
|
|
|
}
|
|
|
|
|
|
|
|
return rw, nil
|
|
|
|
}
|
|
|
|
|
2015-02-05 21:56:50 +00:00
|
|
|
// getVarsFromRequest let's us change request vars implementation for testing
|
|
|
|
// and maybe future changes.
|
|
|
|
var getVarsFromRequest = mux.Vars
|
|
|
|
|
|
|
|
// WithVars extracts gorilla/mux vars and makes them available on the returned
|
|
|
|
// context. Variables are available at keys with the prefix "vars.". For
|
|
|
|
// example, if looking for the variable "name", it can be accessed as
|
|
|
|
// "vars.name". Implementations that are accessing values need not know that
|
|
|
|
// the underlying context is implemented with gorilla/mux vars.
|
2017-08-11 22:31:16 +00:00
|
|
|
func WithVars(ctx context.Context, r *http.Request) context.Context {
|
2015-02-05 21:56:50 +00:00
|
|
|
return &muxVarsContext{
|
|
|
|
Context: ctx,
|
|
|
|
vars: getVarsFromRequest(r),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequestLogger returns a logger that contains fields from the request in
|
|
|
|
// the current context. If the request is not available in the context, no
|
|
|
|
// fields will display. Request loggers can safely be pushed onto the context.
|
2017-08-11 22:31:16 +00:00
|
|
|
func GetRequestLogger(ctx context.Context) Logger {
|
2015-02-05 21:56:50 +00:00
|
|
|
return GetLogger(ctx,
|
|
|
|
"http.request.id",
|
|
|
|
"http.request.method",
|
|
|
|
"http.request.host",
|
|
|
|
"http.request.uri",
|
|
|
|
"http.request.referer",
|
|
|
|
"http.request.useragent",
|
|
|
|
"http.request.remoteaddr",
|
|
|
|
"http.request.contenttype")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetResponseLogger reads the current response stats and builds a logger.
|
|
|
|
// Because the values are read at call time, pushing a logger returned from
|
|
|
|
// this function on the context will lead to missing or invalid data. Only
|
|
|
|
// call this at the end of a request, after the response has been written.
|
2017-08-11 22:31:16 +00:00
|
|
|
func GetResponseLogger(ctx context.Context) Logger {
|
2015-02-05 21:56:50 +00:00
|
|
|
l := getLogrusLogger(ctx,
|
|
|
|
"http.response.written",
|
|
|
|
"http.response.status",
|
|
|
|
"http.response.contenttype")
|
|
|
|
|
|
|
|
duration := Since(ctx, "http.request.startedat")
|
|
|
|
|
|
|
|
if duration > 0 {
|
2015-04-01 16:46:35 +00:00
|
|
|
l = l.WithField("http.response.duration", duration.String())
|
2015-02-05 21:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpRequestContext makes information about a request available to context.
|
|
|
|
type httpRequestContext struct {
|
2017-08-11 22:31:16 +00:00
|
|
|
context.Context
|
2015-02-05 21:56:50 +00:00
|
|
|
|
|
|
|
startedAt time.Time
|
|
|
|
id string
|
|
|
|
r *http.Request
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns a keyed element of the request for use in the context. To get
|
|
|
|
// the request itself, query "request". For other components, access them as
|
|
|
|
// "request.<component>". For example, r.RequestURI
|
|
|
|
func (ctx *httpRequestContext) Value(key interface{}) interface{} {
|
|
|
|
if keyStr, ok := key.(string); ok {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
switch keyStr {
|
|
|
|
case "http.request":
|
2015-02-05 21:56:50 +00:00
|
|
|
return ctx.r
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.uri":
|
2015-02-05 21:56:50 +00:00
|
|
|
return ctx.r.RequestURI
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.remoteaddr":
|
2015-03-24 23:46:08 +00:00
|
|
|
return RemoteAddr(ctx.r)
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.method":
|
2015-02-05 21:56:50 +00:00
|
|
|
return ctx.r.Method
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.host":
|
2015-02-05 21:56:50 +00:00
|
|
|
return ctx.r.Host
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.referer":
|
2015-02-05 21:56:50 +00:00
|
|
|
referer := ctx.r.Referer()
|
|
|
|
if referer != "" {
|
|
|
|
return referer
|
|
|
|
}
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.useragent":
|
2015-02-05 21:56:50 +00:00
|
|
|
return ctx.r.UserAgent()
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.id":
|
2015-02-05 21:56:50 +00:00
|
|
|
return ctx.id
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.startedat":
|
2015-02-05 21:56:50 +00:00
|
|
|
return ctx.startedAt
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.request.contenttype":
|
|
|
|
if ct := ctx.r.Header.Get("Content-Type"); ct != "" {
|
2015-02-05 21:56:50 +00:00
|
|
|
return ct
|
|
|
|
}
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
default:
|
|
|
|
// no match; fall back to standard behavior below
|
2015-02-05 21:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Context.Value(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
type muxVarsContext struct {
|
2017-08-11 22:31:16 +00:00
|
|
|
context.Context
|
2015-02-05 21:56:50 +00:00
|
|
|
vars map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *muxVarsContext) Value(key interface{}) interface{} {
|
|
|
|
if keyStr, ok := key.(string); ok {
|
|
|
|
if keyStr == "vars" {
|
|
|
|
return ctx.vars
|
|
|
|
}
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
// TODO(thaJeztah): this considers "vars.FOO" and "FOO" to be equal.
|
|
|
|
// We need to check if that's intentional (could be a bug).
|
|
|
|
if v, ok := ctx.vars[strings.TrimPrefix(keyStr, "vars.")]; ok {
|
2015-02-05 21:56:50 +00:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Context.Value(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// instrumentedResponseWriter provides response writer information in a
|
2015-08-06 22:14:44 +00:00
|
|
|
// context. This variant is only used in the case where CloseNotifier is not
|
|
|
|
// implemented by the parent ResponseWriter.
|
2015-02-05 21:56:50 +00:00
|
|
|
type instrumentedResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
2017-08-11 22:31:16 +00:00
|
|
|
context.Context
|
2015-02-05 21:56:50 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
status int
|
|
|
|
written int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irw *instrumentedResponseWriter) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = irw.ResponseWriter.Write(p)
|
|
|
|
|
|
|
|
irw.mu.Lock()
|
|
|
|
irw.written += int64(n)
|
|
|
|
|
|
|
|
// Guess the likely status if not set.
|
|
|
|
if irw.status == 0 {
|
|
|
|
irw.status = http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
irw.mu.Unlock()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irw *instrumentedResponseWriter) WriteHeader(status int) {
|
|
|
|
irw.ResponseWriter.WriteHeader(status)
|
|
|
|
|
|
|
|
irw.mu.Lock()
|
|
|
|
irw.status = status
|
|
|
|
irw.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irw *instrumentedResponseWriter) Flush() {
|
|
|
|
if flusher, ok := irw.ResponseWriter.(http.Flusher); ok {
|
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (irw *instrumentedResponseWriter) Value(key interface{}) interface{} {
|
|
|
|
if keyStr, ok := key.(string); ok {
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
switch keyStr {
|
|
|
|
case "http.response":
|
2015-04-20 18:15:01 +00:00
|
|
|
return irw
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.response.written":
|
|
|
|
irw.mu.Lock()
|
|
|
|
defer irw.mu.Unlock()
|
2015-02-05 21:56:50 +00:00
|
|
|
return irw.written
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.response.status":
|
|
|
|
irw.mu.Lock()
|
|
|
|
defer irw.mu.Unlock()
|
2015-05-05 21:21:33 +00:00
|
|
|
return irw.status
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
case "http.response.contenttype":
|
|
|
|
if ct := irw.Header().Get("Content-Type"); ct != "" {
|
|
|
|
return ct
|
2015-02-05 21:56:50 +00:00
|
|
|
}
|
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:
```go
func BenchmarkSplit(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_ = strings.SplitN(s, "=", 2)[0]
}
}
}
func BenchmarkCut(b *testing.B) {
b.ReportAllocs()
data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
for i := 0; i < b.N; i++ {
for _, s := range data {
_, _, _ = strings.Cut(s, "=")
}
}
}
```
BenchmarkSplit
BenchmarkSplit-10 8244206 128.0 ns/op 128 B/op 4 allocs/op
BenchmarkCut
BenchmarkCut-10 54411998 21.80 ns/op 0 B/op 0 allocs/op
While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints;
- for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.
- in some cases it we were using `strings.Split()`, but _actually_ were trying to match
a prefix; for those I replaced the code to just match (and/or strip) the prefix.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-02 19:32:03 +00:00
|
|
|
default:
|
|
|
|
// no match; fall back to standard behavior below
|
2015-02-05 21:56:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return irw.Context.Value(key)
|
|
|
|
}
|