Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issues created by the date string being localized #2217

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

easymoney322
Copy link

Fixes #2215.

Since this implementation also involves stream locales, I thought it would be a good idea to replace char arrays with strings, as these arrays would be casted to strings anyway and require size managment that in current implementation is prone to fail (see the issue).

I've added an overload for string to function datetoCustomFormattedString and added getHttpFullDateStr function, that does exactly the same as getHttpFullDate, but returns the string type.

I've left old functions just-in-case, and adjusted github workflow as containers were missing en_US locale. Seems to be working fine.

Here is a minimal working example:

#include <iostream>
#include <string>
#include <functional>
#include <locale>
#include <drogon/drogon.h>
#include <codecvt>

typedef std::function<void(const drogon::HttpResponsePtr&)> Callback;

void nodeInfo(const drogon::HttpRequestPtr& request, Callback&& callback)
{
  drogon::HttpResponsePtr response = drogon::HttpResponse::newHttpResponse(
    drogon::k200OK,
    drogon::ContentType::CT_TEXT_PLAIN);  //?newCustomHttpResponse
  response->setVersion(drogon::Version::kHttp11);
  response->addHeader("ServerTest", "Drogon");
  response->setBody("Hello\r");
  std::cout << response->getCreationDate().toFormattedString(false);
  callback(response);
}

void InitWebServer()
{
  // Locks caller thread
  trantor::Logger::setLogLevel(trantor::Logger::kTrace);
  drogon::app()
    .setLogLevel(trantor::Logger::kTrace)
    .enableDateHeader(true)
    .enableServerHeader(false)
    .enableSendfile(false)
    .addListener("0.0.0.0", 1781)
    .setThreadNum(1)
    .registerHandler("/metrics", &nodeInfo, { drogon::Get })
    .run();
  return;
}

int main()
{
  std::setlocale(LC_ALL, "ru-RU");
#ifdef _WIN32
  std::locale::global(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
#endif _WIN32
  std::cout << "Привет мир!" << std::endl;
  InitWebServer();
}

This is the program output:

Привет мир!
20241128 18:52:50.872000 UTC 29240 TRACE [registerHandler] pathPattern:/metrics - drogon/HttpAppFramework.h:531
20241128 18:52:50.875000 UTC 29240 TRACE [run] Start to run... - HttpAppFrameworkImpl.cc:526
20241128 18:52:50.896000 UTC 29240 TRACE [createListeners] thread num=1 - ListenerManager.cc:84
20241128 18:52:50.900000 UTC 29240 TRACE [createNonblockingSocketOrDie] sock=412 - Socket.h:46
20241128 18:52:50.901000 UTC 29240 TRACE [start] HttpServer[drogon] starts listening on 0.0.0.0:1781 - HttpServer.cc:96
20241128 18:52:50.903000 UTC 38136 TRACE [operator ()] map size=1 - TcpServer.cc:131
20241128 18:53:14.780000 UTC 38136 TRACE [newConnection] new connection:fd=180 address=127.0.0.1:6493 - TcpServer.cc:62
20241128 18:53:14.782000 UTC 38136 TRACE [TcpConnectionImpl] new connection:127.0.0.1:6493->127.0.0.1:1781 - TcpConnectionImpl.cc:78
20241128 18:53:14.784000 UTC 36840 TRACE [operator ()] connectEstablished - TcpConnectionImpl.cc:262
20241128 18:53:14.785000 UTC 36840 TRACE [onHttpRequest] new request:127.0.0.1:6493->127.0.0.1:1781 - HttpServer.cc:409
20241128 18:53:14.786000 UTC 36840 TRACE [onHttpRequest] Headers GET /metrics - HttpServer.cc:411
20241128 18:53:14.787000 UTC 36840 TRACE [onHttpRequest] http path=/metrics - HttpServer.cc:412
20241128 18:53:1420241128 18:53:14.790000 UTC 36840 TRACE [renderToBuffer] response(no body):HTTP/1.1 200 OK
content-length: 6
content-type: text/plain; charset=utf-8
servertest: Drogon
date: Thu, 28 Nov 2024 18:53:14 GMT

 - HttpResponseImpl.cc:717

and this is the request result from Postman:

GET /metrics HTTP/1.1
User-Agent: PostmanRuntime/7.42.0
Accept: */*
Host: 127.0.0.1:1781
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
 
HTTP/1.1 200 OK
content-length: 6
content-type: text/plain; charset=utf-8
servertest: Drogon
date: Thu, 28 Nov 2024 18:53:14 GMT
 
Hello

@easymoney322
Copy link
Author

I can't seem to fix whitespaces as this repo does not contain .Editorconfig, thus the problem with the format.sh job.
Visual Studio, Neovim, Notepad++, GitHub IDE and GitHub preview are all treating identations in this project differently.

There are extra-white spaces on an empty line in Cmake.yml workflow, and there are missaligned whitespaces for arguments in Utilities.h. Can someone who already set their editor help me with this, so I wont be bruteforcing whitespaces that I can't see?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The date header breaks the server if used with locale
1 participant