-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.h
More file actions
107 lines (91 loc) · 2.68 KB
/
httpserver.h
File metadata and controls
107 lines (91 loc) · 2.68 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright (C) 2012 Lars Hall
*
* This file is part of StreamRecorder.
*
* StreamRecorder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* StreamRecorder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with StreamRecorder. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HTTPSERVER_H
#define HTTPSERVER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string>
#include <fstream>
#include <sys/stat.h>
#include <list>
#include <vector>
#include <iostream>
#include "thread.h"
#define HTTP_MAX_PENDING 10
#define HTTP_MAX_BUFFER 1024
#define HTTP_MAX_TOKEN 1024
#define HTTP_MAX_FILE_SIZE 100000
using std::string;
using std::ifstream;
using std::ios;
using std::pair;
using std::list;
using std::vector;
class WebFrontend;
struct Request;
class HttpServer : public Thread
{
public:
typedef list<pair<string, string> > RequestParams;
enum RequestType
{
HTTP_GET = 0,
HTTP_POST,
HTTP_DELETE,
HTTP_PUT,
HTTP_UNKNOWN
};
private:
WebFrontend *frontend;
string serverPath;
bool started;
int sockfd;
short port;
void startServer();
void parseRequest(Request &request, const string &req);
/// Retuns start of param list if any otherwise -1
int parseReqParams(RequestParams ¶ms,
const string &req, bool isPost);
/// returns -1 if file not found or cannot read
int loadFile(uint8_t *bytes, const string &filename);
string createHeader(int status, const string &contentType,
const Request &request, const string &reason = "");
/// Url decodes a string (percent decoding)
string percentDecode(const string &data);
public:
HttpServer(uint16_t port, const string &serverPath,
WebFrontend *frontend);
static void splitString(vector<string> &tokens,
const string &str, const string &sep);
~HttpServer();
void run();
};
struct Request
{
string requestStr;
string path;
HttpServer::RequestType type;
HttpServer::RequestParams params;
};
#endif