Skip to content

Commit

Permalink
follow redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanp413 committed Apr 5, 2022
1 parent 3b3f86d commit 604554f
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions components/openvsx-proxy/pkg/modifyresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -98,20 +99,35 @@ func (o *OpenVSXProxy) ModifyResponse(r *http.Response) error {
newBody, err := r.Request.GetBody()
if err != nil {
log.WithFields(logFields).WithError(err).Error("error getting body for redirect")
return err
return nil
}

log.WithFields(logFields).Infof("Doing redirect request: %v", r.Header.Get("Location"))
redirectReq, err := http.NewRequestWithContext(r.Request.Context(), r.Request.Method, r.Header.Get("Location"), newBody)
if err != nil {
log.WithFields(logFields).WithError(err)
return nil
}
log.WithFields(logFields).Infof("Doing redirect request: %v", r.Header.Get("Location"))
r, err = o.client.Do(redirectReq)
if err != nil {
log.WithFields(logFields).WithError(err).Error("error doing redirect request")
return err
return nil
}

dumpreq, err := httputil.DumpRequest(redirectReq, false)
if err == nil {
log.WithFields(logFields).Infof("dump redirect request: %v", string(dumpreq))
}

dumpres, err := httputil.DumpResponse(r, false)
if err == nil {
log.WithFields(logFields).Infof("dump redirect resp: %v", string(dumpres))
}

rawBody, err = ioutil.ReadAll(r.Body)
if err != nil {
log.WithFields(logFields).WithError(err).Error("error reading redirect response raw body")
return err
return nil
}
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(rawBody))
Expand All @@ -124,12 +140,13 @@ func (o *OpenVSXProxy) ModifyResponse(r *http.Response) error {
gzipReader, err := gzip.NewReader(ioutil.NopCloser(bytes.NewBuffer(rawBody)))
if err != nil {
log.WithFields(logFields).WithError(err)
return nil
}

body, err = ioutil.ReadAll(gzipReader)
if err != nil {
log.WithFields(logFields).WithError(err).Error("error reading response body")
return err
return nil
}
gzipReader.Close()

Expand Down

0 comments on commit 604554f

Please sign in to comment.