Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit 18d2d47

Browse files
authored
Fix swagger scheme (#2221)
A fixup up for #2217 to properly adjust the schemes defined in swagger. Previously the X-Forwarded-Proto wasn't respected but now it is.
1 parent 633aee2 commit 18d2d47

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

main.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -411,21 +411,20 @@ func main() {
411411
func(res http.ResponseWriter, req *http.Request, url url.Values) {
412412
b, err := swagger.Asset("swagger.json")
413413
if err != nil {
414-
res.WriteHeader(404)
414+
res.WriteHeader(http.StatusNotFound)
415415
return
416416
}
417417

418418
s := string(b)
419419
// replace swagger host with host from request
420-
newHost := rest.AbsoluteURL(req, "")
421-
newHost = strings.Replace(newHost, "http://", "", -1)
422-
newHost = strings.Replace(newHost, "https://", "", -1)
423-
s = strings.Replace(s, `"host":"openshift.io"`, `"host":"`+newHost+`"`, -1)
424-
425-
// replace schemes in swagger with the current URL scheme
426-
if req.URL != nil && strings.ToLower(req.URL.Scheme) == "https" {
427-
s = strings.Replace(s, `"schemes":["http"]`, `"schemes":["https"]`, -1)
420+
u, err := rest.AbsoluteURLAsURL(req, "")
421+
if err != nil {
422+
res.WriteHeader(http.StatusInternalServerError)
423+
res.Write([]byte(err.Error()))
424+
return
428425
}
426+
s = strings.Replace(s, `"host":"openshift.io"`, `"host":"`+u.Host+`"`, -1)
427+
s = strings.Replace(s, `"schemes":["http"]`, `"schemes":["`+u.Scheme+`"]`, -1)
429428

430429
res.Header().Set("Access-Control-Allow-Origin", "*")
431430
res.Header().Set("Access-Control-Allow-Methods", "GET")

rest/url.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88
"net/http"
9+
"net/url"
910
"strings"
1011

1112
"github.com/fabric8-services/fabric8-wit/errors"
@@ -24,6 +25,12 @@ func AbsoluteURL(req *http.Request, relative string) string {
2425
return fmt.Sprintf("%s://%s%s", scheme, req.Host, relative)
2526
}
2627

28+
// AbsoluteURLAsURL returns the result of AbsoluteURL parsed into a URL
29+
// structure and a potential parsing error.
30+
func AbsoluteURLAsURL(req *http.Request, relative string) (*url.URL, error) {
31+
return url.Parse(AbsoluteURL(req, relative))
32+
}
33+
2734
// ReplaceDomainPrefix replaces the last name in the host by a new name. Example: api.service.domain.org -> sso.service.domain.org
2835
func ReplaceDomainPrefix(host string, replaceBy string) (string, error) {
2936
split := strings.SplitN(host, ".", 2)

0 commit comments

Comments
 (0)