dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Copyright (c) 2007 D. Richard Hipp dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This program is free software; you can redistribute it and/or dbda8d6ce9 2007-07-21 drh: ** modify it under the terms of the GNU General Public dbda8d6ce9 2007-07-21 drh: ** License version 2 as published by the Free Software Foundation. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This program is distributed in the hope that it will be useful, dbda8d6ce9 2007-07-21 drh: ** but WITHOUT ANY WARRANTY; without even the implied warranty of dbda8d6ce9 2007-07-21 drh: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU dbda8d6ce9 2007-07-21 drh: ** General Public License for more details. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** You should have received a copy of the GNU General Public dbda8d6ce9 2007-07-21 drh: ** License along with this library; if not, write to the dbda8d6ce9 2007-07-21 drh: ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, dbda8d6ce9 2007-07-21 drh: ** Boston, MA 02111-1307, USA. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** Author contact information: dbda8d6ce9 2007-07-21 drh: ** drh@hwaci.com dbda8d6ce9 2007-07-21 drh: ** http://www.hwaci.com/drh/ dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ******************************************************************************* dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** This file contains code for parsing URLs that appear on the command-line dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: #include "config.h" dbda8d6ce9 2007-07-21 drh: #include "url.h" dbda8d6ce9 2007-07-21 drh: dbda8d6ce9 2007-07-21 drh: /* dbda8d6ce9 2007-07-21 drh: ** Parse the given URL. Populate variables in the global "g" structure. dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: ** g.urlIsFile True if this is a file URL dbda8d6ce9 2007-07-21 drh: ** g.urlName Hostname for HTTP:. Filename for FILE: dbda8d6ce9 2007-07-21 drh: ** g.urlPort Port name for HTTP. dbda8d6ce9 2007-07-21 drh: ** g.urlPath Path name for HTTP. e621b6dbe3 2007-07-30 drh: ** g.urlUser Userid. e621b6dbe3 2007-07-30 drh: ** g.urlPasswd Password. dbda8d6ce9 2007-07-21 drh: ** g.urlCanonical The URL in canonical form e621b6dbe3 2007-07-30 drh: ** e621b6dbe3 2007-07-30 drh: ** HTTP url format is: e621b6dbe3 2007-07-30 drh: ** e621b6dbe3 2007-07-30 drh: ** http://userid:password@host:port/path?query#fragment dbda8d6ce9 2007-07-21 drh: ** dbda8d6ce9 2007-07-21 drh: */ dbda8d6ce9 2007-07-21 drh: void url_parse(const char *zUrl){ dbda8d6ce9 2007-07-21 drh: int i, j, c; e621b6dbe3 2007-07-30 drh: char *zFile = 0; dbda8d6ce9 2007-07-21 drh: if( strncmp(zUrl, "http:", 5)==0 ){ dbda8d6ce9 2007-07-21 drh: g.urlIsFile = 0; e621b6dbe3 2007-07-30 drh: for(i=7; (c=zUrl[i])!=0 && c!='/' && c!='@'; i++){} e621b6dbe3 2007-07-30 drh: if( c=='@' ){ e621b6dbe3 2007-07-30 drh: for(j=7; j<i && zUrl[j]!=':'; j++){} e621b6dbe3 2007-07-30 drh: g.urlUser = mprintf("%.*s", j-7, &zUrl[7]); e621b6dbe3 2007-07-30 drh: if( j<i ){ e621b6dbe3 2007-07-30 drh: g.urlPasswd = mprintf("%.*s", i-j-1, &zUrl[j+1]); e621b6dbe3 2007-07-30 drh: } e621b6dbe3 2007-07-30 drh: for(j=i+1; (c=zUrl[j])!=0 && c!='/' && c!=':'; j++){} e621b6dbe3 2007-07-30 drh: g.urlName = mprintf("%.*s", j-i-1, &zUrl[i+1]); e621b6dbe3 2007-07-30 drh: i = j; e621b6dbe3 2007-07-30 drh: }else{ e621b6dbe3 2007-07-30 drh: for(i=7; (c=zUrl[i])!=0 && c!='/' && c!=':'; i++){} e621b6dbe3 2007-07-30 drh: g.urlName = mprintf("%.*s", i-7, &zUrl[7]); e621b6dbe3 2007-07-30 drh: } dbda8d6ce9 2007-07-21 drh: for(j=0; g.urlName[j]; j++){ g.urlName[j] = tolower(g.urlName[j]); } dbda8d6ce9 2007-07-21 drh: if( c==':' ){ dbda8d6ce9 2007-07-21 drh: g.urlPort = 0; dbda8d6ce9 2007-07-21 drh: i++; dbda8d6ce9 2007-07-21 drh: while( (c = zUrl[i])!=0 && isdigit(c) ){ dbda8d6ce9 2007-07-21 drh: g.urlPort = g.urlPort*10 + c - '0'; dbda8d6ce9 2007-07-21 drh: i++; dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: }else{ dbda8d6ce9 2007-07-21 drh: g.urlPort = 80; dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: g.urlPath = mprintf(&zUrl[i]); dbda8d6ce9 2007-07-21 drh: dehttpize(g.urlName); dbda8d6ce9 2007-07-21 drh: dehttpize(g.urlPath); dbda8d6ce9 2007-07-21 drh: g.urlCanonical = mprintf("http://%T:%d%T", g.urlName, g.urlPort, g.urlPath); dbda8d6ce9 2007-07-21 drh: }else if( strncmp(zUrl, "file:", 5)==0 ){ dbda8d6ce9 2007-07-21 drh: g.urlIsFile = 1; dbda8d6ce9 2007-07-21 drh: if( zUrl[5]=='/' && zUrl[6]=='/' ){ dbda8d6ce9 2007-07-21 drh: i = 7; dbda8d6ce9 2007-07-21 drh: }else{ dbda8d6ce9 2007-07-21 drh: i = 5; dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: zFile = mprintf("%s", &zUrl[i]); dbda8d6ce9 2007-07-21 drh: }else if( file_isfile(zUrl) ){ dbda8d6ce9 2007-07-21 drh: g.urlIsFile = 1; dbda8d6ce9 2007-07-21 drh: zFile = mprintf("%s", zUrl); dbda8d6ce9 2007-07-21 drh: }else if( file_isdir(zUrl)==1 ){ dbda8d6ce9 2007-07-21 drh: zFile = mprintf("%s/FOSSIL", zUrl); dbda8d6ce9 2007-07-21 drh: if( file_isfile(zFile) ){ dbda8d6ce9 2007-07-21 drh: g.urlIsFile = 1; dbda8d6ce9 2007-07-21 drh: }else{ dbda8d6ce9 2007-07-21 drh: free(zFile); dbda8d6ce9 2007-07-21 drh: fossil_panic("unknown repository: %s", zUrl); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: }else{ dbda8d6ce9 2007-07-21 drh: fossil_panic("unknown repository: %s", zUrl); dbda8d6ce9 2007-07-21 drh: } dbda8d6ce9 2007-07-21 drh: if( g.urlIsFile ){ dbda8d6ce9 2007-07-21 drh: Blob cfile; dbda8d6ce9 2007-07-21 drh: dehttpize(zFile); dbda8d6ce9 2007-07-21 drh: file_canonical_name(zFile, &cfile); dbda8d6ce9 2007-07-21 drh: free(zFile); dbda8d6ce9 2007-07-21 drh: g.urlName = mprintf("%b", &cfile); dbda8d6ce9 2007-07-21 drh: g.urlCanonical = mprintf("file://%T", g.urlName); dbda8d6ce9 2007-07-21 drh: blob_reset(&cfile); dbda8d6ce9 2007-07-21 drh: } e621b6dbe3 2007-07-30 drh: } e621b6dbe3 2007-07-30 drh: e621b6dbe3 2007-07-30 drh: /* e621b6dbe3 2007-07-30 drh: ** COMMAND: test-urlparser e621b6dbe3 2007-07-30 drh: */ e621b6dbe3 2007-07-30 drh: void cmd_test_urlparser(void){ e621b6dbe3 2007-07-30 drh: if( g.argc!=3 ){ e621b6dbe3 2007-07-30 drh: usage("URL"); e621b6dbe3 2007-07-30 drh: } e621b6dbe3 2007-07-30 drh: url_parse(g.argv[2]); e621b6dbe3 2007-07-30 drh: printf("g.urlIsFile = %d\n", g.urlIsFile); e621b6dbe3 2007-07-30 drh: printf("g.urlName = %s\n", g.urlName); e621b6dbe3 2007-07-30 drh: printf("g.urlPort = %d\n", g.urlPort); e621b6dbe3 2007-07-30 drh: printf("g.urlPath = %s\n", g.urlPath); e621b6dbe3 2007-07-30 drh: printf("g.urlUser = %s\n", g.urlUser); e621b6dbe3 2007-07-30 drh: printf("g.urlPasswd = %s\n", g.urlPasswd); e621b6dbe3 2007-07-30 drh: printf("g.urlCanonical = %s\n", g.urlCanonical); dbda8d6ce9 2007-07-21 drh: }