[Release] wrt-installer_0.1.58
[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
32 namespace {
33 const char *SCHEME_HTTP = "http";
34 const char *SCHEME_HTTPS = "https";
35 }
36
37 WacWidgetId::WacWidgetId(const DPL::OptionalString &widgetId) :
38     m_schemaMatch(false)
39 {
40     if (!widgetId.IsNull()) {
41         std::string wid = DPL::ToUTF8String(*widgetId);
42         parse(wid.c_str());
43     }
44 }
45
46 bool WacWidgetId::matchHost(const DPL::String &second) const
47 {
48     LogDebug("m_schemaMatch is: " << m_schemaMatch);
49     if (!m_schemaMatch) {
50         return false;
51     }
52
53     LogDebug("Matching DNS identity: " << m_host <<
54              " " << DPL::ToUTF8String(second));
55
56     return m_host == DPL::ToUTF8String(second);
57 }
58
59 void WacWidgetId::parse(const char *url)
60 {
61     LogDebug("Widget id to parse: " << url);
62
63     std::unique_ptr<iri_struct, std::function<void(iri_struct*)> >
64     iri(iri_parse(url), iri_destroy);
65
66     if (!iri.get()) {
67         LogError("Error in parsing widget id.");
68         return; // m_schemaMatch == false;
69     }
70
71     std::string scheme;
72
73     if (iri.get()->scheme) {
74         scheme = iri.get()->scheme;
75     } else {
76         LogWarning("Error. No scheme in widget id.");
77         return; // m_schemaMatch == false;
78     }
79
80     // should we support HTTP and HTTPS? wac says nothing
81     // std::transform(m_scheme.begin(), m_scheme.end(), m_scheme.begin(),
82     // 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 }