/* Saved model endpoints: their rows in ~/.pic/pic.sqlite and the checks on a name and a URL
* (endpoints.h). Fetching their lists is endpoints_fetch.c's. */
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cred.h"
#include "endpoints.h"
#include "models.h"
#include "platform.h"
#include "userdb.h"
static long long ep_now_ms(void) { return (long long)(pic_platform_get()->wall_ns() / 1000000); }
typedef struct {
const char *name;
int taken;
} ep_taken;
static int ep_provider_is(void *ctx, const char *provider) {
ep_taken *t = ctx;
t->taken |= strcmp(provider, t->name) == 0;
return t->taken;
}
int pic_endpoint_name_check(const char *name, char *err, size_t errcap) {
size_t n = strlen(name);
ep_taken t = {name, 0};
if (n == 0 || n > 32 || !(islower((unsigned char)name[0]) || isdigit((unsigned char)name[0]))) {
(void)snprintf(err, errcap, "an endpoint's name is 1-32 of a-z, 0-9, '-', '_' and '.', starting with a letter or digit");
return -1;
}
for (size_t i = 0; i < n; i++) {
unsigned char c = (unsigned char)name[i];
if (!(islower(c) || isdigit(c) || c == '-' || c == '_' || c == '.')) {
(void)snprintf(err, errcap, "an endpoint's name is 1-32 of a-z, 0-9, '-', '_' and '.', starting with a letter or digit");
return -1;
}
}
(void)pic_providers_each(ep_provider_is, &t);
if (t.taken) {
(void)snprintf(err, errcap, "%s is a provider pic already has; give the endpoint another name", name);
return -1;
}
return 0;
}
int pic_endpoint_url_check(const char *url, char *out, size_t cap, char *err, size_t errcap) {
const char *host, *end;
size_t n = strlen(url), hn;
int tls;
while (n > 0 && url[n - 1] == '/') {
n--;
}
tls = strncmp(url, "https://", 8) == 0;
if (!tls && strncmp(url, "http://", 7) != 0) {
(void)snprintf(err, errcap, "the URL starts with https:// (or http:// to this machine)");
return -1;
}
for (size_t i = 0; i < n; i++) {
if ((unsigned char)url[i] <= ' ' || url[i] == 0x7f) {
(void)snprintf(err, errcap, "the URL has a space or a control character in it");
return -1;
}
if (url[i] == '?' || url[i] == '#') {
(void)snprintf(err, errcap, "the URL is the API's base, without a query: http://127.0.0.1:8317/v1, say");
return -1;
}
}
host = url + (tls ? 8 : 7);
end = host + strcspn(host, "/?#");
if (end > url + n) {
end = url + n;
}
if (memchr(host, '@', (size_t)(end - host))) {
(void)snprintf(err, errcap, "the URL names a user: give the key with --key instead");
return -1;
}
hn = (size_t)(end - host);
if (hn == 0 || host[0] == ':') {
(void)snprintf(err, errcap, "the URL has no host");
return -1;
}
if (!tls) {
size_t h = host[0] == '[' ? strcspn(host, "]") + 1 : strcspn(host, ":/?#");
if (!((h == 9 && strncmp(host, "127.0.0.1", 9) == 0) || (h == 9 && strncmp(host, "localhost", 9) == 0) ||
(h == 5 && strncmp(host, "[::1]", 5) == 0))) {
(void)snprintf(err, errcap, "plain http:// only to this machine (127.0.0.1, localhost); use https://");
return -1;
}
}
if (n >= cap || n >= PIC_ENDPOINT_URL_CAP) {
(void)snprintf(err, errcap, "the URL is longer than %d bytes", PIC_ENDPOINT_URL_CAP - 1);
return -1;
}
(void)snprintf(out, cap, "%.*s", (int)n, url);
return 0;
}
int pic_endpoint_save(const char *name, const char *url, char *err, size_t errcap) {
char norm[PIC_ENDPOINT_URL_CAP];
sqlite3 *db;
sqlite3_stmt *q = NULL;
int rc = -1;
if (pic_endpoint_name_check(name, err, errcap) != 0 || pic_endpoint_url_check(url, norm, sizeof norm, err, errcap) != 0) {
return -1;
}
if (!(db = pic_userdb(err, errcap))) {
return -1;
}
if (sqlite3_prepare_v2(db,
"INSERT INTO picu_endpoint(name, url, models, added_ms) VALUES(?1, ?2, '[]', ?3)"
" ON CONFLICT(name) DO UPDATE SET url = excluded.url,"
" models = iif(url = excluded.url, models, '[]'),"
" fetched_ms = iif(url = excluded.url, fetched_ms, NULL),"
" error = iif(url = excluded.url, error, NULL)",
-1, &q, NULL) == SQLITE_OK) {
sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC);
sqlite3_bind_text(q, 2, norm, -1, SQLITE_STATIC);
sqlite3_bind_int64(q, 3, ep_now_ms());
rc = sqlite3_step(q) == SQLITE_DONE ? 0 : -1;
}
if (rc != 0) {
(void)snprintf(err, errcap, "cannot save the endpoint in ~/.pic/pic.sqlite: %s", sqlite3_errmsg(db));
}
sqlite3_finalize(q);
return rc;
}
int pic_endpoint_note(const char *name, const char *models, const char *error) {
sqlite3 *db = pic_userdb(NULL, 0);
sqlite3_stmt *q = NULL;
int rc = -1;
if (db && sqlite3_prepare_v2(db,
models ? "UPDATE picu_endpoint SET models = json(?2), fetched_ms = ?3, error = NULL WHERE name = ?1"
: "UPDATE picu_endpoint SET error = ?4 WHERE name = ?1",
-1, &q, NULL) == SQLITE_OK) {
sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC);
if (models) {
sqlite3_bind_text(q, 2, models, -1, SQLITE_STATIC);
sqlite3_bind_int64(q, 3, ep_now_ms());
} else {
sqlite3_bind_text(q, 4, error ? error : "the fetch failed", -1, SQLITE_STATIC);
}
rc = sqlite3_step(q) == SQLITE_DONE && sqlite3_changes(db) == 1 ? 0 : -1;
}
sqlite3_finalize(q);
return rc;
}
int pic_endpoint_remove(const char *name) {
sqlite3 *db = pic_userdb(NULL, 0);
sqlite3_stmt *q = NULL;
int rc = -1;
if (db && sqlite3_prepare_v2(db, "DELETE FROM picu_endpoint WHERE name = ?1", -1, &q, NULL) == SQLITE_OK) {
sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC);
rc = sqlite3_step(q) == SQLITE_DONE ? sqlite3_changes(db) > 0 : -1;
}
sqlite3_finalize(q);
if (rc == 1 && pic_cred_logout(name) < 0) {
rc = -1;
}
return rc;
}
int pic_endpoints_each(int (*fn)(void *ctx, const pic_endpoint *e), void *ctx) {
sqlite3 *db = pic_userdb(NULL, 0);
sqlite3_stmt *q = NULL;
if (!db || sqlite3_prepare_v2(db,
"SELECT name, url, models, error, json_array_length(models), added_ms,"
" coalesce(fetched_ms, 0) FROM picu_endpoint ORDER BY name",
-1, &q, NULL) != SQLITE_OK) {
sqlite3_finalize(q);
return -1;
}
while (sqlite3_step(q) == SQLITE_ROW) {
pic_endpoint e;
e.name = (const char *)sqlite3_column_text(q, 0);
e.url = (const char *)sqlite3_column_text(q, 1);
e.models = (const char *)sqlite3_column_text(q, 2);
e.error = (const char *)sqlite3_column_text(q, 3);
e.count = sqlite3_column_int(q, 4);
e.added_ms = sqlite3_column_int64(q, 5);
e.fetched_ms = sqlite3_column_int64(q, 6);
if (fn(ctx, &e)) {
break;
}
}
sqlite3_finalize(q);
return 0;
}
int pic_endpoint_url(const char *name, char *out, size_t cap) {
sqlite3 *db = pic_userdb(NULL, 0);
sqlite3_stmt *q = NULL;
int found = 0;
if (db && sqlite3_prepare_v2(db, "SELECT url FROM picu_endpoint WHERE name = ?1", -1, &q, NULL) == SQLITE_OK) {
sqlite3_bind_text(q, 1, name, -1, SQLITE_STATIC);
if (sqlite3_step(q) == SQLITE_ROW) {
(void)snprintf(out, cap, "%s", (const char *)sqlite3_column_text(q, 0));
found = 1;
}
}
sqlite3_finalize(q);
return found;
}
int pic_endpoint_models_each(int (*fn)(void *ctx, const char *provider, const char *json), void *ctx) {
sqlite3 *db = pic_userdb(NULL, 0);
sqlite3_stmt *q = NULL;
if (!db || sqlite3_prepare_v2(db,
"SELECT e.name, json_object('id', json_extract(m.value, '$.id'), 'endpoint', e.name,"
" 'ownedBy', json_extract(m.value, '$.owned_by'))"
" FROM picu_endpoint e, json_each(e.models) m"
" WHERE json_valid(e.models) AND json_type(m.value, '$.id') = 'text'"
" ORDER BY e.name, m.key",
-1, &q, NULL) != SQLITE_OK) {
sqlite3_finalize(q);
return -1;
}
while (sqlite3_step(q) == SQLITE_ROW) {
if (fn(ctx, (const char *)sqlite3_column_text(q, 0), (const char *)sqlite3_column_text(q, 1))) {
break;
}
}
sqlite3_finalize(q);
return 0;
}