private void ProcessContext(HttpListenerContext context)
{
string path, rawUrl = context.Request.RawUrl;
if (rawUrl == "")
return;
string[] splitUrl = rawUrl.Split('?');
string rawMessage;
RestRequest request = null;
// Path 가져오기
path = splitUrl[0].ToLower();
if (path.Length == 0)
return;
if (path.Length > 1 && path[path.Length - 1] == '/')
path = path.Remove(path.Length - 1);
// Query / Message Body 가져오기
if (context.Request.HttpMethod == "GET")
{
if (splitUrl.Length > 1)
{
rawMessage = splitUrl[1];
request = new RestRequest(HttpMethodType.Get, rawUrl, path, rawMessage);
}
else
request = new RestRequest(HttpMethodType.Get, rawUrl, path, "");
}
if (context.Request.HttpMethod == "POST")
{
using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
{
rawMessage = reader.ReadToEnd();
request = new RestRequest(HttpMethodType.Post, rawUrl, path, rawMessage);
}
}
// Routing
RequestHandler handler;
if (request == null)
return;
using (_lock.ReaderLock)
{
if (_routes.TryGetValue(path, out handler) == false)
return;
}
SpinWorker.Work(() =>
{
handler(request, context.Response);
});
}