Skip to content

Commit

Permalink
[#28] Added RESTServer to implement REST API
Browse files Browse the repository at this point in the history
Signed-off-by: Clovis Durand <cd.clovel19@gmail.com>

Basic HTTPRequest parser

Signed-off-by: Clovis Durand <cd.clovel19@gmail.com>
  • Loading branch information
Clovel committed Apr 8, 2020
1 parent d9b01cf commit fd647ac
Show file tree
Hide file tree
Showing 12 changed files with 1,005 additions and 5 deletions.
2 changes: 2 additions & 0 deletions generator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/inc
${CMAKE_CURRENT_SOURCE_DIR}/deps/initools/inc
${CMAKE_CURRENT_SOURCE_DIR}/deps/rapidjson/include
${CMAKE_CURRENT_SOURCE_DIR}/src/rest/inc
)

#------------------------------------------------------------------------------
Expand All @@ -137,6 +138,7 @@ include_directories(

# Dependencies
add_subdirectory(deps)
add_subdirectory(src/rest)

# Main build
add_subdirectory(src)
Expand Down
2 changes: 1 addition & 1 deletion generator/deps/initools
9 changes: 5 additions & 4 deletions generator/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES
#----------------------------------------------------------------------------
# The installation is prepended by the CMAKE_INSTALL_PREFIX variable
install(TARGETS ${CMAKE_PROJECT_NAME}-util
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)

install(TARGETS ${CMAKE_PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
Expand Down
7 changes: 7 additions & 0 deletions generator/src/rest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#
# Copyright (C) 2020 Clovis Durand
#
# -----------------------------------------------------------------------------

# Sub-directories -----------------------------------------
add_subdirectory(src)
112 changes: 112 additions & 0 deletions generator/src/rest/inc/HTTPRequest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* @brief HTTPRequest class
*
* @file HTTPRequest.hpp
*/

#ifndef HTTPREQUEST_HPP
#define HTTPREQUEST_HPP

#endif /* HTTPREQUEST_HPP */

/* Includes -------------------------------------------- */
/* C++ system */
#include <exception>
#include <string>
#include <vector>

/* Defines --------------------------------------------- */

/* Type definitions ------------------------------------ */
typedef enum _httpVersion {
HTTP_VERSION_UNKNOWN = 0U,
HTTP_VERSION_1_0,
HTTP_VERSION_1_1,
HTTP_VERSION_2_0
} httpVersion_t;

typedef enum _httpMethods {
HTTP_METHOD_UNKNOWN = 0U,
HTTP_METHOD_GET,
HTTP_METHOD_HEAD,
HTTP_METHOD_POST,
HTTP_METHOD_PUT,
HTTP_METHOD_DELETE,
HTTP_METHOD_CONNECT,
HTTP_METHOD_OPTIONS,
HTTP_METHOD_TRACE,
HTTP_METHOD_PATCH
} httpMethod_t;

typedef std::vector<std::string> httpBody_t;

/* Forward declarations -------------------------------- */

/* HTTPRequest exception ------------------------------- */
class HTTPRequestException : public std::exception {
virtual const char *what(void) const throw()
{
return "HTTPRequest exception occured !";
}
};

/* HTTPRequest class ----------------------------------- */
class HTTPRequest {
public:
/* Contructors */
HTTPRequest(const std::string &pMsg);

/* Destructor */
virtual ~HTTPRequest();

/* Getters */
std::string msg(void) const;
std::string methodStr(void) const;
httpMethod_t method(void) const;
std::string httpVersionStr(void) const;
httpVersion_t httpVersion(void) const;
std::string host(void) const;
std::string URL(void) const;
std::string userAgent(void) const;
std::string accept(void) const;
std::string acceptLanguage(void) const;
std::string acceptEncoding(void) const;
std::string referer(void) const;
std::string upgradeInsecureRequest(void) const;
std::string headers(void) const;
std::string URI(void) const;
std::string query(void) const;
httpBody_t body(void) const;
std::string payload(void) const;

/* Setters */
/* TODO */
// void setMsg(const std::string &pMsg);
// void setMethodStr(const std::string &pMsg);
// void setHTTPVersionStr(const std::string &pMsg);
// void setHeaders(const std::string &pMsg);
// void setURI(const std::string &pMsg);
// void setHost(const std::string &pMsg);
// void setURL(const std::string &pMsg);
// void setQuery(const std::string &pMsg);

protected:
private:
/* Request contents */
std::string mMsg; /**< Raw message received */
std::string mMethodStr; /**< HTTP method */
std::string mHTTPVersionStr; /**< HTTP Version */
std::string mURL; /**< HTTP URL */
std::string mHost; /**< HTTP Host */
std::string mUserAgent; /**< HTTP Client user agent */
std::string mAccept; /**< HTTP Accept field */
std::string mAcceptLanguage; /**< HTTP Accept language */
std::string mAcceptEncoding; /**< HTTP Accept encoding */
std::string mReferer; /**> HTTP referer */
std::string mUpgradeInsecureRequest; /**< HTTP UpgradeInsecureRequest flag */
std::string mHeaders; /**< HTTP Headers */
std::string mURI; /**< HTTP URI */
std::string mQuery; /**< HTTP Query */
httpBody_t mBody; /**< HTTP Body */
std::string mPayload; /**< HTTP payload */
};
65 changes: 65 additions & 0 deletions generator/src/rest/inc/RESTServer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @brief RESTServer class
*
* @file RESTServer.hpp
*/

#ifndef RESTSERVER_HPP
#define RESTSERVER_HPP

/* Includes -------------------------------------------- */
/* C++ system */
#include <exception>
#include <string>

/* Defines --------------------------------------------- */

/* Type definitions ------------------------------------ */

/* Forward declarations -------------------------------- */

/* RESTServer exception -------------------------------- */
class RESTServerException : public std::exception {
virtual const char *what(void) const throw()
{
return "RESTServer exception occured !";
}
};

/* RESTServer class ------------------------------------ */
class RESTServer {
public:
/* Contructors */
RESTServer(const std::string &pAddr, const std::string pPort, const std::string &pPath);

/* Destructor */
virtual ~RESTServer();

/* Getters */
std::string address(void) const;
std::string port(void) const;
std::string apiPath(void) const;

/* Server management */
bool open(void);
bool listen(void);
bool close(void);

protected:
private:
/* Request processing functions */
bool processClientMessage(const char * const pMsg, const size_t &pReadBytes, const int &pClientSocket) const;
// bool processGetRequest(const std::string &pMsg) const;
// bool processPutRequest(const std::string &pMsg) const;
// bool processPostRequest(const std::string &pMsg) const;
// bool processDelRequest(const std::string &pMsg) const;

/* Networking member variables */
int mServerSocket;

std::string mAddr;
std::string mPort;
std::string mPath;
};

#endif /* RESTSERVER_HPP */
136 changes: 136 additions & 0 deletions generator/src/rest/resources/webpage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Let There Be Light project
*
* @file webpage.cpp
*/

/* Includes -------------------------------------------- */
#include "webpage.hpp"

/* Defines --------------------------------------------- */

/* Variable declarations ------------------------------- */
/* Web page variables ---------------------------------- */
const char * const htmlResponseCode200 = R"=====(HTTP/1.1 200 OK
Content-type:text/html
Connection: close
)=====";

const char * const htmlResponseCode400 = R"=====(HTTP/1.0 400 BAD REQUEST
Content-Type: text/html
Connection: close
<!DOCTYPE html>
<html lang ="en">
<head>
<title>400 - BAD REQUEST</title>
</head>
<body>
Your browser sent a bad request,
such as a POST without a Content-Length.
</body>
</html>
)=====";

const char * const htmlResponseCode404 = R"=====(HTTP/1.0 404 NOT FOUND
Content-Type: text/html
Connection: close
<!DOCTYPE html>
<html lang ="en">
<head>
<title>404 - Not found</title>
</head>
<body>
The server could not fulfill your request because the resource specified"
is unavailable or nonexistent.
<br>"
Please check that you entered the correct URL.
</br>
</body>
</html>
)=====";

