Skip to content

Commit 0cf80d4

Browse files
committed
Remove stage1-specific URL test (buildBackendURL not in stage2)
1 parent 7d377e7 commit 0cf80d4

File tree

4 files changed

+36
-210
lines changed

4 files changed

+36
-210
lines changed

backend.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -755,23 +755,14 @@ func (b *Backend) doForward(ctx context.Context, rpcReqs []*RPCReq, isBatch bool
755755
return nil, wrapErr(err, "error creating backend request")
756756
}
757757

758+
// Forward all collected headers (includes URL-derived and request headers)
758759
headersToForward := GetHeadersToForward(ctx)
759-
if len(headersToForward) != 0 {
760-
for k, v := range headersToForward {
761-
for _, value := range v {
762-
httpReq.Header.Add(k, value)
763-
}
760+
for k, v := range headersToForward {
761+
for _, value := range v {
762+
httpReq.Header.Add(k, value)
764763
}
765764
}
766765

767-
// Forward original URL context as headers for all methods
768-
if path, ok := ctx.Value(ContextKeyPath).(string); ok && path != "" && path != "/" {
769-
httpReq.Header.Set("X-Original-Path", path)
770-
}
771-
if rawQuery, ok := ctx.Value(ContextKeyRawQuery).(string); ok && rawQuery != "" {
772-
httpReq.Header.Set("X-Original-Query", rawQuery)
773-
}
774-
775766
if b.authPassword != "" {
776767
httpReq.SetBasicAuth(b.authUsername, b.authPassword)
777768
}

backend_test.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,25 +240,31 @@ func TestHeaderForwarding(t *testing.T) {
240240

241241
for _, tt := range tests {
242242
t.Run(tt.name, func(t *testing.T) {
243-
// Create context with path and query
243+
// Create context with headers to forward (unified approach)
244244
ctx := context.Background()
245+
headersToForward := make(map[string][]string)
246+
245247
if tt.path != "" && tt.path != "/" {
246-
ctx = context.WithValue(ctx, ContextKeyPath, tt.path)
248+
headersToForward["X-Original-Path"] = []string{tt.path}
247249
}
248250
if tt.query != "" {
249-
ctx = context.WithValue(ctx, ContextKeyRawQuery, tt.query)
251+
headersToForward["X-Original-Query"] = []string{tt.query}
252+
}
253+
254+
if len(headersToForward) > 0 {
255+
ctx = context.WithValue(ctx, ContextKeyHeadersToForward, headersToForward)
250256
}
251257

252258
// Create a mock HTTP request to verify headers are set
253259
req, err := http.NewRequestWithContext(ctx, "POST", "http://backend:8080", nil)
254260
require.NoError(t, err)
255261

256262
// Simulate the header forwarding logic from doForward()
257-
if path, ok := ctx.Value(ContextKeyPath).(string); ok && path != "" && path != "/" {
258-
req.Header.Set("X-Original-Path", path)
259-
}
260-
if rawQuery, ok := ctx.Value(ContextKeyRawQuery).(string); ok && rawQuery != "" {
261-
req.Header.Set("X-Original-Query", rawQuery)
263+
headersToForwardFromCtx := GetHeadersToForward(ctx)
264+
for k, v := range headersToForwardFromCtx {
265+
for _, value := range v {
266+
req.Header.Add(k, value)
267+
}
262268
}
263269

264270
// Verify the expected headers are set correctly

backend_url_edge_cases_test.go

Lines changed: 0 additions & 172 deletions
This file was deleted.

server.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ const (
3939
ContextKeyOpTxProxyAuth = "op_txproxy_auth"
4040
ContextKeyInteropValidationStrategy = "interop_validation_strategy"
4141
ContextKeyHeadersToForward = "headers_to_forward"
42-
ContextKeyRawQuery = "raw_query"
43-
ContextKeyPath = "path"
4442
DefaultOpTxProxyAuthHeader = "X-Optimism-Signature"
4543
FlashbotsAuthHeader = "X-Flashbots-Signature"
4644
DefaultMaxBatchRPCCallsLimit = 100
@@ -782,10 +780,6 @@ func (s *Server) populateContext(w http.ResponseWriter, r *http.Request) context
782780

783781
ctx := context.WithValue(r.Context(), ContextKeyXForwardedFor, xff) // nolint:staticcheck
784782

785-
// Store query parameters and path for forwarding to backend
786-
ctx = context.WithValue(ctx, ContextKeyRawQuery, r.URL.RawQuery) // nolint:staticcheck
787-
ctx = context.WithValue(ctx, ContextKeyPath, r.URL.Path) // nolint:staticcheck
788-
789783
opTxProxyAuth := r.Header.Get(DefaultOpTxProxyAuthHeader)
790784
if opTxProxyAuth != "" {
791785
ctx = context.WithValue(ctx, ContextKeyOpTxProxyAuth, opTxProxyAuth) // nolint:staticcheck
@@ -802,19 +796,26 @@ func (s *Server) populateContext(w http.ResponseWriter, r *http.Request) context
802796
ctx = context.WithValue(ctx, ContextKeyAuth, s.authenticatedPaths[authorization]) // nolint:staticcheck
803797
}
804798

805-
if len(s.allowedDynamicHeaders) > 0 {
806-
filteredHeaderValues := make(map[string][]string)
807-
for _, h := range s.allowedDynamicHeaders {
808-
values := r.Header.Values(h)
809-
if len(values) > 0 {
810-
filteredHeaderValues[h] = values
811-
}
812-
}
813-
if len(filteredHeaderValues) > 0 {
814-
log.Info("proxying dynamic headers")
815-
ctx = context.WithValue(ctx, ContextKeyHeadersToForward, filteredHeaderValues) // nolint:staticcheck
799+
// Collect all headers to forward to backends
800+
headersToForward := make(map[string][]string)
801+
802+
// Always include URL context as synthetic headers
803+
if r.URL.Path != "" && r.URL.Path != "/" {
804+
headersToForward["X-Original-Path"] = []string{r.URL.Path}
805+
}
806+
if r.URL.RawQuery != "" {
807+
headersToForward["X-Original-Query"] = []string{r.URL.RawQuery}
808+
}
809+
810+
// Conditionally include allowed request headers
811+
for _, h := range s.allowedDynamicHeaders {
812+
if values := r.Header.Values(h); len(values) > 0 {
813+
headersToForward[h] = values
816814
}
815+
}
817816

817+
if len(headersToForward) > 0 {
818+
ctx = context.WithValue(ctx, ContextKeyHeadersToForward, headersToForward) // nolint:staticcheck
818819
}
819820

820821
return context.WithValue(

0 commit comments

Comments
 (0)