Beispiel #1
0
        private DreamMessage GetFile(DreamContext context)
        {
            DreamMessage message;

            string[] parts    = context.GetSuffixes(UriPathFormat.Decoded);
            string   filename = _resourcesPath;

            foreach (string part in parts)
            {
                if (part.EqualsInvariant(".."))
                {
                    _log.WarnFormat("attempted to access file outside of target folder: {0}", string.Join("/", parts));
                    throw new DreamBadRequestException("paths cannot contain '..'");
                }
                filename = Path.Combine(filename, part);
            }
            try {
                message = DreamMessage.FromFile(filename, context.Verb == Verb.HEAD);
            } catch (FileNotFoundException e) {
                message = DreamMessage.NotFound("resource not found: " + String.Join("/", context.GetSuffixes(UriPathFormat.Decoded)));
            } catch (Exception e) {
                message = DreamMessage.BadRequest("invalid path");
            }
            return(message);
        }
Beispiel #2
0
        public Yield GetFileHandler(DreamContext context, DreamMessage request, Result<DreamMessage> response)
        {
            string suffixPath = string.Join("" + Path.DirectorySeparatorChar, context.GetSuffixes(UriPathFormat.Decoded));
            string filename = Path.Combine(_path, suffixPath);
            if(Directory.Exists(filename)) {
                XDoc ret = new XDoc("files");
                string pattern = context.GetParam("pattern", "");
                AddDirectories(new DirectoryInfo(filename), pattern, ret);
                AddFiles(new DirectoryInfo(filename), pattern, ret);
                response.Return(DreamMessage.Ok(ret));
                yield break;
            }

            DreamMessage message;
            try {
                message = DreamMessage.FromFile(filename, StringUtil.EqualsInvariant(context.Verb, "HEAD"));
            } catch(FileNotFoundException) {
                message = DreamMessage.NotFound("file not found");
            } catch(Exception) {
                message = DreamMessage.BadRequest("invalid path");
            }

            // open file and stream it to the requester
            response.Return(message);
        }
Beispiel #3
0
        public Yield GetFileHandler(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            string suffixPath = string.Join("" + Path.DirectorySeparatorChar, context.GetSuffixes(UriPathFormat.Decoded));
            string filename   = Path.Combine(_path, suffixPath);

            if (Directory.Exists(filename))
            {
                XDoc   ret     = new XDoc("files");
                string pattern = context.GetParam("pattern", "");
                AddDirectories(new DirectoryInfo(filename), pattern, ret);
                AddFiles(new DirectoryInfo(filename), pattern, ret);
                response.Return(DreamMessage.Ok(ret));
                yield break;
            }

            DreamMessage message;

            try {
                message = DreamMessage.FromFile(filename, StringUtil.EqualsInvariant(context.Verb, "HEAD"));
            } catch (FileNotFoundException) {
                message = DreamMessage.NotFound("file not found");
            } catch (Exception) {
                message = DreamMessage.BadRequest("invalid path");
            }

            // open file and stream it to the requester
            response.Return(message);
        }
        public Yield ProxyToService(DreamContext context, DreamMessage request, Result <DreamMessage> response)
        {
            PermissionsBL.IsUserAllowed(DekiContext.Current.User, Permissions.ADMIN);

            //Private feature requires api-key
            var identifier = context.GetParam("id");

            ServiceRepository.IServiceInfo serviceInfo = null;
            if (identifier.StartsWith("="))
            {
                serviceInfo = DekiContext.Current.Instance.RunningServices[XUri.Decode(identifier.Substring(1))];
            }
            else
            {
                uint serviceId;
                if (uint.TryParse(identifier, out serviceId))
                {
                    serviceInfo = DekiContext.Current.Instance.RunningServices[serviceId];
                }
                else
                {
                    throw new DreamBadRequestException(string.Format("Invalid id '{0}'", identifier));
                }
            }
            if (serviceInfo == null)
            {
                throw new ServiceNotFoundException(identifier);
            }
            var proxyUri = serviceInfo.ServiceUri.At(context.GetSuffixes(UriPathFormat.Original).Skip(1).ToArray());

            yield return(context.Relay(Plug.New(proxyUri), request, response));
        }
Beispiel #5
0
        private string GetPath(DreamContext context)
        {
            var parts = context.GetSuffixes(UriPathFormat.Decoded);

            if (parts.Where(p => p.EqualsInvariant("..")).Any())
            {
                throw new DreamBadRequestException("paths cannot contain '..'");
            }
            if (_privateRoot && (parts.Length == 0 || (parts.Length == 1 && !context.Uri.TrailingSlash)))
            {
                throw new DreamForbiddenException("Root level access is forbidden for this storage service");
            }
            return(context.Uri.GetRelativePathTo(context.Service.Self.Uri).IfNullOrEmpty("/"));
        }
Beispiel #6
0
        private string GetPath(DreamContext context)
        {
            string[] parts = context.GetSuffixes(UriPathFormat.Decoded);
            string   path  = _path;

            foreach (string part in parts)
            {
                if (part.EqualsInvariant(".."))
                {
                    throw new DreamBadRequestException("paths cannot contain '..'");
                }
                path = Path.Combine(path, part);
            }
            if (_privateRoot && (parts.Length == 0 || (parts.Length == 1 && !Directory.Exists(path))))
            {
                throw new DreamForbiddenException("Root level access is forbidden for this storage service");
            }
            return(path);
        }
