-
Notifications
You must be signed in to change notification settings - Fork 75
feat added url parser #209
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # URL Parser in Cairo | ||
|
|
||
| This module provides URL parsing functionality implemented in Cairo. It can parse URLs into their components including protocol, domain, path, query parameters, and fragment identifiers. | ||
|
|
||
| ## Features | ||
|
|
||
| - Extract protocol (http, https, etc.) | ||
| - Extract domain and subdomains | ||
| - Extract path components | ||
| - Extract query parameters | ||
| - Extract fragment identifiers | ||
|
|
||
| ## Usage | ||
|
|
||
| ```cairo | ||
| use url_parser::URLParserTrait; | ||
|
|
||
| fn main() { | ||
| let url = 'https://example.com/path?param=value#section1'; | ||
| let parsed_url = URLParserTrait::parse_url(url); | ||
|
|
||
| // Access URL components | ||
| let protocol = parsed_url.protocol; // 'https' | ||
| let domain = parsed_url.domain; // 'example.com' | ||
| let path = parsed_url.path; // '/path' | ||
| let query = parsed_url.query; // 'param=value' | ||
| let fragment = parsed_url.fragment; // 'section1' | ||
| } | ||
| ``` | ||
|
|
||
| ## Building and Testing | ||
|
|
||
| 1. Navigate to the url_parser directory: | ||
|
|
||
| ```bash | ||
| cd examples/cairo/scripts/url_parser | ||
| ``` | ||
|
|
||
| 2. Build the project: | ||
|
|
||
| ```bash | ||
| scarb build | ||
| ``` | ||
|
|
||
| 3. Run the tests: | ||
|
|
||
| ```bash | ||
| scarb test | ||
| ``` | ||
|
|
||
| ## Implementation Details | ||
|
|
||
| The URL parser implements the following traits: | ||
|
|
||
| - `URLParserTrait`: Main trait containing all parsing functions | ||
| - `URL`: Struct containing parsed URL components | ||
|
|
||
| Each component is extracted using dedicated functions that handle different URL formats and edge cases. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "url_parser" | ||
| version = "0.1.0" | ||
|
|
||
| [dependencies] | ||
| starknet = ">=2.3.1" | ||
|
|
||
| [[target.starknet-contract]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| use core::array::ArrayTrait; | ||
| use core::option::OptionTrait; | ||
| use core::traits::Into; | ||
| use core::clone::Clone; | ||
|
|
||
| #[derive(Drop, Clone)] | ||
| struct URL { | ||
| protocol: felt252, | ||
| domain: felt252, | ||
| path: felt252, | ||
| query: felt252, | ||
| fragment: felt252, | ||
| } | ||
|
|
||
| trait URLParserTrait { | ||
| fn parse_url(url: felt252) -> URL; | ||
| fn extract_protocol(url: felt252) -> felt252; | ||
| fn extract_domain(url: felt252) -> felt252; | ||
| fn extract_path(url: felt252) -> felt252; | ||
| fn extract_query(url: felt252) -> felt252; | ||
| fn extract_fragment(url: felt252) -> felt252; | ||
| } | ||
|
|
||
| impl URLParser of URLParserTrait { | ||
| fn parse_url(url: felt252) -> URL { | ||
| URL { | ||
| protocol: URLParser::extract_protocol(url), | ||
| domain: URLParser::extract_domain(url), | ||
| path: URLParser::extract_path(url), | ||
| query: URLParser::extract_query(url), | ||
| fragment: URLParser::extract_fragment(url) | ||
| } | ||
| } | ||
|
|
||
| fn extract_protocol(url: felt252) -> felt252 { | ||
| // Implementation for extracting protocol | ||
| // TODO: Add protocol extraction logic | ||
| 'https' | ||
| } | ||
|
|
||
| fn extract_domain(url: felt252) -> felt252 { | ||
| // Implementation for extracting domain | ||
| // TODO: Add domain extraction logic | ||
| 'example.com' | ||
| } | ||
|
|
||
| fn extract_path(url: felt252) -> felt252 { | ||
| // Implementation for extracting path | ||
| // TODO: Add path extraction logic | ||
| '/path' | ||
| } | ||
|
|
||
| fn extract_query(url: felt252) -> felt252 { | ||
| // Implementation for extracting query parameters | ||
| // TODO: Add query extraction logic | ||
| 'param=value' | ||
| } | ||
|
|
||
| fn extract_fragment(url: felt252) -> felt252 { | ||
| // Implementation for extracting fragment | ||
| // TODO: Add fragment extraction logic | ||
| 'section1' | ||
| } | ||
| } | ||
Simplyauf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| use core::debug::PrintTrait; | ||
| use url_parser::URLParserTrait; | ||
|
|
||
| #[test] | ||
| fn test_url_parser() { | ||
| let test_url = 'https://example.com/path?param=value#section1'; | ||
| let parsed_url = URLParserTrait::parse_url(test_url); | ||
|
|
||
| assert(parsed_url.protocol == 'https', 'Invalid protocol'); | ||
| assert(parsed_url.domain == 'example.com', 'Invalid domain'); | ||
| assert(parsed_url.path == '/path', 'Invalid path'); | ||
| assert(parsed_url.query == 'param=value', 'Invalid query'); | ||
| assert(parsed_url.fragment == 'section1', 'Invalid fragment'); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_protocol_extraction() { | ||
| let test_url = 'https://example.com'; | ||
| let protocol = URLParserTrait::extract_protocol(test_url); | ||
| assert(protocol == 'https', 'Protocol extraction failed'); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_domain_extraction() { | ||
| let test_url = 'https://example.com'; | ||
| let domain = URLParserTrait::extract_domain(test_url); | ||
| assert(domain == 'example.com', 'Domain extraction failed'); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.