diff --git a/.gitignore b/.gitignore index 9c40adaa6..efe5d4ebe 100644 --- a/.gitignore +++ b/.gitignore @@ -361,6 +361,9 @@ build /dotnet/src/dotnetcore/GxDataInitialization/net6.0/GXDataInitialization.deps.json /dotnet/src/dotnetcore/GxNetCoreStartup/net6.0/GxNetCoreStartup.deps.json /dotnet/src/dotnetcore/Reor/net6.0/Reor.deps.json -.out + +/dotnet/outsonar.txt +/dotnet/output.txt /dotnet/Veracode.sln /dotnet/VeracodeSolution.sln + diff --git a/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs b/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs index 8f1084f65..e2779a881 100644 --- a/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs +++ b/dotnet/src/dotnetcore/GxClasses/Domain/GXXmlReadWrite.cs @@ -235,7 +235,15 @@ public short OpenResponse(IGxHttpClient httpClient) Close(); EntitiesContainer.Reset(); CreateXMLSettings(); - XMLInput = httpClient.ReceiveStream; + GxHttpClient gxHttpClient = httpClient as GxHttpClient; + if (gxHttpClient!=null && gxHttpClient.ReceiveData != null) + { + XMLInput = new MemoryStream(gxHttpClient.ReceiveData); + } + else + { + XMLInput = string.Empty; + } return 0; } diff --git a/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj b/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj index 58285ee69..f745f0137 100644 --- a/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj +++ b/dotnet/src/dotnetcore/GxClasses/GxClasses.csproj @@ -119,7 +119,6 @@ - diff --git a/dotnet/src/dotnetcore/GxClasses/Helpers/Cryptography/AesCryptoServiceProvider.cs b/dotnet/src/dotnetcore/GxClasses/Helpers/Cryptography/AesCryptoServiceProvider.cs deleted file mode 100644 index fd1bba4d8..000000000 --- a/dotnet/src/dotnetcore/GxClasses/Helpers/Cryptography/AesCryptoServiceProvider.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System.Security.Cryptography; - -namespace GeneXus.Encryption -{ - internal class AesCryptoServiceProvider - { - Aes _aes; - public AesCryptoServiceProvider() - { - _aes = Aes.Create(); - } - - public ICryptoTransform CreateEncryptor() { - return _aes.CreateEncryptor(); - } - - public ICryptoTransform CreateDecryptor() - { - return _aes.CreateDecryptor(); - } - - public CipherMode Mode - { - get { return _aes.Mode; } - set { _aes.Mode = value; } - } - - public PaddingMode Padding - { - get { return _aes.Padding; } - set { _aes.Padding = value; } - } - - public byte[] Key - { - get { return _aes.Key; } - set { _aes.Key = value; } - } - - public void Clear() - { - _aes.Dispose(); - } - - } -} \ No newline at end of file diff --git a/dotnet/src/dotnetcore/GxMail/GxMail.csproj b/dotnet/src/dotnetcore/GxMail/GxMail.csproj index 42e76af3a..fbeb0ea93 100644 --- a/dotnet/src/dotnetcore/GxMail/GxMail.csproj +++ b/dotnet/src/dotnetcore/GxMail/GxMail.csproj @@ -72,7 +72,6 @@ - diff --git a/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs b/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs index eef865a77..932ec4b11 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs @@ -5486,14 +5486,18 @@ private static Bitmap BitmapCreateFromStream(string filePathOrUrl) { using (Stream s = ImageFile(filePathOrUrl).GetStream()) { - return new Bitmap(s); + if (s != null) + return new Bitmap(s); + return null; } } private static Image ImageCreateFromStream(string filePathOrUrl) { using (Stream s = ImageFile(filePathOrUrl).GetStream()) { - return Image.FromStream(s); + if (s != null) + return Image.FromStream(s); + return null; } } @@ -5508,6 +5512,8 @@ public static string Resize(string imageFile, int width, int height, bool keepAs { using (Image image = ImageCreateFromStream(imageFile)) { + if (image == null) + return string.Empty; int newheight = height; // Prevent using images internal thumbnail image.RotateFlip(RotateFlipType.Rotate180FlipNone); @@ -5667,7 +5673,10 @@ public static int GetImageWidth(string imageFile) { using (Bitmap bmp = BitmapCreateFromStream(imageFile)) { - return bmp.Width; + if (bmp != null) + { + return bmp.Width; + } } } catch (Exception ex) diff --git a/dotnet/src/dotnetframework/GxClasses/Core/Web/GxHttpServer.cs b/dotnet/src/dotnetframework/GxClasses/Core/Web/GxHttpServer.cs index 92b8ffe18..0be9d4f55 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/Web/GxHttpServer.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/Web/GxHttpServer.cs @@ -17,6 +17,7 @@ namespace GeneXus.Http.Server using System.Net.Http; using Stubble.Core.Contexts; using System.Net.Mime; + #endif public class GxHttpCookie @@ -394,6 +395,22 @@ public string GetValue( string name) return string.Empty; } } + // create function to convert stream to string + +#if NETCORE + const int StreamReaderDefaultBufferSize = -1; +#else + const int StreamReaderDefaultBufferSize = 1024; +#endif + private string GetStringFromStream(Stream stream) + { + if (stream == null) + return string.Empty; + using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.UTF8, true, StreamReaderDefaultBufferSize, true)) + { + return reader.ReadToEnd(); + } + } public override string ToString() { if (_httpReq == null) @@ -401,10 +418,7 @@ public override string ToString() #if NETCORE return _httpReq.GetRawBodyString(); #else - using (StreamReader reader = new StreamReader(_httpReq.InputStream)) - { - return reader.ReadToEnd(); - } + return GetStringFromStream(_httpReq.InputStream); #endif } public void ToFile(string FileName) diff --git a/dotnet/src/dotnetframework/GxClasses/Data/GXDataMemory.cs b/dotnet/src/dotnetframework/GxClasses/Data/GXDataMemory.cs index 5ef53027f..5ac71ea95 100644 --- a/dotnet/src/dotnetframework/GxClasses/Data/GXDataMemory.cs +++ b/dotnet/src/dotnetframework/GxClasses/Data/GXDataMemory.cs @@ -871,18 +871,7 @@ public static object GetCopyFrom(object value) { MemoryStream memStream = new MemoryStream(); Stream oStream = (Stream)value; - byte[] buffer = new byte[4096]; - - oStream.Seek(0, SeekOrigin.Begin); - - int len; - do - { - len = oStream.Read(buffer, 0, buffer.Length); - memStream.Write(buffer, 0, len); - } - while (len > 0); - + oStream.CopyTo(memStream); return memStream; } else diff --git a/dotnet/src/dotnetframework/GxClasses/Data/GXDataPostgreSQL.cs b/dotnet/src/dotnetframework/GxClasses/Data/GXDataPostgreSQL.cs index f13f25d44..b9fd60ab1 100644 --- a/dotnet/src/dotnetframework/GxClasses/Data/GXDataPostgreSQL.cs +++ b/dotnet/src/dotnetframework/GxClasses/Data/GXDataPostgreSQL.cs @@ -361,33 +361,35 @@ private byte[] ByteaTextToByteArray(byte[] BackendData) } else { - MemoryStream ms = new MemoryStream(); - while (byteAPosition < byteALength) + using (MemoryStream ms = new MemoryStream()) { - byte octalValue; - byte b = BackendData[byteAPosition]; - if (b >= 0x20 && b < 0x7F && b != 0x27 && b != 0x5C) + while (byteAPosition < byteALength) { - octalValue = BackendData[byteAPosition]; - byteAPosition++; - ms.WriteByte((Byte)octalValue); - } - else if (BackendData[byteAPosition] == ASCIIByteBackSlash && - byteAPosition + 3 < byteALength && - BackendData[byteAPosition + 1] >= ASCIIByteb0 && BackendData[byteAPosition + 1] <= ASCIIByteb7 && - BackendData[byteAPosition + 2] >= ASCIIByteb0 && BackendData[byteAPosition + 2] <= ASCIIByteb7 && - BackendData[byteAPosition + 3] >= ASCIIByteb0 && BackendData[byteAPosition + 3] <= ASCIIByteb7) - { - octalValue = FastConverter.ToByteEscapeFormat(BackendData, byteAPosition + 1); - byteAPosition += 4; - ms.WriteByte((Byte)octalValue); - } - else - { - return BackendData; + byte octalValue; + byte b = BackendData[byteAPosition]; + if (b >= 0x20 && b < 0x7F && b != 0x27 && b != 0x5C) + { + octalValue = BackendData[byteAPosition]; + byteAPosition++; + ms.WriteByte((Byte)octalValue); + } + else if (BackendData[byteAPosition] == ASCIIByteBackSlash && + byteAPosition + 3 < byteALength && + BackendData[byteAPosition + 1] >= ASCIIByteb0 && BackendData[byteAPosition + 1] <= ASCIIByteb7 && + BackendData[byteAPosition + 2] >= ASCIIByteb0 && BackendData[byteAPosition + 2] <= ASCIIByteb7 && + BackendData[byteAPosition + 3] >= ASCIIByteb0 && BackendData[byteAPosition + 3] <= ASCIIByteb7) + { + octalValue = FastConverter.ToByteEscapeFormat(BackendData, byteAPosition + 1); + byteAPosition += 4; + ms.WriteByte((Byte)octalValue); + } + else + { + return BackendData; + } } + return ms.ToArray(); } - return ms.ToArray(); } }catch(Exception ex) { diff --git a/dotnet/src/dotnetframework/GxClasses/Domain/GXFileIO.cs b/dotnet/src/dotnetframework/GxClasses/Domain/GXFileIO.cs index 8abbd48d3..32ea642ae 100644 --- a/dotnet/src/dotnetframework/GxClasses/Domain/GXFileIO.cs +++ b/dotnet/src/dotnetframework/GxClasses/Domain/GXFileIO.cs @@ -224,10 +224,10 @@ public void MoveTo(string desDirName) public class GxFileInfo : IGxFileInfo { private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxFileInfo)); - FileInfo _file; - string _baseDirectory; + private FileInfo _file; + private string _baseDirectory; - public GxFileInfo(FileInfo file) + public GxFileInfo(FileInfo file) { _file = file; } @@ -657,7 +657,7 @@ public class GxFile private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GxFile)); internal IGxFileInfo _file; - string _baseDirectory; + string _baseDirectory; int _lastError; string _lastErrorDescription; string _source; @@ -1040,11 +1040,11 @@ public Stream GetStream() _lastError = 0; _lastErrorDescription = ""; if (!validSource()) - return new MemoryStream(); + return null; if (!Exists()) { _lastError = 2; - return new MemoryStream(); + return null; } try { @@ -1054,7 +1054,7 @@ public Stream GetStream() { setError(e); } - return new MemoryStream(); + return null; } public bool PathIsRooted() { @@ -1437,7 +1437,7 @@ public void Open(String encoding) private FileStream _fileStreamReader; private StreamReader _fileReader; - public void OpenWrite(String encoding) + public void OpenWrite(String encoding) { _lastError = 0; _lastErrorDescription = ""; @@ -1568,6 +1568,8 @@ public void Close() } } } + + } public class GxDirectory diff --git a/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs b/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs index 1d43a4fd0..3ad6a928b 100644 --- a/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs +++ b/dotnet/src/dotnetframework/GxClasses/Domain/GXXmlReadWrite.cs @@ -48,6 +48,7 @@ public class GXXMLReader : IDisposable public const int ValidationXDR = 4; private XmlTextReader treader; + private MemoryStream mreader; private XmlValidatingReader vreader; @@ -229,7 +230,14 @@ public short OpenResponse(IGxHttpClient httpClient) { if (treader != null) Close(); EntitiesContainer.Reset(); - treader = new XmlTextReader(httpClient.ReceiveStream); + GxHttpClient gxHttpClient = httpClient as GxHttpClient; + if (gxHttpClient != null && gxHttpClient.ReceiveData != null) + { + mreader = new MemoryStream(gxHttpClient.ReceiveData); + treader = new XmlTextReader(mreader); + } + else + treader = new XmlTextReader(string.Empty); if (treader != null) { SetDtdProcessing(treader, Resolver, validationType); @@ -290,6 +298,7 @@ public void OpenFromString(string s) Uri baseUri = new Uri( sBaseDirectory ); Resolver.Myself = baseUri; treader = null; + mreader = null; try { if (File.Exists(s)) @@ -629,6 +638,11 @@ public short Close() treader.Close(); treader = null; } + if (mreader != null) + { + mreader.Close(); + mreader = null; + } if ( vreader != null ) { vreader.Close(); diff --git a/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs b/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs index db488e0d6..201cc6c19 100644 --- a/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs +++ b/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs @@ -56,15 +56,16 @@ internal MultiPartTemplate() HeaderTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: {2}\r\n\r\n"; } } - public class GxHttpClient : IGxHttpClient + public class GxHttpClient : IGxHttpClient, IDisposable { private static readonly ILog log = log4net.LogManager.GetLogger(typeof(GeneXus.Http.Client.GxHttpClient)); public const int _Basic = 0; public const int _Digest = 1; public const int _NTLM = 2; public const int _Kerberos = 3; + const int StreamWriterDefaultBufferSize = 1024; Stream _sendStream; - Stream _receiveStream; + byte[] _receiveData; int _timeout = 30000; short _statusCode = 0; string _proxyHost; @@ -120,17 +121,21 @@ public Stream SendStream _sendStream = value; } } + public Stream ReceiveStream { get { - if (_receiveStream == null) - _receiveStream = new MemoryStream(); - - return _receiveStream; + return null; + } + } + internal byte[] ReceiveData + { + get + { + return _receiveData; } } - #if NETCORE [SecuritySafeCritical] @@ -407,7 +412,7 @@ public void AddVariable(string name, string value) _formVars.Add(name, value); } - void sendVariables(Stream reqStream) + void SendVariables(Stream reqStream) { List vars = new List(); for (int i = 0; i < _formVars.Count; i++) @@ -418,8 +423,15 @@ void sendVariables(Stream reqStream) if (vars.Count > 0) { string buffer = string.Join(variableSeparator(), vars.ToArray()); - StreamWriter sw = new StreamWriter(reqStream); - sw.Write(buffer); + WriteToStream(buffer, reqStream); + } + } + void WriteToStream(string data, Stream stream, Encoding encoding=null) + { + Encoding streamEncoding = encoding != null ? encoding : new UTF8Encoding(false, true); + using (StreamWriter sw = new StreamWriter(stream, streamEncoding, StreamWriterDefaultBufferSize, true)) + { + sw.Write(data); sw.Flush(); } } @@ -445,13 +457,7 @@ string buildVariableToSend(string key, string value) } public void AddString(string s) { - StreamWriter sw; - if (_contentEncoding != null) - sw = new StreamWriter(SendStream, _contentEncoding); - else - sw = new StreamWriter(SendStream); - sw.Write(s); - sw.Flush(); + WriteToStream(s, SendStream, _contentEncoding); } public void ClearFiles() { @@ -653,7 +659,7 @@ HttpResponseMessage ExecuteRequest(string method, string requestUrl, CookieConta using (MemoryStream reqStream = new MemoryStream()) { - sendVariables(reqStream); + SendVariables(reqStream); SendStream.Seek(0, SeekOrigin.Begin); BytesRead = SendStream.Read(Buffer, 0, 1024); GXLogging.Debug(log, "Start SendStream.Read: BytesRead " + BytesRead); @@ -675,9 +681,7 @@ HttpResponseMessage ExecuteRequest(string method, string requestUrl, CookieConta } void ReadReponseContent(HttpResponseMessage response) { - _receiveStream = new MemoryStream(); - int BytesRead; - Byte[] Buffer = new Byte[1024]; + _receiveData = Array.Empty(); try { Stream stream = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); @@ -686,9 +690,6 @@ void ReadReponseContent(HttpResponseMessage response) charset = null; else charset = response.Content.Headers.ContentType.CharSet; - Buffer = new Byte[1024]; - BytesRead = stream.Read(Buffer, 0, 1024); - GXLogging.Debug(log, "BytesRead " + BytesRead); bool encodingFound = false; if (!string.IsNullOrEmpty(charset)) { @@ -706,16 +707,18 @@ void ReadReponseContent(HttpResponseMessage response) charset = String.Empty; } } - while (BytesRead > 0) + + using (MemoryStream ms = new MemoryStream()) { - if (!encodingFound) - { - _encoding = DetectEncoding(charset, out encodingFound, Buffer, BytesRead); - } - _receiveStream.Write(Buffer, 0, BytesRead); - BytesRead = stream.Read(Buffer, 0, 1024); - GXLogging.Debug(log, "BytesRead " + BytesRead); + stream.CopyTo(ms); + _receiveData = ms.ToArray(); } + int bytesRead = _receiveData.Length; + GXLogging.Debug(log, "BytesRead " + _receiveData.Length); + if (bytesRead > 0 && !encodingFound) + { + _encoding = DetectEncoding(charset, out encodingFound, _receiveData, bytesRead); + } } catch (IOException ioEx) { @@ -724,8 +727,9 @@ void ReadReponseContent(HttpResponseMessage response) else throw ioEx; } - _receiveStream.Seek(0, SeekOrigin.Begin); } + + public void Execute(string method, string name) { if (Config.GetValueOf("useoldhttpclient", out string useOld) && useOld.StartsWith("y", StringComparison.OrdinalIgnoreCase)) @@ -824,6 +828,8 @@ public void HttpClientExecute(string method, string name) GXLogging.Debug(log, "_responseString " + ToString()); } NameValueCollection _respHeaders; + private bool disposedValue; + void LoadResponseHeaders(HttpResponseMessage resp) { _respHeaders = new NameValueCollection(); @@ -1046,7 +1052,7 @@ HttpWebRequest buildRequest(string method, string requestUrl, CookieContainer co using (Stream reqStream = req.GetRequestStreamAsync().GetAwaiter().GetResult()) #endif { - sendVariables(reqStream); + SendVariables(reqStream); SendStream.Seek(0, SeekOrigin.Begin); BytesRead = SendStream.Read(Buffer, 0, 1024); GXLogging.Debug(log, "Start SendStream.Read: BytesRead " + BytesRead); @@ -1118,8 +1124,6 @@ private void WebExecute(string method, string name) { HttpWebRequest req; HttpWebResponse resp = null; - int BytesRead; - Byte[] Buffer = new Byte[1024]; _errCode = 0; _errDescription = string.Empty; @@ -1169,16 +1173,14 @@ private void WebExecute(string method, string name) GXLogging.Debug(log, "Reading response..."); loadResponseHeaders(resp); - _receiveStream = new MemoryStream(); + + _receiveData = Array.Empty(); + String charset = resp.ContentType; using (Stream rStream = resp.GetResponseStream()) { try { - Buffer = new Byte[1024]; - BytesRead = rStream.Read(Buffer, 0, 1024); - GXLogging.Debug(log, "BytesRead " + BytesRead); bool encodingFound = false; - String charset = resp.ContentType; if (!string.IsNullOrEmpty(charset)) { int idx = charset.IndexOf("charset="); @@ -1195,15 +1197,17 @@ private void WebExecute(string method, string name) charset = String.Empty; } } - while (BytesRead > 0) + using (MemoryStream ms = new MemoryStream()) { - if (!encodingFound) - { - _encoding = DetectEncoding(charset, out encodingFound, Buffer, BytesRead); - } - _receiveStream.Write(Buffer, 0, BytesRead); - BytesRead = rStream.Read(Buffer, 0, 1024); - GXLogging.Debug(log, "BytesRead " + BytesRead); + rStream.CopyTo(ms); + _receiveData = ms.ToArray(); + } + int bytesRead = _receiveData.Length; + GXLogging.Debug(log, "BytesRead " + bytesRead); + + if (bytesRead > 0 && !encodingFound) + { + _encoding = DetectEncoding(charset, out encodingFound, _receiveData, bytesRead); } } catch (IOException ioEx) @@ -1214,7 +1218,6 @@ private void WebExecute(string method, string name) throw ioEx; } } - _receiveStream.Seek(0, SeekOrigin.Begin); _statusCode = (short)resp.StatusCode; _statusDescription = resp.StatusDescription; resp.Close(); @@ -1306,34 +1309,23 @@ public override string ToString() { if (_encoding == null) _encoding = Encoding.UTF8; - MemoryStream ms = (MemoryStream)ReceiveStream; - ms.Seek(0, SeekOrigin.Begin); - Byte[] Buffer = ms.ToArray(); - return _encoding.GetString(Buffer, 0, Buffer.Length); + if (_receiveData == null) + return string.Empty; + return _encoding.GetString(_receiveData); } public void ToFile(string fileName) { - FileStream fs; string pathName = fileName; #if !NETCORE if (HttpContext.Current != null) #endif - if (fileName.IndexOfAny(new char[] { '\\', ':' }) == -1) - pathName = Path.Combine(GxContext.StaticPhysicalPath(), fileName); -#pragma warning disable SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}' - using (fs = new FileStream(pathName, FileMode.Create, FileAccess.Write)) -#pragma warning restore SCS0018 // Path traversal: injection possible in {1} argument passed to '{0}' + if (fileName.IndexOfAny(new char[] { '\\', ':' }) == -1) + pathName = Path.Combine(GxContext.StaticPhysicalPath(), fileName); + + if (_receiveData != null) { - ReceiveStream.Seek(0, SeekOrigin.Begin); - Byte[] Buffer = new Byte[1024]; - int BytesRead = ReceiveStream.Read(Buffer, 0, 1024); - while (BytesRead > 0) - { - fs.Write(Buffer, 0, BytesRead); - BytesRead = ReceiveStream.Read(Buffer, 0, 1024); - } - ReceiveStream.Seek(0, SeekOrigin.Begin); + File.WriteAllBytes(pathName, _receiveData); } } @@ -1400,6 +1392,29 @@ public void AddCertificate(string file, string pass) } _certificateCollection.Add(c); } + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + _receiveData = null; + _sendStream?.Dispose(); + } + disposedValue = true; + } + } + ~GxHttpClient() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } } class GxAuthScheme diff --git a/dotnet/src/dotnetframework/GxExcel/GxExcelEPPlus.cs b/dotnet/src/dotnetframework/GxExcel/GxExcelEPPlus.cs index 64a53a3dc..e923b8ecd 100644 --- a/dotnet/src/dotnetframework/GxExcel/GxExcelEPPlus.cs +++ b/dotnet/src/dotnetframework/GxExcel/GxExcelEPPlus.cs @@ -26,9 +26,19 @@ public short Open(String fileName) GxFile temp = new GxFile(Path.GetDirectoryName(template), template); if (temp.Exists()) { - GXLogging.Debug(log, "Opening Template " + template); - p = new ExcelPackage(temp.GetStream()); - OpenFromTemplate = true; + Stream stream = temp.GetStream(); + if (stream != null) + { + GXLogging.Debug(log, "Opening Template " + template); + p = new ExcelPackage(stream); + OpenFromTemplate = true; + } + else + { + errCod = 4; + errDescription = "Invalid template."; + return errCod; + } } else @@ -47,7 +57,19 @@ public short Open(String fileName) fileName += Constants.EXCEL2007Extension; } if (file.IsExternalFile) - p = new ExcelPackage(file.GetStream()); + { + Stream stream = file.GetStream(); + if (stream != null) + { + p = new ExcelPackage(file.GetStream()); + } + else + { + errCod = 4; + errDescription = "Invalid file."; + return errCod; + } + } else p = new ExcelPackage(new FileInfo(fileName)); } diff --git a/dotnet/src/dotnetframework/GxExcel/GxExcelLite.cs b/dotnet/src/dotnetframework/GxExcel/GxExcelLite.cs index a652df75c..b7c7b1d04 100644 --- a/dotnet/src/dotnetframework/GxExcel/GxExcelLite.cs +++ b/dotnet/src/dotnetframework/GxExcel/GxExcelLite.cs @@ -476,9 +476,11 @@ public short Open(String fileName) { GXLogging.Debug(log,"Opening Template " + template); var stream = templateFile.GetStream(); - stream.Position = 0; - - GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream }); + if (stream != null) + { + stream.Position = 0; + GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream }); + } } else { @@ -490,9 +492,11 @@ public short Open(String fileName) else if (file.Exists()) { var stream = file.GetStream(); - stream.Position = 0; - - GxExcelUtils.Invoke(ef, "LoadXls", new object[]{stream}); + if (stream != null) + { + stream.Position = 0; + GxExcelUtils.Invoke(ef, "LoadXls", new object[] { stream }); + } } else { diff --git a/dotnet/test/DotNetCoreUnitTest/HttpClient/StreamHandling.cs b/dotnet/test/DotNetCoreUnitTest/HttpClient/StreamHandling.cs new file mode 100644 index 000000000..cf1db2e0f --- /dev/null +++ b/dotnet/test/DotNetCoreUnitTest/HttpClient/StreamHandling.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using GeneXus.Http.Client; +using GeneXus.XML; +using Xunit; + +namespace DotNetCoreUnitTest.HttpClientTest +{ + public class StreamHandling + { + [Fact] + public void CreateHttpClient() + { + using (GxHttpClient client = new GxHttpClient()) + { + client.Execute("GET", "https://raw.githubusercontent.com/OFFLINE-GmbH/Online-FTP-S3/master/phpunit.xml"); + Assert.Equal(client.StatusCode, (short)System.Net.HttpStatusCode.OK); + Assert.True(client.ToString().Length > 0); + + // No try to consume the string again in order to verify it works several times. + Assert.True(client.ToString().Length > 0); + + using (GXXMLReader reader = new GXXMLReader()) + { + reader.OpenResponse(client); + Assert.True(reader.Read() > 0); + } + + Assert.True(client.ToString().Length > 0); + } + + + } + } +} diff --git a/dotnet/test/DotNetCoreUnitTest/StringUtil/StringUtilTests.cs b/dotnet/test/DotNetCoreUnitTest/StringUtil/StringUtilTests.cs index a9e7cb262..2735191e9 100644 --- a/dotnet/test/DotNetCoreUnitTest/StringUtil/StringUtilTests.cs +++ b/dotnet/test/DotNetCoreUnitTest/StringUtil/StringUtilTests.cs @@ -32,9 +32,7 @@ public void TestJSONEncodeDoNotEncodeGreaterCharacter() json +=""; string expectedJsonEncoded = "\\t\\t\\t\\t\\t\\t\\t-88.076680,43.945580 -88.077480,43.945930 -88.078530,43.946390 -88.078960\\t\\t\\t\\t#MyLine\\t"; - string jsonEncoded = StringUtil.JSONEncode(json); - File.WriteAllText(@"C:\temp\json.txt", jsonEncoded); Assert.Equal(jsonEncoded, expectedJsonEncoded); } diff --git a/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs b/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs index a03f17559..650f44036 100644 --- a/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs +++ b/dotnet/test/DotNetUnitTest/Domain/GxHttpClientTest.cs @@ -10,15 +10,17 @@ public class GxHttpClientTest [Fact] public void AddHeaderWithSpecialCharactersDoesNotThrowException() { - GxHttpClient httpclient = new GxHttpClient(); - string headerValue = "d3890093-289b-4f87-adad-f2ebea826e8f!8db3bc7ac3d38933c3b0c91a3bcdab60b9bbb3f607a1c9b312b24374e750243f3a31d7e90a4c55@SSORT!d3890093-289b-4f87-adad-f2ebea826e8f!8c7564ac08514ff988ba6c8c6ba3fc0c"; - string headerName = "Authorization"; - httpclient.AddHeader(headerName, headerValue); - httpclient.Host="accountstest.genexus.com"; - httpclient.Secure=1; - httpclient.BaseURL =@"oauth/gam/v2.0/dummy/requesttokenanduserinfo"; - httpclient.Execute("GET", string.Empty); - Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode); + using (GxHttpClient httpclient = new GxHttpClient()) + { + string headerValue = "d3890093-289b-4f87-adad-f2ebea826e8f!8db3bc7ac3d38933c3b0c91a3bcdab60b9bbb3f607a1c9b312b24374e750243f3a31d7e90a4c55@SSORT!d3890093-289b-4f87-adad-f2ebea826e8f!8c7564ac08514ff988ba6c8c6ba3fc0c"; + string headerName = "Authorization"; + httpclient.AddHeader(headerName, headerValue); + httpclient.Host = "accountstest.genexus.com"; + httpclient.Secure = 1; + httpclient.BaseURL = @"oauth/gam/v2.0/dummy/requesttokenanduserinfo"; + httpclient.Execute("GET", string.Empty); + Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode); + } } }