コード例 #1
0
 public RequestHandler(HttpRequest leftSideRequest, RemoteApplication application, TrafficLogger logger)
 {
     _leftSideRequest = leftSideRequest;
     _application     = application;
     _auth            = new Authentication(_leftSideRequest);
     _logger          = logger;
 }
コード例 #2
0
 public void SetLogger(TrafficLogger logger)
 {
     if (RootExecutionScope != null)
     {
         RootExecutionScope._logger = logger;
     }
 }
コード例 #3
0
 private TrafficLogger GetLogger(HttpContext context, RemoteApplication remoteApplication, TraceScope traceScope)
 {
     if (remoteApplication.LogTraffic)
     {
         Authentication auth   = new Authentication(context.Request);
         TrafficLogger  logger = new TrafficLogger(remoteApplication.RemoteApplicationProxyPath, auth.UserId, traceScope, context.Request.Url);
         return(logger);
     }
     return(null);
 }
コード例 #4
0
        //protected virtual string LeftSideUrlTail (string leftSideUrlPart)
        //{
        //  if (!leftSideUrlPart.StartsWith (_path))
        //    throw new ApplicationException ("Url does not start with Path of this Application.");

        //  return leftSideUrlPart.Substring (_path.Length);
        //}

        public virtual HttpWebRequest CreateRightSideRequest(HttpRequest leftSideRequest, Stream inputBuffer, TrafficLogger logger)
        {
            RequestHandler requestHandler   = new RequestHandler(leftSideRequest, this, logger);
            HttpWebRequest rightSideRequest = requestHandler.CreateRightSideRequest(inputBuffer);

            _isSoap = requestHandler.IsSoap;

            //lock (_history)
            //{
            // no problem if history is corrupt, we do not lock
            if (_history.Count >= HistoryMaxLength)
            {
                _history.RemoveAt(0);
            }
            _history.Add(new RemoteApplicationHistory(leftSideRequest, rightSideRequest));
            //}

            return(rightSideRequest);
        }
