/* Copyright 1997 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/***************************************************/
/* File   : URLutils.h                             */
/*                                                 */
/* Purpose: URL manipulation for the browser.      */
/*                                                 */
/* Author : Merlyn Kline for Customer browser     */
/*          This source adapted by A.D.Hodgkinson  */
/*          from various original functions        */
/*                                                 */
/* History: 06-Feb-97: Created.                    */
/***************************************************/

#include "URI.h" /* URI handler API, in URILib:h */

/* General definitions */

#define Internal_URL      "!intrnl!"  /* Special string (i.e. hack...) to mark a URL as internal only. */
#define Int_URL_Len       8           /* Saves having to strlen(Internal_URL) in various places.       */

#define ForExternalHImage "pextimage" /* (With 'Go back') */
#define ForExternalNImage "pextimnoh" /* (No 'Go back')   */
#define ForScrapFile      "scrapfile"
#define ForGoBack         "goback"
#define ForGoRecover      "gorecover"
#define ForGoHome         "gohome"
#define ForAbout          "about"

#define ProtocolSeparator "//"
#define ProtocolSepShort  "/"         /* For 'file:/' not 'file://' */
#define FileMethod        "file:"
#define HTTPmethod        "http:"
#define FTPmethod         "ftp:"
#define GopherMethod      "gopher:"
#define AboutMethod       "about:"

/* Structures */

/* For any URL, this can hold a more complete description than     */
/* strings, and makes comparing two URLs in a valid manner easier. */

typedef struct url_description
{
  char * full;     /* Complete, canonicalised URL         */

  char * protocol; /* Such as 'http' or 'mailto'          */
  char * host;     /* E.g. 'www.acorn.com'                */
  char * port;     /* For example '8080'                  */

  char * user;     /* E.g. 'ahodgkin'                     */
  char * password; /* E.g. 'NotMine'                      */
  char * account;  /* As in ftp://user:pass:account@host/ */

  char * path;     /* Speaks for itself                   */

  char * query;    /* CGI info - after a '?' in a URL     */
  char * fragment; /* Anchor info - after a '#' in a URL  */
}
url_description;

/* If using the URI handler, need to keep an array of all */
/* URIs sent out so that if they get back to the browser  */
/* and it can handle them, the browser_data struct (see   */
/* below) that they were allocated for and various useful */
/* flags associated with the URI can be recovered.        */
/*                                                        */
/* In practice, the items are stored in a linked list to  */
/* allow easy removal of items without copying memory     */
/* around all over the place.                             */

/* Flags definitions */

#define URIQueue_RecordInHistory (1u<<0)

/* Structure for queue entries */

typedef struct uri_queue
{
  unsigned int       flags;
  browser_data     * b;
  URI_handle_t       uri_handle;

  struct uri_queue * prev;
  struct uri_queue * next;

} uri_queue;

/* Function prototypes */

int               urlutils_urlsscmp           (const char * url_s1, const char * url_s2);
int               urlutils_urldscmp           (const url_description * url_d, const char * url_s);
int               urlutils_urlddcmp           (const url_description * url_d1, const url_description * url_d2);
url_description * urlutils_return_description (const char * url_s);
void              urlutils_free_description   (url_description * url_d);

void              urlutils_pathname_to_url    (char * path, int buffersize);
void              urlutils_url_to_pathname    (char * url, int buffersize);
void              urlutils_translate_pathname (char * path);
char            * urlutils_leafname_from_url  (char * url, char * leaf, int size);
void              urlutils_host_name_from_url (char * url, char * host, int size);
int               urlutils_filetype_from_url  (const char * url);

void              urlutils_create_hotlist_url (char * buffer, int size);
void              urlutils_create_home_url    (char * buffer, int size);

char            * urlutils_fix_url            (char * buffer, int buffersize);

void              urlutils_load_uri_file      (char * buffer, size_t size, char * tbuffer, size_t tsize, char * path);
void              urlutils_extract_uri        (char * buffer, size_t file_size);

int               urlutils_internal_extra     (char * iurl);
int               urlutils_internal_tail      (char * iurl);
void              urlutils_set_displayed      (browser_data * b, char * iurl);

int               urlutils_check_protocols    (char * url);
int               urlutils_cycle_protocol     (char * url, int size);

_kernel_oserror * urlutils_dispatch           (browser_data * b, char * uri, unsigned int flags);
_kernel_oserror * urlutils_remove_from_queue  (URI_handle_t uri_handle);
uri_queue       * urlutils_find_queue_entry   (URI_handle_t uri_handle);