Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of pyend instruction for execution of commands right after page serving #68

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 3 additions & 46 deletions MicroWebSrv2/httpRequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,24 @@
from binascii import a2b_base64
import json

# ============================================================================
# ===( HttpRequest )==========================================================
# ============================================================================




class HttpRequest :

MAX_RECV_HEADER_LINES = 100

# ------------------------------------------------------------------------

def __init__(self, microWebSrv2, xasCli) :
self._mws2 = microWebSrv2
self._xasCli = xasCli
self._waitForRecvRequest()

# ------------------------------------------------------------------------

def _recvLine(self, onRecv) :
self._xasCli.AsyncRecvLine(onLineRecv=onRecv, timeoutSec=self._mws2._timeoutSec)

# ------------------------------------------------------------------------

def _waitForRecvRequest(self) :
self._httpVer = ''
Expand All @@ -40,7 +37,6 @@ def _waitForRecvRequest(self) :
self._response = HttpResponse(self._mws2, self)
self._recvLine(self._onFirstLineRecv)

# ------------------------------------------------------------------------

def _onFirstLineRecv(self, xasCli, line, arg) :
try :
Expand All @@ -65,7 +61,6 @@ def _onFirstLineRecv(self, xasCli, line, arg) :
except :
self._response.ReturnBadRequest()

# ------------------------------------------------------------------------

def _onHeaderLineRecv(self, xasCli, line, arg) :
try :
Expand All @@ -83,7 +78,6 @@ def _onHeaderLineRecv(self, xasCli, line, arg) :
except :
self._response.ReturnBadRequest()

# ------------------------------------------------------------------------

def _processRequest(self) :
if not self._processRequestModules() :
Expand Down Expand Up @@ -113,7 +107,6 @@ def _processRequest(self) :
else :
self._response.ReturnNotImplemented()

# ------------------------------------------------------------------------

def _processRequestModules(self) :
for modName, modInstance in self._mws2._modules.items() :
Expand All @@ -127,7 +120,6 @@ def _processRequestModules(self) :
self._mws2.ERROR )
return False

# ------------------------------------------------------------------------

def _processRequestRoutes(self) :
self._routeResult = ResolveRoute(self._method, self._path)
Expand Down Expand Up @@ -156,7 +148,6 @@ def onContentRecv(xasCli, content, arg) :
return True
return False

# ------------------------------------------------------------------------

def _routeRequest(self) :
try :
Expand All @@ -175,7 +166,6 @@ def _routeRequest(self) :
self._mws2.ERROR )
self._response.ReturnInternalServerError()

# ------------------------------------------------------------------------

def GetPostedURLEncodedForm(self) :
res = { }
Expand All @@ -191,7 +181,6 @@ def GetPostedURLEncodedForm(self) :
pass
return res

# ------------------------------------------------------------------------

def GetPostedJSONObject(self) :
if self.ContentType.lower() == 'application/json' :
Expand All @@ -202,14 +191,12 @@ def GetPostedJSONObject(self) :
pass
return None

# ------------------------------------------------------------------------

def GetHeader(self, name) :
if not isinstance(name, str) or len(name) == 0 :
raise ValueError('"name" must be a not empty string.')
return self._headers.get(name.lower(), '')

# ------------------------------------------------------------------------

def CheckBasicAuth(self, username, password) :
if not isinstance(username, str) :
Expand All @@ -229,7 +216,6 @@ def CheckBasicAuth(self, username, password) :
pass
return False

# ------------------------------------------------------------------------

def CheckBearerAuth(self, token) :
if not isinstance(token, str) :
Expand All @@ -245,55 +231,46 @@ def CheckBearerAuth(self, token) :
pass
return False

# ------------------------------------------------------------------------

@property
def UserAddress(self) :
return self._xasCli.CliAddr

# ------------------------------------------------------------------------

@property
def IsSSL(self) :
return self._xasCli.IsSSL

# ------------------------------------------------------------------------

@property
def HttpVer(self) :
return self._httpVer

# ------------------------------------------------------------------------

@property
def Method(self) :
return self._method

# ------------------------------------------------------------------------

@property
def Path(self) :
return self._path

# ------------------------------------------------------------------------

@property
def QueryString(self) :
return self._queryString

# ------------------------------------------------------------------------

@property
def QueryParams(self) :
return self._queryParams

# ------------------------------------------------------------------------

@property
def Host(self) :
return self._headers.get('host', '')

# ------------------------------------------------------------------------

@property
def Accept(self) :
Expand All @@ -302,7 +279,6 @@ def Accept(self) :
return [x.strip() for x in s.split(',')]
return [ ]

# ------------------------------------------------------------------------

@property
def AcceptEncodings(self) :
Expand All @@ -311,7 +287,6 @@ def AcceptEncodings(self) :
return [x.strip() for x in s.split(',')]
return [ ]

# ------------------------------------------------------------------------

@property
def AcceptLanguages(self) :
Expand All @@ -320,7 +295,6 @@ def AcceptLanguages(self) :
return [x.strip() for x in s.split(',')]
return [ ]

# ------------------------------------------------------------------------

@property
def Cookies(self) :
Expand All @@ -329,25 +303,21 @@ def Cookies(self) :
return [x.strip() for x in s.split(';')]
return [ ]

# ------------------------------------------------------------------------

@property
def CacheControl(self) :
return self._headers.get('cache-control', '')

# ------------------------------------------------------------------------

@property
def Referer(self) :
return self._headers.get('referer', '')

# ------------------------------------------------------------------------

@property
def ContentType(self) :
return self._headers.get('content-type', '').split(';', 1)[0].strip()

# ------------------------------------------------------------------------

@property
def ContentLength(self) :
Expand All @@ -356,60 +326,47 @@ def ContentLength(self) :
except :
return 0

# ------------------------------------------------------------------------

@property
def UserAgent(self) :
return self._headers.get('user-agent', '')

# ------------------------------------------------------------------------

@property
def Authorization(self) :
return self._headers.get('authorization', '')

# ------------------------------------------------------------------------

@property
def Origin(self) :
return self._headers.get('origin', '')

# ------------------------------------------------------------------------

@property
def IsKeepAlive(self) :
return ('keep-alive' in self._headers.get('connection', '').lower())

# ------------------------------------------------------------------------

@property
def IsUpgrade(self) :
return ('upgrade' in self._headers.get('connection', '').lower())

# ------------------------------------------------------------------------

@property
def Upgrade(self) :
return self._headers.get('upgrade', '')

# ------------------------------------------------------------------------

@property
def Content(self) :
return self._content

# ------------------------------------------------------------------------

@property
def Response(self) :
return self._response

# ------------------------------------------------------------------------

@property
def XAsyncTCPClient(self) :
return self._xasCli

# ============================================================================
# ============================================================================
# ============================================================================
Loading