Ada 2.8.0
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}
129
132 if (!r) {
133 return ada_string_create(nullptr, 0);
134 }
135 std::string_view out = r->get_href();
136 return ada_string_create(out.data(), out.length());
137}
138
141 if (!r) {
142 return ada_string_create(nullptr, 0);
143 }
144 std::string_view out = r->get_username();
145 return ada_string_create(out.data(), out.length());
146}
147
150 if (!r) {
151 return ada_string_create(nullptr, 0);
152 }
153 std::string_view out = r->get_password();
154 return ada_string_create(out.data(), out.length());
155}
156
159 if (!r) {
160 return ada_string_create(nullptr, 0);
161 }
162 std::string_view out = r->get_port();
163 return ada_string_create(out.data(), out.length());
164}
165
168 if (!r) {
169 return ada_string_create(nullptr, 0);
170 }
171 std::string_view out = r->get_hash();
172 return ada_string_create(out.data(), out.length());
173}
174
177 if (!r) {
178 return ada_string_create(nullptr, 0);
179 }
180 std::string_view out = r->get_host();
181 return ada_string_create(out.data(), out.length());
182}
183
186 if (!r) {
187 return ada_string_create(nullptr, 0);
188 }
189 std::string_view out = r->get_hostname();
190 return ada_string_create(out.data(), out.length());
191}
192
195 if (!r) {
196 return ada_string_create(nullptr, 0);
197 }
198 std::string_view out = r->get_pathname();
199 return ada_string_create(out.data(), out.length());
200}
201
204 if (!r) {
205 return ada_string_create(nullptr, 0);
206 }
207 std::string_view out = r->get_search();
208 return ada_string_create(out.data(), out.length());
209}
210
213 if (!r) {
214 return ada_string_create(nullptr, 0);
215 }
216 std::string_view out = r->get_protocol();
217 return ada_string_create(out.data(), out.length());
218}
219
220uint8_t ada_get_host_type(ada_url result) noexcept {
222 if (!r) {
223 return 0;
224 }
225 return r->host_type;
226}
227
228uint8_t ada_get_scheme_type(ada_url result) noexcept {
230 if (!r) {
231 return 0;
232 }
233 return r->type;
234}
235
236bool ada_set_href(ada_url result, const char* input, size_t length) noexcept {
238 if (!r) {
239 return false;
240 }
241 return r->set_href(std::string_view(input, length));
242}
243
244bool ada_set_host(ada_url result, const char* input, size_t length) noexcept {
246 if (!r) {
247 return false;
248 }
249 return r->set_host(std::string_view(input, length));
250}
251
252bool ada_set_hostname(ada_url result, const char* input,
253 size_t length) noexcept {
255 if (!r) {
256 return false;
257 }
258 return r->set_hostname(std::string_view(input, length));
259}
260
261bool ada_set_protocol(ada_url result, const char* input,
262 size_t length) noexcept {
264 if (!r) {
265 return false;
266 }
267 return r->set_protocol(std::string_view(input, length));
268}
269
270bool ada_set_username(ada_url result, const char* input,
271 size_t length) noexcept {
273 if (!r) {
274 return false;
275 }
276 return r->set_username(std::string_view(input, length));
277}
278
279bool ada_set_password(ada_url result, const char* input,
280 size_t length) noexcept {
282 if (!r) {
283 return false;
284 }
285 return r->set_password(std::string_view(input, length));
286}
287
288bool ada_set_port(ada_url result, const char* input, size_t length) noexcept {
290 if (!r) {
291 return false;
292 }
293 return r->set_port(std::string_view(input, length));
294}
295
296bool ada_set_pathname(ada_url result, const char* input,
297 size_t length) noexcept {
299 if (!r) {
300 return false;
301 }
302 return r->set_pathname(std::string_view(input, length));
303}
304
312void ada_set_search(ada_url result, const char* input, size_t length) noexcept {
314 if (r) {
315 r->set_search(std::string_view(input, length));
316 }
317}
318
326void ada_set_hash(ada_url result, const char* input, size_t length) noexcept {
328 if (r) {
329 r->set_hash(std::string_view(input, length));
330 }
331}
332
333void ada_clear_port(ada_url result) noexcept {
335 if (r) {
336 r->clear_port();
337 }
338}
339
346void ada_clear_hash(ada_url result) noexcept {
348 if (r) {
349 r->clear_hash();
350 }
351}
352
359void ada_clear_search(ada_url result) noexcept {
361 if (r) {
362 r->clear_search();
363 }
364}
365
366bool ada_has_credentials(ada_url result) noexcept {
368 if (!r) {
369 return false;
370 }
371 return r->has_credentials();
372}
373
374bool ada_has_empty_hostname(ada_url result) noexcept {
376 if (!r) {
377 return false;
378 }
379 return r->has_empty_hostname();
380}
381
382bool ada_has_hostname(ada_url result) noexcept {
384 if (!r) {
385 return false;
386 }
387 return r->has_hostname();
388}
389
390bool ada_has_non_empty_username(ada_url result) noexcept {
392 if (!r) {
393 return false;
394 }
395 return r->has_non_empty_username();
396}
397
398bool ada_has_non_empty_password(ada_url result) noexcept {
400 if (!r) {
401 return false;
402 }
403 return r->has_non_empty_password();
404}
405
406bool ada_has_port(ada_url result) noexcept {
408 if (!r) {
409 return false;
410 }
411 return r->has_port();
412}
413
414bool ada_has_password(ada_url result) noexcept {
416 if (!r) {
417 return false;
418 }
419 return r->has_password();
420}
421
422bool ada_has_hash(ada_url result) noexcept {
424 if (!r) {
425 return false;
426 }
427 return r->has_hash();
428}
429
430bool ada_has_search(ada_url result) noexcept {
432 if (!r) {
433 return false;
434 }
435 return r->has_search();
436}
437
438// returns a pointer to the internal url_aggregator::url_components
440 static_assert(sizeof(ada_url_components) == sizeof(ada::url_components));
442 if (!r) {
443 return nullptr;
444 }
445 return reinterpret_cast<const ada_url_components*>(&r->get_components());
446}
447
448ada_owned_string ada_idna_to_unicode(const char* input, size_t length) {
449 std::string out = ada::idna::to_unicode(std::string_view(input, length));
450 ada_owned_string owned{};
451 owned.length = out.length();
452 owned.data = new char[owned.length];
453 memcpy((void*)owned.data, out.data(), owned.length);
454 return owned;
455}
456
457ada_owned_string ada_idna_to_ascii(const char* input, size_t length) {
458 std::string out = ada::idna::to_ascii(std::string_view(input, length));
459 ada_owned_string owned{};
460 owned.length = out.size();
461 owned.data = new char[owned.length];
462 memcpy((void*)owned.data, out.data(), owned.length);
463 return owned;
464}
465
467 size_t length) {
469 ada::url_search_params(std::string_view(input, length)));
470}
471
473 auto* r = (ada::result<ada::url_search_params>*)result;
474 delete r;
475}
476
480 if (!r) return ada_owned_string{nullptr, 0};
481 std::string out = r->to_string();
482 ada_owned_string owned{};
483 owned.length = out.size();
484 owned.data = new char[owned.length];
485 memcpy((void*)owned.data, out.data(), owned.length);
486 return owned;
487}
488
492 if (!r) {
493 return 0;
494 }
495 return r->size();
496}
497
501 if (r) {
502 r->sort();
503 }
504}
505
507 size_t key_length, const char* value,
508 size_t value_length) {
511 if (r) {
512 r->append(std::string_view(key, key_length),
513 std::string_view(value, value_length));
514 }
515}
516
517void ada_search_params_set(ada_url_search_params result, const char* key,
518 size_t key_length, const char* value,
519 size_t value_length) {
522 if (r) {
523 r->set(std::string_view(key, key_length),
524 std::string_view(value, value_length));
525 }
526}
527
529 size_t key_length) {
532 if (r) {
533 r->remove(std::string_view(key, key_length));
534 }
535}
536
538 const char* key, size_t key_length,
539 const char* value, size_t value_length) {
542 if (r) {
543 r->remove(std::string_view(key, key_length),
544 std::string_view(value, value_length));
545 }
546}
547
548bool ada_search_params_has(ada_url_search_params result, const char* key,
549 size_t key_length) {
552 if (!r) {
553 return false;
554 }
555 return r->has(std::string_view(key, key_length));
556}
557
559 size_t key_length, const char* value,
560 size_t value_length) {
563 if (!r) {
564 return false;
565 }
566 return r->has(std::string_view(key, key_length),
567 std::string_view(value, value_length));
568}
569
571 size_t key_length) {
574 if (!r) {
575 return ada_string_create(nullptr, 0);
576 }
577 auto found = r->get(std::string_view(key, key_length));
578 if (!found.has_value()) {
579 return ada_string_create(nullptr, 0);
580 }
581 return ada_string_create(found->data(), found->length());
582}
583
585 const char* key, size_t key_length) {
588 if (!r) {
590 std::vector<std::string>());
591 }
593 r->get_all(std::string_view(key, key_length)));
594}
595
606
617
628
630 auto* r = (ada::result<std::vector<std::string>>*)result;
631 delete r;
632}
633
635 auto* r = (ada::result<std::vector<std::string>>*)result;
636 if (!r) {
637 return 0;
638 }
639 return (*r)->size();
640}
641
643 auto* r = (ada::result<std::vector<std::string>>*)result;
644 if (!r) {
645 return ada_string_create(nullptr, 0);
646 }
647 std::string_view view = (*r)->at(index);
648 return ada_string_create(view.data(), view.length());
649}
650
655
659 if (!r) {
660 return ada_string_create(nullptr, 0);
661 }
662 auto next = (*r)->next();
663 if (!next.has_value()) {
664 return ada_string_create(nullptr, 0);
665 }
666 return ada_string_create(next->data(), next->length());
667}
668
672 if (!r) {
673 return false;
674 }
675 return (*r)->has_next();
676}
677
683
687 if (!r) {
688 return ada_string_create(nullptr, 0);
689 }
690 auto next = (*r)->next();
691 if (!next.has_value()) {
692 return ada_string_create(nullptr, 0);
693 }
694 return ada_string_create(next->data(), next->length());
695}
696
700 if (!r) {
701 return false;
702 }
703 return (*r)->has_next();
704}
705
711
715 if (!r) return {ada_string_create(nullptr, 0), ada_string_create(nullptr, 0)};
716 auto next = (*r)->next();
717 if (!next.has_value()) {
718 return {ada_string_create(nullptr, 0), ada_string_create(nullptr, 0)};
719 }
720 return ada_string_pair{
721 ada_string_create(next->first.data(), next->first.length()),
722 ada_string_create(next->second.data(), next->second.length())};
723}
724
728 if (!r) {
729 return false;
730 }
731 return (*r)->has_next();
732}
733
734} // 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:466
ada_string ada_get_search(ada_url result) noexcept
Definition ada_c.cpp:202
ada_strings ada_search_params_get_all(ada_url_search_params result, const char *key, size_t key_length)
Definition ada_c.cpp:584
uint8_t ada_get_scheme_type(ada_url result) noexcept
Definition ada_c.cpp:228
bool ada_has_search(ada_url result) noexcept
Definition ada_c.cpp:430
void ada_free_search_params_entries_iter(ada_url_search_params_entries_iter result)
Definition ada_c.cpp:706
bool ada_search_params_keys_iter_has_next(ada_url_search_params_keys_iter result)
Definition ada_c.cpp:669
ada_string ada_search_params_values_iter_next(ada_url_search_params_values_iter result)
Definition ada_c.cpp:684
void ada_free_search_params_keys_iter(ada_url_search_params_keys_iter result)
Definition ada_c.cpp:651
void ada_clear_hash(ada_url result) noexcept
Definition ada_c.cpp:346
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:558
void ada_free_strings(ada_strings result)
Definition ada_c.cpp:629
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:506
ada_url_search_params_entries_iter ada_search_params_get_entries(ada_url_search_params result)
Definition ada_c.cpp:618
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:570
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:220
void * ada_strings
Definition ada_c.cpp:10
ada_string ada_get_protocol(ada_url result) noexcept
Definition ada_c.cpp:211
void ada_search_params_sort(ada_url_search_params result)
Definition ada_c.cpp:498
bool ada_has_credentials(ada_url result) noexcept
Definition ada_c.cpp:366
bool ada_set_pathname(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:296
ada_string ada_get_host(ada_url result) noexcept
Definition ada_c.cpp:175
bool ada_set_password(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:279
ada_string ada_get_port(ada_url result) noexcept
Definition ada_c.cpp:157
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:448
ada_string ada_get_hash(ada_url result) noexcept
Definition ada_c.cpp:166
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:537
ada_string ada_get_hostname(ada_url result) noexcept
Definition ada_c.cpp:184
void ada_free_search_params(ada_url_search_params result)
Definition ada_c.cpp:472
bool ada_set_username(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:270
bool ada_has_empty_hostname(ada_url result) noexcept
Definition ada_c.cpp:374
bool ada_has_hash(ada_url result) noexcept
Definition ada_c.cpp:422
ada_string ada_get_href(ada_url result) noexcept
Definition ada_c.cpp:130
ada_string ada_get_password(ada_url result) noexcept
Definition ada_c.cpp:148
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:261
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:477
ada_string ada_strings_get(ada_strings result, size_t index)
Definition ada_c.cpp:642
void ada_set_search(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:312
const ada_url_components * ada_get_components(ada_url result) noexcept
Definition ada_c.cpp:439
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:725
size_t ada_search_params_size(ada_url_search_params result)
Definition ada_c.cpp:489
ada_string ada_get_pathname(ada_url result) noexcept
Definition ada_c.cpp:193
void ada_free_search_params_values_iter(ada_url_search_params_values_iter result)
Definition ada_c.cpp:678
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:656
void ada_clear_port(ada_url result) noexcept
Definition ada_c.cpp:333
bool ada_set_hostname(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:252
bool ada_has_hostname(ada_url result) noexcept
Definition ada_c.cpp:382
ada_string_pair ada_search_params_entries_iter_next(ada_url_search_params_entries_iter result)
Definition ada_c.cpp:712
ada_url_search_params_keys_iter ada_search_params_get_keys(ada_url_search_params result)
Definition ada_c.cpp:596
ada_owned_string ada_idna_to_ascii(const char *input, size_t length)
Definition ada_c.cpp:457
bool ada_has_non_empty_password(ada_url result) noexcept
Definition ada_c.cpp:398
bool ada_set_href(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:236
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:414
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:697
bool ada_set_port(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:288
bool ada_has_port(ada_url result) noexcept
Definition ada_c.cpp:406
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:517
bool ada_set_host(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:244
size_t ada_strings_size(ada_strings result)
Definition ada_c.cpp:634
ada_url_search_params_values_iter ada_search_params_get_values(ada_url_search_params result)
Definition ada_c.cpp:607
void ada_set_hash(ada_url result, const char *input, size_t length) noexcept
Definition ada_c.cpp:326
bool ada_search_params_has(ada_url_search_params result, const char *key, size_t key_length)
Definition ada_c.cpp:548
bool ada_has_non_empty_username(ada_url result) noexcept
Definition ada_c.cpp:390
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:528
void ada_clear_search(ada_url result) noexcept
Definition ada_c.cpp:359
ada_string ada_get_username(ada_url result) noexcept
Definition ada_c.cpp:139
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