Added a simple parser for HTTP request. Can't find anything suitable to use in python...
authorStas Shtin <antisvin@gmail.com>
Wed, 14 Apr 2010 10:13:57 +0000 (14:13 +0400)
committerStas Shtin <antisvin@gmail.com>
Wed, 14 Apr 2010 10:13:57 +0000 (14:13 +0400)
src/ipypbx/http.py [new file with mode: 0644]
src/ipypbx/network.py [deleted file]
src/ipypbx/state.py [deleted file]

diff --git a/src/ipypbx/http.py b/src/ipypbx/http.py
new file mode 100644 (file)
index 0000000..1fc4c87
--- /dev/null
@@ -0,0 +1,117 @@
+# Copyright (c) Stas Shtin, 2010
+
+# This file is part of IPyPBX.
+
+# IPyPBX is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# IPyPBX is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with IPyPBX.  If not, see <http://www.gnu.org/licenses/>.
+
+from PyQt4 import QtCore, QtNetwork
+
+
+class FreeswitchConfigServer(QtNetwork.QTcpServer):
+    def setSocket(self, host, port):
+        self.host = host
+        self.port = port
+
+    def startServer(self):
+        self.newConnection.connect(self.clientConnecting)
+        self.listen(QtNetwork.QHostAddress(self.host), self.port)
+
+    def stopServer(self):
+        self.close()
+
+    def clientConnecting(self, socket):
+        if self.hasPendingConnections():
+             connectingClient = self.server.nextPendingConnection()
+             connectingClient.readyRead.connect(self.receiveData)
+
+    def receiveData(self, socket):
+        self.
+        while socket.canReadLine():
+            line = socket.readLine().strip()
+            
+
+
+class HttpParseError(Exception):
+    pass
+
+
+class HttpRequestParser(object):
+    """
+    A simple state machine for parsing HTTP requests.
+    """
+    HTTP_NONE, HTTP_REQUEST, HTTP_HEADERS, HTTP_EMPTY, HTTP_MESSAGE = range(5)
+    HTTP_STATES = ['NONE', 'REQUEST', 'HEADERS', 'EMPTY', 'MESSAGE']
+    
+    def __init__(self):
+        super(HttpParser, self).__init__()
+
+    def reset(self):
+        """
+        Reset parser to initial state.
+        """
+        # Initial values for request data.
+        self.method = None
+        self.request_path = None
+        self.http_version = None
+        self.message = ''
+        
+        # Set initial state.
+        self.state = HTTP_NONE        
+
+    def handle(self, line):
+        """
+        Dispatch line to current state handler.
+        """
+        for state in HTTP_STATES:
+            if getattr(self, 'HTTP_%s' % state) == self.state:
+                getattr(self, 'handle%s' % state.title()).(line)
+                break
+        else:
+            raise HttpParseError('Unknown HTTP state')
+                
+    def handleNone(self, line):
+        """
+        Pass line to next state.
+        """
+        self.state += 1
+        self.handle(line)
+
+    def handleRequest(self, line):
+        """
+        Retrieve HTTP method, request path and HTTP version from request.
+        """
+        self.method, self.request_path, self.http_version = line.split(' ')
+        self.state += 1
+
+    def handleHeaders(self, line):
+        """
+        Parse headers while not found an empty line.
+        """
+        if line:
+            key, value = line.split(': ')
+            self.headers[key] = value
+        else:
+            self.state += 1
+
+    def handleEmpty(self, line):
+        """
+        Empty line separator is found - proceed to next state.
+        """
+        self.state += 1
+
+    def handleMessage(self, line):
+        """
+        Append to message body.
+        """
+        self.message += line
diff --git a/src/ipypbx/network.py b/src/ipypbx/network.py
deleted file mode 100644 (file)
index 1e526b6..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-# Copyright (c) Stas Shtin, 2010
-
-# This file is part of IPyPBX.
-
-# IPyPBX is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# IPyPBX is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with IPyPBX.  If not, see <http://www.gnu.org/licenses/>.
-
-
-from PyQt import QtNetwork
-
-
-class FreeswitchConfigServer(QTcpServer):
-    """
-    This is a server that gets instantiated for every connection.
-    """
-    def __init__(self, host, port, parent=None):
-        super(FreeswitchConfig, self).__init__(self, parent)
-
-        self.newConnection.connect(self, handleClientConnection)
-        self.listen(host, port)
-
-    def handleClientConnection(self):
-        if self.hasPendingConnections():
-            connectionClient = self.nextClientConnection()
-            connectionClient.readyRead.connect(self.receiveData)
diff --git a/src/ipypbx/state.py b/src/ipypbx/state.py
deleted file mode 100644 (file)
index 71f190a..0000000
+++ /dev/null
@@ -1,39 +0,0 @@
-# Copyright (c) Stas Shtin, 2010
-
-# This file is part of IPyPBX.
-
-# IPyPBX is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-
-# IPyPBX is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-
-# You should have received a copy of the GNU General Public License
-# along with IPyPBX.  If not, see <http://www.gnu.org/licenses/>.
-
-import os
-from axiom.store import Store
-from ipypbx import models
-
-
-# Working directory path.
-PREFIX = os.path.expanduser('~/.ipypbx')
-
-# Create it if necessary.
-if not os.path.exists(PREFIX):
-    os.mkdir(PREFIX, 0700)
-
-# Initialize sqlite DB file.
-#store = Store(os.path.join(PREFIX, 'ipypbx.db'))
-
-# Program state data.
-sipProfiles = []
-domains = []
-gateways = []
-endpoints = []
-extensions = []
-