// Check if the specified header matches the test string, applying the header match type
// specified.
323241
func HeaderMatch(hdr http.Header, nm string, matchType HeaderMatchType, test string) bool {
	// First get the header value
	val := hdr[http.CanonicalHeaderKey(nm)]
	if len(val) == 0 {
		return false
	}
	// Prepare the match test
	test = strings.ToLower(test)
	for _, v := range val {
		v = strings.Trim(strings.ToLower(v), " \n\t")
		switch matchType {
		case HmEquals:
			if v == test {
				return true
			}
		case HmStartsWith:
			if strings.HasPrefix(v, test) {
				return true
			}
		case HmEndsWith:
			if strings.HasSuffix(v, test) {
				return true
			}
		case HmContains:
			if strings.Contains(v, test) {
				return true
			}
		}
	}
	return false
}
1273355
func match(r *http.Request, sess *sessions.Session, refresh bool) bool {

	valid := false
	path := r.URL.Path

	if SingleToken {
		path = "/"
	}

	// If tokens exists
	if token, ok := sess.Values[TokenName]; ok {

		// Token submitted via POST
		sentToken := r.FormValue(TokenName)

		// Detect the content type
		switch r.Header.Get("Content-Type") {
		case "application/x-www-form-urlencoded":
			sentToken = r.FormValue(TokenName)
			break
		case "application/json":
			// Prevents throwing an error if nil
			b := bytes.NewBuffer(make([]byte, 0))
			body_reader := io.TeeReader(r.Body, b)
			if r.Body == nil {
				break
			}
			var t interface{}
			decoder := json.NewDecoder(body_reader)
			err := decoder.Decode(&t)

			// If the response is JSON
			if err == nil {
				vals := t.(map[string]interface{})
				// Update the token value
				sentToken = fmt.Sprintf("%v", vals[TokenName])
			}
			r.Body = ioutil.NopCloser(b)
			break
		}

		// If token is empty in the form, it is not valid
		if sentToken == "" {
			valid = false
		} else {
			// Check token against same page URL
			if sentToken == token.(StringMap)[path] {
				valid = true
			} else {
				// Extract the relative referer path
				offset := strings.Index(r.Referer(), r.Host) + len(r.Host)

				// Make sure no errors can be thrown
				if offset != 0 && offset < len(r.Referer()) {
					// Check token against previous page
					if sentToken == token.(StringMap)[r.Referer()[offset:]] {
						valid = true
					}
				}

			}
		}

		if refresh {
			delete(token.(StringMap), path)
		}
	}

	return valid
}
707046
func HTTP2HeaderFieldPrefix(name, valuePrefix string) Matcher {
	return func(r io.Reader) bool {
		return matchHTTP2Field(ioutil.Discard, r, name, func(gotValue string) bool {
			return strings.HasPrefix(gotValue, valuePrefix)
		})
	}
}
1338061
func (typ TokenType) MatchString() string {
	tokInfo, ok := TokenNameMap[typ]
	//u.Debugf("matchstring: '%v' '%v'  '%v'", tokInfo.T, tokInfo.Kw, tokInfo.Description)
	if ok {
		if tokInfo.HasSpaces {
			return tokInfo.firstWord
		}
		return tokInfo.Kw
	}
	return "not implemented"
}
1124254
func tokenHeaderString(name string, where *string, value ast.Value) (err error) {
	var ok bool
	var v ast.String
	if v, ok = value.(ast.String); !ok {
		err = fmt.Errorf("%s: must be a string", name)
		return
	}
	*where = string(v)
	return
}
1013833
func (self *simplePBundle) Header(s string) (string, bool) {
	v, ok := self.h[strings.ToLower(s)]
	return v, ok
}
523941
func (m unitMatcher) matchString(s string) bool {
	for _, pattern := range m.patterns {
		ok, err := path.Match(pattern, s)
		if err != nil {
			// We validate patterns, so should never get here.
			panic(fmt.Errorf("pattern syntax error in %q", pattern))
		} else if ok {
			return true
		}
	}
	return false
}
617457
func (r *Response) Header(header string) *String {
	value := ""
	if !r.chain.failed() {
		value = r.resp.Header.Get(header)
	}
	return &String{r.chain, value}
}
423641
func ParseHeader(s string) http.Header {
	h := http.Header{}
	lines := strings.Split(s, "\n")
	rx := regexp.MustCompile(`^([^\s+]+):\s*(.*)$`)
	for _, line := range lines {
		m := rx.FindStringSubmatch(line)
		if len(m) == 3 {
			key := strings.TrimSpace(m[1])
			val := strings.TrimSpace(m[2])
			if len(key) > 0 {
				h.Add(key, val)
			}
		}
	}
	return h
}
703117
func (ctx *Context) Type(str string) {
	ctx.Res.Set(HeaderContentType, str)
}
326466
func (r *ResponseMatcher) match(res *http.Response) (bool, int) {
	if r.method != "" {
		if r.method != res.Request.Method {
			return false, 0
		}
	}
	if r.contentType != "" {
		if r.contentType != getContentType(res.Header.Get("Content-Type")) {
			return false, 0
		}
	}
	if r.minStatus != 0 || r.maxStatus != 0 {
		if res.StatusCode < r.minStatus || res.StatusCode > r.maxStatus {
			return false, 0
		}
	}
	if r.scheme != "" {
		if res.Request.URL.Scheme != r.scheme {
			return false, 0
		}
	}
	if r.host != "" {
		if res.Request.URL.Host != r.host {
			return false, 0
		}
	}
	if r.predicate != nil {
		if !r.predicate(res) {
			return false, 0
		}
	}
	if r.path != "" {
		if strings.HasPrefix(res.Request.URL.Path, r.path) {
			return true, len(r.path)
		}
		return false, 0
	}
	return true, 0
}
66299
func (h Headers) Compare(other Headers) bool {
	if len(h) != len(other) {
		return false
	}
	for i, v := range h {
		if v != other[i] {
			return false
		}
	}
	return true
}
692250
func IsMatches(str, pattern string) bool {
	match, _ := regexp.MatchString(pattern, str)
	return match
}
1012509
func IsWildcardMatch(s string, p string) bool {
	dp := make([][]bool, len(p)+1)
	for i := range dp {
		dp[i] = make([]bool, len(s)+1)
		for j := range dp[i] {
			dp[i][j] = false
		}
	}
	dp[0][0] = true
	for j := 1; j <= len(p); j++ {
		pattern := p[j-1]
		dp[j][0] = dp[j-1][0] && pattern == '*'
		for i := 1; i <= len(s); i++ {
			letter := s[i-1]
			if pattern != '*' {
				dp[j][i] = dp[j-1][i-1] && (pattern == '?' || pattern == letter)
			} else {
				dp[j][i] = dp[j][i-1] || dp[j-1][i]
			}
		}
	}
	return dp[len(p)][len(s)]
}
881917
func checkHeaders(a, b jose.Header) error {
	if len(a)+len(b) == 0 {
		return ErrTwoEmptyHeaders
	}
	for key := range a {
		if b.Has(key) && !IgnoreDupes {
			return ErrDuplicateHeaderParameter
		}
	}
	return nil
}
108227
func (r *Request) MatchType(kind string) *Request {
	mime := BodyTypeAliases[kind]
	if mime != "" {
		kind = mime
	}
	r.Header.Set("Content-Type", kind)
	return r
}
146502
func (h *DBHeader) ValidateSha256(hash [32]byte) error {
	sha := h.GetSha256()
	if !reflect.DeepEqual(sha, hash) {
		return errors.New("Sha256 of header mismatching")
	}
	return nil
}