Skip to content

Commit

Permalink
Correctly processing proxy request to avoid abortHandler panic (fixes a…
Browse files Browse the repository at this point in the history
  • Loading branch information
vania-pooh committed Dec 21, 2018
1 parent 35243e1 commit 088f234
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sudo: required
language: go

go:
- 1.11.x
- 1.11.1

services:
- docker
Expand Down
6 changes: 4 additions & 2 deletions selenoid.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,10 @@ func generateRandomFileName(extension string) string {

func proxy(w http.ResponseWriter, r *http.Request) {
done := make(chan func())
go func(w http.ResponseWriter, r *http.Request) {
go func() {
(<-done)()
}()
func(w http.ResponseWriter, r *http.Request) {
cancel := func() {}
defer func() {
done <- cancel
Expand Down Expand Up @@ -447,7 +450,6 @@ func proxy(w http.ResponseWriter, r *http.Request) {
},
}).ServeHTTP(w, r)
}(w, r)
go (<-done)()
}

func reverseProxy(hostFn func(sess *session.Session) string, status string) func(http.ResponseWriter, *http.Request) {
Expand Down
17 changes: 17 additions & 0 deletions selenoid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,23 @@ func TestProxySession(t *testing.T) {
queue.Release()
}

func TestProxySessionPanicOnAbortHandler(t *testing.T) {

manager = &HTTPTest{Handler: Selenium()}

resp, err := http.Post(With(srv.URL).Path("/wd/hub/session"), "", bytes.NewReader([]byte("{}")))
AssertThat(t, err, Is{nil})
var sess map[string]string
AssertThat(t, resp, AllOf{Code{http.StatusOK}, IsJson{&sess}})

req, _ := http.NewRequest(http.MethodGet, With(srv.URL).Path(fmt.Sprintf("/wd/hub/session/%s/url?abort-handler=true", sess["sessionId"])), nil)
resp, err = http.DefaultClient.Do(req)
AssertThat(t, err, Not{nil})

sessions.Remove(sess["sessionId"])
queue.Release()
}

func TestSessionDeleted(t *testing.T) {
canceled := false
ch := make(chan bool)
Expand Down
8 changes: 8 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -121,6 +122,13 @@ func Selenium() http.Handler {
http.Error(w, "Session not found", http.StatusNotFound)
return
}
if r.FormValue("abort-handler") != "" {
out := "this call was relayed by the reverse proxy"
// Setting wrong Content-Length leads to abort handler error
w.Header().Add("Content-Length", strconv.Itoa(2*len(out)))
fmt.Fprintln(w, out)
return
}
d, _ := time.ParseDuration(r.FormValue("timeout"))
if r.Method != http.MethodDelete {
<-time.After(d)
Expand Down

0 comments on commit 088f234

Please sign in to comment.