~mark-mims/juju-core/packaging-with-alternatives

« back to all changes in this revision

Viewing changes to src/code.google.com/p/go.net/websocket/server.go

  • Committer: Dave Cheney
  • Date: 2013-01-28 10:23:17 UTC
  • Revision ID: david.cheney@canonical.com-20130128102317-fbxahdfoplrglb9m
Add websocket dep to overlay. 

Someone may be able to improve this if they can show me how to do nest a hg repo.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
5
package websocket
 
6
 
 
7
import (
 
8
        "bufio"
 
9
        "fmt"
 
10
        "io"
 
11
        "net/http"
 
12
)
 
13
 
 
14
func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Request) (conn *Conn, err error) {
 
15
        config := new(Config)
 
16
        var hs serverHandshaker = &hybiServerHandshaker{Config: config}
 
17
        code, err := hs.ReadHandshake(buf.Reader, req)
 
18
        if err == ErrBadWebSocketVersion {
 
19
                fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
 
20
                fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion)
 
21
                buf.WriteString("\r\n")
 
22
                buf.WriteString(err.Error())
 
23
                buf.Flush()
 
24
                return
 
25
        }
 
26
        if err != nil {
 
27
                hs = &hixie76ServerHandshaker{Config: config}
 
28
                code, err = hs.ReadHandshake(buf.Reader, req)
 
29
        }
 
30
        if err != nil {
 
31
                hs = &hixie75ServerHandshaker{Config: config}
 
32
                code, err = hs.ReadHandshake(buf.Reader, req)
 
33
        }
 
34
        if err != nil {
 
35
                fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
 
36
                buf.WriteString("\r\n")
 
37
                buf.WriteString(err.Error())
 
38
                buf.Flush()
 
39
                return
 
40
        }
 
41
        config.Protocol = nil
 
42
 
 
43
        err = hs.AcceptHandshake(buf.Writer)
 
44
        if err != nil {
 
45
                code = http.StatusBadRequest
 
46
                fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
 
47
                buf.WriteString("\r\n")
 
48
                buf.Flush()
 
49
                return
 
50
        }
 
51
        conn = hs.NewServerConn(buf, rwc, req)
 
52
        return
 
53
}
 
54
 
 
55
// Handler is an interface to a WebSocket.
 
56
type Handler func(*Conn)
 
57
 
 
58
// ServeHTTP implements the http.Handler interface for a Web Socket
 
59
func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 
60
        rwc, buf, err := w.(http.Hijacker).Hijack()
 
61
        if err != nil {
 
62
                panic("Hijack failed: " + err.Error())
 
63
                return
 
64
        }
 
65
        // The server should abort the WebSocket connection if it finds
 
66
        // the client did not send a handshake that matches with protocol
 
67
        // specification.
 
68
        defer rwc.Close()
 
69
        conn, err := newServerConn(rwc, buf, req)
 
70
        if err != nil {
 
71
                return
 
72
        }
 
73
        if conn == nil {
 
74
                panic("unexpected nil conn")
 
75
        }
 
76
        h(conn)
 
77
}