diff --git a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs index 219769018..b4ef99197 100644 --- a/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs +++ b/dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs @@ -204,7 +204,7 @@ public interface IGxContext void DoAjaxRefreshForm(); void DoAjaxRefreshCmp(String sPrefix); #if !NETCORE - void DoAjaxLoad(int SId, GXWebRow row); + void DoAjaxLoad(int SId, GXWebRow row); #endif void DoAjaxAddLines(int SId, int lines); void DoAjaxSetFocus(string ControlName); @@ -269,9 +269,7 @@ public interface IGxContext CookieContainer GetCookieContainer(string url, bool includeCookies = true); bool WillRedirect(); GXSOAPContext SoapContext { get; set; } -#if NETCORE void UpdateSessionCookieContainer(); -#endif string GetCacheInvalidationToken(); string GetURLBuildNumber(string resourcePath, string urlBuildNumber); } @@ -479,23 +477,26 @@ private Dictionary> ToSerializableCookieContainer(Di if (cookies == null) return null; Dictionary> serializableCookies = new Dictionary>(); - foreach(string key in cookies.Keys) + foreach (string key in cookies.Keys) { + serializableCookies[key] = cookies[key].GetCookies(); + } return serializableCookies; } +#endif private Dictionary FromSerializableCookieContainer(Dictionary> cookies) { if (cookies == null) return null; Dictionary serializableCookies = new Dictionary(); - + foreach (string key in cookies.Keys) { CookieCollection cookieco = new CookieCollection(); IEnumerable cookiesEnum = cookies[key]; - foreach(Cookie c in cookiesEnum) + foreach (Cookie c in cookiesEnum) { cookieco.Add(c); } @@ -505,19 +506,14 @@ private Dictionary FromSerializableCookieContainer(Dict } return serializableCookies; } -#else - private Dictionary ToSerializableCookieContainer(Dictionary cookies){ - return cookies; - } - private Dictionary FromSerializableCookieContainer(Dictionary cookies) - { - return cookies; - } -#endif public void UpdateSessionCookieContainer() { IGxSession tempStorage = GetSession(); +#if NETCORE tempStorage.Set(COOKIE_CONTAINER, ToSerializableCookieContainer(cookieContainers)); +#else + tempStorage.Set(COOKIE_CONTAINER, cookieContainers); +#endif } public CookieContainer GetCookieContainer(string url, bool includeCookies = true) { @@ -528,12 +524,16 @@ public CookieContainer GetCookieContainer(string url, bool includeCookies = true #if NETCORE cookieContainers = FromSerializableCookieContainer(tempStorage.Get>>(COOKIE_CONTAINER)); #else - cookieContainers =tempStorage.Get>(COOKIE_CONTAINER); + cookieContainers = tempStorage.Get>(COOKIE_CONTAINER); #endif if (cookieContainers == null) { cookieContainers = new Dictionary(); +#if NETCORE tempStorage.Set(COOKIE_CONTAINER, ToSerializableCookieContainer(cookieContainers)); +#else + tempStorage.Set(COOKIE_CONTAINER, cookieContainers); +#endif } string domain = (new Uri(url)).GetLeftPart(UriPartial.Authority); if (cookieContainers.TryGetValue(domain, out container) && includeCookies) @@ -562,19 +562,19 @@ static public GxContext Current get { #if !NETCORE - if (HttpContext.Current != null) - { - - GxContext currCtx = (GxContext)HttpContext.Current.Items["CURRENT_GX_CONTEXT"]; - if (currCtx != null) - return currCtx; - } - else - { - - return _currentGxContext; - } - return null; + if (HttpContext.Current != null) + { + + GxContext currCtx = (GxContext)HttpContext.Current.Items["CURRENT_GX_CONTEXT"]; + if (currCtx != null) + return currCtx; + } + else + { + + return _currentGxContext; + } + return null; #else return _currentGxContext; #endif @@ -583,11 +583,11 @@ static public GxContext Current static void setContext(GxContext ctx) { #if !NETCORE - if (HttpContext.Current != null) - HttpContext.Current.Items["CURRENT_GX_CONTEXT"] = ctx; - else + if (HttpContext.Current != null) + HttpContext.Current.Items["CURRENT_GX_CONTEXT"] = ctx; + else #endif - _currentGxContext = ctx; + _currentGxContext = ctx; } public LocalUtil localUtil @@ -694,7 +694,7 @@ public static bool GetHttpRequestPostedFile(IGxContext gxContext, string varName GxUploadHelper.CacheUploadFile(fileGuid, Path.GetFileName(pf.FileName), ext, file, gxContext); - return true; + return true; } } return false; @@ -973,7 +973,7 @@ public void StatusMessage(string message) #if NETCORE ILog statusLog = log4net.LogManager.GetLogger(frame.GetMethod().DeclaringType); #else - ILog statusLog = log4net.LogManager.GetLogger(frame.GetMethod().DeclaringType.FullName); + ILog statusLog = log4net.LogManager.GetLogger(frame.GetMethod().DeclaringType.FullName); #endif GXLogging.Info(statusLog, message); Console.WriteLine(message); @@ -1025,7 +1025,7 @@ public string GetCssProperty(string propName, string propValue) { int browserType = GetBrowserType(); - #region Align +#region Align if (string.Compare(propName, "align", true) == 0) { if (browserType == BROWSER_FIREFOX) @@ -1037,7 +1037,7 @@ public string GetCssProperty(string propName, string propValue) return "-khtml-" + propValue; } } - #endregion +#endregion return propValue; } @@ -1083,12 +1083,12 @@ private void LocalInitialize() _handle = GxUserInfo.NewHandle(); } #if !NETCORE - if (Preferences.Instrumented) - { - GxUserInfo.setProperty(_handle, GxDefaultProps.USER_NAME, Environment.UserName); - GxUserInfo.setProperty(_handle, GxDefaultProps.PGM_NAME, AppDomain.CurrentDomain.FriendlyName); - GxUserInfo.setProperty(_handle, GxDefaultProps.START_TIME, DateTime.Now.ToString()); - } + if (Preferences.Instrumented) + { + GxUserInfo.setProperty(_handle, GxDefaultProps.USER_NAME, Environment.UserName); + GxUserInfo.setProperty(_handle, GxDefaultProps.PGM_NAME, AppDomain.CurrentDomain.FriendlyName); + GxUserInfo.setProperty(_handle, GxDefaultProps.START_TIME, DateTime.Now.ToString()); + } #endif GX_msglist = new msglist(); Config.LoadConfiguration(); @@ -1106,7 +1106,7 @@ public bool IsMultipartRequest #if NETCORE return MultipartRequestHelper.IsMultipartContentType(this.HttpContext.Request.ContentType); #else - return this.HttpContext.Request.Files.Count > 0; + return this.HttpContext.Request.Files.Count > 0; #endif else @@ -1133,10 +1133,10 @@ public HttpContext HttpContext get { #if !NETCORE - if (_HttpContext == null && HttpContext.Current != null) - { - HttpContext = HttpContext.Current; - } + if (_HttpContext == null && HttpContext.Current != null) + { + HttpContext = HttpContext.Current; + } #endif return _HttpContext; } @@ -1682,7 +1682,7 @@ public T ReadSessionKey(string key) where T : class else if (_HttpContext != null && _HttpContext.Session != null) { #if !NETCORE - return (T)_HttpContext.Session[key]; + return (T)_HttpContext.Session[key]; #else string value = _HttpContext.Session.GetString(key); if (value != null) @@ -1706,7 +1706,7 @@ public bool WriteSessionKey(string key, T value) where T : class { #if !NETCORE - HttpContext.Session[key] = value; + HttpContext.Session[key] = value; #else if (!_HttpContext.Response.HasStarted) { @@ -1939,7 +1939,7 @@ public int GetBrowserType() String userAgent; try { - if (_HttpContext != null && _HttpContext.Request!=null) + if (_HttpContext != null && _HttpContext.Request != null) userAgent = _HttpContext.Request.GetUserAgent(); else return 0; @@ -2102,7 +2102,7 @@ public virtual short GetHttpSecure() { try { - if (HttpContext==null) + if (HttpContext == null) return 0; if (_HttpContext.Request.GetIsSecureFrontEnd()) { @@ -2280,7 +2280,7 @@ public string GetCookie(string name) else cookieVal = HttpUtility.UrlDecode(cookie.Value); #else - cookieVal = HttpUtility.UrlDecode(cookie.Value); + cookieVal = HttpUtility.UrlDecode(cookie.Value); #endif } return cookieVal; @@ -2338,26 +2338,26 @@ public short SetCookie(string name, string cookieValue, string path, DateTime ex _HttpContext.Response.Cookies.Append(name, cookie.Value, cookieOptions); localCookies[name] = cookie; #else - if (_HttpContext.Response.Cookies.Get(name) != null) - { - - try - { - _HttpContext.Response.Cookies.Set(cookie); - } - catch (HttpException) { } - localCookies.Set(cookie); - } - else - { - - try - { - _HttpContext.Response.Cookies.Add(cookie); - } - catch (HttpException) { } - localCookies.Add(cookie); - } + if (_HttpContext.Response.Cookies.Get(name) != null) + { + + try + { + _HttpContext.Response.Cookies.Set(cookie); + } + catch (HttpException) { } + localCookies.Set(cookie); + } + else + { + + try + { + _HttpContext.Response.Cookies.Add(cookie); + } + catch (HttpException) { } + localCookies.Add(cookie); + } #endif return 0; } @@ -2370,7 +2370,7 @@ public byte ResponseContentType(String sContentType) #if NETCORE if (!_HttpContext.Response.HasStarted) #endif - _HttpContext.Response.ContentType = sContentType; + _HttpContext.Response.ContentType = sContentType; return 0; } public byte RespondFile(string name) @@ -2400,33 +2400,33 @@ private void SetCustomHttpHeader(string name, string value) _HttpContext.Response.AppendHeader(name, value); #if !NETCORE - switch (name.ToUpper()) - { - case "CACHE-CONTROL": - var Cache = _HttpContext.Response.Cache; - string[] values = value.Split(','); - foreach (string v in values) - { - switch (v.Trim().ToUpper()) - { - case "PUBLIC": - Cache.SetCacheability(HttpCacheability.Public); - break; - case "PRIVATE": - Cache.SetCacheability(HttpCacheability.Private); - break; - case "NO-CACHE": - Cache.SetCacheability(HttpCacheability.NoCache); - break; - case "NO-STORE": - Cache.AppendCacheExtension("no-store, must-revalidate"); - break; - default: - break; - } - } - break; - } + switch (name.ToUpper()) + { + case "CACHE-CONTROL": + var Cache = _HttpContext.Response.Cache; + string[] values = value.Split(','); + foreach (string v in values) + { + switch (v.Trim().ToUpper()) + { + case "PUBLIC": + Cache.SetCacheability(HttpCacheability.Public); + break; + case "PRIVATE": + Cache.SetCacheability(HttpCacheability.Private); + break; + case "NO-CACHE": + Cache.SetCacheability(HttpCacheability.NoCache); + break; + case "NO-STORE": + Cache.AppendCacheExtension("no-store, must-revalidate"); + break; + default: + break; + } + } + break; + } #else switch (name.ToUpper()) { @@ -2479,10 +2479,10 @@ private void DoRedirect(string jumpUrl) private void DoForward(string jumpUrl) { #if !NETCORE - _HttpContext.Items["gx_webcall_method"] = "forward"; - _HttpContext.RewritePath(jumpUrl); - IHttpHandler handler = new GeneXus.HttpHandlerFactory.HandlerFactory().GetHandler(_HttpContext, "GET", jumpUrl, jumpUrl); - handler.ProcessRequest(_HttpContext); + _HttpContext.Items["gx_webcall_method"] = "forward"; + _HttpContext.RewritePath(jumpUrl); + IHttpHandler handler = new GeneXus.HttpHandlerFactory.HandlerFactory().GetHandler(_HttpContext, "GET", jumpUrl, jumpUrl); + handler.ProcessRequest(_HttpContext); #endif } protected void httpRedirect(String jumpUrl) @@ -2655,14 +2655,14 @@ public void DoAjaxRefreshCmp(String sPrefix) httpAjaxContext.appendAjaxCommand("cmp_refresh", sPrefix); } #if !NETCORE - public void DoAjaxLoad(int SId, GXWebRow row) - { - JObject JSONRow = new JObject(); - JSONRow.Put("grid", SId); - JSONRow.Put("props", row.parentGrid.GetJSONObject()); - JSONRow.Put("values", row.parentGrid.GetValues()); - httpAjaxContext.appendLoadData(SId, JSONRow); - } + public void DoAjaxLoad(int SId, GXWebRow row) + { + JObject JSONRow = new JObject(); + JSONRow.Put("grid", SId); + JSONRow.Put("props", row.parentGrid.GetJSONObject()); + JSONRow.Put("values", row.parentGrid.GetValues()); + httpAjaxContext.appendLoadData(SId, JSONRow); + } #endif public void DoAjaxAddLines(int SId, int lines) { @@ -2715,10 +2715,10 @@ public string BuildHTMLColor(int lColor) float getHTMLVersion() { #if !NETCORE - if (_HttpContext.Request.Browser.MajorVersion >= 4 && - (_HttpContext.Request.Browser.Type.ToUpper().IndexOf("IE") > -1 || - _HttpContext.Request.Browser.Type.ToUpper().IndexOf("NS") > -1)) - return 4.0f; + if (_HttpContext.Request.Browser.MajorVersion >= 4 && + (_HttpContext.Request.Browser.Type.ToUpper().IndexOf("IE") > -1 || + _HttpContext.Request.Browser.Type.ToUpper().IndexOf("NS") > -1)) + return 4.0f; #endif return 1.0f; } @@ -2756,7 +2756,7 @@ public virtual string GetServerName() if (Config.GetValueOf("SERVER_NAME", out serverName)) return serverName; #if !NETCORE - serverName = _HttpContext.Request.ServerVariables["http_host"]; + serverName = _HttpContext.Request.ServerVariables["http_host"]; #endif if (String.IsNullOrEmpty(serverName)) { @@ -2809,11 +2809,11 @@ private bool RequestDefaultPort { get { - try - { - return _HttpContext.Request.IsDefaultPort(); - } - catch + try + { + return _HttpContext.Request.IsDefaultPort(); + } + catch { return false; } @@ -2908,7 +2908,7 @@ public static bool IsRestService get { #if !NETCORE - return WebOperationContext.Current != null; + return WebOperationContext.Current != null; #else return false; #endif @@ -2922,7 +2922,7 @@ public static bool IsHttpContext get { #if !NETCORE - return HttpContext.Current != null; + return HttpContext.Current != null; #else return _isHttpContext; #endif @@ -3013,16 +3013,16 @@ public string ExtensionForContentType(string contentType) return String.Empty; } static String[][] contentTypes = new string[][] { - new string[] {"txt" , "text/plain"}, - new string[] {"rtx" , "text/richtext"}, - new string[] {"htm" , MediaTypesNames.TextHtml}, - new string[] {"html" , MediaTypesNames.TextHtml}, - new string[] {"xml" , "text/xml"}, - new string[] {"rtf" , "text/rtf"}, + new string[] {"txt" , "text/plain"}, + new string[] {"rtx" , "text/richtext"}, + new string[] {"htm" , MediaTypesNames.TextHtml}, + new string[] {"html" , MediaTypesNames.TextHtml}, + new string[] {"xml" , "text/xml"}, + new string[] {"rtf" , "text/rtf"}, new string[] {"a3gpp" , "audio/3gpp"}, new string[] {"aif" , "audio/x-aiff"}, new string[] {"aif" , "audio/aiff"}, - new string[] {"au" , "audio/basic"}, + new string[] {"au" , "audio/basic"}, new string[] {"m4a" , "audio/mp4"}, new string[] {"m4a" , "audio/x-m4a"}, new string[] {"mp4" , "video/mp4"}, @@ -3034,26 +3034,26 @@ public string ExtensionForContentType(string contentType) new string[] {"bmp" , "image/bmp"}, new string[] {"gif" , "image/gif"}, new string[] {"jpg" , "image/jpeg"}, - new string[] {"jpeg" , "image/jpeg"}, + new string[] {"jpeg" , "image/jpeg"}, new string[] {"jpe" , "image/jpeg"}, new string[] {"jpg" , "application/jpg"}, new string[] {"jpeg" , "application/jpeg"}, - new string[] {"jfif" , "image/pjpeg"}, + new string[] {"jfif" , "image/pjpeg"}, new string[] {"tif" , "image/tiff"}, - new string[] {"tiff" , "image/tiff"}, + new string[] {"tiff" , "image/tiff"}, new string[] {"png" , "image/png"}, new string[] {"png" , "image/x-png"}, new string[] {"mpg" , "video/mpeg"}, - new string[] {"mpeg" , "video/mpeg"}, + new string[] {"mpeg" , "video/mpeg"}, new string[] {"mov" , "video/quicktime"}, - new string[] {"qt" , "video/quicktime"}, + new string[] {"qt" , "video/quicktime"}, new string[] {"avi" , "video/x-msvideo"}, new string[] {"divx" , "video/x-divx"}, new string[] {"3gp" , "video/3gpp"}, new string[] {"3g2" , "video/3gpp2"}, new string[] {"exe" , "application/octet-stream"}, new string[] {"dll" , "application/x-msdownload"}, - new string[] {"ps" , "application/postscript"}, + new string[] {"ps" , "application/postscript"}, new string[] {"pdf" , "application/pdf"}, new string[] {"svg" , "image/svg+xml"}, new string[] {"tgz" , "application/x-compressed"}, @@ -3061,12 +3061,12 @@ public string ExtensionForContentType(string contentType) new string[] {"zip" , "application/x-zip-compressed"}, new string[] {"tar" , "application/x-tar"}, new string[] {"rar" , "application/x-rar-compressed"}, - new string[] {"ram" , "audio/vnd.rn-realaudio" }, - new string[] {"gz" , "application/x-gzip"}, + new string[] {"ram" , "audio/vnd.rn-realaudio" }, + new string[] {"gz" , "application/x-gzip"}, new string[] {"xls" , "application/vnd.ms-excel"}, - new string[] {"xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, - new string[] { "doc" , "application/msword"}, - new string[] { "docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + new string[] {"xlsx" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + new string[] { "doc" , "application/msword"}, + new string[] { "docx" , "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, }; public int GetSoapErr() diff --git a/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs b/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs index 3f33872f5..9f72aa3d5 100644 --- a/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs +++ b/dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs @@ -20,6 +20,9 @@ namespace GeneXus.Http.Client using GeneXus.Configuration; using GeneXus.Utils; using log4net; +#if NETCORE + using Microsoft.AspNetCore.WebUtilities; +#endif using Mime; @@ -621,11 +624,13 @@ HttpResponseMessage ExecuteRequest(string method, string requestUrl, CookieConta { handler.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => ServicePointManager.ServerCertificateValidationCallback(sender, certificate, chain, sslPolicyErrors)); } + handler.CookieUsePolicy = CookieUsePolicy.UseSpecifiedCookieContainer; #endif if (GXUtil.CompressResponse()) { handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; } + handler.CookieContainer = cookies; foreach (X509Certificate2 cert in _certificateCollection) @@ -742,10 +747,11 @@ public void HttpClientExecute(string method, string name) bool contextCookies = _context != null && !String.IsNullOrEmpty(requestUrl); CookieContainer cookies = contextCookies ? _context.GetCookieContainer(requestUrl, IncludeCookies) : new CookieContainer(); response = ExecuteRequest(method, requestUrl, cookies); -#if NETCORE + if (contextCookies) _context.UpdateSessionCookieContainer(); -#endif + + } #if NETCORE catch (AggregateException aex) @@ -804,11 +810,11 @@ public void HttpClientExecute(string method, string name) LoadResponseHeaders(response); ReadReponseContent(response); _statusCode = ((short)response.StatusCode); - _statusDescription = response.ReasonPhrase; + _statusDescription = GetStatusCodeDescrption(response); if ((_statusCode >= 400 && _statusCode < 600) && _errCode != 1) { _errCode = 1; - _errDescription = "The remote server returned an error: (" + _statusCode + ") " + response.ReasonPhrase + "."; + _errDescription = "The remote server returned an error: (" + _statusCode + ") " + _statusDescription + "."; } ClearSendStream(); GXLogging.Debug(log, "_responseString " + ToString()); @@ -826,6 +832,26 @@ void LoadResponseHeaders(HttpResponseMessage resp) _respHeaders.Add(header.Key, String.Join(",", header.Value)); } } + + private string GetStatusCodeDescrption(HttpResponseMessage message) + { +#if NETCORE + string statusCodeDescription = ReasonPhrases.GetReasonPhrase((int)message.StatusCode); +#else + string statusCodeDescription = HttpWorkerRequest.GetStatusDescription((int)message.StatusCode); +#endif + string reasonPhrase = message.ReasonPhrase; + + if (string.IsNullOrEmpty(reasonPhrase)) + { + return statusCodeDescription; + } + else + { + return reasonPhrase; + } + } + private void setHeaders(HttpWebRequest req) { string contentType = null; @@ -1289,8 +1315,8 @@ public void ToFile(string fileName) #if !NETCORE if (HttpContext.Current != null) #endif - if (fileName.IndexOfAny(new char[] { '\\', ':' }) == -1) - pathName = Path.Combine(GxContext.StaticPhysicalPath(), fileName); + 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}'