Skip to content

Commit c385f29

Browse files
committed
1.2.0.1 - Added support RedirectStandardInput for CMD (from request body)
1 parent ab352b5 commit c385f29

8 files changed

Lines changed: 55 additions & 26 deletions

File tree

sample/PSWebApi.OwinSample/Controllers/PSWebApiController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@ public HttpResponseMessage InvokeCMD(string script, JToken argumentsFromBody)
3333
{
3434
string physicalFullPath = script.LocalFullPath();
3535
CmdArgumentResolver argResolver = new CmdArgumentResolver(Path.GetExtension(physicalFullPath));
36-
string allArguments = argResolver.GatherInputArguments(this.Request, argumentsFromBody, ConfigHelper.CmdForceArgumentQuote);
36+
string redirectStandardInput = argumentsFromBody.DistinguishStandardInput();
37+
string allArguments = argResolver.GatherInputArguments(this.Request, (redirectStandardInput == null) ? argumentsFromBody : null, ConfigHelper.CmdForceArgumentQuote);
3738

38-
return this.InvokeCmd(physicalFullPath, allArguments, ConfigHelper.CmdTimeoutSeconds);
39+
return this.InvokeCmd(physicalFullPath, allArguments, redirectStandardInput, ConfigHelper.CmdTimeoutSeconds);
3940
}
4041

4142
[AcceptVerbs("GET", "POST", "PUT", "DELETE")]
4243
public Task<HttpResponseMessage> InvokeCMD_Async(string script, JToken argumentsFromBody, CancellationToken cancellationToken)
4344
{
4445
string physicalFullPath = script.LocalFullPath();
4546
CmdArgumentResolver argResolver = new CmdArgumentResolver(Path.GetExtension(physicalFullPath));
46-
string allArguments = argResolver.GatherInputArguments(this.Request, argumentsFromBody, ConfigHelper.CmdForceArgumentQuote);
47+
string redirectStandardInput = argumentsFromBody.DistinguishStandardInput();
48+
string allArguments = argResolver.GatherInputArguments(this.Request, (redirectStandardInput == null) ? argumentsFromBody : null, ConfigHelper.CmdForceArgumentQuote);
4749

48-
return this.InvokeCmdAsync(physicalFullPath, allArguments, cancellationToken);
50+
return this.InvokeCmdAsync(physicalFullPath, allArguments, redirectStandardInput, cancellationToken);
4951
}
5052
}
5153
}

sample/PSWebApi.OwinSample/PSWebApi.OwinSample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
<WarningLevel>4</WarningLevel>
4141
</PropertyGroup>
4242
<ItemGroup>
43-
<Reference Include="DataBooster.PSWebApi, Version=1.1.9.0, Culture=neutral, PublicKeyToken=ee1eb06d9feeb8dc, processorArchitecture=MSIL">
44-
<HintPath>..\..\packages\DataBooster.PSWebApi.1.1.9\lib\net45\DataBooster.PSWebApi.dll</HintPath>
43+
<Reference Include="DataBooster.PSWebApi, Version=1.2.0.1, Culture=neutral, PublicKeyToken=ee1eb06d9feeb8dc, processorArchitecture=MSIL">
44+
<HintPath>..\..\packages\DataBooster.PSWebApi.1.2.0.1\lib\net45\DataBooster.PSWebApi.dll</HintPath>
4545
<Private>True</Private>
4646
</Reference>
4747
<Reference Include="Microsoft.CSharp" />

sample/PSWebApi.OwinSample/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
//
3232
// You can specify all the values or you can default the Revision and Build Numbers
3333
// by using the '*' as shown below:
34-
[assembly: AssemblyVersion("1.1.9.0")]
35-
[assembly: AssemblyFileVersion("1.1.9.0")]
34+
[assembly: AssemblyVersion("1.2.0.1")]
35+
[assembly: AssemblyFileVersion("1.2.0.1")]

sample/PSWebApi.OwinSample/Startup.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,42 @@ public class Startup
1414

