示例#1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="T:Stumps.Web.ViewModules.StumpEditorModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="T:Stumps.Server.IStumpsHost"/> used by the instance.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public StumpEditorModule(IStumpsHost stumpsHost)
        {
            if (stumpsHost == null)
            {
                throw new ArgumentNullException("stumpsHost");
            }

            Get["/proxy/{serverId}/recording/{recordIndex}/newstump"] = _ =>
            {
                var serverId = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var server = stumpsHost.FindServer(serverId);

                var model = new
                {
                    StumpName = "Stump - " + System.Environment.TickCount.ToString(CultureInfo.InvariantCulture),
                    Origin = (int)StumpOrigin.RecordedContext,
                    StumpId = string.Empty,
                    ProxyId = server.ServerId,
                    ExternalHostName = server.UseSsl ? server.RemoteServerHostName + " (SSL)" : server.RemoteServerHostName,
                    LocalWebsite = "http://localhost:" + server.ListeningPort.ToString(CultureInfo.InvariantCulture) + "/",
                    BackUrl = "/proxy/" + serverId + "/recordings",
                    CreateButtonText = "Create New Stump",
                    LoadRecord = true,
                    LoadStump = false,
                    RecordIndex = recordIndex
                };

                return View["stumpeditor", model];
            };

            Get["/proxy/{serverId}/stumps/{stumpId}"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId = (string)_.stumpId;
                var server = stumpsHost.FindServer(serverId);
                var stump = server.FindStump(stumpId);

                var model = new
                {
                    StumpName = stump.StumpName,
                    Origin = (int)StumpOrigin.ExistingStump,
                    StumpId = stump.StumpId,
                    ProxyId = server.ServerId,
                    ExternalHostName = server.UseSsl ? server.RemoteServerHostName + " (SSL)" : server.RemoteServerHostName,
                    LocalWebsite = "http://localhost:" + server.ListeningPort.ToString(CultureInfo.InvariantCulture) + "/",
                    BackUrl = "/proxy/" + serverId + "/stumps",
                    CreateButtonText = "Save Stump",
                    LoadRecord = false,
                    LoadStump = true,
                    RecordIndex = 0
                };

                return View["stumpeditor", model];
            };
        }
示例#2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="StumpEditorModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="IStumpsHost"/> used by the instance.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public StumpEditorModule(IStumpsHost stumpsHost)
        {
            stumpsHost = stumpsHost ?? throw new ArgumentNullException(nameof(stumpsHost));

            Get["/proxy/{serverId}/recording/{recordIndex}/newstump"] = _ =>
            {
                var serverId    = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var server      = stumpsHost.FindServer(serverId);

                var model = new
                {
                    StumpName        = $"Stump - {Environment.TickCount}",
                    Origin           = (int)StumpOrigin.RecordedContext,
                    StumpId          = string.Empty,
                    ProxyId          = server.ServerId,
                    ExternalHostName = server.UseSsl ? server.RemoteServerHostName + " (SSL)" : server.RemoteServerHostName,
                    LocalWebsite     = $"http://localhost:{server.ListeningPort}/",
                    BackUrl          = $"/proxy/{serverId}/recordings",
                    CreateButtonText = "Create New Stump",
                    LoadRecord       = true,
                    LoadStump        = false,
                    RecordIndex      = recordIndex
                };

                return(View["stumpeditor", model]);
            };

            Get["/proxy/{serverId}/stumps/{stumpId}"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId  = (string)_.stumpId;
                var server   = stumpsHost.FindServer(serverId);
                var stump    = server.FindStump(stumpId);

                var model = new
                {
                    StumpName        = stump.StumpName,
                    Origin           = (int)StumpOrigin.ExistingStump,
                    StumpId          = stump.StumpId,
                    ProxyId          = server.ServerId,
                    ExternalHostName = server.UseSsl ? server.RemoteServerHostName + " (SSL)" : server.RemoteServerHostName,
                    LocalWebsite     = $"http://localhost:{server.ListeningPort}/",
                    BackUrl          = $"/proxy/{serverId}/stumps",
                    CreateButtonText = "Save Stump",
                    LoadRecord       = false,
                    LoadStump        = true,
                    RecordIndex      = 0
                };

                return(View["stumpeditor", model]);
            };
        }
