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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04a0cd98ff9826b233dbc567276e1dad5485c0068ac48c5d6809929876b05c73
4
- data.tar.gz: '08cb5e19c101d70dfd1a34a189be595829b03de8ed150fbaadc7edeb36ae4504'
3
+ metadata.gz: f2812cf0db75135ec8b6beef1c4496fbf6e7e3504f59c70a7dca0f7a37874e0d
4
+ data.tar.gz: 1217caabbbefc2ad78a18150d34f03cc58fb634c04a84167e5ff32909bd74a25
5
5
  SHA512:
6
- metadata.gz: fdc713b945f0874b05cf622941387ff0f90f74fd6f9a84b938f334f31e15ccfa7cb899afd9a8b1f3cbf8afb03474a7e40a959bbdf17a674b48b3356e3a11c4a9
7
- data.tar.gz: ad799965b0b8bf958255b0a4be711e7f40f0a527d424a5d00c637220e4a4431ac6c199bc57346d61cb09f3b45d8fc4e56aa7788d53ba8ffc579ae23f84657ef7
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
- require 'uriparser'
13
+ ```ruby
14
+ require 'uriparser'
14
15
 
15
- UriParser.parse('https://localhost:9000/path/?a=1#x') # return UriParser::URI object
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
- # Will override URI#parse and Kernel#URI methods
20
- require 'uriparser/uri_gem'
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 ruby benchmark library in a Mac OS X (10.8.5)/ 2.9Ghz Intel Core I5/ 8GB 1600 MHz DDR3 machine:
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 0.750000 0.000000 0.750000 ( 0.750619)
28
- UriParser 0.110000 0.010000 0.120000 ( 0.111885)
29
- Addressable 1.880000 0.000000 1.880000 ( 1.886091)
30
- --------------------------------------- total: 2.750000sec
31
-
32
- user system total real
33
- URI 0.730000 0.000000 0.730000 ( 0.728169)
34
- UriParser 0.110000 0.000000 0.110000 ( 0.107235)
35
- Addressable 1.870000 0.000000 1.870000 ( 1.863334)
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) ((uri_ptr->uri_field.afterLast == uri_ptr->uri_field.first) ? Qnil : \
10
- rb_str_new(uri_ptr->uri_field.first, uri_ptr->uri_field.afterLast - uri_ptr->uri_field.first))
11
-
12
- #define RB_URIPARSER_ATTR_READER(attribute, original) \
13
- static VALUE \
14
- rb_uriparser_get_##attribute(VALUE self) \
15
- { \
16
- struct uri_data *data; \
17
- \
18
- /* lazy load */ \
19
- Data_Get_Struct(self, struct uri_data, data); \
20
- if(RB_TYPE_P(data->attribute, T_UNDEF)) { \
21
- if(data->uri) { \
22
- data->attribute = URI_TEXT_RANGE(data->uri, original); \
23
- } else { \
24
- data->attribute = Qnil; \
25
- } \
26
- } \
27
- \
28
- return data->attribute; \
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
- VALUE old; \
36
- struct uri_data *data; \
37
- \
38
- Data_Get_Struct(self, struct uri_data, data); \
39
- old = data->attribute; \
40
- if(NIL_P(attribute)) { \
41
- data->attribute = Qnil; \
42
- } else { \
43
- data->attribute = StringValue(attribute); \
44
- } \
45
- data->updated = TRUE; \
46
- rb_gc_mark(old); \
47
- \
48
- return data->attribute; \
49
- }
50
-
51
- #define RB_URIPARSER_ATTR_ACCESSOR(attribute, original) \
52
- RB_URIPARSER_ATTR_READER(attribute, original); \
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
- UriUriA *uri; /* Parsed URI data */
64
- bool updated; /* flag if any field was updated */
65
- VALUE scheme;
66
- VALUE userinfo;
67
- VALUE host;
68
- VALUE str_port;
69
- VALUE path;
70
- VALUE query;
71
- VALUE fragment;
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
- struct uri_data *ptr = p;
86
-
87
- if (ptr) {
88
- reset_fields(ptr);
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
- struct uri_data *ptr = p;
96
-
97
- if(ptr) {
98
- free_uri(ptr->uri);
99
- xfree(ptr);
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
- struct uri_data *data = ALLOC(struct uri_data);
107
-
108
- if(data) {
109
- data->uri = NULL;
110
- data->updated = FALSE;
111
- data->scheme = Qundef;
112
- data->userinfo = Qundef;
113
- data->host = Qundef;
114
- data->str_port = Qundef;
115
- data->path = Qundef;
116
- data->query = Qundef;
117
- data->fragment = Qundef;
118
- } else {
119
- rb_raise(rb_eRuntimeError, "unable to create UriParser::URI class");
120
- }
121
-
122
- return Data_Wrap_Struct(klass, rb_uriparser_mark, rb_uriparser_free, data);
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
- char *str_uri = StringValueCStr(uri_obj);
129
- UriUriA *uri = ALLOC(UriUriA);
130
- struct uri_data *data;
131
- VALUE generic_uri;
135
+ char *str_uri = StringValueCStr(uri_obj);
136
+ UriUriA *uri = ALLOC(UriUriA);
137
+ struct uri_data *data;
138
+ VALUE generic_uri;
132
139
 
133
- generic_uri = rb_class_new_instance(0, NULL, rb_cUri_Class);
134
- Data_Get_Struct(generic_uri, struct uri_data, data);
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
- data->uri = uri;
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
- if(parse_uri(str_uri, uri) != URI_SUCCESS) {
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
- return self;
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
- struct uri_data *data;
163
-
164
- /* lazy load */
165
- Data_Get_Struct(self, struct uri_data, data);
166
- if(RB_TYPE_P(data->path, T_UNDEF)) {
167
- if(data->uri) {
168
- if(data->uri->pathHead) {
169
- /* starts with slash */
170
- UriPathSegmentA *path_segment = data->uri->pathHead;
171
- data->path = rb_str_new("/", 1);
172
- do { /* go through the linked list */
173
- rb_str_cat(data->path, path_segment->text.first,
174
- path_segment->text.afterLast - path_segment->text.first +
175
- (*path_segment->text.afterLast == '/')); /* check if there is a slash to add */
176
- path_segment = path_segment->next;
177
- } while(path_segment);
178
- } else {
179
- data->path = rb_str_new("", 0);
180
- }
181
- } else {
182
- data->path = Qnil;
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
- return data->path;
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
- struct uri_data *data;
194
-
195
- Data_Get_Struct(self, struct uri_data, data);
196
- update_uri(self);
197
-
198
- if(uriNormalizeSyntaxA(data->uri) != URI_SUCCESS) {
199
- rb_raise(rb_eStandardError, "unable to normalize the URI");
200
- }
201
- /* Invalidate any previous field value */
202
- reset_fields(data);
203
-
204
- return self;
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
- return Qnil;
222
+ return Qnil;
211
223
  }
212
224
 
213
225
  static VALUE
214
226
  rb_uriparser_unescape(VALUE self)
215
227
  {
216
- return Qnil;
228
+ return Qnil;
217
229
  }
218
230
 
219
231
  static VALUE
220
232
  rb_uriparser_to_s(VALUE self)
221
233
  {
222
- int chars_required;
223
- char *str_uri;
224
- UriUriA *uri = update_uri(self);
225
- VALUE obj_str_uri;
226
-
227
- if(uriToStringCharsRequiredA(uri, &chars_required) != URI_SUCCESS ||
228
- !(str_uri = ALLOC_N(char, ++chars_required)) ||
229
- uriToStringA(str_uri, uri, chars_required, NULL) != URI_SUCCESS) {
230
- rb_raise(rb_eStandardError, "unable to convert to string");
231
- }
232
-
233
- obj_str_uri = rb_str_new2(str_uri);
234
- xfree(str_uri);
235
- return obj_str_uri;
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
- data->updated = FALSE;
242
-
243
- rb_gc_mark(data->scheme);
244
- data->scheme = Qundef;
245
-
246
- rb_gc_mark(data->userinfo);
247
- data->userinfo = Qundef;
248
-
249
- rb_gc_mark(data->host);
250
- data->host = Qundef;
251
-
252
- rb_gc_mark(data->str_port);
253
- data->str_port = Qundef;
254
-
255
- rb_gc_mark(data->path);
256
- data->path = Qundef;
257
-
258
- rb_gc_mark(data->query);
259
- data->query = Qundef;
260
-
261
- rb_gc_mark(data->fragment);
262
- data->fragment = Qundef;
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
- rb_uriparser_get_scheme(uri);
269
- rb_uriparser_get_userinfo(uri);
270
- rb_uriparser_get_host(uri);
271
- rb_uriparser_get_str_port(uri);
272
- rb_uriparser_get_path(uri);
273
- rb_uriparser_get_query(uri);
274
- rb_uriparser_get_fragment(uri);
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
- VALUE str_uri = rb_str_new2("");
281
-
282
- if(VALID_TYPE(data->scheme)) {
283
- rb_str_cat2(str_uri, StringValueCStr(data->scheme));
284
- rb_str_cat2(str_uri, "://");
285
- }
286
-
287
- if(VALID_TYPE(data->userinfo)) {
288
- rb_str_cat2(str_uri, StringValueCStr(data->userinfo));
289
- rb_str_cat2(str_uri, "@");
290
- }
291
-
292
- if(VALID_TYPE(data->host)) {
293
- rb_str_cat2(str_uri, StringValueCStr(data->host));
294
- }
295
-
296
- if(VALID_TYPE(data->str_port)) {
297
- rb_str_cat2(str_uri, ":");
298
- rb_str_cat2(str_uri, StringValueCStr(data->str_port));
299
- }
300
-
301
- if(VALID_TYPE(data->path)) {
302
- rb_str_cat2(str_uri, StringValueCStr(data->path));
303
- }
304
-
305
- if(VALID_TYPE(data->query)) {
306
- rb_str_cat2(str_uri, "?");
307
- rb_str_cat2(str_uri, StringValueCStr(data->query));
308
- }
309
-
310
- if(VALID_TYPE(data->fragment)) {
311
- rb_str_cat2(str_uri, "#");
312
- rb_str_cat2(str_uri, StringValueCStr(data->fragment));
313
- }
314
-
315
- return str_uri;
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
- uri_parse_state.uri = uri;
322
-
323
- return uriParseUriA(&uri_parse_state, str_uri);
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
- if(uri) {
330
- uriFreeUriMembersA(uri);
331
- xfree(uri);
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
- struct uri_data *data;
339
-
340
- Data_Get_Struct(uri_obj, struct uri_data, data);
341
- if(!data->uri || data->updated) {
342
- VALUE new_uri;
343
- UriUriA *uri = ALLOC(UriUriA);
344
-
345
- if(data->updated) {
346
- populate_fields(uri_obj);
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
- if(parse_uri(StringValueCStr(new_uri), uri) != URI_SUCCESS) {
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
- rb_mUriParser = rb_define_module("UriParser");
370
- rb_cUri_Class = rb_define_class_under(rb_mUriParser, "URI", rb_cObject);
371
-
372
- rb_define_alloc_func(rb_cUri_Class, rb_uriparser_s_allocate);
373
- rb_define_method(rb_cUri_Class, "initialize", rb_uriparser_initialize, 0);
374
- rb_define_method(rb_cUri_Class, "scheme", rb_uriparser_get_scheme, 0);
375
- rb_define_method(rb_cUri_Class, "scheme=", rb_uriparser_set_scheme, 1);
376
- rb_define_method(rb_cUri_Class, "userinfo", rb_uriparser_get_userinfo, 0);
377
- rb_define_method(rb_cUri_Class, "userinfo=", rb_uriparser_set_userinfo, 1);
378
- rb_define_method(rb_cUri_Class, "host", rb_uriparser_get_host, 0);
379
- rb_define_method(rb_cUri_Class, "host=", rb_uriparser_set_host, 1);
380
- rb_define_method(rb_cUri_Class, "str_port", rb_uriparser_get_str_port, 0);
381
- rb_define_method(rb_cUri_Class, "str_port=", rb_uriparser_set_str_port, 1);
382
- rb_define_method(rb_cUri_Class, "path", rb_uriparser_get_path, 0);
383
- rb_define_method(rb_cUri_Class, "path=", rb_uriparser_set_path, 1);
384
- rb_define_method(rb_cUri_Class, "query", rb_uriparser_get_query, 0);
385
- rb_define_method(rb_cUri_Class, "query=", rb_uriparser_set_query, 1);
386
- rb_define_method(rb_cUri_Class, "fragment", rb_uriparser_get_fragment, 0);
387
- rb_define_method(rb_cUri_Class, "fragment=", rb_uriparser_set_fragment, 1);
388
-
389
- rb_define_method(rb_cUri_Class, "normalize!", rb_uriparser_normalize_bang, 0);
390
- rb_define_method(rb_cUri_Class, "escape", rb_uriparser_escape, 0);
391
- rb_define_method(rb_cUri_Class, "unescape", rb_uriparser_unescape, 0);
392
- rb_define_method(rb_cUri_Class, "to_s", rb_uriparser_to_s, 0);
393
-
394
- rb_define_singleton_method(rb_mUriParser, "parse", rb_uriparser_s_parse, 1);
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
+ }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UriParser
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
@@ -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(StandardError) { UriParser.parse('invalid uri') }
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.0
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-26 00:00:00.000000000 Z
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: