Ejemplo n.º 1
0
        public void Test_HexStringConversion()
        {
            const string complex = "http://github.com/+123%$";

            var s = TextConversion.ToEscapedHexString(complex);

            Assert.AreEqual("http%3A%2F%2Fgithub%2Ecom%2F%2B123%25%24", s);

            s = TextConversion.FromEscapedHexString(s);

            Assert.AreEqual(complex, s);
        }
 public static string ToEscapedString(object value)
 {
     if (value is string)
     {
         return(TextConversion.ToEscapedHexString
                (
                    (string)value,
                    c => !Char.IsLetterOrDigit(c) &&
                    c != '.' &&
                    c != '_'
                ));
     }
     else
     {
         return(ToString(value));
     }
 }
        protected override void UnhandledExceptionFromSingleCommand(Exception e)
        {
            try
            {
                Predicate <char> isEscapeChar = (c) => c == '\n' || c == '\"' || c == '\r';

                if (e is DirectoryNodeNotFoundException)
                {
                    WriteTextBlock("{0} CODE={1} URI=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.DIRECTORY_NOT_FOUND, ((NodeNotFoundException)e).NodeAddress.Uri);

                    Flush();
                }
                else if (e is FileNodeNotFoundException)
                {
                    WriteTextBlock("{0} CODE={1} URI=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.FILE_NOT_FOUND, ((NodeNotFoundException)e).NodeAddress.Uri);

                    Flush();
                }
                else if (e is NodeNotFoundException)
                {
                    WriteTextBlock("{0} CODE={1} URI=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.FILE_NOT_FOUND, ((NodeNotFoundException)e).NodeAddress.Uri);

                    Flush();
                }
                else if (e is ObjectDisposedException)
                {
                    this.RunLevel = DisconnectedRunLevel.Default;
                }
                else if (e is IOException)
                {
                    WriteTextBlock("{0} CODE={1} DETAILS=\"{2}: {3}\"",
                                   ResponseCodes.ERROR, ErrorCodes.IO_ERROR, e.GetType().Name, TextConversion.ToEscapedHexString(e.ToString(), isEscapeChar));

                    Flush();
                }
                else if (e is FileSystemServerException)
                {
                    WriteTextBlock("{0} CODE={1} DETAILS=\"{2}\"",
                                   ResponseCodes.ERROR, ((FileSystemServerException)e).ErrorCode, TextConversion.ToEscapedHexString(e.ToString(), isEscapeChar));

                    Flush();
                }
                else if (e is CommandNotSupportedException)
                {
                    WriteTextBlock("{0} CODE={1} DETAILS=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.INVALID_COMMAND, TextConversion.ToEscapedHexString(e.ToString(), isEscapeChar));

                    Flush();
                }
                else if (e is ProcessNextCommandException)
                {
                    return;
                }
                else if (e is MalformedUriException)
                {
                    WriteTextBlock("{0} CODE={1} DETAILS=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.MALFORMED_URI, TextConversion.ToEscapedHexString(e.ToString(), isEscapeChar));

                    Flush();
                }
                else if (e is NotSupportedException)
                {
                    WriteTextBlock("{0} CODE={1} DETAILS=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.NOT_SUPPORTED, TextConversion.ToEscapedHexString(e.ToString(), isEscapeChar));

                    Flush();
                }
                else if (e is UnauthorizedAccessException)
                {
                    WriteTextBlock("{0} CODE={1} DETAILS=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.UNAUTHORISED, TextConversion.ToEscapedHexString(e.ToString(), isEscapeChar));

                    Flush();
                }
                else if (e is DisconnectedException)
                {
                    RunLevel = DisconnectedRunLevel.Default;
                }
                else
                {
                    WriteTextBlock("{0} CODE={1} DETAILS=\"{2}\"",
                                   ResponseCodes.ERROR, ErrorCodes.UNEXPECTED, TextConversion.ToEscapedHexString(e.ToString(), isEscapeChar));

                    Flush();

                    RunLevel = DisconnectedRunLevel.Default;
                }
            }
            catch (IOException)
            {
                RunLevel = DisconnectedRunLevel.Default;
            }
        }
        public virtual void WriteError(string errorCode, string messageFormat, params object[] args)
        {
            var s = String.Format(messageFormat, args);

            if (s.Length > 0)
            {
                WriteTextBlock("{0} CODE={1} MESSAGE=\"{2}\"", ResponseCodes.ERROR, errorCode, TextConversion.ToEscapedHexString(s));
            }
            else
            {
                WriteTextBlock("{0} CODE={1}", ResponseCodes.ERROR, errorCode);
            }

            Flush();
        }
Ejemplo n.º 5
0
        public override void Process(Command command)
        {
            var count   = 0;
            var options = this.LoadOptions <CommandOptions>((TextCommand)command);
            var dir     = this.Connection.FileSystemManager.ResolveDirectory(options.Uri);

            dir.Refresh();

            if (options.Regex != null)
            {
                options.Regex = TextConversion.FromEscapedHexString(options.Regex);
            }

            if (!dir.Exists)
            {
                throw new DirectoryNodeNotFoundException(dir.Address);
            }

            Connection.WriteOk();

            if (options.IncludeAttributes)
            {
                Exception       exception = null;
                InvocationQueue queue;

                if (t_InvocationQueue == null)
                {
                    t_InvocationQueue = new InvocationQueue();
                }

                queue = t_InvocationQueue;

                queue.TaskAsynchronisity = TaskAsynchronisity.AsyncWithSystemPoolThread;

                queue.Start();

                try
                {
                    IEnumerable <INode> children = null;

                    if (String.IsNullOrEmpty(options.Regex))
                    {
                        children = dir.GetChildren();
                    }
                    else
                    {
                        children = dir.GetChildren(RegexBasedNodePredicateHelper.New(options.Regex));
                    }

                    foreach (var node in children)
                    {
                        var enclosedNode = node;

                        if (queue.TaskState == TaskState.Stopped)
                        {
                            break;
                        }

                        queue.Enqueue
                        (
                            delegate
                        {
                            try
                            {
                                if (enclosedNode.NodeType == NodeType.Directory)
                                {
                                    Connection.WriteTextPartialBlock("d:");
                                }
                                else
                                {
                                    Connection.WriteTextPartialBlock("f:");
                                }

                                Connection.WriteTextBlock(TextConversion.ToEscapedHexString(enclosedNode.Name, TextConversion.IsStandardUrlEscapedChar));

                                AttributesCommands.PrintAttributes(this.Connection, enclosedNode);

                                count++;

                                if (count % 15 == 0)
                                {
                                    Connection.Flush();
                                }
                            }
                            catch (Exception e)
                            {
                                queue.Stop();

                                exception = e;
                            }
                        }
                        );
                    }

                    if (queue.TaskState == TaskState.Stopped)
                    {
                        throw exception;
                    }
                }
                finally
                {
                    queue.Enqueue(queue.Stop);

                    queue.WaitForAnyTaskState(value => value != TaskState.Running);

                    queue.Reset();

                    Connection.Flush();
                }
            }
            else
            {
                IEnumerable <string> children;

                if (string.IsNullOrEmpty(options.Regex))
                {
                    children = dir.GetChildNames(NodeType.Directory);
                }
                else
                {
                    children = dir.GetChildNames(NodeType.Directory, PredicateUtils.NewRegex(options.Regex));
                }

                foreach (var name in children)
                {
                    Connection.WriteTextPartialBlock("d:");
                    Connection.WriteTextBlock(TextConversion.ToEscapedHexString(name, TextConversion.IsStandardUrlEscapedChar));

                    count++;

                    if (count % 15 == 0)
                    {
                        Connection.Flush();
                    }
                }

                count = 0;

                if (string.IsNullOrEmpty(options.Regex))
                {
                    children = dir.GetChildNames(NodeType.File);
                }
                else
                {
                    children = dir.GetChildNames(NodeType.File, PredicateUtils.NewRegex(options.Regex));
                }

                foreach (var name in children)
                {
                    Connection.WriteTextPartialBlock("f:");
                    Connection.WriteTextBlock(TextConversion.ToEscapedHexString(name));

                    count++;

                    if (count % 15 == 0)
                    {
                        Connection.Flush();
                    }
                }
            }

            Connection.Flush();
        }
        public static void Parse(string uri, out string scheme, out string userName,
                                 out string password, out string serverName, out int?port, out string parentUri, out string path, out string query)
        {
            int y;
            var x = uri.IndexOf("://", StringComparison.Ordinal);

            if (x < 0)
            {
                throw new MalformedUriException(uri);
            }

            if (x == uri.Length - 1)
            {
                throw new MalformedUriException(uri);
            }

            scheme = uri.Substring(0, x);

            x = ParseNetworkPart(uri, x + 3, out userName, out password, out serverName, out port);

            if (uri[x] == '/')
            {
                x++;
            }

            if (x >= uri.Length || (x < uri.Length && uri[x] != '['))
            {
                y         = x;
                parentUri = "";
            }
            else
            {
                y = ParseParentUri(uri, x);

                parentUri = uri.Substring(x + 1, y - x - 1);

                if (Local.LocalNodeAddress.CanParse(parentUri))
                {
                    if (parentUri.IndexOf("://") < 0)
                    {
                        parentUri = "file://" + TextConversion.ToEscapedHexString(parentUri, TextConversion.IsStandardUrlEscapedChar);
                    }
                    else
                    {
                        parentUri = TextConversion.ToReEscapedHexString(parentUri, TextConversion.IsStandardUrlEscapedChar);
                    }
                }
                else
                {
                    parentUri = TextConversion.ToReEscapedHexString(parentUri, TextConversion.IsStandardUrlEscapedChar);
                }
            }

            if (y + 1 >= uri.Length)
            {
                path  = "/";
                query = "";
            }
            else
            {
                if (uri[y + 1] != FileSystemManager.SeperatorChar)
                {
                    throw new MalformedUriException();
                }

                path = uri.Substring(y + 1);

                if ((x = path.IndexOf('?')) >= 0)
                {
                    query = path.Substring(x + 1);
                    path  = path.Substring(0, x);
                }
                else
                {
                    query = "";
                }
            }
        }
        public override IEnumerable <NetworkFileSystemEntry> ListAttributes(string uri, string regex)
        {
            Predicate <string> acceptName = null;

            using (this.AcquireCommandContext(false))
            {
                try
                {
                    try
                    {
                        if (!String.IsNullOrEmpty(regex))
                        {
                            try
                            {
                                SendCommand(DefaultTryCount, @"list -a -regex=""{0}"" ""{1}""", TextConversion.ToEscapedHexString(regex), uri).ProcessError();
                            }
                            catch (TextNetworkProtocolErrorResponseException)
                            {
                                ReadReady();

                                SendCommand(DefaultTryCount, @"list -a ""{1}""", regex, uri).ProcessError();

                                acceptName = PredicateUtils.NewRegex(regex);
                            }
                        }
                        else
                        {
                            SendCommand(DefaultTryCount, @"list -a ""{0}""", uri).ProcessError();
                        }
                    }
                    catch
                    {
                        ReadReady();

                        throw;
                    }
                }
                catch (TextNetworkProtocolException)
                {
                    this.connected = false;

                    throw;
                }
                catch (IOException)
                {
                    this.connected = false;

                    throw;
                }

                var enumerator = TextNetworkProtocol.ReadEntries(this.ReadNextLine).GetEnumerator();

                try
                {
                    for (; ;)
                    {
                        try
                        {
                            if (!enumerator.MoveNext())
                            {
                                break;
                            }
                        }
                        catch (TextNetworkProtocolException)
                        {
                            this.connected = false;

                            throw;
                        }
                        catch (IOException)
                        {
                            this.connected = false;

                            throw;
                        }

                        if (enumerator.Current.Right != null)
                        {
                            CommandResponse response;

                            response = ParseResponse(enumerator.Current.Right);

                            response.ProcessError();
                        }

                        if (acceptName != null)
                        {
                            if (acceptName(enumerator.Current.Left.Name))
                            {
                                yield return(enumerator.Current.Left);
                            }
                        }
                        else
                        {
                            yield return(enumerator.Current.Left);
                        }
                    }
                }
                finally
                {
                    enumerator.Dispose();
                }
            }
        }
        public override IEnumerable <Pair <string, NodeType> > List(string uri, string regex)
        {
            Predicate <string> acceptName = null;

            using (this.AcquireCommandContext(false))
            {
                try
                {
                    try
                    {
                        if (!String.IsNullOrEmpty(regex))
                        {
                            SendCommand(DefaultTryCount, @"list -regex=""{0}"" ""{1}""", TextConversion.ToEscapedHexString(regex), uri).ProcessError();
                        }
                        else
                        {
                            SendCommand(DefaultTryCount, @"list ""{0}""", uri).ProcessError();

                            acceptName = PredicateUtils.NewRegex(regex);
                        }
                    }
                    catch
                    {
                        ReadReady();

                        throw;
                    }
                }
                catch (TextNetworkProtocolException)
                {
                    this.connected = false;

                    throw;
                }
                catch (IOException)
                {
                    this.connected = false;

                    throw;
                }

                for (; ;)
                {
                    string   line;
                    NodeType currentNodeType;
                    Pair <string, string> currentFile;

                    try
                    {
                        line = TextConversion.FromEscapedHexString(ReadNextLine());
                    }
                    catch (TextNetworkProtocolException)
                    {
                        this.connected = false;

                        throw;
                    }
                    catch (IOException)
                    {
                        this.connected = false;

                        throw;
                    }

                    if (line.EqualsIgnoreCase(ResponseCodes.READY))
                    {
                        break;
                    }

                    currentFile = line.SplitAroundFirstCharFromLeft(':');

                    currentFile.Right = TextConversion.FromEscapedHexString(currentFile.Right);

                    currentNodeType = TextNetworkProtocol.GetNodeType(currentFile.Left);

                    if (currentNodeType == null || currentFile.Right.Length == 0)
                    {
                        continue;
                    }

                    if (acceptName != null)
                    {
                        if (acceptName(currentFile.Right))
                        {
                            yield return(new Pair <string, NodeType>(currentFile.Right, currentNodeType));
                        }
                    }
                    else
                    {
                        yield return(new Pair <string, NodeType>(currentFile.Right, currentNodeType));
                    }
                }
            }
        }
Ejemplo n.º 9
0
 public static string UrlEncode(string instring)
 {
     return(TextConversion.ToEscapedHexString(instring, TextConversion.IsStandardUrlEscapedChar));
 }