Ejemplo n.º 1
0
func ExpandURL(shortUrl string) (expandedUrl string, err os.Error) {
	param := http.EncodeQuery(map[string][]string{"shortUrl": {shortUrl}})
	res, _, err := http.Get("https://www.googleapis.com/urlshortener/v1/url?" + param)
	if err != nil {
		return
	}
	if res.StatusCode != 200 {
		err = os.NewError("failed to post")
		return
	}
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}
	var decbuf bytes.Buffer
	decbuf.Write(b)
	dec := json.NewDecoder(&decbuf)
	var out map[string]interface{}
	err = dec.Decode(&out)
	if err != nil {
		return
	}
	expandedUrl = out["longUrl"].(string)
	return
}
Ejemplo n.º 2
0
Archivo: http.go Proyecto: vdobler/ft
// Perform a GET request for the test t.
func Get(t *Test) (r *http.Response, finalUrl string, cookies []*http.Cookie, err os.Error) {
	var url = t.Url // <-- Patched

	var req http.Request
	req.Method = "GET"
	req.ProtoMajor = 1
	req.ProtoMinor = 1
	req.Header = http.Header{}

	if len(t.Param) > 0 {
		ep := http.EncodeQuery(t.Param)
		if strings.Contains(url, "?") {
			url = url + "&" + ep
		} else {
			url = url + "?" + ep
		}
	}
	req.URL, err = http.ParseURL(url)
	if err != nil {
		err = &http.URLError{"Get", url, err}
		return
	}

	addHeadersAndCookies(&req, t)
	url = req.URL.String()
	debug("Will get from %s", req.URL.String())
	r, finalUrl, cookies, err = DoAndFollow(&req, t.Dump)
	return
}
Ejemplo n.º 3
0
// NewPOSTTask creates a Task that will POST to a path with the given form data.
func NewPOSTTask(path string, params map[string][]string) *Task {
	return &Task{
		Path:    path,
		Payload: []byte(http.EncodeQuery(params)),
		Method:  "POST",
	}
}
// NewPOSTTask creates a Task that will POST to a path with the given form data.
func NewPOSTTask(path string, params map[string][]string) *Task {
	h := make(http.Header)
	h.Set("Content-Type", "application/x-www-form-urlencoded")
	return &Task{
		Path:    path,
		Payload: []byte(http.EncodeQuery(params)),
		Header:  h,
		Method:  "POST",
	}
}
Ejemplo n.º 5
0
// Appends the parameters to the URL, escaping the key and value strings.
// If the URL already has parameters, an & will be inserted.
//
// Trailing &'s in the inurl will cause duplicate &'s in the outurl.
func AppendParams(inurl http.URL, parms map[string][]string) (outurl http.URL) {
	outurl = inurl
	pstr := http.EncodeQuery(parms)
	if len(pstr) > 0 {
		if len(outurl.RawQuery) > 0 {
			outurl.RawQuery += "&"
		}
		outurl.RawQuery += pstr
	}
	return
}
Ejemplo n.º 6
0
func newQuery(id cryptools.NamedSigner, endpoint awsconn.Endpoint,
	domain, action string,
	params map[string]string) (hreq *http.Request, err os.Error) {

	if params == nil {
		params = make(map[string]string)
	}
	req := &Request{
		//Method: method,
		Host: endpoint.GetURL().Host,
		URL: &http.URL{
			Scheme: endpoint.GetURL().Scheme,
			Host:   endpoint.GetURL().Host,
			Path:   endpoint.GetURL().Path,
		},
	}
	if req.URL.Path == "" {
		req.URL.Path = "/"
	}
	if _, ok := params["Version"]; !ok {
		params["Version"] = DEFAULT_VERSION
	}
	if _, ok := params["SignatureVersion"]; !ok {
		params["SignatureVersion"] = DEFAULT_SIGNATURE_VERSION
	}
	if _, ok := params["SignatureMethod"]; !ok {
		params["SignatureMethod"] = DEFAULT_SIGNATURE_METHOD
	}
	if _, ok := params["Timestamp"]; !ok {
		params["Timestamp"] = time.UTC().Format("2006-01-02T15:04:05-07:00")
	}
	// ListDomains, Select don't require a DomainName attribute
	if domain != "" {
		params["DomainName"] = domain
	}
	params["Action"] = action
	params["AWSAccessKeyId"] = id.SignerName()

	req.Form = maptools.StringStringToStringStrings(params)
	if len(http.EncodeQuery(req.Form)) > 512 {
		req.Method = "POST"
	} else {
		req.Method = "GET"
	}

	if _, ok := params["Signature"]; !ok {
		err = req.Sign(id)
	}
	if err == nil {
		hreq = (*http.Request)(req)
	}
	return
}
Ejemplo n.º 7
0
func (self CBUIReturnVerifier) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
	if req.FormValue("signatureVersion") != "2" {
		req.Form["CBUI.Error"] = []string{"Invalid CBUI signature version"}
		self.Failure.ServeHTTP(rw, req)
		self.Logger.Printf("signatureVersion not provided: %v", req.URL.RawQuery)
		return
	}
	myurl := &http.URL{
		Host:   req.Host,
		Path:   req.URL.Path,
		Scheme: "http",
	}
	if rw.UsingTLS() {
		myurl.Scheme += "s"
	}

	vurl := &http.URL{
		Scheme: self.GatewayURL.Scheme,
		Host:   self.GatewayURL.Host,
		Path:   self.GatewayURL.Path,
		RawQuery: http.EncodeQuery(map[string][]string{
			"UrlEndPoint":    []string{myurl.String()},
			"HttpParameters": []string{req.URL.RawQuery},
			"Action":         []string{"VerifySignature"},
			"Version":        []string{"2008-09-17"},
		}),
	}

	self.Logger.Printf("Verifying signature from %s", self.GatewayURL.String())

	resp, _, err := http.Get(vurl.String())
	if err != nil {
		self.Logger.Printf("Get Failed: %v", err)
		req.Form["CBUI.Error"] = []string{err.String()}
		self.Failure.ServeHTTP(rw, req)
	} else {
		xresp := cbuiResponse{}
		err = xml.Unmarshal(resp.Body, &xresp)
		if err != nil {
			req.Form["CBUI.Error"] = []string{err.String()}
			self.Failure.ServeHTTP(rw, req)
			return
		}
		if xresp.VerifySignatureResult.VerificationStatus != "Success" {
			req.Form["CBUI.Error"] = []string{"Amazon refused signature verification"}
			self.Failure.ServeHTTP(rw, req)
			return
		}
		req.Form["CBUI.Ok"] = []string{"true"}
		self.Success.ServeHTTP(rw, req)
	}
}
Ejemplo n.º 8
0
func AnalyticsURL(shortUrl string) (info AnalyticsInfo, err os.Error) {
	param := http.EncodeQuery(map[string][]string{"shortUrl": {shortUrl}, "projection": {"FULL"}})
	var res *http.Response
	res, _, err = http.Get("https://www.googleapis.com/urlshortener/v1/url?" + param)
	if err != nil {
		return
	}
	if res.StatusCode != 200 {
		err = os.NewError("failed to post")
		return
	}
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return
	}
	err = json.Unmarshal(b, &info)
	return
}
Ejemplo n.º 9
0
func (self *PrepaidGateway) PrepaidCobrandedURL(params map[string]string) (url *http.URL, err os.Error) {
	params["pipelineName"] = "SetupPrepaid"
	if _, ok := params["callerReferenceFunding"]; !ok {
		return nil, os.NewError("callerReferenceFunding is mandatory")
	}
	if _, ok := params["callerReferenceSender"]; !ok {
		return nil, os.NewError("callerReferenceSender is mandatory")
	}
	if _, ok := params["fundingAmount"]; !ok {
		return nil, os.NewError("fundingAmount is mandatory")
	}

	ireq, err := calculateCobrandingURL(self.signer, self.gwep, params)
	if err == nil {
		url = ireq.URL
		if len(url.RawQuery) > 0 {
			url.RawQuery += "&"
		}
		url.RawQuery += http.EncodeQuery(ireq.Form)
	}
	return
}
Ejemplo n.º 10
0
Archivo: http.go Proyecto: vdobler/ft
// PostForm issues a POST to the specified URL,
// with data's keys and values urlencoded as the request body.
//
// Caller should close r.Body when done reading from it.
func Post(t *Test) (r *http.Response, finalUrl string, cookies []*http.Cookie, err os.Error) {
	var req http.Request
	var url = t.Url
	req.Method = "POST"
	req.ProtoMajor = 1
	req.ProtoMinor = 1
	req.Close = true
	var body *bytes.Buffer
	var contentType string
	if hasFile(&t.Param) || t.Method == "POST:mp" {
		var boundary string
		body, boundary = multipartBody(&t.Param)
		contentType = "multipart/form-data; boundary=" + boundary
	} else {
		contentType = "application/x-www-form-urlencoded"
		bodystr := http.EncodeQuery(t.Param)
		body = bytes.NewBuffer([]byte(bodystr))
	}

	req.Body = nopCloser{body}
	req.Header = http.Header{
		"Content-Type":   {contentType},
		"Content-Length": {strconv.Itoa(body.Len())},
	}
	addHeadersAndCookies(&req, t)

	req.ContentLength = int64(body.Len())
	req.URL, err = http.ParseURL(url)
	if err != nil {
		return nil, url, cookies, err
	}
	debug("Will post to %s", req.URL.String())

	r, finalUrl, cookies, err = DoAndFollow(&req, t.Dump)
	return
}
Ejemplo n.º 11
0
// dash runs the given method and command on the dashboard.
// If args is not nil, it is the query or post parameters.
// If resp is not nil, dash unmarshals the body as JSON into resp.
func dash(meth, cmd string, resp interface{}, args param) os.Error {
	var r *http.Response
	var err os.Error
	if *verbose {
		log.Println("dash", cmd, args)
	}
	cmd = "http://" + *dashboard + "/" + cmd
	switch meth {
	case "GET":
		if args != nil {
			m := make(map[string][]string)
			for k, v := range args {
				m[k] = []string{v}
			}
			cmd += "?" + http.EncodeQuery(m)
		}
		r, err = http.Get(cmd)
	case "POST":
		r, err = http.PostForm(cmd, args)
	default:
		return fmt.Errorf("unknown method %q", meth)
	}
	if err != nil {
		return err
	}
	defer r.Body.Close()
	var buf bytes.Buffer
	buf.ReadFrom(r.Body)
	if resp != nil {
		if err = json.Unmarshal(buf.Bytes(), resp); err != nil {
			log.Printf("json unmarshal %#q: %s\n", buf.Bytes(), err)
			return err
		}
	}
	return nil
}