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 #include <alibabacloud/oss/OssRequest.h>
18 #include <alibabacloud/oss/Const.h>
19 #include <sstream>
20 #include "http/HttpType.h"
21 #include "utils/Utils.h"
22 #include "model/ModelError.h"
23 #include "utils/FileSystemUtils.h"
24 
25 using namespace AlibabaCloud::OSS;
26 
OssRequest()27  OssRequest:: OssRequest():
28     ServiceRequest(),
29     bucket_(),
30     key_()
31 {
32 }
33 
OssRequest(const std::string & bucket,const std::string & key)34 OssRequest:: OssRequest(const std::string &bucket, const std::string &key):
35     ServiceRequest(),
36     bucket_(bucket),
37     key_(key)
38 {
39 }
40 
41 
Headers() const42 HeaderCollection OssRequest::Headers() const
43 {
44     auto headers = specialHeaders();
45 
46     if(headers.size() == 0 || (headers.size() > 0 && headers.count(Http::CONTENT_TYPE) == 0))
47     {
48         headers[Http::CONTENT_TYPE] = "application/xml";
49     }
50 
51     return headers;
52 }
53 
Parameters() const54 ParameterCollection OssRequest::Parameters() const
55 {
56     return  specialParameters();
57 }
58 
Body() const59 std::shared_ptr<std::iostream> OssRequest::Body() const
60 {
61     std::string&& p = payload();
62     std::shared_ptr<std::iostream> payloadBody;
63     if (!p.empty())
64     {
65       payloadBody = std::make_shared<std::stringstream>();
66       *payloadBody << p;
67     }
68     return payloadBody;
69 }
70 
validate() const71 int OssRequest::validate() const
72 {
73     return 0;
74 }
75 
validateMessage(int code) const76 const char * OssRequest::validateMessage(int code) const
77 {
78     return GetModelErrorMsg(code);
79 }
80 
payload() const81 std::string OssRequest::payload() const
82 {
83     return "";
84 }
85 
specialHeaders() const86 HeaderCollection OssRequest::specialHeaders() const
87 {
88     return HeaderCollection();
89 }
90 
specialParameters() const91 ParameterCollection OssRequest::specialParameters() const
92 {
93     return ParameterCollection();
94 }
95 
bucket() const96 const std::string& OssRequest::bucket() const
97 {
98     return bucket_;
99 }
100 
key() const101 const std::string& OssRequest::key()  const
102 {
103     return key_;
104 }
105 
validate() const106 int OssBucketRequest::validate() const
107 {
108     if (!IsValidBucketName(Bucket())) {
109         return ARG_ERROR_BUCKET_NAME;
110     }
111     return 0;
112 }
113 
setBucket(const std::string & bucket)114 void OssBucketRequest::setBucket(const std::string &bucket)
115 {
116     bucket_ = bucket;
117 }
118 
Bucket() const119 const std::string& OssBucketRequest::Bucket() const
120 {
121     return OssRequest::bucket();
122 }
123 
validate() const124 int OssObjectRequest::validate() const
125 {
126     if (!IsValidBucketName(Bucket())) {
127         return ARG_ERROR_BUCKET_NAME;
128     }
129 
130     if (!IsValidObjectKey(Key())) {
131         return ARG_ERROR_OBJECT_NAME;
132     }
133 
134     return 0;
135 }
136 
setBucket(const std::string & bucket)137 void OssObjectRequest::setBucket(const std::string &bucket)
138 {
139     bucket_ = bucket;
140 }
141 
Bucket() const142 const std::string& OssObjectRequest::Bucket() const
143 {
144     return OssRequest::bucket();
145 }
146 
setKey(const std::string & key)147 void OssObjectRequest::setKey(const std::string &key)
148 {
149     key_ = key;
150 }
151 
Key() const152 const std::string& OssObjectRequest::Key() const
153 {
154     return OssRequest::key();
155 }
156 
157 
setRequestPayer(AlibabaCloud::OSS::RequestPayer key)158 void OssObjectRequest::setRequestPayer(AlibabaCloud::OSS::RequestPayer key)
159 {
160     requestPayer_ = key;
161 }
162 
RequestPayer() const163 AlibabaCloud::OSS::RequestPayer OssObjectRequest::RequestPayer() const
164 {
165     return requestPayer_;
166 }
167 
setVersionId(const std::string & versionId)168 void OssObjectRequest::setVersionId(const std::string& versionId)
169 {
170     versionId_ = versionId;
171 }
172 
VersionId() const173 const std::string& OssObjectRequest::VersionId() const
174 {
175     return versionId_;
176 }
177 
specialHeaders() const178 HeaderCollection OssObjectRequest::specialHeaders() const
179 {
180     auto headers = OssRequest::specialHeaders();
181     if (requestPayer_ == AlibabaCloud::OSS::RequestPayer::Requester) {
182         headers["x-oss-request-payer"] = ToLower(ToRequestPayerName(AlibabaCloud::OSS::RequestPayer::Requester));
183     }
184     return headers;
185 }
186 
specialParameters() const187 ParameterCollection OssObjectRequest::specialParameters() const
188 {
189     auto parameters = OssRequest::specialParameters();
190     if (!versionId_.empty()) {
191         parameters["versionId"] = versionId_;
192     }
193     return parameters;
194 }
195 
validate() const196 int OssResumableBaseRequest::validate() const
197 {
198     if (!IsValidBucketName(Bucket())) {
199         return ARG_ERROR_BUCKET_NAME;
200     }
201 
202     if (!IsValidObjectKey(Key())) {
203         return ARG_ERROR_OBJECT_NAME;
204     }
205 
206     if (partSize_ < PartSizeLowerLimit) {
207         return ARG_ERROR_CHECK_PART_SIZE_LOWER;
208     }
209 
210     if (threadNum_ <= 0) {
211         return ARG_ERROR_CHECK_THREAD_NUM_LOWER;
212     }
213 
214 #if !defined(_WIN32)
215     if (!checkpointDirW_.empty()) {
216         return ARG_ERROR_PATH_NOT_SUPPORT_WSTRING_TYPE;
217     }
218 #endif
219 
220     // if directory do not exist, return error
221     if (hasCheckpointDir()) {
222         if ((!checkpointDir_.empty() && !IsDirectoryExist(checkpointDir_))
223 #ifdef _WIN32
224             || (!checkpointDirW_.empty() && !IsDirectoryExist(checkpointDirW_))
225 #endif
226             ) {
227             return ARG_ERROR_CHECK_POINT_DIR_NONEXIST;
228         }
229     }
230 
231     return 0;
232 }
233 
validateMessage(int code) const234 const char *OssResumableBaseRequest::validateMessage(int code) const
235 {
236     return GetModelErrorMsg(code);
237 }
238 
setBucket(const std::string & bucket)239 void OssResumableBaseRequest::setBucket(const std::string &bucket)
240 {
241     bucket_ = bucket;
242 }
243 
Bucket() const244 const std::string& OssResumableBaseRequest::Bucket() const
245 {
246     return OssRequest::bucket();
247 }
248 
setKey(const std::string & key)249 void OssResumableBaseRequest::setKey(const std::string &key)
250 {
251     key_ = key;
252 }
253 
Key() const254 const std::string& OssResumableBaseRequest::Key() const
255 {
256     return OssRequest::key();
257 }
258 
setPartSize(uint64_t partSize)259 void OssResumableBaseRequest::setPartSize(uint64_t partSize)
260 {
261     partSize_ = partSize;
262 }
263 
PartSize() const264 uint64_t OssResumableBaseRequest::PartSize() const
265 {
266     return partSize_;
267 }
268 
setObjectSize(uint64_t objectSize)269 void OssResumableBaseRequest::setObjectSize(uint64_t objectSize)
270 {
271     objectSize_ = objectSize;
272 }
273 
ObjectSize() const274 uint64_t OssResumableBaseRequest::ObjectSize() const
275 {
276     return objectSize_;
277 }
278 
setThreadNum(uint32_t threadNum)279 void OssResumableBaseRequest::setThreadNum(uint32_t threadNum)
280 {
281     threadNum_ = threadNum;
282 }
283 
ThreadNum() const284 uint32_t OssResumableBaseRequest::ThreadNum() const
285 {
286     return threadNum_;
287 }
288 
setCheckpointDir(const std::string & checkpointDir)289 void OssResumableBaseRequest::setCheckpointDir(const std::string &checkpointDir)
290 {
291     checkpointDir_ = checkpointDir;
292     checkpointDirW_.clear();
293 }
294 
CheckpointDir() const295 const std::string& OssResumableBaseRequest::CheckpointDir() const
296 {
297     return checkpointDir_;
298 }
299 
setCheckpointDir(const std::wstring & checkpointDir)300 void OssResumableBaseRequest::setCheckpointDir(const std::wstring& checkpointDir)
301 {
302     checkpointDirW_ = checkpointDir;
303     checkpointDir_.clear();
304 }
305 
CheckpointDirW() const306 const std::wstring& OssResumableBaseRequest::CheckpointDirW() const
307 {
308     return checkpointDirW_;
309 }
310 
setObjectMtime(const std::string & mtime)311 void OssResumableBaseRequest::setObjectMtime(const std::string &mtime)
312 {
313     mtime_ = mtime;
314 }
315 
ObjectMtime() const316 const std::string& OssResumableBaseRequest::ObjectMtime() const
317 {
318     return mtime_;
319 }
320 
setRequestPayer(AlibabaCloud::OSS::RequestPayer value)321 void OssResumableBaseRequest::setRequestPayer(AlibabaCloud::OSS::RequestPayer value)
322 {
323     requestPayer_ = value;
324 }
325 
RequestPayer() const326 AlibabaCloud::OSS::RequestPayer OssResumableBaseRequest::RequestPayer() const
327 {
328     return requestPayer_;
329 }
330 
setTrafficLimit(uint64_t value)331 void OssResumableBaseRequest::setTrafficLimit(uint64_t value)
332 {
333     trafficLimit_ = value;
334 }
335 
TrafficLimit() const336 uint64_t OssResumableBaseRequest::TrafficLimit() const
337 {
338     return trafficLimit_;
339 }
340 
setVersionId(const std::string & versionId)341 void OssResumableBaseRequest::setVersionId(const std::string& versionId)
342 {
343     versionId_ = versionId;
344 }
345 
VersionId() const346 const std::string& OssResumableBaseRequest::VersionId() const
347 {
348     return versionId_;
349 }
350 
hasCheckpointDir() const351 bool OssResumableBaseRequest::hasCheckpointDir() const
352 {
353     return (!checkpointDir_.empty() || !checkpointDirW_.empty());
354 }
355 
setBucket(const std::string & bucket)356 void LiveChannelRequest::setBucket(const std::string &bucket)
357 {
358     bucket_ = bucket;
359 }
360 
Bucket() const361 const std::string& LiveChannelRequest::Bucket() const
362 {
363     return bucket_;
364 }
365 
setChannelName(const std::string & channelName)366 void LiveChannelRequest::setChannelName(const std::string &channelName)
367 {
368     channelName_ = channelName;
369     key_ = channelName;
370 }
371 
ChannelName() const372 const std::string& LiveChannelRequest::ChannelName() const
373 {
374     return channelName_;
375 }
376 
validate() const377 int LiveChannelRequest::validate() const
378 {
379     if (!IsValidBucketName(Bucket())) {
380         return ARG_ERROR_BUCKET_NAME;
381     }
382 
383     if(!IsValidChannelName(channelName_))
384     {
385         return ARG_ERROR_LIVECHANNEL_BAD_CHANNELNAME_PARAM;
386     }
387 
388     return 0;
389 }
390