Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 13:30 UTC
rewrite: skip query rename when source key is absent (#7599)
Tao committed
Mar 29, 2026 at 03:10 UTC
6f6771aa1deecd39405991d9ae4e69d4d93b1e5f
2 files changed
+58
-1
modules/caddyhttp/rewrite/rewrite.go
+8
-1
@@ -529,7 +529,14 @@ func (q *queryOps) do(r *http.Request, repl *caddy.Replacer) {
529
if key == "" || val == "" {
530
continue
531
}
532
- query[val] = query[key]
532
+ if key == val {
533
+ continue
534
+ }
535
+ originalValues, ok := query[key]
536
+ if !ok {
537
+ continue
538
+ }
539
+ query[val] = originalValues
540
delete(query, key)
541
}
542
modules/caddyhttp/rewrite/rewrite_test.go
+50
@@ -16,6 +16,7 @@ package rewrite
16
17
import (
18
"net/http"
19
+ "reflect"
20
"regexp"
21
"testing"
22
@@ -397,6 +398,55 @@ func TestRewrite(t *testing.T) {
398
}
399
}
400
401
+func TestQueryOpsRenameNoOpCases(t *testing.T) {
402
+ repl := caddy.NewReplacer()
403
+
404
+ for i, tc := range []struct {
405
+ input *http.Request
406
+ expect map[string][]string
407
+ ops *queryOps
408
+ }{
409
+ {
410
+ ops: &queryOps{
411
+ Rename: []queryOpsArguments{{Key: "ID", Val: "id"}},
412
+ },
413
+ input: newRequest(t, "GET", "/?page=test&id=5&test=100"),
414
+ expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
415
+ },
416
+ {
417
+ ops: &queryOps{
418
+ Rename: []queryOpsArguments{{Key: "id", Val: "id"}},
419
+ },
420
+ input: newRequest(t, "GET", "/?page=test&id=5&test=100"),
421
+ expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
422
+ },
423
+ {
424
+ ops: &queryOps{
425
+ Rename: []queryOpsArguments{{Key: "ID", Val: "id"}},
426
+ },
427
+ input: newRequest(t, "GET", "/?page=test&ID=5&test=100"),
428
+ expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
429
+ },
430
+ {
431
+ ops: &queryOps{
432
+ Rename: []queryOpsArguments{{Key: "ID", Val: "id"}},
433
+ },
434
+ input: newRequest(t, "GET", "/?page=test&ID=5&id=7&test=100"),
435
+ expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
436
+ },
437
+ } {
438
+ repl.Set("http.request.uri", tc.input.RequestURI)
439
+ repl.Set("http.request.uri.path", tc.input.URL.Path)
440
+ repl.Set("http.request.uri.query", tc.input.URL.RawQuery)
441
+
442
+ tc.ops.do(tc.input, repl)
443
+
444
+ if actual := tc.input.URL.Query(); !reflect.DeepEqual(tc.expect, map[string][]string(actual)) {
445
+ t.Errorf("Test %d: Expected query=%v but got %v", i, tc.expect, actual)
446
+ }
447
+ }
448
+}
449
+
450
func newRequest(t *testing.T, method, uri string) *http.Request {
451
req, err := http.NewRequest(method, uri, nil)
452
if err != nil {