Permalink
Browse files

add HttpServer to WP7

  • Loading branch information...
1 parent 133b43c commit 8c192a2cf00af7ac40078ed354efb835083f06a4 genywind committed with dmitrys Mar 17, 2011
View
@@ -40,6 +40,8 @@ platform/wp7/bin/*
platform/wp7/WPApplication/rho/*
platform/wp7/WPApplication/obj/*
platform/wp7/WPApplication/Bin/*
+platform/wp7/*.sdf
+platform/wp7/rhodes/obj/*
rhosync-client/ObjectiveC/Tests/SyncClientTest/build/*
rhosync-client/ObjectiveC/Tests/build/*
rhosync-client/ObjectiveC/build/*
View
@@ -2,6 +2,9 @@
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rhodes", "Rhodes\Rhodes.csproj", "{F6FA3ABB-AE86-4913-95D6-8A34C79DBA82}"
+ ProjectSection(ProjectDependencies) = postProject
+ {9CAF724B-1466-4A76-9251-5ADB132F636F} = {9CAF724B-1466-4A76-9251-5ADB132F636F}
+ EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RhoRuby", "RhoRuby\RhoRuby.csproj", "{9CAF724B-1466-4A76-9251-5ADB132F636F}"
EndProject
@@ -15,6 +15,7 @@
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Resources;
+using rho.common;
namespace Rhodes
{
@@ -69,6 +70,8 @@ private void WebBrowser_OnLoadCompleted(object sender, NavigationEventArgs e)
private void WebBrowser_OnNavigating(object sender, NavigatingEventArgs e)
{
+ RHODESAPP().HttpServer.decide("GET", e.Uri, "");
+
/* if (e.Uri.AbsoluteUri.Equals("http://www.google.com/") )
{
e.Cancel = true;
@@ -86,7 +86,10 @@
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
- <Compile Include="rho\common\CRhodesApp.cs" />
+ <Compile Include="rho\common\RhodesApp.cs" />
+ <Compile Include="rho\common\RhoFile.cs" />
+ <Compile Include="rho\common\RhoFilePath.cs" />
+ <Compile Include="rho\net\HttpServer.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
@@ -0,0 +1,29 @@
+using System;
+using System.IO.IsolatedStorage;
+using System.Windows;
+using System.Windows.Resources;
+using System.IO;
+
+namespace rho.common
+{
+ public class CRhoFile
+ {
+ public static bool isResourceFileExist(String path)
+ {
+ if (path.StartsWith("/"))
+ path = path.Substring(1);
+
+ StreamResourceInfo sr = Application.GetResourceStream(new Uri(path, UriKind.Relative));
+ if (sr == null)
+ return false;
+
+ return sr != null;
+ }
+
+ public static bool isFileExist(String path)
+ {
+ IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
+ return isoStore.FileExists(path);
+ }
+ }
+}
@@ -0,0 +1,50 @@
+using System;
+
+namespace rho.common
+{
+ public class CFilePath
+ {
+ public static String join(String path1, String path2)
+ {
+ bool bSlash1 = path1.EndsWith("/") || path1.EndsWith("\\");
+ bool bSlash2 = path2.StartsWith("/") || path2.StartsWith("\\");
+
+ String res;
+ if (bSlash1 && bSlash2)
+ res = path1 + path2.Substring(1);
+ else if (bSlash1 || bSlash2)
+ res = path1 + path2;
+ else
+ res = path1 + '/' + path2;
+
+ return res;
+ }
+
+ public static String getExtension(String path)
+ {
+ int nDot = path.LastIndexOf('.');
+ if (nDot >= 0)
+ return path.Substring(0, nDot);
+
+ return "";
+ }
+
+ public static String getBaseName(String path)
+ {
+ int nSlash = findLastSlash(path);
+ if (nSlash >= 0 )
+ return path.Substring(nSlash + 1, path.Length - (nSlash + 1));
+
+ return path;
+ }
+
+ static int findLastSlash(String path)
+ {
+ int nPos = path.LastIndexOf('/');
+ if (nPos >= 0)
+ return nPos;
+
+ return path.LastIndexOf('\\');
+ }
+ }
+}
@@ -9,27 +9,27 @@
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
+using rho.net;
using RhoRuby;
-namespace Rhodes
+namespace rho.common
{
public sealed class CRhodesApp
{
private static readonly CRhodesApp instance = new CRhodesApp();
private WebBrowser _webBrowser;
private RhoRubyFramework _rhoframework;
+ private CHttpServer m_httpServer;
public WebBrowser WebBrowser
{
get { return _webBrowser; }
set { _webBrowser = value; }
}
-
- public void Init()
+ public CHttpServer HttpServer
{
- RhoRubyFramework.WebBrowser = _webBrowser;
- _rhoframework = new RhoRubyFramework();
+ get { return m_httpServer; }
}
private CRhodesApp() {}
@@ -41,5 +41,18 @@ public static CRhodesApp Instance
return instance;
}
}
+
+ public void Init()
+ {
+ m_httpServer = new CHttpServer(CFilePath.join(getRhoRootPath(), "apps"));
+
+ RhoRubyFramework.WebBrowser = _webBrowser;
+ _rhoframework = new RhoRubyFramework();
+ }
+
+ String getRhoRootPath()
+ {
+ return "/rho/";
+ }
}
}
@@ -0,0 +1,149 @@
+using System;
+using rho.common;
+using System.Collections.Generic;
+
+namespace rho.net
+{
+ public class CHttpServer
+ {
+ String m_root;
+
+ class CRoute
+ {
+ public String application;
+ public String model;
+ public String id;
+ public String action;
+
+ public static bool isid(String s)
+ {
+ return s.Length > 2 && s[0] == '{' && s[s.Length - 1] == '}';
+ }
+ };
+
+ public CHttpServer(String strFileRoot)
+ {
+ m_root = strFileRoot;
+ }
+
+ public bool process_registered(Uri uri)
+ {
+ return false;
+ }
+
+ public bool decide(String strMethod, Uri uri, String strBody)
+ {
+ if (process_registered(uri))
+ return true;
+
+ CRoute route = new CRoute();
+ if (dispatch(uri, route))
+ {
+ return true;
+ }
+
+ String fullPath = CFilePath.join(m_root, uri.OriginalString);
+ String strIndexFile = getIndex(fullPath);
+ if (strIndexFile.Length > 0 )
+ {
+ if (!CRhoFile.isResourceFileExist(strIndexFile))
+ {
+ String error = "<html><font size=\"+4\"><h2>404 Not Found.</h2> The file " + uri + " was not found.</font></html>";
+ //TODO: return error page
+ return false;
+ }
+
+ return true;
+ }
+
+ //TODO: return file
+ return false;
+ }
+
+ bool dispatch(Uri uri, CRoute route)
+ {
+ if (isknowntype(uri))
+ return false;
+
+ // Trying to parse route
+ if (!parse_route(uri, route))
+ return false;
+
+ // Convert CamelCase to underscore_case
+ String controllerName = "";
+ for (int i = 0; i < route.model.Length; i++)
+ {
+ if (Char.IsUpper(route.model[i]) && i > 0)
+ controllerName += '_';
+
+ controllerName += route.model[i];
+ }
+ controllerName = controllerName.ToLower();
+
+ String model_name_controller = m_root + "/" + route.application + "/" + route.model + "/" + controllerName + "_controller.rb";
+ String controller = m_root + "/" + route.application + "/" + route.model + "/controller.rb";
+
+ return CRhoFile.isResourceFileExist(model_name_controller) || CRhoFile.isResourceFileExist(controller);
+ }
+
+ bool parse_route(Uri uri, CRoute route)
+ {
+ String strPath = uri.OriginalString;//.GetComponents(UriComponents.Path, UriFormat.SafeUnescaped);
+ String[] arParts = strPath.Split('/');
+
+ if (arParts.Length < 2)
+ return false;
+
+ route.application = arParts[0];
+ route.model = arParts[1];
+
+ if (arParts.Length < 4)
+ return true;
+
+ String aoi = arParts[2];
+ if (CRoute.isid(aoi))
+ {
+ route.id = aoi;
+ route.action = arParts[3];
+ }
+ else
+ {
+ route.id = arParts[3];
+ route.action = aoi;
+ }
+
+ return true;
+ }
+
+ bool isknowntype(Uri uri)
+ {
+ String[] ignored_exts = { ".css", ".js", ".html", ".htm", ".png", ".bmp", ".jpg", ".jpeg", ".gif" };
+
+ String strExt = CFilePath.getExtension(uri.ToString()).ToLower();
+ return find_in_string_array(strExt, ignored_exts);
+ }
+
+ String getIndex(String path)
+ {
+ String[] index_files = { "index_erb.rb", "index.html", "index.htm", "index.php", "index.cgi" };
+
+ String strExt = CFilePath.getExtension(path);
+ if (strExt.Length == 0)
+ return CFilePath.join( path, index_files[0] );
+
+ String strFileName = CFilePath.getBaseName(path);
+ return find_in_string_array(strFileName, index_files) ? path : "";
+ }
+
+ bool find_in_string_array(String str, String[] ar)
+ {
+ for (int i = 0; i < ar.Length; i++)
+ {
+ if (str.CompareTo(ar[i]) == 0)
+ return true;
+ }
+
+ return false;
+ }
+ }
+}

0 comments on commit 8c192a2

Please sign in to comment.