/
http_stream_dialer.go
103 lines (83 loc) · 2.3 KB
/
http_stream_dialer.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package broadcast
import (
"errors"
"fmt"
shoutcast "github.com/tryphon/go-shoutcast"
"net"
"net/http"
"net/url"
)
type HttpStreamDialer interface {
Connect(output *HttpStreamOutput) (net.Conn, error)
}
type Icecast2Dialer struct {
}
func (dialer *Icecast2Dialer) Connect(output *HttpStreamOutput) (net.Conn, error) {
var connection net.Conn
dialTimeout := func(network, addr string) (net.Conn, error) {
newConnection, err := net.DialTimeout(network, addr, output.GetWriteTimeout())
if err != nil {
return nil, err
}
connection = newConnection
return newConnection, nil
}
transport := http.Transport{Dial: dialTimeout}
client := http.Client{Transport: &transport}
request, err := http.NewRequest("SOURCE", output.Target, nil)
if err != nil {
return nil, err
}
request.Header.Add("Content-type", output.Format.ContentType())
request.Header.Add("User-Agent", "Go Broadcast v0")
if output.Description != nil {
for attribute, value := range output.Description.IcecastHeaders() {
Log.Debugf("IceCast header: %s=%s", attribute, value)
request.Header.Add(attribute, value)
}
}
// request.SetBasicAuth("source", password)
response, err := client.Do(request)
if err != nil {
return nil, err
}
Log.Debugf("HTTP Response : %s", response.Status)
if response.Status != "200 OK" {
err = fmt.Errorf("Server Error : %s", response.Status)
return nil, err
}
return connection, nil
}
type ShoutcastDialer struct {
}
func (dialer *ShoutcastDialer) Client(output *HttpStreamOutput) (*shoutcast.Client, error) {
targetURL, err := url.Parse(output.Target)
if err != nil {
return nil, err
}
password, ok := targetURL.User.Password()
if !ok {
return nil, errors.New("No specified password")
}
description := output.Description
if description == nil {
description = &StreamDescription{}
}
headers := description.ShoutcastHeaders()
headers["content-type"] = output.Format.ContentType()
Log.Debugf("ShoutCast headers: %v", headers)
client := &shoutcast.Client{
Host: targetURL.Host,
Password: password,
Timeout: output.GetWriteTimeout(),
Headers: headers,
}
return client, nil
}
func (dialer *ShoutcastDialer) Connect(output *HttpStreamOutput) (net.Conn, error) {
client, err := dialer.Client(output)
if err != nil {
return nil, err
}
return client.Connect()
}