moved code for HTTP request handling to separate functions
authorHolger Schemel <info@artsoft.org>
Sat, 25 Sep 2021 00:26:15 +0000 (02:26 +0200)
committerHolger Schemel <info@artsoft.org>
Sat, 25 Sep 2021 00:26:15 +0000 (02:26 +0200)
src/files.c
src/screens.c

index fa1c14f8cadc7491f6a6b1f53ee27c1cc5ba8da2..6d99afccd0af57da1162fedb1ebe9c443f0be85a 100644 (file)
@@ -9094,13 +9094,11 @@ static void FreeThreadData_ApiGetScore(void *data_raw)
   checked_free(data);
 }
 
-static void ApiGetScore_HttpRequest(struct HttpRequest *request,
-                                   struct HttpResponse *response,
-                                   void *data_raw)
+static boolean SetRequest_ApiGetScore(struct HttpRequest *request,
+                                     void *data_raw)
 {
   struct ApiGetScoreThreadData *data = data_raw;
   int level_nr = data->level_nr;
-  char *score_cache_filename = data->score_cache_filename;
 
   request->hostname = setup.api_server_hostname;
   request->port     = API_SERVER_PORT;
@@ -9118,25 +9116,18 @@ static void ApiGetScore_HttpRequest(struct HttpRequest *request,
           getPasswordJSON(setup.api_server_password),
           getProgramRealVersionString(),
           getProgramPlatformString(),
-          levelset.identifier, level_nr);
+          levelset.identifier,
+          level_nr);
 
   ConvertHttpRequestBodyToServerEncoding(request);
 
-  if (!DoHttpRequest(request, response))
-  {
-    Error("HTTP request failed: %s", GetHttpError());
-
-    return;
-  }
-
-  if (!HTTP_SUCCESS(response->status_code))
-  {
-    Error("server failed to handle request: %d %s",
-         response->status_code,
-         response->status_text);
+  return TRUE;
+}
 
