forked from SimulPiscator/AirSane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpserver.h
134 lines (116 loc) · 4.33 KB
/
httpserver.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
AirSane Imaging Daemon
Copyright (C) 2018 Simul Piscator
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HTTPSERVER_H
#define HTTPSERVER_H
#include <string>
#include <iostream>
#include "basic/dictionary.h"
class HttpServer
{
public:
enum {
HTTP_OK = 200,
HTTP_CREATED = 201,
HTTP_BAD_REQUEST = 400,
HTTP_NOT_FOUND = 404,
HTTP_METHOD_NOT_ALLOWED = 405,
HTTP_SERVICE_UNAVAILABLE = 503,
};
static const char *HTTP_GET, *HTTP_POST, *HTTP_DELETE;
static const char
*HTTP_HEADER_CONTENT_LENGTH, *HTTP_HEADER_CONTENT_TYPE,
*HTTP_HEADER_LOCATION, *HTTP_HEADER_ACCEPT,
*HTTP_HEADER_USER_AGENT, *HTTP_HEADER_REFERER,
*HTTP_HEADER_TRANSFER_ENCODING, *HTTP_HEADER_CONNECTION,
*HTTP_HEADER_CONTENT_DISPOSITION, *HTTP_HEADER_REFRESH;
static const char
*MIME_TYPE_JPEG, *MIME_TYPE_PDF, *MIME_TYPE_PNG;
static std::string fileExtension(const std::string& mimeType);
HttpServer();
~HttpServer();
HttpServer(const HttpServer&) = delete;
HttpServer& operator=(const HttpServer&) = delete;
const std::string& hostname() const;
HttpServer& setInterfaceName(const std::string&);
const std::string& interfaceName() const;
HttpServer& setInterfaceIndex(int);
enum { anyInterface = -1, invalidInterface = -2 };
int interfaceIndex() const;
HttpServer& setPort(uint16_t port);
uint16_t port() const;
HttpServer& setBacklog(int);
int backlog() const;
bool run();
bool terminate(int status);
int terminationStatus() const;
class Request
{
public:
Request(std::istream&);
bool isValid() const { return mValid; }
const std::string& uri() const { return mUri; }
const std::string& method() const { return mMethod; }
const std::string& protocol() const { return mProtocol; }
const std::string& header(const std::string& s) const;
const Dictionary& headers() const { return mHeaders; }
int contentLength() const;
const std::string& content() const;
bool hasFormData() const;
const Dictionary& formData() const;
const std::istream& stream() const { return mStream; }
std::string& logInfo() { return mLogInfo; }
std::ostream& print(std::ostream&) const;
private:
std::istream& mStream;
bool mValid;
std::string mUri, mMethod, mProtocol, mLogInfo;
Dictionary mHeaders;
mutable std::string mContent;
mutable Dictionary mFormData;
};
class Response
{
public:
Response(std::ostream&);
~Response();
Response& setStatus(int status) { mStatus = status; return *this; }
int status() const { return mStatus; }
Response& setHeader(const std::string& key, const std::string& value);
Response& setHeader(const std::string& key, int value);
const std::string& header(const std::string& key) const;
std::ostream& send();
void sendWithContent(const std::string&);
bool sent() const { return mSent; }
std::streampos contentBegin() const { return mContentBegin; }
std::ostream& print(std::ostream&) const;
private:
std::ostream& sendHeaders();
bool mSent;
std::streampos mContentBegin;
int mStatus;
std::ostream& mStream;
Dictionary mHeaders;
struct Chunkstream;
Chunkstream* mpChunkstream;
};
protected:
virtual void onRequest(const Request&, Response&);
private:
struct Private;
Private* p;
};
inline std::ostream& operator<<(std::ostream& os, const HttpServer::Request& r) { return r.print(os); }
inline std::ostream& operator<<(std::ostream& os, const HttpServer::Response& r) { return r.print(os); }
#endif // HTTPSERVER_H