private async Task ProcessContainer(HttpContext context, PathString remainingPath)
        {
            lock (Globals.ConsoleLock)
                Console.WriteLine($"{nameof(remainingPath)}: {remainingPath}");

            string[] pathParts = remainingPath.Value.Split(Characters.UrlPathSeparator, StringSplitOptions.RemoveEmptyEntries);

            if (pathParts.Length < 2)
            {
                context.Response.StatusCode = 400;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Container: return {context.Response.StatusCode}");
                return;
            }

            string id = pathParts[0];

            lock (Globals.ConsoleLock)
                Console.WriteLine($"{nameof(id)}: {id}");

            int packageIndex = MasterData.FindPackageIndex(id);

            if (packageIndex < 0)
            {
                context.Response.StatusCode = 404;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Container: return {context.Response.StatusCode}");
                return;
            }

            if (string.Equals(pathParts[1], "index.json", StringComparison.OrdinalIgnoreCase))
            {
                await ProcessContainerIndex(context, packageIndex);
            }
            else
            {
                Version version;
                if (Version.TryParse(pathParts[1], out version))
                {
                    await ProcessContainerVersion(context, packageIndex, version.Normalize());
                }
            }
        }
        private async Task ProcessContainerVersion(HttpContext context, int packageIndex, Version version)
        {
            lock (Globals.ConsoleLock)
                Console.WriteLine($"ProcessContainerVersion [version: {version}]");

            byte[] content = MasterData.Use(packageIndex, version, (packages, contentFacade, _packageIndex, _version) =>
            {
                VersionInfo[] versions = packages[_packageIndex].Versions;

                for (int i = 0; i < versions.Length; i++)
                {
                    if (versions[i].Version == _version)
                    {
                        byte[] result;
                        if (contentFacade.TryGetValue(versions[i].ContentId, out result))
                        {
                            return(result);
                        }
                    }
                }

                return(null);
            });

            if (content != null)
            {
                context.Response.StatusCode  = 200;
                context.Response.ContentType = "application/octet-stream";
                await context.Response.Body.WriteAsync(content, 0, content.Length);

                return;
            }

            context.Response.StatusCode = 404;
            lock (Globals.ConsoleLock)
                Console.WriteLine($"ContainerVersion: return {context.Response.StatusCode}");
        }
Esempio n. 3
0
        private async Task ProcessDownload(HttpContext context, string downloadPath)
        {
            byte[] content = MasterData.Use(downloadPath, (packageInfo, contentFacade, _downloadPath) =>
            {
                byte[] _content;
                if (contentFacade.TryGetValue(_downloadPath.TrimStart(Characters.UrlPathSeparator), out _content))
                {
                    return(_content);
                }
                return(null);
            });

            if (content == null)
            {
                context.Response.StatusCode = 404;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Download: return {context.Response.StatusCode}");
                return;
            }

            context.Response.StatusCode  = 200;
            context.Response.ContentType = "application/octet-stream";
            await context.Response.Body.WriteAsync(content, 0, content.Length);
        }
Esempio n. 4
0
        private async Task FindPackage(HttpContext context)
        {
            var strings = context.Request.Query["id"];

            if (strings.Count == 0)
            {
                context.Response.StatusCode = 400;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Find: return {context.Response.StatusCode}");
                return;
            }

            var id = strings[0].Trim(Characters.SingleQuote);

            if (id.Length == 0)
            {
                context.Response.StatusCode = 400;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Find: return {context.Response.StatusCode}");
                return;
            }

            VersionInfo[] versions = MasterData.Use(id, (packages, _, _id) =>
            {
                foreach (var packageInfo in packages)
                {
                    if (string.Equals(packageInfo.Id, _id, StringComparison.OrdinalIgnoreCase))
                    {
                        return(packageInfo.Versions);
                    }
                }
                return(null);
            });

            if (versions == null)
            {
                versions = Globals.EmptyVersionInfoArray;
            }
            else
            {
                lock (Globals.ConsoleLock)
                {
                    Console.WriteLine();
                    Console.WriteLine("--- Find -------------------------------------");
                    foreach (var v in versions)
                    {
                        Console.WriteLine($"{id} - {v.Version}");
                    }
                    Console.WriteLine("----------------------------------------------");
                    Console.WriteLine();
                }
            }

            var doc = new XElement(
                XmlElements.feed,
                new XAttribute(XmlElements.baze, XmlNamespaces.baze),
                new XAttribute(XmlElements.m, XmlNamespaces.m),
                new XAttribute(XmlElements.d, XmlNamespaces.d),
                new XAttribute(XmlElements.georss, XmlNamespaces.georss),
                new XAttribute(XmlElements.gml, XmlNamespaces.gml),
                new XElement(XmlElements.m_count, versions.Length.ToString()),
                versions.Select(x =>
                                new XElement(
                                    XmlElements.entry,
                                    new XElement(XmlElements.id, $"{context.Request.Scheme}://{context.Request.Host}{Segment}/Packages(Id='{id}',Version='{x.Version}')"),
                                    new XElement(
                                        XmlElements.content,
                                        new XAttribute("type", "application/zip"),
                                        new XAttribute("src", $"{context.Request.Scheme}://{context.Request.Host}{Segment}/download/{x.ContentId}")
                                        ),
                                    new XElement(
                                        XmlElements.m_properties,
                                        new XElement(XmlElements.d_id, id),
                                        new XElement(XmlElements.d_version, x.Version)
                                        )
                                    )
                                )
                );

            versions = null;

            var bytes = Encoding.UTF8.GetBytes(doc.ToString(SaveOptions.DisableFormatting));

            context.Response.StatusCode  = 200;
            context.Response.ContentType = "application/xml";
            await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
        }
