* Website - Common website function for GET and HEAD * Gets metadata and object for website or redirects * @param {object} request - normalized request object * @param {object} log - Werelogs instance * @param {function} callback - callback to function in route * @return {undefined}
(request, log, callback)
| 134 | * @return {undefined} |
| 135 | */ |
| 136 | function website(request, log, callback) { |
| 137 | if (request.method === 'HEAD') { |
| 138 | // eslint-disable-next-line no-param-reassign |
| 139 | callback = callbackGetToHead(callback); |
| 140 | } |
| 141 | const methodCapitalized = capitalize(request.method); |
| 142 | const action = request.method === 'HEAD' ? 'headObject' : 'getObject'; |
| 143 | log.debug('processing request', { method: `website${methodCapitalized}` }); |
| 144 | const bucketName = request.bucketName; |
| 145 | const reqObjectKey = request.objectKey ? request.objectKey : ''; |
| 146 | |
| 147 | return metadata.getBucket(bucketName, log, (err, bucket) => { |
| 148 | if (err) { |
| 149 | log.trace('error retrieving bucket metadata', { error: err }); |
| 150 | monitoring.promMetrics( |
| 151 | request.method, bucketName, err.code, action); |
| 152 | return callback(err, false); |
| 153 | } |
| 154 | if (bucketShield(bucket, `object${methodCapitalized}`)) { |
| 155 | log.trace('bucket in transient/deleted state so shielding'); |
| 156 | monitoring.promMetrics( |
| 157 | request.method, bucketName, 404, action); |
| 158 | return callback(errors.NoSuchBucket, false); |
| 159 | } |
| 160 | const corsHeaders = collectCorsHeaders(request.headers.origin, |
| 161 | request.method, bucket); |
| 162 | // bucket ACL's do not matter for website head since it is always the |
| 163 | // head of an object. object ACL's are what matter |
| 164 | const websiteConfig = bucket.getWebsiteConfiguration(); |
| 165 | if (!websiteConfig) { |
| 166 | monitoring.promMetrics( |
| 167 | request.method, bucketName, 404, action); |
| 168 | return callback(errors.NoSuchWebsiteConfiguration, false, null, |
| 169 | corsHeaders); |
| 170 | } |
| 171 | // any errors above would be our own created generic error html |
| 172 | // if have a website config, error going forward would be user's |
| 173 | // redirect or error page if they set either in the config |
| 174 | |
| 175 | // handle redirect all |
| 176 | if (websiteConfig.getRedirectAllRequestsTo()) { |
| 177 | return callback(null, false, null, corsHeaders, |
| 178 | websiteConfig.getRedirectAllRequestsTo(), reqObjectKey); |
| 179 | } |
| 180 | |
| 181 | // check whether need to redirect based on key |
| 182 | const routingRules = websiteConfig.getRoutingRules(); |
| 183 | const keyRoutingRule = findRoutingRule(routingRules, reqObjectKey); |
| 184 | |
| 185 | if (keyRoutingRule) { |
| 186 | // TODO: optimize by not rerouting if only routing |
| 187 | // rule is to change out key |
| 188 | return callback(null, false, null, corsHeaders, |
| 189 | keyRoutingRule, reqObjectKey); |
| 190 | } |
| 191 | |
| 192 | appendWebsiteIndexDocument(request, websiteConfig.getIndexDocument()); |
| 193 |
nothing calls this directly
no test coverage detected