protected override RequestResponse <Result> DeserializeMessage(ResponseBodyStream data)
        {
            if (data.OperationResult != OperationResult.Ok)
            {
                throw new Exception($"{nameof(ListRequest)} failed with result code {data.OperationResult}");
            }

            var res = new Result
            {
                OperationResult = data.OperationResult,
                Revision        = Revision.FromStream(data),
                FullPath        = _fullPath
            };

            if ((Options & Option.TotalSpace) != 0)
            {
                res.TotalSpace = data.ReadULong();
            }
            if ((Options & Option.UsedSpace) != 0)
            {
                res.UsedSpace = data.ReadULong();
            }

            res.FingerPrint = data.ReadBytesByLength();

            res.Item = Deserialize(data, _fullPath);


            return(new RequestResponse <Result>
            {
                Ok = data.OperationResult == OperationResult.Ok,
                Result = res
            });
        }
        protected override RequestResponse <Result> DeserializeMessage(ResponseBodyStream data)
        {
            switch (data.OperationResult)
            {
            case OperationResult.Ok:
                break;

            default:
                throw new Exception($"{nameof(SharedFoldersListRequest)} failed with result code {data.OperationResult}");
            }


            var res = new Result
            {
                OperationResult = data.OperationResult,
                Container       = new Dictionary <string, FsFolder>()
            };


            //var z = data.ReadAllBytes();
            //
            var opres = data.OperationResult; //data.ReadShort();

            switch (opres)
            {
            case 0:
                long cnt = data.ReadPu32();
                for (long j = 0; j < cnt; j++)
                {
                    var    treeId   = data.ReadTreeId();
                    string fullPath = data.ReadString();
                    res.Container[fullPath] = new FsFolder(fullPath, treeId, CloudFolderType.Shared, null, 0);
                    data.ReadULong();
                }
                break;

            default:
                throw new Exception($"{nameof(SharedFoldersListRequest)}: Unknown parse operation {opres}");
            }

            return(new RequestResponse <Result>
            {
                Ok = data.OperationResult == OperationResult.Ok,
                Result = res
            });
        }
Example #3
0
        protected override RequestResponse <Result> DeserializeMessage(ResponseBodyStream data)
        {
            switch (data.OperationResult)
            {
            case OperationResult.Ok:
                break;

            case OperationResult.NotExists:
                //throw new NotFoundException($"{nameof(ListRequest)} failed with result code {data.OperationResult}", HttpStatusCode.NotFound);
                throw new RequestException(_fullPath)
                      {
                          StatusCode = HttpStatusCode.NotFound
                      };

            default:
                throw new Exception($"{nameof(ListRequest)} failed with result code {data.OperationResult}");
            }


            var res = new Result
            {
                OperationResult = data.OperationResult,
                Revision        = Revision.FromStream(data),
                FullPath        = _fullPath
            };

            if ((Options & Option.TotalSpace) != 0)
            {
                res.TotalSpace = data.ReadULong();
            }
            if ((Options & Option.UsedSpace) != 0)
            {
                res.UsedSpace = data.ReadULong();
            }

            res.FingerPrint = data.ReadBytesByLength();
            res.Item        = Deserialize(data, _fullPath);

            return(new RequestResponse <Result>
            {
                Ok = data.OperationResult == OperationResult.Ok,
                Result = res
            });
        }
Example #4
0
        protected override RequestResponse <Result> DeserializeMessage(ResponseBodyStream data)
        {
            OpResult opres = (OpResult)(int)data.OperationResult;

            if (opres == OpResult.Ok)
            {
                return(new RequestResponse <Result>
                {
                    Ok = true,
                    Result = new Result
                    {
                        OperationResult = data.OperationResult,
                        Path = _fullPath
                    }
                });
            }

            throw new Exception($"{nameof(CreateFolderRequest)} failed with result code {opres}");
        }
        protected override RequestResponse <Result> DeserializeMessage(ResponseBodyStream data)
        {
            var opres = (OpResult)(int)data.OperationResult;

            if (opres == OpResult.Ok)
            {
                return(new RequestResponse <Result>
                {
                    Ok = true,
                    Result = new Result
                    {
                        OperationResult = data.OperationResult,
                        OneRevision = Revision.FromStream(data),
                        TwoRevision = Revision.FromStream(data)
                    }
                });
            }

            throw new Exception($"{nameof(MoveRequest)} failed with result code {opres}");
        }
