Ada 2.7.8
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
ada_c.cpp
Go to the documentation of this file.
1#include "ada.h"
2
5}
6
7extern "C" {
8typedef void* ada_url;
10typedef void* ada_strings;
14
15struct ada_string {
16 const char* data;
17 size_t length;
18};
19
20struct ada_owned_string {
21 const char* data;
22 size_t length;
23};
24
25struct ada_string_pair {
28};
29
30ada_string ada_string_create(const char* data, size_t length) {
31 ada_string out{};
32 out.data = data;
33 out.length = length;
34 return out;
35}
36
37struct ada_url_components {
38 /*
39 * By using 32-bit integers, we implicitly assume that the URL string
40 * cannot exceed 4 GB.
41 *
42 * https://user:[email protected]:1234/foo/bar?baz#quux
43 * | | | | ^^^^| | |
44 * | | | | | | | `----- hash_start
45 * | | | | | | `--------- search_start
46 * | | | | | `----------------- pathname_start
47 * | | | | `--------------------- port
48 * | | | `----------------------- host_end
49 * | | `---------------------------------- host_start
50 * | `--------------------------------------- username_end
51 * `--------------------------------------------- protocol_end
52 */
53 uint32_t protocol_end;
58 uint32_t username_end;
59 uint32_t host_start;
60 uint32_t host_end;
61 uint32_t port;
62 uint32_t pathname_start;
63 uint32_t search_start;
64 uint32_t hash_start;
65};
66
67ada_url ada_parse(const char* input, size_t length) noexcept {
69 ada::parse<ada::url_aggregator>(std::string_view(input, length)));
70}
71
72ada_url ada_parse_with_base(const char* input, size_t input_length,
73 const char* base, size_t base_length) noexcept {
74 auto base_out =
75 ada::parse<ada::url_aggregator>(std::string_view(base, base_length));
76
77 if (!base_out) {
78 return new ada::result<ada::url_aggregator>(base_out);
79 }
80
82 std::string_view(input, input_length), &base_out.value()));
83}
84
85bool ada_can_parse(const char* input, size_t length) noexcept {
86 return ada::can_parse(std::string_view(input, length));
87}
88
89bool ada_can_parse_with_base(const char* input, size_t input_length,
90 const char* base, size_t base_length) noexcept {
91 std::string_view base_view(base, base_length);
92 return ada::can_parse(std::string_view(input, input_length), &base_view);
93}
94
95void ada_free(ada_url result) noexcept {
96 auto* r = (ada::result<ada::url_aggregator>*)result;
97 delete r;
98}
99
104
105bool ada_is_valid(ada_url result) noexcept {
107 return r.has_value();
108}
109
110// caller must free the result with ada_free_owned_string
113 ada_owned_string owned{};
114 if (!r) {
115 owned.data = nullptr;
116 owned.length = 0;
117 return owned;
118 }
119 std::string out = r->get_origin();
120 owned.length = out.size();
121 owned.data = new char[owned.length];
122 memcpy((void*)owned.data, out.data(), owned.length);
123 return owned;
124}
125
127 delete[] owned.data;
128 owned.data = nullptr;
129 owned.length = 0;
130}
131
134 if (!r) {
135 return ada_string_create(nullptr, 0);
136 }
137 std::string_view out = r->get_href();
138 return ada_string_create(out.data(), out.length());
139}
140
143 if (!r) {
144 return ada_string_create(nullptr, 0);
145 }
146 std::string_view out = r->get_username();
147 return ada_string_create(out.data(), out.length());
148}
149
152 if (!r) {
153 return ada_string_create(nullptr, 0);
154 }
155 std::string_view out = r->get_password();
156 return ada_string_create(out.data(), out.length());
157}
158
161 if (!r) {
162 return ada_string_create(nullptr, 0);
163 }
164 std::string_view out = r->get_port();
165 return ada_string_create(out.data(), out.length());
166}
167
170 if (!r) {
171 return ada_string_create(nullptr, 0);
172 }
173 std::string_view out = r->get_hash();
174 return ada_string_create(out.data(), out.length());
175}
176
179 if (!r) {
180 return ada_string_create(nullptr, 0);
181 }
182 std::string_view out = r->get_host();
183 return ada_string_create(out.data(), out.length());
184}
185
188 if (!r) {
189 return ada_string_create(nullptr, 0);
190 }
191 std::string_view out = r->get_hostname();
192 return ada_string_create(out.data(), out.length());
193}
194
197 if (!r) {
198 return ada_string_create(nullptr, 0);
199 }
200 std::string_view out = r->get_pathname();
201 return ada_string_create(out.data(), out.length());
202}
203
206 if (!r) {
207 return ada_string_create(nullptr, 0);
208 }
209 std::string_view out = r->get_search();
210 return ada_string_create(out.data(), out.length());
211}
212
215 if (!r) {
216 return ada_string_create(nullptr, 0);
217 }
218 std::string_view out = r->get_protocol();
219 return ada_string_create(out.data(), out.length());
220}
221
222uint8_t ada_get_host_type(ada_url result) noexcept {
224 if (!r) {
225 return 0;
226 }
227 return r->host_type;
228}
229
230uint8_t ada_get_scheme_type(ada_url result) noexcept {
232 if (!r) {
233 return 0;
234 }
235 return r->type;
236}
237
238bool ada_set_href(ada_url result, const char* input, size_t length) noexcept {
240 if (!r) {
241 return false;
242 }
243 return r->set_href(std::string_view(input, length));
244}
245
246bool ada_set_host(ada_url result, const char* input, size_t length) noexcept {
248 if (!r) {
249 return false;
250 }
251 return r->set_host(std::string_view(input, length));
252}
253
254bool ada_set_hostname(ada_url result, const char* input,
255 size_t length) noexcept {
257 if (!r) {
258 return false;
259 }
260 return r->set_hostname(std::string_view(input, length));
261}
262
263bool ada_set_protocol(ada_url result, const char* input,
264 size_t length) noexcept {
266 if (!r) {
267 return false;
268 }
269 return r->set_protocol(std::string_view(input, length));
270}
271
272bool ada_set_username(ada_url result, const char* input,
273 size_t length) noexcept {
275 if (!r) {
276 return false;
277 }
278 return r->set_username(std::string_view(input, length));
279}
280
281bool ada_set_password(ada_url result, const char* input,
282 size_t length) noexcept {
284 if (!r) {
285 return false;
286 }
287 return r->set_password(std::string_view(input, length));
288}
289
290bool ada_set_port(ada_url result, const char* input, size_t length) noexcept {
292 if (!r) {
293 return false;
294 }
295 return r->set_port(std::string_view(input, length));
296}
297
298bool ada_set_pathname(ada_url result, const char* input,
299 size_t length) noexcept {
301 if (!r) {
302 return false;
303 }
304 return r->set_pathname(std::string_view(input, length));
305}
306
314void ada_set_search(ada_url result, const char* input, size_t length) noexcept {
316 if (r) {
317 r->set_search(std::string_view(input, length));
318 }
319}
320
328void ada_set_hash(ada_url result, const char* input, size_t length) noexcept {
330 if (r) {
331 r->set_hash(std::string_view(input, length));
332 }
333}
334
335void ada_clear_port(ada_url result) noexcept {
337 if (r) {
338 r->clear_port();
339 }
340}
341
348void ada_clear_hash(ada_url result) noexcept {
350 if (r) {
351 r->clear_hash();
352 }
353}
354
361void ada_clear_search(ada_url result) noexcept {
363 if (r) {
364 r->clear_search();
365 }
366}
367
368bool ada_has_credentials(ada_url result) noexcept {
370 if (!r) {
371 return false;
372 }
373 return r->has_credentials();
374}
375
376bool ada_has_empty_hostname(ada_url result) noexcept {
378 if (!r) {
379 return false;
380 }
381 return r->has_empty_hostname();
382}
383
384bool ada_has_hostname(ada_url result) noexcept {
386 if (!r) {
387 return false;
388 }
389 return r->has_hostname();
390}
391
392bool ada_has_non_empty_username(ada_url result) noexcept {
394 if (!r) {
395 return false;
396 }
397 return r->has_non_empty_username();
398}
399
400bool ada_has_non_empty_password(ada_url result) noexcept {
402 if (!r) {
403 return false;
404 }
405 return r->has_non_empty_password();
406}
407
408bool ada_has_port(ada_url result) noexcept {
410 if (!r) {
411 return false;
412 }
413 return r->has_port();
414}
415
416bool ada_has_password(ada_url result) noexcept {
418 if (!r) {
419 return false;
420 }
421 return r->has_password();
422}
423
424bool ada_has_hash(ada_url result) noexcept {
426 if (!r) {
427 return false;
428 }
429 return r->has_hash();
430}
431
432bool ada_has_search(ada_url result) noexcept {
434 if (!r) {
435 return false;
436 }
437 return r->has_search();
438}
439
440// returns a pointer to the internal url_aggregator::url_components
442 static_assert(sizeof(ada_url_components) == sizeof(ada::url_components));
444 if (!r) {
445 return nullptr;
446 }
447 return reinterpret_cast<const ada_url_components*>(&r->get_components());
448}
449
450ada_owned_string ada_idna_to_unicode(const char* input, size_t length) {
451 std::string out = ada::idna::to_unicode(std::string_view(input, length));
452 ada_owned_string owned{};
453 owned.length = out.length();
454 owned.data = new char[owned.length];
455 memcpy((void*)owned.data, out.data(), owned.length);
456 return owned;
457}
458
459ada_owned_string ada_idna_to_ascii(const char* input, size_t length) {
460 std::string out = ada::idna::to_ascii(std::string_view(input, length));
461 ada_owned_string owned{};
462 owned.length = out.size();
463 owned.data = new char[owned.length];
464 memcpy((void*)owned.data, out.data(), owned.length);
465 return owned;
466}
467
469 size_t length) {
471 ada::url_search_params(std::string_view(input, length)));
472}
473
475 auto* r = (ada::result<ada::url_search_params>*)result;
476 delete r;
477}
478
482 if (!r) return ada_owned_string{nullptr, 0};
483 std::string out = r->to_string();
484 ada_owned_string owned{};
485 owned.length = out.size();
486 owned.data = new char[owned.length];
487 memcpy((void*)owned.data, out.data(), owned.length);
488 return owned;
489}
490
494 if (!r) {
495 return 0;
496 }
497 return r->size();
498}
499
503 if (r) {
504 r->sort();
505 }
506}
507
509 size_t key_length, const char* value,
510 size_t value_length) {
513 if (r) {
514 r->append(std::string_view(key, key_length),
515 std::string_view(value, value_length));
516 }
517}
518
519void ada_search_params_set(ada_url_search_params result, const char* key,
520 size_t key_length, const char* value,
521 size_t value_length) {
524 if (r) {
525 r->set(std::string_view(key, key_length),
526 std::string_view(value, value_length));
527 }
528}
529
531 size_t key_length) {
534 if (r) {
535 r->remove(std::string_view(key, key_length));
536 }
537}
538
540 const char* key, size_t key_length,
541 const char* value, size_t value_length) {
544 if (r) {
545 r->remove(std::string_view(key, key_length),
546 std::string_view(value, value_length));
547 }
548}
549
550bool ada_search_params_has(ada_url_search_params result, const char* key,
551 size_t key_length) {
554 if (!r) {
555 return false;
556 }
557 return r->has(std::string_view(key, key_length));
558}
559
561 size_t key_length, const char* value,
562 size_t value_length) {
565 if (!r) {
566 return false;
567 }
568 return r->has(std::string_view(key, key_length),
569 std::string_view(value, value_length));
570}
571
573 size_t key_length) {
576 if (!r) {
577 return ada_string_create(nullptr, 0);
578 }
579 auto found = r->get(std::string_view(key, key_length));
580 if (!found.has_value()) {
581 return ada_string_create(nullptr, 0);
582 }
583 return ada_string_create(found->data(), found->length());
584}
585
587 const char* key, size_t key_length) {
590 if (!r) {
592 std::vector<std::string>());
593 }
595 r->get_all(std::string_view(key, key_length)));
596}
597
608
619
630
632 auto* r = (ada::result<std::vector<std::string>>*)result;
633 delete r;
634}
635
637 auto* r = (ada::result<std::vector<std::string>>*)result;
638 if (!r) {
639 return 0;
640 }
641 return (*r)->size();
642}
643
645 auto* r = (ada::result<std::vector<std::string>>*)result;
646 if (!r) {
647 return ada_string_create(nullptr, 0);
648 }
649 std::string_view view = (*r)->at(index);
650 return ada_string_create(view.data(), view.length());
651}
652
657
661 if (!r) {
662 return ada_string_create(nullptr, 0);
663 }
664 auto next = (*r)->next();
665 if (!next.has_value()) {
666 return ada_string_create(nullptr, 0);
667 }
668 return ada_string_create(next->data(), next->length());
669}
670
674 if (!r) {
675 return false;
676 }
677 return (*r)->has_next();
678}
679
685
689 if (!r) {
690 return ada_string_create(nullptr, 0);
691 }
692 auto next = (*r)->next();
693 if (!next.has_value()) {
694 return ada_string_create(nullptr, 0);
695 }
696 return ada_string_create(next->data(), next->length());
697}
698
702 if (!r) {
703 return false;
704 }
705 return (*r)->has_next();
706}
707
713
717 if (!r) return {ada_string_create(nullptr, 0), ada_string_create(nullptr, 0)};
718 auto next = (*r)->next();
719 if (!next.has_value()) {
720 return {ada_string_create(nullptr, 0), ada_string_create(nullptr, 0)};
721 }
722 return ada_string_pair{
723 ada_string_create(next->first.data(), next->first.length()),
724 ada_string_create(next->second.data(), next->second.length())};
725}
726
730 if (!r) {
731 return false;
732 }
733 return (*r)->has_next();
734}
735
736} // extern "C"
Includes all definitions for Ada.
ada_url_search_params ada_parse_search_params(const char *input, size_t length)
Definition ada_c.cpp:468
ada_string ada_get_search(ada_url result) noexcept
Definition ada_c.cpp:204
ada_strings ada_search_params_get_all(ada_url_search_params result, const char *key, size_t key_length)
Definition ada_c.cpp:586
uint8_t ada_get_scheme_type(ada_url result) noexcept
Definition ada_c.cpp:230
bool ada_has_search(ada_url result) noexcept
Definition ada_c.cpp:432
void ada_free_search_params_entries_iter(ada_url_search_params_entries_iter result)
Definition ada_c.cpp:708
bool ada_search_params_keys_iter_has_next(ada_url_search_params_keys_iter result)
Definition ada_c.cpp:671
ada_string ada_search_params_values_iter_next(ada_url_search_params_values_iter result)
Definition ada_c.cpp:686
void ada_free_search_params_keys_iter(ada_url_search_params_keys_iter result)
Definition ada_c.cpp:653
void ada_clear_hash(ada_url result) noexcept
Definition ada_c.cpp:348
bool ada_search_params_has_value(ada_url_search_params result, const char *key, size_t key_length, const char *value, size_t value_length)
Definition ada_c.cpp:560
void ada_free_strings(ada_strings result)
Definition ada_c.cpp:631
void ada_search_params_append(ada_url_search_params result, const char *key, size_t key_length, const char *value, size_t value_length)
Definition ada_c.cpp:508
ada_url_search_params_entries_iter ada_search_params_get_entries(ada_url_search_params result)
Definition ada_c.cpp:620
void * ada_url_search_params_entries_iter
Definition ada_c.cpp:13
ada_string ada_search_params_get(ada_url_search_params result, const char *key, size_t key_length)
Definition ada_c.cpp:572
void ada_free(ada_url result) noexcept
Definition ada_c.cpp:95
bool ada_can_parse_with_base(const char *input, size_t input_length, const char *base, size_t base_length) noexcept
Definition ada_c.cpp:89
void ada_free_owned_string(ada_owned_string owned) noexcept
Definition ada_c.cpp:126
ada_url ada_parse(const char *input, size_t length) noexcept
Definition ada_c.cpp:67
uint8_t ada_get_host_type(ada_url result) noexcept
Definition ada_c.cpp:222
void * ada_strings
Definition ada_c.cpp:10
ada_string ada_get_protocol(ada_url result) noexcept
Definition ada_c.cpp:213
void ada_search_params_sort(ada_url_search_params result)
Definition ada_c.cpp:500
bool ada_has_credentials(ada_url result) noexcept
Definition ada_c.cpp:368
bool ada_set_pathname(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:298
ada_string ada_get_host(ada_url result) noexcept
Definition ada_c.cpp:177
bool ada_set_password(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:281
ada_string ada_get_port(ada_url result) noexcept
Definition ada_c.cpp:159
ada_string ada_string_create(const char *data, size_t length)
Definition ada_c.cpp:30
ada::result< ada::url_aggregator > & get_instance(void *result) noexcept
Definition ada_c.cpp:3
ada_owned_string ada_idna_to_unicode(const char *input, size_t length)
Definition ada_c.cpp:450
ada_string ada_get_hash(ada_url result) noexcept
Definition ada_c.cpp:168
ada_owned_string ada_get_origin(ada_url result) noexcept
Definition ada_c.cpp:111
void ada_search_params_remove_value(ada_url_search_params result, const char *key, size_t key_length, const char *value, size_t value_length)
Definition ada_c.cpp:539
ada_string ada_get_hostname(ada_url result) noexcept
Definition ada_c.cpp:186
void ada_free_search_params(ada_url_search_params result)
Definition ada_c.cpp:474
bool ada_set_username(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:272
bool ada_has_empty_hostname(ada_url result) noexcept
Definition ada_c.cpp:376
bool ada_has_hash(ada_url result) noexcept
Definition ada_c.cpp:424
ada_string ada_get_href(ada_url result) noexcept
Definition ada_c.cpp:132
ada_string ada_get_password(ada_url result) noexcept
Definition ada_c.cpp:150
void * ada_url_search_params
Definition ada_c.cpp:9
bool ada_set_protocol(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:263
bool ada_can_parse(const char *input, size_t length) noexcept
Definition ada_c.cpp:85
bool ada_is_valid(ada_url result) noexcept
Definition ada_c.cpp:105
ada_owned_string ada_search_params_to_string(ada_url_search_params result)
Definition ada_c.cpp:479
ada_string ada_strings_get(ada_strings result, size_t index)
Definition ada_c.cpp:644
void ada_set_search(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:314
const ada_url_components * ada_get_components(ada_url result) noexcept
Definition ada_c.cpp:441
void * ada_url
Definition ada_c.cpp:8
bool ada_search_params_entries_iter_has_next(ada_url_search_params_entries_iter result)
Definition ada_c.cpp:727
size_t ada_search_params_size(ada_url_search_params result)
Definition ada_c.cpp:491
ada_string ada_get_pathname(ada_url result) noexcept
Definition ada_c.cpp:195
void ada_free_search_params_values_iter(ada_url_search_params_values_iter result)
Definition ada_c.cpp:680
ada_url ada_parse_with_base(const char *input, size_t input_length, const char *base, size_t base_length) noexcept
Definition ada_c.cpp:72
ada_string ada_search_params_keys_iter_next(ada_url_search_params_keys_iter result)
Definition ada_c.cpp:658
void ada_clear_port(ada_url result) noexcept
Definition ada_c.cpp:335
bool ada_set_hostname(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:254
bool ada_has_hostname(ada_url result) noexcept
Definition ada_c.cpp:384
ada_string_pair ada_search_params_entries_iter_next(ada_url_search_params_entries_iter result)
Definition ada_c.cpp:714
ada_url_search_params_keys_iter ada_search_params_get_keys(ada_url_search_params result)
Definition ada_c.cpp:598
ada_owned_string ada_idna_to_ascii(const char *input, size_t length)
Definition ada_c.cpp:459
bool ada_has_non_empty_password(ada_url result) noexcept
Definition ada_c.cpp:400
bool ada_set_href(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:238
ada_url ada_copy(ada_url input) noexcept
Definition ada_c.cpp:100
bool ada_has_password(ada_url result) noexcept
Definition ada_c.cpp:416
void * ada_url_search_params_keys_iter
Definition ada_c.cpp:11
bool ada_search_params_values_iter_has_next(ada_url_search_params_values_iter result)
Definition ada_c.cpp:699
bool ada_set_port(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:290
bool ada_has_port(ada_url result) noexcept
Definition ada_c.cpp:408
void ada_search_params_set(ada_url_search_params result, const char *key, size_t key_length, const char *value, size_t value_length)
Definition ada_c.cpp:519
bool ada_set_host(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:246
size_t ada_strings_size(ada_strings result)
Definition ada_c.cpp:636
ada_url_search_params_values_iter ada_search_params_get_values(ada_url_search_params result)
Definition ada_c.cpp:609
void ada_set_hash(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:328
bool ada_search_params_has(ada_url_search_params result, const char *key, size_t key_length)
Definition ada_c.cpp:550
bool ada_has_non_empty_username(ada_url result) noexcept
Definition ada_c.cpp:392
void * ada_url_search_params_values_iter
Definition ada_c.cpp:12
void ada_search_params_remove(ada_url_search_params result, const char *key, size_t key_length)
Definition ada_c.cpp:530
void ada_clear_search(ada_url result) noexcept
Definition ada_c.cpp:361
ada_string ada_get_username(ada_url result) noexcept
Definition ada_c.cpp:141
void * ada_url_search_params_entries_iter
Definition ada_c.h:125
void * ada_strings
Definition ada_c.h:116
void * ada_url_search_params
Definition ada_c.h:113
void * ada_url
Definition ada_c.h:39
void * ada_url_search_params_keys_iter
Definition ada_c.h:117
void * ada_url_search_params_values_iter
Definition ada_c.h:118
std::string to_ascii(std::string_view ut8_string)
std::string to_unicode(std::string_view input)
bool can_parse(std::string_view input, const std::string_view *base_input=nullptr)
tl::expected< result_type, ada::errors > result
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
URL Component representations using offsets.
size_t length
Definition ada_c.h:25
const char * data
Definition ada_c.h:24
ada_string value
Definition ada_c.h:122
ada_string key
Definition ada_c.h:121
const char * data
Definition ada_c.h:18
size_t length
Definition ada_c.h:19
uint32_t username_end
Definition ada_c.h:30
uint32_t host_end
Definition ada_c.h:32
uint32_t hash_start
Definition ada_c.h:36
uint32_t pathname_start
Definition ada_c.h:34
uint32_t port
Definition ada_c.h:33
uint32_t protocol_end
Definition ada_c.h:29
uint32_t search_start
Definition ada_c.h:35
uint32_t host_start
Definition ada_c.h:31