Skip to content

Commit 9887078

Browse files
committed
fix(web): Improved cache and HTTP connection management
- Removed hardcoded "Cache-Control" setting, restoring default configuration. - Re-enabled "keep-alive" functionality for HTTP connections. - Corrected "Date" and "Last-Modified" header timestamps. - Increased file read buffer for potential performance improvements.
1 parent 2307194 commit 9887078

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

MIG/Gateways/WebServiceGateway.cs

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private void Worker(object state)
249249
}
250250

251251
response.Headers.Set(HttpResponseHeader.Server, "MIG WebService Gateway");
252-
response.KeepAlive = false;
252+
//response.KeepAlive = false;
253253

254254
bool requestHasAuthorizationHeader = request.Headers["Authorization"] != null;
255255
string remoteAddress = request.RemoteEndPoint.Address.ToString();
@@ -292,11 +292,14 @@ private void Worker(object state)
292292
var user = OnUserAuthentication(new UserAuthenticationEventArgs(username));
293293
// Also `user.Password` must be encrypted using the `Digest.CreatePassword(..)` method,
294294
// otherwise authentication will fail
295-
password = Digest.CreatePassword(username, user.Realm, password);
296-
if (user != null && user.Name == username && user.Password == password)
295+
try
297296
{
298-
verified = true;
299-
}
297+
password = Digest.CreatePassword(username, user.Realm, password);
298+
if (user.Name == username && user.Password == password)
299+
{
300+
verified = true;
301+
}
302+
} catch { /* ignored */ }
300303
}
301304
else if (authorizationSchema == WebAuthenticationSchema.Digest)
302305
{
@@ -468,22 +471,18 @@ private void Worker(object state)
468471
else if (url.ToLower().EndsWith(".webm"))
469472
{
470473
response.ContentType = "video/webm";
471-
disableCacheControl = true;
472474
}
473475
else if (url.ToLower().EndsWith(".mp4"))
474476
{
475477
response.ContentType = "video/mp4";
476-
disableCacheControl = true;
477478
}
478479
else if (url.ToLower().EndsWith(".m3u8"))
479480
{
480481
response.ContentType = "application/x-mpegURL";
481-
disableCacheControl = true;
482482
}
483483
else if (url.ToLower().EndsWith(".ts"))
484484
{
485485
response.ContentType = "video/mp2t";
486-
disableCacheControl = true;
487486
}
488487
else if (url.ToLower().EndsWith(".appcache"))
489488
{
@@ -519,12 +518,11 @@ private void Worker(object state)
519518
{
520519
// TODO: !IMPORTANT! exclude from caching files that contains SSI tags!
521520
response.StatusCode = (int)HttpStatusCode.NotModified;
522-
//!!DISABLED!! - The following line was preventing browser to load file from cache
523-
//response.Headers.Set(HttpResponseHeader.Date, file.LastWriteTimeUtc.ToString().Replace(",", "."));
521+
response.Headers.Set(HttpResponseHeader.Date, file.LastWriteTimeUtc.ToString("R"));
524522
}
525523
else
526524
{
527-
response.Headers.Set(HttpResponseHeader.LastModified, file.LastWriteTimeUtc.ToString().Replace(",", "."));
525+
response.Headers.Set(HttpResponseHeader.LastModified, file.LastWriteTimeUtc.ToString("R"));
528526
if (disableCacheControl)
529527
{
530528
response.Headers.Set(HttpResponseHeader.CacheControl, "no-cache, no-store, must-revalidate");
@@ -569,7 +567,14 @@ private void Worker(object state)
569567
response.ContentLength64 = file.Length;
570568
using (var fileStream = file.OpenRead())
571569
{
572-
fileStream.CopyTo(response.OutputStream);
570+
try
571+
{
572+
fileStream.CopyTo(response.OutputStream);
573+
}
574+
catch (HttpListenerException)
575+
{
576+
// client closed connection
577+
}
573578
}
574579
}
575580
else
@@ -586,16 +591,24 @@ private void Worker(object state)
586591
response.StatusCode = (int)HttpStatusCode.PartialContent;
587592
response.ContentLength64 = contentLength;
588593
response.Headers.Set("Content-Range", $"bytes {start}-{end}/{file.Length}");
589-
byte[] buffer = new byte[8192]; // Buffer da 8KB
594+
byte[] buffer = new byte[65536];
590595
long bytesRemaining = contentLength;
591596
using (var fileStream = file.OpenRead())
592597
{
593598
fileStream.Seek(start, SeekOrigin.Begin);
594599
int bytesRead;
595600
while (bytesRemaining > 0 && (bytesRead = fileStream.Read(buffer, 0, (int)Math.Min(buffer.Length, bytesRemaining))) > 0)
596601
{
597-
response.OutputStream.Write(buffer, 0, bytesRead);
598-
bytesRemaining -= bytesRead;
602+
try
603+
{
604+
response.OutputStream.Write(buffer, 0, bytesRead);
605+
bytesRemaining -= bytesRead;
606+
}
607+
catch (HttpListenerException)
608+
{
609+
// client closed connection
610+
break;
611+
}
599612
}
600613
}
601614
}
@@ -665,9 +678,9 @@ private void Worker(object state)
665678
}
666679
try { response.Close(); } catch {
667680
// TODO: add logging
668-
}
669-
try { response.Abort(); } catch {
670-
// TODO: add logging
681+
try { response.Abort(); } catch {
682+
// TODO: add logging
683+
}
671684
}
672685
}
673686
}
@@ -794,7 +807,7 @@ private void SendResponseObject(HttpListenerContext context, object responseObje
794807
if (responseObject != null && responseObject.GetType().Equals(typeof(byte[])) == false)
795808
{
796809
string responseText = "";
797-
if (responseObject.GetType() == typeof(String))
810+
if (responseObject is string)
798811
{
799812
responseText = responseObject.ToString();
800813
}

0 commit comments

Comments
 (0)