�
��@saddkZddkZddkZddkZddkZdefd��YZdeifd��YZ de
fd��YZdeifd ��YZ
dd
klZlZlZlZlZlZlZddkZddkZddkZddkZddkZdeifd��YZd
eifd��YZdad�Zd�Zd�Z dS(i�Nt
RangeErrorcBseZdZRS(s6Error raised when an unsatisfiable range is requested.(t__name__t
__module__t__doc__(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyRstHTTPRangeHandlercBs eZdZd�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
addinfourltget_full_urltcodetmsg(tselftreqtfpRR thdrstr((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pythttp_error_2066s cCstd��dS(NsRequested Range Not Satisfiable(R(R
RRRR R
((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pythttp_error_416=s(RRRRR(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyRs tRangeableFileObjectcBsheZdZd�Zd�Zd�Zdd�Zdd�Zdd�Zd �Z d
�Z
d�ZRS(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(tfotrange_tuple_normalizet firstbytetlastbytetrealpost_do_seek(R
Rtrangetup((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyt__init__Vs cCst|i|�S(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.(tgetattrR(R
tname((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyt__getattr__dscCs|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.
(RR(R
((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyttelljsicCs�|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(tAssertionErrorRRtIOErrorRR(R
toffsettwhencet
realoffset((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pytseekss
i�cCs:|i|�}|ii|�}|it|�7_|S(s`Read within the range.
This method will limit the size read based on the range.
(t_calc_read_sizeRtreadRtlen(R
tsizetrslt((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR%�scCs:|i|�}|ii|�}|it|�7_|S(sfRead lines within the range.
This method will limit the size read based on the range.
(R$RtreadlineRR&(R
R'R(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR)�scCs^|ioP|djo/|i||ijo|i|i}qVqZ|i|i}n|S(sSHandles calculating the amount of data to read based on
the range.
i�(RR(R
R'((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR$�s
cCsP|djpt�t|id|i�}||i|�|i|7_dS(s�Seek based on whether wrapped object supports seek().
offset is relative to the current position (self.realpos).
iR#N(RRRt_poor_mans_seekR(R
R R#((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR�scCs�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(RR%R&R(R
R tpostbufsizetbuf((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR*�s
(RRRRRRR#R%R)R$RR*(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyRAs tFileRangeHandlercBseZdZd�ZRS(s~FileHandler subclass that adds Range support.
This class handles Range headers exactly like an HTTP
server would.
cCs�ddk}ddk}|i�}|i�}ti|�}ti|�}|ti}|i i
|ti�} |i|�d}
|oOti
|�\}}|pti|�|i�jotid��q�nt|d�}|iidd�}
t|
�}
|
djpt�|
o}|
\}}|djo
|}n|djp||jp
||jotd��n||}t|||f�}n|id|
pd || f�}ti||d
|�S(Ni�isfile not on local hosttrbtRangetsRequested Range Not Satisfiables6Content-Type: %s
Content-Length: %d
Last-Modified: %s
s
text/plainsfile:((t mimetypestemailtget_hosttget_selectorRturl2pathnametoststattST_SIZEtUtilst
formatdatetST_MTIMEt
guess_typet splitporttsockett
gethostbynamet get_namesturllib2tURLErrortopentheaderstgettNonetrange_header_to_tupleRRRtmessage_from_stringR(R
RR2R3thosttfilet localfiletstatsR'tmodifiedtmtypetportRtbrangetfbtlbRE((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pytopen_local_file�s:
#
'
(RRRRT(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR.�s(R>t splitusertsplitpasswdt splitattrtunquotetaddclosehookRtFTPRangeHandlercBseZd�Zd�ZRS(cCs�|i�}|ptdd��nt|�\}}|djo
ti}n
t|�}t|�\}}|ot|�\}}nd}t |�}t |pd�}t |pd�}yt
i|�}Wn't
ij
o}t
i|��nXt|i��\}}|id�} tt | �} | d | d} }
| o| do| d} ny+|i||||| �}|
odpd }xP|D]H}
t|
�\}
}|
i�d
jo|djo|i�}q�q�Wd}t|iidd��}|djpt�|o'|\}}|djo
|}qVn|i|
||�\}}|o�|\}}|djo[|djp
|djotd��n|}||}|djotd��qq||}t|d|f�}nd}ti|i��d}|o|d|7}n|dj o|djo|d|7}nt i!|�}t"|||i��SWn4ti#j
o%}td|�t$i%�d�nXdS(Ns ftp errors
no host givenR1t/i�iitItDttypetatAtitdR0s@Requested Range Not Satisfiable due to unobtainable file length.sRequested Range Not SatisfiablesContent-Type: %s
sContent-Length: %d
i(R_R`RaR\RbR]((&R4RR>RGtftplibtFTP_PORTtintRURVRXR?R@terrorRBRCRWR5tsplittmaptconnect_ftptlowertupperRHRERFRtretrfileRRR2R=RR3RIRt
all_errorstsystexc_info(R
RRJRPtusertpasswdR tpathtattrstdirsRKtfwR^tattrtvaluetrestt range_tupRRRSRtretrlenRERO((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pytftp_open�s|
cCst|||||�}|S(N(t
ftpwrapper(R
RpRqRJRPRtRu((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyRiEs(RRR{Ri(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyRZ�s OR|cBseZdd�ZRS(c
Cs/|i�|djod}d}nd|}d}y|ii|�Wn0tij
o!|i�|ii|�nXd}|o,|o$y|ii|�Wn4tij
o%}t d|�t
i�d�nX|ii|�y#d |}|ii||�}Wq�tij
o�}t
|�id
�o;|i||�\}} t||df�}|| fSq�t
|�id�p t d|�t
i�d�q�q�Xn|pA|iid�|od
|}nd}|ii|�}nd|_t|did�|i�|dfS(NRbR]sTYPE AisTYPE is ftp errorisRETR t501R1t550sLIST tLISTR/(RbR](tendtransfertftptvoidcmdRcRmtinitRGtnlstt
error_permRRnRotntransfercmdtstrt
startswithRlRtbusyRYtmakefile(
R
RKR^RxtcmdtisdirtconntreasonRRz((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyRlNsJ
!
) N(RRRGRl(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR|IscCs�|djodSntdjoddk}|id�anti|�}|oNt|idd��}|o'|do|d|ddf}n|SndS(s�Get a (firstbyte,lastbyte) tuple from a Range header value.
Range headers have the form "bytes=<firstbyte>-<lastbyte>". 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.
i�Ns^bytes=(\d{1,})-(\d*)iii((RGt_rangeretretcompiletmatchRtgroup(trange_headerR�R�ttup((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyRH�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=<firstbyte>-<lastbyte>" or None
if no range is needed.
iisbytes=%s-%sN(RGR(Ry((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pytrange_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.
iR1isInvalid byte range: %s-%sN(NR1(iR1(RGRet
IndexErrorR(RyRRRS((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pyR�s&
(!R7R8RRBtemail.UtilsR3RRtBaseHandlerRtobjectRtFileHandlerR.R>RURVRWRXRYRRcR?RnR2t
FTPHandlerRZR|RGR�RHR�R(((sB/sys/lib/python/lib/python2.5/site-packages/mercurial/byterange.pys<module>s(#�,4T:
|