httptrace first commit

This commit is contained in:
Anand Babu (AB) Periasamy
2015-03-11 01:24:39 -07:00
parent 74c42cccf2
commit ba0fdcaa33
6 changed files with 113 additions and 17 deletions
+4 -4
View File
@@ -108,9 +108,9 @@ func doConfigure(c *cli.Context) {
info(msg)
}
// NewClient - get new client
func getNewClient(config *mcConfig) (*s3.Client, error) {
return &s3.Client{
Auth: &config.S3.Auth,
Transport: http.DefaultTransport,
}, nil
var trace s3Trace
s3client := s3.NewClient(&config.S3.Auth, http.DefaultTransport, trace)
return s3client, nil
}
+5
View File
@@ -65,6 +65,11 @@ func (a bySize) Less(i, j int) bool { return a[i].Size < a[j].Size }
func (c *Client) ListBuckets() ([]*Bucket, error) {
req := newReq(c.endpoint() + "/")
c.Auth.signRequest(req)
if c.Trace != nil {
c.Trace.Request(req)
}
res, err := c.transport().RoundTrip(req)
if err != nil {
return nil, err
+15 -12
View File
@@ -55,12 +55,6 @@ const (
MaxKeys = 1000
)
// Client is an Amazon S3 client.
type Client struct {
*Auth
Transport http.RoundTripper // or nil for the default
}
// Bucket - carries s3 bucket reply header
type Bucket struct {
Name string
@@ -90,6 +84,21 @@ type listBucketResults struct {
CommonPrefixes []*Prefix
}
// Client is an Amazon S3 client.
type Client struct {
*Auth
Transport http.RoundTripper // or nil for the default
Trace HttpTracer // if not nil, Tracing will be enabled
}
func NewClient(auth *Auth, transport http.RoundTripper, trace HttpTracer) *Client {
return &Client{
Auth: auth,
Transport: transport,
Trace: trace,
}
}
func (c *Client) transport() http.RoundTripper {
if c.Transport != nil {
return c.Transport
@@ -158,12 +167,6 @@ func parseListAllMyBuckets(r io.Reader) ([]*Bucket, error) {
return res.Buckets.Bucket, nil
}
// NewClient - get new client
func NewClient(auth *Auth) (client *Client) {
client = &Client{auth, http.DefaultTransport}
return
}
// IsValidBucket reports whether bucket is a valid bucket name, per Amazon's naming restrictions.
// See http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
func IsValidBucket(bucket string) bool {
+26
View File
@@ -0,0 +1,26 @@
/*
* Mini Object Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package s3
import (
"net/http"
)
type HttpTracer interface {
Request(req *http.Request)
Response(res *http.Response)
}
+1 -1
View File
@@ -67,6 +67,6 @@ func (c *xmlTime) UnmarshalXMLAttr(attr xml.Attr) error {
return nil
}
func (c *xmlTime) format() string {
func (c *xmlTime) String() string {
return c.Time.Format(iso8601Format)
}
+62
View File
@@ -0,0 +1,62 @@
/*
* Mini Object Storage, (C) 2014,2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"io"
"net/http"
"net/http/httputil"
)
type s3Trace struct {
BodyTraceFlag bool // Include Body
RequestTransportFlag bool // Include additional http.Transport adds such as User-Agent
Writer io.Writer // Console device to write to
}
// Trace HTTP Request
func (t s3Trace) Request(req *http.Request) {
if t.RequestTransportFlag {
reqTrace, err := httputil.DumpRequestOut(req, t.BodyTraceFlag)
if err == nil {
t.trace(reqTrace)
}
} else {
reqTrace, err := httputil.DumpRequest(req, t.BodyTraceFlag)
if err == nil {
t.trace(reqTrace)
}
}
}
// Trace HTTP Response
func (t s3Trace) Response(res *http.Response) {
resTrace, err := httputil.DumpResponse(res, t.BodyTraceFlag)
if err == nil {
t.trace(resTrace)
}
}
// Trace HTTP Response
func (t s3Trace) trace(data []byte) {
if t.Writer != nil {
fmt.Fprintf(t.Writer, "%s\n", data)
} else {
fmt.Printf("%s\n", data)
}
}