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 74 contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 o implement the
0390: 66 69 6c 65 20 74 72 61 6e 73 66 65 72 20 70 72 file transfer pr
03a0: 6f 74 6f 63 6f 6c 2e 0a 2a 2f 0a 23 69 6e 63 6c otocol..*/.#incl
03b0: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 ude "config.h".#
03c0: 69 6e 63 6c 75 64 65 20 22 78 66 65 72 2e 68 22 include "xfer.h"
03d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 54 6f 6b ../*.** The aTok
03e0: 65 6e 5b 30 2e 2e 6e 54 6f 6b 65 6e 2d 31 5d 20 en[0..nToken-1]
03f0: 62 6c 6f 62 20 61 72 72 61 79 20 69 73 20 61 20 blob array is a
0400: 70 61 72 73 65 20 6f 66 20 61 20 22 66 69 6c 65 parse of a "file
0410: 22 20 6c 69 6e 65 20 0a 2a 2a 20 6d 65 73 73 61 " line .** messa
0420: 67 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e ge. This routin
0430: 65 20 66 69 6e 69 73 68 65 73 20 70 61 72 73 69 e finishes parsi
0440: 6e 67 20 74 68 61 74 20 6d 65 73 73 61 67 65 20 ng that message
0450: 61 6e 64 20 64 6f 65 73 0a 2a 2a 20 61 20 72 65 and does.** a re
0460: 63 6f 72 64 20 69 6e 73 65 72 74 20 6f 66 20 74 cord insert of t
0470: 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 he file..**.** T
0480: 68 65 20 66 69 6c 65 20 6c 69 6e 65 20 69 73 20 he file line is
0490: 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f in one of the fo
04a0: 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 6f 72 6d llowing two form
04b0: 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66 69 s:.**.** fi
04c0: 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 le UUID SIZE \n
04d0: 43 4f 4e 54 45 4e 54 0a 2a 2a 20 20 20 20 20 20 CONTENT.**
04e0: 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53 file UUID DELTAS
04f0: 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 RC SIZE \n CONTE
0500: 4e 54 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e NT.**.** The con
0510: 74 65 6e 74 20 69 73 20 53 49 5a 45 20 62 79 74 tent is SIZE byt
0520: 65 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 66 es immediately f
0530: 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 6e 65 77 ollowing the new
0540: 6c 69 6e 65 2e 0a 2a 2a 20 49 66 20 44 45 4c 54 line..** If DELT
0550: 41 53 52 43 20 65 78 69 73 74 73 2c 20 74 68 65 ASRC exists, the
0560: 6e 20 74 68 65 20 43 4f 4e 54 45 4e 54 20 69 73 n the CONTENT is
0570: 20 61 20 64 65 6c 74 61 20 61 67 61 69 6e 73 74 a delta against
0580: 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 the.** content
0590: 6f 66 20 44 45 4c 54 41 53 52 43 2e 0a 2a 2a 0a of DELTASRC..**.
05a0: 2a 2a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 20 ** If any error
05b0: 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61 20 occurs, write a
05c0: 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 70 45 72 message into pEr
05d0: 72 20 77 68 69 63 68 20 68 61 73 20 61 6c 72 65 r which has alre
05e0: 61 64 79 0a 2a 2a 20 62 65 20 69 6e 69 74 69 61 ady.** be initia
05f0: 6c 69 7a 65 64 20 74 6f 20 61 6e 20 65 6d 70 74 lized to an empt
0600: 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 73 74 61 y string..*/.sta
0610: 74 69 63 20 76 6f 69 64 20 78 66 65 72 5f 61 63 tic void xfer_ac
0620: 63 65 70 74 5f 66 69 6c 65 28 42 6c 6f 62 20 2a cept_file(Blob *
0630: 70 49 6e 2c 20 42 6c 6f 62 20 2a 61 54 6f 6b 65 pIn, Blob *aToke
0640: 6e 2c 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 42 n, int nToken, B
0650: 6c 6f 62 20 2a 70 45 72 72 29 7b 0a 20 20 69 6e lob *pErr){. in
0660: 74 20 6e 3b 0a 20 20 69 6e 74 20 72 69 64 3b 0a t n;. int rid;.
0670: 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c 20 Blob content,
0680: 68 61 73 68 3b 0a 20 20 0a 20 20 69 66 28 20 6e hash;. . if( n
0690: 54 6f 6b 65 6e 3c 33 20 7c 7c 20 6e 54 6f 6b 65 Token<3 || nToke
06a0: 6e 3e 34 20 7c 7c 20 21 62 6c 6f 62 5f 69 73 5f n>4 || !blob_is_
06b0: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 uuid(&aToken[1])
06c0: 0a 20 20 20 20 20 20 20 7c 7c 20 21 62 6c 6f 62 . || !blob
06d0: 5f 69 73 5f 69 6e 74 28 26 61 54 6f 6b 65 6e 5b _is_int(&aToken[
06e0: 6e 54 6f 6b 65 6e 2d 31 5d 2c 20 26 6e 29 20 7c nToken-1], &n) |
06f0: 7c 20 6e 3c 3d 30 0a 20 20 20 20 20 20 20 7c 7c | n<=0. ||
0700: 20 28 6e 54 6f 6b 65 6e 3d 3d 34 20 26 26 20 21 (nToken==4 && !
0710: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
0720: 6f 6b 65 6e 5b 32 5d 29 29 20 29 7b 0a 20 20 20 oken[2])) ){.
0730: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0740: 72 72 2c 20 22 6d 61 6c 66 6f 72 6d 65 64 20 66 rr, "malformed f
0750: 69 6c 65 20 6c 69 6e 65 22 29 3b 0a 20 20 20 20 ile line");.
0760: 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 62 6c return;. }. bl
0770: 6f 62 5f 7a 65 72 6f 28 26 63 6f 6e 74 65 6e 74 ob_zero(&content
0780: 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 );. blob_zero(&
0790: 68 61 73 68 29 3b 0a 20 20 62 6c 6f 62 5f 65 78 hash);. blob_ex
07a0: 74 72 61 63 74 28 70 49 6e 2c 20 6e 2c 20 26 63 tract(pIn, n, &c
07b0: 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 6e ontent);. if( n
07c0: 54 6f 6b 65 6e 3d 3d 34 20 29 7b 0a 20 20 20 20 Token==4 ){.
07d0: 42 6c 6f 62 20 73 72 63 3b 0a 20 20 20 20 69 6e Blob src;. in
07e0: 74 20 73 72 63 69 64 20 3d 20 64 62 5f 69 6e 74 t srcid = db_int
07f0: 28 30 2c 20 22 53 45 4c 45 43 54 20 72 69 64 20 (0, "SELECT rid
0800: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 FROM blob WHERE
0810: 75 75 69 64 3d 25 42 22 2c 20 26 61 54 6f 6b 65 uuid=%B", &aToke
0820: 6e 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28 20 73 n[2]);. if( s
0830: 72 63 69 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 rcid==0 ){.
0840: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0850: 72 72 2c 20 22 75 6e 6b 6e 6f 77 6e 20 64 65 6c rr, "unknown del
0860: 74 61 20 73 6f 75 72 63 65 3a 20 25 62 22 2c 20 ta source: %b",
0870: 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20 &aToken[2]);.
0880: 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d return;. }
0890: 0a 20 20 20 20 63 6f 6e 74 65 6e 74 5f 67 65 74 . content_get
08a0: 28 73 72 63 69 64 2c 20 26 73 72 63 29 3b 0a 20 (srcid, &src);.
08b0: 20 20 20 62 6c 6f 62 5f 64 65 6c 74 61 5f 61 70 blob_delta_ap
08c0: 70 6c 79 28 26 73 72 63 2c 20 26 63 6f 6e 74 65 ply(&src, &conte
08d0: 6e 74 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 nt, &content);.
08e0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 blob_reset(&s
08f0: 72 63 29 3b 0a 20 20 7d 0a 20 20 73 68 61 31 73 rc);. }. sha1s
0900: 75 6d 5f 62 6c 6f 62 28 26 63 6f 6e 74 65 6e 74 um_blob(&content
0910: 2c 20 26 68 61 73 68 29 3b 0a 20 20 69 66 28 20 , &hash);. if(
0920: 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61 54 !blob_eq_str(&aT
0930: 6f 6b 65 6e 5b 31 5d 2c 20 62 6c 6f 62 5f 73 74 oken[1], blob_st
0940: 72 28 26 68 61 73 68 29 2c 20 2d 31 29 20 29 7b r(&hash), -1) ){
0950: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 . blob_append
0960: 66 28 70 45 72 72 2c 20 22 63 6f 6e 74 65 6e 74 f(pErr, "content
0970: 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 does not match
0980: 73 68 61 31 20 68 61 73 68 22 29 3b 0a 20 20 7d sha1 hash");. }
0990: 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 . blob_reset(&h
09a0: 61 73 68 29 3b 0a 20 20 72 69 64 20 3d 20 63 6f ash);. rid = co
09b0: 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f 6e 74 65 ntent_put(&conte
09c0: 6e 74 2c 20 30 29 3b 0a 20 20 6d 61 6e 69 66 65 nt, 0);. manife
09d0: 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28 72 69 64 st_crosslink(rid
09e0: 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 20 69 , &content);. i
09f0: 66 28 20 72 69 64 3d 3d 30 20 29 7b 0a 20 20 20 f( rid==0 ){.
0a00: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 45 blob_appendf(pE
0a10: 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a 45 72 72 rr, "%s", g.zErr
0a20: 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a Msg);. }.}../*.
0a30: 2a 2a 20 53 65 6e 64 20 74 68 65 20 66 69 6c 65 ** Send the file
0a40: 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 72 identified by r
0a50: 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4f 75 id..**.** If pOu
0a60: 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 t is not NULL, t
0a70: 68 65 6e 20 61 70 70 65 6e 64 20 74 68 65 20 74 hen append the t
0a80: 65 78 74 20 6f 66 20 74 68 65 20 73 65 6e 64 20 ext of the send
0a90: 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f 20 70 4f message.** to pO
0aa0: 75 74 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 20 ut. Otherwise,
0ab0: 61 70 70 65 6e 64 20 74 68 65 20 74 65 78 74 20 append the text
0ac0: 74 6f 20 74 68 65 20 43 47 49 20 6f 75 74 70 75 to the CGI outpu
0ad0: 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 t..*/.static int
0ae0: 20 73 65 6e 64 5f 66 69 6c 65 28 69 6e 74 20 72 send_file(int r
0af0: 69 64 2c 20 42 6c 6f 62 20 2a 70 4f 75 74 29 7b id, Blob *pOut){
0b00: 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65 6e 74 2c . Blob content,
0b10: 20 75 75 69 64 3b 0a 20 20 69 6e 74 20 73 69 7a uuid;. int siz
0b20: 65 3b 0a 0a 23 69 66 20 30 0a 53 45 4c 45 43 54 e;..#if 0.SELECT
0b30: 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c 74 srcid FROM delt
0b40: 61 0a 20 57 48 45 52 45 20 72 69 64 3d 25 64 0a a. WHERE rid=%d.
0b50: 20 20 20 41 4e 44 20 45 58 49 53 54 53 28 53 45 AND EXISTS(SE
0b60: 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 65 LECT 1 FROM onre
0b70: 6d 6f 74 65 20 57 48 45 52 45 20 72 69 64 3d 73 mote WHERE rid=s
0b80: 72 63 69 64 29 0a 55 4e 49 4f 4e 20 41 4c 4c 0a rcid).UNION ALL.
0b90: 53 45 4c 45 43 54 20 69 64 20 46 52 4f 4d 20 64 SELECT id FROM d
0ba0: 65 6c 74 61 0a 20 57 48 45 52 45 20 73 72 63 69 elta. WHERE srci
0bb0: 64 3d 25 64 0a 20 20 20 41 4e 44 20 45 58 49 53 d=%d. AND EXIS
0bc0: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d TS(SELECT 1 FROM
0bd0: 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45 52 45 20 onremote WHERE
0be0: 72 69 64 3d 64 65 6c 74 61 2e 72 69 64 29 0a 4c rid=delta.rid).L
0bf0: 49 4d 49 54 20 31 0a 23 65 6e 64 69 66 0a 0a 20 IMIT 1.#endif..
0c00: 20 2f 2a 20 54 4f 44 4f 3a 0a 20 20 2a 2a 20 43 /* TODO:. ** C
0c10: 68 65 63 6b 20 66 6f 72 20 72 65 6c 61 74 65 64 heck for related
0c20: 20 66 69 6c 65 73 20 69 6e 20 74 68 65 20 6f 6e files in the on
0c30: 72 65 6d 6f 74 65 20 54 45 4d 50 20 74 61 62 6c remote TEMP tabl
0c40: 65 2e 20 20 49 66 20 72 65 6c 61 74 65 64 0a 20 e. If related.
0c50: 20 2a 2a 20 66 69 6c 65 73 20 61 72 65 20 66 6f ** files are fo
0c60: 75 6e 64 2c 20 74 68 65 6e 20 73 65 6e 64 20 61 und, then send a
0c70: 20 64 65 6c 74 61 20 72 61 74 68 65 72 20 74 68 delta rather th
0c80: 61 6e 20 74 68 65 20 77 68 6f 6c 65 20 66 69 6c an the whole fil
0c90: 65 2e 0a 20 20 2a 2f 0a 0a 20 20 62 6c 6f 62 5f e.. */.. blob_
0ca0: 7a 65 72 6f 28 26 75 75 69 64 29 3b 0a 20 20 64 zero(&uuid);. d
0cb0: 62 5f 62 6c 6f 62 28 26 75 75 69 64 2c 20 22 53 b_blob(&uuid, "S
0cc0: 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 ELECT uuid FROM
0cd0: 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25 blob WHERE rid=%
0ce0: 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 d AND size>=0",
0cf0: 72 69 64 29 3b 0a 20 20 69 66 28 20 62 6c 6f 62 rid);. if( blob
0d00: 5f 73 69 7a 65 28 26 75 75 69 64 29 3d 3d 30 20 _size(&uuid)==0
0d10: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
0d20: 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e 74 5f 67 . }. content_g
0d30: 65 74 28 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 et(rid, &content
0d40: 29 3b 0a 20 20 73 69 7a 65 20 3d 20 62 6c 6f 62 );. size = blob
0d50: 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e 74 29 3b _size(&content);
0d60: 0a 20 20 69 66 28 20 70 4f 75 74 20 29 7b 0a 20 . if( pOut ){.
0d70: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 blob_appendf(
0d80: 70 4f 75 74 2c 20 22 66 69 6c 65 20 25 62 20 25 pOut, "file %b %
0d90: 64 5c 6e 22 2c 20 26 75 75 69 64 2c 20 73 69 7a d\n", &uuid, siz
0da0: 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 e);. blob_app
0db0: 65 6e 64 28 70 4f 75 74 2c 20 62 6c 6f 62 5f 62 end(pOut, blob_b
0dc0: 75 66 66 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c uffer(&content),
0dd0: 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b size);. }else{
0de0: 0a 20 20 20 20 63 67 69 5f 70 72 69 6e 74 66 28 . cgi_printf(
0df0: 22 66 69 6c 65 20 25 62 20 25 64 5c 6e 22 2c 20 "file %b %d\n",
0e00: 26 75 75 69 64 2c 20 73 69 7a 65 29 3b 0a 20 20 &uuid, size);.
0e10: 20 20 63 67 69 5f 61 70 70 65 6e 64 5f 63 6f 6e cgi_append_con
0e20: 74 65 6e 74 28 62 6c 6f 62 5f 62 75 66 66 65 72 tent(blob_buffer
0e30: 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73 69 7a 65 (&content), size
0e40: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 );. }. blob_re
0e50: 73 65 74 28 26 63 6f 6e 74 65 6e 74 29 3b 0a 20 set(&content);.
0e60: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 75 75 69 blob_reset(&uui
0e70: 64 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 d);. db_multi_e
0e80: 78 65 63 28 22 49 4e 53 45 52 54 20 4f 52 20 49 xec("INSERT OR I
0e90: 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d GNORE INTO onrem
0ea0: 6f 74 65 20 56 41 4c 55 45 53 28 25 64 29 22 2c ote VALUES(%d)",
0eb0: 20 72 69 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 rid);. return
0ec0: 73 69 7a 65 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 size;.}.../*.**
0ed0: 53 65 6e 64 20 61 6c 6c 20 70 65 6e 64 69 6e 67 Send all pending
0ee0: 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 files..*/.stati
0ef0: 63 20 69 6e 74 20 73 65 6e 64 5f 61 6c 6c 5f 70 c int send_all_p
0f00: 65 6e 64 69 6e 67 28 42 6c 6f 62 20 2a 70 4f 75 ending(Blob *pOu
0f10: 74 29 7b 0a 20 20 69 6e 74 20 69 52 69 64 53 65 t){. int iRidSe
0f20: 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 73 65 nt = 0;. int se
0f30: 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 53 nt = 0;. int nS
0f40: 65 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6d ent = 0;. int m
0f50: 61 78 53 69 7a 65 20 3d 20 64 62 5f 67 65 74 5f axSize = db_get_
0f60: 69 6e 74 28 22 68 74 74 70 2d 6d 73 67 2d 73 69 int("http-msg-si
0f70: 7a 65 22 2c 20 35 30 30 30 30 30 29 3b 0a 20 20 ze", 500000);.
0f80: 53 74 6d 74 20 71 3b 0a 23 69 66 20 30 0a 20 20 Stmt q;.#if 0.
0f90: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 db_multi_exec(.
0fa0: 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 "CREATE TEMP
0fb0: 54 41 42 4c 45 20 70 72 69 6f 72 69 74 79 28 72 TABLE priority(r
0fc0: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 id INTEGER PRIMA
0fd0: 52 59 20 4b 45 59 29 3b 22 0a 20 20 20 20 22 49 RY KEY);". "I
0fe0: 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6f 72 NSERT INTO prior
0ff0: 69 74 79 22 0a 20 20 20 20 22 20 53 45 4c 45 43 ity". " SELEC
1000: 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c T srcid FROM del
1010: 74 61 22 0a 20 20 20 20 22 20 20 57 48 45 52 45 ta". " WHERE
1020: 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 EXISTS(SELECT 1
1030: 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 FROM onremote W
1040: 48 45 52 45 20 6f 6e 72 65 6d 6f 74 65 2e 72 69 HERE onremote.ri
1050: 64 3d 64 65 6c 74 61 2e 72 69 64 29 22 0a 20 20 d=delta.rid)".
1060: 20 20 22 20 20 20 20 41 4e 44 20 45 58 49 53 54 " AND EXIST
1070: 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 S(SELECT 1 FROM
1080: 70 65 6e 64 69 6e 67 20 57 48 45 52 45 20 64 65 pending WHERE de
1090: 6c 74 61 2e 73 72 63 69 64 3d 70 65 6e 64 69 6e lta.srcid=pendin
10a0: 67 2e 72 69 64 29 3b 22 0a 20 20 20 20 22 49 4e g.rid);". "IN
10b0: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 SERT OR IGNORE I
10c0: 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 20 NTO priority".
10d0: 20 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 46 " SELECT rid F
10e0: 52 4f 4d 20 64 65 6c 74 61 22 0a 20 20 20 20 22 ROM delta". "
10f0: 20 20 57 48 45 52 45 20 45 58 49 53 54 53 28 53 WHERE EXISTS(S
1100: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 ELECT 1 FROM onr
1110: 65 6d 6f 74 65 20 57 48 45 52 45 20 6f 6e 72 65 emote WHERE onre
1120: 6d 6f 74 65 2e 72 69 64 3d 64 65 6c 74 61 2e 73 mote.rid=delta.s
1130: 72 63 69 64 29 22 0a 20 20 20 20 22 20 20 20 20 rcid)". "
1140: 41 4e 44 20 45 58 49 53 54 53 28 53 45 4c 45 43 AND EXISTS(SELEC
1150: 54 20 31 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 T 1 FROM pending
1160: 20 57 48 45 52 45 20 64 65 6c 74 61 2e 72 69 64 WHERE delta.rid
1170: 3d 70 65 6e 64 69 6e 67 2e 72 69 64 29 3b 22 0a =pending.rid);".
1180: 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 73 65 );. while( se
1190: 6e 74 3c 6d 61 78 53 69 7a 65 20 26 26 20 28 72 nt<maxSize && (r
11a0: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 id = db_int(0, "
11b0: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 SELECT rid FROM
11c0: 70 72 69 6f 72 69 74 79 22 29 29 21 3d 30 20 29 priority"))!=0 )
11d0: 7b 0a 20 20 20 20 73 65 6e 74 20 2b 3d 20 73 65 {. sent += se
11e0: 6e 64 5f 66 69 6c 65 28 72 69 64 2c 20 70 4f 75 nd_file(rid, pOu
11f0: 74 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 t);. db_multi
1200: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 22 49 4e _exec(. "IN
1210: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 SERT OR IGNORE I
1220: 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 20 NTO priority".
1230: 20 20 20 20 22 20 53 45 4c 45 43 54 20 73 72 63 " SELECT src
1240: 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 id FROM delta WH
1250: 45 52 45 20 72 69 64 3d 25 64 22 0a 20 20 20 20 ERE rid=%d".
1260: 20 20 22 20 55 4e 49 4f 4e 20 41 4c 4c 22 0a 20 " UNION ALL".
1270: 20 20 20 20 20 22 20 53 45 4c 45 43 54 20 72 69 " SELECT ri
1280: 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 d FROM delta WHE
1290: 52 45 20 73 72 63 69 64 3d 25 64 22 2c 0a 20 20 RE srcid=%d",.
12a0: 20 20 20 20 72 69 64 2c 20 72 69 64 0a 20 20 20 rid, rid.
12b0: 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 );. }.#endif.
12c0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 db_prepare(&q,
12d0: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d "SELECT rid FROM
12e0: 20 70 65 6e 64 69 6e 67 20 4f 52 44 45 52 20 42 pending ORDER B
12f0: 59 20 72 69 64 22 29 3b 0a 20 20 77 68 69 6c 65 Y rid");. while
1300: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 ( db_step(&q)==S
1310: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 QLITE_ROW ){.
1320: 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 6f int rid = db_co
1330: 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b lumn_int(&q, 0);
1340: 0a 20 20 20 20 69 66 28 20 73 65 6e 74 3c 6d 61 . if( sent<ma
1350: 78 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 73 xSize ){. s
1360: 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66 69 6c 65 ent += send_file
1370: 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a 20 20 20 (rid, pOut);.
1380: 20 20 20 6e 53 65 6e 74 2b 2b 3b 0a 20 20 20 20 nSent++;.
1390: 20 20 69 52 69 64 53 65 6e 74 20 3d 20 72 69 64 iRidSent = rid
13a0: 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ;. }else{.
13b0: 20 20 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d char *zUuid =
13c0: 20 64 62 5f 74 65 78 74 28 30 2c 0a 20 20 20 20 db_text(0,.
13d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
13e0: 20 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 "SELECT uuid F
13f0: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 ROM blob WHERE r
1400: 69 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d id=%d AND size>=
1410: 30 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 0", rid);.
1420: 69 66 28 20 7a 55 75 69 64 20 29 7b 0a 20 20 20 if( zUuid ){.
1430: 20 20 20 20 20 69 66 28 20 70 4f 75 74 20 29 7b if( pOut ){
1440: 0a 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f . blob_
1450: 61 70 70 65 6e 64 66 28 70 4f 75 74 2c 20 22 69 appendf(pOut, "i
1460: 67 6f 74 20 25 73 5c 6e 22 2c 20 7a 55 75 69 64 got %s\n", zUuid
1470: 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 );. }else
1480: 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 5f {. cgi_
1490: 70 72 69 6e 74 66 28 22 69 67 6f 74 20 25 73 5c printf("igot %s\
14a0: 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20 n", zUuid);.
14b0: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 72 }. fr
14c0: 65 65 28 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 ee(zUuid);.
14d0: 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 }. }. }. d
14e0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a b_finalize(&q);.
14f0: 20 20 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 . /* Delete t
1500: 68 65 20 27 70 65 6e 64 69 6e 67 27 20 72 65 63 he 'pending' rec
1510: 6f 72 64 73 20 66 6f 72 20 61 6c 6c 20 66 69 6c ords for all fil
1520: 65 73 20 6a 75 73 74 20 73 65 6e 74 2e 20 4f 74 es just sent. Ot
1530: 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 77 65 herwise,. ** we
1540: 20 63 61 6e 20 77 69 6e 64 20 75 70 20 73 65 6e can wind up sen
1550: 64 69 6e 67 20 73 6f 6d 65 20 66 69 6c 65 73 20 ding some files
1560: 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2e 0a more than once..
1570: 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 65 6e 74 */. if( nSent
1580: 3e 30 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 6c >0 ){. db_mul
1590: 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 20 ti_exec("DELETE
15a0: 46 52 4f 4d 20 70 65 6e 64 69 6e 67 20 57 48 45 FROM pending WHE
15b0: 52 45 20 72 69 64 20 3c 3d 20 25 64 22 2c 20 69 RE rid <= %d", i
15c0: 52 69 64 53 65 6e 74 29 3b 0a 20 20 7d 0a 0a 23 RidSent);. }..#
15d0: 69 66 20 30 0a 20 20 64 62 5f 6d 75 6c 74 69 5f if 0. db_multi_
15e0: 65 78 65 63 28 22 44 52 4f 50 20 54 41 42 4c 45 exec("DROP TABLE
15f0: 20 70 72 69 6f 72 69 74 79 22 29 3b 0a 23 65 6e priority");.#en
1600: 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 53 65 dif. return nSe
1610: 6e 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 nt;.}.../*.** Ch
1620: 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74 75 72 eck the signatur
1630: 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63 61 74 e on an applicat
1640: 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70 61 79 ion/x-fossil pay
1650: 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20 62 79 load received by
1660: 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73 65 72 .** the HTTP ser
1670: 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e 61 74 ver. The signat
1680: 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20 6f 66 ure is a line of
1690: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 the following f
16a0: 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 orm:.**.**
16b0: 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20 4e 4f login LOGIN NO
16c0: 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a 2a 2a NCE SIGNATURE.**
16d0: 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20 69 73 .** The NONCE is
16e0: 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e 67 a random string
16f0: 2e 20 20 54 68 65 20 73 65 72 76 65 72 20 77 69 . The server wi
1700: 6c 6c 20 6e 65 76 65 72 20 61 63 63 65 70 74 20 ll never accept
1710: 61 0a 2a 2a 20 72 65 70 65 61 74 20 4e 4f 4e 43 a.** repeat NONC
1720: 45 2e 20 20 53 49 47 4e 41 54 55 52 45 20 69 73 E. SIGNATURE is
1730: 20 74 68 65 20 53 48 41 31 20 63 68 65 63 6b 73 the SHA1 checks
1740: 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45 20 um of the NONCE
1750: 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 .** concatenated
1760: 20 77 69 74 68 20 74 68 65 20 75 73 65 72 73 20 with the users
1770: 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 password..**.**
1780: 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 74 The parameters t
1790: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 o this routine a
17a0: 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20 62 6c re ephermeral bl
17b0: 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a obs holding the.
17c0: 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45 20 ** LOGIN, NONCE
17d0: 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e 0a 2a and SIGNATURE..*
17e0: 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e *.** This routin
17f0: 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f e attempts to lo
1800: 63 61 74 65 20 74 68 65 20 75 73 65 72 20 61 6e cate the user an
1810: 64 20 76 65 72 69 66 79 20 74 68 65 20 73 69 67 d verify the sig
1820: 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65 76 nature..** If ev
1830: 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b 73 20 erything checks
1840: 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e 43 41 out, the USER.CA
1850: 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68 65 P column for the
1860: 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20 69 USER table.** i
1870: 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20 73 s consulted to s
1880: 65 74 20 70 72 69 76 69 6c 65 67 65 73 20 69 6e et privileges in
1890: 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20 76 61 the global g va
18a0: 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 riable..**.** If
18b0: 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c 73 20 anything fails
18c0: 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e 6f to check out, no
18d0: 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 changes are mad
18e0: 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65 73 2e e to privileges.
18f0: 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72 65 .**.** Signature
1900: 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20 74 generation on t
1910: 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 20 69 he client side i
1920: 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 s handled by the
1930: 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68 61 6e .** http_exchan
1940: 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f ge() routine..*/
1950: 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67 69 .void check_logi
1960: 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c 20 n(Blob *pLogin,
1970: 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42 6c Blob *pNonce, Bl
1980: 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53 74 6d ob *pSig){. Stm
1990: 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a t q;. int rc;..
19a0: 20 20 69 66 28 20 64 62 5f 65 78 69 73 74 73 28 if( db_exists(
19b0: 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 72 "SELECT 1 FROM r
19c0: 63 76 66 72 6f 6d 20 57 48 45 52 45 20 6e 6f 6e cvfrom WHERE non
19d0: 63 65 3d 25 42 22 2c 20 70 4e 6f 6e 63 65 29 20 ce=%B", pNonce)
19e0: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 ){. return;
19f0: 2f 2a 20 4e 65 76 65 72 20 61 63 63 65 70 74 20 /* Never accept
1a00: 61 20 72 65 70 65 61 74 65 64 20 6e 6f 6e 63 65 a repeated nonce
1a10: 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 70 72 65 */. }. db_pre
1a20: 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 pare(&q, "SELECT
1a30: 20 70 77 2c 20 63 61 70 2c 20 75 69 64 20 46 52 pw, cap, uid FR
1a40: 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c 6f OM user WHERE lo
1a50: 67 69 6e 3d 25 42 22 2c 20 70 4c 6f 67 69 6e 29 gin=%B", pLogin)
1a60: 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 28 ;. if( db_step(
1a70: 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 &q)==SQLITE_ROW
1a80: 29 7b 0a 20 20 20 20 42 6c 6f 62 20 70 77 2c 20 ){. Blob pw,
1a90: 63 6f 6d 62 69 6e 65 64 2c 20 68 61 73 68 3b 0a combined, hash;.
1aa0: 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 blob_zero(&p
1ab0: 77 29 3b 0a 20 20 20 20 64 62 5f 65 70 68 65 6d w);. db_ephem
1ac0: 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c eral_blob(&q, 0,
1ad0: 20 26 70 77 29 3b 0a 20 20 20 20 62 6c 6f 62 5f &pw);. blob_
1ae0: 7a 65 72 6f 28 26 63 6f 6d 62 69 6e 65 64 29 3b zero(&combined);
1af0: 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26 . blob_copy(&
1b00: 63 6f 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e 63 65 combined, pNonce
1b10: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 );. blob_appe
1b20: 6e 64 28 26 63 6f 6d 62 69 6e 65 64 2c 20 62 6c nd(&combined, bl
1b30: 6f 62 5f 62 75 66 66 65 72 28 26 70 77 29 2c 20 ob_buffer(&pw),
1b40: 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29 29 3b blob_size(&pw));
1b50: 0a 20 20 20 20 2f 2a 20 43 47 49 44 45 42 55 47 . /* CGIDEBUG
1b60: 28 28 22 70 72 65 73 69 67 3d 5b 25 73 5d 5c 6e (("presig=[%s]\n
1b70: 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26 63 6f 6d ", blob_str(&com
1b80: 62 69 6e 65 64 29 29 29 3b 20 2a 2f 0a 20 20 20 bined))); */.
1b90: 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 26 63 sha1sum_blob(&c
1ba0: 6f 6d 62 69 6e 65 64 2c 20 26 68 61 73 68 29 3b ombined, &hash);
1bb0: 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 62 5f 63 . rc = blob_c
1bc0: 6f 6d 70 61 72 65 28 26 68 61 73 68 2c 20 70 53 ompare(&hash, pS
1bd0: 69 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 ig);. blob_re
1be0: 73 65 74 28 26 68 61 73 68 29 3b 0a 20 20 20 20 set(&hash);.
1bf0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f 6d 62 blob_reset(&comb
1c00: 69 6e 65 64 29 3b 0a 20 20 20 20 69 66 28 20 72 ined);. if( r
1c10: 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f c==0 ){. co
1c20: 6e 73 74 20 63 68 61 72 20 2a 7a 43 61 70 3b 0a nst char *zCap;.
1c30: 20 20 20 20 20 20 7a 43 61 70 20 3d 20 64 62 5f zCap = db_
1c40: 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c 20 column_text(&q,
1c50: 31 29 3b 0a 20 20 20 20 20 20 6c 6f 67 69 6e 5f 1);. login_
1c60: 73 65 74 5f 63 61 70 61 62 69 6c 69 74 69 65 73 set_capabilities
1c70: 28 7a 43 61 70 29 3b 0a 20 20 20 20 20 20 67 2e (zCap);. g.
1c80: 75 73 65 72 55 69 64 20 3d 20 64 62 5f 63 6f 6c userUid = db_col
1c90: 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29 3b 0a umn_int(&q, 2);.
1ca0: 20 20 20 20 20 20 67 2e 7a 4c 6f 67 69 6e 20 3d g.zLogin =
1cb0: 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20 70 mprintf("%b", p
1cc0: 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 20 20 67 2e Login);. g.
1cd0: 7a 4e 6f 6e 63 65 20 3d 20 6d 70 72 69 6e 74 66 zNonce = mprintf
1ce0: 28 22 25 62 22 2c 20 70 4e 6f 6e 63 65 29 3b 0a ("%b", pNonce);.
1cf0: 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 72 }. }. db_r
1d00: 65 73 65 74 28 26 71 29 3b 0a 7d 0a 0a 0a 2f 2a eset(&q);.}.../*
1d10: 0a 2a 2a 20 49 66 20 74 68 69 73 20 76 61 72 69 .** If this vari
1d20: 61 62 6c 65 20 69 73 20 73 65 74 2c 20 64 69 73 able is set, dis
1d30: 61 62 6c 65 20 6c 6f 67 69 6e 20 63 68 65 63 6b able login check
1d40: 73 2e 20 20 55 73 65 64 20 66 6f 72 20 64 65 62 s. Used for deb
1d50: 75 67 67 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a ugging.** only..
1d60: 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 69 */.static int di
1d70: 73 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 30 3b 0a sableLogin = 0;.
1d80: 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 45 3a 20 ./*.** WEBPAGE:
1d90: 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 xfer.**.** This
1da0: 69 73 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 is the transfer
1db0: 68 61 6e 64 6c 65 72 20 6f 6e 20 74 68 65 20 73 handler on the s
1dc0: 65 72 76 65 72 20 73 69 64 65 2e 20 20 54 68 65 erver side. The
1dd0: 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 6d 65 73 transfer.** mes
1de0: 73 61 67 65 20 68 61 73 20 62 65 65 6e 20 75 6e sage has been un
1df0: 63 6f 6d 70 72 65 73 73 65 64 20 61 6e 64 20 70 compressed and p
1e00: 6c 61 63 65 64 20 69 6e 20 74 68 65 20 67 2e 63 laced in the g.c
1e10: 67 69 49 6e 20 62 6c 6f 62 2e 0a 2a 2a 20 50 72 giIn blob..** Pr
1e20: 6f 63 65 73 73 20 74 68 69 73 20 6d 65 73 73 61 ocess this messa
1e30: 67 65 20 61 6e 64 20 66 6f 72 6d 20 61 6e 20 61 ge and form an a
1e40: 70 70 72 6f 70 72 69 61 74 65 20 72 65 70 6c 79 ppropriate reply
1e50: 2e 0a 2a 2f 0a 76 6f 69 64 20 70 61 67 65 5f 78 ..*/.void page_x
1e60: 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 fer(void){. int
1e70: 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 69 nToken;. int i
1e80: 73 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 sPull = 0;. int
1e90: 20 69 73 50 75 73 68 20 3d 20 30 3b 0a 20 20 69 isPush = 0;. i
1ea0: 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20 20 42 nt nErr = 0;. B
1eb0: 6c 6f 62 20 6c 69 6e 65 2c 20 65 72 72 6d 73 67 lob line, errmsg
1ec0: 2c 20 61 54 6f 6b 65 6e 5b 35 5d 3b 0a 0a 20 20 , aToken[5];..
1ed0: 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73 61 63 db_begin_transac
1ee0: 74 69 6f 6e 28 29 3b 0a 20 20 62 6c 6f 62 61 72 tion();. blobar
1ef0: 72 61 79 5f 7a 65 72 6f 28 61 54 6f 6b 65 6e 2c ray_zero(aToken,
1f00: 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b count(aToken));
1f10: 0a 20 20 63 67 69 5f 73 65 74 5f 63 6f 6e 74 65 . cgi_set_conte
1f20: 6e 74 5f 74 79 70 65 28 67 2e 7a 43 6f 6e 74 65 nt_type(g.zConte
1f30: 6e 74 54 79 70 65 29 3b 0a 20 20 62 6c 6f 62 5f ntType);. blob_
1f40: 7a 65 72 6f 28 26 65 72 72 6d 73 67 29 3b 0a 20 zero(&errmsg);.
1f50: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
1f60: 20 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d "CREATE TEM
1f70: 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 P TABLE onremote
1f80: 28 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 (rid INTEGER PRI
1f90: 4d 41 52 59 20 4b 45 59 29 3b 22 20 2f 2a 20 43 MARY KEY);" /* C
1fa0: 6c 69 65 6e 74 20 68 61 73 20 2a 2f 0a 20 20 20 lient has */.
1fb0: 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 "CREATE TEMP T
1fc0: 41 42 4c 45 20 70 65 6e 64 69 6e 67 28 72 69 64 ABLE pending(rid
1fd0: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
1fe0: 20 4b 45 59 29 3b 22 20 20 2f 2a 20 43 6c 69 65 KEY);" /* Clie
1ff0: 6e 74 20 6e 65 65 64 73 20 2a 2f 0a 20 20 29 3b nt needs */. );
2000: 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c . while( blob_l
2010: 69 6e 65 28 26 67 2e 63 67 69 49 6e 2c 20 26 6c ine(&g.cgiIn, &l
2020: 69 6e 65 29 20 29 7b 0a 20 20 20 20 6e 54 6f 6b ine) ){. nTok
2030: 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 en = blob_tokeni
2040: 7a 65 28 26 6c 69 6e 65 2c 20 61 54 6f 6b 65 6e ze(&line, aToken
2050: 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 , count(aToken))
2060: 3b 0a 0a 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 ;.. /* file
2070: 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f UUID SIZE \n CO
2080: 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 20 20 20 66 NTENT. ** f
2090: 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53 52 ile UUID DELTASR
20a0: 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e C SIZE \n CONTEN
20b0: 54 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 T. **. **
20c0: 41 63 63 65 70 74 20 61 20 66 69 6c 65 20 66 72 Accept a file fr
20d0: 6f 6d 20 74 68 65 20 63 6c 69 65 6e 74 2e 0a 20 om the client..
20e0: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c */. if( bl
20f0: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d ob_eq(&aToken[0]
2100: 2c 20 22 66 69 6c 65 22 29 20 26 26 20 6e 54 6f , "file") && nTo
2110: 6b 65 6e 3e 3d 32 20 26 26 20 6e 54 6f 6b 65 6e ken>=2 && nToken
2120: 3c 3d 33 20 29 7b 0a 20 20 20 20 20 20 69 66 28 <=3 ){. if(
2130: 20 21 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 !isPush ){.
2140: 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f cgi_reset_co
2150: 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 ntent();.
2160: 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 @ error not\sau
2170: 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 77 72 thorized\sto\swr
2180: 69 74 65 0a 20 20 20 20 20 20 20 20 6e 45 72 72 ite. nErr
2190: 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 ++;. brea
21a0: 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
21b0: 20 78 66 65 72 5f 61 63 63 65 70 74 5f 66 69 6c xfer_accept_fil
21c0: 65 28 26 67 2e 63 67 69 49 6e 2c 20 61 54 6f 6b e(&g.cgiIn, aTok
21d0: 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 26 65 72 72 en, nToken, &err
21e0: 6d 73 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 msg);. if(
21f0: 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 72 6d 73 blob_size(&errms
2200: 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67 g) ){. cg
2210: 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 i_reset_content(
2220: 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65 72 72 );. @ err
2230: 6f 72 20 25 54 28 62 6c 6f 62 5f 73 74 72 28 26 or %T(blob_str(&
2240: 65 72 72 6d 73 67 29 29 0a 20 20 20 20 20 20 20 errmsg)).
2250: 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 nErr++;.
2260: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
2270: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f }else.. /
2280: 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 * gimme UUID.
2290: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 **. ** Cli
22a0: 65 6e 74 20 69 73 20 72 65 71 75 65 73 74 69 6e ent is requestin
22b0: 67 20 61 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a g a file. */.
22c0: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 if( blob_eq(
22d0: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d &aToken[0], "gim
22e0: 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d me") && nToken==
22f0: 32 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 2 && blob_is_uui
2300: 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b d(&aToken[1]) ){
2310: 0a 20 20 20 20 20 20 69 66 28 20 69 73 50 75 6c . if( isPul
2320: 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62 5f l ){. db_
2330: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 multi_exec(.
2340: 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 "INSERT OR
2350: 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e IGNORE INTO pen
2360: 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20 ding(rid) ".
2370: 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 72 69 "SELECT ri
2380: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 d FROM blob WHER
2390: 45 20 75 75 69 64 3d 25 42 20 41 4e 44 20 73 69 E uuid=%B AND si
23a0: 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b 65 6e 5b ze>=0", &aToken[
23b0: 31 5d 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20 1]. );.
23c0: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a }. }else.
23d0: 0a 20 20 20 20 2f 2a 20 20 20 69 67 6f 74 20 55 . /* igot U
23e0: 55 49 44 0a 20 20 20 20 2a 2a 20 20 20 6c 65 61 UID. ** lea
23f0: 66 20 55 55 49 44 0a 20 20 20 20 2a 2a 0a 20 20 f UUID. **.
2400: 20 20 2a 2a 20 43 6c 69 65 6e 74 20 61 6e 6e 6f ** Client anno
2410: 75 6e 63 65 73 20 74 68 61 74 20 69 74 20 68 61 unces that it ha
2420: 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 s a particular f
2430: 69 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 ile. */. i
2440: 66 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 f( nToken==2.
2450: 20 20 20 20 20 20 20 26 26 20 28 62 6c 6f 62 5f && (blob_
2460: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 eq(&aToken[0], "
2470: 69 67 6f 74 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 igot") || blob_e
2480: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 6c 65 q(&aToken[0],"le
2490: 61 66 22 29 29 0a 20 20 20 20 20 20 20 20 20 20 af")).
24a0: 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 && blob_is_uuid(
24b0: 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 &aToken[1]) ){.
24c0: 20 20 20 20 20 69 66 28 20 69 73 50 75 6c 6c 20 if( isPull
24d0: 7c 7c 20 69 73 50 75 73 68 20 29 7b 0a 20 20 20 || isPush ){.
24e0: 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d 20 64 int rid = d
24f0: 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 b_int(0, "SELECT
2500: 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 rid FROM blob W
2510: 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c 20 26 HERE uuid=%B", &
2520: 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 aToken[1]);.
2530: 20 20 20 20 69 66 28 20 72 69 64 3e 30 20 29 7b if( rid>0 ){
2540: 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 6d 75 . db_mu
2550: 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 lti_exec(.
2560: 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 "INSERT OR
2570: 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e 72 IGNORE INTO onr
2580: 65 6d 6f 74 65 28 72 69 64 29 20 56 41 4c 55 45 emote(rid) VALUE
2590: 53 28 25 64 29 22 2c 20 72 69 64 0a 20 20 20 20 S(%d)", rid.
25a0: 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
25b0: 20 20 20 69 66 28 20 69 73 50 75 6c 6c 20 26 26 if( isPull &&
25c0: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e blob_eq(&aToken
25d0: 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 29 7b 0a [0], "leaf") ){.
25e0: 20 20 20 20 20 20 20 20 20 20 20 20 64 62 5f 6d db_m
25f0: 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 ulti_exec(.
2600: 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 "INSERT
2610: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 OR IGNORE INTO
2620: 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 pending(rid) ".
2630: 20 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45 "SE
2640: 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20 70 6c LECT cid FROM pl
2650: 69 6e 6b 20 57 48 45 52 45 20 70 69 64 3d 25 64 ink WHERE pid=%d
2660: 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20 20 20 ", rid.
2670: 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 );.
2680: 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 }. }else
2690: 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a 20 20 if( isPush ){.
26a0: 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e 74 5f content_
26b0: 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73 74 72 28 put(0, blob_str(
26c0: 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20 &aToken[1]));.
26d0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a }. }.
26e0: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f }else.. /
26f0: 2a 20 20 20 20 70 75 6c 6c 20 20 53 45 52 56 45 * pull SERVE
2700: 52 43 4f 44 45 20 20 50 52 4f 4a 45 43 54 43 4f RCODE PROJECTCO
2710: 44 45 0a 20 20 20 20 2a 2a 20 20 20 20 70 75 73 DE. ** pus
2720: 68 20 20 53 45 52 56 45 52 43 4f 44 45 20 20 50 h SERVERCODE P
2730: 52 4f 4a 45 43 54 43 4f 44 45 0a 20 20 20 20 2a ROJECTCODE. *
2740: 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6c 69 *. ** The cli
2750: 65 6e 74 20 77 61 6e 74 73 20 65 69 74 68 65 72 ent wants either
2760: 20 73 65 6e 64 20 6f 72 20 72 65 63 65 69 76 65 send or receive
2770: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 . */. if(
2780: 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20 20 20 nToken==3.
2790: 20 20 20 20 20 20 20 20 20 26 26 20 28 62 6c 6f && (blo
27a0: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c b_eq(&aToken[0],
27b0: 20 22 70 75 6c 6c 22 29 20 7c 7c 20 62 6c 6f 62 "pull") || blob
27c0: 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 _eq(&aToken[0],
27d0: 22 70 75 73 68 22 29 29 0a 20 20 20 20 20 20 20 "push")).
27e0: 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f && blob_
27f0: 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b is_uuid(&aToken[
2800: 31 5d 29 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 1]) && blob_is_u
2810: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32 5d 29 20 uid(&aToken[2])
2820: 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 ){. const c
2830: 68 61 72 20 2a 7a 53 43 6f 64 65 3b 0a 20 20 20 har *zSCode;.
2840: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
2850: 50 43 6f 64 65 3b 0a 0a 20 20 20 20 20 20 7a 53 PCode;.. zS
2860: 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22 73 Code = db_get("s
2870: 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 30 29 3b erver-code", 0);
2880: 0a 20 20 20 20 20 20 69 66 28 20 7a 53 43 6f 64 . if( zSCod
2890: 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
28a0: 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 fossil_panic("mi
28b0: 73 73 69 6e 67 20 73 65 72 76 65 72 20 63 6f 64 ssing server cod
28c0: 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 e");. }.
28d0: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 if( blob_eq_s
28e0: 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 7a tr(&aToken[1], z
28f0: 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 SCode, -1) ){.
2900: 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f cgi_reset_
2910: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 content();.
2920: 20 20 20 40 20 65 72 72 6f 72 20 73 65 72 76 65 @ error serve
2930: 72 5c 73 6c 6f 6f 70 0a 20 20 20 20 20 20 20 20 r\sloop.
2940: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nErr++;.
2950: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
2960: 20 20 20 20 20 7a 50 43 6f 64 65 20 3d 20 64 62 zPCode = db
2970: 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f _get("project-co
2980: 64 65 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 de", 0);. i
2990: 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20 29 7b 0a f( zPCode==0 ){.
29a0: 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 fossil_p
29b0: 61 6e 69 63 28 22 6d 69 73 73 69 6e 67 20 70 72 anic("missing pr
29c0: 6f 6a 65 63 74 20 63 6f 64 65 22 29 3b 0a 20 20 oject code");.
29d0: 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 }. if(
29e0: 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61 54 !blob_eq_str(&aT
29f0: 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f 64 65 2c oken[2], zPCode,
2a00: 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 -1) ){.
2a10: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e cgi_reset_conten
2a20: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65 t();. @ e
2a30: 72 72 6f 72 20 77 72 6f 6e 67 5c 73 70 72 6f 6a rror wrong\sproj
2a40: 65 63 74 0a 20 20 20 20 20 20 20 20 6e 45 72 72 ect. nErr
2a50: 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 ++;. brea
2a60: 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 k;. }.
2a70: 20 6c 6f 67 69 6e 5f 63 68 65 63 6b 5f 63 72 65 login_check_cre
2a80: 64 65 6e 74 69 61 6c 73 28 29 3b 0a 20 20 20 20 dentials();.
2a90: 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 if( blob_eq(&a
2aa0: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c 22 Token[0], "pull"
2ab0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 ) ){. if(
2ac0: 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 !g.okRead ){.
2ad0: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 cgi_rese
2ae0: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 t_content();.
2af0: 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e @ error n
2b00: 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 ot\sauthorized\s
2b10: 74 6f 5c 73 72 65 61 64 0a 20 20 20 20 20 20 20 to\sread.
2b20: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 nErr++;.
2b30: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
2b40: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 73 }. is
2b50: 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20 20 Pull = 1;.
2b60: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 }else{. i
2b70: 66 28 20 21 67 2e 6f 6b 57 72 69 74 65 20 29 7b f( !g.okWrite ){
2b80: 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 5f 72 . cgi_r
2b90: 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a eset_content();.
2ba0: 20 20 20 20 20 20 20 20 20 20 40 20 65 72 72 6f @ erro
2bb0: 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 r not\sauthorize
2bc0: 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a 20 20 20 d\sto\swrite.
2bd0: 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 nErr++;.
2be0: 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a break;.
2bf0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
2c00: 20 20 69 73 50 75 73 68 20 3d 20 31 3b 0a 0a 20 isPush = 1;..
2c10: 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 }. }else
2c20: 0a 0a 20 20 20 20 2f 2a 20 20 20 20 63 6c 6f 6e .. /* clon
2c30: 65 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 e. **. **
2c40: 54 68 65 20 63 6c 69 65 6e 74 20 6b 6e 6f 77 73 The client knows
2c50: 20 6e 6f 74 68 69 6e 67 2e 20 20 54 65 6c 6c 20 nothing. Tell
2c60: 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 all.. */.
2c70: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f if( blob_eq(&aTo
2c80: 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f 6e 65 22 29 ken[0], "clone")
2c90: 20 29 7b 0a 20 20 20 20 20 20 6c 6f 67 69 6e 5f ){. login_
2ca0: 63 68 65 63 6b 5f 63 72 65 64 65 6e 74 69 61 6c check_credential
2cb0: 73 28 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 s();. if( !
2cc0: 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 20 g.okRead ){.
2cd0: 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f cgi_reset_co
2ce0: 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 ntent();.
2cf0: 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 @ error not\sau
2d00: 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 72 65 thorized\sto\sre
2d10: 61 64 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b ad. nErr+
2d20: 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b +;. break
2d30: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 ;. }.
2d40: 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 isPull = 1;.
2d50: 20 20 40 20 70 75 73 68 20 25 73 28 64 62 5f 67 @ push %s(db_g
2d60: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 et("server-code"
2d70: 2c 20 22 78 22 29 29 20 25 73 28 64 62 5f 67 65 , "x")) %s(db_ge
2d80: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 t("project-code"
2d90: 2c 20 22 78 22 29 29 0a 20 20 20 20 20 20 64 62 , "x")). db
2da0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 _multi_exec(.
2db0: 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 "INSERT OR
2dc0: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64 IGNORE INTO pend
2dd0: 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20 20 ing(rid) ".
2de0: 20 20 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 "SELECT rid F
2df0: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 73 ROM blob WHERE s
2e00: 69 7a 65 3e 3d 30 22 0a 20 20 20 20 20 20 29 3b ize>=0". );
2e10: 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 . }else..
2e20: 2f 2a 20 20 20 20 6c 6f 67 69 6e 20 20 55 53 45 /* login USE
2e30: 52 20 20 4e 4f 4e 43 45 20 20 53 49 47 4e 41 54 R NONCE SIGNAT
2e40: 55 52 45 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a URE. **. *
2e50: 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20 76 61 * Check for a va
2e60: 6c 69 64 20 6c 6f 67 69 6e 2e 20 20 54 68 69 73 lid login. This
2e70: 20 68 61 73 20 74 6f 20 68 61 70 70 65 6e 20 62 has to happen b
2e80: 65 66 6f 72 65 20 61 6e 79 74 68 69 6e 67 20 65 efore anything e
2e90: 6c 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 lse.. */.
2ea0: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f if( blob_eq(&aTo
2eb0: 6b 65 6e 5b 30 5d 2c 20 22 6c 6f 67 69 6e 22 29 ken[0], "login")
2ec0: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b && nToken==4 ){
2ed0: 0a 20 20 20 20 20 20 69 66 28 20 64 69 73 61 62 . if( disab
2ee0: 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20 20 20 20 20 leLogin ){.
2ef0: 20 20 20 67 2e 6f 6b 52 65 61 64 20 3d 20 67 2e g.okRead = g.
2f00: 6f 6b 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 20 okWrite = 1;.
2f10: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 }else{.
2f20: 20 20 63 68 65 63 6b 5f 6c 6f 67 69 6e 28 26 61 check_login(&a
2f30: 54 6f 6b 65 6e 5b 31 5d 2c 20 26 61 54 6f 6b 65 Token[1], &aToke
2f40: 6e 5b 32 5d 2c 20 26 61 54 6f 6b 65 6e 5b 33 5d n[2], &aToken[3]
2f50: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
2f60: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 55 6e 6b else.. /* Unk
2f70: 6e 6f 77 6e 20 6d 65 73 73 61 67 65 0a 20 20 20 nown message.
2f80: 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 */. {.
2f90: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e cgi_reset_conten
2fa0: 74 28 29 3b 0a 20 20 20 20 20 20 40 20 65 72 72 t();. @ err
2fb0: 6f 72 20 62 61 64 5c 73 63 6f 6d 6d 61 6e 64 3a or bad\scommand:
2fc0: 5c 73 25 46 28 62 6c 6f 62 5f 73 74 72 28 26 6c \s%F(blob_str(&l
2fd0: 69 6e 65 29 29 0a 20 20 20 20 7d 0a 20 20 20 20 ine)). }.
2fe0: 62 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74 28 blobarray_reset(
2ff0: 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b aToken, nToken);
3000: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 69 . }.. /* The i
3010: 6e 70 75 74 20 6d 65 73 73 61 67 65 20 68 61 73 nput message has
3020: 20 6e 6f 77 20 62 65 65 6e 20 70 72 6f 63 65 73 now been proces
3030: 73 65 64 2e 20 20 47 65 6e 65 72 61 74 65 20 61 sed. Generate a
3040: 20 72 65 70 6c 79 2e 20 2a 2f 0a 20 20 69 66 28 reply. */. if(
3050: 20 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 53 isPush ){. S
3060: 74 6d 74 20 71 3b 0a 20 20 20 20 64 62 5f 70 72 tmt q;. db_pr
3070: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 epare(&q, "SELEC
3080: 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 T uuid FROM blob
3090: 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22 29 3b WHERE size<0");
30a0: 0a 20 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73 . while( db_s
30b0: 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f tep(&q)==SQLITE_
30c0: 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e ROW ){. con
30d0: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d st char *zUuid =
30e0: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 db_column_text(
30f0: 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 40 20 &q, 0);. @
3100: 67 69 6d 6d 65 20 25 73 28 7a 55 75 69 64 29 0a gimme %s(zUuid).
3110: 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e }. db_fin
3120: 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d 0a 20 alize(&q);. }.
3130: 20 69 66 28 20 69 73 50 75 6c 6c 20 29 7b 0a 20 if( isPull ){.
3140: 20 20 20 73 65 6e 64 5f 61 6c 6c 5f 70 65 6e 64 send_all_pend
3150: 69 6e 67 28 30 29 3b 0a 20 20 7d 0a 20 20 64 62 ing(0);. }. db
3160: 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e _end_transaction
3170: 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f (0);.}../*.** CO
3180: 4d 4d 41 4e 44 3a 20 74 65 73 74 2d 78 66 65 72 MMAND: test-xfer
3190: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6d 6d .**.** This comm
31a0: 61 6e 64 20 69 73 20 75 73 65 64 20 66 6f 72 20 and is used for
31b0: 64 65 62 75 67 67 69 6e 67 20 74 68 65 20 73 65 debugging the se
31c0: 72 76 65 72 2e 20 20 54 68 65 72 65 20 69 73 20 rver. There is
31d0: 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75 a single.** argu
31e0: 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 74 68 ment which is th
31f0: 65 20 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 63 e uncompressed c
3200: 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 22 78 66 ontent of an "xf
3210: 65 72 22 20 6d 65 73 73 61 67 65 0a 2a 2a 20 66 er" message.** f
3220: 72 6f 6d 20 63 6c 69 65 6e 74 20 74 6f 20 73 65 rom client to se
3230: 72 76 65 72 2e 20 20 54 68 69 73 20 63 6f 6d 6d rver. This comm
3240: 61 6e 64 20 69 6e 74 65 72 70 72 65 74 73 20 74 and interprets t
3250: 68 61 74 20 6d 65 73 73 61 67 65 20 61 73 0a 2a hat message as.*
3260: 2a 20 69 66 20 68 61 64 20 62 65 65 6e 20 72 65 * if had been re
3270: 63 65 69 76 65 64 20 62 79 20 74 68 65 20 73 65 ceived by the se
3280: 72 76 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74 rver..**.** On t
3290: 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 2c 20 he client side,
32a0: 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 run:.**.**
32b0: 66 6f 73 73 69 6c 20 70 75 73 68 20 68 74 74 70 fossil push http
32c0: 3a 2f 2f 62 6f 67 75 73 2f 20 2d 2d 68 74 74 70 ://bogus/ --http
32d0: 74 72 61 63 65 0a 2a 2a 0a 2a 2a 20 4f 72 20 61 trace.**.** Or a
32e0: 20 73 69 6d 69 6c 61 72 20 63 6f 6d 6d 61 6e 64 similar command
32f0: 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 to provide the
3300: 6f 75 74 70 75 74 2e 20 20 54 68 65 20 63 6f 6e output. The con
3310: 74 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6d tent of the.** m
3320: 65 73 73 61 67 65 20 77 69 6c 6c 20 61 70 70 65 essage will appe
3330: 61 72 20 6f 6e 20 73 74 61 6e 64 61 72 64 20 6f ar on standard o
3340: 75 74 70 75 74 2e 20 20 43 61 70 74 75 72 65 20 utput. Capture
3350: 74 68 69 73 20 6d 65 73 73 61 67 65 0a 2a 2a 20 this message.**
3360: 69 6e 74 6f 20 61 20 66 69 6c 65 20 6e 61 6d 65 into a file name
3370: 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20 d (for example)
3380: 6f 75 74 2e 74 78 74 2e 20 20 54 68 65 6e 20 72 out.txt. Then r
3390: 75 6e 20 74 68 65 0a 2a 2a 20 73 65 72 76 65 72 un the.** server
33a0: 20 69 6e 20 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 20 in gdb:.**.**
33b0: 20 20 20 67 64 62 20 66 6f 73 73 69 6c 0a 2a 2a gdb fossil.**
33c0: 20 20 20 20 20 72 20 74 65 73 74 2d 78 66 65 72 r test-xfer
33d0: 20 6f 75 74 2e 74 78 74 0a 2a 2f 0a 76 6f 69 64 out.txt.*/.void
33e0: 20 63 6d 64 5f 74 65 73 74 5f 78 66 65 72 28 76 cmd_test_xfer(v
33f0: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 6f 74 55 oid){. int notU
3400: 73 65 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67 sed;. if( g.arg
3410: 63 21 3d 32 20 26 26 20 67 2e 61 72 67 63 21 3d c!=2 && g.argc!=
3420: 33 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22 3 ){. usage("
3430: 3f 4d 45 53 53 41 47 45 46 49 4c 45 3f 22 29 3b ?MESSAGEFILE?");
3440: 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 73 74 5f 62 . }. db_must_b
3450: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b e_within_tree();
3460: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 67 2e . blob_zero(&g.
3470: 63 67 69 49 6e 29 3b 0a 20 20 62 6c 6f 62 5f 72 cgiIn);. blob_r
3480: 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 67 ead_from_file(&g
3490: 2e 63 67 69 49 6e 2c 20 67 2e 61 72 67 63 3d 3d .cgiIn, g.argc==
34a0: 32 20 3f 20 22 2d 22 20 3a 20 67 2e 61 72 67 76 2 ? "-" : g.argv
34b0: 5b 32 5d 29 3b 0a 20 20 64 69 73 61 62 6c 65 4c [2]);. disableL
34c0: 6f 67 69 6e 20 3d 20 31 3b 0a 20 20 70 61 67 65 ogin = 1;. page
34d0: 5f 78 66 65 72 28 29 3b 0a 20 20 70 72 69 6e 74 _xfer();. print
34e0: 66 28 22 25 73 5c 6e 22 2c 20 63 67 69 5f 65 78 f("%s\n", cgi_ex
34f0: 74 72 61 63 74 5f 63 6f 6e 74 65 6e 74 28 26 6e tract_content(&n
3500: 6f 74 55 73 65 64 29 29 3b 0a 7d 0a 0a 0a 2f 2a otUsed));.}.../*
3510: 0a 2a 2a 20 53 79 6e 63 20 74 6f 20 74 68 65 20 .** Sync to the
3520: 68 6f 73 74 20 69 64 65 6e 74 69 66 69 65 64 20 host identified
3530: 69 6e 20 67 2e 75 72 6c 4e 61 6d 65 20 61 6e 64 in g.urlName and
3540: 20 67 2e 75 72 6c 50 61 74 68 2e 20 20 54 68 69 g.urlPath. Thi
3550: 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 s.** routine is
3560: 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 63 6c called by the cl
3570: 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 63 6f ient..**.** Reco
3580: 72 64 73 20 61 72 65 20 70 75 73 68 65 64 20 74 rds are pushed t
3590: 6f 20 74 68 65 20 73 65 72 76 65 72 20 69 66 20 o the server if
35a0: 70 75 73 68 46 6c 61 67 20 69 73 20 74 72 75 65 pushFlag is true
35b0: 2e 20 20 52 65 63 6f 72 64 73 0a 2a 2a 20 61 72 . Records.** ar
35c0: 65 20 70 75 6c 6c 65 64 20 69 66 20 70 75 6c 6c e pulled if pull
35d0: 46 6c 61 67 20 69 73 20 74 72 75 65 2e 20 20 41 Flag is true. A
35e0: 20 66 75 6c 6c 20 73 79 6e 63 20 6f 63 63 75 72 full sync occur
35f0: 73 20 69 66 20 62 6f 74 68 20 61 72 65 0a 2a 2a s if both are.**
3600: 20 74 72 75 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63 true..*/.void c
3610: 6c 69 65 6e 74 5f 73 79 6e 63 28 69 6e 74 20 70 lient_sync(int p
3620: 75 73 68 46 6c 61 67 2c 20 69 6e 74 20 70 75 6c ushFlag, int pul
3630: 6c 46 6c 61 67 2c 20 69 6e 74 20 63 6c 6f 6e 65 lFlag, int clone
3640: 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 67 6f 20 Flag){. int go
3650: 3d 20 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c = 1; /* L
3660: 6f 6f 70 20 75 6e 74 69 6c 20 7a 65 72 6f 20 2a oop until zero *
3670: 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a /. int nToken;.
3680: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
3690: 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22 73 Code = db_get("s
36a0: 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 22 78 22 erver-code", "x"
36b0: 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 );. const char
36c0: 2a 7a 50 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 *zPCode = db_get
36d0: 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c ("project-code",
36e0: 20 30 29 3b 0a 20 20 69 6e 74 20 6e 46 69 6c 65 0);. int nFile
36f0: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 4d 73 67 = 0;. int nMsg
3700: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 52 65 71 = 0;. int nReq
3710: 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20 73 65 6e = 0;. Blob sen
3720: 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 d; /* Tex
3730: 74 20 77 65 20 61 72 65 20 73 65 6e 64 69 6e 67 t we are sending
3740: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 2a to the server *
3750: 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76 3b 20 20 /. Blob recv;
3760: 20 20 20 20 20 20 2f 2a 20 52 65 70 6c 79 20 77 /* Reply w
3770: 65 20 67 6f 74 20 62 61 63 6b 20 66 72 6f 6d 20 e got back from
3780: 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 the server */.
3790: 42 6c 6f 62 20 6c 69 6e 65 3b 20 20 20 20 20 20 Blob line;
37a0: 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 6c 69 /* A single li
37b0: 6e 65 20 6f 66 20 74 68 65 20 72 65 70 6c 79 20 ne of the reply
37c0: 2a 2f 0a 20 20 42 6c 6f 62 20 61 54 6f 6b 65 6e */. Blob aToken
37d0: 5b 35 5d 3b 20 20 20 2f 2a 20 41 20 74 6f 6b 65 [5]; /* A toke
37e0: 6e 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 69 6e 65 nization of line
37f0: 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72 72 6d 73 */. Blob errms
3800: 67 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 g; /* Error
3810: 20 6d 65 73 73 61 67 65 20 2a 2f 0a 0a 20 20 61 message */.. a
3820: 73 73 65 72 74 28 20 70 75 73 68 46 6c 61 67 20 ssert( pushFlag
3830: 7c 7c 20 70 75 6c 6c 46 6c 61 67 20 7c 7c 20 63 || pullFlag || c
3840: 6c 6f 6e 65 46 6c 61 67 20 29 3b 0a 20 20 61 73 loneFlag );. as
3850: 73 65 72 74 28 20 21 67 2e 75 72 6c 49 73 46 69 sert( !g.urlIsFi
3860: 6c 65 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f le ); /
3870: 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b * This only work
3880: 73 20 66 6f 72 20 6e 65 74 77 6f 72 6b 69 6e 67 s for networking
3890: 20 2a 2f 0a 0a 20 20 64 62 5f 62 65 67 69 6e 5f */.. db_begin_
38a0: 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 transaction();.
38b0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
38c0: 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 73 20 77 /* Records w
38d0: 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 74 68 65 hich we know the
38e0: 20 6f 74 68 65 72 20 73 69 64 65 20 61 6c 73 6f other side also
38f0: 20 68 61 73 20 2a 2f 0a 20 20 20 20 22 43 52 45 has */. "CRE
3900: 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f ATE TEMP TABLE o
3910: 6e 72 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45 nremote(rid INTE
3920: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29 GER PRIMARY KEY)
3930: 3b 22 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 ;". /* Record
3940: 73 20 77 65 20 6b 6e 6f 77 20 74 68 65 20 6f 74 s we know the ot
3950: 68 65 72 20 73 69 64 65 20 6e 65 65 64 73 20 2a her side needs *
3960: 2f 0a 20 20 20 20 22 43 52 45 41 54 45 20 54 45 /. "CREATE TE
3970: 4d 50 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67 MP TABLE pending
3980: 28 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 (rid INTEGER PRI
3990: 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 20 29 3b MARY KEY);". );
39a0: 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f 7a 65 72 . blobarray_zer
39b0: 6f 28 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 o(aToken, count(
39c0: 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62 6c 6f 62 aToken));. blob
39d0: 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b 0a 20 20 _zero(&send);.
39e0: 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65 63 76 29 blob_zero(&recv)
39f0: 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 ;. blob_zero(&e
3a00: 72 72 6d 73 67 29 3b 0a 0a 0a 20 20 77 68 69 6c rrmsg);... whil
3a10: 65 28 20 67 6f 20 29 7b 0a 20 20 20 20 67 6f 20 e( go ){. go
3a20: 3d 20 30 3b 0a 20 20 20 20 6e 46 69 6c 65 20 3d = 0;. nFile =
3a30: 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 3d 20 30 nReq = nMsg = 0
3a40: 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 ;.. /* Genera
3a50: 74 65 20 61 20 72 65 71 75 65 73 74 20 74 6f 20 te a request to
3a60: 62 65 20 73 65 6e 74 20 74 6f 20 74 68 65 20 73 be sent to the s
3a70: 65 72 76 65 72 2e 0a 20 20 20 20 2a 2a 20 41 6c erver.. ** Al
3a80: 77 61 79 73 20 62 65 67 69 6e 20 77 69 74 68 20 ways begin with
3a90: 61 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c 20 6f a clone, pull, o
3aa0: 72 20 70 75 73 68 20 6d 65 73 73 61 67 65 0a 20 r push message.
3ab0: 20 20 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 69 */. . i
3ac0: 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20 29 7b 0a f( cloneFlag ){.
3ad0: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e blob_appen
3ae0: 64 66 28 26 73 65 6e 64 2c 20 22 63 6c 6f 6e 65 df(&send, "clone
3af0: 5c 6e 22 29 3b 0a 20 20 20 20 20 20 70 75 73 68 \n");. push
3b00: 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 Flag = 0;.
3b10: 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b 0a 20 20 pullFlag = 0;.
3b20: 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 nMsg++;.
3b30: 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46 6c }else if( pullFl
3b40: 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 ag ){. blob
3b50: 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 _appendf(&send,
3b60: 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20 "pull %s %s\n",
3b70: 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b zSCode, zPCode);
3b80: 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 . nMsg++;.
3b90: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 75 73 }. if( pus
3ba0: 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62 hFlag ){. b
3bb0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e lob_appendf(&sen
3bc0: 64 2c 20 22 70 75 73 68 20 25 73 20 25 73 5c 6e d, "push %s %s\n
3bd0: 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 ", zSCode, zPCod
3be0: 65 29 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b e);. nMsg++
3bf0: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 ;. }.. if(
3c00: 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20 pullFlag ){.
3c10: 20 20 20 2f 2a 20 53 65 6e 64 20 67 69 6d 6d 65 /* Send gimme
3c20: 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65 76 65 message for eve
3c30: 72 79 20 70 68 61 6e 74 6f 6d 20 74 68 61 74 20 ry phantom that
3c40: 77 65 20 68 6f 6c 64 2e 0a 20 20 20 20 20 20 2a we hold.. *
3c50: 2f 0a 20 20 20 20 20 20 53 74 6d 74 20 71 3b 0a /. Stmt q;.
3c60: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65 db_prepare
3c70: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 75 75 69 (&q, "SELECT uui
3c80: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 d FROM blob WHER
3c90: 45 20 73 69 7a 65 3c 30 22 29 3b 0a 20 20 20 20 E size<0");.
3ca0: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 while( db_step
3cb0: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&q)==SQLITE_ROW
3cc0: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 ){. cons
3cd0: 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 t char *zUuid =
3ce0: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 db_column_text(&
3cf0: 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 q, 0);. b
3d00: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e lob_appendf(&sen
3d10: 64 2c 22 67 69 6d 6d 65 20 25 73 5c 6e 22 2c 20 d,"gimme %s\n",
3d20: 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 20 20 zUuid);.
3d30: 6e 52 65 71 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a nReq++;. }.
3d40: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a db_finaliz
3d50: 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 e(&q);. }..
3d60: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 29 if( pushFlag )
3d70: 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e 64 20 {. /* Send
3d80: 74 68 65 20 73 65 72 76 65 72 20 61 6e 79 20 66 the server any f
3d90: 69 6c 65 73 20 74 68 61 74 20 74 68 65 20 73 65 iles that the se
3da0: 72 76 65 72 20 68 61 73 20 72 65 71 75 65 73 74 rver has request
3db0: 65 64 20 2a 2f 0a 20 20 20 20 20 20 6e 46 69 6c ed */. nFil
3dc0: 65 20 2b 3d 20 73 65 6e 64 5f 61 6c 6c 5f 70 65 e += send_all_pe
3dd0: 6e 64 69 6e 67 28 26 73 65 6e 64 29 3b 0a 20 20 nding(&send);.
3de0: 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 75 6c }.. if( pul
3df0: 6c 46 6c 61 67 20 7c 7c 20 70 75 73 68 46 6c 61 lFlag || pushFla
3e00: 67 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6c g ){. /* Al
3e10: 77 61 79 73 20 73 65 6e 64 20 6f 75 72 20 6c 65 ways send our le
3e20: 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 20 53 74 aves */. St
3e30: 6d 74 20 71 3b 0a 20 20 20 20 20 20 64 62 5f 70 mt q;. db_p
3e40: 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20 repare(&q, .
3e50: 20 20 20 20 20 22 53 45 4c 45 43 54 20 75 75 69 "SELECT uui
3e60: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 d FROM blob WHER
3e70: 45 20 72 69 64 20 49 4e 22 0a 20 20 20 20 20 20 E rid IN".
3e80: 20 20 20 22 20 20 28 53 45 4c 45 43 54 20 63 69 " (SELECT ci
3e90: 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 45 58 43 d FROM plink EXC
3ea0: 45 50 54 20 53 45 4c 45 43 54 20 70 69 64 20 46 EPT SELECT pid F
3eb0: 52 4f 4d 20 70 6c 69 6e 6b 29 22 0a 20 20 20 20 ROM plink)".
3ec0: 20 20 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 );. while
3ed0: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 ( db_step(&q)==S
3ee0: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 QLITE_ROW ){.
3ef0: 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 const char
3f00: 2a 7a 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75 *zUuid = db_colu
3f10: 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a mn_text(&q, 0);.
3f20: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 blob_app
3f30: 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 6c 65 61 endf(&send, "lea
3f40: 66 20 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b f %s\n", zUuid);
3f50: 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b . nMsg++;
3f60: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 . }. d
3f70: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a b_finalize(&q);.
3f80: 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 78 }.. /* Ex
3f90: 63 68 61 6e 67 65 20 6d 65 73 73 61 67 65 73 20 change messages
3fa0: 77 69 74 68 20 74 68 65 20 73 65 72 76 65 72 20 with the server
3fb0: 2a 2f 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53 */. printf("S
3fc0: 65 6e 64 3a 20 20 20 20 20 20 25 64 20 66 69 6c end: %d fil
3fd0: 65 73 2c 20 25 64 20 72 65 71 75 65 73 74 73 2c es, %d requests,
3fe0: 20 25 64 20 6f 74 68 65 72 20 6d 65 73 73 61 67 %d other messag
3ff0: 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 es\n",.
4000: 20 20 20 6e 46 69 6c 65 2c 20 6e 52 65 71 2c 20 nFile, nReq,
4010: 6e 4d 73 67 29 3b 0a 20 20 20 20 6e 46 69 6c 65 nMsg);. nFile
4020: 20 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 3d = nReq = nMsg =
4030: 20 30 3b 0a 20 20 20 20 68 74 74 70 5f 65 78 63 0;. http_exc
4040: 68 61 6e 67 65 28 26 73 65 6e 64 2c 20 26 72 65 hange(&send, &re
4050: 63 76 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 cv);. blob_re
4060: 73 65 74 28 26 73 65 6e 64 29 3b 0a 0a 20 20 20 set(&send);..
4070: 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20 /* Process the
4080: 72 65 70 6c 79 20 74 68 61 74 20 63 61 6d 65 20 reply that came
4090: 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 65 back from the se
40a0: 72 76 65 72 20 2a 2f 0a 20 20 20 20 77 68 69 6c rver */. whil
40b0: 65 28 20 62 6c 6f 62 5f 6c 69 6e 65 28 26 72 65 e( blob_line(&re
40c0: 63 76 2c 20 26 6c 69 6e 65 29 20 29 7b 0a 20 20 cv, &line) ){.
40d0: 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f nToken = blo
40e0: 62 5f 74 6f 6b 65 6e 69 7a 65 28 26 6c 69 6e 65 b_tokenize(&line
40f0: 2c 20 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 , aToken, count(
4100: 61 54 6f 6b 65 6e 29 29 3b 0a 0a 20 20 20 20 20 aToken));..
4110: 20 2f 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20 /* file UUID
4120: 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a SIZE \n CONTENT.
4130: 20 20 20 20 20 20 2a 2a 20 20 20 66 69 6c 65 20 ** file
4140: 55 55 49 44 20 44 45 4c 54 41 53 52 43 20 53 49 UUID DELTASRC SI
4150: 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 ZE \n CONTENT.
4160: 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
4170: 52 65 63 65 69 76 65 20 61 20 66 69 6c 65 20 74 Receive a file t
4180: 72 61 6e 73 6d 69 74 74 65 64 20 66 72 6f 6d 20 ransmitted from
4190: 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 0a 20 the other side.
41a0: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 */. if
41b0: 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 ( blob_eq(&aToke
41c0: 6e 5b 30 5d 2c 22 66 69 6c 65 22 29 20 29 7b 0a n[0],"file") ){.
41d0: 20 20 20 20 20 20 20 20 78 66 65 72 5f 61 63 63 xfer_acc
41e0: 65 70 74 5f 66 69 6c 65 28 26 72 65 63 76 2c 20 ept_file(&recv,
41f0: 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 aToken, nToken,
4200: 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 &errmsg);.
4210: 20 20 6e 46 69 6c 65 2b 2b 3b 0a 20 20 20 20 20 nFile++;.
4220: 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 go = 1;.
4230: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a }else.. /*
4240: 20 20 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 20 gimme UUID.
4250: 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 **. **
4260: 53 65 72 76 65 72 20 69 73 20 72 65 71 75 65 73 Server is reques
4270: 74 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 20 20 ting a file.
4280: 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62 */. if( b
4290: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 lob_eq(&aToken[0
42a0: 5d 2c 20 22 67 69 6d 6d 65 22 29 20 26 26 20 6e ], "gimme") && n
42b0: 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 Token==2.
42c0: 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f && blob_
42d0: 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b is_uuid(&aToken[
42e0: 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 1]) ){. n
42f0: 52 65 71 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69 Req++;. i
4300: 66 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 f( pushFlag ){.
4310: 20 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 db_mult
4320: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 20 i_exec(.
4330: 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 "INSERT OR I
4340: 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64 69 GNORE INTO pendi
4350: 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20 20 20 ng(rid) ".
4360: 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 72 69 "SELECT ri
4370: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 d FROM blob WHER
4380: 45 20 75 75 69 64 3d 25 42 20 41 4e 44 20 73 69 E uuid=%B AND si
4390: 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b 65 6e 5b ze>=0", &aToken[
43a0: 31 5d 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 1]. );.
43b0: 20 20 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31 go = 1
43c0: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 ;. }.
43d0: 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20 20 20 20 }else. .
43e0: 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44 0a /* igot UUID.
43f0: 20 20 20 20 20 20 2a 2a 20 20 20 6c 65 61 66 20 ** leaf
4400: 55 55 49 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 UUID. **.
4410: 20 20 20 20 2a 2a 20 53 65 72 76 65 72 20 70 72 ** Server pr
4420: 6f 63 6c 61 69 6d 73 20 74 68 61 74 20 69 74 20 oclaims that it
4430: 68 61 73 20 61 20 70 61 72 74 69 63 75 6c 61 72 has a particular
4440: 20 66 69 6c 65 2e 20 20 41 20 6c 65 61 66 20 6d file. A leaf m
4450: 65 73 73 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 essage. **
4460: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66 means that the f
4470: 69 6c 65 20 69 73 20 61 20 6c 65 61 66 20 6d 61 ile is a leaf ma
4480: 6e 69 66 65 73 74 20 6f 6e 20 74 68 65 20 73 65 nifest on the se
4490: 72 76 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 rver.. */.
44a0: 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d if( nToken=
44b0: 3d 32 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 =2.
44c0: 20 26 26 20 28 62 6c 6f 62 5f 65 71 28 26 61 54 && (blob_eq(&aT
44d0: 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f 74 22 29 oken[0], "igot")
44e0: 20 7c 7c 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f || blob_eq(&aTo
44f0: 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 29 ken[0], "leaf"))
4500: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 . &
4510: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 & blob_is_uuid(&
4520: 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20 aToken[1]) ){.
4530: 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d 20 int rid =
4540: 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43 db_int(0, "SELEC
4550: 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 T rid FROM blob
4560: 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c 20 WHERE uuid=%B",
4570: 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 &aToken[1]);.
4580: 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 nMsg++;.
4590: 20 20 20 20 20 69 66 28 20 72 69 64 3e 30 20 29 if( rid>0 )
45a0: 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 6d {. db_m
45b0: 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 ulti_exec(.
45c0: 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f "INSERT O
45d0: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e R IGNORE INTO on
45e0: 72 65 6d 6f 74 65 28 72 69 64 29 20 56 41 4c 55 remote(rid) VALU
45f0: 45 53 28 25 64 29 22 2c 20 72 69 64 0a 20 20 20 ES(%d)", rid.
4600: 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 );.
4610: 20 20 20 20 2f 2a 20 41 64 64 20 74 6f 20 74 68 /* Add to th
4620: 65 20 70 65 6e 64 69 6e 67 20 73 65 74 20 61 6c e pending set al
4630: 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68 l children of th
4640: 65 20 73 65 72 76 65 72 27 73 20 6c 65 61 76 65 e server's leave
4650: 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 s */. i
4660: 66 28 20 70 75 73 68 46 6c 61 67 20 26 26 20 62 f( pushFlag && b
4670: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 lob_eq(&aToken[0
4680: 5d 2c 20 22 6c 65 61 66 22 29 20 29 7b 0a 20 20 ], "leaf") ){.
4690: 20 20 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c db_mul
46a0: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 ti_exec(.
46b0: 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f "INSERT O
46c0: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 R IGNORE INTO pe
46d0: 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 nding(rid) ".
46e0: 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 "SELE
46f0: 43 54 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e CT cid FROM plin
4700: 6b 20 57 48 45 52 45 20 70 69 64 3d 25 64 22 2c k WHERE pid=%d",
4710: 20 72 69 64 0a 20 20 20 20 20 20 20 20 20 20 20 rid.
4720: 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 );.
4730: 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 go = 1;.
4740: 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66 }. if
4750: 28 20 70 75 6c 6c 46 6c 61 67 20 26 26 20 21 67 ( pullFlag && !g
4760: 6f 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 o && .
4770: 20 20 20 20 64 62 5f 65 78 69 73 74 73 28 22 53 db_exists("S
4780: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 62 6c 6f ELECT 1 FROM blo
4790: 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 20 41 b WHERE rid=%d A
47a0: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64 ND size>=0", rid
47b0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
47c0: 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 go = 1;.
47d0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c }. }el
47e0: 73 65 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 se if( pullFlag
47f0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 20 ){. go
4800: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63 = 1;. c
4810: 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c ontent_put(0, bl
4820: 6f 62 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 ob_str(&aToken[1
4830: 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 ]));. }.
4840: 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20 }else. .
4850: 20 20 20 20 2f 2a 20 20 20 70 75 73 68 20 20 53 /* push S
4860: 45 52 56 45 52 43 4f 44 45 20 20 50 52 4f 44 55 ERVERCODE PRODU
4870: 43 54 43 4f 44 45 0a 20 20 20 20 20 20 2a 2a 0a CTCODE. **.
4880: 20 20 20 20 20 20 2a 2a 20 53 68 6f 75 6c 64 20 ** Should
4890: 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 6e 20 72 only happen in r
48a0: 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 63 6c 6f esponse to a clo
48b0: 6e 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ne.. */.
48c0: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 if( blob_eq(&
48d0: 61 54 6f 6b 65 6e 5b 30 5d 2c 22 70 75 73 68 22 aToken[0],"push"
48e0: 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 33 20 26 ) && nToken==3 &
48f0: 26 20 63 6c 6f 6e 65 46 6c 61 67 0a 20 20 20 20 & cloneFlag.
4900: 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f && blo
4910: 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 b_is_uuid(&aToke
4920: 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 5f 69 73 n[1]) && blob_is
4930: 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32 5d _uuid(&aToken[2]
4940: 29 20 29 7b 0a 0a 20 20 20 20 20 20 20 20 69 66 ) ){.. if
4950: 28 20 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61 ( blob_eq_str(&a
4960: 54 6f 6b 65 6e 5b 31 5d 2c 20 7a 53 43 6f 64 65 Token[1], zSCode
4970: 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20 , -1) ){.
4980: 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28 fossil_fatal(
4990: 22 73 65 72 76 65 72 20 6c 6f 6f 70 22 29 3b 0a "server loop");.
49a0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
49b0: 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 20 20 nMsg++;.
49c0: 20 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20 if( zPCode==0
49d0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 50 43 ){. zPC
49e0: 6f 64 65 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 ode = mprintf("%
49f0: 62 22 2c 20 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b b", &aToken[2]);
4a00: 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 73 65 . db_se
4a10: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 t("project-code"
4a20: 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20 , zPCode);.
4a30: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 6c 6f }. clo
4a40: 6e 65 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 neFlag = 0;.
4a50: 20 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 31 pullFlag = 1
4a60: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 ;. }else..
4a70: 20 20 20 20 20 2f 2a 20 20 20 65 72 72 6f 72 20 /* error
4a80: 4d 45 53 53 41 47 45 0a 20 20 20 20 20 20 2a 2a MESSAGE. **
4a90: 0a 20 20 20 20 20 20 2a 2a 20 52 65 70 6f 72 74 . ** Report
4aa0: 20 61 6e 20 65 72 72 6f 72 0a 20 20 20 20 20 20 an error.
4ab0: 2a 2f 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 */ .
4ac0: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 if( blob_eq(&aT
4ad0: 6f 6b 65 6e 5b 30 5d 2c 22 65 72 72 6f 72 22 29 oken[0],"error")
4ae0: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 32 20 29 7b && nToken==2 ){
4af0: 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a . char *z
4b00: 4d 73 67 20 3d 20 62 6c 6f 62 5f 74 65 72 6d 69 Msg = blob_termi
4b10: 6e 61 74 65 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 nate(&aToken[1])
4b20: 3b 0a 20 20 20 20 20 20 20 20 64 65 66 6f 73 73 ;. defoss
4b30: 69 6c 69 7a 65 28 7a 4d 73 67 29 3b 0a 20 20 20 ilize(zMsg);.
4b40: 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 blob_append
4b50: 66 28 26 65 72 72 6d 73 67 2c 20 22 73 65 72 76 f(&errmsg, "serv
4b60: 65 72 20 73 61 79 73 3a 20 25 73 22 2c 20 7a 4d er says: %s", zM
4b70: 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 sg);. }else
4b80: 0a 0a 20 20 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f .. /* Unkno
4b90: 77 6e 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20 wn message */.
4ba0: 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 62 6c {. bl
4bb0: 6f 62 5f 61 70 70 65 6e 64 66 28 26 65 72 72 6d ob_appendf(&errm
4bc0: 73 67 2c 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6d sg, "unknown com
4bd0: 6d 61 6e 64 3a 20 25 62 22 2c 20 26 61 54 6f 6b mand: %b", &aTok
4be0: 65 6e 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 0a en[0]);. }.
4bf0: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f . if( blob_
4c00: 73 69 7a 65 28 26 65 72 72 6d 73 67 29 20 29 7b size(&errmsg) ){
4c10: 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f . fossil_
4c20: 66 61 74 61 6c 28 22 25 62 22 2c 20 26 65 72 72 fatal("%b", &err
4c30: 6d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 msg);. }.
4c40: 20 20 20 20 62 6c 6f 62 61 72 72 61 79 5f 72 65 blobarray_re
4c50: 73 65 74 28 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b set(aToken, nTok
4c60: 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62 en);. }. b
4c70: 6c 6f 62 5f 72 65 73 65 74 28 26 72 65 63 76 29 lob_reset(&recv)
4c80: 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 52 65 ;. printf("Re
4c90: 63 65 69 76 65 64 3a 20 20 25 64 20 66 69 6c 65 ceived: %d file
4ca0: 73 2c 20 25 64 20 72 65 71 75 65 73 74 73 2c 20 s, %d requests,
4cb0: 25 64 20 6f 74 68 65 72 20 6d 65 73 73 61 67 65 %d other message
4cc0: 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 s\n",.
4cd0: 20 20 6e 46 69 6c 65 2c 20 6e 52 65 71 2c 20 6e nFile, nReq, n
4ce0: 4d 73 67 29 3b 0a 20 20 20 20 6e 46 69 6c 65 20 Msg);. nFile
4cf0: 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 3d 20 = nReq = nMsg =
4d00: 30 3b 0a 20 20 7d 3b 0a 20 20 68 74 74 70 5f 63 0;. };. http_c
4d10: 6c 6f 73 65 28 29 3b 0a 20 20 64 62 5f 65 6e 64 lose();. db_end
4d20: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b _transaction(0);
4d30: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 . db_multi_exec
4d40: 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c (. "DROP TABL
4d50: 45 20 6f 6e 72 65 6d 6f 74 65 3b 22 0a 20 20 20 E onremote;".
4d60: 20 22 44 52 4f 50 20 54 41 42 4c 45 20 70 65 6e "DROP TABLE pen
4d70: 64 69 6e 67 3b 22 0a 20 20 29 3b 0a 7d 0a ding;". );.}.