-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathWinFormsInterceptRequestHandler.cs
More file actions
171 lines (153 loc) · 6.84 KB
/
WinFormsInterceptRequestHandler.cs
File metadata and controls
171 lines (153 loc) · 6.84 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
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
#region Copyright
// Copyright © 2026, TeamDev. All rights reserved.
//
// Redistribution and use in source and/or binary forms, with or without
// modification, must retain the above copyright notice and the following
// disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using DotNetBrowser.Handlers;
using DotNetBrowser.Net;
using DotNetBrowser.Net.Handlers;
namespace DotNetBrowser.WinForms.Demo
{
internal class WinFormsInterceptRequestHandler : IHandler<InterceptRequestParameters, InterceptRequestResponse>
{
private const string Domain = "http://internal.host/";
private const string PrefixTemplate = "{0}.Resources.";
private static readonly TraceSource Log = new TraceSource("DotNetBrowser.WinForms.Demo");
private readonly string prefix;
public WinFormsInterceptRequestHandler()
{
prefix = string.Format(PrefixTemplate, typeof(WinFormsInterceptRequestHandler).Namespace);
}
public InterceptRequestResponse Handle(InterceptRequestParameters parameters)
{
string url = parameters.UrlRequest.Url;
if (!url.StartsWith(Domain))
{
return InterceptRequestResponse.Proceed();
}
UrlRequestJob urlRequestJob;
try
{
string resourcePath = ConvertToResourcePath(url);
byte[] content = FindResource(resourcePath);
if (content != null)
{
MimeType mimeType = GetMimeType(resourcePath);
urlRequestJob = parameters.Network.CreateUrlRequestJob(parameters.UrlRequest,
new UrlRequestJobOptions
{
HttpStatusCode = HttpStatusCode.OK,
Headers = new List<HttpHeader>
{
new HttpHeader("Content-Type",
mimeType.Value)
}
});
urlRequestJob.Write(content);
}
else
{
Debug.WriteLine("Resource was not found.");
urlRequestJob = parameters.Network.CreateUrlRequestJob(parameters.UrlRequest,
new UrlRequestJobOptions
{
HttpStatusCode = HttpStatusCode.NotFound
});
}
urlRequestJob.Complete();
}
catch (Exception e)
{
Debug.WriteLine(e);
Log.TraceEvent(TraceEventType.Error, 1, e.ToString());
urlRequestJob = parameters.Network.CreateUrlRequestJob(parameters.UrlRequest,
new UrlRequestJobOptions
{
HttpStatusCode =
HttpStatusCode.InternalServerError
});
}
return InterceptRequestResponse.Intercept(urlRequestJob);
}
private string ConvertToResourcePath(string url)
{
string path = url.Replace(Domain, string.Empty);
if (string.IsNullOrWhiteSpace(path) || Equals(path, "/"))
{
path = "index.html";
}
string resourcePath = path.Replace("/", ".");
resourcePath = prefix + resourcePath;
Debug.WriteLine("URL: " + url);
Debug.WriteLine("Resource: " + resourcePath);
return resourcePath;
}
private byte[] FindResource(string url)
{
try
{
using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(url))
{
if (resourceStream == null)
{
return null;
}
using (MemoryStream ms = new MemoryStream())
{
resourceStream.CopyTo(ms);
return ms.ToArray();
}
}
}
catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}
private MimeType GetMimeType(string url)
{
string extension = Path.GetExtension(url);
if (extension.StartsWith("."))
{
extension = extension.Substring(1);
}
switch (extension.ToLower())
{
case "css":
return MimeType.TextCss;
case "htm":
case "html":
return MimeType.TextHtml;
case "ico":
return MimeType.Create("image/x-icon");
case "js":
return MimeType.TextJavascript;
case "json":
return MimeType.ApplicationJson;
default:
return MimeType.ApplicationOctetStream;
}
}
}
}