forked from madari/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransport_jsonppolling.go
64 lines (52 loc) · 1.35 KB
/
transport_jsonppolling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package socketio
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"strconv"
)
var JSONPPolling = &Transport{
Name: "jsonp-polling",
Type: PollingTransport,
PostEncoded: true,
Hijack: jsonpPollingHijack,
}
func jsonpPollingHijack(w http.ResponseWriter, req *http.Request, proceed func(Socket)) error {
rwc, _, err := w.(http.Hijacker).Hijack()
if err == nil {
conn := rwc.(*net.TCPConn)
var buf bytes.Buffer
buf.WriteString("HTTP/1.0 200 OK\r\n")
buf.WriteString("Content-Type: text/javascript; charset=UTF-8\r\n")
buf.WriteString("X-XSS-Protection: 0\r\n")
i, _ := strconv.ParseUint(req.FormValue("i"), 10, 0)
if _, err = buf.WriteTo(conn); err == nil {
proceed(&jsonpPollingSocket{conn, i, make([]byte, 1)})
}
}
return err
}
type jsonpPollingSocket struct {
net.Conn
i uint64
rb []byte
}
// Write sends a single message to the wire and closes the connection.
func (s *jsonpPollingSocket) Write(p []byte) (int, error) {
defer s.Close()
var buf bytes.Buffer
fmt.Fprintf(&buf, "io.j[%d](", s.i)
enc := json.NewEncoder(&buf)
if err := enc.Encode(string(p)); err != nil {
return 0, err
}
buf.WriteString(");")
return fmt.Fprintf(s.Conn, "Content-Length: %d\r\n\r\n%s", buf.Len(), buf.Bytes())
}
func (s *jsonpPollingSocket) Receive(p *[]byte) (err error) {
_, err = s.Read(s.rb)
*p = s.rb
return
}