| 1 |  |  | package config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 	"bytes" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | 	"context" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 	"fmt" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | 	"io/ioutil" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | 	"math/rand" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | 	"net/http" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | 	"net/http/httputil" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | 	"time" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | // These debugging codes are copeid and modified from https://github.com/motemen/go-loghttp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | // Copyright (c) 2014 motemen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | var _ http.RoundTripper = &DebugTransport{} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | // DebugTransport implements http.RoundTripper and showing request/response contents. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | type DebugTransport struct { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	Transport http.RoundTripper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | var DefaultDebugTransport = &DebugTransport{ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	Transport: http.DefaultTransport, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | type contextKey struct{} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | var contextKeyRequestStart = &contextKey{} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	ctx := context.WithValue(req.Context(), contextKeyRequestStart, time.Now()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	req = req.WithContext(ctx) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	num := rand.Int63() // #nosec G404 | this number is used only for debugging and readability | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	t.showDumpRequest(req, num) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	resp, err := t.transport().RoundTrip(req) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	if err != nil { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		return resp, err | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	t.showDumpResponse(resp, num) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	return resp, err | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | // showDumpRequest shows dumps of requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | func (t *DebugTransport) showDumpRequest(req *http.Request, num int64) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	if req == nil || req.Body == nil { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	body, err := ioutil.ReadAll(req.Body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	if err != nil { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	req.Body = ioutil.NopCloser(bytes.NewReader(body)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	dump, err := httputil.DumpRequest(req, false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	if err != nil { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		return | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	fmt.Printf("---- [[AWS Request (#%d)]] ----\n[[Request Header]]\n%s[[Request Body]]\n%s\n\n", num, string(dump), body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  | func (t *DebugTransport) showDumpResponse(resp *http.Response, num int64) { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  | 	if resp == nil || resp.Body == nil { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  | 		return | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 	body, err := ioutil.ReadAll(resp.Body) | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 	if err != nil { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 		return | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 	defer resp.Body.Close() | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 	resp.Body = ioutil.NopCloser(bytes.NewReader(body)) | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 	dump, err := httputil.DumpResponse(resp, false) | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 	if err != nil { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 		return | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 	fmt.Printf("---- [[AWS Response (#%d)]] ----\n[[Response Header]]\n%s[[Response Body]]\n%s\n\n\n", num, string(dump), body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | func (t *DebugTransport) transport() http.RoundTripper { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	if t.Transport != nil { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		return t.Transport | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	return http.DefaultTransport | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |