Apply the secure log in exception log
[framework/osp/net.git] / src / http / FNetHttp_HttpCookieStorageManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FNetHttp_HttpCookieStorageManagerImpl.cpp
20  * @brief               This is the implementation file for HttpCookieStorageManagerImpl class.
21  */
22
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>
30 #include <FIoFile.h>
31 #include <FNetHttpHttpCookieStorageManager.h>
32 #include <FBaseSysLog.h>
33 #include "FNetHttp_HttpCommon.h"
34 #include "FNetHttp_HttpSessionImpl.h"
35 #include "FNetHttp_HttpCookieStorageManagerImpl.h"
36
37 using namespace std;
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;
43
44 namespace Tizen { namespace Net { namespace Http
45 {
46 _HttpCookieStorageManagerImpl::_HttpCookieStorageManagerImpl()
47 {
48 }
49
50 _HttpCookieStorageManagerImpl::~_HttpCookieStorageManagerImpl()
51 {
52 }
53
54 HttpCookieStorageManager*
55 _HttpCookieStorageManagerImpl::CreateHttpCookieStorageManagerN(void)
56 {
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.");
59
60         return pHttpCookieStorageManager;
61 }
62
63 void
64 _HttpCookieStorageManagerImpl::DeleteHttpCookieStorageManager(HttpCookieStorageManager* pHttpCookieStorageManager)
65 {
66         delete pHttpCookieStorageManager;
67 }
68
69 result
70 _HttpCookieStorageManagerImpl::GetCookies(const Tizen::Base::String& url, Tizen::Base::String& cookies) const
71 {
72         result r = E_SUCCESS;
73
74         SysSecureTryReturnResult(NID_NET_HTTP, !url.IsEmpty(),
75                                            E_INVALID_ARG, "The input argument is empty(%ls).", url.GetPointer());
76
77         Mutex* pHttpMutex = null;
78         Uri uri;
79         String hostName;
80         String domainName;
81         int indexOfPrefix = -1;
82         bool hasProtocolScheme = false;
83
84         hasProtocolScheme = _HttpUtility::HasProtocolScheme(url);
85         if (!hasProtocolScheme)
86         {
87                 //Add the prefix as "http://"
88                 hostName = _HTTP_PROTOCOL_SCHEME + url;
89         }
90         else
91         {
92                 hostName = url;
93         }
94
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());
99
100         hostName = uri.GetHost();
101         SysSecureLog(NID_NET_HTTP, " The host name of cookie is %ls.", hostName.GetPointer());
102
103         r = hostName.IndexOf(L"www", 0, indexOfPrefix);
104         if (r == E_SUCCESS && indexOfPrefix >= 0)
105         {
106                 r = hostName.SubString(String(L"www").GetLength(), domainName);
107         }
108         else
109         {
110                 domainName = hostName;
111         }
112
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());
116
117         String filePath = _HttpUtility::GetCookieFilePath();
118         r = GetLastResult();
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.");
122
123         pHttpMutex = _HttpUtility::GetHttpMutex();
124
125         MutexGuard locked(*pHttpMutex);
126         SysTryReturnResult(NID_NET_HTTP, locked.IsLocked(), E_SYSTEM,
127                         "Failed to lock mutex.");
128
129         if (File::IsFileExist(filePath))
130         {
131                 File cookieFile;
132                 String cookieLine;
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());
137
138                 while (cookieFile.Read(cookieLine) == E_SUCCESS)
139                 {
140                         if (!cookieLine.StartsWith(_CURL_COOKIE_HTTP_ONLY_PREFIX, false))
141                         {
142                                 //Ignore the comment.
143                                 if (cookieLine.StartsWith(L"#", false) || cookieLine.Equals(L"\n", false))
144                                 {
145                                         continue;
146                                 }
147                         }
148
149                         SysSecureLog(NID_NET_HTTP, "The cookie line is %ls.", cookieLine.GetPointer());
150
151                         StringTokenizer headerToken(cookieLine, L"\t");
152                         int index = 0;
153
154                         while (headerToken.HasMoreTokens())
155                         {
156                                 String cookieField;
157                                 r = headerToken.GetNextToken(cookieField);
158                                 cookieField.Trim();
159                                 SysSecureLog(NID_NET_HTTP, "The cookie field is %ls.", cookieField.GetPointer());
160                                 if (r == E_SUCCESS && !cookieField.IsEmpty())
161                                 {
162                                         if (index == _CURL_COOKIE_DOMAIN_NAME_INDEX)
163                                         {
164                                                 int indexOf = -1;
165                                                 r = cookieField.IndexOf(domainName, 0, indexOf);
166                                                 if (r == E_SUCCESS && indexOf >= 0)
167                                                 {
168                                                         SysSecureLog(NID_NET_HTTP, "Found the cookie domain(%ls).", cookieField.GetPointer());
169                                                 }
170
171                                         }
172                                         else if (index == _CURL_COOKIE_COOKIE_NAME_INDEX)
173                                         {
174                                                 if (!cookies.IsEmpty())
175                                                 {
176                                                         cookies.Append(L";");
177                                                 }
178
179                                                 cookies.Append(cookieField);
180                                         }
181                                         else if (index == _CURL_COOKIE_COOKIE_VALUE_INDEX)
182                                         {
183                                                 cookies.Append(L"=");
184                                                 cookies.Append(cookieField);
185                                         }
186                                 }
187                                 index++;
188                         }
189                 }
190
191         }
192         else
193         {
194                 SysLog(NID_NET_HTTP, "The cookie file is not exist.");
195         }
196
197         SysSecureLog(NID_NET_HTTP, "The domain name is %ls, the cookies are %ls.", domainName.GetPointer(), cookies.GetPointer());
198
199         return r;
200 }
201
202 result
203 _HttpCookieStorageManagerImpl::RemoveCookies(const Tizen::Base::String& url)
204 {
205         result r = E_SUCCESS;
206
207         SysSecureTryReturnResult(NID_NET_HTTP, !url.IsEmpty(),
208                                            E_INVALID_ARG, "The input argument is empty(%ls).", url.GetPointer());
209
210         Mutex* pHttpMutex = null;
211         LinkedList cookieList;
212         String hostName;
213         String domainName;
214         int indexOfPrefix = -1;
215         bool hasProtocolScheme = false;
216
217         hasProtocolScheme = _HttpUtility::HasProtocolScheme(url);
218         if (!hasProtocolScheme)
219         {
220                 //Add the prefix as "http://"
221                 hostName = _HTTP_PROTOCOL_SCHEME + url;
222         }
223         else
224         {
225                 hostName = url;
226         }
227
228         Uri uri;
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());
233
234         hostName = uri.GetHost();
235         SysSecureLog(NID_NET_HTTP, "The host name of url is %ls.", hostName.GetPointer());
236
237         r = hostName.IndexOf(L"www", 0, indexOfPrefix);
238         if (r == E_SUCCESS && indexOfPrefix >= 0)
239         {
240                 r = hostName.SubString(String(L"www").GetLength(), domainName);
241         }
242         else
243         {
244                 domainName = hostName;
245         }
246
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());
250
251         String filePath = _HttpUtility::GetCookieFilePath();
252         r = GetLastResult();
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.");
256
257         pHttpMutex = _HttpUtility::GetHttpMutex();
258
259         MutexGuard locked(*pHttpMutex);
260         SysTryReturnResult(NID_NET_HTTP, locked.IsLocked(), E_SYSTEM,
261                         "Failed to lock mutex.");
262
263         if (File::IsFileExist(filePath))
264         {
265                 bool isMatchedCookies = false;
266                 String cookieLine;
267                 unique_ptr<File> pCookieFile(new (std::nothrow) File());
268                 SysTryReturnResult(NID_NET_HTTP, pCookieFile != null, E_OUT_OF_MEMORY,
269                                         "Memory allocation failed.");
270
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());
275
276                 while (pCookieFile->Read(cookieLine) == E_SUCCESS)
277                 {
278                         if (!cookieLine.StartsWith(_CURL_COOKIE_HTTP_ONLY_PREFIX, false))
279                         {
280                                 //Ignore the comment.
281                                 if (cookieLine.StartsWith(L"#", 0) || cookieLine.Equals(L"\n", false) == true)
282                                 {
283                                         String* pCookieLine = new (std::nothrow) String(cookieLine);
284                                         SysTryReturnResult(NID_NET_HTTP, pCookieLine != null,E_OUT_OF_MEMORY,
285                                                                 "Memory allocation failed.");
286
287                                         cookieList.Add(*pCookieLine);
288                                         continue;
289                                 }
290                         }
291
292                         SysSecureLog(NID_NET_HTTP, "The cookie line is %ls.", cookieLine.GetPointer());
293                         StringTokenizer headerToken(cookieLine, L"\t");
294                         int index = 0;
295
296                         while (headerToken.HasMoreTokens())
297                         {
298                                 String cookieField;
299                                 r = headerToken.GetNextToken(cookieField);
300                                 cookieField.Trim();
301                                 SysSecureLog(NID_NET_HTTP, "The cookie field is %ls.", cookieField.GetPointer());
302                                 if (r == E_SUCCESS && cookieField.IsEmpty() == false)
303                                 {
304                                         if (index == _CURL_COOKIE_DOMAIN_NAME_INDEX)
305                                         {
306                                                 int indexOf = -1;
307                                                 r = cookieField.IndexOf(domainName, 0, indexOf);
308                                                 if (r == E_SUCCESS && indexOf >= 0)
309                                                 {
310                                                         SysSecureLog(NID_NET_HTTP, "Found the cookie domain(%ls) to be removed.", cookieField.GetPointer());
311                                                         isMatchedCookies = true;
312                                                         break;
313                                                 }
314                                                 else
315                                                 {
316                                                         String* pCookieLine = new (std::nothrow) String(cookieLine);
317                                                         SysTryReturnResult(NID_NET_HTTP, pCookieLine != null, E_OUT_OF_MEMORY,
318                                                                                 "Memory allocation failed.");
319
320                                                         cookieList.Add(*pCookieLine);
321                                                         SysSecureLog(NID_NET_HTTP, "The cookie domain[%ls] is saved.", cookieField.GetPointer());
322                                                         break;
323                                                 }
324                                         }
325                                 }
326                                 index++;
327                         }
328                 }
329
330                 if (isMatchedCookies == true)
331                 {
332                         int countOfCookie = cookieList.GetCount();
333
334                         pCookieFile.reset(new (std::nothrow) File());
335                         SysTryReturnResult(NID_NET_HTTP, pCookieFile != null, E_OUT_OF_MEMORY,
336                                                 "Memory allocation failed.");
337
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());
342
343                         for (int i = 0; i < countOfCookie; i++)
344                         {
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());
349
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());
354                         }
355
356                         cookieList.RemoveAll(true);
357                 }
358                 else
359                 {
360                         SysSecureLog(NID_NET_HTTP, " Not found the Cookie domain(%ls).", domainName.GetPointer());
361                 }
362
363         }
364         else
365         {
366                 SysLog(NID_NET_HTTP, "The cookie file is not exist.");
367         }
368
369         SysSecureLog(NID_NET_HTTP, "The Cookie of Domain Name(%ls) is removed.", domainName.GetPointer());
370
371         return r;
372 }
373
374 result
375 _HttpCookieStorageManagerImpl::RemoveAllCookies()
376 {
377         result r = E_SUCCESS;
378         Mutex* pHttpMutex = null;
379
380         String filePath = _HttpUtility::GetCookieFilePath();
381         SysTryReturnResult(NID_NET_HTTP, !filePath.IsEmpty(),
382                                            E_INVALID_STATE, "The cookie path is empty.");
383
384         pHttpMutex = _HttpUtility::GetHttpMutex();
385
386         MutexGuard locked(*pHttpMutex);
387         SysTryReturnResult(NID_NET_HTTP, locked.IsLocked(), E_SYSTEM,
388                         "Failed to lock mutex.");
389
390         if (File::IsFileExist(filePath))
391         {
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());
396
397                 SysSecureLog(NID_NET_HTTP, "The cookie file(%ls) was removed.", filePath.GetPointer());
398         }
399
400         return r;
401 }
402
403 _HttpCookieStorageManagerImpl*
404 _HttpCookieStorageManagerImpl::GetInstance(HttpCookieStorageManager& httpCookieStorageManager)
405 {
406         return httpCookieStorageManager.__pHttpCookieStorageManagerImpl;
407 }
408
409 const _HttpCookieStorageManagerImpl*
410 _HttpCookieStorageManagerImpl::GetInstance(const HttpCookieStorageManager& httpCookieStorageManager)
411 {
412         return httpCookieStorageManager.__pHttpCookieStorageManagerImpl;
413 }
414
415 } } } // Tizen::Net::Http