forked from xedakini/replicator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.py
More file actions
150 lines (118 loc) · 3.79 KB
/
Copy pathRequest.py
File metadata and controls
150 lines (118 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import Params, Protocol, socket, os, fiber
class HttpRequest:
def __init__( self, client ):
self.client = client
def recv( self ):
chunk = self.client.recv( Params.MAXCHUNK )
assert chunk, 'client closed connection prematurely'
return chunk
def __iter__( self ):
return self.__parse()
def __parse( self ):
recvbuf = ''
while '\n' not in recvbuf:
yield fiber.RECV( self.client, Params.TIMEOUT )
recvbuf += self.recv()
header, recvbuf = recvbuf.split( '\n', 1 )
print 'Client sends', header.rstrip()
self.__parse_header( header )
args = {}
while True:
while '\n' not in recvbuf:
yield fiber.RECV( self.client, Params.TIMEOUT )
recvbuf += self.recv()
line, recvbuf = recvbuf.split( '\n', 1 )
if ':' in line:
if Params.VERBOSE > 1:
print '>', line.rstrip()
key, value = line.split( ':', 1 )
key = key.title()
assert key not in args, 'duplicate key: %s' % key
args[ key ] = value.strip()
elif line and line != '\r':
print 'Ignored header line: %r' % line.rstrip()
else:
break
self.__parse_args( args )
if self.size:
if Params.VERBOSE:
print 'Opening temporary file for POST upload'
self.body = os.tmpfile()
self.body.write( recvbuf )
while self.body.tell() < self.size:
yield fiber.RECV( self.client, Params.TIMEOUT )
self.body.write( self.recv() )
assert self.body.tell() == self.size, 'message body exceeds content-length'
else:
assert not recvbuf, 'client sends junk data'
def __parse_header( self, line ):
fields = line.split()
assert len( fields ) == 3, 'invalid header line: %r' % line.rstrip()
cmd, url, dummy = fields
if url.startswith( 'http://' ):
host = url[ 7: ]
port = 80
if cmd == 'GET':
proto = Protocol.HttpProtocol
else:
proto = Protocol.BlindProtocol
elif url.startswith( 'ftp://' ):
assert cmd == 'GET', '%s request unsupported for ftp' % cmd
proto = Protocol.FtpProtocol
host = url[ 6: ]
port = 21
else:
raise AssertionError, 'invalid url: %s' % url
if '/' in host:
host, path = host.split( '/', 1 )
else:
path = ''
if ':' in host:
host, port = host.split( ':' )
port = int( port )
self.cmd = cmd
self.addr = ( host, port )
self.path = path
self.cache = '%s:%i/%s' % ( host, port, path )
self.Protocol = proto
def __parse_args( self, args ):
size = int( args.get( 'Content-Length', 0 ) )
if size:
assert self.cmd == 'POST', '%s request conflicts with message body' % self.cmd
if 'Range' in args:
try:
rangestr = args[ 'Range' ]
assert rangestr.startswith( 'bytes=' )
beg, end = rangestr[ 6: ].split( '-' )
if not beg:
range = int( end ), -1 # FIX!
elif not end:
range = int( beg ), -1
else:
range = int( beg ), int( end ) + 1
except:
raise AssertionError, 'invalid range specification: %s' % range
else:
range = 0, -1
args[ 'Host' ] = self.addr[ 0 ]
args[ 'Connection' ] = 'close'
args.pop( 'Keep-Alive', None )
args.pop( 'Proxy-Connection', None )
args.pop( 'Proxy-Authorization', None )
self.args = args
self.size = size
self.range = range
def recvbuf( self ):
lines = [ '%s /%s HTTP/1.1' % ( self.cmd, self.path ) ]
lines.extend( map( ': '.join, self.args.items() ) )
lines.append( '' )
if self.size:
self.body.seek( 0 )
lines.append( self.body.read() )
else:
lines.append( '' )
return '\r\n'.join( lines )
def __hash__( self ):
return hash( self.cache )
def __eq__( self, other ):
assert self.cache == other.cache