<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using IronRuby.Builtins;
using IronRuby.StandardLibrary.StringIO;

namespace IronRuby.Rack.Handler {
    public class IIS {

        public readonly Application App;
        
        private static IIS _current;
        public  static IIS  Current { get { return _current; } }

        private IIS(Application app) {
            App = app;
        }

        public static void Run(Application app) {
            Run(app, new Dictionary<string, string>());
        }

        public static void Run(Application app, 
            IDictionary<string, string> options) {
            // TODO: make sure IIS is running

            if (_current == null) {
                 _current = new IIS(app);
            }
        }

        public void Handle(Request request, Response response) {

            var handle_scope = RubyEngine.Engine.CreateScope(); 
            handle_scope.SetVariable("__request", request);
            handle_scope.SetVariable("__response", response);

            // The environment must be an true instance of Hash (no subclassing
            // allowed) that includes CGI-like headers. The application is free 
            // to modify the environment. The environment is required to 
            // include these variables (adopted from PEP333), except when 
            // they’d be empty, but see below.
            var env = new Hash(RubyEngine.Context);

            // REQUEST_METHOD:	The HTTP request method, such as “GET” or 
            //   “POST”. This cannot ever be an empty string, and so is always
            //   required.
            // The REQUEST_METHOD must be a valid token.

            env["REQUEST_METHOD"] = request.OrigionalRequest.RequestType;

            // SCRIPT_NAME:	The initial portion of the request URL’s “path” 
            //   that corresponds to the application object, so that the 
            //   application knows its virtual “location”. This may be an empty
            //   string, if the application corresponds to the “root” of the 
            //   server.
            // The SCRIPT_NAME, if non-empty, must start with /
            
            env["SCRIPT_NAME"] = request.OrigionalRequest.Path;
            if(env["SCRIPT_NAME"].ToString() != string.Empty && !((string)env["SCRIPT_NAME"]).StartsWith("/")) {
                env["SCRIPT_NAME"] = "/" + env["SCRIPT_NAME"];
            }
            
            // SCRIPT_NAME never should be /, but instead be empty.

            if(env["SCRIPT_NAME"].ToString() == "/") 
                env["SCRIPT_NAME"] = "";
            
            // PATH_INFO:	The remainder of the request URL’s “path”, 
            //   designating the virtual “location” of the request’s target 
            //   within the application. This may be an empty string, if the 
            //   request URL targets the application root and does not have a 
            //   trailing slash. This value may be percent-encoded when I 
            //   originating from a URL.
            // The PATH_INFO, if non-empty, must start with /
           
            env["PATH_INFO"] = request.OrigionalRequest.PathInfo;
            if(env["PATH_INFO"].ToString() != string.Empty && !((string)env["PATH_INFO"]).StartsWith("/")) {
                env["PATH_INFO"] = "/" + env["PATH_INFO"];
            }
            
            // PATH_INFO should be / if SCRIPT_NAME is empty. 
            
            if(env["SCRIPT_NAME"].ToString() == string.Empty) 
                env["PATH_INFO"] = "/";

            // One of SCRIPT_NAME or PATH_INFO must be set.

            // TODO

            // QUERY_STRING:	The portion of the request URL that follows the
            //   ?, if any. May be empty, but is always required!
            
            env["QUERY_STRING"] = request.QueryString ?? "";

            // SERVER_NAME, SERVER_PORT:	When combined with SCRIPT_NAME and
            //   PATH_INFO, these variables can be used to complete the URL. 
            //   Note, however, that HTTP_HOST, if present, should be used in
            //   preference to SERVER_NAME for reconstructing the request URL. 
            //   SERVER_NAME and SERVER_PORT can never be empty strings, and so
            //   are always required.
            
            env["SERVER_NAME"] = "";
            env["SERVER_PORT"] = "";
            
            // HTTP_ Variables:	Variables corresponding to the client-supplied 
            //   HTTP request headers (i.e., variables whose names begin with 
            //   HTTP_). The presence or absence of these variables should 
            //   correspond with the presence or absence of the appropriate 
            //   HTTP header in the request.
            // The CONTENT_LENGTH, if given, must consist of digits only.
            
            foreach (var pair in request.Headers) {
                var prepend = "HTTP_";
                if (pair.Key.ToString() == "Content-Length" || pair.Key.ToString() == "Content-Type")
                    prepend = "";
                env[prepend + pair.Key.ToString().ToUpper().Replace("-", "_")] = pair.Value;
            }

            // In addition to this, the Rack environment must include these 
            // Rack-specific variables:
            //
            // rack.version:	The Array [1,0], representing this version of 
            //   Rack.
            
            env["rack.version"] = new int[]{1,0};
            
            // rack.url_scheme:	http or https, depending on the request URL.<commit_msg>Server name and server port (cherry picked from commit <PASSWORD>)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using IronRuby.Builtins;
using IronRuby.StandardLibrary.StringIO;

namespace IronRuby.Rack.Handler {
    public class IIS {

        public readonly Application App;
        
        private static IIS _current;
        public  static IIS  Current { get { return _current; } }

        private IIS(Application app) {
            App = app;
        }

        public static void Run(Application app) {
            Run(app, new Dictionary<string, string>());
        }

        public static void Run(Application app, 
            IDictionary<string, string> options) {
            // TODO: make sure IIS is running

            if (_current == null) {
                 _current = new IIS(app);
            }
        }

        public void Handle(Request request, Response response) {

            var handle_scope = RubyEngine.Engine.CreateScope(); 
            handle_scope.SetVariable("__request", request);
            handle_scope.SetVariable("__response", response);

            // The environment must be an true instance of Hash (no subclassing
            // allowed) that includes CGI-like headers. The application is free 
            // to modify the environment. The environment is required to 
            // include these variables (adopted from PEP333), except when 
            // they’d be empty, but see below.
            var env = new Hash(RubyEngine.Context);

            // REQUEST_METHOD:	The HTTP request method, such as “GET” or 
            //   “POST”. This cannot ever be an empty string, and so is always
            //   required.
            // The REQUEST_METHOD must be a valid token.

            env["REQUEST_METHOD"] = request.OrigionalRequest.RequestType;

            // SCRIPT_NAME:	The initial portion of the request URL’s “path” 
            //   that corresponds to the application object, so that the 
            //   application knows its virtual “location”. This may be an empty
            //   string, if the application corresponds to the “root” of the 
            //   server.
            // The SCRIPT_NAME, if non-empty, must start with /
            
            env["SCRIPT_NAME"] = request.OrigionalRequest.Path;
            if(env["SCRIPT_NAME"].ToString() != string.Empty && !((string)env["SCRIPT_NAME"]).StartsWith("/")) {
                env["SCRIPT_NAME"] = "/" + env["SCRIPT_NAME"];
            }
            
            // SCRIPT_NAME never should be /, but instead be empty.

            if(env["SCRIPT_NAME"].ToString() == "/") 
                env["SCRIPT_NAME"] = "";
            
            // PATH_INFO:	The remainder of the request URL’s “path”, 
            //   designating the virtual “location” of the request’s target 
            //   within the application. This may be an empty string, if the 
            //   request URL targets the application root and does not have a 
            //   trailing slash. This value may be percent-encoded when I 
            //   originating from a URL.
            // The PATH_INFO, if non-empty, must start with /
           
            env["PATH_INFO"] = request.OrigionalRequest.PathInfo;
            if(env["PATH_INFO"].ToString() != string.Empty && !((string)env["PATH_INFO"]).StartsWith("/")) {
                env["PATH_INFO"] = "/" + env["PATH_INFO"];
            }
            
            // PATH_INFO should be / if SCRIPT_NAME is empty. 
            
            if(env["SCRIPT_NAME"].ToString() == string.Empty) 
                env["PATH_INFO"] = "/";

            // One of SCRIPT_NAME or PATH_INFO must be set.

            // TODO

            // QUERY_STRING:	The portion of the request URL that follows the
            //   ?, if any. May be empty, but is always required!
            
            env["QUERY_STRING"] = request.QueryString ?? "";

            // SERVER_NAME, SERVER_PORT:	When combined with SCRIPT_NAME and
            //   PATH_INFO, these variables can be used to complete the URL. 
            //   Note, however, that HTTP_HOST, if present, should be used in
            //   preference to SERVER_NAME for reconstructing the request URL. 
            //   SERVER_NAME and SERVER_PORT can never be empty strings, and so
            //   are always required.
            
            env["SERVER_NAME"] = request.OrigionalRequest.Url.Host;
            env["SERVER_PORT"] = request.OrigionalRequest.Url.Port;
            
            // HTTP_ Variables:	Variables corresponding to the client-supplied 
            //   HTTP request headers (i.e., variables whose names begin with 
            //   HTTP_). The presence or absence of these variables should 
            //   correspond with the presence or absence of the appropriate 
            //   HTTP header in the request.
            // The CONTENT_LENGTH, if given, must consist of digits only.
            
            foreach (var pair in request.Headers) {
                var prepend = "HTTP_";
                if (pair.Key.ToString() == "Content-Length" || pair.Key.ToString() == "Content-Type")
                    prepend = "";
                env[prepend + pair.Key.ToString().ToUpper().Replace("-", "_")] = pair.Value;
            }

            // In addition to this, the Rack environment must include these 
            // Rack-specific variables:
            //
            // rack.version:	The Array [1,0], representing this version of 
            //   Rack.
            
            env["rack.version"] = new int[]{1,0};
            
            // rack.url_scheme:	http or https, depending on the request URL.
{'fraction_non_alphanumeric': 0.09706167828465544, 'fraction_numerical': 0.0012353304508956147, 'mean_word_length': 1.6769012753897024, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '6793877', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2771, 'n_words': 962}