2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2012 The CefSharp Authors. All rights reserved.
|
2015-08-03 10:41:29 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
using System;
|
2012-02-24 17:03:55 -08:00
|
|
|
using System.IO;
|
2018-09-10 02:41:13 +02:00
|
|
|
using System.Linq;
|
2015-06-24 17:00:37 +10:00
|
|
|
using System.Net;
|
2012-02-24 17:03:55 -08:00
|
|
|
using System.Text;
|
2015-04-28 08:30:11 +10:00
|
|
|
using System.Threading.Tasks;
|
2012-02-24 17:03:55 -08:00
|
|
|
|
|
|
|
|
namespace CefSharp.Example
|
|
|
|
|
{
|
2017-03-02 14:14:24 +10:00
|
|
|
internal class CefSharpSchemeHandler : ResourceHandler
|
2012-02-24 17:03:55 -08:00
|
|
|
{
|
2019-06-21 12:07:52 +10:00
|
|
|
public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback)
|
2012-02-24 17:03:55 -08:00
|
|
|
{
|
|
|
|
|
var uri = new Uri(request.Url);
|
2013-09-18 22:46:27 +03:00
|
|
|
var fileName = uri.AbsolutePath;
|
2012-02-24 17:03:55 -08:00
|
|
|
|
2017-03-02 14:14:24 +10:00
|
|
|
Task.Run(() =>
|
2015-09-25 11:40:51 +10:00
|
|
|
{
|
2017-03-02 14:14:24 +10:00
|
|
|
using (callback)
|
2015-09-25 11:40:51 +10:00
|
|
|
{
|
2017-03-02 14:14:24 +10:00
|
|
|
Stream stream = null;
|
2015-09-25 11:40:51 +10:00
|
|
|
|
2017-03-02 14:14:24 +10:00
|
|
|
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);
|
|
|
|
|
}
|
2016-02-11 15:33:14 +10:00
|
|
|
|
2017-03-02 14:14:24 +10:00
|
|
|
if (string.Equals(fileName, "/PostDataAjaxTest.html", StringComparison.OrdinalIgnoreCase))
|
2016-02-18 15:02:26 +10:00
|
|
|
{
|
2017-03-02 14:14:24 +10:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-16 19:40:11 +10:00
|
|
|
|
2018-09-10 02:41:13 +02:00
|
|
|
if (stream == null)
|
2017-03-02 14:14:24 +10:00
|
|
|
{
|
|
|
|
|
callback.Cancel();
|
|
|
|
|
}
|
|
|
|
|
else
|
2018-09-10 02:41:13 +02:00
|
|
|
{
|
2017-03-02 14:14:24 +10:00
|
|
|
//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;
|
2015-06-15 21:02:24 +10:00
|
|
|
|
2015-09-09 11:45:57 +10:00
|
|
|
callback.Continue();
|
|
|
|
|
}
|
2017-03-02 14:14:24 +10:00
|
|
|
}
|
|
|
|
|
});
|
2016-02-18 15:02:26 +10:00
|
|
|
|
2019-06-21 12:07:52 +10:00
|
|
|
return CefReturnValue.ContinueAsync;
|
2016-02-18 15:02:26 +10:00
|
|
|
}
|
2012-02-24 17:03:55 -08:00
|
|
|
}
|
|
|
|
|
}
|