Skip to content

Commit

Permalink
Update lib/crow to CppCrow/Crow (#52)
Browse files Browse the repository at this point in the history
* Update crow
 * use CROW_USE_LOCALTIMEZONE
* Update response codes to valid http-codes
- Remove 208, it is a WebDAV code and not supported by crow.
- Remove 408, it is not required to respond in 408 scenari, and was incorrect use
  • Loading branch information
kingster committed Mar 21, 2022
1 parent c901e91 commit c75b4a7
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
- name: Upload Artifacts
uses: actions/upload-artifact@v2
with:
name: tinyphone
name: tinyphone.msi
path: |
./tinyphone-installer/bin/Release/tinyphone_installer.msi
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,5 @@ GitHubVS.sln.DotSettings
*.dll
lib/datadog-cpp

logs/
logs/
.vagrant/
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
url = https://github.com/xiph/opus.git
[submodule "lib/crow"]
path = lib/crow
url = git@github.com:kingster/crow.git
url = git@github.com:kingster/Crow.git
[submodule "lib/boost"]
path = lib/boost
url = https://github.com/boostorg/boost.git
Expand Down
2 changes: 1 addition & 1 deletion lib/crow
Submodule crow updated from 3e5d33 to 588020
19 changes: 11 additions & 8 deletions tinyphone/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "portaudio.h"
#include <boost/foreach.hpp>

#define CROW_MAIN

using namespace std;
using namespace pj;
using json = nlohmann::json;
Expand Down Expand Up @@ -184,14 +186,14 @@ void TinyPhoneHttpServer::Start() {
tp::MetricsClient.increment("api.login.exists");
try {
phone.EnableAccount(existing_account);
return tp::response(208, {
return tp::response(200, {
{ "message", "Account already exists" },
{ "account_name", account_name },
{ "id", existing_account->getId() },
{ "result", 202 }
{ "result", 200 }
});
} catch(...) {
return tp::response(408, {
return tp::response(202, {
{ "message", "Account already exists, but login progress, please try again in sometime" },
{ "account_name", account_name },
{ "id", existing_account->getId() },
Expand All @@ -217,7 +219,7 @@ void TinyPhoneHttpServer::Start() {
});
}
else {
return tp::response(408, {
return tp::response(202, {
{ "message", "Account login still in progress" },
{ "account_name", account_name },
{ "result", 202 }
Expand Down Expand Up @@ -716,7 +718,8 @@ void TinyPhoneHttpServer::Start() {
auto tar_bytes = file_all_bytes(tmp_file);
remove(tmp_file.c_str());

auto response = crow::response(tar_bytes);
std::string data_str(tar_bytes.begin(), tar_bytes.end());
auto response = crow::response(data_str);
response.set_header("Content-Type", "application/octet-stream");

std::string ip_addr = local_ip_address();
Expand All @@ -739,11 +742,11 @@ void TinyPhoneHttpServer::Start() {
json response = {
{"message", "Server Shutdown Recieved"},
{"result", 401},
{"source", req.remoteIpAddress},
{"source", req.remote_ip_address},
};
CROW_LOG_INFO << "Shutdown Request from client: " << req.remoteIpAddress;
CROW_LOG_INFO << "Shutdown Request from client: " << req.remote_ip_address;
tp::MetricsClient.increment("api.exit");
if (req.remoteIpAddress.compare("127.0.0.1") == 0) {
if (req.remote_ip_address.compare("127.0.0.1") == 0) {
CROW_LOG_INFO << "Shutdown Request from localhost authenticated";
response["result"] = 200;
app.stop();
Expand Down
10 changes: 6 additions & 4 deletions tinyphone/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#ifndef SERVER_HEADER_FILE_H
#define SERVER_HEADER_FILE_H
#define CROW_USE_LOCALTIMEZONE 1

#include <crow.h>
#include <iostream>
Expand Down Expand Up @@ -29,15 +30,16 @@ class TinyPhoneHTTPLogHandler : public crow::ILogHandler {
log_writer.close();
}

void log(const std::string &message, crow::LogLevel /*level*/) override {
log_writer << message ;
void log(const std::string message, crow::LogLevel /*level*/) {
log_writer << message << std::endl ;
#ifdef _DEBUG
std::cout << message;
std::cout << message << std::endl ;
#endif
log_writer.flush();
}
};


struct TinyPhoneMiddleware
{
std::string message;
Expand Down Expand Up @@ -103,4 +105,4 @@ class TinyPhoneHttpServer {
};

#endif


0 comments on commit c75b4a7

Please sign in to comment.