|
| 1 | +/* |
| 2 | + Copyright (c) DataStax, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include "http_server.hpp" |
| 18 | + |
| 19 | +using datastax::String; |
| 20 | +using datastax::internal::Memory; |
| 21 | +using datastax::internal::OStringStream; |
| 22 | +using datastax::internal::ScopedMutex; |
| 23 | +using datastax::internal::core::Address; |
| 24 | +using datastax::internal::core::EventLoop; |
| 25 | +using datastax::internal::core::Task; |
| 26 | + |
| 27 | +String response(int status, const String& body = "", const String& content_type = "") { |
| 28 | + OStringStream ss; |
| 29 | + ss << "HTTP/1.0 " << status << " " << http_status_str(static_cast<http_status>(status)) << "\r\n"; |
| 30 | + if (!body.empty()) { |
| 31 | + ss << "Content-Type: "; |
| 32 | + if (content_type.empty()) { |
| 33 | + ss << "text/plain"; |
| 34 | + } else { |
| 35 | + ss << content_type; |
| 36 | + } |
| 37 | + ss << "\r\nContent-Length: " << body.size() << "\r\n\r\n" << body; |
| 38 | + } else { |
| 39 | + ss << "\r\n"; |
| 40 | + } |
| 41 | + |
| 42 | + return ss.str(); |
| 43 | +} |
| 44 | + |
| 45 | +using namespace mockssandra; |
| 46 | +using namespace mockssandra::http; |
| 47 | + |
| 48 | +void Server::listen() { |
| 49 | + server_connection_->listen(&event_loop_group_); |
| 50 | + server_connection_->wait_listen(); |
| 51 | +} |
| 52 | + |
| 53 | +void Server::close() { |
| 54 | + if (server_connection_) { |
| 55 | + server_connection_->close(); |
| 56 | + server_connection_->wait_close(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +bool Server::use_ssl(const String& key, const String& cert, const String& password /*= ""*/, |
| 61 | + const String& client_cert /*= ""*/) { |
| 62 | + return server_connection_->use_ssl(key, cert, password, client_cert); |
| 63 | +} |
| 64 | + |
| 65 | +Server::ClientConnection::ClientConnection(internal::ServerConnection* server_connection, |
| 66 | + Server* server) |
| 67 | + : internal::ClientConnection(server_connection) |
| 68 | + , path_(server->path()) |
| 69 | + , content_type_(server->content_type()) |
| 70 | + , response_body_(server->response_body()) |
| 71 | + , response_status_code_(server->response_status_code()) |
| 72 | + , enable_valid_response_(server->enable_valid_response()) |
| 73 | + , close_connnection_after_request_(server->close_connnection_after_request()) { |
| 74 | + http_parser_init(&parser_, HTTP_REQUEST); |
| 75 | + http_parser_settings_init(&parser_settings_); |
| 76 | + |
| 77 | + parser_.data = this; |
| 78 | + parser_settings_.on_url = on_url; |
| 79 | +} |
| 80 | + |
| 81 | +void Server::ClientConnection::on_read(const char* data, size_t len) { |
| 82 | + request_ = String(data, len); |
| 83 | + size_t parsed = http_parser_execute(&parser_, &parser_settings_, data, len); |
| 84 | + if (parsed < static_cast<size_t>(len)) { |
| 85 | + enum http_errno err = HTTP_PARSER_ERRNO(&parser_); |
| 86 | + fprintf(stderr, "%s: %s\n", http_errno_name(err), http_errno_description(err)); |
| 87 | + close(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +int Server::ClientConnection::on_url(http_parser* parser, const char* buf, size_t len) { |
| 92 | + ClientConnection* self = static_cast<ClientConnection*>(parser->data); |
| 93 | + self->handle_url(buf, len); |
| 94 | + return 0; |
| 95 | +} |
| 96 | + |
| 97 | +void Server::ClientConnection::handle_url(const char* buf, size_t len) { |
| 98 | + String path(buf, len); |
| 99 | + if (path.substr(0, path.find("?")) == path_) { // Compare without query parameters |
| 100 | + if (enable_valid_response_) { |
| 101 | + if (response_body_.empty()) { |
| 102 | + write(response(response_status_code_, request_)); // Echo response |
| 103 | + } else { |
| 104 | + write(response(response_status_code_, response_body_, content_type_)); |
| 105 | + } |
| 106 | + } else { |
| 107 | + write("Invalid HTTP server response"); |
| 108 | + } |
| 109 | + } else { |
| 110 | + write(response(404)); |
| 111 | + } |
| 112 | + // From the HTTP/1.0 protocol specification: |
| 113 | + // |
| 114 | + // > When an Entity-Body is included with a message, the length of that body may be determined in |
| 115 | + // > one of two ways. If a Content-Length header field is present, its value in bytes represents |
| 116 | + // > the length of the Entity-Body. Otherwise, the body length is determined by the closing of the |
| 117 | + // > connection by the server. |
| 118 | + if (close_connnection_after_request_) { |
| 119 | + close(); |
| 120 | + } |
| 121 | +} |
0 commit comments