mirror of https://gitlab.com/bashrc2/epicyon
Don't use payload function when parsing form-data
parent
18088169e6
commit
f76c173f59
18
content.py
18
content.py
|
@ -979,16 +979,21 @@ def saveMediaInFormPOST(mediaBytes, debug: bool,
|
|||
return filename, attachmentMediaType
|
||||
|
||||
|
||||
def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}:
|
||||
def extractTextFieldsInPOST(postBytes, boundary, debug: bool,
|
||||
unitTestData=None) -> {}:
|
||||
"""Returns a dictionary containing the text fields of a http form POST
|
||||
The boundary argument comes from the http header
|
||||
"""
|
||||
msg = email.parser.BytesParser().parsebytes(postBytes)
|
||||
if not unitTestData:
|
||||
msgBytes = email.parser.BytesParser().parsebytes(postBytes)
|
||||
messageFields = msgBytes.decode('utf-8')
|
||||
else:
|
||||
messageFields = unitTestData
|
||||
|
||||
if debug:
|
||||
print('DEBUG: POST arriving ' +
|
||||
msg.get_payload(decode=True).decode('utf-8'))
|
||||
messageFields = msg.get_payload(decode=True)
|
||||
messageFields = messageFields.decode('utf-8').split(boundary)
|
||||
print('DEBUG: POST arriving ' + messageFields)
|
||||
|
||||
messageFields = messageFields.split(boundary)
|
||||
fields = {}
|
||||
# examine each section of the POST, separated by the boundary
|
||||
for f in messageFields:
|
||||
|
@ -1002,6 +1007,7 @@ def extractTextFieldsInPOST(postBytes, boundary, debug: bool) -> {}:
|
|||
postKey = postStr.split('"', 1)[0]
|
||||
postValueStr = postStr.split('"', 1)[1]
|
||||
if ';' in postValueStr:
|
||||
if postKey != 'message':
|
||||
continue
|
||||
if '\r\n' not in postValueStr:
|
||||
continue
|
||||
|
|
39
tests.py
39
tests.py
|
@ -77,6 +77,7 @@ from inbox import jsonPostAllowsComments
|
|||
from inbox import validInbox
|
||||
from inbox import validInboxFilenames
|
||||
from categories import guessHashtagCategory
|
||||
from content import extractTextFieldsInPOST
|
||||
from content import validHashTag
|
||||
from content import htmlReplaceEmailQuote
|
||||
from content import htmlReplaceQuoteMarks
|
||||
|
@ -3330,9 +3331,47 @@ def testMarkdownToHtml():
|
|||
'Or <img class="markdownImage" src="/cat.jpg" alt="pounce" />.'
|
||||
|
||||
|
||||
def testExtractTextFieldsInPOST():
|
||||
print('testExtractTextFieldsInPOST')
|
||||
boundary = '-----------------------------116202748023898664511855843036'
|
||||
formData = '-----------------------------116202748023898664511855' + \
|
||||
'843036\r\nContent-Disposition: form-data; name="submitPost"' + \
|
||||
'\r\n\r\nSubmit\r\n-----------------------------116202748023' + \
|
||||
'898664511855843036\r\nContent-Disposition: form-data; name=' + \
|
||||
'"subject"\r\n\r\n\r\n-----------------------------116202748' + \
|
||||
'023898664511855843036\r\nContent-Disposition: form-data; na' + \
|
||||
'me="message"\r\n\r\nThis is a ; test\r\n-------------------' + \
|
||||
'----------116202748023898664511855843036\r\nContent-Disposi' + \
|
||||
'tion: form-data; name="commentsEnabled"\r\n\r\non\r\n------' + \
|
||||
'-----------------------116202748023898664511855843036\r\nCo' + \
|
||||
'ntent-Disposition: form-data; name="eventDate"\r\n\r\n\r\n' + \
|
||||
'-----------------------------116202748023898664511855843036' + \
|
||||
'\r\nContent-Disposition: form-data; name="eventTime"\r\n\r' + \
|
||||
'\n\r\n-----------------------------116202748023898664511855' + \
|
||||
'843036\r\nContent-Disposition: form-data; name="location"' + \
|
||||
'\r\n\r\n\r\n-----------------------------116202748023898664' + \
|
||||
'511855843036\r\nContent-Disposition: form-data; name=' + \
|
||||
'"imageDescription"\r\n\r\n\r\n-----------------------------' + \
|
||||
'116202748023898664511855843036\r\nContent-Disposition: ' + \
|
||||
'form-data; name="attachpic"; filename=""\r\nContent-Type: ' + \
|
||||
'application/octet-stream\r\n\r\n\r\n----------------------' + \
|
||||
'-------116202748023898664511855843036--\r\n'
|
||||
debug = False
|
||||
fields = extractTextFieldsInPOST(None, boundary, debug, formData)
|
||||
assert fields['submitPost'] == 'Submit'
|
||||
assert fields['subject'] == ''
|
||||
assert fields['commentsEnabled'] == 'on'
|
||||
assert fields['eventDate'] == ''
|
||||
assert fields['eventTime'] == ''
|
||||
assert fields['location'] == ''
|
||||
assert fields['imageDescription'] == ''
|
||||
assert fields['message'] == 'This is a ; test'
|
||||
|
||||
|
||||
def runAllTests():
|
||||
print('Running tests...')
|
||||
testFunctions()
|
||||
testExtractTextFieldsInPOST()
|
||||
testMarkdownToHtml()
|
||||
testValidHashTag()
|
||||
testPrepareHtmlPostNickname()
|
||||
|
|
Loading…
Reference in New Issue