added class to parse XML in HTTP response

pull/40/head
Matchman Green 2012-05-06 02:37:54 +08:00
parent b42e7c6501
commit 5e615de7cd
5 changed files with 127 additions and 2 deletions

View File

@ -21,10 +21,12 @@
#include "http/Download.hh"
#include "http/StringResponse.hh"
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#include "protocol/OAuth2.hh"
#include "util/OS.hh"
#include "util/Path.hh"
#include "xml/Node.hh"
#include <algorithm>
#include <iostream>
@ -161,7 +163,11 @@ bool File::Upload( std::streambuf *file, const http::Headers& auth )
uphdr.push_back( "Expect:" ) ;
uphdr.push_back( "Accept:" ) ;
http.Put( uplink, data, &str, uphdr ) ;
http::XmlResponse xml ;
http.Put( uplink, data, &xml, uphdr ) ;
std::cout << xml.Response() << std::endl ;
return true ;
}

View File

@ -0,0 +1,46 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "XmlResponse.hh"
#include "xml/Node.hh"
namespace gr { namespace http {
XmlResponse::XmlResponse()
{
}
std::size_t XmlResponse::OnData( void *data, std::size_t count )
{
m_tb.ParseData( reinterpret_cast<char*>(data), count ) ;
return count ;
}
void XmlResponse::Finish()
{
m_tb.ParseData( 0, 0, true ) ;
}
xml::Node XmlResponse::Response() const
{
return m_tb.Result() ;
}
} } // end of namespace

View File

@ -0,0 +1,47 @@
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2012 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#include "Receivable.hh"
#include "xml/TreeBuilder.hh"
namespace gr { namespace xml
{
class Node ;
} }
namespace gr { namespace http {
class XmlResponse : public Receivable
{
public :
XmlResponse() ;
std::size_t OnData( void *data, std::size_t count ) ;
void Finish() ;
xml::Node Response() const ;
private :
xml::TreeBuilder m_tb ;
} ;
} } // end of namespace

View File

@ -22,9 +22,12 @@
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <iterator>
#include <stdexcept>
// debugging
#include <iostream>
namespace gr { namespace xml {
class Node::Impl
@ -281,4 +284,23 @@ std::string Node::Value() const
return m_ptr->Value() ;
}
std::vector<Node> Node::Children() const
{
std::vector<Node> result ;
for ( Impl::iterator i = m_ptr->Begin() ; i != m_ptr->End() ; ++i )
result.push_back( Node( (*i)->AddRef() ) ) ;
return result ;
}
std::ostream& operator<<( std::ostream& os, const Node& node )
{
os << '<' << node.Name() << '>' ;
std::vector<Node> c = node.Children() ;
std::copy( c.begin(), c.end(), std::ostream_iterator<Node>(os, "\n") ) ;
return os ;
}
} } // end namespace

View File

@ -53,6 +53,8 @@ public :
Type GetType() const ;
static bool IsCompatible( Type parent, Type child ) ;
std::vector<Node> Children() const ;
private :
class Impl ;
@ -64,4 +66,6 @@ private :
Impl *m_ptr ;
} ;
std::ostream& operator<<( std::ostream& os, const Node& node ) ;
} } // end of namespace