예제 #1
0
파일: Listener.cs 프로젝트: theanti9/irek
 /// <summary>
 /// Initializes a new instance of the <see cref="Listener"/> class.
 /// </summary>
 /// <param name="cfg">The Config Object.</param>
 public Listener(Config cfg, List<UrlMapItem> UrlMap, Hashtable modulelist)
 {
     port = int.Parse(cfg.Get("port"));
     config = cfg;
     GlobalUrlMap = UrlMap;
     ServicePointManager.DefaultConnectionLimit = 20;
     ModuleList = modulelist;
 }
예제 #2
0
        /// <summary>
        /// Handles the specified request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="config">The config.</param>
        /// <param name="UrlMap">The Url Map list</param>
        /// <param name="ModuleList">The ModuleList hashtable</param>
        /// <returns>byte[] server response</returns>
        public static byte[] Handle(string request, ref Config config, ref List<UrlMapItem> UrlMap, ref Hashtable ModuleList)
        {
            string firstline = request.Split((new string[] { "\r\n" }),StringSplitOptions.None)[0];
            StringBuilder logline = new StringBuilder();
            logline.Append(firstline);
            int space = firstline.IndexOf(' ');
            string path = firstline.Substring(space + 1, firstline.LastIndexOf(' ')-space-1);
            string method = null;
            string staticpath = null;
            Regex rx = null;
            foreach (UrlMapItem item in UrlMap) {
                if (item.IsMatch(path))
                {
                    method = item.Method;
                    rx = item.rx;
                    staticpath = item.Path;
                }
            }
            logline.Append(" : " + method);
            if (!String.IsNullOrEmpty(staticpath)) {
                logline.Append(" " + staticpath);
            }
            if (string.IsNullOrEmpty(method))
            {
                logline.Append(" : 404");
                Logger.GetInstance().LogAccess(logline.ToString());
                return Encoding.ASCII.GetBytes(Get404());

            }
            if (method == "irek.static") {
                Match match = rx.Match(path);
                string addpath = match.Groups[1].Value;
                if (!staticpath.EndsWith(@"\"))
                {
                    staticpath += "\\";
                }
            #if (Windows && !Unix)
                staticpath = staticpath + addpath.Replace("/", "\\");
            #else
                staticpath = staticpath + addpath;
            #endif
                if (File.Exists(staticpath))
                {
                    int numbytes = (int)new FileInfo(staticpath).Length;
                    BinaryReader br = new BinaryReader(new FileStream(staticpath, FileMode.Open, FileAccess.Read));
                    string contenttype = Mimes.Get(staticpath.Substring(staticpath.LastIndexOf(".")));
                    string head = (new Header("HTTP/1.1", contenttype, numbytes, " 200 OK")).GetHeader();
                    int len = numbytes + head.Length;
                    byte[] buff = new byte[len];
                    Buffer.BlockCopy(Encoding.ASCII.GetBytes(head), 0, buff, 0, head.Length);
                    Buffer.BlockCopy(br.ReadBytes(numbytes), 0, buff, head.Length, numbytes);
                    logline.Append(" : 200");
                    Logger.GetInstance().LogAccess(logline.ToString());
                    return buff;
                }
                else
                {
                    logline.Append(" : 404");
                    Logger.GetInstance().LogAccess(logline.ToString());
                    return Encoding.ASCII.GetBytes(Get404());
                }
            }
            Request rq = new Request(request, rx);
            string methodnamespace = method.Substring(0, method.IndexOf('.'));
            try
            {
                Assembly assembly = (Assembly)ModuleList[methodnamespace];
                Type t = assembly.GetType(method.Substring(0, method.LastIndexOf('.')));
                MethodInfo m = t.GetMethod(method.Substring(method.LastIndexOf('.') + 1));
                Page p = (Page)m.Invoke(null, (new object[] { rq }));
                p.PageHeader.SetCookie("ISESSION=" + Util.RandomString(16) + ";path=/");
                return Encoding.ASCII.GetBytes(p.GetHeader() + p.GetBody());
            }
            catch (Exception e)
            {
                Logger.GetInstance().LogError(e.Message + " From " + e.Source);
                return Encoding.ASCII.GetBytes(Get404());
            }
        }
예제 #3
0
파일: Server.cs 프로젝트: theanti9/irek
        /// <summary>
        /// Initializes a new instance of the <see cref="Server"/> class.
        /// </summary>
        public Server()
        {
            Console.WriteLine("Loading Configuration...");
            try
            {
#if (Windows && !Unix)
				ServerConfig = new Config(Directory.GetCurrentDirectory() + "\\irek.conf");
#else
               	ServerConfig = new Config(Directory.GetCurrentDirectory() + "/irek.conf");
#endif
            }
            catch (NoSuchConfigurationFileException e)
            {
                Logger.GetInstance().LogError(e.Message);
                Environment.Exit(1);
            }
			try {
#if (Windows && !Unix)
				ModuleConfig = new ModuleConfReader(Directory.GetCurrentDirectory() + "\\modules.conf");
#else
				ModuleConfig = new ModuleConfReader(Directory.GetCurrentDirectory() + "/modules.conf");
#endif
			} catch (NoSuchConfigurationFileException e) {
				Logger.GetInstance().LogError(e.Message);
				Environment.Exit(1);
			}
            foreach (string depstring in ModuleConfig.Dependencies)
            {
				try {
	                Dependency dep = new Dependency(depstring);
	                dep.Load();
				} catch (MissingDependencyException e) {
					Logger.GetInstance().LogError(e.Message);
					Environment.Exit(1);
				}
            }

            GlobalUrlMap = new List<UrlMapItem>();
			ModuleList = new Hashtable();
			try {
	            foreach (string modstring in ModuleConfig.Modules)
	            {
	                Module mod = new Module(modstring);
	                mod.Load();
					ModuleList.Add(mod.ModuleNamespace, mod.ModuleAssembly);
					List<UrlMapItem> tempmap = mod.UrlMap;
	                foreach (UrlMapItem mapitem in tempmap)
	                {
						foreach (UrlMapItem item in GlobalUrlMap) {
							if (item.UrlPattern == mapitem.UrlPattern) {
								throw new DuplicateUrlPatternException("Error: Same url pattern used in multiple places");
							}
						}
	                    GlobalUrlMap.Add(mapitem);
	                }
	            }
			} catch (DuplicateUrlPatternException e) {
				Logger.GetInstance().LogError(e.Message);
				Environment.Exit(1);
			}
            
            Console.WriteLine("Initializing...");
            Listener listener = new Listener(ServerConfig, GlobalUrlMap, ModuleList);
			try {
            	listener.Run();
			} catch (Exception e) {
				Logger.GetInstance().LogError(e.Message + " From " + e.Source);
				Environment.Exit(1);
			}
        }