예제 #1
0
        public void HandleWebRequest(object sender, UriRequestEventArgs args)
        {
            lock (staticFolders) {
                try {
                    foreach (var x in staticFolders)
                    {
                        var found = Helpers.FindLocalServerPath(args.Request.Url, x);
                        if (found != null)
                        {
                            var mimetype = LookupMimeType(found);
                            args.Handled = true;
                            args.SetResponseType(mimetype);
                            args.SetResponseState(200);
                            // stream the file
                            using (var f = File.OpenRead(found)) {
                                using (var sr = new BufferedStream(f)) {
                                    var bb    = new byte[4096];
                                    var count = 0;
                                    do
                                    {
                                        count = sr.Read(bb, 0, bb.Length);
                                        if (count == 0)
                                        {
                                            break;
                                        }
                                        args.ResponsStream.BaseStream.Write(bb, 0, count);
                                    } while (true);
                                }
                            }
                            break;
                        }
                    }

                    // either 404 or a file in our xr-includes list
                    if (args.Handled == false)
                    {
                        foreach (var route in requestRoutes)
                        {
                            if (route.Match.IsMatch(args.Request.Url.AbsolutePath))
                            {
                                var context = Activator.CreateInstance(route.ContextType) as RequestContextBase;
                                if (context.Load(args))
                                {
                                    var proc = new Processor()
                                    {
                                        RootDirectory = TemplateRoot,
                                        Context       = context
                                    };
                                    proc.Transform(route.VirtualPathFile, args.ResponsStream);
                                }
                                break;
                            }
                        }
                    }
                } catch (Exception ex) {
                    Console.Error.WriteLine(ex);
                    throw;
                }
            }
        }
예제 #2
0
		public virtual bool Load (UriRequestEventArgs req)
		{
			req.Handled = true;
			req.SetResponseState (200);
			req.SetResponseType (GetContentType (req.Request.Url));
			return req.Handled;
		}
예제 #3
0
 void SetHandled(UriRequestEventArgs args, int status)
 {
     args.Handled = true;
     args.SetResponseState(status);
     args.SetResponseType("application/json");
 }
예제 #4
0
        void WebServer_Json(object sender, UriRequestEventArgs args)
        {
            if (!args.Handled)
            {
                if (args.Request.Url.AbsolutePath != "/json/") return;
                args.Handled = true;
                try
                {

                    var reqtype = args.Request.ContentType;
                    var reqenc = args.Request.ContentEncoding;
                    var len = args.Request.ContentLength64;

                    var sr = new StreamReader(args.Request.InputStream);

                    var m = jsonrpc.ReadMethod(sr);
                    var r = jsonrpc.RunMethod(m);
                    args.SetResponseState(200);
                    args.SetResponseType("application/json");
                    jsonrpc.WriteResult(r, args.ResponsStream);

                }
                catch (Exception e)
                {
                    Log.Print("exception!: {0}", e);
                    args.SetResponseState(500);
                    args.SetResponseType("text/plain");
                    args.ResponsStream.WriteLine(e.ToString());
                }
            }
        }
예제 #5
0
        void WebServer_FileServer(object sender, UriRequestEventArgs args)
        {
            if (!args.Handled)
            {
                var path = args.Request.Url.AbsolutePath;
                var proc = GetProcessor();
                var localpath = proc.VirtualToLocalPath(path);
                if (Directory.Exists(localpath))
                {
                    var indexpage = Path.Combine(localpath,"index.html");
                    if (File.Exists(indexpage))
                        localpath = indexpage;
                    path = string.Join("/", new string[] { path, "index.html" });
                }

                if (File.Exists(localpath))
                {
                    args.SetResponseState(200);
                    args.SetResponseType(GetContentType(localpath));
                    args.Handled = true;

                    if ( path.StartsWith("/static/") )
                    {
                        using ( var fh = File.OpenRead(localpath) )
                        {
                            var buf = new byte[8192];
                            int count = 0;
                            do {
                                count = fh.Read( buf, 0, buf.Length );
                                if ( count > 0 ) args.ResponsStream.BaseStream.Write( buf, 0, count );
                                if (count < buf.Length) break;
                            } while ( true );
                        }
                    } else {
                        proc.Context = GetPageModel(path);
                        proc.Transform(path, args.ResponsStream);
                    }
                }

            }
        }
예제 #6
0
 void WebServer_FileNotFound(object sender, UriRequestEventArgs args)
 {
     if (!args.Handled)
     {
         args.SetResponseState(404);
         args.SetResponseType("text/plain");
         args.Handled = true;
         args.ResponsStream.WriteLine("not found : {0}", args.Request.Url);
     }
 }
예제 #7
0
        void WebServer_FileIndex(object sender, UriRequestEventArgs args)
        {
            if (!args.Handled)
            {
                var path = args.Request.Url.AbsolutePath;
                if (path == "/" || path.StartsWith("/static/"))
                {
                    var proc = GetProcessor();
                    args.SetResponseState(200);
                    args.SetResponseType("text/html");

                    var dirs = System.IO.Directory.GetDirectories(proc.VirtualToLocalPath(args.Request.Url.AbsolutePath));
                    var files = System.IO.Directory.GetFiles(proc.VirtualToLocalPath(args.Request.Url.AbsolutePath));
                    args.ResponsStream.WriteLine("<html><head><title>Index of {0}</title></head>", args.Request.Url.AbsolutePath);
                    args.ResponsStream.WriteLine("<body><h1>Index Of {0}</h1><hr><ul>", args.Request.Url.AbsolutePath);

                    foreach (var d in dirs)
                    {
                        args.ResponsStream.WriteLine("<li>[DIR] <a href='{0}'>{0}</a></li>", d);
                    }

                    foreach (var f in files)
                    {
                        var fn = System.IO.Path.GetFileName(f);
                        args.ResponsStream.WriteLine("<li><a href='{0}'>{0}</a></li>", fn);
                    }

                    args.ResponsStream.WriteLine("</ul></body></html>");
                    args.Handled = true;
                }
            }
        }