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 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)