-    return;
-  }
+static void HandleResponse_ApiGetScore(struct HttpResponse *response,
+                                      void *data_raw)
+{
+  struct ApiGetScoreThreadData *data = data_raw;
 
   if (response->body_size == 0)
   {
@@ -9147,7 +9138,7 @@ static void ApiGetScore_HttpRequest(struct HttpRequest *request,
 
   ConvertHttpResponseBodyToClientEncoding(response);
 
-  char *filename = score_cache_filename;
+  char *filename = data->score_cache_filename;
   FILE *file;
   int i;
 
@@ -9171,6 +9162,41 @@ static void ApiGetScore_HttpRequest(struct HttpRequest *request,
   server_scores.updated = TRUE;
 }
 
+static void ApiGetScore_HttpRequestExt(struct HttpRequest *request,
+                                      struct HttpResponse *response,
+                                      void *data_raw)
+{
+  if (!SetRequest_ApiGetScore(request, data_raw))
+    return;
+
+  if (!DoHttpRequest(request, response))
+  {
+    Error("HTTP request failed: %s", GetHttpError());
+
+    return;
+  }
+
+  if (!HTTP_SUCCESS(response->status_code))
+  {
+    Error("server failed to handle request: %d %s",
+         response->status_code,
+         response->status_text);
+
+    return;
+  }
+
+  HandleResponse_ApiGetScore(response, data_raw);
+}
+
+static void ApiGetScore_HttpRequest(struct HttpRequest *request,
+                                   struct HttpResponse *response,
+                                   void *data_raw)
+{
+  ApiGetScore_HttpRequestExt(request, response, data_raw);
+
+  FreeThreadData_ApiGetScore(data_raw);
+}
+
 static int ApiGetScoreThread(void *data_raw)
 {
   struct HttpRequest *request = checked_calloc(sizeof(struct HttpRequest));
@@ -9178,8 +9204,6 @@ static int ApiGetScoreThread(void *data_raw)
 
   ApiGetScore_HttpRequest(request, response, data_raw);
 
-  FreeThreadData_ApiGetScore(data_raw);
-
   checked_free(request);
   checked_free(response);
 
@@ -9366,14 +9390,13 @@ static void FreeThreadData_ApiAddScore(void *data_raw)
   checked_free(data);
 }
 
-static void ApiAddScore_HttpRequest(struct HttpRequest *request,
-                                   struct HttpResponse *response,
-                                   void *data_raw)
+static boolean SetRequest_ApiAddScore(struct HttpRequest *request,
+                                     void *data_raw)
 {
   struct ApiAddScoreThreadData *data = data_raw;
-  int level_nr = data->level_nr;
-  char *score_tape_filename = data->score_tape_filename;
   struct ScoreEntry *score_entry = &data->score_entry;
+  char *score_tape_filename = data->score_tape_filename;
+  int level_nr = data->level_nr;
 
   request->hostname = setup.api_server_hostname;
   request->port     = API_SERVER_PORT;
@@ -9386,7 +9409,7 @@ static void ApiAddScore_HttpRequest(struct HttpRequest *request,
   {
     Error("loading and base64 encoding score tape file failed");
 
-    return;
+    return FALSE;
   }
 
   char *player_name_raw = score_entry->name;
@@ -9460,6 +9483,22 @@ static void ApiAddScore_HttpRequest(struct HttpRequest *request,
 
   ConvertHttpRequestBodyToServerEncoding(request);
 
+  return TRUE;
+}
+
+static void HandleResponse_ApiAddScore(struct HttpResponse *response,
+                                      void *data_raw)
+{
+  server_scores.uploaded = TRUE;
+}
+
+static void ApiAddScore_HttpRequestExt(struct HttpRequest *request,
+                                      struct HttpResponse *response,
+                                      void *data_raw)
+{
+  if (!SetRequest_ApiAddScore(request, data_raw))
+    return;
+
   if (!DoHttpRequest(request, response))
   {
     Error("HTTP request failed: %s", GetHttpError());
@@ -9476,7 +9515,16 @@ static void ApiAddScore_HttpRequest(struct HttpRequest *request,
     return;
   }
 
-  server_scores.uploaded = TRUE;
+  HandleResponse_ApiAddScore(response, data_raw);
+}
+
+static void ApiAddScore_HttpRequest(struct HttpRequest *request,
+                                   struct HttpResponse *response,
+                                   void *data_raw)
+{
+  ApiAddScore_HttpRequestExt(request, response, data_raw);
+
+  FreeThreadData_ApiAddScore(data_raw);
 }
 
 static int ApiAddScoreThread(void *data_raw)
@@ -9486,8 +9534,6 @@ static int ApiAddScoreThread(void *data_raw)
 
   ApiAddScore_HttpRequest(request, response, data_raw);
 
-  FreeThreadData_ApiAddScore(data_raw);
-
   checked_free(request);
   checked_free(response);
 
index 307d100efad9479257426cc2a58127f4cde70c67..55aa0c22f855bd6abad91e51fcfd88a8353108d5 100644 (file)
@@ -4068,9 +4068,8 @@ static void FreeThreadData_ApiRenamePlayer(void *data_raw)
   checked_free(data);
 }
 
-static void ApiRenamePlayer_HttpRequest(struct HttpRequest *request,
-                                       struct HttpResponse *response,
-                                       void *data_raw)
+static boolean SetRequest_ApiRenamePlayer(struct HttpRequest *request,
+                                         void *data_raw)
 {
   struct ApiRenamePlayerThreadData *data = data_raw;
   char *player_name_raw = data->player_name;
@@ -4103,6 +4102,22 @@ static void ApiRenamePlayer_HttpRequest(struct HttpRequest *request,
 
   ConvertHttpRequestBodyToServerEncoding(request);
 
+  return TRUE;
+}
+
+static void HandleResponse_ApiRenamePlayer(struct HttpResponse *response,
+                                          void *data_raw)
+{
+  // nothing to do here
+}
+
+static void ApiRenamePlayer_HttpRequestExt(struct HttpRequest *request,
+                                          struct HttpResponse *response,
+                                          void *data_raw)
+{
+  if (!SetRequest_ApiRenamePlayer(request, data_raw))
+    return;
+
   if (!DoHttpRequest(request, response))
   {
     Error("HTTP request failed: %s", GetHttpError());
@@ -4118,6 +4133,17 @@ static void ApiRenamePlayer_HttpRequest(struct HttpRequest *request,
 
     return;
   }
+
+  HandleResponse_ApiRenamePlayer(response, data_raw);
+}
+
+static void ApiRenamePlayer_HttpRequest(struct HttpRequest *request,
+                                   struct HttpResponse *response,
+                                   void *data_raw)
+{
+  ApiRenamePlayer_HttpRequestExt(request, response, data_raw);
+
+  FreeThreadData_ApiRenamePlayer(data_raw);
 }
 
 static int ApiRenamePlayerThread(void *data_raw)
@@ -4127,8 +4153,6 @@ static int ApiRenamePlayerThread(void *data_raw)
 
   ApiRenamePlayer_HttpRequest(request, response, data_raw);
 
-  FreeThreadData_ApiRenamePlayer(data_raw);
-
   checked_free(request);
   checked_free(response);