1 | using System;
|
---|
2 | using System.Net;
|
---|
3 | using System.IO;
|
---|
4 | using System.Web;
|
---|
5 | using System.Text;
|
---|
6 | using System.Text.RegularExpressions;
|
---|
7 |
|
---|
8 | namespace SharpVectors.Net
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Summary description for DataWebResponse.
|
---|
12 | /// </summary>
|
---|
13 | /// <remarks>According to http://www.ietf.org/rfc/rfc2397.txt</remarks>
|
---|
14 | public class DataWebResponse : WebResponse
|
---|
15 | {
|
---|
16 | private byte[] decodedData;
|
---|
17 |
|
---|
18 | private static Regex re = new Regex(@"^data:(?<mediatype>.*?),(?<data>.*)$", RegexOptions.Singleline);
|
---|
19 | private static Regex wsRemover = new Regex(@"\s", RegexOptions.Singleline);
|
---|
20 | private static Regex charsetFinder = new Regex(@"charset=(?<charset>[^;]+)", RegexOptions.Singleline);
|
---|
21 |
|
---|
22 | internal DataWebResponse(Uri uri)
|
---|
23 | {
|
---|
24 | this.responseUri = uri;
|
---|
25 |
|
---|
26 | string fullUri = HttpUtility.UrlDecode(uri.AbsoluteUri);
|
---|
27 | fullUri = fullUri.Replace(' ', '+');
|
---|
28 |
|
---|
29 | // remove all whitespace
|
---|
30 | fullUri = contentType = wsRemover.Replace(fullUri, "");
|
---|
31 |
|
---|
32 | Match match = re.Match(fullUri);
|
---|
33 |
|
---|
34 | if(match.Success)
|
---|
35 | {
|
---|
36 | contentType = match.Groups["mediatype"].Value;
|
---|
37 |
|
---|
38 | string data = match.Groups["data"].Value;
|
---|
39 |
|
---|
40 |
|
---|
41 | if(contentType.Length == 0)
|
---|
42 | {
|
---|
43 | contentType = "text/plain;charset=US-ASCII";
|
---|
44 | }
|
---|
45 | else if (contentType.StartsWith(";"))
|
---|
46 | {
|
---|
47 | if (contentType.IndexOf(";charset=") > 0)
|
---|
48 | {
|
---|
49 | contentType = "text/plain" + contentType;
|
---|
50 | }
|
---|
51 | else
|
---|
52 | {
|
---|
53 | throw new Exception("Malformed data URI");
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | if(contentType.EndsWith(";base64"))
|
---|
58 | {
|
---|
59 | contentType = contentType.Remove(contentType.Length - 7, 7);
|
---|
60 | decodedData = Convert.FromBase64String(data);
|
---|
61 | }
|
---|
62 | else
|
---|
63 | {
|
---|
64 | Match charsetMatch = charsetFinder.Match(contentType);
|
---|
65 | if(charsetMatch.Success && charsetMatch.Groups["charset"].Success)
|
---|
66 | {
|
---|
67 | try
|
---|
68 | {
|
---|
69 | contentEncoding = Encoding.GetEncoding(charsetMatch.Groups["charset"].Value);
|
---|
70 | }
|
---|
71 | catch(NotSupportedException)
|
---|
72 | {
|
---|
73 | contentEncoding = Encoding.ASCII;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | decodedData = HttpUtility.UrlDecodeToBytes(data);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | else
|
---|
81 | {
|
---|
82 | throw new Exception("Malformed data URI");
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override long ContentLength
|
---|
87 | {
|
---|
88 | get
|
---|
89 | {
|
---|
90 | return decodedData.Length;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | private Encoding contentEncoding = Encoding.ASCII;
|
---|
95 | public Encoding ContentEncoding
|
---|
96 | {
|
---|
97 | get
|
---|
98 | {
|
---|
99 | return contentEncoding;
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | private string contentType;
|
---|
104 | public override string ContentType
|
---|
105 | {
|
---|
106 | get
|
---|
107 | {
|
---|
108 | return contentType;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | private Uri responseUri;
|
---|
113 | public override Uri ResponseUri
|
---|
114 | {
|
---|
115 | get
|
---|
116 | {
|
---|
117 | return responseUri;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | public override Stream GetResponseStream()
|
---|
122 | {
|
---|
123 | MemoryStream ms;
|
---|
124 | ms = new MemoryStream(decodedData, false);
|
---|
125 | ms.Position = 0;
|
---|
126 | return ms;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | } |
---|