Skip to content

Commit

Permalink
* Refactored methods
Browse files Browse the repository at this point in the history
* go format
  • Loading branch information
dberth committed Nov 21, 2016
1 parent 53de6cc commit 13f46bb
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions composition/composition_handler.go
Expand Up @@ -61,13 +61,8 @@ func (agg *CompositionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
for _, res := range results {
if res.Err == nil && res.Content != nil {

if agg.handleForwardingRequests(res, w, r) {
// Return if it's a forwarded request
return
}

if agg.handleRequestsWithBody(res, w, r) {
// Return if it's a request body
// Handle responses with 30x status code or with response bodies
if agg.handleNonMergeableResponses(res, w, r) {
return
}

Expand All @@ -83,6 +78,8 @@ func (agg *CompositionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

status := agg.extractStatusCode(results, w, r)

agg.copyHeadersIfNeeded(results, w, r)

// Overwrite Content-Type to ensure, that the encoding is correct
w.Header().Set("Content-Type", "text/html; charset=utf-8")

Expand All @@ -97,17 +94,37 @@ func (agg *CompositionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
w.Write(html)
}

func (agg *CompositionHandler) handleNonMergeableResponses(result *FetchResult, w http.ResponseWriter, r *http.Request) bool {

if agg.handle30xResponses(result, w, r) {
// Return if it's a forwarded status code
return true
}

if agg.handleStreamResponses(result, w, r) {
// Return if it's a response with body
return true
}

return false
}

func (agg *CompositionHandler) extractStatusCode(results []*FetchResult, w http.ResponseWriter, r *http.Request) (statusCode int) {
// Take status code and headers from first fetch definition
if len(results) > 0 {
copyHeaders(results[0].Content.HttpHeader(), w.Header(), ForwardResponseHeaders)
if results[0].Content.HttpStatusCode() != 0 {
return results[0].Content.HttpStatusCode()
}
}
return 200
}

func (agg *CompositionHandler) copyHeadersIfNeeded(results []*FetchResult, w http.ResponseWriter, r *http.Request) {
// Take status code and headers from first fetch definition
if len(results) > 0 {
copyHeaders(results[0].Content.HttpHeader(), w.Header(), ForwardResponseHeaders)
}
}

func (agg *CompositionHandler) processHtml(mergeContext ContentMerger, w http.ResponseWriter, r *http.Request) ([]byte, bool) {
html, err := mergeContext.GetHtml()
if err != nil {
Expand Down Expand Up @@ -140,7 +157,7 @@ func (agg *CompositionHandler) handleEmptyFetcher(fetcher FetchResultSupplier, w
return false
}

func (agg *CompositionHandler) handleForwardingRequests(result *FetchResult, w http.ResponseWriter, r *http.Request) bool {
func (agg *CompositionHandler) handle30xResponses(result *FetchResult, w http.ResponseWriter, r *http.Request) bool {
if result.Content.HttpStatusCode() >= 300 && result.Content.HttpStatusCode() <= 308 {
copyHeaders(result.Content.HttpHeader(), w.Header(), ForwardResponseHeaders)
w.WriteHeader(result.Content.HttpStatusCode())
Expand All @@ -149,7 +166,7 @@ func (agg *CompositionHandler) handleForwardingRequests(result *FetchResult, w h
return false
}

func (agg *CompositionHandler) handleRequestsWithBody(result *FetchResult, w http.ResponseWriter, r *http.Request) bool {
func (agg *CompositionHandler) handleStreamResponses(result *FetchResult, w http.ResponseWriter, r *http.Request) bool {
if result.Content.Reader() != nil {
copyHeaders(result.Content.HttpHeader(), w.Header(), ForwardResponseHeaders)
w.WriteHeader(result.Content.HttpStatusCode())
Expand Down

0 comments on commit 13f46bb

Please sign in to comment.