add request/response body to verbose output / add DavSession logfunc

Tue, 12 Sep 2023 21:07:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 12 Sep 2023 21:07:54 +0200
changeset 788
9b9420041d8e
parent 787
bd96cbb0e205
child 789
378b5ab86f77

add request/response body to verbose output / add DavSession logfunc

dav/main.c file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
libidav/session.c file | annotate | diff | comparison | revisions
libidav/webdav.c file | annotate | diff | comparison | revisions
libidav/webdav.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Tue Sep 12 18:09:19 2023 +0200
+++ b/dav/main.c	Tue Sep 12 21:07:54 2023 +0200
@@ -365,6 +365,7 @@
     if (cmd_getoption(a, "verbose")) {
         curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L);
         curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
+        sn->logfunc = dav_verbose_log;
     }
        
     return 0;
--- a/dav/sync.c	Tue Sep 12 18:09:19 2023 +0200
+++ b/dav/sync.c	Tue Sep 12 21:07:54 2023 +0200
@@ -721,6 +721,7 @@
     if (cmd_getoption(a, "verbose")) {
         curl_easy_setopt(sn->handle, CURLOPT_VERBOSE, 1L);
         curl_easy_setopt(sn->handle, CURLOPT_STDERR, stderr);
+        sn->logfunc = dav_verbose_log;
     }
     
     // lock repository
--- a/libidav/session.c	Tue Sep 12 18:09:19 2023 +0200
+++ b/libidav/session.c	Tue Sep 12 21:07:54 2023 +0200
@@ -174,18 +174,42 @@
     CURLcode ret = curl_easy_perform(sn->handle);
     long http_status;
     curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
-    if(ret == CURLE_OK && http_status == 401 && sn->auth_prompt) {
-        if(!sn->auth_prompt(sn, sn->authprompt_userdata)) {
+    if(ret == CURLE_OK) {
+        if(sn->logfunc) {
+            char *log_method;
+            char *log_url;
+            curl_easy_getinfo(sn->handle, CURLINFO_EFFECTIVE_URL, &log_url);
+            curl_easy_getinfo(sn->handle, CURLINFO_EFFECTIVE_METHOD , &log_method);
+            char *log_reqbody = NULL;
+            size_t log_reqbodylen = 0;
+            char *log_rpbody = NULL;
+            size_t log_rpbodylen = 0;
             if(request) {
-                cxBufferSeek(request, 0, SEEK_SET);
+                log_reqbody = request->space;
+                log_reqbodylen = request->size;
             }
             if(response) {
-                cxBufferSeek(response, 0, SEEK_SET);
+                log_rpbody = response->space;
+                log_rpbodylen = response->size;
             }
-            ret = curl_easy_perform(sn->handle);
-            curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
+            sn->logfunc(sn, log_method, log_url, log_reqbody, log_reqbodylen, http_status, log_rpbody, log_rpbodylen);
         }
+        
+        if(http_status == 401 && sn->auth_prompt) {
+            if(!sn->auth_prompt(sn, sn->authprompt_userdata)) {
+                if(request) {
+                    cxBufferSeek(request, 0, SEEK_SET);
+                }
+                if(response) {
+                    cxBufferSeek(response, 0, SEEK_SET);
+                }
+                ret = curl_easy_perform(sn->handle);
+                curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
+            }
+        }
+        
     }
+    
     if(status) {
         *status = http_status;
     }
--- a/libidav/webdav.c	Tue Sep 12 18:09:19 2023 +0200
+++ b/libidav/webdav.c	Tue Sep 12 21:07:54 2023 +0200
@@ -413,3 +413,17 @@
 
 
 
+void dav_verbose_log(
+        DavSession *sn,
+        const char *method,
+        const char *url,
+        const char *request_body,
+        size_t request_bodylen,
+        int status,
+        const char *response_body,
+        size_t response_bodylen)
+{
+    fprintf(stderr, "# method: %s url: %s status: %d\n", method, url, status);
+    fprintf(stderr, "# request len: %d\n%.*s\n", (int)request_bodylen, (int)request_bodylen, request_body);
+    fprintf(stderr, "# response len: %d\n%.*s\n", (int)response_bodylen, (int)response_bodylen, response_body);
+}
--- a/libidav/webdav.h	Tue Sep 12 18:09:19 2023 +0200
+++ b/libidav/webdav.h	Tue Sep 12 21:07:54 2023 +0200
@@ -73,6 +73,17 @@
 typedef int(*dav_auth_func)(DavSession *, void *);
 typedef void(*dav_progress_func)(DavResource *, int64_t, int64_t, void *);
 
+
+typedef void(*dav_rqlog_func)(
+        DavSession *sn,
+        const char *method,
+        const char *url,
+        const char *request_body,
+        size_t request_bodylen,
+        int status,
+        const char *response_body,
+        size_t response_bodylen);
+
 enum DavError {
     DAV_OK = 0,
     DAV_ERROR,
@@ -160,6 +171,8 @@
     int(*auth_prompt)(DavSession *sn, void *userdata);
     void *authprompt_userdata;
     
+    dav_rqlog_func logfunc;
+    
     void(*get_progress)(DavResource *res, int64_t total, int64_t now, void *userdata);
     void(*put_progress)(DavResource *res, int64_t total, int64_t now, void *userdata);
     void *progress_userdata;
@@ -348,6 +361,16 @@
 size_t dav_write(const void *buf, size_t size, size_t nitems, DavOutputStream *out);
 int dav_outputstream_close(DavOutputStream *out);
 
+void dav_verbose_log(
+        DavSession *sn,
+        const char *method,
+        const char *url,
+        const char *request_body,
+        size_t request_bodylen,
+        int status,
+        const char *response_body,
+        size_t response_bodylen);
+
 // private
 int dav_propfind(DavSession *sn, DavResource *root, CxBuffer *rqbuf);
 

mercurial