| 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 "s3deleteparser.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 delete_result_prop = "DeleteResult"; |
| 32 |
const std::string delete_prop = "Deleted"; |
| 33 |
const std::string key_prop = "Key"; |
| 34 |
const std::string error_prop = "Error"; |
| 35 |
const std::string code_prop = "Code"; |
| 36 |
const std::string message_prop = "Message"; |
| 37 |
|
| 38 |
struct S3DeleteParser::Internal{ |
| 39 |
std::string current; |
| 40 |
std::string prefix; |
| 41 |
int entry_count; |
| 42 |
std::stack<std::string> stack_status; |
| 43 |
std::deque<FileDeleteStatus> del_status; |
| 44 |
FileDeleteStatus status; |
| 45 |
std::deque<FileProperties> props; // not used |
| 46 |
|
| 47 |
int start_elem(const std::string &elem){ |
| 48 |
// new tag, clean content; |
| 49 |
current.clear(); |
| 50 |
|
| 51 |
// check XML nested level, security protection |
| 52 |
if(stack_status.size() < 200){ |
| 53 |
stack_status.push(elem); |
| 54 |
}else{ |
| 55 |
throw DavixException(davix_scope_xml_parser(), StatusCode::ParsingError, "Impossible to parse S3 content, corrupted XML"); |
| 56 |
} |
| 57 |
|
| 58 |
// check element, if it is "deleted" this recource has beed deleted successfully |
| 59 |
// or the resource did not exist in the first place, either way, log it |
| 60 |
if( StrUtil::compare_ncase(delete_prop, elem) ==0){ |
| 61 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "deleted entry found", elem.c_str()); |
| 62 |
status.clear(); |
| 63 |
entry_count = 0; |
| 64 |
} |
| 65 |
|
| 66 |
// check element, if "Error" there has been problem with deleting this resource |
| 67 |
// the code returned will have to be mapped to http code |
| 68 |
if( StrUtil::compare_ncase(error_prop, elem) ==0){ |
| 69 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "error entry found", elem.c_str()); |
| 70 |
status.clear(); |
| 71 |
status.error = true; |
| 72 |
entry_count = 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
return 1; |
| 77 |
} |
| 78 |
|
| 79 |
int add_chunk(const std::string & chunk){ |
| 80 |
current.append(chunk); |
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
int end_elem(const std::string &elem){ |
| 85 |
StrUtil::trim(current); |
| 86 |
|
| 87 |
// if "Key", current is file name |
| 88 |
// if "Code", current is error code |
| 89 |
// if "Message", current is error message |
| 90 |
|
| 91 |
// if "Delete", end of successful delete entry for that resource, push it |
| 92 |
if( StrUtil::compare_ncase(delete_prop, elem) ==0){ |
| 93 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push deleted status for {}", status.filename.c_str()); |
| 94 |
del_status.push_back(status); |
| 95 |
entry_count++; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
// if "Error", end of error entry for that resource, push it |
| 100 |
if( StrUtil::compare_ncase(error_prop, elem) ==0){ |
| 101 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "push error status for {}", status.filename.c_str()); |
| 102 |
del_status.push_back(status); |
| 103 |
entry_count++; |
| 104 |
} |
| 105 |
|
| 106 |
// if "Key", current is file name |
| 107 |
if( StrUtil::compare_ncase(key_prop, elem) ==0){ |
| 108 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "key found for {}", current); |
| 109 |
status.filename = current; |
| 110 |
} |
| 111 |
|
| 112 |
// if "Code", current is error code |
| 113 |
if( StrUtil::compare_ncase(code_prop, elem) ==0){ |
| 114 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "code found {}", current); |
| 115 |
status.error_code = current; |
| 116 |
} |
| 117 |
|
| 118 |
// if "Message", current is error message |
| 119 |
if( StrUtil::compare_ncase(message_prop, elem) ==0){ |
| 120 |
DAVIX_SLOG(DAVIX_LOG_TRACE, DAVIX_LOG_XML, "error message found {}", current); |
| 121 |
status.message = current; |
| 122 |
} |
| 123 |
|
| 124 |
// reduce stack size |
| 125 |
if(stack_status.size() > 0) |
| 126 |
stack_status.pop(); |
| 127 |
current.clear(); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
}; |
| 132 |
|
| 133 |
S3DeleteParser::S3DeleteParser() : d_ptr(new Internal()) |
| 134 |
{ |
| 135 |
} |
| 136 |
|
| 137 |
S3DeleteParser::~S3DeleteParser(){ |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
int S3DeleteParser::parserStartElemCb(int parent, const char *nspace, const char *name, const char **atts){ |
| 142 |
(void) parent; |
| 143 |
(void) nspace; |
| 144 |
(void) atts; |
| 145 |
return d_ptr->start_elem(std::string(name)); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
int S3DeleteParser::parserCdataCb(int state, const char *cdata, size_t len){ |
| 150 |
(void) state; |
| 151 |
(void) len; |
| 152 |
return d_ptr->add_chunk(std::string(cdata, len)); |
| 153 |
} |
| 154 |
|
| 155 |
int S3DeleteParser::parserEndElemCb(int state, const char *nspace, const char *name){ |
| 156 |
(void) state; |
| 157 |
(void) nspace; |
| 158 |
return d_ptr->end_elem(std::string(name)); |
| 159 |
} |
| 160 |
|
| 161 |
std::deque<FileDeleteStatus> & S3DeleteParser::getDeleteStatus(){ |
| 162 |
return d_ptr->del_status; |
| 163 |
} |
| 164 |
|
| 165 |
// not used |
| 166 |
std::deque<FileProperties> & S3DeleteParser::getProperties(){ |
| 167 |
return d_ptr->props; |
| 168 |
} |
| 169 |
|
| 170 |
} |