Skip to content

Commit

Permalink
Add form parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
hugglesfox committed Sep 13, 2020
1 parent a8eca5c commit 67bbebd
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ For a more complete example, see [examples/todo/](https://github.com/hugglesfox/
- All interactions with raw http requests are managed with middlewares.
- Middlewares are pluggable therefore are enabled an a route to route basis.
- If a middleware fails then the request isn't handled by that route.
- Various built in middlewares are available for routing, form parsing (WIP) and CSRF token management (WIP).
- Various built in middlewares are available for routing and form parsing.
- The built in router middleware allows for variable path and argument handling.
- Yes that does mean you do not have to use the built in router if you wish.
Expand Down
5 changes: 5 additions & 0 deletions examples/forms/README.md
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).
44 changes: 44 additions & 0 deletions examples/forms/forms.cpp
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;
}
1 change: 1 addition & 0 deletions examples/forms/sounds/README.md
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...
16 changes: 16 additions & 0 deletions examples/forms/sounds/hello.html
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>
1 change: 1 addition & 0 deletions src/firestorm.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "cookie.h"
#include "error.h"
#include "form.h"
#include "headers.h"
#include "html.h"
#include "middleware.h"
Expand Down
14 changes: 14 additions & 0 deletions src/form.cpp
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;
}
29 changes: 29 additions & 0 deletions src/form.h
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

0 comments on commit 67bbebd

Please sign in to comment.