Skip to content

Commit f1af4f0

Browse files
committed
Code cleanup
1 parent b14c1bb commit f1af4f0

15 files changed

+87
-176
lines changed

src/OpenRasta.Hosting.AspNet.Tests.Integration/Properties/AssemblyInfo.cs

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#endregion
1010

1111
using System.Reflection;
12-
using System.Runtime.CompilerServices;
13-
using System.Runtime.InteropServices;
1412

1513
// General Information about an assembly is controlled through the following
1614
// set of attributes. Change these attribute values to modify the information

src/OpenRasta.Hosting.AspNet.Tests.Integration/aspnet_server_context.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
using System;
22
using System.Diagnostics;
3-
using System.IO;
43
using System.Net;
54
using System.Text;
65
using NUnit.Framework;
7-
using OpenRasta.Configuration;
86
using OpenRasta.Hosting.AspNet.AspNetHttpListener;
97
using OpenRasta.Web;
108

src/OpenRasta.Hosting.AspNet/AspNetCommunicationContext.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static AspNetCommunicationContext Current
3030
}
3131
}
3232

33-
public AspNetCommunicationContext(ILogger logger, HttpContext context, AspNetRequest request,
33+
AspNetCommunicationContext(ILogger logger, HttpContext context, AspNetRequest request,
3434
AspNetResponse response)
3535
{
3636
NativeContext = context;
@@ -47,29 +47,29 @@ public Uri ApplicationBaseUri
4747
if (NativeContext == null)
4848
return null;
4949

50-
string baseUri = "{0}://{1}/".With(NativeContext.Request.Url.Scheme,
50+
var baseUri = "{0}://{1}/".With(NativeContext.Request.Url.Scheme,
5151
NativeContext.Request.ServerVariables["HTTP_HOST"]);
5252

53+
// ReSharper disable once AssignNullToNotNullAttribute
5354
var appBaseUri = new Uri(new Uri(baseUri), new Uri(NativeContext.Request.ApplicationPath, UriKind.Relative));
5455
return appBaseUri;
5556
}
5657
}
5758

5859

59-
6060
public OperationResult OperationResult { get; set; }
61-
public PipelineData PipelineData { get; set; }
62-
public IRequest Request { get; private set; }
63-
public IResponse Response { get; private set; }
61+
public PipelineData PipelineData { get; }
62+
public IRequest Request { get; }
63+
public IResponse Response { get; }
6464

65-
public IList<Error> ServerErrors { get; private set; }
65+
public IList<Error> ServerErrors { get; }
6666

6767
public IPrincipal User
6868
{
69-
get { return NativeContext.User; }
70-
set { NativeContext.User = value; }
69+
get => NativeContext.User;
70+
set => NativeContext.User = value;
7171
}
7272

73-
HttpContext NativeContext { get; set; }
73+
HttpContext NativeContext { get; }
7474
}
7575
}

src/OpenRasta.Hosting.AspNet/AspNetHost.cs

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System;
22
using System.Linq;
33
using System.Reflection;
4-
using System.Threading;
54
using System.Threading.Tasks;
65
using System.Web;
76
using System.Web.Compilation;
87
using OpenRasta.Concordia;
98
using OpenRasta.Configuration;
10-
using OpenRasta.Configuration.MetaModel;
119
using OpenRasta.DI;
1210
using OpenRasta.Diagnostics;
1311
using OpenRasta.Pipeline;
@@ -23,18 +21,20 @@ public class AspNetHost : IHost, IHostStartWithStartupProperties
2321

2422
event EventHandler _legacyStart;
2523
event EventHandler<StartupProperties> _start;
24+
2625
event EventHandler IHost.Start
2726
{
2827
add => _legacyStart += value;
2928
remove => _legacyStart -= value;
3029
}
30+
3131
event EventHandler<StartupProperties> IHostStartWithStartupProperties.Start
3232
{
3333
add => _start += value;
3434
remove => _start -= value;
3535
}
3636

37-
protected internal virtual void RaiseStart()
37+
protected internal void RaiseStart()
3838
{
3939
_legacyStart.Raise(this);
4040
var start = _start;
@@ -45,9 +45,7 @@ protected internal virtual void RaiseStart()
4545

4646
public string ApplicationVirtualPath => HttpRuntime.AppDomainAppVirtualPath;
4747

48-
IConfigurationSource _configurationSource;
49-
50-
Lazy<IConfigurationSource> _configSourceFactory;
48+
readonly Lazy<IConfigurationSource> _configSourceFactory;
5149
readonly Lazy<IDependencyResolverAccessor> _resolverAccessor;
5250

5351

@@ -58,24 +56,23 @@ public AspNetHost(StartupProperties properties)
5856
_configSourceFactory = new Lazy<IConfigurationSource>(ConfigurationSourceLocator);
5957
}
6058

