Home > .NET > [REPOST] – ASP.NET Server Variables, take 2

[REPOST] – ASP.NET Server Variables, take 2

June 6, 2008

The last thing I did yesterday before I went home was write the code for (and post) a simple class for reading HTTP Request Server Variables. I like the idea, because it allows the consumer to use Intellisense and variable names instead of doing a value lookup using a string.

Nick at Coder Journal left a comment (thanks Nick!) suggesting we do this in a more static way to avoid unnecessarily processing all the variables. I had originally wanted to do something like that, but I also want to separate this class from any particular ASP.NET project, so here is my take on a static-only version that does not require access to the HttpContext object:

public static class ServerVariables2
{
public static NameValueCollection VariableCollection { get; private set; }public static void Clear()
{
VariableCollection = null;
}

public static void Initialize(NameValueCollection values)
{
Clear();
VariableCollection = values;
}

private static string ValidateAndAcquire(string VariableName)
{
if (VariableCollection == null)
{
throw new ArgumentNullException(“VariableCollection is not initialized.”);
}
else if (!VariableCollection.AllKeys.Contains(VariableName))
{
throw new ArgumentOutOfRangeException(
String.Format(“Variable {0} not found in VariableCollection.”, VariableName));
}
else
{
return VariableCollection[VariableName];
}
}

public static string ALL_HTTP { get { return ValidateAndAcquire(“ALL_HTTP”); } }
public static string ALL_RAW { get { return ValidateAndAcquire(“ALL_RAW”); } }
public static string APPL_MD_PATH { get { return ValidateAndAcquire(“APPL_MD_PATH”); } }
public static string APPL_PHYSICAL_PATH { get { return ValidateAndAcquire(“APPL_PHYSICAL_PATH”); } }
public static string AUTH_TYPE { get { return ValidateAndAcquire(“AUTH_TYPE”); } }
public static string AUTH_USER { get { return ValidateAndAcquire(“AUTH_USER”); } }
public static string AUTH_PASSWORD { get { return ValidateAndAcquire(“AUTH_PASSWORD”); } }
public static string LOGON_USER { get { return ValidateAndAcquire(“LOGON_USER”); } }
public static string REMOTE_USER { get { return ValidateAndAcquire(“REMOTE_USER”); } }
public static string CERT_COOKIE { get { return ValidateAndAcquire(“CERT_COOKIE”); } }
public static string CERT_FLAGS { get { return ValidateAndAcquire(“CERT_FLAGS”); } }
public static string CERT_ISSUER { get { return ValidateAndAcquire(“CERT_ISSUER”); } }
public static string CERT_KEYSIZE { get { return ValidateAndAcquire(“CERT_KEYSIZE”); } }
public static string CERT_SECRETKEYSIZE { get { return ValidateAndAcquire(“CERT_SECRETKEYSIZE”); } }
public static string CERT_SERIALNUMBER { get { return ValidateAndAcquire(“CERT_SERIALNUMBER”); } }
public static string CERT_SERVER_ISSUER { get { return ValidateAndAcquire(“CERT_SERVER_ISSUER”); } }
public static string CERT_SERVER_SUBJECT { get { return ValidateAndAcquire(“CERT_SERVER_SUBJECT”); } }
public static string CERT_SUBJECT { get { return ValidateAndAcquire(“CERT_SUBJECT”); } }
public static string CONTENT_LENGTH { get { return ValidateAndAcquire(“CONTENT_LENGTH”); } }
public static string CONTENT_TYPE { get { return ValidateAndAcquire(“CONTENT_TYPE”); } }
public static string GATEWAY_INTERFACE { get { return ValidateAndAcquire(“GATEWAY_INTERFACE”); } }
public static string HTTPS { get { return ValidateAndAcquire(“HTTPS”); } }
public static string HTTPS_KEYSIZE { get { return ValidateAndAcquire(“HTTPS_KEYSIZE”); } }
public static string HTTPS_SECRETKEYSIZE { get { return ValidateAndAcquire(“HTTPS_SECRETKEYSIZE”); } }
public static string HTTPS_SERVER_ISSUER { get { return ValidateAndAcquire(“HTTPS_SERVER_ISSUER”); } }
public static string HTTPS_SERVER_SUBJECT { get { return ValidateAndAcquire(“HTTPS_SERVER_SUBJECT”); } }
public static string INSTANCE_ID { get { return ValidateAndAcquire(“INSTANCE_ID”); } }
public static string INSTANCE_META_PATH { get { return ValidateAndAcquire(“INSTANCE_META_PATH”); } }
public static string LOCAL_ADDR { get { return ValidateAndAcquire(“LOCAL_ADDR”); } }
public static string PATH_INFO { get { return ValidateAndAcquire(“PATH_INFO”); } }
public static string PATH_TRANSLATED { get { return ValidateAndAcquire(“PATH_TRANSLATED”); } }
public static string QUERY_STRING { get { return ValidateAndAcquire(“QUERY_STRING”); } }
public static string REMOTE_ADDR { get { return ValidateAndAcquire(“REMOTE_ADDR”); } }
public static string REMOTE_HOST { get { return ValidateAndAcquire(“REMOTE_HOST”); } }
public static string REMOTE_PORT { get { return ValidateAndAcquire(“REMOTE_PORT”); } }
public static string REQUEST_METHOD { get { return ValidateAndAcquire(“REQUEST_METHOD”); } }
public static string SCRIPT_NAME { get { return ValidateAndAcquire(“SCRIPT_NAME”); } }
public static string SERVER_NAME { get { return ValidateAndAcquire(“SERVER_NAME”); } }
public static string SERVER_PORT { get { return ValidateAndAcquire(“SERVER_PORT”); } }
public static string SERVER_PORT_SECURE { get { return ValidateAndAcquire(“SERVER_PORT_SECURE”); } }
public static string SERVER_PROTOCOL { get { return ValidateAndAcquire(“SERVER_PROTOCOL”); } }
public static string SERVER_SOFTWARE { get { return ValidateAndAcquire(“SERVER_SOFTWARE”); } }
public static string URL { get { return ValidateAndAcquire(“URL”); } }
public static string HTTP_CONNECTION { get { return ValidateAndAcquire(“HTTP_CONNECTION”); } }
public static string HTTP_KEEP_ALIVE { get { return ValidateAndAcquire(“HTTP_KEEP_ALIVE”); } }
public static string HTTP_ACCEPT { get { return ValidateAndAcquire(“HTTP_ACCEPT”); } }
public static string HTTP_ACCEPT_CHARSET { get { return ValidateAndAcquire(“HTTP_ACCEPT_CHARSET”); } }
public static string HTTP_ACCEPT_ENCODING { get { return ValidateAndAcquire(“HTTP_ACCEPT_ENCODING”); } }
public static string HTTP_ACCEPT_LANGUAGE { get { return ValidateAndAcquire(“HTTP_ACCEPT_LANGUAGE”); } }
public static string HTTP_COOKIE { get { return ValidateAndAcquire(“HTTP_COOKIE”); } }
public static string HTTP_HOST { get { return ValidateAndAcquire(“HTTP_HOST”); } }
public static string HTTP_REFERER { get { return ValidateAndAcquire(“HTTP_REFERER”); } }
public static string HTTP_USER_AGENT { get { return ValidateAndAcquire(“HTTP_USER_AGENT”); } }

}

