| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2026 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 #include "jmapsession.h" |
| |
30 #include "session.h" |
| |
31 |
| |
32 #include <cx/buffer.h> |
| |
33 |
| |
34 static void jm_session_curl_destructor(CURL *handle) { |
| |
35 curl_easy_cleanup(handle); |
| |
36 } |
| |
37 |
| |
38 JMAPSession* jmap_session_new_with_user(const char *session_url, const char *user, const char *password) { |
| |
39 CURL *handle = curl_easy_init(); |
| |
40 if(!handle) { |
| |
41 return NULL; |
| |
42 } |
| |
43 curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L); |
| |
44 |
| |
45 if(user && password) { |
| |
46 if(jm_session_set_auth(handle, user, password)) { |
| |
47 curl_easy_cleanup(handle); |
| |
48 return NULL; |
| |
49 } |
| |
50 } |
| |
51 |
| |
52 CxMempool *mp = cxMempoolCreate(256, CX_MEMPOOL_TYPE_SIMPLE); |
| |
53 if(!mp) { |
| |
54 curl_easy_cleanup(handle); |
| |
55 return NULL; |
| |
56 } |
| |
57 cxMempoolRegister(mp, handle, (cx_destructor_func)jm_session_curl_destructor); |
| |
58 |
| |
59 JMAPSession *sn = cxZalloc(mp->allocator, sizeof(JMAPSession)); |
| |
60 if(!sn) { |
| |
61 cxMempoolFree(mp); |
| |
62 return NULL; |
| |
63 } |
| |
64 |
| |
65 sn->handle = handle; |
| |
66 sn->mp = mp; |
| |
67 |
| |
68 CxJsonValue *sessionValue = jm_query_session(sn, session_url); |
| |
69 if(!sessionValue) { |
| |
70 cxMempoolFree(mp); |
| |
71 return NULL; |
| |
72 } |
| |
73 sn->obj = sessionValue; |
| |
74 |
| |
75 CxJsonValue *username = cxJsonObjGet(sessionValue, "username"); |
| |
76 CxJsonValue *apiUrl = cxJsonObjGet(sessionValue, "apiUrl"); |
| |
77 CxJsonValue *downloadUrl = cxJsonObjGet(sessionValue, "downloadUrl"); |
| |
78 CxJsonValue *uploadUrl = cxJsonObjGet(sessionValue, "uploadUrl"); |
| |
79 |
| |
80 if(cxJsonIsString(username)) { |
| |
81 sn->username = cx_strdup_a(mp->allocator, username->string.ptr).ptr; |
| |
82 } |
| |
83 if(cxJsonIsString(apiUrl)) { |
| |
84 sn->apiUrl = cx_strdup_a(mp->allocator, apiUrl->string.ptr).ptr; |
| |
85 } |
| |
86 if(cxJsonIsString(downloadUrl)) { |
| |
87 sn->downloadUrl = cx_strdup_a(mp->allocator, downloadUrl->string.ptr).ptr; |
| |
88 } |
| |
89 if(cxJsonIsString(uploadUrl)) { |
| |
90 sn->uploadUrl = cx_strdup_a(mp->allocator, uploadUrl->string.ptr).ptr; |
| |
91 } |
| |
92 |
| |
93 return sn; |
| |
94 } |
| |
95 |
| |
96 int jm_session_set_auth(CURL *handle, const char *user, const char *password) { |
| |
97 return dav_curl_set_auth(handle, cx_str(user), cx_str(password)); |
| |
98 } |
| |
99 |
| |
100 CxJsonValue* jm_query_session(JMAPSession *sn, const char *url) { |
| |
101 curl_easy_setopt(sn->handle, CURLOPT_URL, url); |
| |
102 |
| |
103 CxBuffer *response = cxBufferCreate(NULL, NULL, 1024, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| |
104 if(!response) { |
| |
105 return NULL; |
| |
106 } |
| |
107 |
| |
108 curl_easy_setopt(sn->handle, CURLOPT_WRITEFUNCTION, cxBufferWrite); |
| |
109 curl_easy_setopt(sn->handle, CURLOPT_WRITEDATA, response); |
| |
110 |
| |
111 CxJsonValue *json = NULL; |
| |
112 CURLcode ret = curl_easy_perform(sn->handle); |
| |
113 long http_status; |
| |
114 curl_easy_getinfo(sn->handle, CURLINFO_RESPONSE_CODE, &http_status); |
| |
115 if(ret == CURLE_OK) { |
| |
116 if(http_status >= 200 && http_status <= 299) { |
| |
117 (void)cx_json_from_string(sn->mp->allocator, cx_strn(response->space, response->size), &json); |
| |
118 } |
| |
119 } |
| |
120 cxBufferFree(response); |
| |
121 |
| |
122 return json; |
| |
123 } |