Commit 4ad29048 authored by Bat's avatar Bat
Browse files

Merge branch 'master' into 'master'

fix #58 + limiting database to really supported types

See merge request !20
parents 189b30fd 0498cf10
Pipeline #9461 passed with stage
in 4 minutes and 6 seconds
......@@ -6,7 +6,7 @@ using Valse;
* It contains manual tests.
*/
void main (string[] args) {
Valse.log_level = DebugLevel.ERRORS;
Valse.log_level = DebugLevel.DEBUG;
Router r = new Router (9000);
Router.options.process_count = 150;
r.register ("user", new UserController ());
......
......@@ -8,7 +8,7 @@ Create a new account
<p>
You are going to create an acount. But first we need some personl data about you.
</p>
<form method="post" enctype="multipart/form-data">
<form method="post">
<label for="name">Username</label>
<input type="text" name="name" id="name" placeholder="John Doe">
......
......@@ -35,6 +35,17 @@ int main (string[] args) {
}
});
Test.add_func ("/valse/utils/decodeuri", () => {
string expected = "2 + 2 = 4, hello \"world!\"";
string encoded = "2+%2B+2+%3D+4%2C+hello+%22world!%22";
string result = decode_uri (encoded);
if (result != expected) {
print ("Expected %s, but got %s\n", expected, result);
Test.fail ();
}
});
Test.run ();
return 0;
......
......@@ -92,6 +92,10 @@ namespace Valse {
type = "REAL";
break;
}
if (type == "NONE") {
continue;
}
// We look if we are on the first property
if (first) {
// if so, we don't put a comma before
......@@ -145,13 +149,17 @@ namespace Valse {
string prop = spec.name.replace ("-", "_");
obj.get_property (prop, ref val);
vals += "\"" + ((DateTime) val).format ("%F %H-%M-%S.000") + "\"";
} else {
} else if (!spec.value_type.is_a (typeof (Object))) {
// we get the value of the variable
query += spec.name.replace ("-", "_");
GLib.Value val = GLib.Value (typeof (string));
string prop = spec.name.replace ("-", "_");
obj.get_property (prop, ref val);
vals += (string) val;
} else {
// removing ", " at the end
query = query[0:query.length - 2];
vals = vals[0:vals.length - 2];
}
// we close quotes, if needed
......@@ -206,6 +214,10 @@ namespace Valse {
// we choose the good type
Type val_type = typeof (string);
foreach (ParamSpec spec in ((ObjectClass) type.class_ref ()).list_properties ()) {
if (!(spec.value_type.name () in new string[]{ "gint", "gdouble", "gchararray", "GDateTime" })) {
continue;
}
if (spec.name.replace ("-", "_") == names [i]) {
val_type = spec.value_type;
}
......@@ -279,6 +291,10 @@ namespace Valse {
Type val_type = typeof (string); // the type of the variable to define
foreach (ParamSpec spec in ((ObjectClass) type.class_ref ()).list_properties ()) {
if (!(spec.value_type.name () in new string[]{ "gint", "gdouble", "gchararray", "GDateTime" })) {
continue;
}
if (first_prop == "") { // we set the name of the first prop, if it isn't already set
first_prop = spec.name.replace ("-", "_");
}
......@@ -334,6 +350,9 @@ namespace Valse {
int i = 1;
foreach (ParamSpec spec in up_version.get_class ().list_properties ()) {
if (!(spec.value_type.name () in new string[]{ "gint", "gdouble", "gchararray", "GDateTime" })) {
continue;
}
// for each property of the object
query += spec.name.replace ("-", "_");
......
......@@ -63,6 +63,9 @@ namespace Valse {
public Request (SCGI.Request base_req) {
bool is_multipart = false;
string boundary = "";
bool is_url_encoded = false;
int content_length = 0;
base_req.params.foreach ((entry) => {
var k = entry.key;
var v = entry.value;
......@@ -106,15 +109,20 @@ namespace Valse {
this.path = v;
} else if (k == "CONTENT_TYPE") {
try {
print ("content type : '%s'\n", v);
var multipart_re = new Regex ("multipart\\/form-data; boundary=(?<boundary>.*)");
MatchInfo info;
if (multipart_re.match (v, 0, out info)) {
boundary = info.fetch_named ("boundary").replace ("\r\n", "");
is_multipart = true;
} else if (v == "application/x-www-form-urlencoded") {
is_url_encoded = true;
}
} catch (Error err) {
log_error (err.message);
}
} else if (k == "CONTENT_LENGTH") {
content_length = int.parse (v);
}
return true;
});
......@@ -162,6 +170,28 @@ namespace Valse {
} catch (Error err) {
log_error (err.message);
}
} else if (is_url_encoded) {
try {
DataInputStream dis = (DataInputStream) base_req.input;
uint8[] data = {};
uint8 d;
for (int i = 0; i < content_length; i++) {
data += dis.read_byte ();
}
string line = (string) data;
line = line.replace ("\r", "");
line = line.replace ("\n", "");
foreach (string pair in line.split("&")) {
if (pair.split ("=").length != 2) {
assert_not_reached ();
}
var k = decode_uri (pair.split ("=")[0]);
var v = decode_uri (pair.split ("=")[1]);
this.form_data[k] = v;
}
} catch (Error err) {
log_error (err.message);
}
}
}
......
using Gee;
namespace Valse {
const string NAME = "Valse";
const string VERSION = "-dev";
......@@ -49,6 +51,64 @@ namespace Valse {
}
}
/**
* Decode an URI
*
* @param encoded The encoded URI to decode
*/
public string decode_uri (string encoded, bool for_form = true) {
HashMap<string, string> decoder = new HashMap<string, string> ();
if (for_form) {
decoder[" "] = "+";
} else {
decoder[" "] = "%20";
}
decoder["\r"] = "%0A";
decoder["\n"] = "%0D";
decoder["!"] = "%21";
decoder["\""] = "%22";
decoder["#"] = "%23";
decoder["$"] = "%24";
decoder["%"] = "%25";
decoder["&"] = "%26";
decoder["'"] = "%27";
decoder["("] = "%28";
decoder[")"] = "%29";
decoder["*"] = "%2A";
decoder["+"] = "%2B";
decoder[","] = "%2C";
decoder["-"] = "%2D";
decoder["."] = "%2E";
decoder["/"] = "%2F";
decoder[":"] = "%3A";
decoder[";"] = "%3B";
decoder["<"] = "%3C";
decoder["="] = "%3D";
decoder[">"] = "%3E";
decoder["?"] = "%3F";
decoder["@"] = "%40";
decoder["["] = "%5B";
decoder["\\"] = "%5C";
decoder["]"] = "%5D";
decoder["^"] = "%5E";
decoder["_"] = "%5F";
decoder["`"] = "%60";
decoder["{"] = "%7B";
decoder["|"] = "%7C";
decoder["}"] = "%7D";
decoder["~"] = "%7E";
string res = encoded;
foreach (var e in decoder.entries) {
res = res.replace (e.value, e.key);
}
return res;
}
/*
* Custom errors.
*/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment