mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-19 07:43:54 +01:00
feat(rules): add protocol matching functionality
- Introduced a new checker for HTTP protocols (http, https, h3) in the routing rules. - Added corresponding test cases to validate protocol matching behavior in requests.
This commit is contained in:
@@ -31,6 +31,7 @@ const (
|
||||
OnCookie = "cookie"
|
||||
OnForm = "form"
|
||||
OnPostForm = "postform"
|
||||
OnProto = "proto"
|
||||
OnMethod = "method"
|
||||
OnHost = "host"
|
||||
OnPath = "path"
|
||||
@@ -226,6 +227,41 @@ var checkers = map[string]struct {
|
||||
}
|
||||
},
|
||||
},
|
||||
OnProto: {
|
||||
help: Help{
|
||||
command: OnProto,
|
||||
args: map[string]string{
|
||||
"proto": "the http protocol (http, https, h3)",
|
||||
},
|
||||
},
|
||||
validate: func(args []string) (any, gperr.Error) {
|
||||
if len(args) != 1 {
|
||||
return nil, ErrExpectOneArg
|
||||
}
|
||||
proto := args[0]
|
||||
if proto != "http" && proto != "https" && proto != "h3" {
|
||||
return nil, ErrInvalidArguments.Withf("proto: %q", proto)
|
||||
}
|
||||
return proto, nil
|
||||
},
|
||||
builder: func(args any) CheckFunc {
|
||||
proto := args.(string)
|
||||
switch proto {
|
||||
case "http":
|
||||
return func(w http.ResponseWriter, r *http.Request) bool {
|
||||
return r.TLS == nil
|
||||
}
|
||||
case "https":
|
||||
return func(w http.ResponseWriter, r *http.Request) bool {
|
||||
return r.TLS != nil
|
||||
}
|
||||
default: // h3
|
||||
return func(w http.ResponseWriter, r *http.Request) bool {
|
||||
return r.TLS != nil && r.ProtoMajor == 3
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
OnMethod: {
|
||||
help: Help{
|
||||
command: OnMethod,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package rules_test
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -65,6 +66,30 @@ func genCorrectnessTestCases(field string, genRequest func(k, v string) *http.Re
|
||||
|
||||
func TestOnCorrectness(t *testing.T) {
|
||||
tests := []testCorrectness{
|
||||
{
|
||||
name: "proto_match_http",
|
||||
checker: "proto http",
|
||||
input: &http.Request{TLS: nil},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "proto_match_https",
|
||||
checker: "proto https",
|
||||
input: &http.Request{TLS: &tls.ConnectionState{}},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "proto_match_h3",
|
||||
checker: "proto h3",
|
||||
input: &http.Request{TLS: &tls.ConnectionState{}, ProtoMajor: 3},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "proto_no_match_h3",
|
||||
checker: "proto h3",
|
||||
input: &http.Request{TLS: &tls.ConnectionState{}, ProtoMajor: 2},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "method_match",
|
||||
checker: "method GET",
|
||||
|
||||
Reference in New Issue
Block a user