forked from cefsharp/CefSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CefSharpSchemeHandler.cs
69 lines (61 loc) · 2.66 KB
/
CefSharpSchemeHandler.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
// Copyright © 2012 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace CefSharp.Example
{
internal class CefSharpSchemeHandler : ResourceHandler
{
public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback)
{
var uri = new Uri(request.Url);
var fileName = uri.AbsolutePath;
Task.Run(() =>
{
using (callback)
{
Stream stream = null;
if (string.Equals(fileName, "/PostDataTest.html", StringComparison.OrdinalIgnoreCase))
{
var postDataElement = request.PostData.Elements.FirstOrDefault();
stream = ResourceHandler.GetMemoryStream("Post Data: " + (postDataElement == null ? "null" : postDataElement.GetBody()), Encoding.UTF8);
}
if (string.Equals(fileName, "/PostDataAjaxTest.html", StringComparison.OrdinalIgnoreCase))
{
var postData = request.PostData;
if (postData == null)
{
stream = ResourceHandler.GetMemoryStream("Post Data: null", Encoding.UTF8);
}
else
{
var postDataElement = postData.Elements.FirstOrDefault();
stream = ResourceHandler.GetMemoryStream("Post Data: " + (postDataElement == null ? "null" : postDataElement.GetBody()), Encoding.UTF8);
}
}
if (stream == null)
{
callback.Cancel();
}
else
{
//Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer
stream.Position = 0;
//Populate the response values - No longer need to implement GetResponseHeaders (unless you need to perform a redirect)
ResponseLength = stream.Length;
MimeType = "text/html";
StatusCode = (int)HttpStatusCode.OK;
Stream = stream;
callback.Continue();
}
}
});
return CefReturnValue.ContinueAsync;
}
}
}