예제 #1
0
파일: http_handler.py 프로젝트: akleiw/KSP
	def handle_call(self):
		# logging.debug("## %s", self.requestline)

		# all requests handled by the same Handler instance come from the same device, always
		device = self.last_device if hasattr(self, 'last_device') else None
		if device:
			logging.debug("%s last_device = %s", id(self), device)
		if not device:
			# look for a device record, will be automatically created if the features is enabled
			device = devices.detect(request.client_ip(self), cookie = request.xfsn(self),
									kind = request.guess_client(self), serial = request.get_device_serial(self))
			if not device:
				logging.error("failed to identify device for %s", self.requestline)
				return 403
			# if hasattr(self, 'last_device') and self.last_device != device:
			# 	logging.debug("identified device %s", device)
			if not device.is_provisional():
				self.last_device = device
		if device.context_failed(): # failed to create a proper SSL context
			logging.warn("denying access to unregistered device %s", device)
			return 401

		request.read_body_and_length(self)
		http_debug("%s", self)

		# strip possible path prefix
		self.path = self.path[self._prefix_len:]

		# and finally got to the part where we handle the request
		handler = self.server.find_handler(self)
		response = None
		if handler:
			try:
				response = handler.call(self, device)
			except ExceptionResponse as er:
				response = er.response
		if response is None:
			logging.warn("not found (%s) %s", self.headers.get('Host'), self.requestline)
			return 404
		if type(response) == int: # returned just a status code
			return response

		self.close_connection = response.will_close == True # will_close is a tristate...

		http_debug("replying with %s", response)
		self.send_response_only(response.status, response.reason)

		header_strings = [ k + ': ' + str(v) for k, v in response.headers.items() ]
		self.wfile.write(bytes('\r\n'.join(header_strings), 'latin1'))
		self.wfile.write(b'\r\n\r\n')

		byte_count = response.write_to(self.wfile) # writes the body
		self.log_request(response.status, byte_count)
예제 #2
0
파일: http_handler.py 프로젝트: MSylvia/KSP
	def _detect_device(self):
		# all requests handled by the same Handler instance come from the same device, always
		device = self.last_device
		if device:
			logging.debug("%s last_device = %s", id(self), device)
		if not device:
			# look for a device record, will be automatically created if the features is enabled
			device = devices.detect(request.client_ip(self), cookie = request.xfsn(self),
									kind = request.guess_client(self), serial = request.get_device_serial(self))
			if not device:
				logging.error("failed to identify device for %s", self.requestline)
				return None, 403
			if self.last_device != device:
				logging.debug("identified device %s", device)
			if not device.is_provisional():
				self.last_device = device
				logging.debug("guessed device %s", device)
		if device.context_failed(): # failed to create a proper SSL context
			logging.warn("denying access to unregistered device %s", device)
			return None, 401
		return device, None
예제 #3
0
 def _detect_device(self):
     # all requests handled by the same Handler instance come from the same device, always
     device = self.last_device
     if device:
         logging.debug("%s last_device = %s", id(self), device)
     if not device:
         # look for a device record, will be automatically created if the features is enabled
         device = devices.detect(request.client_ip(self),
                                 cookie=request.xfsn(self),
                                 kind=request.guess_client(self),
                                 serial=request.get_device_serial(self))
         if not device:
             logging.error("failed to identify device for %s",
                           self.requestline)
             return None, 403
         if self.last_device != device:
             logging.debug("identified device %s", device)
         if not device.is_provisional():
             self.last_device = device
             logging.debug("guessed device %s", device)
     if device.context_failed():  # failed to create a proper SSL context
         logging.warn("denying access to unregistered device %s", device)
         return None, 401
     return device, None