Debian Codegraph @

src/xml/azurepropparser.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 "azurepropparser.hpp"
23 #include <utils/davix_azure_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
41 /*struct S3PropParser::Internal{
42 std::string current;
43 std::string prefix;
44 std::string prefix_to_remove;
45 bool inside_com_prefix;
46 int prop_count;
47 std::stack<std::string> stack_status;
48 std::deque<FileProperties> props;
49
50 FileProperties property;
51 S3ListingMode::S3ListingMode _s3_listing_mode;
52
53 int start_elem(const std::string &elem){
54 // new tag, clean content;
55 current.clear();
56
57 // check XML nested level, security protection
58 if(stack_status.size() < 200){
59 stack_status.push(elem);
60 }else{
61 throw DavixException(davix_scope_xml_parser(), StatusCode::ParsingError, "Impossible to parse S3 content, corrupted XML");
62 }
63
64 // check element, if collection name add first entry
65 if( StrUtil::compare_ncase(col_prop, elem) ==0){
66 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "collection found", elem.c_str());
67 property.clear();
68 prop_count = 0;
69 }
70
71 // check element, if new entry clear current entry
72 if( StrUtil::compare_ncase(delimiter_prop, elem) ==0){
73 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "new element found", elem.c_str());
74 property.clear();
75 }
76
77 // check element, if common prefixes set flag
78 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && StrUtil::compare_ncase(com_prefix_prop, elem) ==0){
79 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "common prefixes found", elem.c_str());
80 inside_com_prefix = true;
81 }
82
83 // check element, if prefix clear current entry
84 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && StrUtil::compare_ncase(prefix_prop, elem) ==0){
85 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "prefix found", elem.c_str());
86 property.clear();
87 }
88
89 return 1;
90 }
91
92 int add_chunk(const std::string & chunk){
93 current.append(chunk);
94 return 0;
95 }
96
97 int end_elem(const std::string &elem){
98 StrUtil::trim(current);
99
100 // found prefix
101 if( (_s3_listing_mode == S3ListingMode::Hierarchical) &&
102 StrUtil::compare_ncase(prefix_prop, elem) ==0 &&
103 !current.empty()){
104 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "new prefix {}", current.c_str());
105 prefix = current;
106 if(inside_com_prefix){ // all keys would have been processed by now, just common prefixes left, use as DIRs
107 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new common prefix {}", current.c_str());
108 current = current.erase(current.size()-1,1);
109 property.filename = current.erase(0, prefix_to_remove.size());
110 property.info.mode = 0755 | S_IFDIR;
111 property.info.mode &= ~(S_IFREG);
112 props.push_back(property);
113 prop_count++;
114 }
115 }
116
117 // new name new fileprop
118 if( StrUtil::compare_ncase(name_prop, elem) ==0){
119
120 if((_s3_listing_mode == S3ListingMode::Flat)){ // flat mode
121 property.filename = current.erase(0,prefix.size());
122 }
123 else if(prefix.empty()){ // at root level
124 property.filename = current;
125 }
126 else if(!prefix.empty()){
127 if(prefix.compare((prefix.size()-1),1,"/")){ // prefix doesn't end with '/', file
128 property.filename = current;
129 }
130 else if(!(StrUtil::compare_ncase(prefix, current) ==0)){ // folder
131 property.filename = current.erase(0, prefix_to_remove.size());
132
133 }
134 }
135
136 if(!property.filename.empty())
137 property.info.mode = 0755;
138 }
139
140 if( StrUtil::compare_ncase(size_prop, elem) ==0){
141 try{
142 dav_size_t size = toType<dav_size_t, std::string>()(current);
143 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element size {}", size);
144 property.info.size = size;
145 }catch(...){
146 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element size");
147 }
148 }
149
150 if( StrUtil::compare_ncase(last_modified_prop, elem) ==0){
151 try{
152 time_t mtime = S3::s3TimeConverter(current);
153 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element LastModified {}", current);
154 property.info.mtime = mtime;
155 property.info.ctime = mtime;
156 }catch(...){
157 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element LastModified");
158 }
159 }
160
161 // found bucket name
162 // push it as first item to identify bucket
163 if( StrUtil::compare_ncase(col_prop, elem) ==0){
164 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push collection", elem.c_str());
165 property.filename = current;
166 property.info.mode |= S_IFDIR;
167 property.info.mode &= ~(S_IFREG);
168 props.push_back(property);
169 }
170
171 // check element, if end entry push new entry
172 if( StrUtil::compare_ncase(delimiter_prop, elem) ==0){
173 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new element {}", elem.c_str());
174 props.push_back(property);
175 prop_count++;
176 }
177
178 // check element, if end common prefix reset flag
179 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && StrUtil::compare_ncase(com_prefix_prop, elem) ==0){
180 inside_com_prefix = false;
181 }
182
183 // end of xml respond and still no property, requested key exists but isn't a directory
184 if( (_s3_listing_mode == S3ListingMode::Hierarchical) && (StrUtil::compare_ncase(listbucketresult_prop, elem) ==0) && (prop_count ==0) ){
185 throw DavixException(davix_scope_directory_listing_str(), StatusCode::IsNotADirectory, "Not a S3 directory");
186 }
187
188 // reduce stack size
189 if(stack_status.size() > 0)
190 stack_status.pop();
191 current.clear();
192 return 0;
193 }
194
195 }; */
196
197 struct AzurePropParser::Internal {
198 std::string current;
199 std::string name;
200 std::string prefix_to_remove;
201 std::deque<FileProperties> props;
202 FileProperties property;
203 bool inside_prefix;
204
205 int start_elem(const std::string &elem){
206 // new tag, clean content;
207 current.clear();
208
209 // starting a new blob?
210 if(StrUtil::compare_ncase("Blob", elem) ==0){
211 inside_prefix = false;
212 }
213
214 // starting a blob prefix?
215 if(StrUtil::compare_ncase("BlobPrefix", elem) == 0) {
216 inside_prefix = true;
217 }
218
219 //std::cout << "start_elem: " << elem << std::endl;
220 return 1;
221 }
222
223 int add_chunk(const std::string & chunk){
224 current.append(chunk);
225 return 0;
226 }
227
228 int end_elem(const std::string &elem){
229 // name
230 if(StrUtil::compare_ncase("Name", elem) == 0) {
231 property.filename = current.erase(0, prefix_to_remove.size());
232 }
233
234 // processing a BlobPrefix? Add new dir
235 if( StrUtil::compare_ncase("BlobPrefix", elem) ==0){
236 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new common prefix {}", current.c_str());
237 property.filename = current.erase(current.size()-1, 1);
238 property.info.mode = 0755 | S_IFDIR;
239 property.info.mode &= ~(S_IFREG);
240 props.push_back(property);
241 }
242
243 // blob has ended? push new entry
244 if( StrUtil::compare_ncase("Blob", elem) ==0){
245 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push new element {}", elem.c_str());
246 property.info.mode = 0755;
247 props.push_back(property);
248 }
249
250 if( StrUtil::compare_ncase("Content-Length", elem) ==0){
251 try{
252 dav_size_t size = toType<dav_size_t, std::string>()(current);
253 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element size {}", size);
254 property.info.size = size;
255 }catch(...){
256 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element size");
257 }
258 }
259
260 if( StrUtil::compare_ncase("Last-Modified", elem) ==0){
261 try{
262 time_t mtime = S3::s3TimeConverter(current);
263 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "element LastModified {}", current);
264 property.info.mtime = mtime;
265 property.info.ctime = mtime;
266 }catch(...){
267 DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "Unable to parse element LastModified");
268 }
269 }
270
271 return 0;
272 }
273 };
274
275
276 AzurePropParser::AzurePropParser(std::string prefix) : d_ptr(new Internal())
277 {
278 if(prefix[prefix.size() - 1] != '/') {
279 d_ptr->prefix_to_remove = prefix + "/";
280 }
281 else {
282 d_ptr->prefix_to_remove = prefix;
283 }
284
285 if(d_ptr->prefix_to_remove == "/") {
286 d_ptr->prefix_to_remove = "";
287 }
288 }
289
290 AzurePropParser::~AzurePropParser(){ }
291
292
293 int AzurePropParser::parserStartElemCb(int parent, const char *nspace, const char *name, const char **atts){
294 /*(void) parent;
295 (void) nspace;
296 (void) atts;
297 return d_ptr->start_elem(std::string(name)); */
298
299 return d_ptr->start_elem(std::string(name));
300 }
301
302 int AzurePropParser::parserCdataCb(int state, const char *cdata, size_t len){
303 /*(void) state;
304 (void) len;
305 return d_ptr->add_chunk(std::string(cdata, len));*/
306 //std::cout << "cdata: " << std::string(cdata, len) << std::endl;
307 return d_ptr->add_chunk(std::string(cdata, len));
308 }
309
310 int AzurePropParser::parserEndElemCb(int state, const char *nspace, const char *name){
311 /*(void) state;
312 (void) nspace;
313 return d_ptr->end_elem(std::string(name));*/
314
315 return d_ptr->end_elem(std::string(name));
316 }
317
318 std::deque<FileProperties> & AzurePropParser::getProperties(){
319 return d_ptr->props;
320 }
321
322
323 }