/// <summary>
        ///     Asserts that the <see cref="IStumpsServer" /> should return an HTTP 404 error message for requests that cannot be handled.
        /// </summary>
        /// <param name="server">The <see cref="IStumpsServer" /> that is processing incoming HTTP requests.</param>
        /// <returns>The calling <see cref="IStumpsServer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="server"/> is <c>null</c>.</exception>
        public static IStumpsServer RespondsWithHttp404(this IStumpsServer server)
        {
            server = server ?? throw new ArgumentNullException(nameof(server));

            server.DefaultResponse = FallbackResponse.Http404NotFound;
            return(server);
        }
        /// <summary>
        ///     Asserts that the <see cref="IStumpsServer" /> should redirect traffic to a specified remote HTTP server.
        /// </summary>
        /// <param name="server">The <see cref="IStumpsServer" /> that is processing incoming HTTP requests.</param>
        /// <param name="remoteServer">The <see cref="Uri"/> for the remote HTTP server.</param>
        /// <returns>The calling <see cref="IStumpsServer" />.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="server"/> is <c>null</c>.</exception>
        public static IStumpsServer IsProxyFor(this IStumpsServer server, Uri remoteServer)
        {
            server = server ?? throw new ArgumentNullException(nameof(server));

            server.RemoteHttpServer = remoteServer;
            return(server);
        }
        /// <summary>
        ///     Asserts that the <see cref="IStumpsServer" /> should not redirect traffic to a remote HTTP server.
        /// </summary>
        /// <param name="server">The <see cref="IStumpsServer" /> that is processing incoming HTTP requests.</param>
        /// <returns>The calling <see cref="IStumpsServer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="server"/> is <c>null</c>.</exception>
        public static IStumpsServer IsNotAProxy(this IStumpsServer server)
        {
            server = server ?? throw new ArgumentNullException(nameof(server));

            server.RemoteHttpServer = null;
            return(server);
        }
        /// <summary>
        ///     Asserts that the <see cref="IStumpsServer" /> should return an HTTP 503 error message for requests that cannot be handled.
        /// </summary>
        /// <param name="server">The <see cref="IStumpsServer" /> that is processing incoming HTTP requests.</param>
        /// <returns>The calling <see cref="IStumpsServer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="server"/> is <c>null</c>.</exception>
        public static IStumpsServer RespondsWithHttp503(this IStumpsServer server)
        {
            server = server ?? throw new ArgumentNullException(nameof(server));

            server.DefaultResponse = FallbackResponse.Http503ServiceUnavailable;
            return(server);
        }
        /// <summary>
        ///     Asserts that the <see cref="IStumpsServer" /> will handle certain HTTP with a <see cref="Stump" />.
        /// </summary>
        /// <param name="server">The <see cref="IStumpsServer" /> that is processing incoming HTTP requests.</param>
        /// <param name="stumpId">The unique identifier for the <see cref="Stump"/> being created.</param>
        /// <returns>A <see cref="Stump"/> created for the <paramref name="server"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="server"/> is <c>null</c>.</exception>
        public static Stump HandlesRequest(this IStumpsServer server, string stumpId)
        {
            server = server ?? throw new ArgumentNullException(nameof(server));

            var stump = server.AddNewStump(stumpId);

            return(stump);
        }
        /// <summary>
        ///     Asserts that the <see cref="IStumpsServer" /> should redirect traffic to a specified remote HTTP server.
        /// </summary>
        /// <param name="server">The <see cref="IStumpsServer" /> that is processing incoming HTTP requests.</param>
        /// <param name="remoteServerUrl">The URL for the remote HTTP server.</param>
        /// <returns>The calling <see cref="IStumpsServer" />.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="server"/> is <c>null</c>.</exception>
        public static IStumpsServer IsProxyFor(this IStumpsServer server, string remoteServerUrl)
        {
            server = server ?? throw new ArgumentNullException(nameof(server));

            var uri = new Uri(remoteServerUrl);

            server.IsProxyFor(uri);
            return(server);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Shows the response to an HTTP request on the screen.
        /// </summary>
        /// <param name="server">The <see cref="StumpsServer"/> that processed the request.</param>
        /// <param name="e">The <see cref="StumpsContextEventArgs"/> instance containing the event data.</param>
        public static void ShowHttpResponse(IStumpsServer server, StumpsContextEventArgs e)
        {
            var message = FriendlyOrigin[e.ResponseOrigin];

            if (e.ResponseOrigin == HttpResponseOrigin.Stump)
            {
                message += e.StumpId;
            }
            else if (e.ResponseOrigin == HttpResponseOrigin.RemoteServer)
            {
                message += server.RemoteHttpServer.DnsSafeHost;
            }

            Console.WriteLine("[{0}] => {1} {2}", message, e.Context.Request.HttpMethod, e.Context.Request.RawUrl);
        }
Exemplo n.º 8
0
        /// <summary>
        ///     Initializes the Stumps server controlled by this instance.
        /// </summary>
        private void InitializeServer()
        {
            // Find the persisted server entity
            var entity = _dataAccess.ServerFind(this.ServerId);
            this.AutoStart = entity.AutoStart;
            this.RemoteServerHostName = entity.RemoteServerHostName;
            this.ListeningPort = entity.Port;
            this.UseSsl = entity.UseSsl;
            this.UseHttpsForIncommingConnections = entity.UseHttpsForIncommingConnections;

            this.RecordingBehavior = entity.DisableStumpsWhenRecording
                                         ? RecordingBehavior.DisableStumps
                                         : RecordingBehavior.LeaveStumpsUnchanged;

            if (!string.IsNullOrWhiteSpace(this.RemoteServerHostName))
            {
                var pattern = this.UseSsl
                                  ? StumpsServerInstance.SecureUriFormat
                                  : StumpsServerInstance.InsecureUriFormat;

                var uriString = string.Format(CultureInfo.InvariantCulture, pattern, this.RemoteServerHostName);

                var uri = new Uri(uriString);

                _server = _serverFactory.CreateServer(this.ListeningPort, uri);
                _server.UseHttpsForIncommingConnections = this.UseHttpsForIncommingConnections;
            }
            else
            {
                // TODO: Choose which method to use for the fallback when no remote server is available.
                _server = _serverFactory.CreateServer(this.ListeningPort, FallbackResponse.Http503ServiceUnavailable);
            }

            _server.RequestFinished += (o, e) =>
            {
                if (this.RecordTraffic)
                {
                    this.Recordings.Add(e.Context);
                }
            };
        }