bee7daf22f8b3541bc7ab5958e6b5e032e12b487
[platform/framework/native/net.git] / src / http / FNetHttpHttpSession.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                FNetHttpHttpSession.cpp
20  * @brief               This is the implementation file for HttpSession class.
21  *
22  * This file contains the implementation of HttpSession class.
23  */
24
25 #include <unique_ptr.h>
26 #include <FBaseResult.h>
27 #include <FBaseUtilStringUtil.h>
28 #include <FNetHttpHttpSession.h>
29 #include <FBaseSysLog.h>
30 #include <FSec_AccessController.h>
31 #include "FNetHttp_HttpSessionImpl.h"
32
33 using namespace std;
34 using namespace Tizen::Base;
35 using namespace Tizen::Base::Runtime;
36 using namespace Tizen::Base::Utility;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Security;
39 using namespace Tizen::Net;
40
41 namespace Tizen { namespace Net { namespace Http
42 {
43
44 HttpSession::HttpSession()
45         : __pHttpSessionImpl(null)
46 {
47 }
48
49 HttpSession::~HttpSession(void)
50 {
51         if (__pHttpSessionImpl != null)
52         {
53                 delete __pHttpSessionImpl;
54                 __pHttpSessionImpl = null;
55         }
56 }
57
58 result
59 HttpSession::Construct(NetHttpSessionMode sessionMode, const String* pProxyAddr, const String& hostAddr,
60                                            const HttpHeader* pCommonHeader,
61                                            NetHttpCookieFlag flag)
62 {
63         SysAssertf(__pHttpSessionImpl == null,
64                            "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
65
66         result r = E_SUCCESS;
67
68         unique_ptr<_HttpSessionImpl> pHttpSessionImpl(new (std::nothrow) _HttpSessionImpl(this));
69         SysTryReturnResult(NID_NET_HTTP, pHttpSessionImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
70
71         r = pHttpSessionImpl->Construct(sessionMode, pProxyAddr, hostAddr, pCommonHeader, flag);
72         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "Propagating.");
73
74         __pHttpSessionImpl = pHttpSessionImpl.release();
75
76         return r;
77 }
78
79 result
80 HttpSession::Construct(const Tizen::Net::NetConnection& netConnection, NetHttpSessionMode sessionMode, const String* pProxyAddr,
81                                            const String& hostAddr, const HttpHeader* pCommonHeader,
82                                            NetHttpCookieFlag flag)
83 {
84         SysAssertf(__pHttpSessionImpl == null,
85                            "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
86
87         result r = E_SUCCESS;
88
89         unique_ptr<_HttpSessionImpl> pHttpSessionImpl(new (std::nothrow) _HttpSessionImpl(this));
90         SysTryReturnResult(NID_NET_HTTP, pHttpSessionImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
91
92         r = pHttpSessionImpl->Construct(netConnection, sessionMode, pProxyAddr, hostAddr, pCommonHeader, flag);
93         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "Propagating.");
94
95         __pHttpSessionImpl = pHttpSessionImpl.release();
96
97         return r;
98 }
99
100 HttpCookieStorageManager*
101 HttpSession::GetCookieStorageManager(void) const
102 {
103         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
104
105         result r = E_SUCCESS;
106         HttpCookieStorageManager* pHttpCookieStorageManager = null;
107
108         pHttpCookieStorageManager = __pHttpSessionImpl->GetCookieStorageManager();
109         r = GetLastResult();
110         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, pHttpCookieStorageManager, r, "[%s] Propagating.", GetErrorMessage(r));
111
112         return pHttpCookieStorageManager;
113 }
114
115 result
116 HttpSession::SetAutoRedirectionEnabled(bool enable)
117 {
118         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
119
120         result r = E_SUCCESS;
121
122         r = __pHttpSessionImpl->SetAutoRedirectionEnabled(enable);
123         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "Propagating.");
124
125         return r;
126 }
127
128 bool
129 HttpSession::IsAutoRedirectionEnabled(void) const
130 {
131         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
132
133         result r = E_SUCCESS;
134
135         bool rs = false;
136
137         rs = __pHttpSessionImpl->IsAutoRedirectionEnabled();
138         r = GetLastResult();
139         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, rs, r, "[%s] Propagating.", GetErrorMessage(r));
140
141         return rs;
142 }
143
144 HttpTransaction*
145 HttpSession::OpenTransactionN()
146 {
147         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
148
149         result r = E_SUCCESS;
150         HttpTransaction* pHttpTransaction = null;
151
152         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
153         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
154         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, null, r, "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
155
156         pHttpTransaction = __pHttpSessionImpl->OpenTransactionN();
157         r = GetLastResult();
158         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, pHttpTransaction, r, "[%s] Propagating.", GetErrorMessage(r));
159
160         return pHttpTransaction;
161 }
162
163
164 HttpTransaction*
165 HttpSession::OpenTransactionN(const HttpAuthentication& auth)
166 {
167         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
168
169         result r = E_SUCCESS;
170         HttpTransaction* pHttpTransaction = null;
171
172         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
173         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
174         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, null, r, "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
175
176         pHttpTransaction = __pHttpSessionImpl->OpenTransactionN(auth);
177         r = GetLastResult();
178         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, pHttpTransaction, r, "[%s] Propagating.", GetErrorMessage(r));
179
180         return pHttpTransaction;
181 }
182
183 result
184 HttpSession::CloseAllTransactions(void)
185 {
186         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
187
188         result r = E_SUCCESS;
189
190         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
191         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
192         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "The application does not have the privilege to call this method.", GetErrorMessage(r));
193
194         r = __pHttpSessionImpl->CloseAllTransactions();
195         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "Propagating.");
196
197         return r;
198 }
199
200 result
201 HttpSession::CancelTransaction(HttpTransaction& httpTransaction)
202 {
203         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
204
205         result r = E_SUCCESS;
206
207         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
208         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
209         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "The application does not have the privilege to call this method.");
210
211         r = __pHttpSessionImpl->CancelTransaction(httpTransaction);
212         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "Propagating.");
213
214         return r;
215 }
216
217 result
218 HttpSession::CloseTransaction(HttpTransaction& httpTransaction)
219 {
220         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
221
222         result r = E_SUCCESS;
223
224         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
225         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
226         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "The application does not have the privilege to call this method.");
227
228         r = __pHttpSessionImpl->CloseTransaction(httpTransaction);
229         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r, "Propagating.");
230
231         return r;
232 }
233
234 int
235 HttpSession::GetActiveTransactionCount(void) const
236 {
237         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
238
239         result r = E_SUCCESS;
240         int activeTransactionCount = -1;
241
242         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
243         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
244         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, -1, r, "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
245
246         activeTransactionCount = __pHttpSessionImpl->GetActiveTransactionCount();
247         r = GetLastResult();
248         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, activeTransactionCount, r, "[%s] Propagating.", GetErrorMessage(r));
249
250         return activeTransactionCount;
251 }
252
253 int
254 HttpSession::GetMaxTransactionCount(void) const
255 {
256         SysAssertf(__pHttpSessionImpl != null, "Not yet constructed. Construct() should be called before use.");
257
258         result r = E_SUCCESS;
259         int maxTransactionCount = -1;
260
261         r = _AccessController::CheckUserPrivilege(_PRV_HTTP);
262         r = TransExceptionsExclusive(r, E_PRIVILEGE_DENIED, E_OUT_OF_MEMORY);
263         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, -1, r, "[%s] The application does not have the privilege to call this method.", GetErrorMessage(r));
264
265         maxTransactionCount = __pHttpSessionImpl->GetMaxTransactionCount();
266         r = GetLastResult();
267
268         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, maxTransactionCount, r, "[%s] Propagating.", GetErrorMessage(r));
269
270         return maxTransactionCount;
271 }
272
273 } } } // Tizen::Net::Http