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. dbda8d6ce9 2007-07-21 drh: ** g.urlCanonical The URL in canonical form 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; dbda8d6ce9 2007-07-21 drh: char *zFile; dbda8d6ce9 2007-07-21 drh: if( strncmp(zUrl, "http:", 5)==0 ){ dbda8d6ce9 2007-07-21 drh: g.urlIsFile = 0; dbda8d6ce9 2007-07-21 drh: for(i=7; (c=zUrl[i])!=0 && c!=':' && c!='/'; i++){} dbda8d6ce9 2007-07-21 drh: g.urlName = mprintf("%.*s", i-7, &zUrl[7]); 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: } dbda8d6ce9 2007-07-21 drh: }