diff --git a/GreenshotPlugin/Core/NetworkHelper.cs b/GreenshotPlugin/Core/NetworkHelper.cs index 5a9ec8877..de8ff9204 100644 --- a/GreenshotPlugin/Core/NetworkHelper.cs +++ b/GreenshotPlugin/Core/NetworkHelper.cs @@ -52,11 +52,17 @@ namespace GreenshotPlugin.Core { private static readonly CoreConfiguration Config = IniConfig.GetIniSection(); static NetworkHelper() { - // Disable certificate checking - ServicePointManager.ServerCertificateValidationCallback += - delegate { - return true; - }; + try + { + // Disable certificate checking + ServicePointManager.ServerCertificateValidationCallback += delegate { + return true; + }; + } + catch (Exception ex) + { + LOG.Warn("An error has occured while allowing self-signed certificates:", ex); + } } /// @@ -137,12 +143,18 @@ namespace GreenshotPlugin.Core { using (StreamReader streamReader = new StreamReader(memoryStream, Encoding.UTF8, true)) { content = streamReader.ReadLine(); } - Regex imageUrlRegex = new Regex(@"(http|https)://.*(\.png|\.gif|\.jpg|\.tiff|\.jpeg|\.bmp)"); - Match match = imageUrlRegex.Match(content); - if (match.Success) { - using (MemoryStream memoryStream2 = GetAsMemoryStream(match.Value)) { - using (Image image = Image.FromStream(memoryStream2)) { - return ImageHelper.Clone(image, PixelFormat.Format32bppArgb); + if (!string.IsNullOrEmpty(content)) + { + Regex imageUrlRegex = new Regex(@"(http|https)://.*(\.png|\.gif|\.jpg|\.tiff|\.jpeg|\.bmp)"); + Match match = imageUrlRegex.Match(content); + if (match.Success) + { + using (MemoryStream memoryStream2 = GetAsMemoryStream(match.Value)) + { + using (Image image = Image.FromStream(memoryStream2)) + { + return ImageHelper.Clone(image, PixelFormat.Format32bppArgb); + } } } } @@ -193,12 +205,7 @@ namespace GreenshotPlugin.Core { /// WebRequest public static HttpWebRequest CreateWebRequest(Uri uri) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); - if (Config.UseProxy) { - webRequest.Proxy = CreateProxy(uri); - } else { - // BUG-1655: Fix that Greenshot always uses the default proxy even if the "use default proxy" checkbox is unset - webRequest.Proxy = null; - } + webRequest.Proxy = Config.UseProxy ? CreateProxy(uri) : null; // Make sure the default credentials are available webRequest.Credentials = CredentialCache.DefaultCredentials; @@ -396,9 +403,10 @@ namespace GreenshotPlugin.Core { /// Post the parameters "x-www-form-urlencoded" /// /// + /// public static void UploadFormUrlEncoded(HttpWebRequest webRequest, IDictionary parameters) { webRequest.ContentType = "application/x-www-form-urlencoded"; - string urlEncoded = NetworkHelper.GenerateQueryParameters(parameters); + string urlEncoded = GenerateQueryParameters(parameters); byte[] data = Encoding.UTF8.GetBytes(urlEncoded); using (var requestStream = webRequest.GetRequestStream()) { @@ -460,6 +468,7 @@ namespace GreenshotPlugin.Core { /// /// /// + /// /// public static string GetResponseAsString(HttpWebRequest webRequest, bool alsoReturnContentOnError) { string responseData = null; @@ -552,10 +561,10 @@ namespace GreenshotPlugin.Core { /// A container to supply files to a Multi-part form data upload /// public class ByteContainer : IBinaryContainer { - private readonly byte[] file; - private readonly string fileName; - private readonly string contentType; - private readonly int fileSize; + private readonly byte[] _file; + private readonly string _fileName; + private readonly string _contentType; + private readonly int _fileSize; public ByteContainer(byte[] file) : this(file, null) { } public ByteContainer(byte[] file, string filename) : this(file, filename, null) { @@ -563,14 +572,10 @@ namespace GreenshotPlugin.Core { public ByteContainer(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) { } public ByteContainer(byte[] file, string filename, string contenttype, int filesize) { - this.file = file; - fileName = filename; - contentType = contenttype; - if (filesize == 0) { - fileSize = file.Length; - } else { - fileSize = filesize; - } + _file = file; + _fileName = filename; + _contentType = contenttype; + _fileSize = filesize == 0 ? file.Length : filesize; } /// @@ -578,7 +583,7 @@ namespace GreenshotPlugin.Core { /// /// string public string ToBase64String(Base64FormattingOptions formattingOptions) { - return Convert.ToBase64String(file, 0, fileSize, formattingOptions); + return Convert.ToBase64String(_file, 0, _fileSize, formattingOptions); } /// @@ -586,7 +591,7 @@ namespace GreenshotPlugin.Core { /// /// byte[] public byte[] ToByteArray() { - return file; + return _file; } /// @@ -600,13 +605,13 @@ namespace GreenshotPlugin.Core { string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n", boundary, name, - fileName ?? name, - contentType ?? "application/octet-stream"); + _fileName ?? name, + _contentType ?? "application/octet-stream"); formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header)); // Write the file data directly to the Stream, rather than serializing it to a string. - formDataStream.Write(file, 0, fileSize); + formDataStream.Write(_file, 0, _fileSize); } /// @@ -615,7 +620,7 @@ namespace GreenshotPlugin.Core { /// Stream to write to public void WriteToStream(Stream dataStream) { // Write the file data directly to the Stream, rather than serializing it to a string. - dataStream.Write(file, 0, fileSize); + dataStream.Write(_file, 0, _fileSize); } /// @@ -623,8 +628,8 @@ namespace GreenshotPlugin.Core { /// /// public void Upload(HttpWebRequest webRequest) { - webRequest.ContentType = contentType; - webRequest.ContentLength = fileSize; + webRequest.ContentType = _contentType; + webRequest.ContentLength = _fileSize; using (var requestStream = webRequest.GetRequestStream()) { WriteToStream(requestStream); } @@ -635,14 +640,14 @@ namespace GreenshotPlugin.Core { /// A container to supply images to a Multi-part form data upload /// public class BitmapContainer : IBinaryContainer { - private readonly Bitmap bitmap; - private readonly SurfaceOutputSettings outputSettings; - private readonly string fileName; + private readonly Bitmap _bitmap; + private readonly SurfaceOutputSettings _outputSettings; + private readonly string _fileName; public BitmapContainer(Bitmap bitmap, SurfaceOutputSettings outputSettings, string filename) { - this.bitmap = bitmap; - this.outputSettings = outputSettings; - fileName = filename; + _bitmap = bitmap; + _outputSettings = outputSettings; + _fileName = filename; } /// @@ -652,7 +657,7 @@ namespace GreenshotPlugin.Core { /// string public string ToBase64String(Base64FormattingOptions formattingOptions) { using (MemoryStream stream = new MemoryStream()) { - ImageOutput.SaveToStream(bitmap, null, stream, outputSettings); + ImageOutput.SaveToStream(_bitmap, null, stream, _outputSettings); return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions); } } @@ -664,7 +669,7 @@ namespace GreenshotPlugin.Core { /// byte[] public byte[] ToByteArray() { using (MemoryStream stream = new MemoryStream()) { - ImageOutput.SaveToStream(bitmap, null, stream, outputSettings); + ImageOutput.SaveToStream(_bitmap, null, stream, _outputSettings); return stream.ToArray(); } } @@ -680,11 +685,11 @@ namespace GreenshotPlugin.Core { string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n", boundary, name, - fileName ?? name, - "image/" + outputSettings.Format.ToString()); + _fileName ?? name, + "image/" + _outputSettings.Format); formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header)); - ImageOutput.SaveToStream(bitmap, null, formDataStream, outputSettings); + ImageOutput.SaveToStream(_bitmap, null, formDataStream, _outputSettings); } /// @@ -693,7 +698,7 @@ namespace GreenshotPlugin.Core { /// public void WriteToStream(Stream dataStream) { // Write the file data directly to the Stream, rather than serializing it to a string. - ImageOutput.SaveToStream(bitmap, null, dataStream, outputSettings); + ImageOutput.SaveToStream(_bitmap, null, dataStream, _outputSettings); } /// @@ -701,7 +706,7 @@ namespace GreenshotPlugin.Core { /// /// public void Upload(HttpWebRequest webRequest) { - webRequest.ContentType = "image/" + outputSettings.Format.ToString(); + webRequest.ContentType = "image/" + _outputSettings.Format; using (var requestStream = webRequest.GetRequestStream()) { WriteToStream(requestStream); } @@ -712,14 +717,14 @@ namespace GreenshotPlugin.Core { /// A container to supply surfaces to a Multi-part form data upload /// public class SurfaceContainer : IBinaryContainer { - private readonly ISurface surface; - private readonly SurfaceOutputSettings outputSettings; - private readonly string fileName; + private readonly ISurface _surface; + private readonly SurfaceOutputSettings _outputSettings; + private readonly string _fileName; public SurfaceContainer(ISurface surface, SurfaceOutputSettings outputSettings, string filename) { - this.surface = surface; - this.outputSettings = outputSettings; - fileName = filename; + _surface = surface; + _outputSettings = outputSettings; + _fileName = filename; } /// @@ -729,7 +734,7 @@ namespace GreenshotPlugin.Core { /// string public string ToBase64String(Base64FormattingOptions formattingOptions) { using (MemoryStream stream = new MemoryStream()) { - ImageOutput.SaveToStream(surface, stream, outputSettings); + ImageOutput.SaveToStream(_surface, stream, _outputSettings); return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions); } } @@ -741,7 +746,7 @@ namespace GreenshotPlugin.Core { /// byte[] public byte[] ToByteArray() { using (MemoryStream stream = new MemoryStream()) { - ImageOutput.SaveToStream(surface, stream, outputSettings); + ImageOutput.SaveToStream(_surface, stream, _outputSettings); return stream.ToArray(); } } @@ -757,11 +762,11 @@ namespace GreenshotPlugin.Core { string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n", boundary, name, - fileName ?? name, - "image/" + outputSettings.Format); + _fileName ?? name, + "image/" + _outputSettings.Format); formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header)); - ImageOutput.SaveToStream(surface, formDataStream, outputSettings); + ImageOutput.SaveToStream(_surface, formDataStream, _outputSettings); } /// @@ -770,7 +775,7 @@ namespace GreenshotPlugin.Core { /// public void WriteToStream(Stream dataStream) { // Write the file data directly to the Stream, rather than serializing it to a string. - ImageOutput.SaveToStream(surface, dataStream, outputSettings); + ImageOutput.SaveToStream(_surface, dataStream, _outputSettings); } /// @@ -778,7 +783,7 @@ namespace GreenshotPlugin.Core { /// /// public void Upload(HttpWebRequest webRequest) { - webRequest.ContentType = "image/" + outputSettings.Format.ToString(); + webRequest.ContentType = "image/" + _outputSettings.Format.ToString(); using (var requestStream = webRequest.GetRequestStream()) { WriteToStream(requestStream); }