/usr/include/ola/http/HTTPServer.h is in libola-dev 0.10.5.nojsmin-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | /*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* HTTPServer.h
* The Base HTTP Server class.
* Copyright (C) 2005 Simon Newton
*/
#ifndef INCLUDE_OLA_HTTP_HTTPSERVER_H_
#define INCLUDE_OLA_HTTP_HTTPSERVER_H_
#include <ola/Callback.h>
#include <ola/base/Macro.h>
#include <ola/io/Descriptor.h>
#include <ola/io/SelectServer.h>
#include <ola/thread/Thread.h>
#include <ola/web/Json.h>
// 0.4.6 of microhttp doesn't include stdarg so we do it here.
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <ola/win/CleanWinSock2.h>
#else
#include <sys/select.h>
#include <sys/socket.h>
#endif // _WIN32
#include <microhttpd.h>
#include <map>
#include <set>
#include <string>
#include <vector>
namespace ola {
namespace http {
/*
* Represents the HTTP request
*/
class HTTPRequest {
public:
HTTPRequest(const std::string &url,
const std::string &method,
const std::string &version,
struct MHD_Connection *connection);
~HTTPRequest();
bool Init();
// accessors
const std::string Url() const { return m_url; }
const std::string Method() const { return m_method; }
const std::string Version() const { return m_version; }
void AddHeader(const std::string &key, const std::string &value);
void AddPostParameter(const std::string &key, const std::string &value);
void ProcessPostData(const char *data, size_t *data_size);
const std::string GetHeader(const std::string &key) const;
bool CheckParameterExists(const std::string &key) const;
const std::string GetParameter(const std::string &key) const;
const std::string GetPostParameter(const std::string &key) const;
bool InFlight() const { return m_in_flight; }
void SetInFlight() { m_in_flight = true; }
private:
std::string m_url;
std::string m_method;
std::string m_version;
struct MHD_Connection *m_connection;
std::map<std::string, std::string> m_headers;
std::map<std::string, std::string> m_post_params;
struct MHD_PostProcessor *m_processor;
bool m_in_flight;
static const unsigned int K_POST_BUFFER_SIZE = 1024;
DISALLOW_COPY_AND_ASSIGN(HTTPRequest);
};
/*
* Represents the HTTP Response
*/
class HTTPResponse {
public:
explicit HTTPResponse(struct MHD_Connection *connection):
m_connection(connection),
m_status_code(MHD_HTTP_OK) {}
void Append(const std::string &data) { m_data.append(data); }
void SetContentType(const std::string &type);
void SetHeader(const std::string &key, const std::string &value);
void SetStatus(unsigned int status) { m_status_code = status; }
void SetNoCache();
int SendJson(const ola::web::JsonValue &json);
int Send();
struct MHD_Connection *Connection() const { return m_connection; }
private:
std::string m_data;
struct MHD_Connection *m_connection;
typedef std::multimap<std::string, std::string> HeadersMultiMap;
HeadersMultiMap m_headers;
unsigned int m_status_code;
DISALLOW_COPY_AND_ASSIGN(HTTPResponse);
};
/**
* @addtogroup http_server
* @{
* @class HTTPServer
* @brief The base HTTP Server.
*
* This is a simple HTTP Server built around libmicrohttpd. It runs in a
* separate thread.
*
* @examplepara
* @code
* HTTPServer::HTTPServerOptions options;
* options.port = ...;
* HTTPServer server(options);
* server.Init();
* server.Run();
* // get on with life and later...
* server.Stop();
* @endcode
* @}
*/
class HTTPServer: public ola::thread::Thread {
public:
typedef ola::Callback2<int, const HTTPRequest*, HTTPResponse*>
BaseHTTPCallback;
struct HTTPServerOptions {
public:
// The port to listen on
uint16_t port;
// The root for content served with ServeStaticContent();
std::string data_dir;
HTTPServerOptions()
: port(0),
data_dir("") {
}
};
explicit HTTPServer(const HTTPServerOptions &options);
virtual ~HTTPServer();
bool Init();
void *Run();
void Stop();
void UpdateSockets();
/**
* Called when there is HTTP IO activity to deal with. This is a noop as
* MHD_run is called in UpdateSockets above.
*/
void HandleHTTPIO() {}
int DispatchRequest(const HTTPRequest *request, HTTPResponse *response);
// Register a callback handler.
bool RegisterHandler(const std::string &path, BaseHTTPCallback *handler);
// Register a file handler.
bool RegisterFile(const std::string &path,
const std::string &content_type);
bool RegisterFile(const std::string &path,
const std::string &file,
const std::string &content_type);
// Set the default handler.
void RegisterDefaultHandler(BaseHTTPCallback *handler);
void Handlers(std::vector<std::string> *handlers) const;
const std::string DataDir() const { return m_data_dir; }
// Return an error
int ServeError(HTTPResponse *response, const std::string &details = "");
int ServeNotFound(HTTPResponse *response);
static int ServeRedirect(HTTPResponse *response, const std::string &location);
// Return the contents of a file.
int ServeStaticContent(const std::string &path,
const std::string &content_type,
HTTPResponse *response);
static const char CONTENT_TYPE_PLAIN[];
static const char CONTENT_TYPE_HTML[];
static const char CONTENT_TYPE_GIF[];
static const char CONTENT_TYPE_PNG[];
static const char CONTENT_TYPE_CSS[];
static const char CONTENT_TYPE_JS[];
static const char CONTENT_TYPE_OCT[];
// Expose the SelectServer
ola::io::SelectServer *SelectServer() { return m_select_server.get(); }
static struct MHD_Response *BuildResponse(void *data, size_t size);
private :
typedef struct {
std::string file_path;
std::string content_type;
} static_file_info;
struct DescriptorState {
public:
explicit DescriptorState(ola::io::UnmanagedFileDescriptor *_descriptor)
: descriptor(_descriptor), read(0), write(0) {}
ola::io::UnmanagedFileDescriptor *descriptor;
uint8_t read : 1;
uint8_t write : 1;
uint8_t : 6;
};
struct Descriptor_lt {
bool operator()(const DescriptorState *d1,
const DescriptorState *d2) const {
return d1->descriptor->ReadDescriptor() <
d2->descriptor->ReadDescriptor();
}
};
typedef std::set<DescriptorState*, Descriptor_lt> SocketSet;
struct MHD_Daemon *m_httpd;
std::auto_ptr<ola::io::SelectServer> m_select_server;
SocketSet m_sockets;
std::map<std::string, BaseHTTPCallback*> m_handlers;
std::map<std::string, static_file_info> m_static_content;
BaseHTTPCallback *m_default_handler;
unsigned int m_port;
std::string m_data_dir;
int ServeStaticContent(static_file_info *file_info,
HTTPResponse *response);
void InsertSocket(bool is_readable, bool is_writeable, int fd);
void FreeSocket(DescriptorState *state);
DISALLOW_COPY_AND_ASSIGN(HTTPServer);
};
} // namespace http
} // namespace ola
#endif // INCLUDE_OLA_HTTP_HTTPSERVER_H_
|