| 1 | /* |
| 2 | Copyright (C) 2025 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "grn_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/ |
| 20 | #include "grn_http_client.h"cxx . . $ `<file>/lib/grn_http_client.h`/ |
| 21 | |
| 22 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 23 | # include <curl/curl.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | struct _grn_http_clientcxx . . $ _grn_http_client# { |
| 29 | grn_obj_headercxx . . $ grn_obj_header# headercxx . . $ _grn_http_client#header.; |
| 30 | |
| 31 | /* This is just for grn_http_client_write() */ |
| 32 | grn_ctxcxx . . $ grn_ctx# *ctxcxx . . $ _grn_http_client#ctx.; |
| 33 | |
| 34 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 35 | CURLcxx . . $ CURL# *curlcxx . . $ _grn_http_client#curl.; |
| 36 | struct curl_slistcxx . . $ curl_slist# *headerscxx . . $ _grn_http_client#headers.; |
| 37 | #endif |
| 38 | grn_objcxx . . $ grn_obj# urlcxx . . $ _grn_http_client#url.; |
| 39 | grn_objcxx . . $ grn_obj# output_buffercxx . . $ _grn_http_client#output_buffer.; |
| 40 | grn_objcxx . . $ grn_obj# output_pathcxx . . $ _grn_http_client#output_path.; |
| 41 | FILEcxx . . $ FILE# *output_filecxx . . $ _grn_http_client#output_file.; |
| 42 | }; |
| 43 | |
| 44 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 45 | static inline grn_rccxx . . $ grn_rc# |
| 46 | grn_curl_code_to_rccxx . . $ grn_curl_code_to_rc(89f90b1b8c71b71e).(CURLcodecxx . . $ CURLcode# code) |
| 47 | { |
| 48 | /* TODO: Add more codes: https://curl.se/libcurl/c/libcurl-errors.html */ |
| 49 | switch (code) { |
| 50 | case CURLE_OKcxx . . $ CURLE_OK.: |
| 51 | return GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 52 | case CURLE_UNSUPPORTED_PROTOCOLcxx . . $ CURLE_UNSUPPORTED_PROTOCOL.: |
| 53 | return GRN_OPERATION_NOT_SUPPORTEDcxx . . $ GRN_OPERATION_NOT_SUPPORTED.; |
| 54 | case CURLE_URL_MALFORMATcxx . . $ CURLE_URL_MALFORMAT.: |
| 55 | return GRN_INVALID_ARGUMENTcxx . . $ GRN_INVALID_ARGUMENT.; |
| 56 | default: |
| 57 | return GRN_UNKNOWN_ERRORcxx . . $ GRN_UNKNOWN_ERROR.; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static size_t |
| 62 | grn_http_client_writecxx . . $ grn_http_client_write(5cfc6da464ad5a60).(char *data, |
| 63 | size_t size, /* size is always 1 */ |
| 64 | size_t n_elements, |
| 65 | void *user_data) |
| 66 | { |
| 67 | grn_http_clientcxx . . $ grn_http_client# *client = user_data; |
| 68 | |
| 69 | if (client->output_filecxx . . $ _grn_http_client#output_file.) { |
| 70 | return fwritecxx . . $ fwrite(ab0609fa07c47d6e).(data, size, n_elements, client->output_filecxx . . $ _grn_http_client#output_file.); |
| 71 | } else { |
| 72 | GRN_TEXT_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(client->ctx, &(client->output_buffer), data, size * n_elements); |
| 73 | return size * n_elements; |
| 74 | } |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | grn_http_clientcxx . . $ grn_http_client# * |
| 79 | grn_http_client_opencxx . . $ grn_http_client_open(662b74376dee3a94).(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 80 | { |
| 81 | const char *tag = "[http-client][open]"; |
| 82 | |
| 83 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 84 | |
| 85 | grn_http_clientcxx . . $ grn_http_client# *client = GRN_CALLOCcxx . . $ `lib/grn_alloc.h:42:9`!(sizeof(grn_http_client)); |
| 86 | if (!client) { |
| 87 | char errbuf[GRN_CTX_MSGSIZEcxx . . $ `include/groonga/groonga.h:334:9`!]; |
| 88 | grn_strcpycxx . . $ `include/groonga/portability.h:101:11`!(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 89 | ERRcxx . . $ `lib/grn_error.h:156:9`!(ctx->rc, "%s failed to allocate memory: %s", tag, errbuf); |
| 90 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 91 | } |
| 92 | |
| 93 | client->headercxx . . $ _grn_http_client#header..typecxx . . $ _grn_obj_header#type. = GRN_HTTP_CLIENTcxx . . $ `include/groonga/groonga.h:903:9`!; |
| 94 | client->headercxx . . $ _grn_http_client#header..impl_flagscxx . . $ _grn_obj_header#impl_flags. = 0; |
| 95 | client->headercxx . . $ _grn_http_client#header..flagscxx . . $ _grn_obj_header#flags. = 0; |
| 96 | client->headercxx . . $ _grn_http_client#header..domaincxx . . $ _grn_obj_header#domain. = GRN_ID_NILcxx . . $ `include/groonga/groonga.h:55:9`!; |
| 97 | |
| 98 | client->ctxcxx . . $ _grn_http_client#ctx. = ctx; |
| 99 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 100 | client->curlcxx . . $ _grn_http_client#curl. = curl_easy_initcxx . . $ curl_easy_init(50c7df7de5344ebb).(); |
| 101 | if (!client->curlcxx . . $ _grn_http_client#curl.) { |
| 102 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_UNKNOWN_ERROR, "%s failed to initialize curl", tag); |
| 103 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(client); |
| 104 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 105 | } |
| 106 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 107 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_NOPROGRESS, 1L); |
| 108 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_USERAGENT, "Groonga/" VERSIONcxx . . $ `build/config.h:7:9`!); |
| 109 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_WRITEDATA, client); |
| 110 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_WRITEFUNCTION, grn_http_client_write); |
| 111 | /* We can debug HTTP request/response by this. */ |
| 112 | /* curl_easy_setopt(client->curl, CURLOPT_VERBOSE, 1L); */ |
| 113 | client->headerscxx . . $ _grn_http_client#headers. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 114 | #endif |
| 115 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&(client->url), 0); |
| 116 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&(client->output_buffer), 0); |
| 117 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&(client->output_path), 0); |
| 118 | client->output_filecxx . . $ _grn_http_client#output_file. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 119 | |
| 120 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(client); |
| 121 | } |
| 122 | |
| 123 | grn_rccxx . . $ grn_rc# |
| 124 | grn_http_client_closecxx . . $ grn_http_client_close(ff7a8688477384c7).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_http_clientcxx . . $ grn_http_client# *client) |
| 125 | { |
| 126 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 127 | |
| 128 | if (!client) { |
| 129 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 130 | } |
| 131 | |
| 132 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 133 | curl_easy_cleanupcxx . . $ curl_easy_cleanup(5d22bdacc48458e8).(client->curlcxx . . $ _grn_http_client#curl.); |
| 134 | curl_slist_free_allcxx . . $ curl_slist_free_all(47f08f6a83d6b302).(client->headerscxx . . $ _grn_http_client#headers.); |
| 135 | #endif |
| 136 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &(client->url)); |
| 137 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &(client->output_buffer)); |
| 138 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &(client->output_path)); |
| 139 | if (client->output_filecxx . . $ _grn_http_client#output_file.) { |
| 140 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(client->output_filecxx . . $ _grn_http_client#output_file.); |
| 141 | } |
| 142 | |
| 143 | GRN_FREEcxx . . $ `lib/grn_alloc.h:49:9`!(client); |
| 144 | |
| 145 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 146 | } |
| 147 | |
| 148 | grn_rccxx . . $ grn_rc# |
| 149 | grn_http_client_set_user_agentcxx . . $ grn_http_client_set_user_agent(37cd477a4f5ae2f1).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 150 | grn_http_clientcxx . . $ grn_http_client# *client, |
| 151 | const char *user_agent) |
| 152 | { |
| 153 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 154 | |
| 155 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 156 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_USERAGENT, user_agent); |
| 157 | #endif |
| 158 | |
| 159 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 160 | } |
| 161 | |
| 162 | grn_rccxx . . $ grn_rc# |
| 163 | grn_http_client_set_urlcxx . . $ grn_http_client_set_url(37cd477a4f5ae2f1).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 164 | grn_http_clientcxx . . $ grn_http_client# *client, |
| 165 | const char *url) |
| 166 | { |
| 167 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 168 | |
| 169 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 170 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_URL, url); |
| 171 | #endif |
| 172 | GRN_TEXT_SETScxx . . $ `include/groonga/groonga.h:2783:9`!(ctx, &(client->url), url); |
| 173 | |
| 174 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 175 | } |
| 176 | |
| 177 | grn_rccxx . . $ grn_rc# |
| 178 | grn_http_client_add_headercxx . . $ grn_http_client_add_header(37cd477a4f5ae2f1).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 179 | grn_http_clientcxx . . $ grn_http_client# *client, |
| 180 | const char *header) |
| 181 | { |
| 182 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 183 | |
| 184 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 185 | client->headerscxx . . $ _grn_http_client#headers. = curl_slist_appendcxx . . $ curl_slist_append(a1f1fe6101157931).(client->headerscxx . . $ _grn_http_client#headers., header); |
| 186 | curl_easy_setoptcxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:44:9`!(client->curl, CURLOPT_HTTPHEADER, client->headers); |
| 187 | #endif |
| 188 | |
| 189 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 190 | } |
| 191 | |
| 192 | grn_rccxx . . $ grn_rc# |
| 193 | grn_http_client_set_output_pathcxx . . $ grn_http_client_set_output_path(37cd477a4f5ae2f1).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 194 | grn_http_clientcxx . . $ grn_http_client# *client, |
| 195 | const char *path) |
| 196 | { |
| 197 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 198 | |
| 199 | if (path) { |
| 200 | GRN_TEXT_SETScxx . . $ `include/groonga/groonga.h:2783:9`!(ctx, &(client->output_path), path); |
| 201 | } else { |
| 202 | GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(client->output_path)); |
| 203 | } |
| 204 | if (GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&(client->output_path)) > 0) { |
| 205 | GRN_TEXT_PUTCcxx . . $ `include/groonga/groonga.h:2775:9`!(ctx, &(client->output_path), '\0'); |
| 206 | } |
| 207 | |
| 208 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 209 | } |
| 210 | |
| 211 | grn_rccxx . . $ grn_rc# |
| 212 | grn_http_client_downloadcxx . . $ grn_http_client_download(ff7a8688477384c7).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_http_clientcxx . . $ grn_http_client# *client) |
| 213 | { |
| 214 | const char *tag = "[http-client][download]"; |
| 215 | |
| 216 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 217 | |
| 218 | #ifdef GRN_WITH_CURLcxx . . $ `build/config.h:97:9`! |
| 219 | GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(&(client->output_buffer)); |
| 220 | if (client->output_filecxx . . $ _grn_http_client#output_file.) { |
| 221 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(client->output_filecxx . . $ _grn_http_client#output_file.); |
| 222 | client->output_filecxx . . $ _grn_http_client#output_file. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 223 | } |
| 224 | if (GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&(client->output_path)) > 0) { |
| 225 | /* TODO: We must use "wb" on Windows but we will use Windows |
| 226 | * native HTTP API instead of curl on Windows. */ |
| 227 | client->output_filecxx . . $ _grn_http_client#output_file. = fopencxx . . $ fopen(82ec0787cd37e0a3).(GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&(client->output_path)), "w"); |
| 228 | } |
| 229 | |
| 230 | CURLcodecxx . . $ CURLcode# code = curl_easy_performcxx . . $ curl_easy_perform(f4dec095beb37eff).(client->curlcxx . . $ _grn_http_client#curl.); |
| 231 | if (code != CURLE_OKcxx . . $ CURLE_OK.) { |
| 232 | ERRcxx . . $ `lib/grn_error.h:156:9`!(grn_curl_code_to_rc(code), |
| 233 | "%s failed: <%.*s>: %s", |
| 234 | tag, |
| 235 | (int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&(client->url)), |
| 236 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&(client->url)), |
| 237 | curl_easy_strerror(code)); |
| 238 | goto exit; |
| 239 | } |
| 240 | |
| 241 | long response_code; |
| 242 | curl_easy_getinfocxx . . $ `<external>/2615863945346914028/typecheck-gcc.h:163:9`!(client->curl, CURLINFO_RESPONSE_CODE, &response_code); |
| 243 | if (response_code != 200) { |
| 244 | ERRcxx . . $ `lib/grn_error.h:156:9`!(grn_curl_code_to_rc(code), |
| 245 | "%s failed: <%.*s>: <%d>: <%.*s>", |
| 246 | tag, |
| 247 | (int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&(client->url)), |
| 248 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&(client->url)), |
| 249 | (int)response_code, |
| 250 | (int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(&(client->output_buffer)), |
| 251 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&(client->output_buffer))); |
| 252 | goto exit; |
| 253 | } |
| 254 | |
| 255 | exit: |
| 256 | if (client->output_filecxx . . $ _grn_http_client#output_file.) { |
| 257 | fclosecxx . . $ fclose(89bc1efcdac0ca22).(client->output_filecxx . . $ _grn_http_client#output_file.); |
| 258 | client->output_filecxx . . $ _grn_http_client#output_file. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 259 | } |
| 260 | #else |
| 261 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "%s curl isn't enabled", tag); |
| 262 | #endif |
| 263 | |
| 264 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 265 | } |
| 266 | |
| 267 | grn_objcxx . . $ grn_obj# * |
| 268 | grn_http_client_get_outputcxx . . $ grn_http_client_get_output(80b1eeab0e886d89).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_http_clientcxx . . $ grn_http_client# *client) |
| 269 | { |
| 270 | return &(client->output_buffercxx . . $ _grn_http_client#output_buffer.); |
| 271 | } |