// ParseRequestTokenStr parses the URL-encoded query string and returns
// a RequestToken.
178030
func ParseRequestTokenStr(str string) (*RequestToken, error) {
	//parse the request token from the body
	parts, err := url.ParseQuery(str)
	if err != nil {
		return nil, err
	}

	token := RequestToken{}
	token.token  = parts.Get("oauth_token")
	token.secret = parts.Get("oauth_token_secret")
	token.callbackConfirmed = parts.Get("oauth_callback_confirmed") == "true"

	//some error checking ...
	switch {
	case len(token.token) == 0  : return nil, errors.New(str)
	case len(token.secret) == 0 : return nil, errors.New(str)
	}

	return &token, nil
}
569532
func ParseJWTFromRequest(req *http.Request) (jwt.JWT, error) {
	if b, ok := fromHeader(req); ok {
		return ParseJWT(b)
	}
	if b, ok := fromForm(req); ok {
		return ParseJWT(b)
	}
	return nil, ErrNoTokenInRequest
}
396524
func ParseURL(urlStr string) (result *url.URL, err error) {
	result, err = url.Parse(urlStr)
	if result != nil && result.Scheme == "" {
		result = nil
		err = fmt.Errorf("invalid URL <%s>", urlStr)
	}
	return
}
1094200
func URLString(ustring string) RequestOption {
	return func(req *http.Request) (*http.Request, error) {
		u, err := url.Parse(ustring)
		if err != nil {
			return nil, err
		}

		req.URL = u
		return req, nil
	}
}
361996
func ParseRequestLine(line []byte) (r RequestLine, ok bool) {
	var i int
	for i = 0; i < len(line); i++ {
		c := line[i]
		if !OctetTypes[c].IsToken() {
			if i > 0 && c == ' ' {
				break
			}
			return
		}
	}
	if i == len(line) {
		return
	}

	var proto []byte
	r.Method = line[:i]
	r.URI, proto = split2(line[i+1:], ' ')
	if len(r.URI) == 0 {
		return
	}
	if major, minor, ok := ParseVersion(proto); ok {
		r.Version.Major = major
		r.Version.Minor = minor
		return r, true
	}

	return r, false
}
294419
func ParseRequestToken(response string) (*RequestToken, error) {
	val, err := url.ParseQuery(strings.TrimSpace(response))
	if err != nil {
		return nil, err
	}

	ret := &RequestToken{}

	oauth_problem := val.Get("oauth_problem")
	if oauth_problem != "" {
		ret.OAuthProblem = oauth_problem
		return ret, flickErr.NewError(flickErr.RequestTokenError, oauth_problem)
	}

	confirmed, _ := strconv.ParseBool(val.Get("oauth_callback_confirmed"))
	ret.OauthCallbackConfirmed = confirmed
	ret.OauthToken = val.Get("oauth_token")
	ret.OauthTokenSecret = val.Get("oauth_token_secret")

	return ret, nil
}
641400
func urlsMatchStr(glob string, target string) (bool, error) {
	globUrl, err := parseSchemelessUrl(glob)
	if err != nil {
		return false, err
	}
	targetUrl, err := parseSchemelessUrl(target)
	if err != nil {
		return false, err
	}
	return urlsMatch(globUrl, targetUrl)
}
1302183
func ParseOAuthToken(response string) (*OAuthToken, error) {
	val, err := url.ParseQuery(strings.TrimSpace(response))
	if err != nil {
		return nil, err
	}

	ret := &OAuthToken{}

	oauth_problem := val.Get("oauth_problem")
	if oauth_problem != "" {
		ret.OAuthProblem = oauth_problem
		return ret, flickErr.NewError(flickErr.OAuthTokenError, oauth_problem)
	}

	ret.OAuthToken = val.Get("oauth_token")
	ret.OAuthTokenSecret = val.Get("oauth_token_secret")
	ret.Fullname = val.Get("fullname")
	ret.UserNsid = val.Get("user_nsid")
	ret.Username = val.Get("username")

	return ret, nil
}
357261
func parseEnvURLStr(envURL string) (*url.URL, string, string, *probe.Error) {
	var envURLStr string
	u, accessKey, secretKey, err := parseEnvURL(envURL)
	if err != nil {
		// url parsing can fail when accessKey/secretKey contains non url encoded values
		// such as #. Strip accessKey/secretKey from envURL and parse again.
		re := regexp.MustCompile("^(https?://)(.*?):(.*?)@(.*?)$")
		res := re.FindAllStringSubmatch(envURL, -1)
		// regex will return full match, scheme, accessKey, secretKey and endpoint:port as
		// captured groups.
		if res == nil || len(res[0]) != 5 {
			return nil, "", "", err
		}
		for k, v := range res[0] {
			if k == 2 {
				accessKey = fmt.Sprintf("%s", v)
			}
			if k == 3 {
				secretKey = fmt.Sprintf("%s", v)
			}
			if k == 1 || k == 4 {
				envURLStr = fmt.Sprintf("%s%s", envURLStr, v)
			}
		}
		u, _, _, err = parseEnvURL(envURLStr)
		if err != nil {
			return nil, "", "", err
		}
	}
	// Check if username:password is provided in URL, with no
	// access keys or secret we proceed and perform anonymous
	// requests.
	if u.User != nil {
		accessKey = u.User.Username()
		secretKey, _ = u.User.Password()
	}
	return u, accessKey, secretKey, nil
}
446964
func GetAuthnRequestURL(baseURL string, b64XML string, state string) (string, error) {
	u, err := url.Parse(baseURL)
	if err != nil {
		return "", err
	}

	q := u.Query()
	q.Add("SAMLRequest", b64XML)
	q.Add("RelayState", state)
	u.RawQuery = q.Encode()
	return u.String(), nil
}
1106447
func ParseQueryInt(param string, req *http.Request, params imageserver.Params) error {
	s := req.URL.Query().Get(param)
	if s == "" {
		return nil
	}
	i, err := strconv.Atoi(s)
	if err != nil {
		return newParseTypeParamError(param, "int", err)
	}
	params.Set(param, i)
	return nil
}
386760
func ParseFilterString(ctx context.Context, rawSearchString string) (criteria.Expression, *QueryOptions, error) {
	fm := map[string]interface{}{}
	// Parsing/Unmarshalling JSON encoding/json
	err := json.Unmarshal([]byte(rawSearchString), &fm)

	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"err":             err,
			"rawSearchString": rawSearchString,
		}, "failed to unmarshal raw search string")
		return nil, nil, errors.NewBadParameterError("expression", rawSearchString+": "+err.Error())
	}
	q := Query{}
	parseMap(fm, &q)

	q.Options = parseOptions(fm)

	exp, err := q.generateExpression()
	return exp, q.Options, err
}
624461
func ParseRequestHeader(header string) (*Auth, error) {
	auth := &Auth{ActualTimestamp: Now()}
	err := auth.ParseHeader(header, AuthHeader)
	if err != nil {
		return nil, err
	}

	if auth.Credentials.ID == "" {
		return nil, AuthFormatError{"id", "missing or empty"}
	}
	if auth.Timestamp.IsZero() {
		return nil, AuthFormatError{"ts", "missing, empty, or zero"}
	}
	if auth.Nonce == "" {
		return nil, AuthFormatError{"nonce", "missing or empty"}
	}
	auth.ReqHash = true

	return auth, nil
}
648560
func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
	query := req.URL.Query()
	if wait := query.Get("wait"); wait != "" {
		dur, err := time.ParseDuration(wait)
		if err != nil {
			resp.WriteHeader(http.StatusBadRequest)
			fmt.Fprint(resp, "Invalid wait time")
			return true
		}
		b.MaxQueryTime = dur
	}
	if idx := query.Get("index"); idx != "" {
		index, err := strconv.ParseUint(idx, 10, 64)
		if err != nil {
			resp.WriteHeader(http.StatusBadRequest)
			fmt.Fprint(resp, "Invalid index")
			return true
		}
		b.MinQueryIndex = index
	}
	return false
}
657851
func (ts TokenSource) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
	token, err := ts.Token()
	if err != nil {
		return nil, err
	}
	return map[string]string{
		"authorization": token.Type() + " " + token.AccessToken,
	}, nil
}
1124709
func ParseRequest(contentType string, body io.Reader) (*Request, error) {
	if err := checkContentType(contentType); err != nil {
		return nil, err
	}

	r := &Request{}
	if err := parseReqOrResp(body, r); err != nil {
		return nil, err
	}

	return r, nil
}
1263736
func parseEnvURL(envURL string) (*url.URL, string, string, *probe.Error) {
	u, e := url.Parse(envURL)
	if e != nil {
		return nil, "", "", probe.NewError(e).Trace(envURL)
	}

	var accessKey, secretKey string
	// Check if username:password is provided in URL, with no
	// access keys or secret we proceed and perform anonymous
	// requests.
	if u.User != nil {
		accessKey = u.User.Username()
		secretKey, _ = u.User.Password()
	}

	// Look for if URL has invalid values and return error.
	if !((u.Scheme == "http" || u.Scheme == "https") &&
		(u.Path == "/" || u.Path == "") && u.Opaque == "" &&
		u.ForceQuery == false && u.RawQuery == "" && u.Fragment == "") {
		return nil, "", "", errInvalidArgument().Trace(u.String())
	}

	// Now that we have validated the URL to be in expected style.
	u.User = nil

	return u, accessKey, secretKey, nil
}