Skip to content
This repository has been archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
centralize the header const values
Browse files Browse the repository at this point in the history
  • Loading branch information
robbyt committed Sep 3, 2024
1 parent f4274e3 commit 7764ef5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
10 changes: 3 additions & 7 deletions proxy/addons/addIDToHeaders.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package addons

import (
"github.com/proxati/llm_proxy/v2/schema/headers"
"github.com/proxati/llm_proxy/v2/version"
px "github.com/proxati/mitmproxy/proxy"
)

const (
idHeader = "X-Llm_proxy-id"
idVersion = "X-Llm_proxy-version"
)

type AddIDToHeaders struct {
px.BaseAddon
}
Expand All @@ -19,6 +15,6 @@ func NewAddIDToHeaders() *AddIDToHeaders {
}

func (c *AddIDToHeaders) Response(f *px.Flow) {
f.Response.Header.Add(idHeader, f.Id.String())
f.Response.Header.Add(idVersion, version.String())
f.Response.Header.Add(headers.ProxyID, f.Id.String())
f.Response.Header.Add(headers.Version, version.String())
}
11 changes: 4 additions & 7 deletions proxy/addons/scheme_upgrader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package addons
import (
"log/slog"

"github.com/proxati/llm_proxy/v2/schema/headers"
px "github.com/proxati/mitmproxy/proxy"
)

const (
upgradedHeader = "X-Llm_proxy-scheme-upgraded"
)

type SchemeUpgrader struct {
px.BaseAddon
logger *slog.Logger
Expand All @@ -31,14 +28,14 @@ func (c *SchemeUpgrader) Request(f *px.Flow) {
}

// add a header to the request to indicate that the scheme was upgraded
f.Request.Header.Add(upgradedHeader, "true")
f.Request.Header.Add(headers.SchemeUpgraded, "true")

// upgrade the connection from http to https, so when sent upstream it will be encrypted
f.Request.URL.Scheme = "https"
}

func (c *SchemeUpgrader) Response(f *px.Flow) {
if f.Request.Header.Get(upgradedHeader) != "" {
f.Response.Header.Add(upgradedHeader, "true")
if f.Request.Header.Get(headers.SchemeUpgraded) != "" {
f.Response.Header.Add(headers.SchemeUpgraded, "true")
}
}
5 changes: 3 additions & 2 deletions proxy/addons/scheme_upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"log/slog"
"testing"

"github.com/proxati/llm_proxy/v2/schema/headers"
px "github.com/proxati/mitmproxy/proxy"
"github.com/stretchr/testify/assert"
)
Expand All @@ -21,7 +22,7 @@ func TestSchemeUpgrader_Request(t *testing.T) {

upgrader.Request(flow)
assert.Equal(t, "https", flow.Request.URL.Scheme)
assert.Equal(t, "true", flow.Request.Header.Get(upgradedHeader))
assert.Equal(t, "true", flow.Request.Header.Get(headers.SchemeUpgraded))
}

func TestSchemeUpgrader_Request_HTTPS(t *testing.T) {
Expand All @@ -37,5 +38,5 @@ func TestSchemeUpgrader_Request_HTTPS(t *testing.T) {

upgrader.Request(flow)
assert.Equal(t, "https", flow.Request.URL.Scheme)
assert.Equal(t, "", flow.Request.Header.Get(upgradedHeader))
assert.Equal(t, "", flow.Request.Header.Get(headers.SchemeUpgraded))
}
7 changes: 7 additions & 0 deletions schema/headers/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package headers

const (
ProxyID = "X-Llm_proxy-id"
Version = "X-Llm_proxy-version"
SchemeUpgraded = "X-Llm_proxy-scheme-upgraded"
)

0 comments on commit 7764ef5

Please sign in to comment.