private static HttpResponseBase OnServeFiles(ITcpChannel channel, HttpRequestBase request) { //do not do anything, lib will handle it. if (request.Uri.AbsolutePath.StartsWith("/cqs")) { return(null); } var response = request.CreateResponse(); var uriWithoutTrailingSlash = request.Uri.AbsolutePath.TrimEnd('/'); var path = Debugger.IsAttached ? Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\public\\", uriWithoutTrailingSlash)) : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "public\\", uriWithoutTrailingSlash); if (path.EndsWith("\\")) { path = Path.Combine(path, "index.html"); } if (!File.Exists(path)) { response.StatusCode = 404; return(response); } var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var extension = Path.GetExtension(path).TrimStart('.'); response.ContentType = ApacheMimeTypes.Apache.MimeTypes[extension]; response.Body = stream; return(response); }
private bool RespondWithRaw(Stream body, int statusCode, string mimeType, Dictionary <string, string> headers = null) { ITcpChannel _channel; lock (lockObject) { if (channel == null) { return(false); } _channel = channel; channel = null; } try { IHttpResponse response = request.CreateResponse(); request = null; response.ContentLength = (int)body.Length; response.StatusCode = statusCode; response.ContentType = mimeType; if (currentUser != null) { response.AddHeader("X-User-Name", currentUser.Value.name); response.AddHeader("X-User-Readonly", currentUser.Value.readOnly ? "Yes" : "No"); } response.AddHeader("Access-Control-Allow-Origin", "*"); response.AddHeader("Access-Control-Allow-Headers", "Authorization"); response.AddHeader("Access-Control-Allow-Methods", "POST, HEAD, PUT, DELETE, GET, OPTIONS"); if (headers != null) { foreach (KeyValuePair <string, string> kvp in headers) { response.AddHeader(kvp.Key, kvp.Value); } } body.Position = 0; response.Body = body; _channel.Send(response); } catch { return(false); } return(true); }
private bool AuthenticateUser(ITcpChannel channel, HttpRequestBase request) { if (channel.Data["Principal"] != null) { Thread.CurrentPrincipal = (IPrincipal)channel.Data["Principal"]; return(true); } try { var user = Authenticator.Authenticate(request); if (user == null) { return(true); } if (PrincipalFactory != null) { var ctx = new PrincipalFactoryContext(request, user); Thread.CurrentPrincipal = PrincipalFactory.Create(ctx); channel.Data["Principal"] = Thread.CurrentPrincipal; return(true); } var roles = user as IUserWithRoles; if (roles == null) { throw new InvalidOperationException( "You must specify a PrincipalFactory if you do not return a IUserWithRoles from your IAccountService."); } Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user.Username), roles.RoleNames); channel.Data["Principal"] = Thread.CurrentPrincipal; } catch (HttpException ex) { var response = request.CreateResponse(); response.StatusCode = ex.HttpCode; response.ReasonPhrase = FirstLine(ex.Message); ; channel.Send(response); return(false); } return(true); }