-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy pathuri_parser_php_parse_url.c
More file actions
184 lines (149 loc) · 5.45 KB
/
uri_parser_php_parse_url.c
File metadata and controls
184 lines (149 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Máté Kocsis <kocsismate@php.net> |
| Tim Düsterhus <timwolla@php.net> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "uri_parser_php_parse_url.h"
#include "php_uri_common.h"
#include "Zend/zend_exceptions.h"
#include "ext/standard/url.h"
static zend_string *decode_component(zend_string *in, php_uri_component_read_mode read_mode)
{
switch (read_mode) {
case PHP_URI_COMPONENT_READ_MODE_RAW:
return zend_string_copy(in);
case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_ASCII:
case PHP_URI_COMPONENT_READ_MODE_NORMALIZED_UNICODE: {
zend_string *out = zend_string_alloc(ZSTR_LEN(in), false);
ZSTR_LEN(out) = php_raw_url_decode_ex(ZSTR_VAL(out), ZSTR_VAL(in), ZSTR_LEN(in));
return out;
}
EMPTY_SWITCH_DEFAULT_CASE();
}
}
static zend_result uri_parser_php_parse_url_scheme_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->scheme) {
ZVAL_STR(retval, decode_component(parse_url_uri->scheme, read_mode));
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static zend_result uri_parser_php_parse_url_username_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->user) {
ZVAL_STR(retval, decode_component(parse_url_uri->user, read_mode));
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static zend_result uri_parser_php_parse_url_password_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->pass) {
ZVAL_STR(retval, decode_component(parse_url_uri->pass, read_mode));
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static zend_result uri_parser_php_parse_url_host_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->host) {
ZVAL_STR(retval, decode_component(parse_url_uri->host, read_mode));
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static zend_result uri_parser_php_parse_url_port_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->port) {
ZVAL_LONG(retval, parse_url_uri->port);
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static zend_result uri_parser_php_parse_url_path_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->path) {
ZVAL_STR(retval, decode_component(parse_url_uri->path, read_mode));
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static zend_result uri_parser_php_parse_url_query_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->query) {
ZVAL_STR(retval, decode_component(parse_url_uri->query, read_mode));
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static zend_result uri_parser_php_parse_url_fragment_read(void *uri, php_uri_component_read_mode read_mode, zval *retval)
{
const php_url *parse_url_uri = uri;
if (parse_url_uri->fragment) {
ZVAL_STR(retval, decode_component(parse_url_uri->fragment, read_mode));
} else {
ZVAL_NULL(retval);
}
return SUCCESS;
}
static void *uri_parser_php_parse_url_parse(const char *uri_str, size_t uri_str_len, const void *base_url, zval *errors, bool silent)
{
bool has_port;
php_url *url = php_url_parse_ex2(uri_str, uri_str_len, &has_port);
if (url == NULL && !silent) {
zend_throw_exception(php_uri_ce_invalid_uri_exception, "The specified URI is malformed", 0);
}
return url;
}
static void uri_parser_php_parse_url_destroy(void *uri)
{
php_url *parse_url_uri = uri;
if (UNEXPECTED(parse_url_uri == NULL)) {
return;
}
php_url_free(parse_url_uri);
}
const php_uri_parser php_uri_parser_php_parse_url = {
.name = PHP_URI_PARSER_PHP_PARSE_URL,
.parse = uri_parser_php_parse_url_parse,
.clone = NULL,
.to_string = NULL,
.destroy = uri_parser_php_parse_url_destroy,
{
.scheme = {.read = uri_parser_php_parse_url_scheme_read, .write = NULL},
.username = {.read = uri_parser_php_parse_url_username_read, .write = NULL},
.password = {.read = uri_parser_php_parse_url_password_read, .write = NULL},
.host = {.read = uri_parser_php_parse_url_host_read, .write = NULL},
.port = {.read = uri_parser_php_parse_url_port_read, .write = NULL},
.path = {.read = uri_parser_php_parse_url_path_read, .write = NULL},
.query = {.read = uri_parser_php_parse_url_query_read, .write = NULL},
.fragment = {.read = uri_parser_php_parse_url_fragment_read, .write = NULL},
}
};