-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderer.cs
More file actions
105 lines (94 loc) · 3.11 KB
/
Renderer.cs
File metadata and controls
105 lines (94 loc) · 3.11 KB
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
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace Https
{
class Renderer
{
readonly StreamWriter _output;
readonly StreamWriter _info;
public Renderer(StreamWriter output, StreamWriter info)
{
_output = output;
_info = info;
}
public async Task WriteResponse(HttpResponseMessage response)
{
_info.Write("HTTP/");
_info.Write(response.Version);
_info.Write(" ");
_info.Write((int)response.StatusCode);
_info.Write(" ");
_info.WriteLine(response.ReasonPhrase);
WriteHeaders(response.Headers, response.Content.Headers);
await ResponseContentFormatter.As(response, _output);
}
public void WriteHeaders(HttpResponseHeaders responseHeaders, HttpContentHeaders contentHeaders)
{
var headers = responseHeaders.Concat(contentHeaders);
foreach (var header in headers)
{
foreach (var value in header.Value)
{
_info.Write(header.Key);
_info.Write(":");
_info.Write(" ");
_info.WriteLine(value);
}
}
}
public void WriteException(Exception ex)
{
var help = WriteException(ex, 0);
switch (help)
{
case ExceptionHelp.Timeout:
_info.WriteLine("Request failed to complete within timeout. Try increasing the timeout with the --timeout flag");
break;
case ExceptionHelp.IgnoreCertificate:
_info.WriteLine("Ensure you trust the server certificate or try using the --ignore-certificate flag");
break;
}
}
ExceptionHelp WriteException(Exception ex, int depth)
{
if (depth > 0)
{
_info.Write(new string('\t', depth));
}
_info.WriteLine(ex.Message);
var exceptionHelp = ExceptionHelp.None;
if (ex is TaskCanceledException || ex is OperationCanceledException)
{
return ExceptionHelp.Timeout;
}
else
{
switch (ex.Message)
{
case "The SSL connection could not be established, see inner exception.":
exceptionHelp = ExceptionHelp.IgnoreCertificate;
break;
}
}
if (ex.InnerException != null)
{
var otherHelp = WriteException(ex.InnerException, depth + 1);
if (otherHelp != ExceptionHelp.None)
{
return otherHelp;
}
}
return exceptionHelp;
}
enum ExceptionHelp
{
None = 0,
IgnoreCertificate = 1,
Timeout = 2
}
}
}