Skip to content

Commit

Permalink
Merge pull request #10 from issmirnov/ssl_control
Browse files Browse the repository at this point in the history
allow disabling SSL expansion
  • Loading branch information
issmirnov authored Jun 9, 2017
2 parents 44c88f1 + c1acc1a commit f911665
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
delimEnd = "### Zap Shortcuts :end ##\n"
expandKey = "expand"
queryKey = "query"
sslKey = "ssl_off"
)

// parseYaml takes a file name and returns a gabs config object.
Expand Down
7 changes: 6 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ g:
expand: issmirnov/zap
s:
query: "search?q="
z:
expand: zero.com
ssl_off: yes
zz:
expand: zero.ssl.on.com
ssl_off: no
`

func parseDummyYaml() (*gabs.Container, error) {
Expand Down
12 changes: 10 additions & 2 deletions web.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@ func IndexHandler(a *context, w http.ResponseWriter, r *http.Request) (int, erro
host = r.Host
}

var hostConfig *gabs.Container
var ok bool

// Check if host present in config.
children, _ := a.config.ChildrenMap()
if _, ok := children[host]; !ok {
if hostConfig, ok = children[host]; !ok {
return 404, fmt.Errorf("Shortcut '%s' not found in config.", host)
}

tokens := tokenize(host + r.URL.Path)
var path bytes.Buffer
path.WriteString("https:/") // second slash appended in expand() call
if s := hostConfig.Path(sslKey).Data(); s != nil && s.(bool) {
path.WriteString("http:/") // second slash appended in expand() call
} else {
path.WriteString("https:/") // second slash appended in expand() call
}

expand(a.config, tokens.Front(), &path)

// send result
Expand Down
26 changes: 26 additions & 0 deletions web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ func TestIndexHandler(t *testing.T) {
So(rr.Header().Get("Location"), ShouldEqual, "https://github.com/search?q=")
})
})
Convey("When we GET http://z/ with ssl_off", func() {
req, err := http.NewRequest("GET", "/", nil)
So(err, ShouldBeNil)
req.Host = "z"

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

Convey("The result should be a 302 to http://zero.com/", func() {
So(rr.Code, ShouldEqual, http.StatusFound)
So(rr.Header().Get("Location"), ShouldEqual, "http://zero.com/")
})
})
Convey("When we GET http://zz/ with ssl_off: no ", func() {
req, err := http.NewRequest("GET", "/", nil)
So(err, ShouldBeNil)
req.Host = "zz"

rr := httptest.NewRecorder()
handler.ServeHTTP(rr, req)

Convey("The result should be a 302 to https://zero.ssl.on.com", func() {
So(rr.Code, ShouldEqual, http.StatusFound)
So(rr.Header().Get("Location"), ShouldEqual, "https://zero.ssl.on.com/")
})
})
})
}

Expand Down

0 comments on commit f911665

Please sign in to comment.