Integrated methods to parse query string
This commit is contained in:
parent
fa01992fa1
commit
dd3cd23ac3
@ -337,12 +337,12 @@ namespace WebSocketSharp.Net
|
|||||||
/// Gets the query string included in the request.
|
/// Gets the query string included in the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// A <see cref="NameValueCollection"/> that contains the query string parameters
|
/// A <see cref="NameValueCollection"/> that contains the query string parameters.
|
||||||
/// included in the request.
|
|
||||||
/// </value>
|
/// </value>
|
||||||
public NameValueCollection QueryString {
|
public NameValueCollection QueryString {
|
||||||
get {
|
get {
|
||||||
return _queryString ?? (_queryString = HttpUtility.ParseQueryStringSimply (_url.Query));
|
return _queryString ??
|
||||||
|
(_queryString = HttpUtility.ParseQueryStringInternally (_url.Query, Encoding.UTF8));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -675,57 +675,33 @@ namespace WebSocketSharp.Net
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void ParseQueryString (
|
internal static NameValueCollection ParseQueryStringInternally (string query, Encoding encoding)
|
||||||
string query, Encoding encoding, NameValueCollection result)
|
|
||||||
{
|
{
|
||||||
if (query.Length == 0)
|
int len;
|
||||||
return;
|
if (query == null || (len = query.Length) == 0 || (len == 1 && query [0] == '?'))
|
||||||
|
return new NameValueCollection (1);
|
||||||
|
|
||||||
var decoded = HtmlDecode (query);
|
if (query [0] == '?')
|
||||||
var decodedLength = decoded.Length;
|
query = query.Substring (1);
|
||||||
var namePos = 0;
|
|
||||||
var first = true;
|
|
||||||
while (namePos <= decodedLength) {
|
|
||||||
var valuePos = -1;
|
|
||||||
var valueEnd = -1;
|
|
||||||
for (int q = namePos; q < decodedLength; q++) {
|
|
||||||
if (valuePos == -1 && decoded [q] == '=') {
|
|
||||||
valuePos = q + 1;
|
|
||||||
}
|
|
||||||
else if (decoded [q] == '&') {
|
|
||||||
valueEnd = q;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (first) {
|
var res = new QueryStringCollection ();
|
||||||
first = false;
|
var components = query.Split ('&');
|
||||||
if (decoded [namePos] == '?')
|
foreach (var component in components) {
|
||||||
namePos++;
|
var i = component.IndexOf ('=');
|
||||||
}
|
if (i > -1) {
|
||||||
|
var name = UrlDecode (component.Substring (0, i), encoding);
|
||||||
|
var val = component.Length > i + 1
|
||||||
|
? UrlDecode (component.Substring (i + 1), encoding)
|
||||||
|
: String.Empty;
|
||||||
|
|
||||||
string name;
|
res.Add (name, val);
|
||||||
if (valuePos == -1) {
|
|
||||||
name = null;
|
|
||||||
valuePos = namePos;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
name = UrlDecode (decoded.Substring (namePos, valuePos - namePos - 1), encoding);
|
res.Add (null, UrlDecode (component, encoding));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (valueEnd < 0) {
|
|
||||||
namePos = -1;
|
|
||||||
valueEnd = decoded.Length;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
namePos = valueEnd + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
var value = UrlDecode (decoded.Substring (valuePos, valueEnd - valuePos), encoding);
|
|
||||||
result.Add (name, value);
|
|
||||||
if (namePos == -1)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string UrlDecodeInternally (
|
internal static string UrlDecodeInternally (
|
||||||
@ -1074,7 +1050,10 @@ namespace WebSocketSharp.Net
|
|||||||
|
|
||||||
public static NameValueCollection ParseQueryString (string query)
|
public static NameValueCollection ParseQueryString (string query)
|
||||||
{
|
{
|
||||||
return ParseQueryString (query, Encoding.UTF8);
|
if (query == null)
|
||||||
|
throw new ArgumentNullException ("query");
|
||||||
|
|
||||||
|
return ParseQueryStringInternally (query, Encoding.UTF8);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NameValueCollection ParseQueryString (string query, Encoding encoding)
|
public static NameValueCollection ParseQueryString (string query, Encoding encoding)
|
||||||
@ -1082,50 +1061,10 @@ namespace WebSocketSharp.Net
|
|||||||
if (query == null)
|
if (query == null)
|
||||||
throw new ArgumentNullException ("query");
|
throw new ArgumentNullException ("query");
|
||||||
|
|
||||||
var len = query.Length;
|
|
||||||
if (len == 0 || (len == 1 && query [0] == '?'))
|
|
||||||
return new NameValueCollection (1);
|
|
||||||
|
|
||||||
if (query [0] == '?')
|
|
||||||
query = query.Substring (1);
|
|
||||||
|
|
||||||
if (encoding == null)
|
if (encoding == null)
|
||||||
encoding = Encoding.UTF8;
|
throw new ArgumentNullException ("encoding");
|
||||||
|
|
||||||
var res = new QueryStringCollection ();
|
return ParseQueryStringInternally (query, encoding);
|
||||||
ParseQueryString (query, encoding, res);
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Used by HttpListenerRequest and TcpListenerWebSocketContext.
|
|
||||||
public static NameValueCollection ParseQueryStringSimply (string query)
|
|
||||||
{
|
|
||||||
int len;
|
|
||||||
if (query == null || (len = query.Length) == 0 || (len == 1 && query [0] == '?'))
|
|
||||||
return new NameValueCollection (1);
|
|
||||||
|
|
||||||
if (query [0] == '?')
|
|
||||||
query = query.Substring (1);
|
|
||||||
|
|
||||||
var res = new QueryStringCollection ();
|
|
||||||
var components = query.Split ('&');
|
|
||||||
foreach (var component in components) {
|
|
||||||
var i = component.IndexOf ('=');
|
|
||||||
if (i > -1) {
|
|
||||||
var name = UrlDecode (component.Substring (0, i), Encoding.UTF8);
|
|
||||||
var val = component.Length > i + 1
|
|
||||||
? UrlDecode (component.Substring (i + 1), Encoding.UTF8)
|
|
||||||
: String.Empty;
|
|
||||||
|
|
||||||
res.Add (name, val);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.Add (null, UrlDecode (component, Encoding.UTF8));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string UrlDecode (string s)
|
public static string UrlDecode (string s)
|
||||||
|
@ -168,10 +168,10 @@ namespace WebSocketSharp.Net.WebSockets
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the query string variables included in the request.
|
/// Gets the query string included in the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// A <see cref="NameValueCollection"/> that contains the query string variables.
|
/// A <see cref="NameValueCollection"/> that contains the query string parameters.
|
||||||
/// </value>
|
/// </value>
|
||||||
public override NameValueCollection QueryString {
|
public override NameValueCollection QueryString {
|
||||||
get {
|
get {
|
||||||
|
@ -32,6 +32,7 @@ using System.Collections.Specialized;
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace WebSocketSharp.Net.WebSockets
|
namespace WebSocketSharp.Net.WebSockets
|
||||||
{
|
{
|
||||||
@ -183,16 +184,16 @@ namespace WebSocketSharp.Net.WebSockets
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the query string variables included in the request.
|
/// Gets the query string included in the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// A <see cref="NameValueCollection"/> that contains the query string variables.
|
/// A <see cref="NameValueCollection"/> that contains the query string parameters.
|
||||||
/// </value>
|
/// </value>
|
||||||
public override NameValueCollection QueryString {
|
public override NameValueCollection QueryString {
|
||||||
get {
|
get {
|
||||||
return _queryString ??
|
return _queryString ??
|
||||||
(_queryString =
|
(_queryString = HttpUtility.ParseQueryStringInternally (
|
||||||
HttpUtility.ParseQueryStringSimply (_uri != null ? _uri.Query : null));
|
_uri != null ? _uri.Query : null, Encoding.UTF8));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,10 +119,10 @@ namespace WebSocketSharp.Net.WebSockets
|
|||||||
public abstract string Origin { get; }
|
public abstract string Origin { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the query string variables included in the request.
|
/// Gets the query string included in the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>
|
/// <value>
|
||||||
/// A <see cref="NameValueCollection"/> that contains the query string variables.
|
/// A <see cref="NameValueCollection"/> that contains the query string parameters.
|
||||||
/// </value>
|
/// </value>
|
||||||
public abstract NameValueCollection QueryString { get; }
|
public abstract NameValueCollection QueryString { get; }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user