Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update C api #491

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion fuzz/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {

// clear methods
out_aggregator->clear_port();
out_aggregator->clear_pathname();
out_aggregator->clear_search();
out_aggregator->clear_hash();
}
Expand Down
2 changes: 1 addition & 1 deletion include/ada/url_aggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ struct url_aggregator : url_base {

inline void clear_port();
inline void clear_hash();
inline void clear_pathname() override;
inline void clear_search() override;

private:
Expand Down Expand Up @@ -277,6 +276,7 @@ struct url_aggregator : url_base {
inline uint32_t retrieve_base_port() const;
inline void clear_hostname();
inline void clear_password();
inline void clear_pathname() override;
inline bool has_dash_dot() const noexcept;
void delete_dash_dot();
inline void consume_prepared_path(std::string_view input);
Expand Down
1 change: 0 additions & 1 deletion include/ada_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ void ada_set_hash(ada_url result, const char* input, size_t length);
// url_aggregator clear methods
void ada_clear_port(ada_url result);
void ada_clear_hash(ada_url result);
void ada_clear_pathname(ada_url result);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ada_clear_pathname can be implemented as set_pathname(""). I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I added as a commit comment but I don't think we need another function for this.

void ada_clear_search(ada_url result);

// url_aggregator functions
Expand Down
33 changes: 26 additions & 7 deletions src/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,27 @@ bool ada_set_pathname(ada_url result, const char* input,
return r->set_pathname(std::string_view(input, length));
}

/**
* Update the search/query of the URL.
*
* If a URL has `?` as the search value, passing empty string to this function
* does not remove the attribute. If you need to remove it, please use
* `ada_clear_search` method.
*/
void ada_set_search(ada_url result, const char* input, size_t length) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
r->set_search(std::string_view(input, length));
}
}

/**
* Update the hash/fragment of the URL.
*
* If a URL has `#` as the hash value, passing empty string to this function
* does not remove the attribute. If you need to remove it, please use
* `ada_clear_hash` method.
*/
void ada_set_hash(ada_url result, const char* input, size_t length) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
Expand All @@ -308,20 +322,25 @@ void ada_clear_port(ada_url result) noexcept {
}
}

/**
* Removes the hash of the URL.
*
* Despite `ada_set_hash` method, this function allows the complete
* removal of the hash attribute, even if it has a value of `#`.
*/
void ada_clear_hash(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
r->clear_hash();
}
}

void ada_clear_pathname(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
r->clear_pathname();
}
}

/**
* Removes the search of the URL.
*
* Despite `ada_set_search` method, this function allows the complete
* removal of the search attribute, even if it has a value of `?`.
*/
void ada_clear_search(ada_url result) noexcept {
ada::result<ada::url_aggregator>& r = get_instance(result);
if (r) {
Expand Down
34 changes: 32 additions & 2 deletions tests/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ TEST(ada_c, setters) {

ada_set_pathname(url, "new-pathname", strlen("new-pathname"));
ASSERT_EQ(convert_string(ada_get_pathname(url)), "/new-pathname");
ada_clear_pathname(url);
ASSERT_EQ(convert_string(ada_get_pathname(url)), "");

ada_set_search(url, "new-search", strlen("new-search"));
ASSERT_EQ(convert_string(ada_get_search(url)), "?new-search");
Expand Down Expand Up @@ -179,3 +177,35 @@ TEST(ada_c, ada_idna) {
ada_free_owned_string(unicode);
SUCCEED();
}

TEST(ada_c, ada_clear_hash) {
// Make sure a hash attribute with `#` is removed.
std::string_view input = "https://www.google.com/hello-world?query=1#";
ada_url out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));

ada_clear_hash(out);
ASSERT_EQ(convert_string(ada_get_hash(out)), "");
ASSERT_FALSE(ada_has_hash(out));
ASSERT_EQ(convert_string(ada_get_href(out)),
"https://www.google.com/hello-world?query=1");

ada_free(out);
SUCCEED();
}

TEST(ada_c, ada_clear_search) {
// Make sure a search attribute with `?` is removed.
std::string_view input = "https://www.google.com/hello-world?#hash";
ada_url out = ada_parse(input.data(), input.size());
ASSERT_TRUE(ada_is_valid(out));

ada_clear_search(out);
ASSERT_EQ(convert_string(ada_get_search(out)), "");
ASSERT_FALSE(ada_has_search(out));
ASSERT_EQ(convert_string(ada_get_href(out)),
"https://www.google.com/hello-world#hash");

ada_free(out);
SUCCEED();
}
Loading