webcpp

A simple framework to organize C++ CGI applications

31
2
C++

webcpp

Codacy Badge

A simple framework to organize C++ CGI applications

Installation

In order to Install the library, run

user@name: git clone https://github.com/Wittmaxi/webcpp
user@name: cd webcpp
user@name: make install 

Include <webcpp/webcpp.hpp>.

!! IMPORTANT : compile with -lwcp

Compatibility and Future

This library was developed on and for *nix systems. A port to windows should be easy to do.
This library will keep backwards compatibility. Components which names are changed will have a typedef to the old name.

Purpose

WebCPP is a template engine to simplify the task of creating C++ Web-Applications.

It consists of

  1. A template engine that generates HTML code
  2. A set of tools that simplify the interaction with the user

Usage

In order to understand this quick walkthrough, you should read this first!

Output Stream

The entire set of tools outputs it’s response to the global variable WCP::wcpout. Wcpout has the following functions to retrieve the content. If none of them is called, the destructor will automatically output the entire content to the standard-output.

void outputToFile(const std::string &fileName);
void outputToSTDOUT();
std::string getString();

Every output should be done through wcpout!

HTTP Header

In order to tell the webserver what to do with your code output, you need an HTTP-Header.
This header is constituted by the class WCP::HTTPHeader Instantiate it and call it’s print function before doing any other output!
The HTTP Header also provides methods for setting cookies and other HTTP-Functionalities
void HTTPHeader::addCookie(const std::string &name, const std::string &value, const bool HTTPonly, const std::string &expiry)
This will set a cookie in the client’s browser.
void HTTPHeader::addFlag(const std::string &flag)
This function will add an unfiltered Line to the HTTP request. To use with care!

Once you are done providing the HTTPHeader with this information, call
wcpout << yourHTTPHeader;
to print it out!

Functionality

The template engine works by having blocks that accept an infinite amount of other Blocks as constructor elements.

For example, like this:

BlockType1 {
  BlockType2{ 
  },
  BlockType2 {
    BlockType2 {
    
    }, 
    BlockType3 {
    
    }
  }
}

-The entire Library is wrapped inside of the namespace WCP::.
-All Blocks inherit from WCP::Block


Any Block implemented by the Library can be outputted by means of std::cout.
Example:

std::cout << Document {

};

The entire Page should be wrapped inside of the Document block, which generates the <html> ... </html> part of the website.

Blocks commonly found in HTML-Headers

The <head> </head> is generated by the Block called Head. It accepts as constructor arguments an infinite amount of Blocks that inherit from WCP::HeaderObject


WCP::Title
Accepts a WCP::Text as argument. Creates the Website-Title


WCP::Include
Accepts three std::strings as argument. The first argument is the hyperreference, the second is the type of the included file and the third is the relationship to the current document.


WCP::IncludeScript
Accepts a std::string as argument, which is a hyperreference to a script document.


WCP::HttpEquiv
Accepts two std::strings as arguments. The first is the equiv argument of <meta http-equiv="">, the second is the value.


WCP::Charset
Accepts a std::string with the name of the charset of the page. The use of “utf-8” is recommended.


WCP::Meta
Accepts two std::strings, the first one being for the name attribute of the <meta> tag, the second for the content attribute.

Blocks commonly found in HTML-Bodies

The entire <body> </body> Section is generated by WCP::Body.


WCP::H1 -> WCP::H6
Generate the <h1> </h1> to <h6> </h6> tags respectively.


WCP::LineBreak
Generates a linebreak. One object of it is created, called Break.


WCP::Centered
Centers the content.


WCP::Table
Creates a table


WCP::Row
Creates a row in a table


WCP::Cell
Creates a Cell in a Table-Row


WCP::HighlightedCell
Creates a highlighted Cell in a Table-Row


WCP::Container
Creates a Container.


WCP::NavBar
Creates a navbar.


WCP::HyperLink
Creates an Hyperlink.


WCP::Marked
Creates a Marked section


WCP::Form
Creates a Form


WCP::Input
creates an input element inside of a form.


WCP::Paragraph
Creates a paragraph


WCP::Image
Includes an Image


WCP::Picture

Optional Wrapper around Images


WCP::Source
To be used inside of WCP::Picture. States a source for a picture.


WCP::HorizontalLine
Creates a Horizontal line. Can’t take any arguments.


WCP::Quote
Creates a blockquote


WCP::Label
Creates a Label


WCP::Text
Prints out Text that escapes dangerous characters.


WCP::UnformattedText
Prints out an unprocessed Text.


WCP::ConvenientText
Prints out a Text that escapes dangerous characters, changes line breaks “\n” to the html-tag “< br >” and replace tabulators “\t” with a box that is 40 pixels wide.


WCP::Function
Expects a std::function <void(void)> as argument. It doesn’t print anything, but runs the given function in between the opening parent and closing parent tag.

Attributes

HTML - Tags can have attributes. Any Block, that can take an infinite amount of Blocks can also take an Infinite amount of Attributes. Attributes accept a string that will be added to the HTML tag in the following way: <html_tag + "specified argument's string">. WCP::Attribute just adds a string, while the other attributes add a certain attribute specifier. Class attribute, for example, changes the tag to <html class="specified string">


WCP::Attribute
An attribute that can be anything.


WCP::ClassAttribute
Assigns one or multiple Css-Classes to the Block.


WCP::IdAttribute
Assigns one or multiple CSS-IDs to the Block.


WCP::StyleAttribute
Directly assigns Css-Style to the block


WCP::SourceAttribute
Assigns a content-source to a Block


WCP::HyperReference
Assigns a href attribute to a Block.


WCP::FormMethod
Sets the Method of data processing of a Form. Possible values “GET” or “POST”


WCP::InputType
Sets the type of an Input Block


WCP::InputValue
Sets the standard value of an Input Block.

Environment Variables

All the Environment Variables are handled by the Namespace WCP::ENV.
To initialize the handler, use
WCP::ENV::initializeENV().
Then, you can get both the arguments passed as POST and GET request with both

WCP::ENV::GET (std::string name) and WCP::ENV::POST(std::string name)
Both functions return a UTF8-String that has already escaped the browser escape signals.

WCP::ENV::POSTFILE (std::string name) in multipart encoded post-requests, files that were sent can be retrieved by means of POSTFILE. It returns a struct File which looks like this:

struct FILE {
    std::string filename;
    std::string datatype;
    std::string content;
};

WCP::ENV::COOKIE (std::string name)
Returns the value of the cookie with name “name”.