You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-18Lines changed: 29 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,25 +8,25 @@ PSWebApi is a simple library for building ASP.NET Web APIs (RESTful Services) by
8
8
Similar to the sister repository [DbWebApi](https://github.com/DataBooster/DbWebApi), any managed http client can invoke PowerShell scripts, batch files and executables through the PS-WebApi as the following,
*(captures the StdOut and returns it as plain text)*
31
31
32
32
Often times, in some intranet applications, some functional requirements can be quickly implemented by succinct PowerShell scripts or command line scripts. However, for some integratability reason, this practice was out of sight from most existing projects.
@@ -66,8 +66,8 @@ The only one configuration item that must be customized is the **ScriptRoot** in
66
66
## HTTP Client
67
67
#### Request-URI
68
68
69
-
If there is a PowerShell script "D:\scripts-root\Dept1\ps\test\demo1.ps1", the HTTP client should call it by URL like `http://localhost:1608/ps.json`/__Dept1/ps/test/demo1.ps1__?p1=1&=arg....
70
-
If there is a batch file "D:\scripts-root\Dept1\bat\test\demo2.bat", the HTTP client should call it by URL like `http://localhost:1608/cmd`/__Dept1/bat/test/demo2.bat__?=1&=arg....
69
+
If there is a PowerShell script "D:\scripts-root\Dept1\ps\test\demo1.ps1", the HTTP client should call it by URL like `http://base-uri/ps.json`/__Dept1/ps/test/demo1.ps1__?p1=1&=arg....
70
+
If there is a batch file "D:\scripts-root\Dept1\bat\test\demo2.bat", the HTTP client should call it by URL like `http://base-uri/cmd`/__Dept1/bat/test/demo2.bat__?=1&=arg....
71
71
Calling executable file follows the same pattern as batch file.
72
72
73
73
#### Response MediaType
@@ -226,21 +226,32 @@ When there is only one item (single value) in the array, the single JSON value c
226
226
test-args.bat "single value argument"
227
227
```
228
228
229
-
- ***Escaping***
230
-
Due to the particularity of Windows command line argument parsing, PSWebApi uses the following rules by default to determine whether or not a string value must be surrounded by extra double quotation marks:
231
-
- A string will remain unchanged if the command-line parser (CMD.EXE) can interpret it as a single argument;
229
+
- ***Escaping and Quoting***
230
+
Since there is no unified standard to escape and quote a Windows command line argument, it needs to be fully controlled by your own customized PSWebApi service *(please see a reference implementation [CmdArgumentResolver.cs](https://github.com/DataBooster/PS-WebApi/blob/master/sample/PSWebApi.OwinSample/CmdArgumentResolver.cs) in the sample project)*. The PSWebApi library only offers two common methods for escaping and quoting:
231
+
- CmdArgumentsBuilder.**QuoteExeArgument**
232
+
For most Microsoft C/C++/C# console applications (follow the rules described in https://msdn.microsoft.com/en-us/library/17w5ykft.aspx or https://msdn.microsoft.com/en-us/library/a1y7w461.aspx):
233
+
1. A string will remain unchanged if it can be interpreted (by [CommandLineToArgvW](https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391.aspx)) as a single argument;
232
234
For examples,
233
235
`3.14`
234
236
`A_string_without_white_space`
235
237
`"A string already surrounded by double quotation marks"`
236
238
`/name_without_white_space:"value with spaces"`
237
-
- Otherwise, if a string would be interpreted as multiple broken arguments or a string contains any unclosed double-quotation mark *(non-literal)*, then the original string will be surrounded by an extra pair of double-quotation marks at the outermost layer, and all the originally nested quotes will be escaped using the `\"` as **literal** double-quotation marks.
239
+
2. Otherwise, if a string would be interpreted as multiple broken arguments or a string contains any unclosed double-quotation mark *(non-literal)*, then the original string will be surrounded by an extra pair of double-quotation marks at the outermost layer, and all the originally nested quotes will be escaped using the `\"` as **literal** double-quotation marks.
238
240
For examples,
239
-
`She's 5'5" tall.`
240
-
will be encoded/escaped as
241
-
`"She's 5'5\" tall."`
241
+
`She's 5'5" tall.`
242
+
will be encoded/escaped as
243
+
`"She's 5'5\" tall."`
242
244
243
-
A test batch [test-args.bat](https://github.com/DataBooster/PS-WebApi/tree/master/sample/PSWebApi.OwinSample/scripts-root/bat-scripts) *(shipped with the sample project [PSWebApi.OwinSample](https://github.com/DataBooster/PS-WebApi/releases))* can be used to give it a try.
245
+
A test batch [test-args.exe](https://github.com/DataBooster/PS-WebApi/tree/master/sample/Test-Args) *(build release mode to `..\PSWebApi.OwinSample\scripts-root\exe-apps\`)* can be used to give it a try.
246
+
247
+
- CmdArgumentsBuilder.**QuoteBatArgument**
248
+
For batch files:
249
+
1. A string will remain unchanged if it can be interpreted (by Windows command-line parser *CMD.EXE*) as a single argument;
250
+
2. Otherwise, if a string would be interpreted as multiple broken arguments or a string contains any unclosed double-quotation mark (non-literal), then the original string will be surrounded by an extra pair of double-quotation marks at the outermost layer, and each originally nested double-quotation mark will be escaped as double double-quotation marks.
0 commit comments