Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions examples/cairo/scripts/url_parser/README.md
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.
8 changes: 8 additions & 0 deletions examples/cairo/scripts/url_parser/Scarb.toml
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]]
64 changes: 64 additions & 0 deletions examples/cairo/scripts/url_parser/src/lib.cairo
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'
}
}
28 changes: 28 additions & 0 deletions examples/cairo/scripts/url_parser/src/test.cairo
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');
}