OSDN Git Service

[Refactor] スコアサーバへの送信にcpp-httplibを使用
authorHabu <habu1010+github@gmail.com>
Sat, 17 Jun 2023 22:57:35 +0000 (07:57 +0900)
committerHabu <habu1010+github@gmail.com>
Thu, 22 Jun 2023 09:34:09 +0000 (18:34 +0900)
スコアサーバへのスコア送信を、従来のlibcurlを使用した処理からcpp-httplib
を使用した処理に置き換える。

Hengband/Hengband/Hengband.vcxproj
Hengband/Hengband/Hengband.vcxproj.filters
src/Makefile.am
src/external-lib/include-httplib.h [new file with mode: 0644]
src/io/report.cpp

index 9444db6..766036b 100644 (file)
     <ClCompile Include="..\..\src\cmd-action\cmd-tunnel.cpp" />\r
     <ClCompile Include="..\..\src\action\movement-execution.cpp" />\r
     <ClCompile Include="..\..\src\core\score-util.cpp" />\r
+    <ClCompile Include="..\..\src\external-lib\httplib.cpp" />\r
     <ClCompile Include="..\..\src\item-info\flavor-initializer.cpp" />\r
     <ClCompile Include="..\..\src\load\item\item-loader-base.cpp" />\r
     <ClCompile Include="..\..\src\load\item\item-loader-factory.cpp" />\r
     <ClInclude Include="..\..\src\action\open-close-execution.h" />\r
     <ClInclude Include="..\..\src\action\open-util.h" />\r
     <ClInclude Include="..\..\src\action\run-execution.h" />\r
+    <ClInclude Include="..\..\src\external-lib\httplib.h" />\r
+    <ClInclude Include="..\..\src\external-lib\include-httplib.h" />\r
     <ClInclude Include="..\..\src\item-info\flavor-initializer.h" />\r
     <ClInclude Include="..\..\src\load\item\item-loader-version-types.h" />\r
     <ClInclude Include="..\..\src\load\item\item-loader-base.h" />\r
index 6930c19..d7c6b3e 100644 (file)
     <ClCompile Include="..\..\src\core\score-util.cpp">\r
       <Filter>core</Filter>\r
     </ClCompile>\r
+    <ClCompile Include="..\..\src\external-lib\httplib.cpp">\r
+      <Filter>external-lib</Filter>\r
+    </ClCompile>\r
     <ClCompile Include="..\..\src\view\display-scores.cpp">\r
       <Filter>view</Filter>\r
     </ClCompile>\r
     <ClInclude Include="..\..\src\system\redrawing-flags-updater.h">\r
       <Filter>system</Filter>\r
     </ClInclude>\r
+    <ClInclude Include="..\..\src\external-lib\httplib.h">\r
+      <Filter>external-lib</Filter>\r
+    </ClInclude>\r
+    <ClInclude Include="..\..\src\external-lib\include-httplib.h">\r
+      <Filter>external-lib</Filter>\r
+    </ClInclude>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <None Include="..\..\src\wall.bmp" />\r
     <Filter Include="item-info">\r
       <UniqueIdentifier>{3aefd2e8-8dd8-41dd-bc6b-7dadd75dfd6b}</UniqueIdentifier>\r
     </Filter>\r
+    <Filter Include="external-lib">\r
+      <UniqueIdentifier>{b0f75b81-c15a-43de-a96e-139123aa2ebb}</UniqueIdentifier>\r
+    </Filter>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ResourceCompile Include="..\..\src\angband.rc" />\r
index d5f9311..c4d6d27 100644 (file)
@@ -193,6 +193,9 @@ hengband_SOURCES = \
        effect/effect-processor.cpp effect/effect-processor.h \
        effect/effect-characteristics.h \
        \
+       external-lib/httplib.cpp external-lib/httplib.h \
+       external-lib/include-httplib.h \
+       \
        flavor/flag-inscriptions-table.cpp flavor/flag-inscriptions-table.h \
        flavor/flavor-describer.cpp flavor/flavor-describer.h \
        flavor/flavor-util.cpp flavor/flavor-util.h \
diff --git a/src/external-lib/include-httplib.h b/src/external-lib/include-httplib.h
new file mode 100644 (file)
index 0000000..f7b2c6f
--- /dev/null
@@ -0,0 +1,25 @@
+#pragma once
+
+#include "system/angband.h"
+
+#ifdef WORLD_SCORE
+
+#define CPPHTTPLIB_OPENSSL_SUPPORT
+
+// MSVCの警告レベルを最大に設定してあるためcpp-httplibとOpenSSLのヘッダが
+// 大量の警告を出力してしまうのでヘッダのインクルード時のみ警告を抑制しておく
+#if defined(_MSC_VER)
+#pragma warning(push, 0)
+#endif
+
+#include "external-lib/httplib.h"
+
+#if defined(_MSC_VER)
+#pragma warning(pop)
+#endif
+
+#ifdef WINDOWS
+#include <Windows.h>
+#endif
+
+#endif // WORLD_SCORE
index 6c0c766..724a7c7 100644 (file)
@@ -10,6 +10,7 @@
 #include "core/stuff-handler.h"
 #include "core/turn-compensator.h"
 #include "core/visuals-reseter.h"
