File: XmlMtomDictionaryReader.cs

package info (click to toggle)
mono 2.6.7-5.1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 327,344 kB
  • ctags: 413,649
  • sloc: cs: 2,471,883; xml: 1,768,594; ansic: 350,665; sh: 13,644; makefile: 8,640; perl: 1,784; asm: 717; cpp: 209; python: 146; sql: 81; sed: 16
file content (630 lines) | stat: -rw-r--r-- 14,841 bytes parent folder | download | duplicates (5)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//
// XmlMtomDictionaryReader.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2009 Novell, Inc.  http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mime;
using System.Reflection;
using System.Text;
using System.Xml;

namespace System.Xml
{
	internal class XmlMtomDictionaryReader : XmlDictionaryReader
	{
		public XmlMtomDictionaryReader (
			Stream stream, Encoding encoding,
			XmlDictionaryReaderQuotas quotas)
		{
			this.stream = stream;
			this.encoding = encoding;
			this.quotas = quotas;

			Initialize ();
		}

		public XmlMtomDictionaryReader (
			Stream stream, Encoding [] encodings, string contentType,
			XmlDictionaryReaderQuotas quotas,
			int maxBufferSize,
			OnXmlDictionaryReaderClose onClose)
		{
			this.stream = stream;
			this.encodings = encodings;
			content_type = contentType != null ? CreateContentType (contentType) : null;
			this.quotas = quotas;
			this.max_buffer_size = maxBufferSize;
			on_close = onClose;

			Initialize ();
		}

		Stream stream;
		Encoding encoding;
		Encoding [] encodings;
		ContentType content_type;
		XmlDictionaryReaderQuotas quotas;
		int max_buffer_size;
		OnXmlDictionaryReaderClose on_close;

		Dictionary<string,MimeEncodedStream> readers = new Dictionary<string,MimeEncodedStream> ();

		void Initialize ()
		{
			var nt = new NameTable ();
			initial_reader = new NonInteractiveStateXmlReader (String.Empty, nt, ReadState.Initial);
			eof_reader = new NonInteractiveStateXmlReader (String.Empty, nt, ReadState.EndOfFile);
			xml_reader = initial_reader;
		}

		ContentType CreateContentType (string contentTypeString)
		{
			ContentType c = null;
			foreach (var s_ in contentTypeString.Split (';')) {
				var s = s_.Trim ();
				if (c == null) {
					// first one
					c = new ContentType (s);
					continue;
				}
				int idx = s.IndexOf ('=');
				if (idx < 0)
					throw new XmlException ("Invalid content type header");
				var val = StripBraces (s.Substring (idx + 1));
				c.Parameters [s.Substring (0, idx)] = val;
			}
			return c;
		}

		XmlReader xml_reader, initial_reader, eof_reader, part_reader;
		XmlReader Reader {
			get { return part_reader ?? xml_reader; }
		}

		public override bool EOF {
			get { return Reader == eof_reader; }
		}

		public override void Close ()
		{
			if (!EOF && on_close != null)
				on_close (this);
			xml_reader = eof_reader;
		}

		public override bool Read ()
		{
			if (EOF)
				return false;

			if (Reader == initial_reader)
				SetupPrimaryReader ();

			if (part_reader != null)
				part_reader = null;

			if (!Reader.Read ()) {
				xml_reader = eof_reader;
				return false;
			}
			if (Reader.LocalName == "Include" && Reader.NamespaceURI == "http://www.w3.org/2004/08/xop/include") {
				string cid = Reader.GetAttribute ("href");
				if (!cid.StartsWith ("cid:"))
					throw new XmlException ("Cannot resolve non-cid href attribute value in XOP Include element");
				cid = cid.Substring (4);
				if (!readers.ContainsKey (cid))
					ReadToIdentifiedStream (cid);
				part_reader = new MultiPartedXmlReader (Reader, readers [cid]);
			}
			return true;
		}

		void SetupPrimaryReader ()
		{
			ReadOptionalMimeHeaders ();
			if (current_content_type != null)
				content_type = current_content_type;

			if (content_type == null)
				throw new XmlException ("Content-Type header for the MTOM message was not found");
			if (content_type.Boundary == null)
				throw new XmlException ("Content-Type header for the MTOM message must contain 'boundary' parameter");

			if (encoding == null && content_type.CharSet != null)
				encoding = Encoding.GetEncoding (content_type.CharSet);
			if (encoding == null && encodings == null)
				throw new XmlException ("Encoding specification is required either in the constructor argument or the content-type header");

			// consume the first identifier.
			string ident = "--" + content_type.Boundary;
			string idline;

			while (true) {
				idline = ReadAsciiLine ().Trim ();
				if (idline == null)
					return;
				if (idline.Length != 0)
					break;
			}
			if (!idline.StartsWith (ident, StringComparison.Ordinal))
				throw new XmlException (String.Format ("Unexpected boundary line was found. Expected boundary is '{0}' but it was '{1}'", content_type.Boundary, idline));

			string start = content_type.Parameters ["start"];
			ReadToIdentifiedStream (start);

			xml_reader = XmlReader.Create (readers [start].CreateTextReader ());
		}