Beispiel #7
0
 public Yield GetHttp(DreamContext context, DreamMessage request, Result<DreamMessage> response)
 {
     yield return context.Relay(_redirect.At(context.GetSuffixes(UriPathFormat.Original)), request, response);
 }
        public Yield ProxyToService(DreamContext context, DreamMessage request, Result<DreamMessage> response) {
            PermissionsBL.IsUserAllowed(DekiContext.Current.User, Permissions.ADMIN);

            //Private feature requires api-key
            var identifier = context.GetParam("id");
            ServiceRepository.IServiceInfo serviceInfo = null;
            if(identifier.StartsWith("=")) {
                serviceInfo = DekiContext.Current.Instance.RunningServices[XUri.Decode(identifier.Substring(1))];
            } else {
                uint serviceId;
                if(uint.TryParse(identifier, out serviceId)) {
                    serviceInfo = DekiContext.Current.Instance.RunningServices[serviceId];
                } else {
                    throw new DreamBadRequestException(string.Format("Invalid id '{0}'", identifier));
                }
            }
            if(serviceInfo == null) {
                response.Return(DreamMessage.NotFound(string.Format(DekiResources.SERVICE_NOT_FOUND, identifier)));
                yield break;
            }
            var proxyUri = serviceInfo.ServiceUri.At(context.GetSuffixes(UriPathFormat.Original).Skip(1).ToArray());

            yield return context.Relay(Plug.New(proxyUri), request, response);
        }
 private DreamMessage GetFile(DreamContext context) {
     DreamMessage message;
     string[] parts = context.GetSuffixes(UriPathFormat.Decoded);
     string filename = _resourcesPath;
     foreach(string part in parts) {
         if(part.EqualsInvariant("..")) {
             _log.WarnFormat("attempted to access file outside of target folder: {0}", string.Join("/", parts));
             throw new DreamBadRequestException("paths cannot contain '..'");
         }
         filename = Path.Combine(filename, part);
     }
     try {
         message = DreamMessage.FromFile(filename, context.Verb == Verb.HEAD);
     } catch(FileNotFoundException e) {
         message = DreamMessage.NotFound("resource not found: " + String.Join("/", context.GetSuffixes(UriPathFormat.Decoded)));
     } catch(Exception e) {
         message = DreamMessage.BadRequest("invalid path");
     }
     return message;
 }
Beispiel #10
0
 public Yield GetFiles(DreamContext context, DreamMessage request, Result<DreamMessage> response) {
     if(_resourcesUri != null) {
         yield return context.Relay(Plug.New(_resourcesUri).AtPath(string.Join("/", context.GetSuffixes(UriPathFormat.Original))), request, response);
     } else {
         response.Return(GetFile(context));
     }
     yield break;
 }
Beispiel #11
0
 public Yield GetHttp(DreamContext context, DreamMessage request, Result <DreamMessage> response)
 {
     yield return(context.Relay(_redirect.At(context.GetSuffixes(UriPathFormat.Original)), request, response));
 }
Beispiel #12
0
 public Yield PostPageMessage(DreamContext context, DreamMessage request, Result<DreamMessage> response) {
     if(UserBL.IsAnonymous(DekiContext.Current.User)) {
         response.Return(DreamMessage.Forbidden("A logged-in user is required"));
         yield break;
     }
     if(request.ContentLength > 128 * 1024) {
         response.Return(DreamMessage.BadRequest("Content-length cannot exceed 128KB)"));
         yield break;
     }
     PageBE page = PageBL_AuthorizePage(context, null, Permissions.READ, false);
     XDoc body = new XDoc("body");
     switch(request.ContentType.FullType) {
     case "text/plain":
         body.Attr("content-type", request.ContentType.ToString())
             .Value(request.AsText());
         break;
     default:
         body.Attr("content-type", request.ContentType.ToString())
             .Add(request.ToDocument());
         break;
     }
     string[] path = context.GetSuffixes(UriPathFormat.Original);
     path = ArrayUtil.SubArray(path, 1);
     DekiContext.Current.Instance.EventSink.PageMessage(DekiContext.Current.Now, page, DekiContext.Current.User, body, path);
     response.Return(DreamMessage.Ok());
     yield break;
 }
Beispiel #13
0
 private string GetPath(DreamContext context)
 {
     string[] parts = context.GetSuffixes(UriPathFormat.Decoded);
     string path = _path;
     foreach(string part in parts) {
         if(part.EqualsInvariant("..")) {
             throw new DreamBadRequestException("paths cannot contain '..'");
         }
         path = Path.Combine(path, part);
     }
     if(_privateRoot && (parts.Length == 0 || (parts.Length == 1 && !Directory.Exists(path)))) {
         throw new DreamForbiddenException("Root level access is forbidden for this storage service");
     }
     return path;
 }
Beispiel #14
0
 public Yield GetFiles(DreamContext context, DreamMessage request, Result <DreamMessage> response)
 {
     if (_resourcesUri != null)
     {
         yield return(context.Relay(Plug.New(_resourcesUri).AtPath(string.Join("/", context.GetSuffixes(UriPathFormat.Original))), request, response));
     }
     else
     {
         response.Return(GetFile(context));
     }
     yield break;
 }
Beispiel #15
0
 private string GetPath(DreamContext context)
 {
     var parts = context.GetSuffixes(UriPathFormat.Decoded);
     if(parts.Where(p => p.EqualsInvariant("..")).Any()) {
         throw new DreamBadRequestException("paths cannot contain '..'");
     }
     if(_privateRoot && (parts.Length == 0 || (parts.Length == 1 && !context.Uri.TrailingSlash))) {
         throw new DreamForbiddenException("Root level access is forbidden for this storage service");
     }
     return context.Uri.GetRelativePathTo(context.Service.Self.Uri).IfNullOrEmpty("/");
 }