| 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" |
| 20 | #include "grn_http_client.h" |
| 21 | |
| 22 | #ifdef GRN_WITH_CURL |
| 23 | # include <curl/curl.h> |
| 24 | #endif |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | struct _grn_http_client { |
| 29 | grn_obj_header header; |
| 30 | |
| 31 | /* This is just for grn_http_client_write() */ |
| 32 | grn_ctx *ctx; |
| 33 | |
| 34 | #ifdef GRN_WITH_CURL |
| 35 | CURL *curl; |
| 36 | struct curl_slist *headers; |
| 37 | #endif |
| 38 | grn_obj url; |
| 39 | grn_obj output_buffer; |
| 40 | grn_obj output_path; |
| 41 | FILE *output_file; |
| 42 | }; |
| 43 | |
| 44 | #ifdef GRN_WITH_CURL |
| 45 | static inline grn_rc |
| 46 | grn_curl_code_to_rc(CURLcode code) |
| 47 | { |
| 48 | /* TODO: Add more codes: https://curl.se/libcurl/c/libcurl-errors.html */ |
| 49 | switch (code) { |
| 50 | case CURLE_OK: |
| 51 | return GRN_SUCCESS; |
| 52 | case CURLE_UNSUPPORTED_PROTOCOL: |
| 53 | return GRN_OPERATION_NOT_SUPPORTED; |
| 54 | case CURLE_URL_MALFORMAT: |
| 55 | return GRN_INVALID_ARGUMENT; |
| 56 | default: |
| 57 | return GRN_UNKNOWN_ERROR; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static size_t |
| 62 | grn_http_client_write(char *data, |
| 63 | size_t size, /* size is always 1 */ |
| 64 | size_t n_elements, |
| 65 | void *user_data) |
| 66 | { |
| 67 | grn_http_client *client = user_data; |
| 68 | |
| 69 | if (client->output_file) { |
| 70 | return fwrite(data, size, n_elements, client->output_file); |
| 71 | } else { |
| 72 | GRN_TEXT_PUT(client->ctx, &(client->output_buffer), data, size * n_elements); |
| 73 | return size * n_elements; |
| 74 | } |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | grn_http_client * |
| 79 | grn_http_client_open(grn_ctx *ctx) |
| 80 | { |
| 81 | const char *tag = "[http-client][open]"; |
| 82 | |
| 83 | GRN_API_ENTER; |
| 84 | |
| 85 | grn_http_client *client = GRN_CALLOC(sizeof(grn_http_client)); |
| 86 | if (!client) { |
| 87 | char errbuf[GRN_CTX_MSGSIZE]; |
| 88 | grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 89 | ERR(ctx->rc, "%s failed to allocate memory: %s", tag, errbuf); |
| 90 | GRN_API_RETURN(NULL); |
| 91 | } |
| 92 | |
| 93 | client->header.type = GRN_HTTP_CLIENT; |
| 94 | client->header.impl_flags = 0; |
| 95 | client->header.flags = 0; |
| 96 | client->header.domain = GRN_ID_NIL; |
| 97 | |
| 98 | client->ctx = ctx; |
| 99 | #ifdef GRN_WITH_CURL |
| 100 | client->curl = curl_easy_init(); |
| 101 | if (!client->curl) { |
| 102 | ERR(GRN_UNKNOWN_ERROR, "%s failed to initialize curl", tag); |
| 103 | GRN_FREE(client); |
| 104 | GRN_API_RETURN(NULL); |
| 105 | } |
| 106 | curl_easy_setopt(client->curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 107 | curl_easy_setopt(client->curl, CURLOPT_NOPROGRESS, 1L); |
| 108 | curl_easy_setopt(client->curl, CURLOPT_USERAGENT, "Groonga/" VERSION); |
| 109 | curl_easy_setopt(client->curl, CURLOPT_WRITEDATA, client); |
| 110 | curl_easy_setopt(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->headers = NULL; |
| 114 | #endif |
| 115 | GRN_TEXT_INIT(&(client->url), 0); |
| 116 | GRN_TEXT_INIT(&(client->output_buffer), 0); |
| 117 | GRN_TEXT_INIT(&(client->output_path), 0); |
| 118 | client->output_file = NULL; |
| 119 | |
| 120 | GRN_API_RETURN(client); |
| 121 | } |
| 122 | |
| 123 | grn_rc |
| 124 | grn_http_client_close(grn_ctx *ctx, grn_http_client *client) |
| 125 | { |
| 126 | GRN_API_ENTER; |
| 127 | |
| 128 | if (!client) { |
| 129 | GRN_API_RETURN(ctx->rc); |
| 130 | } |
| 131 | |
| 132 | #ifdef GRN_WITH_CURL |
| 133 | curl_easy_cleanup(client->curl); |
| 134 | curl_slist_free_all(client->headers); |
| 135 | #endif |
| 136 | GRN_OBJ_FIN(ctx, &(client->url)); |
| 137 | GRN_OBJ_FIN(ctx, &(client->output_buffer)); |
| 138 | GRN_OBJ_FIN(ctx, &(client->output_path)); |
| 139 | if (client->output_file) { |
| 140 | fclose(client->output_file); |
| 141 | } |
| 142 | |
| 143 | GRN_FREE(client); |
| 144 | |
| 145 | GRN_API_RETURN(ctx->rc); |
| 146 | } |
| 147 | |
| 148 | grn_rc |
| 149 | grn_http_client_set_user_agent(grn_ctx *ctx, |
| 150 | grn_http_client *client, |
| 151 | const char *user_agent) |
| 152 | { |
| 153 | GRN_API_ENTER; |
| 154 | |
| 155 | #ifdef GRN_WITH_CURL |
| 156 | curl_easy_setopt(client->curl, CURLOPT_USERAGENT, user_agent); |
| 157 | #endif |
| 158 | |
| 159 | GRN_API_RETURN(ctx->rc); |
| 160 | } |
| 161 | |
| 162 | grn_rc |
| 163 | grn_http_client_set_url(grn_ctx *ctx, |
| 164 | grn_http_client *client, |
| 165 | const char *url) |
| 166 | { |
| 167 | GRN_API_ENTER; |
| 168 | |
| 169 | #ifdef GRN_WITH_CURL |
| 170 | curl_easy_setopt(client->curl, CURLOPT_URL, url); |
| 171 | #endif |
| 172 | GRN_TEXT_SETS(ctx, &(client->url), url); |
| 173 | |
| 174 | GRN_API_RETURN(ctx->rc); |
| 175 | } |
| 176 | |
| 177 | grn_rc |
| 178 | grn_http_client_add_header(grn_ctx *ctx, |
| 179 | grn_http_client *client, |
| 180 | const char *header) |
| 181 | { |
| 182 | GRN_API_ENTER; |
| 183 | |
| 184 | #ifdef GRN_WITH_CURL |
| 185 | client->headers = curl_slist_append(client->headers, header); |
| 186 | curl_easy_setopt(client->curl, CURLOPT_HTTPHEADER, client->headers); |
| 187 | #endif |
| 188 | |
| 189 | GRN_API_RETURN(ctx->rc); |
| 190 | } |
| 191 | |
| 192 | grn_rc |
| 193 | grn_http_client_set_output_path(grn_ctx *ctx, |
| 194 | grn_http_client *client, |
| 195 | const char *path) |
| 196 | { |
| 197 | GRN_API_ENTER; |
| 198 | |
| 199 | if (path) { |
| 200 | GRN_TEXT_SETS(ctx, &(client->output_path), path); |
| 201 | } else { |
| 202 | GRN_BULK_REWIND(&(client->output_path)); |
| 203 | } |
| 204 | if (GRN_TEXT_LEN(&(client->output_path)) > 0) { |
| 205 | GRN_TEXT_PUTC(ctx, &(client->output_path), '\0'); |
| 206 | } |
| 207 | |
| 208 | GRN_API_RETURN(ctx->rc); |
| 209 | } |
| 210 | |
| 211 | grn_rc |
| 212 | grn_http_client_download(grn_ctx *ctx, grn_http_client *client) |
| 213 | { |
| 214 | const char *tag = "[http-client][download]"; |
| 215 | |
| 216 | GRN_API_ENTER; |
| 217 | |
| 218 | #ifdef GRN_WITH_CURL |
| 219 | GRN_BULK_REWIND(&(client->output_buffer)); |
| 220 | if (client->output_file) { |
| 221 | fclose(client->output_file); |
| 222 | client->output_file = NULL; |
| 223 | } |
| 224 | if (GRN_TEXT_LEN(&(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_file = fopen(GRN_TEXT_VALUE(&(client->output_path)), "w"); |
| 228 | } |
| 229 | |
| 230 | CURLcode code = curl_easy_perform(client->curl); |
| 231 | if (code != CURLE_OK) { |
| 232 | ERR(grn_curl_code_to_rc(code), |
| 233 | "%s failed: <%.*s>: %s", |
| 234 | tag, |
| 235 | (int)GRN_TEXT_LEN(&(client->url)), |
| 236 | GRN_TEXT_VALUE(&(client->url)), |
| 237 | curl_easy_strerror(code)); |
| 238 | goto exit; |
| 239 | } |
| 240 | |
| 241 | long response_code; |
| 242 | curl_easy_getinfo(client->curl, CURLINFO_RESPONSE_CODE, &response_code); |
| 243 | if (response_code != 200) { |
| 244 | ERR(grn_curl_code_to_rc(code), |
| 245 | "%s failed: <%.*s>: <%d>: <%.*s>", |
| 246 | tag, |
| 247 | (int)GRN_TEXT_LEN(&(client->url)), |
| 248 | GRN_TEXT_VALUE(&(client->url)), |
| 249 | (int)response_code, |
| 250 | (int)GRN_TEXT_LEN(&(client->output_buffer)), |
| 251 | GRN_TEXT_VALUE(&(client->output_buffer))); |
| 252 | goto exit; |
| 253 | } |
| 254 | |
| 255 | exit: |
| 256 | if (client->output_file) { |
| 257 | fclose(client->output_file); |
| 258 | client->output_file = NULL; |
| 259 | } |
| 260 | #else |
| 261 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "%s curl isn't enabled", tag); |
| 262 | #endif |
| 263 | |
| 264 | GRN_API_RETURN(ctx->rc); |
| 265 | } |
| 266 | |
| 267 | grn_obj * |
| 268 | grn_http_client_get_output(grn_ctx *ctx, grn_http_client *client) |
| 269 | { |
| 270 | return &(client->output_buffer); |
| 271 | } |