tizen beta release
[framework/web/wrt-commons.git] / modules / vcore / src / vcore / SoupMessageSendBase.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*!
17  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version     0.1
19  * @file        SoupMessageSendBase.cpp
20  * @brief       Simple wrapper for soup.
21  */
22 #include "SoupMessageSendBase.h"
23
24 #include <dpl/assert.h>
25 #include <dpl/foreach.h>
26 #include <dpl/log/log.h>
27
28 namespace SoupWrapper {
29
30 SoupMessageSendBase::SoupMessageSendBase()
31   : m_status(STATUS_IDLE)
32   , m_timeout(30)
33   , m_tryCount(5)
34 {}
35
36 SoupMessageSendBase::~SoupMessageSendBase(){
37     Assert(m_status == STATUS_IDLE);
38 }
39
40 void SoupMessageSendBase::setHeader(const std::string &property, const std::string &value){
41     Assert(m_status == STATUS_IDLE);
42     m_headerMap[property] = value;
43 }
44
45 void SoupMessageSendBase::setHost(const std::string &host){
46     Assert(m_status == STATUS_IDLE);
47     m_host = host;
48 }
49
50 void SoupMessageSendBase::setRequest(const std::string &contentType, const MessageBuffer &message){
51     Assert(m_status == STATUS_IDLE);
52     m_requestType = contentType;
53     m_requestBuffer = message;
54 }
55
56 SoupMessageSendBase::MessageBuffer SoupMessageSendBase::getResponse() const {
57     Assert(m_status == STATUS_IDLE);
58     return m_responseBuffer;
59 }
60
61 void SoupMessageSendBase::setTimeout(int seconds) {
62     Assert(m_status == STATUS_IDLE);
63     Assert(seconds >= 0);
64     m_timeout = seconds;
65 }
66
67 void SoupMessageSendBase::setRetry(int retry) {
68     Assert(m_status == STATUS_IDLE);
69     Assert(retry >= 0);
70     m_tryCount = retry + 1;
71 }
72
73
74 SoupMessage* SoupMessageSendBase::createRequest(){
75     SoupMessage *message;
76
77     LogInfo("Soup message will be send to: " << m_host.c_str());
78
79     if (!m_requestBuffer.empty()) {
80         message = soup_message_new("POST", m_host.c_str());
81     } else {
82         message = soup_message_new("GET", m_host.c_str());
83     }
84
85     if (!message) {
86         LogError("Error creating request!");
87         return 0;
88     }
89
90     FOREACH(it, m_headerMap){
91         soup_message_headers_append(message->request_headers,
92                                     it->first.c_str(),
93                                     it->second.c_str());
94     }
95
96     if (!m_requestBuffer.empty()) {
97         soup_message_set_http_version(message, SOUP_HTTP_1_0);
98         soup_message_set_request(message,
99           m_requestType.c_str(),
100           SOUP_MEMORY_COPY,
101           &m_requestBuffer[0],
102           m_requestBuffer.size());
103     }
104     soup_message_set_flags(message, SOUP_MESSAGE_NO_REDIRECT);
105     return message;
106 }
107
108 } // namespace ValidationCore