예제 #1
0
        public void Handle(string pattern, Action <HttpListenerContext> callback)
        {
            if (pattern == "/")
            {
                handlers.Callback = callback;
                return;
            }
            var children = handlers.ChildNodes;
            var segments = pattern.Split('/').Where(s => s.Length > 0).Select(s => s.ToLower()).ToArray();

            foreach (string s in new ArraySegment <string>(segments, 0, segments.Length - 1))
            {
                if (!children.ContainsKey(s))
                {
                    children[s] = new HandlerNode();
                }
                children = children[s].ChildNodes;
            }
            children[segments.Last()] = new HandlerNode(callback, pattern.Last() != '/');
        }
예제 #2
0
 public HttpMux(params string[] prefixes)
 {
     handlers = new HandlerNode();
     Listener = new HttpListener();
     NotFound = DefaultNotFound;
     foreach (string s in prefixes)
     {
         string ss = s;
         if (ss[0] == ':' && int.TryParse(ss.Substring(1, ss.Length - 1), out int x))
         {
             ss = "localhost" + ss;
         }
         if (ss.Last() != '/')
         {
             ss += '/';
         }
         if (!ss.StartsWith("http://") || !ss.StartsWith("https://"))
         {
             ss = "http://" + ss;
         }
         AddPrefix(ss);
     }
 }