forked from alexballas/go2tv
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
httphandlers.go
241 lines (198 loc) · 5.22 KB
/
httphandlers.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package httphandlers
import (
"bytes"
"fmt"
"html"
"io"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/chyroc/go2tv/soapcalls"
"github.com/chyroc/go2tv/utils"
)
// HTTPserver - new http.Server instance.
type HTTPserver struct {
http *http.Server
mux *http.ServeMux
}
// Screen interface.
type Screen interface {
EmitMsg(string)
Fini()
}
// Emit .
func Emit(scr Screen, s string) {
scr.EmitMsg(s)
}
// Close .
func Close(scr Screen) {
scr.Fini()
}
// ServeFiles - Start HTTP server and serve the files.
func (s *HTTPserver) ServeFiles(serverStarted chan<- struct{}, media, subtitles interface{},
tvpayload *soapcalls.TVPayload, screen Screen) error {
mURL, err := url.Parse(tvpayload.MediaURL)
if err != nil {
return fmt.Errorf("failed to parse MediaURL: %w", err)
}
sURL, err := url.Parse(tvpayload.SubtitlesURL)
if err != nil {
return fmt.Errorf("failed to parse SubtitlesURL: %w", err)
}
callbackURL, err := url.Parse(tvpayload.CallbackURL)
if err != nil {
return fmt.Errorf("failed to parse CallbackURL: %w", err)
}
s.mux.HandleFunc(mURL.Path, s.serveMediaHandler(tvpayload, media))
s.mux.HandleFunc(sURL.Path, s.serveSubtitlesHandler(subtitles))
s.mux.HandleFunc(callbackURL.Path, s.callbackHandler(tvpayload, screen))
ln, err := net.Listen("tcp", s.http.Addr)
if err != nil {
return fmt.Errorf("server listen error: %w", err)
}
serverStarted <- struct{}{}
s.http.Serve(ln)
return nil
}
func (s *HTTPserver) serveMediaHandler(tv *soapcalls.TVPayload, media interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
serveContent(w, req, tv, media, true)
}
}
func (s *HTTPserver) serveSubtitlesHandler(subs interface{}) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
serveContent(w, req, nil, subs, false)
}
}
func (s *HTTPserver) callbackHandler(tv *soapcalls.TVPayload, screen Screen) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
reqParsed, _ := io.ReadAll(req.Body)
sidVal, sidExists := req.Header["Sid"]
if !sidExists {
http.NotFound(w, req)
return
}
if sidVal[0] == "" {
http.NotFound(w, req)
return
}
uuid := strings.TrimPrefix(sidVal[0], "uuid:")
// Apparently we should ignore the first message
// On some media renderers we receive a STOPPED message
// even before we start streaming.
seq, err := soapcalls.GetSequence(uuid)
if err != nil {
http.NotFound(w, req)
return
}
if seq == 0 {
soapcalls.IncreaseSequence(uuid)
fmt.Fprintf(w, "OK\n")
return
}
reqParsedUnescape := html.UnescapeString(string(reqParsed))
previousstate, newstate, err := soapcalls.EventNotifyParser(reqParsedUnescape)
if err != nil {
http.NotFound(w, req)
return
}
if !soapcalls.UpdateMRstate(previousstate, newstate, uuid) {
http.NotFound(w, req)
return
}
switch newstate {
case "PLAYING":
Emit(screen, "Playing")
case "PAUSED_PLAYBACK":
Emit(screen, "Paused")
case "STOPPED":
Emit(screen, "Stopped")
tv.UnsubscribeSoapCall(uuid)
Close(screen)
}
}
}
// StopServeFiles .
func (s *HTTPserver) StopServeFiles() {
s.http.Close()
}
// NewServer - create a new HTTP server.
func NewServer(a string) *HTTPserver {
mux := http.NewServeMux()
srv := HTTPserver{
http: &http.Server{Addr: a, Handler: mux},
mux: mux,
}
return &srv
}
func serveContent(w http.ResponseWriter, r *http.Request, tv *soapcalls.TVPayload, s interface{}, isMedia bool) {
respHeader := w.Header()
if isMedia {
respHeader["transferMode.dlna.org"] = []string{"Streaming"}
respHeader["realTimeInfo.dlna.org"] = []string{"DLNA.ORG_TLAG=*"}
} else {
respHeader["transferMode.dlna.org"] = []string{"Interactive"}
}
var mediaType string
if tv != nil {
mediaType = tv.MediaType
}
switch f := s.(type) {
case string:
if r.Header.Get("getcontentFeatures.dlna.org") == "1" {
contentFeatures, err := utils.BuildContentFeatures(mediaType, "01", false)
if err != nil {
http.NotFound(w, r)
return
}
respHeader["contentFeatures.dlna.org"] = []string{contentFeatures}
}
filePath, err := os.Open(f)
if err != nil {
http.NotFound(w, r)
return
}
defer filePath.Close()
fileStat, err := filePath.Stat()
if err != nil {
http.NotFound(w, r)
return
}
name := strings.TrimLeft(r.URL.Path, "/")
http.ServeContent(w, r, name, fileStat.ModTime(), filePath)
case []byte:
if r.Header.Get("getcontentFeatures.dlna.org") == "1" {
contentFeatures, err := utils.BuildContentFeatures(mediaType, "01", false)
if err != nil {
http.NotFound(w, r)
return
}
respHeader["contentFeatures.dlna.org"] = []string{contentFeatures}
}
bReader := bytes.NewReader(f)
name := strings.TrimLeft(r.URL.Path, "/")
http.ServeContent(w, r, name, time.Now(), bReader)
case io.ReadCloser:
if r.Header.Get("getcontentFeatures.dlna.org") == "1" {
contentFeatures, err := utils.BuildContentFeatures(mediaType, "00", false)
if err != nil {
http.NotFound(w, r)
return
}
respHeader["contentFeatures.dlna.org"] = []string{contentFeatures}
}
// No seek support
if r.Method == http.MethodGet {
io.Copy(w, f)
f.Close()
} else {
w.WriteHeader(http.StatusOK)
}
default:
http.NotFound(w, r)
return
}
}