github.com/google/martian/v3@v3.3.3/martianurl/url_regex_matcher.go (about) 1 // Copyright 2017 Google Inc. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package martianurl 16 17 import ( 18 "net/http" 19 "net/url" 20 "regexp" 21 ) 22 23 // RegexMatcher is a conditional evaluator of request urls to be used in 24 // filters that take conditionals. 25 type RegexMatcher struct { 26 r *regexp.Regexp 27 } 28 29 // NewRegexMatcher builds a new url matcher from a compiled Regexp. 30 func NewRegexMatcher(r *regexp.Regexp) *RegexMatcher { 31 return &RegexMatcher{ 32 r: r, 33 } 34 } 35 36 // MatchRequest retuns true if the request URL matches r. 37 func (m *RegexMatcher) MatchRequest(req *http.Request) bool { 38 return m.matches(req.URL) 39 } 40 41 // MatchResponse retuns true if the response URL matches r. 42 func (m *RegexMatcher) MatchResponse(res *http.Response) bool { 43 return m.matches(res.Request.URL) 44 } 45 46 // matches checks if a url matches r. 47 func (m *RegexMatcher) matches(u *url.URL) bool { 48 return m.r.MatchString(u.String()) 49 }