-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhttpserver.h
175 lines (151 loc) · 4.68 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
AirSane Imaging Daemon
Copyright (C) 2018-2023 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 "basic/dictionary.h"
#include <iostream>
#include <string>
#include <cstdint>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.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_CONFLICT = 409,
HTTP_SERVICE_UNAVAILABLE = 503,
};
static std::string statusReason(int status);
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);
static std::string toRelativeUrl(const std::string&);
typedef union
{
sockaddr sa;
sockaddr_in in;
sockaddr_in6 in6;
sockaddr_un un;
} Sockaddr;
static std::string ipString(const Sockaddr&);
static uint16_t portNumber(const Sockaddr&);
HttpServer();
virtual ~HttpServer();
HttpServer(const HttpServer&) = delete;
HttpServer& operator=(const HttpServer&) = delete;
HttpServer& setInterfaceName(const std::string&);
const std::string& interfaceName() const;
HttpServer& setInterfaceIndex(int);
enum
{
anyInterface = -1,
invalidInterface = 0
};
int interfaceIndex() const;
HttpServer& setPort(uint16_t port);
uint16_t port() const;
HttpServer& setUnixSocket(const std::string&);
const std::string& unixSocket() const;
HttpServer& setBacklog(int);
int backlog() const;
HttpServer& applyAccessFile(const class AccessFile&);
bool run();
bool terminate(int status);
int terminationStatus() const;
int lastError() const;
class Request
{
public:
explicit 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:
explicit 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();
std::ostream& mStream;
bool mSent;
std::streampos mContentBegin;
int mStatus;
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