1/*
2 * Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16#include <aws/core/http/standard/StandardHttpRequest.h>
17
18#include <aws/core/utils/StringUtils.h>
19
20#include <iostream>
21#include <algorithm>
22#include <cassert>
23
24using namespace Aws::Http;
25using namespace Aws::Http::Standard;
26using namespace Aws::Utils;
27
28static bool IsDefaultPort(const URI& uri)
29{
30 switch(uri.GetPort())
31 {
32 case 80:
33 return uri.GetScheme() == Scheme::HTTP;
34 case 443:
35 return uri.GetScheme() == Scheme::HTTPS;
36 default:
37 return false;
38 }
39}
40
41StandardHttpRequest::StandardHttpRequest(const URI& uri, HttpMethod method) :
42 HttpRequest(uri, method),
43 bodyStream(nullptr),
44 m_responseStreamFactory()
45{
46 if(IsDefaultPort(uri))
47 {
48 StandardHttpRequest::SetHeaderValue(HOST_HEADER, uri.GetAuthority());
49 }
50 else
51 {
52 Aws::StringStream host;
53 host << uri.GetAuthority() << ":" << uri.GetPort();
54 StandardHttpRequest::SetHeaderValue(HOST_HEADER, host.str());
55 }
56}
57
58HeaderValueCollection StandardHttpRequest::GetHeaders() const
59{
60 HeaderValueCollection headers;
61
62 for (HeaderValueCollection::const_iterator iter = headerMap.begin(); iter != headerMap.end(); ++iter)
63 {
64 headers.emplace(HeaderValuePair(iter->first, iter->second));
65 }
66
67 return headers;
68}
69
70const Aws::String& StandardHttpRequest::GetHeaderValue(const char* headerName) const
71{
72 auto iter = headerMap.find(headerName);
73 assert (iter != headerMap.end());
74 return iter->second;
75}
76
77void StandardHttpRequest::SetHeaderValue(const char* headerName, const Aws::String& headerValue)
78{
79 headerMap[StringUtils::ToLower(headerName)] = StringUtils::Trim(headerValue.c_str());
80}
81
82void StandardHttpRequest::SetHeaderValue(const Aws::String& headerName, const Aws::String& headerValue)
83{
84 headerMap[StringUtils::ToLower(headerName.c_str())] = StringUtils::Trim(headerValue.c_str());
85}
86
87void StandardHttpRequest::DeleteHeader(const char* headerName)
88{
89 headerMap.erase(StringUtils::ToLower(headerName));
90}
91
92bool StandardHttpRequest::HasHeader(const char* headerName) const
93{
94 return headerMap.find(StringUtils::ToLower(headerName)) != headerMap.end();
95}
96
97int64_t StandardHttpRequest::GetSize() const
98{
99 int64_t size = 0;
100
101 std::for_each(headerMap.cbegin(), headerMap.cend(), [&](const HeaderValueCollection::value_type& kvPair){ size += kvPair.first.length(); size += kvPair.second.length(); });
102
103 return size;
104}
105
106const Aws::IOStreamFactory& StandardHttpRequest::GetResponseStreamFactory() const
107{
108 return m_responseStreamFactory;
109}
110
111void StandardHttpRequest::SetResponseStreamFactory(const Aws::IOStreamFactory& factory)
112{
113 m_responseStreamFactory = factory;
114}
115