mirror of https://git.48k.eu/ogclient
Add HTTP parser support
The new OpenGnsys support to communicate server and client side will be HTTP. This new class allows us the support for parsing all the message received from the server in HTTP format.more_events
parent
076e15bb29
commit
bfdeae840c
|
@ -0,0 +1,54 @@
|
|||
from mimetools import Message
|
||||
from StringIO import StringIO
|
||||
|
||||
class HTTPParser:
|
||||
def __init__(self):
|
||||
self.requestLine = None
|
||||
self.headersAlone = None
|
||||
self.headers = None
|
||||
self.host = None
|
||||
self.contentType = None
|
||||
self.contentLen = None
|
||||
self.operation = None
|
||||
self.URI = None
|
||||
|
||||
def parser(self,data):
|
||||
self.requestLine, self.headersAlone = data.split('\n', 1)
|
||||
self.headers = Message(StringIO(self.headersAlone))
|
||||
|
||||
if 'host' in self.headers.keys():
|
||||
self.host = self.headers['host']
|
||||
|
||||
if 'content-type' in self.headers.keys():
|
||||
self.contentType = self.headers['content-type']
|
||||
|
||||
if 'content-length' in self.headers.keys():
|
||||
self.contentLen = int(self.headers['content-length'])
|
||||
|
||||
if (not self.requestLine == None or not self.requestLine == ''):
|
||||
self.operation = self.requestLine.split('/', 1)[0]
|
||||
self.URI = self.requestLine.split('/', 1)[1]
|
||||
|
||||
def getHeaderLine(self):
|
||||
return self.headersAlone
|
||||
|
||||
def getRequestLine(self):
|
||||
return self.requestLine
|
||||
|
||||
def getHeaderParsed(self):
|
||||
return self.headers
|
||||
|
||||
def getHost(self):
|
||||
return self.host
|
||||
|
||||
def getContentType(self):
|
||||
return self.contentType
|
||||
|
||||
def getContentLen(self):
|
||||
return self.contentLen
|
||||
|
||||
def getRequestOP(self):
|
||||
return self.operation
|
||||
|
||||
def getURI(self):
|
||||
return self.URI
|
|
@ -2,9 +2,8 @@ import errno
|
|||
import select
|
||||
import socket
|
||||
import time
|
||||
import httplib
|
||||
from mimetools import Message
|
||||
from StringIO import StringIO
|
||||
|
||||
from HTTPParser import *
|
||||
from enum import Enum
|
||||
|
||||
class State(Enum):
|
||||
|
@ -66,8 +65,8 @@ class ogClient:
|
|||
self.sock.close()
|
||||
self.connect()
|
||||
|
||||
print "received " + data
|
||||
self.data = self.data + data
|
||||
httpparser = HTTPParser()
|
||||
|
||||
if not self.trailer:
|
||||
if self.data.find("\r\n") > 0:
|
||||
|
@ -75,18 +74,13 @@ class ogClient:
|
|||
request_line, headers_alone = self.data.split('\n', 1)
|
||||
headers = Message(StringIO(headers_alone))
|
||||
|
||||
print "DEBUG: \r\n trailer received"
|
||||
print "DEBUG: HTTP keys are " + str(headers.keys())
|
||||
|
||||
if 'content-length' in headers.keys():
|
||||
self.content_len = int(headers['content-length'])
|
||||
|
||||
self.trailer = True
|
||||
|
||||
if self.trailer and len(self.data) >= self.content_len:
|
||||
#
|
||||
# TODO: handle request here
|
||||
#
|
||||
httpparser.parser(self.data)
|
||||
|
||||
self.sock.send("HTTP/1.0 200 OK\r\n\r\n")
|
||||
|
||||
|
@ -94,4 +88,3 @@ class ogClient:
|
|||
self.data = ""
|
||||
self.content_len = 0
|
||||
self.trailer = False
|
||||
print "DEBUG: request has been processed!"
|
||||
|
|
Loading…
Reference in New Issue