Commit c20e9ef8 authored by Nico Caprioli's avatar Nico Caprioli

Merge remote-tracking branch 'upstream/master'

parents e183294c ae904949
...@@ -87,7 +87,6 @@ protected: ...@@ -87,7 +87,6 @@ protected:
}; };
namespace Uri { namespace Uri {
typedef std::string Fragment;
class Query { class Query {
public: public:
...@@ -97,12 +96,15 @@ namespace Uri { ...@@ -97,12 +96,15 @@ namespace Uri {
void add(std::string name, std::string value); void add(std::string name, std::string value);
Optional<std::string> get(const std::string& name) const; Optional<std::string> get(const std::string& name) const;
bool has(const std::string& name) const; bool has(const std::string& name) const;
// Return empty string or "?key1=value1&key2=value2" if query exist
std::string as_str() const;
void clear() { void clear() {
params.clear(); params.clear();
} }
private: private:
//first is key second is value
std::unordered_map<std::string, std::string> params; std::unordered_map<std::string, std::string> params;
}; };
} // namespace Uri } // namespace Uri
......
...@@ -36,6 +36,7 @@ namespace Mime { ...@@ -36,6 +36,7 @@ namespace Mime {
SUB_TYPE(Javascript, "javascript") \ SUB_TYPE(Javascript, "javascript") \
SUB_TYPE(Css , "css") \ SUB_TYPE(Css , "css") \
\ \
SUB_TYPE(OctetStream, "octet-stream") \
SUB_TYPE(Json , "json") \ SUB_TYPE(Json , "json") \
SUB_TYPE(FormUrlEncoded, "x-www-form-urlencoded") \ SUB_TYPE(FormUrlEncoded, "x-www-form-urlencoded") \
\ \
......
...@@ -463,43 +463,55 @@ Connection::handleResponsePacket(const char* buffer, size_t bytes) { ...@@ -463,43 +463,55 @@ Connection::handleResponsePacket(const char* buffer, size_t bytes) {
parser_.feed(buffer, bytes); parser_.feed(buffer, bytes);
if (parser_.parse() == Private::State::Done) { if (parser_.parse() == Private::State::Done) {
auto req = std::move(inflightRequests.front()); if (!inflightRequests.empty()) {
inflightRequests.pop_back(); auto req = std::move(inflightRequests.front());
inflightRequests.pop_back();
if (req.timer) { if (req.timer) {
req.timer->disarm(); req.timer->disarm();
timerPool_.releaseTimer(req.timer); timerPool_.releaseTimer(req.timer);
} }
req.resolve(std::move(parser_.response)); req.resolve(std::move(parser_.response));
req.onDone(); if (req.onDone)
req.onDone();
}
parser_.reset(); parser_.reset();
} }
} }
void void
Connection::handleError(const char* error) { Connection::handleError(const char* error) {
auto req = std::move(inflightRequests.front()); if (!inflightRequests.empty()) {
if (req.timer) { auto req = std::move(inflightRequests.front());
req.timer->disarm(); if (req.timer) {
timerPool_.releaseTimer(req.timer); req.timer->disarm();
} timerPool_.releaseTimer(req.timer);
}
req.reject(Error(error)); req.reject(Error(error));
req.onDone(); if (req.onDone)
req.onDone();
}
} }
void void
Connection::handleTimeout() { Connection::handleTimeout() {
auto req = std::move(inflightRequests.front()); if (!inflightRequests.empty()) {
inflightRequests.pop_back(); auto req = std::move(inflightRequests.front());
inflightRequests.pop_back();
timerPool_.releaseTimer(req.timer);
timerPool_.releaseTimer(req.timer); if (req.onDone)
req.onDone(); req.onDone();
/* @API: create a TimeoutException */ /* @API: create a TimeoutException */
req.reject(std::runtime_error("Timeout")); req.reject(std::runtime_error("Timeout"));
}
} }
Async::Promise<Response> Async::Promise<Response>
Connection::perform( Connection::perform(
const Http::Request& request, const Http::Request& request,
......
...@@ -500,6 +500,18 @@ namespace Uri { ...@@ -500,6 +500,18 @@ namespace Uri {
return Some(it->second); return Some(it->second);
} }
std::string
Query::as_str() const {
std::string query_url;
for(const auto &e : params) {
query_url += "&" + e.first + "=" + e.second;
}
if(not query_url.empty()) {
query_url[0] = '?'; // replace first `&` with `?`
} else {/* query_url is empty */}
return query_url;
}
bool bool
Query::has(const std::string& name) const { Query::has(const std::string& name) const {
...@@ -668,10 +680,17 @@ serveFile(ResponseWriter& response, const char* fileName, const Mime::MediaType& ...@@ -668,10 +680,17 @@ serveFile(ResponseWriter& response, const char* fileName, const Mime::MediaType&
int fd = open(fileName, O_RDONLY); int fd = open(fileName, O_RDONLY);
if (fd == -1) { if (fd == -1) {
std::string str_error(strerror(errno));
if(errno == ENOENT) {
throw HttpError(Http::Code::Not_Found, std::move(str_error));
}
//eles if TODO
/* @Improvement: maybe could we check for errno here and emit a different error /* @Improvement: maybe could we check for errno here and emit a different error
message message
*/ */
throw HttpError(Http::Code::Not_Found, ""); else {
throw HttpError(Http::Code::Internal_Server_Error, std::move(str_error));
}
} }
int res = ::fstat(fd, &sb); int res = ::fstat(fd, &sb);
......
...@@ -82,7 +82,9 @@ MediaType::fromFile(const char* fileName) ...@@ -82,7 +82,9 @@ MediaType::fromFile(const char* fileName)
{ "bmp", Type::Image, Subtype::Bmp }, { "bmp", Type::Image, Subtype::Bmp },
{ "txt", Type::Text, Subtype::Plain }, { "txt", Type::Text, Subtype::Plain },
{ "md", Type::Text, Subtype::Plain } { "md", Type::Text, Subtype::Plain },
{ "bin", Type::Application, Subtype::OctetStream},
}; };
for (const auto& ext: KnownExtensions) { for (const auto& ext: KnownExtensions) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment