2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
25 #include "DAVCommon.h"
27 #include "utils/log.h"
28 #include "DllLibCurl.h"
29 #include "utils/XBMCTinyXML.h"
30 #include "utils/RegExp.h"
32 using namespace XFILE;
33 using namespace XCURL;
35 CDAVFile::CDAVFile(void)
41 CDAVFile::~CDAVFile(void)
45 bool CDAVFile::Execute(const CURL& url)
48 ParseAndCorrectUrl(url2);
50 CLog::Log(LOGDEBUG, "CDAVFile::Execute(%p) %s", (void*)this, m_url.c_str());
52 ASSERT(!(!m_state->m_easyHandle ^ !m_state->m_multiHandle));
53 if( m_state->m_easyHandle == NULL )
54 g_curlInterface.easy_aquire(url2.GetProtocol(), url2.GetHostName(), &m_state->m_easyHandle, &m_state->m_multiHandle );
56 // setup common curl options
57 SetCommonOptions(m_state);
58 SetRequestHeaders(m_state);
60 lastResponseCode = m_state->Connect(m_bufferSize);
61 if( lastResponseCode < 0 || lastResponseCode >= 400)
65 if (CURLE_OK == g_curlInterface.easy_getinfo(m_state->m_easyHandle, CURLINFO_EFFECTIVE_URL,&efurl) && efurl)
68 if (lastResponseCode == 207)
70 CStdString strResponse;
71 ReadData(strResponse);
73 CXBMCTinyXML davResponse;
74 davResponse.Parse(strResponse);
76 if (!davResponse.Parse(strResponse))
78 CLog::Log(LOGERROR, "%s - Unable to process dav response (%s)", __FUNCTION__, m_url.c_str());
84 // Iterate over all responses
85 for (pChild = davResponse.RootElement()->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
87 if (CDAVCommon::ValueWithoutNamespace(pChild, "response"))
89 CStdString sRetCode = CDAVCommon::GetStatusTag(pChild->ToElement());
91 rxCode.RegComp("HTTP/1\\.1\\s(\\d+)\\s.*");
92 if (rxCode.RegFind(sRetCode) >= 0)
94 if (rxCode.GetSubCount())
96 lastResponseCode = atoi(rxCode.GetMatch(1).c_str());
97 if( lastResponseCode < 0 || lastResponseCode >= 400)
109 bool CDAVFile::Delete(const CURL& url)
115 CStdString strRequest = "DELETE";
117 dav.SetCustomRequest(strRequest);
119 if (!dav.Execute(url))
121 CLog::Log(LOGERROR, "%s - Unable to delete dav resource (%s)", __FUNCTION__, url.Get().c_str());
130 bool CDAVFile::Rename(const CURL& url, const CURL& urlnew)
138 CStdString strProtocol = url2.GetTranslatedProtocol();
139 url2.SetProtocol(strProtocol);
141 CStdString strRequest = "MOVE";
142 dav.SetCustomRequest(strRequest);
143 dav.SetRequestHeader("Destination", url2.GetWithoutUserDetails());
145 if (!dav.Execute(url))
147 CLog::Log(LOGERROR, "%s - Unable to rename dav resource (%s)", __FUNCTION__, url.Get().c_str());