#ifndef SPFILES_INTERNAL_H #define SPFILES_INTERNAL_H #include #include #include #include "spfiles.h" #include "scorpiox-http.h" #include "scorpiox-http-util.h" #include "yyjson.h" #define SPFILES_HOST_DEFAULT "https://gcp.scorpioplayer.com" #define SPFILES_VERSION_STR "1.0.0" #define SPFILES_ERRBUF 256 #define SPFILES_MAX_BODY (32u * 1024u * 1024u) /* wire in; refuse bigger */ #define SPFILES_FILES_TTL_DEFAULT 300 #define SPFILES_KV_TTL_DEFAULT 900 #define SPFILES_SESSIONS_TTL_DEFAULT 60 /* ---- zero-copy section indexes (pointers into the yyjson arena) ---- */ typedef struct { const char *key, *imdb, *parent, *filename, *path, *extension; const char *sub_langs, *sub_lang_titles, *created, *updated; long long size, duration; int season, episode; /* -1 absent */ int is_hdr, is_hevc, is_dolby; /* -1 absent, 0/1 */ int sub_count, audio, w, h; /* -1 absent */ yyjson_val *obj; /* the raw object (file_json) */ /* entries */ const char **e_uri, **e_url; int *e_active, *e_src; size_t n_entries; } spfiles_file; typedef struct { const char *key, *value; const char *p[10]; } spfiles_kvrow; typedef struct { /* ProductionPlaylist inner JSON */ const char *key, *value, *name, *logo, *origin; long long id; const char **imdb; size_t n_imdb; } spfiles_playlist; typedef struct { const char *key, *updated; double value; long long size; int season, episode; } spfiles_progress; typedef struct { const char *key, *uri, *label, *filename, *updated; long long id, parent_id; int enabled, is_child; } spfiles_sub; typedef struct { const char *email, *name, *id, *state, *type; long long total, used, remaining; int files_count; } spfiles_storage; typedef struct { /* /api/AAA/GetPlaybackSessions row */ const char *id, *created, *value; } spfiles_session; typedef struct { /* SCORPIOX_PlaybackSession_* index row */ const char *key, *user, *imdb, *sessionkey; int profile, season, episode; long long size; } spfiles_sessionix; /* ---- process state ---- */ typedef struct { const scorpiox_http *http; int have_http; char host[256]; char err[SPFILES_ERRBUF]; long http_code; /* ☢️ arm gate — default 0. Every write export checks this FIRST. */ int writes_armed; /* getfiles (one slot per id|s|e; keep the last doc live) */ yyjson_doc *files_doc; spfiles_file *files; size_t n_files; char files_key[192]; time_t files_at; int files_ttl; int files_valid; /* keyvalue reads: one generic slot per kind */ yyjson_doc *kv_doc; spfiles_kvrow *kvrows; size_t n_kvrows; char kv_kind[32]; char kv_key[256]; time_t kv_at; int kv_ttl; int kv_valid; /* parsed projections over the kv slot */ spfiles_playlist *plays; size_t n_plays; spfiles_progress *progs; size_t n_progs; spfiles_sub *subs; size_t n_subs; spfiles_sessionix *sessix; size_t n_sessix; spfiles_storage *stores; size_t n_stores; char stores_b64[4096]; /* transient; wiped after decode */ /* sessions (POST result) */ yyjson_doc *sess_doc; spfiles_session *sess; size_t n_sess; char sess_key[512]; time_t sess_at; int sess_ttl; int sess_valid; } spfiles_state; extern spfiles_state g_sf; /* util (spfiles_util.c) */ void spfiles_set_err(const char *fmt, ...); char *spfiles_strdup(const char *s); char *spfiles_strndup(const char *s, size_t n); char *spfiles_url_encode(const char *s); long long spfiles_stable_hash_impl(const char *utf8, size_t len); /* case-agnostic key lookup (SPEC D15): pascal, camel, case-insensitive scan */ yyjson_val *spfiles_geti(yyjson_val *obj, const char *pascal, const char *camel); int spfiles_js(const char **out, yyjson_val *obj, const char *pascal, const char *camel); int spfiles_ji(int *out, yyjson_val *obj, const char *pascal, const char *camel); int spfiles_jll(long long *out, yyjson_val *obj, const char *pascal, const char *camel); int spfiles_jd(double *out, yyjson_val *obj, const char *pascal, const char *camel); int spfiles_jb(int *out, yyjson_val *obj, const char *pascal, const char *camel); int spfiles_cache_expired(time_t at, int ttl); /* http (spfiles_http.c) */ int spfiles_http_get(const char *url, const char *const *extra_headers, unsigned char **out_body, size_t *out_len, scorpiox_buf *out_hdrs, long *out_code, char *errbuf, size_t errcap); int spfiles_http_post(const char *url, const char *body, size_t body_len, const char *content_type, unsigned char **out_body, size_t *out_len, long *out_code, char *errbuf, size_t errcap); /* endpoint fetches (spfiles_files.c / spfiles_kv.c / spfiles_sessions.c) */ int spfiles_files_fetch(const char *imdb, int season, int episode); int spfiles_files_fetch_keys(const char *const *keys, size_t n); int spfiles_files_index(yyjson_val *arr); void spfiles_files_free(void); int spfiles_kv_fetch(const char *kind, const char *prefix); void spfiles_kv_free(void); int spfiles_index_playlists(yyjson_val *rows); int spfiles_index_progress(yyjson_val *rows); int spfiles_index_subs(const char *imdb, int season, int episode, yyjson_val *rows); int spfiles_index_sessionix(yyjson_val *rows); int spfiles_storages_fetch(void); int spfiles_sessions_fetch(const char *ids); void spfiles_sessions_free(void); void spfiles_free_all(void); /* fake (spfiles_fake.c) */ const scorpiox_http *spfiles_fake_backend(const char *corpus_dir); void spfiles_fake_install(void); long long spfiles_fake_calls_internal(void); long long spfiles_fake_fail_internal(void); const char *spfiles_fake_last_url_internal(void); void spfiles_fake_set_mode(int mode); void spfiles_fake_wipe_stats(void); #endif int spfiles_storages_project(yyjson_val *rows); /* test/fuzz hook */