Lagrange [work/v1.15]

GmRequest: Use unique IDs to avoid confusion

=> 7a9ade7740de07f7091940c7d38e4ee3781562ec

diff --git a/src/gmrequest.c b/src/gmrequest.c
index c968990c..693bfe4a 100644
--- a/src/gmrequest.c
+++ b/src/gmrequest.c
@@ -116,6 +116,8 @@ void deserialize_GmResponse(iGmResponse *d, iStream *ins) {
 
 /*----------------------------------------------------------------------------------------------*/
 
+static iAtomicInt idGen_;
+
 enum iGmRequestState {
     initialized_GmRequestState,
     receivingHeader_GmRequestState,
@@ -126,6 +128,7 @@ enum iGmRequestState {
 
 struct Impl_GmRequest {
     iObject              object;
+    uint32_t             id;
     iMutex *             mtx;
     iGmCerts *           certs; /* not owned */
     enum iGmRequestState state;
@@ -471,7 +474,8 @@ static void beginGopherConnection_GmRequest_(iGmRequest *d, const iString *host,
 /*----------------------------------------------------------------------------------------------*/
 
 void init_GmRequest(iGmRequest *d, iGmCerts *certs) {
-    d->mtx = new_Mutex();
+    d->mtx  = new_Mutex();
+    d->id   = add_Atomic(&idGen_, 1) + 1;
     d->resp = new_GmResponse();
     d->isFilterEnabled = iTrue;
     d->isRespLocked    = iFalse;
@@ -713,11 +717,18 @@ void unlockResponse_GmRequest(iGmRequest *d) {
     }
 }
 
+uint32_t id_GmRequest(const iGmRequest *d) {
+    return d ? d->id : 0;
+}
+
 iBool isFinished_GmRequest(const iGmRequest *d) {
-    iBool done;
-    iGuardMutex(d->mtx,
-                done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState));
-    return done;
+    if (d) {
+        iBool done;
+        iGuardMutex(d->mtx,
+                    done = (d->state == finished_GmRequestState || d->state == failure_GmRequestState));
+        return done;
+    }
+    return iTrue;
 }
 
 enum iGmStatusCode status_GmRequest(const iGmRequest *d) {
diff --git a/src/gmrequest.h b/src/gmrequest.h
index 9f20e0eb..2cf9e4ff 100644
--- a/src/gmrequest.h
+++ b/src/gmrequest.h
@@ -73,6 +73,7 @@ void                cancel_GmRequest            (iGmRequest *);
 iGmResponse *       lockResponse_GmRequest      (iGmRequest *);
 void                unlockResponse_GmRequest    (iGmRequest *);
 
+uint32_t            id_GmRequest                (const iGmRequest *); /* unique ID */
 iBool               isFinished_GmRequest        (const iGmRequest *);
 enum iGmStatusCode  status_GmRequest            (const iGmRequest *);
 const iString *     meta_GmRequest              (const iGmRequest *);
diff --git a/src/ui/command.c b/src/ui/command.c
index c5ca164e..3ae0f0c9 100644
--- a/src/ui/command.c
+++ b/src/ui/command.c
@@ -50,6 +50,15 @@ int arg_Command(const char *cmd) {
     return argLabel_Command(cmd, "arg");
 }
 
+uint32_t argU32Label_Command(const char *cmd, const char *label) {
+    const iString *tok = tokenString_(label);
+    const char *ptr = strstr(cmd, cstr_String(tok));
+    if (ptr) {
+        return strtoul(ptr + size_String(tok), NULL, 10);
+    }
+    return 0;
+}
+
 float argfLabel_Command(const char *cmd, const char *label) {
     const iString *tok = tokenString_(label);
     const char *ptr = strstr(cmd, cstr_String(tok));
diff --git a/src/ui/command.h b/src/ui/command.h
index 43992b8e..10a29101 100644
--- a/src/ui/command.h
+++ b/src/ui/command.h
@@ -25,16 +25,17 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
 #include 
 #include 
 
-iBool   equal_Command           (const char *commandWithArgs, const char *command);
-
-int     arg_Command             (const char *); /* arg: */
-float   argf_Command            (const char *); /* arg: */
-int     argLabel_Command        (const char *, const char *label);
-float   argfLabel_Command       (const char *, const char *label);
-void *  pointer_Command         (const char *); /* ptr: */
-void *  pointerLabel_Command    (const char *, const char *label);
-iInt2   coord_Command           (const char *);
-iInt2   dir_Command             (const char *);
+iBool       equal_Command           (const char *commandWithArgs, const char *command);
+
+int         arg_Command             (const char *); /* arg: */
+float       argf_Command            (const char *); /* arg: */
+int         argLabel_Command        (const char *, const char *label);
+uint32_t    argU32Label_Command     (const char *, const char *label);
+float       argfLabel_Command       (const char *, const char *label);
+void *      pointer_Command         (const char *); /* ptr: */
+void *      pointerLabel_Command    (const char *, const char *label);
+iInt2       coord_Command           (const char *);
+iInt2       dir_Command             (const char *);
 
 const iString * string_Command      (const char *, const char *label); /* space-delimited */
 iRangecc        range_Command       (const char *, const char *label); /* space-delimited */
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c
index f3c9ea82..13ef878f 100644
--- a/src/ui/documentwidget.c
+++ b/src/ui/documentwidget.c
@@ -394,13 +394,21 @@ static void requestUpdated_DocumentWidget_(iAnyObject *obj) {
     iDocumentWidget *d = obj;
     const int wasUpdated = exchange_Atomic(&d->isRequestUpdated, iTrue);
     if (!wasUpdated) {
-        postCommand_Widget(obj, "document.request.updated doc:%p request:%p", d, d->request);
+        postCommand_Widget(obj,
+                           "document.request.updated doc:%p reqid:%u request:%p",
+                           d,
+                           id_GmRequest(d->request),
+                           d->request);
     }
 }
 
 static void requestFinished_DocumentWidget_(iAnyObject *obj) {
     iDocumentWidget *d = obj;
-    postCommand_Widget(obj, "document.request.finished doc:%p request:%p", d, d->request);
+    postCommand_Widget(obj,
+                       "document.request.finished doc:%p reqid:%u request:%p",
+                       d,
+                       id_GmRequest(d->request),
+                       d->request);
 }
 
 static int documentWidth_DocumentWidget_(const iDocumentWidget *d) {
@@ -965,7 +973,7 @@ static void updateDocument_DocumentWidget_(iDocumentWidget *d, const iGmResponse
     if (d->state == ready_RequestState) {
         return;
     }
-    const iBool isRequestFinished = !d->request || isFinished_GmRequest(d->request);
+    const iBool isRequestFinished = isFinished_GmRequest(d->request);
     /* TODO: Do document update in the background. However, that requires a text metrics calculator
        that does not try to cache the glyph bitmaps. */
     const enum iGmStatusCode statusCode = response->statusCode;
@@ -1892,7 +1900,7 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
         return iTrue;
     }
     else if (equalWidget_Command(cmd, w, "document.request.updated") &&
-             d->request && pointerLabel_Command(cmd, "request") == d->request) {
+             id_GmRequest(d->request) == argU32Label_Command(cmd, "reqid")) {
         set_Block(&d->sourceContent, &lockResponse_GmRequest(d->request)->body);
         unlockResponse_GmRequest(d->request);
         if (document_App() == d) {
@@ -1903,7 +1911,7 @@ static iBool handleCommand_DocumentWidget_(iDocumentWidget *d, const char *cmd)
         return iFalse;
     }
     else if (equalWidget_Command(cmd, w, "document.request.finished") &&
-             d->request && pointerLabel_Command(cmd, "request") == d->request) {
+             id_GmRequest(d->request) == argU32Label_Command(cmd, "reqid")) {
         set_Block(&d->sourceContent, body_GmRequest(d->request));
         if (!isSuccess_GmStatusCode(status_GmRequest(d->request))) {
             format_String(&d->sourceHeader,
Proxy Information
Original URL
gemini://git.skyjake.fi/lagrange/work%2Fv1.15/cdiff/7a9ade7740de07f7091940c7d38e4ee3781562ec
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
67.378736 milliseconds
Gemini-to-HTML Time
0.748234 milliseconds

This content has been proxied by September (ba2dc).