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 1da7988
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions components/openvsx-proxy/pkg/modifyresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -95,23 +97,42 @@ func (o *OpenVSXProxy) ModifyResponse(r *http.Response) error {

// no error (status code < 500)
if r.StatusCode >= 300 && r.StatusCode < 400 && o.Config.FollowRedirects > 0 && r.Header.Get("Location") != "" {
newBody, err := r.Request.GetBody()
if err != nil {
log.WithFields(logFields).WithError(err).Error("error getting body for redirect")
return err
var newBody io.ReadCloser
if r.Request.Body != nil {
rawReqBody, err := ioutil.ReadAll(r.Request.Body)
if err != nil {
log.WithFields(logFields).WithError(err).Error("error reading request raw body")
return nil
}
newBody = ioutil.NopCloser(bytes.NewBuffer(rawReqBody))
}

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, true)
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 +145,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 1da7988

Please sign in to comment.