		int buffer_length;
		byte [] buffer;
		int peek_char;

		ContentType current_content_type;
		int content_index;
		string current_content_id, current_content_encoding;

		void ReadToIdentifiedStream (string id)
		{
			while (true) {
				if (!ReadNextStream ())
					throw new XmlException (String.Format ("The stream '{0}' did not appear", id));
				if (current_content_id == id || id == null)
					break;
			}
		}

		bool ReadNextStream ()
		{
			ReadOptionalMimeHeaders ();
			string ident = "--" + content_type.Boundary;

			StringBuilder sb = new StringBuilder ();
			while (true) {
				string n = ReadAsciiLine ();
				if (n == null && sb.Length == 0)
					return false;
				else if (n == null || n.StartsWith (ident, StringComparison.Ordinal))
					break;
				sb.Append (n);
			}
			readers.Add (current_content_id, new MimeEncodedStream (current_content_id, current_content_encoding, sb.ToString ()));
			return true;
		}

		void ReadOptionalMimeHeaders ()
		{
			peek_char = stream.ReadByte ();
			if (peek_char == '-') // no header
				return;
			ReadMimeHeaders ();
		}

		string ReadAllHeaderLines ()
		{
			string s = String.Empty;

			while (true) {
				var n = ReadAsciiLine ();
				if (n.Length == 0)
					return s;
				n = n.TrimEnd ();
				s += n;
				if (n [n.Length - 1] != ';')
					s += '\n';
			}
		}

		void ReadMimeHeaders ()
		{
			foreach (var s in ReadAllHeaderLines ().Split ('\n')) {
				if (s.Length == 0)
					continue;
				int idx = s.IndexOf (':');
				if (idx < 0)
					throw new XmlException (String.Format ("Unexpected header string: {0}", s));
				string v = StripBraces (s.Substring (idx + 1).Trim ());
				switch (s.Substring (0, idx).ToLower ()) {
				case "content-type":
					current_content_type = CreateContentType (v);
					break;
				case "content-id":
					current_content_id = v;
					break;
				case "content-transfer-encoding":
					current_content_encoding = v;
					break;
				}
			}
		}

		string StripBraces (string s)
		{
			// could be foo, <foo>, "foo" and "<foo>".
			if (s.Length >= 2 && s [0] == '"' && s [s.Length - 1] == '"')
				s = s.Substring (1, s.Length - 2);
			if (s.Length >= 2 && s [0] == '<' && s [s.Length - 1] == '>')
				s = s.Substring (1, s.Length - 2);
			return s;
		}

		string ReadAsciiLine ()
		{
			if (buffer == null)
				buffer = new byte [1024];
			int bpos = 0;
			int b = peek_char;
			bool skipRead = b >= 0;
			peek_char = -1;
			while (true) {
				if (skipRead)
					skipRead = false;
				else
					b = stream.ReadByte ();
				if (b < 0) {
					if (bpos > 0)
						throw new XmlException ("The stream ends without end of line");
					return null;
				}
				if (b == '\r') {
					b = stream.ReadByte ();
					if (b < 0) {
						buffer [bpos++] = (byte) '\r';
						break;
					}
					else if (b == '\n')
						break;
					buffer [bpos++] = (byte) '\r';
					skipRead = true;
				}
				else
					buffer [bpos++] = (byte) b;
				if (bpos == buffer.Length) {
					var newbuf = new byte [buffer.Length << 1];
					Array.Copy (buffer, 0, newbuf, 0, buffer.Length);
					buffer = newbuf;
				}
			}
			return Encoding.ASCII.GetString (buffer, 0, bpos);
		}

		// The rest are just reader delegation.

		public override int AttributeCount {
			get { return Reader.AttributeCount; }
		}

		public override string BaseURI {
			get { return Reader.BaseURI; }
		}

		public override int Depth {
			get { return Reader.Depth; }
		}

		public override bool HasValue {
			get { return Reader.HasValue; }
		}

		public override bool IsEmptyElement {
			get { return Reader.IsEmptyElement; }
		}

		public override string LocalName {
			get { return Reader.LocalName; }
		}

		public override string NamespaceURI {
			get { return Reader.NamespaceURI; }
		}

		public override XmlNameTable NameTable {
			get { return Reader.NameTable; }
		}

		public override XmlNodeType NodeType {
			get { return Reader.NodeType; }
		}

		public override string Prefix {
			get { return Reader.Prefix; }
		}

		public override ReadState ReadState {
			get { return Reader.ReadState; }
		}

		public override string Value {
			get { return Reader.Value; }
		}

		public override bool MoveToElement ()
		{
			return Reader.MoveToElement ();
		}

		public override string GetAttribute (int index)
		{
			return Reader.GetAttribute (index);
		}

