/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2015 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using Greenshot.IniFile; using Greenshot.Plugin; using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Globalization; using System.IO; using System.Net; using System.Text; using System.Text.RegularExpressions; namespace GreenshotPlugin.Core { /// /// HTTP Method to make sure we have the correct method /// public enum HTTPMethod { GET, POST, PUT, DELETE, HEAD }; /// /// Description of NetworkHelper. /// public static class NetworkHelper { private static readonly CoreConfiguration Config = IniConfig.GetIniSection(); static NetworkHelper() { try { // Disable certificate checking ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; } catch (Exception ex) { LOG.Warn("An error has occured while allowing self-signed certificates:", ex); } } /// /// Download a uri response as string /// /// An Uri to specify the download location /// string with the file content public static string GetAsString(Uri uri) { return GetResponseAsString(CreateWebRequest(uri)); } /// /// Download the FavIcon as a Bitmap /// /// /// Bitmap with the FavIcon public static Bitmap DownloadFavIcon(Uri baseUri) { Uri url = new Uri(baseUri, new Uri("favicon.ico")); try { HttpWebRequest request = CreateWebRequest(url); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (request.HaveResponse) { using (Stream responseStream = response.GetResponseStream()) { if (responseStream != null) { using (Image image = Image.FromStream(responseStream)) { return (image.Height > 16 && image.Width > 16) ? new Bitmap(image, 16, 16) : new Bitmap(image); } } } } } } catch (Exception e) { LOG.Error("Problem downloading the FavIcon from: " + baseUri, e); } return null; } /// /// Download the uri into a memorystream, without catching exceptions /// /// Of an image /// MemoryStream which is already seeked to 0 public static MemoryStream GetAsMemoryStream(string url) { HttpWebRequest request = CreateWebRequest(url); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { MemoryStream memoryStream = new MemoryStream(); using (Stream responseStream = response.GetResponseStream()) { if (responseStream != null) { responseStream.CopyTo(memoryStream); } // Make sure it can be used directly memoryStream.Seek(0, SeekOrigin.Begin); } return memoryStream; } } /// /// Download the uri to Bitmap /// /// Of an image /// Bitmap public static Image DownloadImage(string url) { try { string content; using (MemoryStream memoryStream = GetAsMemoryStream(url)) { try { using (Image image = Image.FromStream(memoryStream)) { return ImageHelper.Clone(image, PixelFormat.Format32bppArgb); } } catch (Exception) { // If we arrive here, the image loading didn't work, try to see if the response has a http(s) URL to an image and just take this instead. using (StreamReader streamReader = new StreamReader(memoryStream, Encoding.UTF8, true)) { content = streamReader.ReadLine(); } 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); } } } } throw; } } } catch (Exception e) { LOG.Error("Problem downloading the image from: " + url, e); } return null; } /// /// Helper method to create a web request with a lot of default settings /// /// string with uri to connect to /// WebRequest public static HttpWebRequest CreateWebRequest(string uri) { return CreateWebRequest(new Uri(uri)); } /// /// Helper method to create a web request with a lot of default settings /// /// string with uri to connect to /// /// Method to use /// WebRequest public static HttpWebRequest CreateWebRequest(string uri, HTTPMethod method) { return CreateWebRequest(new Uri(uri), method); } /// /// Helper method to create a web request with a lot of default settings /// /// Uri with uri to connect to /// Method to use /// WebRequest public static HttpWebRequest CreateWebRequest(Uri uri, HTTPMethod method) { HttpWebRequest webRequest = CreateWebRequest(uri); webRequest.Method = method.ToString(); return webRequest; } /// /// Helper method to create a web request, eventually with proxy /// /// Uri with uri to connect to /// WebRequest public static HttpWebRequest CreateWebRequest(Uri uri) { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.Proxy = Config.UseProxy ? CreateProxy(uri) : null; // Make sure the default credentials are available webRequest.Credentials = CredentialCache.DefaultCredentials; // Allow redirect, this is usually needed so that we don't get a problem when a service moves webRequest.AllowAutoRedirect = true; // Set default timeouts webRequest.Timeout = Config.WebRequestTimeout * 1000; webRequest.ReadWriteTimeout = Config.WebRequestReadWriteTimeout * 1000; return webRequest; } /// /// Create a IWebProxy Object which can be used to access the Internet /// This method will check the configuration if the proxy is allowed to be used. /// Usages can be found in the DownloadFavIcon or Jira and Confluence plugins /// /// /// IWebProxy filled with all the proxy details or null if none is set/wanted public static IWebProxy CreateProxy(Uri uri) { IWebProxy proxyToUse = null; if (Config.UseProxy) { proxyToUse = WebRequest.DefaultWebProxy; if (proxyToUse != null) { proxyToUse.Credentials = CredentialCache.DefaultCredentials; if (LOG.IsDebugEnabled) { // check the proxy for the Uri if (!proxyToUse.IsBypassed(uri)) { Uri proxyUri = proxyToUse.GetProxy(uri); if (proxyUri != null) { LOG.Debug("Using proxy: " + proxyUri + " for " + uri); } else { LOG.Debug("No proxy found!"); } } else { LOG.Debug("Proxy bypass for: " + uri); } } } else { LOG.Debug("No proxy found!"); } } return proxyToUse; } /// /// UrlEncodes a string without the requirement for System.Web /// /// /// // [Obsolete("Use System.Uri.EscapeDataString instead")] public static string UrlEncode(string text) { if (!string.IsNullOrEmpty(text)) { // Sytem.Uri provides reliable parsing, but doesn't encode spaces. return Uri.EscapeDataString(text).Replace("%20", "+"); } return null; } /// /// A wrapper around the EscapeDataString, as the limit is 32766 characters /// See: http://msdn.microsoft.com/en-us/library/system.uri.escapedatastring%28v=vs.110%29.aspx /// /// /// escaped data string public static string EscapeDataString(string text) { if (!string.IsNullOrEmpty(text)) { StringBuilder result = new StringBuilder(); int currentLocation = 0; while (currentLocation < text.Length) { string process = text.Substring(currentLocation, Math.Min(16384, text.Length - currentLocation)); result.Append(Uri.EscapeDataString(process)); currentLocation = currentLocation + 16384; } return result.ToString(); } return null; } /// /// UrlDecodes a string without requiring System.Web /// /// String to decode. /// decoded string public static string UrlDecode(string text) { // pre-process for + sign space formatting since System.Uri doesn't handle it // plus literals are encoded as %2b normally so this should be safe text = text.Replace("+", " "); return Uri.UnescapeDataString(text); } /// /// ParseQueryString without the requirement for System.Web /// /// /// IDictionary string, string public static IDictionary ParseQueryString(string queryString) { IDictionary parameters = new SortedDictionary(); // remove anything other than query string from uri if (queryString.Contains("?")) { queryString = queryString.Substring(queryString.IndexOf('?') + 1); } foreach (string vp in Regex.Split(queryString, "&")) { if (string.IsNullOrEmpty(vp)) { continue; } string[] singlePair = Regex.Split(vp, "="); if (parameters.ContainsKey(singlePair[0])) { parameters.Remove(singlePair[0]); } parameters.Add(singlePair[0], singlePair.Length == 2 ? singlePair[1] : string.Empty); } return parameters; } /// /// Generate the query paramters /// /// the list of query parameters /// a string with the query parameters public static string GenerateQueryParameters(IDictionary queryParameters) { if (queryParameters == null || queryParameters.Count == 0) { return string.Empty; } queryParameters = new SortedDictionary(queryParameters); StringBuilder sb = new StringBuilder(); foreach (string key in queryParameters.Keys) { sb.AppendFormat(CultureInfo.InvariantCulture, "{0}={1}&", key, UrlEncode(string.Format("{0}", queryParameters[key]))); } sb.Remove(sb.Length - 1, 1); return sb.ToString(); } /// /// Write Multipart Form Data directly to the HttpWebRequest /// /// HttpWebRequest to write the multipart form data to /// Parameters to include in the multipart form data public static void WriteMultipartFormData(HttpWebRequest webRequest, IDictionary postParameters) { string boundary = String.Format("----------{0:N}", Guid.NewGuid()); webRequest.ContentType = "multipart/form-data; boundary=" + boundary; using (Stream formDataStream = webRequest.GetRequestStream()) { WriteMultipartFormData(formDataStream, boundary, postParameters); } } /// /// Write Multipart Form Data to the HttpListenerResponse /// /// HttpListenerResponse /// Parameters to include in the multipart form data public static void WriteMultipartFormData(HttpListenerResponse response, IDictionary postParameters) { string boundary = String.Format("----------{0:N}", Guid.NewGuid()); response.ContentType = "multipart/form-data; boundary=" + boundary; WriteMultipartFormData(response.OutputStream, boundary, postParameters); } /// /// Write Multipart Form Data to a Stream, content-type should be set before this! /// /// Stream to write the multipart form data to /// String boundary for the multipart/form-data /// Parameters to include in the multipart form data public static void WriteMultipartFormData(Stream formDataStream, string boundary, IDictionary postParameters) { bool needsClrf = false; foreach (var param in postParameters) { // Add a CRLF to allow multiple parameters to be added. // Skip it on the first parameter, add it to subsequent parameters. if (needsClrf) { formDataStream.Write(Encoding.UTF8.GetBytes("\r\n"), 0, Encoding.UTF8.GetByteCount("\r\n")); } needsClrf = true; if (param.Value is IBinaryContainer) { IBinaryContainer binaryParameter = (IBinaryContainer)param.Value; binaryParameter.WriteFormDataToStream(boundary, param.Key, formDataStream); } else { string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}", boundary, param.Key, param.Value); formDataStream.Write(Encoding.UTF8.GetBytes(postData), 0, Encoding.UTF8.GetByteCount(postData)); } } // Add the end of the request. Start with a newline string footer = "\r\n--" + boundary + "--\r\n"; formDataStream.Write(Encoding.UTF8.GetBytes(footer), 0, Encoding.UTF8.GetByteCount(footer)); } /// /// Post the parameters "x-www-form-urlencoded" /// /// /// public static void UploadFormUrlEncoded(HttpWebRequest webRequest, IDictionary parameters) { webRequest.ContentType = "application/x-www-form-urlencoded"; string urlEncoded = GenerateQueryParameters(parameters); byte[] data = Encoding.UTF8.GetBytes(urlEncoded); using (var requestStream = webRequest.GetRequestStream()) { requestStream.Write(data, 0, data.Length); } } /// /// Log the headers of the WebResponse, if IsDebugEnabled /// /// WebResponse private static void DebugHeaders(WebResponse response) { if (!LOG.IsDebugEnabled) { return; } LOG.DebugFormat("Debug information on the response from {0} :", response.ResponseUri); foreach (string key in response.Headers.AllKeys) { LOG.DebugFormat("Reponse-header: {0}={1}", key, response.Headers[key]); } } /// /// Process the web response. /// /// The request object. /// The response data. /// This method should handle the StatusCode better! public static string GetResponseAsString(HttpWebRequest webRequest) { return GetResponseAsString(webRequest, false); } /// /// Read the response as string /// /// /// string or null private static string GetResponseAsString(HttpWebResponse response) { string responseData = null; if (response == null) { return null; } using (response) { Stream responseStream = response.GetResponseStream(); if (responseStream != null) { using (StreamReader reader = new StreamReader(responseStream, true)) { responseData = reader.ReadToEnd(); } } } return responseData; } /// /// /// /// /// /// public static string GetResponseAsString(HttpWebRequest webRequest, bool alsoReturnContentOnError) { string responseData = null; HttpWebResponse response = null; bool isHttpError = false; try { response = (HttpWebResponse)webRequest.GetResponse(); LOG.InfoFormat("Response status: {0}", response.StatusCode); isHttpError = (int)response.StatusCode >= 300; if (isHttpError) { LOG.ErrorFormat("HTTP error {0}", response.StatusCode); } DebugHeaders(response); responseData = GetResponseAsString(response); if (isHttpError) { LOG.ErrorFormat("HTTP response {0}", responseData); } } catch (WebException e) { response = (HttpWebResponse)e.Response; HttpStatusCode statusCode = HttpStatusCode.Unused; if (response != null) { statusCode = response.StatusCode; LOG.ErrorFormat("HTTP error {0}", statusCode); string errorContent = GetResponseAsString(response); if (alsoReturnContentOnError) { return errorContent; } LOG.ErrorFormat("Content: {0}", errorContent); } LOG.Error("WebException: ", e); if (statusCode == HttpStatusCode.Unauthorized) { throw new UnauthorizedAccessException(e.Message); } throw; } finally { if (response != null) { if (isHttpError) { LOG.ErrorFormat("HTTP error {0} with content: {1}", response.StatusCode, responseData); } response.Close(); } } return responseData; } /// /// Get LastModified for a URI /// /// Uri /// DateTime public static DateTime GetLastModified(Uri uri) { try { HttpWebRequest webRequest = CreateWebRequest(uri); webRequest.Method = HTTPMethod.HEAD.ToString(); using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { LOG.DebugFormat("RSS feed was updated at {0}", webResponse.LastModified); return webResponse.LastModified; } } catch (Exception wE) { LOG.WarnFormat("Problem requesting HTTP - HEAD on uri {0}", uri); LOG.Warn(wE.Message); // Pretend it is old return DateTime.MinValue; } } } /// /// This interface can be used to pass binary information around, like byte[] or Image /// public interface IBinaryContainer { void WriteFormDataToStream(string boundary, string name, Stream formDataStream); void WriteToStream(Stream formDataStream); string ToBase64String(Base64FormattingOptions formattingOptions); byte[] ToByteArray(); void Upload(HttpWebRequest webRequest); } /// /// 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; public ByteContainer(byte[] file) : this(file, null) { } public ByteContainer(byte[] file, string filename) : this(file, filename, null) { } public ByteContainer(byte[] file, string filename, string contenttype) : this(file, filename, contenttype, 0) { } public ByteContainer(byte[] file, string filename, string contenttype, int filesize) { _file = file; _fileName = filename; _contentType = contenttype; _fileSize = filesize == 0 ? file.Length : filesize; } /// /// Create a Base64String from the byte[] /// /// string public string ToBase64String(Base64FormattingOptions formattingOptions) { return Convert.ToBase64String(_file, 0, _fileSize, formattingOptions); } /// /// Returns the initial byte-array which was supplied when creating the FileParameter /// /// byte[] public byte[] ToByteArray() { return _file; } /// /// Write Multipart Form Data directly to the HttpWebRequest response stream /// /// Separator /// name /// Stream to write to public void WriteFormDataToStream(string boundary, string name, Stream formDataStream) { // Add just the first part of this param, since we will write the file data directly to the Stream 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"); 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); } /// /// A plain "write data to stream" /// /// 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); } /// /// Upload the file to the webrequest /// /// public void Upload(HttpWebRequest webRequest) { webRequest.ContentType = _contentType; webRequest.ContentLength = _fileSize; using (var requestStream = webRequest.GetRequestStream()) { WriteToStream(requestStream); } } } /// /// 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; public BitmapContainer(Bitmap bitmap, SurfaceOutputSettings outputSettings, string filename) { _bitmap = bitmap; _outputSettings = outputSettings; _fileName = filename; } /// /// Create a Base64String from the image by saving it to a memory stream and converting it. /// Should be avoided if possible, as this uses a lot of memory. /// /// string public string ToBase64String(Base64FormattingOptions formattingOptions) { using (MemoryStream stream = new MemoryStream()) { ImageOutput.SaveToStream(_bitmap, null, stream, _outputSettings); return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions); } } /// /// Create a byte[] from the image by saving it to a memory stream. /// Should be avoided if possible, as this uses a lot of memory. /// /// byte[] public byte[] ToByteArray() { using (MemoryStream stream = new MemoryStream()) { ImageOutput.SaveToStream(_bitmap, null, stream, _outputSettings); return stream.ToArray(); } } /// /// Write Multipart Form Data directly to the HttpWebRequest response stream /// /// Separator /// Name of the thing/file /// Stream to write to public void WriteFormDataToStream(string boundary, string name, Stream formDataStream) { // Add just the first part of this param, since we will write the file data directly to the Stream 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); formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header)); ImageOutput.SaveToStream(_bitmap, null, formDataStream, _outputSettings); } /// /// A plain "write data to stream" /// /// 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); } /// /// Upload the image to the webrequest /// /// public void Upload(HttpWebRequest webRequest) { webRequest.ContentType = "image/" + _outputSettings.Format; using (var requestStream = webRequest.GetRequestStream()) { WriteToStream(requestStream); } } } /// /// 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; public SurfaceContainer(ISurface surface, SurfaceOutputSettings outputSettings, string filename) { _surface = surface; _outputSettings = outputSettings; _fileName = filename; } /// /// Create a Base64String from the Surface by saving it to a memory stream and converting it. /// Should be avoided if possible, as this uses a lot of memory. /// /// string public string ToBase64String(Base64FormattingOptions formattingOptions) { using (MemoryStream stream = new MemoryStream()) { ImageOutput.SaveToStream(_surface, stream, _outputSettings); return Convert.ToBase64String(stream.GetBuffer(), 0, (int)stream.Length, formattingOptions); } } /// /// Create a byte[] from the image by saving it to a memory stream. /// Should be avoided if possible, as this uses a lot of memory. /// /// byte[] public byte[] ToByteArray() { using (MemoryStream stream = new MemoryStream()) { ImageOutput.SaveToStream(_surface, stream, _outputSettings); return stream.ToArray(); } } /// /// Write Multipart Form Data directly to the HttpWebRequest response stream /// /// Multipart separator /// Name of the thing /// Stream to write to public void WriteFormDataToStream(string boundary, string name, Stream formDataStream) { // Add just the first part of this param, since we will write the file data directly to the Stream 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); formDataStream.Write(Encoding.UTF8.GetBytes(header), 0, Encoding.UTF8.GetByteCount(header)); ImageOutput.SaveToStream(_surface, formDataStream, _outputSettings); } /// /// A plain "write data to stream" /// /// 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); } /// /// Upload the Surface as image to the webrequest /// /// public void Upload(HttpWebRequest webRequest) { webRequest.ContentType = "image/" + _outputSettings.Format.ToString(); using (var requestStream = webRequest.GetRequestStream()) { WriteToStream(requestStream); } } } }