Esempio n. 1
1
// Initialise the http.Transport for go1.7+
func (ci *ConfigInfo) initTransport(t *http.Transport) {
	t.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
		return dialContextTimeout(ctx, network, address, ci.ConnectTimeout, ci.Timeout)
	}
	t.IdleConnTimeout = 60 * time.Second
	t.ExpectContinueTimeout = ci.ConnectTimeout
}
Esempio n. 2
0
func (auth *Auth) NewClient() *http.Client {
	var client *http.Client
	var transport *http.Transport
	if auth.Settings["insecure"] != "" || auth.Settings["proxy_host"] != "" {
		transport = &http.Transport{}
	}
	if auth.Settings["insecure"] != "" {
		//FIXME
		os.Setenv("HTTP_PROXY", "http://127.0.0.1:10004")
		os.Setenv("NO_PROXY", "")
		var config *tls.Config = &tls.Config{
			InsecureSkipVerify: true,
		}
		transport.TLSClientConfig = config
	}
	if auth.Settings["proxy_host"] != "" {
		turl, _ := url.Parse("http://" + auth.Settings["proxy_host"] + ":" + auth.Settings["proxy_port"])
		transport.Proxy = http.ProxyURL(turl)
	}
	if auth.Settings["insecure"] != "" || auth.Settings["proxy_host"] != "" {
		client = &http.Client{
			Jar:       auth.CookieJar,
			Transport: transport,
		}
	} else {
		client = &http.Client{
			Jar: auth.CookieJar,
		}
	}
	return client
}
Esempio n. 3
0
func main() {

	transport := new(http.Transport)
	transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

	client := new(http.Client)

	client.Transport = transport

	url := "https://10.80.65.70:9443/rest/agent/1a5dc1ef-f0e6-4aad-839a-a7880d539e8d"
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		panic(err)
	}

	creds := convertCredentials("admin", "admin")

	req.Header.Add("Authorization", "Basic "+creds)

	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	fmt.Println(resp)

}
func configureTransport(t1 *http.Transport) error {
	connPool := new(clientConnPool)
	t2 := &Transport{ConnPool: noDialClientConnPool{connPool}}
	if err := registerHTTPSProtocol(t1, noDialH2RoundTripper{t2}); err != nil {
		return err
	}
	if t1.TLSClientConfig == nil {
		t1.TLSClientConfig = new(tls.Config)
	}
	if !strSliceContains(t1.TLSClientConfig.NextProtos, "h2") {
		t1.TLSClientConfig.NextProtos = append([]string{"h2"}, t1.TLSClientConfig.NextProtos...)
	}
	upgradeFn := func(authority string, c *tls.Conn) http.RoundTripper {
		cc, err := t2.NewClientConn(c)
		if err != nil {
			c.Close()
			return erringRoundTripper{err}
		}
		connPool.addConn(authorityAddr(authority), cc)
		return t2
	}
	if m := t1.TLSNextProto; len(m) == 0 {
		t1.TLSNextProto = map[string]func(string, *tls.Conn) http.RoundTripper{
			"h2": upgradeFn,
		}
	} else {
		m["h2"] = upgradeFn
	}
	return nil
}
Esempio n. 5
0
func login(c *cli.Context) {
	println("echo login,", c.Args().Get(0), c.Args().Get(1))

	// client := &http.Client{}
	reqest, _ := http.NewRequest("GET", "https://passport.baidu.com", nil)

	reqest.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
	reqest.Header.Set("Accept-Encoding", "gzip, deflate, sdch")
	reqest.Header.Set("Accept-Language", "en-US,en;q=0.8,zh-CN;q=0.6,zh-TW;q=0.4")
	reqest.Header.Set("Connection", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36")

	transport := http.Transport{}
	response, err := transport.RoundTrip(reqest)
	if err != nil {
		// t.Fatal(err)
	}
	// // Check if you received the status codes you expect. There may
	// // status codes other than 200 which are acceptable.
	// if resp.StatusCode != 200 && resp.StatusCode != 302 {
	//     t.Fatal("Failed with status", resp.Status)
	// }

	// response,_ := client.Do(reqest)
	if response.StatusCode == 302 {
		// body, _ := ioutil.ReadAll(response.Body)
		// bodystr := string(body);
		fmt.Println("cookie: ", response.Cookies())
	} else {
		fmt.Println("error, code=", response.StatusCode)
	}

	println("login done")
}
Esempio n. 6
0
// send sends a request, kills it after passed timeout and writes the result to passed channel
func send(httpClient *http.Client, tr *http.Transport, req request, timeout time.Duration, resp chan request) {
	if *debug {
		fmt.Println("Sending a request to", req.httpReq.URL)
	}
	// Send the HTTP request in it's own goroutine using a HTTP client to be able to abort the request if reached timeout
	go func() {
		start := time.Now()
		resp, err := httpClient.Do(req.httpReq)
		if err != nil {
			req.err = err
		} else {
			req.StatusCode = resp.StatusCode
			req.Duration = time.Since(start)
			resp.Body.Close()
		}
		// Indicate the request is finished
		close(req.done)
	}()
	// Either the request finished, or the timeout triggers
	select {
	case <-req.done:
		// Request is done, please continue
	case <-time.After(timeout):
		req.Duration = timeout
		// Manually cancel the request in flight
		tr.CancelRequest(req.httpReq)
		req.err = errTimeout
	}
	// Send back on the response channel
	resp <- req
}
Esempio n. 7
0
// New creates a new Firebase reference
func New(url string) *Firebase {

	var tr *http.Transport
	tr = &http.Transport{
		DisableKeepAlives: true, // https://code.google.com/p/go/issues/detail?id=3514
		Dial: func(network, address string) (net.Conn, error) {
			start := time.Now()
			c, err := net.DialTimeout(network, address, TimeoutDuration)
			tr.ResponseHeaderTimeout = TimeoutDuration - time.Since(start)
			return c, err
		},
	}

	var client *http.Client
	client = &http.Client{
		Transport:     tr,
		CheckRedirect: redirectPreserveHeaders,
	}

	return &Firebase{
		url:          sanitizeURL(url),
		params:       _url.Values{},
		client:       client,
		stopWatching: make(chan struct{}),
	}
}
Esempio n. 8
0
// Worker
func worker(t *http.Transport, reqChan chan *http.Request, respChan chan Response) {
	for req := range reqChan {
		resp, err := t.RoundTrip(req)
		r := Response{resp, err}
		respChan <- r
	}
}
Esempio n. 9
0
// Creates a new Client with the provided options.
func New(o Options) (*Client, error) {
	if len(o.Endpoints) == 0 {
		return nil, missingEtcdEndpoint
	}

	if o.Timeout == 0 {
		o.Timeout = defaultTimeout
	}

	httpClient := &http.Client{Timeout: o.Timeout}

	if o.Insecure {
		var transport *http.Transport
		if dt, ok := http.DefaultTransport.(*http.Transport); ok {
			dtc := *dt
			transport = &dtc
		} else {
			transport = &http.Transport{
				Proxy: http.ProxyFromEnvironment,
				Dial: (&net.Dialer{
					Timeout:   30 * time.Second,
					KeepAlive: 30 * time.Second}).Dial,
				TLSHandshakeTimeout: 10 * time.Second}
		}

		transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
		httpClient.Transport = transport
	}

	return &Client{
		endpoints:  o.Endpoints,
		routesRoot: o.Prefix + routesPath,
		client:     httpClient,
		etcdIndex:  0}, nil
}
Esempio n. 10
0
func image(command *bot.Cmd, matches []string) (msg string, err error) {
	client := &http.Client{}
	request, _ := http.NewRequest("GET", fmt.Sprintf(imageURL, url.QueryEscape(matches[1])), nil)
	request.Header.Set("Ocp-Apim-Subscription-Key", bot.Config.Bing)

	response, _ := client.Do(request)
	defer response.Body.Close()
	body, _ := ioutil.ReadAll(response.Body)

	var results ImageResults
	json.Unmarshal(body, &results)

	if err != nil {
		return fmt.Sprintf("No results for %s", matches[1]), nil
	}

	if len(results.Value) == 0 {
		return fmt.Sprintf("No results for %s", matches[1]), nil
	}

	pageURL := results.Value[0].ContentURL

	transport := http.Transport{}

	request, _ = http.NewRequest("HEAD", pageURL, nil)
	response, _ = transport.RoundTrip(request)
	pageURL = response.Header.Get("Location")

	output := fmt.Sprintf("Bing | %s | %s", matches[1], pageURL)
	return output, nil
}
Esempio n. 11
0
// Creates a new Infoblox client with the supplied user/pass configuration.
// Supports the use of HTTP proxies through the $HTTP_PROXY env var.
// For example:
//     export HTTP_PROXY=http://localhost:8888
//
// When using a proxy, disable TLS certificate verification with the following:
//    sslVerify = false
func NewClient(host, username, password string, sslVerify bool) *Client {
	var (
		req, _    = http.NewRequest("GET", host, nil)
		proxy, _  = http.ProxyFromEnvironment(req)
		transport *http.Transport
		tlsconfig *tls.Config
	)
	tlsconfig = &tls.Config{
		InsecureSkipVerify: !sslVerify,
	}
	if tlsconfig.InsecureSkipVerify {
		fmt.Println("WARNING: SSL cert verification  disabled!")
	}
	transport = &http.Transport{
		TLSClientConfig: tlsconfig,
	}
	if proxy != nil {
		transport.Proxy = http.ProxyURL(proxy)
	}
	return &Client{
		Host: host,
		HttpClient: &http.Client{
			Transport: transport,
		},
		Username: username,
		Password: password,
	}
}
Esempio n. 12
0
func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
	transport := new(http.Transport)
	h, ok := w.(http.Hijacker)

	if !ok {
		return errors.New("Unable to hijack connection")
	}

	r.Host = srv.addr
	r.URL.Host = r.Host

	if len(r.URL.Scheme) == 0 {
		r.URL.Scheme = "http"
	}

	response, err := transport.RoundTrip(r)

	if err != nil {
		return err
	}

	conn, _, err := h.Hijack()

	if err != nil {
		return err
	}

	defer conn.Close()
	defer response.Body.Close()
	return response.Write(conn)
}
Esempio n. 13
0
func (r exampleDomainRewriter) RoundTrip(req *http.Request) (resp *http.Response, err error) {
	req.URL.Scheme = "http"
	req.URL.Host = r.RewriteURL.Host

	t := http.Transport{}
	return t.RoundTrip(req)
}
Esempio n. 14
0
func (this *RequestHandler) HandleHttpRequest(transport *http.Transport, address string) (*http.Response, error) {
	this.request.URL.Scheme = "http"
	this.request.URL.Host = address

	if host, _, err := net.SplitHostPort(this.request.RemoteAddr); err == nil {
		xForwardFor := append(this.request.Header["X-Forwarded-For"], host)
		this.request.Header.Set("X-Forwarded-For", strings.Join(xForwardFor, ", "))
	} else {
		log.Println("set X-Forwarded-For error:", err)
	}

	if _, ok := this.request.Header[http.CanonicalHeaderKey("X-Request-Start")]; !ok {
		this.request.Header.Set("X-Request-Start", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
	}

	this.request.Close = true
	this.request.Header.Del("Connection")

	response, err := transport.RoundTrip(this.request)
	if err != nil {
		return response, err
	}

	for k, vv := range response.Header {
		for _, v := range vv {
			this.response.Header().Add(k, v)
		}
	}

	return response, err
}
func NewProxyAuthTransport(rawTransport *http.Transport, customHeaders http.Header) (*ProxyAuthTransport, error) {
	dialFn := rawTransport.Dial
	if dialFn == nil {
		dialFn = net.Dial
	}
	tr := &ProxyAuthTransport{Dial: dialFn, CustomHeaders: customHeaders}
	proxyUrlFn := rawTransport.Proxy
	if proxyUrlFn != nil {
		wrappedDialFn := tr.wrapTransportDial()
		rawTransport.Dial = wrappedDialFn
		proxyUrl, err := proxyUrlFn(nil)
		if err != nil {
			return nil, err
		}
		if proxyUrl.Scheme != "http" {
			return nil, fmt.Errorf("Only HTTP proxy supported, for SOCKS use http.Transport with custom dialers & upstreamproxy.NewProxyDialFunc")
		}
		if proxyUrl.User != nil {
			tr.Username = proxyUrl.User.Username()
			tr.Password, _ = proxyUrl.User.Password()
		}
		// strip username and password from the proxyURL because
		// we do not want the wrapped transport to handle authentication
		proxyUrl.User = nil
		rawTransport.Proxy = http.ProxyURL(proxyUrl)
	}

	tr.Transport = rawTransport
	return tr, nil
}
Esempio n. 16
0
// Proxy the request to the given endpoint, execute observers and middlewares chains
func (l *HttpLocation) proxyToEndpoint(tr *http.Transport, o *Options, endpoint endpoint.Endpoint, req request.Request) (*http.Response, error) {

	a := &request.BaseAttempt{Endpoint: endpoint}

	l.observerChain.ObserveRequest(req)
	defer l.observerChain.ObserveResponse(req, a)
	defer req.AddAttempt(a)

	it := l.middlewareChain.GetIter()
	defer l.unwindIter(it, req, a)

	for v := it.Next(); v != nil; v = it.Next() {
		a.Response, a.Error = v.ProcessRequest(req)
		if a.Response != nil || a.Error != nil {
			// Move the iterator forward to count it again once we unwind the chain
			it.Next()
			log.Errorf("Midleware intercepted request with response=%s, error=%s", a.Response.Status, a.Error)
			return a.Response, a.Error
		}
	}

	// Forward the request and mirror the response
	start := o.TimeProvider.UtcNow()
	a.Response, a.Error = tr.RoundTrip(req.GetHttpRequest())
	a.Duration = o.TimeProvider.UtcNow().Sub(start)
	return a.Response, a.Error
}
Esempio n. 17
0
func main() {
	flag.Parse()

	if *port == 0 {
		xport, err := strconv.Atoi(os.Getenv("PORT"))
		if err != nil {
			fmt.Println("Please specify the HTTP port (either flag or environment)")
			os.Exit(1)
		}
		*port = xport
	}

	if flag.NArg() < 1 {
		fmt.Println("Specify remote URL on the command line")
		os.Exit(1)
	}

	remote, err := url.Parse(flag.Arg(0))
	if err != nil {
		fmt.Println("error parsing remote URL", err)
		os.Exit(1)
	}

	transport := new(http.Transport)

	switch remote.Scheme {
	case "http":
		if *ca != "" {
			log.Println("ignoring ca flag for non-https remote")
		}

	case "https":
		if *ca != "" {
			pool := x509.NewCertPool()
			data, err := ioutil.ReadFile(*ca)
			if err != nil {
				log.Fatal(err)
				os.Exit(1)
			}
			pool.AppendCertsFromPEM(data)
			tlsconfig := new(tls.Config)
			tlsconfig.RootCAs = pool
			transport.TLSClientConfig = tlsconfig
		}

	default:
		fmt.Println("unsupported remote scheme:", remote.Scheme)
		os.Exit(1)
	}

	rp := &ReverseProxy{
		remote,
		transport,
	}

	log.Fatal(http.ListenAndServe(
		fmt.Sprintf(":%v", *port), rp))
}
Esempio n. 18
0
func NewClient(e *Env, config *ClientConfig, needCookie bool) *Client {
	var jar *cookiejar.Jar
	if needCookie && (config == nil || config.UseCookies) && e.GetTorMode().UseCookies() {
		jar, _ = cookiejar.New(nil)
	}

	var timeout time.Duration

	// from http.DefaultTransport;
	//
	// TODO: change this back (see comment below) to allow
	// shared Transport after we switch to go1.7
	xprt := http.Transport{
		Proxy: http.ProxyFromEnvironment,
		Dial: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: 30 * time.Second,
		}).Dial,
		TLSHandshakeTimeout:   10 * time.Second,
		ExpectContinueTimeout: 1 * time.Second,
	}

	// This disables HTTP/2. There's a bug introduced between (go1.6, go1.6.3]
	// that makes client.Get hang on certain HTTP/2 servers. See CORE-3441 for
	// details.
	//
	// TODO: remove this after the bug is fixed.
	xprt.TLSNextProto = make(
		map[string]func(authority string, c *tls.Conn) http.RoundTripper)

	if (config != nil && config.RootCAs != nil) || e.GetTorMode().Enabled() {
		if config != nil && config.RootCAs != nil {
			xprt.TLSClientConfig = &tls.Config{RootCAs: config.RootCAs}
		}
		if e.GetTorMode().Enabled() {
			dialSocksProxy := socks.DialSocksProxy(socks.SOCKS5, e.GetTorProxy())
			xprt.Dial = dialSocksProxy
		} else {
			xprt.Proxy = http.ProxyFromEnvironment
		}
	}
	if config == nil || config.Timeout == 0 {
		timeout = HTTPDefaultTimeout
	} else {
		timeout = config.Timeout
	}

	ret := &Client{
		cli:    &http.Client{Timeout: timeout},
		config: config,
	}
	if jar != nil {
		ret.cli.Jar = jar
	}
	ret.cli.Transport = &xprt
	return ret
}
Esempio n. 19
0
// registerHTTPSProtocol calls Transport.RegisterProtocol but
// convering panics into errors.
func registerHTTPSProtocol(t *http.Transport, rt http.RoundTripper) (err error) {
	defer func() {
		if e := recover(); e != nil {
			err = fmt.Errorf("%v", e)
		}
	}()
	t.RegisterProtocol("https", rt)
	return nil
}
Esempio n. 20
0
File: http.go Progetto: lopaka/rsc
// newRawClient creates an http package Client taking into account both the parameters and package
// variables.
func newRawClient(noredirect bool) *http.Client {
	tr := http.Transport{ResponseHeaderTimeout: ResponseHeaderTimeout, Proxy: http.ProxyFromEnvironment}
	tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: NoCertCheck}
	c := http.Client{Transport: &tr}
	if noredirect {
		c.CheckRedirect = func(*http.Request, []*http.Request) error {
			return fmt.Errorf(noRedirectError)
		}
	}
	return &c
}
Esempio n. 21
0
// New returns a Supervisor interface type connected to the net.URL specified in u
//
// Optionally specify a http.Transport to use, will use default http.Transport if nil.
// This will also register a
func New(url string, transport *http.Transport) Supervisor {
	if transport == nil {
		transport = new(http.Transport)
	}

	transport.RegisterProtocol("unix", new(supervisorTransport))

	//xmlrpc.NewClient never returns an error
	client, _ := xmlrpc.NewClient(url, transport)
	return &supervisor{client}
}
Esempio n. 22
0
func TestAuthRequest(t *testing.T) {
	const backendResponse = "I am the backend"
	const backendStatus = 200
	mux := http.NewServeMux()
	mux.HandleFunc(serverConfig.ProtectPath, genericRequestHandlerAuthWrapper)
	mux.HandleFunc(serverConfig.CallbackPath, oauthCallbackHandler)
	server = httptest.NewServer(mux)

	backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if len(r.TransferEncoding) > 0 {
			t.Errorf("backend got unexpected TransferEncoding: %v", r.TransferEncoding)
		}
		if r.Header.Get("X-Forwarded-For") == "" {
			t.Errorf("didn't get X-Forwarded-For header")
		}

		w.Header().Set("X-Foo", "bar")
		http.SetCookie(w, &http.Cookie{Name: "flavor", Value: "chocolateChip"})
		w.WriteHeader(backendStatus)
		w.Write([]byte(backendResponse))
	}))
	defer backend.Close()
	backendURL, err := url.Parse(backend.URL)
	if err != nil {
		t.Fatal(err)
	}

	serverConfig.ProxyURL = *backendURL
	// Test request & response
	req, _ := http.NewRequest("GET", server.URL, nil)
	req.Host = "some-name"
	req.Header.Set("Connection", "close")
	req.Close = true

	// Handle the request
	transport := new(http.Transport)
	response, err := transport.RoundTrip(req)
	if err != nil {
		t.Fatalf("Get: %v", err)
	}
	if g, e := response.StatusCode, backendStatus; g != e {
		t.Errorf("got response.StatusCode %d; expected %d", g, e)
	}
	bodyBytes, _ := ioutil.ReadAll(response.Body)
	if g, e := string(bodyBytes), backendResponse; g != e {
		t.Errorf("got body %q; expected %q", g, e)
	}
	if g, e := response.Header.Get("X-Foo"), "bar"; g != e {
		t.Errorf("got X-Foo %q; expected %q", g, e)
	}
	if cookie := response.Cookies()[0]; cookie.Name != "flavor" {
		t.Errorf("unexpected cookie %q", cookie.Name)
	}
}
Esempio n. 23
0
// SetTransportDefaults applies the defaults from http.DefaultTransport
// for the Proxy, Dial, and TLSHandshakeTimeout fields if unset
func SetTransportDefaults(t *http.Transport) *http.Transport {
	if t.Proxy == nil {
		t.Proxy = defaultTransport.Proxy
	}
	if t.Dial == nil {
		t.Dial = defaultTransport.Dial
	}
	if t.TLSHandshakeTimeout == 0 {
		t.TLSHandshakeTimeout = defaultTransport.TLSHandshakeTimeout
	}
	return t
}
Esempio n. 24
0
func relayHttpRequest(
	client *http.Client,
	transport *http.Transport,
	request *http.Request,
	responseWriter http.ResponseWriter) {

	// Transform received request struct before using as input to relayed request
	request.Close = false
	request.RequestURI = ""
	for _, key := range hopHeaders {
		request.Header.Del(key)
	}

	// Relay the HTTP request and get the response. Use a client when supplied,
	// otherwise a transport. A client handles cookies and redirects, and a
	// transport does not.
	var response *http.Response
	var err error
	if client != nil {
		response, err = client.Do(request)
	} else {
		response, err = transport.RoundTrip(request)
	}

	if err != nil {
		NoticeAlert("%s", common.ContextError(FilterUrlError(err)))
		forceClose(responseWriter)
		return
	}
	defer response.Body.Close()

	// Relay the remote response headers
	for _, key := range hopHeaders {
		response.Header.Del(key)
	}
	for key, _ := range responseWriter.Header() {
		responseWriter.Header().Del(key)
	}
	for key, values := range response.Header {
		for _, value := range values {
			responseWriter.Header().Add(key, value)
		}
	}

	// Relay the response code and body
	responseWriter.WriteHeader(response.StatusCode)
	_, err = io.Copy(responseWriter, response.Body)
	if err != nil {
		NoticeAlert("%s", common.ContextError(err))
		forceClose(responseWriter)
		return
	}
}
Esempio n. 25
0
func Fanout(transport *http.Transport, request *http.Request, endpoints []*Endpoint) (*http.Response, error) {
	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		return nil, err
	}

	responses := make(chan *http.Response, len(endpoints))
	errs := make(chan error, len(endpoints))

	for _, e := range endpoints {
		url := *request.URL
		url.Scheme = "http"
		url.Host = e.Addr

		req := *request
		req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
		req.URL = &url
		req.Close = true

		request := &req

		go func() {
			response, err := transport.RoundTrip(request)

			if err != nil {
				errs <- err
			} else {
				responses <- response
			}
		}()

		defer transport.CancelRequest(request)
	}

	var fanoutErr error
	var response *http.Response

	for i := 0; i < len(endpoints); i++ {
		select {
		case fanoutErr = <-errs:
		case response = <-responses:
			if response.StatusCode < 400 {
				return response, nil
			}
		}
	}

	if response != nil {
		return response, nil
	}

	return nil, fanoutErr
}
Esempio n. 26
0
func main() {

	transport := new(http.Transport)
	transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}

	client := new(http.Client)

	client.Transport = transport

	url := "https://10.80.65.70:9443/rest/agent/1a5dc1ef-f0e6-4aad-839a-a7880d539e8d/log/9429c03e-a07a-48ef-8a74-a4c2ace992a2"
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		panic(err)
	}

	creds := convertCredentials("admin", "admin")

	req.Header.Add("Authorization", "Basic "+creds)

	resp, err := client.Do(req)
	if err != nil {
		panic(err)
	}

	//	bytes, err := ioutil.ReadAll(resp.Body)
	//	if err != nil {
	//		panic(err)
	//	}

	//temp := resp.Header["Content-Disposition"]
	//temp2 := string.Fields("[attachment; filename=")
	//temp2 := strings.Trim(temp, "[attachment; filename=")
	//fmt.Println(temp2)
	//fmt.Println(bytes)
	//var result map[string]interface{}
	//err = json.Unmarshal(bytes, &result)
	//if err != nil {
	//		panic(err)
	//	}
	//	fmt.Println(result)

	/*
		defer resp.Body.Close()
		out, err := os.Create("filename.tar.gz")
		if err != nil {
			panic(err)
		}
		defer out.Close()
		io.Copy(out, resp.Body)
		fmt.Println(out)
	*/
}
Esempio n. 27
0
// SetOldTransportDefaults applies the defaults from http.DefaultTransport
// for the Proxy, Dial, and TLSHandshakeTimeout fields if unset
func SetOldTransportDefaults(t *http.Transport) *http.Transport {
	if t.Proxy == nil || isDefault(t.Proxy) {
		// http.ProxyFromEnvironment doesn't respect CIDRs and that makes it impossible to exclude things like pod and service IPs from proxy settings
		// ProxierWithNoProxyCIDR allows CIDR rules in NO_PROXY
		t.Proxy = NewProxierWithNoProxyCIDR(http.ProxyFromEnvironment)
	}
	if t.Dial == nil {
		t.Dial = defaultTransport.Dial
	}
	if t.TLSHandshakeTimeout == 0 {
		t.TLSHandshakeTimeout = defaultTransport.TLSHandshakeTimeout
	}
	return t
}
Esempio n. 28
0
// ConfigureTCPTransport configures the specified Transport according to the
// specified proto and addr.
// If the proto is unix (using a unix socket to communicate) the compression
// is disabled.
func ConfigureTCPTransport(tr *http.Transport, proto, addr string) {
	// Why 32? See https://github.com/docker/docker/pull/8035.
	timeout := 32 * time.Second
	if proto == "unix" {
		// No need for compression in local communications.
		tr.DisableCompression = true
		tr.Dial = func(_, _ string) (net.Conn, error) {
			return net.DialTimeout(proto, addr, timeout)
		}
	} else {
		tr.Proxy = http.ProxyFromEnvironment
		tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
	}
}
Esempio n. 29
0
func RoundRobin(transport *http.Transport, request *http.Request, endpoints []*Endpoint) (*http.Response, error) {
	startingPoint := rand.Intn(len(endpoints))

	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		return nil, err
	}

	attempts := 0
	i := startingPoint

	for {
		if i == len(endpoints) {
			i = 0
		}

		url := *request.URL
		url.Scheme = "http"
		url.Host = endpoints[i].Addr

		req := *request
		req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
		req.URL = &url
		req.Close = true

		request := &req

		response, err := transport.RoundTrip(request)

		attempts++
		i++

		if attempts == len(endpoints) {
			return response, err
		}

		if err != nil {
			continue
		}

		if response.StatusCode < 400 {
			return response, nil
		}

		response.Body.Close()
	}

	panic("unreachable")
}
Esempio n. 30
0
// BenchmarkExpand may consume all open files in server. When running this
// test, you should configure max open files server can open. This can
// commonly be done through "ulimit -n 10000" shell command.
func BenchmarkExpand(b *testing.B) {
	b.StopTimer()
	shortReq, _ := json.Marshal(shortReq{LongURL: "http://www.google.com"})
	req, err := http.NewRequest(
		"POST",
		"http://127.0.0.1:3030/short",
		bytes.NewBuffer(shortReq))
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		b.Fatal(err)
	}
	if resp.StatusCode != http.StatusOK {
		b.Fatalf("http response status: %v", resp.StatusCode)
	}

	body, _ := ioutil.ReadAll(resp.Body)
	var shortResp shortResp
	json.Unmarshal(body, &shortResp)
	shortURL := shortResp.ShortURL
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		req, err := http.NewRequest(
			"GET",
			fmt.Sprintf("%v", shortURL),
			nil,
		)
		if err != nil {
			b.Fatal(err)
		}

		transport := http.Transport{}
		resp, err := transport.RoundTrip(req)

		if err != nil {
			b.Fatal(err)
		}

		if resp.StatusCode != http.StatusTemporaryRedirect {
			b.Log(shortURL)
			b.Log(resp.Request.URL)
			b.Fatalf("http response status: %v", resp.StatusCode)
		}
	}
}