Skip to content

Commit 6defba2

Browse files
committed
Simplify backend URL parsing by requiring proxy_to to include the scheme and update example configurations accordingly.
1 parent f28056b commit 6defba2

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Response: Returns backend response to client
4545

4646
```csv
4747
subdomain,proxy_to,strip_path,strip_query
48-
eth,backend.example.com/api,false,false
48+
eth,https://backend.example.com/api,false,false
4949
```
5050

5151
### Fields
@@ -57,12 +57,12 @@ eth,backend.example.com/api,false,false
5757

5858
### Examples
5959

60-
| Configuration | Incoming Request | Proxied Request |
61-
|----------------------------------------------|-------------------------------------------------|------------------------------------------------|
62-
| `eth,backend.example.com/v1/abc,true,true` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc` |
63-
| `eth,backend.example.com/v1/abc,false,true` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc/foo` |
64-
| `eth,backend.example.com/v1/abc,false,false` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc/foo?bar=1` |
65-
| `eth,backend.example.com/v1/abc,true,false` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc?bar=1` |
60+
| Configuration | Incoming Request | Proxied Request |
61+
|------------------------------------------------------|-------------------------------------------------|------------------------------------------------|
62+
| `eth,https://backend.example.com/v1/abc,true,true` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc` |
63+
| `eth,https://backend.example.com/v1/abc,false,true` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc/foo` |
64+
| `eth,https://backend.example.com/v1/abc,false,false` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc/foo?bar=1` |
65+
| `eth,https://backend.example.com/v1/abc,true,false` | `https://eth.test-api.pocket.network/foo?bar=1` | `https://backend.example.com/v1/abc?bar=1` |
6666

6767
### Header Forwarding
6868

examples/proxies.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
subdomain,proxy_to,strip_path,strip_query
2-
httpbin_strip,localhost:4040/get,true,true
3-
httpbin_path,localhost:4040/anything,false,true
4-
httpbin,localhost:4040,false,false
2+
httpbin_strip,http://localhost:4040/get,true,true
3+
httpbin_path,http://localhost:4040/anything,false,true
4+
httpbin,http://localhost:4040,false,false

main.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,8 @@ func (s *ProxyService) HandleProxy(w http.ResponseWriter, r *http.Request) {
277277
return
278278
}
279279

280-
// Parse backend URL
281-
scheme := "https"
282-
if r.TLS == nil {
283-
scheme = "http"
284-
}
285-
targetURL, err := url.Parse(fmt.Sprintf("%s://%s", scheme, rule.ProxyTo))
280+
// Parse backend URL (proxy_to must include scheme: http:// or https://)
281+
targetURL, err := url.Parse(rule.ProxyTo)
286282
if err != nil {
287283
log.Printf("ERROR: Invalid proxy_to URL for subdomain '%s': %v", subdomain, err)
288284
proxyRequestsTotal.WithLabelValues(subdomain, "500").Inc()
@@ -302,7 +298,7 @@ func (s *ProxyService) HandleProxy(w http.ResponseWriter, r *http.Request) {
302298
startTime: start,
303299
rule: rule,
304300
targetURL: targetURL,
305-
scheme: scheme,
301+
scheme: targetURL.Scheme,
306302
host: host,
307303
clientIP: clientIP,
308304
originalPath: r.URL.Path,
@@ -311,7 +307,7 @@ func (s *ProxyService) HandleProxy(w http.ResponseWriter, r *http.Request) {
311307
r = r.WithContext(ctx)
312308

313309
// Get or create a cached proxy for this backend
314-
cacheKey := fmt.Sprintf("%s://%s", scheme, rule.ProxyTo)
310+
cacheKey := targetURL.Scheme + "://" + targetURL.Host
315311
var proxy *httputil.ReverseProxy
316312

317313
if cached, ok := s.proxies.Load(cacheKey); ok {

0 commit comments

Comments
 (0)