Example #6
0
        protected override RequestResponse <Result> DeserializeMessage(ResponseBodyStream data)
        {
            var opres = (OpResult)(int)data.OperationResult;

            if (!SuccessCodes.Contains(opres))
            {
                throw new Exception($"{nameof(MobAddFileRequest)} failed with operation result code {opres} ({(int)opres})");
            }

            var res = new RequestResponse <Result>
            {
                Ok     = true,
                Result = new Result
                {
                    IsSuccess       = true,
                    OperationResult = data.OperationResult,
                    Path            = _fullPath
                }
            };

            return(res);
        }
Example #7
0
        private FsItem GetItem(ResponseBodyStream data, FsFolder folder)
        {
            FsItem item;
            TreeId treeId;

            int head = data.ReadIntSpl();

            if ((head & 4096) != 0)
            {
                byte[] nodeId = data.ReadNBytes(16);
            }
            string name = data.ReadNBytesAsString(data.ReadShort());

            data.ReadULong(); // dunno

            ulong?GetFolderSize() => (Options & Option.FolderSize) != 0
                ? (ulong?)data.ReadULong()
                : null;

            void ProcessDelete()
            {
                if ((Options & Option.Delete) != 0)
                {
                    data.ReadPu32();  // dunno
                    data.ReadPu32();  // dunno
                }
            }

            int opresult = head & 3;

            switch (opresult)
            {
            case 0:               // folder?
                treeId = data.ReadTreeId();
                data.ReadULong(); // dunno
                data.ReadULong(); // dunno
                ProcessDelete();

                item = new FsFolder(WebDavPath.Combine(folder.FullPath, name), treeId, CloudFolderType.MountPoint, folder, GetFolderSize());
                break;

            case 1:
                var    modifDate = data.ReadDate();
                ulong  size      = data.ReadULong();
                byte[] sha1      = data.ReadNBytes(20);

                //item = new FsFile(WebDavPath.Combine(folder.FullPath == string.Empty ? _fullPath : folder.FullPath, name), modifDate, sha1, size);
                item = new FsFile(WebDavPath.Combine(folder.FullPath, name), modifDate, sha1, size);
                break;

            case 2:
                data.ReadULong();     // dunno
                ProcessDelete();

                item = new FsFolder(WebDavPath.Combine(folder.FullPath, name), null, CloudFolderType.Generic, folder, GetFolderSize());
                break;

            case 3:
                data.ReadULong();     // dunno
                treeId = data.ReadTreeId();
                ProcessDelete();

                item = new FsFolder(WebDavPath.Combine(folder.FullPath, name), treeId, CloudFolderType.Shared, folder, GetFolderSize());
                break;

            default:
                throw new Exception("unknown opresult " + opresult);
            }

            return(item);
        }
Example #8
0
        private FsItem Deserialize(ResponseBodyStream data, string fullPath)
        {
            fullPath = WebDavPath.Clean(fullPath);

            var      fakeRoot      = new FsFolder(WebDavPath.Parent(fullPath), null, CloudFolderType.Generic, null, null);
            FsFolder currentFolder = fakeRoot;
            FsFolder lastFolder    = null;
            int      lvl           = 0;

            var parseOp = (ParseOp)data.ReadShort();

            while (parseOp != ParseOp.Done)
            {
                switch (parseOp)
                {
                case ParseOp.ReadItem:
                    break;

                case ParseOp.Pin:
                    if (lastFolder != null)
                    {
                        currentFolder = lastFolder;
                        lvl++;
                        parseOp = (ParseOp)data.ReadShort();
                        continue;
                    }
                    else
                    {
                        throw new Exception("lastFolder = null");
                    }

                case ParseOp.PinUpper:
                    if (currentFolder == fakeRoot)
                    {
                        parseOp = (ParseOp)data.ReadShort();
                        continue;
                    }
                    else if (currentFolder.Parent != null)
                    {
                        currentFolder = currentFolder.Parent;
                        lvl--;
                        parseOp = (ParseOp)data.ReadShort();
                        if (currentFolder == null)
                        {
                            throw new Exception("No parent folder A");
                        }
                        continue;
                    }
                    else
                    {
                        throw new Exception("No parent folder B");
                    }

                case ParseOp.Unknown15:
                    long skip = data.ReadPu32();
                    for (long i = 0; i < skip; i++)
                    {
                        data.ReadPu32();
                        data.ReadPu32();
                    }
                    break;

                default:
                    throw new Exception("Unknown parse operation");
                }
                FsItem item = GetItem(data, currentFolder);
                currentFolder.Items.Add(item);

                if (item is FsFolder fsFolder)
                {
                    lastFolder = fsFolder;
                    fsFolder.IsChildsLoaded = lvl < Depth;
                }

                parseOp = (ParseOp)data.ReadShort();
            }

            var res = fakeRoot.Items[0];

            return(res);
        }
