Skip to content

Commit

Permalink
Merge pull request #11 from dmcgowan/remove_auth_handler
Browse files Browse the repository at this point in the history
Remove auth handler
  • Loading branch information
dmcgowan committed Jun 16, 2014
2 parents ab814e2 + 92e2d65 commit 8e32120
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
if err != nil {
panic(err)
}
go spdyConn.Serve(spdystream.NoOpStreamHandler, spdystream.RejectAuthHandler)
go spdyConn.Serve(spdystream.NoOpStreamHandler)
stream := spdyConn.CreateStream(http.Header{}, nil)
err = stream.Open(false)
if err != nil {
Expand Down Expand Up @@ -66,7 +66,7 @@ func main() {
if err != nil {
panic(err)
}
go spdyConn.Serve(spdystream.MirrorStreamHandler, spdystream.NoAuthHandler)
go spdyConn.Serve(spdystream.MirrorStreamHandler)
}
}
~~~~
Expand Down
36 changes: 16 additions & 20 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *Connection) Ping() (time.Duration, error) {
// Serve handles frames sent from the server, including reply frames
// which are needed to fully initiate connections. Both clients and servers
// should call Serve in a separate goroutine before creating streams.
func (s *Connection) Serve(newHandler StreamHandler, authHandler AuthHandler) {
func (s *Connection) Serve(newHandler StreamHandler) {
// Parition queues to ensure stream frames are handled
// by the same worker, ensuring order is maintained
frameQueues := make([]*PriorityFrameQueue, FRAME_WORKERS)
Expand All @@ -132,7 +132,7 @@ func (s *Connection) Serve(newHandler StreamHandler, authHandler AuthHandler) {
frameQueue.Drain()
}(frameQueues[i])

go s.frameHandler(frameQueues[i], newHandler, authHandler)
go s.frameHandler(frameQueues[i], newHandler)
}

var partitionRoundRobin int
Expand Down Expand Up @@ -175,7 +175,7 @@ func (s *Connection) Serve(newHandler StreamHandler, authHandler AuthHandler) {
}
}

func (s *Connection) frameHandler(frameQueue *PriorityFrameQueue, newHandler StreamHandler, authHandler AuthHandler) {
func (s *Connection) frameHandler(frameQueue *PriorityFrameQueue, newHandler StreamHandler) {
for {
popFrame := frameQueue.Pop()
if popFrame == nil {
Expand All @@ -185,7 +185,7 @@ func (s *Connection) frameHandler(frameQueue *PriorityFrameQueue, newHandler Str
var frameErr error
switch frame := popFrame.(type) {
case *spdy.SynStreamFrame:
frameErr = s.handleStreamFrame(frame, newHandler, authHandler)
frameErr = s.handleStreamFrame(frame, newHandler)
case *spdy.SynReplyFrame:
frameErr = s.handleReplyFrame(frame)
case *spdy.DataFrame:
Expand Down Expand Up @@ -216,7 +216,7 @@ func (s *Connection) getStreamPriority(streamId spdy.StreamId) uint8 {
return stream.priority
}

func (s *Connection) handleStreamFrame(frame *spdy.SynStreamFrame, newHandler StreamHandler, authHandler AuthHandler) error {
func (s *Connection) handleStreamFrame(frame *spdy.SynStreamFrame, newHandler StreamHandler) error {
validationErr := s.validateStreamId(frame.StreamId)
if validationErr != nil {
errorFrame := &spdy.RstStreamFrame{
Expand Down Expand Up @@ -255,21 +255,6 @@ func (s *Connection) handleStreamFrame(frame *spdy.SynStreamFrame, newHandler St
s.streams[frame.StreamId] = stream
s.streamLock.Unlock()

if !authHandler(frame.Headers, frame.Slot, uint32(frame.AssociatedToStreamId)) {
stream.Close()
errorFrame := &spdy.RstStreamFrame{
StreamId: frame.StreamId,
Status: spdy.RefusedStream,
}
s.writeLock.Lock()
writeErr := s.framer.WriteFrame(errorFrame)
s.writeLock.Unlock()
if writeErr != nil {
return writeErr
}
return nil
}

newHandler(stream)

return nil
Expand Down Expand Up @@ -479,6 +464,17 @@ func (s *Connection) sendReply(headers http.Header, stream *Stream, fin bool) er
return s.framer.WriteFrame(replyFrame)
}

func (s *Connection) sendReset(status spdy.RstStreamStatus, stream *Stream) error {
resetFrame := &spdy.RstStreamFrame{
StreamId: stream.streamId,
Status: status,
}

s.writeLock.Lock()
defer s.writeLock.Unlock()
return s.framer.WriteFrame(resetFrame)
}

func (s *Connection) sendStream(stream *Stream, fin bool) error {
var flags spdy.ControlFlags
if fin {
Expand Down
10 changes: 0 additions & 10 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,3 @@ func MirrorStreamHandler(stream *Stream) {
func NoOpStreamHandler(stream *Stream) {
stream.SendReply(http.Header{}, false)
}

// NoAuthHandler skips authentication.
func NoAuthHandler(header http.Header, slot uint8, parent uint32) bool {
return true
}

// RejectAuthHandler rejects all remotely initiated connections.
func RejectAuthHandler(header http.Header, slot uint8, parent uint32) bool {
return false
}
13 changes: 8 additions & 5 deletions spdy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestSpdyStreams(t *testing.T) {
if spdyErr != nil {
t.Fatalf("Error creating spdy connection: %s", spdyErr)
}
go spdyConn.Serve(NoOpStreamHandler, RejectAuthHandler)
go spdyConn.Serve(NoOpStreamHandler)

authenticated = true
stream, streamErr := spdyConn.CreateStream(http.Header{}, nil, false)
Expand Down Expand Up @@ -163,7 +163,7 @@ func TestPing(t *testing.T) {
if spdyErr != nil {
t.Fatalf("Error creating spdy connection: %s", spdyErr)
}
go spdyConn.Serve(NoOpStreamHandler, RejectAuthHandler)
go spdyConn.Serve(NoOpStreamHandler)

pingTime, pingErr := spdyConn.Ping()
if pingErr != nil {
Expand All @@ -182,8 +182,11 @@ func TestPing(t *testing.T) {

var authenticated bool

func authStreamHandler(header http.Header, slot uint8, parent uint32) bool {
return authenticated
func authStreamHandler(stream *Stream) {
if !authenticated {
stream.Refuse()
}
MirrorStreamHandler(stream)
}

func runServer(listen string, wg *sync.WaitGroup) (io.Closer, error) {
Expand All @@ -200,7 +203,7 @@ func runServer(listen string, wg *sync.WaitGroup) (io.Closer, error) {
}

spdyConn, _ := NewConnection(conn, true)
go spdyConn.Serve(MirrorStreamHandler, authStreamHandler)
go spdyConn.Serve(authStreamHandler)

}
wg.Done()
Expand Down
21 changes: 21 additions & 0 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func (s *Stream) SendHeader(headers http.Header, fin bool) error {
return s.conn.sendHeaders(headers, s, fin)
}

// SendReply sends a reply on a stream, only valid to be called once
// when handling a new stream
func (s *Stream) SendReply(headers http.Header, fin bool) error {
if s.replied {
return nil
Expand All @@ -154,6 +156,25 @@ func (s *Stream) SendReply(headers http.Header, fin bool) error {
return s.conn.sendReply(headers, s, fin)
}

// Refuse sends a reset frame with the status refuse, only
// valid to be called once when handling a new stream. This
// may be used to indicate that a stream is not allowed
// when http status codes are not being used.
func (s *Stream) Refuse() error {
if s.replied {
return nil
}
s.replied = true
return s.conn.sendReset(spdy.RefusedStream, s)
}

// Cancel sends a reset frame with the status canceled. This
// can be used at any time by the creator of the Stream to
// indicate the stream is no longer needed.
func (s *Stream) Cancel() error {
return s.conn.sendReset(spdy.Cancel, s)
}

// ReceiveHeader receives a header sent on the other side
// of the stream. This function will block until a header
// is received or stream is closed.
Expand Down

0 comments on commit 8e32120

Please sign in to comment.