Esempio n. 5
0
        private async Task ProcessPackages(HttpContext context)
        {
            var path = Uri.UnescapeDataString(context.Request.Path.Value);

            int start = path.IndexOf('(');

            if (start == -1)
            {
                context.Response.StatusCode = 400;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Packages: return {context.Response.StatusCode}");
                return;
            }

            start++;

            int end = path.IndexOf(')', start);

            if (end == -1)
            {
                context.Response.StatusCode = 400;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Packages: return {context.Response.StatusCode}");
                return;
            }

            string  id      = null;
            Version version = null;

            var parts = path.Substring(start, end - start).Split(Characters.Coma);

            foreach (var part in parts)
            {
                var kv = part.Split(new[] { '=' }, 2);

                if (string.Equals(kv[0], "id", StringComparison.OrdinalIgnoreCase))
                {
                    id = kv[1].Trim(Characters.SingleQuote);
                }
                else if (string.Equals(kv[0], "version", StringComparison.OrdinalIgnoreCase))
                {
                    Version.TryParse(kv[1].Trim(Characters.SingleQuote), out version);
                }
            }

            if (string.IsNullOrWhiteSpace(id) || version == null)
            {
                context.Response.StatusCode = 400;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Packages: return {context.Response.StatusCode}");
                return;
            }

            version = version.Normalize();

            string contentId = MasterData.FindContentId(id, version);

            if (contentId == null)
            {
                context.Response.StatusCode = 404;
                lock (Globals.ConsoleLock)
                    Console.WriteLine($"Packages: return {context.Response.StatusCode}");
                return;
            }

            var doc = new XElement(XmlElements.entry,
                                   new XAttribute(XmlElements.baze, XmlNamespaces.baze),
                                   new XAttribute(XmlElements.m, XmlNamespaces.m),
                                   new XAttribute(XmlElements.d, XmlNamespaces.d),
                                   new XAttribute(XmlElements.georss, XmlNamespaces.georss),
                                   new XAttribute(XmlElements.gml, XmlNamespaces.gml),
                                   new XElement(XmlElements.id, $"{context.Request.Scheme}://{context.Request.Host}{Segment}/Packages(Id='{id}',Version='{version}')"),
                                   new XElement(
                                       XmlElements.content,
                                       new XAttribute("type", "application/zip"),
                                       new XAttribute("src", $"{context.Request.Scheme}://{context.Request.Host}{Segment}/download/{contentId}")
                                       ),
                                   new XElement(
                                       XmlElements.m_properties,
                                       new XElement(XmlElements.d_id, id),
                                       new XElement(XmlElements.d_version, version)
                                       )
                                   );

            byte[] bytes = Encoding.UTF8.GetBytes(doc.ToString(SaveOptions.DisableFormatting));

            context.Response.StatusCode  = 200;
            context.Response.ContentType = "application/xml";
            await context.Response.Body.WriteAsync(bytes, 0, bytes.Length);
        }
Esempio n. 6
0
 private Task ProcessSearch(HttpContext context)
 {
     return(MasterData.Use(context, ProcessSearchSafe));
 }