Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / 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 "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     LogDebug("Proxy ptr:" << (void*)proxy.get() <<
47              " Proxy addr: " << proxy.get());
48
49     for(int tryCount = 0; tryCount < m_tryCount; ++ tryCount){
50         LogDebug("Try(" << tryCount << ") to download " << m_host);
51
52         ScopedSoupSession session(soup_session_async_new_with_options(
53               SOUP_SESSION_ASYNC_CONTEXT,
54               &*context,
55               SOUP_SESSION_TIMEOUT,
56               m_timeout,
57               SOUP_SESSION_PROXY_URI,
58               proxyURI.get(),
59               NULL));
60
61         ScopedSoupMessage msg;
62
63         msg.Reset(createRequest());
64
65         if (!msg) {
66             LogError("Unable to send HTTP request.");
67             m_status = STATUS_IDLE;
68             return REQUEST_STATUS_CONNECTION_ERROR;
69         }
70         soup_session_send_message(&*session, &*msg);
71
72         // if (SOUP_STATUS_IS_SUCCESSFUL(msg->status_code))
73
74         if (msg->status_code == SOUP_STATUS_OK) {
75             m_responseBuffer.resize(msg->response_body->length);
76             memcpy(&m_responseBuffer[0],
77               msg->response_body->data,
78               msg->response_body->length);
79             // We are done.
80             m_status = STATUS_IDLE;
81             return REQUEST_STATUS_OK;
82         } else {
83             LogWarning("Soup failed with code " << msg->status_code
84               << " message \n------------\n"
85               << msg->response_body->data
86               << "\n--------------\n");
87         }
88     }
89
90     m_status = STATUS_IDLE;
91     return REQUEST_STATUS_CONNECTION_ERROR;
92 }
93
94 } // namespave ValidationCore