/
NotFoundHttpHandler.cs
117 lines (104 loc) · 4.63 KB
/
NotFoundHttpHandler.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
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using ServiceStack.Host.AspNet;
using ServiceStack.Text;
using ServiceStack.Logging;
using ServiceStack.Web;
namespace ServiceStack.Host.Handlers
{
public class NotFoundHttpHandler : HttpAsyncTaskHandler
{
private static readonly ILog Log = LogManager.GetLogger(typeof(NotFoundHttpHandler));
public bool? IsIntegratedPipeline { get; set; }
public string WebHostPhysicalPath { get; set; }
public List<string> WebHostRootFileNames { get; set; }
public string WebHostUrl { get; set; }
public string DefaultRootFileName { get; set; }
public string DefaultHandler { get; set; }
public override void ProcessRequest(IRequest request, IResponse response, string operationName)
{
Log.ErrorFormat("{0} Request not found: {1}", request.UserHostAddress, request.RawUrl);
var text = new StringBuilder();
if (HostContext.DebugMode)
{
text.AppendLine("Handler for Request not found: \n\n")
.AppendLine("Request.HttpMethod: " + request.Verb)
.AppendLine("Request.PathInfo: " + request.PathInfo)
.AppendLine("Request.QueryString: " + request.QueryString)
.AppendLine("Request.RawUrl: " + request.RawUrl);
}
else
{
text.Append("404");
}
response.ContentType = "text/plain";
response.StatusCode = 404;
response.EndHttpHandlerRequest(skipClose: true, afterBody: r => r.Write(text.ToString()));
}
public override void ProcessRequest(HttpContextBase context)
{
var request = context.Request;
var response = context.Response;
var httpReq = context.ToRequest(GetType().GetOperationName());
if (!request.IsLocal)
{
ProcessRequestAsync(httpReq, httpReq.Response, null);
return;
}
Log.ErrorFormat("{0} Request not found: {1}", request.UserHostAddress, request.RawUrl);
var sb = new StringBuilder();
sb.AppendLine("Handler for Request not found: \n\n");
sb.AppendLine("Request.ApplicationPath: " + request.ApplicationPath);
sb.AppendLine("Request.CurrentExecutionFilePath: " + request.CurrentExecutionFilePath);
sb.AppendLine("Request.FilePath: " + request.FilePath);
sb.AppendLine("Request.HttpMethod: " + request.HttpMethod);
sb.AppendLine("Request.MapPath('~'): " + request.MapPath("~"));
sb.AppendLine("Request.Path: " + request.Path);
sb.AppendLine("Request.PathInfo: " + request.PathInfo);
sb.AppendLine("Request.ResolvedPathInfo: " + httpReq.PathInfo);
sb.AppendLine("Request.PhysicalPath: " + request.PhysicalPath);
sb.AppendLine("Request.PhysicalApplicationPath: " + request.PhysicalApplicationPath);
sb.AppendLine("Request.QueryString: " + request.QueryString);
sb.AppendLine("Request.RawUrl: " + request.RawUrl);
try
{
sb.AppendLine("Request.Url.AbsoluteUri: " + request.Url.AbsoluteUri);
sb.AppendLine("Request.Url.AbsolutePath: " + request.Url.AbsolutePath);
sb.AppendLine("Request.Url.Fragment: " + request.Url.Fragment);
sb.AppendLine("Request.Url.Host: " + request.Url.Host);
sb.AppendLine("Request.Url.LocalPath: " + request.Url.LocalPath);
sb.AppendLine("Request.Url.Port: " + request.Url.Port);
sb.AppendLine("Request.Url.Query: " + request.Url.Query);
sb.AppendLine("Request.Url.Scheme: " + request.Url.Scheme);
sb.AppendLine("Request.Url.Segments: " + request.Url.Segments);
}
catch (Exception ex)
{
sb.AppendLine("Request.Url ERROR: " + ex.Message);
}
if (IsIntegratedPipeline.HasValue)
sb.AppendLine("App.IsIntegratedPipeline: " + IsIntegratedPipeline);
if (!WebHostPhysicalPath.IsNullOrEmpty())
sb.AppendLine("App.WebHostPhysicalPath: " + WebHostPhysicalPath);
if (!WebHostRootFileNames.IsEmpty())
sb.AppendLine("App.WebHostRootFileNames: " + TypeSerializer.SerializeToString(WebHostRootFileNames));
if (!WebHostUrl.IsNullOrEmpty())
sb.AppendLine("App.ApplicationBaseUrl: " + WebHostUrl);
if (!DefaultRootFileName.IsNullOrEmpty())
sb.AppendLine("App.DefaultRootFileName: " + DefaultRootFileName);
if (!DefaultHandler.IsNullOrEmpty())
sb.AppendLine("App.DefaultHandler: " + DefaultHandler);
if (!HttpHandlerFactory.DebugLastHandlerArgs.IsNullOrEmpty())
sb.AppendLine("App.DebugLastHandlerArgs: " + HttpHandlerFactory.DebugLastHandlerArgs);
response.ContentType = "text/plain";
response.StatusCode = 404;
response.EndHttpHandlerRequest(skipClose:true, afterBody: r => r.Write(sb.ToString()));
}
public override bool IsReusable
{
get { return true; }
}
}
}