~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/storage/client.py

  • Committer: Duncan McGreggor
  • Date: 2009-09-05 00:26:12 UTC
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: duncan@canonical.com-20090905002612-cuk3ndv1tkpadgag
Replaced all occurances of ' in all the modules with " (excepting proper usage
of apostrophes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
class S3Request(object):
20
20
 
21
 
    def __init__(self, verb, bucket=None, object_name=None, data='',
 
21
    def __init__(self, verb, bucket=None, object_name=None, data="",
22
22
                 content_type=None, metadata={}, creds=None, endpoint=None):
23
23
        self.verb = verb
24
24
        self.bucket = bucket
31
31
        self.date = datetimeToString()
32
32
 
33
33
    def get_path(self):
34
 
        path = '/'
 
34
        path = "/"
35
35
        if self.bucket is not None:
36
36
            path += self.bucket
37
37
            if self.object_name is not None:
38
 
                path += '/' + self.object_name
 
38
                path += "/" + self.object_name
39
39
        return path
40
40
 
41
41
    def get_uri(self):
45
45
        return self.endpoint.get_uri()
46
46
 
47
47
    def get_headers(self):
48
 
        headers = {'Content-Length': len(self.data),
49
 
                   'Content-MD5': calculate_md5(self.data),
50
 
                   'Date': self.date}
 
48
        headers = {"Content-Length": len(self.data),
 
49
                   "Content-MD5": calculate_md5(self.data),
 
50
                   "Date": self.date}
51
51
 
52
52
        for key, value in self.metadata.iteritems():
53
 
            headers['x-amz-meta-' + key] = value
 
53
            headers["x-amz-meta-" + key] = value
54
54
 
55
55
        if self.content_type is not None:
56
 
            headers['Content-Type'] = self.content_type
 
56
            headers["Content-Type"] = self.content_type
57
57
 
58
58
        if self.creds is not None:
59
59
            signature = self.get_signature(headers)
60
 
            headers['Authorization'] = 'AWS %s:%s' % (
 
60
            headers["Authorization"] = "AWS %s:%s" % (
61
61
                self.creds.access_key, signature)
62
62
        return headers
63
63
 
65
65
        return self.get_path()
66
66
 
67
67
    def get_canonicalized_amz_headers(self, headers):
68
 
        result = ''
 
68
        result = ""
69
69
        headers = [(name.lower(), value) for name, value in headers.iteritems()
70
 
            if name.lower().startswith('x-amz-')]
 
70
            if name.lower().startswith("x-amz-")]
71
71
        headers.sort()
72
 
        return ''.join('%s:%s\n' % (name, value) for name, value in headers)
 
72
        return "".join("%s:%s\n" % (name, value) for name, value in headers)
73
73
 
74
74
    def get_signature(self, headers):
75
 
        text = (self.verb + '\n' + 
76
 
                headers.get('Content-MD5', '') + '\n' +
77
 
                headers.get('Content-Type', '') + '\n' +
78
 
                headers.get('Date', '') + '\n' +
 
75
        text = (self.verb + "\n" + 
 
76
                headers.get("Content-MD5", "") + "\n" +
 
77
                headers.get("Content-Type", "") + "\n" +
 
78
                headers.get("Date", "") + "\n" +
79
79
                self.get_canonicalized_amz_headers(headers) +
80
80
                self.get_canonicalized_resource())
81
81
        return self.creds.sign(text)
114
114
        for bucket in root.find("Buckets"):
115
115
            timeText = bucket.findtext("CreationDate")
116
116
            yield {
117
 
                'name': bucket.findtext("Name"),
118
 
                'created': Time.fromISO8601TimeAndDate(timeText),
 
117
                "name": bucket.findtext("Name"),
 
118
                "created": Time.fromISO8601TimeAndDate(timeText),
119
119
                }
120
120
 
121
121
    def list_buckets(self):
125
125
        Returns a list of all the buckets owned by the authenticated sender of
126
126
        the request.
127
127
        """
128
 
        deferred = self.make_request('GET').submit()
 
128
        deferred = self.make_request("GET").submit()
129
129
        deferred.addCallback(self._parse_bucket_list)
130
130
        return deferred
131
131
 
133
133
        """
134
134
        Create a new bucket.
135
135
        """
136
 
        return self.make_request('PUT', bucket).submit()
 
136
        return self.make_request("PUT", bucket).submit()
137
137
 
138
138
    def delete_bucket(self, bucket):
139
139
        """
141
141
 
142
142
        The bucket must be empty before it can be deleted.
143
143
        """
144
 
        return self.make_request('DELETE', bucket).submit()
 
144
        return self.make_request("DELETE", bucket).submit()
145
145
 
146
146
    def put_object(self, bucket, object_name, data, content_type=None,
147
147
                   metadata={}):
150
150
 
151
151
        Any existing object of the same name will be replaced.
152
152
        """
153
 
        return self.make_request('PUT', bucket, object_name, data,
 
153
        return self.make_request("PUT", bucket, object_name, data,
154
154
                                 content_type, metadata).submit()
155
155
 
156
156
    def get_object(self, bucket, object_name):
157
157
        """
158
158
        Get an object from a bucket.
159
159
        """
160
 
        return self.make_request('GET', bucket, object_name).submit()
 
160
        return self.make_request("GET", bucket, object_name).submit()
161
161
 
162
162
    def head_object(self, bucket, object_name):
163
163
        """
167
167
        Currently the metadata is not returned to the caller either, so this
168
168
        method is mostly useless, and only provided for completeness.
169
169
        """
170
 
        return self.make_request('HEAD', bucket, object_name).submit()
 
170
        return self.make_request("HEAD", bucket, object_name).submit()
171
171
 
172
172
    def delete_object(self, bucket, object_name):
173
173
        """
175
175
 
176
176
        Once deleted, there is no method to restore or undelete an object.
177
177
        """
178
 
        return self.make_request('DELETE', bucket, object_name).submit()
 
178
        return self.make_request("DELETE", bucket, object_name).submit()