From ba0fdcaa335e69977d3c86512bc7591f57de1160 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Tue, 10 Mar 2015 01:47:23 -0700 Subject: [PATCH] httptrace first commit --- configure.go | 8 +++--- pkg/s3/bucket.go | 5 ++++ pkg/s3/client.go | 27 +++++++++++--------- pkg/s3/httptrace.go | 26 +++++++++++++++++++ pkg/s3/xmltime.go | 2 +- s3-trace.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 pkg/s3/httptrace.go create mode 100644 s3-trace.go diff --git a/configure.go b/configure.go index 86464e3c..9cab59ce 100644 --- a/configure.go +++ b/configure.go @@ -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 } diff --git a/pkg/s3/bucket.go b/pkg/s3/bucket.go index ec92ef14..1ebc5af3 100644 --- a/pkg/s3/bucket.go +++ b/pkg/s3/bucket.go @@ -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 diff --git a/pkg/s3/client.go b/pkg/s3/client.go index 47687ffb..5d380377 100644 --- a/pkg/s3/client.go +++ b/pkg/s3/client.go @@ -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 { diff --git a/pkg/s3/httptrace.go b/pkg/s3/httptrace.go new file mode 100644 index 00000000..1148e3a5 --- /dev/null +++ b/pkg/s3/httptrace.go @@ -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) +} diff --git a/pkg/s3/xmltime.go b/pkg/s3/xmltime.go index 623deed2..3f7cc4fc 100644 --- a/pkg/s3/xmltime.go +++ b/pkg/s3/xmltime.go @@ -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) } diff --git a/s3-trace.go b/s3-trace.go new file mode 100644 index 00000000..03d5c02a --- /dev/null +++ b/s3-trace.go @@ -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) + } +}