Debian Codegraph @

src/xml/s3propparser.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2013
4 * Author: Adrien Devresse <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include "s3propparser.hpp"
23 #include <utils/davix_s3_utils.hpp>
24
25 #include <stack>
26 #include <utils/davix_logger_internal.hpp>
27
28
29 namespace Davix{
30
31 const std::string col_prop = "Name";
32 const std::string delimiter_prop ="Contents";
33 const std::string name_prop = "Key";
34 const std::string size_prop = "Size";
35
36 const std::string prefix_prop = "Prefix";
37 const std::string com_prefix_prop = "CommonPrefixes";
38 const std::string listbucketresult_prop = "ListBucketResult";
39 const std::string last_modified_prop = "LastModified";
40 const std::string istruncated_prop = "IsTruncated";
41 const std::string nextmarker_prop = "NextMarker";
42
43 struct S3PropParser::Internal{
44 std::string current;
45 std::string prefix;
46 std::string prefix_to_remove;
47 bool inside_com_prefix;
48 int prop_count;
49 std::stack<std::string> stack_status;
50 std::deque<FileProperties> props;
51
52 FileProperties property;
53 S3ListingMode::S3ListingMode _s3_listing_mode;
54
55 bool istruncated = false;
56 std::string nextmarker;
57 std::string nextmarker_last_key;
58
59 int start_elem(const std::string &elem){
60 // new tag, clean content;
61 current.clear();
62
63 // check XML nested level, security protection
64 if(stack_status.size() < 200){
65 stack_status.push(elem);
66 }else{
67 throw DavixException(davix_scope_xml_parser(), StatusCode::ParsingError, "Impossible to parse S3 content, corrupted XML");
68 }
69
70 // check element, if collection name add first entry
71 if( StrUtil::compare_ncase(col_prop, elem) ==0){
72 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "collection found", elem.c_str());
73 property.clear();
74 prop_count = 0;
75 }
76
77 // check element, if new entry clear current entry
78 if( StrUtil::compare_ncase(delimiter_prop, elem) ==0){
79 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "new element found", elem.c_str());
80 property.clear();
81 }
82
83 // check element, if common prefixes set flag
84 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && StrUtil::compare_ncase(com_prefix_prop, elem) ==0){
85 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "common prefixes found", elem.c_str());
86 inside_com_prefix = true;
87 }
88
89 // check element, if prefix clear current entry
90 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && StrUtil::compare_ncase(prefix_prop, elem) ==0){
91 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "prefix found", elem.c_str());
92 property.clear();
93 }
94
95 return 1;
96 }
97
98 int add_chunk(const std::string & chunk){
99 current.append(chunk);
100 return 0;
101 }
102
103 int end_elem(const std::string &elem){
104 StrUtil::trim(current);
105
106 // found prefix
107 if( (_s3_listing_mode == S3ListingMode::Hierarchical) &&
108 StrUtil::compare_ncase(prefix_prop, elem) ==0 &&
109 !current.empty()){
110 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "new prefix {}", current.c_str());
111 prefix = current;
112 if(inside_com_prefix){ // all keys would have been processed by now, just common prefixes left, use as DIRs
113 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new common prefix {}", current.c_str());
114 // Save key for "truncated reply with missing NextMarker" scenario
115 nextmarker_last_key = current;
116 current = current.erase(current.size()-1,1);
117 property.filename = current.erase(0, prefix_to_remove.size());
118 property.info.mode = 0755 | S_IFDIR;
119 property.info.mode &= ~(S_IFREG);
120 props.push_back(property);
121 prop_count++;
122 }
123 }
124
125 // new name new fileprop
126 if( StrUtil::compare_ncase(name_prop, elem) ==0){
127 // Save key for "truncated reply with missing NextMarker" scenario
128 nextmarker_last_key = current;
129
130 if(_s3_listing_mode == S3ListingMode::Flat) { // flat mode
131 property.filename = current.erase(0,prefix.size());
132 }
133 else if(prefix.empty()){ // at root level
134 property.filename = current;
135 }
136 else if(!prefix.empty()){
137 if(prefix.compare((prefix.size()-1),1,"/")){ // prefix doesn't end with '/', file
138 property.filename = current;
139 }
140 else if(!(StrUtil::compare_ncase(prefix, current) ==0)){ // folder
141 property.filename = current.erase(0, prefix_to_remove.size());
142
143 }
144 }
145
146 if(!property.filename.empty())
147 property.info.mode = 0755;
148 }
149
150 if( StrUtil::compare_ncase(size_prop, elem) ==0){
151 try{
152 dav_size_t size = toType<dav_size_t, std::string>()(current);
153 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element size {}", size);
154 property.info.size = size;
155 }catch(...){
156 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element size");
157 }
158 }
159
160 if( StrUtil::compare_ncase(last_modified_prop, elem) ==0){
161 try{
162 time_t mtime = S3::s3TimeConverter(current);
163 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element LastModified {}", current);
164 property.info.mtime = mtime;
165 property.info.ctime = mtime;
166 }catch(...){
167 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element LastModified");
168 }
169 }
170
171 // found bucket name
172 // push it as first item to identify bucket
173 if( StrUtil::compare_ncase(col_prop, elem) ==0){
174 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push collection", elem.c_str());
175 property.filename = current;
176 property.info.mode |= S_IFDIR;
177 property.info.mode &= ~(S_IFREG);
178 props.push_back(property);
179 }
180
181 // check element, if end entry push new entry
182 if( StrUtil::compare_ncase(delimiter_prop, elem) ==0){
183 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new element {}", elem.c_str());
184
185 prop_count++;
186 // empty filename? That means this is the first "Contents" entry,
187 // indicating the bucket name, which we have already pushed. Skip.
188 if(property.filename.size() > 0) {
189 props.push_back(property);
190 }
191 }
192
193 // check element, if end common prefix reset flag
194 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && StrUtil::compare_ncase(com_prefix_prop, elem) ==0){
195 inside_com_prefix = false;
196 }
197
198 // end of xml respond and still no property, requested key exists but isn't a directory
199 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && (StrUtil::compare_ncase(listbucketresult_prop, elem) ==0) && (prop_count ==0) ){
200 throw DavixException(davix_scope_directory_listing_str(), StatusCode::IsNotADirectory, "Not a S3 directory");
201 }
202
203 // IsTruncated
204 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && (StrUtil::compare_ncase(istruncated_prop, elem) ==0)){
205 istruncated = (current == "True" || current == "true");
206 }
207
208 // NextMarker
209 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && (StrUtil::compare_ncase(nextmarker_prop, elem) ==0)){
210 nextmarker = current;
211 }
212
213 // reduce stack size
214 if(stack_status.size() > 0)
215 stack_status.pop();
216 current.clear();
217 return 0;
218 }
219
220 };
221
222 S3PropParser::S3PropParser() : d_ptr(new Internal())
223 {
224 S3PropParser(S3ListingMode::Hierarchical, "");
225 }
226
227
228 S3PropParser::S3PropParser(S3ListingMode::S3ListingMode s3_listing_mode) : d_ptr(new Internal())
229 {
230 S3PropParser(s3_listing_mode, "");
231 }
232
233
234 S3PropParser::S3PropParser(S3ListingMode::S3ListingMode s3_listing_mode, std::string s3_prefix) : d_ptr(new Internal())
235 {
236 d_ptr->_s3_listing_mode = s3_listing_mode;
237
238 if(!s3_prefix.empty()){
239 if(s3_prefix.compare(s3_prefix.size()-1,1,"/")==0)
240 d_ptr->prefix_to_remove = s3_prefix.erase(0,1);
241 else
242 d_ptr->prefix_to_remove = s3_prefix;
243 }
244 }
245
246
247 S3PropParser::~S3PropParser(){
248 }
249
250
251 int S3PropParser::parserStartElemCb(int parent, const char *nspace, const char *name, const char **atts){
252 (void) parent;
253 (void) nspace;
254 (void) atts;
255 return d_ptr->start_elem(std::string(name));
256
257 }
258
259 int S3PropParser::parserCdataCb(int state, const char *cdata, size_t len){
260 (void) state;
261 (void) len;
262 return d_ptr->add_chunk(std::string(cdata, len));
263 }
264
265 int S3PropParser::parserEndElemCb(int state, const char *nspace, const char *name){
266 (void) state;
267 (void) nspace;
268 return d_ptr->end_elem(std::string(name));
269 }
270
271 std::deque<FileProperties> & S3PropParser::getProperties(){
272 return d_ptr->props;
273 }
274
275 std::string S3PropParser::getNextMarker() {
276 if (d_ptr->istruncated) {
277 if (!d_ptr->nextmarker.empty()) {
278 return d_ptr->nextmarker;
279 }
280
281 return d_ptr->nextmarker_last_key;
282 }
283
284 return "";
285 }
286
287
288 }