| 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 <davix_internal.hpp> |
| 23 |
#include "davdeletexmlparser.hpp" |
| 24 |
|
| 25 |
#include <utils/davix_logger_internal.hpp> |
| 26 |
#include <status/davixstatusrequest.hpp> |
| 27 |
#include "libs/datetime/datetime_utils.hpp" |
| 28 |
#include <utils/stringutils.hpp> |
| 29 |
|
| 30 |
using namespace StrUtil; |
| 31 |
|
| 32 |
namespace Davix { |
| 33 |
|
| 34 |
|
| 35 |
const Xml::XmlPTree prop_response(Xml::ElementStart, "response"); |
| 36 |
|
| 37 |
static std::unique_ptr<Xml::XmlPTree> webDavTree; |
| 38 |
|
| 39 |
static std::once_flag _l_init; |
| 40 |
|
| 41 |
struct DavDeleteXMLParser::DavxDeleteXmlIntern{ |
| 42 |
DavxDeleteXmlIntern() : _stack(), |
| 43 |
_props(), _current_props(), _last_response_status(500), _last_filename(){ |
| 44 |
_stack.reserve(10); |
| 45 |
char_buffer.reserve(1024); |
| 46 |
} |
| 47 |
|
| 48 |
// node stack |
| 49 |
std::vector<Xml::XmlPTree> _stack; |
| 50 |
|
| 51 |
// props |
| 52 |
std::deque<FileProperties> _props; |
| 53 |
FileProperties _current_props; |
| 54 |
int _last_response_status; |
| 55 |
std::string _last_filename; |
| 56 |
|
| 57 |
// buffer |
| 58 |
std::string char_buffer; |
| 59 |
|
| 60 |
inline void appendChars(const char *buff, size_t len){ |
| 61 |
char_buffer.append(std::string(buff, len)); |
| 62 |
} |
| 63 |
|
| 64 |
inline void clear(){ |
| 65 |
char_buffer.clear(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
inline void add_new_elem(){ |
| 70 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_XML, " properties detected "); |
| 71 |
_current_props.clear(); |
| 72 |
_current_props.filename = _last_filename; // setup the current filename |
| 73 |
_current_props.req_status = _last_response_status; |
| 74 |
} |
| 75 |
|
| 76 |
inline void store_new_elem(){ |
| 77 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_XML, " end of properties... "); |
| 78 |
_props.push_back(_current_props); |
| 79 |
} |
| 80 |
|
| 81 |
inline void update_elem(){ |
| 82 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_XML, " updating propertie's info "); |
| 83 |
_current_props.filename = _last_filename; |
| 84 |
_current_props.req_status = _last_response_status; |
| 85 |
} |
| 86 |
|
| 87 |
}; |
| 88 |
|
| 89 |
typedef void (*properties_cb)(DavDeleteXMLParser::DavxDeleteXmlIntern & par, const std::string & name); |
| 90 |
|
| 91 |
static void check_href(DavDeleteXMLParser::DavxDeleteXmlIntern & par, const std::string & name){ |
| 92 |
std::string _href(name); |
| 93 |
rtrim(_href, isSlash()); // remove trailing slash |
| 94 |
std::string::reverse_iterator it = std::find(_href.rbegin(), _href.rend(), '/'); |
| 95 |
if( it == _href.rend()){ |
| 96 |
par._last_filename.assign(_href); |
| 97 |
}else{ |
| 98 |
par._last_filename.assign(it.base(), _href.end()); |
| 99 |
} |
| 100 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_XML, " href/filename parsed -> {} ", par._last_filename.c_str() ); |
| 101 |
} |
| 102 |
|
| 103 |
static void check_status(DavDeleteXMLParser::DavxDeleteXmlIntern & par, const std::string & name){ |
| 104 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_XML, " status found -> parse it"); |
| 105 |
std::string str_status(name); |
| 106 |
ltrim(str_status, StrUtil::isSpace()); |
| 107 |
std::string::iterator it1, it2; |
| 108 |
it1 = std::find(str_status.begin(), str_status.end(), ' '); |
| 109 |
if( it1 != str_status.end()){ |
| 110 |
it2 = std::find(it1+1, str_status.end(), ' '); |
| 111 |
std::string str_status_parsed(it1+1, it2); |
| 112 |
unsigned long res = strtoul(str_status_parsed.c_str(), NULL, 10); |
| 113 |
if(res != ULONG_MAX){ |
| 114 |
DAVIX_SLOG(DAVIX_LOG_DEBUG, DAVIX_LOG_XML, " status value : {}", res); |
| 115 |
par._last_response_status = res; |
| 116 |
return; |
| 117 |
} |
| 118 |
} |
| 119 |
DAVIX_SLOG(DAVIX_LOG_VERBOSE, DAVIX_LOG_XML, "Invalid dav status field value"); |
| 120 |
errno =0; |
| 121 |
} |
| 122 |
|
| 123 |
static void init_webdavTree(){ |
| 124 |
// Nodes list |
| 125 |
webDavTree.reset(new Xml::XmlPTree(Xml::ElementStart, "multistatus")); |
| 126 |
webDavTree->addChild(Xml::XmlPTree(Xml::ElementStart, "response")); |
| 127 |
Xml::XmlPTree::iterator it = webDavTree->beginChildren(); |
| 128 |
it->addChild(Xml::XmlPTree(Xml::ElementStart, "href", Xml::XmlPTree::ChildrenList(), (void*) &check_href)); |
| 129 |
it->addChild(Xml::XmlPTree(Xml::ElementStart, "status", Xml::XmlPTree::ChildrenList(), (void*) &check_status)); |
| 130 |
} |
| 131 |
|
| 132 |
DavDeleteXMLParser::DavDeleteXMLParser() : |
| 133 |
d_ptr(new DavxDeleteXmlIntern()) |
| 134 |
{ |
| 135 |
std::call_once(_l_init, init_webdavTree); |
| 136 |
} |
| 137 |
|
| 138 |
DavDeleteXMLParser::~DavDeleteXMLParser(){ |
| 139 |
delete d_ptr; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
std::deque<FileProperties> & DavDeleteXMLParser::getProperties(){ |
| 144 |
return d_ptr->_props; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
int DavDeleteXMLParser::parserStartElemCb(int parent, const char *nspace, const char *name, const char **atts){ |
| 149 |
(void) parent; |
| 150 |
(void) name; |
| 151 |
(void) nspace; |
| 152 |
(void) atts; |
| 153 |
// add elem to stack |
| 154 |
Xml::XmlPTree node(Xml::ElementStart, name); |
| 155 |
d_ptr->_stack.push_back(node); |
| 156 |
|
| 157 |
// if beginning of prop, add new element |
| 158 |
if(node.compareNode(prop_response)){ |
| 159 |
d_ptr->add_new_elem(); |
| 160 |
} |
| 161 |
|
| 162 |
return 1; |
| 163 |
} |
| 164 |
|
| 165 |
int DavDeleteXMLParser::parserCdataCb(int state, const char *cdata, size_t len){ |
| 166 |
(void) state; |
| 167 |
d_ptr->appendChars(cdata, len); |
| 168 |
return 0; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
int DavDeleteXMLParser::parserEndElemCb(int state, const char *nspace, const char *name){ |
| 173 |
(void) state; |
| 174 |
(void) nspace; |
| 175 |
Xml::XmlPTree node(Xml::ElementStart, name); |
| 176 |
|
| 177 |
if(node.compareNode(prop_response)){ |
| 178 |
d_ptr->store_new_elem(); |
| 179 |
} |
| 180 |
|
| 181 |
// find potential interesting data |
| 182 |
std::vector<Xml::XmlPTree::ptr_type> chain; |
| 183 |
if(d_ptr->char_buffer.size() != 0){ |
| 184 |
chain = webDavTree->findChain(d_ptr->_stack); |
| 185 |
if(chain.size() > 0){ |
| 186 |
properties_cb cb = ((properties_cb) chain.at(chain.size()-1)->getMeta()); |
| 187 |
if(cb){ |
| 188 |
StrUtil::trim(d_ptr->char_buffer); |
| 189 |
cb(*d_ptr, d_ptr->char_buffer); |
| 190 |
} |
| 191 |
} |
| 192 |
d_ptr->update_elem(); |
| 193 |
} |
| 194 |
|
| 195 |
// cleaning work |
| 196 |
if(d_ptr->_stack.size() == 0) |
| 197 |
throw DavixException(davix_scope_xml_parser(),StatusCode::ParsingError, "Corrupted Parser Stack, Invalid XML"); |
| 198 |
d_ptr->_stack.pop_back(); |
| 199 |
d_ptr->clear(); |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
} // namespace Davix |