コード例 #5
0
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            using (TraceScope traceScope = new TraceScope(context))
            {
                traceScope.TraceEvent(TraceEventType.Verbose, (int)Event.ProcessingRequest,
                                      "Processing request started.");
                if (context.Request == null)
                {
                    //happens when application starts first time
                    traceScope.TraceEvent(TraceEventType.Verbose, (int)Event.ProcessingRequest, "Context is null.");
                    return;
                }

                using (var executionScope = new ExecutionScope(nameof(IHttpHandler.ProcessRequest)))
                {
                    if (!s_initialized)
                    {
                        Initialize();
                    }

                    Authentication auth = new Authentication(context.Request);
                    if (ProcessUiPage(context, auth))
                    {
                        return;
                    }

                    RemoteApplication remoteApplication = RemoteApplication.GetRemoteApplication(context.Request);

                    if (remoteApplication == null)
                    {
                        traceScope.TraceEvent(TraceEventType.Warning, (int)Event.NotFound,
                                              "No RemoteApplication found. Ending with 404.");
                        context.Response.StatusCode = 404;
                        context.Response.End();
                    }

                    TrafficLogger logger = GetLogger(context, remoteApplication, traceScope);
                    executionScope.SetLogger(logger);

                    HttpWebRequest  webRequest  = null;
                    HttpWebResponse webResponse = null;

                    using (Stream inputBuffer = CopyFilter.GetInputStream(context.Request))
                    {
                        for (int tries = 0; tries <= Properties.Settings.Default.NetworkRetryCount; tries++)
                        {
                            if (tries > 0)
                            {
                                System.Threading.Thread.Sleep(Properties.Settings.Default.NetworkRetryDelay);
                                traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                      "trying again.");
                                inputBuffer.Position = 0;
                            }

                            using (new ExecutionScope(nameof(remoteApplication.CreateRightSideRequest)))
                            {
                                try
                                {
                                    webRequest = remoteApplication.CreateRightSideRequest(context.Request, inputBuffer, logger);

                                    if (remoteApplication.LogTraffic)
                                    {
                                        logger.LogRequestInformation(webRequest);
                                    }
                                }
                                catch (AuthorizationException e)
                                {
                                    traceScope.TraceEvent(TraceEventType.Verbose, (int)Event.NotAuthorized, e.Message);
                                    context.Response.StatusCode = 403;
                                    context.Response.End();
                                }
                            }

                            traceScope.TraceEvent(TraceEventType.Verbose, (int)Event.ProcessingRequest,
                                                  "Processing request finished.");

                            using (new ExecutionScope(nameof(auth.GetWebResponse)))
                            {
                                try
                                {
                                    webResponse = auth.GetWebResponse(webRequest, traceScope);
                                }
                                catch (WebException ex)
                                {
                                    traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                          "Could not get right side response: {0}", ex.Message);
                                    if (ex.Response == null)
                                    {
                                        if (IsRetryable(ex, remoteApplication.IsSoap))
                                        {
                                            if (webRequest != null)
                                            {
                                                webRequest.Abort();
                                                webRequest = null;
                                            }
                                            continue;
                                        }
                                        else if (ex.Status == WebExceptionStatus.Timeout)
                                        {
                                            traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                                  "Ending with 408 Timeout.");
                                            context.Response.StatusCode = 408;
                                            context.Response.End();

                                            /*
                                             * 408 Request Timeout Der Server hat eine erwartete Anfrage nicht innerhalb des dafür festgelegten Maximalzeitraums erhalten. Die Verbindung zum anfragenden Browser wird deshalb abgebaut. Angeforderte Daten werden nicht übertragen.
                                             * 412 Precondition Failed Eine oder mehrere Bedingungen, die bei der Anfrage gestellt wurden, treffen nicht zu. Die angeforderten Daten werden deshalb nicht übertragen.
                                             * 500 Internal Server Error Der Server kann die angeforderten Daten nicht senden, weil auf dem Server ein Fehler aufgetreten ist. Beispielsweise konnte das aufgerufene CGI-Script nicht gestartet werden.
                                             * 502 Bad Gateway Zum Bearbeiten der Anfrage musste der Server einen anderen Server aufrufen, erhielt dabei jedoch eine Fehlermeldung. Die angeforderten Daten können deshalb nicht gesendet werden.
                                             * 503 Service Unavailable Der Server kann die Anfrage wegen Überlastung nicht bearbeiten. Die angeforderten Daten können deshalb nicht gesendet werden. In der Statusmeldung kann stehen, wann die Anfrage frühestens wieder bearbeitet werden kann. Im Gegensatz zum Status-Code 202 verarbeitet der Server die Daten nicht, sobald er wieder Kapazitäten hat.
                                             * 504 Gateway Timeout Zum Bearbeiten der Anfrage musste der Server einen anderen Server aufrufen, erhielt dabei jedoch nach einem festgelegten Maximalzeitraum keine Antwort. Die angeforderten Daten können deshalb nicht gesendet werden.
                                             */
                                        }
                                        else
                                        {
                                            traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse, "Ending with 500.");
                                            traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                                  "No response available.");
                                            traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                                  "Input stream follows.");
                                            LogInputStream(context, inputBuffer, traceScope);
                                            context.Response.StatusCode        = 500;
                                            context.Response.StatusDescription = ex.Message;
                                            context.Response.End();
                                        }
                                    }
                                    else
                                    {
                                        webResponse = (HttpWebResponse)ex.Response;
                                    }
                                } // end catch webResponse = auth.GetWebResponse (webRequest);
                            }

                            if (ShouldRetry(webResponse, remoteApplication.IsSoap))
                            {
                                traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                      "Received " + webResponse.StatusCode);
                                if (tries < Properties.Settings.Default.NetworkRetryCount)
                                {
                                    webResponse.Close();
                                }
                                webRequest = null;
                                continue;
                            }
                            break;
                        } // end for tries
                    }     // end using inputBuffer

                    if (webResponse == null)
                    {
                        traceScope.TraceEvent(TraceEventType.Verbose, (int)Event.ProcessingResponse,
                                              "Response is null.");
                        context.Response.StatusCode        = 502;
                        context.Response.StatusDescription = "No response reveived from upstream server.";
                        context.Response.End();
                    }

                    bool errorEncountered = webResponse.StatusCode.Equals(HttpStatusCode.InternalServerError);
                    bool bufferResponse   = Settings.Default.BufferRightSide || errorEncountered || remoteApplication.LogTraffic;
                    using (Stream output = GetOutputStream(context.Response, webResponse.ContentLength, bufferResponse))
                    {
                        using (HttpWebResponse response = webResponse)
                        {
                            traceScope.TraceEvent(TraceEventType.Verbose, (int)Event.ProcessingResponse,
                                                  "Processing response started.");
                            if (remoteApplication.LogTraffic)
                            {
                                logger.LogResponseInformation(response);
                            }

                            remoteApplication.ShapeHttpResponse(response, context.Response);

                            Stream rightSideResponseStream = response.GetResponseStream();

                            CopyFilter filter = new CopyFilter(response.ContentLength);
                            filter.FilterStream(rightSideResponseStream, output);

                            traceScope.TraceEvent(TraceEventType.Verbose, (int)Event.ProcessingResponse,
                                                  "Processing response finished.");
                        }

                        if (errorEncountered)
                        {
                            output.Position = 0;
                            byte[] buffer = new byte[output.Length];
                            output.Read(buffer, 0, (int)output.Length);
                            Encoding encoding = Encoding.UTF8;
                            if (context.Response.ContentEncoding != null)
                            {
                                encoding = context.Response.ContentEncoding;
                            }
                            traceScope.TraceEvent(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                  "Response stream follows.");
                            traceScope.TraceData(TraceEventType.Error, (int)Event.ProcessingResponse,
                                                 encoding.GetString(buffer));
                        }

                        if (remoteApplication.LogTraffic)
                        {
                            logger.LogResponseContent((MemoryStream)output);
                        }

                        if (bufferResponse)
                        {
                            output.Position = 0;
                            CopyFilter filter = new CopyFilter(output.Length);
                            filter.FilterStream(output, context.Response.OutputStream);
                        }

                        context.Response.End();
                    }
                }
            }
        }