Refactored HttpListenerRequest.cs
This commit is contained in:
parent
c1dd8d659e
commit
034e245272
@ -55,15 +55,9 @@ namespace WebSocketSharp.Net
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
public sealed class HttpListenerRequest
|
public sealed class HttpListenerRequest
|
||||||
{
|
{
|
||||||
#region Private Static Fields
|
|
||||||
|
|
||||||
private static byte [] _100continue =
|
|
||||||
Encoding.ASCII.GetBytes ("HTTP/1.1 100 Continue\r\n\r\n");
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Private Fields
|
#region Private Fields
|
||||||
|
|
||||||
|
private static readonly byte[] _100continue;
|
||||||
private string[] _acceptTypes;
|
private string[] _acceptTypes;
|
||||||
private bool _chunked;
|
private bool _chunked;
|
||||||
private Encoding _contentEncoding;
|
private Encoding _contentEncoding;
|
||||||
@ -88,6 +82,15 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Static Constructor
|
||||||
|
|
||||||
|
static HttpListenerRequest ()
|
||||||
|
{
|
||||||
|
_100continue = Encoding.ASCII.GetBytes ("HTTP/1.1 100 Continue\r\n\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Internal Constructors
|
#region Internal Constructors
|
||||||
|
|
||||||
internal HttpListenerRequest (HttpListenerContext context)
|
internal HttpListenerRequest (HttpListenerContext context)
|
||||||
@ -464,12 +467,12 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
private static bool tryCreateVersion (string version, out Version result)
|
private static bool tryCreateVersion (string version, out Version result)
|
||||||
{
|
{
|
||||||
result = null;
|
|
||||||
try {
|
try {
|
||||||
result = new Version (version);
|
result = new Version (version);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
result = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -502,9 +505,9 @@ namespace WebSocketSharp.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (lower == "content-length") {
|
if (lower == "content-length") {
|
||||||
long length;
|
long len;
|
||||||
if (Int64.TryParse (val, out length) && length >= 0) {
|
if (Int64.TryParse (val, out len) && len >= 0) {
|
||||||
_contentLength = length;
|
_contentLength = len;
|
||||||
_contentLengthWasSet = true;
|
_contentLengthWasSet = true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -515,11 +518,11 @@ namespace WebSocketSharp.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (lower == "content-type") {
|
if (lower == "content-type") {
|
||||||
var contents = val.Split (';');
|
var parts = val.Split (';');
|
||||||
foreach (var content in contents) {
|
foreach (var p in parts) {
|
||||||
var tmp = content.Trim ();
|
var part = p.Trim ();
|
||||||
if (tmp.StartsWith ("charset")) {
|
if (part.StartsWith ("charset", StringComparison.OrdinalIgnoreCase)) {
|
||||||
var charset = tmp.GetValue ("=");
|
var charset = part.GetValue ("=");
|
||||||
if (charset != null && charset.Length > 0) {
|
if (charset != null && charset.Length > 0) {
|
||||||
try {
|
try {
|
||||||
_contentEncoding = Encoding.GetEncoding (charset.Trim ('"'));
|
_contentEncoding = Encoding.GetEncoding (charset.Trim ('"'));
|
||||||
@ -592,15 +595,15 @@ namespace WebSocketSharp.Net
|
|||||||
if (!HasEntityBody)
|
if (!HasEntityBody)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
var length = 2048;
|
var len = 2048;
|
||||||
if (_contentLength > 0)
|
if (_contentLength > 0)
|
||||||
length = (int) Math.Min (_contentLength, (long) length);
|
len = (int) Math.Min (_contentLength, (long) len);
|
||||||
|
|
||||||
var buff = new byte [length];
|
var buff = new byte[len];
|
||||||
while (true) {
|
while (true) {
|
||||||
// TODO: Test if MS has a timeout when doing this.
|
// TODO: Test if MS has a timeout when doing this.
|
||||||
try {
|
try {
|
||||||
var ares = InputStream.BeginRead (buff, 0, length, null, null);
|
var ares = InputStream.BeginRead (buff, 0, len, null, null);
|
||||||
if (!ares.IsCompleted && !ares.AsyncWaitHandle.WaitOne (100))
|
if (!ares.IsCompleted && !ares.AsyncWaitHandle.WaitOne (100))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -715,11 +718,11 @@ namespace WebSocketSharp.Net
|
|||||||
/// </returns>
|
/// </returns>
|
||||||
public override string ToString ()
|
public override string ToString ()
|
||||||
{
|
{
|
||||||
var buff = new StringBuilder (64);
|
var output = new StringBuilder (64);
|
||||||
buff.AppendFormat ("{0} {1} HTTP/{2}\r\n", _method, _uri, _version);
|
output.AppendFormat ("{0} {1} HTTP/{2}\r\n", _method, _uri, _version);
|
||||||
buff.Append (_headers.ToString ());
|
output.Append (_headers.ToString ());
|
||||||
|
|
||||||
return buff.ToString ();
|
return output.ToString ();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
Loading…
Reference in New Issue
Block a user