<commit_before>/*
 * Copyright (c) 2018 <NAME>
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 * 
 * https://github.com/piranhacms/piranha.core
 * 
 */

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Piranha.AspNetCore.Services;
using Piranha.Web;
using X.Web.Sitemap;

namespace Piranha.AspNetCore
{
    public class SitemapMiddleware : MiddlewareBase
    {
        /// <summary>
        /// Creates a new middleware instance.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline</param>
        /// <param name="factory">The logger factory</param>
        public SitemapMiddleware(RequestDelegate next, ILoggerFactory factory = null) : base(next, factory) { }

        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <param name="api">The current api</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context, IApi api, IApplicationService service)
        {
            if (!IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url = context.Request.Path.HasValue ? context.Request.Path.Value : "";
                var host = context.Request.Host.Host;
                var scheme = context.Request.Scheme;
                var port = context.Request.Host.Port;
                var baseUrl = scheme + "://" + host + (port.HasValue ? $":{port}" : "");

                if (url.ToLower() == "/sitemap.xml")
                {
                    if (_logger != null)
                        _logger.LogInformation($"Sitemap.xml requested, generating");

                    // Get the requested site by hostname
                    var siteId = service.Site.Id;

                    // Get the sitemap for the site
                    var pages = api.Sites.GetSitemap(siteId);

                    // Generate sitemap.xml
                    var sitemap = new Sitemap();

                    foreach (var page in pages)
                    {
                        var urls = GetPageUrls(api, page, baseUrl);

                        if (urls.Count > 0)
                            sitemap.AddRange(urls);
                    }<commit_msg>Added ContentType to Sitemap.xml. Fixes #350<commit_after>/*
 * Copyright (c) 2018 <NAME>
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 * 
 * https://github.com/piranhacms/piranha.core
 * 
 */

using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Piranha.AspNetCore.Services;
using Piranha.Web;
using X.Web.Sitemap;

namespace Piranha.AspNetCore
{
    public class SitemapMiddleware : MiddlewareBase
    {
        /// <summary>
        /// Creates a new middleware instance.
        /// </summary>
        /// <param name="next">The next middleware in the pipeline</param>
        /// <param name="factory">The logger factory</param>
        public SitemapMiddleware(RequestDelegate next, ILoggerFactory factory = null) : base(next, factory) { }

        /// <summary>
        /// Invokes the middleware.
        /// </summary>
        /// <param name="context">The current http context</param>
        /// <param name="api">The current api</param>
        /// <returns>An async task</returns>
        public override async Task Invoke(HttpContext context, IApi api, IApplicationService service)
        {
            if (!IsHandled(context) && !context.Request.Path.Value.StartsWith("/manager/assets/"))
            {
                var url = context.Request.Path.HasValue ? context.Request.Path.Value : "";
                var host = context.Request.Host.Host;
                var scheme = context.Request.Scheme;
                var port = context.Request.Host.Port;
                var baseUrl = scheme + "://" + host + (port.HasValue ? $":{port}" : "");

                if (url.ToLower() == "/sitemap.xml")
                {
                    if (_logger != null)
                        _logger.LogInformation($"Sitemap.xml requested, generating");

                    // Get the requested site by hostname
                    var siteId = service.Site.Id;

                    // Get the sitemap for the site
                    var pages = api.Sites.GetSitemap(siteId);

                    // Generate sitemap.xml
                    var sitemap = new Sitemap();

                    foreach (var page in pages)
                    {
                        var urls = GetPageUrls(api, page, baseUrl);

                        if (urls.Count > 0)
                            sitemap.AddRange(urls);
                    }
                    context.Response.ContentType = "application/xml";
{'fraction_non_alphanumeric': 0.11329569787813899, 'fraction_numerical': 0.002530659918240218, 'mean_word_length': 1.7564377682403434, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7381616', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1328, 'n_words': 351}