private HttpRequestBase wrapEntity(SdkHttpFullRequest request, HttpEntityEnclosingRequestBase entityEnclosingRequest, String encodedParams) { /* * We should never reuse the entity of the previous request, since * reading from the buffered entity will bypass reading from the * original request content. And if the content contains InputStream * wrappers that were added for validation-purpose (e.g. * Md5DigestCalculationInputStream), these wrappers would never be * read and updated again after AmazonHttpClient resets it in * preparation for the retry. Eventually, these wrappers would * return incorrect validation result. */ if (request.getContent() != null) { HttpEntity entity = new RepeatableInputStreamRequestEntity(request); if (request.getHeaders().get(HttpHeaders.CONTENT_LENGTH) == null) { entity = ApacheUtils.newBufferedHttpEntity(entity); } entityEnclosingRequest.setEntity(entity); } return entityEnclosingRequest; }
/** * Configures the headers in the specified Apache HTTP request. */ private void addHeadersToRequest(HttpRequestBase httpRequest, SdkHttpFullRequest request) { httpRequest.addHeader(HttpHeaders.HOST, getHostHeaderValue(request.getEndpoint())); // Copy over any other headers already in our request request.getHeaders().entrySet().stream() /* * HttpClient4 fills in the Content-Length header and complains if * it's already present, so we skip it here. We also skip the Host * header to avoid sending it twice, which will interfere with some * signing schemes. */ .filter(e -> !IGNORE_HEADERS.contains(e.getKey())) .forEach(e -> e.getValue().stream() .forEach(h -> httpRequest.addHeader(e.getKey(), h))); /* Set content type and encoding */ if (httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE) == null || httpRequest.getHeaders(HttpHeaders.CONTENT_TYPE).length == 0) { httpRequest.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded; " + "charset=" + lowerCase(DEFAULT_ENCODING)); } }