forked from autogrow/wray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_transport.go
59 lines (51 loc) · 1.16 KB
/
http_transport.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
package wray
import (
"bytes"
"encoding/json"
"errors"
// "fmt"
"io/ioutil"
"net/http"
"net/url"
)
// HTTPTransport models a faye protocol transport over HTTP long polling
type HTTPTransport struct {
url string
}
func (t HTTPTransport) isUsable(clientURL string) bool {
parsedURL, err := url.Parse(clientURL)
if err != nil {
return false
}
if parsedURL.Scheme == "http" || parsedURL.Scheme == "https" {
return true
}
return false
}
func (t HTTPTransport) connectionType() string {
return "long-polling"
}
func (t HTTPTransport) send(msg json.Marshaler) (decoder, error) {
b, err := json.Marshal(msg)
if err != nil {
return nil, err
}
buffer := bytes.NewBuffer(b)
responseData, err := http.Post(t.url, "application/json", buffer)
if err != nil {
return nil, err
}
if responseData.StatusCode != 200 {
return nil, errors.New(responseData.Status)
}
defer responseData.Body.Close()
jsonData, err := ioutil.ReadAll(responseData.Body)
if err != nil {
return nil, err
}
// fmt.Println("BUFFFERERERERER!!! ", string(jsonData))
return json.NewDecoder(bytes.NewBuffer(jsonData)), nil
}
func (t *HTTPTransport) setURL(url string) {
t.url = url
}