libidav/jmapsession.c

branch
dav-2
changeset 925
61a231a8d8f5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libidav/jmapsession.c	Wed Jul 08 15:34:41 2026 +0200
@@ -0,0 +1,123 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "jmapsession.h"
+#include "session.h"
+
+#include <cx/buffer.h>
+
+static void jm_session_curl_destructor(CURL *handle) {
+    curl_easy_cleanup(handle);
+}
+
+JMAPSession* jmap_session_new_with_user(const char *session_url, const char *user, const char *password) {
+    CURL *handle = curl_easy_init();
+    if(!handle) {
+        return NULL;
+    }
+    curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
+    
+    if(user && password) {
+        if(jm_session_set_auth(handle, user, password)) {
+            curl_easy_cleanup(handle);
+            return NULL;
+        }
+    }
+    
+    CxMempool *mp = cxMempoolCreate(256, CX_MEMPOOL_TYPE_SIMPLE);
+    if(!mp) {
+        curl_easy_cleanup(handle);
+        return NULL;
+    }
+    cxMempoolRegister(mp, handle, (cx_destructor_func)jm_session_curl_destructor);
+    
+    JMAPSession *sn = cxZalloc(mp->allocator, sizeof(JMAPSession));
+    if(!sn) {
+        cxMempoolFree(mp);
+        return NULL;
+    }
+    
+    sn->handle = handle;
+    sn->mp = mp;
+    
+    CxJsonValue *sessionValue = jm_query_session(sn, session_url);
+    if(!sessionValue) {
+        cxMempoolFree(mp);
+        return NULL;
+    }
+    sn->obj = sessionValue;
+    
+    CxJsonValue *username = cxJsonObjGet(sessionValue, "username");
+    CxJsonValue *apiUrl = cxJsonObjGet(sessionValue, "apiUrl");
+    CxJsonValue *downloadUrl = cxJsonObjGet(sessionValue, "downloadUrl");
+    CxJsonValue *uploadUrl = cxJsonObjGet(sessionValue, "uploadUrl");
+    
+    if(cxJsonIsString(username)) {
+        sn->username = cx_strdup_a(mp->allocator, username->string.ptr).ptr;
+    }
+    if(cxJsonIsString(apiUrl)) {
+        sn->apiUrl = cx_strdup_a(mp->allocator, apiUrl->string.ptr).ptr;
+    }
+    if(cxJsonIsString(downloadUrl)) {
+        sn->downloadUrl = cx_strdup_a(mp->allocator, downloadUrl->string.ptr).ptr;
+    }
+    if(cxJsonIsString(uploadUrl)) {
+        sn->uploadUrl = cx_strdup_a(mp->allocator, uploadUrl->string.ptr).ptr;
+    }
+    
+    return sn;
+}
+
+int jm_session_set_auth(CURL *handle, const char *user, const char *password) {
+    return dav_curl_set_auth(handle, cx_str(user), cx_str(password));
+}
+
+CxJsonValue* jm_query_session(JMAPSession *sn, const char *url) {
+    curl_easy_setopt(sn->handle, CURLOPT_URL, url);
+    
+    CxBuffer *response = cxBufferCreate(NULL, NULL, 1024, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
+    if(!response) {
+        return NULL;
+    }
+    
+    curl_easy_setopt(sn->handle, CURLOPT_WRITEFUNCTION, cxBufferWrite);
+    curl_easy_setopt(sn->handle, CURLOPT_WRITEDATA, response);
+    
+    CxJsonValue *json = NULL;
+    CURLcode ret = curl_easy_perform(sn->handle);
+    long http_status;
+    curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status);
+    if(ret == CURLE_OK) {
+        if(http_status >= 200 && http_status <= 299) {
+            (void)cx_json_from_string(sn->mp->allocator, cx_strn(response->space, response->size), &json);
+        }
+    }
+    cxBufferFree(response);
+    
+    return json;
+}

mercurial