def acceptConnection(self): msgFromWs = io.readMessage(self.csocket) while msgFromWs != '<ENQ>' and msgFromWs != '<EOT>': msgFromWs = io.readMessage(self.csocket) if msgFromWs == '<ENQ>': io.writeMessage(self.csocket, '<ACK>') else: raise NetworkException('Impossible to accept the gateway connection. ')
def sendHTMLresponseToClient(self, html_page): pageLength = len(html_page) s = sm.setCode('HTTP/1.1', 200) s = sm.setMessageAnswer(s, 'OK') s = sm.setContentLength(s, pageLength) s = sm.setContentType(s, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + html_page io.writeMessage(self.csocket, s)
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 sendHomepage(self): page = io.readFile(os.curdir + '/index.html') pageLength = len(page) # 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, 'text/html') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + page io.writeMessage(self.csocket, s)
def sendFavicon(self): #favicon = io.readFile(os.curdir + '/favicon.ico') favicon = '' faviconLength = len(favicon) # s is the response message s = sm.setCode('HTTP/1.1', 200) s = sm.setMessageAnswer(s, 'OK') s = sm.setContentLength(s, faviconLength) s = sm.setContentType(s, 'image/x-icon') s = sm.setConnection(s, 'Close') s = sm.setServer(s, server_name + '\n\n') s = s + favicon print(f"Message to send to the client as response: favicon ") io.writeMessage(self.csocket, s)
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 sendNotExistingPath(self): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 404 Not Found </div> <div id="details"> The path specified in the HTTP request does not exist. </div>''' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 404) s = sm.setMessageAnswer(s, 'Not Found') 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)
def sendMethodNotAllowed(self): html_page = io.readFile(os.path.curdir + '/error.html') newContent = '''<body>\n<div id="msg_instruction"> 405 Method Not Allowed </div> <div id="details"> The method specified in the HTTP requested is not allowed. the only method supported is GET </div>''' v = html_page.split('<body>') updatedHTML = v[0] + newContent + v[1] page_length = len(updatedHTML) s = sm.setCode('HTTP/1.1', 405) s = sm.setMessageAnswer(s, 'Method Not Allowed') 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)
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)
def receiveMessageAndRespond(self, p_socket): message = io.readMessage(p_socket) if message != '<EOT>': content = message.split('<STX>')[1].split('<ETX>')[0] lrcReceived = message.split('<ETX>')[1] lrcCalculated = str(sm.getLRCvalueFromString(content)) receivedEOT = False print(f'lrcCalculated = {lrcCalculated}\nlrcReceived = {lrcReceived}') while (not receivedEOT) and (lrcCalculated != lrcReceived): io.writeMessage(p_socket, '<NACK>') message = io.readMessage(p_socket) if message == '<EOT>': receivedEOT = True else: content = message.split('<STX>')[1].split('<ETX>')[0] lrcReceived = message.split('<ETX>')[1] lrcCalculated = str(sm.getLRCvalueFromString(content)) if not receivedEOT: io.writeMessage(p_socket,'<ACK>') response = self.handleMessageAndGetResponse(content) lrcResponse = sm.getLRCvalueFromString(response) msgToSend = f'<STX>{response}<ETX>{lrcResponse}' io.writeMessage(p_socket,msgToSend) response = io.readMessage(p_socket) counter = 0 while response != '<ACK>' and '<EOT>' not in response and counter<4: counter += 1 io.writeMessage(p_socket, msgToSend) response = io.readMessage(p_socket) if response != '<ACK>' and '<EOT>' not in response: io.writeMessage(p_socket, '<EOT>') raise NetworkException('The gateway cannot receive data correctly. ') elif '<EOT>' not in response: endMessage = io.readMessage(p_socket) #waiting for <EOT> else: raise NetworkException("The processor couldn't receive data correctly from the Gateway. ")
def sendMessageAndGetResponse(self, p_socket, message): #returns the response lrc = sm.getLRCvalueFromString(message) s = '<STX>' + message + '<ETX>' + str(lrc) io.writeMessage(p_socket, s) response = io.readMessage(p_socket) counter = 0 while response != '<ACK>' and counter < 4: counter += 1 io.writeMessage(p_socket, s) response = io.readMessage(p_socket) if response != '<ACK>': io.writeMessage(p_socket, '<EOT>') raise NetworkException( 'The gateway server cannot receive data correctly. ') else: #<ACK> for the data sent response = io.readMessage(p_socket) #the answer receivedEOT = False while not receivedEOT and not sm.isLRC_ok(response): io.writeMessage(p_socket, '<NACK>') response = io.readMessage(p_socket) if response == '<EOT>': receivedEOT = True if receivedEOT == False: #means that I received correctly the response io.writeMessage(p_socket, '<ACK>') io.writeMessage(p_socket, '<EOT>') return response.split('<STX>')[1].split('<ETX>')[0] else: raise NetworkException( "The web server couldn't receive the response correctly from the gateway server. " )