示例#3
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="T:Stumps.Web.ViewModules.DeleteStumpModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="T:Stumps.Server.IStumpsHost"/> used by the instance.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public DeleteStumpModule(IStumpsHost stumpsHost)
        {
            if (stumpsHost == null)
            {
                throw new ArgumentNullException("stumpsHost");
            }

            Get["/proxy/{serverId}/stumps/{stumpId}/delete"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId = (string)_.stumpId;
                var server = stumpsHost.FindServer(serverId);
                var stump = server.FindStump(stumpId);

                var model = new
                {
                    StumpName = stump.StumpName,
                    StumpId = stump.StumpId,
                    ProxyId = server.ServerId
                };

                return View["deletestump", model];

            };
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="T:Stumps.Web.ApiModules.ProxyServerStatusModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="T:Stumps.Server.IStumpsHost"/> used by the instance.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public ProxyServerStatusModule(IStumpsHost stumpsHost)
        {
            if (stumpsHost == null)
            {
                throw new ArgumentNullException("stumpsHost");
            }

            Get["/api/proxy/{serverId}/status"] = _ =>
            {

                var serverId = (string)_.serverId;
                var server = stumpsHost.FindServer(serverId);

                var model = new RunningStatusModel
                {
                    IsRunning = server.IsRunning
                };

                return Response.AsJson(model);

            };

            Put["/api/proxy/{serverId}/status"] = _ =>
            {

                var serverId = (string)_.serverId;
                var environment = stumpsHost.FindServer(serverId);

                var model = this.Bind<RunningStatusModel>();

                if (model.IsRunning && !environment.IsRunning)
                {
                    stumpsHost.Start(serverId);
                }
                else if (!model.IsRunning && environment.IsRunning)
                {
                    stumpsHost.Shutdown(serverId);
                }

                return Response.AsJson(model);

            };
        }
示例#5
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="ProxyServerStatusModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="IStumpsHost"/> used by the instance.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public ProxyServerStatusModule(IStumpsHost stumpsHost)
        {
            stumpsHost = stumpsHost ?? throw new ArgumentNullException(nameof(stumpsHost));

            Get["/api/proxy/{serverId}/status"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server   = stumpsHost.FindServer(serverId);

                var model = new RunningStatusModel
                {
                    IsRunning = server.IsRunning
                };

                return(Response.AsJson(model));
            };

            Put["/api/proxy/{serverId}/status"] = _ =>
            {
                var serverId    = (string)_.serverId;
                var environment = stumpsHost.FindServer(serverId);

                var model = this.Bind <RunningStatusModel>();

                if (model.IsRunning && !environment.IsRunning)
                {
                    stumpsHost.Start(serverId);
                }
                else if (!model.IsRunning && environment.IsRunning)
                {
                    stumpsHost.Shutdown(serverId);
                }

                return(Response.AsJson(model));
            };
        }
        /// <summary>
        ///     Initializes a new instance of the <see cref="DeleteWebsiteModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="IStumpsHost"/> used by the instance.</param>
        /// <exception cref="ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public DeleteWebsiteModule(IStumpsHost stumpsHost)
        {
            stumpsHost = stumpsHost ?? throw new ArgumentNullException(nameof(stumpsHost));

            Get["/proxy/{serverId}/delete"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server   = stumpsHost.FindServer(serverId);

                var model = new
                {
                    ProxyId          = server.ServerId,
                    ExternalHostName = server.RemoteServerHostName
                };

                return(View["deletewebsite", model]);
            };
        }
示例#7
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="T:Stumps.Web.ViewModules.RecordingsModule"/> class.
        /// </summary>
        /// <param name="serverHost">The <see cref="T:Stumps.Server.IStumpsHost"/> used by the instance.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="serverHost"/> is <c>null</c>.</exception>
        public RecordingsModule(IStumpsHost serverHost)
        {
            if (serverHost == null)
            {
                throw new ArgumentNullException("serverHost");
            }

            Get["/proxy/{serverId}/recordings"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server = serverHost.FindServer(serverId);

                var recordingModelArray = new ArrayList();

                var lastIndex = -1;

                var recordingList = server.Recordings.Find(-1);
                for (var i = 0; i < recordingList.Count; i++)
                {
                    var recordingModel = new
                    {
                        Index = i,
                        Method = recordingList[i].Request.HttpMethod,
                        RawUrl = recordingList[i].Request.RawUrl,
                        StatusCode = recordingList[i].Response.StatusCode
                    };

                    recordingModelArray.Add(recordingModel);
                    lastIndex = i;
                }

                var model = new
                {
                    ProxyId = server.ServerId,
                    ExternalHostName = server.UseSsl ? server.RemoteServerHostName + " (SSL)" : server.RemoteServerHostName,
                    LocalWebsite = "http://localhost:" + server.ListeningPort.ToString(CultureInfo.InvariantCulture) + "/",
                    IsRecording = server.RecordTraffic,
                    LastIndex = lastIndex,
                    Recordings = recordingModelArray
                };

                return View["recordings", model];
            };
        }
示例#8
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="RecordingsModule"/> class.
        /// </summary>
        /// <param name="serverHost">The <see cref="IStumpsHost"/> used by the instance.</param>
        /// <exception cref="ArgumentNullException"><paramref name="serverHost"/> is <c>null</c>.</exception>
        public RecordingsModule(IStumpsHost serverHost)
        {
            serverHost = serverHost ?? throw new ArgumentNullException(nameof(serverHost));

            Get["/proxy/{serverId}/recordings"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server   = serverHost.FindServer(serverId);

                var recordingModelArray = new ArrayList();

                var lastIndex = -1;

                var recordingList = server.Recordings.Find(-1);
                for (var i = 0; i < recordingList.Count; i++)
                {
                    var recordingModel = new
                    {
                        Index      = i,
                        Method     = recordingList[i].Request.HttpMethod,
                        RawUrl     = recordingList[i].Request.RawUrl,
                        StatusCode = recordingList[i].Response.StatusCode
                    };

                    recordingModelArray.Add(recordingModel);
                    lastIndex = i;
                }

                var model = new
                {
                    ProxyId          = server.ServerId,
                    ExternalHostName = server.UseSsl ? server.RemoteServerHostName + " (SSL)" : server.RemoteServerHostName,
                    LocalWebsite     = $"http://localhost:{server.ListeningPort}/",
                    IsRecording      = server.RecordTraffic,
                    LastIndex        = lastIndex,
                    Recordings       = recordingModelArray
                };

                return(View["recordings", model]);
            };
        }
示例#9
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="T:Stumps.Web.ViewModules.DeleteWebsiteModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="T:Stumps.Server.IStumpsHost"/> used by the instance.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public DeleteWebsiteModule(IStumpsHost stumpsHost)
        {
            if (stumpsHost == null)
            {
                throw new ArgumentNullException("stumpsHost");
            }

            Get["/proxy/{serverId}/delete"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server = stumpsHost.FindServer(serverId);

                var model = new
                {
                    ProxyId = server.ServerId,
                    ExternalHostName = server.RemoteServerHostName
                };

                return View["deletewebsite", model];
            };
        }
示例#10
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="T:Stumps.Web.ViewModules.StumpsOverviewModule"/> class.
        /// </summary>
        /// <param name="stumpsHost">The <see cref="T:Stumps.Server.IStumpsHost"/> used by the instance.</param>
        /// <exception cref="System.ArgumentNullException"><paramref name="stumpsHost"/> is <c>null</c>.</exception>
        public StumpsOverviewModule(IStumpsHost stumpsHost)
        {
            if (stumpsHost == null)
            {
                throw new ArgumentNullException("stumpsHost");
            }

            Get["/proxy/{serverId}/stumps"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server = stumpsHost.FindServer(serverId);

                var stumpModelArray = new ArrayList();

                var stumpContractList = server.FindAllContracts();
                stumpContractList = stumpContractList.OrderBy(x => x.StumpName).ToList();

                foreach (var contract in stumpContractList)
                {
                    var stumpModel = new
                    {
                        StumpId = contract.StumpId,
                        StumpName = contract.StumpName
                    };

                    stumpModelArray.Add(stumpModel);
                }

                var model = new
                {
                    ProxyId = server.ServerId,
                    ExternalHostName = server.UseSsl ? server.RemoteServerHostName + " (SSL)" : server.RemoteServerHostName,
                    LocalWebsite = "http://localhost:" + server.ListeningPort.ToString(CultureInfo.InvariantCulture) + "/",
                    Stumps = stumpModelArray
                };

                return View["stumpsoverview", model];
            };
        }
示例#11
0
        public StumpsModule(IStumpsHost serverHost)
        {
            if (serverHost == null)
            {
                throw new ArgumentNullException("serverHost");
            }

            Get["/api/proxy/{serverId}/stumps/{stumpId}"] = _ =>
            {

                var serverId = (string)_.serverId;
                var stumpId = (string)_.stumpId;
                var server = serverHost.FindServer(serverId);
                var stump = server.FindStump(stumpId);

                var model = CreateStumpModel(stump, serverId, stumpId);

                return Response.AsJson(model);

            };

            Get["/api/proxy/{serverId}/stumps/{stumpId}/request"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId = (string)_.stumpId;
                var server = serverHost.FindServer(serverId);
                var stump = server.FindStump(stumpId);

                var ms = new System.IO.MemoryStream(stump.OriginalRequest.GetBody());

                return Response.FromStream(ms, stump.OriginalRequest.Headers["Content-Type"]);
            };

            Get["/api/proxy/{serverId}/stumps/{stumpId}/response"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId = (string)_.stumpId;
                var server = serverHost.FindServer(serverId);
                var stump = server.FindStump(stumpId);

                var ms = new System.IO.MemoryStream(stump.Response.GetBody());

                return Response.FromStream(ms, stump.Response.Headers["Content-Type"] ?? string.Empty);
            };

            Post["/api/proxy/{serverId}/stumps"] = _ =>
            {

                var serverId = (string)_.serverId;
                var server = serverHost.FindServer(serverId);

                var model = this.Bind<StumpModel>();
                var contract = CreateContractFromRecord(model, server);

                server.CreateStump(contract);

                return HttpStatusCode.OK;

            };

            Put["/api/proxy/{serverId}/stumps/{stumpId}"] = _ =>
            {

                var serverId = (string)_.serverId;

                var server = serverHost.FindServer(serverId);

                var model = this.Bind<StumpModel>();
                var contract = CreateContractFromStump(model, server);

                if (server.FindStump(contract.StumpId).Equals(null))
                {
                    throw new ArgumentException("Stump name cannot be null.");
                }

                if (server.StumpNameExists(contract.StumpName))
                {
                    var oldStump = server.FindStump(contract.StumpId);
                    if (!oldStump.StumpName.Equals(contract.StumpName, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException("Attempting to create a stump with a name that already exists.");
                    }
                }

                server.DeleteStump(model.StumpId);
                server.CreateStump(contract);

                var stump = server.FindStump(model.StumpId);

                var returnModel = CreateStumpModel(stump, serverId, model.StumpId);

                return Response.AsJson(returnModel);

            };

            Delete["/api/proxy/{serverId}/stumps/{stumpId}/delete"] = _ =>
            {

                var serverId = (string)_.serverId;
                var stumpId = (string)_.stumpId;
                var server = serverHost.FindServer(serverId);
                server.DeleteStump(stumpId);

                return HttpStatusCode.OK;

            };

            Get["/api/proxy/{serverId}/stumps/isStumpNameAvailable/{stumpName}"] = _ =>
            {

                var serverId = (string)_.serverId;
                var stumpName = (string)_.stumpName;
                var server = serverHost.FindServer(serverId);

                var isStumpNameAvailable = !server.StumpNameExists(stumpName);

                var model = new
                {
                    StumpNameIsAvailable = isStumpNameAvailable
                };

                return Response.AsJson(model);

            };
        }
示例#12
0
        public StumpsModule(IStumpsHost serverHost)
        {
            serverHost = serverHost ?? throw new ArgumentNullException(nameof(serverHost));

            Get["/api/proxy/{serverId}/stumps/{stumpId}"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId  = (string)_.stumpId;
                var server   = serverHost.FindServer(serverId);
                var stump    = server.FindStump(stumpId);

                var model = CreateStumpModel(stump, serverId, stumpId);

                return(Response.AsJson(model));
            };

            Get["/api/proxy/{serverId}/stumps/{stumpId}/request"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId  = (string)_.stumpId;
                var server   = serverHost.FindServer(serverId);
                var stump    = server.FindStump(stumpId);

                var ms = new System.IO.MemoryStream(stump.OriginalRequest.GetBody());

                return(Response.FromStream(ms, stump.OriginalRequest.Headers["Content-Type"]));
            };

            Get["/api/proxy/{serverId}/stumps/{stumpId}/response"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId  = (string)_.stumpId;
                var server   = serverHost.FindServer(serverId);
                var stump    = server.FindStump(stumpId);

                var ms = new System.IO.MemoryStream(stump.Response.GetBody());

                return(Response.FromStream(ms, stump.Response.Headers["Content-Type"] ?? string.Empty));
            };

            Post["/api/proxy/{serverId}/stumps"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server   = serverHost.FindServer(serverId);

                var model    = this.Bind <StumpModel>();
                var contract = CreateContractFromRecord(model, server);

                server.CreateStump(contract);

                return(HttpStatusCode.OK);
            };

            Put["/api/proxy/{serverId}/stumps/{stumpId}"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server   = serverHost.FindServer(serverId);

                var model    = this.Bind <StumpModel>();
                var contract = CreateContractFromStump(model, server);

                if (server.FindStump(contract.StumpId).Equals(null))
                {
                    throw new ArgumentException("Stump name cannot be null.");
                }

                if (server.StumpNameExists(contract.StumpName))
                {
                    var oldStump = server.FindStump(contract.StumpId);
                    if (!oldStump.StumpName.Equals(contract.StumpName, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new ArgumentException("Attempting to create a stump with a name that already exists.");
                    }
                }

                server.DeleteStump(model.StumpId);
                server.CreateStump(contract);

                var stump = server.FindStump(model.StumpId);

                var returnModel = CreateStumpModel(stump, serverId, model.StumpId);

                return(Response.AsJson(returnModel));
            };

            Delete["/api/proxy/{serverId}/stumps/{stumpId}/delete"] = _ =>
            {
                var serverId = (string)_.serverId;
                var stumpId  = (string)_.stumpId;
                var server   = serverHost.FindServer(serverId);
                server.DeleteStump(stumpId);

                return(HttpStatusCode.OK);
            };

            Get["/api/proxy/{serverId}/stumps/isStumpNameAvailable/{stumpName}"] = _ =>
            {
                var serverId  = (string)_.serverId;
                var stumpName = (string)_.stumpName;
                var server    = serverHost.FindServer(serverId);

                var isStumpNameAvailable = !server.StumpNameExists(stumpName);

                var model = new
                {
                    StumpNameIsAvailable = isStumpNameAvailable
                };

                return(Response.AsJson(model));
            };
        }
示例#13
0
        public RecordingModule(IStumpsHost stumpsHost)
        {
            if (stumpsHost == null)
            {
                throw new ArgumentNullException("stumpsHost");
            }

            Get["/api/proxy/{serverId}/recording"] = _ =>
            {
                var serverId = (string)_.serverId;
                var environment = stumpsHost.FindServer(serverId);
                var afterIndex = -1;

                if (Request.Query.after != null)
                {
                    var afterIndexString = (string)Request.Query.after;
                    afterIndex = int.TryParse(afterIndexString, out afterIndex) ? afterIndex : -1;
                }

                var recordingList = environment.Recordings.Find(afterIndex);
                var modelList = new List<RecordingModel>();

                foreach (var recording in recordingList)
                {
                    afterIndex++;

                    var model = new RecordingModel
                    {
                        Index = afterIndex,
                        Date = recording.ReceivedDate,
                        Method = recording.Request.HttpMethod,
                        RawUrl = recording.Request.RawUrl,
                        RequestSize = recording.Request.BodyLength,
                        ResponseSize = recording.Response.BodyLength,
                        StatusCode = recording.Response.StatusCode,
                        StatusDescription = recording.Response.StatusDescription
                    };

                    modelList.Add(model);
                }

                return Response.AsJson(modelList);
            };

            Get["/api/proxy/{serverId}/recording/{recordIndex}"] = _ =>
            {
                var serverId = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var server = stumpsHost.FindServer(serverId);

                var record = server.Recordings.FindAt(recordIndex);

                var model = new RecordingDetailsModel
                {
                    Index = recordIndex,
                    RequestBody = string.Empty,
                    RequestBodyIsImage = record.Request.BodyType == HttpBodyClassification.Image,
                    RequestBodyIsText = record.Request.BodyType == HttpBodyClassification.Text,
                    RequestBodyLength = record.Request.BodyLength,
                    RequestBodyUrl = "/api/proxy/" + serverId + "/recording/" + recordIndex + "/request",
                    RequestHttpMethod = record.Request.HttpMethod,
                    RequestRawUrl = record.Request.RawUrl,
                    RequestDate = record.ReceivedDate,
                    ResponseBody = string.Empty,
                    ResponseBodyIsImage = record.Response.BodyType == HttpBodyClassification.Image,
                    ResponseBodyIsText = record.Response.BodyType == HttpBodyClassification.Text,
                    ResponseBodyLength = record.Response.BodyLength,
                    ResponseBodyUrl = "/api/proxy/" + serverId + "/recording/" + recordIndex + "/response",
                    ResponseStatusCode = record.Response.StatusCode,
                    ResponseStatusDescription = record.Response.StatusDescription
                };

                model.RequestBody = record.Request.BodyType == HttpBodyClassification.Text
                                         ? record.Request.GetBodyAsString()
                                         : string.Empty;

                model.ResponseBody = record.Response.BodyType == HttpBodyClassification.Text
                                          ? record.Response.GetBodyAsString()
                                          : string.Empty;

                model.RequestHeaders = GenerateHeaderModels(record.Request);
                model.ResponseHeaders = GenerateHeaderModels(record.Response);

                return Response.AsJson(model);
            };

            Get["/api/proxy/{serverId}/recording/{recordIndex}/request"] = _ =>
            {
                var serverId = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var environment = stumpsHost.FindServer(serverId);

                var record = environment.Recordings.FindAt(recordIndex);

                var ms = new System.IO.MemoryStream(record.Request.GetBody());

                return Response.FromStream(ms, record.Request.Headers["Content-Type"]);
            };

            Get["/api/proxy/{serverId}/recording/{recordIndex}/response"] = _ =>
            {
                var serverId = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var server = stumpsHost.FindServer(serverId);

                var record = server.Recordings.FindAt(recordIndex);

                var ms = new System.IO.MemoryStream(record.Response.GetBody());

                return Response.FromStream(ms, record.Response.Headers["Content-Type"]);
            };

            Get["/api/proxy/{serverId}/recording/status"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server = stumpsHost.FindServer(serverId);

                var model = new RecordStatusModel
                {
                    RecordTraffic = server.RecordTraffic
                };

                return Response.AsJson(model);
            };

            Put["/api/proxy/{serverId}/recording/status"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server = stumpsHost.FindServer(serverId);

                var model = this.Bind<RecordStatusModel>();

                if (model.RecordTraffic)
                {
                    server.Recordings.Clear();
                }

                server.RecordTraffic = model.RecordTraffic;

                return Response.AsJson(model);
            };
        }
示例#14
0
        public RecordingModule(IStumpsHost stumpsHost)
        {
            stumpsHost = stumpsHost ?? throw new ArgumentNullException(nameof(stumpsHost));

            Get["/api/proxy/{serverId}/recording"] = _ =>
            {
                var serverId    = (string)_.serverId;
                var environment = stumpsHost.FindServer(serverId);
                var afterIndex  = -1;

                if (Request.Query.after != null)
                {
                    var afterIndexString = (string)Request.Query.after;
                    afterIndex = int.TryParse(afterIndexString, out afterIndex) ? afterIndex : -1;
                }

                var recordingList = environment.Recordings.Find(afterIndex);
                var modelList     = new List <RecordingModel>();

                foreach (var recording in recordingList)
                {
                    afterIndex++;

                    var model = new RecordingModel
                    {
                        Index             = afterIndex,
                        Date              = recording.ReceivedDate,
                        Method            = recording.Request.HttpMethod,
                        RawUrl            = recording.Request.RawUrl,
                        RequestSize       = recording.Request.BodyLength,
                        ResponseSize      = recording.Response.BodyLength,
                        StatusCode        = recording.Response.StatusCode,
                        StatusDescription = recording.Response.StatusDescription
                    };

                    modelList.Add(model);
                }

                return(Response.AsJson(modelList));
            };

            Get["/api/proxy/{serverId}/recording/{recordIndex}"] = _ =>
            {
                var serverId    = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var server      = stumpsHost.FindServer(serverId);

                var record = server.Recordings.FindAt(recordIndex);

                var model = new RecordingDetailsModel
                {
                    Index                     = recordIndex,
                    RequestBody               = string.Empty,
                    RequestBodyIsImage        = record.Request.BodyType == HttpBodyClassification.Image,
                    RequestBodyIsText         = record.Request.BodyType == HttpBodyClassification.Text,
                    RequestBodyLength         = record.Request.BodyLength,
                    RequestBodyUrl            = "/api/proxy/" + serverId + "/recording/" + recordIndex + "/request",
                    RequestHttpMethod         = record.Request.HttpMethod,
                    RequestRawUrl             = record.Request.RawUrl,
                    RequestDate               = record.ReceivedDate,
                    ResponseBody              = string.Empty,
                    ResponseBodyIsImage       = record.Response.BodyType == HttpBodyClassification.Image,
                    ResponseBodyIsText        = record.Response.BodyType == HttpBodyClassification.Text,
                    ResponseBodyLength        = record.Response.BodyLength,
                    ResponseBodyUrl           = "/api/proxy/" + serverId + "/recording/" + recordIndex + "/response",
                    ResponseStatusCode        = record.Response.StatusCode,
                    ResponseStatusDescription = record.Response.StatusDescription
                };

                model.RequestBody = record.Request.BodyType == HttpBodyClassification.Text
                                         ? record.Request.GetBodyAsString()
                                         : string.Empty;

                model.ResponseBody = record.Response.BodyType == HttpBodyClassification.Text
                                          ? record.Response.GetBodyAsString()
                                          : string.Empty;

                model.RequestHeaders  = GenerateHeaderModels(record.Request);
                model.ResponseHeaders = GenerateHeaderModels(record.Response);

                return(Response.AsJson(model));
            };

            Get["/api/proxy/{serverId}/recording/{recordIndex}/request"] = _ =>
            {
                var serverId    = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var environment = stumpsHost.FindServer(serverId);

                var record = environment.Recordings.FindAt(recordIndex);

                var ms = new System.IO.MemoryStream(record.Request.GetBody());

                return(Response.FromStream(ms, record.Request.Headers["Content-Type"]));
            };

            Get["/api/proxy/{serverId}/recording/{recordIndex}/response"] = _ =>
            {
                var serverId    = (string)_.serverId;
                var recordIndex = (int)_.recordIndex;
                var server      = stumpsHost.FindServer(serverId);

                var record = server.Recordings.FindAt(recordIndex);

                var ms = new System.IO.MemoryStream(record.Response.GetBody());

                return(Response.FromStream(ms, record.Response.Headers["Content-Type"]));
            };

            Get["/api/proxy/{serverId}/recording/status"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server   = stumpsHost.FindServer(serverId);

                var model = new RecordStatusModel
                {
                    RecordTraffic = server.RecordTraffic
                };

                return(Response.AsJson(model));
            };

            Put["/api/proxy/{serverId}/recording/status"] = _ =>
            {
                var serverId = (string)_.serverId;
                var server   = stumpsHost.FindServer(serverId);

                var model = this.Bind <RecordStatusModel>();

                if (model.RecordTraffic)
                {
                    server.Recordings.Clear();
                }

                server.RecordTraffic = model.RecordTraffic;

                return(Response.AsJson(model));
            };
        }