Esempio n. 1
0
        public HttpResponse Handle(HttpRq request)
        {
            String filePath = Environment.CurrentDirectory + "/" + request.Uri;
            if (!this.FileExists("C:\\", filePath, 3))
            {
                return new HttpResponse(request.ProtocolVersion, HttpStatusCode.NotFound, "File not found");
            }

            String fileContents = File.ReadAllText(filePath);
            var response = new HttpResponse(request.ProtocolVersion, HttpStatusCode.OK, fileContents);
            return response;
        }
Esempio n. 2
0
        private Controller CreateController(HttpRq request)
        {
            var controllerClassName = request.Action.ControllerName + "Controller";
            var type = Assembly.GetEntryAssembly().GetTypes()
                    .FirstOrDefault(x => x.Name.ToLower() == controllerClassName.ToLower() && typeof(Controller).IsAssignableFrom(x));

            if (type == null)
            {
                throw new HttpNotFound(string.Format("Controller with name {0} not found!", controllerClassName));
            }

            var instance = (Controller)Activator.CreateInstance(type, request);
            return instance;
        }
Esempio n. 3
0
        public HttpResponse GetResponse(string requestAsString)
        {
            HttpRq request;

            try
            {
                var requestParser = new HttpRq("GET", "/", "1.1");
                request = requestParser.Parse(requestAsString);
            }
            catch (Exception ex)
            {
                return new HttpResponse(new Version(1, 1), HttpStatusCode.BadRequest, ex.Message);
            }

            var response = this.Process(request);
            return response;
        }
Esempio n. 4
0
        private HttpResponse Process(HttpRq request)
        {
            if (request.Method.ToLower() == "options")
            {
                var routes = Assembly.GetEntryAssembly().GetTypes()
                        .Where(x => x.Name.EndsWith("Controller") && typeof(Controller).IsAssignableFrom(x))
                        .Select(x => new { x.Name, Methods = x.GetMethods().Where(m => m.ReturnType == typeof(IActionResult)) })
                        .SelectMany(x => x.Methods.Select(m => string.Format("/{0}/{1}/{{parameter}}", x.Name.Replace("Controller", string.Empty), m.Name)))
                        .ToList();

                return new HttpResponse(request.ProtocolVersion, HttpStatusCode.OK, string.Join(Environment.NewLine, routes));
            }
            else if (new StaticFileHandler().CanHandle(request))
            {
                return new StaticFileHandler().Handle(request);
            }
            else if (request.ProtocolVersion.Major <= 3)
            {
                HttpResponse response;

                try
                {
                    var controller = this.CreateController(request);
                    var actionInvoker = new NewActionInvoker();
                    var actionResult = actionInvoker.InvokeAction(controller, request.Action);
                    response = actionResult.GetResponse();
                }
                catch (HttpNotFound exception)
                {
                    response = new HttpResponse(request.ProtocolVersion, HttpStatusCode.NotFound, exception.Message);
                }
                catch (Exception exception)
                {
                    response = new HttpResponse(request.ProtocolVersion, HttpStatusCode.InternalServerError, exception.Message);
                }

                return response;
            }
            else
            {
                return new HttpResponse(request.ProtocolVersion, HttpStatusCode.NotImplemented, NotHandledRequest);
            }
        }
Esempio n. 5
0
 protected Controller(HttpRq r)
 {
     this.Request = r;
 }
Esempio n. 6
0
 public ApiController(HttpRq request)
     : base(request)
 {
 }
Esempio n. 7
0
 public HomeController(HttpRq request)
     : base(request)
 {
 }
Esempio n. 8
0
 public JsonActionResultWithCors(HttpRq request, object model, string corsSettings)
     : base(request, model)
 {
     this.ResponseHeaders.Add(new KeyValuePair<string, string>("Access-Control-Allow-Origin", corsSettings));
 }
Esempio n. 9
0
 public JsonActionResult(HttpRq rq, object m)
 {
     this.model = m;
     this.Request = rq;
     this.ResponseHeaders = new List<KeyValuePair<string, string>>();
 }
Esempio n. 10
0
 public bool CanHandle(HttpRq request)
 {
     return request.Uri.LastIndexOf(".", StringComparison.Ordinal) > request.Uri.LastIndexOf("/", StringComparison.Ordinal);
 }
Esempio n. 11
0
 public JsonActionResultWithoutCaching(HttpRq r, object model)
     : base(r, model)
 {
     this.ResponseHeaders.Add(new KeyValuePair<string, string>(CacheControl, Keys));
     throw new Exception();
 }
Esempio n. 12
0
 private void AddHeaderToRequest(HttpRq r, string headerLine)
 {
     var hp = headerLine.Split(new[] { ':' }, 2);
     var hn = hp[0].Trim();
     var hv = hp.Length == 2 ? hp[1].Trim() : string.Empty;
     r.AddHeader(hn, hv);
 }
Esempio n. 13
0
        private HttpRq CreateRequest(string frl)
        {
            var firstRequestLineParts = frl.Split(' ');

            if (firstRequestLineParts.Length != 3)
            {
                throw new HttpNotFound.ParserException(
                    "Invalid format for the first request line. Expected format: [Method] [Uri] HTTP/[Version]");
            }

            var requestObject = new HttpRq(
                firstRequestLineParts[0],
                firstRequestLineParts[1],
                firstRequestLineParts[2]);

            return requestObject;
        }