Example #1
0
 public void DoLogin(Uri pLoginUrl, string tPost, string MimeTypeOverride = null, cdeConcurrentDictionary <string, string> paddHeader = null)
 {
     if (MyRequestData == null)
     {
         MyRequestData = new TheRequestData();
     }
     MyRequestData.PostData   = TheCommonUtils.CUTF8String2Array(tPost);
     MyRequestData.RequestUri = pLoginUrl;
     MyRequestData.HttpMethod = "POST";
     MyRequestData.Header     = null;
     if (paddHeader != null)
     {
         MyRequestData.Header = new cdeConcurrentDictionary <string, string>();
         foreach (string key in paddHeader.Keys)
         {
             MyRequestData.Header.TryAdd(key, paddHeader[key]);
         }
     }
     if (!string.IsNullOrEmpty(MimeTypeOverride))
     {
         MyRequestData.ResponseMimeType = MimeTypeOverride; // "application/x-www-form-urlencoded; charset=UTF-8";
     }
     else
     {
         MyRequestData.ResponseMimeType = mPostMimeType; // "application/x-www-form-urlencoded; charset=UTF-8";
     }
     MyRequestData.ResponseBuffer  = null;
     MyRequestData.TimeOut         = 15000;
     MyRequestData.DisableRedirect = true;
     MyRequestData.RequestCookies  = MyRequestData?.SessionState?.StateCookies;
     MyRest.PostRESTAsync(MyRequestData, LoginSuccess, sinkError);
 }
Example #2
0
        public void RegisterDeviceWithServer(Uri pTargetServer, Action <string, TheUPnPDeviceInfo> pCallback)
        {
            TheREST tRest   = new TheREST();
            var     builder = new UriBuilder(pTargetServer)
            {
                Path = "DEVICEREG.JSON"
            };

            tRest.PostRESTAsync(builder.Uri, sinkRegResult, TheCommonUtils.SerializeObjectToJSONString(MyDeviceUPnPInfo), pCallback, sinkRegResult);
        }
        private void ReadHttpPage(TheRequestData pRequest, Guid MyAppGuid, string tMagixc, Action <TheRequestData> pSinkProcessResponse) //TheRelayAppInfo MyApp,
        {
            TheRelayAppInfo MyApp = TheThingRegistry.GetThingObjectByMID(MyBaseEngine.GetEngineName(), MyAppGuid) as TheRelayAppInfo;    // MyRelayApps.MyMirrorCache.GetEntryByFunc(s => s.cdeMID.Equals(MyAppGuid));

            if (MyApp == null)
            {
                if (pRequest.cdeN.Equals(TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID))
                {
                    TSM tTSM = new TSM(MyBaseEngine.GetEngineName(), "WEBRELAY_REQUEST")
                    {
                        PLB = pRequest.PostData
                    };
                    pRequest.PostData       = null;
                    pRequest.ResponseBuffer = null;
                    if (!string.IsNullOrEmpty(pRequest.CookieString))
                    {
                        pRequest.CookieString += ";";
                    }
                    else
                    {
                        pRequest.CookieString = "";
                    }
                    pRequest.CookieString += tMagixc;
                    if (string.IsNullOrEmpty(TheBaseAssets.MyServiceHostInfo.RootDir))
                    {
                        pRequest.RequestUriString = pRequest.RequestUri.ToString();
                    }
                    else
                    {
                        pRequest.RequestUriString = pRequest.RequestUri.Scheme + "://" + pRequest.RequestUri.Host + ":" + pRequest.RequestUri.Port + pRequest.cdeRealPage;
                        if (!string.IsNullOrEmpty(pRequest.RequestUri.Query))
                        {
                            pRequest.RequestUriString += "?" + pRequest.RequestUri.Query;
                        }
                    }
                    TheBaseAssets.MySYSLOG.WriteToLog(400, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(MyBaseEngine.GetEngineName(), string.Format("Requesting Page:{0}", pRequest.RequestUriString), eMsgLevel.l6_Debug));

                    tTSM.PLS = TheCommonUtils.SerializeObjectToJSONString(pRequest);
                    tTSM.SID = pRequest.SessionState.GetSID();
                    TheCommCore.PublishCentral(tTSM); //  .PublishToNode(MyApp.HostUrl, pRequest.SessionState.SScopeID, tTSM);
                }
                return;
            }
            if (TheCommonUtils.IsUrlLocalhost(MyApp.GetBaseThing().Address))
            {
                TheCommonUtils.GetAnyFile(pRequest);
                if (tMagixc != null)
                {
                    pRequest.CookieString = tMagixc;
                }
                pSinkProcessResponse(pRequest);
            }
            else
            {
                int    relPathIndex = pRequest.RequestUri.AbsolutePath.IndexOf('/', 1);
                string relPath      = relPathIndex == -1 ? "" : pRequest.RequestUri.AbsolutePath.Substring(relPathIndex + 1) + pRequest.RequestUri.Query;
                var    tUri         = pRequest.RequestUri.AbsolutePath.StartsWith("/CDEWRA") ? new Uri(MyApp.GetBaseThing().Address + MyApp.HomePage + relPath) : new Uri(MyApp.GetBaseThing().Address.TrimEnd('/') + pRequest.RequestUri.AbsolutePath + pRequest.RequestUri.Query);
                if (!string.IsNullOrEmpty(tMagixc))
                {
                    if (!string.IsNullOrEmpty(pRequest.CookieString))
                    {
                        pRequest.CookieString += ";";
                    }
                    else
                    {
                        pRequest.CookieString = "";
                    }
                    pRequest.CookieString += tMagixc;
                }
                pRequest.RequestUri       = tUri;
                pRequest.ErrorDescription = "";
                pRequest.RequestCookies   = pRequest.SessionState.StateCookies;
                if (!string.IsNullOrEmpty(MyApp.SUID) && !string.IsNullOrEmpty(MyApp.SPWD))
                {
                    pRequest.UID = MyApp.SUID;
                    pRequest.PWD = MyApp.SPWD;
                }
                if (MyApp.IsHTTP10)
                {
                    pRequest.HttpVersion = 1.0;
                }
                if (string.IsNullOrEmpty(MyApp.CloudUrl))
                {
                    MyApp.CloudUrl = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false);
                }
                pRequest.Header = null;
                if (pRequest.PostData != null && pRequest.PostData.Length > 0)      //MONO Relay might not work right!
                {
                    TheREST MyRest = new TheREST();
                    MyRest.PostRESTAsync(pRequest, pSinkProcessResponse, pSinkProcessResponse);
                }
                else
                {
                    TheREST.GetRESTAsync(pRequest, pSinkProcessResponse, pSinkProcessResponse);
                }
                TheBaseAssets.MySYSLOG.WriteToLog(400, TSM.L(eDEBUG_LEVELS.VERBOSE) ? null : new TSM(MyBaseEngine.GetEngineName(), string.Format("Requesting Page:{0} Sent", pRequest.RequestUri), eMsgLevel.l3_ImportantMessage));
            }
        }
