2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FNetHttp_HttpCookieStorageManagerImpl.cpp
20 * @brief This is the implementation file for HttpCookieStorageManagerImpl class.
23 #include <unique_ptr.h>
24 #include <FBaseString.h>
25 #include <FBaseRtMutex.h>
26 #include <FBaseRtMutexGuard.h>
27 #include <FBaseUtil.h>
28 #include <FBaseColLinkedList.h>
29 #include <FBaseColArrayList.h>
31 #include <FNetHttpHttpCookieStorageManager.h>
32 #include <FBaseSysLog.h>
33 #include "FNetHttp_HttpCommon.h"
34 #include "FNetHttp_HttpSessionImpl.h"
35 #include "FNetHttp_HttpCookieStorageManagerImpl.h"
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Runtime;
40 using namespace Tizen::Base::Utility;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Io;
44 namespace Tizen { namespace Net { namespace Http
46 _HttpCookieStorageManagerImpl::_HttpCookieStorageManagerImpl()
50 _HttpCookieStorageManagerImpl::~_HttpCookieStorageManagerImpl()
54 HttpCookieStorageManager*
55 _HttpCookieStorageManagerImpl::CreateHttpCookieStorageManagerN(void)
57 HttpCookieStorageManager* pHttpCookieStorageManager = new (std::nothrow) HttpCookieStorageManager();
58 SysTryReturn(NID_NET_HTTP, pHttpCookieStorageManager != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
60 return pHttpCookieStorageManager;
64 _HttpCookieStorageManagerImpl::DeleteHttpCookieStorageManager(HttpCookieStorageManager* pHttpCookieStorageManager)
66 delete pHttpCookieStorageManager;
70 _HttpCookieStorageManagerImpl::GetCookies(const Tizen::Base::String& url, Tizen::Base::String& cookies) const
74 SysSecureTryReturnResult(NID_NET_HTTP, !url.IsEmpty(),
75 E_INVALID_ARG, "The input argument is empty(%ls).", url.GetPointer());
77 Mutex* pHttpMutex = null;
81 int indexOfPrefix = -1;
82 bool hasProtocolScheme = false;
84 hasProtocolScheme = _HttpUtility::HasProtocolScheme(url);
85 if (!hasProtocolScheme)
87 //Add the prefix as "http://"
88 hostName = _HTTP_PROTOCOL_SCHEME + url;
95 r = uri.SetUri(hostName);
96 r = TransExceptionsExclusive(r, E_INVALID_ARG, E_OUT_OF_MEMORY);
97 SysSecureTryReturnResult(NID_NET_HTTP, r == E_SUCCESS,
98 r, "Failed to check the url(%ls).", url.GetPointer());
100 hostName = uri.GetHost();
101 SysSecureLog(NID_NET_HTTP, " The host name of cookie is %ls.", hostName.GetPointer());
103 r = hostName.IndexOf(L"www", 0, indexOfPrefix);
104 if (r == E_SUCCESS && indexOfPrefix >= 0)
106 r = hostName.SubString(String(L"www").GetLength(), domainName);
110 domainName = hostName;
113 SysSecureLog(NID_NET_HTTP, "The domain name of cookie is %ls.", domainName.GetPointer());
114 SysSecureTryReturnResult(NID_NET_HTTP, !domainName.IsEmpty(),
115 E_INVALID_ARG, "The input argument is invalid(%ls).", url.GetPointer());
117 String filePath = _HttpUtility::GetCookieFilePath();
119 r = TransExceptionsExclusive(r, E_INVALID_STATE, E_OUT_OF_MEMORY);
120 SysTryReturnResult(NID_NET_HTTP, !filePath.IsEmpty(),
121 r, "The cookie path of system is empty.");
123 pHttpMutex = _HttpUtility::GetHttpMutex();
125 MutexGuard locked(*pHttpMutex);
126 SysTryReturnResult(NID_NET_HTTP, locked.IsLocked(), E_SYSTEM,
127 "Failed to lock mutex.");
129 if (File::IsFileExist(filePath))
133 r = cookieFile.Construct(filePath, L"r", false);
134 r = TransExceptionsExclusive(r, E_INVALID_STATE, E_OUT_OF_MEMORY);
135 SysSecureTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
136 "Failed to construct the cookie path(%ls).", filePath.GetPointer());
138 while (cookieFile.Read(cookieLine) == E_SUCCESS)
140 if (!cookieLine.StartsWith(_CURL_COOKIE_HTTP_ONLY_PREFIX, false))
142 //Ignore the comment.
143 if (cookieLine.StartsWith(L"#", false) || cookieLine.Equals(L"\n", false))
149 SysSecureLog(NID_NET_HTTP, "The cookie line is %ls.", cookieLine.GetPointer());
151 StringTokenizer headerToken(cookieLine, L"\t");
154 while (headerToken.HasMoreTokens())
157 r = headerToken.GetNextToken(cookieField);
159 SysSecureLog(NID_NET_HTTP, "The cookie field is %ls.", cookieField.GetPointer());
160 if (r == E_SUCCESS && !cookieField.IsEmpty())
162 if (index == _CURL_COOKIE_DOMAIN_NAME_INDEX)
165 r = cookieField.IndexOf(domainName, 0, indexOf);
166 if (r == E_SUCCESS && indexOf >= 0)
168 SysSecureLog(NID_NET_HTTP, "Found the cookie domain(%ls).", cookieField.GetPointer());
172 else if (index == _CURL_COOKIE_COOKIE_NAME_INDEX)
174 if (!cookies.IsEmpty())
176 cookies.Append(L";");
179 cookies.Append(cookieField);
181 else if (index == _CURL_COOKIE_COOKIE_VALUE_INDEX)
183 cookies.Append(L"=");
184 cookies.Append(cookieField);
194 SysLog(NID_NET_HTTP, "The cookie file is not exist.");
197 SysSecureLog(NID_NET_HTTP, "The domain name is %ls, the cookies are %ls.", domainName.GetPointer(), cookies.GetPointer());
203 _HttpCookieStorageManagerImpl::RemoveCookies(const Tizen::Base::String& url)
205 result r = E_SUCCESS;
207 SysSecureTryReturnResult(NID_NET_HTTP, !url.IsEmpty(),
208 E_INVALID_ARG, "The input argument is empty(%ls).", url.GetPointer());
210 Mutex* pHttpMutex = null;
211 LinkedList cookieList;
214 int indexOfPrefix = -1;
215 bool hasProtocolScheme = false;
217 hasProtocolScheme = _HttpUtility::HasProtocolScheme(url);
218 if (!hasProtocolScheme)
220 //Add the prefix as "http://"
221 hostName = _HTTP_PROTOCOL_SCHEME + url;
229 r = uri.SetUri(hostName);
230 r = TransExceptionsExclusive(r, E_INVALID_ARG, E_OUT_OF_MEMORY);
231 SysSecureTryReturnResult(NID_NET_HTTP, r == E_SUCCESS,
232 r, "Failed to check the url(%ls).", url.GetPointer());
234 hostName = uri.GetHost();
235 SysSecureLog(NID_NET_HTTP, "The host name of url is %ls.", hostName.GetPointer());
237 r = hostName.IndexOf(L"www", 0, indexOfPrefix);
238 if (r == E_SUCCESS && indexOfPrefix >= 0)
240 r = hostName.SubString(String(L"www").GetLength(), domainName);
244 domainName = hostName;
247 SysSecureLog(NID_NET_HTTP, " The domain name of url is %ls.", domainName.GetPointer());
248 SysSecureTryReturnResult(NID_NET_HTTP, !domainName.IsEmpty(),
249 E_INVALID_ARG, "The input argument is invalid(%ls).", url.GetPointer());
251 String filePath = _HttpUtility::GetCookieFilePath();
253 r = TransExceptionsExclusive(r, E_INVALID_STATE, E_OUT_OF_MEMORY);
254 SysTryReturnResult(NID_NET_HTTP, !filePath.IsEmpty(),
255 r, "The cookie path is empty.");
257 pHttpMutex = _HttpUtility::GetHttpMutex();
259 MutexGuard locked(*pHttpMutex);
260 SysTryReturnResult(NID_NET_HTTP, locked.IsLocked(), E_SYSTEM,
261 "Failed to lock mutex.");
263 if (File::IsFileExist(filePath))
265 bool isMatchedCookies = false;
267 unique_ptr<File> pCookieFile(new (std::nothrow) File());
268 SysTryReturnResult(NID_NET_HTTP, pCookieFile != null, E_OUT_OF_MEMORY,
269 "Memory allocation failed.");
271 r = pCookieFile->Construct(filePath, L"r", false);
272 r = TransExceptionsExclusive(r, E_INVALID_STATE, E_OUT_OF_MEMORY);
273 SysSecureTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
274 "Failed to construct the cookie path(%ls).", filePath.GetPointer());
276 while (pCookieFile->Read(cookieLine) == E_SUCCESS)
278 if (!cookieLine.StartsWith(_CURL_COOKIE_HTTP_ONLY_PREFIX, false))
280 //Ignore the comment.
281 if (cookieLine.StartsWith(L"#", 0) || cookieLine.Equals(L"\n", false) == true)
283 String* pCookieLine = new (std::nothrow) String(cookieLine);
284 SysTryReturnResult(NID_NET_HTTP, pCookieLine != null,E_OUT_OF_MEMORY,
285 "Memory allocation failed.");
287 cookieList.Add(*pCookieLine);
292 SysSecureLog(NID_NET_HTTP, "The cookie line is %ls.", cookieLine.GetPointer());
293 StringTokenizer headerToken(cookieLine, L"\t");
296 while (headerToken.HasMoreTokens())
299 r = headerToken.GetNextToken(cookieField);
301 SysSecureLog(NID_NET_HTTP, "The cookie field is %ls.", cookieField.GetPointer());
302 if (r == E_SUCCESS && cookieField.IsEmpty() == false)
304 if (index == _CURL_COOKIE_DOMAIN_NAME_INDEX)
307 r = cookieField.IndexOf(domainName, 0, indexOf);
308 if (r == E_SUCCESS && indexOf >= 0)
310 SysSecureLog(NID_NET_HTTP, "Found the cookie domain(%ls) to be removed.", cookieField.GetPointer());
311 isMatchedCookies = true;
316 String* pCookieLine = new (std::nothrow) String(cookieLine);
317 SysTryReturnResult(NID_NET_HTTP, pCookieLine != null, E_OUT_OF_MEMORY,
318 "Memory allocation failed.");
320 cookieList.Add(*pCookieLine);
321 SysSecureLog(NID_NET_HTTP, "The cookie domain[%ls] is saved.", cookieField.GetPointer());
330 if (isMatchedCookies == true)
332 int countOfCookie = cookieList.GetCount();
334 pCookieFile.reset(new (std::nothrow) File());
335 SysTryReturnResult(NID_NET_HTTP, pCookieFile != null, E_OUT_OF_MEMORY,
336 "Memory allocation failed.");
338 r = pCookieFile->Construct(filePath, L"w", true);
339 r = TransExceptionsExclusive(r, E_INVALID_STATE, E_OUT_OF_MEMORY);
340 SysSecureTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
341 "Failed to construct the cookie path(%ls).", filePath.GetPointer());
343 for (int i = 0; i < countOfCookie; i++)
345 String* pCookieLine = dynamic_cast< String* >(cookieList.GetAt(i));
346 SysTryReturnResult(NID_NET_HTTP, pCookieLine != null, E_SYSTEM,
347 "A system error has occurred.");
348 SysSecureLog(NID_NET_HTTP, "[%d] The cookie line is %ls.", i, pCookieLine->GetPointer());
350 r = pCookieFile->Write(*pCookieLine);
351 r = TransExceptionsExclusive(r, E_SYSTEM, E_OUT_OF_MEMORY);
352 SysSecureTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
353 "Failed to write the cookie file.(%ls).", filePath.GetPointer());
356 cookieList.RemoveAll(true);
360 SysSecureLog(NID_NET_HTTP, " Not found the Cookie domain(%ls).", domainName.GetPointer());
366 SysLog(NID_NET_HTTP, "The cookie file is not exist.");
369 SysSecureLog(NID_NET_HTTP, "The Cookie of Domain Name(%ls) is removed.", domainName.GetPointer());
375 _HttpCookieStorageManagerImpl::RemoveAllCookies()
377 result r = E_SUCCESS;
378 Mutex* pHttpMutex = null;
380 String filePath = _HttpUtility::GetCookieFilePath();
381 SysTryReturnResult(NID_NET_HTTP, !filePath.IsEmpty(),
382 E_INVALID_STATE, "The cookie path is empty.");
384 pHttpMutex = _HttpUtility::GetHttpMutex();
386 MutexGuard locked(*pHttpMutex);
387 SysTryReturnResult(NID_NET_HTTP, locked.IsLocked(), E_SYSTEM,
388 "Failed to lock mutex.");
390 if (File::IsFileExist(filePath))
392 r = File::Remove(filePath);
393 r = TransExceptionsExclusive(r, E_INVALID_STATE, E_OUT_OF_MEMORY);
394 SysSecureTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
395 "Failed to remove the cookie file[%ls].", filePath.GetPointer());
397 SysSecureLog(NID_NET_HTTP, "The cookie file(%ls) was removed.", filePath.GetPointer());
403 _HttpCookieStorageManagerImpl*
404 _HttpCookieStorageManagerImpl::GetInstance(HttpCookieStorageManager& httpCookieStorageManager)
406 return httpCookieStorageManager.__pHttpCookieStorageManagerImpl;
409 const _HttpCookieStorageManagerImpl*
410 _HttpCookieStorageManagerImpl::GetInstance(const HttpCookieStorageManager& httpCookieStorageManager)
412 return httpCookieStorageManager.__pHttpCookieStorageManagerImpl;
415 } } } // Tizen::Net::Http