Commit 1b0af335 authored by Jacob Vosmaer (GitLab)'s avatar Jacob Vosmaer (GitLab)

Merge branch '90-parse-content-types' into 'master'

Add helper.IsContentType and use it everywhere

Closes #90

See merge request !114
parents 2de1f1e3 b03e9b9f
......@@ -6,7 +6,6 @@ import (
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
"strconv"
......@@ -288,6 +287,5 @@ func bufferResponse(r io.Reader) (*bytes.Buffer, error) {
}
func validResponseContentType(resp *http.Response) bool {
parsed, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
return err == nil && parsed == ResponseContentType
return helper.IsContentType(ResponseContentType, resp.Header.Get("Content-Type"))
}
......@@ -44,7 +44,7 @@ func (b *blocker) WriteHeader(status int) {
return
}
if b.Header().Get("Content-Type") == ResponseContentType {
if helper.IsContentType(ResponseContentType, b.Header().Get("Content-Type")) {
b.status = 500
b.Header().Del("Content-Length")
b.hijacked = true
......
......@@ -3,6 +3,7 @@ package helper
import (
"errors"
"log"
"mime"
"net"
"net/http"
"net/url"
......@@ -160,3 +161,8 @@ func SetForwardedFor(newHeaders *http.Header, originalRequest *http.Request) {
newHeaders.Set("X-Forwarded-For", header)
}
}
func IsContentType(expected, actual string) bool {
parsed, _, err := mime.ParseMediaType(actual)
return err == nil && parsed == expected
}
package upstream
import (
"mime"
"net/http"
"path"
"regexp"
......@@ -67,8 +66,7 @@ func wsRoute(regexpStr string, handler http.Handler, matchers ...matcherFunc) ro
// Creates matcherFuncs for a particular content type.
func isContentType(contentType string) func(*http.Request) bool {
return func(r *http.Request) bool {
parsed, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
return err == nil && contentType == parsed
return helper.IsContentType(contentType, r.Header.Get("Content-Type"))
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment