private void SetupAndInvokeGetRequest(Type channel,
                                              MethodInfo method,
                                              ChannelMethodDeserializerFactory dsrFactory,
                                              HttpListenerRequest request,
                                              HttpListenerResponse response,
                                              ChannelMethodInfo methodDescription,
                                              ChannelConfigurationInfo channelConfig,
                                              List <object> channelRequestBody,
                                              bool authenticated,
                                              bool hasParams)
        {
            InitChannelMethodContext(channelConfig.Endpoint, request, response, authenticated, channelConfig.HttpMethod, channelRequestBody);
            if (hasParams)
            {
                dsrFactory         = new ChannelMethodDeserializerFactory(request.QueryString);
                channelRequestBody = dsrFactory.DeserializeFromQueryParameters(methodDescription);
                _requestActivator.GetActivateWithParameters(channel, method, channelRequestBody, response);
            }
            else
            {
                _requestActivator.GetActivateWithoutParameters(channel, method, response);
            }

            _contextProvider.DestroyChannelMethodContext(channelConfig.Endpoint);
        }
        private bool TryInvokeGetRequest(Type channel,
                                         MethodInfo method,
                                         HttpListenerRequest request,
                                         HttpListenerResponse response,
                                         ChannelMethodDeserializerFactory dsrFactory,
                                         ChannelMethodInfo methodDescription,
                                         ChannelConfigurationInfo channelConfig,
                                         List <object> channelRequestBody,
                                         bool authenticated)
        {
            try
            {
                if (request.QueryString.AllKeys.Length > 0)
                {
                    SetupAndInvokeGetRequest(channel, method, dsrFactory, request, response, methodDescription, channelConfig, channelRequestBody, authenticated, true);
                }
                else if (request.QueryString.AllKeys.Length == 0)
                {
                    if (methodDescription.Parameters.Count > 0)
                    {
                        StreamWriter writer = new StreamWriter(response.OutputStream);
                        _msgService.ExceptionHandler(writer, new TargetParameterCountException(), response);
                        writer.Close();
                        return(false);
                    }

                    SetupAndInvokeGetRequest(channel, method, dsrFactory, request, response, methodDescription, channelConfig, channelRequestBody, authenticated, false);
                }
                return(true);
            }
            catch (Exception ex)
            {
                using (StreamWriter writer = new StreamWriter(response.OutputStream))
                {
                    _msgService.ExceptionHandler(writer, ex, response);
                    LogChannel.Write(LogSeverity.Fatal, ex.Message);
                }

                return(true);
            }
        }
        internal void SetupAndInvokeGetRequest(Type channel,
                                               MethodInfo method,
                                               HttpListenerRequest request,
                                               HttpListenerResponse response,
                                               ChannelMethodInfo methodDescription,
                                               ChannelConfigurationInfo channelConfig,
                                               List <object> channelRequestBody,
                                               bool authenticated,
                                               bool hasParams,
                                               CacheExecutionResult cacheExecutionResult)
        {
            //Context should be initialized before invoking the method because ChannelBase relies on Context
            InitChannelMethodContext(channelConfig.Endpoint, request, response, authenticated, channelConfig.HttpMethod, channelRequestBody);
            if (hasParams)
            {
                //Since request body will be processed in Heuristics if cache is enabled
                //Data is already stored in Data.Parameters property of CacheExecutionResult
                if (!cacheExecutionResult.DataProcessed)
                {
                    _dsrFactory        = new ChannelMethodDeserializerFactory(request.QueryString);
                    channelRequestBody = _dsrFactory.DeserializeFromQueryParameters(methodDescription);
                }
                else
                {
                    channelRequestBody = cacheExecutionResult.Data.Parameters;
                }

                _requestActivator.GetActivateWithParameters(channel, method, channelRequestBody, response);
            }
            else
            {
                _requestActivator.GetActivateWithoutParameters(channel, method, response);
            }

            _contextProvider.DestroyChannelMethodContext(channelConfig.Endpoint);
        }
        private bool AuthenticationFailedIfRequired(HttpListenerContext context, HttpListenerRequest request, HttpListenerResponse response, ChannelConfigurationInfo channelConfig, out bool authenticated)
        {
            bool failed      = false;
            bool validCookie = false;

            authenticated = false;
            bool authorized = false;

            if (channelConfig.ChannelAttribute.EnableSessions)
            {
                validCookie = ValidSession(request);
            }
            if (channelConfig.AuthenticationRequired && !validCookie)
            {
                try
                {
                    ChannelAuthenticationContext authContext = new ChannelAuthenticationContext
                    {
                        Context = context,
                        Scheme  = channelConfig.AuthScheme,
                        BasicAuthenticationDelegate = _basicAuthenticationMethod,
                        TokenAuthenticationDelegate = _tokenAuthenticationMethod,
                        AuthenticationSettings      = _settings
                    };

                    KeyValuePair <bool, object> authenticationResult = _authenticationService.CheckAuthenticationAndGetResponseObject(authContext);
                    if (authenticationResult.Key == true)
                    {
                        authenticated = true;
                    }
                    else
                    {
                        _msgService.FailedAuthenticationResponse(channelConfig.AuthScheme, response);
                        failed = true;
                    }
                    LogChannel.Write(LogSeverity.Info, "User Authenticated");
                    string claimName  = channelConfig.AuthorizeAttribute.ClaimName;
                    string claimValue = channelConfig.AuthorizeAttribute.ClaimValue;
                    if (!String.IsNullOrEmpty(claimName) && !String.IsNullOrEmpty(claimValue))
                    {
                        if (authenticationResult.Value.GetType() == typeof(ClaimsPrincipal))
                        {
                            authorized = _authenticationService.Authorized(claimName, claimValue, (ClaimsPrincipal)authenticationResult.Value);
                        }
                        else
                        {
                            authorized = _authenticationService.Authorized(claimName, claimValue, (Claim[])authenticationResult.Value);
                        }

                        if (!authorized)
                        {
                            _msgService.FailedAuthorizationResponse(response);
                            LogChannel.Write(LogSeverity.Error, "Failed authorization");
                            failed = true;
                        }
                        else
                        {
                            LogChannel.Write(LogSeverity.Info, "User Authorized");
                        }
                    }
                }
                catch (Exception ex)
                {
                    using (StreamWriter writer = new StreamWriter(response.OutputStream))
                    {
                        _msgService.ExceptionHandler(writer, ex, response);
                        LogChannel.Write(LogSeverity.Error, "Authentication Failed");
                        failed = true;
                    }
                }
                if (!authenticated)
                {
                    failed = true;
                }
                else
                {
                    if (channelConfig.ChannelAttribute.EnableSessions)
                    {
                        string sessionKey    = Guid.NewGuid().ToString();
                        Cookie sessionCookie = new Cookie()
                        {
                            Expires = DateTime.Now.AddMinutes(30),
                            Name    = "channelAuthCookie",
                            Secure  = true,
                            Value   = sessionKey
                        };
                        response.SetCookie(sessionCookie);
                        _sessionKeys.Add(sessionCookie);
                    }
                }
            }

            return(failed);
        }
        public void StartListening(MethodInfo method, Type channel, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            HttpListener httpChannel = new HttpListener();

            ChannelConfigurationInfo channelConfig = _configuration.Configure(httpChannel, channel, method, _baseURL);

            //Keep the ChannelMethod open for new requests
            while (true)
            {
                try
                {
                    httpChannel.Start();
                }
                catch (HttpListenerException hle)
                {
                    Console.WriteLine($"System.Net.HttpListenerException encountered on {channelConfig.MethodUrl} with reason :{hle.Message}");
                    return;
                }

                Console.WriteLine($"Listening on {channelConfig.MethodUrl}");
                HttpListenerContext      context       = httpChannel.GetContext();
                HttpListenerRequest      request       = context.Request;
                HttpListenerResponse     response      = context.Response;
                IChannelHeuristicContext heuristicsCtx = _services.Get <IChannelHeuristicContext>();

                bool executedIfCached = ExecuteIfCached(channel, method, request, response, heuristicsCtx);
                if (executedIfCached)
                {
                    heuristicsCtx.Clear();
                    goto EndRequest;
                }

                LogChannel.Write(LogSeverity.Info, $"Request coming to {channelConfig.Endpoint.Name}");
                LogChannel.Write(LogSeverity.Info, $"HttpMethod:{request.HttpMethod}");

                bool authFailed = AuthenticationFailedIfRequired(context, request, response, channelConfig, out bool authenticated);
                if (authFailed)
                {
                    goto EndRequest;
                }

                //Check if the Http Method is correct
                if (channelConfig.HttpMethod.ToString() != request.HttpMethod && channelConfig.HttpMethod != ChannelHttpMethod.Unknown)
                {
                    _msgService.WrongHttpMethod(response, channelConfig.HttpMethod);
                    LogChannel.Write(LogSeverity.Error, "Wrong HttpMethod... Closing request");
                    goto EndRequest;
                }


                ChannelMethodInfo methodDescription         = _channelMethodDescriptor.GetMethodDescription(method);
                List <object>     channelRequestBody        = null;
                ChannelMethodDeserializerFactory dsrFactory = null;

                if (request.HttpMethod == "GET")
                {
                    bool invoked = TryInvokeGetRequest(channel, method, request, response, dsrFactory, methodDescription, channelConfig, channelRequestBody, authenticated);
                    goto EndRequest;
                }

                //Enter only if Request Body is supplied with POST Method
                if (request.HasEntityBody == true && request.HttpMethod == "POST")
                {
                    StreamWriter writer = new StreamWriter(response.OutputStream);
                    try
                    {
                        dsrFactory         = new ChannelMethodDeserializerFactory(request.InputStream);
                        channelRequestBody = dsrFactory.DeserializeFromBody(methodDescription, request.ContentType);
                        InitChannelMethodContext(channelConfig.Endpoint, request, response, authenticated, channelConfig.HttpMethod, channelRequestBody);
                        _requestActivator.PostActivate(channel, method, channelRequestBody, response);
                    }
                    catch (ChannelMethodContentTypeException cEx)
                    {
                        response.StatusCode = 400;
                        _msgService.ExceptionHandler(writer, cEx, response);
                        LogChannel.Write(LogSeverity.Error, cEx.Message);
                    }
                    catch (ChannelMethodParameterException pEx)
                    {
                        response.StatusCode = 400;
                        _msgService.ExceptionHandler(writer, pEx, response);
                        LogChannel.Write(LogSeverity.Error, pEx.Message);
                    }
                    catch (TargetParameterCountException tEx)
                    {
                        response.StatusCode = 400;
                        _msgService.ExceptionHandler(writer, tEx, response);
                        LogChannel.Write(LogSeverity.Error, tEx.Message);
                    }
                    catch (Exception ex)
                    {
                        response.StatusCode = 500;
                        _msgService.ExceptionHandler(writer, ex, response);
                        LogChannel.Write(LogSeverity.Fatal, ex.Message);
                    }
                    finally
                    {
                        _contextProvider.DestroyChannelMethodContext(channelConfig.Endpoint);
                        writer.Flush();
                        writer.Close();
                    }
                }

EndRequest:
                LogChannel.Write(LogSeverity.Debug, "Request finished...");
                LogChannel.Write(LogSeverity.Debug, "Closing the response");
                response.Close();
            }
        }
        internal void TryInvokePostRequest(Type channel,
                                           MethodInfo method,
                                           HttpListenerRequest request,
                                           HttpListenerResponse response,
                                           List <object> channelRequestBody,
                                           ChannelMethodInfo methodDescription,
                                           ChannelConfigurationInfo channelConfig,
                                           bool authenticated,
                                           CacheExecutionResult cacheExecutionResult)
        {
            StreamWriter writer = new StreamWriter(response.OutputStream);

            try
            {
                //Since request body will be processed in Heuristics if cache is enabled
                //InputStream is flushed and data is already stored in Data.Parameters
                //property of CacheExecutionResult
                if (!cacheExecutionResult.DataProcessed)
                {
                    _dsrFactory        = new ChannelMethodDeserializerFactory(request.InputStream);
                    channelRequestBody = _dsrFactory.DeserializeFromBody(methodDescription, request.ContentType);
                }
                else
                {
                    channelRequestBody = cacheExecutionResult.Data.Parameters;
                }

                InitChannelMethodContext(channelConfig.Endpoint, request, response, authenticated, channelConfig.HttpMethod, channelRequestBody);
                _requestActivator.PostActivate(channel, method, channelRequestBody, response);
            }
            catch (ChannelMethodContentTypeException cEx)
            {
                response.StatusCode = 400;
                _msgService.ExceptionHandler(writer, cEx, response);
                LogChannel.Write(LogSeverity.Error, cEx.Message);
            }
            catch (ChannelMethodParameterException pEx)
            {
                response.StatusCode = 400;
                _msgService.ExceptionHandler(writer, pEx, response);
                LogChannel.Write(LogSeverity.Error, pEx.Message);
            }
            catch (TargetParameterCountException tEx)
            {
                response.StatusCode = 400;
                _msgService.ExceptionHandler(writer, tEx, response);
                LogChannel.Write(LogSeverity.Error, tEx.Message);
            }
            catch (Exception ex)
            {
                response.StatusCode = 500;
                _msgService.ExceptionHandler(writer, ex, response);
                LogChannel.Write(LogSeverity.Fatal, ex.Message);
            }
            finally
            {
                _contextProvider.DestroyChannelMethodContext(channelConfig.Endpoint);
                writer.Flush();
                writer.Close();
            }
        }