@@ -144,6 +144,8 @@ func loadCAKeys() error {
144144 return nil
145145}
146146
147+ var awsHostnameRegexp = regexp .MustCompile (`^.*\.amazonaws\.com(?:\.cn)?$` )
148+
147149func createProxy (addr string ) {
148150 err := loadCAKeys ()
149151 if err != nil {
@@ -153,17 +155,12 @@ func createProxy(addr string) {
153155 proxy := goproxy .NewProxyHttpServer ()
154156 proxy .Logger = log .New (io .Discard , "" , log .LstdFlags )
155157 proxy .OnRequest ().HandleConnect (goproxy .AlwaysMitm )
156- proxy .OnRequest ().DoFunc (func (req * http.Request , ctx * goproxy.ProxyCtx ) (* http.Request , * http.Response ) { // TODO: Move to onResponse for HTTP response codes
157- body , _ := ioutil .ReadAll (req .Body )
158-
159- isAWSHostname , _ := regexp .MatchString (`^.*\.amazonaws\.com(?:\.cn)?$` , req .Host )
160- if isAWSHostname {
161- handleAWSRequest (req , body , 200 )
158+ proxy .OnResponse ().DoFunc (func (resp * http.Response , ctx * goproxy.ProxyCtx ) * http.Response {
159+ if awsHostnameRegexp .MatchString (resp .Request .Host ) {
160+ handleAWSRequest (resp )
162161 }
163162
164- req .Body = ioutil .NopCloser (bytes .NewBuffer (body ))
165-
166- return req , nil
163+ return resp
167164 })
168165 log .Fatal (http .ListenAndServe (addr , proxy ))
169166}
@@ -278,9 +275,12 @@ type ActionCandidate struct {
278275 Operation ServiceOperation
279276}
280277
281- func handleAWSRequest (req * http.Request , body []byte , respCode int ) {
278+ func handleAWSRequest (resp * http.Response ) {
279+ req := resp .Request
282280 host := req .Host
283- uri := req .RequestURI
281+
282+ body , _ := ioutil .ReadAll (req .Body )
283+ req .Body = ioutil .NopCloser (bytes .NewBuffer (body ))
284284
285285 var endpointUriPrefix string
286286 var service string
@@ -391,21 +391,17 @@ func handleAWSRequest(req *http.Request, body []byte, respCode int) {
391391 }
392392 } else if serviceDef .Metadata .Protocol == "rest-json" || serviceDef .Metadata .Protocol == "rest-xml" {
393393 // URL param schema
394- urlobj , err := url .ParseRequestURI (uri )
395- if err != nil {
396- return
397- }
398- vals := urlobj .Query ()
394+ vals := req .URL .Query ()
399395
400396 actionCandidates := []ActionCandidate {}
401397
402398 // path part
403399 OperationLoop:
404400 for operationName , operation := range serviceDef .Operations {
405- path := urlobj .Path
401+ path := req . URL .Path
406402 if serviceDef .Metadata .EndpointPrefix == "s3" && strings .HasPrefix (operation .Http .RequestURI , "/{Bucket}" ) && endpointUriPrefix != "" { // https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html#VirtualHostingSpecifyBucket
407- if len (urlobj .Path ) > 1 {
408- path = "/" + endpointUriPrefix + "/" + urlobj .Path [1 :]
403+ if len (req . URL .Path ) > 1 {
404+ path = "/" + endpointUriPrefix + "/" + req . URL .Path [1 :]
409405 } else {
410406 path = "/" + endpointUriPrefix
411407 }
@@ -585,7 +581,7 @@ func handleAWSRequest(req *http.Request, body []byte, respCode int) {
585581 Method : action ,
586582 Parameters : params ,
587583 URIParameters : uriparams ,
588- FinalHTTPStatusCode : respCode ,
584+ FinalHTTPStatusCode : resp . StatusCode ,
589585 })
590586
591587 handleLoggedCall ()
0 commit comments