From 427646ed6f9241399203d883014dc6cc6ab37aec Mon Sep 17 00:00:00 2001 From: bertrand Date: Fri, 23 Apr 1999 23:16:14 +0000 Subject: I needed the url functions to use GString, and I wanted a more general 1999-04-24 bertrand * camel/url-util.[ch]: I needed the url functions to use GString, and I wanted a more general scheme so I finally started rewriting the whole thing from scratch. No more code from gzilla :( svn path=/trunk/; revision=872 --- camel/url-util.c | 402 +++++++++++++++++++++---------------------------------- 1 file changed, 149 insertions(+), 253 deletions(-) (limited to 'camel/url-util.c') diff --git a/camel/url-util.c b/camel/url-util.c index f4b769aad8..d1ea09c6c9 100644 --- a/camel/url-util.c +++ b/camel/url-util.c @@ -1,11 +1,9 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* url-util.c : utility functions to parse URLs */ + /* - * This code is adapted form gzillaurl.c (http://www.gzilla.com) - * Copyright (C) Raph Levien - * - * Modifications by Bertrand Guiheneuf + * 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 @@ -25,7 +23,14 @@ - +/* + 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. */ #include /* for isalpha */ @@ -35,292 +40,183 @@ -/** - * g_url_is_absolute: - * @url: - * - * - * - * Return value: - **/ -gboolean -g_url_is_absolute (const char *url) -{ - gint i; - - for (i = 0; url[i] != '\0'; i++) { - if (url[i] == ':') - return TRUE; - else if (!isalpha (url[i])) - return FALSE; - } - return FALSE; -} -/** - * g_url_match_method: - * @url: - * @method: - * - * - * - * Return value: TRUE if the method matches - **/ -gboolean -g_url_match_method (const char *url, const char *method) +static gboolean +find_protocol(GString *url, GString **protocol, guint *position, gboolean *error) { - gint i; - - for (i = 0; method[i] != '\0'; i++) - if (url[i] != method[i]) return FALSE; - return (url[i] == ':'); -} - - - -/** - * g_url_add_slash: - * @url: - * @size_url: - * - * Add the trailing slash if necessary. Return FALSE if there isn't room - * - * Return value: - **/ -gboolean -g_url_add_slash (char *url, gint size_url) -{ - char hostname[256]; - gint port; - char *tail; + guint i; + gchar *str_url; + gint len_url; + gchar *str_protocol; + + str_url = url->str; + len_url = url->len; + + *protocol = NULL; + *error = FALSE; + i=*position; + + /* find a ':' */ + while ( (i= size_new_url) - return FALSE; - strcpy (new_url, relative_url); - return g_url_add_slash (new_url, size_new_url); - } + gchar *str_url; + gint len_url; + gchar *str_user; - /* Assure that we have enough room for at least the base URL. */ - if (strlen (base_url) >= size_new_url) - return FALSE; + str_url = url->str; + len_url = url->len; + + *user = NULL; + i=*position; - /* Copy http://hostname:port/ from base_url to new_url */ - i = 0; - if (g_url_match_method (base_url, "http") || - g_url_match_method (base_url, "ftp")) { - while (base_url[i] != '\0' && base_url[i] != ':') - new_url[i] = base_url[i++]; - if (base_url[i] != '\0') - new_url[i] = base_url[i++]; - if (base_url[i] != '\0') - new_url[i] = base_url[i++]; - if (base_url[i] != '\0') - new_url[i] = base_url[i++]; - while (base_url[i] != '\0' && base_url[i] != '/') - new_url[i] = base_url[i++]; - } else { - while (base_url[i] != '\0' && base_url[i] != ':') - new_url[i] = base_url[i++]; - if (base_url[i] != '\0') - new_url[i] = base_url[i++]; - } - - if (relative_url[0] == '/') { - if (i + strlen (relative_url) >= size_new_url) - return FALSE; - strcpy (new_url + i, relative_url); - return g_url_add_slash (new_url, size_new_url); - } - - /* At this point, i points to the first slash following the hostname - (and port) in base_url. */ - - /* Now, figure how many ..'s to follow. */ - num_dotdot = 0; - j = 0; - while (relative_url[j] != '\0') { - if (relative_url[j] == '.' && - relative_url[j + 1] == '/') { - j += 2; - } else if (relative_url[j] == '.' && - relative_url[j + 1] == '.' && - relative_url[j + 2] == '/') { - j += 3; - num_dotdot++; - } else { - break; - } - } - - /* Find num_dotdot+1 slashes back from the end, point k there. */ - - for (k = strlen (base_url); k > i && num_dotdot >= 0; k--) - if (base_url[k - 1] == '/') - num_dotdot--; - - if (k + 1 + strlen (relative_url) - j >= size_new_url) - return FALSE; - - while (i < k) - new_url[i] = base_url[i++]; - if (relative_url[0] == '#') - while (base_url[i] != '\0') - new_url[i] = base_url[i++]; - else if (base_url[i] == '/' || base_url[i] == '\0') - new_url[i++] = '/'; - strcpy (new_url + i, relative_url + j); - return g_url_add_slash (new_url, size_new_url); -} - - + /* find a '@' */ + while ((i= hostname_size) return NULL; - memcpy (hostname, url + i, j - i); - hostname[j - i] = '\0'; - if (url[j] == ':') { - *port = atoi (url + j + 1); - for (j++; url[j] != '\0' && url[j] != '/'; j++); - } - return url + j; + } +static gboolean +find_passwd(GString *url, GString **passwd, guint *position, gboolean *error) +{ + guint i; + + gchar *str_url; + gint len_url; + gchar *str_passwd; + + str_url = url->str; + len_url = url->len; + + *passwd = NULL; + i=*position; + + /* find a '@' */ + while ((istr); + + /* Try to find the protocol */ + found = find_protocol(url, &protocol, &position, &error); + if (found) { + printf("protocol found : %s\n", protocol->str); + } else printf("protocol not found in URL\n\n"); + printf("posistion of the next item:\n"); + printf("%s\n", url->str); + for(i=0; istr); + } else printf("user name not found in URL\n"); + printf("posistion of the next item:\n"); + printf("%s\n", url->str); + for(i=0; istr); + printf("\n"); + } else printf("passwd not found in URL\n"); + printf("posistion of the next item:\n"); + printf("%s\n", url->str); + for(i=0; i