summaryrefslogtreecommitdiffhomepage
path: root/server.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2023-10-09 00:19:42 +0900
committernsfisis <nsfisis@gmail.com>2023-10-09 00:19:42 +0900
commitd137a764d050e3d5296da2830a32f6d83bdb364f (patch)
treebee59ab913cd7fc03c8f1209aaa12d012dafe161 /server.go
parent6f97769e4fc893e7607652e3cbfcf5698e2256f4 (diff)
downloadmioproxy-d137a764d050e3d5296da2830a32f6d83bdb364f.tar.gz
mioproxy-d137a764d050e3d5296da2830a32f6d83bdb364f.tar.zst
mioproxy-d137a764d050e3d5296da2830a32f6d83bdb364f.zip
support path-based proxy
Diffstat (limited to 'server.go')
-rw-r--r--server.go16
1 files changed, 15 insertions, 1 deletions
diff --git a/server.go b/server.go
index 47c52dd..830a6e4 100644
--- a/server.go
+++ b/server.go
@@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httputil"
"net/url"
+ "strings"
)
type multipleReverseProxyServer struct {
@@ -16,10 +17,22 @@ type multipleReverseProxyServer struct {
type rewriteRule struct {
fromHost string
+ fromPath string
toUrl *url.URL
proxy *httputil.ReverseProxy
}
+func (r *rewriteRule) matches(host, path string) bool {
+ ret := true
+ if r.fromHost != "" {
+ ret = ret && r.fromHost == host
+ }
+ if r.fromPath != "" {
+ ret = ret && strings.HasPrefix(path+"/", r.fromPath)
+ }
+ return ret
+}
+
func newMultipleReverseProxyServer(ps []ProxyConfig) *multipleReverseProxyServer {
var rules []rewriteRule
for _, p := range ps {
@@ -30,6 +43,7 @@ func newMultipleReverseProxyServer(ps []ProxyConfig) *multipleReverseProxyServer
}
rules = append(rules, rewriteRule{
fromHost: p.From.Host,
+ fromPath: p.From.Path,
toUrl: targetUrl,
proxy: &httputil.ReverseProxy{
Rewrite: func(r *httputil.ProxyRequest) {
@@ -50,7 +64,7 @@ func (s *multipleReverseProxyServer) tryServeHTTP(
hostWithoutPort string,
) bool {
for _, rule := range s.rules {
- if rule.fromHost == hostWithoutPort {
+ if rule.matches(hostWithoutPort, r.URL.Path) {
rule.proxy.ServeHTTP(w, r)
return true
}