Source code formating unification
[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 <set>
24 #include <string>
25 #include <dpl/utils/warp_iri.h>
26 #include <dpl/string.h>
27 #include <dpl/auto_ptr.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://", "tel:", "sms:",
44                                      "mmsto:", "mailto:", "data:", "blob:", 0 };
45
46 const DPL::String SCHEMA_HTTP = DPL::FromUTF8String("http");
47 const DPL::String SCHEMA_HTTPS = DPL::FromUTF8String("https");
48 const DPL::String SCHEMA_FTP = DPL::FromUTF8String("ftp");
49 } // namespace anonymous
50
51 // This will create AutoPtr deleter for iri_struct.
52 // Deleter must be in the same namespace as definition of AutoPtr.
53
54 namespace DPL {
55 DECLARE_DELETER(iri_struct, iri_destroy)
56 } // namespace DPL
57
58 WarpIRI::WarpIRI() :
59     m_domain(false),
60     m_port(UNKNOWN_PORT),
61     m_isAccessDefinition(false),
62     m_isIRIValid(false)
63 {}
64
65 void WarpIRI::set(const char *p_iri,
66                   bool domain)
67 {
68     if (!p_iri) {
69         m_isAccessDefinition = m_isIRIValid = false;
70         return;
71     }
72
73     m_domain = domain;
74     m_isAccessDefinition = true;
75     m_isIRIValid = true;
76     m_host.clear();
77
78     if (strcmp(p_iri, "*") == 0) {
79         return;
80     }
81
82     DPL::AutoPtr<iri_struct> iri(iri_parse(p_iri));
83
84     if (!iri.get()) {
85         LogError("Error in iri_parse!");
86         m_isIRIValid = false;
87         m_isAccessDefinition = false;
88         return;
89     }
90
91     if (iri->scheme == NULL || iri->host == NULL) {
92         m_isIRIValid = false;
93         m_isAccessDefinition = false;
94         return;
95     }
96
97     // all of this must be NULL in WARP definition
98     if (iri->user || iri->path || iri->query || iri->anchor) {
99         m_isAccessDefinition = false;
100     }
101
102     m_schema = DPL::FromASCIIString(std::string(iri->scheme));
103     m_port = static_cast<unsigned int>(iri->port);
104
105     if (m_port == 0) {
106         m_port = getPort(m_schema);
107         if (m_port == UNKNOWN_PORT) {
108             m_isAccessDefinition = false;
109             return;
110         }
111     }
112
113     DPL::String str = DPL::FromASCIIString(std::string(iri->host));
114
115     std::string utf8host = iri->host;
116     std::list<std::string> hostTokenList;
117     DPL::Tokenize(utf8host, ".", std::front_inserter(hostTokenList));
118
119     if (SCHEMA_HTTP == m_schema || SCHEMA_HTTPS == m_schema) {
120         FOREACH(i, hostTokenList) {
121             char *output = NULL;
122             int rc = idna_to_ascii_8z(i->c_str(),
123                                       &output,
124                                       IDNA_USE_STD3_ASCII_RULES);
125
126             if (IDNA_SUCCESS != rc) {
127                 LogWarning("libidn error: " << rc << " " <<
128                            idna_strerror((Idna_rc)rc));
129                 m_isIRIValid = false;
130                 m_isAccessDefinition = false;
131             } else {
132                 std::string token(output);
133                 std::transform(token.begin(),
134                                token.end(),
135                                token.begin(),
136                                    ::tolower);
137                 m_host.push_back(DPL::FromUTF8String(token));
138             }
139             free(output);
140         }
141     } else {
142         FOREACH(i, hostTokenList){
143             m_host.push_back(DPL::FromUTF8String(*i));
144         }
145     }
146 }
147
148 void WarpIRI::set(const DPL::String &iristring,
149                   bool domain)
150 {
151     set(DPL::ToUTF8String(iristring).c_str(), domain);
152 }
153
154 unsigned int WarpIRI::getPort(const DPL::String &schema) const
155 {
156     unsigned int port = UNKNOWN_PORT;
157     if (schema == SCHEMA_HTTP) {
158         port = 80;
159     } else if (schema == SCHEMA_HTTPS) {
160         port = 443;
161     } else if (schema == SCHEMA_FTP) {
162         port = 21;
163     }
164     return port;
165 }
166
167 bool WarpIRI::isSubDomain(const WarpIRI &second) const
168 {
169     if (!m_isAccessDefinition || !second.m_isIRIValid) {
170         return false;
171     }
172     if (m_schema != second.m_schema) {
173         return false;
174     }
175     if (m_port != second.m_port) {
176         return false;
177     }
178
179     size_t size = m_host.size() < second.m_host.size() ?
180         m_host.size() : second.m_host.size();
181
182     if (m_host.size() > second.m_host.size()) {
183         return false;
184     }
185
186     if (!m_domain && (m_host.size() != second.m_host.size())) {
187         return false;
188     }
189
190     for (size_t i = 0; i < size; ++i) {
191         if (DPL::StringCompare(m_host[i], second.m_host[i])) {
192             return false;
193         }
194     }
195     return true;
196 }
197
198 bool WarpIRI::isAccessDefinition() const
199 {
200     return m_isAccessDefinition;
201 }
202
203 bool WarpIRI::getSubDomain() const
204 {
205     return m_domain;
206 }
207
208 bool WarpIRI::isIRISchemaIgnored(const char *iri)
209 {
210     for (int i = 0; IRI_IGNORED_SCHEME[i]; ++i) {
211         if (0 ==
212             strncmp(iri, IRI_IGNORED_SCHEME[i],
213                     strlen(IRI_IGNORED_SCHEME[i])))
214         {
215             return true;
216         }
217     }
218     return false;
219 }