@@ -26,26 +26,40 @@ var (
26
26
reLocalAddresses = regexp .MustCompile (` localAddresses="[^"]*"` )
27
27
)
28
28
29
- func sslMiddleware (next http.Handler ) http.Handler {
29
+ func plexTvMiddleware (next http.Handler ) http.Handler {
30
30
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
31
- if plexClient .sslHost != "" && r .Body != nil {
32
- name := "Connection[][uri]"
33
- query := r .URL .Query ()
34
- if query .Has (name ) {
35
- query .Set (name , "https://" + plexClient .sslHost )
36
- }
37
- nr := cloneRequest (r , r .Header , query )
31
+ if plexClient .sslHost == "" {
32
+ next .ServeHTTP (w , r )
33
+ return
34
+ }
35
+
36
+ var bodyBytes []byte
37
+ path := r .URL .EscapedPath ()
38
+ query := r .URL .Query ()
39
+ if r .Body != nil {
40
+ bodyBytes , _ = io .ReadAll (r .Body )
41
+ common .GetLogger ().Printf ("%s" , string (bodyBytes ))
42
+ }
38
43
39
- bodyBytes , _ := io .ReadAll (r .Body )
44
+ switch path {
45
+ case "/servers.xml" :
40
46
sslHost := fmt .Sprintf ("address=\" %s\" scheme=\" https\" " , plexClient .sslHost )
41
- modifiedBody := bytes .ReplaceAll (bodyBytes , []byte ("host=\" \" " ), []byte (sslHost ))
42
- modifiedBody = reLocalAddresses .ReplaceAll (modifiedBody , []byte ("" ))
43
- nr .Body = io .NopCloser (bytes .NewReader (modifiedBody ))
47
+ bodyBytes = bytes .ReplaceAll (bodyBytes , []byte ("host=\" \" " ), []byte (sslHost ))
48
+ bodyBytes = reLocalAddresses .ReplaceAll (bodyBytes , []byte ("" ))
49
+ default :
50
+ if strings .HasPrefix (path , "/devices/" ) {
51
+ name := "Connection[][uri]"
52
+ if query .Has (name ) {
53
+ query .Set (name , "https://" + plexClient .sslHost )
54
+ }
55
+ }
56
+ }
44
57
45
- next . ServeHTTP ( w , nr )
46
- } else {
47
- next . ServeHTTP ( w , r )
58
+ nr := cloneRequest ( r , r . Header , query )
59
+ if bodyBytes != nil {
60
+ nr . Body = io . NopCloser ( bytes . NewReader ( bodyBytes ) )
48
61
}
62
+ next .ServeHTTP (w , nr )
49
63
})
50
64
}
51
65
0 commit comments