Refactor log system
[platform/core/security/cert-svc.git] / vcore / src / vcore / SoupMessageSendSync.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        SoupMessageSendSync.cpp
20  * @brief       Implementation of soup synchronous interface.
21  */
22 #include <vcore/SoupMessageSendSync.h>
23
24 #include <memory>
25 #include <functional>
26
27 #include <vconf.h>
28
29 #include <dpl/log/log.h>
30
31 namespace SoupWrapper {
32
33 SoupMessageSendBase::RequestStatus SoupMessageSendSync::sendSync()
34 {
35     Assert(m_status == STATUS_IDLE);
36     m_status = STATUS_SEND_SYNC;
37
38     ScopedGMainContext context(g_main_context_new());
39
40     std::unique_ptr<char,std::function<void(void*)> >
41             proxy(vconf_get_str(VCONFKEY_NETWORK_PROXY), free);
42
43     std::unique_ptr <SoupURI, std::function<void(SoupURI*)> >
44                    proxyURI(soup_uri_new (proxy.get()), soup_uri_free);
45
46     for(int tryCount = 0; tryCount < m_tryCount; ++ tryCount){
47         LogDebug("Try(" << tryCount << ") to download " << m_host);
48
49         ScopedSoupSession session(soup_session_async_new_with_options(
50               SOUP_SESSION_ASYNC_CONTEXT,
51               &*context,
52               SOUP_SESSION_TIMEOUT,
53               m_timeout,
54               SOUP_SESSION_PROXY_URI,
55               proxyURI.get(),
56               NULL));
57
58         ScopedSoupMessage msg;
59
60         msg.Reset(createRequest());
61
62         if (!msg) {
63             LogError("Unable to send HTTP request.");
64             m_status = STATUS_IDLE;
65             return REQUEST_STATUS_CONNECTION_ERROR;
66         }
67         soup_session_send_message(&*session, &*msg);
68
69         // if (SOUP_STATUS_IS_SUCCESSFUL(msg->status_code))
70
71         if (msg->status_code == SOUP_STATUS_OK) {
72             m_responseBuffer.resize(msg->response_body->length);
73             memcpy(&m_responseBuffer[0],
74               msg->response_body->data,
75               msg->response_body->length);
76             // We are done.
77             m_status = STATUS_IDLE;
78             return REQUEST_STATUS_OK;
79         } else {
80             LogWarning("Soup failed with code [" << msg->status_code << "] message [" << msg->response_body->data << "]");
81         }
82     }
83
84     m_status = STATUS_IDLE;
85     return REQUEST_STATUS_CONNECTION_ERROR;
86 }
87
88 } // namespave ValidationCore