diff --git a/src/HyperFastCgi/AppHosts/AspNet/AspNetNativeWebRequest.cs b/src/HyperFastCgi/AppHosts/AspNet/AspNetNativeWebRequest.cs
index 96474d4..761746b 100644
--- a/src/HyperFastCgi/AppHosts/AspNet/AspNetNativeWebRequest.cs
+++ b/src/HyperFastCgi/AppHosts/AspNet/AspNetNativeWebRequest.cs
@@ -84,14 +84,20 @@ static AspNetNativeWebRequest ()
private int input_data_offset;
public int PortNumber {
- get {
- if (port < 0)
- //FIXME: tryparse
- port = int.Parse (GetParameter (
- "SERVER_PORT"));
+ get
+ {
+ if (port < 0)
+ {
+ if (!int.TryParse(GetParameter("SERVER_PORT"), out port)) {
+ Logger.Write(LogLevel.Error, "fastcgi_param 'SERVER_PORT' not set! Setting to default value '80'! "
+ + "Please add 'fastcgi_param SERVER_PORT $server_port;' to your webserver config!");
+ port = 80;
+ }
- return port;
- }
+ }
+
+ return port;
+ }
}
public string Path {
@@ -143,7 +149,7 @@ public AspNetNativeWebRequest (ulong requestId, int requestNumber, IApplicationH
public void AddHeader (string header, string value)
{
- if (!String.IsNullOrEmpty (header)) {
+ if (!string.IsNullOrEmpty (header)) {
int idx = HttpWorkerRequest.GetKnownRequestHeaderIndex (header);
if (idx != -1) {
@@ -154,34 +160,48 @@ public void AddHeader (string header, string value)
}
}
+ ///
+ /// TODO errors can happen here, changing return value to bool and then evaluating the return value outside could help
+ ///
+ ///
public void AddBodyPart (byte[] data)
{
if (input_data == null) {
- int len = 0;
+ int len;
string slen = GetParameter ("CONTENT_LENGTH");
if (slen == null) {
- //TODO: error, throw an exception
+ //Instead of throwing an Exception, write to error log and return
+ Logger.Write(LogLevel.Error, "fastcgi_param 'CONTENT_LENGTH' not set! "
+ + "Please add 'fastcgi_param SERVER_PORT $server_port;' to your webserver config!");
+
+ return;
}
+
if (!int.TryParse (slen, NumberStyles.None, CultureInfo.InvariantCulture, out len)) {
- //TODO: error, throw an exception
+ Logger.Write(LogLevel.Error, "Invalid CONTENT_LENGTH: {0}", slen);
+
+ return;
}
input_data = new byte[len];
}
if (input_data_offset + data.Length > input_data.Length) {
- //TODO: throw an exception
+ //Instead of throwing an Exception, write to error log and return
+ Logger.Write(LogLevel.Error, "Buffer too small: input_data (size {0}) but should be at least {1}", input_data.Length, (input_data_offset + data.Length));
+
+ return;
}
- Buffer.BlockCopy (data, 0, input_data, input_data_offset, data.Length);
+ Buffer.BlockCopy (data, 0, input_data, input_data_offset, data.Length);
input_data_offset += data.Length;
}
public string GetParameter (string parameter)
{
if (parameter_table != null && parameter_table.ContainsKey (parameter))
- return (string)parameter_table [parameter];
+ return parameter_table [parameter];
return null;
}
@@ -268,8 +288,7 @@ public override void SendResponseFromMemory (byte[] data, int length)
public override void SendStatus (int statusCode, string statusDescription)
{
- AppendHeaderLine ("Status: {0} {1}",
- statusCode, statusDescription);
+ AppendHeaderLine ("Status: {0} {1}", statusCode, statusDescription);
}
public override void SendUnknownResponseHeader (string name, string value)
@@ -294,7 +313,7 @@ public override bool IsEntireEntityBodyIsPreloaded ()
public override string GetPathInfo ()
{
- return GetParameter ("PATH_INFO") ?? String.Empty;
+ return GetParameter ("PATH_INFO") ?? string.Empty;
}
public override string GetRawUrl ()
@@ -310,7 +329,7 @@ public override string GetRawUrl ()
StringBuilder b = new StringBuilder (GetUriPath ());
string query = GetQueryString ();
- if (query != null && query.Length > 0) {
+ if (!string.IsNullOrEmpty(query)) {
b.Append ('?');
b.Append (query);
}
@@ -337,17 +356,17 @@ public override string GetHttpVersion ()
public override string GetLocalAddress ()
{
string address = GetParameter ("SERVER_ADDR");
- if (address != null && address.Length > 0)
+ if (!string.IsNullOrEmpty(address))
return address;
address = AddressFromHostName (
GetParameter ("HTTP_HOST"));
- if (address != null && address.Length > 0)
+ if (!string.IsNullOrEmpty(address))
return address;
address = AddressFromHostName (
GetParameter ("SERVER_NAME"));
- if (address != null && address.Length > 0)
+ if (!string.IsNullOrEmpty(address))
return address;
return base.GetLocalAddress ();
@@ -378,7 +397,7 @@ public override byte [] GetQueryStringRawBytes ()
public override string GetRemoteAddress ()
{
string addr = GetParameter ("REMOTE_ADDR");
- return addr != null && addr.Length > 0 ?
+ return !string.IsNullOrEmpty(addr) ?
addr : base.GetRemoteAddress ();
}
@@ -399,7 +418,7 @@ public override string GetRemoteName ()
public override int GetRemotePort ()
{
string port = GetParameter ("REMOTE_PORT");
- if (port == null || port.Length == 0)
+ if (string.IsNullOrEmpty(port))
return base.GetRemotePort ();
try {
@@ -413,10 +432,10 @@ public override string GetServerVariable (string name)
{
string value = GetParameter (name);
- if (value == null)
+ if (value == null && name != null)
value = Environment.GetEnvironmentVariable (name);
- return value != null ? value : base.GetServerVariable (name);
+ return value ?? base.GetServerVariable (name);
}
public override string GetUriPath ()
@@ -553,7 +572,7 @@ private static string AddressFromHostName (string host)
if (host == null || host.Length > 126)
return null;
- System.Net.IPAddress[] addresses = null;
+ IPAddress[] addresses = null;
try {
addresses = Dns.GetHostAddresses (host);
} catch (System.Net.Sockets.SocketException) {
@@ -570,7 +589,7 @@ private static string AddressFromHostName (string host)
private static string HostNameFromString (string host)
{
- if (host == null || host.Length == 0)
+ if (string.IsNullOrEmpty(host))
return null;
int colon_index = host.IndexOf (':');
@@ -592,15 +611,17 @@ private static void SetDefaultIndexFiles (string list)
List files = new List ();
string [] fs = list.Split (',');
- foreach (string f in fs) {
- string trimmed = f.Trim ();
- if (trimmed == "")
- continue;
+ for (int index = 0; index < fs.Length; index++)
+ {
+ string f = fs[index];
+ string trimmed = f.Trim();
+ if (trimmed == "")
+ continue;
- files.Add (trimmed);
- }
+ files.Add(trimmed);
+ }
- indexFiles = files.ToArray ();
+ indexFiles = files.ToArray ();
}
#endregion
@@ -613,16 +634,16 @@ public void Process (IWebResponse response)
#endregion
#region IWebResponse implementation
- public void Send (int status, string description, IDictionary headers)
+ public void Send (int status, string description, IDictionary sendHeaders)
{
SendStatus (status, description);
- foreach (KeyValuePair pair in headers) {
+ foreach (KeyValuePair pair in sendHeaders) {
SendUnknownResponseHeader (pair.Key, pair.Value);
}
}
- public void Send (int status, string description, IDictionary headers, byte[] response)
+ public void Send (int status, string description, IDictionary sendHeaders, byte[] response)
{
- Send (status, description, headers);
+ Send (status, description, sendHeaders);
SendResponseFromMemory (response, response.Length);
}
public void Send (byte[] response)
diff --git a/src/HyperFastCgi/AppHosts/AspNet/MonoWorkerRequest.cs b/src/HyperFastCgi/AppHosts/AspNet/MonoWorkerRequest.cs
index 0f3f088..ce43c47 100644
--- a/src/HyperFastCgi/AppHosts/AspNet/MonoWorkerRequest.cs
+++ b/src/HyperFastCgi/AppHosts/AspNet/MonoWorkerRequest.cs
@@ -150,7 +150,7 @@ static MonoWorkerRequest ()
}
public MonoWorkerRequest (HyperFastCgi.Interfaces.IApplicationHost appHost)
- : base (String.Empty, String.Empty, null)
+ : base (string.Empty, string.Empty, null)
{
if (appHost == null)
throw new ArgumentNullException ("appHost");
@@ -256,12 +256,11 @@ public override string MapPath (string path)
if (eventResult != null)
return eventResult;
- string hostVPath = HostVPath;
+ string currentHostVPath = HostVPath;
int hostVPathLen = HostVPath.Length;
int pathLen = path != null ? path.Length : 0;
- bool inThisApp;
- inThisApp = path.StartsWith (hostVPath, StringComparison.Ordinal);
+ bool inThisApp = path.StartsWith (currentHostVPath, StringComparison.Ordinal);
if (pathLen == 0 || (inThisApp && (pathLen == hostVPathLen || (pathLen == hostVPathLen + 1 && path [pathLen - 1] == '/')))) {
if (needToReplacePathSeparator)
@@ -314,18 +313,14 @@ public void ProcessRequest ()
error = ex.GetHtmlErrorMessage ();
} catch (Exception ex) {
inUnhandledException = true;
- HttpException hex = new HttpException (400, "Bad request", ex);
- if (hex != null) // just a precaution
- error = hex.GetHtmlErrorMessage ();
- else
- error = String.Format (defaultExceptionHtml, ex.Message);
+ error = new HttpException(400, "Bad request", ex).GetHtmlErrorMessage();
}
if (!inUnhandledException)
return;
- if (error.Length == 0)
- error = String.Format (defaultExceptionHtml, "Unknown error");
+ if (error != null && error.Length == 0)
+ error = string.Format (defaultExceptionHtml, "Unknown error");
try {
SendStatus (400, "Bad request");
@@ -333,17 +328,20 @@ public void ProcessRequest ()
SendUnknownResponseHeader ("Date", DateTime.Now.ToUniversalTime ().ToString ("r"));
Encoding enc = Encoding.UTF8;
- if (enc == null)
- enc = Encoding.ASCII;
- byte[] bytes = enc.GetBytes (error);
+ SendUnknownResponseHeader("Content-Type", "text/html; charset=" + enc.WebName);
+
+ if (error != null)
+ {
+ byte[] bytes = enc.GetBytes(error);
+ SendUnknownResponseHeader("Content-Length", bytes.Length.ToString());
+ SendResponseFromMemory(bytes, bytes.Length);
+ }
+
+ FlushResponse (true);
- SendUnknownResponseHeader ("Content-Type", "text/html; charset=" + enc.WebName);
- SendUnknownResponseHeader ("Content-Length", bytes.Length.ToString ());
- SendResponseFromMemory (bytes, bytes.Length);
- FlushResponse (true);
} catch (Exception ex) { // should "never" happen
- throw ex;
+ throw; // rethrow
}
}
@@ -380,7 +378,7 @@ public override void SendKnownResponseHeader (int index, string value)
public override string GetServerVariable (string name)
{
if (server_variables == null)
- return String.Empty;
+ return string.Empty;
if (IsSecure ()) {
X509Certificate client = ClientCertificate;
@@ -388,7 +386,7 @@ public override string GetServerVariable (string name)
case "CERT_COOKIE":
if (cert_cookie == null) {
if (client == null)
- cert_cookie = String.Empty;
+ cert_cookie = string.Empty;
else
cert_cookie = client.GetCertHashString ();
}
@@ -396,7 +394,7 @@ public override string GetServerVariable (string name)
case "CERT_ISSUER":
if (cert_issuer == null) {
if (client == null)
- cert_issuer = String.Empty;
+ cert_issuer = string.Empty;
else
cert_issuer = client.Issuer;
}
@@ -404,7 +402,7 @@ public override string GetServerVariable (string name)
case "CERT_SERIALNUMBER":
if (cert_serial == null) {
if (client == null)
- cert_serial = String.Empty;
+ cert_serial = string.Empty;
else
cert_serial = client.GetSerialNumberString ();
}
@@ -412,7 +410,7 @@ public override string GetServerVariable (string name)
case "CERT_SUBJECT":
if (cert_subject == null) {
if (client == null)
- cert_subject = String.Empty;
+ cert_subject = string.Empty;
else
cert_subject = client.Subject;
}
@@ -421,7 +419,7 @@ public override string GetServerVariable (string name)
}
string s = server_variables [name];
- return (s == null) ? String.Empty : s;
+ return s ?? string.Empty;
}
public void AddServerVariable (string name, string value)
diff --git a/src/HyperFastCgi/AppHosts/Raw/BaseRawRequest.cs b/src/HyperFastCgi/AppHosts/Raw/BaseRawRequest.cs
index b507646..05e7e10 100644
--- a/src/HyperFastCgi/AppHosts/Raw/BaseRawRequest.cs
+++ b/src/HyperFastCgi/AppHosts/Raw/BaseRawRequest.cs
@@ -114,16 +114,16 @@ public IDictionary ServerVariables {
#region IWebResponse implementation
- public void Send (int status, string description, IDictionary headers)
+ public void Send (int status, string description, IDictionary sendHeaders)
{
Status = status;
StatusDescription = description;
- responseHeaders = (Dictionary)headers;
+ responseHeaders = (Dictionary)sendHeaders;
}
- public void Send (int status, string description, IDictionary headers, byte[] response)
+ public void Send (int status, string description, IDictionary sendHeaders, byte[] response)
{
- Send (status, description, headers);
+ Send (status, description, sendHeaders);
Send (response);
}
diff --git a/src/HyperFastCgi/Interfaces/IWebRequest.cs b/src/HyperFastCgi/Interfaces/IWebRequest.cs
index b219d52..e80ffec 100644
--- a/src/HyperFastCgi/Interfaces/IWebRequest.cs
+++ b/src/HyperFastCgi/Interfaces/IWebRequest.cs
@@ -41,11 +41,12 @@ public interface IWebRequest
///
/// Header name.
/// Header value.
- /// This method is called by transport when new header has come
+ /// This method is called by transport when new header has come
void AddHeader(string name, string value);
///
/// Adds the content data
+ /// TODO: Change return value to "bool" to be able to report (buffer) errors?
///
/// content data
/// The method is called by transport to add the part of content (post) data.
@@ -62,7 +63,7 @@ public interface IWebRequest
/// Processes the request.
///
/// Response
- /// The method is called by transport to process Web Request
+ /// The method is called by transport to process Web Request
void Process (IWebResponse response);
}
}
diff --git a/src/HyperFastCgi/Interfaces/IWebResponse.cs b/src/HyperFastCgi/Interfaces/IWebResponse.cs
index 48c2ed7..be81116 100644
--- a/src/HyperFastCgi/Interfaces/IWebResponse.cs
+++ b/src/HyperFastCgi/Interfaces/IWebResponse.cs
@@ -8,9 +8,9 @@ public interface IWebResponse
{
IWebRequest Request { get; }
- void Send (int status, string description, IDictionary headers);
+ void Send (int status, string description, IDictionary sendHeaders);
- void Send (int status, string description, IDictionary headers, byte[] response);
+ void Send (int status, string description, IDictionary sendHeaders, byte[] response);
void Send (byte[] response);