?? encoding_chunked.py
字號:
# encoding_chunked, amitp@cs.stanford.edu, March 2000## Deal with Transfer-encoding: chunked [HTTP/1.1]# TEST CASE:# http://www.apache.org/from proxy4_base import *class UnchunkStream: # States: # if bytes_remaining is None: # we're in the "need chunk size" state # else: # we're reading up to bytes_remaining elements of data def __init__(self): self.buffer = '' self.bytes_remaining = None self.closed = 0 def decode(self, string): self.buffer = self.buffer + string string = '' while self.buffer and not self.closed: # Keep looking for alternating chunk lengths and chunk content if self.bytes_remaining is None: # We want to find a chunk length i = find(self.buffer, '\n') if i >= 0: # We have a line; let's hope it's a chunk length line = strip(self.buffer[:i]) # Remove this line from the buffer self.buffer = self.buffer[i+1:] if line: # NOTE: chunklen can be followed by r";.*" self.bytes_remaining = atoi(line, 16) # chunklen is hex print 'chunk len:', color(2, self.bytes_remaining) if self.bytes_remaining == 0: # End of stream self.closed = 1 # NOTE: at this point, we should read # footers until we get to a blank line else: break if self.bytes_remaining is not None: # We know how many bytes we need data = self.buffer[:self.bytes_remaining] string = string + data self.buffer = self.buffer[self.bytes_remaining:] self.bytes_remaining = self.bytes_remaining - len(data) assert self.bytes_remaining >= 0 if self.bytes_remaining == 0: # We reached the end of the chunk self.bytes_remaining = None return string def flush(self): string = self.buffer self.buffer = '' return string
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -