This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeb.h
88 lines (70 loc) · 2.54 KB
/
Web.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Created by Frank Steiler on 4/17/17 as part of NOOBS4IoT (https://github.com/steilerDev/NOOBS4IoT)
//
// Web.h:
// This file contains several classes and helper functions providing basic socket communication based on standard
// C sockets. Besides the getIP() function (which uses QtNetwork) no external non-standard library is required.
// For more information see https://github.com/steilerDev/NOOBS4IoT/wiki.
//
// This file is licensed under a GNU General Public License v3.0 (c) Frank Steiler.
// See https://raw.githubusercontent.com/steilerDev/NOOBS4IoT/master/LICENSE for more information.
//
#ifndef WEB_WEB_H
#define WEB_WEB_H
#include <string>
#include <map>
#include <dirent.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <vector>
#include <fstream>
#include <sstream>
#ifdef QT_CORE_LIB
#include <QString>
#include <QtNetwork/QNetworkInterface>
#endif
#define BUFFER_SIZE 4096
#define HEADER_BUFFER 4096
using namespace std;
namespace Web {
class Web {
public:
map<string, string> header;
string body;
protected:
Web(int socket): body(), _socket(socket) {};
/*
* Fills header and body member attribute;
*/
bool receive();
/*
* Writes the header line, header and body to the socket, make sure that header line + header does not exceed HEADER BUFFER
*/
bool send();
// The header line (e.g. GET URI HTTP/1.1 or HTTP/1.1 200 OK)
string headerLine;
private:
// This function will read from the socket and store the result in receiveString
bool readReceive(string *bufferString);
// This function will parse the request in request string and populate header line, header and body
bool parseReceive(const string &bufferString, const bool skipHeader);
// This function will parse a single header line and add it to the header map
bool parseHeaderLine(const string &headerLineString);
// This function will parse a single body line and append it to the body string
bool parseBodyLine(const string &bodyLineString);
int _socket;
ssize_t _bytesRead;
};
// This helper function splits the given string by the sep char
vector<string> split(const string &text, const char sep);
#ifdef QT_CORE_LIB
// This helper returns the IP address of all available interfaces using the QtNetworking API
QString getIP();
#endif
}
#endif //WEB_WEB_H