Subversion Repositories Kolibri OS

Rev

Rev 3584 | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 2011 Michael Drake <tlsa@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.  * NetSurf URL handling (interface).
  21.  */
  22.  
  23. #ifndef _NETSURF_UTILS_NSURL_H_
  24. #define _NETSURF_UTILS_NSURL_H_
  25.  
  26. #include <libwapcaplet/libwapcaplet.h>
  27. #include "utils/errors.h"
  28.  
  29.  
  30. /** NetSurf URL object */
  31. typedef struct nsurl nsurl;
  32.  
  33.  
  34. typedef enum nsurl_component {
  35.         NSURL_SCHEME            = (1 << 0),
  36.         NSURL_USERNAME          = (1 << 1),
  37.         NSURL_PASSWORD          = (1 << 2),
  38.         NSURL_CREDENTIALS       = NSURL_USERNAME | NSURL_PASSWORD,
  39.         NSURL_HOST              = (1 << 3),
  40.         NSURL_PORT              = (1 << 4),
  41.         NSURL_AUTHORITY         = NSURL_CREDENTIALS | NSURL_HOST | NSURL_PORT,
  42.         NSURL_PATH              = (1 << 5),
  43.         NSURL_QUERY             = (1 << 6),
  44.         NSURL_COMPLETE          = NSURL_SCHEME | NSURL_AUTHORITY |
  45.                                   NSURL_PATH | NSURL_QUERY,
  46.         NSURL_FRAGMENT          = (1 << 7),
  47.         NSURL_WITH_FRAGMENT     = NSURL_COMPLETE | NSURL_FRAGMENT
  48. } nsurl_component;
  49.  
  50.  
  51. /**
  52.  * Create a NetSurf URL object from a URL string
  53.  *
  54.  * \param url_s   String to create NetSurf URL from
  55.  * \param url     Returns a NetSurf URL
  56.  * \return NSERROR_OK on success, appropriate error otherwise
  57.  *
  58.  * If return value != NSERROR_OK, nothing will be returned in url.
  59.  *
  60.  * It is up to the client to call nsurl_destroy when they are finished with
  61.  * the created object.
  62.  */
  63. nserror nsurl_create(const char * const url_s, nsurl **url);
  64.  
  65.  
  66. /**
  67.  * Increment the reference count to a NetSurf URL object
  68.  *
  69.  * \param url     NetSurf URL to create another reference to
  70.  * \return The NetSurf URL pointer to use as the copy
  71.  *
  72.  * Use this when copying a NetSurf URL into a persistent data structure.
  73.  */
  74. nsurl *nsurl_ref(nsurl *url);
  75.  
  76.  
  77. /**
  78.  * Drop a reference to a NetSurf URL object
  79.  *
  80.  * \param url     NetSurf URL to drop reference to
  81.  *
  82.  * When the reference count reaches zero then the NetSurf URL will be destroyed
  83.  */
  84. void nsurl_unref(nsurl *url);
  85.  
  86.  
  87. /**
  88.  * Compare two URLs
  89.  *
  90.  * \param url1    First NetSurf URL
  91.  * \param url2    Second NetSurf URL
  92.  * \param parts   The URL components to be compared
  93.  * \param match   Returns true if url1 and url2 matched, else false
  94.  * \return NSERROR_OK on success, appropriate error otherwise
  95.  *
  96.  * If return value != NSERROR_OK, match will be false.
  97.  */
  98. bool nsurl_compare(const nsurl *url1, const nsurl *url2, nsurl_component parts);
  99.  
  100.  
  101. /**
  102.  * Get URL (section) as a string, from a NetSurf URL object
  103.  *
  104.  * \param url     NetSurf URL
  105.  * \param parts   The required URL components.
  106.  * \param url_s   Returns a url string
  107.  * \param url_l   Returns length of url_s
  108.  * \return NSERROR_OK on success, appropriate error otherwise
  109.  *
  110.  * If return value != NSERROR_OK, nothing will be returned in url_s or url_l.
  111.  *
  112.  * The string returned in url_s is owned by the client and it is up to them
  113.  * to free it.  It includes a trailing '\0'.
  114.  *
  115.  * The length returned in url_l excludes the trailing '\0'.
  116.  *
  117.  * That the required URL components be consecutive is not enforced, however,
  118.  * non-consecutive URL components generally make no sense.  The exception
  119.  * is removal of credentials from a URL, such as for display in browser
  120.  * window URL bar.  'NSURL_COMPLETE &~ NSURL_PASSWORD' would remove the
  121.  * password from a complete URL.
  122.  */
  123. nserror nsurl_get(const nsurl *url, nsurl_component parts,
  124.                 char **url_s, size_t *url_l);
  125.  
  126.  
  127. /**
  128.  * Get part of a URL as a lwc_string, from a NetSurf URL object
  129.  *
  130.  * \param url     NetSurf URL object
  131.  * \param part    The URL component required
  132.  * \return the required component as an lwc_string, or NULL
  133.  *
  134.  * The caller owns the returned lwc_string and should call lwc_string_unref
  135.  * when they are done with it.
  136.  *
  137.  * The valid values for the part parameter are:
  138.  *    NSURL_SCHEME
  139.  *    NSURL_USERNAME
  140.  *    NSURL_PASSWORD
  141.  *    NSURL_HOST
  142.  *    NSURL_PORT
  143.  *    NSURL_PATH
  144.  *    NSURL_QUERY
  145.  *    NSURL_FRAGMENT
  146.  */
  147. lwc_string *nsurl_get_component(const nsurl *url, nsurl_component part);
  148.  
  149.  
  150. /**
  151.  * Enquire about the existence of componenets in a given URL
  152.  *
  153.  * \param url     NetSurf URL object
  154.  * \param part    The URL components confirm existence of
  155.  * \return true iff the component in question exists in url
  156.  *
  157.  * The valid values for the part parameter are:
  158.  *    NSURL_SCHEME
  159.  *    NSURL_USERNAME
  160.  *    NSURL_PASSWORD
  161.  *    NSURL_CREDENTIALS
  162.  *    NSURL_HOST
  163.  *    NSURL_PORT
  164.  *    NSURL_PATH
  165.  *    NSURL_QUERY
  166.  *    NSURL_FRAGMENT
  167.  */
  168. bool nsurl_has_component(const nsurl *url, nsurl_component part);
  169.  
  170.  
  171. /**
  172.  * Access a NetSurf URL object as a string
  173.  *
  174.  * \param url     NetSurf URL to retrieve a string pointer for.
  175.  * \return the required string
  176.  *
  177.  * The returned string is owned by the NetSurf URL object.  It will die
  178.  * with the NetSurf URL object.  Keep a reference to the URL if you need it.
  179.  *
  180.  * The returned string has a trailing '\0'.
  181.  */
  182. const char *nsurl_access(const nsurl *url);
  183.  
  184.  
  185. /**
  186.  * Access a URL's path leaf as a string
  187.  *
  188.  * \param url     NetSurf URL to retrieve a string pointer for.
  189.  * \return the required string
  190.  *
  191.  * The returned string is owned by the NetSurf URL object.  It will die
  192.  * with the NetSurf URL object.  Keep a reference to the URL if you need it.
  193.  *
  194.  * The returned string has a trailing '\0'.
  195.  */
  196. const char *nsurl_access_leaf(const nsurl *url);
  197.  
  198.  
  199. /**
  200.  * Find the length of a NetSurf URL object's URL, as returned by nsurl_access
  201.  *
  202.  * \param url     NetSurf URL to find length of.
  203.  * \return the required string
  204.  *
  205.  * The returned length excludes the trailing '\0'.
  206.  */
  207. size_t nsurl_length(const nsurl *url);
  208.  
  209.  
  210. /**
  211.  * Join a base url to a relative link part, creating a new NetSurf URL object
  212.  *
  213.  * \param base    NetSurf URL containing the base to join rel to
  214.  * \param rel     String containing the relative link part
  215.  * \param joined  Returns joined NetSurf URL
  216.  * \return NSERROR_OK on success, appropriate error otherwise
  217.  *
  218.  * If return value != NSERROR_OK, nothing will be returned in join.
  219.  *
  220.  * It is up to the client to call nsurl_destroy when they are finished with
  221.  * the created object.
  222.  */
  223. nserror nsurl_join(const nsurl *base, const char *rel, nsurl **joined);
  224.  
  225.  
  226. /**
  227.  * Create a NetSurf URL object without a fragment from a NetSurf URL
  228.  *
  229.  * \param url     NetSurf URL to create new NetSurf URL from
  230.  * \param no_frag Returns new NetSurf URL without fragment
  231.  * \return NSERROR_OK on success, appropriate error otherwise
  232.  *
  233.  * If return value != NSERROR_OK, nothing will be returned in no_frag.
  234.  *
  235.  * It is up to the client to call nsurl_destroy when they are finished with
  236.  * the created object.
  237.  */
  238. nserror nsurl_defragment(const nsurl *url, nsurl **no_frag);
  239.  
  240.  
  241. /**
  242.  * Create a NetSurf URL object, adding a fragment to an existing URL object
  243.  *
  244.  * \param url     NetSurf URL to create new NetSurf URL from
  245.  * \param frag    Fragment to add
  246.  * \param new_url Returns new NetSurf URL without fragment
  247.  * \return NSERROR_OK on success, appropriate error otherwise
  248.  *
  249.  * If return value != NSERROR_OK, nothing will be returned in new_url.
  250.  *
  251.  * It is up to the client to call nsurl_destroy when they are finished with
  252.  * the created object.
  253.  *
  254.  * Any fragment in url is replaced with frag in new_url.
  255.  */
  256. nserror nsurl_refragment(const nsurl *url, lwc_string *frag, nsurl **new_url);
  257.  
  258.  
  259. /**
  260.  * Create a NetSurf URL object, with query string replaced
  261.  *
  262.  * \param url     NetSurf URL to create new NetSurf URL from
  263.  * \param query   Query string to use
  264.  * \param new_url Returns new NetSurf URL with query string provided
  265.  * \return NSERROR_OK on success, appropriate error otherwise
  266.  *
  267.  * If return value != NSERROR_OK, nothing will be returned in new_url.
  268.  *
  269.  * It is up to the client to call nsurl_destroy when they are finished with
  270.  * the created object.
  271.  *
  272.  * Any query component in url is replaced with query in new_url.
  273.  */
  274. nserror nsurl_replace_query(const nsurl *url, const char *query,
  275.                 nsurl **new_url);
  276.  
  277.  
  278. /**
  279.  * Create a NetSurf URL object for URL with parent location of an existing URL.
  280.  *
  281.  * \param url     NetSurf URL to create new NetSurf URL from
  282.  * \param new_url Returns new NetSurf URL with parent URL path
  283.  * \return NSERROR_OK on success, appropriate error otherwise
  284.  *
  285.  * If return value != NSERROR_OK, nothing will be returned in new_url.
  286.  *
  287.  * It is up to the client to call nsurl_destroy when they are finished with
  288.  * the created object.
  289.  *
  290.  * As well as stripping top most path segment, query and fragments are stripped.
  291.  */
  292. nserror nsurl_parent(const nsurl *url, nsurl **new_url);
  293.  
  294. #endif
  295.