-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.go
More file actions
111 lines (93 loc) · 3.02 KB
/
Copy pathhelpers.go
File metadata and controls
111 lines (93 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package groxy
import "net"
// BodyTransform transforms a request or response body.
type BodyTransform func([]byte) ([]byte, error)
// AddRequestHeader returns middleware that sets a request header before the
// request is sent upstream.
func AddRequestHeader(key, value string) Middleware {
return newRequestMiddleware("AddRequestHeader", func(ctx *RequestContext) error {
ctx.Request.Header.Set(key, value)
return nil
})
}
// AddResponseHeader returns middleware that sets a response header before the
// response is sent back to the client.
func AddResponseHeader(key, value string) Middleware {
return newResponseMiddleware("AddResponseHeader", func(ctx *ResponseContext) error {
ctx.Response.Header.Set(key, value)
return nil
})
}
// RemoveRequestHeader returns middleware that deletes a request header before
// the request is sent upstream.
func RemoveRequestHeader(key string) Middleware {
return newRequestMiddleware("RemoveRequestHeader", func(ctx *RequestContext) error {
ctx.Request.Header.Del(key)
return nil
})
}
// RemoveResponseHeader returns middleware that deletes a response header before
// the response is sent back to the client.
func RemoveResponseHeader(key string) Middleware {
return newResponseMiddleware("RemoveResponseHeader", func(ctx *ResponseContext) error {
ctx.Response.Header.Del(key)
return nil
})
}
// BlockHost returns middleware that blocks normal HTTP requests to host.
func BlockHost(host string, statusCode int, message string) Middleware {
return newRequestMiddleware("BlockHost", func(ctx *RequestContext) error {
if ctx.Request.URL.Hostname() == host {
return Block(statusCode, message)
}
return nil
})
}
// BlockConnectHost returns middleware that blocks CONNECT tunnels to host.
func BlockConnectHost(host string, statusCode int, message string) Middleware {
return newConnectMiddleware("BlockConnectHost", func(ctx *ConnectContext) error {
if connectHostname(ctx.Host) == host {
return Block(statusCode, message)
}
return nil
})
}
// TransformRequestBody returns middleware that replaces a request body with the
// bytes returned by transform.
func TransformRequestBody(transform BodyTransform) Middleware {
return newRequestMiddleware("TransformRequestBody", func(ctx *RequestContext) error {
body, err := ctx.BodyBytes()
if err != nil {
return err
}
updatedBody, err := transform(body)
if err != nil {
return err
}
ctx.SetBody(updatedBody)
return nil
})
}
// TransformResponseBody returns middleware that replaces a response body with
// the bytes returned by transform.
func TransformResponseBody(transform BodyTransform) Middleware {
return newResponseMiddleware("TransformResponseBody", func(ctx *ResponseContext) error {
body, err := ctx.BodyBytes()
if err != nil {
return err
}
updatedBody, err := transform(body)
if err != nil {
return err
}
ctx.SetBody(updatedBody)
return nil
})
}
func connectHostname(hostport string) string {
host, _, err := net.SplitHostPort(hostport)
if err != nil {
return hostport
}
return host
}