| 177 | } |
| 178 | |
| 179 | bool Server::Start() { |
| 180 | LOG(TRACE) << "Entering Server::Start"; |
| 181 | |
| 182 | std::string listening_port_option = this->GetListeningPorts(true); |
| 183 | this->options_["listening_ports"] = listening_port_option; |
| 184 | |
| 185 | std::string acl_option = this->GetAccessControlList(); |
| 186 | if (acl_option.size() > 0) { |
| 187 | this->options_["access_control_list"] = acl_option; |
| 188 | } |
| 189 | |
| 190 | this->options_["enable_keep_alive"] = "yes"; |
| 191 | |
| 192 | std::vector<const char*> options; |
| 193 | this->GenerateOptionsList(&options); |
| 194 | |
| 195 | mg_callbacks callbacks = {}; |
| 196 | callbacks.begin_request = &OnNewHttpRequest; |
| 197 | context_ = mg_start(&callbacks, this, &options[0]); |
| 198 | if (context_ == NULL) { |
| 199 | std::string ipv4_port_option = this->GetListeningPorts(false); |
| 200 | if (listening_port_option == ipv4_port_option) { |
| 201 | // If the IPv4 and IPv6 versions of the port option string |
| 202 | // are equal, then either a host to bind to or an ACL was |
| 203 | // specified, so there is no need to retry. |
| 204 | LOG(WARN) << "Failed to start Civetweb"; |
| 205 | return false; |
| 206 | } else { |
| 207 | // If we fail, a host and ACL aren't specified, we might not |
| 208 | // be able to bind to an IPv6 address. Try again to bind to |
| 209 | // the IPv4 loopback only. |
| 210 | LOG(INFO) << "Failed first attempt to start Civetweb. Attempt start with IPv4 only"; |
| 211 | this->options_["listening_ports"] = listening_port_option; |
| 212 | options.clear(); |
| 213 | this->GenerateOptionsList(&options); |
| 214 | context_ = mg_start(&callbacks, this, &options[0]); |
| 215 | if (context_ == NULL) { |
| 216 | LOG(WARN) << "Failed to start Civetweb"; |
| 217 | return false; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | void Server::Stop() { |
| 225 | LOG(TRACE) << "Entering Server::Stop"; |
no test coverage detected