Skip to content

Commit

Permalink
feat: remove specific headers from request before sending to target (#1)
Browse files Browse the repository at this point in the history
The code has been updated to remove specific headers from the request before sending it to the target. This is done by checking for query parameters with a specific prefix and removing the corresponding headers from the request. This change ensures that unwanted headers are not sent to the target server.
  • Loading branch information
xhofe committed Mar 3, 2024
1 parent e9a1e55 commit 7c64512
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion proxies.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/gin-gonic/gin"
)

var magicQueryKeyPrefix = "_sp_"

var HttpClient = &http.Client{}

func proxiesHandle(app *gin.Engine) {
Expand All @@ -28,8 +30,15 @@ func proxy(source string, target string) gin.HandlerFunc {
target = strings.Replace(target, ":"+param.Key, param.Value, 1)
}
}
rawQuery := ctx.Request.URL.Query()
if ctx.Request.URL.RawQuery != "" {
target += "?" + ctx.Request.URL.RawQuery
query := ctx.Request.URL.Query()
for k, _ := range query {
if strings.HasPrefix(k, magicQueryKeyPrefix) {
query.Del(k)
}
}
target += "?" + query.Encode()
}
if !strings.HasPrefix(target, "http") {
target = "https://" + target
Expand All @@ -48,6 +57,9 @@ func proxy(source string, target string) gin.HandlerFunc {
for h, val := range ctx.Request.Header {
req.Header[h] = val
}
for _, v := range rawQuery[magicQueryKeyPrefix+"del_headers"] {
req.Header.Del(v)
}
res, err := HttpClient.Do(req)
if err != nil {
ctx.JSON(500, gin.H{
Expand Down
15 changes: 15 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"net/url"
"strings"
)

func getKeysByPrefix(query url.Values, prefix string) (keys []string) {
for k, _ := range query {
if strings.HasPrefix(k, prefix) {
keys = append(keys, k)
}
}
return
}

0 comments on commit 7c64512

Please sign in to comment.