Skip to content

Commit

Permalink
url json/session/create creates a session with random key
Browse files Browse the repository at this point in the history
  • Loading branch information
greghaynes committed Aug 19, 2010
1 parent a344829 commit fa04433
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/pyscripts/plugins/JSONAPI/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from xsbs.http.jsonapi import JsonSite, JsonUserSite, site as jsonSite
from plugins.JSONAPI.admin import setup as setupAdmin
from plugins.JSONAPI.accounts import setup as setupAccounts
from plugins.JSONAPI.sessions import setup as setupSessions

from xsbs.players import all as allClients, player, playerCount, spectatorCount
from xsbs.users import userAuth
Expand Down Expand Up @@ -74,6 +75,7 @@ def setup():
jsonSite.putChild('server', ServerSite())
setupAdmin(jsonSite)
setupAccounts(jsonSite)
setupSessions(jsonSite)

setup()

15 changes: 15 additions & 0 deletions src/pyscripts/plugins/JSONAPI/sessions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from xsbs.http import server
from xsbs.http.jsonapi import site as apiSite, JsonSite, JsonSessionSite

import json

class CreateSessionSite(JsonSessionSite):
def render_session_JSON(self, request, session):
session = server.session_manager.createSession()
return json.dumps({'key': session.key})

def setup(jsonSite):
sessionSite = JsonSite()
sessionSite.putChild('create', CreateSessionSite())
jsonSite.putChild('session', sessionSite)

4 changes: 2 additions & 2 deletions src/pyscripts/xsbs/http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from xsbs.settings import PluginConfig
from xsbs.events import eventHandler

from sessionmanager import SessionManager
from session import SessionManager

config = PluginConfig('httpserver')
port = config.getOption('Config', 'port', '8081')
Expand All @@ -21,7 +21,7 @@ def __init__(self, address, port):
self.port = port
self.root_site = RootSite()
self.server_site = server.Site(self.root_site)
self.sessionManager = SessionManager()
self.session_manager = SessionManager()
def start(self):
self.connection = reactor.listenTCP(port, self.server_site)
def stop(self):
Expand Down
5 changes: 3 additions & 2 deletions src/pyscripts/xsbs/http/jsonapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from xsbs.users import userAuth
from xsbs.users.privilege import isUserAtLeastMaster, isUserMaster, isUserAdmin


try:
import json
except ImportError:
import simplejson as json

def setJsonHeaders(request):
request.setHeader('Content-Type', 'application/json')
#request.setHeader('Content-Type', 'application/json')
request.setHeader('Access-Control-Allow-Origin', '*')
request.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
request.setHeader('Access-Control-Allow-Headers', 'X-PINGOTHER, X-Requested-With, Accept')
Expand Down Expand Up @@ -70,7 +71,7 @@ def render_OPTIONS(self, request):
def render_JSON(self, request):
return ''

class JsonSessionSite(resource.Resource):
class JsonSessionSite(JsonSite):
def render_JSON(self, request):
try:
session = request.session
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import time
import random
import hashlib

class Session(dict):
def __init__(self, key):
Expand All @@ -12,9 +14,22 @@ class SessionManager(object):
def __init__(self, stale_secs=3600):
self.sessions = {}
self.stale_secs = stale_secs
self.randgen = random.Random()
def removeStales(self):
cur_time = time.time()
for session in self.sessions.values():
if (session.touch_time + self.stale_secs) < cur_time:
del self.sessions[session.key]
def createSession(self):
'''Create a session with a unique key
Returns newly created session'''
sess = None
while sess == None:
testkey = hashlib.sha1(str(self.randgen.randint(0, 10000))).hexdigest()
try:
currsess = self.sessions[testkey]
except KeyError:
sess = Session(testkey)
self.sessions[sess.key] = sess
return sess

0 comments on commit fa04433

Please sign in to comment.