def handle(self): httpRequestObj = self.parseHttpRequest() if not httpRequestObj: return httpResponseObj = None for (pathRe, actionFunc) in configuredURLs: m = pathRe.match(httpRequestObj.path) if m: httpRequestObj.pathGroup = m.groups() httpRequestObj.pathDict = m.groupdict() try: httpResponseObj = actionFunc(httpRequestObj) except: traceback.print_exc() self.sendResponse(httpRequestObj, HttpResponse.errorResponse(503, "Exception thrown")) return break if not httpResponseObj: self.sendResponse(httpRequestObj, HttpResponse.errorResponse(404, "Not Found")) return # Simulate delay from Internet 100ms, seems to help the theromostat # accept the response. time.sleep(0.1) self.sendResponse(httpRequestObj, httpResponseObj)
def urlWeather(request): postalCode = request.pathDict['postalCode'] host_url = "http://{}/weather/{}/forecast".format(request.host, postalCode) cliResp = requests.request( method=request.method, url=host_url, headers={key: value for (key, value) in request.headers if key != 'Host'}, data=request.body, allow_redirects=False) if cliResp.status_code != 200: return HttpResponse.errorResponse(cliResp.status_code, "Message") logging.info(cliResp.text) response = HttpResponse.okResponse() response.headers.append(("Cache-Control", "private")) response.addContentLengthHeader(len(cliResp.text)) response.addContentTypeHeader("application/xml; charset=utf-8") response.addServerHeader() response.addRequestContextHeader() response.addAccessControlHeader() response.addDateHeader() response.body = cliResp.text return response
def makeSystemsOduFaultsResponse(): response = HttpResponse.errorResponse(404, "Not found") #response = HttpResponse.okResponse() #response.headers.append(("Cache-Control", "private")) #response.addContentTypeHeader("application/xml; charset=utf-8") #response.addServerHeader() #response.addRequestContextHeader() #response.addAccessControlHeader() #response.addDateHeader() #response.addContentLengthHeader(0) return response
def makeApiResponse(code, message, body, contentType=None): if code == 200: response = HttpResponse.okResponse() else: response = HttpResponse.errorResponse(code, message) if body: response.addContentLengthHeader(len(body)) response.addContentTypeHeader(contentType) response.body = body response.addDateHeader() return response
def parseHttpRequest(self): first_line = self.rfile.readline().decode("utf-8") (http_method, http_path, http_version) = first_line.strip().split(" ") if not http_version == HttpRequest.VERSION_1_1: self.sendResponse(HttpRequest(http_version, http_method, http_path, ""), HttpResponse.errorResponse(400, "Bad Request")) return None http_query_string = None if '?' in http_path: (http_path, http_query_string) = http_path.split("?", 1) httpRequestObj = HttpRequest(http_version, http_method, http_path, http_query_string) next_line = self.rfile.readline().decode("utf-8") while not next_line == "\r\n": (k, v) = next_line.split(":", 1) # Remove space after : and \r\n at the end v = v[1:] v = v[:-2] httpRequestObj.headers.append((k, v)) next_line = self.rfile.readline().decode("utf-8") # Saw \r\n line, read body try: httpRequestObj.parseHeaders() except: traceback.print_exc() self.sendResponse(httpRequestObj, HttpResponse.errorResponse(400, "Bad Request")) return None if http_method == HttpRequest.METHOD_POST: if not httpRequestObj.contentLength or not httpRequestObj.contentType: return httpRequestObj # We use a non-blocking socket and set a timeout to try and limit # the chance of thermostat from locking up our server. Ideally # we should have done the same when reading the headers. self.connection.setblocking(0) numLeft = httpRequestObj.contentLength timeLeft = 15 * 10 httpRequestObj.body = "" while numLeft > 0: bytesRead = self.rfile.read1(numLeft) if not bytesRead: if timeLeft == 0: logging.warning(" Timeout witing for body, need {} more bytes".format(numLeft)) self.sendResponse(httpRequestObj, HttpResponse.errorResponse(400, "Bad Request")) return None time.sleep(0.1) timeLeft = timeLeft - 1 continue httpRequestObj.body = httpRequestObj.body + bytesRead.decode("utf-8") numLeft = numLeft - len(bytesRead) if not bytesRead and numLeft > 0: logging.warning(" Need {} more bytes from body".format(numLeft)) self.sendResponse(httpRequestObj, HttpResponse.errorResponse(400, "Bad Request")) return None httpRequestObj.parseBody() return httpRequestObj