Example #4
0
        public override void OnNewEvent(TheProcessMessage timer, object unused)
        {
            TheEventLogEntry tEntry = timer?.Cookie as TheEventLogEntry;

            if (tEntry == null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(MyBaseThing.Address) && tEntry?.Message?.TXT?.Contains(MyBaseThing.Address) == true) //Avoid recursive
            {
                return;
            }
            var t = new TheGELFLogEntry()
            {
                version = "1.1",
                host    = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false),
                level   = (int)tEntry.Message?.LVL,
#if CDE_NET45 || CDE_NET4 || CDE_NET35
                timestamp = TheCommonUtils.CDbl($"{(tEntry.Message.TIM.Subtract(new DateTime(1970, 1, 1))).TotalSeconds}.{tEntry.Message.TIM.Millisecond}"),
#else
                timestamp = TheCommonUtils.CDbl($"{tEntry.Message.TIM.ToUnixTimeSeconds()}.{tEntry.Message.TIM.Millisecond}"),
#endif
                full_message  = tEntry.Message?.PLS,
                short_message = tEntry.Message?.TXT,
                _log_id       = tEntry.EventID,
                _serial_no    = tEntry.Serial,
                _engine       = tEntry.Message.ENG,
                _device_id    = tEntry.Message.GetOriginator().ToString()
            };

            try
            {
                if (!string.IsNullOrEmpty(MyBaseThing.Address))
                {
                    var pData = new TheRequestData()
                    {
                        RequestUri       = new Uri($"{MyBaseThing.Address}/gelf"),
                        ResponseMimeType = "application/json",
                        PostData         = TheCommonUtils.CUTF8String2Array(TheCommonUtils.SerializeObjectToJSONString <TheGELFLogEntry>(t)),
                        DontCompress     = true
                    };

                    MyRest.PostRESTAsync(pData, (okData) =>
                    {
                        MyBaseThing.StatusLevel = 1;
                    }, (errData) =>
                    {
                        MyBaseThing.LastMessage = $"Post to Log Error: {errData.ErrorDescription}";
                        MyBaseThing.StatusLevel = 2;
                    });
                }
            }
            catch (Exception e)
            {
                MyBaseThing.LastMessage = $"Cannot Post to Log Error: {e.Message}";
                MyBaseThing.StatusLevel = 2;
            }

            if (TheThing.GetSafePropertyBool(MyBaseThing, "ToConsole"))
            {
                Console.WriteLine(TheCommonUtils.SerializeObjectToJSONString <TheGELFLogEntry>(t));
            }
        }