Ejemplo n.º 1
0
        private void ProcessContext(HttpListenerContext context)
        {
            String path, rawUrl = context.Request.RawUrl;

            if (rawUrl == "")
            {
                return;
            }


            String[]      splitUrl = rawUrl.Split('?');
            String        rawMessage;
            WebAPIRequest 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 WebAPIRequest(WebMethodType.Get, rawUrl, path, rawMessage);
                }
                else
                {
                    request = new WebAPIRequest(WebMethodType.Get, rawUrl, path, "");
                }
            }
            if (context.Request.HttpMethod == "POST")
            {
                using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
                {
                    rawMessage = reader.ReadToEnd();
                    request    = new WebAPIRequest(WebMethodType.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);
            });
        }
Ejemplo n.º 2
0
        private void ProcessContext(HttpListenerContext context)
        {
            String path, rawUrl = context.Request.RawUrl;
            if (rawUrl == "")
                return;


            String[] splitUrl = rawUrl.Split('?');
            String rawMessage;
            WebAPIRequest 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 WebAPIRequest(WebMethodType.Get, rawUrl, path, rawMessage);
                }
                else
                    request = new WebAPIRequest(WebMethodType.Get, rawUrl, path, "");
            }
            if (context.Request.HttpMethod == "POST")
            {
                using (var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding))
                {
                    rawMessage = reader.ReadToEnd();
                    request = new WebAPIRequest(WebMethodType.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);
            });
        }