-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageTracer.cs
191 lines (179 loc) · 6.66 KB
/
MessageTracer.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
namespace WebServiceStudio
{
using System;
using System.IO;
using System.Text;
using System.Xml;
internal class MessageTracer
{
public const string ApplicationXml = "application/soap+xml";
public const string SoapEnvUri = "http://schemas.xmlsoap.org/soap/envelope/";
public const string TextHtml = "text/html";
public const string TextPlain = "text/plain";
public const string TextXml = "text/xml";
private static void DumpBytes(Stream stream, int length, StringBuilder sb)
{
byte[] buffer = new byte[0x400];
int num = 0x10;
char[] chArray = new char[0x30];
char[] chArray2 = new char[0x10];
int num2 = 0;
int num3 = 0;
do
{
num3 = stream.Read(buffer, 0, min(length - num2, buffer.Length));
int index = 0;
while (index < num3)
{
if ((num2 % num) == 0)
{
for (int i = 0; i < num; i++)
{
char ch;
chArray2[i] = ch = ' ';
chArray[(3 * i) + 2] = ch;
chArray[3 * i] = chArray[(3 * i) + 1] = ch;
}
}
byte num6 = buffer[index];
int num7 = num2 % num;
chArray[3 * num7] = HexChar((num6 >> 4) & 15);
chArray[(3 * num7) + 1] = HexChar(num6 & 15);
chArray2[num7] = char.IsControl((char) num6) ? '.' : ((char) num6);
if (((num2 % num) == (num - 1)) || (index == (num3 - 1)))
{
sb.Append(string.Format("{0,8}: {1} {2}\n", (num2 / 0x10) * 0x10, new string(chArray), new string(chArray2)));
}
index++;
num2++;
}
}
while ((num2 < length) && (num3 > 0));
}
private static string GetCharset(string contentType)
{
int index = contentType.IndexOf(';');
if (index >= 0)
{
string strA = contentType.Substring(index + 1).TrimStart(null);
if (string.Compare(strA, 0, "charset", 0, "charset".Length, true) == 0)
{
string str2 = strA.Substring("charset".Length);
int num2 = str2.IndexOf('=');
if (num2 >= 0)
{
return str2.Substring(num2 + 1).Trim(new char[] { ' ', '\'', '"', '\t' });
}
}
}
return string.Empty;
}
private static Encoding GetEncoding(string contentType)
{
string charset = GetCharset(contentType);
Encoding encoding = null;
try
{
if (charset.Length > 0)
{
encoding = Encoding.GetEncoding(charset);
}
}
catch (Exception)
{
}
return ((encoding == null) ? new ASCIIEncoding() : encoding);
}
private static char HexChar(int nibble)
{
if (nibble < 10)
{
return (char) (nibble + 0x30);
}
return (char) (nibble + 0x37);
}
private static int min(int a, int b)
{
return ((a < b) ? a : b);
}
internal static string ReadMessage(Stream from, string contentType)
{
return ReadMessage(from, (int) from.Length, contentType);
}
internal static string ReadMessage(Stream from, int len, string contentType)
{
if ((contentType.StartsWith("text/xml") || contentType.StartsWith("application/soap+xml")) || (contentType == "http://schemas.xmlsoap.org/soap/envelope/"))
{
byte[] bytes = ReadStream(from, len);
XmlDocument document = new XmlDocument();
document.InnerXml = GetEncoding(contentType).GetString(bytes);
StringWriter w = new StringWriter();
XmlTextWriter writer2 = new XmlTextWriter(w);
writer2.Formatting = Formatting.Indented;
document.Save(writer2);
return w.ToString();
}
if (contentType.StartsWith("text"))
{
byte[] buffer = ReadStream(from, len);
from.Read(buffer, 0, len);
return GetEncoding(contentType).GetString(buffer, 0, len);
}
StringBuilder sb = new StringBuilder();
DumpBytes(from, len, sb);
return sb.ToString();
}
private static byte[] ReadStream(Stream stream, int len)
{
if (len >= 0)
{
byte[] buffer = new byte[len];
stream.Read(buffer, 0, len);
return buffer;
}
Chunk next = null;
Chunk chunk2 = null;
int num = 0;
while (true)
{
Chunk chunk3 = new Chunk();
if (next == null)
{
next = chunk3;
}
chunk3.Buffer = new byte[0x400];
chunk3.Size = stream.Read(chunk3.Buffer, 0, chunk3.Buffer.Length);
chunk3.Next = null;
if (chunk2 != null)
{
chunk2.Next = chunk3;
}
num += chunk3.Size;
if (chunk3.Size < chunk3.Buffer.Length)
{
break;
}
chunk2 = chunk3;
}
byte[] destinationArray = new byte[num];
while (next != null)
{
Array.Copy(next.Buffer, 0, destinationArray, 0, next.Size);
next = next.Next;
}
return destinationArray;
}
internal static int WriteMessage(Stream stream, string contentType, string str)
{
byte[] bytes = new UTF8Encoding(true).GetBytes(str);
stream.Write(bytes, 0, bytes.Length);
return bytes.Length;
}
private class Chunk
{
public byte[] Buffer;
public MessageTracer.Chunk Next;
public int Size;
}
}
}