func sortQuery(u *url.URL) { q := u.Query() if len(q) == 0 { return } arKeys := make([]string, len(q)) i := 0 for k, _ := range q { arKeys[i] = k i++ } sort.Strings(arKeys) buf := new(bytes.Buffer) for _, k := range arKeys { sort.Strings(q[k]) for _, v := range q[k] { if buf.Len() > 0 { buf.WriteRune('&') } buf.WriteString(fmt.Sprintf("%s=%s", k, url.QueryEscape(v))) } } // Rebuild the raw query string u.RawQuery = buf.String() }
func SignAmazonUrl(origUrl *url.URL, api AmazonProductAPI) (signedUrl string, err error) { escapeUrl := strings.Replace(origUrl.RawQuery, ",", "%2C", -1) escapeUrl = strings.Replace(escapeUrl, ":", "%3A", -1) params := strings.Split(escapeUrl, "&") sort.Strings(params) sortedParams := strings.Join(params, "&") toSign := fmt.Sprintf("GET\n%s\n%s\n%s", origUrl.Host, origUrl.Path, sortedParams) hasher := hmac.New(sha256.New, []byte(api.SecretKey)) _, err = hasher.Write([]byte(toSign)) if err != nil { return "", err } hash := base64.StdEncoding.EncodeToString(hasher.Sum(nil)) hash = url.QueryEscape(hash) newParams := fmt.Sprintf("%s&Signature=%s", sortedParams, hash) origUrl.RawQuery = newParams return origUrl.String(), nil }
func returnlatlng(address string) interface{} { var Url *url.URL Url, err := url.Parse("http://maps.google.com") if err != nil { panic("Error Panic") } Url.Path += "/maps/api/geocode/json" //fmt.Println(address) parameters := url.Values{} parameters.Add("address", address) Url.RawQuery = parameters.Encode() Url.RawQuery += "&sensor=false" // fmt.Println("URL " + Url.String()) res, err := http.Get(Url.String()) // if err != nil { panic("Error Panic") } // fmt.Println(res) defer res.Body.Close() var v map[string]interface{} dec := json.NewDecoder(res.Body) if err := dec.Decode(&v); err != nil { fmt.Println("ERROR: " + err.Error()) } lat := v["results"].([]interface{})[0].(map[string]interface{})["geometry"].(map[string]interface{})["location"] //.(map[string]interface{})["lat"] //fmt.Println(lat) // lng := v["results"].([]interface{})[0].(map[string] interface{})["geometry"].(map[string] interface{})["location"].(map[string]interface{})["lng"] //fmt.Println("The latittude",lat,"and the longitude",lng) return lat }
func Test_buildRequestUrl(t *testing.T) { user, _ := user.Current() url1 := url.URL{Scheme: "http", Host: "localhost:8080", Path: "/webhdfs/v1/test"} q := url1.Query() q.Set("user.name", user.Username) url1.RawQuery = q.Encode() conf := Configuration{Addr: url1.Host} u, err := buildRequestUrl(conf, &Path{Name: "/test"}, nil) if err != nil { t.Fatal(err) } if url1 != *u { t.Errorf("Expecting url [%v], but got [%v]", url1.String(), u.String()) } // test with params v := url.Values{} v.Add("op1", "OP_1") v.Add("op2", "OP_2") v.Add("user.name", user.Username) url1.RawQuery = v.Encode() params := map[string]string{ "op1": "OP_1", "op2": "OP_2", } u, err = buildRequestUrl(conf, &Path{Name: "/test"}, ¶ms) if url1 != *u { t.Errorf("Expecting url [%v], but got [%v]", url1.String(), u.String()) } }
func (cam *Camera) url(action string, params *map[string]string) string { u := url.URL{} q := url.Values{} u.Scheme = "http" u.Host = cam.ipaddress u.Path = "/camera/" + action if params != nil { for key, value := range *params { q.Set(key, value) } } // GoPro expects parameters in a certain order, // the password always has to be the first. // So this won't work at the moment: // q.Set("t", cam.password) // Instead we prepend the password "manually": u.RawQuery = "t=" + cam.password if query := q.Encode(); len(query) > 0 { u.RawQuery += "&" + query } return u.String() }
func getLinkToNextPage( requestURL url.URL, offset int64, limit int64, total int64, ) LinkType { maxOffset := GetMaxOffset(total, limit) if offset+limit <= maxOffset { offset = offset + limit q := requestURL.Query() q.Set("offset", strconv.FormatInt(offset, 10)) requestURL.RawQuery = q.Encode() } else if offset == 0 { offset = 0 q := requestURL.Query() q.Del("offset") requestURL.RawQuery = q.Encode() } return LinkType{ Rel: "next", Href: requestURL.String(), Title: getPageNumberAsTitle(offset, limit), } }
// URL returns redirect url for application authentication func (a Auth) URL() string { u := url.URL{} u.Host = oauthHost u.Scheme = oauthScheme u.Path = oauthPath if len(a.RedirectURI) == 0 { a.RedirectURI = oauthRedirectURI } if len(a.ResponseType) == 0 { a.ResponseType = oauthResponseType } if len(a.Display) == 0 { a.Display = oauthDisplay } values := u.Query() values.Add(paramResponseType, a.ResponseType) values.Add(paramScope, a.Scope.String()) values.Add(paramAppID, int64s(a.ID)) values.Add(paramRedirectURI, a.RedirectURI) values.Add(paramVersion, defaultVersion) values.Add(paramDisplay, a.Display) u.RawQuery = values.Encode() return u.String() }
func GetSinglePricePrepped(name string, appid uint64) (*AssetPrice, error) { var Url *url.URL Url, err := url.Parse(baseUrl + "/market/priceoverview") if err != nil { panic(err) } parameters := url.Values{} parameters.Add("currency", Currency) parameters.Add("appid", fmt.Sprintf("%v", appid)) parameters.Add("market_hash_name", name) Url.RawQuery = parameters.Encode() client := http.Client{Timeout: time.Duration(60) * time.Second} req, _ := http.NewRequest("GET", Url.String(), nil) resp, err := client.Do(req) if err != nil { log.Println(err) return nil, err } defer resp.Body.Close() var jsonResp AssetPrice err = json.NewDecoder(resp.Body).Decode(&jsonResp) if err != nil { return nil, err } return &jsonResp, nil }
func apicall(u *neturl.URL) (*http.Response, error) { apikeys := []string{ "sehm98r8ss3fddjfs8rwxur435xjav94", "trtrn4tdhzeruuypctm55a8m5td9z4ce", "cc7qrdaxn9h47ds39nmgzb9hnutxfsrr", "cbygdtw624xmfn2hr2sw3ayaeekg9cvy", "7zsnd2q7npg5bch8hacae7t28xskn8vr", } // if val, ok := options["apikey"]; ok { // apikeys = append([]string{to.String(val)}, apikeys...) // } rawQuery := u.RawQuery if len(rawQuery) > 0 { rawQuery += "&apikey=" } else { rawQuery += "apikey=" } var resp *http.Response var apierr error for _, apikey := range apikeys { u.RawQuery = rawQuery + apikey resp, apierr = http.Get(u.String()) if apierr == nil { return resp, nil } } return nil, merry.Wrap(apierr) }
func rpmURL(cmd RpmCmd, cs RpmControls) string { var u url.URL u.Host = cmd.Collector u.Path = "agent_listener/invoke_raw_method" if cs.UseTLS { u.Scheme = "https" } else { u.Scheme = "http" } query := url.Values{} query.Set("marshal_format", "json") query.Set("protocol_version", procotolVersion) query.Set("method", cmd.Name) query.Set("license_key", cs.License) if len(cmd.RunID) > 0 { query.Set("run_id", cmd.RunID) } u.RawQuery = query.Encode() return u.String() }
func encodeToken(referal *url.URL, outhToken *oauth2.Token) error { str := outhToken.Extra("expires_in") expiresIn, err := strconv.Atoi(fmt.Sprintf("%v", str)) if err != nil { return errs.WithStack(errors.New("cant convert expires_in to integer " + err.Error())) } str = outhToken.Extra("refresh_expires_in") refreshExpiresIn, err := strconv.Atoi(fmt.Sprintf("%v", str)) if err != nil { return errs.WithStack(errors.New("cant convert refresh_expires_in to integer " + err.Error())) } tokenData := &app.TokenData{ AccessToken: &outhToken.AccessToken, RefreshToken: &outhToken.RefreshToken, TokenType: &outhToken.TokenType, ExpiresIn: &expiresIn, RefreshExpiresIn: &refreshExpiresIn, } b, err := json.Marshal(tokenData) if err != nil { return errs.WithStack(errors.New("cant marshal token data struct " + err.Error())) } parameters := url.Values{} parameters.Add("token", outhToken.AccessToken) // Temporary keep the old "token" param. We will drop this param as soon as UI adopt the new json param. parameters.Add("token_json", string(b)) referal.RawQuery = parameters.Encode() return nil }
func GetNameBySymbol(s string) (string, bool) { u := url.URL{Scheme: "http", Host: "hq.sinajs.cn", Path: "/"} q := u.Query() q.Set("list", s) u.RawQuery = q.Encode() log.Println(u.String()) res, err := http.Get(u.String()) if err != nil { log.Println(err.Error()) return "", false } body, err := ioutil.ReadAll(res.Body) defer res.Body.Close() if err != nil { log.Println(err.Error()) return "", false } // log.Println(GBKtoUTF8(string(body))) content := string(body) if strings.Contains(content, ",") != true { log.Println("Invalid symbol") return "", false } name := content[strings.Index(content, "\"")+1 : strings.Index(content, ",")] log.Println(GBKtoUTF8(name)) // log.Printf("length:%d\n", len(name)) return GBKtoUTF8(name), true }
// URL returns a suitable URL, such as for the Google Authenticator app // example: otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example func (otp *Totp) url() (string, error) { // verify the proper initialization if err := totpHasBeenInitialized(otp); err != nil { return "", err } secret := base32.StdEncoding.EncodeToString(otp.key) u := url.URL{} v := url.Values{} u.Scheme = "otpauth" u.Host = "totp" u.Path = otp.label() v.Add("secret", secret) v.Add("counter", fmt.Sprintf("%d", otp.getIntCounter())) v.Add("issuer", otp.issuer) v.Add("digits", strconv.Itoa(otp.digits)) v.Add("period", strconv.Itoa(otp.stepSize)) switch otp.hashFunction { case crypto.SHA256: v.Add("algorithm", "SHA256") break case crypto.SHA512: v.Add("algorithm", "SHA512") break default: v.Add("algorithm", "SHA1") break } u.RawQuery = v.Encode() return u.String(), nil }
// Download and parse OAuth2 JSON access request func downloadData(method string, u *url.URL, auth *BasicAuth, transport http.RoundTripper, output ResponseData) error { var postdata url.Values var body io.Reader var contenttype string if method == "POST" { // Convert query parameters to post data postdata = u.Query() u.RawQuery = "" body = strings.NewReader(postdata.Encode()) contenttype = "application/x-www-form-urlencoded" } // Create a new request preq, err := http.NewRequest(method, u.String(), body) if err != nil { return err } if auth != nil { preq.SetBasicAuth(auth.Username, auth.Password) } // Set content type for post request if contenttype != "" { preq.Header.Set("Content-Type", contenttype) } // Explicitly set accept header to JSON preq.Header.Set("Accept", "application/json") // do request client := &http.Client{} if transport != nil { client.Transport = transport } presp, err := client.Do(preq) if err != nil { return err } // must close body defer presp.Body.Close() // decode JSON and detect OAuth error jdec := json.NewDecoder(presp.Body) if err = jdec.Decode(&output); err == nil { if em, eok := output["error"]; eok { return NewError(fmt.Sprintf("%v", em), fmt.Sprintf("%v", output["error_description"]), fmt.Sprintf("%v", output["error_uri"]), fmt.Sprintf("%v", output["state"])) } } // If no OAuth error was detected, make sure we don't return success in an error case if err == nil && presp.StatusCode != 200 { return errors.New(fmt.Sprintf("Invalid status code (%d): %s", presp.StatusCode, presp.Status)) } return err }
func ForwardToErrorHandler(w http.ResponseWriter, r *http.Request, err error, errorHandlerURL url.URL) { q := errorHandlerURL.Query() q.Set("error", err.Error()) errorHandlerURL.RawQuery = q.Encode() http.Redirect(w, r, errorHandlerURL.String(), http.StatusFound) }
func TwitchLoginHandler(w http.ResponseWriter, r *http.Request) { token, err := controllerhelpers.GetToken(r) if err == http.ErrNoCookie { http.Error(w, "You are not logged in.", http.StatusUnauthorized) return } else if err != nil { http.Error(w, "Invalid jwt", http.StatusBadRequest) return } id := token.Claims.(*controllerhelpers.TF2StadiumClaims).PlayerID player, _ := player.GetPlayerByID(id) loginURL := url.URL{ Scheme: "https", Host: "api.twitch.tv", Path: "kraken/oauth2/authorize", } //twitchRedirectURL := config.Constants.PublicAddress + "/" + "twitchAuth" twitchRedirectURL, _ := url.Parse(config.Constants.PublicAddress) twitchRedirectURL.Path = "twitchAuth" values := loginURL.Query() values.Set("response_type", "code") values.Set("client_id", config.Constants.TwitchClientID) values.Set("redirect_uri", twitchRedirectURL.String()) values.Set("scope", "channel_check_subscription user_subscriptions channel_subscriptions user_read") values.Set("state", xsrftoken.Generate(config.Constants.CookieStoreSecret, player.SteamID, "GET")) loginURL.RawQuery = values.Encode() http.Redirect(w, r, loginURL.String(), http.StatusTemporaryRedirect) }
func (s *server) buildURL(method, id string, opts streamOpts) string { loc := url.URL{ Scheme: "http", Host: s.config.Addr, Path: fmt.Sprintf("/%s/%s", method, id), } if s.config.TLSConfig != nil { loc.Scheme = "https" } query := url.Values{} if opts.stdin { query.Add(urlParamStdin, "1") } if opts.stdout { query.Add(urlParamStdout, "1") } if opts.stderr { query.Add(urlParamStderr, "1") } if opts.tty { query.Add(urlParamTTY, "1") } for _, c := range opts.command { query.Add(urlParamCommand, c) } loc.RawQuery = query.Encode() return loc.String() }
//Req : base request for ipfs access func (fs *IPfsfs) Req(path string, arg string) (resp *http.Response, size int64, err error) { u := url.URL{} u.Scheme = "http" u.Host = ipfsHost u.Path = api + path if arg != "" { val := url.Values{} val.Set("arg", arg) val.Set("encoding", "json") u.RawQuery = val.Encode() } logger.Debug("URL : %s", u.String()) resp, err = http.Get(u.String()) if resp == nil { return nil, 0, err } size = resp.ContentLength if resp.StatusCode != 200 { return resp, 0, errors.New(resp.Status) } if err != nil { return resp, 0, err } return resp, size, err }
// Parses the querystring (q= parameter) and moves special tokens such as // "lang:c" from the querystring into separate arguments. func RewriteQuery(u url.URL) url.URL { // query is a copy which we will modify using Set() and use in the result query := u.Query() querystr := query.Get("q") queryWords := []string{} for _, word := range strings.Split(querystr, " ") { fmt.Printf("word = %v\n", word) lower := strings.ToLower(word) if strings.HasPrefix(lower, "filetype:") { query.Add("filetype", strings.ToLower(word[len("filetype:"):])) } else if strings.HasPrefix(lower, "-filetype:") { query.Add("nfiletype", strings.ToLower(word[len("-filetype:"):])) } else if strings.HasPrefix(lower, "package:") { query.Set("package", word[len("package:"):]) } else if strings.HasPrefix(lower, "pkg:") { query.Set("package", word[len("pkg:"):]) } else if strings.HasPrefix(lower, "-package:") { query.Add("npackage", word[len("-package:"):]) } else if strings.HasPrefix(lower, "-pkg:") { query.Add("npackage", word[len("-pkg:"):]) } else if strings.HasPrefix(lower, "path:") || strings.HasPrefix(lower, "file:") { query.Add("path", word[len("path:"):]) } else if strings.HasPrefix(lower, "-path:") || strings.HasPrefix(lower, "-file:") { query.Add("npath", word[len("-path:"):]) } else { queryWords = append(queryWords, word) } } query.Set("q", strings.Join(queryWords, " ")) u.RawQuery = query.Encode() return u }
// NewRequest creates a new HTTP request and applies necessary auth headers if // set. func (c *Client) NewRequest(params map[string]string, method string, u url.URL, body io.Reader) *http.Request { p := url.Values{} // Build up our request parameters for k, v := range params { p.Add(k, v) } // Add the params to our URL u.RawQuery = p.Encode() // Build the request, no point in checking for errors here as we're just // passing a string version of an url.URL struct and http.NewRequest returns // error only if can't process an url.ParseRequestURI(). req, _ := http.NewRequest(method, u.String(), body) if c.VCDAuthHeader != "" && c.VCDToken != "" { // Add the authorization header req.Header.Add(c.VCDAuthHeader, c.VCDToken) // Add the Accept header for VCD req.Header.Add("Accept", "application/*+xml;version=5.6") } return req }
func DownloadOneStockPerDay(symbol string, date string) error { u := url.URL{Scheme: "http", Host: "market.finance.sina.com.cn", Path: "/downxls.php"} q := u.Query() q.Set("date", date) q.Set("symbol", symbol) u.RawQuery = q.Encode() log.Println(u.String()) td := GetStockInOneDay(u.String()) if len(td) > 0 { var ops DBOps ops.Init() err := ops.Open() if err != nil { log.Println("open the database failed.") log.Fatal(err) } defer ops.Close() ops.CopyDataIntoTable(td) } else { log.Printf("%s:%s#No DATA\n", symbol, date) } return nil }
func BuildSwfPlayUrl(conf *Configure, fileType FileType, videoUrl, swfUrl string, timeOut time.Duration) (string, error) { fid := "" for _, p := range strings.Split(videoUrl, "&") { kv := strings.Split(p, "=") if len(kv) == 2 && kv[0] == "fid" { fid = kv[1] break } } if len(fid) == 0 { return "", errors.New("invalid vidoe url, fid not found!") } if len(swfUrl) == 0 { swfUrl = SwfUrl } playUrl := url.URL{} playUrl.Path = swfUrl params := url.Values{} params.Add("vk", videoUrl) if fileType == Private { playToken := CreatePlayToken(conf.AccessKey, conf.SecretKey, fid, timeOut) params.Add("tk", playToken) } playUrl.RawQuery = params.Encode() return playUrl.String(), nil }
func getRepos(URL *url.URL) (map[string]string, error) { URL.RawQuery = "format=JSON" resp, err := http.Get(URL.String()) if err != nil { return nil, err } defer resp.Body.Close() content, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } const xssTag = ")]}'\n" content = bytes.TrimPrefix(content, []byte(xssTag)) m := map[string]*Project{} if err := json.Unmarshal(content, &m); err != nil { return nil, err } result := map[string]string{} for k, v := range m { result[k] = v.CloneURL } return result, nil }
func removeQueryArgument(u *url.URL, arguments ...string) { query := u.Query() for _, argument := range arguments { query.Del(argument) } u.RawQuery = query.Encode() }
func Shorten(w http.ResponseWriter, r *http.Request) { u := url.URL{ Scheme: "https", Host: "www.googleapis.com", Path: "/urlshortener/v1/url", } if schedule.Conf.ShortURLKey != "" { u.RawQuery = "key=" + schedule.Conf.ShortURLKey } j, err := json.Marshal(struct { LongURL string `json:"longUrl"` }{ r.Referer(), }) if err != nil { serveError(w, err) return } req, err := http.Post(u.String(), "application/json", bytes.NewBuffer(j)) if err != nil { serveError(w, err) return } io.Copy(w, req.Body) req.Body.Close() }
func (c *chain) match(URL *url.URL) bool { path := strings.Split(URL.EscapedPath(), "/") lenPath := len(path) query := URL.Query() if c.lenPattern > lenPath { return false } if c.pattern[c.lenPattern-1] != "*" && c.lenPattern < lenPath { return false } for key, value := range c.pattern { if len(value) == 0 { if len(path[key]) == 0 { continue } return false } if value[0] == ':' { query.Add(value[1:], path[key]) continue } if value[0] != '*' && value != path[key] { return false } } URL.RawQuery = query.Encode() return true }
func (gw2 *GW2Api) fetchEndpoint(ver, tag string, params url.Values, result interface{}) (err error) { var endpoint *url.URL endpoint, _ = url.Parse("https://api.guildwars2.com") endpoint.Path += "/" + ver + "/" + tag if params != nil { endpoint.RawQuery = params.Encode() } var resp *http.Response if resp, err = gw2.client.Get(endpoint.String()); err != nil { return err } var data []byte if data, err = ioutil.ReadAll(resp.Body); err != nil { return err } defer resp.Body.Close() if err = json.Unmarshal(data, &result); err != nil { var gwerr APIError if err = json.Unmarshal(data, &gwerr); err != nil { return err } return fmt.Errorf("Endpoint returned error: %s", gwerr) } return }
// applyToURL merged the filter into a URL. func (gf *GameFilter) applyToURL(u *url.URL) { if gf == nil { return } values := u.Query() if len(gf.Name) > 0 { values.Set("name", gf.Name) } if len(gf.Abbreviation) > 0 { values.Set("abbreviation", gf.Abbreviation) } if gf.Released > 0 { values.Set("released", strconv.Itoa(gf.Released)) } if len(gf.Platform) > 0 { values.Set("platform", gf.Platform) } if len(gf.Region) > 0 { values.Set("region", gf.Region) } if len(gf.Moderator) > 0 { values.Set("moderator", gf.Moderator) } gf.Romhack.applyToQuery("romhack", &values) u.RawQuery = values.Encode() }
func Detect_language(q string) string { var Url *url.URL Url, err := url.Parse("https://www.googleapis.com") if err != nil { fmt.Println(err) } Url.Path += "/language/translate/v2/detect" parameters := url.Values{} parameters.Add("q", q) parameters.Add("key", Google_key()) Url.RawQuery = parameters.Encode() resp, err := http.Get(Url.String()) if err != nil { fmt.Println(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) var data gtresponse json.Unmarshal(body, &data) lang := data.Data.Detections[0][0].Language return lang }
func queryHTTPTracker(infoHashes []string, u *url.URL) (tr []InfoHashDetails, err error) { uq := u.Query() for _, infoHash := range infoHashes { uq.Add("info_hash", infoHash) } log.Println("HTTP INFO HASH DEBUG REMOVE THIS", uq) // Don't report IPv6 address, the user might prefer to keep // that information private when communicating with IPv4 hosts. if false { ipv6Address, err := findLocalIPV6AddressFor(u.Host) if err == nil { log.Println("our ipv6", ipv6Address) uq.Add("ipv6", ipv6Address) } } // This might reorder the existing query string in the Announce url // This might break some broken trackers that don't parse URLs properly. u.RawQuery = uq.Encode() tr, err = getTrackerInfo(u.String()) if tr == nil || err != nil { log.Println("Error: Could not fetch tracker info:", err) } return }