-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8eca5c
commit 67bbebd
Showing
8 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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,5 @@ | ||
# Forms | ||
|
||
A basic form parsing example. | ||
|
||
**Note:** The HTML template is in the sounds folder, this should be names server but due to a bug in the current develop branch of skm, all server resources need to be put in the sounds folder (this might be fixed by the time your reading this though). |
This file contains 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,44 @@ | ||
#include "../../src/firestorm.h" | ||
|
||
string name; | ||
|
||
class HelloForm : public Route { | ||
public: | ||
UriArgs uri_args; | ||
FormData form_data; | ||
|
||
Outcome middlewares() { | ||
return MiddleWares<HelloForm>() | ||
.add(new Router<HelloForm>(HTTP_POST_METHOD, {"/hello"})) | ||
.add(new Form<HelloForm>()) | ||
.outcome(*this); | ||
} | ||
|
||
Response response() { | ||
name = form_data["name"]; | ||
return redirect("/hello"); | ||
} | ||
}; | ||
|
||
class Hello : public Route { | ||
public: | ||
UriArgs uri_args; | ||
|
||
Outcome middlewares() { | ||
return MiddleWares<Hello>() | ||
.add(new Router<Hello>(HTTP_GET_METHOD, {"/hello"})) | ||
.outcome(*this); | ||
} | ||
|
||
Response response() { | ||
return html("hello.html", {{"name", name}}, HTTP_STATUS_OK); | ||
} | ||
}; | ||
|
||
int main() { | ||
FireStorm() | ||
.add_route(new Hello()) | ||
.add_route(new HelloForm()) | ||
.ignite(5000); | ||
return 0; | ||
} |
This file contains 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 @@ | ||
This folder should be named server but currently on the develop branch of skm all the server resources are in the sounds folder... |
This file contains 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,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en" dir="ltr"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Hello</title> | ||
</head> | ||
<body> | ||
<form action="/hello" method="post"> | ||
<label for="name">Name: </label> | ||
<input type="text" id="name" name="name"><br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
|
||
<h1>Hello {name}</h1> | ||
</body> | ||
</html> |
This file contains 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
This file contains 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,14 @@ | ||
#include "form.h" | ||
|
||
// Parse a url encoded form | ||
FormData parse_form(string data) { | ||
FormData result; | ||
|
||
for (string field : split_at(data, '&')) { | ||
vector<string> parts = split_at(field, '='); | ||
string name = parts.front(); | ||
result[name] = parts.back(); | ||
} | ||
|
||
return result; | ||
} |
This file contains 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,29 @@ | ||
#ifndef FIRESTORM_FORM | ||
#define FIRESTORM_FORM | ||
|
||
#include "../include/splashkit/splashkit.h" | ||
#include <unordered_map> | ||
|
||
#include "headers.h" | ||
#include "middleware.h" | ||
#include "utils.h" | ||
|
||
using FormData = std::unordered_map<string, string>; | ||
|
||
FormData parse_form(string data); | ||
|
||
// A middleware to parse urlencoded forms | ||
template <typename R> class Form : public MiddleWare<R> { | ||
public: | ||
Outcome outcome(R &route, http_request request) { | ||
if (headers(request)["Content-Type"] != | ||
"application/x-www-form-urlencoded") { | ||
return Outcome::Failure; | ||
} | ||
|
||
route.form_data = parse_form(request_body(request)); | ||
return Outcome::Success; | ||
} | ||
}; | ||
|
||
#endif |