-
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.
[#28] Added RESTServer to implement REST API
Signed-off-by: Clovis Durand <cd.clovel19@gmail.com> Basic HTTPRequest parser Signed-off-by: Clovis Durand <cd.clovel19@gmail.com>
- Loading branch information
Showing
12 changed files
with
1,005 additions
and
5 deletions.
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
Submodule initools
updated
3 files
+4 −21 | CMakeLists.txt | |
+2 −2 | docs/CMakeLists.txt | |
+1 −1 | tests/CMakeLists.txt |
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,7 @@ | ||
# | ||
# Copyright (C) 2020 Clovis Durand | ||
# | ||
# ----------------------------------------------------------------------------- | ||
|
||
# Sub-directories ----------------------------------------- | ||
add_subdirectory(src) |
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,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 */ | ||
}; |
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,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 */ |
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,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> | ||
)====="; |
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,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 */ |
Oops, something went wrong.