private void HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
        {
            HttpDynvokeResponse dynResp = null;

            string[] parts = request.Url.AbsolutePath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (request.Url.AbsolutePath.StartsWith("/generated.js"))
            {
                dynResp = GeneratedJS;
            }
            else if (parts.Length != 2)
            {
                dynResp = BAD_REQUEST;
            }
            else
            {
                HttpDynvokeRequest dynReq = new HttpDynvokeRequest();

                dynReq.Controller  = parts [0];
                dynReq.Action      = parts [1];
                dynReq.RequestBody = Uri.UnescapeDataString(new StreamReader(request.InputStream, request.ContentEncoding).ReadToEnd());

                dynResp = this.HttpDynvoke.HandleRequest(dynReq);
            }

            response.StatusCode  = dynResp.StatusCode;
            response.ContentType = dynResp.ContentType;
            response.AddHeader("Content-Length", dynResp.ResponseBody.length);

            using (StreamWriter writer = new StreamWriter(response.OutputStream))
                writer.Write(dynResp.ResponseBody);
        }
Exemple #2
0
        public HttpDynvokeResponse HandleRequest(HttpDynvokeRequest request)
        {
            DateTime requestStart = DateTime.Now;

            try
            {
                string json = request.RequestBody;

                JsonParamProvider jsonParams = new JsonParamProvider(json);

                DynvokeMethod method = this.Dynvoke.PrepTarget(request.Controller, request.Action, jsonParams);

                if (method == null)
                {
                    return(NOT_FOUND);
                }

                if (!method.ReadyToCall)
                {
                    return(MISSING_ARGUMENTS);
                }

                object returnObj = method.Call();
                string outputStr = "";

                if (method.ReturnType != typeof(void))
                {
                    outputStr = JsonConvert.SerializeObject(returnObj);
                    Log.Debug("Called method, returned: [" + outputStr + "]");
                }
                else
                {
                    Log.Debug("Called method, void return");
                }

                return(new HttpDynvokeResponse(200, outputStr, "application/json"));
            }
            catch (Exception e)
            {
                Log.Error("Exception thrown while handling request!\n" + e.ToString());
                return(SERVER_ERROR);
            }
            finally
            {
                Log.Debug("Total response time in [" + DateTime.Now.Subtract(requestStart).TotalMilliseconds + "] ms");
            }
        }