Esempio n. 1
0
        public int GetNextServer()
        {
            if (!_session.Vars.ContainsKey("server"))
            {
                _session.Vars["server"] = _newClientStrategy.GetNextServer();
            }

            return((int)_session.Vars["server"]);
        }
Esempio n. 2
0
        private bool ProcessRequest(IHttpRequest aRequest, IHttpResponse aResponse, IHttpSession aSession)
        {
            session.HandleSession(aRequest, aResponse);

            IServerDispatcherStrategy serverDispatcher = ChooseStrategy(session);

            int index = serverDispatcher.GetNextServer();

            if (index == -1)
            {
                return(false);
            }

            Logger.WriteLine("Forwarding {0} {1} to server {2}.", aRequest.Method, aRequest.UriPath, _servers[index]);

            HttpWebRequest request = PrepareServerRequest(aRequest, index);


            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                PrepareClientResponse(aResponse, response);
                SetResponseCookies(response, aResponse, index, aRequest.Uri.Host);
                HandleResponse(response, aResponse, aRequest, _servers[index]);
            }
            catch (WebException e) when(e.Status == WebExceptionStatus.Timeout)
            {
                _servers.RemoveAt(index);
            }
            catch (WebException exception)
            {
                if (exception.Response != null)
                {
                    HttpWebResponse response = (HttpWebResponse)exception.Response;
                    PrepareClientResponse(aResponse, response);
                    SetResponseCookies(response, aResponse, index, aRequest.Uri.Host);
                    HandleResponse(response, aResponse, aRequest, _servers[index]);
                }
                else
                {
                    aResponse.Status = HttpStatusCode.InternalServerError;
                    StreamWriter writer = new StreamWriter(aResponse.Body);
                    writer.Write(exception.Message);
                    writer.Flush();
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLine("Exception! - {0}: {1}", ex.GetType().Name, ex.Message);
                aResponse.Status = HttpStatusCode.InternalServerError;
                StreamWriter writer = new StreamWriter(aResponse.Body);
                writer.Write($"{ex.GetType().Name}: {ex.Message}");
                writer.Flush();
            }
            return(true);
        }