Skip to content

Instantly share code, notes, and snippets.

@adiroiban
Last active May 11, 2016 13:41
Show Gist options
  • Save adiroiban/9ed3f61965a264d2e2d4cd05d9614070 to your computer and use it in GitHub Desktop.
Save adiroiban/9ed3f61965a264d2e2d4cd05d9614070 to your computer and use it in GitHub Desktop.
SharePoint online error to PUT with Transfer-Encoding: chunked
PUT 200: OK
<HTML><DIV dir="ltr"><H2>Microsoft SharePoint Foundation Error.</H2>
<P>
<B>User:</B> please report details to this Web site's Webmaster.
<P>
<P>
<B>Webmaster:</B> please see the server's application event log for more details.
</P></DIV>
POST 200: OK
<HTML><DIV dir="ltr"><H2>Microsoft SharePoint Foundation Error.</H2>
<P>
<B>User:</B> please report details to this Web site's Webmaster.
<P>
<P>
<B>Webmaster:</B> please see the server's application event log for more details.
</P></DIV>
#
# This is a script which demonstrates SharePoint online failure to process
# chunked HTTP PUT requests
#
import os, requests
SETTINGS = {
'username' : 'test@chevah.onmicrosoft.com',
'password' : 'your-pass-here',
'site': 'https://chevah.sharepoint.com',
'remote_path': '/test-wedbav/Shared%20Documents/uploaded.txt',
}
SETTINGS['endpoint'] = SETTINGS['site'] + '/_forms/default.aspx?wa=wsignin1.0'
spauth = None
def generate_saml(options):
# SAML.xml from https://github.com/lstak/node-sharepoint
content = (
'<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" '
'xmlns:a="http://www.w3.org/2005/08/addressing" '
'xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">'
'<s:Header><a:Action s:mustUnderstand="1">'
'http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</a:Action>'
'<a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous'
'</a:Address></a:ReplyTo><a:To s:mustUnderstand="1">'
'https://login.microsoftonline.com/extSTS.srf</a:To>'
'<o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">'
'<o:UsernameToken><o:Username>%(username)s</o:Username>'
'<o:Password>%(password)s</o:Password></o:UsernameToken></o:Security>'
'</s:Header><s:Body><t:RequestSecurityToken xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">'
'<wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">'
'<a:EndpointReference><a:Address>%(endpoint)s</a:Address>'
'</a:EndpointReference></wsp:AppliesTo>'
'<t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType>'
'<t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>'
'<t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>'
'</t:RequestSecurityToken></s:Body></s:Envelope>'
)
return content % SETTINGS
def request_token(options):
saml = generate_saml(options)
result = requests.post('https://login.microsoftonline.com/extSTS.srf', data=saml)
return result.text.split('<wsse:BinarySecurityToken Id="Compact0">')[1].split('</wsse:BinarySecurityToken>')[0]
def data_chunker():
"""
Generate chunks of 400 bytes.
"""
for i in xrange(10):
yield '1234' * 100
# Get the token
token = request_token(SETTINGS)
# Then use that token to get the cookie.
result = requests.post(SETTINGS['endpoint'], token)
# Extract the cookies.
spauth = 'FedAuth=' + result.cookies['FedAuth'] + '; rtFa=' + result.cookies['rtFa']
url = SETTINGS['site'] + SETTINGS['remote_path']
# Upload the new data using PUT.
result = requests.post(url, headers={'Cookie':spauth}, data=data_chunker())
print 'PUT %s: %s' % (result.status_code, result.reason)
print result.text.encode('utf-8')
# Upload the new data using POST.
result = requests.post(url, headers={'Cookie':spauth}, data=data_chunker())
print 'POST %s: %s' % (result.status_code, result.reason)
print result.text.encode('utf-8')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment