2 ;;;; Copyright (c) 2008 Zachary Beane, All Rights Reserved
4 ;;;; Redistribution and use in source and binary forms, with or without
5 ;;;; modification, are permitted provided that the following conditions
8 ;;;; * Redistributions of source code must retain the above copyright
9 ;;;; notice, this list of conditions and the following disclaimer.
11 ;;;; * Redistributions in binary form must reproduce the above
12 ;;;; copyright notice, this list of conditions and the following
13 ;;;; disclaimer in the documentation and/or other materials
14 ;;;; provided with the distribution.
16 ;;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
17 ;;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ;;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ;;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 ;;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ;;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 ;;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 ;;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ;;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 ;;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ;;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 (defvar *response-element-classes
*
33 (make-hash-table :test
'equal
))
35 (defun set-element-class (element-name class
)
36 (setf (gethash element-name
*response-element-classes
*) class
))
50 :accessor http-phrase
)
52 :initarg
:http-headers
53 :accessor http-headers
))
58 :http-phrase
"<uninitialized>"
62 (defmethod print-object ((response response
) stream
)
63 (print-unreadable-object (response stream
:type t
:identity t
)
64 (format stream
"~D ~S" (http-code response
) (http-phrase response
))))
66 (defgeneric xml-string
(response)
68 (flexi-streams:octets-to-string
(body response
) :external-format
:utf-8
)))
70 (defgeneric response-specialized-class
(name)
72 (gethash name
*response-element-classes
*)))
74 (defgeneric specialized-initialize
(object source
)
75 (:method
(object (source t
))
78 (defgeneric content-length
(response)
80 (parse-integer (bvalue :content-length
(http-headers response
)))))
82 (defgeneric specialize-response
(response)
83 (:method
((response response
))
84 (cond ((or (null (body response
))
85 (and (not (streamp (body response
)))
86 (zerop (length (body response
)))))
89 (let* ((source (xml-source (body response
)))
90 (type (xml-document-element source
))
91 (class (response-specialized-class type
)))
93 (change-class response class
)
94 (specialized-initialize response source
))
97 (defun request-response (request &key
100 (handler 'specialize-response
))
101 (setf (endpoint request
) (redirected-endpoint (endpoint request
)
103 (ensure-amz-header request
"date"
104 (iso8601-basic-timestamp-string (date request
)))
105 (multiple-value-bind (body code headers uri stream must-close phrase
)
106 (send request
:want-stream body-stream
)
107 (declare (ignore uri must-close
))
109 (make-instance 'response
114 :http-headers headers
)))
116 (funcall handler response
)
117 (with-open-stream (stream stream
)
118 (funcall handler response
))))))
120 (defun submit-request (request
121 &key body-stream keep-stream
122 (handler 'specialize-response
))
123 ;; The original endpoint has to be stashed so it can be updated as
124 ;; needed by AuthorizationHeaderMalformed responses after being
125 ;; clobbered in the request by TemporaryRedirect responses.
126 (let ((original-endpoint (endpoint request
)))
129 (let ((response (request-response request
130 :keep-stream keep-stream
131 :body-stream body-stream
133 (maybe-signal-error response
)
134 (setf (request response
) request
)
136 (temporary-redirect (condition)
137 (setf (endpoint request
)
138 (request-error-endpoint condition
)))
139 (authorization-header-malformed (condition)
140 (let ((region (request-error-region condition
)))
141 (setf (redirection-data original-endpoint
(bucket request
))
142 (list (endpoint request
)
144 (setf (region request
) region
)))
145 (permanent-redirect (condition)
146 ;; Remember the new endpoint long-term
147 (let ((new-endpoint (request-error-endpoint condition
))
148 (new-region (cdr (assoc :x-amz-bucket-region
149 (http-headers (request-error-response condition
))))))
150 (setf (redirection-data (endpoint request
)
152 (list new-endpoint
(or new-region
(region request
))))
153 (setf (endpoint request
) new-endpoint
)
155 (setf (region request
) new-region
))))
157 ;; Per the S3 docs, InternalErrors should simply be retried