Skip to content

Latest commit

 

History

History
91 lines (63 loc) · 2.35 KB

REDIRECTION.md

File metadata and controls

91 lines (63 loc) · 2.35 KB

Redirection Module Tutorial

Let's implement a simple redirection module.

Purpose

This module will handle incoming http requests and send back a redirection response. For example we might want to redirect all requests to google.com.

So for the following incoming request:

GET /google HTTP/1.1

We'll then get the following response:

HTTP/1.1 301 Moved Permanently
Location: www.google.com

Tutorial

First, let's implement the IHandlerModule interface base.

#include "ziapi/Module.hpp"

class PhpCgiModule : public ziapi::IHandlerModule {
public:
    void Init(const ziapi::config::Node &cfg) override {}

    [[nodiscard]] Version GetVersion() const noexcept { return {4, 0, 0}; }

    [[nodiscard]] Version GetCompatibleApiVersion() const noexcept { return {4, 0, 0}; }

    [[nodiscard]] const char *GetName() const noexcept { return "Redirection Module"; }

    [[nodiscard]] const char *GetDescription() const noexcept { return "Redirects the request to another location."; }

    [[nodiscard]] double GetHandlerPriority() const noexcept {
        // Directions should be treated in priority
        return 0.9;
    }

    void Handle(ziapi::http::Context &ctx, const ziapi::http::Request &req, ziapi::http::Response &res) override {}
};

Let's give our module a configuration. Let's load from the config the route to which we will redirect requests. We'll store the value in a member variable of our module called redirection_route_.

...

void Init(const Config &cfg)
{
    /// We'll load from the configuration where to redirect to!
    redirection_route_ = cfg["modules"]["redirection"]["route"];
}

...

We want to redirect all requests so we just return true in our ShouldHandle().

...

[[nodiscard]] bool ShouldHandle(const http::Context &, const http::Request &req) const
{
    return true;
}

...

Let's now implement the Handle() method. We simply redirect each request to the redirection_route_ by changing the Location header on the response. We also set the appropriate status code.

...

void Handle(http::Context &, const http::Request &, http::Response &res)
{
    res.headers[ziapi::http::header::kLocation] = redirection_route_;
    res.status_code = ziapi::http::code::kMovedPermanently;
}

...

You can check the full source code for this example here.