Skip to content

Commit 6571859

Browse files
committed
Merge pull request #24 from mweibel/feature/20-expose-http-request
Fixes #20 by exposing the initial http.Request on the session
2 parents 50b75e1 + 64b59d7 commit 6571859

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

sockjs/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (h *handler) sessionByRequest(req *http.Request) (*session, error) {
104104
}
105105
sess, exists := h.sessions[sessionID]
106106
if !exists {
107-
sess = newSession(sessionID, h.options.DisconnectDelay, h.options.HeartbeatDelay)
107+
sess = newSession(req, sessionID, h.options.DisconnectDelay, h.options.HeartbeatDelay)
108108
h.sessions[sessionID] = sess
109109
if h.handlerFunc != nil {
110110
go h.handlerFunc(sess)

sockjs/jsonp_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,13 @@ func TestHandler_jsonpSendNoSession(t *testing.T) {
6969

7070
func TestHandler_jsonpSend(t *testing.T) {
7171
h := newTestHandler()
72-
sess := newSession("session", time.Second, time.Second)
73-
h.sessions["session"] = sess
7472

7573
rw := httptest.NewRecorder()
7674
req, _ := http.NewRequest("POST", "/server/session/jsonp_send", strings.NewReader("[\"message\"]"))
75+
76+
sess := newSession(req, "session", time.Second, time.Second)
77+
h.sessions["session"] = sess
78+
7779
var done = make(chan struct{})
7880
go func() {
7981
h.jsonpSend(rw, req)

sockjs/session.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/gob"
55
"errors"
66
"io"
7+
"net/http"
78
"sync"
89
"time"
910
)
@@ -31,6 +32,7 @@ var (
3132
type session struct {
3233
sync.Mutex
3334
id string
35+
req *http.Request
3436
state sessionState
3537
// protocol dependent receiver (xhr, eventsource, ...)
3638
recv receiver
@@ -69,10 +71,11 @@ type receiver interface {
6971
}
7072

7173
// Session is a central component that handles receiving and sending frames. It maintains internal state
72-
func newSession(sessionID string, sessionTimeoutInterval, heartbeatInterval time.Duration) *session {
74+
func newSession(req *http.Request, sessionID string, sessionTimeoutInterval, heartbeatInterval time.Duration) *session {
7375
r, w := io.Pipe()
7476
s := &session{
7577
id: sessionID,
78+
req: req,
7679
msgReader: r,
7780
msgWriter: w,
7881
msgEncoder: gob.NewEncoder(w),
@@ -215,3 +218,7 @@ func (s *session) Send(msg string) error {
215218
}
216219

217220
func (s *session) ID() string { return s.id }
221+
222+
func (s *session) Request() *http.Request {
223+
return s.req
224+
}

sockjs/session_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"sync"
77
"testing"
88
"time"
9+
"net/http"
10+
"strings"
911
)
1012

1113
func newTestSession() *session {
1214
// session with long expiration and heartbeats with ID
13-
return newSession("sessionId", 1000*time.Second, 1000*time.Second)
15+
return newSession(nil, "sessionId", 1000*time.Second, 1000*time.Second)
1416
}
1517

1618
func TestSession_Create(t *testing.T) {
@@ -28,6 +30,18 @@ func TestSession_Create(t *testing.T) {
2830
}
2931
}
3032

33+
func TestSession_Request(t *testing.T) {
34+
req, _ := http.NewRequest("POST", "/server/session/jsonp_send", strings.NewReader("[\"message\"]"))
35+
sess := newSession(req, "session", time.Second, time.Second)
36+
37+
if sess.Request() == nil {
38+
t.Error("Session initial request should have been saved.")
39+
}
40+
if sess.Request().URL.String() != req.URL.String() {
41+
t.Errorf("Expected stored session request URL to equal %s, got %s", req.URL.String(), sess.Request().URL.String())
42+
}
43+
}
44+
3145
func TestSession_ConcurrentSend(t *testing.T) {
3246
session := newTestSession()
3347
done := make(chan bool)
@@ -75,7 +89,7 @@ func TestSession_AttachReceiver(t *testing.T) {
7589
}
7690

7791
func TestSession_Timeout(t *testing.T) {
78-
sess := newSession("id", 10*time.Millisecond, 10*time.Second)
92+
sess := newSession(nil, "id", 10*time.Millisecond, 10*time.Second)
7993
select {
8094
case <-sess.closeCh:
8195
case <-time.After(20 * time.Millisecond):
@@ -94,7 +108,7 @@ func TestSession_TimeoutOfClosedSession(t *testing.T) {
94108
t.Errorf("Unexcpected error '%v'", r)
95109
}
96110
}()
97-
sess := newSession("id", 1*time.Millisecond, time.Second)
111+
sess := newSession(nil, "id", 1*time.Millisecond, time.Second)
98112
sess.closing()
99113
time.Sleep(1 * time.Millisecond)
100114
sess.closing()
@@ -106,7 +120,7 @@ func TestSession_AttachReceiverAndCheckHeartbeats(t *testing.T) {
106120
t.Errorf("Unexcpected error '%v'", r)
107121
}
108122
}()
109-
session := newSession("id", time.Second, 10*time.Millisecond) // 10ms heartbeats
123+
session := newSession(nil, "id", time.Second, 10*time.Millisecond) // 10ms heartbeats
110124
recv := newTestReceiver()
111125
defer close(recv.doneCh)
112126
session.attachReceiver(recv)
@@ -211,7 +225,7 @@ func TestSession_Closing(t *testing.T) {
211225
}
212226

213227
// Session as Session Tests
214-
func TestSession_AsSession(t *testing.T) { var _ Session = newSession("id", 0, 0) }
228+
func TestSession_AsSession(t *testing.T) { var _ Session = newSession(nil, "id", 0, 0) }
215229

216230
func TestSession_SessionRecv(t *testing.T) {
217231
s := newTestSession()

sockjs/sockjs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package sockjs
22

3+
import "net/http"
4+
35
// Session represents a connection between server and client.
46
type Session interface {
57
// Id returns a session id
68
ID() string
9+
// Request returns the first http request
10+
Request() *http.Request
711
// Recv reads one text frame from session
812
Recv() (string, error)
913
// Send sends one text frame to session

sockjs/websocket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (h *handler) sockjsWebsocket(rw http.ResponseWriter, req *http.Request) {
2626
return
2727
}
2828
sessID, _ := h.parseSessionID(req.URL)
29-
sess := newSession(sessID, h.options.DisconnectDelay, h.options.HeartbeatDelay)
29+
sess := newSession(req, sessID, h.options.DisconnectDelay, h.options.HeartbeatDelay)
3030
if h.handlerFunc != nil {
3131
go h.handlerFunc(sess)
3232
}

sockjs/xhr_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ func TestHandler_XhrSendWrongUrlPath(t *testing.T) {
4646

4747
func TestHandler_XhrSendToExistingSession(t *testing.T) {
4848
h := newTestHandler()
49-
sess := newSession("session", time.Second, time.Second)
50-
h.sessions["session"] = sess
51-
5249
rec := httptest.NewRecorder()
5350
req, _ := http.NewRequest("POST", "/server/session/xhr_send", strings.NewReader("[\"some message\"]"))
51+
sess := newSession(req, "session", time.Second, time.Second)
52+
h.sessions["session"] = sess
53+
54+
req, _ = http.NewRequest("POST", "/server/session/xhr_send", strings.NewReader("[\"some message\"]"))
5455
var done = make(chan bool)
5556
go func() {
5657
h.xhrSend(rec, req)
@@ -124,11 +125,12 @@ func TestHandler_XhrPollConnectionInterrupted(t *testing.T) {
124125

125126
func TestHandler_XhrPollAnotherConnectionExists(t *testing.T) {
126127
h := newTestHandler()
128+
req, _ := http.NewRequest("POST", "/server/session/xhr", nil)
127129
// turn of timeoutes and heartbeats
128-
sess := newSession("session", time.Hour, time.Hour)
130+
sess := newSession(req, "session", time.Hour, time.Hour)
129131
h.sessions["session"] = sess
130132
sess.attachReceiver(newTestReceiver())
131-
req, _ := http.NewRequest("POST", "/server/session/xhr", nil)
133+
req, _ = http.NewRequest("POST", "/server/session/xhr", nil)
132134
rw2 := httptest.NewRecorder()
133135
h.xhrPoll(rw2, req)
134136
if rw2.Body.String() != "c[2010,\"Another connection still open\"]\n" {

0 commit comments

Comments
 (0)