		public override string GetAttribute (string name)
		{
			return Reader.GetAttribute (name);
		}

		public override string GetAttribute (string localName, string namespaceURI)
		{
			return Reader.GetAttribute (localName, namespaceURI);
		}

		public override void MoveToAttribute (int index)
		{
			Reader.MoveToAttribute (index);
		}

		public override bool MoveToAttribute (string name)
		{
			return Reader.MoveToAttribute (name);
		}

		public override bool MoveToAttribute (string localName, string namespaceURI)
		{
			return Reader.MoveToAttribute (localName, namespaceURI);
		}

		public override bool MoveToFirstAttribute ()
		{
			return Reader.MoveToFirstAttribute ();
		}

		public override bool MoveToNextAttribute ()
		{
			return Reader.MoveToNextAttribute ();
		}

		public override string LookupNamespace (string prefix)
		{
			return Reader.LookupNamespace (prefix);
		}

		public override bool ReadAttributeValue ()
		{
			return Reader.ReadAttributeValue ();
		}

		public override void ResolveEntity ()
		{
			Reader.ResolveEntity ();
		}
	}

	class NonInteractiveStateXmlReader : DummyStateXmlReader
	{
		public NonInteractiveStateXmlReader (string baseUri, XmlNameTable nameTable, ReadState readState)
			: base (baseUri, nameTable, readState)
		{
		}

		public override int Depth {
			get { return 0; }
		}

		public override bool HasValue {
			get { return false; }
		}

		public override string Value {
			get { return String.Empty; }
		}

		public override XmlNodeType NodeType {
			get { return XmlNodeType.None; }
		}
	}

	class MultiPartedXmlReader : DummyStateXmlReader
	{
		public MultiPartedXmlReader (XmlReader reader, MimeEncodedStream value)
			: base (reader.BaseURI, reader.NameTable, reader.ReadState)
		{
			this.owner = reader;
			this.value = value.CreateTextReader ().ReadToEnd ();
		}

		XmlReader owner;
		string value;

		public override int Depth {
			get { return owner.Depth; }
		}

		public override bool HasValue {
			get { return true; }
		}

		public override string Value {
			get { return value; }
		}

		public override XmlNodeType NodeType {
			get { return XmlNodeType.Text; }
		}
	}

	abstract class DummyStateXmlReader : XmlReader
	{
		protected DummyStateXmlReader (string baseUri, XmlNameTable nameTable, ReadState readState)
		{
			base_uri = baseUri;
			name_table = nameTable;
			read_state = readState;
		}

		string base_uri;
		XmlNameTable name_table;
		ReadState read_state;

		public override string BaseURI {
			get { return base_uri; }
		}

		public override bool EOF {
			get { return false; }
		}

		public override void Close ()
		{
			throw new NotSupportedException ();
		}

		public override bool Read ()
		{
			throw new NotSupportedException ();
		}

		// The rest are just reader delegation.

		public override int AttributeCount {
			get { return 0; }
		}

		public override bool IsEmptyElement {
			get { return false; }
		}

		public override string LocalName {
			get { return String.Empty; }
		}

		public override string NamespaceURI {
			get { return String.Empty; }
		}

		public override XmlNameTable NameTable {
			get { return name_table; }
		}

		public override string Prefix {
			get { return String.Empty; }
		}

		public override ReadState ReadState {
			get { return read_state; }
		}

		public override bool MoveToElement ()
		{
			return false;
		}

		public override string GetAttribute (int index)
		{
			return null;
		}

		public override string GetAttribute (string name)
		{
			return null;
		}

		public override string GetAttribute (string localName, string namespaceURI)
		{
			return null;
		}

		public override void MoveToAttribute (int index)
		{
			throw new ArgumentOutOfRangeException ();
		}

		public override bool MoveToAttribute (string name)
		{
			return false;
		}

		public override bool MoveToAttribute (string localName, string namespaceURI)
		{
			return false;
		}

		public override bool MoveToFirstAttribute ()
		{
			return false;
		}

		public override bool MoveToNextAttribute ()
		{
			return false;
		}

		public override string LookupNamespace (string prefix)
		{
			return null;
		}

		public override bool ReadAttributeValue ()
		{
			return false;
		}

		public override void ResolveEntity ()
		{
			throw new InvalidOperationException ();
		}
	}

	class MimeEncodedStream
	{
		public MimeEncodedStream (string id, string contentEncoding, string value)
		{
			Id = id;
			ContentEncoding = contentEncoding;
			EncodedString = value;
		}

		public string Id { get; set; }
		public string ContentEncoding { get; set; }
		public string EncodedString { get; set; }

		public string DecodedBase64String {
			get { return Convert.ToBase64String (Encoding.ASCII.GetBytes (EncodedString)); }
		}

		public TextReader CreateTextReader ()
		{
			switch (ContentEncoding) {
			case "7bit":
			case "8bit":
				return new StringReader (EncodedString);
			default:
				return new StringReader (DecodedBase64String);
			}
		}
	}
}