74ccd7cf1f7e1d76742d3e5f0b27be874fe177e7
[platform/core/security/cert-svc.git] / vcore / src / vcore / WacOrigin.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 "WacOrigin.h"
23
24 #include <algorithm>
25 #include <ctype.h>
26 #include <idna.h>
27
28 #include <dpl/log/log.h>
29
30 #include <iri.h>
31 #include "ValidatorCommon.h"
32
33 namespace {
34 const std::string SCHEME_HTTP = "http";
35 const std::string SCHEME_HTTPS = "https";
36 const int PORT_HTTP = 80;
37 const int PORT_HTTPS = 443;
38 }
39
40 namespace ValidationCore {
41 VC_DECLARE_DELETER(iri_struct, iri_destroy)
42
43 WacOrigin::WacOrigin(const std::string &url) :
44     m_port(0),
45     m_parseFailed(false)
46 {
47     parse(url.c_str());
48 }
49
50 WacOrigin::WacOrigin(const char *url) :
51     m_port(0),
52     m_parseFailed(false)
53 {
54     parse(url);
55 }
56
57 bool WacOrigin::operator==(const WacOrigin &second) const
58 {
59     if (m_parseFailed || second.m_parseFailed) {
60         return false;
61     }
62
63     return (m_scheme == second.m_scheme) &&
64            (m_host == second.m_host) &&
65            (m_port == second.m_port);
66 }
67
68 void WacOrigin::parse(const char *url)
69 {
70     // Step are taken from algorihtm:
71     // http://www.w3.org/TR/html5/origin-0.html#origin-0
72
73     // Step 1
74     // Step 2
75     AutoPtr<iri_struct> iri(iri_parse(url));
76     if (!iri.get()) {
77         m_parseFailed = true;
78         return;
79     }
80
81     if (iri->scheme) {
82         m_scheme = iri->scheme;
83     } else {
84         m_parseFailed = true;
85         return;
86     }
87
88     // Step 3 - Skip this point.
89     // WAC 2.0 PRV - we are suport only "http" and "https" schemas.
90
91     // Step 4 - Todo
92
93     // Step 5
94     std::transform(m_scheme.begin(), m_scheme.end(), m_scheme.begin(), tolower);
95
96     // Step 6 - we only support "http" and "https" schemas
97     if ((m_scheme != SCHEME_HTTP) && (m_scheme != SCHEME_HTTPS)) {
98         m_parseFailed = true;
99         return;
100     }
101
102     // Step 7 - Skip. We do not support "file" schema.
103
104     // Step 8
105     if (iri->host) {
106         m_host = iri->host;
107     } else {
108         m_parseFailed = true;
109         return;
110     }
111
112     // Step 9
113     char *output = NULL;
114     if (IDNA_SUCCESS !=
115         idna_to_ascii_lz(m_host.c_str(), &output, IDNA_USE_STD3_ASCII_RULES)) {
116         LogError("libidn error");
117         m_parseFailed = true;
118         free(output);
119         return;
120     }
121     m_host = output;
122     free(output);
123
124     // Step 10
125     std::transform(m_host.begin(), m_host.end(), m_host.begin(), ::tolower);
126
127     // Step 11
128     if (iri->port) {
129         m_port = iri->port;
130     } else {
131         setPort();
132     }
133
134     // Step 12 - Skip it. We do not return anything.
135     // User should create geters if he need access to schema/host/port.
136 }
137
138 void WacOrigin::setPort()
139 {
140     if (SCHEME_HTTP == m_scheme) {
141         m_port = PORT_HTTP;
142         return;
143     } else if (SCHEME_HTTPS == m_scheme) {
144         m_port = PORT_HTTPS;
145         return;
146     } else {
147         LogDebug("Scheme %s is not support by WAC2.0 " << m_scheme);
148         m_parseFailed = true;
149     }
150 }
151 } // namespace ValidationCore