Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add option to lowercase the parameters before sorting. #4

Merged
merged 8 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.20.x]
go-version: [1.21.x]
platform: [ubuntu-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
48 changes: 41 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,63 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/teodorescuserban/caddy-argsort)](https://goreportcard.com/report/github.com/teodorescuserban/caddy-argsort)

This is a caddy plugin. Works with caddy 2.
Sort the request query arguments.
Sort the request query arguments. Optionally case insensitive.

## Usage

Caddyfile:
### Set the module order

You will need to specify the execution order of this module in your caddyfile. This is done in the global options block.

```caddyfile
# Add this block in top-level settings:
{
...
order argsort before rewrite
order argsort before header
...
}
```

### Simple usage

Once the order has been set in the global options block, use `argsort` in any server block

```caddyfile
{
order argsort before header
}

# use argsort keyword in any server
:8881 {
header Content-Type "text/html; charset=utf-8"
respond "Hello."
argsort
}
```

### Optional case insensitive usage

Once the order has been set in the global options block, use `argsort lowecase` in any server block

```caddyfile
{
order argsort before header
}

:8881 {
header Content-Type "text/html; charset=utf-8"
respond "Hello."
argsort lowercase
}
```

### Forward the normalized request to an upstream

Once the order has been set in the global options block, you ensure query arguments sorting for an upstream server

```caddyfile
{
order argsort before header
}

# or ensure query arguments sorting then passing it to another server
# (useful when using other web server in the backend)
:8882 {
argsort
reverse_proxy localhost:8883
Expand Down
34 changes: 30 additions & 4 deletions argsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package argsort

import (
"net/http"
"net/url"
"strings"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
Expand All @@ -17,6 +19,7 @@ func init() {
// Middleware implements an HTTP handler that
// reorders the query arguments.
type Middleware struct {
Lowercase bool `json:"lowercase,omitempty"`
}

// CaddyModule returns the Caddy module information.
Expand All @@ -39,14 +42,37 @@ func (m *Middleware) Validate() error {

// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
// url.Values.Encode() will just do the sort for us
r.URL.RawQuery = r.URL.Query().Encode()
// url.Values.Encode() is doing the sort for us
if m.Lowercase {
values := url.Values{}
for k, s := range r.URL.Query() {
for _, v := range s {
values.Add(strings.ToLower(k), v)
}
}
r.URL.RawQuery = values.Encode()
} else {
r.URL.RawQuery = r.URL.Query().Encode()
}

return next.ServeHTTP(w, r)
}

// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d.Next() // consume directive name
// argsort [lower]
func (a *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
// Set default value for Lower
a.Lowercase = false

for d.Next() {
if d.NextArg() {
if d.Val() == "lowercase" {
a.Lowercase = true
} else {
return d.ArgErr()
}
}
}
return nil
}

Expand Down
10 changes: 10 additions & 0 deletions test/Caddyfile.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
debug
order argsort before rewrite
}

:2015 {
argsort
header Content-Type "text/html; charset=utf-8"
respond "{http.request.uri.query}"
}
10 changes: 10 additions & 0 deletions test/Caddyfile.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
debug
order argsort before rewrite
}

:2015 {
argsort lowercase
header Content-Type "text/html; charset=utf-8"
respond "{http.request.uri.query}"
}
6 changes: 5 additions & 1 deletion test/run-test
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ echobold() {

test() {
echobold "test case ${1}"
$XCADDY run --config test/Caddyfile.${1}&
$XCADDY run --adapter caddyfile --config test/Caddyfile.${1}&
pid="$!"
retry $(cat test/test-${1}.req) > output${1}
diff output${1} test/test-${1}.res
Expand All @@ -51,3 +51,7 @@ $XCADDY list-modules
test "1"

test "2"

test "3"

test "4"
2 changes: 1 addition & 1 deletion test/test-2.req
Original file line number Diff line number Diff line change
@@ -1 +1 @@
http://localhost:2015/baz?z=bar&a=foo
http://localhost:2015/baz?z=bar&m=baz&a=foo
2 changes: 1 addition & 1 deletion test/test-2.res
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a=foo&z=bar
a=foo&m=baz&z=bar
1 change: 1 addition & 0 deletions test/test-3.req
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost:2015/baz?Z=bar&M=baz&a=foo
1 change: 1 addition & 0 deletions test/test-3.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
M=baz&Z=bar&a=foo
1 change: 1 addition & 0 deletions test/test-4.req
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost:2015/baz?Z=bar&M=baz&a=foo
1 change: 1 addition & 0 deletions test/test-4.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=foo&m=baz&z=bar
Loading