| 1 | /* |
| 2 | * HTTPDirFS - HTTP Directory Filesystem |
| 3 | * |
| 4 | * Copyright (C) 2020-2026 Fufu Fang <[email protected]> |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * In addition, as a special exception, the copyright holders give |
| 20 | * permission to link the code of portions of this program with the OpenSSL |
| 21 | * library. |
| 22 | */ |
| 23 | |
| 24 | #ifndef LINK_H |
| 25 | #define LINK_H |
| 26 | |
| 27 | /** |
| 28 | * \file link.h |
| 29 | * \brief Link structure and handling functions header |
| 30 | */ |
| 31 | |
| 32 | #include <curl/curl.h> |
| 33 | #include <limits.h> |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | #include "memcache.h" |
| 37 | #include "sonic.h" |
| 38 | |
| 39 | typedef struct Cache Cache; |
| 40 | typedef struct Link Link; |
| 41 | typedef struct LinkTable LinkTable; |
| 42 | |
| 43 | |
| 44 | /** |
| 45 | * \brief the link type |
| 46 | */ |
| 47 | typedef enum { |
| 48 | LINK_HEAD = 'H', |
| 49 | LINK_DIR = 'D', |
| 50 | LINK_FILE = 'F', |
| 51 | LINK_INVALID = 'I', |
| 52 | LINK_UNINITIALISED_FILE = 'U', |
| 53 | LINK_UNINITIALISED_DIR = 'V', |
| 54 | } LinkType; |
| 55 | |
| 56 | /** |
| 57 | * \brief link table type |
| 58 | * \details index 0 contains the Link for the base URL |
| 59 | */ |
| 60 | struct LinkTable { |
| 61 | int size; |
| 62 | time_t index_time; |
| 63 | Link **links; |
| 64 | int refcount; |
| 65 | int orphaned; |
| 66 | struct LinkTable *parent_tbl; |
| 67 | Link *parent_link; |
| 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * \brief Link type data structure |
| 72 | */ |
| 73 | struct Link { |
| 74 | /** \brief The parent LinkTable of this link */ |
| 75 | struct LinkTable *parent_table; |
| 76 | /** \brief The link name in the last level of the URL */ |
| 77 | char linkname[NAME_MAX + 1]; |
| 78 | /** \brief This is for storing the unescaped path */ |
| 79 | char linkpath[NAME_MAX + 1]; |
| 80 | /** \brief The full URL of the file */ |
| 81 | char f_url[PATH_MAX + 1]; |
| 82 | /** \brief The type of the link */ |
| 83 | LinkType type; |
| 84 | /** \brief CURLINFO_CONTENT_LENGTH_DOWNLOAD of the file */ |
| 85 | size_t content_length; |
| 86 | /** \brief The next LinkTable level, if it is a LINK_DIR */ |
| 87 | LinkTable *next_table; |
| 88 | /** \brief CURLINFO_FILETIME obtained from the server */ |
| 89 | long time; |
| 90 | /** \brief The pointer associated with the cache file */ |
| 91 | Cache *cache_ptr; |
| 92 | /** \brief Stores *sonic related data */ |
| 93 | Sonic sonic; |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * \brief root link table |
| 98 | */ |
| 99 | extern LinkTable *ROOT_LINK_TBL; |
| 100 | |
| 101 | /** |
| 102 | * \brief the offset for calculating partial URL |
| 103 | */ |
| 104 | extern int ROOT_LINK_OFFSET; |
| 105 | |
| 106 | /** |
| 107 | * \brief initialise link sub-system. |
| 108 | */ |
| 109 | LinkTable *LinkSystem_init(const char *raw_url); |
| 110 | |
| 111 | /** |
| 112 | * \brief Set the stats of a link, after curl multi handle finished querying |
| 113 | */ |
| 114 | void Link_set_file_stat(Link *this_link, CURL *curl); |
| 115 | |
| 116 | /** |
| 117 | * \brief create a new LinkTable |
| 118 | */ |
| 119 | LinkTable *LinkTable_new(const char *url); |
| 120 | |
| 121 | /** |
| 122 | * \brief download a path |
| 123 | * \return the number of bytes downloaded |
| 124 | */ |
| 125 | long path_download(const char *path, char *output_buf, size_t size, |
| 126 | off_t offset); |
| 127 | |
| 128 | /** |
| 129 | * \brief Download a Link |
| 130 | * \return the number of bytes downloaded |
| 131 | */ |
| 132 | long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset, |
| 133 | Cache *cf); |
| 134 | |
| 135 | /** |
| 136 | * \brief find the link associated with a path |
| 137 | */ |
| 138 | Link *path_to_Link(const char *path); |
| 139 | |
| 140 | /** |
| 141 | * \brief return the link table for the associated path |
| 142 | */ |
| 143 | LinkTable *path_to_LinkTable(const char *path); |
| 144 | |
| 145 | /** |
| 146 | * \brief dump a link table to the disk. |
| 147 | */ |
| 148 | int LinkTable_disk_save(LinkTable *linktbl, const char *dirn); |
| 149 | |
| 150 | /** |
| 151 | * \brief load a link table from the disk. |
| 152 | * \param[in] dirn We expected the unescaped_path here! |
| 153 | */ |
| 154 | LinkTable *LinkTable_disk_open(const char *dirn); |
| 155 | |
| 156 | /** |
| 157 | * \brief Download a link's content to the memory |
| 158 | * \warning You MUST free the memory field in TransferStruct after use! |
| 159 | */ |
| 160 | TransferStruct Link_download_full(Link *head_link); |
| 161 | |
| 162 | /** |
| 163 | * \brief Allocate a LinkTable |
| 164 | * \note This does not fill in the LinkTable. |
| 165 | */ |
| 166 | LinkTable *LinkTable_alloc(const char *url); |
| 167 | |
| 168 | /** |
| 169 | * \brief free a LinkTable |
| 170 | */ |
| 171 | void LinkTable_free(LinkTable *linktbl); |
| 172 | |
| 173 | /** |
| 174 | * \brief increment the reference count of a LinkTable |
| 175 | */ |
| 176 | void LinkTable_ref(LinkTable *tbl); |
| 177 | |
| 178 | /** |
| 179 | * \brief decrement the reference count of a LinkTable |
| 180 | */ |
| 181 | void LinkTable_unref(LinkTable *tbl); |
| 182 | |
| 183 | /** |
| 184 | * \brief mark a LinkTable as orphaned so it can be evicted |
| 185 | */ |
| 186 | void LinkTable_mark_orphaned(LinkTable *tbl); |
| 187 | |
| 188 | /** |
| 189 | * \brief print a LinkTable |
| 190 | */ |
| 191 | void LinkTable_print(LinkTable *linktbl); |
| 192 | |
| 193 | /** |
| 194 | * \brief add a Link to a LinkTable |
| 195 | */ |
| 196 | void LinkTable_add(LinkTable *linktbl, Link *link); |
| 197 | |
| 198 | /** |
| 199 | * \brief Parse HTML content and populate LinkTable with unique links. |
| 200 | */ |
| 201 | void LinkTable_parse_html(LinkTable *linktbl, const char *url, |
| 202 | const char *html); |
| 203 | |
| 204 | /* |
| 205 | * Functions exposed for unit testing duplicated URL logic |
| 206 | */ |
| 207 | typedef struct LinkHashSet LinkHashSet; |
| 208 | |
| 209 | /** |
| 210 | * \brief Check if two link names are equal, normalizing any single trailing |
| 211 | * slash. |
| 212 | * \param str_a The first link name string to compare. |
| 213 | * \param str_b The second link name string to compare. |
| 214 | * \return 1 if they are equivalent, 0 otherwise. |
| 215 | */ |
| 216 | int link_linknames_equal(const char *str_a, const char *str_b); |
| 217 | |
| 218 | /** |
| 219 | * \brief Generate a hash value for a link name, ignoring any trailing slashes. |
| 220 | * \param str The link name string to hash. |
| 221 | * \return The generated unsigned int hash value. |
| 222 | */ |
| 223 | unsigned int link_hash_str(const char *str); |
| 224 | |
| 225 | /** |
| 226 | * \brief Create a new LinkHashSet with a specified initial capacity. |
| 227 | * \param capacity The initial number of buckets to allocate. |
| 228 | * \return Pointer to the newly allocated LinkHashSet. |
| 229 | */ |
| 230 | LinkHashSet *LinkHashSet_new(int capacity); |
| 231 | |
| 232 | /** |
| 233 | * \brief Add a link name to the LinkHashSet if it is not already present. |
| 234 | * \param set The LinkHashSet to insert the link name into. |
| 235 | * \param linkname The link name string to add. |
| 236 | * \return 1 if successfully added (not a duplicate), 0 if it is a duplicate. |
| 237 | */ |
| 238 | int LinkHashSet_add(LinkHashSet *set, const char *linkname); |
| 239 | |
| 240 | /** |
| 241 | * \brief Free all memory allocated for a LinkHashSet. |
| 242 | * \param set The LinkHashSet to deallocate. |
| 243 | */ |
| 244 | void LinkHashSet_free(LinkHashSet *set); |
| 245 | |
| 246 | /** |
| 247 | * \brief Check if a URL is an external (absolute) http/https URL. |
| 248 | * \return 1 if the URL starts with http:// or https://, 0 otherwise |
| 249 | */ |
| 250 | int is_external_url(const char *url); |
| 251 | |
| 252 | /** |
| 253 | * \brief Check if link_url has a different origin than page_url. |
| 254 | * \details Compares scheme + host + port. Malformed URLs are treated as |
| 255 | * cross-origin. |
| 256 | * \return 1 if cross-origin or either URL is malformed, 0 if same origin |
| 257 | */ |
| 258 | int is_cross_origin(const char *page_url, const char *link_url); |
| 259 | |
| 260 | /** |
| 261 | * \brief Extract the filename component from an external URL. |
| 262 | * \details For "http://example.com/path/file.iso" returns "file.iso". |
| 263 | * For "http://example.com/path/dir/" returns "dir". |
| 264 | * Query strings are stripped. Returns "" for root-only URLs. |
| 265 | * \note The caller must free the returned string with FREE(). |
| 266 | */ |
| 267 | char *external_url_to_filename(const char *url); |
| 268 | |
| 269 | /** |
| 270 | * \brief Safely generate the cache path for a given URL, handling cross-origin |
| 271 | * external links. |
| 272 | * \note The caller must free the returned string with FREE(). |
| 273 | */ |
| 274 | char *url_to_cache_path(const char *url); |
| 275 | #endif |