def handle_read(self): if (not self.isDownloading): ## sending header + deciding self.logger.debug("Host sending header data") header_string = self.recv(4096) self.logger.debug('%s -> (%i)'%(header_string,len(header_string))) if (len(header_string)>0 and HeaderHandler.get_headerpad() in header_string): headerHandler = HeaderHandler(header_string) self.logger.debug("Header:\n%s"%str(headerHandler)) if ('Content-Length' in headerHandler): contentLength = int(headerHandler.get_info('Content-Length')) self.totalContentLength = contentLength global cache_threshold if (contentLength>int(cache_threshold)): self.perform_accelerated_request(contentLength) else: ### Perform cache download self.perform_cache_request(headerHandler) else: self.perform_request() else: self.logger.debug("No header returned.") #self.perform_request() #time.sleep(1) self.close()
def handle_connect(self): self.logger.debug("Sending HEAD request") host=self.request.get_info("Host") path =self.request.get_info("Path") head_request = HeaderHandler.create_headerrequest(host, path=path) self.logger.debug(head_request) self.send(head_request)
def handle_read(self): self.logger.debug("Host sending data") try: self.logger.debug("reading from socket") data = self.recv(2048) self.clientSock.send(data) ##pass to client self.data += data self.logger.debug(len(self.data)) self.logger.debug(self.contentLength) if (HeaderHandler.get_headerpad() in data): ##HeaderHandeler.get_headerpad()-->\r\n\r\n headerstring = data.split(HeaderHandler.get_headerpad())[0]+HeaderHandler.get_headerpad() self.logger.debug(headerstring) headerHandler = HeaderHandler(headerstring) if (headerHandler.get_info('Content-Length')!=None): self.contentLength = int(headerHandler.get_info('Content-Length')) + len(headerstring) else: global total_cache_size self.contentLength = total_cache_size if (len(self.data)>= self.contentLength and not(self.isDoneCaching)): print("updated") self.cacheTable.update(self.cacheKey, self.data) self.isDoneCaching = True except(KeyboardInterrupt): self.logger.debug("exception caught. Done downloading") self.close()
def handle_read(self): try: #self.logger.debug("reading from socket") data = self.recv(4096) #self.logger.debug(str(data)) if (HeaderHandler.get_headerpad() in data): ## chunked encoding header fromRange, toRange = int(self.chunkRange[0]), int(self.chunkRange[1]) headerAndData = data.split(HeaderHandler.get_headerpad()) header_string = headerAndData[0]+HeaderHandler.get_headerpad() headerObj = HeaderHandler(header_string) self.logger.debug("headerPan: \n%s"%str(headerObj)) #headerObj.update('Transfer-Encoding', 'chunked') headerObj.update('HTTP', 'HTTP/1.1 200 OK') contentLength = headerObj.get_info('Content-Length') if (contentLength==None): contentLength = toRange-fromRange+1 ## important +1, range counts itself on both sides else: contentLength = int(contentLength) headerObj.remove('Content-Length') headerObj.remove('Content-Range') self.contentLength = contentLength header_string = headerObj.get_request() # hexstring = ''#str(hex(contentLength))[2:] # _data = hexstring+'\r\n'+headerAndData[1] _data = headerAndData[1] # add header or nott if (self.isFirstTime): data = header_string + _data else: data = _data self.curLength+=len(data) chunkBuffer = self.chunkTable[self.chunkIndex] ## accessing buffer collection chunkBuffer.append(data) self.streamToMaster() ''' no longer doing chunk transfer''' # if (len(self.data)>=self.contentLength): # #if(self.isLastTime): self.data+='\r\n0' # #self.data+='\r\n' # self.logger.debug("Host sending data") # self.logger.debug("size-->%s"%len(self.data)) # self.logger.debug('\n%r\n'%self.data) # #self.clientSock.send(data) except(KeyboardInterrupt, socket.timeout): self.logger.debug("exception caught. Done downloading") self.close()