61-
public IConfigurationSource ConfigurationSource
62-
{
63-
get { return _configurationSource ?? _configSourceFactory.Value; }
64-
set { _configurationSource = value; }
65-
}
59+
IConfigurationSource ConfigurationSource => _configSourceFactory.Value;
6660

6761
public IDependencyResolverAccessor ResolverAccessor => _resolverAccessor.Value;
6862

6963
IDependencyResolverAccessor CreateResolverAccessor()
7064
{
65+
// ReSharper disable once SuspiciousTypeConversion.Global - Declared API without test
7166
return (ConfigurationSource as IDependencyResolverAccessor)
7267
?? DependencyResolverAccessorLocator();
7368
}
7469

7570
public static Func<IConfigurationSource> ConfigurationSourceLocator = FindTypeInProject<IConfigurationSource>;
76-
public static Func<IDependencyResolverAccessor> DependencyResolverAccessorLocator = FindTypeInProject<IDependencyResolverAccessor>;
7771

78-
public static T FindTypeInProject<T>() where T : class
72+
static readonly Func<IDependencyResolverAccessor> DependencyResolverAccessorLocator =
73+
FindTypeInProject<IDependencyResolverAccessor>;
74+
75+
static T FindTypeInProject<T>() where T : class
7976
{
8077
// forces global.asax to be compiled.
8178
BuildManager.GetReferencedAssemblies();
@@ -95,6 +92,7 @@ public static T FindTypeInProject<T>() where T : class
9592
}
9693
catch
9794
{
95+
// ignored - if none found, returns null on purpose
9896
}
9997
}
10098
return null;
@@ -130,7 +128,7 @@ static Assembly ResolveToAlreadyLoaded(object sender, ResolveEventArgs args)
130128
return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(_ => _.FullName == args.Name);
131129
}
132130

133-
public static bool NotFrameworkAssembly(Assembly assembly)
131+
static bool NotFrameworkAssembly(Assembly assembly)
134132
{
135133
switch (assembly.GetName().Name)
136134
{
@@ -153,8 +151,6 @@ public bool ConfigureLeafDependencies(IDependencyResolver resolver)
153151
{
154152
if (ConfigurationSource != null)
155153
resolver.AddDependencyInstance<IConfigurationSource>(ConfigurationSource);
156-
var uris = resolver.Resolve<IUriResolver>();
157-
var config = resolver.Resolve<IMetaModelRepository>();
158154
return true;
159155
}
160156

@@ -171,18 +167,16 @@ protected internal void RaiseIncomingRequestProcessed(ICommunicationContext cont
171167
IncomingRequestProcessed.Raise(this, new IncomingRequestProcessedEventArgs(context));
172168
}
173169

174-
protected internal virtual Task RaiseIncomingRequestReceived(ICommunicationContext context)
170+
protected internal Task RaiseIncomingRequestReceived(ICommunicationContext context)
175171
{
176172
var incomingRequestReceivedEventArgs = new IncomingRequestReceivedEventArgs(context);
177173
IncomingRequestReceived.Raise(this, incomingRequestReceivedEventArgs);
178174
return incomingRequestReceivedEventArgs.RunTask;
179175
}
180176

181-
182-
protected internal virtual void RaiseStop()
177+
protected internal void RaiseStop()
183178
{
184179
Stop.Raise(this);
185180
}
186-
187181
}
188182
}

src/OpenRasta.Hosting.AspNet/AspNetHttpListener/HttpListenerWorkerRequest.cs

+19-34
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Diagnostics;
44
using System.Globalization;
55
using System.IO;
6-
using System.Linq;
76
using System.Net;
87
using System.Web;
98

@@ -18,22 +17,19 @@ public class HttpListenerWorkerRequest : HttpWorkerRequest
1817
public HttpListenerWorkerRequest(
1918
HttpListenerContext context, string vdir, string pdir)
2019
{
21-
if (null == context)
22-
throw new ArgumentNullException(nameof(context));
2320
if (null == vdir || vdir.Equals(string.Empty))
2421
throw new ArgumentException("vdir");
2522
if (null == pdir || pdir.Equals(string.Empty))
2623
throw new ArgumentException("pdir");
2724

28-
_context = context;
25+
_context = context ?? throw new ArgumentNullException(nameof(context));
2926
_virtualDir = vdir;
3027
_physicalDir = pdir;
3128
_context.Response.SendChunked = false;
3229
}
3330

