Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksey Lim <alsroot@sugarlabs.org>2012-10-01 21:13:28 (GMT)
committer Aleksey Lim <alsroot@sugarlabs.org>2012-10-01 21:15:20 (GMT)
commitc45e7004e7c30602d06391a3b09a40a51e10461c (patch)
tree2ab762aecf670aec1ba819abc081c74163c46c95
parent73779cd58ac946a72f5d740d039c8c27d4fc2fa1 (diff)
Serve Contributor Hub from /hub path for IPC clients to workaround lack of CORS for SSE
-rw-r--r--sugar_network/local/__init__.py6
-rw-r--r--sugar_network/local/ipc_client.py14
-rw-r--r--sugar_network/toolkit/router.py26
3 files changed, 31 insertions, 15 deletions
diff --git a/sugar_network/local/__init__.py b/sugar_network/local/__init__.py
index 2a149ad..c143c60 100644
--- a/sugar_network/local/__init__.py
+++ b/sugar_network/local/__init__.py
@@ -78,6 +78,12 @@ ipc_port = Option(
'port number to listen for incomming connections from IPC clients',
default=5001, type_cast=int)
+hub_root = Option(
+ 'path to Contributor Hub site directory to serve from /hub location '
+ 'for IPC clients to workaround lack of CORS for SSE while using Hub '
+ 'from file:// url',
+ default='/usr/share/sugar-network/hub')
+
def path(*args):
"""Calculate a path from the root.
diff --git a/sugar_network/local/ipc_client.py b/sugar_network/local/ipc_client.py
index 7c7ae46..dbedf4f 100644
--- a/sugar_network/local/ipc_client.py
+++ b/sugar_network/local/ipc_client.py
@@ -13,10 +13,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+from os.path import join
+
import active_document as ad
from sugar_network.toolkit import router
-from sugar_network.local import sugar
+from sugar_network.local import sugar, hub_root
class Router(router.Router):
@@ -25,5 +27,15 @@ class Router(router.Router):
return sugar.uid()
def call(self, request, response):
+ if request.environ['PATH_INFO'] == '/hub':
+ raise ad.Redirect('/hub/')
+ if request.path and request.path[0] == 'hub':
+ return self._serve_hub(request.path[1:])
request.access_level = ad.ACCESS_LOCAL
return router.Router.call(self, request, response)
+
+ def _serve_hub(self, path):
+ if not path:
+ path = ['index.html']
+ path = join(hub_root.value, *path)
+ return router.stream_reader(file(path, 'rb'))
diff --git a/sugar_network/toolkit/router.py b/sugar_network/toolkit/router.py
index 29c8192..af0979d 100644
--- a/sugar_network/toolkit/router.py
+++ b/sugar_network/toolkit/router.py
@@ -64,6 +64,17 @@ def route(method, path):
return decorate
+def stream_reader(stream):
+ try:
+ while True:
+ chunk = stream.read(BUFFER_SIZE)
+ if not chunk:
+ break
+ yield chunk
+ finally:
+ stream.close()
+
+
class Router(object):
def __init__(self, commands):
@@ -150,7 +161,7 @@ class Router(object):
result.seek(0, 2)
response.content_length = result.tell()
result.seek(0)
- result = _stream_reader(result)
+ result = stream_reader(result)
return result
@@ -303,8 +314,6 @@ class _Request(Request):
self.if_modified_since = time.mktime(if_modified_since)
scope = len(self.path)
- enforce(scope >= 0 and scope < 4, BadRequest,
- 'Incorrect requested path')
if scope == 3:
self['document'], self['guid'], self['prop'] = self.path
elif scope == 2:
@@ -391,14 +400,3 @@ def _parse_accept_language(accept_language):
langs.insert(len(langs) - index, lang)
return langs
-
-
-def _stream_reader(stream):
- try:
- while True:
- chunk = stream.read(BUFFER_SIZE)
- if not chunk:
- break
- yield chunk
- finally:
- stream.close()