Skip to content

Commit

Permalink
1.2.0.1 - Added support RedirectStandardInput for CMD (from request b…
Browse files Browse the repository at this point in the history
…ody)
  • Loading branch information
AbelCheng committed Sep 29, 2017
1 parent ab352b5 commit c385f29
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 26 deletions.
10 changes: 6 additions & 4 deletions sample/PSWebApi.OwinSample/Controllers/PSWebApiController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ public HttpResponseMessage InvokeCMD(string script, JToken argumentsFromBody)
{
string physicalFullPath = script.LocalFullPath();
CmdArgumentResolver argResolver = new CmdArgumentResolver(Path.GetExtension(physicalFullPath));
string allArguments = argResolver.GatherInputArguments(this.Request, argumentsFromBody, ConfigHelper.CmdForceArgumentQuote);
string redirectStandardInput = argumentsFromBody.DistinguishStandardInput();
string allArguments = argResolver.GatherInputArguments(this.Request, (redirectStandardInput == null) ? argumentsFromBody : null, ConfigHelper.CmdForceArgumentQuote);

return this.InvokeCmd(physicalFullPath, allArguments, ConfigHelper.CmdTimeoutSeconds);
return this.InvokeCmd(physicalFullPath, allArguments, redirectStandardInput, ConfigHelper.CmdTimeoutSeconds);
}

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

return this.InvokeCmdAsync(physicalFullPath, allArguments, cancellationToken);
return this.InvokeCmdAsync(physicalFullPath, allArguments, redirectStandardInput, cancellationToken);
}
}
}
4 changes: 2 additions & 2 deletions sample/PSWebApi.OwinSample/PSWebApi.OwinSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="DataBooster.PSWebApi, Version=1.1.9.0, Culture=neutral, PublicKeyToken=ee1eb06d9feeb8dc, processorArchitecture=MSIL">
<HintPath>..\..\packages\DataBooster.PSWebApi.1.1.9\lib\net45\DataBooster.PSWebApi.dll</HintPath>
<Reference Include="DataBooster.PSWebApi, Version=1.2.0.1, Culture=neutral, PublicKeyToken=ee1eb06d9feeb8dc, processorArchitecture=MSIL">
<HintPath>..\..\packages\DataBooster.PSWebApi.1.2.0.1\lib\net45\DataBooster.PSWebApi.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
Expand Down
4 changes: 2 additions & 2 deletions sample/PSWebApi.OwinSample/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.1.9.0")]
[assembly: AssemblyFileVersion("1.1.9.0")]
[assembly: AssemblyVersion("1.2.0.1")]
[assembly: AssemblyFileVersion("1.2.0.1")]
28 changes: 13 additions & 15 deletions sample/PSWebApi.OwinSample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,42 @@ public class Startup

static Startup()
{
HttpConfiguration config = new HttpConfiguration();
HttpConfiguration httpConfig = new HttpConfiguration();
PSConfiguration psConfig = httpConfig.RegisterPsWebApi();

PSMediaTypeFormatter psMediaTypeFormatter = new PSMediaTypeFormatter();
config.Formatters.Insert(0, psMediaTypeFormatter);
EnableCors(httpConfig);
httpConfig.MapHttpAttributeRoutes();

EnableCors(config);
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
httpConfig.Routes.MapHttpRoute(
name: "PSWebApi",
routeTemplate: "ps/{*script}",
defaults: new { controller = "PSWebApi", action = "InvokePS_Async" },
constraints: new { script = @".+\.ps1$" }
);

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

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

config.Routes.MapHttpRoute(
httpConfig.Routes.MapHttpRoute(
name: "MiscApi",
routeTemplate: "api/{controller}/{action}"
);

//#if DEBUG
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
//#endif
_httpServer = new HttpServer(config);
//#if DEBUG
httpConfig.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
//#endif
_httpServer = new HttpServer(httpConfig);
}

public void Configuration(IAppBuilder app)
Expand Down
2 changes: 1 addition & 1 deletion sample/PSWebApi.OwinSample/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DataBooster.PSWebApi" version="1.1.9" targetFramework="net45" />
<package id="DataBooster.PSWebApi" version="1.2.0.1" targetFramework="net45" />
<package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
Expand Down
13 changes: 13 additions & 0 deletions src/DataBooster.PSWebApi/PSControllerExtensions.async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ private static Task<IList<PSObject>> InvokeAsync(this PowerShell ps, Cancellatio
}
}

