static void AddContentBodyToRequest()

in GameLiftPlugin/Source/AWSSDK/Include/smithy/client/common/AwsSmithyClientUtils.h [53:128]


            static void AddContentBodyToRequest(const std::shared_ptr<Aws::Http::HttpRequest>& httpRequest,
                                                const std::shared_ptr<Aws::IOStream>& body,
                                                const std::shared_ptr<Aws::Http::HttpClient>& httpClient,
                                                bool needsContentMd5,
                                                bool isChunked)
            {
                assert(httpRequest);

                httpRequest->AddContentBody(body);

                //If there is no body, we have a content length of 0
                //note: we also used to remove content-type, but S3 actually needs content-type on InitiateMultipartUpload and it isn't
                //forbidden by the spec. If we start getting weird errors related to this, make sure it isn't caused by this removal.
                if (!body)
                {
                    AWS_LOGSTREAM_TRACE(AWS_SMITHY_CLIENT_UTILS_TAG, "No content body, content-length headers");

                    if (httpRequest->GetMethod() == HttpMethod::HTTP_POST || httpRequest->GetMethod() ==
                        HttpMethod::HTTP_PUT)
                    {
                        httpRequest->SetHeaderValue(Aws::Http::CONTENT_LENGTH_HEADER, "0");
                    }
                    else
                    {
                        httpRequest->DeleteHeader(Aws::Http::CONTENT_LENGTH_HEADER);
                    }
                }

                //Add transfer-encoding:chunked to header
                if (body && isChunked && !httpRequest->HasHeader(Aws::Http::CONTENT_LENGTH_HEADER))
                {
                    httpRequest->SetTransferEncoding(Aws::Http::CHUNKED_VALUE);
                }
                //in the scenario where we are adding a content body as a stream, the request object likely already
                //has a content-length header set and we don't want to seek the stream just to find this information.
                else if (body && !httpRequest->HasHeader(Aws::Http::CONTENT_LENGTH_HEADER))
                {
                    assert(httpClient);
                    if (!httpClient->SupportsChunkedTransferEncoding())
                    {
                        AWS_LOGSTREAM_WARN(AWS_SMITHY_CLIENT_UTILS_TAG,
                                           "This http client doesn't support transfer-encoding:chunked. " <<
                                           "The request may fail if it's not a seekable stream.");
                    }
                    AWS_LOGSTREAM_TRACE(AWS_SMITHY_CLIENT_UTILS_TAG,
                                        "Found body, but content-length has not been set, attempting to compute content-length");
                    body->seekg(0, body->end);
                    auto streamSize = body->tellg();
                    body->seekg(0, body->beg);
                    Aws::StringStream ss;
                    ss << streamSize;
                    httpRequest->SetContentLength(ss.str());
                }

                if (needsContentMd5 && body && !httpRequest->HasHeader(Aws::Http::CONTENT_MD5_HEADER))
                {
                    AWS_LOGSTREAM_TRACE(AWS_SMITHY_CLIENT_UTILS_TAG, "Found body, and content-md5 needs to be set" <<
                                        ", attempting to compute content-md5");

                    //changing the internal state of the hash computation is not a logical state
                    //change as far as constness goes for this class. Due to the platform specificness
                    //of hash computations, we can't control the fact that computing a hash mutates
                    //state on some platforms such as windows (but that isn't a concern of this class.

                    // TODO: check refactoring from:
                    // auto md5HashResult = const_cast<AWSClient*>(this)->m_hash->Calculate(*body);
                    Aws::Utils::Crypto::MD5 hash;
                    auto md5HashResult = hash.Calculate(*body);
                    body->clear();
                    if (md5HashResult.IsSuccess())
                    {
                        httpRequest->SetHeaderValue(Aws::Http::CONTENT_MD5_HEADER,
                                                    Aws::Utils::HashingUtils::Base64Encode(md5HashResult.GetResult()));
                    }
                }
            }