/usr/share/common-lisp/source/zs3/response.lisp is in cl-zs3 1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | ;;;;
;;;; Copyright (c) 2008, 2015 Zachary Beane, All Rights Reserved
;;;;
;;;; Redistribution and use in source and binary forms, with or without
;;;; modification, are permitted provided that the following conditions
;;;; are met:
;;;;
;;;; * Redistributions of source code must retain the above copyright
;;;; notice, this list of conditions and the following disclaimer.
;;;;
;;;; * Redistributions in binary form must reproduce the above
;;;; copyright notice, this list of conditions and the following
;;;; disclaimer in the documentation and/or other materials
;;;; provided with the distribution.
;;;;
;;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;;;;
;;;; response.lisp
(in-package #:zs3)
(defvar *response-element-classes*
(make-hash-table :test 'equal))
(defun set-element-class (element-name class)
(setf (gethash element-name *response-element-classes*) class))
(defclass response ()
((request
:initarg :request
:accessor request)
(body
:initarg :body
:accessor body)
(http-code
:initarg :http-code
:accessor http-code)
(http-phrase
:initarg :http-phrase
:accessor http-phrase)
(http-headers
:initarg :http-headers
:accessor http-headers))
(:default-initargs
:request nil
:body nil
:http-code 999
:http-phrase "<uninitialized>"
:http-headers nil))
(defmethod print-object ((response response) stream)
(print-unreadable-object (response stream :type t :identity t)
(format stream "~D ~S" (http-code response) (http-phrase response))))
(defgeneric xml-string (response)
(:method (response)
(flexi-streams:octets-to-string (body response) :external-format :utf-8)))
(defgeneric response-specialized-class (name)
(:method (name)
(gethash name *response-element-classes*)))
(defgeneric specialized-initialize (object source)
(:method (object (source t))
object))
(defgeneric content-length (response)
(:method (response)
(parse-integer (drakma:header-value :content-length (http-headers response)))))
(defgeneric specialize-response (response)
(:method ((response response))
(cond ((or (null (body response))
(and (not (streamp (body response)))
(zerop (length (body response)))))
(when (<= 500 (http-code response) 599)
(change-class response 'amazon-error)
(specialized-initialize response nil))
response)
(t
(let* ((source (xml-source (body response)))
(type (xml-document-element source))
(class (response-specialized-class type)))
(when class
(change-class response class)
(specialized-initialize response source))
response)))))
(defun close-keep-alive ()
(when *keep-alive-stream*
(ignore-errors (close *keep-alive-stream*))
(setq *keep-alive-stream* nil)))
(defun request-response (request &key
body-stream
keep-stream
(handler 'specialize-response))
(setf (endpoint request) (redirected-endpoint (endpoint request)
(bucket request)))
(ensure-amz-header request "date"
(iso8601-basic-timestamp-string (date request)))
(multiple-value-bind (body code headers uri stream must-close phrase)
(send request :want-stream body-stream
:stream *keep-alive-stream*)
(declare (ignore uri))
(let ((response
(make-instance 'response
:request request
:body body
:http-code code
:http-phrase phrase
:http-headers headers)))
(if (and keep-stream (not must-close))
(progn
(when *use-keep-alive*
(unless (eq *keep-alive-stream* stream)
(close-keep-alive)
(setq *keep-alive-stream* stream)))
(funcall handler response))
(with-open-stream (stream stream)
(declare (ignorable stream))
(setq *keep-alive-stream* nil)
(funcall handler response))))))
(defvar *backoff* (cons 3 100)
"Used as the default value of :BACKOFF when submitting a request.
The value should be a cons of two numbers: how many times to try
before giving up, and how long to wait (in ms) before trying for
the second time. Each subsequent attempt will double that time.")
(defun submit-request (request
&key body-stream
(keep-stream *use-keep-alive*)
(handler 'specialize-response))
;; The original endpoint has to be stashed so it can be updated as
;; needed by AuthorizationHeaderMalformed responses after being
;; clobbered in the request by TemporaryRedirect responses.
(let* ((original-endpoint (endpoint request))
(backoff *backoff*)
(tries (car backoff))
(delay (/ (cdr backoff) 1000)))
(loop
(handler-case
(let ((response (request-response request
:keep-stream keep-stream
:body-stream body-stream
:handler handler)))
(maybe-signal-error response)
(setf (request response) request)
(return response))
(temporary-redirect (condition)
(setf (endpoint request)
(request-error-endpoint condition)))
(authorization-header-malformed (condition)
(let ((region (request-error-region condition)))
(setf (redirection-data original-endpoint (bucket request))
(list (endpoint request)
region))
(setf (region request) region)))
(permanent-redirect (condition)
;; Remember the new endpoint long-term
(let ((new-endpoint (request-error-endpoint condition))
(new-region (cdr (assoc :x-amz-bucket-region
(http-headers (request-error-response condition))))))
(setf (redirection-data (endpoint request)
(bucket request))
(list new-endpoint (or new-region (region request))))
(setf (endpoint request) new-endpoint)
(when new-region
(setf (region request) new-region))))
(internal-error (e)
;; Per the S3 docs, InternalErrors should be retried. Up to
;; a point.
(close-keep-alive)
(when (minusp (decf tries))
;; We've exceeded our failure allowance. Resignal.
(error e))
(sleep delay)
(incf delay delay))
(error (e)
;; Ensure that we don't reuse the stream, it may be the source of
;; our error. Then resignal.
(close-keep-alive)
(error e))))))
|