From 271dd95b2494dd13b6064dd4aac41fb9fb560a66 Mon Sep 17 00:00:00 2001 From: Nestal Wan Date: Thu, 7 Jun 2012 18:26:57 +0800 Subject: [PATCH] escape the document header. fix HTTP 400 when uploading --- grive/src/main.cc | 5 +++- libgrive/src/drive/Drive.cc | 4 ++-- libgrive/src/drive/Resource.cc | 14 ++++++++---- libgrive/src/xml/String.cc | 42 ++++++++++++++++++++++++++++++++++ libgrive/src/xml/String.hh | 28 +++++++++++++++++++++++ 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 libgrive/src/xml/String.cc create mode 100644 libgrive/src/xml/String.hh diff --git a/grive/src/main.cc b/grive/src/main.cc index 5791df2..b3e8a4d 100644 --- a/grive/src/main.cc +++ b/grive/src/main.cc @@ -146,7 +146,6 @@ int Main( int argc, char **argv ) } catch ( Exception& e ) { - Trace( "cannot read config: %1%", boost::diagnostic_information(e) ) ; Log( "Please run grive with the \"-a\" option if this is the " "first time you're accessing your Google Drive!", @@ -177,4 +176,8 @@ int main( int argc, char **argv ) Log( "exception: %1%", boost::diagnostic_information(e), log::critical ) ; return -1 ; } + catch ( ... ) + { + return -1 ; + } } diff --git a/libgrive/src/drive/Drive.cc b/libgrive/src/drive/Drive.cc index 01af5e5..91483b4 100644 --- a/libgrive/src/drive/Drive.cc +++ b/libgrive/src/drive/Drive.cc @@ -69,8 +69,8 @@ Drive::Drive( OAuth2& auth, const Json& options ) : // get metadata http::XmlResponse xrsp ; - http::ResponseLog log( "meta-", ".xml", &xrsp ) ; - http.Get( "https://docs.google.com/feeds/metadata/default", &log, m_http_hdr ) ; +// http::ResponseLog log( "meta-", ".xml", &xrsp ) ; + http.Get( "https://docs.google.com/feeds/metadata/default", &xrsp, m_http_hdr ) ; Trace( "return %1%", xrsp.Response()["docs:largestChangestamp"] ) ; m_state.ChangeStamp( std::atoi(xrsp.Response()["docs:largestChangestamp"]["@value"].front().Value().c_str()) ) ; diff --git a/libgrive/src/drive/Resource.cc b/libgrive/src/drive/Resource.cc index c709427..89cde59 100644 --- a/libgrive/src/drive/Resource.cc +++ b/libgrive/src/drive/Resource.cc @@ -33,6 +33,7 @@ #include "util/StdioFile.hh" #include "xml/Node.hh" #include "xml/NodeSet.hh" +#include "xml/String.hh" #include @@ -327,20 +328,20 @@ void Resource::Sync( http::Agent *http, const http::Header& auth ) Log( "sync %1% parent deleted in local.", Path(), log::verbose ) ; else { - Log( "sync %1% deleted in local. deleting remote", Path(), log::verbose ) ; + Log( "sync %1% deleted in local. deleting remote", Path(), log::info ) ; DeleteRemote( http, auth ) ; } break ; case local_changed : - Log( "sync %1% changed in local. uploading", Path(), log::verbose ) ; + Log( "sync %1% changed in local. uploading", Path(), log::info ) ; if ( EditContent( http, auth ) ) m_state = sync ; break ; case remote_new : case remote_changed : - Log( "sync %1% changed in remote. downloading", Path(), log::verbose ) ; + Log( "sync %1% changed in remote. downloading", Path(), log::info ) ; Download( http, Path(), auth ) ; m_state = sync ; break ; @@ -350,7 +351,7 @@ void Resource::Sync( http::Agent *http, const http::Header& auth ) Log( "sync %1% parent deleted in remote.", Path(), log::verbose ) ; else { - Log( "sync %1% deleted in remote. deleting local", Path(), log::verbose ) ; + Log( "sync %1% deleted in remote. deleting local", Path(), log::info ) ; DeleteLocal() ; } break ; @@ -502,7 +503,10 @@ bool Resource::Upload( http::Agent* http, const std::string& link, const http::H hdr.Add( "If-Match: " + m_entry.ETag() ) ; hdr.Add( "Expect:" ) ; - std::string meta = (boost::format( xml_meta ) % m_entry.Kind() % Name()).str() ; + std::string meta = (boost::format( xml_meta ) + % m_entry.Kind() + % xml::Escape(Name()) + ).str() ; http::StringResponse str ; if ( post ) diff --git a/libgrive/src/xml/String.cc b/libgrive/src/xml/String.cc new file mode 100644 index 0000000..8c44edf --- /dev/null +++ b/libgrive/src/xml/String.cc @@ -0,0 +1,42 @@ +/* + 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 "String.hh" + +namespace gr { namespace xml { + +std::string Escape( const std::string& str ) +{ + std::string result ; + for ( std::string::const_iterator i = str.begin() ; i != str.end() ; ++i ) + { + switch ( *i ) + { + case '\"': result.append( """ ) ; break ; + case '\'': result.append( "'" ) ; break ; + case '<': result.append( "<" ) ; break ; + case '>': result.append( ">" ) ; break ; + case '&': result.append( "&" ) ; break ; + default: result.push_back( *i ) ; break ; + } + } + return result ; +} + +} } // end of namespace diff --git a/libgrive/src/xml/String.hh b/libgrive/src/xml/String.hh new file mode 100644 index 0000000..e41b3ab --- /dev/null +++ b/libgrive/src/xml/String.hh @@ -0,0 +1,28 @@ +/* + 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 + +namespace gr { namespace xml { + +std::string Escape( const std::string& str ) ; + +}} // end of namespace