Example #1
0
// GetMetaData retrieves instance metadata about the current machine.
//
// See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html for more details.
func GetMetaData(path string) (contents []byte, err error) {
	c := http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (net.Conn, error) {
				deadline := time.Now().Add(5 * time.Second)
				c, err := net.DialTimeout(netw, addr, time.Second*2)
				if err != nil {
					return nil, err
				}
				c.SetDeadline(deadline)
				return c, nil
			},
		},
	}

	url := "http://169.254.169.254/latest/meta-data/" + path

	resp, err := c.Get(url)
	if err != nil {
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		err = fmt.Errorf("Code %d returned for url %s", resp.StatusCode, url)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return
	}
	return []byte(body), err
}
Example #2
0
func SendCreateAccount(url, userName string, timeout time.Duration) (*createAccountResponse, error) {

	request := fmt.Sprintf("<?xml version=\"1.0\" "+
		"encoding=\"UTF-8\"?>"+
		"<SOAP-ENV:Envelope xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" "+
		"xmlns:ns0=\"http://schemas.xmlsoap.org/soap/encoding/\" "+
		"xmlns:ns1=\"http://vendors.services.windfindtech.com\" "+
		"xmlns:ns2=\"http://schemas.xmlsoap.org/soap/envelope/\" "+
		"xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "+
		"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "+
		"SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
		"<SOAP-ENV:Header/>"+
		"<ns2:Body>"+
		"<ns1:createAccount>"+
		"<createAccountRequest>"+
		"<site>i-Shanghai</site>"+
		"<userName>%s</userName>"+
		"</createAccountRequest>"+
		"</ns1:createAccount>"+
		"</ns2:Body>"+
		"</SOAP-ENV:Envelope>", userName)
	//fmt.Println(request)

	//define http client
	c := http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (net.Conn, error) {
				deadline := time.Now().Add(25 * time.Second)
				c, err := net.DialTimeout(netw, addr, time.Second*timeout)
				if err != nil {
					return nil, err
				}
				c.SetDeadline(deadline)
				return c, nil
			},
		},
	}

	//send http post by httpclient
	body := bytes.NewBuffer([]byte(request))
	res, err := c.Post(url, "text/xml; charset=utf-8", body)
	if err != nil {
		fmt.Println(err)
		return nil, err
	} else {
		response, errt := DecodeResponseBody(res.Body)
		if nil != errt {
			fmt.Errorf("err")
		}

		return response, nil
	}

}
Example #3
0
// doHttpRequest sends hreq and returns the http response from the server.
// If resp is not nil, the XML data contained in the response
// body will be unmarshalled on it.
func (client *Client) doHttpRequest(hreq *http.Request, resp interface{}) (*http.Response, error) {
	c := http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (c net.Conn, err error) {
				deadline := time.Now().Add(client.ReadTimeout)
				if client.ConnectTimeout > 0 {
					c, err = net.DialTimeout(netw, addr, client.ConnectTimeout)
				} else {
					c, err = net.Dial(netw, addr)
				}
				if err != nil {
					return
				}
				if client.ReadTimeout > 0 {
					err = c.SetDeadline(deadline)
				}
				return
			},
			Proxy: http.ProxyFromEnvironment,
		},
	}

	hresp, err := c.Do(hreq)
	if err != nil {
		return nil, err
	}
	if client.debug {
		log.Printf("%s %s %d\n", hreq.Method, hreq.URL.String(), hresp.StatusCode)
		contentType := hresp.Header.Get("Content-Type")
		if contentType == "application/xml" || contentType == "text/xml" {
			dump, _ := httputil.DumpResponse(hresp, true)
			log.Printf("} -> %s\n", dump)
		} else {
			log.Printf("Response Content-Type: %s\n", contentType)
		}
	}
	if hresp.StatusCode != 200 && hresp.StatusCode != 204 && hresp.StatusCode != 206 {
		return nil, client.buildError(hresp)
	}
	if resp != nil {
		err = xml.NewDecoder(hresp.Body).Decode(resp)
		hresp.Body.Close()

		if client.debug {
			log.Printf("aliyungo.oss> decoded xml into %#v", resp)
		}

	}
	return hresp, err
}
Example #4
0
// doHttpRequest sends hreq and returns the http response from the server.
// If resp is not nil, the XML data contained in the response
// body will be unmarshalled on it.
func (s3 *S3) doHttpRequest(hreq *http.Request, resp interface{}) (*http.Response, error) {
	c := http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (c net.Conn, err error) {
				deadline := time.Now().Add(s3.ReadTimeout)
				if s3.ConnectTimeout > 0 {
					c, err = net.DialTimeout(netw, addr, s3.ConnectTimeout)
				} else {
					c, err = net.Dial(netw, addr)
				}
				if err != nil {
					return
				}
				if s3.ReadTimeout > 0 {
					err = c.SetDeadline(deadline)
				}
				return
			},
			Proxy: http.ProxyFromEnvironment,
		},
	}

	hresp, err := c.Do(hreq)
	if err != nil {
		return nil, err
	}
	if debug {
		dump, _ := httputil.DumpResponse(hresp, true)
		log.Printf("} -> %s\n", dump)
	}
	if hresp.StatusCode != 200 && hresp.StatusCode != 204 && hresp.StatusCode != 206 {
		return nil, buildError(hresp)
	}
	if resp != nil {
		err = xml.NewDecoder(hresp.Body).Decode(resp)
		hresp.Body.Close()

		if debug {
			log.Printf("goamz.s3> decoded xml into %#v", resp)
		}

	}
	return hresp, err
}
Example #5
0
func CallbackHttp(json string) (e error) {

	log.Printf("We're gonna launch http notif...")

	httpUrl := GetLocalWigo().GetConfig().Notifications.HttpUrl

	// Create http client with timeout
	c := http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
			Dial: func(netw, addr string) (net.Conn, error) {
				deadline := time.Now().Add(5 * time.Second)
				c, err := net.DialTimeout(netw, addr, time.Second*5)
				if err != nil {
					return nil, err
				}
				c.SetDeadline(deadline)
				return c, nil
			},
		},
	}

	// Make post values
	postValues := url.Values{}
	postValues.Add("Notification", string(json))

	// Make request
	resp, reqErr := c.PostForm(httpUrl, postValues)
	if reqErr != nil {
		log.Printf("Error sending callback to url %s : %s", httpUrl, reqErr)
		return reqErr
	} else {
		log.Printf(" - Sent to http url : %s", httpUrl)
	}

	resp.Body.Close()

	return nil
}
Example #6
0
func getTimeoutHttpClient(timeout int) *http.Client {
	c := http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (net.Conn, error) {
				deadline := time.Now().Add(time.Duration(timeout) * time.Second)
				c, err := net.DialTimeout(netw, addr, time.Duration(timeout)*time.Second)
				if c != nil {
					c.SetDeadline(deadline)
				}
				return c, err
			},
		},

		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			if len(via) >= 100 {
				return errors.New("stopped after 100 redirects")
			}
			return nil
		},
	}

	return &c
}
Example #7
0
File: s3.go Project: hailocab/goamz
// run sends req and returns the http response from the server.
// If resp is not nil, the XML data contained in the response
// body will be unmarshalled on it.
func (s3 *S3) run(req *request, resp interface{}) (*http.Response, error) {
	if debug {
		log.Printf("Running S3 request: %#v", req)
	}

	u, err := req.url()
	if err != nil {
		return nil, err
	}

	hreq := http.Request{
		URL:        u,
		Method:     req.method,
		ProtoMajor: 1,
		ProtoMinor: 1,
		Close:      true,
		Header:     req.headers,
	}

	if v, ok := req.headers["Content-Length"]; ok {
		hreq.ContentLength, _ = strconv.ParseInt(v[0], 10, 64)
		delete(req.headers, "Content-Length")
	}
	if req.payload != nil {
		hreq.Body = ioutil.NopCloser(req.payload)
	}

	c := http.Client{
		Transport: &http.Transport{
			Dial: func(netw, addr string) (c net.Conn, err error) {
				deadline := time.Now().Add(s3.ReadTimeout)
				if s3.ConnectTimeout > 0 {
					c, err = net.DialTimeout(netw, addr, s3.ConnectTimeout)
				} else {
					c, err = net.Dial(netw, addr)
				}
				if err != nil {
					return
				}
				if s3.ReadTimeout > 0 {
					err = c.SetDeadline(deadline)
				}
				return
			},
		},
	}

	hresp, err := c.Do(&hreq)
	if err != nil {
		return nil, err
	}
	if debug {
		dump, _ := httputil.DumpResponse(hresp, true)
		log.Printf("} -> %s\n", dump)
	}
	if hresp.StatusCode != 200 && hresp.StatusCode != 204 {
		return nil, buildError(hresp)
	}
	if resp != nil {
		err = xml.NewDecoder(hresp.Body).Decode(resp)
		hresp.Body.Close()

		if debug {
			log.Printf("goamz.s3> decoded xml into %#v", resp)
		}

	}
	return hresp, err
}