Permalink
Browse files

Exposed the URI query parameters via IRequest/Request

  • Loading branch information...
chris.nicola
chris.nicola committed Jan 24, 2011
1 parent 90318fa commit 257068933ec9991b5fb42afca01ced617683ae80
Showing with 29 additions and 16 deletions.
  1. +17 −0 src/Nancy/Extensions/StringExtensions.cs
  2. +2 −1 src/Nancy/Hosting/NancyHandler.cs
  3. +10 −15 src/Nancy/IRequest.cs
@@ -1,3 +1,6 @@
using System.Linq;
using System.Web;

namespace Nancy.Extensions
{
using System;
@@ -47,5 +50,19 @@ public static bool IsParameterized(this string segment)

return parameterMatch.Success;
}

/// <summary>
/// Gets a dynamic dictionary back from a Uri query string
/// </summary>
/// <param name="queryString">The query string to extract values from</param>
/// <returns>A dynamic dictionary containing the query string values</returns>
public static DynamicDictionary AsQueryDictionary(this string queryString)
{
var coll = HttpUtility.ParseQueryString(queryString);
var ret = new DynamicDictionary();
foreach (var key in coll.AllKeys.Where(key => key != null))
ret[key] = coll[key];
return ret;
}
}
}
@@ -37,7 +37,8 @@ private static IRequest CreateNancyRequest(HttpContextBase context)
context.Request.AppRelativeCurrentExecutionFilePath.Replace("~",""),
context.Request.Headers.ToDictionary(),
context.Request.InputStream,
context.Request.Url.Scheme);
context.Request.Url.Scheme,
context.Request.Url.Query);
}

private static void SetNancyResponseToHttpResponse(HttpContextBase context, Response response)
@@ -1,10 +1,11 @@
using Nancy.Extensions;

namespace Nancy
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

public interface IRequest
{
@@ -18,6 +19,8 @@ public interface IRequest

dynamic Form { get; }

dynamic Query { get; }

string Protocol { get; }
}

@@ -30,7 +33,7 @@ public Request(string method, string uri, string protocol)
{
}

public Request(string method, string uri, IDictionary<string, IEnumerable<string>> headers, Stream body, string protocol)
public Request(string method, string uri, IDictionary<string, IEnumerable<string>> headers, Stream body, string protocol, string query = "")
{
if (method == null)
throw new ArgumentNullException("method", "The value of the method parameter cannot be null.");
@@ -61,8 +64,11 @@ public Request(string method, string uri, IDictionary<string, IEnumerable<string
this.Method = method;
this.Uri = uri;
this.Protocol = protocol;
this.Query = query.AsQueryDictionary();
}

public dynamic Query { get; set; }

public Stream Body { get; set; }

public dynamic Form
@@ -72,27 +78,16 @@ public dynamic Form

private dynamic GetFormData()
{
var ret = new DynamicDictionary();

if (this.Headers.Keys.Any(x => x.Equals("content-type", StringComparison.OrdinalIgnoreCase)))
{
var contentType = this.Headers["content-type"].First();
if (contentType.Equals("application/x-www-form-urlencoded", StringComparison.OrdinalIgnoreCase))
{
var reader = new StreamReader(this.Body);
var coll = HttpUtility.ParseQueryString(reader.ReadToEnd());

foreach (var key in coll.AllKeys)
{
if(key != null)
{
ret[key] = coll[key];
}
}
return reader.ReadToEnd().AsQueryDictionary();
}
}

return ret;
return new DynamicDictionary();
}

public IDictionary<string, IEnumerable<string>> Headers { get; private set; }

0 comments on commit 2570689

Please sign in to comment.