public HttpResponse Handle(HttpRequest request) { var path = Path.Combine(_baseDirectory, request.Url.TrimStart('/')); var fileInfo = new FileInfo(path); if (!fileInfo.FullName.StartsWith(_baseDirectory)) { return new HttpResponse(HttpStatusCode.Forbidden); } if (File.Exists(path)) { var response = new HttpResponse(HttpStatusCode.OK, File.ReadAllBytes(path)); var fileExtension = Path.GetExtension(path); if (_mimeTypes.ContainsKey(fileExtension)) { response.AddHeader("Content-Type", _mimeTypes[fileExtension]); } return response; } if (_next != null) { return _next.Handle(request); } return new HttpResponse(HttpStatusCode.NotFound); }
protected HttpResponse Json(HttpStatusCode statusCode, object obj) { //var body = JsonConvert.SerializeObject(obj) var body = JsonSerializer.Serialize(obj); var response = new HttpResponse(statusCode, body); response.AddHeader("Content-Type", "application/json; charset=utf-8"); return response; }