aboutsummaryrefslogtreecommitdiff
path: root/utils/ipn_sqlclr/XmlReaderSpy.cs
blob: f89ece9df09c7b9725836371b7d7fedd89ca83a1 (plain)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web.Services.Protocols;
using System.Xml;
using System.IO;

namespace ipn_sqlclr
{
	public class XmlReaderSpy : StreamReader
	{
		private readonly StringBuilder _sb = new StringBuilder();
		public XmlReaderSpy(Stream stream, Encoding encoding, bool p, int bufferSize) : base(stream, encoding, p, bufferSize)
		{
		}

		public override int Read(char[] buffer, int index, int count)
		{
			var ret = base.Read(buffer, index, count);
			if(ret > 0)
				_sb.Append(buffer, index, ret);
			return ret;
		}

		public override string ReadToEnd()
		{
			var ret = base.ReadToEnd();
			_sb.Append(ret);
			return ret;
		}

		public override string ReadLine()
		{
			var ret = base.ReadLine();
			_sb.Append(ret);
			return ret;
		}

		public override int ReadBlock(char[] buffer, int index, int count)
		{
			return Read(buffer, index, count);
		}

		public string Xml
		{
			get { return _sb.ToString().Replace("<?xml version='1.0' encoding='UTF-8'?>", ""); }
		}
	}

	public class XmlWriterSpy : XmlWriter
	{
		private readonly XmlWriter _base;
		private readonly XmlTextWriter _xtw;
		private readonly StringWriter _sw;

		/// <summary>
		/// Extracted XML.
		/// </summary>
		public string Xml
		{
			get
			{
				return (_sw != null) ? _sw.ToString() : string.Empty;
			}
		}

		public XmlWriterSpy(XmlWriter parent)
		{
			_base = parent;
			_sw = new StringWriter();
			_xtw = new XmlTextWriter(_sw);
		}

		#region Abstract properties and methods that must be implemented

		public override WriteState WriteState
		{
			get
			{
				return _base.WriteState;
			}
		}

		public override void Close()
		{
			_base.Close();
			_xtw.Close();
			_sw.Close();
		}

		public override void Flush()
		{
			_base.Flush();
			_xtw.Flush();
			_sw.Flush();
		}

		public override string LookupPrefix(string ns)
		{
			return _base.LookupPrefix(ns);
		}

		public override void WriteBase64(byte[] buffer, int index, int count)
		{
			_base.WriteBase64(buffer, index, count);
			_xtw.WriteBase64(buffer, index, count);
		}

		public override void WriteCData(string text)
		{
			_base.WriteCData(text);
			_xtw.WriteCData(text);
		}

		public override void WriteCharEntity(char ch)
		{
			_base.WriteCharEntity(ch);
			_xtw.WriteCharEntity(ch);
		}

		public override void WriteChars(char[] buffer, int index, int count)
		{
			_base.WriteChars(buffer, index, count);
			_xtw.WriteChars(buffer, index, count);
		}

		public override void WriteComment(string text)
		{
			_base.WriteComment(text);
			_xtw.WriteComment(text);
		}

		public override void WriteDocType(string name, string pubid, string sysid, string subset)
		{
			_base.WriteDocType(name, pubid, sysid, subset);
			_xtw.WriteDocType(name, pubid, sysid, subset);
		}

		public override void WriteEndAttribute()
		{
			_base.WriteEndAttribute();
			_xtw.WriteEndAttribute();
		}

		public override void WriteEndDocument()
		{
			_base.WriteEndDocument();
			_xtw.WriteEndDocument();
		}

		public override void WriteEndElement()
		{
			_base.WriteEndElement();
			_xtw.WriteEndElement();
		}

		public override void WriteEntityRef(string name)
		{
			_base.WriteEntityRef(name);
			_xtw.WriteEntityRef(name);
		}

		public override void WriteFullEndElement()
		{
			_base.WriteFullEndElement();
			_xtw.WriteFullEndElement();
		}

		public override void WriteProcessingInstruction(string name, string text)
		{
			_base.WriteProcessingInstruction(name, text);
			_xtw.WriteProcessingInstruction(name, text);
		}

		public override void WriteRaw(string data)
		{
			_base.WriteRaw(data);
			_xtw.WriteRaw(data);
		}

		public override void WriteRaw(char[] buffer, int index, int count)
		{
			_base.WriteRaw(buffer, index, count);
			_xtw.WriteRaw(buffer, index, count);
		}

		public override void WriteStartAttribute(string prefix, string localName, string ns)
		{
			_base.WriteStartAttribute(prefix, localName, ns);
			_xtw.WriteStartAttribute(prefix, localName, ns);
		}

		public override void WriteStartDocument(bool standalone)
		{
			_base.WriteStartDocument(standalone);
			_xtw.WriteStartDocument(standalone);
		}

		public override void WriteStartDocument()
		{
			_base.WriteStartDocument();
			_xtw.WriteStartDocument();
		}

		public override void WriteStartElement(string prefix, string localName, string ns)
		{
			_base.WriteStartElement(prefix, localName, ns);
			_xtw.WriteStartElement(prefix, localName, ns);
		}

		public override void WriteString(string text)
		{
			_base.WriteString(text);
			_xtw.WriteString(text);
		}

		public override void WriteSurrogateCharEntity(char lowChar, char highChar)
		{
			_base.WriteSurrogateCharEntity(lowChar, highChar);
			_xtw.WriteSurrogateCharEntity(lowChar, highChar);

		}

		public override void WriteWhitespace(string ws)
		{
			_base.WriteWhitespace(ws);
			_xtw.WriteWhitespace(ws);
		}

		#endregion
	}

	public class XmlReaderSpyService : SoapHttpClientProtocol
	{
		protected XmlReaderSpyService(X509Certificate clientCert, string url)
		{
			Url = url;
			ClientCertificates.Add(clientCert);
		}

		private XmlReaderSpy _xmlReaderSpy;
		private XmlWriterSpy _xmlWriterSpy;

		public string GetRequestXml()
		{
			if (_xmlWriterSpy != null)
				return _xmlWriterSpy.Xml;
			return string.Empty;
		}
		public string GetResponseXml()
		{
			if (_xmlReaderSpy != null)
			{
				return _xmlReaderSpy.Xml;
			}
			return string.Empty;
		}

		protected override XmlReader GetReaderForMessage(SoapClientMessage message, int bufferSize)
		{
			Encoding encoding = Encoding.UTF8;
			if (bufferSize < 0x200)
			{
				bufferSize = 0x200;
			}
			var reader = new XmlTextReader(_xmlReaderSpy = new XmlReaderSpy(message.Stream, encoding, true, bufferSize))
			{
				DtdProcessing = DtdProcessing.Prohibit,
				Normalization = true,
				XmlResolver = null
			};
			return reader;
		}

		protected override XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize)
		{
			_xmlWriterSpy = new XmlWriterSpy(base.GetWriterForMessage(message, bufferSize));
			return _xmlWriterSpy;
		}
	}
}