}
CardDAV2::~CardDAV2(){
+
curl_easy_cleanup(ConnectionSession);
ConnectionSession = nullptr;
+
+ if (HeaderList != nullptr){
+ curl_slist_free_all(HeaderList);
+ HeaderList = nullptr;
+ }
+
}
#if defined(__APPLE__)
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;
ConnectResult = COCONNECT_OK;
AuthPassed = true;
ValidResponse = true;
- } else if (SessionResponseCode == 403){
+ } else if (SessionResponseCode == 401){
ConnectResult = COCONNECT_AUTHFAIL;
AuthPassed = false;
ValidResponse = true;
} else if (SessionResponseCode == 403){
AuthPassed = false;
ValidResponse = true;
- } else if (SessionResponseCode >= 200) {
+ } else if (SessionResponseCode >= 400) {
AuthPassed = false;
ValidResponse = true;
} else {
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;
}
COServerResponse CardDAV2::AddContact(std::string Location, std::string Data){
+ // 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, "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");
+
+ curl_easy_setopt(ConnectionSession, CURLOPT_HTTPHEADER, HeaderList);
+
+ 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 == 201 || 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::EditContact(std::string Location, std::string Data){
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);
if (DoAuthentication == true){
curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str());
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);
if (DoAuthentication == true){
curl_easy_setopt(ConnectionSession, CURLOPT_USERPWD, UsernamePassword.c_str());
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){
SessionErrorBuffer[0] = '\0';
PageData = "";
PageHeader = "";
+ if (HeaderList != nullptr){
+ curl_slist_free_all(HeaderList);
+ HeaderList = nullptr;
+ }
}
-vector<string> CardDAV2::GetDAVHeader(){
+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<string> CardDAV2::GetDAVHeader(){
+
+ // Go through each of the lines looking for the
+ // 'DAV:' section.
string HeaderName;
string HeaderValue;
- bool DAVFound = false;
bool FastForward = false;
vector<string> DAVHeaderList;