Different answer is given when server is requested with HTTP GET

This commit is contained in:
Uriziel 2013-01-16 20:27:32 +01:00
parent d6eb5259d2
commit 181083eec7
5 changed files with 29 additions and 8 deletions

View File

@ -107,12 +107,14 @@ en = {
"server-isolate-room-argument" : 'should rooms be isolated?',
"server-motd-argument": "path to file from which motd will be fetched",
"server-messed-up-motd": "Message of the Day has unescaped placeholders. All $ signs should be doubled ($$).",
"server-http-reply-argument": "path to file from which http reply will be fetched",
"server-default-http-reply": "This server should not be requested with your browser, but Syncplay software available from http://syncplay.pl",
#Server errors
"unknown-command-server-error" : "Unknown command\n%s", #message
"not-json-server-error" : "Not a json encoded string\n%s", #message
"unknown-command-server-error" : "Unknown command {}", #message
"not-json-server-error" : "Not a json encoded string {}", #message
"not-known-server-error" : "You must be known to server before sending this command",
"client-drop-server-error" : "Client drop: %s -- %s", #host, error
"client-drop-server-error" : "Client drop: {} -- {}", #host, error
"password-required-server-error" : "Password required",
"wrong-password-server-error" : "Wrong password supplied",
"hello-server-error" : "Not enough Hello arguments",

View File

@ -39,7 +39,11 @@ class JSONCommandProtocol(LineReceiver):
try:
messages = json.loads(line)
except:
self.dropWithError(getMessage("en", "not-json-server-error").format(line))
if ("GET / HTTP/1." in line):
self.handleHttpRequest(line)
self.drop()
else:
self.dropWithError(getMessage("en", "not-json-server-error").format(line))
return
self.handleMessages(messages)
@ -189,6 +193,9 @@ class SyncClientProtocol(JSONCommandProtocol):
self._client.updateGlobalState(position, paused, doSeek, setBy, latency)
position, paused, doSeek, stateChange = self._client.getLocalState()
self.sendState(position, paused, doSeek, latencyCalculation, stateChange)
def handleHttpRequest(self, request):
pass
def sendState(self, position, paused, doSeek, latencyCalculation, stateChange = False):
state = {}
@ -241,7 +248,7 @@ class SyncServerProtocol(JSONCommandProtocol):
return wrapper
def dropWithError(self, error):
print getMessage("en", "client-drop-server-error").format((self.transport.getPeer().host, error))
print getMessage("en", "client-drop-server-error").format(self.transport.getPeer().host, error)
self.sendError(error)
self.drop()
@ -394,6 +401,9 @@ class SyncServerProtocol(JSONCommandProtocol):
if(self.serverIgnoringOnTheFly == 0):
self._factory.updateWatcherState(self, position, paused, doSeek, latencyCalculation)
def handleHttpRequest(self, request):
self.sendLine(self._factory.gethttpRequestReply())
def handleError(self, error):
self.dropWithError(error["message"]) #TODO: more processing and fallbacking

View File

@ -14,12 +14,13 @@ import os
from string import Template
class SyncFactory(Factory):
def __init__(self, password = '', motdFilePath = None):
def __init__(self, password = '', motdFilePath = None, httpReplyFilePath= None):
print getMessage("en", "welcome-server-notification").format(syncplay.version)
if(password):
password = hashlib.md5(password).hexdigest()
self.password = password
self._motdFilePath = motdFilePath
self._httpReplyFilePath = httpReplyFilePath
self._rooms = {}
self._roomStates = {}
self._roomUpdate = threading.RLock()
@ -99,6 +100,13 @@ class SyncFactory(Factory):
else:
return ""
def gethttpRequestReply(self):
if(self._httpReplyFilePath and os.path.isfile(self._httpReplyFilePath)):
tmpl = codecs.open(self._httpReplyFilePath, "r", "utf-8-sig").read()
return tmpl
else:
return getMessage("en", "server-default-http-reply")
def sendState(self, watcherProtocol, doSeek = False, senderLatency = 0, forcedUpdate = False):
watcher = self.getWatcher(watcherProtocol)
if(not watcher):

View File

@ -206,4 +206,5 @@ class ServerConfigurationGetter(object):
self._argparser.add_argument('--port', metavar='port', type=str, nargs='?', help=getMessage("en", "server-port-argument"))
self._argparser.add_argument('--password', metavar='password', type=str, nargs='?', help=getMessage("en", "server-password-argument"))
self._argparser.add_argument('--isolate-rooms', action='store_true', help=getMessage("en", "server-isolate-room-argument"))
self._argparser.add_argument('--motd-file', metavar='motd', type=str, nargs='?', help=getMessage("en", "server-motd-argument"))
self._argparser.add_argument('--motd-file', metavar='file', type=str, nargs='?', help=getMessage("en", "server-motd-argument"))
self._argparser.add_argument('--http-reply-file', metavar='file', type=str, nargs='?', help=getMessage("en", "server-http-reply-argument"))

View File

@ -11,5 +11,5 @@ args = argsGetter.getConfiguration()
if(not args.isolate_rooms):
reactor.listenTCP(int(args.port), SyncFactory(args.password, args.motd_file))
else:
reactor.listenTCP(int(args.port), SyncIsolatedFactory(args.password, args.motd_file))
reactor.listenTCP(int(args.port), SyncIsolatedFactory(args.password, args.motd_file, args.http_reply_file))
reactor.run()