-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWriteHttpResponseCommand.cs
81 lines (72 loc) · 2.51 KB
/
WriteHttpResponseCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace SimpleHttpListener
{
using System.Management.Automation;
using System.Net;
[Cmdlet(VerbsCommunications.Write,"Response",DefaultParameterSetName="Status")]
[OutputType(typeof(Request))]
public class WriteHttpResponseCommand : PSCmdlet
{
public WriteHttpResponseCommand()
{
this.Status = HttpStatusCode.OK;
this.ContentType = "text/html";
}
[Parameter(Mandatory = true, ValueFromPipeline = true)]
[ValidateNotNull()]
public Request Request { get; set; }
[Parameter(ParameterSetName="Status")]
[PSDefaultValue(Value = HttpStatusCode.OK)]
public HttpStatusCode Status { get; set; }
[Parameter(ParameterSetName = "Status")]
[PSDefaultValue(Value = "text/html")]
[AllowNull()]
public string ContentType { get; set; }
[Parameter(ParameterSetName = "Status")]
[AllowEmptyString()]
[AllowNull()]
public string Body { get; set; }
[Parameter(ParameterSetName = "Location",Mandatory = true)]
[ValidateNotNull()]
public string Location { get; set; }
[Parameter()]
public SwitchParameter Stop { get; set; }
[Parameter()]
public SwitchParameter PassThru { get; set; }
protected override void ProcessRecord()
{
switch(ParameterSetName)
{
case "Status":
Request.Response.Status = Status;
Request.Response.ContentType = ContentType;
Request.Response.Body = Body;
break;
case "Location":
Request.Response.Status = HttpStatusCode.Found;
Request.Response.Location = Location;
Request.Response.ContentType = null;
Request.Response.Body = null;
break;
}
WriteVerbose("Write-Response >> " + Request.Response);
Request.Response.Close();
Request.Close();
if (PassThru)
{
WriteObject(Request);
}
if (Stop)
{
Request.Listener.Close();
}
}
protected override void StopProcessing()
{
if (Request != null)
{
Request.Close();
}
base.StopProcessing();
}
}
}