-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 273ecad
Showing
20 changed files
with
2,066 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Compiled Object files, Static and Dynamic libs (Shared Objects) | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Folders | ||
_obj | ||
_test | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
[568vq].out | ||
|
||
*.cgo1.go | ||
*.cgo2.c | ||
_cgo_defun.c | ||
_cgo_gotypes.go | ||
_cgo_export.* | ||
|
||
_testmain.go | ||
|
||
*.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Copyright (c) 2013, Gorilla web toolkit | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
Redistributions in binary form must reproduce the above copyright notice, this | ||
list of conditions and the following disclaimer in the documentation and/or | ||
other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# WebSocket | ||
|
||
This project is a [Go](http://golang.org/) implementation of the | ||
[WebSocket](http://www.rfc-editor.org/rfc/rfc6455.txt) protocol. | ||
|
||
The project passes the server tests in the [Autobahn WebSockets Test | ||
Suite](http://autobahn.ws/testsuite) using the application in the [examples/autobahn | ||
subdirectory](https://github.com/gorilla/websocket/tree/master/examples/autobahn). | ||
|
||
## Documentation | ||
|
||
* [Reference](http://godoc.org/github.com/gorilla/websocket) | ||
* [Chat example](https://github.com/gorilla/websocket/tree/master/examples/chat) | ||
|
||
## Features | ||
|
||
- Send and receive ping, pong and close control messages. | ||
- Limit size of received messages. | ||
- Stream messages. | ||
- Specify IO buffer sizes. | ||
- Application has full control over origin checks and sub-protocol negotiation. | ||
|
||
## Installation | ||
|
||
go get github.com/gorilla/websocket | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2013 Gary Burd. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package websocket | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// ErrBadHandshake is returned when the server response to opening handshake is | ||
// invalid. | ||
var ErrBadHandshake = errors.New("websocket: bad handshake") | ||
|
||
// NewClient creates a new client connection using the given net connection. | ||
// The URL u specifies the host and request URI. Use requestHeader to specify | ||
// the origin (Origin), subprotocols (Set-WebSocket-Protocol) and cookies | ||
// (Cookie). Use the response.Header to get the selected subprotocol | ||
// (Sec-WebSocket-Protocol) and cookies (Set-Cookie). | ||
// | ||
// If the WebSocket handshake fails, ErrBadHandshake is returned along with a | ||
// non-nil *http.Response so that callers can handle redirects, authentication, | ||
// etc. | ||
func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufSize, writeBufSize int) (c *Conn, response *http.Response, err error) { | ||
challengeKey, err := generateChallengeKey() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
acceptKey := computeAcceptKey(challengeKey) | ||
|
||
c = newConn(netConn, false, readBufSize, writeBufSize) | ||
p := c.writeBuf[:0] | ||
p = append(p, "GET "...) | ||
p = append(p, u.RequestURI()...) | ||
p = append(p, " HTTP/1.1\r\nHost: "...) | ||
p = append(p, u.Host...) | ||
p = append(p, "\r\nUpgrade: websocket\r\nConnection: upgrade\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: "...) | ||
p = append(p, challengeKey...) | ||
p = append(p, "\r\n"...) | ||
for k, vs := range requestHeader { | ||
for _, v := range vs { | ||
p = append(p, k...) | ||
p = append(p, ": "...) | ||
p = append(p, v...) | ||
p = append(p, "\r\n"...) | ||
} | ||
} | ||
p = append(p, "\r\n"...) | ||
|
||
if _, err := netConn.Write(p); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
resp, err := http.ReadResponse(c.br, &http.Request{Method: "GET", URL: u}) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if resp.StatusCode != 101 || | ||
!strings.EqualFold(resp.Header.Get("Upgrade"), "websocket") || | ||
!strings.EqualFold(resp.Header.Get("Connection"), "upgrade") || | ||
resp.Header.Get("Sec-Websocket-Accept") != acceptKey { | ||
return nil, resp, ErrBadHandshake | ||
} | ||
return c, resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2013 Gary Burd. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package websocket_test | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
type wsHandler struct { | ||
*testing.T | ||
} | ||
|
||
func (t wsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != "GET" { | ||
http.Error(w, "Method not allowed", 405) | ||
t.Logf("bad method: %s", r.Method) | ||
return | ||
} | ||
if r.Header.Get("Origin") != "http://"+r.Host { | ||
http.Error(w, "Origin not allowed", 403) | ||
t.Logf("bad origin: %s", r.Header.Get("Origin")) | ||
return | ||
} | ||
ws, err := websocket.Upgrade(w, r, http.Header{"Set-Cookie": {"sessionID=1234"}}, 1024, 1024) | ||
if _, ok := err.(websocket.HandshakeError); ok { | ||
t.Logf("bad handshake: %v", err) | ||
http.Error(w, "Not a websocket handshake", 400) | ||
return | ||
} else if err != nil { | ||
t.Logf("upgrade error: %v", err) | ||
return | ||
} | ||
defer ws.Close() | ||
for { | ||
op, r, err := ws.NextReader() | ||
if err != nil { | ||
if err != io.EOF { | ||
t.Logf("NextReader: %v", err) | ||
} | ||
return | ||
} | ||
if op == websocket.PongMessage { | ||
continue | ||
} | ||
w, err := ws.NextWriter(op) | ||
if err != nil { | ||
t.Logf("NextWriter: %v", err) | ||
return | ||
} | ||
if _, err = io.Copy(w, r); err != nil { | ||
t.Logf("Copy: %v", err) | ||
return | ||
} | ||
if err := w.Close(); err != nil { | ||
t.Logf("Close: %v", err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func TestClientServer(t *testing.T) { | ||
s := httptest.NewServer(wsHandler{t}) | ||
defer s.Close() | ||
u, _ := url.Parse(s.URL) | ||
c, err := net.Dial("tcp", u.Host) | ||
if err != nil { | ||
t.Fatalf("Dial: %v", err) | ||
} | ||
ws, resp, err := websocket.NewClient(c, u, http.Header{"Origin": {s.URL}}, 1024, 1024) | ||
if err != nil { | ||
t.Fatalf("NewClient: %v", err) | ||
} | ||
defer ws.Close() | ||
|
||
var sessionID string | ||
for _, c := range resp.Cookies() { | ||
if c.Name == "sessionID" { | ||
sessionID = c.Value | ||
} | ||
} | ||
if sessionID != "1234" { | ||
t.Error("Set-Cookie not received from the server.") | ||
} | ||
|
||
w, _ := ws.NextWriter(websocket.TextMessage) | ||
io.WriteString(w, "HELLO") | ||
w.Close() | ||
ws.SetReadDeadline(time.Now().Add(1 * time.Second)) | ||
op, r, err := ws.NextReader() | ||
if err != nil { | ||
t.Fatalf("NextReader: %v", err) | ||
} | ||
if op != websocket.TextMessage { | ||
t.Fatalf("op=%d, want %d", op, websocket.TextMessage) | ||
} | ||
b, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
t.Fatalf("ReadAll: %v", err) | ||
} | ||
if string(b) != "HELLO" { | ||
t.Fatalf("message=%s, want %s", b, "HELLO") | ||
} | ||
} |
Oops, something went wrong.