ruby-uriparser 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +31 -14
- data/ext/uriparser_ext/uriparser.c +290 -274
- data/lib/uriparser/version.rb +1 -1
- data/test/uriparser_test.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2812cf0db75135ec8b6beef1c4496fbf6e7e3504f59c70a7dca0f7a37874e0d
|
4
|
+
data.tar.gz: 1217caabbbefc2ad78a18150d34f03cc58fb634c04a84167e5ff32909bd74a25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f88e772b2e79912705c80d0e33d2c572930f57da9a6ee18f18390ce259fb9ddd68a125a64fcd35d195368e2ec7b283cc0ae502288fb705d82b1a6c2fa0e55abd
|
7
|
+
data.tar.gz: adb0c5cf6d7bfe0860f55f7b577bdfe5256e4f4f3ac080cc0cfde6edfa59fb5bcaaee35d95cdc2aaeaebc4a40ea0fad067af70a7f16a37fe5b57ed33e7e6ad4a
|
data/README.md
CHANGED
@@ -10,29 +10,46 @@ It is a fairly reasonable hypothesis to consider that __all__ Ruby Rack applicat
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
-
|
13
|
+
```ruby
|
14
|
+
require 'uriparser'
|
14
15
|
|
15
|
-
|
16
|
+
UriParser.parse('https://localhost:9000/path/?a=1#x') # return UriParser::URI object
|
17
|
+
```
|
16
18
|
|
17
19
|
If you want to override the URI class just include the following line in your code:
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
```ruby
|
22
|
+
# Will override URI#parse and Kernel#URI methods
|
23
|
+
require 'uriparser/uri_gem'
|
24
|
+
```
|
21
25
|
|
22
26
|
## Benchmark
|
23
27
|
|
24
|
-
The following numbers were computed for 100,000 `URI.parse` calls using the
|
28
|
+
The following numbers were computed for 100,000 `URI.parse` calls using the Ruby 2.6 benchmark library in a Mac OS X (10.15.6)/ 2,7 GHz Dual-Core Intel Core i5/ 16 GB 1867 MHz DDR3:
|
25
29
|
|
30
|
+
# Complex URLs
|
26
31
|
Rehearsal ------------------------------------------------
|
27
|
-
URI
|
28
|
-
UriParser 0.
|
29
|
-
Addressable
|
30
|
-
--------------------------------------- total:
|
31
|
-
|
32
|
-
|
33
|
-
URI
|
34
|
-
UriParser 0.
|
35
|
-
Addressable
|
32
|
+
URI 1.139526 0.007341 1.146867 ( 1.189431)
|
33
|
+
UriParser 0.148833 0.008094 0.156927 ( 0.163185)
|
34
|
+
Addressable 2.096471 0.008317 2.104788 ( 2.152700)
|
35
|
+
--------------------------------------- total: 3.408582sec
|
36
|
+
|
37
|
+
user system total real
|
38
|
+
URI 1.131435 0.007336 1.138771 ( 1.215291)
|
39
|
+
UriParser 0.202037 0.005046 0.207083 ( 0.259723)
|
40
|
+
Addressable 2.393879 0.019947 2.413826 ( 2.676498)
|
41
|
+
|
42
|
+
# Simple URLs
|
43
|
+
Rehearsal ------------------------------------------------
|
44
|
+
URI 0.623932 0.002970 0.626902 ( 0.641151)
|
45
|
+
UriParser 0.145798 0.005798 0.151596 ( 0.162660)
|
46
|
+
Addressable 2.222884 0.025276 2.248160 ( 2.511593)
|
47
|
+
--------------------------------------- total: 3.026658sec
|
48
|
+
|
49
|
+
user system total real
|
50
|
+
URI 0.702562 0.007480 0.710042 ( 0.763542)
|
51
|
+
UriParser 0.153022 0.004182 0.157204 ( 0.170975)
|
52
|
+
Addressable 1.879151 0.008923 1.888074 ( 1.958248)
|
36
53
|
|
37
54
|
## Installation
|
38
55
|
|
@@ -6,69 +6,75 @@
|
|
6
6
|
#define TRUE 1
|
7
7
|
typedef char bool;
|
8
8
|
|
9
|
-
#define URI_TEXT_RANGE(uri_ptr, uri_field)
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
9
|
+
#define URI_TEXT_RANGE(uri_ptr, uri_field) \
|
10
|
+
((uri_ptr->uri_field.afterLast == uri_ptr->uri_field.first) ? \
|
11
|
+
Qnil : \
|
12
|
+
rb_str_new(uri_ptr->uri_field.first, uri_ptr->uri_field.afterLast - uri_ptr->uri_field.first))
|
13
|
+
|
14
|
+
#define RB_URIPARSER_ATTR_READER(attribute, original) \
|
15
|
+
static VALUE \
|
16
|
+
rb_uriparser_get_##attribute(VALUE self) \
|
17
|
+
{ \
|
18
|
+
struct uri_data *data; \
|
19
|
+
\
|
20
|
+
/* lazy load */ \
|
21
|
+
Data_Get_Struct(self, struct uri_data, data); \
|
22
|
+
if(RB_TYPE_P(data->attribute, T_UNDEF)) { \
|
23
|
+
if(data->uri) { \
|
24
|
+
data->attribute = URI_TEXT_RANGE(data->uri, original); \
|
25
|
+
} \
|
26
|
+
else { \
|
27
|
+
data->attribute = Qnil; \
|
28
|
+
} \
|
29
|
+
} \
|
30
|
+
\
|
31
|
+
return data->attribute; \
|
29
32
|
}
|
30
33
|
|
31
|
-
#define RB_URIPARSER_ATTR_WRITER(attribute)
|
32
|
-
static VALUE
|
33
|
-
rb_uriparser_set_##attribute(VALUE self, VALUE attribute)
|
34
|
-
{
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
34
|
+
#define RB_URIPARSER_ATTR_WRITER(attribute) \
|
35
|
+
static VALUE \
|
36
|
+
rb_uriparser_set_##attribute(VALUE self, VALUE attribute) \
|
37
|
+
{ \
|
38
|
+
VALUE old; \
|
39
|
+
struct uri_data *data; \
|
40
|
+
\
|
41
|
+
Data_Get_Struct(self, struct uri_data, data); \
|
42
|
+
old = data->attribute; \
|
43
|
+
if(NIL_P(attribute)) { \
|
44
|
+
data->attribute = Qnil; \
|
45
|
+
} \
|
46
|
+
else { \
|
47
|
+
data->attribute = StringValue(attribute); \
|
48
|
+
} \
|
49
|
+
data->updated = TRUE; \
|
50
|
+
rb_gc_mark(old); \
|
51
|
+
\
|
52
|
+
return data->attribute; \
|
53
|
+
}
|
54
|
+
|
55
|
+
#define RB_URIPARSER_ATTR_ACCESSOR(attribute, original) \
|
56
|
+
RB_URIPARSER_ATTR_READER(attribute, original); \
|
53
57
|
RB_URIPARSER_ATTR_WRITER(attribute);
|
54
58
|
|
55
59
|
#define VALID_TYPE(v) (!(NIL_P(v) || RB_TYPE_P(v, T_UNDEF)))
|
56
60
|
|
57
61
|
/* Parser structure reused across requests */
|
58
|
-
static UriParserStateA uri_parse_state;
|
62
|
+
static UriParserStateA uri_parse_state;
|
59
63
|
static VALUE rb_mUriParser;
|
60
64
|
static VALUE rb_cUri_Class;
|
65
|
+
static VALUE rb_eError;
|
66
|
+
static VALUE rb_eUriInvalidURIError;
|
61
67
|
|
62
68
|
struct uri_data {
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
69
|
+
UriUriA *uri; /* Parsed URI data */
|
70
|
+
bool updated; /* flag if any field was updated */
|
71
|
+
VALUE scheme;
|
72
|
+
VALUE userinfo;
|
73
|
+
VALUE host;
|
74
|
+
VALUE str_port;
|
75
|
+
VALUE path;
|
76
|
+
VALUE query;
|
77
|
+
VALUE fragment;
|
72
78
|
};
|
73
79
|
|
74
80
|
/* Helper methods prototypes */
|
@@ -82,70 +88,71 @@ static UriUriA* update_uri(VALUE);
|
|
82
88
|
static void
|
83
89
|
rb_uriparser_mark(void *p)
|
84
90
|
{
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
91
|
+
struct uri_data *ptr = p;
|
92
|
+
|
93
|
+
if( ptr ) {
|
94
|
+
reset_fields(ptr);
|
95
|
+
}
|
90
96
|
}
|
91
97
|
|
92
|
-
static void
|
98
|
+
static void
|
93
99
|
rb_uriparser_free(void *p)
|
94
100
|
{
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
+
struct uri_data *ptr = p;
|
102
|
+
|
103
|
+
if( ptr ) {
|
104
|
+
free_uri(ptr->uri);
|
105
|
+
xfree(ptr);
|
106
|
+
}
|
101
107
|
}
|
102
108
|
|
103
109
|
static VALUE
|
104
110
|
rb_uriparser_s_allocate(VALUE klass)
|
105
111
|
{
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
112
|
+
struct uri_data *data = ALLOC(struct uri_data);
|
113
|
+
|
114
|
+
if( data ) {
|
115
|
+
data->uri = NULL;
|
116
|
+
data->updated = FALSE;
|
117
|
+
data->scheme = Qundef;
|
118
|
+
data->userinfo = Qundef;
|
119
|
+
data->host = Qundef;
|
120
|
+
data->str_port = Qundef;
|
121
|
+
data->path = Qundef;
|
122
|
+
data->query = Qundef;
|
123
|
+
data->fragment = Qundef;
|
124
|
+
}
|
125
|
+
else {
|
126
|
+
rb_raise(rb_eRuntimeError, "unable to create UriParser::URI class");
|
127
|
+
}
|
128
|
+
|
129
|
+
return Data_Wrap_Struct(klass, rb_uriparser_mark, rb_uriparser_free, data);
|
123
130
|
}
|
124
131
|
|
125
132
|
static VALUE
|
126
133
|
rb_uriparser_s_parse(VALUE klass, VALUE uri_obj)
|
127
134
|
{
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
135
|
+
char *str_uri = StringValueCStr(uri_obj);
|
136
|
+
UriUriA *uri = ALLOC(UriUriA);
|
137
|
+
struct uri_data *data;
|
138
|
+
VALUE generic_uri;
|
132
139
|
|
133
|
-
|
134
|
-
|
140
|
+
generic_uri = rb_class_new_instance(0, NULL, rb_cUri_Class);
|
141
|
+
Data_Get_Struct(generic_uri, struct uri_data, data);
|
135
142
|
|
136
|
-
|
143
|
+
data->uri = uri;
|
144
|
+
|
145
|
+
if( parse_uri(str_uri, uri) != URI_SUCCESS ) {
|
146
|
+
rb_raise(rb_eUriInvalidURIError, "unable to parse the URI: %s", str_uri);
|
147
|
+
}
|
137
148
|
|
138
|
-
|
139
|
-
rb_raise(rb_eStandardError, "unable to parse the URI: %s", str_uri);
|
140
|
-
}
|
141
|
-
|
142
|
-
return generic_uri;
|
149
|
+
return generic_uri;
|
143
150
|
}
|
144
151
|
|
145
152
|
static VALUE
|
146
|
-
rb_uriparser_initialize(VALUE self)
|
153
|
+
rb_uriparser_initialize(VALUE self)
|
147
154
|
{
|
148
|
-
|
155
|
+
return self;
|
149
156
|
}
|
150
157
|
|
151
158
|
RB_URIPARSER_ATTR_ACCESSOR(scheme, scheme);
|
@@ -159,237 +166,246 @@ RB_URIPARSER_ATTR_WRITER(path);
|
|
159
166
|
static VALUE
|
160
167
|
rb_uriparser_get_path(VALUE self)
|
161
168
|
{
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
169
|
+
struct uri_data *data;
|
170
|
+
|
171
|
+
/* lazy load */
|
172
|
+
Data_Get_Struct(self, struct uri_data, data);
|
173
|
+
if( RB_TYPE_P(data->path, T_UNDEF) ) {
|
174
|
+
if( data->uri ) {
|
175
|
+
if( data->uri->pathHead ) {
|
176
|
+
/* starts with slash */
|
177
|
+
UriPathSegmentA *path_segment = data->uri->pathHead;
|
178
|
+
data->path = rb_str_new("/", 1);
|
179
|
+
do { /* go through the linked list */
|
180
|
+
rb_str_cat(
|
181
|
+
data->path, path_segment->text.first,
|
182
|
+
path_segment->text.afterLast
|
183
|
+
- path_segment->text.first
|
184
|
+
+ (*path_segment->text.afterLast == '/')
|
185
|
+
); /* check if there is a slash to add */
|
186
|
+
path_segment = path_segment->next;
|
187
|
+
} while( path_segment );
|
188
|
+
}
|
189
|
+
else {
|
190
|
+
data->path = rb_str_new("", 0);
|
191
|
+
}
|
192
|
+
}
|
193
|
+
else {
|
194
|
+
data->path = Qnil;
|
195
|
+
}
|
183
196
|
}
|
184
|
-
}
|
185
197
|
|
186
|
-
|
198
|
+
return data->path;
|
187
199
|
}
|
188
200
|
|
189
201
|
/* TODO: Include option mask */
|
190
202
|
static VALUE
|
191
203
|
rb_uriparser_normalize_bang(VALUE self)
|
192
204
|
{
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
+
struct uri_data *data;
|
206
|
+
|
207
|
+
Data_Get_Struct(self, struct uri_data, data);
|
208
|
+
update_uri(self);
|
209
|
+
|
210
|
+
if( uriNormalizeSyntaxA(data->uri) != URI_SUCCESS ) {
|
211
|
+
rb_raise(rb_eError, "unable to normalize the URI");
|
212
|
+
}
|
213
|
+
/* Invalidate any previous field value */
|
214
|
+
reset_fields(data);
|
215
|
+
|
216
|
+
return self;
|
205
217
|
}
|
206
218
|
|
207
219
|
static VALUE
|
208
220
|
rb_uriparser_escape(VALUE self)
|
209
221
|
{
|
210
|
-
|
222
|
+
return Qnil;
|
211
223
|
}
|
212
224
|
|
213
225
|
static VALUE
|
214
226
|
rb_uriparser_unescape(VALUE self)
|
215
227
|
{
|
216
|
-
|
228
|
+
return Qnil;
|
217
229
|
}
|
218
230
|
|
219
231
|
static VALUE
|
220
232
|
rb_uriparser_to_s(VALUE self)
|
221
233
|
{
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
234
|
+
int chars_required;
|
235
|
+
char *str_uri;
|
236
|
+
UriUriA *uri = update_uri(self);
|
237
|
+
VALUE obj_str_uri;
|
238
|
+
|
239
|
+
if(
|
240
|
+
uriToStringCharsRequiredA(uri, &chars_required) != URI_SUCCESS
|
241
|
+
|| !(str_uri = ALLOC_N(char, ++chars_required))
|
242
|
+
|| uriToStringA(str_uri, uri, chars_required, NULL) != URI_SUCCESS
|
243
|
+
) {
|
244
|
+
rb_raise(rb_eError, "unable to convert to string");
|
245
|
+
}
|
246
|
+
|
247
|
+
obj_str_uri = rb_str_new2(str_uri);
|
248
|
+
xfree(str_uri);
|
249
|
+
return obj_str_uri;
|
236
250
|
}
|
237
251
|
|
238
252
|
static void
|
239
|
-
reset_fields(struct uri_data *data)
|
253
|
+
reset_fields(struct uri_data *data)
|
240
254
|
{
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
255
|
+
data->updated = FALSE;
|
256
|
+
|
257
|
+
rb_gc_mark(data->scheme);
|
258
|
+
data->scheme = Qundef;
|
259
|
+
|
260
|
+
rb_gc_mark(data->userinfo);
|
261
|
+
data->userinfo = Qundef;
|
262
|
+
|
263
|
+
rb_gc_mark(data->host);
|
264
|
+
data->host = Qundef;
|
265
|
+
|
266
|
+
rb_gc_mark(data->str_port);
|
267
|
+
data->str_port = Qundef;
|
268
|
+
|
269
|
+
rb_gc_mark(data->path);
|
270
|
+
data->path = Qundef;
|
271
|
+
|
272
|
+
rb_gc_mark(data->query);
|
273
|
+
data->query = Qundef;
|
274
|
+
|
275
|
+
rb_gc_mark(data->fragment);
|
276
|
+
data->fragment = Qundef;
|
263
277
|
}
|
264
278
|
|
265
279
|
static void
|
266
|
-
populate_fields(VALUE uri)
|
280
|
+
populate_fields(VALUE uri)
|
267
281
|
{
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
282
|
+
rb_uriparser_get_scheme(uri);
|
283
|
+
rb_uriparser_get_userinfo(uri);
|
284
|
+
rb_uriparser_get_host(uri);
|
285
|
+
rb_uriparser_get_str_port(uri);
|
286
|
+
rb_uriparser_get_path(uri);
|
287
|
+
rb_uriparser_get_query(uri);
|
288
|
+
rb_uriparser_get_fragment(uri);
|
275
289
|
}
|
276
290
|
|
277
291
|
static VALUE
|
278
|
-
compose_uri_from_data(struct uri_data *data)
|
292
|
+
compose_uri_from_data(struct uri_data *data)
|
279
293
|
{
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
294
|
+
VALUE str_uri = rb_str_new2("");
|
295
|
+
|
296
|
+
if( VALID_TYPE(data->scheme) ) {
|
297
|
+
rb_str_cat2(str_uri, StringValueCStr(data->scheme));
|
298
|
+
rb_str_cat2(str_uri, "://");
|
299
|
+
}
|
300
|
+
|
301
|
+
if( VALID_TYPE(data->userinfo) ) {
|
302
|
+
rb_str_cat2(str_uri, StringValueCStr(data->userinfo));
|
303
|
+
rb_str_cat2(str_uri, "@");
|
304
|
+
}
|
305
|
+
|
306
|
+
if( VALID_TYPE(data->host) ) {
|
307
|
+
rb_str_cat2(str_uri, StringValueCStr(data->host));
|
308
|
+
}
|
309
|
+
|
310
|
+
if( VALID_TYPE(data->str_port) ) {
|
311
|
+
rb_str_cat2(str_uri, ":");
|
312
|
+
rb_str_cat2(str_uri, StringValueCStr(data->str_port));
|
313
|
+
}
|
314
|
+
|
315
|
+
if( VALID_TYPE(data->path) ) {
|
316
|
+
rb_str_cat2(str_uri, StringValueCStr(data->path));
|
317
|
+
}
|
318
|
+
|
319
|
+
if( VALID_TYPE(data->query) ) {
|
320
|
+
rb_str_cat2(str_uri, "?");
|
321
|
+
rb_str_cat2(str_uri, StringValueCStr(data->query));
|
322
|
+
}
|
323
|
+
|
324
|
+
if( VALID_TYPE(data->fragment) ) {
|
325
|
+
rb_str_cat2(str_uri, "#");
|
326
|
+
rb_str_cat2(str_uri, StringValueCStr(data->fragment));
|
327
|
+
}
|
328
|
+
|
329
|
+
return str_uri;
|
316
330
|
}
|
317
331
|
|
318
332
|
static int
|
319
|
-
parse_uri(const char *str_uri, UriUriA *uri)
|
333
|
+
parse_uri(const char *str_uri, UriUriA *uri)
|
320
334
|
{
|
321
|
-
|
322
|
-
|
323
|
-
|
335
|
+
uri_parse_state.uri = uri;
|
336
|
+
|
337
|
+
return uriParseUriA(&uri_parse_state, str_uri);
|
324
338
|
}
|
325
339
|
|
326
|
-
static void
|
340
|
+
static void
|
327
341
|
free_uri(UriUriA *uri)
|
328
342
|
{
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
343
|
+
if( uri ) {
|
344
|
+
uriFreeUriMembersA(uri);
|
345
|
+
xfree(uri);
|
346
|
+
}
|
333
347
|
}
|
334
348
|
|
335
349
|
static UriUriA *
|
336
350
|
update_uri(VALUE uri_obj)
|
337
351
|
{
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
352
|
+
struct uri_data *data;
|
353
|
+
|
354
|
+
Data_Get_Struct(uri_obj, struct uri_data, data);
|
355
|
+
if( !data->uri || data->updated ) {
|
356
|
+
VALUE new_uri;
|
357
|
+
UriUriA *uri = ALLOC(UriUriA);
|
358
|
+
|
359
|
+
if( data->updated ) {
|
360
|
+
populate_fields(uri_obj);
|
361
|
+
}
|
362
|
+
/* Compute and parse the new URI */
|
363
|
+
new_uri = compose_uri_from_data(data);
|
364
|
+
|
365
|
+
if( parse_uri(StringValueCStr(new_uri), uri) != URI_SUCCESS ) {
|
366
|
+
free_uri(uri);
|
367
|
+
rb_gc_mark(new_uri);
|
368
|
+
rb_raise(rb_eUriInvalidURIError, "invalid URI (%s) to normalize", StringValueCStr(new_uri));
|
369
|
+
}
|
370
|
+
|
371
|
+
free_uri(data->uri);
|
372
|
+
rb_gc_mark(new_uri);
|
373
|
+
data->uri = uri;
|
347
374
|
}
|
348
|
-
/* Compute and parse the new URI */
|
349
|
-
new_uri = compose_uri_from_data(data);
|
350
375
|
|
351
|
-
|
352
|
-
free_uri(uri);
|
353
|
-
rb_gc_mark(new_uri);
|
354
|
-
rb_raise(rb_eStandardError, "invalid URI (%s) to normalize", StringValueCStr(new_uri));
|
355
|
-
}
|
356
|
-
|
357
|
-
free_uri(data->uri);
|
358
|
-
rb_gc_mark(new_uri);
|
359
|
-
data->uri = uri;
|
360
|
-
}
|
361
|
-
|
362
|
-
return data->uri;
|
376
|
+
return data->uri;
|
363
377
|
}
|
364
378
|
|
365
|
-
|
366
379
|
void
|
367
380
|
Init_uriparser_ext()
|
368
381
|
{
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
382
|
+
rb_mUriParser = rb_define_module("UriParser");
|
383
|
+
rb_cUri_Class = rb_define_class_under(rb_mUriParser, "URI", rb_cObject);
|
384
|
+
/* NOTE: Reuse URI exceptions */
|
385
|
+
rb_eError = rb_path2class("URI::Error");
|
386
|
+
rb_eUriInvalidURIError = rb_path2class("URI::InvalidURIError");
|
387
|
+
|
388
|
+
rb_define_alloc_func(rb_cUri_Class, rb_uriparser_s_allocate);
|
389
|
+
rb_define_method(rb_cUri_Class, "initialize", rb_uriparser_initialize, 0);
|
390
|
+
rb_define_method(rb_cUri_Class, "scheme", rb_uriparser_get_scheme, 0);
|
391
|
+
rb_define_method(rb_cUri_Class, "scheme=", rb_uriparser_set_scheme, 1);
|
392
|
+
rb_define_method(rb_cUri_Class, "userinfo", rb_uriparser_get_userinfo, 0);
|
393
|
+
rb_define_method(rb_cUri_Class, "userinfo=", rb_uriparser_set_userinfo, 1);
|
394
|
+
rb_define_method(rb_cUri_Class, "host", rb_uriparser_get_host, 0);
|
395
|
+
rb_define_method(rb_cUri_Class, "host=", rb_uriparser_set_host, 1);
|
396
|
+
rb_define_method(rb_cUri_Class, "str_port", rb_uriparser_get_str_port, 0);
|
397
|
+
rb_define_method(rb_cUri_Class, "str_port=", rb_uriparser_set_str_port, 1);
|
398
|
+
rb_define_method(rb_cUri_Class, "path", rb_uriparser_get_path, 0);
|
399
|
+
rb_define_method(rb_cUri_Class, "path=", rb_uriparser_set_path, 1);
|
400
|
+
rb_define_method(rb_cUri_Class, "query", rb_uriparser_get_query, 0);
|
401
|
+
rb_define_method(rb_cUri_Class, "query=", rb_uriparser_set_query, 1);
|
402
|
+
rb_define_method(rb_cUri_Class, "fragment", rb_uriparser_get_fragment, 0);
|
403
|
+
rb_define_method(rb_cUri_Class, "fragment=", rb_uriparser_set_fragment, 1);
|
404
|
+
|
405
|
+
rb_define_method(rb_cUri_Class, "normalize!", rb_uriparser_normalize_bang, 0);
|
406
|
+
rb_define_method(rb_cUri_Class, "escape", rb_uriparser_escape, 0);
|
407
|
+
rb_define_method(rb_cUri_Class, "unescape", rb_uriparser_unescape, 0);
|
408
|
+
rb_define_method(rb_cUri_Class, "to_s", rb_uriparser_to_s, 0);
|
409
|
+
|
410
|
+
rb_define_singleton_method(rb_mUriParser, "parse", rb_uriparser_s_parse, 1);
|
411
|
+
}
|
data/lib/uriparser/version.rb
CHANGED
data/test/uriparser_test.rb
CHANGED
@@ -22,7 +22,7 @@ class UriParserTest < Minitest::Test # rubocop:disable Metrics/ClassLength
|
|
22
22
|
|
23
23
|
context 'URI parsing' do
|
24
24
|
should 'raise an exception if was unable to parse the URI' do
|
25
|
-
assert_raises(
|
25
|
+
assert_raises(URI::InvalidURIError) { UriParser.parse('invalid uri') }
|
26
26
|
end
|
27
27
|
|
28
28
|
should 'parse valid URI' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-uriparser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thiago Lewin
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-09-
|
11
|
+
date: 2020-09-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake-compiler
|
@@ -81,7 +81,7 @@ homepage: https://github.com/tlewin/ruby-uriparser
|
|
81
81
|
licenses:
|
82
82
|
- MIT
|
83
83
|
metadata: {}
|
84
|
-
post_install_message:
|
84
|
+
post_install_message:
|
85
85
|
rdoc_options: []
|
86
86
|
require_paths:
|
87
87
|
- lib
|
@@ -97,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
version: '0'
|
98
98
|
requirements: []
|
99
99
|
rubygems_version: 3.0.1
|
100
|
-
signing_key:
|
100
|
+
signing_key:
|
101
101
|
specification_version: 4
|
102
102
|
summary: Ruby wrapper for uriparser C library
|
103
103
|
test_files:
|