Skip to content

Commit

Permalink
Refine the connection close
Browse files Browse the repository at this point in the history
  • Loading branch information
sprinfall committed Jul 15, 2019
1 parent 2a20030 commit 46ea52c
Show file tree
Hide file tree
Showing 24 changed files with 214 additions and 147 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,17 @@ int main(int argc, char* argv[]) {
// ...
try {
webcc::Server server(8080, 2);
webcc::Server server(8080);
server.Route("/books", std::make_shared<BookListView>(), { "GET", "POST" });
server.Route("/books",
std::make_shared<BookListView>(),
{ "GET", "POST" });
server.Route(webcc::R("/books/(\\d+)"), std::make_shared<BookDetailView>(),
server.Route(webcc::R("/books/(\\d+)"),
std::make_shared<BookDetailView>(),
{ "GET", "PUT", "DELETE" });
server.Run();
server.Start();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
Expand Down
15 changes: 13 additions & 2 deletions examples/client_basics.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cassert>
#include <iostream>

#include "webcc/client_session.h"
Expand All @@ -11,8 +12,6 @@ int main() {
webcc::ResponsePtr r;

try {
r = session.Head("http://httpbin.org/get");

r = session.Request(webcc::RequestBuilder{}.
Get("http://httpbin.org/get").
Query("key1", "value1").
Expand All @@ -21,20 +20,32 @@ int main() {
Header("Accept", "application/json")
());

assert(r->status() == webcc::Status::kOK);
assert(!r->data().empty());

r = session.Get("http://httpbin.org/get",
{ "key1", "value1", "key2", "value2" },
{ "Accept", "application/json" });

assert(r->status() == webcc::Status::kOK);
assert(!r->data().empty());

r = session.Request(webcc::RequestBuilder{}.
Post("http://httpbin.org/post").
Body("{'name'='Adam', 'age'=20}").
Json().Utf8()
());

assert(r->status() == webcc::Status::kOK);
assert(!r->data().empty());

#if WEBCC_ENABLE_SSL

r = session.Get("https://httpbin.org/get");

assert(r->status() == webcc::Status::kOK);
assert(!r->data().empty());

#endif // WEBCC_ENABLE_SSL

} catch (const webcc::Error& error) {
Expand Down
6 changes: 2 additions & 4 deletions examples/file_upload_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,12 @@ int main(int argc, char* argv[]) {

std::uint16_t port = static_cast<std::uint16_t>(std::atoi(argv[1]));

std::size_t workers = 2;

try {
webcc::Server server(port, workers);
webcc::Server server(port);

server.Route("/upload", std::make_shared<FileUploadView>(), { "POST" });

server.Run();
server.Start();

} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
Expand Down
5 changes: 4 additions & 1 deletion examples/hello_world_server.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "webcc/logger.h"
#include "webcc/response_builder.h"
#include "webcc/server.h"

Expand All @@ -13,12 +14,14 @@ class HelloView : public webcc::View {
};

int main() {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);

try {
webcc::Server server(8080);

server.Route("/", std::make_shared<HelloView>());

server.Run();
server.Start();

} catch (const std::exception&) {
return 1;
Expand Down
3 changes: 3 additions & 0 deletions examples/rest_book_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,8 @@ int main(int argc, char* argv[]) {
PrintBookList(books);
}

std::cout << "Press any key to exit: ";
std::getchar();

return 0;
}
4 changes: 2 additions & 2 deletions examples/rest_book_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ int main(int argc, char* argv[]) {
std::size_t workers = 2;

try {
webcc::Server server(port, workers);
webcc::Server server(port);

server.Route("/books",
std::make_shared<BookListView>(sleep_seconds),
Expand All @@ -237,7 +237,7 @@ int main(int argc, char* argv[]) {
std::make_shared<BookDetailView>(sleep_seconds),
{ "GET", "PUT", "DELETE" });

server.Run();
server.Start(workers);

} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions examples/static_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ int main(int argc, char* argv[]) {
std::string doc_root = argv[2];

try {
webcc::Server server(port, 1, doc_root);
webcc::Server server(port, doc_root);

server.Run();
server.Start();

} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
Expand Down
1 change: 1 addition & 0 deletions webcc/body.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace webcc {

class Body {
public:
Body() = default;
virtual ~Body() = default;

// Get the size in bytes of the body.
Expand Down
26 changes: 12 additions & 14 deletions webcc/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ Error Client::Request(RequestPtr request, bool connect) {
// Response to HEAD could also have Content-Length.
// Set this flag to skip the reading and parsing of the body.
// The test against HttpBin.org shows that:
// - If request.Accept-Encoding is "gzip, deflate", the response doesn't
// - If request.Accept-Encoding is "gzip, deflate", the response won't
// have Content-Length;
// - If request.Accept-Encoding is "identity", the response do have
// - If request.Accept-Encoding is "identity", the response will have
// Content-Length.
if (request->method() == methods::kHead) {
response_parser_.set_ignroe_body(true);
} else {
// Reset in case the connection is persistent.
response_parser_.set_ignroe_body(false);
}

if (connect) {
Expand All @@ -38,7 +41,7 @@ Error Client::Request(RequestPtr request, bool connect) {
}
}

WriteReqeust(request);
WriteRequest(request);

if (error_) {
return error_;
Expand All @@ -58,12 +61,7 @@ void Client::Close() {

LOG_INFO("Close socket...");

boost::system::error_code ec;
socket_->Close(&ec);

if (ec) {
LOG_ERRO("Socket close error (%s).", ec.message().c_str());
}
socket_->Close();
}

void Client::Restart() {
Expand Down Expand Up @@ -112,23 +110,23 @@ void Client::DoConnect(RequestPtr request, const std::string& default_port) {
LOG_ERRO("Host resolve error (%s): %s, %s.", ec.message().c_str(),
request->host().c_str(), port.c_str());
error_.Set(Error::kResolveError, "Host resolve error");
return;
}

LOG_VERB("Connect to server...");

// Use sync API directly since we don't need timeout control.

if (!socket_->Connect(request->host(), endpoints, &ec)) {
LOG_ERRO("Socket connect error (%s).", ec.message().c_str());
Close();
// TODO: Handshake error
if (!socket_->Connect(request->host(), endpoints)) {
error_.Set(Error::kConnectError, "Endpoint connect error");
Close();
return;
}

LOG_VERB("Socket connected.");
}

void Client::WriteReqeust(RequestPtr request) {
void Client::WriteRequest(RequestPtr request) {
LOG_VERB("HTTP request:\n%s", request->Dump().c_str());

// NOTE:
Expand Down
2 changes: 1 addition & 1 deletion webcc/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Client {

void DoConnect(RequestPtr request, const std::string& default_port);

void WriteReqeust(RequestPtr request);
void WriteRequest(RequestPtr request);

void ReadResponse();

Expand Down
16 changes: 14 additions & 2 deletions webcc/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,34 @@ namespace webcc {

// -----------------------------------------------------------------------------

void Headers::Set(const std::string& key, const std::string& value) {
bool Headers::Set(const std::string& key, const std::string& value) {
if (value.empty()) {
return false;
}

auto it = Find(key);
if (it != headers_.end()) {
it->second = value;
} else {
headers_.push_back({ key, value });
}

return true;
}

void Headers::Set(std::string&& key, std::string&& value) {
bool Headers::Set(std::string&& key, std::string&& value) {
if (value.empty()) {
return false;
}

auto it = Find(key);
if (it != headers_.end()) {
it->second = std::move(value);
} else {
headers_.push_back({ std::move(key), std::move(value) });
}

return true;
}

bool Headers::Has(const std::string& key) const {
Expand Down
4 changes: 2 additions & 2 deletions webcc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Headers {
return headers_;
}

void Set(const std::string& key, const std::string& value);
bool Set(const std::string& key, const std::string& value);

void Set(std::string&& key, std::string&& value);
bool Set(std::string&& key, std::string&& value);

bool Has(const std::string& key) const;

Expand Down
Loading

0 comments on commit 46ea52c

Please sign in to comment.