3431
public override void CloseConnection()
3532
{
36-
// _context.Close();
3733
}
3834

3935
public override void EndOfRequest()
@@ -58,19 +54,12 @@ public override string GetAppPathTranslated()
5854

5955
public override string GetFilePath()
6056
{
61-
// TODO: this is a hack
62-
string s = _context.Request.Url.LocalPath;
63-
if (s.IndexOf(".aspx") != -1)
64-
s = s.Substring(0, s.IndexOf(".aspx") + 5);
65-
else if (s.IndexOf(".asmx") != -1)
66-
s = s.Substring(0, s.IndexOf(".asmx") + 5);
67-
68-
return s;
57+
return _context.Request.Url.LocalPath;
6958
}
7059

7160
public override string GetFilePathTranslated()
7261
{
73-
string s = GetFilePath();
62+
var s = GetFilePath();
7463
s = s.Substring(_virtualDir.Length);
7564
s = s.Replace('/', Path.DirectorySeparatorChar);
7665
return Path.Combine(_physicalDir, s);
@@ -83,9 +72,7 @@ public override string GetHttpVerbName()
8372

8473
public override string GetHttpVersion()
8574
{
86-
return string.Format("HTTP/{0}.{1}",
87-
_context.Request.ProtocolVersion.Major,
88-
_context.Request.ProtocolVersion.Minor);
75+
return $"HTTP/{_context.Request.ProtocolVersion.Major}.{_context.Request.ProtocolVersion.Minor}";
8976
}
9077

9178
public override string MapPath(string virtualPath)
@@ -111,28 +98,30 @@ public override string GetKnownRequestHeader(int index)
11198

11299
public override string GetLocalAddress()
113100
{
114-
return _context.Request.LocalEndPoint.Address.ToString();
101+
// ReSharper disable once PossibleNullReferenceException
102+
return _context.Request.LocalEndPoint?.Address.ToString();
115103
}
116104

117105
public override int GetLocalPort()
118106
{
107+
// ReSharper disable once PossibleNullReferenceException
119108
return _context.Request.LocalEndPoint.Port;
120109
}
121110

122111
public override string GetPathInfo()
123112
{
124-
string s1 = GetFilePath();
125-
string s2 = _context.Request.Url.LocalPath;
113+
var s1 = GetFilePath();
114+
var s2 = _context.Request.Url.LocalPath;
126115
if (s1.Length == s2.Length)
127116
return string.Empty;
128117
return s2.Substring(s1.Length);
129118
}
130119

131120
public override string GetQueryString()
132121
{
133-
string queryString = string.Empty;
134-
string rawUrl = _context.Request.RawUrl;
135-
int index = rawUrl.IndexOf('?');
122+
var queryString = string.Empty;
123+
var rawUrl = _context.Request.RawUrl;
124+
var index = rawUrl.IndexOf('?');
136125
if (index != -1)
137126
queryString = rawUrl.Substring(index + 1);
138127
return queryString;
@@ -176,21 +165,17 @@ public override string GetUnknownRequestHeader(string name)
176165

177166
public override string[][] GetUnknownRequestHeaders()
178167
{
179-
string[][] unknownRequestHeaders;
180168
var headers = _context.Request.Headers;
181-
int count = headers.Count;
169+
var count = headers.Count;
182170
var headerPairs = new List<string[]>(count);
183-
for (int i = 0; i < count; i++)
171+
for (var i = 0; i < count; i++)
184172
{
185-
string headerName = headers.GetKey(i);
186-
if (GetKnownRequestHeaderIndex(headerName) == -1)
187-
{
188-
string headerValue = headers.Get(i);
189-
headerPairs.Add(new[] {headerName, headerValue});
190-
}
173+
var headerName = headers.GetKey(i);
174+
if (GetKnownRequestHeaderIndex(headerName) != -1) continue;
175+
var headerValue = headers.Get(i);
176+
headerPairs.Add(new[] {headerName, headerValue});
191177
}
192-
unknownRequestHeaders = headerPairs.ToArray();
193-
return unknownRequestHeaders;
178+
return headerPairs.ToArray();
194179
}
195180

196181
public override string GetUriPath()

src/OpenRasta.Hosting.AspNet/AspNetLogSourceExtensions.cs

-27
This file was deleted.

0 commit comments

Comments
 (0)