parent
a6e7438987
commit
0a03d7ad9f
@ -1,58 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using MediaBrowser.Model.Services;
|
||||
using ServiceStack;
|
||||
|
||||
namespace ServiceStack.Host
|
||||
namespace Emby.Server.Implementations.Services
|
||||
{
|
||||
public class ContentTypes
|
||||
public class RequestHelper
|
||||
{
|
||||
public static ContentTypes Instance = new ContentTypes();
|
||||
|
||||
public void SerializeToStream(IRequest req, object response, Stream responseStream)
|
||||
public static Func<Type, Stream, object> GetRequestReader(string contentType)
|
||||
{
|
||||
var contentType = req.ResponseContentType;
|
||||
var serializer = GetStreamSerializer(contentType);
|
||||
|
||||
serializer(response, responseStream);
|
||||
}
|
||||
|
||||
public static Action<object, Stream> GetStreamSerializer(string contentType)
|
||||
{
|
||||
switch (GetRealContentType(contentType))
|
||||
switch (GetContentTypeWithoutEncoding(contentType))
|
||||
{
|
||||
case "application/xml":
|
||||
case "text/xml":
|
||||
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
|
||||
return (o, s) => ServiceStackHost.Instance.SerializeToXml(o, s);
|
||||
return ServiceStackHost.Instance.DeserializeXml;
|
||||
|
||||
case "application/json":
|
||||
case "text/json":
|
||||
return (o, s) => ServiceStackHost.Instance.SerializeToJson(o, s);
|
||||
return ServiceStackHost.Instance.DeserializeJson;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Func<Type, Stream, object> GetStreamDeserializer(string contentType)
|
||||
public static Action<object, Stream> GetResponseWriter(string contentType)
|
||||
{
|
||||
switch (GetRealContentType(contentType))
|
||||
switch (GetContentTypeWithoutEncoding(contentType))
|
||||
{
|
||||
case "application/xml":
|
||||
case "text/xml":
|
||||
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
|
||||
return ServiceStackHost.Instance.DeserializeXml;
|
||||
return (o, s) => ServiceStackHost.Instance.SerializeToXml(o, s);
|
||||
|
||||
case "application/json":
|
||||
case "text/json":
|
||||
return ServiceStackHost.Instance.DeserializeJson;
|
||||
return (o, s) => ServiceStackHost.Instance.SerializeToJson(o, s);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string GetRealContentType(string contentType)
|
||||
private static string GetContentTypeWithoutEncoding(string contentType)
|
||||
{
|
||||
return contentType == null
|
||||
? null
|
@ -0,0 +1,297 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Emby.Server.Implementations.HttpServer;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Services;
|
||||
using ServiceStack;
|
||||
|
||||
namespace Emby.Server.Implementations.Services
|
||||
{
|
||||
public class ServiceHandler
|
||||
{
|
||||
public async Task<object> HandleResponseAsync(object response)
|
||||
{
|
||||
var taskResponse = response as Task;
|
||||
|
||||
if (taskResponse == null)
|
||||
{
|
||||
return response;
|
||||
}
|
||||
|
||||
await taskResponse.ConfigureAwait(false);
|
||||
|
||||
var taskResult = GetTaskResult(taskResponse);
|
||||
|
||||
var subTask = taskResult as Task;
|
||||
if (subTask != null)
|
||||
{
|
||||
taskResult = GetTaskResult(subTask);
|
||||
}
|
||||
|
||||
return taskResult;
|
||||
}
|
||||
|
||||
internal static object GetTaskResult(Task task)
|
||||
{
|
||||
try
|
||||
{
|
||||
var taskObject = task as Task<object>;
|
||||
if (taskObject != null)
|
||||
{
|
||||
return taskObject.Result;
|
||||
}
|
||||
|
||||
task.Wait();
|
||||
|
||||
var type = task.GetType().GetTypeInfo();
|
||||
if (!type.IsGenericType)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return type.GetDeclaredProperty("Result").GetValue(task);
|
||||
}
|
||||
catch (TypeAccessException)
|
||||
{
|
||||
return null; //return null for void Task's
|
||||
}
|
||||
}
|
||||
|
||||
protected static object CreateContentTypeRequest(IRequest httpReq, Type requestType, string contentType)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(contentType) && httpReq.ContentLength > 0)
|
||||
{
|
||||
var deserializer = RequestHelper.GetRequestReader(contentType);
|
||||
if (deserializer != null)
|
||||
{
|
||||
return deserializer(requestType, httpReq.InputStream);
|
||||
}
|
||||
}
|
||||
return ServiceStackHost.Instance.CreateInstance(requestType); //Return an empty DTO, even for empty request bodies
|
||||
}
|
||||
|
||||
public static RestPath FindMatchingRestPath(string httpMethod, string pathInfo, ILogger logger, out string contentType)
|
||||
{
|
||||
pathInfo = GetSanitizedPathInfo(pathInfo, out contentType);
|
||||
|
||||
return ServiceController.Instance.GetRestPathForRequest(httpMethod, pathInfo, logger);
|
||||
}
|
||||
|
||||
public static string GetSanitizedPathInfo(string pathInfo, out string contentType)
|
||||
{
|
||||
contentType = null;
|
||||
var pos = pathInfo.LastIndexOf('.');
|
||||
if (pos >= 0)
|
||||
{
|
||||
var format = pathInfo.Substring(pos + 1);
|
||||
contentType = GetFormatContentType(format);
|
||||
if (contentType != null)
|
||||
{
|
||||
pathInfo = pathInfo.Substring(0, pos);
|
||||
}
|
||||
}
|
||||
return pathInfo;
|
||||
}
|
||||
|
||||
private static string GetFormatContentType(string format)
|
||||
{
|
||||
//built-in formats
|
||||
if (format == "json")
|
||||
return "application/json";
|
||||
if (format == "xml")
|
||||
return "application/xml";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public RestPath GetRestPath(string httpMethod, string pathInfo)
|
||||
{
|
||||
if (this.RestPath == null)
|
||||
{
|
||||
string contentType;
|
||||
this.RestPath = FindMatchingRestPath(httpMethod, pathInfo, new NullLogger(), out contentType);
|
||||
|
||||
if (contentType != null)
|
||||
ResponseContentType = contentType;
|
||||
}
|
||||
return this.RestPath;
|
||||
}
|
||||
|
||||
public RestPath RestPath { get; set; }
|
||||
|
||||
// Set from SSHHF.GetHandlerForPathInfo()
|
||||
public string ResponseContentType { get; set; }
|
||||
|
||||
public async Task ProcessRequestAsync(HttpListenerHost appHost, IRequest httpReq, IResponse httpRes, ILogger logger, string operationName)
|
||||
{
|
||||
var restPath = GetRestPath(httpReq.Verb, httpReq.PathInfo);
|
||||
if (restPath == null)
|
||||
{
|
||||
throw new NotSupportedException("No RestPath found for: " + httpReq.Verb + " " + httpReq.PathInfo);
|
||||
}
|
||||
|
||||
SetRoute(httpReq, restPath);
|
||||
|
||||
if (ResponseContentType != null)
|
||||
httpReq.ResponseContentType = ResponseContentType;
|
||||
|
||||
var request = httpReq.Dto = CreateRequest(httpReq, restPath, logger);
|
||||
|
||||
appHost.ApplyRequestFilters(httpReq, httpRes, request);
|
||||
|
||||
var rawResponse = await appHost.ServiceController.Execute(appHost, request, httpReq).ConfigureAwait(false);
|
||||
|
||||
var response = await HandleResponseAsync(rawResponse).ConfigureAwait(false);
|
||||
|
||||
// Apply response filters
|
||||
foreach (var responseFilter in appHost.GlobalResponseFilters)
|
||||
{
|
||||
responseFilter(httpReq, httpRes, response);
|
||||
}
|
||||
|
||||
await ResponseHelper.WriteToResponse(httpRes, httpReq, response).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static object CreateRequest(IRequest httpReq, RestPath restPath, ILogger logger)
|
||||
{
|
||||
var requestType = restPath.RequestType;
|
||||
|
||||
if (RequireqRequestStream(requestType))
|
||||
{
|
||||
// Used by IRequiresRequestStream
|
||||
return CreateRequiresRequestStreamRequest(httpReq, requestType);
|
||||
}
|
||||
|
||||
var requestParams = GetFlattenedRequestParams(httpReq);
|
||||
return CreateRequest(httpReq, restPath, requestParams);
|
||||
}
|
||||
|
||||
private static bool RequireqRequestStream(Type requestType)
|
||||
{
|
||||
var requiresRequestStreamTypeInfo = typeof(IRequiresRequestStream).GetTypeInfo();
|
||||
|
||||
return requiresRequestStreamTypeInfo.IsAssignableFrom(requestType.GetTypeInfo());
|
||||
}
|
||||
|
||||
private static IRequiresRequestStream CreateRequiresRequestStreamRequest(IRequest req, Type requestType)
|
||||
{
|
||||
var restPath = GetRoute(req);
|
||||
var request = ServiceHandler.CreateRequest(req, restPath, GetRequestParams(req), ServiceStackHost.Instance.CreateInstance(requestType));
|
||||
|
||||
var rawReq = (IRequiresRequestStream)request;
|
||||
rawReq.RequestStream = req.InputStream;
|
||||
return rawReq;
|
||||
}
|
||||
|
||||
public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams)
|
||||
{
|
||||
var requestDto = CreateContentTypeRequest(httpReq, restPath.RequestType, httpReq.ContentType);
|
||||
|
||||
return CreateRequest(httpReq, restPath, requestParams, requestDto);
|
||||
}
|
||||
|
||||
public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams, object requestDto)
|
||||
{
|
||||
string contentType;
|
||||
var pathInfo = !restPath.IsWildCardPath
|
||||
? GetSanitizedPathInfo(httpReq.PathInfo, out contentType)
|
||||
: httpReq.PathInfo;
|
||||
|
||||
return restPath.CreateRequest(pathInfo, requestParams, requestDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Duplicate Params are given a unique key by appending a #1 suffix
|
||||
/// </summary>
|
||||
private static Dictionary<string, string> GetRequestParams(IRequest request)
|
||||
{
|
||||
var map = new Dictionary<string, string>();
|
||||
|
||||
foreach (var name in request.QueryString.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
|
||||
var values = request.QueryString.GetValues(name);
|
||||
if (values.Length == 1)
|
||||
{
|
||||
map[name] = values[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
map[name + (i == 0 ? "" : "#" + i)] = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((IsMethod(request.Verb, "POST") || IsMethod(request.Verb, "PUT")) && request.FormData != null)
|
||||
{
|
||||
foreach (var name in request.FormData.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
|
||||
var values = request.FormData.GetValues(name);
|
||||
if (values.Length == 1)
|
||||
{
|
||||
map[name] = values[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
map[name + (i == 0 ? "" : "#" + i)] = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private static bool IsMethod(string method, string expected)
|
||||
{
|
||||
return string.Equals(method, expected, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Duplicate params have their values joined together in a comma-delimited string
|
||||
/// </summary>
|
||||
private static Dictionary<string, string> GetFlattenedRequestParams(IRequest request)
|
||||
{
|
||||
var map = new Dictionary<string, string>();
|
||||
|
||||
foreach (var name in request.QueryString.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
map[name] = request.QueryString[name];
|
||||
}
|
||||
|
||||
if ((IsMethod(request.Verb, "POST") || IsMethod(request.Verb, "PUT")) && request.FormData != null)
|
||||
{
|
||||
foreach (var name in request.FormData.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
map[name] = request.FormData[name];
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private static void SetRoute(IRequest req, RestPath route)
|
||||
{
|
||||
req.Items["__route"] = route;
|
||||
}
|
||||
|
||||
private static RestPath GetRoute(IRequest req)
|
||||
{
|
||||
object route;
|
||||
req.Items.TryGetValue("__route", out route);
|
||||
return route as RestPath;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,177 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Services;
|
||||
|
||||
namespace ServiceStack.Host
|
||||
{
|
||||
public class RestHandler
|
||||
{
|
||||
public string RequestName { get; set; }
|
||||
|
||||
public async Task<object> HandleResponseAsync(object response)
|
||||
{
|
||||
var taskResponse = response as Task;
|
||||
|
||||
if (taskResponse == null)
|
||||
{
|
||||
return response;
|
||||
}
|
||||
|
||||
await taskResponse.ConfigureAwait(false);
|
||||
|
||||
var taskResult = ServiceStackHost.Instance.GetTaskResult(taskResponse, RequestName);
|
||||
|
||||
var subTask = taskResult as Task;
|
||||
if (subTask != null)
|
||||
{
|
||||
taskResult = ServiceStackHost.Instance.GetTaskResult(subTask, RequestName);
|
||||
}
|
||||
|
||||
return taskResult;
|
||||
}
|
||||
|
||||
protected static object CreateContentTypeRequest(IRequest httpReq, Type requestType, string contentType)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(contentType) && httpReq.ContentLength > 0)
|
||||
{
|
||||
var deserializer = ContentTypes.Instance.GetStreamDeserializer(contentType);
|
||||
if (deserializer != null)
|
||||
{
|
||||
return deserializer(requestType, httpReq.InputStream);
|
||||
}
|
||||
}
|
||||
return ServiceStackHost.Instance.CreateInstance(requestType); //Return an empty DTO, even for empty request bodies
|
||||
}
|
||||
|
||||
protected static object GetCustomRequestFromBinder(IRequest httpReq, Type requestType)
|
||||
{
|
||||
Func<IRequest, object> requestFactoryFn;
|
||||
ServiceStackHost.Instance.ServiceController.RequestTypeFactoryMap.TryGetValue(
|
||||
requestType, out requestFactoryFn);
|
||||
|
||||
return requestFactoryFn != null ? requestFactoryFn(httpReq) : null;
|
||||
}
|
||||
|
||||
public static RestPath FindMatchingRestPath(string httpMethod, string pathInfo, ILogger logger, out string contentType)
|
||||
{
|
||||
pathInfo = GetSanitizedPathInfo(pathInfo, out contentType);
|
||||
|
||||
return ServiceStackHost.Instance.ServiceController.GetRestPathForRequest(httpMethod, pathInfo, logger);
|
||||
}
|
||||
|
||||
public static string GetSanitizedPathInfo(string pathInfo, out string contentType)
|
||||
{
|
||||
contentType = null;
|
||||
var pos = pathInfo.LastIndexOf('.');
|
||||
if (pos >= 0)
|
||||
{
|
||||
var format = pathInfo.Substring(pos + 1);
|
||||
contentType = GetFormatContentType(format);
|
||||
if (contentType != null)
|
||||
{
|
||||
pathInfo = pathInfo.Substring(0, pos);
|
||||
}
|
||||
}
|
||||
return pathInfo;
|
||||
}
|
||||
|
||||
private static string GetFormatContentType(string format)
|
||||
{
|
||||
//built-in formats
|
||||
if (format == "json")
|
||||
return "application/json";
|
||||
if (format == "xml")
|
||||
return "application/xml";
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public RestPath GetRestPath(string httpMethod, string pathInfo)
|
||||
{
|
||||
if (this.RestPath == null)
|
||||
{
|
||||
string contentType;
|
||||
this.RestPath = FindMatchingRestPath(httpMethod, pathInfo, new NullLogger(), out contentType);
|
||||
|
||||
if (contentType != null)
|
||||
ResponseContentType = contentType;
|
||||
}
|
||||
return this.RestPath;
|
||||
}
|
||||
|
||||
public RestPath RestPath { get; set; }
|
||||
|
||||
// Set from SSHHF.GetHandlerForPathInfo()
|
||||
public string ResponseContentType { get; set; }
|
||||
|
||||
public async Task ProcessRequestAsync(IRequest httpReq, IResponse httpRes, string operationName)
|
||||
{
|
||||
var appHost = ServiceStackHost.Instance;
|
||||
|
||||
var restPath = GetRestPath(httpReq.Verb, httpReq.PathInfo);
|
||||
if (restPath == null)
|
||||
{
|
||||
throw new NotSupportedException("No RestPath found for: " + httpReq.Verb + " " + httpReq.PathInfo);
|
||||
}
|
||||
httpReq.SetRoute(restPath);
|
||||
|
||||
if (ResponseContentType != null)
|
||||
httpReq.ResponseContentType = ResponseContentType;
|
||||
|
||||
var request = httpReq.Dto = CreateRequest(httpReq, restPath);
|
||||
|
||||
appHost.ApplyRequestFilters(httpReq, httpRes, request);
|
||||
|
||||
var rawResponse = await ServiceStackHost.Instance.ServiceController.Execute(request, httpReq).ConfigureAwait(false);
|
||||
|
||||
var response = await HandleResponseAsync(rawResponse).ConfigureAwait(false);
|
||||
|
||||
appHost.ApplyResponseFilters(httpReq, httpRes, response);
|
||||
|
||||
await httpRes.WriteToResponse(httpReq, response).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public static object CreateRequest(IRequest httpReq, RestPath restPath)
|
||||
{
|
||||
var dtoFromBinder = GetCustomRequestFromBinder(httpReq, restPath.RequestType);
|
||||
if (dtoFromBinder != null)
|
||||
return dtoFromBinder;
|
||||
|
||||
var requestParams = httpReq.GetFlattenedRequestParams();
|
||||
return CreateRequest(httpReq, restPath, requestParams);
|
||||
}
|
||||
|
||||
public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams)
|
||||
{
|
||||
var requestDto = CreateContentTypeRequest(httpReq, restPath.RequestType, httpReq.ContentType);
|
||||
|
||||
return CreateRequest(httpReq, restPath, requestParams, requestDto);
|
||||
}
|
||||
|
||||
public static object CreateRequest(IRequest httpReq, RestPath restPath, Dictionary<string, string> requestParams, object requestDto)
|
||||
{
|
||||
string contentType;
|
||||
var pathInfo = !restPath.IsWildCardPath
|
||||
? GetSanitizedPathInfo(httpReq.PathInfo, out contentType)
|
||||
: httpReq.PathInfo;
|
||||
|
||||
return restPath.CreateRequest(pathInfo, requestParams, requestDto);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used in Unit tests
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public object CreateRequest(IRequest httpReq, string operationName)
|
||||
{
|
||||
if (this.RestPath == null)
|
||||
throw new ArgumentNullException("No RestPath found");
|
||||
|
||||
return CreateRequest(httpReq, this.RestPath);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ServiceStack.Host
|
||||
{
|
||||
public class ServiceMetadata
|
||||
{
|
||||
public ServiceMetadata()
|
||||
{
|
||||
this.OperationsMap = new Dictionary<Type, Type>();
|
||||
}
|
||||
|
||||
public Dictionary<Type, Type> OperationsMap { get; protected set; }
|
||||
|
||||
public void Add(Type serviceType, Type requestType, Type responseType)
|
||||
{
|
||||
this.OperationsMap[requestType] = serviceType;
|
||||
}
|
||||
|
||||
public Type GetServiceTypeByRequest(Type requestType)
|
||||
{
|
||||
Type serviceType;
|
||||
OperationsMap.TryGetValue(requestType, out serviceType);
|
||||
return serviceType;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MediaBrowser.Model.Logging;
|
||||
using MediaBrowser.Model.Services;
|
||||
using ServiceStack.Host;
|
||||
|
||||
namespace ServiceStack
|
||||
{
|
||||
public class HttpHandlerFactory
|
||||
{
|
||||
// Entry point for HttpListener
|
||||
public static RestHandler GetHandler(IHttpRequest httpReq, ILogger logger)
|
||||
{
|
||||
var pathInfo = httpReq.PathInfo;
|
||||
|
||||
var pathParts = pathInfo.TrimStart('/').Split('/');
|
||||
if (pathParts.Length == 0)
|
||||
{
|
||||
logger.Error("Path parts empty for PathInfo: {0}, Url: {1}", pathInfo, httpReq.RawUrl);
|
||||
return null;
|
||||
}
|
||||
|
||||
string contentType;
|
||||
var restPath = RestHandler.FindMatchingRestPath(httpReq.HttpMethod, pathInfo, logger, out contentType);
|
||||
|
||||
if (restPath != null)
|
||||
{
|
||||
return new RestHandler
|
||||
{
|
||||
RestPath = restPath,
|
||||
RequestName = restPath.RequestType.GetOperationName(),
|
||||
ResponseContentType = contentType
|
||||
};
|
||||
}
|
||||
|
||||
logger.Error("Could not find handler for {0}", pathInfo);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,127 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MediaBrowser.Model.Services;
|
||||
using ServiceStack.Host;
|
||||
|
||||
namespace ServiceStack
|
||||
{
|
||||
public static class HttpRequestExtensions
|
||||
{
|
||||
/**
|
||||
*
|
||||
Input: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?q=item#fragment
|
||||
|
||||
Some HttpRequest path and URL properties:
|
||||
Request.ApplicationPath: /Cambia3
|
||||
Request.CurrentExecutionFilePath: /Cambia3/Temp/Test.aspx
|
||||
Request.FilePath: /Cambia3/Temp/Test.aspx
|
||||
Request.Path: /Cambia3/Temp/Test.aspx/path/info
|
||||
Request.PathInfo: /path/info
|
||||
Request.PhysicalApplicationPath: D:\Inetpub\wwwroot\CambiaWeb\Cambia3\
|
||||
Request.QueryString: /Cambia3/Temp/Test.aspx/path/info?query=arg
|
||||
Request.Url.AbsolutePath: /Cambia3/Temp/Test.aspx/path/info
|
||||
Request.Url.AbsoluteUri: http://localhost:96/Cambia3/Temp/Test.aspx/path/info?query=arg
|
||||
Request.Url.Fragment:
|
||||
Request.Url.Host: localhost
|
||||
Request.Url.LocalPath: /Cambia3/Temp/Test.aspx/path/info
|
||||
Request.Url.PathAndQuery: /Cambia3/Temp/Test.aspx/path/info?query=arg
|
||||
Request.Url.Port: 96
|
||||
Request.Url.Query: ?query=arg
|
||||
Request.Url.Scheme: http
|
||||
Request.Url.Segments: /
|
||||
Cambia3/
|
||||
Temp/
|
||||
Test.aspx/
|
||||
path/
|
||||
info
|
||||
* */
|
||||
|
||||
/// <summary>
|
||||
/// Duplicate Params are given a unique key by appending a #1 suffix
|
||||
/// </summary>
|
||||
public static Dictionary<string, string> GetRequestParams(this IRequest request)
|
||||
{
|
||||
var map = new Dictionary<string, string>();
|
||||
|
||||
foreach (var name in request.QueryString.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
|
||||
var values = request.QueryString.GetValues(name);
|
||||
if (values.Length == 1)
|
||||
{
|
||||
map[name] = values[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
map[name + (i == 0 ? "" : "#" + i)] = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((request.Verb == HttpMethods.Post || request.Verb == HttpMethods.Put)
|
||||
&& request.FormData != null)
|
||||
{
|
||||
foreach (var name in request.FormData.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
|
||||
var values = request.FormData.GetValues(name);
|
||||
if (values.Length == 1)
|
||||
{
|
||||
map[name] = values[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
map[name + (i == 0 ? "" : "#" + i)] = values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Duplicate params have their values joined together in a comma-delimited string
|
||||
/// </summary>
|
||||
public static Dictionary<string, string> GetFlattenedRequestParams(this IRequest request)
|
||||
{
|
||||
var map = new Dictionary<string, string>();
|
||||
|
||||
foreach (var name in request.QueryString.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
map[name] = request.QueryString[name];
|
||||
}
|
||||
|
||||
if ((request.Verb == HttpMethods.Post || request.Verb == HttpMethods.Put)
|
||||
&& request.FormData != null)
|
||||
{
|
||||
foreach (var name in request.FormData.Keys)
|
||||
{
|
||||
if (name == null) continue; //thank you ASP.NET
|
||||
map[name] = request.FormData[name];
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
public static void SetRoute(this IRequest req, RestPath route)
|
||||
{
|
||||
req.Items["__route"] = route;
|
||||
}
|
||||
|
||||
public static RestPath GetRoute(this IRequest req)
|
||||
{
|
||||
object route;
|
||||
req.Items.TryGetValue("__route", out route);
|
||||
return route as RestPath;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue