Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. # This software code is made available "AS IS" without warranties of any
  2. # kind. You may copy, display, modify and redistribute the software
  3. # code either by itself or as incorporated into your code; provided that
  4. # you do not remove any proprietary notices. Your use of this software
  5. # code is at your own risk and you waive any claim against Amazon
  6. # Digital Services, Inc. or its affiliates with respect to your use of
  7. # this software code. (c) 2006 Amazon Digital Services, Inc. or its
  8. # affiliates.
  9.  
  10. # generates the aws canonical string for the given parameters
  11. def canonical_string(method, path, headers, expires=None):
  12. interesting_headers = {}
  13. for key in headers:
  14. lk = key.lower()
  15. if lk in ['content-md5', 'content-type', 'date'] or lk.startswith(AMAZON_HEADER_PREFIX):
  16. interesting_headers[lk] = headers[key].strip()
  17.  
  18. # these keys get empty strings if they don't exist
  19. if not interesting_headers.has_key('content-type'):
  20. interesting_headers['content-type'] = ''
  21. if not interesting_headers.has_key('content-md5'):
  22. interesting_headers['content-md5'] = ''
  23.  
  24. # just in case someone used this. it's not necessary in this lib.
  25. if interesting_headers.has_key('x-amz-date'):
  26. interesting_headers['date'] = ''
  27.  
  28. # if you're using expires for query string auth, then it trumps date
  29. # (and x-amz-date)
  30. if expires:
  31. interesting_headers['date'] = str(expires)
  32.  
  33. sorted_header_keys = interesting_headers.keys()
  34. sorted_header_keys.sort()
  35.  
  36. buf = "%s\n" % method
  37. for key in sorted_header_keys:
  38. if key.startswith(AMAZON_HEADER_PREFIX):
  39. buf += "%s:%s\n" % (key, interesting_headers[key])
  40. else:
  41. buf += "%s\n" % interesting_headers[key]
  42.  
  43. # don't include anything after the first ? in the resource...
  44. buf += "/%s" % path.split('?')[0]
  45.  
  46. # ...unless there is an acl or torrent parameter
  47. if re.search("[&?]acl($|=|&)", path):
  48. buf += "?acl"
  49. elif re.search("[&?]torrent($|=|&)", path):
  50. buf += "?torrent"
  51.  
  52. return buf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement