def post_page(self): body_parts = self.request.body.split(b'\r\n--') for part in body_parts: field_part = re.search(b'name="(?P<name>.+)"\r\n\r\n(?P<value>.*)', part) file_part = re.search( b'name="(?P<name>.+)"; filename="(?P<filename>.+)"\r\n' b'Content-Type: application/octet-stream\r\n\r\n(?P<value>.*)', part) if field_part: val = field_part.group('value') name = any_to_unicode(field_part.group('name')) if isinstance(FIELDS[name], list) and all( val != any_to_bytes(x) for x in FIELDS[name]): self.doc.put('BAD') elif not isinstance(FIELDS[name], list) and any_to_bytes( FIELDS[name]) != val: self.doc.put('BAD') elif file_part: val = file_part.group('value') name = any_to_unicode(file_part.group('name')) filename = file_part.group('filename') for file in FILES[name]: if any_to_bytes( file['filename']) == filename and any_to_bytes( file['body']) != val: self.doc.put('BAD') elif re.search(b'name=', part): self.doc.put('BAD')
def post_page(self): errors_count = 0 body_parts = self.request.body.split(b'\r\n--') for part in body_parts: field_part = re.search(b'name="(?P<name>.+)"\r\n\r\n(?P<value>.*)', part) file_part = re.search(b'name="(?P<name>.+)"; filename="(?P<filename>.+)"\r\n' b'Content-Type: application/octet-stream\r\n\r\n(?P<value>.*)', part) if field_part: val = field_part.group('value') name = any_to_unicode(field_part.group('name')) if isinstance(FIELDS[name], list) and all(val != any_to_bytes(x) for x in FIELDS[name]): errors_count += 1 elif not isinstance(FIELDS[name], list) and any_to_bytes(FIELDS[name]) != val: errors_count += 1 elif file_part: val = file_part.group('value') name = any_to_unicode(file_part.group('name')) filename = file_part.group('filename') for file in FILES[name]: if any_to_bytes(file['filename']) == filename and any_to_bytes(file['body']) != val: errors_count += 1 elif re.search(b'name=', part): errors_count += 1 self.json.put({'errors_count': errors_count})
def test_any_to_bytes(self): self.assertEqual(any_to_bytes(5), b'5') self.assertEqual(any_to_bytes(None), b'None') self.assertEqual(any_to_bytes('тест'), 'тест'.encode('utf-8')) self.assertEqual(any_to_bytes('тест'.encode('utf-8')), 'тест'.encode('utf-8'))
def test_any_to_bytes(self): self.assertEqual(any_to_bytes(5), b'5') self.assertEqual(any_to_bytes(None), b'None') self.assertEqual(any_to_bytes(u'тест'), u'тест'.encode('utf-8')) self.assertEqual(any_to_bytes(u'тест'.encode('utf-8')), u'тест'.encode('utf-8'))