| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_MCP_HTTP_COMMON_H |
| 4 | #define NETDATA_MCP_HTTP_COMMON_H |
| 5 | |
| 6 | #include "web/server/web_client.h" |
| 7 | |
| 8 | #include <stdbool.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | static inline bool mcp_http_extract_api_key(struct web_client *w, char *buffer, size_t buffer_len) |
| 12 | { |
| 13 | if (!w || !buffer || buffer_len == 0) |
| 14 | return false; |
| 15 | |
| 16 | if (!w->url_query_string_decoded) |
| 17 | return false; |
| 18 | |
| 19 | const char *query = buffer_tostring(w->url_query_string_decoded); |
| 20 | if (!query || !*query) |
| 21 | return false; |
| 22 | |
| 23 | if (*query == '?') |
| 24 | query++; |
| 25 | |
| 26 | const char *api_key_str = strstr(query, "api_key="); |
| 27 | if (!api_key_str) |
| 28 | return false; |
| 29 | |
| 30 | api_key_str += strlen("api_key="); |
| 31 | |
| 32 | size_t i = 0; |
| 33 | while (api_key_str[i] && api_key_str[i] != '&' && i < buffer_len - 1) { |
| 34 | buffer[i] = api_key_str[i]; |
| 35 | i++; |
| 36 | } |
| 37 | |
| 38 | buffer[i] = '\0'; |
| 39 | return i > 0; |
| 40 | } |
| 41 | |
| 42 | static inline void mcp_http_disable_compression(struct web_client *w) |
| 43 | { |
| 44 | if (!w) |
| 45 | return; |
| 46 | |
| 47 | web_client_flag_clear(w, WEB_CLIENT_CHUNKED_TRANSFER); |
| 48 | w->response.zoutput = false; |
| 49 | } |
| 50 | |
| 51 | #endif // NETDATA_MCP_HTTP_COMMON_H |