From c2361db772591e176fcf2ded2e3b1725cf1c5dd7 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 22 Oct 2017 19:27:09 -0400 Subject: [PATCH] separate encoding from content type values --- .../ContentDirectoryBrowser.cs | 2 +- Emby.Dlna/PlayTo/SsdpHttpClient.cs | 18 ++++++++--- .../HttpClientManager/HttpClientManager.cs | 32 ++++++++++++++++--- MediaBrowser.Common/Net/HttpRequestOptions.cs | 4 +++ Mono.Nat/Upnp/Messages/GetServicesMessage.cs | 4 +++ Mono.Nat/Upnp/Messages/UpnpMessage.cs | 7 +++- 6 files changed, 56 insertions(+), 11 deletions(-) diff --git a/Emby.Dlna/ContentDirectory/ContentDirectoryBrowser.cs b/Emby.Dlna/ContentDirectory/ContentDirectoryBrowser.cs index 9c5c5fb64..61501635f 100644 --- a/Emby.Dlna/ContentDirectory/ContentDirectoryBrowser.cs +++ b/Emby.Dlna/ContentDirectory/ContentDirectoryBrowser.cs @@ -33,7 +33,7 @@ namespace Emby.Dlna.ContentDirectory { CancellationToken = cancellationToken, UserAgent = "Emby", - RequestContentType = "text/xml; charset=\"utf-8\"", + RequestContentType = "text/xml", LogErrorResponseBody = true, Url = request.ContentDirectoryUrl, BufferContent = false diff --git a/Emby.Dlna/PlayTo/SsdpHttpClient.cs b/Emby.Dlna/PlayTo/SsdpHttpClient.cs index 6c66a999c..40fe010a2 100644 --- a/Emby.Dlna/PlayTo/SsdpHttpClient.cs +++ b/Emby.Dlna/PlayTo/SsdpHttpClient.cs @@ -72,7 +72,10 @@ namespace Emby.Dlna.PlayTo Url = url, UserAgent = USERAGENT, LogErrorResponseBody = true, - BufferContent = false + BufferContent = false, + + // The periodic requests may keep some devices awake + LogRequestAsDebug = true }; options.RequestHeaders["HOST"] = ip + ":" + port.ToString(_usCulture); @@ -93,7 +96,10 @@ namespace Emby.Dlna.PlayTo Url = url, UserAgent = USERAGENT, LogErrorResponseBody = true, - BufferContent = false + BufferContent = false, + + // The periodic requests may keep some devices awake + LogRequestAsDebug = true }; options.RequestHeaders["FriendlyName.DLNA.ORG"] = FriendlyName; @@ -125,7 +131,10 @@ namespace Emby.Dlna.PlayTo UserAgent = USERAGENT, LogRequest = logRequest || _config.GetDlnaConfiguration().EnableDebugLog, LogErrorResponseBody = true, - BufferContent = false + BufferContent = false, + + // The periodic requests may keep some devices awake + LogRequestAsDebug = true }; options.RequestHeaders["SOAPAction"] = soapAction; @@ -137,7 +146,8 @@ namespace Emby.Dlna.PlayTo options.RequestHeaders["contentFeatures.dlna.org"] = header; } - options.RequestContentType = "text/xml; charset=\"utf-8\""; + options.RequestContentType = "text/xml"; + options.RequestContentEncoding = Encoding.UTF8; options.RequestContent = postData; return _httpClient.Post(options); diff --git a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs index 3fdd05135..ae53e3a5b 100644 --- a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs +++ b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs @@ -320,8 +320,6 @@ namespace Emby.Server.Implementations.HttpClientManager private async Task GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) { - _logger.Info("Checking for cache file {0}", responseCachePath); - try { if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow) @@ -402,7 +400,17 @@ namespace Emby.Server.Implementations.HttpClientManager var bytes = options.RequestContentBytes ?? Encoding.UTF8.GetBytes(options.RequestContent ?? string.Empty); - httpWebRequest.ContentType = options.RequestContentType ?? "application/x-www-form-urlencoded"; + var contentType = options.RequestContentType ?? "application/x-www-form-urlencoded"; + + if (options.RequestContentEncoding != null) + { + if (options.RequestContentEncoding.Equals(Encoding.UTF8)) + { + contentType = contentType.TrimEnd(';') + "; charset=\"utf-8\""; + } + } + + httpWebRequest.ContentType = contentType; httpWebRequest.ContentLength = bytes.Length; (await httpWebRequest.GetRequestStreamAsync().ConfigureAwait(false)).Write(bytes, 0, bytes.Length); @@ -430,7 +438,14 @@ namespace Emby.Server.Implementations.HttpClientManager if (options.LogRequest) { - _logger.Info("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url); + if (options.LogRequestAsDebug) + { + _logger.Debug("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url); + } + else + { + _logger.Info("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url); + } } try @@ -597,7 +612,14 @@ namespace Emby.Server.Implementations.HttpClientManager if (options.LogRequest) { - _logger.Info("HttpClientManager.GetTempFileResponse url: {0}", options.Url); + if (options.LogRequestAsDebug) + { + _logger.Debug("HttpClientManager.GetTempFileResponse url: {0}", options.Url); + } + else + { + _logger.Info("HttpClientManager.GetTempFileResponse url: {0}", options.Url); + } } var client = GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression); diff --git a/MediaBrowser.Common/Net/HttpRequestOptions.cs b/MediaBrowser.Common/Net/HttpRequestOptions.cs index 4a894e662..0950df021 100644 --- a/MediaBrowser.Common/Net/HttpRequestOptions.cs +++ b/MediaBrowser.Common/Net/HttpRequestOptions.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading; +using System.Text; namespace MediaBrowser.Common.Net { @@ -90,6 +91,7 @@ namespace MediaBrowser.Common.Net public bool BufferContent { get; set; } public bool LogRequest { get; set; } + public bool LogRequestAsDebug { get; set; } public bool LogErrors { get; set; } public bool LogErrorResponseBody { get; set; } @@ -102,6 +104,8 @@ namespace MediaBrowser.Common.Net public bool PreferIpv4 { get; set; } public bool EnableDefaultUserAgent { get; set; } + public Encoding RequestContentEncoding { get; set; } + private string GetHeaderValue(string name) { string value; diff --git a/Mono.Nat/Upnp/Messages/GetServicesMessage.cs b/Mono.Nat/Upnp/Messages/GetServicesMessage.cs index 9d29f98fd..3395b7596 100644 --- a/Mono.Nat/Upnp/Messages/GetServicesMessage.cs +++ b/Mono.Nat/Upnp/Messages/GetServicesMessage.cs @@ -64,6 +64,10 @@ namespace Mono.Nat.Upnp { var req = new HttpRequestOptions(); + // The periodic request logging may keep some devices awake + req.LogRequestAsDebug = true; + req.LogErrors = false; + req.Url = "http://" + this.hostAddress.ToString() + this.servicesDescriptionUrl; req.RequestHeaders.Add("ACCEPT-LANGUAGE", "en"); diff --git a/Mono.Nat/Upnp/Messages/UpnpMessage.cs b/Mono.Nat/Upnp/Messages/UpnpMessage.cs index d15e48b2f..05280172b 100644 --- a/Mono.Nat/Upnp/Messages/UpnpMessage.cs +++ b/Mono.Nat/Upnp/Messages/UpnpMessage.cs @@ -52,9 +52,14 @@ namespace Mono.Nat.Upnp var req = new HttpRequestOptions(); req.LogErrors = false; + + // The periodic request logging may keep some devices awake + req.LogRequestAsDebug = true; + req.Url = ss; req.EnableKeepAlive = false; - req.RequestContentType = "text/xml; charset=\"utf-8\""; + req.RequestContentType = "text/xml"; + req.RequestContentEncoding = Encoding.UTF8; req.RequestHeaders.Add("SOAPACTION", "\"" + device.ServiceType + "#" + upnpMethod + "\""); string bodyString = "