client.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package websocket
  5. import (
  6. "bufio"
  7. "crypto/tls"
  8. "io"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. )
  13. // DialError is an error that occurs while dialling a websocket server.
  14. type DialError struct {
  15. *Config
  16. Err error
  17. }
  18. func (e *DialError) Error() string {
  19. return "websocket.Dial " + e.Config.Location.String() + ": " + e.Err.Error()
  20. }
  21. // NewConfig creates a new WebSocket config for client connection.
  22. func NewConfig(server, origin string) (config *Config, err error) {
  23. config = new(Config)
  24. config.Version = ProtocolVersionHybi13
  25. config.Location, err = url.ParseRequestURI(server)
  26. if err != nil {
  27. return
  28. }
  29. config.Origin, err = url.ParseRequestURI(origin)
  30. if err != nil {
  31. return
  32. }
  33. config.Header = http.Header(make(map[string][]string))
  34. return
  35. }
  36. // NewClient creates a new WebSocket client connection over rwc.
  37. func NewClient(config *Config, rwc io.ReadWriteCloser) (ws *Conn, err error) {
  38. br := bufio.NewReader(rwc)
  39. bw := bufio.NewWriter(rwc)
  40. err = hybiClientHandshake(config, br, bw)
  41. if err != nil {
  42. return
  43. }
  44. buf := bufio.NewReadWriter(br, bw)
  45. ws = newHybiClientConn(config, buf, rwc)
  46. return
  47. }
  48. // Dial opens a new client connection to a WebSocket.
  49. func Dial(url_, protocol, origin string) (ws *Conn, err error) {
  50. config, err := NewConfig(url_, origin)
  51. if err != nil {
  52. return nil, err
  53. }
  54. if protocol != "" {
  55. config.Protocol = []string{protocol}
  56. }
  57. return DialConfig(config)
  58. }
  59. var portMap = map[string]string{
  60. "ws": "80",
  61. "wss": "443",
  62. }
  63. func parseAuthority(location *url.URL) string {
  64. if _, ok := portMap[location.Scheme]; ok {
  65. if _, _, err := net.SplitHostPort(location.Host); err != nil {
  66. return net.JoinHostPort(location.Host, portMap[location.Scheme])
  67. }
  68. }
  69. return location.Host
  70. }
  71. // DialConfig opens a new client connection to a WebSocket with a config.
  72. func DialConfig(config *Config) (ws *Conn, err error) {
  73. var client net.Conn
  74. if config.Location == nil {
  75. return nil, &DialError{config, ErrBadWebSocketLocation}
  76. }
  77. if config.Origin == nil {
  78. return nil, &DialError{config, ErrBadWebSocketOrigin}
  79. }
  80. switch config.Location.Scheme {
  81. case "ws":
  82. client, err = net.Dial("tcp", parseAuthority(config.Location))
  83. case "wss":
  84. client, err = tls.Dial("tcp", parseAuthority(config.Location), config.TlsConfig)
  85. default:
  86. err = ErrBadScheme
  87. }
  88. if err != nil {
  89. goto Error
  90. }
  91. ws, err = NewClient(config, client)
  92. if err != nil {
  93. client.Close()
  94. goto Error
  95. }
  96. return
  97. Error:
  98. return nil, &DialError{config, err}
  99. }