Example #1
0
func compareResponses(t *testing.T, testName string, resp1, resp2 *http.Response) {
	resp1Bs, err := httputil.DumpResponse(resp1, true)
	require.NoError(t, err, "Dump response")
	resp2Bs, err := httputil.DumpResponse(resp2, true)
	require.NoError(t, err, "Dump response")
	assert.Equal(t, string(resp1Bs), string(resp2Bs), "%v: Response mismatch", testName)
}
Example #2
0
File: api.go Project: aut0/restic
// dumpHTTP - dump HTTP request and response.
func (c Client) dumpHTTP(req *http.Request, resp *http.Response) error {
	// Starts http dump.
	_, err := fmt.Fprintln(c.traceOutput, "---------START-HTTP---------")
	if err != nil {
		return err
	}

	// Filter out Signature field from Authorization header.
	c.filterSignature(req)

	// Only display request header.
	reqTrace, err := httputil.DumpRequestOut(req, false)
	if err != nil {
		return err
	}

	// Write request to trace output.
	_, err = fmt.Fprint(c.traceOutput, string(reqTrace))
	if err != nil {
		return err
	}

	// Only display response header.
	var respTrace []byte

	// For errors we make sure to dump response body as well.
	if resp.StatusCode != http.StatusOK &&
		resp.StatusCode != http.StatusPartialContent &&
		resp.StatusCode != http.StatusNoContent {
		respTrace, err = httputil.DumpResponse(resp, true)
		if err != nil {
			return err
		}
	} else {
		respTrace, err = httputil.DumpResponse(resp, false)
		if err != nil {
			return err
		}
	}
	// Write response to trace output.
	_, err = fmt.Fprint(c.traceOutput, strings.TrimSuffix(string(respTrace), "\r\n"))
	if err != nil {
		return err
	}

	// Ends the http dump.
	_, err = fmt.Fprintln(c.traceOutput, "---------END-HTTP---------")
	if err != nil {
		return err
	}

	// Returns success.
	return nil
}
Example #3
0
func TestServeLogs(t *testing.T) {
	fw := newServerTest()

	content := string(`<pre><a href="kubelet.log">kubelet.log</a><a href="google.log">google.log</a></pre>`)

	fw.fakeKubelet.logFunc = func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Header().Add("Content-Type", "text/html")
		w.Write([]byte(content))
	}

	resp, err := http.Get(fw.testHTTPServer.URL + "/logs/")
	if err != nil {
		t.Fatalf("Got error GETing: %v", err)
	}
	defer resp.Body.Close()

	body, err := httputil.DumpResponse(resp, true)
	if err != nil {
		// copying the response body did not work
		t.Errorf("Cannot copy resp: %#v", err)
	}
	result := string(body)
	if !strings.Contains(result, "kubelet.log") || !strings.Contains(result, "google.log") {
		t.Errorf("Received wrong data: %s", result)
	}
}
Example #4
0
File: sns.go Project: robfig/goamz
func (sns *SNS) query(topic *Topic, message *Message, params map[string]string, resp interface{}) error {
	params["Timestamp"] = time.Now().UTC().Format(time.RFC3339)
	u, err := url.Parse(sns.Region.SNSEndpoint)
	if err != nil {
		return err
	}

	sign(sns.Auth, "GET", "/", params, u.Host)
	u.RawQuery = multimap(params).Encode()
	if glog.V(1) {
		glog.V(1).Infoln("REQ:\n", u.String())
	}
	r, err := http.Get(u.String())
	if err != nil {
		return err
	}
	defer r.Body.Close()

	if glog.V(1) {
		dump, _ := httputil.DumpResponse(r, true)
		glog.V(1).Infoln("DUMP:\n", string(dump))
	}

	if r.StatusCode != 200 {
		return buildError(r)
	}
	err = xml.NewDecoder(r.Body).Decode(resp)
	return err
}
Example #5
0
func maybeDumpResponse(t *testing.T, resp *http.Response) {
	if d, err := httputil.DumpResponse(resp, true); err != nil {
		t.Logf("error dumping response: %v", err)
	} else {
		t.Logf("response:\n%s", string(d))
	}
}
Example #6
0
// Response - Trace HTTP Response
func (t Trace) Response(res *http.Response) (err error) {
	resTrace, err := httputil.DumpResponse(res, false) // Only display header
	if err == nil {
		console.Debug(string(resTrace))
	}
	return err
}
Example #7
0
func verifyRedirectSites(fb *client.ChainedServerInfo, c *http.Client, url string) {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Errorf("error make request to %s: %v", url, err)
		return
	}
	req.Header.Set("X-LANTERN-AUTH-TOKEN", fb.AuthToken)
	resp, err := c.Do(req)
	if err != nil {
		log.Errorf("%v: requesting %s failed: %v", fb.Addr, url, err)
		if *verbose {
			reqStr, _ := httputil.DumpRequestOut(req, true)
			log.Debug(string(reqStr))
		}
		return
	}
	defer func() {
		if err := resp.Body.Close(); err != nil {
			log.Debugf("Unable to close response body: %v", err)
		}
	}()
	if resp.StatusCode != 200 {
		log.Errorf("%v: bad status code %v for %s", fb.Addr, resp.StatusCode, url)
		if *verbose {
			respStr, _ := httputil.DumpResponse(resp, true)
			log.Debug(string(respStr))
		}
		return
	}
	log.Debugf("%v via %s: OK.", fb.Addr, url)
}
Example #8
0
func GithubCheckRateLimit(token string) GithubRateLimitModel {
	req, err := http.NewRequest("GET", GithubRateLimitUrl, nil)
	if err != nil {
		panic(err)
	}
	if token != "" {
		req.Header.Add("Authorization", fmt.Sprintf("bearer %s", token))
	}

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

	if resp.StatusCode != 200 {
		dump, err := httputil.DumpResponse(resp, true)
		if err != nil {
			panic(err)
		}
		panic(string(dump))
	}

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

	rateLimit := GithubRateLimitModel{}
	err = json.Unmarshal(rateLimitBytes, &rateLimit)
	if err != nil {
		panic(err)
	}
	spew.Dump(rateLimit)
	return rateLimit
}
Example #9
0
func (a *SMSAction) Do(n notices.Notice) (err error) {
	if mylog.Debugging {
		defer func() { mylog.Debugf("exit SMSAction.Do  %+v", err) }()
	}

	text := util.ExpandFromMap(a.ActionData.Template, n)
	mylog.Debug("SMSAction text to send:", text)
	msg := fmt.Sprintf(`{"to":["tel:%s"], "message": %q}`, a.Parameters["to"], text)
	mylog.Debug("SMSAction msg to send:", msg)
	req, err := http.NewRequest("POST", config.SMSEndpoint(), strings.NewReader(msg))
	if err != nil {
		mylog.Alert("SMSAction.Do", err)
	}
	req.Header.Add("API_KEY", config.APIKey())
	req.Header.Add("API_SECRET", config.APISecret())
	req.Header.Add("Content-Type", "application/json")
	mylog.Debug("SMSAction.Do msg:", msg)
	resp, err := httpClient.Do(req)
	if err != nil {
		mylog.Alert("SMSAction.Do", err)
		return err
	}
	respDump, err := httputil.DumpResponse(resp, true)
	mylog.Debugf("SMSAction.Do server response: (%q,%v)\n", string(respDump), err)
	return err
}
Example #10
0
File: main.go Project: kr/reqlog
func logResp(r *http.Response) {
	if dump, err := httputil.DumpResponse(r, true); err != nil {
		log.Println("reqlog: error dumping response:", err)
	} else {
		log.Println(string(dump))
	}
}
Example #11
0
func DumpResponse(response *http.Response) {
	out, err := httputil.DumpResponse(response, true)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%q\n", out)
}
Example #12
0
func DumpResponse(resp *http.Response, body bool) (dump []byte) {
	if body {
		FixResponse(resp)
		dump, _ = httputil.DumpResponse(resp, body)
	} else {
		var b bytes.Buffer

		text := resp.Status
		if text == "" {
			var ok bool
			text, ok = statusText[resp.StatusCode]
			if !ok {
				text = "status code " + strconv.Itoa(resp.StatusCode)
			}
		}
		protoMajor, protoMinor := strconv.Itoa(resp.ProtoMajor), strconv.Itoa(resp.ProtoMinor)
		statusCode := strconv.Itoa(resp.StatusCode) + " "
		text = strings.TrimPrefix(text, statusCode)
		io.WriteString(&b, "HTTP/"+protoMajor+"."+protoMinor+" "+statusCode+text+"\r\n")

		resp.Header.Write(&b)

		// End-of-header
		io.WriteString(&b, "\r\n")

		dump = b.Bytes()
	}
	return
}
Example #13
0
func MakeRequest(client OAuth2Client, req *http.Request) (*http.Response, *http.Request, error) {
	if req == nil || client == nil {
		return nil, nil, nil
	}
	if mockClient, ok := client.(MockClient); ok {
		resp, err := mockClient.HandleRequest(req)
		return resp, req, err
	}
	c := client.Client()
	if c == nil {
		c = new(http.Client)
	}
	if EnableLogHttpRequests {
		dump, _ := httputil.DumpRequest(req, true)
		log.Print("Making Request:", "\n=================================\n", string(dump), "=================================\n")
	}
	resp, err := c.Do(req)
	if EnableLogHttpResponses {
		if resp != nil {
			dump2, _ := httputil.DumpResponse(resp, true)
			log.Print("Received Response:", "\n=================================\n", string(dump2), "=================================\n")
		}
	}
	return resp, req, err
}
Example #14
0
func (c *conn) doRequest(proxyConn *connInfo, host string, op string, request *request) (resp *http.Response, err error) {
	var body io.Reader
	if request != nil {
		body = request.body
	}
	path := c.id + "/" + c.addr + "/" + op
	req, err := c.config.NewRequest(host, path, "POST", body)
	if err != nil {
		err = fmt.Errorf("Unable to construct request to %s via proxy %s: %s", c.addr, host, err)
		return
	}
	//req.Header.Set(X_ENPROXY_OP, op)
	// Always send our connection id
	//req.Header.Set(X_ENPROXY_ID, c.id)
	// Always send the address that we're trying to reach
	//req.Header.Set(X_ENPROXY_DEST_ADDR, c.addr)
	req.Header.Set("Content-type", "application/octet-stream")
	if request != nil && request.length > 0 {
		// Force identity encoding to appeas CDNs like Fastly that can't
		// handle chunked encoding on requests
		req.TransferEncoding = []string{"identity"}
		req.ContentLength = int64(request.length)
	} else {
		req.ContentLength = 0
	}

	err = req.Write(proxyConn.conn)
	if err != nil {
		err = fmt.Errorf("Error sending request to %s via proxy %s: %s", c.addr, host, err)
		return
	}

	resp, err = http.ReadResponse(proxyConn.bufReader, req)
	if err != nil {
		err = fmt.Errorf("Error reading response from proxy: %s", err)
		return
	}

	// Check response status
	responseOK := resp.StatusCode >= 200 && resp.StatusCode < 300
	if !responseOK {
		// This means we're getting something other than an OK response from the fronting provider
		// itself, which is odd. Try to log the entire response for easier debugging.
		full, er := httputil.DumpResponse(resp, true)
		if er == nil {
			err = fmt.Errorf("Bad response status for read from fronting provider: %s", string(full))
		} else {
			log.Errorf("Could not dump response: %v", er)
			err = fmt.Errorf("Bad response status for read from fronting provider: %s", resp.Status)
		}
		if err := resp.Body.Close(); err != nil {
			log.Debugf("Unable to close response body: %v", err)
		}
		resp = nil
	} else {
		log.Debugf("Got OK from fronting provider")
	}

	return
}
Example #15
0
func (fa Flightaware) UrlToResp(verb string, args map[string]string) (*http.Response, error) {
	var debug = false

	urlToCall := "http://flightxml.flightaware.com/json/FlightXML2/" + verb

	postArgs := url.Values{}
	for k, v := range args {
		postArgs.Set(k, v)
	}
	urlToCall += "?" + postArgs.Encode()

	if req, err := http.NewRequest("GET", urlToCall, nil); err != nil {
		return nil, err
	} else {
		req.SetBasicAuth(fa.APIUsername, fa.APIKey)
		if debug {
			bytes, _ := u.DumpRequest(req, true)
			fmt.Printf(">>>> req\n%s>>>>\n", string(bytes))
		}

		if resp, err := fa.Client.Do(req); err != nil {
			return nil, err
		} else {
			if debug {
				bytes, _ := u.DumpResponse(resp, true)
				fmt.Printf("<<<< resp\n%s<<<<\n", string(bytes))
			}

			return resp, nil
		}
	}
}
Example #16
0
// RoundTrip implements the RoundTripper interface.
func (t *TransportLogger) RoundTrip(req *http.Request) (res *http.Response, err error) {
	transport := t.Transport

	if transport == nil {
		transport = http.DefaultTransport
	}

	writer := t.Writer

	if writer == nil {
		writer = os.Stdout
	}

	reqDump, err := httputil.DumpRequestOut(req, true)

	writer.Write(reqDump)
	writer.Write([]byte("\n"))

	res, err = transport.RoundTrip(req)

	if err != nil {
		return
	}

	resDump, err := httputil.DumpResponse(res, true)

	writer.Write(resDump)
	writer.Write([]byte("\n"))

	return
}
Example #17
0
func (s *Service) AddDebugHandlers() {
	out := s.Config.Logger
	if s.Config.LogLevel == 0 {
		return
	}

	s.Handlers.Sign.PushBack(func(r *Request) {
		dumpedBody, _ := httputil.DumpRequest(r.HTTPRequest, true)

		fmt.Fprintf(out, "=> [%s] %s.%s(%+v)\n", r.Time,
			r.Service.ServiceName, r.Operation.Name, r.Params)
		fmt.Fprintf(out, "---[ REQUEST PRE-SIGN ]------------------------------\n")
		fmt.Fprintf(out, "%s\n", string(dumpedBody))
		fmt.Fprintf(out, "-----------------------------------------------------\n")
	})
	s.Handlers.Send.PushFront(func(r *Request) {
		dumpedBody, _ := httputil.DumpRequestOut(r.HTTPRequest, true)

		fmt.Fprintf(out, "---[ REQUEST POST-SIGN ]-----------------------------\n")
		fmt.Fprintf(out, "%s\n", string(dumpedBody))
		fmt.Fprintf(out, "-----------------------------------------------------\n")
	})
	s.Handlers.Send.PushBack(func(r *Request) {
		fmt.Fprintf(out, "---[ RESPONSE ]--------------------------------------\n")
		if r.HTTPResponse != nil {
			dumpedBody, _ := httputil.DumpResponse(r.HTTPResponse, true)
			fmt.Fprintf(out, "%s\n", string(dumpedBody))
		} else if r.Error != nil {
			fmt.Fprintf(out, "%s\n", r.Error)
		}
		fmt.Fprintf(out, "-----------------------------------------------------\n")
	})
}
Example #18
0
// Issue 15366
func TestH12_AutoGzipWithDumpResponse(t *testing.T) {
	h12Compare{
		Handler: func(w ResponseWriter, r *Request) {
			h := w.Header()
			h.Set("Content-Encoding", "gzip")
			h.Set("Content-Length", "23")
			h.Set("Connection", "keep-alive")
			io.WriteString(w, "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\x00s\xf3\xf7\a\x00\xab'\xd4\x1a\x03\x00\x00\x00")
		},
		EarlyCheckResponse: func(proto string, res *Response) {
			if !res.Uncompressed {
				t.Errorf("%s: expected Uncompressed to be set", proto)
			}
			dump, err := httputil.DumpResponse(res, true)
			if err != nil {
				t.Errorf("%s: DumpResponse: %v", proto, err)
				return
			}
			if strings.Contains(string(dump), "Connection: close") {
				t.Errorf("%s: should not see \"Connection: close\" in dump; got:\n%s", proto, dump)
			}
			if !strings.Contains(string(dump), "FOO") {
				t.Errorf("%s: should see \"FOO\" in response; got:\n%s", proto, dump)
			}
		},
	}.run(t)
}
Example #19
0
func (get *GoGet) Download(i int) {
	defer get.WG.Done()
	if get.DownloadRange[i][0] > get.DownloadRange[i][1] {
		return
	}
	range_i := fmt.Sprintf("%d-%d", get.DownloadRange[i][0], get.DownloadRange[i][1])
	log.Printf("Download #%d bytes %s.\n", i, range_i)

	defer get.TempFiles[i].Close()

	req, err := http.NewRequest("GET", get.Url, nil)
	req.Header.Set("Range", "bytes="+range_i)
	resp, err := get.GetClient.Do(req)
	defer resp.Body.Close()
	if err != nil {
		log.Printf("Download #%d error %v.\n", i, err)
	} else {
		cnt, err := io.Copy(get.TempFiles[i], resp.Body)
		if cnt == int64(get.DownloadRange[i][1]-get.DownloadRange[i][0]+1) {
			log.Printf("Download #%d complete.\n", i)
		} else {
			req_dump, _ := httputil.DumpRequest(req, false)
			resp_dump, _ := httputil.DumpResponse(resp, true)
			log.Panicf("Download error %d %v, expect %d-%d, but got %d.\nRequest: %s\nResponse: %s\n", resp.StatusCode, err, get.DownloadRange[i][0], get.DownloadRange[i][1], cnt, string(req_dump), string(resp_dump))
		}
	}
}
Example #20
0
func (m *BuildClient) _complete_request(method string,
	url string, data []byte) ([]byte, error) {
	var req *http.Request
	if data != nil {
		req, _ = http.NewRequest(method, url, bytes.NewBuffer(data))
	} else {
		req, _ = http.NewRequest(method, url, nil)
	}

	m.SetAuthHeader(req)
	req.Header.Set("Content-Type", "application/json")
	resp, err := m.http_client.Do(req)
	if resp.StatusCode == http.StatusGatewayTimeout {
		return nil, new(Timeout)
	}

	if err == nil {
		dump, err := httputil.DumpResponse(resp, true)
		logging.Debug(string(dump))
		full_response, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return full_response, err
		}
		return full_response, nil
	} else {
		return nil, err
	}
}
Example #21
0
func (c *BClient) get(url string, data, urlData interface{}) error {
	rUrl := getUrl(url, urlData)
	req, _ := http.NewRequest("GET", rUrl, nil)

	signDataRequest(req, c.accessToken, c.ConsumerKey, c.ConsumerSecret)
	if debugOn {
		debug(httputil.DumpRequestOut(req, true))
	}

	resp, err := http.DefaultClient.Do(req)
	defer resp.Body.Close()

	if debugOn {
		debug(httputil.DumpResponse(resp, true))
		log.Println("response code:", resp.StatusCode)
	}

	if resp.StatusCode != 200 {
		log.Println("response code:", resp.StatusCode, resp.Status)
		return errors.New("Http Status != 200")
	}

	if err != nil {
		return err
	}
	return json.NewDecoder(resp.Body).Decode(data)
}
Example #22
0
func TestClientAccess(t *testing.T) {
	// establish a context
	ctx, r := initClientContext(t, "GET", "/access/", nil)
	defer ctx.Release()

	resp, err := http.DefaultClient.Do(r)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()

	out, err := httputil.DumpResponse(resp, true)
	if err != nil {
		t.Fatal(err)
	}

	bodybytes, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}
	if string(bodybytes) != "OK" {
		t.Fatalf(
			"Test failed: unexpected response: url:%s, code:%v, response:\n%s",
			r.URL.String(), resp.StatusCode, string(out))
	}
}
Example #23
0
// AddDebugHandlers injects debug logging handlers into the service to log request
// debug information.
func (s *Service) AddDebugHandlers() {
	out := s.Config.Logger
	if s.Config.LogLevel == 0 {
		return
	}

	s.Handlers.Send.PushFront(func(r *Request) {
		logBody := r.Config.LogHTTPBody
		dumpedBody, _ := httputil.DumpRequestOut(r.HTTPRequest, logBody)

		fmt.Fprintf(out, "---[ REQUEST POST-SIGN ]-----------------------------\n")
		fmt.Fprintf(out, "%s\n", string(dumpedBody))
		fmt.Fprintf(out, "-----------------------------------------------------\n")
	})
	s.Handlers.Send.PushBack(func(r *Request) {
		fmt.Fprintf(out, "---[ RESPONSE ]--------------------------------------\n")
		if r.HTTPResponse != nil {
			logBody := r.Config.LogHTTPBody
			dumpedBody, _ := httputil.DumpResponse(r.HTTPResponse, logBody)
			fmt.Fprintf(out, "%s\n", string(dumpedBody))
		} else if r.Error != nil {
			fmt.Fprintf(out, "%s\n", r.Error)
		}
		fmt.Fprintf(out, "-----------------------------------------------------\n")
	})
}
Example #24
0
func (h *Http) readResponses(tee *conn.Tee, lastTxn chan *HttpTxn) {
	for {
		var err error
		txn := <-lastTxn
		resp, err := http.ReadResponse(tee.ReadBuffer(), txn.Req.Request)
		txn.Duration = time.Since(txn.Start)
		h.reqTimer.Update(txn.Duration)
		if err != nil {
			tee.Warn("Error reading response from server: %v", err)
			// no more responses to be read, we're done
			break
		}
		// make sure we read the body of the response so that
		// we don't block the reader
		_, _ = httputil.DumpResponse(resp, true)

		txn.Resp = &HttpResponse{Response: resp}
		txn.Resp.BodyBytes, txn.Resp.Body, err = extractBody(resp.Body)
		if err != nil {
			tee.Warn("Failed to extract response body: %v", err)
		}

		h.Txns.In() <- txn
	}
}
Example #25
0
// resp = response structure that will get inflated by XML unmarshaling.
func (ec2 *EC2) query(params map[string]string, resp interface{}) error {

	req, err := http.NewRequest("GET", ec2.Region.EC2Endpoint, nil)
	if err != nil {
		return err
	}

	// Add the params passed in to the query string
	query := req.URL.Query()
	for varName, varVal := range params {
		query.Add(varName, varVal)
	}
	query.Add("Timestamp", timeNow().In(time.UTC).Format(time.RFC3339))
	req.URL.RawQuery = query.Encode()

	ec2.Region.Sign(req, ec2.Auth)

	r, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer r.Body.Close()

	if debug {
		dump, _ := httputil.DumpResponse(r, true)
		log.Printf("response:\n")
		log.Printf("%v\n}\n", string(dump))
	}
	if r.StatusCode != 200 {
		return buildError(r)
	}
	return xml.NewDecoder(r.Body).Decode(resp)
}
Example #26
0
func DumpResponse(resp *http.Response, name string) {
	dump, err := httputil.DumpResponse(resp, true)
	if err != nil {
		log.WithField("tag", TAG).Errorf("log failed for get Request")
	}
	ioutil.WriteFile(name, dump, 0777)
}
Example #27
0
func Deal(keyword string) (resp string) {
	//url := "http://qt.gtimg.cn/q=sh000001,sz399001"
	url := "http://qt.gtimg.cn/q=sh000001"

	response, err := http.Get(url)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if response.Status != "200 OK" {
		fmt.Println(response.Status)
		return
	}

	b, _ := httputil.DumpResponse(response, false)
	fmt.Println("dump")
	fmt.Print(string(b))

	data, _ := ioutil.ReadAll(response.Body)

	s := string(data)
	fmt.Println("ReadAll")
	fmt.Printf("%s", s)
	fmt.Println("ReadAll end")

	fmt.Println("Decode start")
	//todo, who can help in here?
	fmt.Println("Decode end")

	return s
}
Example #28
0
func (m *Meta) WriteTo(w io.Writer) (nr int64, err error) {
	if m.req != nil {
		fprintf(&nr, &err, w, "Type: request\r\n")
	} else if m.resp != nil {
		fprintf(&nr, &err, w, "Type: response\r\n")
	}
	fprintf(&nr, &err, w, "ReceivedAt: %v\r\n", m.t)
	fprintf(&nr, &err, w, "Session: %d\r\n", m.sess)
	fprintf(&nr, &err, w, "From: %v\r\n", m.from)
	if m.err != nil {
		// note the empty response
		fprintf(&nr, &err, w, "Error: %v\r\n\r\n\r\n\r\n", m.err)
	} else if m.req != nil {
		fprintf(&nr, &err, w, "\r\n")
		buf, err2 := httputil.DumpRequest(m.req, false)
		if err2 != nil {
			return nr, err2
		}
		write(&nr, &err, w, buf)
	} else if m.resp != nil {
		fprintf(&nr, &err, w, "\r\n")
		buf, err2 := httputil.DumpResponse(m.resp, false)
		if err2 != nil {
			return nr, err2
		}
		write(&nr, &err, w, buf)
	}
	return
}
Example #29
0
File: ec2.go Project: rgarcia/goamz
func (ec2 *EC2) query(params map[string]string, resp interface{}) error {
	params["Version"] = "2013-02-01"
	params["Timestamp"] = timeNow().In(time.UTC).Format(time.RFC3339)
	endpoint, err := url.Parse(ec2.Region.EC2Endpoint)
	if err != nil {
		return err
	}
	if endpoint.Path == "" {
		endpoint.Path = "/"
	}
	sign(ec2.Auth, "GET", endpoint.Path, params, endpoint.Host)
	endpoint.RawQuery = multimap(params).Encode()
	if debug {
		log.Printf("get { %v } -> {\n", endpoint.String())
	}
	r, err := http.Get(endpoint.String())
	if err != nil {
		return err
	}
	defer r.Body.Close()

	if debug {
		dump, _ := httputil.DumpResponse(r, true)
		log.Printf("response:\n")
		log.Printf("%v\n}\n", string(dump))
	}
	if r.StatusCode != 200 {
		return buildError(r)
	}
	err = xml.NewDecoder(r.Body).Decode(resp)
	return err
}
Example #30
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(c *http.Client, hreq *http.Request, resp interface{}) (*http.Response, error) {

	if true {
		log.Printf("%s %s ...\n", hreq.Method, hreq.URL.String())
	}
	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
}