func NewFromURL(rawURL string) (client Client, err error) {
	var (
		u           *url.URL
		host        string
		appID       string
		key         string
		secret      string
		secretGiven bool
		secure      bool
	)

	if u, err = url.Parse(rawURL); err != nil {
		return
	}

	host = u.Host

	matches := pusherPathRegex.FindStringSubmatch(u.Path)
	if len(matches) == 0 {
		err = errors.New("No app ID found")
		return
	}
	appID = matches[1]

	if u.User == nil {
		err = errors.New("Missing <key>:<secret>")
		return
	}
	key = u.User.Username()

	if secret, secretGiven = u.User.Password(); !secretGiven {
		err = errors.New("Missing <secret>")
		return
	}

	secure = u.Scheme == "https"

	client = NewWithOptions(appID, key, secret, Options{
		Secure: secure,
		Host:   host,
	})
	return
}
Esempio n. 2
0
func requestURL(p *Pusher, request *requests.Request, params *requests.Params) (u *url.URL, err error) {
	values := params.URLValues(p.key)

	var path string
	if params.Channel != "" {
		path = fmt.Sprintf(request.PathPattern, p.appID, params.Channel)
	} else {
		path = fmt.Sprintf(request.PathPattern, p.appID)
	}

	var urlUnescaped string
	encodedURLValues := values.Encode()
	if urlUnescaped, err = url.QueryUnescape(encodedURLValues); err != nil {
		err = errors.New(fmt.Sprintf("%s could not be unescaped - %v", encodedURLValues, err))
		return
	}

	unsigned := s.Join([]string{request.Method, path, urlUnescaped}, "\n")
	signed := signatures.HMAC(unsigned, p.secret)
	values.Add("auth_signature", signed)

	host := "api.pusherapp.com"
	scheme := "http"

	if p.Host != "" {
		host = p.Host
	}

	if p.Cluster != "" {
		host = fmt.Sprintf("api-%s.pusher.com", p.Cluster)
	}

	if p.Secure {
		scheme = "https"
	}

	u = &url.URL{
		Scheme:   scheme,
		Host:     host,
		Path:     path,
		RawQuery: values.Encode(),
	}

	return
}
Esempio n. 3
0
func Channels(channels []string) (err error) {
	channelErrors := []string{}
	for _, channel := range channels {
		if len(channel) > 200 {
			channelErrors = append(channelErrors, channelTooLong(channel))
			continue
		}

		if !channelValidationRegex.MatchString(channel) {
			channelErrors = append(channelErrors, channelHasIllegalCharacters(channel))
			continue
		}
	}

	if len(channelErrors) > 0 {
		message := s.Join(channelErrors, ". ")
		err = errors.New(message)
	}

	return
}