/* ** Copyright (c) 2007 D. Richard Hipp ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public ** License version 2 as published by the Free Software Foundation. ** ** 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 library; if not, write to the ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, ** Boston, MA 02111-1307, USA. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ******************************************************************************* ** ** This file contains code for parsing URLs that appear on the command-line */ #include "config.h" #include "url.h" /* ** Parse the given URL. Populate variables in the global "g" structure. ** ** g.urlIsFile True if this is a file URL ** g.urlName Hostname for HTTP:. Filename for FILE: ** g.urlPort Port name for HTTP. ** g.urlPath Path name for HTTP. ** g.urlUser Userid. ** g.urlPasswd Password. ** g.urlCanonical The URL in canonical form ** ** HTTP url format is: ** ** http://userid:password@host:port/path?query#fragment ** */ void url_parse(const char *zUrl){ int i, j, c; char *zFile = 0; if( strncmp(zUrl, "http://", 7)==0 || strncmp(zUrl, "https://", 8)==0 ){ int iStart; g.urlIsFile = 0; if( zUrl[4]=='s' ){ g.urlIsHttps = 1; g.urlProtocol = "https"; g.urlDfltPort = 443; iStart = 8; }else{ g.urlIsHttps = 0; g.urlProtocol = "http"; g.urlDfltPort = 80; iStart = 7; } for(i=iStart; (c=zUrl[i])!=0 && c!='/' && c!='@'; i++){} if( c=='@' ){ for(j=iStart; jurl); p->zBase = zBase; p->nParam = 0; } /* ** Add a fixed parameter to an HQuery. */ void url_add_parameter(HQuery *p, const char *zName, const char *zValue){ assert( p->nParam < count(p->azName) ); assert( p->nParam < count(p->azValue) ); p->azName[p->nParam] = zName; p->azValue[p->nParam] = zValue; p->nParam++; } /* ** Render the URL with a parameter override. */ char *url_render( HQuery *p, /* Base URL */ const char *zName1, /* First override */ const char *zValue1, /* First override value */ const char *zName2, /* Second override */ const char *zValue2 /* Second override value */ ){ const char *zSep = "?"; int i; blob_reset(&p->url); blob_appendf(&p->url, "%s/%s", g.zBaseURL, p->zBase); for(i=0; inParam; i++){ const char *z = p->azValue[i]; if( zName1 && strcmp(zName1,p->azName[i])==0 ){ zName1 = 0; z = zValue1; if( z==0 ) continue; } if( zName2 && strcmp(zName2,p->azName[i])==0 ){ zName2 = 0; z = zValue2; if( z==0 ) continue; } blob_appendf(&p->url, "%s%s=%T", zSep, p->azName[i], z); zSep = "&"; } if( zName1 && zValue1 ){ blob_appendf(&p->url, "%s%s=%T", zSep, zName1, zValue1); } if( zName2 && zValue2 ){ blob_appendf(&p->url, "%s%s=%T", zSep, zName2, zValue2); } return blob_str(&p->url); }