Skip to content

Commit

Permalink
reorga and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
tclesius committed Sep 8, 2024
1 parent 3490ae3 commit 7b73f5a
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 316 deletions.
3 changes: 0 additions & 3 deletions .gitignore

This file was deleted.

1 change: 0 additions & 1 deletion LICENSE

This file was deleted.

33 changes: 0 additions & 33 deletions README.md

This file was deleted.

Binary file removed build/c3c_testout
Binary file not shown.
95 changes: 95 additions & 0 deletions cookie.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import std::collections::map;

struct Cookie
{
String name;
String value;
String domain; // if starting with . for all subdomains valid
String path;
String expires;
String max_age;
String same_site;
// flags
bool secure;
bool http_only;

}

// Cookie Jar
struct CookieJar
{
HashMap(<String, Cookie>) cookies;
}

fn void CookieJar.set_cookie(&self, Cookie cookie)
{
DString key;
key.new_init();
key.appendf("%s,%s,%s", cookie.name, cookie.domain, cookie.path);
self.cookies[key.str_view()] = cookie;
}

fn Cookie! CookieJar.get_cookie(&self, String name , String domain, String path)
{
DString key;
key.new_init();
key.appendf("%s,%s,%s", name, domain, path);
return self.cookies[key.str_view()];
}
///

fn String Cookie.to_string(&self)
{
DString result;
result.new_init();
result.appendf(
"<Cookie %s=%s; Domain=%s; Path=%s; Expires=%s; Max-Age=%s; SameSite=%s; Secure=%b; HttpOnly=%b>",
self.name,
self.value,
self.domain,
self.path,
self.expires,
self.max_age,
self.same_site,
self.secure,
self.http_only
);
return result.str_view();
}

fn void Cookie.parse(&self, String value)
{
// iterate over every set-cookie attribute
foreach(ix, attribute: value.trim().split(";"))
{
// parse key value attributes
if(attribute.contains("="))
{
String[] kv = attribute.trim().split("=");
// the first key value is always the cookie name and cookie value
if(ix == 0)
{
self.name = kv[0];
self.value = kv[1];
}
else
{
switch(attribute.temp_ascii_to_lower())
{
case "domain": self.domain = kv[1];
case "path": self.path = kv[1];
case "expires": self.expires = kv[1];
case "max-age": self.max_age = kv[1];
case "same-site": self.same_site = kv[1];
}
}
continue;
}
// parse flag attributes
switch(attribute.temp_ascii_to_lower())
{
case "secure": self.secure = true;
case "httponly": self.http_only = true;
}
}
}
9 changes: 9 additions & 0 deletions examples/main.c3
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import std::io;
import http;
import url;

fn void main()
{
Response res = http::get("http://jsonip.com")
io::printfn(" Body:\n %s", res.text());
}
94 changes: 0 additions & 94 deletions src/http.c3 → http.c3
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,6 @@ struct Request
List(<Header>) headers;
}

struct Cookie
{
String name;
String value;
String domain; // if starting with . for all subdomains valid
String path;
String expires;
String max_age;
String same_site;
// flags
bool secure;
bool http_only;

}

struct CookieJar
{
HashMap(<String, Cookie>) cookies;
}


struct ContentIterator
Expand Down Expand Up @@ -373,78 +354,3 @@ fn Response delete(String url, Cookie[] cookies = {}, Header[] headers = {}, Str
{
return request("DELETE", .url=url, .cookies=cookies, .headers=headers, .body=body);
}

//// Cookie methods
fn String Cookie.to_string(&self)
{
DString result;
result.new_init();
result.appendf(
"<Cookie %s=%s; Domain=%s; Path=%s; Expires=%s; Max-Age=%s; SameSite=%s; Secure=%b; HttpOnly=%b>",
self.name,
self.value,
self.domain,
self.path,
self.expires,
self.max_age,
self.same_site,
self.secure,
self.http_only
);
return result.str_view();
}

fn void Cookie.parse(&self, String value)
{
// iterate over every set-cookie attribute
foreach(ix, attribute: value.trim().split(";"))
{
// parse key value attributes
if(attribute.contains("="))
{
String[] kv = attribute.trim().split("=");
// the first key value is always the cookie name and cookie value
if(ix == 0)
{
self.name = kv[0];
self.value = kv[1];
}
else
{
switch(attribute.temp_ascii_to_lower())
{
case "domain": self.domain = kv[1];
case "path": self.path = kv[1];
case "expires": self.expires = kv[1];
case "max-age": self.max_age = kv[1];
case "same-site": self.same_site = kv[1];
}
}
continue;
}
// parse flag attributes
switch(attribute.temp_ascii_to_lower())
{
case "secure": self.secure = true;
case "httponly": self.http_only = true;
}
}
}

//// CookieJar methods

fn void CookieJar.set_cookie(&self, Cookie cookie)
{
DString key;
key.new_init();
key.appendf("%s,%s,%s", cookie.name, cookie.domain, cookie.path);
self.cookies[key.str_view()] = cookie;
}

fn Cookie! CookieJar.get_cookie(&self, String name , String domain, String path)
{
DString key;
key.new_init();
key.appendf("%s,%s,%s", name, domain, path);
return self.cookies[key.str_view()];
}
41 changes: 0 additions & 41 deletions project.json

This file was deleted.

18 changes: 0 additions & 18 deletions src/client.py

This file was deleted.

70 changes: 0 additions & 70 deletions src/main.c3

This file was deleted.

Loading

0 comments on commit 7b73f5a

Please sign in to comment.