I like this approach, and in the small amount of testing I’ve done it works well while incurring less overhead.

While I was testing this, I started toying with the idea of a third version that has a single Get method based off an Enumeration. The more I thought about it, the more sense it made: this way, the user still gets the complete benefit of Intellisense by being able to see the list of possible values via the Enum. At the same time, the code is much shorter and no longer has a bunch of properties with hard coded string values. Making a change, like adding a variable or hiding certain values would only require maintaining the Enumeration. Below is the final version of the code, for both the Enum and the class

public enum ServerVariables
{
ALL_HTTP,
ALL_RAW,
APPL_MD_PATH,
APPL_PHYSICAL_PATH,
AUTH_TYPE,
AUTH_USER,
AUTH_PASSWORD,
LOGON_USER,
REMOTE_USER,
CERT_COOKIE,
CERT_FLAGS,
CERT_ISSUER,
CERT_KEYSIZE,
CERT_SECRETKEYSIZE,
CERT_SERIALNUMBER,
CERT_SERVER_ISSUER,
CERT_SERVER_SUBJECT,
CERT_SUBJECT,
CONTENT_LENGTH,
CONTENT_TYPE,
GATEWAY_INTERFACE,
HTTPS,
HTTPS_KEYSIZE,
HTTPS_SECRETKEYSIZE,
HTTPS_SERVER_ISSUER,
HTTPS_SERVER_SUBJECT,
INSTANCE_ID,
INSTANCE_META_PATH,
LOCAL_ADDR,
PATH_INFO,
PATH_TRANSLATED,
QUERY_STRING,
REMOTE_ADDR,
REMOTE_HOST,
REMOTE_PORT,
REQUEST_METHOD,
SCRIPT_NAME,
SERVER_NAME,
SERVER_PORT,
SERVER_PORT_SECURE,
SERVER_PROTOCOL,
SERVER_SOFTWARE,
URL,
HTTP_CONNECTION,
HTTP_KEEP_ALIVE,
HTTP_ACCEPT,
HTTP_ACCEPT_CHARSET,
HTTP_ACCEPT_ENCODING,
HTTP_ACCEPT_LANGUAGE,
HTTP_COOKIE,
HTTP_HOST,
HTTP_REFERER,
HTTP_USER_AGENT
}public static class RequestServerVariable
{
public static NameValueCollection VariableCollection { get; private set; }

public static void Clear()
{
VariableCollection = null;
}

public static void Initialize(NameValueCollection values)
{
Clear();
VariableCollection = values;
}

public static string ValidateAndAcquire(ServerVariables Variable)
{
string VariableName = Enum.GetName(typeof(ServerVariables), Variable);

if (VariableCollection == null)
{
throw new ArgumentNullException(“VariableCollection is not initialized.”);
}
else if (!VariableCollection.AllKeys.Contains(VariableName))
{
throw new ArgumentOutOfRangeException(
String.Format(“Variable {0} not found in VariableCollection.”, VariableName));
}
else
{
return VariableCollection[VariableName];
}
}
}

This approach still requires an initialization routine, but it wouldn’t surprise me if there was a better way to handle that issue. Let me know what you think.

Advertisements
Categories: .NET
%d bloggers like this: