Example #1
0
	def handleClient(self,fileDesc):
		while True:
			try:
				currentClient = self.clients.get(fileDesc, None)
				data = None
				if currentClient:
					data = currentClient.recv(self.size)
				if data:
					self.clientFiles[fileDesc].cache += data
					if self.validRequestInCache(fileDesc):
						break;		
				else:
					if self.debug:
						print "[handleClient] Closing Client #" + str(fileDesc)
					self.poller.unregister(fileDesc)
					self.closeClient(fileDesc)
					return
			except errno.EAGAIN:
				if self.debug:
					print "[RECV ERROR] Try again"
				return
			except errno.EWOULDBLOCK:
				if self.debug:
					print "[RECV ERROR] Operation would block"
				return
		
		if self.debug: print "[DATA]\n" + data + "\n"
		
		requestData = self.removeRequestFromCache(fileDesc)
			
		if self.debug: print "[REQUEST DATA]\n" + requestData + "\n"
		
		request = HTTPRequest(requestData)
		reqCom = request.command
		
		if not (reqCom == 'GET' or reqCom == 'DELETE' or reqCom == 'PUT' or reqCom == 'TRACE' \
		or reqCom == 'HEAD' or reqCom == 'OPTIONS' or reqCom == 'CONNECT'):
			response = self.create400Response()
			if self.debug:
				print "*************** Sending Response ***************\n" + response + "************************************************"
			self.sendResponse(fileDesc, response)
			return
			
		elif reqCom != 'GET' and reqCom != 'HEAD':
			response = self.create501Response()
			if self.debug:
				print "*************** Sending Response ***************\n" + response + "************************************************"
			self.sendResponse(fileDesc, response)
			return

		elif reqCom == 'GET' or reqCom == 'HEAD':
			if request.headers['host'].find(":"):
				host = request.headers['host'][:request.headers['host'].find(":")]
			host = request.headers['host']
			hostPath = self.hosts.get(host, None)
			
			if not hostPath:
				hostPath = self.hosts['default']
				
			if not request.path:
				request.path = "/index.html"
				
			if request.path == "/":
				request.path = "/index.html"
			
			filePath = hostPath + request.path
			
			if not os.path.isfile(filePath):
				response = self.create404Response()
				if self.debug:
					print "*************** Sending Response ***************\n" + response + "************************************************"
				self.sendResponse(fileDesc, response)
				return
			
			if not os.access(filePath, os.R_OK):
				response = self.create403Response()
				if self.debug:
					print "*************** Sending Response ***************\n" + response + "\n************************************************"
				self.sendResponse(fileDesc, response)
				return	
				
			reqFile = open(filePath);
			response = self.create200Response(reqFile)
			if self.debug:
				print "*************** Sending Response ***************\n" + response + "File Contents go here\n ************************************************"
			self.sendResponse(fileDesc, response)
			
			if reqCom == 'HEAD':
				return
						
			if request.headers.get('range', None):
				rangeArray = request.headers['range'].split('=')[1].split('-')
				reqFile.seek(int(rangeArray[0]))
				filePiece = reqFile.read(int(rangeArray[1]) - int(rangeArray[0]))
				self.sendResponse(fileDesc, filePiece)
				return
						
			while True:
				filePiece = reqFile.read(self.size)
				if filePiece == "":
					break
				self.sendResponse(fileDesc, filePiece)