Fixed multiple api request
authorJeroen van der Heijden <jeroen@transceptor.technology>
Mon, 1 Feb 2021 10:53:44 +0000 (11:53 +0100)
committerJeroen van der Heijden <jeroen@transceptor.technology>
Mon, 1 Feb 2021 10:53:44 +0000 (11:53 +0100)
include/siri/version.h
src/siri/api.c

index 0dd82d04b2294f2c9c4cdf1d640f138b8872ad78..3be9a1d773155f115d15c0fda990bf77b2e4dee4 100644 (file)
@@ -15,7 +15,7 @@
  * Note that debian alpha packages should use versions like this:
  *   2.0.34-0alpha0
  */
-#define SIRIDB_VERSION_PRE_RELEASE "-alpha-4"
+#define SIRIDB_VERSION_PRE_RELEASE "-alpha-5"
 
 #ifndef NDEBUG
 #define SIRIDB_VERSION_BUILD_RELEASE "+debug"
index 3e7d849333e0ddb0c16683d91238f3bdbe811954..e806a26abcac9a387d9a646f89122511c8c40e6b 100644 (file)
@@ -117,17 +117,35 @@ static void api__alloc_cb(
         size_t UNUSED_sugsz __attribute__((unused)),
         uv_buf_t * buf)
 {
-    LOGC("ALLOC_CB");
     buf->base = malloc(HTTP_MAX_HEADER_SIZE);
     buf->len = buf->base ? HTTP_MAX_HEADER_SIZE-1 : 0;
 }
 
+static void api__reset(siri_api_request_t * ar)
+{
+    /* Reset buffer in case multiple HTTP requests are used */
+    free (ar->buf);
+    ar->buf = NULL;
+    ar->len = 0;
+
+    if (ar->siridb)
+    {
+        siridb_decref(ar->siridb);
+        ar->siridb = NULL;
+    }
+
+    if (ar->origin)
+    {
+        siridb_user_decref((siridb_user_t *) ar->origin);
+        ar->origin = NULL;
+    }
+}
+
 static void api__data_cb(
         uv_stream_t * uvstream,
         ssize_t n,
         const uv_buf_t * buf)
 {
-    LOGC("DATA_CB");
     size_t parsed;
     siri_api_request_t * ar = uvstream->data;
 
@@ -143,6 +161,8 @@ static void api__data_cb(
         goto done;
     }
 
+    api__reset(ar);
+
     buf->base[HTTP_MAX_HEADER_SIZE-1] = '\0';
 
     parsed = http_parser_execute(
@@ -194,7 +214,6 @@ static void api__get_siridb(siri_api_request_t * ar, const char * at, size_t n)
         --n;
         ++nn;
     }
-
     ar->siridb = siridb_getn(siri.siridb_list, at, nn);
     if (ar->siridb)
     {
@@ -204,7 +223,6 @@ static void api__get_siridb(siri_api_request_t * ar, const char * at, size_t n)
 
 static int api__url_cb(http_parser * parser, const char * at, size_t n)
 {
-    LOGC("URL_CB");
     siri_api_request_t * ar = parser->data;
 
     if (api__starts_with(&at, &n, "/query/", strlen("/query/")))
@@ -272,7 +290,6 @@ static int api__url_cb(http_parser * parser, const char * at, size_t n)
 
 static void api__connection_cb(uv_stream_t * server, int status)
 {
-    LOGC("CONNECTION_CB");
     int rc;
     siri_api_request_t * ar;
 
@@ -381,14 +398,12 @@ static int api__on_authorization(siri_api_request_t * ar, const char * at, size_
 }
 static int api__header_value_cb(http_parser * parser, const char * at, size_t n)
 {
-    LOGC("HEADER_VALUE_CB");
     siri_api_request_t * ar = parser->data;
     return ar->on_state ? ar->on_state(ar, at, n) : 0;
 }
 
 static int api__header_field_cb(http_parser * parser, const char * at, size_t n)
 {
-    LOGC("HEADER_FIELD_CB");
     siri_api_request_t * ar = parser->data;
 
     ar->on_state = API__ICMP_WITH(at, n, "content-type")
@@ -401,7 +416,6 @@ static int api__header_field_cb(http_parser * parser, const char * at, size_t n)
 
 static int api__body_cb(http_parser * parser, const char * at, size_t n)
 {
-    LOGC("BODY_CB");
     size_t offset;
     siri_api_request_t * ar = parser->data;
 
@@ -417,7 +431,6 @@ static int api__body_cb(http_parser * parser, const char * at, size_t n)
 
 static void api__write_cb(uv_write_t * req, int status)
 {
-    LOGC("WRITE_CB");
     if (status)
         log_error(
                 "error writing HTTP API response: `%s`",
@@ -667,7 +680,6 @@ static int api__insert_cb(http_parser * parser)
 
 static int api__query_cb(http_parser * parser)
 {
-    LOGC("QUERY_CB");
     api__query_t q;
     siri_api_request_t * ar = parser->data;
 
@@ -791,7 +803,6 @@ static int api__service_cb(http_parser * parser)
 
 static int api__message_complete_cb(http_parser * parser)
 {
-    LOGC("MESSAGE_COMPLETE_CB");
     siri_api_request_t * ar = parser->data;
 
     switch(ar->request_type)
@@ -811,7 +822,6 @@ static int api__message_complete_cb(http_parser * parser)
 
 static void api__write_free_cb(uv_write_t * req, int status)
 {
-    LOGC("FREE_WRITE_CB");
     free(req->data);
     api__write_cb(req, status);
 }
@@ -822,7 +832,6 @@ static int api__close_resp(
         void * data,
         size_t size)
 {
-    LOGC("CLOSE_RESPONSE_CB");
     char header[API__HEADER_MAX_SZ];
     int header_size = 0;