Exemplo n.º 1
0
        public Task Handle(IHttpContext context, Func <Task> next)
        {
            var uri = context.Request.QueryString.Any()
                ? null
                : string.Join("/", context.Request.Uri.OriginalString.Split('/').Skip(2));

            if (uri == null)
            {
                if (!context.Request.QueryString.TryGetByName("uri", out uri) || uri == null)
                {
                    context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "Invalid request",
                                                                      context.Request.Headers.KeepAliveConnection(),
                                                                      "You need to provide a <code>uri</code> parameter with the URL."
                                                                      );
                    return(Task.Factory.GetCompleted());
                }
            }

            _log.InfoFormat("Requesting user file {0}", uri);
            var task = _np.GetUserFile(uri);

            try
            {
                task.Wait();
            }
            catch
            {
                context.Response = HttpResponse.CreateWithMessage(HttpResponseCode.NotFound, "File not accessible",
                                                                  context.Request.Headers.KeepAliveConnection(),
                                                                  string.Format("<pre><tt><code>{0}</code></tt></pre>",
                                                                                task.Exception == null ? "Unknown error" : task.Exception.ToString())
                                                                  );
                return(Task.Factory.GetCompleted());
            }

            // Return file contents
            context.Response = new HttpResponse(HttpResponseCode.Ok, MimeMapping.GetMimeMapping(uri),
                                                new MemoryStream(task.Result), context.Request.Headers.KeepAliveConnection());

            return(Task.Factory.GetCompleted());
        }
Exemplo n.º 2
0
 public IndexHandler()
 {
     byte[] contents = Encoding.UTF8.GetBytes("Welcome to the Index.");
     _keepAliveResponse = new HttpResponse(HttpResponseCode.Ok, contents, true);
     _response = new HttpResponse(HttpResponseCode.Ok, contents, false);
 }