// writeCookies writes the wire representation of the cookies // to w. Each cookie is written on a separate "Cookie: " line. // This choice is made because HTTP parsers tend to have a limit on // line-length, so it seems safer to place cookies on separate lines. func writeCookies(w io.Writer, kk []*http.Cookie) os.Error { lines := make([]string, 0, len(kk)) var b bytes.Buffer for _, c := range kk { b.Reset() n := c.Name // TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(n), c.Value) if len(c.Path) > 0 { fmt.Fprintf(&b, "; $Path=%s", http.URLEscape(c.Path)) } if len(c.Domain) > 0 { fmt.Fprintf(&b, "; $Domain=%s", http.URLEscape(c.Domain)) } if c.HttpOnly { fmt.Fprintf(&b, "; $HttpOnly") } lines = append(lines, "Cookie: "+b.String()+"\r\n") } sort.Strings(lines) for _, l := range lines { if _, err := io.WriteString(w, l); err != nil { return err } } return nil }
func (t *Tracker) Request(uploaded int, downloaded int, left int, status string) (peers *list.List, err os.Error) { err = nil url := fmt.Sprint(t.url, "?", "info_hash=", http.URLEscape(t.infoHash), "&peer_id=", http.URLEscape(t.peerId), "&port=", http.URLEscape(t.port), "&uploaded=", strconv.Itoa(uploaded), "&downloaded=", strconv.Itoa(downloaded), "&left=", strconv.Itoa(left), "&status=", http.URLEscape(status)) println(url) response, _, err := http.Get(url) if err != nil { return } if response.StatusCode != http.StatusOK { err = os.NewError("http error") } buf := new(bencode.BeString) resReader := bufio.NewReader(response.Body) be, err := buf.Decode(resReader) if err != nil { return } if be.Betype != bencode.Bedict { err = os.NewError("unexpected response from tracker") return } if failure, ok := be.Bedict["failure reason"]; !ok { err = os.NewError("unexpected response from tracker") return } else { print(failure) return } if interval, ok := be.Bedict["interval"]; !ok { err = os.NewError("unexpected response from tracker") return } else { t.interval, err = strconv.Atoi(interval.Bestr) } if _, ok := be.Bedict["peers"]; !ok { err = os.NewError("unexpected response from tracker") return } list := list.New() for i := range be.Bedict["peers"].Belist.Iter() { if i.(*bencode.BeNode).Betype == bencode.Bedict { list.PushFront(i.(*bencode.BeNode).Bedict) } else { err = os.NewError("unexpected response from tracker") return } } peers = list return }
func Urlencode(data map[string]string) string { var buf bytes.Buffer for k, v := range data { buf.WriteString(http.URLEscape(k)) buf.WriteByte('=') buf.WriteString(http.URLEscape(v)) buf.WriteByte('&') } s := buf.String() return s[0 : len(s)-1] }
func toQuery(params map[string]string) string { buf := bytes.NewBuffer(nil) flag := false for key, val := range params { if flag { buf.WriteString("&" + key + "=" + http.URLEscape(val)) } else { flag = true buf.WriteString(key + "=" + http.URLEscape(val)) } } return buf.String() }
func remoteSearch(query string) (res *http.Response, err os.Error) { search := "/search?f=text&q=" + http.URLEscape(query) // list of addresses to try var addrs []string if *serverAddr != "" { // explicit server address - only try this one addrs = []string{*serverAddr} } else { addrs = []string{ defaultAddr, "golang.org", } } // remote search for _, addr := range addrs { url := "http://" + addr + search res, err = http.Get(url) if err == nil && res.StatusCode == http.StatusOK { break } } if err == nil && res.StatusCode != http.StatusOK { err = os.NewError(res.Status) } return }
func escape(str string) string { str = http.URLEscape(str) str = strings.Replace(str, ":", "%3A", -1) str = strings.Replace(str, "/", "%2F", -1) return str }
func tf(c *http.Conn, b string, e string, n int, y int, s string) int { var qs string var ntf int = 0 if len(s) < 1 { return ntf } if s[0] == '#' && len(s) > 1 { qs = s } else { qs = "from:" + s } r, _, err := http.Get(fmt.Sprintf(queryURI, qformat, http.URLEscape(qs), b, e, n)) if err == nil { if r.StatusCode == http.StatusOK { ntf = readjson(c, r.Body, b, e, y) } else { fmt.Printf("Twitter is unable to search for %s (%s)\n", s, r.Status) } r.Body.Close() } else { fmt.Printf("%v\n", err) } return ntf }
func (c *Client) StatusesUpdate(status string, replyId uint64) (err os.Error) { var params string if status == "" { return os.NewError("must need \"status\" parameter.") } params = addParam(params, "status", http.URLEscape(status)) if replyId != 0 { params = addParam(params, "in_reply_to_status_id", fmt.Sprintf("%d", replyId)) } url := c.makeAuthURL(statusesUpdate, params) res, err := http.Post(url, "", bytes.NewBufferString("")) if err != nil { return err } if res.Status != "200 OK" { return os.NewError("Request failed") } return nil }
func TestSuccessfulTokenRequest(t *testing.T) { c := basicConsumer() m := newMocks(t) m.install(c) m.httpClient.ExpectGet( "http://www.mrjon.es/requesttoken", map[string]string{ "oauth_callback": http.URLEscape("http://www.mrjon.es/callback"), "oauth_consumer_key": "consumerkey", "oauth_nonce": "2", "oauth_signature": "MOCK_SIGNATURE", "oauth_signature_method": "HMAC-SHA1", "oauth_timestamp": "1", "oauth_version": "1.0", }, "oauth_token=TOKEN&oauth_token_secret=SECRET") token, url, err := c.GetRequestTokenAndUrl("http://www.mrjon.es/callback") if err != nil { t.Fatal(err) } assertEq(t, "TOKEN", token.Token) assertEq(t, "SECRET", token.Secret) assertEq(t, "consumersecret&", m.signer.UsedKey) assertEq(t, "http://www.mrjon.es/authorizetoken?oauth_token=TOKEN", url) }
func (self *Federation) downloadBlob(rawurl, blobref string) (dependencies []string, err os.Error) { // Get the blob var client http.Client req, err := client.Get(rawurl + "?blobref=" + http.URLEscape(blobref)) if err != nil { return nil, err } // TODO: Improve for large files blob, err := ioutil.ReadAll(req.Body) if err != nil { log.Printf("Error reading request body") return nil, err } log.Printf("Downloaded %v\n", string(blob)) req.Body.Close() self.store.StoreBlob(blob, "") // Check whether the retrieved blob is a schema blob mimetype := grapher.MimeType(blob) if mimetype == "application/x-lightwave-schema" { var schema depSchema err = json.Unmarshal(blob, &schema) if err != nil { log.Printf("Malformed schema blob: %v\n", err) return nil, err } dependencies = schema.Dependencies } return }
func handleLogout(w http.ResponseWriter, r *http.Request) { c = appengine.NewContext(r) returnURL := "/" // parse form err := r.ParseForm() if err != nil { serveError(c, w, err) return } if r.FormValue("continue") != "" { returnURL = r.FormValue("continue") } if useOpenID { // adjust returnURL to bring us back to a local user login form laterReturnUrl := returnURL returnURL = "/Login/?chooseLogin=1&continue=" + http.URLEscape(laterReturnUrl) } // redirect to google logout (for OpenID as well, or else we won't be locally logged out) lourl, err := user.LogoutURL(c, returnURL) if err != nil { c.Errorf("handleLogout: error getting LogoutURL") } c.Debugf("handleLogout: redirecting to logoutURL=%v", lourl) http.Redirect(w, r, lourl, http.StatusFound) return }
// for each search term on the commandline, create a photo grid func main() { for i := 1; i < len(os.Args); i++ { canvas.Start(width, height) canvas.Rect(0, 0, width, height, "fill:black") fs(http.URLEscape(os.Args[i])) canvas.End() } }
func addQueryVariables(url string, variables map[string]string) string { var addition string newUrl := url i := 0 for key, value := range variables { if i == 0 { addition = fmt.Sprintf("?%s=%s", key, http.URLEscape(value)) } else { addition = fmt.Sprintf("&%s=%s", key, http.URLEscape(value)) } newUrl += addition i++ } return newUrl }
// FormEncodedBytes returns a buffer containing the URL form encoding of the // map. func (m Values) FormEncodedBytes() []byte { var b bytes.Buffer sep := false for key, values := range m { escapedKey := http.URLEscape(key) for _, value := range values { if sep { b.WriteByte('&') } else { sep = true } b.WriteString(escapedKey) b.WriteByte('=') b.WriteString(http.URLEscape(value)) } } return b.Bytes() }
func TweetJson(client *Client, message string) (responseStr string, err os.Error) { message = http.URLEscape(message) var form = map[string][]string{ "status": []string{message}, } // send request responseStr, err = send(URLSendTweet, "POST", form, client, "status="+message) return }
func Handler(c http.ResponseWriter, r *http.Request) { host := r.Host s := session.Get(c, r) query, _ := http.ParseQuery(r.URL.RawQuery) continueURLS := query["continue-url"] continueURL := "" if len(continueURLS) >= 1 { continueURL = continueURLS[0] } if len(continueURL) == 0 { continueURL = "/" } fmt.Println(continueURL) s.Set("openid-continue-url", continueURL) fmt.Println(s.Get("openid-name-first")) url := "https://www.google.com/accounts/o8/ud" var urlParams = map[string]string{ "openid.ns": "http://specs.openid.net/auth/2.0", "openid.claimed_id": "http://specs.openid.net/auth/2.0/identifier_select", "openid.identity": "http://specs.openid.net/auth/2.0/identifier_select", "openid.return_to": "http://" + host + "/openid/auth", "openid.realm": "http://" + host, "openid.mode": "checkid_setup", "openid.ns.ui": "http://specs.openid.net/extensions/ui/1.0", "openid.ns.ext1": "http://openid.net/srv/ax/1.0", "openid.ext1.mode": "fetch_request", "openid.ext1.type.email": "http://axschema.org/contact/email", "openid.ext1.type.first": "http://axschema.org/namePerson/first", "openid.ext1.type.last": "http://axschema.org/namePerson/last", "openid.ext1.type.country": "http://axschema.org/contact/country/home", "openid.ext1.type.lang": "http://axschema.org/pref/language", "openid.ext1.required": "email,first,last,country,lang", "openid.ns.oauth": "http://specs.openid.net/extensions/oauth/1.0", "openid.oauth.consumer": host, "openid.oauth.scope": "http://picasaweb.google.com/data/"} queryURL := "?" for name, value := range urlParams { queryURL += http.URLEscape(name) + "=" + http.URLEscape(value) + "&" } queryURL = queryURL[0 : len(queryURL)-1] fmt.Println(queryURL) http.Redirect(c, r, url+queryURL, 307) }
func encodeQuery(args map[string]string) string { i := 0 s := bytes.NewBuffer(nil) for k, v := range args { if i != 0 { s.WriteString("&") } i++ s.WriteString(k + "=" + http.URLEscape(v)) } return s.String() }
// ツイートをポストするメソッド func (c *Client) Post(stat string) { // 送信本文をURLエンコード sStatURL := http.URLEscape(stat) if con, err := net.Dial("tcp", "", "twitter.com:80"); err == nil { io.WriteString(con, "POST /statuses/update.json?status="+sStatURL+" HTTP/1.1\r\n") io.WriteString(con, "Host: twitter.com\r\n") io.WriteString(con, "Authorization: Basic "+c.Auth.Base64enc()+"\r\n") io.WriteString(con, "\r\n") con.Close() } }
// writeSetCookies writes the wire representation of the set-cookies // to w. Each cookie is written on a separate "Set-Cookie: " line. // This choice is made because HTTP parsers tend to have a limit on // line-length, so it seems safer to place cookies on separate lines. func writeSetCookies(w io.Writer, kk []*http.Cookie) os.Error { if kk == nil { return nil } lines := make([]string, 0, len(kk)) var b bytes.Buffer for _, c := range kk { b.Reset() // TODO(petar): c.Value (below) should be unquoted if it is recognized as quoted fmt.Fprintf(&b, "%s=%s", http.CanonicalHeaderKey(c.Name), c.Value) if len(c.Path) > 0 { fmt.Fprintf(&b, "; Path=%s", http.URLEscape(c.Path)) } if len(c.Domain) > 0 { fmt.Fprintf(&b, "; Domain=%s", http.URLEscape(c.Domain)) } if len(c.Expires.Zone) > 0 { fmt.Fprintf(&b, "; Expires=%s", c.Expires.Format(time.RFC1123)) } if c.MaxAge >= 0 { fmt.Fprintf(&b, "; Max-Age=%d", c.MaxAge) } if c.HttpOnly { fmt.Fprintf(&b, "; HttpOnly") } if c.Secure { fmt.Fprintf(&b, "; Secure") } lines = append(lines, "Set-Cookie: "+b.String()+"\r\n") } sort.Strings(lines) for _, l := range lines { if _, err := io.WriteString(w, l); err != nil { return err } } return nil }
// Emphasize and escape a line of text for HTML. URLs are converted into links; // if the URL also appears in the words map, the link is taken from the map (if // the corresponding map value is the empty string, the URL is not converted // into a link). Go identifiers that appear in the words map are italicized; if // the corresponding map value is not the empty string, it is considered a URL // and the word is converted into a link. If nice is set, the remaining text's // appearance is improved where is makes sense (e.g., `` is turned into “ // and '' into ”). func emphasize(w io.Writer, line []byte, words map[string]string, nice bool) { for { m := matchRx.Execute(line) if len(m) == 0 { break } // m >= 6 (two parenthesized sub-regexps in matchRx, 1st one is identRx) // write text before match commentEscape(w, line[0:m[0]], nice) // analyze match match := line[m[0]:m[1]] url := "" italics := false if words != nil { url, italics = words[string(match)] } if m[2] < 0 { // didn't match against first parenthesized sub-regexp; must be match against urlRx if !italics { // no alternative URL in words list, use match instead url = string(match) } italics = false // don't italicize URLs } // write match if len(url) > 0 { w.Write(html_a) w.Write([]byte(http.URLEscape(url))) w.Write(html_aq) } if italics { w.Write(html_i) } commentEscape(w, match, nice) if italics { w.Write(html_endi) } if len(url) > 0 { w.Write(html_enda) } // advance line = line[m[1]:] } commentEscape(w, line, nice) }
func googlefont(f string) []string { empty := []string{} r, err := http.Get(gwfURI + http.URLEscape(f)) if err != nil { return empty } defer r.Body.Close() b, rerr := ioutil.ReadAll(r.Body) if rerr != nil || r.StatusCode != http.StatusOK { return empty } canvas.Def() fmt.Fprintf(canvas.Writer, fontfmt, b) canvas.DefEnd() return strings.Split(fontlist, "|") }
func requireAdminUser(w http.ResponseWriter, r *http.Request) (User string) { c := appengine.NewContext(r) User = requireAnyUser(w, r) if appengine.IsDevAppServer() { // dev app server always admin return // User } // TODO this should absolutely not be a hardcoded userid if User == "ccjava" { // the admin user! return // User } // not admin user c.Debugf("requireAdminUser: %v is not admin, redirecting to %v", User, "/Login/?chooseLogin=1&continue="+http.URLEscape(r.URL.RawPath)) w.Header().Set("Location", "/Login/?chooseLogin=1&continue="+http.URLEscape(r.URL.RawPath)) w.WriteHeader(http.StatusFound) return // User }
func (bucket *Bucket) Sign(req *http.Request) { // gather the string to be signed // method msg := req.Method + "\n" // md5sum if md5, present := req.Header["Content-MD5"]; present { msg += md5 } msg += "\n" // content-type if contentType, present := req.Header["Content-Type"]; present { msg += contentType } msg += "\n" // date msg += req.Header["Date"] + "\n" // add headers for _, key := range AWS_HEADERS { if value, present := req.Header[key]; present { msg += key + ":" + value + "\n" } } // resource: the path components should be URL-encoded, but not the slashes resource := http.URLEscape("/" + bucket.bucket + req.URL.Path) resource = strings.Replace(resource, "%2f", "/", -1) msg += resource // create the signature hmac := hmac.NewSHA1([]byte(bucket.secret)) hmac.Write([]byte(msg)) // get a base64 encoding of the signature encoded := new(bytes.Buffer) encoder := base64.NewEncoder(base64.StdEncoding, encoded) encoder.Write(hmac.Sum()) encoder.Close() signature := encoded.String() req.Header["Authorization"] = "AWS " + bucket.key + ":" + signature }
func (c *Client) ListBucket(bucket string, after string, maxKeys uint) (items []*Item, reterr os.Error) { var bres listBucketResults url := fmt.Sprintf("http://%s.s3.amazonaws.com/?marker=%s&max-keys=%d", bucket, http.URLEscape(after), maxKeys) req := newReq(url) c.Auth.SignRequest(req) res, err := c.httpClient().Do(req) if res != nil && res.Body != nil { defer res.Body.Close() } if err != nil { return nil, err } if err := xml.Unmarshal(res.Body, &bres); err != nil { return nil, err } return bres.Contents, nil }
func main() { flag.Parse() q, err := GetQueue() if err != nil { log.Fatalf("Couldn't create queue: %v\n", err) } id, err := GetAWSIdentity() if err != nil { log.Fatalf("Couldn't get identity: %v\n", err) } msgs, err := q.FetchMessages(id, *max, *timeout) if err != nil { log.Fatalf("Couldn't pop from queue: %v\n", err) } log.Printf("#[%d messages received]", len(msgs)) _form := *outform for mi := range msgs { err = q.DeleteMessage(id, msgs[mi]) if err != nil { log.Printf("Couldn't delete message, not displaying (%v)", err) continue } switch _form { case "text": fmt.Printf("MessageID\t%s\n", msgs[mi].MessageId.String()) fmt.Printf("ReceiptHandle\t%s\n", msgs[mi].ReceiptHandle) fmt.Printf("MessageBody\t%s\n", string(msgs[mi].Body)) case "shell": // TODO: Escape these properly. fmt.Printf("export SQSMessage%dId=\"%s\";\n", mi, msgs[mi].MessageId.String()) fmt.Printf("export SQSMessage%dReceipt=\"%s\";\n", mi, msgs[mi].ReceiptHandle) fmt.Printf("export SQSMessage%dBody=\"%s\";\n", mi, http.URLEscape(string(msgs[mi].Body))) case "raw": os.Stdout.Write(msgs[mi].Body) } } if _form == "json" { enc := json.NewEncoder(os.Stdout) err := enc.Encode(msgs) if err != nil { log.Fatalf("Error decoding messages: %v", err) } } }
func (self *Federation) downloadFrontier(rawurl string, blobref string) (frontier []string, err os.Error) { // Get the blob var client http.Client req, err := client.Get(rawurl + "?frontier=" + http.URLEscape(blobref)) if err != nil { return nil, err } // Process the returned value blob, err := ioutil.ReadAll(req.Body) if err != nil { log.Printf("Error reading request body") return nil, err } req.Body.Close() err = json.Unmarshal(blob, &frontier) if err != nil { log.Printf("Malformed frontier response: %v\n", err) return nil, err } return }
func NewDict(lang, title string) *Dict { d := emptyDict() parsed := new(Api) base := fmt.Sprintf(template, lang, http.URLEscape(title)) response, _, _ := http.Get(base) defer response.Body.Close() xml.Unmarshal(response.Body, parsed) d.original = parsed.Title for { d.add(parsed.Ll) cont := parsed.Any.Langlinks.Llcontinue if cont == "" { break } parsed = new(Api) newResponse, _, _ := http.Get(base + "&llcontinue=" + cont) defer newResponse.Body.Close() xml.Unmarshal(newResponse.Body, parsed) } return d }
func requireAnyUser(w http.ResponseWriter, r *http.Request) (User string) { c := appengine.NewContext(r) u := user.Current(c) if u == nil { // user not logged in if useOpenID { // redirect to our login page returnUrl := "/Login/" + "?chooseLogin=1&continue=" + http.URLEscape(r.URL.RawPath) c.Debugf("handleLogin: redirecting to %v", returnUrl) http.Redirect(w, r, returnUrl, http.StatusFound) return } else { // redirect to google login page returnUrl := r.URL.RawPath loginUrl, err := user.LoginURL(c, returnUrl) if err != nil { c.Errorf("handleLogin: error getting LoginURL for %v %v", returnUrl) } c.Debugf("handleLogin: redirecting to loginURL=%v", loginUrl) http.Redirect(w, r, loginUrl, http.StatusFound) } } // valid user logged in User = u.String() if appengine.IsDevAppServer() { // don't parse as URL for app server (doesn't support openid style usernames on dev login form) c.Debugf("requireAnyUser: running is app server, so not parsing userid as URL. returning user %v", User) return } url, err := http.ParseURL(User) if err != nil { // error parsing URL, redirect to Login? c.Errorf("error parsing User as URL: %v", User) w.Header().Set("Location", "/Login/") w.WriteHeader(http.StatusFound) } User = strings.Split(url.Host, ".", 2)[0] return // User }
func TestMissingRequestToken(t *testing.T) { c := basicConsumer() m := newMocks(t) m.install(c) m.httpClient.ExpectGet( "http://www.mrjon.es/requesttoken", map[string]string{ "oauth_callback": http.URLEscape("http://www.mrjon.es/callback"), "oauth_consumer_key": "consumerkey", "oauth_nonce": "2", "oauth_signature": "MOCK_SIGNATURE", "oauth_signature_method": "HMAC-SHA1", "oauth_timestamp": "1", "oauth_version": "1.0", }, "oauth_token_secret=SECRET") // Missing token _, _, err := c.GetRequestTokenAndUrl("http://www.mrjon.es/callback") if err == nil { t.Fatal("Should have raised an error") } }
func sendsms(number, msg string) (err os.Error) { log.Println("sending sms from " + number + " with the following message: " + msg) var r *http.Response url, _ := http.ParseURL(tropoURL + "&to=" + number + "&msg=" + http.URLEscape(msg) + "&token=" + tropoToken) log.Println("url: " + url.Raw) r, err = Get(url) if err != nil { return } if r.StatusCode != 200 { err = os.ErrorString("Tropo returned: " + r.Status) return } log.Println("tropo returned: " + r.Status) var buf []byte io.ReadFull(r.Body, buf[:]) log.Println(string(buf)) r.Body.Close() return }