Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2009 John-Mark Bell <jmb@netsurf-browser.org>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf 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; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. /** \file
  20.  * Low-level resource cache (interface)
  21.  */
  22.  
  23. #ifndef NETSURF_CONTENT_LLCACHE_H_
  24. #define NETSURF_CONTENT_LLCACHE_H_
  25.  
  26. #include <stdbool.h>
  27. #include <stddef.h>
  28. #include <stdint.h>
  29.  
  30. #include "utils/errors.h"
  31. #include "utils/nsurl.h"
  32.  
  33. struct ssl_cert_info;
  34. struct fetch_multipart_data;
  35.  
  36. /** Handle for low-level cache object */
  37. typedef struct llcache_handle llcache_handle;
  38.  
  39. /** POST data object for low-level cache requests */
  40. typedef struct {
  41.         enum {
  42.                 LLCACHE_POST_URL_ENCODED,
  43.                 LLCACHE_POST_MULTIPART
  44.         } type;                         /**< Type of POST data */
  45.         union {
  46.                 char *urlenc;           /**< URL encoded data */
  47.                 struct fetch_multipart_data *multipart; /**< Multipart data */
  48.         } data;                         /**< POST data content */
  49. } llcache_post_data;
  50.  
  51. /** Low-level cache event types */
  52. typedef enum {
  53.         LLCACHE_EVENT_HAD_HEADERS,      /**< Received all headers */
  54.         LLCACHE_EVENT_HAD_DATA,         /**< Received some data */
  55.         LLCACHE_EVENT_DONE,             /**< Finished fetching data */
  56.  
  57.         LLCACHE_EVENT_ERROR,            /**< An error occurred during fetch */
  58.         LLCACHE_EVENT_PROGRESS,         /**< Fetch progress update */
  59. } llcache_event_type;
  60.  
  61. /** Low-level cache events */
  62. typedef struct {
  63.         llcache_event_type type;        /**< Type of event */
  64.         union {
  65.                 struct {
  66.                         const uint8_t *buf;     /**< Buffer of data */
  67.                         size_t len;     /**< Length of buffer, in bytes */
  68.                 } data;                 /**< Received data */
  69.                 const char *msg;        /**< Error or progress message */
  70.         } data;                         /**< Event data */
  71. } llcache_event;
  72.  
  73. /**
  74.  * Client callback for low-level cache events
  75.  *
  76.  * \param handle  Handle for which event is issued
  77.  * \param event   Event data
  78.  * \param pw      Pointer to client-specific data
  79.  * \return NSERROR_OK on success, appropriate error otherwise.
  80.  */
  81. typedef nserror (*llcache_handle_callback)(llcache_handle *handle,
  82.                 const llcache_event *event, void *pw);
  83.  
  84. /** Flags for low-level cache object retrieval */
  85. enum llcache_retrieve_flag {
  86.         /* Note: We're permitted a maximum of 16 flags which must reside in the
  87.          * bottom 16 bits of the flags word. See hlcache.h for further details.
  88.          */
  89.         /** Force a new fetch */
  90.         LLCACHE_RETRIEVE_FORCE_FETCH    = (1 << 0),
  91.         /** Requested URL was verified */
  92.         LLCACHE_RETRIEVE_VERIFIABLE     = (1 << 1),
  93.         /**< No error pages */
  94.         LLCACHE_RETRIEVE_NO_ERROR_PAGES = (1 << 2),
  95.         /**< Stream data (implies that object is not cacheable) */
  96.         LLCACHE_RETRIEVE_STREAM_DATA    = (1 << 3)
  97. };
  98.  
  99. /** Low-level cache query types */
  100. typedef enum {
  101.         LLCACHE_QUERY_AUTH,             /**< Need authentication details */
  102.         LLCACHE_QUERY_REDIRECT,         /**< Need permission to redirect */
  103.         LLCACHE_QUERY_SSL               /**< SSL chain needs inspection */
  104. } llcache_query_type;
  105.  
  106. /** Low-level cache query */
  107. typedef struct {
  108.         llcache_query_type type;        /**< Type of query */
  109.  
  110.         nsurl *url;                     /**< URL being fetched */
  111.  
  112.         union {
  113.                 struct {
  114.                         const char *realm;      /**< Authentication realm */
  115.                 } auth;
  116.  
  117.                 struct {
  118.                         const char *target;     /**< Redirect target */
  119.                 } redirect;
  120.  
  121.                 struct {
  122.                         const struct ssl_cert_info *certs;
  123.                         size_t num;             /**< Number of certs in chain */
  124.                 } ssl;
  125.         } data;
  126. } llcache_query;
  127.  
  128. /**
  129.  * Response handler for fetch-related queries
  130.  *
  131.  * \param proceed  Whether to proceed with the fetch or not
  132.  * \param cbpw     Opaque value provided to llcache_query_callback
  133.  * \return NSERROR_OK on success, appropriate error otherwise
  134.  */
  135. typedef nserror (*llcache_query_response)(bool proceed, void *cbpw);
  136.  
  137. /**
  138.  * Callback to handle fetch-related queries
  139.  *
  140.  * \param query  Object containing details of query
  141.  * \param pw     Pointer to callback-specific data
  142.  * \param cb     Callback that client should call once query is satisfied
  143.  * \param cbpw   Opaque value to pass into \a cb
  144.  * \return NSERROR_OK on success, appropriate error otherwise
  145.  *
  146.  * \note This callback should return immediately. Once a suitable answer to
  147.  *       the query has been obtained, the provided response callback should be
  148.  *       called. This is intended to be an entirely asynchronous process.
  149.  */
  150. typedef nserror (*llcache_query_callback)(const llcache_query *query, void *pw,
  151.                 llcache_query_response cb, void *cbpw);
  152.  
  153. /**
  154.  * Initialise the low-level cache
  155.  *
  156.  * \param cb  Query handler
  157.  * \param pw  Pointer to query handler data
  158.  * \return NSERROR_OK on success, appropriate error otherwise.
  159.  */
  160. nserror llcache_initialise(llcache_query_callback cb, void *pw, uint32_t llcache_limit);
  161.  
  162. /**
  163.  * Finalise the low-level cache
  164.  */
  165. void llcache_finalise(void);
  166.  
  167. /**
  168.  * Cause the low-level cache to emit any pending notifications.
  169.  *
  170.  * \return NSERROR_OK on success, appropriate error otherwise.
  171.  */
  172. nserror llcache_poll(void);
  173.  
  174. /**
  175.  * Cause the low-level cache to attempt to perform cleanup.  No
  176.  * guarantees are made as to whether or not cleanups will take
  177.  * place and what, if any, space savings will be made.
  178.  */
  179. void llcache_clean(void);
  180.  
  181. /**
  182.  * Retrieve a handle for a low-level cache object
  183.  *
  184.  * \param url      URL of the object to fetch
  185.  * \param flags    Object retrieval flags
  186.  * \param referer  Referring URL, or NULL if none
  187.  * \param post     POST data, or NULL for a GET request
  188.  * \param cb       Client callback for events
  189.  * \param pw       Pointer to client-specific data
  190.  * \param result   Pointer to location to recieve cache handle
  191.  * \return NSERROR_OK on success, appropriate error otherwise
  192.  */
  193. nserror llcache_handle_retrieve(nsurl *url, uint32_t flags,
  194.                 nsurl *referer, const llcache_post_data *post,
  195.                 llcache_handle_callback cb, void *pw,
  196.                 llcache_handle **result);
  197.  
  198. /**
  199.  * Change the callback associated with a low-level cache handle
  200.  *
  201.  * \param handle  Handle to change callback of
  202.  * \param cb      New callback
  203.  * \param pw      Client data for new callback
  204.  * \return NSERROR_OK on success, appropriate error otherwise
  205.  */
  206. nserror llcache_handle_change_callback(llcache_handle *handle,
  207.                 llcache_handle_callback cb, void *pw);
  208.  
  209. /**
  210.  * Release a low-level cache handle
  211.  *
  212.  * \param handle  Handle to release
  213.  * \return NSERROR_OK on success, appropriate error otherwise
  214.  */
  215. nserror llcache_handle_release(llcache_handle *handle);
  216.  
  217. /**
  218.  * Clone a low-level cache handle, producing a new handle to
  219.  * the same fetch/content.
  220.  *
  221.  * \param handle  Handle to clone
  222.  * \param result  Pointer to location to receive cloned handle
  223.  * \return NSERROR_OK on success, appropriate error otherwise
  224.  */
  225. nserror llcache_handle_clone(llcache_handle *handle, llcache_handle **result);
  226.  
  227. /**
  228.  * Abort a low-level fetch, informing all users of this action.
  229.  *
  230.  * \param handle  Handle to abort
  231.  * \return NSERROR_OK on success, appropriate error otherwise
  232.  */
  233. nserror llcache_handle_abort(llcache_handle *handle);
  234.  
  235. /**
  236.  * Force a low-level cache handle into streaming mode
  237.  *
  238.  * \param handle  Handle to stream
  239.  * \return NSERROR_OK on success, appropriate error otherwise
  240.  */
  241. nserror llcache_handle_force_stream(llcache_handle *handle);
  242.  
  243. /**
  244.  * Invalidate cache data for a low-level cache object
  245.  *
  246.  * \param handle  Handle to invalidate
  247.  * \return NSERROR_OK on success, appropriate error otherwise
  248.  */
  249. nserror llcache_handle_invalidate_cache_data(llcache_handle *handle);
  250.  
  251. /**
  252.  * Retrieve the post-redirect URL of a low-level cache object
  253.  *
  254.  * \param handle  Handle to retrieve URL from
  255.  * \return Post-redirect URL of cache object
  256.  */
  257. nsurl *llcache_handle_get_url(const llcache_handle *handle);
  258.  
  259. /**
  260.  * Retrieve source data of a low-level cache object
  261.  *
  262.  * \param handle  Handle to retrieve source data from
  263.  * \param size    Pointer to location to receive byte length of data
  264.  * \return Pointer to source data
  265.  */
  266. const uint8_t *llcache_handle_get_source_data(const llcache_handle *handle,
  267.                 size_t *size);
  268.  
  269. /**
  270.  * Retrieve a header value associated with a low-level cache object
  271.  *
  272.  * \param handle  Handle to retrieve header from
  273.  * \param key     Header name
  274.  * \return Header value, or NULL if header does not exist
  275.  *
  276.  * \todo Make the key an enumeration, to avoid needless string comparisons
  277.  * \todo Forcing the client to parse the header value seems wrong.
  278.  *       Better would be to return the actual value part and an array of
  279.  *       key-value pairs for any additional parameters.
  280.  * \todo Deal with multiple headers of the same key (e.g. Set-Cookie)
  281.  */
  282. const char *llcache_handle_get_header(const llcache_handle *handle,
  283.                 const char *key);
  284.  
  285. /**
  286.  * Determine if the same underlying object is referenced by the given handles
  287.  *
  288.  * \param a  First handle
  289.  * \param b  Second handle
  290.  * \return True if handles reference the same object, false otherwise
  291.  */
  292. bool llcache_handle_references_same_object(const llcache_handle *a,
  293.                 const llcache_handle *b);
  294.  
  295. #endif
  296.