diff --git a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs index 3dcf044f7..ca9214b54 100644 --- a/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs +++ b/MediaBrowser.Common.Implementations/HttpClientManager/HttpClientManager.cs @@ -105,48 +105,12 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager return client; } - private WebRequest GetMonoRequest(HttpRequestOptions options, string method, bool enableHttpCompression) - { - var request = (HttpWebRequest)WebRequest.Create(options.Url); - - if (!string.IsNullOrEmpty(options.AcceptHeader)) - { - request.Accept = options.AcceptHeader; - } - - request.AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None; - - request.CachePolicy = options.CachePolicy == Net.HttpRequestCachePolicy.None ? - new RequestCachePolicy(RequestCacheLevel.BypassCache) : - new RequestCachePolicy(RequestCacheLevel.Revalidate); - - request.ConnectionGroupName = GetHostFromUrl(options.Url); - request.KeepAlive = true; - request.Method = method; - request.Pipelined = true; - request.Timeout = 20000; - - if (!string.IsNullOrEmpty(options.UserAgent)) - { - request.UserAgent = options.UserAgent; - } - - return request; - } - private PropertyInfo _httpBehaviorPropertyInfo; private WebRequest GetRequest(HttpRequestOptions options, string method, bool enableHttpCompression) { -#if __MonoCS__ - return GetMonoRequest(options, method, enableHttpCompression); -#endif + var request = (HttpWebRequest)WebRequest.Create(options.Url); - var request = HttpWebRequest.CreateHttp(options.Url); - - if (!string.IsNullOrEmpty(options.AcceptHeader)) - { - request.Accept = options.AcceptHeader; - } + AddRequestHeaders(request, options); request.AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None; @@ -160,11 +124,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager request.Pipelined = true; request.Timeout = 20000; - if (!string.IsNullOrEmpty(options.UserAgent)) - { - request.UserAgent = options.UserAgent; - } - +#if !__MonoCS__ // This is a hack to prevent KeepAlive from getting disabled internally by the HttpWebRequest // May need to remove this for mono var sp = request.ServicePoint; @@ -173,10 +133,30 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager _httpBehaviorPropertyInfo = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic); } _httpBehaviorPropertyInfo.SetValue(sp, (byte)0, null); +#endif return request; } + private void AddRequestHeaders(HttpWebRequest request, HttpRequestOptions options) + { + foreach (var header in options.RequestHeaders.ToList()) + { + if (string.Equals(header.Key, "Accept", StringComparison.OrdinalIgnoreCase)) + { + request.Accept = header.Value; + } + else if (string.Equals(header.Key, "User-Agent", StringComparison.OrdinalIgnoreCase)) + { + request.UserAgent = header.Value; + } + else + { + request.Headers.Set(header.Key, header.Value); + } + } + } + /// /// Gets the response internal. /// diff --git a/MediaBrowser.Dlna/PlayTo/Device.cs b/MediaBrowser.Dlna/PlayTo/Device.cs index b6de29af6..0bd3d1448 100644 --- a/MediaBrowser.Dlna/PlayTo/Device.cs +++ b/MediaBrowser.Dlna/PlayTo/Device.cs @@ -357,18 +357,26 @@ namespace MediaBrowser.Dlna.PlayTo return; ((Timer)sender).Stop(); - var hasTrack = await GetPositionInfo().ConfigureAwait(false); - // TODO: Why make these requests if hasTrack==false? - if (_count > 4) + try { - await GetTransportInfo().ConfigureAwait(false); - if (!hasTrack) + var hasTrack = await GetPositionInfo().ConfigureAwait(false); + + // TODO: Why make these requests if hasTrack==false? + if (_count > 4) { - await GetMediaInfo().ConfigureAwait(false); + await GetTransportInfo().ConfigureAwait(false); + if (!hasTrack) + { + await GetMediaInfo().ConfigureAwait(false); + } + await GetVolume().ConfigureAwait(false); + _count = 0; } - await GetVolume().ConfigureAwait(false); - _count = 0; + } + catch (Exception ex) + { + _logger.ErrorException("Error updating device info", ex); } _count++; @@ -390,18 +398,8 @@ namespace MediaBrowser.Dlna.PlayTo throw new InvalidOperationException("Unable to find service"); } - XDocument result; - - try - { - result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) - .ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error getting volume info", ex); - return; - } + var result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) + .ConfigureAwait(false); if (result == null || result.Document == null) return; @@ -431,18 +429,8 @@ namespace MediaBrowser.Dlna.PlayTo if (service == null) return; - XDocument result; - - try - { - result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) - .ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error getting transport info", ex); - return; - } + var result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) + .ConfigureAwait(false); if (result == null || result.Document == null) return; @@ -471,18 +459,8 @@ namespace MediaBrowser.Dlna.PlayTo throw new InvalidOperationException("Unable to find service"); } - XDocument result; - - try - { - result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) - .ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error getting media info", ex); - return; - } + var result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) + .ConfigureAwait(false); if (result == null || result.Document == null) return; @@ -522,18 +500,8 @@ namespace MediaBrowser.Dlna.PlayTo throw new InvalidOperationException("Unable to find service"); } - XDocument result; - - try - { - result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) - .ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.ErrorException("Error getting position info", ex); - return false; - } + var result = await new SsdpHttpClient(_httpClient).SendCommandAsync(Properties.BaseUrl, service, command.Name, RendererCommands.BuildPost(command, service.ServiceType)) + .ConfigureAwait(false); if (result == null || result.Document == null) return true; diff --git a/MediaBrowser.Dlna/PlayTo/PlayToManager.cs b/MediaBrowser.Dlna/PlayTo/PlayToManager.cs index 1e81f32f8..59827b810 100644 --- a/MediaBrowser.Dlna/PlayTo/PlayToManager.cs +++ b/MediaBrowser.Dlna/PlayTo/PlayToManager.cs @@ -23,15 +23,15 @@ namespace MediaBrowser.Dlna.PlayTo private readonly ILogger _logger; private readonly ISessionManager _sessionManager; private readonly IHttpClient _httpClient; - private User _defualtUser; private readonly CancellationTokenSource _tokenSource; private ConcurrentDictionary _locations; private readonly IItemRepository _itemRepository; private readonly ILibraryManager _libraryManager; private readonly INetworkManager _networkManager; - - public PlayToManager(ILogger logger, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepository, ILibraryManager libraryManager, INetworkManager networkManager) + private readonly IUserManager _userManager; + + public PlayToManager(ILogger logger, ISessionManager sessionManager, IHttpClient httpClient, IItemRepository itemRepository, ILibraryManager libraryManager, INetworkManager networkManager, IUserManager userManager) { _locations = new ConcurrentDictionary(); _tokenSource = new CancellationTokenSource(); @@ -42,11 +42,11 @@ namespace MediaBrowser.Dlna.PlayTo _itemRepository = itemRepository; _libraryManager = libraryManager; _networkManager = networkManager; + _userManager = userManager; } - public async void Start(User defaultUser) + public async void Start() { - _defualtUser = defaultUser; _logger.Log(LogSeverity.Info, "PlayTo-Manager starting"); _locations = new ConcurrentDictionary(); @@ -216,7 +216,7 @@ namespace MediaBrowser.Dlna.PlayTo { var transcodeProfiles = TranscodeSettings.GetProfileSettings(device.Properties); - var sessionInfo = await _sessionManager.LogSessionActivity(device.Properties.ClientType, device.Properties.Name, device.Properties.UUID, device.Properties.DisplayName, uri.OriginalString, _defualtUser) + var sessionInfo = await _sessionManager.LogSessionActivity(device.Properties.ClientType, device.Properties.Name, device.Properties.UUID, device.Properties.DisplayName, uri.OriginalString, null) .ConfigureAwait(false); var controller = sessionInfo.SessionController as PlayToController; @@ -232,6 +232,23 @@ namespace MediaBrowser.Dlna.PlayTo } } + const string DefaultUser = "Play To"; + private async Task GetPlayToUser() + { + var user = _userManager.Users.FirstOrDefault(u => string.Equals(DefaultUser, u.Name, StringComparison.OrdinalIgnoreCase)); + + if (user == null) + { + user = await _userManager.CreateUser(DefaultUser); + + user.Configuration.IsHidden = true; + user.Configuration.IsAdministrator = false; + user.SaveConfiguration(); + } + + return user; + } + /// /// Determines if the Uri is valid for further inspection or not. /// (the limit for reinspection is 5 minutes) diff --git a/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs b/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs index e998d13c1..41d5fec10 100644 --- a/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs +++ b/MediaBrowser.Dlna/PlayTo/PlayToServerEntryPoint.cs @@ -1,57 +1,28 @@ using MediaBrowser.Common.Net; -using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Logging; -using System.Linq; -using System.Threading.Tasks; namespace MediaBrowser.Dlna.PlayTo { public class PlayToServerEntryPoint : IServerEntryPoint { - const string DefaultUser = "Play To"; - private bool _disposed; - - private readonly IUserManager _userManager; + private readonly PlayToManager _manager; public PlayToServerEntryPoint(ILogManager logManager, ISessionManager sessionManager, IUserManager userManager, IHttpClient httpClient, INetworkManager networkManager, IItemRepository itemRepository, ILibraryManager libraryManager) { - _userManager = userManager; - - _manager = new PlayToManager(logManager.GetLogger("PlayTo"), sessionManager, httpClient, itemRepository, libraryManager, networkManager); + _manager = new PlayToManager(logManager.GetLogger("PlayTo"), sessionManager, httpClient, itemRepository, libraryManager, networkManager, userManager); } - /// - /// Creates the defaultuser if needed. - /// - private async Task CreateUserIfNeeded() + public void Run() { - var user = _userManager.Users.FirstOrDefault(u => u.Name == DefaultUser); - - if (user == null) - { - user = await _userManager.CreateUser(DefaultUser); - - user.Configuration.IsHidden = true; - user.Configuration.IsAdministrator = false; - user.SaveConfiguration(); - } - - return user; + //_manager.Start(); } - public async void Run() - { - //var defaultUser = await CreateUserIfNeeded().ConfigureAwait(false); - - //_manager.Start(defaultUser); - } - #region Dispose public void Dispose() diff --git a/MediaBrowser.Dlna/PlayTo/SsdpHttpClient.cs b/MediaBrowser.Dlna/PlayTo/SsdpHttpClient.cs index 2c57f998c..7179ef762 100644 --- a/MediaBrowser.Dlna/PlayTo/SsdpHttpClient.cs +++ b/MediaBrowser.Dlna/PlayTo/SsdpHttpClient.cs @@ -44,10 +44,10 @@ namespace MediaBrowser.Dlna.PlayTo { var options = new HttpRequestOptions { - Url = url.ToString() + Url = url.ToString(), + UserAgent = USERAGENT }; - options.RequestHeaders["UserAgent"] = USERAGENT; options.RequestHeaders["HOST"] = ip + ":" + port; options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport + ">"; options.RequestHeaders["NT"] = "upnp:event"; @@ -63,10 +63,10 @@ namespace MediaBrowser.Dlna.PlayTo { var options = new HttpRequestOptions { - Url = url.ToString() + Url = url.ToString(), + UserAgent = USERAGENT }; - options.RequestHeaders["UserAgent"] = USERAGENT; options.RequestHeaders["HOST"] = ip + ":" + port; options.RequestHeaders["CALLBACK"] = "<" + localIp + ":" + eventport + ">"; options.RequestHeaders["NT"] = "upnp:event"; @@ -82,10 +82,10 @@ namespace MediaBrowser.Dlna.PlayTo { var options = new HttpRequestOptions { - Url = url.ToString() + Url = url.ToString(), + UserAgent = USERAGENT }; - options.RequestHeaders["UserAgent"] = USERAGENT; options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName; //request.CookieContainer = Container; @@ -105,12 +105,12 @@ namespace MediaBrowser.Dlna.PlayTo var options = new HttpRequestOptions { - Url = url.ToString() + Url = url.ToString(), + UserAgent = USERAGENT }; options.RequestHeaders["SOAPAction"] = soapAction; options.RequestHeaders["Pragma"] = "no-cache"; - options.RequestHeaders["UserAgent"] = USERAGENT; options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName; if (!string.IsNullOrWhiteSpace(header))