public virtual void Start(IPathParser parser)
        {
            if (!port.HasValue)
            {
                throw new InvalidOperationException("A port must be specified before starting the listener.");
            }
            ErrorOccured += OnErrorOccured;
            dispatcher    = new HttpContextDispatcher(parser, actionTracking);

            isListening = true;
            server      = parser.GetServerUrl(null, null);
            listener    = new TcpListener(IPAddress.Parse(server), Port);
            listener.Start();

            listener.BeginAcceptTcpClient(Accept, null);
        }
예제 #2
0
        public void Dispatch(IHttpContext connection)
        {
            RequestHandlerBase handler = null;

            try
            {
                IHttpRequest request = connection.Request;
                if ("/!stats/request".Equals(request.LocalPath, StringComparison.InvariantCultureIgnoreCase))
                {
                    new StatsRenderer(Container.Resolve <ActionTrackingViaPerfCounter>()).Render(connection);
                    return;
                }

                NetworkCredential credential = GetCredential(connection);
                string            tfsUrl     = parser.GetServerUrl(request, credential);
                if (string.IsNullOrEmpty(tfsUrl))
                {
                    SendFileNotFoundResponse(connection);
                    return;
                }

                if (credential != null && (tfsUrl.ToLowerInvariant().EndsWith("codeplex.com") || tfsUrl.ToLowerInvariant().Contains("tfs.codeplex.com")))
                {
                    string username = credential.UserName;
                    string domain   = credential.Domain;
                    if (!username.ToLowerInvariant().EndsWith("_cp"))
                    {
                        username += "_cp";
                    }
                    if (domain == "")
                    {
                        domain = "snd";
                    }
                    credential = new NetworkCredential(username, credential.Password, domain);
                }
                RequestCache.Items["serverUrl"]   = tfsUrl;
                RequestCache.Items["projectName"] = parser.GetProjectName(request);
                RequestCache.Items["credentials"] = credential;

                handler = GetHandler(connection.Request.HttpMethod);
                if (handler == null)
                {
                    actionTracking.Error();
                    SendUnsupportedMethodResponse(connection);
                    return;
                }

                try
                {
                    actionTracking.Request(handler);
                    handler.Handle(connection, parser, credential);
                }
                catch (TargetInvocationException e)
                {
                    ExceptionHelper.PreserveStackTrace(e.InnerException);
                    throw e.InnerException;
                }
            }
            catch (WebException ex)
            {
                actionTracking.Error();

                HttpWebResponse response = ex.Response as HttpWebResponse;

                if (response != null && response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    SendUnauthorizedResponse(connection);
                }
                else
                {
                    throw;
                }
            }
            catch (NetworkAccessDeniedException)
            {
                SendUnauthorizedResponse(connection);
            }
            catch (IOException)
            {
                // Error caused by client cancelling operation under IIS 6
                if (Configuration.LogCancelErrors)
                {
                    throw;
                }
            }
            catch (HttpException ex)
            {
                // Check if error caused by client cancelling operation under IIS 7
                if (!ex.Message.StartsWith("An error occurred while communicating with the remote host.") &&
                    !ex.Message.StartsWith("The remote host closed the connection."))
                {
                    throw;
                }

                if (Configuration.LogCancelErrors)
                {
                    throw;
                }
            }
            finally
            {
                if (handler != null)
                {
                    handler.Cancel();
                }
            }
        }