Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(68563)

Side by Side Diff: Lib/httplib.py

Issue 16037: httplib: header parsing is not delimited
Patch Set: Created 5 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Lib/test/test_httplib.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """HTTP/1.1 client library 1 """HTTP/1.1 client library
2 2
3 <intro stuff goes here> 3 <intro stuff goes here>
4 <other stuff, too> 4 <other stuff, too>
5 5
6 HTTPConnection goes through a number of "states", which define when a client 6 HTTPConnection goes through a number of "states", which define when a client
7 may legally make another request or fetch the response for a particular 7 may legally make another request or fetch the response for a particular
8 request. This diagram details these state transitions: 8 request. This diagram details these state transitions:
9 9
10 (null) 10 (null)
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 500: 'Internal Server Error', 203 500: 'Internal Server Error',
204 501: 'Not Implemented', 204 501: 'Not Implemented',
205 502: 'Bad Gateway', 205 502: 'Bad Gateway',
206 503: 'Service Unavailable', 206 503: 'Service Unavailable',
207 504: 'Gateway Timeout', 207 504: 'Gateway Timeout',
208 505: 'HTTP Version Not Supported', 208 505: 'HTTP Version Not Supported',
209 } 209 }
210 210
211 # maximal amount of data to read at one time in _safe_read 211 # maximal amount of data to read at one time in _safe_read
212 MAXAMOUNT = 1048576 212 MAXAMOUNT = 1048576
213
214 # maximum amount of headers accepted
215 _MAXHEADERS = 100
216
213 217
214 class HTTPMessage(mimetools.Message): 218 class HTTPMessage(mimetools.Message):
215 219
216 def addheader(self, key, value): 220 def addheader(self, key, value):
217 """Add header for field key handling repeats.""" 221 """Add header for field key handling repeats."""
218 prev = self.dict.get(key) 222 prev = self.dict.get(key)
219 if prev is None: 223 if prev is None:
220 self.dict[key] = value 224 self.dict[key] = value
221 else: 225 else:
222 combined = ", ".join((prev, value)) 226 combined = ", ".join((prev, value))
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 self.headers = hlist = [] 264 self.headers = hlist = []
261 self.status = '' 265 self.status = ''
262 headerseen = "" 266 headerseen = ""
263 firstline = 1 267 firstline = 1
264 startofline = unread = tell = None 268 startofline = unread = tell = None
265 if hasattr(self.fp, 'unread'): 269 if hasattr(self.fp, 'unread'):
266 unread = self.fp.unread 270 unread = self.fp.unread
267 elif self.seekable: 271 elif self.seekable:
268 tell = self.fp.tell 272 tell = self.fp.tell
269 while True: 273 while True:
274 if len(hlist) > _MAXHEADERS:
275 raise TooManyHeaders()
270 if tell: 276 if tell:
271 try: 277 try:
272 startofline = tell() 278 startofline = tell()
273 except IOError: 279 except IOError:
274 startofline = tell = None 280 startofline = tell = None
275 self.seekable = 0 281 self.seekable = 0
276 line = self.fp.readline() 282 line = self.fp.readline()
277 if not line: 283 if not line:
278 self.status = 'EOF in headers' 284 self.status = 'EOF in headers'
279 break 285 break
(...skipping 915 matching lines...) Expand 10 before | Expand all | Expand 10 after
1195 class CannotSendHeader(ImproperConnectionState): 1201 class CannotSendHeader(ImproperConnectionState):
1196 pass 1202 pass
1197 1203
1198 class ResponseNotReady(ImproperConnectionState): 1204 class ResponseNotReady(ImproperConnectionState):
1199 pass 1205 pass
1200 1206
1201 class BadStatusLine(HTTPException): 1207 class BadStatusLine(HTTPException):
1202 def __init__(self, line): 1208 def __init__(self, line):
1203 self.args = line, 1209 self.args = line,
1204 self.line = line 1210 self.line = line
1211
1212
1213 class TooManyHeaders(HTTPException):
barry 2013/09/29 19:26:27 We can't introduce a new exception in 2.6.9. Why
1214 def __init__(self):
1215 HTTPException.__init__(self, "got more than %d headers" % _MAXHEADERS)
1205 1216
1206 # for backwards compatibility 1217 # for backwards compatibility
1207 error = HTTPException 1218 error = HTTPException
1208 1219
1209 class LineAndFileWrapper: 1220 class LineAndFileWrapper:
1210 """A limited file-like object for HTTP/0.9 responses.""" 1221 """A limited file-like object for HTTP/0.9 responses."""
1211 1222
1212 # The status-line parsing code calls readline(), which normally 1223 # The status-line parsing code calls readline(), which normally
1213 # get the HTTP status line. For a 0.9 response, however, this is 1224 # get the HTTP status line. For a 0.9 response, however, this is
1214 # actually the first line of the body! Clients need to get a 1225 # actually the first line of the body! Clients need to get a
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
1334 print 'status =', status 1345 print 'status =', status
1335 print 'reason =', reason 1346 print 'reason =', reason
1336 print "read", len(hs.getfile().read()) 1347 print "read", len(hs.getfile().read())
1337 print 1348 print
1338 if headers: 1349 if headers:
1339 for header in headers.headers: print header.strip() 1350 for header in headers.headers: print header.strip()
1340 print 1351 print
1341 1352
1342 if __name__ == '__main__': 1353 if __name__ == '__main__':
1343 test() 1354 test()
OLDNEW
« no previous file with comments | « no previous file | Lib/test/test_httplib.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+