Artifact 1a6d7505c678b61216e02daab9bb25d2299881b0:
File
src/url.c
part of check-in
[3dcaed8d86]
- When accessing a remote repository, if there is a username/password specified as part of the URI, use these to login.
by
dan on
2007-07-28 07:09:25.
0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54 ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66 his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75 u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20 te it and/or.**
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20 modify it under
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65 the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76 lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65 ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73 s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20 tributed in the
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20 l be useful,.**
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20 but WITHOUT ANY
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75 WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69 t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54 * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52 Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55 A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20 RPOSE. See the
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50 GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20 d have received
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63 U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72 g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69 ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65 te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64 e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20 ation, Inc., 59
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53 Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73 uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31 ton, MA 02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20 307, USA..**.**
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69 Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20 nformation:.**
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68 * http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 66 contains code f
0380: 6f 72 20 70 61 72 73 69 6e 67 20 55 52 4c 73 20 or parsing URLs
0390: 74 68 61 74 20 61 70 70 65 61 72 20 6f 6e 20 74 that appear on t
03a0: 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 0a he command-line.
03b0: 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e */.#include "con
03c0: 66 69 67 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 fig.h".#include
03d0: 22 75 72 6c 2e 68 22 0a 0a 2f 2a 20 50 61 72 73 "url.h"../* Pars
03e0: 65 20 61 20 55 52 49 20 61 75 74 68 6f 72 69 74 e a URI authorit
03f0: 79 2e 20 54 68 65 20 70 61 72 73 65 64 20 73 79 y. The parsed sy
0400: 6e 74 61 78 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 ntax is:.**.**
0410: 20 20 20 5b 3c 75 73 65 72 6e 61 6d 65 3e 20 3a [<username> :
0420: 20 3c 70 61 73 73 77 6f 72 64 3e 20 40 5d 20 3c <password> @] <
0430: 68 6f 73 74 6e 61 6d 65 3e 20 5b 3a 20 3c 70 6f hostname> [: <po
0440: 72 74 3e 5d 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a rt>].**.** TODO:
0450: 20 49 66 20 74 68 65 20 69 6e 70 75 74 20 73 74 If the input st
0460: 72 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 6d 61 ring does not ma
0470: 74 63 68 20 74 68 69 73 20 70 61 74 74 65 72 6e tch this pattern
0480: 2c 20 72 65 73 75 6c 74 73 20 61 72 65 0a 2a 2a , results are.**
0490: 20 75 6e 64 65 66 69 6e 65 64 20 28 62 75 74 20 undefined (but
04a0: 73 68 6f 75 6c 64 20 6e 6f 74 20 63 72 61 73 68 should not crash
04b0: 20 6f 72 20 61 6e 79 74 68 69 6e 67 20 6e 61 73 or anything nas
04c0: 74 79 20 6c 69 6b 65 20 74 68 61 74 29 2e 0a 2a ty like that)..*
04d0: 2f 0a 76 6f 69 64 20 75 72 6c 5f 70 61 72 73 65 /.void url_parse
04e0: 5f 61 75 74 68 6f 72 69 74 79 28 63 68 61 72 20 _authority(char
04f0: 63 6f 6e 73 74 20 2a 7a 55 72 69 2c 20 69 6e 74 const *zUri, int
0500: 20 2a 70 49 64 78 29 7b 0a 20 20 63 68 61 72 20 *pIdx){. char
0510: 2a 7a 55 73 65 72 20 3d 20 30 3b 0a 20 20 63 68 *zUser = 0;. ch
0520: 61 72 20 2a 7a 50 61 73 73 20 3d 20 30 3b 0a 20 ar *zPass = 0;.
0530: 20 63 68 61 72 20 2a 7a 48 6f 73 74 20 3d 20 30 char *zHost = 0
0540: 3b 0a 20 20 69 6e 74 20 69 50 6f 72 74 20 3d 20 ;. int iPort =
0550: 38 30 3b 0a 0a 20 20 69 6e 74 20 69 46 69 72 73 80;.. int iFirs
0560: 74 20 3d 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 t = *pIdx;. int
0570: 20 69 43 6f 6c 6f 6e 20 3d 20 2d 31 3b 0a 20 20 iColon = -1;.
0580: 69 6e 74 20 69 69 3b 0a 0a 20 20 2f 2a 20 53 63 int ii;.. /* Sc
0590: 61 6e 20 66 6f 72 20 74 68 65 20 6d 61 67 69 63 an for the magic
05a0: 20 22 40 22 2e 20 49 66 20 74 68 65 20 61 75 74 "@". If the aut
05b0: 68 6f 72 69 74 79 20 63 6f 6e 74 61 69 6e 73 20 hority contains
05c0: 74 68 69 73 20 63 68 61 72 61 63 74 65 72 2c 0a this character,.
05d0: 20 20 2a 2a 20 74 68 65 6e 20 77 65 20 6e 65 65 ** then we nee
05e0: 64 20 74 6f 20 70 61 72 73 65 20 61 20 75 73 65 d to parse a use
05f0: 72 6e 61 6d 65 20 61 6e 64 20 70 61 73 73 77 6f rname and passwo
0600: 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 rd.. */. for(i
0610: 69 3d 69 46 69 72 73 74 3b 20 7a 55 72 69 5b 69 i=iFirst; zUri[i
0620: 69 5d 20 26 26 20 7a 55 72 69 5b 69 69 5d 21 3d i] && zUri[ii]!=
0630: 27 40 27 20 26 26 20 7a 55 72 69 5b 69 69 5d 21 '@' && zUri[ii]!
0640: 3d 20 27 2f 27 3b 20 69 69 2b 2b 29 7b 0a 20 20 = '/'; ii++){.
0650: 20 20 69 66 28 20 7a 55 72 69 5b 69 69 5d 3d 3d if( zUri[ii]==
0660: 27 3a 27 20 29 20 69 43 6f 6c 6f 6e 20 3d 20 69 ':' ) iColon = i
0670: 69 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 i;. }.. /* Par
0680: 73 65 20 74 68 65 20 75 73 65 72 6e 61 6d 65 20 se the username
0690: 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c 29 20 70 and (optional) p
06a0: 61 73 73 77 6f 72 64 2e 20 2a 2f 0a 20 20 69 66 assword. */. if
06b0: 28 20 7a 55 72 69 5b 69 69 5d 3d 3d 27 40 27 20 ( zUri[ii]=='@'
06c0: 29 7b 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 6f ){. if( iColo
06d0: 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 7a 55 n>=0 ){. zU
06e0: 73 65 72 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 ser = mprintf("%
06f0: 2e 2a 73 22 2c 20 69 43 6f 6c 6f 6e 2d 69 46 69 .*s", iColon-iFi
0700: 72 73 74 2c 20 26 7a 55 72 69 5b 69 46 69 72 73 rst, &zUri[iFirs
0710: 74 5d 29 3b 0a 20 20 20 20 20 20 7a 50 61 73 73 t]);. zPass
0720: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 2e 2a 73 = mprintf("%.*s
0730: 22 2c 20 69 69 2d 28 69 43 6f 6c 6f 6e 2b 31 29 ", ii-(iColon+1)
0740: 2c 20 26 7a 55 72 69 5b 69 43 6f 6c 6f 6e 2b 31 , &zUri[iColon+1
0750: 5d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ]);. }else{.
0760: 20 20 20 20 20 7a 55 73 65 72 20 3d 20 6d 70 72 zUser = mpr
0770: 69 6e 74 66 28 22 25 2e 2a 73 22 2c 20 69 69 2d intf("%.*s", ii-
0780: 69 46 69 72 73 74 2c 20 26 7a 55 72 69 5b 69 46 iFirst, &zUri[iF
0790: 69 72 73 74 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 irst]);. }.
07a0: 20 20 69 46 69 72 73 74 20 3d 20 69 69 2b 31 3b iFirst = ii+1;
07b0: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 61 72 73 65 . }.. /* Parse
07c0: 20 74 68 65 20 68 6f 73 74 6e 61 6d 65 2e 20 2a the hostname. *
07d0: 2f 0a 20 20 66 6f 72 28 69 69 3d 69 46 69 72 73 /. for(ii=iFirs
07e0: 74 3b 20 7a 55 72 69 5b 69 69 5d 20 26 26 20 7a t; zUri[ii] && z
07f0: 55 72 69 5b 69 69 5d 21 3d 27 3a 27 20 26 26 20 Uri[ii]!=':' &&
0800: 7a 55 72 69 5b 69 69 5d 21 3d 20 27 2f 27 3b 20 zUri[ii]!= '/';
0810: 69 69 2b 2b 29 3b 0a 20 20 7a 48 6f 73 74 20 3d ii++);. zHost =
0820: 20 6d 70 72 69 6e 74 66 28 22 25 2e 2a 73 22 2c mprintf("%.*s",
0830: 20 69 69 2d 69 46 69 72 73 74 2c 20 26 7a 55 72 ii-iFirst, &zUr
0840: 69 5b 69 46 69 72 73 74 5d 29 3b 0a 0a 20 20 2f i[iFirst]);.. /
0850: 2a 20 50 61 72 73 65 20 74 68 65 20 70 6f 72 74 * Parse the port
0860: 20 6e 75 6d 62 65 72 2c 20 69 66 20 6f 6e 65 20 number, if one
0870: 69 73 20 73 70 65 63 69 66 69 65 64 2e 20 2a 2f is specified. */
0880: 0a 20 20 69 66 28 20 7a 55 72 69 5b 69 69 5d 3d . if( zUri[ii]=
0890: 3d 27 3a 27 20 29 7b 0a 20 20 20 20 69 50 6f 72 =':' ){. iPor
08a0: 74 20 3d 20 61 74 6f 69 28 26 7a 55 72 69 5b 69 t = atoi(&zUri[i
08b0: 69 2b 31 5d 29 3b 0a 20 20 20 20 66 6f 72 28 69 i+1]);. for(i
08c0: 69 3d 69 46 69 72 73 74 3b 20 7a 55 72 69 5b 69 i=iFirst; zUri[i
08d0: 69 5d 20 26 26 20 7a 55 72 69 5b 69 69 5d 21 3d i] && zUri[ii]!=
08e0: 20 27 2f 27 3b 20 69 69 2b 2b 29 3b 0a 20 20 7d '/'; ii++);. }
08f0: 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 67 .. /* Set the g
0900: 2e 75 72 6c 58 58 58 20 76 61 72 69 61 62 6c 65 .urlXXX variable
0910: 73 20 74 6f 20 74 68 65 20 70 61 72 73 65 64 20 s to the parsed
0920: 76 61 6c 75 65 73 2e 20 2a 2f 0a 20 20 64 65 68 values. */. deh
0930: 74 74 70 69 7a 65 28 7a 55 73 65 72 29 3b 0a 20 ttpize(zUser);.
0940: 20 64 65 68 74 74 70 69 7a 65 28 7a 50 61 73 73 dehttpize(zPass
0950: 29 3b 0a 20 20 64 65 68 74 74 70 69 7a 65 28 7a );. dehttpize(z
0960: 48 6f 73 74 29 3b 0a 20 20 67 2e 75 72 6c 55 73 Host);. g.urlUs
0970: 65 72 6e 61 6d 65 20 3d 20 7a 55 73 65 72 3b 0a ername = zUser;.
0980: 20 20 67 2e 75 72 6c 50 61 73 73 77 6f 72 64 20 g.urlPassword
0990: 3d 20 7a 50 61 73 73 3b 0a 20 20 67 2e 75 72 6c = zPass;. g.url
09a0: 4e 61 6d 65 20 3d 20 7a 48 6f 73 74 3b 0a 20 20 Name = zHost;.
09b0: 67 2e 75 72 6c 50 6f 72 74 20 3d 20 69 50 6f 72 g.urlPort = iPor
09c0: 74 3b 0a 0a 20 20 2a 70 49 64 78 20 3d 20 69 69 t;.. *pIdx = ii
09d0: 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 61 73 65 64 ;.}../*.** Based
09e0: 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 73 20 61 on the values a
09f0: 6c 72 65 61 64 79 20 73 74 6f 72 65 64 20 69 6e lready stored in
0a00: 20 74 68 65 20 6f 74 68 65 72 20 67 2e 75 72 6c the other g.url
0a10: 58 58 58 20 76 61 72 69 61 62 6c 65 73 2c 0a 2a XXX variables,.*
0a20: 2a 20 73 65 74 20 74 68 65 20 67 2e 75 72 6c 43 * set the g.urlC
0a30: 61 6e 6f 6e 69 63 61 6c 20 76 61 72 69 61 62 6c anonical variabl
0a40: 65 2e 0a 2a 2f 0a 76 6f 69 64 20 75 72 6c 5f 73 e..*/.void url_s
0a50: 65 74 5f 63 61 6e 6f 6e 28 29 7b 0a 20 20 67 2e et_canon(){. g.
0a60: 75 72 6c 43 61 6e 6f 6e 69 63 61 6c 20 3d 20 6d urlCanonical = m
0a70: 70 72 69 6e 74 66 28 22 68 74 74 70 3a 2f 2f 25 printf("http://%
0a80: 54 25 73 25 54 25 73 25 54 3a 25 64 25 54 22 2c T%s%T%s%T:%d%T",
0a90: 20 0a 20 20 20 20 28 67 2e 75 72 6c 55 73 65 72 . (g.urlUser
0aa0: 6e 61 6d 65 20 3f 20 67 2e 75 72 6c 55 73 65 72 name ? g.urlUser
0ab0: 6e 61 6d 65 20 3a 20 22 22 29 2c 0a 20 20 20 20 name : ""),.
0ac0: 28 67 2e 75 72 6c 50 61 73 73 77 6f 72 64 20 3f (g.urlPassword ?
0ad0: 20 22 3a 22 20 3a 20 22 22 29 2c 0a 20 20 20 20 ":" : ""),.
0ae0: 28 67 2e 75 72 6c 50 61 73 73 77 6f 72 64 20 3f (g.urlPassword ?
0af0: 20 67 2e 75 72 6c 50 61 73 73 77 6f 72 64 20 3a g.urlPassword :
0b00: 20 22 22 29 2c 0a 20 20 20 20 28 67 2e 75 72 6c ""),. (g.url
0b10: 55 73 65 72 6e 61 6d 65 20 3f 20 22 40 22 20 3a Username ? "@" :
0b20: 20 22 22 29 2c 0a 20 20 20 20 67 2e 75 72 6c 4e ""),. g.urlN
0b30: 61 6d 65 2c 20 67 2e 75 72 6c 50 6f 72 74 2c 20 ame, g.urlPort,
0b40: 67 2e 75 72 6c 50 61 74 68 0a 20 20 29 3b 0a 20 g.urlPath. );.
0b50: 20 2f 2a 20 70 72 69 6e 74 66 28 22 25 73 5c 6e /* printf("%s\n
0b60: 22 2c 20 67 2e 75 72 6c 43 61 6e 6f 6e 69 63 61 ", g.urlCanonica
0b70: 6c 29 3b 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 l); */.}../*.**
0b80: 50 61 72 73 65 20 74 68 65 20 67 69 76 65 6e 20 Parse the given
0b90: 55 52 4c 2e 20 20 50 6f 70 75 6c 61 74 65 20 76 URL. Populate v
0ba0: 61 72 69 61 62 6c 65 73 20 69 6e 20 74 68 65 20 ariables in the
0bb0: 67 6c 6f 62 61 6c 20 22 67 22 20 73 74 72 75 63 global "g" struc
0bc0: 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ture..**.**
0bd0: 20 67 2e 75 72 6c 49 73 46 69 6c 65 20 20 20 20 g.urlIsFile
0be0: 20 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69 True if this i
0bf0: 73 20 61 20 66 69 6c 65 20 55 52 4c 0a 2a 2a 20 s a file URL.**
0c00: 20 20 20 20 20 67 2e 75 72 6c 4e 61 6d 65 20 20 g.urlName
0c10: 20 20 20 20 20 20 48 6f 73 74 6e 61 6d 65 20 66 Hostname f
0c20: 6f 72 20 48 54 54 50 3a 2e 20 20 46 69 6c 65 6e or HTTP:. Filen
0c30: 61 6d 65 20 66 6f 72 20 46 49 4c 45 3a 0a 2a 2a ame for FILE:.**
0c40: 20 20 20 20 20 20 67 2e 75 72 6c 50 6f 72 74 20 g.urlPort
0c50: 20 20 20 20 20 20 20 50 6f 72 74 20 6e 61 6d 65 Port name
0c60: 20 66 6f 72 20 48 54 54 50 2e 0a 2a 2a 20 20 20 for HTTP..**
0c70: 20 20 20 67 2e 75 72 6c 50 61 74 68 20 20 20 20 g.urlPath
0c80: 20 20 20 20 50 61 74 68 20 6e 61 6d 65 20 66 6f Path name fo
0c90: 72 20 48 54 54 50 2e 0a 2a 2a 20 20 20 20 20 20 r HTTP..**
0ca0: 67 2e 75 72 6c 43 61 6e 6f 6e 69 63 61 6c 20 20 g.urlCanonical
0cb0: 20 54 68 65 20 55 52 4c 20 69 6e 20 63 61 6e 6f The URL in cano
0cc0: 6e 69 63 61 6c 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a nical form.**.**
0cd0: 20 49 66 20 67 2e 75 72 69 49 73 46 69 6c 65 20 If g.uriIsFile
0ce0: 69 73 20 66 61 6c 73 65 2c 20 69 6e 64 69 63 61 is false, indica
0cf0: 74 69 6e 67 20 61 6e 20 68 74 74 70 20 55 52 49 ting an http URI
0d00: 2c 20 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f , then the follo
0d10: 77 69 6e 67 0a 2a 2a 20 76 61 72 69 61 62 6c 65 wing.** variable
0d20: 73 20 61 72 65 20 61 6c 73 6f 20 70 6f 70 75 6c s are also popul
0d30: 61 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 ated:.**.**
0d40: 20 67 2e 75 72 6c 55 73 65 72 6e 61 6d 65 0a 2a g.urlUsername.*
0d50: 2a 20 20 20 20 20 20 67 2e 75 72 6c 50 61 73 73 * g.urlPass
0d60: 77 6f 72 64 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 3a word.**.** TODO:
0d70: 20 41 74 20 70 72 65 73 65 6e 74 2c 20 74 68 65 At present, the
0d80: 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 73 70 65 only way to spe
0d90: 63 69 66 79 20 61 20 75 73 65 72 6e 61 6d 65 20 cify a username
0da0: 69 73 20 74 6f 20 70 61 73 73 20 69 74 0a 2a 2a is to pass it.**
0db0: 20 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 as part of the
0dc0: 55 52 49 2e 20 49 6e 20 74 68 65 20 66 75 74 75 URI. In the futu
0dd0: 72 65 2c 20 69 66 20 6e 6f 20 70 61 73 73 77 6f re, if no passwo
0de0: 72 64 20 69 73 20 73 70 65 63 69 66 69 65 64 2c rd is specified,
0df0: 20 0a 2a 2a 20 66 6f 73 73 69 6c 20 73 68 6f 75 .** fossil shou
0e00: 6c 64 20 75 73 65 20 74 68 65 20 67 65 74 5f 70 ld use the get_p
0e10: 61 73 73 70 68 72 61 73 65 28 29 20 72 6f 75 74 assphrase() rout
0e20: 69 6e 65 20 28 75 73 65 72 2e 63 29 20 74 6f 20 ine (user.c) to
0e30: 6f 62 74 61 69 6e 0a 2a 2a 20 61 20 70 61 73 73 obtain.** a pass
0e40: 77 6f 72 64 20 66 72 6f 6d 20 74 68 65 20 75 73 word from the us
0e50: 65 72 2e 0a 2a 2f 0a 76 6f 69 64 20 75 72 6c 5f er..*/.void url_
0e60: 70 61 72 73 65 28 63 6f 6e 73 74 20 63 68 61 72 parse(const char
0e70: 20 2a 7a 55 72 6c 29 7b 0a 20 20 69 6e 74 20 69 *zUrl){. int i
0e80: 2c 20 6a 2c 20 63 3b 0a 20 20 63 68 61 72 20 2a , j, c;. char *
0e90: 7a 46 69 6c 65 3b 0a 20 20 69 66 28 20 73 74 72 zFile;. if( str
0ea0: 6e 63 6d 70 28 7a 55 72 6c 2c 20 22 68 74 74 70 ncmp(zUrl, "http
0eb0: 3a 22 2c 20 35 29 3d 3d 30 20 29 7b 0a 20 20 20 :", 5)==0 ){.
0ec0: 20 67 2e 75 72 6c 49 73 46 69 6c 65 20 3d 20 30 g.urlIsFile = 0
0ed0: 3b 0a 0a 20 20 20 20 69 20 3d 20 37 3b 0a 20 20 ;.. i = 7;.
0ee0: 20 20 75 72 6c 5f 70 61 72 73 65 5f 61 75 74 68 url_parse_auth
0ef0: 6f 72 69 74 79 28 7a 55 72 6c 2c 20 26 69 29 3b ority(zUrl, &i);
0f00: 0a 20 20 20 20 67 2e 75 72 6c 50 61 74 68 20 3d . g.urlPath =
0f10: 20 6d 70 72 69 6e 74 66 28 26 7a 55 72 6c 5b 69 mprintf(&zUrl[i
0f20: 5d 29 3b 0a 20 20 20 20 64 65 68 74 74 70 69 7a ]);. dehttpiz
0f30: 65 28 67 2e 75 72 6c 50 61 74 68 29 3b 0a 20 20 e(g.urlPath);.
0f40: 20 20 75 72 6c 5f 73 65 74 5f 63 61 6e 6f 6e 28 url_set_canon(
0f50: 29 3b 0a 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 );.. }else if(
0f60: 73 74 72 6e 63 6d 70 28 7a 55 72 6c 2c 20 22 66 strncmp(zUrl, "f
0f70: 69 6c 65 3a 22 2c 20 35 29 3d 3d 30 20 29 7b 0a ile:", 5)==0 ){.
0f80: 20 20 20 20 67 2e 75 72 6c 49 73 46 69 6c 65 20 g.urlIsFile
0f90: 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 7a 55 72 = 1;. if( zUr
0fa0: 6c 5b 35 5d 3d 3d 27 2f 27 20 26 26 20 7a 55 72 l[5]=='/' && zUr
0fb0: 6c 5b 36 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 l[6]=='/' ){.
0fc0: 20 20 20 69 20 3d 20 37 3b 0a 20 20 20 20 7d 65 i = 7;. }e
0fd0: 6c 73 65 7b 0a 20 20 20 20 20 20 69 20 3d 20 35 lse{. i = 5
0fe0: 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 46 69 6c ;. }. zFil
0ff0: 65 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 73 22 e = mprintf("%s"
1000: 2c 20 26 7a 55 72 6c 5b 69 5d 29 3b 0a 20 20 7d , &zUrl[i]);. }
1010: 65 6c 73 65 20 69 66 28 20 66 69 6c 65 5f 69 73 else if( file_is
1020: 66 69 6c 65 28 7a 55 72 6c 29 20 29 7b 0a 20 20 file(zUrl) ){.
1030: 20 20 67 2e 75 72 6c 49 73 46 69 6c 65 20 3d 20 g.urlIsFile =
1040: 31 3b 0a 20 20 20 20 7a 46 69 6c 65 20 3d 20 6d 1;. zFile = m
1050: 70 72 69 6e 74 66 28 22 25 73 22 2c 20 7a 55 72 printf("%s", zUr
1060: 6c 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 l);. }else if(
1070: 66 69 6c 65 5f 69 73 64 69 72 28 7a 55 72 6c 29 file_isdir(zUrl)
1080: 3d 3d 31 20 29 7b 0a 20 20 20 20 7a 46 69 6c 65 ==1 ){. zFile
1090: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 73 2f 46 = mprintf("%s/F
10a0: 4f 53 53 49 4c 22 2c 20 7a 55 72 6c 29 3b 0a 20 OSSIL", zUrl);.
10b0: 20 20 20 69 66 28 20 66 69 6c 65 5f 69 73 66 69 if( file_isfi
10c0: 6c 65 28 7a 46 69 6c 65 29 20 29 7b 0a 20 20 20 le(zFile) ){.
10d0: 20 20 20 67 2e 75 72 6c 49 73 46 69 6c 65 20 3d g.urlIsFile =
10e0: 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 1;. }else{.
10f0: 20 20 20 20 20 66 72 65 65 28 7a 46 69 6c 65 29 free(zFile)
1100: 3b 0a 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 ;. fossil_p
1110: 61 6e 69 63 28 22 75 6e 6b 6e 6f 77 6e 20 72 65 anic("unknown re
1120: 70 6f 73 69 74 6f 72 79 3a 20 25 73 22 2c 20 7a pository: %s", z
1130: 55 72 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 Url);. }. }e
1140: 6c 73 65 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f lse{. fossil_
1150: 70 61 6e 69 63 28 22 75 6e 6b 6e 6f 77 6e 20 72 panic("unknown r
1160: 65 70 6f 73 69 74 6f 72 79 3a 20 25 73 22 2c 20 epository: %s",
1170: 7a 55 72 6c 29 3b 0a 20 20 7d 0a 20 20 69 66 28 zUrl);. }. if(
1180: 20 67 2e 75 72 6c 49 73 46 69 6c 65 20 29 7b 0a g.urlIsFile ){.
1190: 20 20 20 20 42 6c 6f 62 20 63 66 69 6c 65 3b 0a Blob cfile;.
11a0: 20 20 20 20 64 65 68 74 74 70 69 7a 65 28 7a 46 dehttpize(zF
11b0: 69 6c 65 29 3b 20 20 0a 20 20 20 20 66 69 6c 65 ile); . file
11c0: 5f 63 61 6e 6f 6e 69 63 61 6c 5f 6e 61 6d 65 28 _canonical_name(
11d0: 7a 46 69 6c 65 2c 20 26 63 66 69 6c 65 29 3b 0a zFile, &cfile);.
11e0: 20 20 20 20 66 72 65 65 28 7a 46 69 6c 65 29 3b free(zFile);
11f0: 0a 20 20 20 20 67 2e 75 72 6c 4e 61 6d 65 20 3d . g.urlName =
1200: 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20 26 mprintf("%b", &
1210: 63 66 69 6c 65 29 3b 0a 20 20 20 20 67 2e 75 72 cfile);. g.ur
1220: 6c 43 61 6e 6f 6e 69 63 61 6c 20 3d 20 6d 70 72 lCanonical = mpr
1230: 69 6e 74 66 28 22 66 69 6c 65 3a 2f 2f 25 54 22 intf("file://%T"
1240: 2c 20 67 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 , g.urlName);.
1250: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 66 blob_reset(&cf
1260: 69 6c 65 29 3b 0a 20 20 7d 0a 7d 0a ile);. }.}.