+#include "external-lib/include-httplib.h"
 #include "game-option/special-options.h"
 #include "io-dump/character-dump.h"
 #include "io/input-key-acceptor.h"
 #include <vector>
 
 #ifdef WORLD_SCORE
-#ifdef WINDOWS
-#define CURL_STATICLIB
-#endif
-#include <curl/curl.h>
 
 concptr screen_dump = nullptr;
 
 /*
  * internet resource value
  */
-#define HTTP_TIMEOUT 30 /*!< デフォルトのタイムアウト時間(秒) / Timeout length (second) */
+constexpr auto HTTP_CONNECTION_TIMEOUT = 30; /*!< HTTP接続タイムアウト時間(秒) */
 
 #ifdef JP
-#define SCORE_PATH "http://mars.kmc.gr.jp/~dis/heng_score/register_score.php" /*!< スコア開示URL */
-#else
-#define SCORE_PATH "http://moon.kmc.gr.jp/hengband/hengscore-en/score.cgi" /*!< スコア開示URL */
+constexpr auto SCORE_SERVER_SCHEME_HOST = "http://mars.kmc.gr.jp"; /*!< スコアサーバホスト */
+constexpr auto SCORE_SERVER_PATH = "/~dis/heng_score_test/register_score.php"; /*< スコアサーバパス */
 #endif
 
 /*!
@@ -96,69 +92,35 @@ size_t read_callback(char *buffer, size_t size, size_t nitems, void *userdata)
 
 /*!
  * @brief HTTPによるダンプ内容伝送
- * @param url 伝送先URL
- * @param buf 伝送内容バッファ
+ * @param score ダンプ内容
  * @return 送信に成功した場合TRUE、失敗した場合FALSE
  */
-static bool http_post(concptr url, const std::vector<char> &buf)
+static bool report_score(const std::string &score)
 {
-    bool succeeded = false;
-    CURL *curl = curl_easy_init();
-    if (curl == nullptr) {
-        return false;
-    }
+    httplib::Client cli(SCORE_SERVER_SCHEME_HOST);
+    cli.set_connection_timeout(HTTP_CONNECTION_TIMEOUT);
+    cli.set_follow_location(true);
 
-    struct curl_slist *slist = nullptr;
-    slist = curl_slist_append(slist,
+    httplib::Headers headers = {
+        { "User-Agent", format("Hengband %d.%d.%d", H_VER_MAJOR, H_VER_MINOR, H_VER_PATCH) }
+    };
+
+    auto content_type =
 #ifdef JP
 #ifdef SJIS
-        "Content-Type: text/plain; charset=SHIFT_JIS"
+        "text/plain; charset=SHIFT_JIS"
 #endif
 #ifdef EUC
-        "Content-Type: text/plain; charset=EUC-JP"
+        "text/plain; charset=EUC-JP"
 #endif
 #else
-        "Content-Type: text/plain; charset=ASCII"
+        "text/plain; charset=ASCII"
 #endif
-    );
-
-    curl_easy_setopt(curl, CURLOPT_URL, url);
-    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
-
-    char user_agent[64];
-    snprintf(user_agent, sizeof(user_agent), "Hengband %d.%d.%d", H_VER_MAJOR, H_VER_MINOR, H_VER_PATCH);
-    curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent);
+        ;
 
-    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
-    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
+    const auto res = cli.Post(SCORE_SERVER_PATH, score, content_type);
 
-    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
-    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, HTTP_TIMEOUT);
-    curl_easy_setopt(curl, CURLOPT_TIMEOUT, HTTP_TIMEOUT);
-
-    curl_easy_setopt(curl, CURLOPT_POST, 1);
-
-    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-    curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10);
-    curl_easy_setopt(curl, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
-
-    std::span<const char> data(buf);
-    curl_easy_setopt(curl, CURLOPT_READDATA, &data);
-    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
-    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.size());
-
-    if (curl_easy_perform(curl) == CURLE_OK) {
-        long response_code;
-        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
-        if (response_code == 200) {
-            succeeded = true;
-        }
-    }
-
-    curl_slist_free_all(slist);
-    curl_easy_cleanup(curl);
-
-    return succeeded;
+    return res->status == 200;
 }
 
 /*!
@@ -377,12 +339,14 @@ bool report_score(PlayerType *player_ptr)
         score.insert(score.end(), sv.begin(), sv.end());
     }
 
+    const std::string score_data(score.begin(), score.end());
+
     term_clear();
     while (true) {
         term_fresh();
         prt(_("スコア送信中...", "Sending the score..."), 0, 0);
         term_fresh();
-        if (http_post(SCORE_PATH, score)) {
+        if (report_score(score_data)) {
             return true;
         }