tizen beta release
[framework/web/wrt-installer.git] / src / misc / wac_widget_id.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  * @file
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief
21  */
22 #include "wac_widget_id.h"
23
24 #include <memory>
25 #include <string>
26
27 #include <dpl/log/log.h>
28 #include <dpl/string.h>
29
30 #include <iri.h>
31 #include <vcore/ValidatorCommon.h>
32
33 namespace {
34 const char *SCHEME_HTTP = "http";
35 const char *SCHEME_HTTPS = "https";
36 }
37
38 WacWidgetId::WacWidgetId(const DPL::OptionalString &widgetId) :
39     m_schemaMatch(false)
40 {
41     if (!widgetId.IsNull()) {
42         std::string wid = DPL::ToUTF8String(*widgetId);
43         parse(wid.c_str());
44     }
45 }
46
47 bool WacWidgetId::matchHost(const DPL::String &second) const
48 {
49     LogDebug("m_schemaMatch is: " << m_schemaMatch);
50     if (!m_schemaMatch) {
51         return false;
52     }
53
54     LogDebug("Matching DNS identity: " << m_host <<
55              " " << DPL::ToUTF8String(second));
56
57     return m_host == DPL::ToUTF8String(second);
58 }
59
60 void WacWidgetId::parse(const char *url)
61 {
62     LogDebug("Widget id to parse: " << url);
63
64     std::unique_ptr<iri_struct, std::function<void(iri_struct*)> >
65             iri(iri_parse(url), iri_destroy);
66
67     if (!iri.get()) {
68         LogDebug("Error in parsing widget id.");
69         return; // m_schemaMatch == false;
70     }
71
72     std::string scheme;
73
74     if (iri.get()->scheme) {
75         scheme = iri.get()->scheme;
76     } else {
77         LogWarning("Error. No scheme in widget id.");
78         return; // m_schemaMatch == false;
79     }
80
81     // should we support HTTP and HTTPS? wac says nothing
82     // std::transform(m_scheme.begin(), m_scheme.end(), m_scheme.begin(), tolower);
83
84     // We only match "http" and "https" schemas
85     if ((scheme != SCHEME_HTTP) && (scheme != SCHEME_HTTPS)) {
86         LogWarning("Unknown scheme in widget id." << scheme);
87         return; // m_schemaMatch == false;
88     } else {
89         m_schemaMatch = true;
90     }
91
92     if (iri.get()->host) {
93         m_host = iri.get()->host;
94         LogDebug("Host has been set to: " << m_host);
95     }
96
97     // What to do when host is empty? No info in wac documentation.
98
99     // Any post processing algorithm? No info in wac documentation.
100 }