Example #9
0
        private static Stream GetResponseBodyStream(IDictionary <string, string> headers, Uri uri, string[] splits, string method, AbstractHttpStream s)
        {
            string value;
            string compressedresult = string.Empty;

            if (headers.TryGetValue("content-encoding", out value) || headers.TryGetValue("x-content-encoding", out value))
            {
                /* Content-Encoding */
                /* x-gzip is deprecated but better feel safe about having that */
                if (value == "gzip" || value == "x-gzip")
                {
                    compressedresult = "gzip";
                }
                else if (value == "deflate")
                {
                    compressedresult = "deflate";
                }
                else
                {
                    throw new NotSupportedException("Unsupport content-encoding");
                }
            }

            if (splits[0] == "HTTP/1.0")
            {
                s.IsReusable = false;
            }

            if (headers.TryGetValue("connection", out value))
            {
                value = value.Trim().ToLower();
                if (value == "keep-alive")
                {
                    s.IsReusable = true;
                }
                else if (value == "close")
                {
                    s.IsReusable = false;
                }
            }

            if (method == "HEAD")
            {
                /* HEAD does not have any response data */
                return(new ResponseBodyStream(s, 0, uri.Scheme, uri.Host, uri.Port));
            }
            else if (headers.TryGetValue("content-length", out value))
            {
                long contentLength;
                if (!long.TryParse(value, out contentLength))
                {
                    throw new BadHttpResponseException("Unparseable length in response");
                }
                Stream bs = new ResponseBodyStream(s, contentLength, uri.Scheme, uri.Host, uri.Port);
                if (headers.TryGetValue("transfer-encoding", out value) && value == "chunked")
                {
                    bs = new HttpReadChunkedBodyStream(bs);
                }
                if (compressedresult.Length != 0)
                {
                    if (compressedresult == "gzip")
                    {
                        bs = new GZipStream(bs, CompressionMode.Decompress);
                    }
                    else if (compressedresult == "deflate")
                    {
                        bs = new DeflateStream(bs, CompressionMode.Decompress);
                    }
                }
                return(bs);
            }
            else
            {
                Stream bs = s;
                if (headers.TryGetValue("transfer-encoding", out value) && value == "chunked")
                {
                    bs = new BodyResponseChunkedStream(s, uri.Scheme, uri.Host, uri.Port);
                }
                else
                {
                    s.IsReusable = false;
                }
                if (compressedresult.Length != 0)
                {
                    if (compressedresult == "gzip")
                    {
                        bs = new GZipStream(bs, CompressionMode.Decompress);
                    }
                    else if (compressedresult == "deflate")
                    {
                        bs = new DeflateStream(bs, CompressionMode.Decompress);
                    }
                }
                return(bs);
            }
        }
        private FsItem Deserialize(ResponseBodyStream data, string fullPath)
        {
            var      fakeFolder    = new FsFolder(string.Empty, null, CloudFolderType.Generic, null, null);
            FsFolder currentFolder = fakeFolder;
            FsFolder lastFolder    = null;

            int itemStart = data.ReadShort();

            while (itemStart != 0)
            {
                switch (itemStart)
                {
                case 1:
                    break;

                case 2:
                    if (lastFolder != null)
                    {
                        currentFolder = lastFolder;
                        itemStart     = data.ReadShort();
                        continue;
                    }
                    else
                    {
                        throw new Exception("lastFolder = null");
                    }

                case 3:
                    if (currentFolder == fakeFolder)
                    {
                        itemStart = data.ReadShort();
                        continue;
                    }
                    else if (currentFolder.Parent != null)
                    {
                        currentFolder = currentFolder.Parent;
                        if (currentFolder == null)
                        {
                            throw new Exception("No parent folder");
                        }

                        continue;
                    }
                    else
                    {
                        throw new Exception("No parent folder");
                    }

                case 15:
                    var skip = data.ReadPu32();
                    for (int i = 0; i < skip; i++)
                    {
                        data.ReadPu32();
                        data.ReadPu32();
                    }
                    break;

                default:
                    throw new Exception("Unknown itemStart");
                }
                FsItem item = GetItem(data, currentFolder);
                currentFolder.Items.Add(item);

                if (item is FsFolder fsFolder)
                {
                    lastFolder = fsFolder;
                }

                itemStart = data.ReadShort();
            }

            var res = fakeFolder.Items[0];

            return(res);
        }