1 /*
2  * Copyright 2009-2017 Alibaba Cloud 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  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #include <alibabacloud/oss/model/GetObjectRequest.h>
19 #include <alibabacloud/oss/http/HttpType.h>
20 #include "utils/Utils.h"
21 #include "ModelError.h"
22 #include <set>
23 #include <sstream>
24 using namespace AlibabaCloud::OSS;
25 
GetObjectRequest(const std::string & bucket,const std::string & key)26 GetObjectRequest::GetObjectRequest(const std::string &bucket, const std::string &key):
27     GetObjectRequest(bucket, key, "")
28 {
29 }
30 
GetObjectRequest(const std::string & bucket,const std::string & key,const std::string & process)31 GetObjectRequest::GetObjectRequest(const std::string &bucket, const std::string &key, const std::string &process) :
32     OssObjectRequest(bucket, key),
33     rangeIsSet_(false),
34     process_(process),
35     trafficLimit_(0),
36     rangeIsStandardMode_(false),
37     userAgent_()
38 
39 {
40     setFlags(Flags() | REQUEST_FLAG_CHECK_CRC64);
41 }
42 
GetObjectRequest(const std::string & bucket,const std::string & key,const std::string & modifiedSince,const std::string & unmodifiedSince,const std::vector<std::string> & matchingETags,const std::vector<std::string> & nonmatchingETags,const std::map<std::string,std::string> & responseHeaderParameters)43 GetObjectRequest::GetObjectRequest(const std::string& bucket, const std::string& key,
44     const std::string &modifiedSince, const std::string &unmodifiedSince,
45     const std::vector<std::string> &matchingETags, const std::vector<std::string> &nonmatchingETags,
46     const std::map<std::string, std::string> &responseHeaderParameters) :
47     OssObjectRequest(bucket, key),
48     rangeIsSet_(false),
49     modifiedSince_(modifiedSince),
50     unmodifiedSince_(unmodifiedSince),
51     matchingETags_(matchingETags),
52     nonmatchingETags_(nonmatchingETags),
53     process_(""),
54     responseHeaderParameters_(responseHeaderParameters),
55     trafficLimit_(0),
56     rangeIsStandardMode_(false),
57     userAgent_()
58 
59 {
60 }
61 
setRange(int64_t start,int64_t end)62 void GetObjectRequest::setRange(int64_t start, int64_t end)
63 {
64     range_[0] = start;
65     range_[1] = end;
66     rangeIsSet_ = true;
67     rangeIsStandardMode_ = false;
68 }
69 
setRange(int64_t start,int64_t end,bool standard)70 void GetObjectRequest::setRange(int64_t start, int64_t end, bool standard)
71 {
72     range_[0] = start;
73     range_[1] = end;
74     rangeIsSet_ = true;
75     rangeIsStandardMode_ = standard;
76 }
77 
setModifiedSinceConstraint(const std::string & gmt)78 void GetObjectRequest::setModifiedSinceConstraint(const std::string &gmt)
79 {
80     modifiedSince_ = gmt;
81 }
82 
setUnmodifiedSinceConstraint(const std::string & gmt)83 void GetObjectRequest::setUnmodifiedSinceConstraint(const std::string &gmt)
84 {
85     unmodifiedSince_ = gmt;
86 }
87 
setMatchingETagConstraints(const std::vector<std::string> & match)88 void GetObjectRequest::setMatchingETagConstraints(const std::vector<std::string> &match)
89 {
90     matchingETags_ = match;
91 }
92 
addMatchingETagConstraint(const std::string & match)93 void GetObjectRequest::addMatchingETagConstraint(const std::string &match)
94 {
95     matchingETags_.push_back(match);
96 }
97 
setNonmatchingETagConstraints(const std::vector<std::string> & match)98 void GetObjectRequest::setNonmatchingETagConstraints(const std::vector<std::string> &match)
99 {
100     nonmatchingETags_ = match;
101 }
102 
addNonmatchingETagConstraint(const std::string & match)103 void GetObjectRequest::addNonmatchingETagConstraint(const std::string &match)
104 {
105     nonmatchingETags_.push_back(match);
106 }
107 
setProcess(const std::string & process)108 void GetObjectRequest::setProcess(const std::string &process)
109 {
110     process_ = process;
111 }
112 
addResponseHeaders(RequestResponseHeader header,const std::string & value)113 void GetObjectRequest::addResponseHeaders(RequestResponseHeader header, const std::string &value)
114 {
115     static const char *ResponseHeader[] = {
116         "response-content-type", "response-content-language",
117         "response-expires", "response-cache-control",
118         "response-content-disposition", "response-content-encoding"};
119     responseHeaderParameters_[ResponseHeader[header - RequestResponseHeader::ContentType]] = value;
120 }
121 
setTrafficLimit(uint64_t value)122 void GetObjectRequest::setTrafficLimit(uint64_t value)
123 {
124     trafficLimit_ = value;
125 }
126 
setUserAgent(const std::string & ua)127 void GetObjectRequest::setUserAgent(const std::string& ua)
128 {
129     userAgent_ = ua;
130 }
131 
Range() const132 std::pair<int64_t, int64_t> GetObjectRequest::Range() const
133 {
134     int64_t begin = -1;
135     int64_t end = -1;
136     if (rangeIsSet_) {
137         begin = range_[0];
138         end = range_[1];
139     }
140 
141     return std::pair<int64_t, int64_t>(begin, end);
142 }
143 
validate() const144 int GetObjectRequest::validate() const
145 {
146     int ret = OssObjectRequest::validate();
147     if (ret != 0)
148         return ret;
149 
150     if (rangeIsSet_ && (range_[0] < 0 || range_[1] < -1 || (range_[1] > -1 && range_[1] < range_[0]) ))
151         return ARG_ERROR_OBJECT_RANGE_INVALID;
152 
153     return 0;
154 }
155 
specialHeaders() const156 HeaderCollection GetObjectRequest::specialHeaders() const
157 {
158     auto headers = OssObjectRequest::specialHeaders();
159     if (rangeIsSet_) {
160         std::stringstream ss;
161         ss << "bytes=" << range_[0] << "-";
162         if (range_[1] != -1) ss << range_[1];
163         headers[Http::RANGE] = ss.str();
164 
165         if (rangeIsStandardMode_) {
166             headers["x-oss-range-behavior"] = "standard";
167         }
168     }
169 
170     if (!modifiedSince_.empty())
171     {
172         headers["If-Modified-Since"] = modifiedSince_;
173     }
174 
175     if (!unmodifiedSince_.empty())
176     {
177         headers["If-Unmodified-Since"] = unmodifiedSince_;
178     }
179 
180     if (matchingETags_.size() > 0) {
181         std::stringstream ss;
182         bool first = true;
183         for (auto const& str : matchingETags_) {
184             if (!first) {
185                 ss << ",";
186             }
187             ss << str;
188             first = false;
189         }
190         headers["If-Match"] = ss.str();
191     }
192 
193     if (nonmatchingETags_.size() > 0) {
194         std::stringstream ss;
195         bool first = true;
196         for (auto const& str : nonmatchingETags_) {
197             if (!first) {
198                 ss << ",";
199             }
200             ss << str;
201             first = false;
202         }
203         headers["If-None-Match"] = ss.str();
204     }
205 
206     if (trafficLimit_ != 0) {
207         headers["x-oss-traffic-limit"] = std::to_string(trafficLimit_);
208     }
209 
210     if (!userAgent_.empty()) {
211         headers[Http::USER_AGENT] = userAgent_;
212     }
213     return headers;
214 }
215 
specialParameters() const216 ParameterCollection GetObjectRequest::specialParameters() const
217 {
218     auto parameters = OssObjectRequest::specialParameters();
219     for (auto const& param : responseHeaderParameters_) {
220         parameters[param.first] = param.second;
221     }
222 
223     if (!process_.empty()) {
224         parameters["x-oss-process"] = process_;
225     }
226 
227     return parameters;
228 }
229 
230