def parseRequest(self): msgFromClient = io.readMessage(self.csocket) print(f"The path is: {self.getPath(msgFromClient)}\n") if not (self.checkMethod(msgFromClient)): self.sendMethodNotAllowed() io.closeConnection(self.csocket) elif not self.isPathCorrect(msgFromClient): self.sendNotExistingPath() io.closeConnection(self.csocket) elif self.isFaviconRequest(msgFromClient): self.sendFavicon() io.closeConnection(self.csocket) elif self.isGatewayRequest(msgFromClient): self.handleGatewayRequest(msgFromClient) #the connection will be closed by the handleGatewayRequest method elif self.isHomepageRequest(msgFromClient): self.sendHomepage() io.closeConnection(self.csocket) elif self.isFrontendRequest(msgFromClient): self.manageFrontendRequest(msgFromClient) #the connection will be closed by the manageFrontendRequest else: self.sendBadRequest() io.closeConnection(self.csocket)
def manageAuthRequest(self, msgFromClient): if self.isDataAuthCorrect(msgFromClient): s = self.formatAuthRequest(msgFromClient) responseFromGateway = self.sendMessageAndGetResponse(self.gs, s) self.sendHTMLresponseToClient(responseFromGateway) else: self.sendBadRequest() io.writeMessage(self.gs, '<EOT>') io.closeConnection(self.gs)
def manageFrontendRequest(self, msgFromClient): path = self.getPath(msgFromClient) contentType = 'text/' + path.split('.')[-1] code = io.readFile(os.curdir + path) pageLength = len(code) # s is the response message s = sm.setCode('HTTP/1.1', 200) s = sm.setMessageAnswer(s, 'OK') s = sm.setContentLength(s, pageLength) s = sm.setContentType(s, contentType) s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + code io.writeMessage(self.csocket, s) io.closeConnection(self.csocket)
def sendCommunicationError(self, exc): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 409 Conflict </div> <div id="details"> The gateway server cannot be reached properly. Try again later.<br></div>''' newContent = newContent + f'<div>Error detected: {exc}</div>' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 409) s = sm.setMessageAnswer(s, 'Conflict') s = sm.setContentLength(s, page_length) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + updatedHTML io.writeMessage(self.csocket, s) io.closeConnection(self.csocket)
def sendBadRequest(self): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 400 Bad Request </div> <div id="details"> The server cannot process your request. If it's an auth request, check that all the parameters:<br> name, cardNumber, cvv, expDate and amount are present in the url. </div>''' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 400) s = sm.setMessageAnswer(s, 'Bad Request') s = sm.setContentLength(s, page_length) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + updatedHTML io.writeMessage(self.csocket, s) io.closeConnection(self.csocket)