/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* url-util.c : utility functions to parse URLs */ /* * Copyright (C) 1999 Bertrand Guiheneuf * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ /* Here we deal with URL following the general scheme: protocol://user:password@host:port/name where name is a path-like string (ie dir1/dir2/....) See rfc1738 for the complete description of Uniform Ressource Locators Bertrand. */ /* XXX TODO: recover the words between #'s or ?'s after the path */ #include "url-util.h" /* general item finder */ /* it begins the search at position @position in @url, returns true when the item is found, amd set position after the item */ typedef gboolean find_item_func(GString *url, GString **item, guint *position, gboolean *error); /* used to find one item (protocol, then user .... */ typedef struct { char *item_name; /* item name : for debug only */ GString **item_value; /* where the item value will go */ find_item_func *find_func; /* item finder */ } FindStepStruct; static gboolean find_protocol(GString *url, GString **item, guint *position, gboolean *error); static gboolean find_user(GString *url, GString **item, guint *position, gboolean *error); static gboolean find_passwd(GString *url, GString **item, guint *position, gboolean *error); static gboolean find_host(GString *url, GString **item, guint *position, gboolean *error); static gboolean find_port(GString *url, GString **item, guint *position, gboolean *error); static gboolean find_path(GString *url, GString **item, guint *position, gboolean *error); /** * new_g_url: create an Gurl object from a string * @url_string: The string containing the URL to scan * * This routine takes a GString and parses it as an * URL of the form: * protocol://user:password@host:port/path * there is no test on the values. For example, * "port" can be a string, not only a number ! * The Gurl structure fields ar filled with * the scan results. When a member of the * general URL can not be found, the corresponding * Gurl member is NULL * * Return value: a Gurl structure containng the URL items. **/ Gurl *g_url_new(GString* url_string) { Gurl *g_url; GString *protocol; GString *user; GString *passwd; GString *host; GString *port; GString *path; guint position=0; gboolean error; gboolean found; guint i; g_url = g_new(Gurl,1); #define NB_STEP_URL 6 { FindStepStruct step[NB_STEP_URL] = { { "protocol", &(g_url->protocol), find_protocol}, { "user", &(g_url->user), find_user}, { "password", &(g_url->passwd), find_passwd}, { "host", &(g_url->host), find_host}, { "port", &(g_url->port), find_port}, { "path", &(g_url->path), find_path} }; for (i=0; istr; len_url = url->len; *item = NULL; *error = FALSE; i=*position; /* find a ':' */ while ( (istr; len_url = url->len; *item = NULL; i=*position; /* find a '@' */ while ((istr; len_url = url->len; *item = NULL; i=*position; /* find a '@' */ while ((istr; len_url = url->len; *item = NULL; i=*position; /* find a '/' */ while ((istr; len_url = url->len; *item = NULL; i=*position; /* find a '/' */ while ((istr; len_url = url->len; *item = NULL; i=*position; /* find a '#' */ while ((istr); for (i=0; istr); } else printf("** %s not found in URL\n", test_step[i].item_name); printf("next item position:\n"); printf("%s\n", url->str); for(i_pos=0; i_pos