Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from escaletech/feature/serve-images-via-https
Browse files Browse the repository at this point in the history
Override default transport for document requests
  • Loading branch information
rdumont authored May 8, 2019
2 parents 9418a34 + 7a602f9 commit 07ca1d5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
50 changes: 50 additions & 0 deletions proxy/document_transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package proxy

import (
"bytes"
"io"
"io/ioutil"
"net/http"
"regexp"
)

type bodyTransformation func(body io.ReadCloser, req *http.Request) (io.ReadCloser, error)

func newDocumentTransport(inner http.RoundTripper) *documentTransport {
return &documentTransport{inner, replaceImagesURLProtocol()}
}

type documentTransport struct {
transport http.RoundTripper
transformBody bodyTransformation
}

func (t *documentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
res, err := t.transport.RoundTrip(req)
if err != nil {
return nil, err
}

body, err := t.transformBody(res.Body, req)
if err != nil {
return nil, err
}

res.Body = body

return res, nil
}

var regex = regexp.MustCompile(`(?m)"url":"http(:[^"]+)"`)

func replaceImagesURLProtocol() bodyTransformation {
return func(body io.ReadCloser, req *http.Request) (io.ReadCloser, error) {
content, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
fixed := regex.ReplaceAllString(string(content), `"url":"https$1"`)

return ioutil.NopCloser(bytes.NewReader([]byte(fixed))), nil
}
}
42 changes: 42 additions & 0 deletions proxy/document_transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package proxy

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestDocumentTransport(t *testing.T) {
Convey("RoundTrip", t, func() {
Convey("transforms returned body", func() {
t := &documentTransport{
transport: roundTripReturningBody(http.StatusOK, `"image":{"url":"http://my-fake-image.jpeg"}`),
transformBody: replaceImagesURLProtocol(),
}

// Act
res, err := t.RoundTrip(httptest.NewRequest("GET", "http://target.com", nil))

// Assert
So(err, ShouldBeNil)
body, _ := ioutil.ReadAll(res.Body)
So(string(body), ShouldEqual, `"image":{"url":"https://my-fake-image.jpeg"}`)
})
})

}

func roundTripReturningBody(statusCode int, body string) http.RoundTripper {
return &fakeRoundTripper{
roundTrip: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Header: http.Header{},
StatusCode: statusCode,
Body: newBody(body),
}, nil
},
}
}
1 change: 1 addition & 0 deletions proxy/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func newRootHandler(backendURL string, cache httpcache.Cache) http.Handler {

func newDocumentsHandler(backendURL string, cache httpcache.Cache) http.Handler {
cachingTransport := httpcache.NewTransport(cache)
cachingTransport.Transport = newDocumentTransport(http.DefaultTransport)
httpClient := &http.Client{Transport: cachingTransport}

return newProxy(newRequestBuilder(backendURL, false), httpClient.Do, forwardResponse)
Expand Down
2 changes: 0 additions & 2 deletions proxy/root_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

const cacheDuration = 7 * 24 * 60 * 60 // 7 days

type bodyTransformation func(body io.ReadCloser, req *http.Request) (io.ReadCloser, error)

func newRootTransport(inner http.RoundTripper, backendURL string) *rootTransport {
return &rootTransport{inner, hostReplacer(backendURL)}
}
Expand Down

0 comments on commit 07ca1d5

Please sign in to comment.