59 std::string sport, sthrds;
61 if (fHttpPort.length()>0) sport = fHttpPort;
62 if (fHttpsPort.length()>0) {
63 if (!sport.empty()) sport.append(
",");
64 sport.append(fHttpsPort);
69 DOUT0(
"Starting HTTP server on port(s) %s", sport.c_str());
71 const char *options[100];
74 options[op++] =
"listening_ports";
75 options[op++] = sport.c_str();
76 options[op++] =
"num_threads";
77 options[op++] = sthrds.c_str();
79 if (!fSslCertif.empty()) {
80 options[op++] =
"ssl_certificate";
81 options[op++] = fSslCertif.c_str();
84 if (!fAuthFile.empty() && !fAuthDomain.empty()) {
85 options[op++] =
"global_auth_file";
86 options[op++] = fAuthFile.c_str();
87 options[op++] =
"authentication_domain";
88 options[op++] = fAuthDomain.c_str();
96 fCtx = mg_start(&fCallbacks,
this, options);
100 if (fCtx==0)
EOUT(
"Fail to start civetweb on port %s", sport.c_str());
108 EOUT(
"civetweb: %s",message);
116 const struct mg_request_info *request_info = mg_get_request_info(conn);
118 if (server==0)
return 0;
120 DOUT3(
"BEGIN_REQ: uri:%s query:%s", request_info->local_uri, request_info->query_string);
122 std::string filename;
125 mg_send_file(conn, filename.c_str());
129 std::string content_type, content_header, content_str;
132 if (!server->
Process(request_info->local_uri, request_info->query_string,
133 content_type, content_header, content_str, content_bin)) {
134 mg_printf(conn,
"HTTP/1.1 404 Not Found\r\n"
135 "Content-Length: 0\r\n"
136 "Connection: close\r\n\r\n");
139 if (content_type==
"__file__") {
140 mg_send_file(conn, content_str.c_str());
143 if (!content_bin.
null()) {
145 "HTTP/1.1 200 OK\r\n"
146 "Content-Type: %s\r\n"
148 "Content-Length: %u\r\n"
149 "Connection: keep-alive\r\n"
151 content_type.c_str(),
152 content_header.c_str(),
159 "HTTP/1.1 200 OK\r\n"
160 "Content-Type: %s\r\n"
162 "Content-Length: %d\r\n"
165 content_type.c_str(),
166 content_header.c_str(),
167 (
int) content_str.length(),
168 content_str.c_str());
Reference on memory from memory pool.
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Represents command with its arguments.
bool AsBool(bool dflt=false) const
std::string AsStr(const std::string &dflt="") const
int64_t AsInt(int64_t dflt=0) const
bool null() const
Returns true if reference contains nullptr.
virtual void OnThreadAssigned()
RecordField Cfg(const std::string &name, Command cmd=nullptr) const
Returns configuration field of specified name Configuration value of specified name searched in follo...
Server provides http access to DABC
Civetweb(const std::string &name, dabc::Command cmd=nullptr)
virtual void OnThreadAssigned()
static int log_message_handler(const struct mg_connection *conn, const char *message)
std::string fSslCertif
SSL certificate file name in plm format.
std::string fAuthFile
authentication file, generated by htdigest utility
int fNumThreads
number of civetweb threads
static int begin_request_handler(struct mg_connection *conn, void *)
struct mg_callbacks fCallbacks
std::string fHttpPort
port number for HTTP server
std::string fAuthDomain
realm parameter in authentication, "dabc@server" is default
std::string fHttpsPort
port number for HTTPS server
Server provides http access to DABC
int fDefaultAuth
0 - false, 1 - true, -1 - ignored
bool IsFileRequested(const char *uri, std::string &fname)
Check if file is requested.
bool Process(const char *uri, const char *query, std::string &content_type, std::string &content_header, std::string &content_str, dabc::Buffer &content_bin)
Method process different URL requests, should be called from server thread.
std::string format(const char *fmt,...)