// checkSender returns an error if the sender is not well-formed and
// initializes a zeroed http.Client if one has not been provided.
494635
func checkSender(sender *Sender) error {
	if sender.ApiKey == "" {
		return errors.New("the sender's API key must not be empty")
	}
	if sender.Http == nil {
		sender.Http = new(http.Client)
	}
	return nil
}
125578
func checkHeader(expected string, headers http.Header) bool {
	headerBits := strings.Split(expected, ":")
	if len(headerBits) != 2 {
		lumber.Trace("Failed to check header - bad format")
		return false
	}

	// check if it matches
	return headers.Get(headerBits[0]) == headerBits[1]
}
1296876
func HTTPChecker(r string, statusCode int, timeout time.Duration, headers http.Header) health.Checker {
	return health.CheckFunc(func() error {
		client := http.Client{
			Timeout: timeout,
		}
		req, err := http.NewRequest("HEAD", r, nil)
		if err != nil {
			return errors.New("error creating request: " + r)
		}
		for headerName, headerValues := range headers {
			for _, headerValue := range headerValues {
				req.Header.Add(headerName, headerValue)
			}
		}
		response, err := client.Do(req)
		if err != nil {
			return errors.New("error while checking: " + r)
		}
		if response.StatusCode != statusCode {
			return errors.New("downstream service returned unexpected status: " + strconv.Itoa(response.StatusCode))
		}
		return nil
	})
}
559202
func checkErr(err error) (ok bool) {
	switch err {
	case nil:
		return true
	case redis.Nil:
		return false
	default:
		log.Panicf("rmq redis error is not nil %s", err)
		return false
	}
}
1351089
func CheckDriver(driver string) Check {
	return func() error {
		cli, err := newClient()
		if err != nil {
			return fmt.Errorf("creating client: %v", err)
		}
		info, err := cli.Info()
		if err != nil {
			return fmt.Errorf("getting info: %v", err)
		}
		if info.Driver != driver {
			return fmt.Errorf("driver is '%s' not 'driver'", info.Driver, driver)
		}
		return nil
	}
}
570583
func NewSender(cf ClientFunc, ro ...httpcli.RequestOpt) calls.Sender {
	return calls.SenderFunc(func(ctx context.Context, r calls.Request) (mesos.Response, error) {
		var (
			obj     = r.Call()
			rc, err = DefaultResponseClassifier(obj)
		)
		if err != nil {
			return nil, err
		}

		var req client.Request

		switch r := r.(type) {
		case calls.RequestStreaming:
			req = calls.Push(r, obj)
		default:
			req = calls.NonStreaming(obj)
		}

		return cf(req, rc, append(ro, httpcli.Context(ctx))...)
	})
}
809300
func NewSender(o SenderOptions) (s *Sender) {
	s = &Sender{
		client:     o.Client,
		retryFunc:  o.RetryFunc,
		retryMax:   o.RetryMax,
		retrySleep: o.RetrySleep,
	}
	if s.client == nil {
		s.client = &http.Client{}
	}
	if s.retryFunc == nil {
		s.retryFunc = defaultRetryFunc
	}
	return
}
533190
func (e *Elector) sendErr(err error) {
	e.sendEvent(Event{Err: err})
}
885851
func (h *handler) startSender(e proton.Event) {
	q := h.queues.Get(e.Link().RemoteSource().Address())
	s := &sender{
		link:   makeLink(e.Link(), q, h),
		credit: make(chan struct{}, 1), // Capacity of 1 for signalling.
	}
	h.senders[e.Link()] = s
	go s.run()
}
411277
func (hc *HTTPChecker) Check(timeout time.Duration) *Result {
	msg := fmt.Sprintf("HTTP %s to %s", hc.Method, hc.addr())
	start := time.Now()
	if timeout == time.Duration(0) {
		timeout = defaultHTTPTimeout
	}
	deadline := start.Add(timeout)

	u, err := url.Parse(hc.Request)
	if err != nil {
		return complete(start, "", false, err)
	}
	if hc.Secure {
		u.Scheme = "https"
	} else {
		u.Scheme = "http"
	}
	if u.Host == "" {
		u.Host = hc.IP.String()
	}

	proxy := (func(*http.Request) (*url.URL, error))(nil)
	if hc.Proxy {
		proxy = http.ProxyURL(u)
	}

	conn, err := dialTCP(hc.network(), hc.addr(), timeout, hc.Mark)
	if err != nil {
		return complete(start, "", false, err)
	}
	defer conn.Close()

	dialer := func(net string, addr string) (net.Conn, error) {
		return conn, nil
	}
	tlsConfig := &tls.Config{
		InsecureSkipVerify: !hc.TLSVerify,
	}
	client := &http.Client{
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			return errors.New("redirect not permitted")
		},
		Transport: &http.Transport{
			Dial:            dialer,
			Proxy:           proxy,
			TLSClientConfig: tlsConfig,
		},
	}
	req, err := http.NewRequest(hc.Method, hc.Request, nil)
	req.URL = u

	// If we received a response we want to process it, even in the
	// presence of an error - a redirect 3xx will result in both the
	// response and an error being returned.
	conn.SetDeadline(deadline)
	resp, err := client.Do(req)
	if resp == nil {
		return complete(start, "", false, err)
	}
	if resp.Body != nil {
		defer resp.Body.Close()
	}
	err = nil

	// Check response code.
	var codeOk bool
	if hc.ResponseCode == 0 {
		codeOk = true
	} else if resp.StatusCode == hc.ResponseCode {
		codeOk = true
	}

	// Check response body.
	var bodyOk bool
	msg = fmt.Sprintf("%s; got %s", msg, resp.Status)
	if hc.Response == "" {
		bodyOk = true
	} else if resp.Body != nil {
		buf := make([]byte, len(hc.Response))
		n, err := io.ReadFull(resp.Body, buf)
		if err != nil && err != io.ErrUnexpectedEOF {
			msg = fmt.Sprintf("%s; failed to read HTTP response", msg)
		} else if string(buf) != hc.Response {
			msg = fmt.Sprintf("%s; unexpected response - %q", msg, string(buf[0:n]))
		} else {
			bodyOk = true
		}
	}

	return complete(start, msg, codeOk && bodyOk, err)
}
259259
func (c *sendOptions) Check() error {
	if strings.TrimSpace(c.Recipient) == "" {
		return ErrRecipientMissing
	}
	if strings.TrimSpace(c.Amount) == "" {
		return ErrAmountMissing
	}
	namt, err := stellarnet.ParseStellarAmount(c.Amount)
	if err != nil {
		return ErrInvalidAmount
	}
	if namt < 0 {
		return ErrInvalidAmount
	}

	if c.FromAccountID != "" {
		_, err := strkey.Decode(strkey.VersionByteAccountID, c.FromAccountID)
		if err != nil {
			return ErrInvalidAccountID
		}
	}
	if len(c.Message) > libkb.MaxStellarPaymentNoteLength {
		return ErrMessageTooLong
	}

	return nil
}
810515
func checkErr(err error, handleErr func(string, int)) {
	// unwrap aggregates of 1
	if agg, ok := err.(utilerrors.Aggregate); ok && len(agg.Errors()) == 1 {
		err = agg.Errors()[0]
	}

	if err == nil {
		return
	}

	switch {
	case err == ErrExit:
		handleErr("", DefaultErrorExitCode)
	case kerrors.IsInvalid(err):
		details := err.(*kerrors.StatusError).Status().Details
		s := fmt.Sprintf("The %s %q is invalid", details.Kind, details.Name)
		if len(details.Kind) == 0 && len(details.Name) == 0 {
			s = "The request is invalid"
		}
		if len(details.Causes) > 0 {
			errs := statusCausesToAggrError(details.Causes)
			handleErr(MultilineError(s+": ", errs), DefaultErrorExitCode)
		} else {
			handleErr(s, DefaultErrorExitCode)
		}
	case clientcmd.IsConfigurationInvalid(err):
		handleErr(MultilineError("Error in configuration: ", err), DefaultErrorExitCode)
	default:
		switch err := err.(type) {
		case *meta.NoResourceMatchError:
			switch {
			case len(err.PartialResource.Group) > 0 && len(err.PartialResource.Version) > 0:
				handleErr(fmt.Sprintf("the server doesn't have a resource type %q in group %q and version %q", err.PartialResource.Resource, err.PartialResource.Group, err.PartialResource.Version), DefaultErrorExitCode)
			case len(err.PartialResource.Group) > 0:
				handleErr(fmt.Sprintf("the server doesn't have a resource type %q in group %q", err.PartialResource.Resource, err.PartialResource.Group), DefaultErrorExitCode)
			case len(err.PartialResource.Version) > 0:
				handleErr(fmt.Sprintf("the server doesn't have a resource type %q in version %q", err.PartialResource.Resource, err.PartialResource.Version), DefaultErrorExitCode)
			default:
				handleErr(fmt.Sprintf("the server doesn't have a resource type %q", err.PartialResource.Resource), DefaultErrorExitCode)
			}
		case utilerrors.Aggregate:
			handleErr(MultipleErrors(``, err.Errors()), DefaultErrorExitCode)
		case utilexec.ExitError:
			handleErr(err.Error(), err.ExitStatus())
		default: // for any other error type
			msg, ok := StandardErrorMessage(err)
			if !ok {
				msg = err.Error()
				if !strings.HasPrefix(msg, "error: ") {
					msg = fmt.Sprintf("error: %s", msg)
				}
			}
			handleErr(msg, DefaultErrorExitCode)
		}
	}
}
510643
func checkHeaders(h Headers, p string) error {
	if h == nil {
		return EHDRNIL
	}
	// Length check
	if e := h.Validate(); e != nil {
		return e
	}
	// Empty key / value check
	for i := 0; i < len(h); i += 2 {
		if h[i] == "" {
			return EHDRMTK
		}
		if p == SPL_10 && h[i+1] == "" {
			return EHDRMTV
		}
	}
	// UTF8 check
	if p != SPL_10 {
		_, e := h.ValidateUTF8()
		if e != nil {
			return e
		}
	}
	return nil
}
1257436
func CheckURL(s string) (string, error) {
	u, err := url.Parse(s)
	if err != nil {
		return "", err
	}
	if u.Scheme == "" {
		s = "https://" + s
		u.Scheme = "https"
	}
	if u.Scheme != "https" && u.Scheme != "http" {
		return "", errors.New("Only http:// or https:// scheme is accepted.")
	}
	if u.Scheme != "https" && hostRequiresSSL(u.Host) {
		return "", errors.New("only https:// scheme is accepted for appspot hosts, it can be omitted")
	}
	if _, err = url.Parse(s); err != nil {
		return "", err
	}
	return s, nil
}
665120
func NewSimpleSender(addr string) (Sender, error) {
	c, err := net.ListenPacket("udp", ":0")
	if err != nil {
		return nil, err
	}

	ra, err := net.ResolveUDPAddr("udp", addr)
	if err != nil {
		c.Close()
		return nil, err
	}

	sender := &SimpleSender{
		c:  c,
		ra: ra,
	}

	return sender, nil
}
913551
func (a *Agent) agentHTTPCheck(server bool) *structs.ServiceCheck {
	// Resolve the http check address
	httpCheckAddr := a.config.normalizedAddrs.HTTP
	if *a.config.Consul.ChecksUseAdvertise {
		httpCheckAddr = a.config.AdvertiseAddrs.HTTP
	}
	check := structs.ServiceCheck{
		Name:      a.config.Consul.ClientHTTPCheckName,
		Type:      "http",
		Path:      "/v1/agent/health?type=client",
		Protocol:  "http",
		Interval:  agentHttpCheckInterval,
		Timeout:   agentHttpCheckTimeout,
		PortLabel: httpCheckAddr,
	}
	// Switch to endpoint that doesn't require a leader for servers
	if server {
		check.Name = a.config.Consul.ServerHTTPCheckName
		check.Path = "/v1/agent/health?type=server"
	}
	if !a.config.TLSConfig.EnableHTTP {
		// No HTTPS, return a plain http check
		return &check
	}
	if a.config.TLSConfig.VerifyHTTPSClient {
		a.logger.Warn("not registering Nomad HTTPS Health Check because verify_https_client enabled")
		return nil
	}

	// HTTPS enabled; skip verification
	check.Protocol = "https"
	check.TLSSkipVerify = true
	return &check
}
476923
func (e *Email) parseSender() (string, error) {
	if e.Sender != "" {
		sender, err := mail.ParseAddress(e.Sender)
		if err != nil {
			return "", err
		}
		return sender.Address, nil
	} else {
		from, err := mail.ParseAddress(e.From)
		if err != nil {
			return "", err
		}
		return from.Address, nil
	}
}