1515
static Startup()
1616
{
17-
HttpConfiguration config = new HttpConfiguration();
17+
HttpConfiguration httpConfig = new HttpConfiguration();
18+
PSConfiguration psConfig = httpConfig.RegisterPsWebApi();
1819

19-
PSMediaTypeFormatter psMediaTypeFormatter = new PSMediaTypeFormatter();
20-
config.Formatters.Insert(0, psMediaTypeFormatter);
20+
EnableCors(httpConfig);
21+
httpConfig.MapHttpAttributeRoutes();
2122

22-
EnableCors(config);
23-
config.MapHttpAttributeRoutes();
24-
25-
config.Routes.MapHttpRoute(
23+
httpConfig.Routes.MapHttpRoute(
2624
name: "PSWebApi",
2725
routeTemplate: "ps/{*script}",
2826
defaults: new { controller = "PSWebApi", action = "InvokePS_Async" },
2927
constraints: new { script = @".+\.ps1$" }
3028
);
3129

32-
config.Routes.MapHttpRoute(
30+
httpConfig.Routes.MapHttpRoute(
3331
name: "PSWebApi-Ext",
3432
routeTemplate: "ps.{ext}/{*script}",
3533
defaults: new { controller = "PSWebApi", action = "InvokePS_Async" },
36-
constraints: new { script = @".+\.ps1$", ext = psMediaTypeFormatter.Configuration.UriPathExtConstraint() }
34+
constraints: new { script = @".+\.ps1$", ext = psConfig.UriPathExtConstraint() }
3735
);
3836

39-
config.Routes.MapHttpRoute(
37+
httpConfig.Routes.MapHttpRoute(
4038
name: "CmdWebApi",
4139
routeTemplate: "cmd/{*script}",
4240
defaults: new { controller = "PSWebApi", action = "InvokeCMD_Async", ext = "string" },
4341
constraints: new { script = @".+\.(bat|exe)$" }
4442
);
4543

46-
config.Routes.MapHttpRoute(
44+
httpConfig.Routes.MapHttpRoute(
4745
name: "MiscApi",
4846
routeTemplate: "api/{controller}/{action}"
4947
);
5048

51-
//#if DEBUG
52-
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
53-
//#endif
54-
_httpServer = new HttpServer(config);
49+
//#if DEBUG
50+
httpConfig.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
51+
//#endif
52+
_httpServer = new HttpServer(httpConfig);
5553
}
5654

5755
public void Configuration(IAppBuilder app)

sample/PSWebApi.OwinSample/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="DataBooster.PSWebApi" version="1.1.9" targetFramework="net45" />
3+
<package id="DataBooster.PSWebApi" version="1.2.0.1" targetFramework="net45" />
44
<package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
66
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />

src/DataBooster.PSWebApi/PSControllerExtensions.async.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ private static Task<IList<PSObject>> InvokeAsync(this PowerShell ps, Cancellatio
133133
}
134134
}
135135

136+
/// <summary>
137+
/// Asynchronously invokes a Windows batch file or executable file by using a set of command-line arguments.
138+
/// </summary>
139+
/// <param name="apiController">The ApiController. This is an extension method to ApiController, when you use instance method syntax to call this method, omit this parameter.</param>
140+
/// <param name="scriptPath">The fully qualified location of an application file (batch file or executable file) to be executed.</param>
141+
/// <param name="arguments">Command-line arguments to pass when starting the process.</param>
142+
/// <param name="cancellationToken">The cancellation token can be used to request that the operation be abandoned before completing the execution. Exceptions will be reported via the returned Task object.</param>
143+
/// <returns>A task representing the asynchronous operation.</returns>
144+
public static Task<HttpResponseMessage> InvokeCmdAsync(this ApiController apiController, string scriptPath, string arguments, CancellationToken cancellationToken)
145+
{
146+
return InvokeCmdAsync(apiController, scriptPath, arguments, null, cancellationToken);
147+
}
148+
136149
internal static Task<TResult> AsCanceledTask<TResult>(this CancellationToken cancellationToken, TaskCompletionSource<TResult> taskCompletionSource = null)
137150
{
138151
if (!cancellationToken.IsCancellationRequested)

src/DataBooster.PSWebApi/PSControllerExtensions.cmd.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,18 @@ public static HttpResponseMessage InvokeCmd(this ApiController apiController, st
102102
}
103103
}
104104
}
105+
106+
/// <summary>
107+
/// Synchronously invokes a Windows batch file or executable file by using a set of command-line arguments.
108+
/// </summary>
109+
/// <param name="apiController">The ApiController. This is an extension method to ApiController, when you use instance method syntax to call this method, omit this parameter.</param>
110+
/// <param name="scriptPath">The fully qualified location of an application file (batch file or executable file) to be executed.</param>
111+
/// <param name="arguments">Command-line arguments to pass when starting the process.</param>
112+
/// <param name="timeoutSeconds">The time in seconds to wait for the command to execute before terminating the attempt to execute a command and generating an error.</param>
113+
/// <returns>A complete HttpResponseMessage contains the standard output (stdout) if the application runs successfully, Otherwise, the standard error (stderr).</returns>
114+
public static HttpResponseMessage InvokeCmd(this ApiController apiController, string scriptPath, string arguments, int timeoutSeconds)
115+
{
116+
return InvokeCmd(apiController, scriptPath, arguments, null, timeoutSeconds);
117+
}
105118
}
106119
}

src/DataBooster.PSWebApi/PSControllerExtensions.register.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ public static partial class PSControllerExtensions
1919
/// <param name="config">The <see cref="HttpConfiguration"/>. This is an extension method to HttpConfiguration, when you use instance method syntax to call this method, omit this parameter.</param>
2020
/// <param name="psConfiguration">The configuration for initializing a instance PSMediaTypeFormatter, which contains all currently supported PSConverterRegistry add-ins.</param>
2121
/// <param name="mediaTypes">A list of supported request Content-Types for redirecting StandardInput.</param>
22-
public static void RegisterPsWebApi(this HttpConfiguration config, PSConfiguration psConfiguration = null, IEnumerable<MediaTypeHeaderValue> mediaTypes = null)
22+
public static PSConfiguration RegisterPsWebApi(this HttpConfiguration config, PSConfiguration psConfiguration = null, IEnumerable<MediaTypeHeaderValue> mediaTypes = null)
2323
{
2424
if (config.Properties.ContainsKey(_RegisteredPropertyKey))
2525
throw new InvalidOperationException("Registered PSWebApi Repeatedly");
2626

27-
config.Formatters.Insert(0, new PSMediaTypeFormatter(psConfiguration));
27+
PSMediaTypeFormatter psMediaTypeFormatter = new PSMediaTypeFormatter(psConfiguration);
28+
config.Formatters.Insert(0, psMediaTypeFormatter);
2829
config.Formatters.Insert(1, new CmdMediaTypeFormatter(mediaTypes));
2930

3031
config.Properties.TryAdd(_RegisteredPropertyKey, true);
32+
33+
return psMediaTypeFormatter.Configuration;
3134
}
3235

3336
/// <summary>

0 commit comments

Comments
 (0)