forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bump(code.google.com/p/go.net/websocket): 7127eef
Add code.google.com/p/go.net/websocket to deps.sh and run update.sh.
- Loading branch information
Showing
9 changed files
with
2,176 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
98 changes: 98 additions & 0 deletions
98
third_party/src/code.google.com/p/go.net/websocket/client.go
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,98 @@ | ||
// Copyright 2009 The Go Authors. 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 ( | ||
"bufio" | ||
"crypto/tls" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
// DialError is an error that occurs while dialling a websocket server. | ||
type DialError struct { | ||
*Config | ||
Err error | ||
} | ||
|
||
func (e *DialError) Error() string { | ||
return "websocket.Dial " + e.Config.Location.String() + ": " + e.Err.Error() | ||
} | ||
|
||
// NewConfig creates a new WebSocket config for client connection. | ||
func NewConfig(server, origin string) (config *Config, err error) { | ||
config = new(Config) | ||
config.Version = ProtocolVersionHybi13 | ||
config.Location, err = url.ParseRequestURI(server) | ||
if err != nil { | ||
return | ||
} | ||
config.Origin, err = url.ParseRequestURI(origin) | ||
if err != nil { | ||
return | ||
} | ||
config.Header = http.Header(make(map[string][]string)) | ||
return | ||
} | ||
|
||
// NewClient creates a new WebSocket client connection over rwc. | ||
func NewClient(config *Config, rwc io.ReadWriteCloser) (ws *Conn, err error) { | ||
br := bufio.NewReader(rwc) | ||
bw := bufio.NewWriter(rwc) | ||
err = hybiClientHandshake(config, br, bw) | ||
if err != nil { | ||
return | ||
} | ||
buf := bufio.NewReadWriter(br, bw) | ||
ws = newHybiClientConn(config, buf, rwc) | ||
return | ||
} | ||
|
||
// Dial opens a new client connection to a WebSocket. | ||
func Dial(url_, protocol, origin string) (ws *Conn, err error) { | ||
config, err := NewConfig(url_, origin) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if protocol != "" { | ||
config.Protocol = []string{protocol} | ||
} | ||
return DialConfig(config) | ||
} | ||
|
||
// DialConfig opens a new client connection to a WebSocket with a config. | ||
func DialConfig(config *Config) (ws *Conn, err error) { | ||
var client net.Conn | ||
if config.Location == nil { | ||
return nil, &DialError{config, ErrBadWebSocketLocation} | ||
} | ||
if config.Origin == nil { | ||
return nil, &DialError{config, ErrBadWebSocketOrigin} | ||
} | ||
switch config.Location.Scheme { | ||
case "ws": | ||
client, err = net.Dial("tcp", config.Location.Host) | ||
|
||
case "wss": | ||
client, err = tls.Dial("tcp", config.Location.Host, config.TlsConfig) | ||
|
||
default: | ||
err = ErrBadScheme | ||
} | ||
if err != nil { | ||
goto Error | ||
} | ||
|
||
ws, err = NewClient(config, client) | ||
if err != nil { | ||
goto Error | ||
} | ||
return | ||
|
||
Error: | ||
return nil, &DialError{config, err} | ||
} |
31 changes: 31 additions & 0 deletions
31
third_party/src/code.google.com/p/go.net/websocket/exampledial_test.go
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,31 @@ | ||
// Copyright 2012 The Go Authors. 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 ( | ||
"fmt" | ||
"log" | ||
|
||
"code.google.com/p/go.net/websocket" | ||
) | ||
|
||
// This example demonstrates a trivial client. | ||
func ExampleDial() { | ||
origin := "http://localhost/" | ||
url := "ws://localhost:12345/ws" | ||
ws, err := websocket.Dial(url, "", origin) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if _, err := ws.Write([]byte("hello, world!\n")); err != nil { | ||
log.Fatal(err) | ||
} | ||
var msg = make([]byte, 512) | ||
var n int | ||
if n, err = ws.Read(msg); err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("Received: %s.\n", msg[:n]) | ||
} |
26 changes: 26 additions & 0 deletions
26
third_party/src/code.google.com/p/go.net/websocket/examplehandler_test.go
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 @@ | ||
// Copyright 2012 The Go Authors. 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" | ||
"net/http" | ||
|
||
"code.google.com/p/go.net/websocket" | ||
) | ||
|
||
// Echo the data received on the WebSocket. | ||
func EchoServer(ws *websocket.Conn) { | ||
io.Copy(ws, ws) | ||
} | ||
|
||
// This example demonstrates a trivial echo server. | ||
func ExampleHandler() { | ||
http.Handle("/echo", websocket.Handler(EchoServer)) | ||
err := http.ListenAndServe(":12345", nil) | ||
if err != nil { | ||
panic("ListenAndServe: " + err.Error()) | ||
} | ||
} |
Oops, something went wrong.