/// <summary>
/// Asynchronously invokes a Windows batch file or executable file by using a set of command-line arguments.
/// </summary>
/// <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>
/// <param name="scriptPath">The fully qualified location of an application file (batch file or executable file) to be executed.</param>
/// <param name="arguments">Command-line arguments to pass when starting the process.</param>
/// <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>
/// <returns>A task representing the asynchronous operation.</returns>
public static Task<HttpResponseMessage> InvokeCmdAsync(this ApiController apiController, string scriptPath, string arguments, CancellationToken cancellationToken)
{
return InvokeCmdAsync(apiController, scriptPath, arguments, null, cancellationToken);
}

internal static Task<TResult> AsCanceledTask<TResult>(this CancellationToken cancellationToken, TaskCompletionSource<TResult> taskCompletionSource = null)
{
if (!cancellationToken.IsCancellationRequested)
Expand Down
13 changes: 13 additions & 0 deletions src/DataBooster.PSWebApi/PSControllerExtensions.cmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,18 @@ public static HttpResponseMessage InvokeCmd(this ApiController apiController, st
}
}
}

/// <summary>
/// Synchronously invokes a Windows batch file or executable file by using a set of command-line arguments.
/// </summary>
/// <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>
/// <param name="scriptPath">The fully qualified location of an application file (batch file or executable file) to be executed.</param>
/// <param name="arguments">Command-line arguments to pass when starting the process.</param>
/// <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>
/// <returns>A complete HttpResponseMessage contains the standard output (stdout) if the application runs successfully, Otherwise, the standard error (stderr).</returns>
public static HttpResponseMessage InvokeCmd(this ApiController apiController, string scriptPath, string arguments, int timeoutSeconds)
{
return InvokeCmd(apiController, scriptPath, arguments, null, timeoutSeconds);
}
}
}
7 changes: 5 additions & 2 deletions src/DataBooster.PSWebApi/PSControllerExtensions.register.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ public static partial class PSControllerExtensions
/// <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>
/// <param name="psConfiguration">The configuration for initializing a instance PSMediaTypeFormatter, which contains all currently supported PSConverterRegistry add-ins.</param>
/// <param name="mediaTypes">A list of supported request Content-Types for redirecting StandardInput.</param>
public static void RegisterPsWebApi(this HttpConfiguration config, PSConfiguration psConfiguration = null, IEnumerable<MediaTypeHeaderValue> mediaTypes = null)
public static PSConfiguration RegisterPsWebApi(this HttpConfiguration config, PSConfiguration psConfiguration = null, IEnumerable<MediaTypeHeaderValue> mediaTypes = null)
{
if (config.Properties.ContainsKey(_RegisteredPropertyKey))
throw new InvalidOperationException("Registered PSWebApi Repeatedly");

config.Formatters.Insert(0, new PSMediaTypeFormatter(psConfiguration));
PSMediaTypeFormatter psMediaTypeFormatter = new PSMediaTypeFormatter(psConfiguration);
config.Formatters.Insert(0, psMediaTypeFormatter);
config.Formatters.Insert(1, new CmdMediaTypeFormatter(mediaTypes));

config.Properties.TryAdd(_RegisteredPropertyKey, true);

return psMediaTypeFormatter.Configuration;
}

/// <summary>
Expand Down

0 comments on commit c385f29

Please sign in to comment.