-
Notifications
You must be signed in to change notification settings - Fork 26
/
url_parser.go
38 lines (33 loc) · 851 Bytes
/
url_parser.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
package socketio
import (
"fmt"
"net/url"
"strings"
)
// Parse raw url string and make valid handshake or websockets socket.io url.
type urlParser struct {
raw string
parsed *url.URL
}
func newURLParser(raw string) (*urlParser, error) {
parsed, err := url.Parse(raw)
if err != nil {
return nil, err
}
if parsed.Scheme == "" {
parsed.Scheme = "http"
}
return &urlParser{raw: raw, parsed: parsed}, nil
}
func (u *urlParser) handshake() string {
return fmt.Sprintf("%s/socket.io/1", u.parsed.String())
}
func (u *urlParser) websocket(sessionId string) string {
var host string
if u.parsed.Scheme == "https" {
host = strings.Replace(u.parsed.String(), "https://", "wss://", 1)
} else {
host = strings.Replace(u.parsed.String(), "http://", "ws://", 1)
}
return fmt.Sprintf("%s/socket.io/1/websocket/%s", host, sessionId)
}