Fix for request url

This commit is contained in:
sta
2014-05-28 14:20:46 +09:00
parent 9909c52d2d
commit 331d840775
8 changed files with 143 additions and 155 deletions

View File

@@ -346,6 +346,7 @@ namespace WebSocketSharp.Net
/// </value>
public string RawUrl {
get {
// TODO: Should decode?
return _url.PathAndQuery;
}
}
@@ -467,7 +468,10 @@ namespace WebSocketSharp.Net
var i = component.IndexOf ('=');
if (i > -1) {
var name = HttpUtility.UrlDecode (component.Substring (0, i));
var val = HttpUtility.UrlDecode (component.Substring (i + 1));
var val = component.Length > i + 1
? HttpUtility.UrlDecode (component.Substring (i + 1))
: String.Empty;
res.Add (name, val);
}
else {
@@ -571,7 +575,7 @@ namespace WebSocketSharp.Net
string scheme = null;
string path = null;
if (_uri.StartsWith ("/")) {
path = HttpUtility.UrlDecode (_uri);
path = _uri;
}
else if (_uri.MaybeUri ()) {
Uri uri;
@@ -589,7 +593,7 @@ namespace WebSocketSharp.Net
}
else {
// As authority form
host = HttpUtility.UrlDecode (_uri);
host = _uri;
}
if (scheme == null)

View File

@@ -167,18 +167,6 @@ namespace WebSocketSharp.Net.WebSockets
}
}
/// <summary>
/// Gets the absolute path of the requested URI.
/// </summary>
/// <value>
/// A <see cref="string"/> that represents the absolute path of the requested URI.
/// </value>
public override string Path {
get {
return _context.Request.Url.GetAbsolutePath ();
}
}
/// <summary>
/// Gets the query string variables included in the request.
/// </summary>

View File

@@ -45,14 +45,15 @@ namespace WebSocketSharp.Net.WebSockets
{
#region Private Fields
private TcpClient _client;
private CookieCollection _cookies;
private HandshakeRequest _request;
private bool _secure;
private WebSocketStream _stream;
private Uri _uri;
private IPrincipal _user;
private WebSocket _websocket;
private TcpClient _client;
private CookieCollection _cookies;
private NameValueCollection _queryString;
private HandshakeRequest _request;
private bool _secure;
private WebSocketStream _stream;
private Uri _uri;
private IPrincipal _user;
private WebSocket _websocket;
#endregion
@@ -65,6 +66,7 @@ namespace WebSocketSharp.Net.WebSockets
_secure = secure;
_stream = WebSocketStream.CreateServerStream (client, secure, cert);
_request = _stream.ReadHandshake<HandshakeRequest> (HandshakeRequest.Parse, 90000);
_uri = createRequestUrl (_request, secure);
_websocket = new WebSocket (this, protocol, logger);
}
@@ -178,18 +180,6 @@ namespace WebSocketSharp.Net.WebSockets
}
}
/// <summary>
/// Gets the absolute path of the requested URI.
/// </summary>
/// <value>
/// A <see cref="string"/> that represents the absolute path of the requested URI.
/// </value>
public override string Path {
get {
return _request.RequestUri.GetAbsolutePath ();
}
}
/// <summary>
/// Gets the query string variables included in the request.
/// </summary>
@@ -198,7 +188,8 @@ namespace WebSocketSharp.Net.WebSockets
/// </value>
public override NameValueCollection QueryString {
get {
return _request.QueryString;
return _queryString ??
(_queryString = createQueryString (_uri != null ? _uri.Query : null));
}
}
@@ -210,7 +201,7 @@ namespace WebSocketSharp.Net.WebSockets
/// </value>
public override Uri RequestUri {
get {
return _uri ?? (_uri = createRequestUri ());
return _uri;
}
}
@@ -318,16 +309,78 @@ namespace WebSocketSharp.Net.WebSockets
#region Private Methods
private Uri createRequestUri ()
private static NameValueCollection createQueryString (string query)
{
var scheme = _secure ? "wss" : "ws";
var host = _request.Headers ["Host"];
var rawUri = _request.RequestUri;
var path = rawUri.IsAbsoluteUri
? rawUri.PathAndQuery
: HttpUtility.UrlDecode (_request.RawUrl);
if (query == null || query.Length == 0)
return new NameValueCollection (1);
return String.Format ("{0}://{1}{2}", scheme, host, path).ToUri ();
var res = new NameValueCollection ();
if (query [0] == '?')
query = query.Substring (1);
var components = query.Split ('&');
foreach (var component in components) {
var i = component.IndexOf ('=');
if (i > -1) {
var name = HttpUtility.UrlDecode (component.Substring (0, i));
var val = component.Length > i + 1
? HttpUtility.UrlDecode (component.Substring (i + 1))
: String.Empty;
res.Add (name, val);
}
else {
res.Add (null, HttpUtility.UrlDecode (component));
}
}
return res;
}
private static Uri createRequestUrl (HandshakeRequest request, bool secure)
{
var host = request.Headers ["Host"];
if (host == null || host.Length == 0)
return null;
string scheme = null;
string path = null;
var reqUri = request.RequestUri;
if (reqUri.StartsWith ("/")) {
path = reqUri;
}
else if (reqUri.MaybeUri ()) {
Uri uri;
if (!Uri.TryCreate (reqUri, UriKind.Absolute, out uri) ||
!uri.Scheme.StartsWith ("ws"))
return null;
scheme = uri.Scheme;
host = uri.Authority;
path = uri.PathAndQuery;
}
else if (reqUri == "*") {
}
else {
// As authority form
host = reqUri;
}
if (scheme == null)
scheme = secure ? "wss" : "ws";
var colon = host.IndexOf (':');
if (colon == -1)
host = String.Format ("{0}:{1}", host, scheme == "ws" ? 80 : 443);
var url = String.Format ("{0}://{1}{2}", scheme, host, path);
Uri res;
if (!Uri.TryCreate (url, UriKind.Absolute, out res))
return null;
return res;
}
#endregion

View File

@@ -118,14 +118,6 @@ namespace WebSocketSharp.Net.WebSockets
/// </value>
public abstract string Origin { get; }
/// <summary>
/// Gets the absolute path of the requested URI.
/// </summary>
/// <value>
/// A <see cref="string"/> that represents the absolute path of the requested URI.
/// </value>
public abstract string Path { get; }
/// <summary>
/// Gets the query string variables included in the request.
/// </summary>