mò óRÅDc@ssdkZdkZdkZdkZdkZydklZWn"ej oZdklZnXde fd„ƒYZ dei fd„ƒYZ dfd„ƒYZ deifd „ƒYZd klZlZlZlZlZlZlZdkZdkZdkZdkZdkZd eifd „ƒYZd eifd„ƒYZda d„Z!d„Z"d„Z#dS(N(sStringIOt RangeErrorcBstZdZRS(s6Error raised when an unsatisfiable range is requested.(t__name__t __module__t__doc__(((t7/usr/lib/python2.4/site-packages/mercurial/byterange.pyR!s tHTTPRangeHandlercBs tZdZd„Zd„ZRS(s˜Handler that enables HTTP Range headers. This was extremely simple. The Range header is a HTTP feature to begin with so all this class does is tell urllib2 that the "206 Partial Content" reponse from the HTTP server is what we expected. Example: import urllib2 import byterange range_handler = range.HTTPRangeHandler() opener = urllib2.build_opener(range_handler) # install it urllib2.install_opener(opener) # create Request and set Range header req = urllib2.Request('http://www.python.org/') req.header['Range'] = 'bytes=30-50' f = urllib2.urlopen(req) cCs1ti|||iƒƒ}||_||_|S(N( turllibt addinfourltfpthdrstreqt get_full_urltrtcodetmsg(tselfR RR RR R ((Rthttp_error_206=s  cCstdƒ‚dS(NsRequested Range Not Satisfiable(R(RR RR RR ((Rthttp_error_416Ds(RRRRR(((RR%s  tRangeableFileObjectcBshtZdZd„Zd„Zd„Zdd„Zdd„Zdd„Zd „Z d „Z d „Z RS( sFile object wrapper to enable raw range handling. This was implemented primarilary for handling range specifications for file:// urls. This object effectively makes a file object look like it consists only of a range of bytes in the stream. Examples: # expose 10 bytes, starting at byte position 20, from # /etc/aliases. >>> fo = RangeableFileObject(file('/etc/passwd', 'r'), (20,30)) # seek seeks within the range (to position 23 in this case) >>> fo.seek(3) # tell tells where your at _within the range_ (position 3 in # this case) >>> fo.tell() # read EOFs if an attempt is made to read past the last # byte in the range. the following will return only 7 bytes. >>> fo.read(30) cCs>||_t|ƒ\|_|_d|_|i|iƒdS(s}Create a RangeableFileObject. fo -- a file like object. only the read() method need be supported but supporting an optimized seek() is preferable. rangetup -- a (firstbyte,lastbyte) tuple specifying the range to work over. The file object provided is assumed to be at byte offset 0. iN(tfoRtrange_tuple_normalizetrangetupt firstbytetlastbytetrealpost_do_seek(RRR((Rt__init__]s   cCs4t|i|ƒot|i|ƒSnt|‚dS(s«This effectively allows us to wrap at the instance level. Any attribute not found in _this_ object will be searched for in self.fo. This includes methods.N(thasattrRRtnametgetattrtAttributeError(RR((Rt __getattr__kscCs|i|iS(s4Return the position within the range. This is different from fo.seek in that position 0 is the first byte position of the range tuple. For example, if this object was created with a range tuple of (500,899), tell() will return 0 when at byte position 500 of the file. N(RRR(R((RttellssicCs¬|djpt‚|djo|i|}n<|djo|i|}n|djotdƒ‚n|io||ijo |i}n|i ||iƒdS(seSeek within the byte range. Positioning is identical to that described under tell(). iiis$seek from end of file not supported.N(iii( twhencetAssertionErrorRRtoffsett realoffsetRtIOErrorRR(RR#R!R$((Rtseek|s    iÿÿÿÿcCs:|i|ƒ}|ii|ƒ}|it|ƒ7_|S(s`Read within the range. This method will limit the size read based on the range. N(Rt_calc_read_sizetsizeRtreadtrsltRtlen(RR(R*((RR)s cCs:|i|ƒ}|ii|ƒ}|it|ƒ7_|S(sfRead lines within the range. This method will limit the size read based on the range. N(RR'R(RtreadlineR*RR+(RR(R*((RR,˜s cCs^|ioP|djo/|i||ijo|i|i}qVqZ|i|i}n|S(sSHandles calculating the amount of data to read based on the range. iÿÿÿÿN(RRR(R(RR(((RR'¡s  cCsb|djpt‚t|idƒp|i|ƒn|ii|i|ƒ|i|7_dS(sSeek based on whether wrapped object supports seek(). offset is relative to the current position (self.realpos). iR&N(R#R"RRRt_poor_mans_seekR&R(RR#((RR­s cCsƒd}d}xp||job|||jo||}n|ii|ƒ}t|ƒ|jotdƒ‚n||7}qWdS(s®Seek by calling the wrapped file objects read() method. This is used for file like objects that do not have native seek support. The wrapped objects read() method is called to manually seek to the desired position. offset -- read this number of bytes from the wrapped file object. raise RangeError if we encounter EOF before reaching the specified offset. iisRequested Range Not SatisfiableN( tpostbufsizeR#RRR)tbufR+R(RR#R.R/R0((RR-¸s  ( RRRRRR R&R)R,R'RR-(((RRHs     tFileRangeHandlercBstZdZd„ZRS(s~FileHandler subclass that adds Range support. This class handles Range headers exactly like an HTTP server would. cCsÜdk} dk}|iƒ} |iƒ}ti|ƒ} t i | ƒ}|t i }ti|t iƒ}| i|ƒd}| oOti| ƒ\} }|pti| ƒ|iƒjotidƒ‚qÔnt| dƒ}|ii ddƒ}t#|ƒ}|fjpt$‚|o}|\} } | djo |} n| djp| |jp | |jot'dƒ‚n| | }t(|| | fƒ}n|i)t*d|pd||fƒƒ}ti+||d |ƒS( Nisfile not on local hosttrbtRangetsRequested Range Not Satisfiables6Content-Type: %s Content-Length: %d Last-modified: %s s text/plainsfile:(,t mimetypest mimetoolsR tget_hostthostt get_selectortfileRt url2pathnamet localfiletoststattstatstST_SIZER(trfc822t formatdatetST_MTIMEtmodifiedt guess_typetmtypet splitporttporttsockett gethostbynameRt get_namesturllib2tURLErrortopenRtheaderstgettNonetbrangetrange_header_to_tupleR"tfbtlbRRtMessagetStringIOR(RR R6RRRFR:RHR(R?RTR<R5R8RURRDRO((Rtopen_local_fileÑs6     #   ' )(RRRRX(((RR1Ìs (s splitports splitusers splitpasswds splitattrsunquotes addclosehooks addinfourltFTPRangeHandlercBstZd„Zd„ZRS(NcCsÎ|iƒ}|p td‚nt|ƒ\}}|djo ti}nt |ƒ\}}|ot |ƒ\}} nd} t |ƒ}t |pdƒ}t | pdƒ} yti|ƒ}Wn'tij o}ti|ƒ‚nXt|iƒƒ\}}|idƒ} tt | ƒ} | d | d} }| o| d o| d} ny7|i|| ||| ƒ} |odpd } xP|D]H}t|ƒ\}}|i"ƒd jo|djo|i#ƒ} q•q•Wd}t%|i&i'ddƒƒ}|fjpt)‚|o'|\}} |djo |}qDn| i,|| |ƒ\}}|o—|\}} | djo[|djp |djot/dƒ‚n|} | |}|djot/dƒ‚qùqý| |}t0|d|fƒ}nd}t1i2|i3ƒƒd}|o|d|7}n|dj o|djo|d|7}nt5|ƒ}t7i8|ƒ}t9|||i3ƒƒSWn4ti:j o%}td|ft;i<ƒd‚nXdS(Ns ftp errors no host givenR4t/iÿÿÿÿiitItDttypetatAtitdR3s@Requested Range Not Satisfiable due to unobtainable file length.sRequested Range Not SatisfiablesContent-Type: %s sContent-Length: %d i(s ftp errors no host given(R^R_R`R[RaR\(=R R7R8R%RGRHRQtftplibtFTP_PORTt splitusertusert splitpasswdtpasswdtunquoteRIRJterrorRRLRMt splitattrR9tpathtattrstsplittdirstmapR:Rt connect_ftptfwR]tattrtvaluetlowertuppertrestRSRORPt range_tupR"RTRUtretrfileRtretrlenRRR5RER RFRWtsfR6RVRt all_errorstsystexc_info(RR RRwRvRFRlR:RHRUR]RnRqRgRR8ReRkRyRrRTRsRORz((Rtftp_opensz              cCst|||||ƒ}|S(N(t ftpwrapperReRgR8RHRnRq(RReRgR8RHRnRq((RRpLs(RRR~Rp(((RRYÿs LRcBstZdd„ZRS(Nc Cs/|iƒ|djod}d}nd|}d}y|ii|ƒWn0tij o!|i ƒ|ii|ƒnXd} |o,| o$y|ii |ƒWn4tij o%}td|ftiƒd‚nX|ii|ƒy#d |}|ii||ƒ} Wq·tij o}t|ƒid ƒo;|i||ƒ\}}t||d fƒ}||fSq³t|ƒid ƒp td|ftiƒd‚q³q·Xn| pA|iidƒ|od |}nd}|ii|ƒ} nd|_t| didƒ|iƒ| dfS(NRaR\sTYPE AisTYPE is ftp errorisRETR t501R4t550sLIST tLISTR2(RaR\(Rt endtransferR]tcmdtisdirtftptvoidcmdRbR{tinitRQtconnR:tnlstt error_permtreasonR%R|R}t ntransfercmdRvtstrt startswithRxRRyRtbusyt addclosehooktmakefile( RR:R]RvR…R„RŒRRyR‰((RRxUsF   ! ) (RRRQRx(((RRPscCs¦|djodSntdjodk}|idƒanti|ƒ}|oNt|iddƒƒ}|o'|do|d|ddf}n|SnfS(sËGet a (firstbyte,lastbyte) tuple from a Range header value. Range headers have the form "bytes=-". This function pulls the firstbyte and lastbyte values and returns a (firstbyte,lastbyte) tuple. If lastbyte is not specified in the header value, it is returned as an empty string in the tuple. Return None if range_header is None Return () if range_header does not conform to the range spec pattern. Ns^bytes=(\d{1,})-(\d*)iii( t range_headerRQt_rangeretretcompiletmatchRtgroupttup(R“R™R•R—((RRS…s    cCs_|djodSnt|ƒ}|o3|do|d|ddf}nd|SndS(s•Convert a range tuple to a Range header value. Return a string of the form "bytes=-" or None if no range is needed. iis bytes=%s-%sN(RwRQR(Rw((Rtrange_tuple_to_header s   cCsì|djodSn|d}|djo d}n t|ƒ}y|d}Wntj o d}n6X|djo d}n|djot|ƒ}n||fdjodSn||jotd||fƒ‚n||fS(s6Normalize a (first_byte,last_byte) range tuple. Return a tuple whose first element is guaranteed to be an int and whose second element will be '' (meaning: the last byte) or an int. Finally, return None if the normalized tuple == (0,'') as that is equivelant to retrieving the entire file. iR4isInvalid byte range: %s-%sN(NR4(iR4(RwRQRTtintRUt IndexErrorR(RwRTRU((RR¬s(        ($R=R>RRLRAt cStringIORWt ImportErrorRR%Rt BaseHandlerRRt FileHandlerR1RGRdRfRjRhR‘RRbRIR|R5R6t FTPHandlerRYRRQR”RSRšR(RhR6RR5RšRRGRdRRR1RRbR>R‘RARRLR|RjRRIRWRSRRfRYR=((Rt?s0     #„+1     Q4