-
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package search | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func MatchHTTPHeaders(op TokenType, expr Expression, headers http.Header) (bool, error) { | ||
if headers == nil { | ||
return false, nil | ||
} | ||
|
||
switch op { | ||
case TokOpEq: | ||
strLiteral, ok := expr.(StringLiteral) | ||
if !ok { | ||
return false, errors.New("search: expression must be a string literal") | ||
} | ||
|
||
// Return `true` if at least one header (<key>: <value>) is equal to the string literal. | ||
for key, values := range headers { | ||
for _, value := range values { | ||
if strLiteral.Value == fmt.Sprintf("%v: %v", key, value) { | ||
return true, nil | ||
} | ||
} | ||
} | ||
|
||
return false, nil | ||
case TokOpNotEq: | ||
strLiteral, ok := expr.(StringLiteral) | ||
if !ok { | ||
return false, errors.New("search: expression must be a string literal") | ||
} | ||
|
||
// Return `true` if none of the headers (<key>: <value>) are equal to the string literal. | ||
for key, values := range headers { | ||
for _, value := range values { | ||
if strLiteral.Value == fmt.Sprintf("%v: %v", key, value) { | ||
return false, nil | ||
} | ||
} | ||
} | ||
|
||
return true, nil | ||
case TokOpRe: | ||
re, ok := expr.(RegexpLiteral) | ||
if !ok { | ||
return false, errors.New("search: expression must be a regular expression") | ||
} | ||
|
||
// Return `true` if at least one header (<key>: <value>) matches the regular expression. | ||
for key, values := range headers { | ||
for _, value := range values { | ||
if re.MatchString(fmt.Sprintf("%v: %v", key, value)) { | ||
return true, nil | ||
} | ||
} | ||
} | ||
|
||
return false, nil | ||
case TokOpNotRe: | ||
re, ok := expr.(RegexpLiteral) | ||
if !ok { | ||
return false, errors.New("search: expression must be a regular expression") | ||
} | ||
|
||
// Return `true` if none of the headers (<key>: <value>) match the regular expression. | ||
for key, values := range headers { | ||
for _, value := range values { | ||
if re.MatchString(fmt.Sprintf("%v: %v", key, value)) { | ||
return false, nil | ||
} | ||
} | ||
} | ||
|
||
return true, nil | ||
default: | ||
return false, fmt.Errorf("search: unsupported operator %q", op.String()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters