X-Git-Url: http://Server1/repobrowser/?a=blobdiff_plain;f=source%2Fcarddav2%2Fcarddav2.cpp;h=1d8f632ba36561e661385b3b199b3cdb1c2fe855;hb=c86c371ad772bf27971a737cf7530b0723883464;hp=f5bc2a3eedee3ad4caef413004b80cb54a8c6ba3;hpb=daa33d641181d15710614cad752187a87263bcc9;p=xestiaab%2F.git diff --git a/source/carddav2/carddav2.cpp b/source/carddav2/carddav2.cpp index f5bc2a3..1d8f632 100644 --- a/source/carddav2/carddav2.cpp +++ b/source/carddav2/carddav2.cpp @@ -77,8 +77,15 @@ size_t CardDAV2::WritebackFuncImplementation(char *ptr, size_t size, size_t nmem } CardDAV2::~CardDAV2(){ + curl_easy_cleanup(ConnectionSession); ConnectionSession = nullptr; + + if (HeaderList != nullptr){ + curl_slist_free_all(HeaderList); + HeaderList = nullptr; + } + } #if defined(__APPLE__) @@ -172,11 +179,12 @@ COConnectResult CardDAV2::Connect(bool DoAuthentication){ if (TestMode == true){ SessionResult = curl_easy_perform(ConnectionSession); } else { - + SessionResult = curl_easy_perform(ConnectionSession); } switch(SessionResult){ case CURLE_OK: + case CURLE_HTTP_RETURNED_ERROR: SSLStatus = true; SSLVerified = COSSL_VERIFIED; ConnectResult = COCONNECT_OK; @@ -209,7 +217,7 @@ COConnectResult CardDAV2::Connect(bool DoAuthentication){ ConnectResult = COCONNECT_OK; AuthPassed = true; ValidResponse = true; - } else if (SessionResponseCode == 403){ + } else if (SessionResponseCode == 401){ ConnectResult = COCONNECT_AUTHFAIL; AuthPassed = false; ValidResponse = true; @@ -336,7 +344,7 @@ COServerResponse CardDAV2::GetDefaultPrefix(string *ServerPrefix){ } else if (SessionResponseCode == 403){ AuthPassed = false; ValidResponse = true; - } else if (SessionResponseCode >= 200) { + } else if (SessionResponseCode >= 400) { AuthPassed = false; ValidResponse = true; } else { @@ -393,19 +401,15 @@ COServerResponse CardDAV2::GetDefaultPrefix(string *ServerPrefix){ curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode); if (SessionResponseCode == 200 || SessionResponseCode == 207){ - //ConnectResult = COCONNECT_OK; AuthPassed = true; ValidResponse = true; } else if (SessionResponseCode == 403){ - //ConnectResult = COCONNECT_AUTHFAIL; AuthPassed = false; ValidResponse = true; - } else if (SessionResponseCode >= 200) { - //ConnectResult = COCONNECT_INVALID; + } else if (SessionResponseCode >= 400) { AuthPassed = false; ValidResponse = true; } else { - //ConnectResult = COCONNECT_INVALID; AuthPassed = false; ValidResponse = false; } @@ -472,7 +476,7 @@ COServerResponse CardDAV2::GetDefaultPrefix(string *ServerPrefix){ ValidResponse = false; } - if (ValidResponse == false && AuthPassed == false){ + if (ValidResponse == false || AuthPassed == false){ ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; ServerResponse.EntityTag = ""; ServerResponse.SessionCode = SessionResult; @@ -741,163 +745,812 @@ std::string CardDAV2::GetDefaultAddressBookURI(){ COServerResponse CardDAV2::AddContact(std::string Location, std::string Data){ -} - -COServerResponse CardDAV2::EditContact(std::string Location, std::string Data){ - -} + // Check if authentication was successful, otherwise don't do anything. -COServerResponse CardDAV2::DeleteContact(std::string Location, std::string EntityTag){ + COServerResponse ServerResponse; -} + if (AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = 0; + ServerResponse.ResultCode = 0; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } -COServerResponse CardDAV2::GetServerEntityTagValue(std::string Location){ + ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true); + ResetResults(); -} - -COServerResponse CardDAV2::GetContact(std::string Location){ + string ServerAddressURL = BuildURL(ServerPrefix + Location); + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, Data.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(Data.c_str())); -} + HeaderList = curl_slist_append(HeaderList, "Content-Type: text/vcard; charset=utf-8"); -COContactList CardDAV2::GetContactList(std::string SyncToken){ + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, HeaderList); -} + if (TestMode == true){ + SessionResult = curl_easy_perform(ConnectionSession); + } else { + SessionResult = curl_easy_perform(ConnectionSession); + } -bool CardDAV2::CanDoProcessing(){ - return CanProcess; -} - -bool CardDAV2::CanDoSSL(){ - return SSLStatus; -} - -COSSLVerified CardDAV2::SSLVerify(){ - return SSLVerified; -} - -bool CardDAV2::AbleToLogin(){ - return AuthPassed; -} - -bool CardDAV2::HasValidResponse(){ - return ValidResponse; -} - -bool CardDAV2::IsSelfSigned(){ - return SSLSelfSigned; -} - -void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){ + switch(SessionResult){ + case CURLE_OK: + SSLStatus = true; + SSLVerified = COSSL_VERIFIED; + break; + case CURLE_SSL_CACERT: + case CURLE_SSL_CONNECT_ERROR: + SSLStatus = true; + SSLVerified = COSSL_UNABLETOVERIFY; + break; + default: + break; + }; - std::string ServerAddress = ""; - - string ServerAddressURL = "http://" + ServerAddress + ":" + to_string(ServerPort) + "/"; - string UsernamePassword = ServerUser + ":" + ServerPass; + long SessionResponseCode = 0; - curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddress.c_str()); - curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); - curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); - curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageData); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeader); - curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode); - if (DoAuthentication == true){ - curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); + if (SessionResponseCode == 200 || SessionResponseCode == 201 || SessionResponseCode == 204){ + AuthPassed = true; + ValidResponse = true; + } else if (SessionResponseCode == 403){ + AuthPassed = false; + ValidResponse = true; + } else if (SessionResponseCode >= 400){ + AuthPassed = false; + ValidResponse = true; } else { - curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, ":"); + AuthPassed = false; + ValidResponse = false; + } + + if (ValidResponse == false || AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = ""; + return ServerResponse; } + CanProcess = true; + + ServerResponse.RequestResult = COREQUEST_OK; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = SessionErrorBuffer; + return ServerResponse; + } -void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ - - // Setup the default parameters. +COServerResponse CardDAV2::EditContact(std::string Location, std::string Data){ + + // Check if authentication was successful, otherwise don't do anything. + + COServerResponse ServerResponse; - string ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + "/"; - string UsernamePassword = ServerUser + ":" + ServerPass; + if (AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = 0; + ServerResponse.ResultCode = 0; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } + + ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true); + ResetResults(); + string ServerAddressURL = BuildURL(ServerPrefix + Location); curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); - curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); - curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); - curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageData); - curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeader); - curl_easy_setopt(ConnectionSession, CURLOPT_ERRORBUFFER, SessionErrorBuffer); - curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); - curl_easy_setopt(ConnectionSession, CURLOPT_CERTINFO, 1); - curl_easy_setopt(ConnectionSession, CURLOPT_VERBOSE, 1); - curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, Data.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(Data.c_str())); - if (DoAuthentication == true){ - curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); - } else { - curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, ":"); - } + HeaderList = curl_slist_append(HeaderList, "Content-Type: text/vcard; charset=utf-8"); + + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, HeaderList); - if (EnableSSLBypass == true){ - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 0); - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 0); + if (TestMode == true){ + SessionResult = curl_easy_perform(ConnectionSession); } else { - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 2); - curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 1); + SessionResult = curl_easy_perform(ConnectionSession); } -} - -string CardDAV2::BuildURL(string URI){ + switch(SessionResult){ + case CURLE_OK: + SSLStatus = true; + SSLVerified = COSSL_VERIFIED; + break; + case CURLE_SSL_CACERT: + case CURLE_SSL_CONNECT_ERROR: + SSLStatus = true; + SSLVerified = COSSL_UNABLETOVERIFY; + break; + default: + break; + }; - string ServerAddressURL; + long SessionResponseCode = 0; - if (SSLStatus == true){ - ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + URI; + curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode); + + if (SessionResponseCode == 200 || SessionResponseCode == 201 || SessionResponseCode == 204){ + AuthPassed = true; + ValidResponse = true; + } else if (SessionResponseCode == 403){ + AuthPassed = false; + ValidResponse = true; + } else if (SessionResponseCode >= 400){ + AuthPassed = false; + ValidResponse = true; } else { - ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + URI; + AuthPassed = false; + ValidResponse = false; } - return ServerAddressURL; + if (ValidResponse == false || AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } -} - -string CardDAV2::GetErrorMessage(){ + CanProcess = true; - ErrorMessage = SessionErrorBuffer; - return ErrorMessage; + ServerResponse.RequestResult = COREQUEST_OK; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = SessionErrorBuffer; + return ServerResponse; } -void CardDAV2::ResetResults(){ +COServerResponse CardDAV2::DeleteContact(std::string Location){ - SSLStatus = false; - COSSLVerified SSLVerified = COSSL_NORESULT; - ValidResponse = false; - AuthPassed = false; - CanProcess = false; - SSLSelfSigned = false; - TaskCompleted = false; - ErrorMessage = ""; - SessionErrorBuffer[0] = '\0'; - PageData = ""; - PageHeader = ""; + // Check if authentication was successful, otherwise don't do anything. + + COServerResponse ServerResponse; -} + if (AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = 0; + ServerResponse.ResultCode = 0; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } -vector CardDAV2::GetDAVHeader(){ + ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true); + ResetResults(); - // Go through each of the lines looking for the - // 'DAV:' section. + string ServerAddressURL = BuildURL(ServerPrefix + Location); + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "DELETE"); + if (TestMode == true){ + SessionResult = curl_easy_perform(ConnectionSession); + } else { + SessionResult = curl_easy_perform(ConnectionSession); + } + + switch(SessionResult){ + case CURLE_OK: + SSLStatus = true; + SSLVerified = COSSL_VERIFIED; + break; + case CURLE_SSL_CACERT: + case CURLE_SSL_CONNECT_ERROR: + SSLStatus = true; + SSLVerified = COSSL_UNABLETOVERIFY; + break; + default: + break; + }; + + long SessionResponseCode = 0; + + curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode); + + if (SessionResponseCode == 200 || SessionResponseCode == 202 || SessionResponseCode == 204){ + AuthPassed = true; + ValidResponse = true; + } else if (SessionResponseCode == 403){ + AuthPassed = false; + ValidResponse = true; + } else if (SessionResponseCode >= 400){ + AuthPassed = false; + ValidResponse = true; + } else { + AuthPassed = false; + ValidResponse = false; + } + + if (ValidResponse == false || AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } + + CanProcess = true; + + ServerResponse.RequestResult = COREQUEST_OK; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = SessionErrorBuffer; + return ServerResponse; + +} + +COServerResponse CardDAV2::GetServerEntityTagValue(std::string Location){ + + // Check if authentication was successful, otherwise don't do anything. + + COServerResponse ServerResponse; + + if (AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = 0; + ServerResponse.ResultCode = 0; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } + + ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true); + ResetResults(); + + static const char* GetETagQuery = + "" + "" + "" + "" + "" + ""; + + string ServerAddressURL = BuildURL(ServerPrefix + Location); + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "REPORT"); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, GetETagQuery); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, strlen(GetETagQuery)); + + if (TestMode == true){ + SessionResult = curl_easy_perform(ConnectionSession); + } else { + SessionResult = curl_easy_perform(ConnectionSession); + } + + switch(SessionResult){ + case CURLE_OK: + SSLStatus = true; + SSLVerified = COSSL_VERIFIED; + break; + case CURLE_SSL_CACERT: + case CURLE_SSL_CONNECT_ERROR: + SSLStatus = true; + SSLVerified = COSSL_UNABLETOVERIFY; + break; + default: + break; + }; + + long SessionResponseCode = 0; + + curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode); + + if (SessionResponseCode == 207){ + AuthPassed = true; + ValidResponse = true; + } else if (SessionResponseCode == 403){ + AuthPassed = false; + ValidResponse = true; + } else if (SessionResponseCode >= 400){ + AuthPassed = false; + ValidResponse = true; + } else { + AuthPassed = false; + ValidResponse = false; + } + + if (ValidResponse == false || AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } + + CanProcess = true; + + ServerResponse.RequestResult = COREQUEST_OK; + ServerResponse.EntityTag = GetETagValue(); + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = SessionErrorBuffer; + + return ServerResponse; + +} + +COServerResponse CardDAV2::GetContact(std::string Location, std::string *ContactData){ + + // Check if authentication was successful, otherwise don't do anything. + + COServerResponse ServerResponse; + + if (AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_NOTCONNECTED; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = 0; + ServerResponse.ResultCode = 0; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } + + ServerSSL ? SetupDefaultParametersSSL(true) : SetupDefaultParametersNonSSL(true); + ResetResults(); + + string ServerAddressURL = BuildURL(ServerPrefix + Location); + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); + + if (TestMode == true){ + SessionResult = curl_easy_perform(ConnectionSession); + } else { + SessionResult = curl_easy_perform(ConnectionSession); + } + + switch(SessionResult){ + case CURLE_OK: + SSLStatus = true; + SSLVerified = COSSL_VERIFIED; + break; + case CURLE_SSL_CACERT: + case CURLE_SSL_CONNECT_ERROR: + SSLStatus = true; + SSLVerified = COSSL_UNABLETOVERIFY; + break; + default: + break; + }; + + long SessionResponseCode = 0; + + curl_easy_getinfo(ConnectionSession, CURLINFO_RESPONSE_CODE, &SessionResponseCode); + + if (SessionResponseCode == 200){ + AuthPassed = true; + ValidResponse = true; + } else if (SessionResponseCode == 403){ + AuthPassed = false; + ValidResponse = true; + } else if (SessionResponseCode >= 400){ + AuthPassed = false; + ValidResponse = true; + } else { + AuthPassed = false; + ValidResponse = false; + } + + if (ValidResponse == false && AuthPassed == false){ + ServerResponse.RequestResult = COREQUEST_ERROR_SERVER; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = ""; + return ServerResponse; + } + + CanProcess = true; + + ServerResponse.RequestResult = COREQUEST_OK; + ServerResponse.EntityTag = ""; + ServerResponse.SessionCode = SessionResult; + ServerResponse.ResultCode = SessionResponseCode; + ServerResponse.ResultMessage = SessionErrorBuffer; + + (*ContactData) = PageData; + + return ServerResponse; + +} + +COContactList CardDAV2::GetContactList(std::string SyncToken){ + +} + +bool CardDAV2::CanDoProcessing(){ + return CanProcess; +} + +bool CardDAV2::CanDoSSL(){ + return SSLStatus; +} + +COSSLVerified CardDAV2::SSLVerify(){ + return SSLVerified; +} + +bool CardDAV2::AbleToLogin(){ + return AuthPassed; +} + +bool CardDAV2::HasValidResponse(){ + return ValidResponse; +} + +bool CardDAV2::IsSelfSigned(){ + return SSLSelfSigned; +} + +void CardDAV2::SetupDefaultParametersNonSSL(bool DoAuthentication){ + + std::string ServerAddress = ""; + + string ServerAddressURL = "http://" + ServerAddress + ":" + to_string(ServerPort) + "/"; + string UsernamePassword = ServerUser + ":" + ServerPass; + + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddress.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); + curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); + curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageData); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeader); + curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L); + + if (DoAuthentication == true){ + curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); + } else { + curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, ":"); + } + +} + +void CardDAV2::SetupDefaultParametersSSL(bool DoAuthentication){ + + // Setup the default parameters. + + string ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + "/"; + string UsernamePassword = ServerUser + ":" + ServerPass; + + curl_easy_setopt(ConnectionSession, CURLOPT_URL, ServerAddressURL.c_str()); + curl_easy_setopt(ConnectionSession, CURLOPT_NOPROGRESS, 1L); + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + curl_easy_setopt(ConnectionSession, CURLOPT_TIMEOUT, 60); + curl_easy_setopt(ConnectionSession, CURLOPT_FAILONERROR, true); + curl_easy_setopt(ConnectionSession, CURLOPT_USERAGENT, XSDAB_USERAGENT); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEFUNCTION, CardDAV2::WritebackFunc); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEDATA, &PageData); + curl_easy_setopt(ConnectionSession, CURLOPT_WRITEHEADER, &PageHeader); + curl_easy_setopt(ConnectionSession, CURLOPT_ERRORBUFFER, SessionErrorBuffer); + curl_easy_setopt(ConnectionSession, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(ConnectionSession, CURLOPT_CERTINFO, 1); + curl_easy_setopt(ConnectionSession, CURLOPT_VERBOSE, 1); + curl_easy_setopt(ConnectionSession, CURLOPT_CUSTOMREQUEST, "GET"); + curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDS, nullptr); + curl_easy_setopt(ConnectionSession, CURLOPT_POSTFIELDSIZE, 0L); + + if (DoAuthentication == true){ + curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str()); + } else { + curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, ":"); + } + + if (EnableSSLBypass == true){ + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 0); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 0); + } else { + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYHOST, 2); + curl_easy_setopt(ConnectionSession, CURLOPT_SSL_VERIFYPEER, 1); + } + + if (TestMode == false && ServerAccount.size() > 0){ + + // Check if the server certificate file exists. + + string CertificateFilename = GetAccountDir(ServerAccount, true); + + if (wxFile::Exists(CertificateFilename)){ + + curl_easy_setopt(ConnectionSession, CURLOPT_CAINFO, CertificateFilename.c_str()); + + } + + } + +} + +string CardDAV2::BuildURL(string URI){ + + string ServerAddressURL; + + if (SSLStatus == true){ + ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + URI; + } else { + ServerAddressURL = "https://" + ServerAddress + ":" + to_string(ServerPort) + URI; + } + + return ServerAddressURL; + +} + +string CardDAV2::GetErrorMessage(){ + + ErrorMessage = SessionErrorBuffer; + return ErrorMessage; + +} + +void CardDAV2::ResetResults(){ + + SSLStatus = false; + COSSLVerified SSLVerified = COSSL_NORESULT; + ValidResponse = false; + AuthPassed = false; + CanProcess = false; + SSLSelfSigned = false; + TaskCompleted = false; + ErrorMessage = ""; + SessionErrorBuffer[0] = '\0'; + PageData = ""; + PageHeader = ""; + if (HeaderList != nullptr){ + curl_slist_free_all(HeaderList); + HeaderList = nullptr; + } + +} + +string CardDAV2::GetETagValue(){ + + xmlDocPtr xmlCardDAVDoc; + + xmlCardDAVDoc = xmlReadMemory(PageData.c_str(), (int)PageData.size(), "noname.xml", NULL, 0); + + xmlNodePtr nodeLevel1; + xmlNodePtr nodeLevel2; + xmlNodePtr nodeLevel3; + xmlNodePtr nodeLevel4; + xmlNodePtr nodeLevel5; + xmlNodePtr nodeLevel6; + + //std::map xmlDataMap; + + std::string DataFilename; + std::string ETagData; + + std::string xmlStringSafe; + std::string ETagValue; + + // Tranverse through the catacombs of the response to get our ETag for the file. + + for (nodeLevel1 = xmlCardDAVDoc->children; + nodeLevel1 != NULL; + nodeLevel1 = nodeLevel1->next) + { + + bool HREFFound = FALSE; + bool ETagFound = FALSE; + + for (nodeLevel2 = nodeLevel1->children; + nodeLevel2 != NULL; + nodeLevel2 = nodeLevel2->next) + { + + for (nodeLevel3 = nodeLevel2->children; + nodeLevel3 != NULL; + nodeLevel3 = nodeLevel3->next) + { + + if (!xmlStrcmp(nodeLevel3->name, (const xmlChar *)"href") || + !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"d:href") || + !xmlStrcmp(nodeLevel3->name, (const xmlChar *)"D:href") + ){ + + // Get the filename. + + for (nodeLevel4 = nodeLevel3->children; + nodeLevel4 != NULL; + nodeLevel4 = nodeLevel4->next) + { + + if (!xmlStrcmp(nodeLevel4->name, (const xmlChar *)"text") || + !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"d:text") || + !xmlStrcmp(nodeLevel4->name, (const xmlChar *)"D:text") + ){ + + DataFilename = wxString::FromUTF8((const char*)nodeLevel4->content); + wxStringTokenizer wSTDFilename(DataFilename, wxT("/")); + + while (wSTDFilename.HasMoreTokens()){ + + DataFilename = wSTDFilename.GetNextToken().ToStdString(); + + } + + HREFFound = TRUE; + + } + + + + } + + } else { + + for (nodeLevel4 = nodeLevel3->children; + nodeLevel4 != NULL; + nodeLevel4 = nodeLevel4->next) + { + + for (nodeLevel5 = nodeLevel4->children; + nodeLevel5 != NULL; + nodeLevel5 = nodeLevel5->next) + { + + if (!xmlStrcmp(nodeLevel5->name, (const xmlChar *)"getetag") || + !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"d:getetag") || + !xmlStrcmp(nodeLevel5->name, (const xmlChar *)"D:getetag") + ){ + + for (nodeLevel6 = nodeLevel5->children; + nodeLevel6 != NULL; + nodeLevel6 = nodeLevel6->next) + { + + // Strip the quotes from the ETag. + + ETagData = (const char*)nodeLevel6->content; + if (ETagData[0] == '"' && ETagData[(ETagData.size() - 1)] == '"'){ + + ETagData.erase(0, 1); + ETagData.erase((ETagData.size() - 1)); + + } + + ETagFound = TRUE; + + } + + } + + } + + } + + } + + } + + } + + if (HREFFound == TRUE && ETagFound == TRUE){ + + // Add to the map data. + + ETagValue = ETagData; + + HREFFound = FALSE; + ETagFound = FALSE; + break; + + } + + + } + + xmlFreeDoc(xmlCardDAVDoc); + + return ETagValue; + +} + +string CardDAV2::GetETagHeader(){ + + // Go through each of the lines looking for the + // 'DAV:' section. + + string HeaderName; + string HeaderValue; + bool FastForward = false; + + for (int HeaderSeek = 0; HeaderSeek < PageHeader.size(); HeaderSeek++){ + + if (FastForward == true){ + + if (PageHeader[HeaderSeek] == '\n'){ + FastForward = false; + } + + continue; + + } + + try { + PageHeader.substr(HeaderSeek, 5) == "ETag:"; + } + + catch (const out_of_range &oor){ + break; + } + + if (PageHeader.substr(HeaderSeek, 5) == "ETag:"){ + + int CharacterSeek = 5; + + while ((HeaderSeek + CharacterSeek) < PageHeader.size()){ + + if (PageHeader.substr((HeaderSeek + CharacterSeek), 2) == "\r\n"){ + break; + } + + HeaderValue += PageHeader.substr((HeaderSeek + CharacterSeek), 1); + CharacterSeek++; + } + + break; + + } else { + + FastForward = true; + continue; + + } + + if (PageHeader[HeaderSeek] == '\n'){ + HeaderName = ""; + } + + //HeaderName += PageHeader.substr(HeaderSeek, 1); + + } + + // Check for quotation marks at the start and end and strip + // them out. + + if (HeaderValue.size() >= 2){ + + if (HeaderValue[0] == '"'){ + HeaderValue.erase((HeaderValue.size() - 1)); + HeaderValue.erase(0); + } + + } + + return HeaderValue; + +} + +vector CardDAV2::GetDAVHeader(){ + + // Go through each of the lines looking for the + // 'DAV:' section. string HeaderName; string HeaderValue; - bool DAVFound = false; bool FastForward = false; vector DAVHeaderList;