Skip to content

Commit

Permalink
Merge pull request yuwenyong#15 from yuwenyong/dev
Browse files Browse the repository at this point in the history
v3.0.0
  • Loading branch information
yuwenyong authored Dec 23, 2018
2 parents 7b0b5ac + 9a77c9b commit 60290fa
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/net4cxx/common/define.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ NS_END
#endif


#define NET4CXX_VERSION "2.5.2"
#define NET4CXX_VERSION "3.0.0"
#define NET4CXX_VER "net4cxx/" NET4CXX_VERSION


Expand Down
7 changes: 7 additions & 0 deletions src/net4cxx/plugins/web/httpserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ void HTTPConnection::writeHeaders(ResponseStartLine startLine, HTTPHeaders &head
(_responseStartLine.getCode() < 100 || _responseStartLine.getCode() >= 200) &&
!headers.has("Content-Length") &&
!headers.has("Transfer-Encoding");
if (_requestStartLine.getVersion() == "HTTP/1.1" && _disconnectOnFinish) {
headers["Connection"] = "close";
}
if (_requestStartLine.getVersion() == "HTTP/1.0" &&
boost::to_lower_copy(_requestHeaders->get("Connection")) == "keep-alive") {
headers["Connection"] = "Keep-Alive";
Expand Down Expand Up @@ -161,6 +164,7 @@ void HTTPConnection::writeChunk(const Byte *chunk, size_t length, WriteCallbackT
if (callback) {
_writeCallback = std::move(callback);
}
_pendingWrite = true;
write(formatChunk(chunk, length), true);
}

Expand Down Expand Up @@ -467,6 +471,9 @@ void HTTPConnection::applyXheaders(const HTTPHeaders &headers) {
}
std::string proto = headers.get("X-Forwarded-Proto", _protocol);
proto = headers.get("X-Scheme", proto);
if (!proto.empty()) {
proto = boost::trim_copy(StrUtil::split(proto, ',').back());
}
if (proto == "http" || proto == "https") {
_protocol = std::move(proto);
}
Expand Down
10 changes: 10 additions & 0 deletions src/net4cxx/plugins/web/httputil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ std::tuple<std::string, boost::optional<unsigned short>> HTTPUtil::splitHostAndP
return std::make_tuple(std::move(host), port);
}

QueryArgList HTTPUtil::QStoQSL(const QueryArgListMap &args) {
QueryArgList res;
for (auto &kv: args) {
for (auto &v: kv.second) {
res.emplace_back(kv.first, v);
}
}
return res;
}

std::tuple<std::string, std::shared_ptr<HTTPHeaders>> HTTPUtil::parseHeaders(const char *data, size_t length) {
boost::string_view dv{data, length};
dv.remove_prefix(std::min(dv.find_first_not_of("\r\n"), dv.size()));
Expand Down
2 changes: 2 additions & 0 deletions src/net4cxx/plugins/web/httputil.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ class NET4CXX_COMMON_API HTTPUtil {

static std::tuple<std::string, boost::optional<unsigned short>> splitHostAndPort(const std::string &netloc);

static QueryArgList QStoQSL(const QueryArgListMap &args);

static std::tuple<std::string, std::shared_ptr<HTTPHeaders>> parseHeaders(const char *data, size_t length);

static StringMap parseCookie(const std::string &cookie);
Expand Down
19 changes: 13 additions & 6 deletions src/net4cxx/plugins/web/web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ void RequestHandler::setStatus(int statusCode, const std::string &reason) {
if (!reason.empty()) {
_reason = reason;
} else {
try {
_reason = HTTP_STATUS_CODES.at(statusCode);
} catch (std::out_of_range &e) {
NET4CXX_THROW_EXCEPTION(ValueError, "unknown status code %d", statusCode);
auto iter = HTTP_STATUS_CODES.find(statusCode);
if (iter != HTTP_STATUS_CODES.end()) {
_reason = iter->second;
} else {
_reason = "Unknown";
}
}
}
Expand Down Expand Up @@ -491,6 +492,7 @@ void RequestHandler::handleRequestException(const std::exception_ptr &error) {
try {
std::rethrow_exception(error);
} catch (HTTPError &e) {
sendError(e.getCode(), error);
int statusCode = e.getCode();
if (HTTP_STATUS_CODES.find(statusCode) == HTTP_STATUS_CODES.end() && !e.hasReason()) {
NET4CXX_LOG_ERROR(gGenLog, "Bad HTTP status code: %d", statusCode);
Expand Down Expand Up @@ -540,15 +542,20 @@ void RedirectHandler::initialize(const ArgsType &args) {
}

DeferredPtr RedirectHandler::onGet(const StringVector &args) {
std::string toUrl;
if (args.empty()) {
redirect(_url, _permanent);
toUrl = _url;
} else {
boost::format fmt(_url);
for (auto &arg: args) {
fmt % arg;
}
redirect(fmt.str(), _permanent);
toUrl = fmt.str();
}
if (!_request->getQueryArguments().empty()) {
toUrl = HTTPUtil::urlConcat(std::move(toUrl), HTTPUtil::QStoQSL(_request->getQueryArguments()));
}
redirect(toUrl, _permanent);
return nullptr;
}

Expand Down

0 comments on commit 60290fa

Please sign in to comment.