-
Notifications
You must be signed in to change notification settings - Fork 3
/
CurlExeProcessStream.cs
106 lines (67 loc) · 2.94 KB
/
CurlExeProcessStream.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Gsemac.IO;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
namespace Gsemac.Net.Curl {
public class CurlExeProcessStream :
ProcessStream {
// Public members
public CurlExeProcessStream(string filename, IProcessStreamOptions options = null) :
base(filename, options ?? ProcessStreamOptions.Default) { }
public CurlExeProcessStream(string filename, string arguments, IProcessStreamOptions options = null) :
base(filename, arguments, options ?? ProcessStreamOptions.Default) { }
public CurlExeProcessStream(ProcessStartInfo startInfo, IProcessStreamOptions options = null) :
base(startInfo, options ?? ProcessStreamOptions.Default) { }
public override void Close() {
if (!closed) {
closed = true;
// Abort the process and wait for all reads to finish.
base.Close();
// Check if curl exited with an error, and throw an exception if so.
CurlException ex = GetException();
if (ex is object)
throw ex;
}
}
// Protected members
/// <summary>
/// Returns a <see cref="CurlException"/> if curl exited with error, or null if it exited without error.
/// </summary>
/// <returns>A <see cref="CurlException"/> if curl exited with error, or null if it exited without error.</returns>
protected CurlException GetException() {
if (!ProcessExited)
return null;
CurlException result = null;
try {
int exitCode = ExitCode;
if (exitCode == -1) {
result = new CurlException(exitCode);
}
else if (exitCode != 0) {
// Curl exited with error; attempt to read the error stream.
string stdErrOutput = string.Empty;
try {
using (StreamReader sr = new StreamReader(StandardError))
stdErrOutput = sr.ReadToEnd();
}
catch (Exception) {
// Stream manually disposed by user?
}
// Parse error message.
Match errorMessageMatch = Regex.Match(stdErrOutput, @"curl:\s*\((\d+)\)\s*(.+?)$", RegexOptions.Multiline);
if (errorMessageMatch.Success)
result = new CurlException(exitCode, errorMessageMatch.Groups[2].Value.Trim());
else
result = new CurlException(exitCode);
}
}
catch (Exception) {
// Stream manually disposed by user?
}
return result;
}
// Private members
private bool closed = false;
}
}