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
|
// systemtap compile-server web api server header
// Copyright (C) 2017-2020 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#ifndef __SERVER_H__
#define __SERVER_H__
#include <tuple>
#include <vector>
#include <string>
#include <mutex>
#include <map>
#include <condition_variable>
extern "C"
{
#include <microhttpd.h>
}
#if MHD_VERSION >= 0x00097002
// libmicrohttpd 0.9.71 broke API
#define MHD_RESULT enum MHD_Result
#else
#define MHD_RESULT int
#endif
using namespace std;
struct response
{
unsigned int status_code;
map<string, string> headers;
string file;
string content;
string content_type;
response(const unsigned int code,
const string &type = "text/html; charset=UTF-8") :
status_code(code), content_type(type)
{
}
};
extern response get_404_response();
typedef map<string, vector<string>> post_params_t;
struct request
{
post_params_t params;
string base_dir;
map<string, vector<string>> files;
vector<string> matches;
#if 0
request_headers headers;
std::string body;
#endif
};
struct request_handler
{
string name;
virtual response GET(const request &req);
virtual response PUT(const request &req);
virtual response POST(const request &req);
virtual response DELETE(const request &req);
request_handler(string n) : name(n) {}
};
class server
{
public:
void start();
void wait();
void stop();
server(uint16_t port, std::string &cert_db_path);
~server()
{
stop();
}
void add_request_handler(const string &url_path_re,
request_handler &handler);
const std::string & get_cert_db_path() const { return cert_db_path; }
private:
condition_variable running_cv;
mutex srv_mutex;
uint16_t port;
struct MHD_Daemon *dmn_ipv4;
// FIXME: IPv6 support needed
std::string cert_db_path;
// FIXME: should this be a map?
vector<tuple<string, request_handler *>> request_handlers;
static MHD_RESULT access_handler_shim(void *cls,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **con_cls);
MHD_RESULT access_handler(struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **con_cls);
static void
request_completed_handler_shim(void *cls,
struct MHD_Connection *connection,
void **con_cls,
enum MHD_RequestTerminationCode toe);
void request_completed_handler(struct MHD_Connection *connection,
void **con_cls,
enum MHD_RequestTerminationCode toe);
MHD_RESULT queue_response(const response &response, MHD_Connection *connection);
};
#endif /* __SERVER_H__ */
|