reset on un-pause
authorJeroen van der Heijden <jeroen@transceptor.technology>
Tue, 2 Feb 2021 17:49:02 +0000 (18:49 +0100)
committerJeroen van der Heijden <jeroen@transceptor.technology>
Tue, 2 Feb 2021 17:49:02 +0000 (18:49 +0100)
include/siri/api.h
src/siri/api.c

index 5bdb4305f07c5155f1c31c41694352af2bb15935..cad41e58ca1a1af03304d7d0dc0b4da9a4fddc09 100644 (file)
@@ -38,12 +38,6 @@ typedef enum
     E503_SERVICE_UNAVAILABLE
 } siri_api_header_t;
 
-typedef enum
-{
-    SIRI_API_FLAG_SERVICE_AUTHENTICATED     =1<<0,
-    SIRI_API_FLAG_MESSAGE_COMPLETED         =1<<1,
-} siri_api_flags_t;
-
 typedef struct siri_api_request_s siri_api_request_t;
 
 typedef int (*on_state_cb_t)(siri_api_request_t * ar, const char * at, size_t n);
@@ -69,7 +63,7 @@ struct siri_api_request_s
     siri_api_content_t content_type;
     siri_api_req_t request_type;
     service_request_t service_type;
-    siri_api_flags_t flags;
+    _Bool service_authenticated;
     http_parser parser;
     uv_write_t req;
 };
index 4e32e9ef7480c2975dd93d600e61869e61e3ad8b..b3e4f3b2124abb19d40726ee9a01336055e608f7 100644 (file)
@@ -142,7 +142,7 @@ static void api__reset(siri_api_request_t * ar)
     ar->len = 0;
     ar->size = 0;
     ar->on_state = NULL;
-    ar->flags = 0;
+    ar->service_authenticated = 0;
     ar->request_type = SIRI_API_RT_NONE;
     ar->content_type = SIRI_API_CT_TEXT;
 }
@@ -167,9 +167,6 @@ static void api__data_cb(
         goto done;
     }
 
-    if (ar->flags & SIRI_API_FLAG_MESSAGE_COMPLETED)
-        api__reset(ar);
-
     buf->base[HTTP_MAX_HEADER_SIZE-1] = '\0';
 
     parsed = http_parser_execute(
@@ -382,8 +379,8 @@ static int api__on_authorization(siri_api_request_t * ar, const char * at, size_
     {
         if (ar->request_type == SIRI_APT_RT_SERVICE)
         {
-            if (siri_service_account_check_basic(&siri, at, n))
-                ar->flags |= SIRI_API_FLAG_SERVICE_AUTHENTICATED;
+            ar->service_authenticated = \
+                    siri_service_account_check_basic(&siri, at, n);
             return 0;
         }
         siridb_user_t * user;
@@ -445,6 +442,9 @@ static void api__write_cb(uv_write_t * req, int status)
                 "error writing HTTP API response: `%s`",
                 uv_strerror(status));
 
+    /* reset the API to support multiple request on the same connection */
+    api__reset(ar);
+
     /* Resume parsing */
     http_parser_pause(&ar->parser, 0);
 
@@ -757,7 +757,7 @@ static int api__service_cb(http_parser * parser)
         break;
     }
 
-    if (~ar->flags & SIRI_API_FLAG_SERVICE_AUTHENTICATED)
+    if (!ar->service_authenticated)
         return api__plain_response(ar, E401_UNAUTHORIZED);
 
     switch (ar->content_type)
@@ -817,9 +817,11 @@ static int api__message_complete_cb(http_parser * parser)
 {
     siri_api_request_t * ar = parser->data;
 
-    ar->flags |= SIRI_API_FLAG_MESSAGE_COMPLETED;
-
-    /* Pause the HTTP parser */
+    /* Pause the HTTP parser;
+     * This is required since SiriDB will handle queries and inserts
+     * asynchronously and SiriDB must be sure that the request does not
+     * change during this time. It is also important to write the responses
+     * in order and this solves both issues. */
     http_parser_pause(&ar->parser, 1);
 
     switch(ar->request_type)
@@ -843,8 +845,6 @@ static void api__write_free_cb(uv_write_t * req, int status)
     api__write_cb(req, status);
 }
 
-
-
 static int api__close_resp(
         siri_api_request_t * ar,
         const siri_api_header_t ht,