Initialize Tizen 2.3
[framework/osp/net.git] / src / http / FNetHttp_HttpRequestImpl.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_HttpRequestImpl.cpp
20  * @brief               This is the implementation file for _HttpRequestImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <FBaseString.h>
25 #include <FBaseByteBuffer.h>
26 #include <FNetNetTypes.h>
27 #include <FNetHttpHttpHeader.h>
28 #include <FNetHttpHttpRequest.h>
29 #include <FBaseColIEnumerator.h>
30 #include <FBaseSysLog.h>
31 #include "FNetHttp_HttpCommon.h"
32 #include "FNetHttp_HttpRequestImpl.h"
33 #include "FNetHttp_HttpSessionImpl.h"
34 #include "FNetHttp_HttpTransactionImpl.h"
35 #include "FNetHttp_HttpHeaderImpl.h"
36
37 using namespace std;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Base::Utility;
41
42 namespace Tizen { namespace Net { namespace Http
43 {
44
45 _HttpRequestImpl::_HttpRequestImpl(HttpRequest* pRequest)
46         : __method(NET_HTTP_METHOD_GET)
47         , __httpVersion(HTTP_VERSION_1_1)
48         , __cookieFlag(NET_HTTP_COOKIE_FLAG_ALWAYS_MANUAL)
49         , __recommendedSize(0)
50         , __isFirstChunk(false)
51         , __isLastChunk(false)
52         , __isReceivedTransactionReadyToWriteEvent(false)
53         , __pIHttpEntity(null)
54         , __pSendingBuffer(null)
55         , __pHttpTransactionImpl(null)
56         , __pHttpRequest(pRequest)
57 {
58 }
59
60 _HttpRequestImpl::~_HttpRequestImpl(void)
61 {
62         __pIHttpEntity = null;
63         __pSendingBuffer = null;
64         __pHttpTransactionImpl = null;
65         __pHttpRequest = null;
66 }
67
68 result
69 _HttpRequestImpl::Construct(const _HttpTransactionImpl& httpTransactionImpl, const HttpHeader* pCommonHeader)
70 {
71         result r = E_SUCCESS;
72
73         __pHttpTransactionImpl = const_cast< _HttpTransactionImpl* >(&httpTransactionImpl);
74
75         unique_ptr<HttpHeader> pHeader(new (std::nothrow) HttpHeader(pCommonHeader));
76         r = GetLastResult();
77         SysTryReturnResult(NID_NET_HTTP, pHeader != null, r,
78                                  "Propagating.");
79
80         // Queue construction
81         r = __pHttpRequest->_bodyQueue.Construct();
82         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, E_OUT_OF_MEMORY,
83                                            "Memory allocation failed.");
84
85         _HttpSessionImpl* pHttpSessionImpl = httpTransactionImpl.GetHttpSessioinImpl();
86         SysTryReturnResult(NID_NET_HTTP, pHttpSessionImpl != null,
87                                            E_SYSTEM, "HttpSession must not be null.");
88
89         __cookieFlag = pHttpSessionImpl->GetHttpCookieFlag();
90         __pHttpRequest->_pHeader = pHeader.release();
91
92         // Total Length of the body initialization
93         __pHttpRequest->_totalLen = 0;
94
95         return r;
96 }
97
98 HttpRequest*
99 _HttpRequestImpl::CreateHttpRequestN(void)
100 {
101         return new (std::nothrow) HttpRequest();
102 }
103
104 void
105 _HttpRequestImpl::DeleteHttpRequest(HttpRequest* pHttpRequest)
106 {
107         delete pHttpRequest;
108 }
109
110 bool
111 _HttpRequestImpl::IsEmptyBody(void) const
112 {
113         int countOfCurrentBuffer = __pHttpRequest->_bodyQueue.GetCount();
114         if (countOfCurrentBuffer == 0)
115         {
116                 return true;
117         }
118         else
119         {
120                 return false;
121         }
122 }
123
124 unsigned int
125 _HttpRequestImpl::GetTotalBodyLength(void) const
126 {
127         return __pHttpRequest->_totalLen;
128 }
129
130 void
131 _HttpRequestImpl::SetRecommendedSize(int recommendedSize)
132 {
133         __recommendedSize = recommendedSize;
134 }
135
136 void
137 _HttpRequestImpl::SetReceivedTransactionReadyToWriteEvent(bool isReceviedEvent)
138 {
139         __isReceivedTransactionReadyToWriteEvent = isReceviedEvent;
140 }
141
142 void
143 _HttpRequestImpl::SetFirstChunkBody(bool isFirstChunk)
144 {
145         __isFirstChunk = isFirstChunk;
146 }
147
148 bool
149 _HttpRequestImpl::IsFirstChunkBody(void) const
150 {
151         return __isFirstChunk;
152 }
153
154 bool
155 _HttpRequestImpl::IsLastChunkBody(void) const
156 {
157         return __isLastChunk;
158 }
159
160 bool
161 _HttpRequestImpl::HasCookie(void) const
162 {
163         bool rs = !(__cookieValue.IsEmpty());
164
165         return rs;
166 }
167
168 void
169 _HttpRequestImpl::SetSendingBuffer(ByteBuffer* pBuffer)
170 {
171         __pSendingBuffer = pBuffer;
172 }
173
174 ByteBuffer*
175 _HttpRequestImpl::GetSendingBuffer(void) const
176 {
177         return __pSendingBuffer;
178 }
179
180 Tizen::Base::String
181 _HttpRequestImpl::GetMethodName(void) const
182 {
183         return __methodName;
184 }
185
186 result
187 _HttpRequestImpl::SetMethod(const NetHttpMethod method)
188 {
189         result r = E_SUCCESS;
190
191         SysTryReturnResult(NID_NET_HTTP, __pHttpTransactionImpl->IsSubmitted() == false,
192                                            E_INVALID_STATE, "HttpRequest is already submitted.");
193
194         SysTryReturnResult(NID_NET_HTTP,
195                                            ((method >= NET_HTTP_METHOD_GET) &&
196                                                 (method <= NET_HTTP_METHOD_TRACE)) || method == NET_HTTP_METHOD_POST || method == NET_HTTP_METHOD_PUT || method == NET_HTTP_METHOD_CONNECT,
197                                            E_INVALID_ARG, "The method is invalid.");
198
199         switch (method)
200         {
201         case NET_HTTP_METHOD_GET:
202                 __methodName = L"GET";
203                 break;
204
205         case NET_HTTP_METHOD_OPTIONS:
206                 __methodName = L"OPTIONS";
207                 break;
208
209         case NET_HTTP_METHOD_HEAD:
210                 __methodName = L"HEAD";
211                 break;
212
213         case NET_HTTP_METHOD_DELETE:
214                 __methodName = L"DELETE";
215                 break;
216
217         case NET_HTTP_METHOD_TRACE:
218                 __methodName = L"TRACE";
219                 break;
220
221         case NET_HTTP_METHOD_POST:
222                 __methodName = L"POST";
223                 break;
224
225         case NET_HTTP_METHOD_PUT:
226                 __methodName = L"PUT";
227                 break;
228
229         case NET_HTTP_METHOD_CONNECT:
230                 __methodName = L"CONNECT";
231                 break;
232         }
233
234         __method = method;
235
236         SysLog(NID_NET_HTTP, "The method is %ls.", __methodName.GetPointer());
237
238         return r;
239 }
240
241 result
242 _HttpRequestImpl::SetCustomMethod(const String& method)
243 {
244         result r = E_SUCCESS;
245
246         SysTryReturnResult(NID_NET_HTTP, __pHttpTransactionImpl->IsSubmitted() == false,
247                                            E_INVALID_STATE, "HttpRequest is already submitted.");
248
249         SysTryReturnResult(NID_NET_HTTP, method.IsEmpty() == false,
250                                            E_INVALID_ARG, "The method is empty.");
251
252         __customMethodName = method;
253         __methodName = method;
254
255         SysLog(NID_NET_HTTP, "The custom method is %ls.", method.GetPointer());
256
257         return r;
258 }
259
260 result
261 _HttpRequestImpl::SetVersion(HttpVersion version)
262 {
263         result r = E_SUCCESS;
264
265         SysTryReturnResult(NID_NET_HTTP, __pHttpTransactionImpl->IsSubmitted() == false,
266                                            E_INVALID_STATE, "HttpRequest is already submitted.");
267
268         SysTryReturnResult(NID_NET_HTTP, version == HTTP_VERSION_1_0 || version == HTTP_VERSION_1_1,
269                                            E_INVALID_ARG, "The version is invalid.");
270
271         __httpVersion = version;
272
273         SysLog(NID_NET_HTTP, "The http version is %s.", version == HTTP_VERSION_1_0 ? "HTTP_VERSION_1_0": "HTTP_VERSION_1_1");
274
275         return r;
276 }
277
278 result
279 _HttpRequestImpl::SetUri(const String& uri)
280 {
281         result r = E_SUCCESS;
282
283         SysTryReturnResult(NID_NET_HTTP, __pHttpTransactionImpl->IsSubmitted() == false,
284                                            E_INVALID_STATE, "HttpRequest is already submitted.");
285
286         SysTryReturnResult(NID_NET_HTTP, uri.GetLength() > 0,
287                                            E_INVALID_ARG, "The uri is empty");
288
289         __uri = uri;
290
291         SysSecureLog(NID_NET_HTTP, "The uri is %ls.", uri.GetPointer());
292
293         return r;
294 }
295
296 result
297 _HttpRequestImpl::WriteBody(const Tizen::Base::ByteBuffer& body)
298 {
299         result r = E_SUCCESS;
300
301         unique_ptr<ByteBuffer> pBodyBuffer;
302         int srcBodySize = body.GetRemaining();
303
304         SysTryReturnResult(NID_NET_HTTP, srcBodySize >= 0,
305                                            E_INVALID_ARG, "The remaining of the buffer MUST be greater than or equal to 0.");
306
307         //Before submitting.
308         if (__pHttpTransactionImpl->IsSubmitted() == false)
309         {
310                 pBodyBuffer.reset(new (std::nothrow) ByteBuffer());
311                 SysTryReturnResult(NID_NET_HTTP, pBodyBuffer != null, E_OUT_OF_MEMORY,
312                                                            "[E_OUT_OF_MEMORY] Memory allocation failed.");
313
314                 r = pBodyBuffer->Construct(body);
315                 SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
316                                         "Failed to construct the ByteBuffer.");
317
318                 r = __pHttpRequest->_bodyQueue.Enqueue(*pBodyBuffer);
319                 SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
320                                         "Failed to enqueue the ByteBuffer to the queue.");
321
322                 __pHttpRequest->_totalLen += srcBodySize;
323                 __isFirstChunk = true;
324
325                 SysLog(NID_NET_HTTP, "Enqueue the body(%d) to queue. Total(%ld)", srcBodySize, __pHttpRequest->_totalLen);
326
327                 //After submitting in OnTransactionReadyToWrite().
328         }
329         else if (__pHttpTransactionImpl->IsSubmitted() == true && __pHttpTransactionImpl->IsTransactionReadyToWriteEanbled() == true)
330         {
331                 SysTryReturnResult(NID_NET_HTTP, __method == NET_HTTP_METHOD_POST || __method == NET_HTTP_METHOD_PUT, E_INVALID_STATE,
332                                         "Failed to construct the ByteBuffer.");
333
334                 SysTryReturnResult(NID_NET_HTTP, __isReceivedTransactionReadyToWriteEvent == true, E_INVALID_STATE,
335                                         "OnTransactionReadyToWrite event not received yet.");
336
337                 SysTryReturnResult(NID_NET_HTTP, __recommendedSize >= srcBodySize, E_OUT_OF_RANGE,
338                                         "The buffer size(%d) MUST be smaller than the recommended size(%d).", srcBodySize, __recommendedSize);
339
340                 pBodyBuffer.reset(new (std::nothrow) ByteBuffer());
341                 SysTryReturnResult(NID_NET_HTTP, pBodyBuffer != null, E_OUT_OF_MEMORY,
342                                                            "[E_OUT_OF_MEMORY] Memory allocation failed.");
343
344                 r = pBodyBuffer->Construct(body);
345                 SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
346                                         "Failed to construct the ByteBuffer.");
347
348                 r = __pHttpRequest->_bodyQueue.Enqueue(*pBodyBuffer);
349                 SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
350                                         "Failed to enqueue the ByteBuffer to the queue.");
351
352                 __pHttpRequest->_totalLen += srcBodySize;
353                 __isFirstChunk = false;
354
355                 SysLog(NID_NET_HTTP, "Enqueue the body(%d) to queue.(OnTransactionReadyToWrite). Total(%ld)", srcBodySize, __pHttpRequest->_totalLen);
356         }
357
358         pBodyBuffer.release();
359
360         return r;
361 }
362
363 result
364 _HttpRequestImpl::SetEntity(IHttpEntity& entity)
365 {
366         result r = E_SUCCESS;
367
368         SysTryReturnResult(NID_NET_HTTP, __pHttpTransactionImpl->IsSubmitted() == false,
369                                            E_INVALID_STATE, "HttpTransaction is already submitted.");
370
371         SysTryReturnResult(NID_NET_HTTP, __pHttpRequest->_totalLen == 0,
372                                            E_INVALID_STATE, "The body(buffer) is already set to the HttpRequest.");
373
374         String keyName;
375         String contentLength(L"0");
376         unique_ptr<ByteBuffer> pByteBuffer;
377         long long length = 0;
378         _HttpHeaderImpl* pHeaderImpl = null;
379
380         __pIHttpEntity = &entity;
381
382         pHeaderImpl = _HttpHeaderImpl::GetInstance(*__pHttpRequest->_pHeader);
383
384         //ToDo test.. chunked mode for entity.
385         __pHttpTransactionImpl->EnableTransactionReadyToWrite();
386
387         if (pHeaderImpl->HasHeader(_HTTP_CONTENT_TYPE_HEADER_NAME, keyName))
388         {
389                 pHeaderImpl->RemoveField(keyName);
390                 SysLog(NID_NET_HTTP, "The Content-Type header was removed.");
391         }
392
393         if (pHeaderImpl->HasHeader(_HTTP_CONTENT_LENGTH_HEADER_NAME, keyName))
394         {
395                 pHeaderImpl->RemoveField(keyName);
396                 SysLog(NID_NET_HTTP, "The Content-Length header was removed.");
397         }
398
399         r = pHeaderImpl->AddField(_HTTP_CONTENT_TYPE_HEADER_NAME, entity.GetContentType());
400         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, E_OUT_OF_MEMORY,
401                                 "Memory allocation failed.");
402
403         length = entity.GetContentLength();
404         SysTryReturnResult(NID_NET_HTTP, length != _HTTP_INVALID_CONTENT_LENGTH, E_INVALID_ARG,
405                                 "The content-length of entity is invalid.");
406
407         contentLength = LongLong::ToString(length);
408
409         r = pHeaderImpl->AddField(_HTTP_CONTENT_LENGTH_HEADER_NAME, contentLength);
410         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, E_OUT_OF_MEMORY,
411                                 "Memory allocation failed.");
412
413         SysLog(NID_NET_HTTP, "The Content-Type is %ls.", entity.GetContentType().GetPointer());
414         SysLog(NID_NET_HTTP, "The Content-Length is %ls.", contentLength.GetPointer());
415
416         if (entity.HasNextData())
417         {
418                 //generates the form data from IHttpEntity.
419                 pByteBuffer.reset(entity.GetNextDataN(length));
420                 r = GetLastResult();
421                 SysTryReturnResult(NID_NET_HTTP, pByteBuffer != null, r,
422                                         "Failed to generate form data from the IHttpEntity.");
423
424                 r = WriteBody(*pByteBuffer);
425                 SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS, r,
426                                         "Failed to write the byte buffer.");
427         }
428
429         SysLog(NID_NET_HTTP, "Completed to set the entity.");
430
431         return r;
432 }
433
434 HttpHeader*
435 _HttpRequestImpl::GetHeader(void) const
436 {
437         ClearLastResult();
438
439         SysTryReturn(NID_NET_HTTP, __pHttpRequest->_pHeader != null, null, E_INVALID_HEADER,
440                                  "[E_INVALID_HEADER] HttpHeader must not be null.");
441
442         return __pHttpRequest->_pHeader;
443 }
444
445 NetHttpMethod
446 _HttpRequestImpl::GetMethod() const
447 {
448         ClearLastResult();
449
450         return __method;
451 }
452
453 result
454 _HttpRequestImpl::GetCustomMethod(String& method) const
455 {
456         result r = E_SUCCESS;
457
458         method = __customMethodName;
459
460         return r;
461 }
462
463 HttpVersion
464 _HttpRequestImpl::GetVersion(void) const
465 {
466         ClearLastResult();
467
468         return __httpVersion;
469 }
470
471 result
472 _HttpRequestImpl::GetUri(String& uri) const
473 {
474         result r = E_SUCCESS;
475
476         uri = __uri;
477
478         return r;
479 }
480
481 Tizen::Base::ByteBuffer*
482 _HttpRequestImpl::ReadBodyN()
483 {
484         ClearLastResult();
485         result r = E_SUCCESS;
486
487         unique_ptr<ByteBuffer> pResultBuffer;
488         ByteBuffer* pByteBuffer = null;
489         int resultBufferLen = 0;
490         int countOfCurrentBuffer = 0;
491
492         countOfCurrentBuffer = __pHttpRequest->_bodyQueue.GetCount();
493         if (countOfCurrentBuffer <= 0)
494         {
495                 r = E_EMPTY_BODY;
496                 SetLastResult(r);
497                 SysLog(NID_NET_HTTP, "No more data to read.");
498                 return null;
499         }
500
501         pResultBuffer.reset(new (std::nothrow) ByteBuffer());
502         SysTryReturn(NID_NET_HTTP, pResultBuffer != null, null, E_OUT_OF_MEMORY,
503                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
504
505         r = pResultBuffer->Construct(__pHttpRequest->_totalLen);
506         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, null, E_OUT_OF_MEMORY,
507                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
508
509         for (int i = 0; i < countOfCurrentBuffer; i++)
510         {
511                 int bufferSize = 0;
512
513                 pByteBuffer = dynamic_cast< ByteBuffer* >(__pHttpRequest->_bodyQueue.Dequeue());
514                 SysTryReturn(NID_NET_HTTP, pByteBuffer != null, null, E_EMPTY_BODY,
515                                         "[E_EMPTY_BODY] The buffer of the body is empty.");
516
517                 bufferSize = pByteBuffer->GetRemaining();
518
519                 r = pResultBuffer->CopyFrom(*pByteBuffer);
520                 SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, null, E_OUT_OF_RANGE,
521                                         "[E_OUT_OF_RANGE] Failed to copy the buffer to new buffer.");
522
523                 delete pByteBuffer;
524
525                 if (bufferSize == 0)
526                 {
527                         SysLog(NID_NET_HTTP, "Write the last chunk(Empty Body).");
528                         __isLastChunk = true;
529                         break;
530                 }
531         }
532
533         pResultBuffer->Flip();
534         resultBufferLen = pResultBuffer->GetRemaining();
535         __pHttpRequest->_totalLen = __pHttpRequest->_totalLen - resultBufferLen;
536
537         SysLog(NID_NET_HTTP, "Read the body(%d), Remained Body(%d).", resultBufferLen, __pHttpRequest->_totalLen);
538
539         return pResultBuffer.release();
540 }
541
542 Tizen::Base::ByteBuffer*
543 _HttpRequestImpl::ReadAllBodyN(void) const
544 {
545         ClearLastResult();
546         result r = E_SUCCESS;
547
548         unique_ptr<ByteBuffer> pResultBuffer;
549         ByteBuffer* pByteBuffer = null;
550         int resultBufferLen = 0;
551
552         unique_ptr<IEnumerator> pEnum;
553         int countOfCurrentBuffer = 0;
554
555         countOfCurrentBuffer = __pHttpRequest->_bodyQueue.GetCount();
556         if (countOfCurrentBuffer <= 0)
557         {
558                 r = E_EMPTY_BODY;
559                 SetLastResult(r);
560                 SysLog(NID_NET_HTTP, "No more data to read.");
561                 return null;
562         }
563
564         pEnum.reset(__pHttpRequest->_bodyQueue.GetEnumeratorN());
565         SysTryReturn(NID_NET_HTTP, pEnum != null, null, E_OUT_OF_MEMORY,
566                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
567
568         pResultBuffer.reset(new (std::nothrow) ByteBuffer());
569         SysTryReturn(NID_NET_HTTP, pResultBuffer != null, null, E_OUT_OF_MEMORY,
570                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
571
572         r = pResultBuffer->Construct(__pHttpRequest->_totalLen);
573         SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, null, E_OUT_OF_MEMORY,
574                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
575
576         while (pEnum->MoveNext() == E_SUCCESS)
577         {
578                 int bufferSize = 0;
579
580                 pByteBuffer = dynamic_cast< ByteBuffer* >(pEnum->GetCurrent());
581                 SysTryReturn(NID_NET_HTTP, pByteBuffer != null, null, E_EMPTY_BODY,
582                                         "[E_EMPTY_BODY] The buffer of the body is empty.");
583
584                 bufferSize = pByteBuffer->GetRemaining();
585
586                 r = pResultBuffer->CopyFrom(*pByteBuffer);
587                 SysTryReturn(NID_NET_HTTP, r == E_SUCCESS, null, E_OUT_OF_RANGE,
588                                         "[E_OUT_OF_RANGE] Failed to copy the buffer to new buffer.");
589
590                 pByteBuffer->Flip();
591         }
592
593         pResultBuffer->Flip();
594         resultBufferLen = pResultBuffer->GetRemaining();
595
596         SysLog(NID_NET_HTTP, "Read the body(%d).", resultBufferLen);
597
598         return pResultBuffer.release();
599 }
600
601 IHttpEntity*
602 _HttpRequestImpl::GetEntity(void) const
603 {
604         return __pIHttpEntity;
605 }
606
607 bool
608 _HttpRequestImpl::HasNextData(void)
609 {
610         bool rs = false;
611         rs = __pIHttpEntity->HasNextData();
612
613         return rs;
614 }
615
616 ByteBuffer*
617 _HttpRequestImpl::GetNextDataN(int recommendedSize)
618 {
619         ByteBuffer* pBuffer = null;
620         pBuffer = __pIHttpEntity->GetNextDataN(recommendedSize);
621
622         return pBuffer;
623 }
624
625 result
626 _HttpRequestImpl::Set(_HttpRequestImpl* pHttpRequestImpl)
627 {
628         result r = E_SUCCESS;
629
630         HttpHeader* pHttpHeader = null;
631         HttpHeader* pSrcHttpHeader = null;
632
633         //Sets member variables.
634         __method = pHttpRequestImpl->GetMethod();
635         pHttpRequestImpl->GetCustomMethod(__customMethodName);
636         __methodName = pHttpRequestImpl->GetMethodName();
637         __httpVersion = pHttpRequestImpl->GetVersion();
638         pHttpRequestImpl->GetUri(__uri);
639         __cookieValue = pHttpRequestImpl->GetCookie();
640         __encoding = pHttpRequestImpl->GetAcceptEncoding();
641         __pIHttpEntity = pHttpRequestImpl->GetEntity();
642
643         pHttpHeader = pHttpRequestImpl->GetHeader();
644
645         //Sets request buffer. (message queue).
646         ByteBuffer* pBuffer = null;
647         do
648         {
649                 pBuffer = pHttpRequestImpl->ReadBodyN();
650                 if (pBuffer)
651                 {
652                         this->WriteBody(*pBuffer);
653                 }
654         }
655         while(pBuffer != null);
656
657         // Sets the headers.
658         pSrcHttpHeader = new (std::nothrow) HttpHeader(pHttpHeader);
659         SysTryReturn(NID_NET_HTTP, pSrcHttpHeader != null, null, E_OUT_OF_MEMORY,
660                                 "[E_OUT_OF_MEMORY] Memory allocation failed.");
661
662         __pHttpRequest->_pHeader = pSrcHttpHeader;
663
664         return r;
665 }
666
667 result
668 _HttpRequestImpl::SetCookie(const Tizen::Base::String& cookieString)
669 {
670         result r = E_SUCCESS;
671
672         SysTryReturnResult(NID_NET_HTTP, cookieString.IsEmpty() == false,
673                                            E_INVALID_ARG, "The input argument is empty.");
674
675         SysTryReturnResult(NID_NET_HTTP, __cookieFlag == NET_HTTP_COOKIE_FLAG_ALWAYS_MANUAL,
676                                            E_INVALID_STATE, "The cookieFlag of HttpSession is invalid.");
677
678         SysTryReturnResult(NID_NET_HTTP, __pHttpTransactionImpl->IsSubmitted() == false,
679                                            E_INVALID_STATE, "HttpTransaction is already submitted.");
680
681         String lowerString;
682         String subCookieString;
683
684         r = cookieString.ToLower(lowerString);
685         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS,
686                                            E_INVALID_ARG, "The input argument is invalid.");
687
688         SysTryReturnResult(NID_NET_HTTP, lowerString.StartsWith(L"cookie:", 0),
689                                            E_INVALID_ARG, "Cookie string doesn't start with Cookie:");
690
691         r = cookieString.SubString(_COOKIE_PREFIX_LENGTH, subCookieString);
692         SysTryReturnResult(NID_NET_HTTP, r == E_SUCCESS,
693                                            E_INVALID_ARG, "Failed to get the sub cookie string.");
694
695         subCookieString.Trim();
696
697         __cookieValue = subCookieString;
698
699         SysSecureLog(NID_NET_HTTP, "The cookie string is %ls.", cookieString.GetPointer());
700
701         return r;
702 }
703
704 String
705 _HttpRequestImpl::GetCookie(void) const
706 {
707         ClearLastResult();
708
709         SysTryReturn(NID_NET_HTTP, __cookieValue.IsEmpty() == false, L"", E_INVALID_STATE,
710                                  "[E_INVALID_STATE] Cookies is empty.");
711
712         SysTryReturn(NID_NET_HTTP, __cookieFlag == NET_HTTP_COOKIE_FLAG_ALWAYS_MANUAL, L"", E_INVALID_STATE,
713                                  "[E_INVALID_STATE] The cookieFlag of HttpSession is invalid.");
714
715         return __cookieValue;
716 }
717
718 result
719 _HttpRequestImpl::SetAcceptEncoding(const Tizen::Base::String& encoding)
720 {
721         result r = E_SUCCESS;
722
723         SysTryReturnResult(NID_NET_HTTP, __pHttpTransactionImpl->IsSubmitted() == false, E_INVALID_STATE, "HttpRequest is already submitted.");
724         SysTryReturnResult(NID_NET_HTTP, encoding.IsEmpty() == false, E_INVALID_ARG, "The input argument is empty.");
725
726         __encoding = encoding;
727
728         return r;
729 }
730
731 Tizen::Base::String
732 _HttpRequestImpl::GetAcceptEncoding(void) const
733 {
734         return __encoding;
735 }
736
737 _HttpRequestImpl*
738 _HttpRequestImpl::GetInstance(HttpRequest& httpRequest)
739 {
740         return httpRequest.__pHttpRequestImpl;
741 }
742
743 const _HttpRequestImpl*
744 _HttpRequestImpl::GetInstance(const HttpRequest& httpRequest)
745 {
746         return httpRequest.__pHttpRequestImpl;
747 }
748
749 } } } // Tizen::Net::Http