<commit_before>﻿using System;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.PreprocessRequest;
using Sitecore.Text;
using Sitecore.Web;

namespace Cognifide.PowerShell.Core.Processors
{
    public class RewriteUrl : PreprocessRequestProcessor
    {
        private static int GetVersion(string path)
        {
            int num;
            Assert.ArgumentNotNull(path, "path");
            string str = path.TrimStart(new[] {'/'}).Split(new[] {'/'})[2];
            Assert.IsTrue(str.StartsWith("v"), "Version token is wrong.");
            Assert.IsTrue(int.TryParse(str.Replace("v", string.Empty), out num), "Version not recognized.");
            return num;
        }

        public override void Process(PreprocessRequestArgs arguments)
        {
            Assert.ArgumentNotNull(arguments, "arguments");
            try
            {
                string localPath = arguments.Context.Request.Url.LocalPath;
                if (localPath.StartsWith("/-/script/v1"))
                {
                    Assert.ArgumentNotNull(arguments.Context, "context");
                    Uri url = arguments.Context.Request.Url;
                    string[] sourceArray = url.LocalPath.TrimStart('/').Split('/');
                    if (sourceArray.Length < 3)
                    {
                        return;
                    }
                    int length = sourceArray.Length - 3;
                    var destinationArray = new string[length];
                    Array.Copy(sourceArray, 3, destinationArray, 0, length);
                    string scriptPath = string.Format("/{0}", string.Join("/", destinationArray));
                    string query = url.Query.TrimStart('?');
                    query += string.Format("{0}script={1}", string.IsNullOrEmpty(query) ? "?" : "&", scriptPath);
                    WebUtil.RewriteUrl(
                        new UrlString { Path = "/sitecore modules/PowerShell/Services/RemoteScriptCall.ashx", Query = query }.ToString());
                }
                if (localPath.StartsWith("/-/script/v2"))
                {
                    Assert.ArgumentNotNull(arguments.Context, "context");
                    Uri url = arguments.Context.Request.Url;
                    string[] sourceArray = url.LocalPath.TrimStart('/').Split('/');
                    if (sourceArray.Length < 4)
                    {
                        return;
                    }
                    int length = sourceArray.Length - 4;
                    var destinationArray = new string[length];
                    Array.Copy(sourceArray, 4, destinationArray, 0, length);
                    string scriptPath = string.Format("/{0}", string.Join("/", destinationArray));
                    string query = url.Query.TrimStart('?');
                    query += string.Format("{0}script={1}&sc_database={2}", string.IsNullOrEmpty(query) ? "?" : "&", scriptPath, sourceArray[3]);
                    WebUtil.RewriteUrl(
                        new UrlString { Path = "/sitecore modules/PowerShell/Services/RemoteScriptCall.ashx", Query = query }.ToString());
                }
            }
            catch (Exception exception)
            {
                Log.Error("Error during the SPE API call", exception);
            }
        }
    }
}<commit_msg>Issue #267 - redirect service calls from /Console/ to /sitecore modules/PowerShell/<commit_after>﻿using System;
using Sitecore.Diagnostics;
using Sitecore.Pipelines.PreprocessRequest;
using Sitecore.Text;
using Sitecore.Web;

namespace Cognifide.PowerShell.Core.Processors
{
    public class RewriteUrl : PreprocessRequestProcessor
    {
        private static int GetVersion(string path)
        {
            int num;
            Assert.ArgumentNotNull(path, "path");
            string str = path.TrimStart(new[] {'/'}).Split(new[] {'/'})[2];
            Assert.IsTrue(str.StartsWith("v"), "Version token is wrong.");
            Assert.IsTrue(int.TryParse(str.Replace("v", string.Empty), out num), "Version not recognized.");
            return num;
        }

        public override void Process(PreprocessRequestArgs arguments)
        {
            Assert.ArgumentNotNull(arguments, "arguments");
            try
            {
                Assert.ArgumentNotNull(arguments.Context, "context");
                Uri url = arguments.Context.Request.Url;
                string localPath = url.LocalPath;
                if (localPath.StartsWith("/Console/",StringComparison.OrdinalIgnoreCase))
                {
                    // this bit is for compatibility of solutions integrating with SPE 2.x services in mind
                    WebUtil.RewriteUrl(
                        new UrlString { Path = localPath.ToLowerInvariant().Replace("/console/","/sitecore modules/PowerShell/"), Query = url.Query }.ToString());
                }
                if (localPath.StartsWith("/-/script/v1",StringComparison.OrdinalIgnoreCase))
                {
                    string[] sourceArray = url.LocalPath.TrimStart('/').Split('/');
                    if (sourceArray.Length < 3)
                    {
                        return;
                    }
                    int length = sourceArray.Length - 3;
                    var destinationArray = new string[length];
                    Array.Copy(sourceArray, 3, destinationArray, 0, length);
                    string scriptPath = string.Format("/{0}", string.Join("/", destinationArray));
                    string query = url.Query.TrimStart('?');
                    query += string.Format("{0}script={1}", string.IsNullOrEmpty(query) ? "?" : "&", scriptPath);
                    WebUtil.RewriteUrl(
                        new UrlString { Path = "/sitecore modules/PowerShell/Services/RemoteScriptCall.ashx", Query = query }.ToString());
                }
                if (localPath.StartsWith("/-/script/v2", StringComparison.OrdinalIgnoreCase))
                {
                    string[] sourceArray = url.LocalPath.TrimStart('/').Split('/');
                    if (sourceArray.Length < 4)
                    {
                        return;
                    }
                    int length = sourceArray.Length - 4;
                    var destinationArray = new string[length];
                    Array.Copy(sourceArray, 4, destinationArray, 0, length);
                    string scriptPath = string.Format("/{0}", string.Join("/", destinationArray));
                    string query = url.Query.TrimStart('?');
                    query += string.Format("{0}script={1}&sc_database={2}", string.IsNullOrEmpty(query) ? "?" : "&", scriptPath, sourceArray[3]);
                    WebUtil.RewriteUrl(
                        new UrlString { Path = "/sitecore modules/PowerShell/Services/RemoteScriptCall.ashx", Query = query }.ToString());
                }
            }
            catch (Exception exception)
            {
                Log.Error("Error during the SPE API call", exception);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.12040323725685077, 'fraction_numerical': 0.005963367883004401, 'mean_word_length': 1.6441441441441442, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7562058', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1652, 'n_words': 354}