const char * const htmlResponseCode500 = R"=====(HTTP/1.0 500 Server internal error
Content-Type: text/html
Connection: close
<!DOCTYPE html>
<html lang ="en">
<head>
<title>500 - Server internal error</title>
</head>
<body>
Server-side error prohibited execution.
</body>
</html>
)=====";

const char * const htmlResponseCode501 = R"=====(HTTP/1.1 501 Method Not Implemented
Content-type:text/html
Connection: close
<!DOCTYPE html>
<html lang="en">
<head>
<title>
501 - Method Not Implemented
</title>
</head>
<body>
HTTP request method not supported.
</body>
</html>
)=====";

const char * const htmlDocType = R"=====(<!DOCTYPE html>
)=====";

const char * const htmlPageBegin = R"=====(<html lang="en">
)=====";

const char * const htmlPageEnd = R"=====(</html>
)=====";

// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
const char * const htmlHead = R"=====(
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<style>
html {
font-family: Helvetica;
display: inline-block;
margin: 0px auto;
text-align: center;
}
.button {
background-color: #195B6A;
border: none;
color: white;
padding: 16px 40px;
text-decoration: none;
font-size: 30px;
margin: 2px;
cursor: pointer;
}
.button2 {
background-color: #77878A;
}
</style>
</head>
)=====";

const char * const htmlGeneralKenobi = R"=====(
<html lang ="en">
<head>
<title>web-example</title>
</head>
<body>
- Hello there !
- General Kenobi !
</body>
</html>
)=====";
32 changes: 32 additions & 0 deletions generator/src/rest/resources/webpage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Let There Be Light project
*
* @file webpage.hpp
*/
#ifndef INDEX_HPP
#define INDEX_HPP

/* Includes -------------------------------------------- */

/* Defines --------------------------------------------- */

/* Forward declarations -------------------------------- */
class WiFiClient;

/* Variable declarations ------------------------------- */
extern const char * const htmlResponseCode200;
extern const char * const htmlResponseCode400;
extern const char * const htmlResponseCode404;
extern const char * const htmlResponseCode500;
extern const char * const htmlResponseCode501;

extern const char * const htmlDocType;
extern const char * const htmlPageBegin;
extern const char * const htmlPageEnd;
extern const char * const htmlHead;

extern const char * const htmlGeneralKenobi;

/* Web pages ------------------------------------------- */

#endif /* INDEX_HPP */
Loading

0 comments on commit fd647ac

Please sign in to comment.