Exemplo n.º 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;
                }
            }
        }
Exemplo n.º 2
0
		public virtual bool Load (UriRequestEventArgs req)
		{
			req.Handled = true;
			req.SetResponseState (200);
			req.SetResponseType (GetContentType (req.Request.Url));
			return req.Handled;
		}
Exemplo n.º 3
0
 public void Dispatch(UriRequestEventArgs args)
 {
     try
     {
         var tocall = GetMethodFromUri(args.Request.Url);
         tocall.Invoke(this, new object[] { args });
     }
     catch (MissingMethodException)
     {
         SendNoSuchMethodError(args);
     }
     catch (Exception ex)
     {
         SendError(args, 500, String.Format("{0}:{1}",
             ex.GetType().Name, ex.Message));
     }
 }
Exemplo n.º 4
0
 public void HandleGetSystem(UriRequestEventArgs args)
 {
     Dictionary<string, string> rv = new Dictionary<string, string>();
     var sysname = Mate.LastSystemName;
     if (!string.IsNullOrEmpty(sysname)){
         rv["systemname"] = sysname;
         var sysurl = Mate.GetEDDBSystemUrl(sysname);
         if (!string.IsNullOrEmpty(sysurl))
         {
             long sysid = 0;
             Mate.LookupEDDBSystemID(sysname, out sysid);
             rv["eddbid"] = sysid.ToString();
             rv["eddburl"] = sysurl;
         }
     }
     var json = JsonConvert.SerializeObject(rv);
     SetHandled(args, 200);
     args.ResponsStream.Write(json);
 }
Exemplo n.º 5
0
 public void HandlePollSystem(UriRequestEventArgs args)
 {
     var sysnow = Mate.LastSystemName;
     do
     {
         Mate.Wait();
     } while (sysnow == Mate.LastSystemName);
     HandleGetSystem(args);
 }
Exemplo n.º 6
0
 void SetHandled(UriRequestEventArgs args, int status)
 {
     args.Handled = true;
     args.SetResponseState(status);
     args.SetResponseType("application/json");
 }
Exemplo n.º 7
0
 public void SendNoSuchMethodError(UriRequestEventArgs args)
 {
     SendError(args, 404, "No such method");
 }
Exemplo n.º 8
0
 public void SendError(UriRequestEventArgs args, int status, string err)
 {
     SetHandled(args, status);
     var rv = new Dictionary<string, string>() {
         { "error", err }
     };
     args.ResponsStream.Write(JsonConvert.SerializeObject(rv));
 }
Exemplo n.º 9
0
 public void OnUriRequested(object sender, UriRequestEventArgs args)
 {
     if ( args.Request.Url.AbsolutePath.StartsWith("/api/") ){
            Dispatch(args);
            if (!args.Handled)
            {
                SendNoSuchMethodError(args);
            }
        }
 }
Exemplo n.º 10
0
 void WebServer_UriRequested(object sender, UriRequestEventArgs args)
 {
     Log.Print("requested {0}", args.Request.Url);
 }
Exemplo n.º 11
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());
                }
            }
        }
Exemplo n.º 12
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);
                    }
                }

            }
        }
Exemplo n.º 13
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);
     }
 }
Exemplo n.º 14
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;
                }
            }
        }
Exemplo n.º 15
0
 public virtual void HandleWebRequestStart(object sender, UriRequestEventArgs args)
 {
 }