tizen 2.4 release
[framework/web/wrt-commons.git] / modules / utils / src / warp_iri.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  * This file  have been implemented in compliance with  W3C WARP SPEC.
18  * but there are some patent issue between  W3C WARP SPEC and APPLE.
19  * so if you want to use this file, refer to the README file in root directory
20  */
21 #include <stddef.h>
22 #include <list>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <dpl/utils/warp_iri.h>
27 #include <dpl/string.h>
28 #include <dpl/foreach.h>
29 #include <idna.h>
30 #include <istream>
31 #include <iri.h>
32 //#include <ValidatorCommon.h>
33
34 namespace {
35 // All schemes which are supported by external application should be ignored
36 // by WARP engine.
37 //
38 // Warp specification require from iri to have host element. File protocol
39 // does not contain host element so it's always denied by warp.
40 // Unfortunatly all widgets are using file protocol to load its data from
41 // hard drive. What's why we cannot check any iri with file schema.
42
43 const char *IRI_IGNORED_SCHEME[] = { "file://", "widget://", "app://", "tel:",
44                                      "sms:", "smsto:", "mmsto:", "mailto:", "data:", "blob:",
45                                      "tizen-service:", 0 };
46
47 const DPL::String SCHEMA_HTTP = DPL::FromUTF8String("http");
48 const DPL::String SCHEMA_HTTPS = DPL::FromUTF8String("https");
49 const DPL::String SCHEMA_FTP = DPL::FromUTF8String("ftp");
50 } // namespace anonymous
51
52 WarpIRI::WarpIRI() :
53     m_domain(false),
54     m_port(UNKNOWN_PORT),
55     m_isAccessDefinition(false),
56     m_isIRIValid(false)
57 {}
58
59 void WarpIRI::set(const char *p_iri,
60                   bool domain)
61 {
62     if (!p_iri) {
63         m_isAccessDefinition = m_isIRIValid = false;
64         return;
65     }
66
67     m_domain = domain;
68     m_isAccessDefinition = true;
69     m_isIRIValid = true;
70     m_host.clear();
71
72     if (strcmp(p_iri, "*") == 0) {
73         return;
74     }
75     std::unique_ptr<iri_struct, decltype(&iri_destroy)> iri(iri_parse(p_iri), iri_destroy);
76
77     if (!iri.get()) {
78         WrtLogE("Error in iri_parse!");
79         m_isIRIValid = false;
80         m_isAccessDefinition = false;
81         return;
82     }
83
84     if (iri->scheme == NULL || iri->host == NULL) {
85         m_isIRIValid = false;
86         m_isAccessDefinition = false;
87         return;
88     }
89
90     // all of this must be NULL in WARP definition
91     if (iri->user || iri->path || iri->query || iri->anchor) {
92         m_isAccessDefinition = false;
93     }
94
95     m_schema = DPL::FromASCIIString(std::string(iri->scheme));
96     m_port = static_cast<unsigned int>(iri->port);
97
98     if (m_port == 0) {
99         m_port = getPort(m_schema);
100         if (m_port == UNKNOWN_PORT) {
101             m_isAccessDefinition = false;
102             return;
103         }
104     }
105
106     std::string utf8host = iri->host;
107     std::list<std::string> hostTokenList;
108     DPL::Tokenize(utf8host, ".", std::front_inserter(hostTokenList));
109
110     if (SCHEMA_HTTP == m_schema || SCHEMA_HTTPS == m_schema) {
111         FOREACH(i, hostTokenList) {
112             char *output = NULL;
113             int rc = idna_to_ascii_8z(i->c_str(),
114                                       &output,
115                                       IDNA_USE_STD3_ASCII_RULES);
116
117             if (IDNA_SUCCESS != rc) {
118                 WrtLogW("libidn error: %i %s", rc, idna_strerror((Idna_rc)rc));
119                 m_isIRIValid = false;
120                 m_isAccessDefinition = false;
121             } else {
122                 std::string token(output);
123                 std::transform(token.begin(),
124                                token.end(),
125                                token.begin(),
126                                    ::tolower);
127                 m_host.push_back(DPL::FromUTF8String(token));
128             }
129             free(output);
130         }
131     } else {
132         FOREACH(i, hostTokenList){
133             m_host.push_back(DPL::FromUTF8String(*i));
134         }
135     }
136 }
137
138 void WarpIRI::set(const DPL::String &iristring,
139                   bool domain)
140 {
141     set(DPL::ToUTF8String(iristring).c_str(), domain);
142 }
143
144 unsigned int WarpIRI::getPort(const DPL::String &schema) const
145 {
146     unsigned int port = UNKNOWN_PORT;
147     if (schema == SCHEMA_HTTP) {
148         port = 80;
149     } else if (schema == SCHEMA_HTTPS) {
150         port = 443;
151     } else if (schema == SCHEMA_FTP) {
152         port = 21;
153     }
154     return port;
155 }
156
157 bool WarpIRI::isSubDomain(const WarpIRI &second) const
158 {
159     if (!m_isAccessDefinition || !second.m_isIRIValid) {
160         return false;
161     }
162     if (m_schema != second.m_schema) {
163         return false;
164     }
165     if (m_port != second.m_port) {
166         return false;
167     }
168
169     size_t size = m_host.size() < second.m_host.size() ?
170         m_host.size() : second.m_host.size();
171
172     if (m_host.size() > second.m_host.size()) {
173         return false;
174     }
175
176     if (!m_domain && (m_host.size() != second.m_host.size())) {
177         return false;
178     }
179
180     for (size_t i = 0; i < size; ++i) {
181         if (DPL::StringCompare(m_host[i], second.m_host[i])) {
182             return false;
183         }
184     }
185     return true;
186 }
187
188 bool WarpIRI::isAccessDefinition() const
189 {
190     return m_isAccessDefinition;
191 }
192
193 bool WarpIRI::getSubDomain() const
194 {
195     return m_domain;
196 }
197
198 bool WarpIRI::isIRISchemaIgnored(const char *iri)
199 {
200     for (int i = 0; IRI_IGNORED_SCHEME[i]; ++i) {
201         if (0 ==
202             strncmp(iri, IRI_IGNORED_SCHEME[i],
203                     strlen(IRI_IGNORED_SCHEME[i])))
204         {
205             return true;
206         }
207     }
208     return false;
209 }