Skip to content
This repository was archived by the owner on Jan 15, 2023. It is now read-only.

Commit 119b181

Browse files
committed
Fixing issue #2.
1 parent 957719e commit 119b181

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/Chromely.CefSharp/Browser/Handlers/DefaultAssemblyResourceSchemeHandler.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Net;
7+
using System.Text;
78
using System.Text.RegularExpressions;
89
using System.Threading.Tasks;
910
using CefSharp;
@@ -56,17 +57,15 @@ public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback c
5657
// Check if file exists
5758
if (!fileInfo.Exists)
5859
{
59-
StatusCode = (int)HttpStatusCode.NotFound;
60-
StatusText = STATUSTEXT_FILENOTFOUND;
60+
SetResponseInfoOnFailure((int)HttpStatusCode.NotFound, STATUSTEXT_FILENOTFOUND);
6161
callback.Continue();
6262

6363
Logger.Instance.Log.LogWarning($"File: {file}: {StatusText}");
6464
}
6565
// Check if file exists but empty
6666
else if (fileInfo.Length == 0)
6767
{
68-
StatusCode = (int)HttpStatusCode.BadRequest;
69-
StatusText = STATUSTEXT_ZEROFILESIZE;
68+
SetResponseInfoOnFailure((int)HttpStatusCode.BadRequest, STATUSTEXT_ZEROFILESIZE);
7069
callback.Continue();
7170

7271
Logger.Instance.Log.LogWarning($"File: {file}: {StatusText}");
@@ -89,9 +88,7 @@ public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback c
8988
}
9089
catch (Exception exception)
9190
{
92-
_stream = null;
93-
StatusCode = (int)HttpStatusCode.BadRequest;
94-
StatusText = STATUSTEXT_BADREQUEST;
91+
SetResponseInfoOnFailure((int)HttpStatusCode.BadRequest, STATUSTEXT_BADREQUEST);
9592
Logger.Instance.Log.LogError(exception, exception.Message);
9693
}
9794

@@ -163,5 +160,12 @@ protected void SetResponseInfoOnSuccess()
163160
StatusText = STATUSTEXT_OK;
164161
Stream = _stream;
165162
}
163+
164+
protected void SetResponseInfoOnFailure(int status, string statusText)
165+
{
166+
_stream = GetMemoryStream(statusText, Encoding.UTF8);
167+
StatusCode = status;
168+
StatusText = statusText;
169+
}
166170
}
167171
}

src/Chromely.CefSharp/Browser/Handlers/DefaultResourceSchemeHandler.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.IO;
66
using System.Net;
7+
using System.Text;
78
using System.Threading.Tasks;
89
using CefSharp;
910
using Chromely.Core.Logging;
@@ -49,17 +50,15 @@ public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback c
4950
// Check if file exists
5051
if (!fileInfo.Exists)
5152
{
52-
StatusCode = (int)HttpStatusCode.NotFound;
53-
StatusText = STATUSTEXT_FILENOTFOUND;
53+
SetResponseInfoOnFailure((int)HttpStatusCode.NotFound, STATUSTEXT_FILENOTFOUND);
5454
callback.Continue();
5555

5656
Logger.Instance.Log.LogWarning($"File: {file}: {StatusText}");
5757
}
5858
// Check if file exists but empty
5959
else if (fileInfo.Length == 0)
6060
{
61-
StatusCode = (int)HttpStatusCode.BadRequest;
62-
StatusText = STATUSTEXT_ZEROFILESIZE;
61+
SetResponseInfoOnFailure((int)HttpStatusCode.BadRequest, STATUSTEXT_ZEROFILESIZE);
6362
callback.Continue();
6463

6564
Logger.Instance.Log.LogWarning($"File: {file}: {StatusText}");
@@ -83,9 +82,7 @@ public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback c
8382
}
8483
catch (Exception exception)
8584
{
86-
_stream = null;
87-
StatusCode = (int)HttpStatusCode.BadRequest;
88-
StatusText = STATUSTEXT_BADREQUEST;
85+
SetResponseInfoOnFailure((int)HttpStatusCode.BadRequest, STATUSTEXT_BADREQUEST);
8986
Logger.Instance.Log.LogError(exception, exception.Message);
9087
}
9188

@@ -116,5 +113,12 @@ protected virtual void SetResponseInfoOnSuccess()
116113
StatusText = STATUSTEXT_OK;
117114
Stream = _stream;
118115
}
116+
117+
protected void SetResponseInfoOnFailure(int status, string statusText)
118+
{
119+
_stream = GetMemoryStream(statusText, Encoding.UTF8);
120+
StatusCode = status;
121+
StatusText = statusText;
122+
}
119123
}
120124
}

0 commit comments

Comments
 (0)