<commit_before>using UnityEngine;
using System;
using System.IO;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Linq;
using System.Threading;

namespace CUDLR {

  public class RequestContext
  {
    public HttpListenerContext context;
    public Match match;
    public bool pass;
    public string path;
    public int currentRoute;

    public HttpListenerRequest Request { get { return context.Request; } }
    public HttpListenerResponse Response { get { return context.Response; } }

    public RequestContext(HttpListenerContext ctx)
    {
      context = ctx;
      match = null;
      pass = false;
      path = WWW.UnEscapeURL(context.Request.Url.AbsolutePath);
      if (path == "/")
        path = "/index.html";
      currentRoute = 0;
    }
  }


  public class Server : MonoBehaviour {

    [SerializeField]
    public int Port = 55055;

    [SerializeField]
    public bool RegisterLogCallback = false;

    private static Thread mainThread;
    private static string fileRoot;
    private static HttpListener listener = new HttpListener();
    private static List<RouteAttribute> registeredRoutes;
    private static Queue<RequestContext> mainRequests = new Queue<RequestContext>();

    // List of supported files
    // FIXME add an api to register new types
    private static Dictionary<string, string> fileTypes = new Dictionary<string, string> {
      {"js",   "application/javascript"},
      {"json", "application/json"},
      {"jpg",  "image/jpeg" },
      {"jpeg", "image/jpeg"},
      {"gif",  "image/gif"},
      {"png",  "image/png"},
      {"css",  "text/css"},
      {"htm",  "text/html"},
      {"html", "text/html"},
      {"ico",  "image/x-icon"},
    };

    public virtual void Awake() {
      mainThread = Thread.CurrentThread;
      fileRoot = Path.Combine(Application.streamingAssetsPath, "CUDLR");

      // Start server
      Debug.Log("Starting CUDLR Server on port : " + Port);
      listener.Prefixes.Add("http://*:"+Port+"/");
      listener.Start();
      listener.BeginGetContext(ListenerCallback, null);

      StartCoroutine(HandleRequests());
    }

    public void OnApplicationPause(bool paused) {
      if (paused) {
        listener.Stop();
      }
      else {
        listener.Start();
        listener.BeginGetContext(ListenerCallback, null);
       }
    }

    public virtual void OnDestroy() {
      listener.Close();
    }

    private void RegisterRoutes() {
      if ( registeredRoutes == null ) {
        registeredRoutes = new List<RouteAttribute>();

        foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
          foreach(Type type in assembly.GetTypes()) {
            // FIXME add support for non-static methods (FindObjectByType?)
            foreach(MethodInfo method in type.GetMethods(BindingFlags.Public|BindingFlags.Static)) {
              RouteAttribute[] attrs = method.GetCustomAttributes(typeof(RouteAttribute), true) as RouteAttribute[];
              if (attrs.Length == 0)
                continue;

              RouteAttribute.Callback cbm = Delegate.CreateDelegate(typeof(RouteAttribute.Callback), method, false) as RouteAttribute.Callback;
              if (cbm == null) {
                Debug.LogError(string.Format("Method {0}.{1} takes the wrong arguments for a console route.", type, method.Name));
                continue;
              }

              // try with a bare action
              foreach(RouteAttribute route in attrs) {
                if (route.m_route == null) {
                  Debug.LogError(string.Format("Method {0}.{1} needs a valid route regexp.", type, method.Name));
                  continue;
                }
<commit_msg>Update Server.cs<commit_after>using UnityEngine;
using System;
using System.IO;
using System.Net;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Linq;
using System.Threading;

namespace CUDLR {

  public class RequestContext
  {
    public HttpListenerContext context;
    public Match match;
    public bool pass;
    public string path;
    public int currentRoute;

    public HttpListenerRequest Request { get { return context.Request; } }
    public HttpListenerResponse Response { get { return context.Response; } }

    public RequestContext(HttpListenerContext ctx)
    {
      context = ctx;
      match = null;
      pass = false;
      path = WWW.UnEscapeURL(context.Request.Url.AbsolutePath);
      if (path == "/")
        path = "/index.html";
      currentRoute = 0;
    }
  }


  public class Server : MonoBehaviour {

    [SerializeField]
    public int Port = 55055;

    [SerializeField]
    public bool RegisterLogCallback = false;

    private static Thread mainThread;
    private static string fileRoot;
    private static HttpListener listener;
    private static List<RouteAttribute> registeredRoutes;
    private static Queue<RequestContext> mainRequests = new Queue<RequestContext>();

    // List of supported files
    // FIXME add an api to register new types
    private static Dictionary<string, string> fileTypes = new Dictionary<string, string> {
      {"js",   "application/javascript"},
      {"json", "application/json"},
      {"jpg",  "image/jpeg" },
      {"jpeg", "image/jpeg"},
      {"gif",  "image/gif"},
      {"png",  "image/png"},
      {"css",  "text/css"},
      {"htm",  "text/html"},
      {"html", "text/html"},
      {"ico",  "image/x-icon"},
    };

    public virtual void Awake() {
      mainThread = Thread.CurrentThread;
      fileRoot = Path.Combine(Application.streamingAssetsPath, "CUDLR");

      // Start server
      Debug.Log("Starting CUDLR Server on port : " + Port);
      listener = new HttpListener();
      listener.Prefixes.Add("http://*:"+Port+"/");
      listener.Start();
      listener.BeginGetContext(ListenerCallback, null);

      StartCoroutine(HandleRequests());
    }

    public void OnApplicationPause(bool paused) {
      if (paused) {
        listener.Stop();
      }
      else {
        listener.Start();
        listener.BeginGetContext(ListenerCallback, null);
       }
    }

    public virtual void OnDestroy() {
      listener.Close();
      listener = null;
    }

    private void RegisterRoutes() {
      if ( registeredRoutes == null ) {
        registeredRoutes = new List<RouteAttribute>();

        foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
          foreach(Type type in assembly.GetTypes()) {
            // FIXME add support for non-static methods (FindObjectByType?)
            foreach(MethodInfo method in type.GetMethods(BindingFlags.Public|BindingFlags.Static)) {
              RouteAttribute[] attrs = method.GetCustomAttributes(typeof(RouteAttribute), true) as RouteAttribute[];
              if (attrs.Length == 0)
                continue;

              RouteAttribute.Callback cbm = Delegate.CreateDelegate(typeof(RouteAttribute.Callback), method, false) as RouteAttribute.Callback;
              if (cbm == null) {
                Debug.LogError(string.Format("Method {0}.{1} takes the wrong arguments for a console route.", type, method.Name));
                continue;
              }

              // try with a bare action
              foreach(RouteAttribute route in attrs) {
                if (route.m_route == null) {
                  Debug.LogError(string.Format("Method {0}.{1} needs a valid route regexp.", type, method.Name));
                  continue;
                }

{'fraction_non_alphanumeric': 0.11171875, 'fraction_numerical': 0.002864583333333333, 'mean_word_length': 2.6145882352941174, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '6850918', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2009, 'n_words': 544}