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 31 30 30 30 30 30 30 29 3b 0a 20 ze", 1000000);.
0f80: 20 53 74 6d 74 20 71 3b 0a 23 69 66 20 30 0a 20 Stmt q;.#if 0.
0f90: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
0fa0: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 "CREATE TEMP
0fb0: 20 54 41 42 4c 45 20 70 72 69 6f 72 69 74 79 28 TABLE priority(
0fc0: 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d rid INTEGER PRIM
0fd0: 41 52 59 20 4b 45 59 29 3b 22 0a 20 20 20 20 22 ARY KEY);". "
0fe0: 49 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6f INSERT INTO prio
0ff0: 72 69 74 79 22 0a 20 20 20 20 22 20 53 45 4c 45 rity". " SELE
1000: 43 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 CT srcid FROM de
1010: 6c 74 61 22 0a 20 20 20 20 22 20 20 57 48 45 52 lta". " WHER
1020: 45 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 E EXISTS(SELECT
1030: 31 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 1 FROM onremote
1040: 57 48 45 52 45 20 6f 6e 72 65 6d 6f 74 65 2e 72 WHERE onremote.r
1050: 69 64 3d 64 65 6c 74 61 2e 72 69 64 29 22 0a 20 id=delta.rid)".
1060: 20 20 20 22 20 20 20 20 41 4e 44 20 45 58 49 53 " AND EXIS
1070: 54 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d TS(SELECT 1 FROM
1080: 20 70 65 6e 64 69 6e 67 20 57 48 45 52 45 20 64 pending WHERE d
1090: 65 6c 74 61 2e 73 72 63 69 64 3d 70 65 6e 64 69 elta.srcid=pendi
10a0: 6e 67 2e 72 69 64 29 3b 22 0a 20 20 20 20 22 49 ng.rid);". "I
10b0: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 NSERT OR IGNORE
10c0: 49 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 INTO priority".
10d0: 20 20 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 " SELECT rid
10e0: 46 52 4f 4d 20 64 65 6c 74 61 22 0a 20 20 20 20 FROM delta".
10f0: 22 20 20 57 48 45 52 45 20 45 58 49 53 54 53 28 " WHERE EXISTS(
1100: 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e SELECT 1 FROM on
1110: 72 65 6d 6f 74 65 20 57 48 45 52 45 20 6f 6e 72 remote WHERE onr
1120: 65 6d 6f 74 65 2e 72 69 64 3d 64 65 6c 74 61 2e emote.rid=delta.
1130: 73 72 63 69 64 29 22 0a 20 20 20 20 22 20 20 20 srcid)". "
1140: 20 41 4e 44 20 45 58 49 53 54 53 28 53 45 4c 45 AND EXISTS(SELE
1150: 43 54 20 31 20 46 52 4f 4d 20 70 65 6e 64 69 6e CT 1 FROM pendin
1160: 67 20 57 48 45 52 45 20 64 65 6c 74 61 2e 72 69 g WHERE delta.ri
1170: 64 3d 70 65 6e 64 69 6e 67 2e 72 69 64 29 3b 22 d=pending.rid);"
1180: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 73 . );. while( s
1190: 65 6e 74 3c 6d 61 78 53 69 7a 65 20 26 26 20 28 ent<maxSize && (
11a0: 72 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 rid = db_int(0,
11b0: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d "SELECT rid FROM
11c0: 20 70 72 69 6f 72 69 74 79 22 29 29 21 3d 30 20 priority"))!=0
11d0: 29 7b 0a 20 20 20 20 73 65 6e 74 20 2b 3d 20 73 ){. sent += s
11e0: 65 6e 64 5f 66 69 6c 65 28 72 69 64 2c 20 70 4f end_file(rid, pO
11f0: 75 74 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 ut);. db_mult
1200: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 22 49 i_exec(. "I
1210: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 NSERT OR IGNORE
1220: 49 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 INTO priority".
1230: 20 20 20 20 20 22 20 53 45 4c 45 43 54 20 73 72 " SELECT sr
1240: 63 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 cid FROM delta W
1250: 48 45 52 45 20 72 69 64 3d 25 64 22 0a 20 20 20 HERE rid=%d".
1260: 20 20 20 22 20 55 4e 49 4f 4e 20 41 4c 4c 22 0a " UNION ALL".
1270: 20 20 20 20 20 20 22 20 53 45 4c 45 43 54 20 72 " SELECT r
1280: 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 id FROM delta WH
1290: 45 52 45 20 73 72 63 69 64 3d 25 64 22 2c 0a 20 ERE srcid=%d",.
12a0: 20 20 20 20 20 72 69 64 2c 20 72 69 64 0a 20 20 rid, rid.
12b0: 20 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a );. }.#endif.
12c0: 20 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c db_prepare(&q,
12d0: 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f "SELECT rid FRO
12e0: 4d 20 70 65 6e 64 69 6e 67 20 4f 52 44 45 52 20 M pending ORDER
12f0: 42 59 20 72 69 64 22 29 3b 0a 20 20 77 68 69 6c BY rid");. whil
1300: 65 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d e( db_step(&q)==
1310: 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 SQLITE_ROW ){.
1320: 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 int rid = db_c
1330: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 olumn_int(&q, 0)
1340: 3b 0a 20 20 20 20 69 66 28 20 73 65 6e 74 3c 6d ;. if( sent<m
1350: 61 78 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 axSize ){.
1360: 73 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66 69 6c sent += send_fil
1370: 65 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a 20 20 e(rid, pOut);.
1380: 20 20 20 20 6e 53 65 6e 74 2b 2b 3b 0a 20 20 20 nSent++;.
1390: 20 20 20 69 52 69 64 53 65 6e 74 20 3d 20 72 69 iRidSent = ri
13a0: 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 d;. }else{.
13b0: 20 20 20 20 63 68 61 72 20 2a 7a 55 75 69 64 20 char *zUuid
13c0: 3d 20 64 62 5f 74 65 78 74 28 30 2c 0a 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 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 "SELECT uuid
13f0: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 FROM blob WHERE
1400: 72 69 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e rid=%d AND size>
1410: 3d 30 22 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 =0", rid);.
1420: 20 69 66 28 20 7a 55 75 69 64 20 29 7b 0a 20 20 if( zUuid ){.
1430: 20 20 20 20 20 20 69 66 28 20 70 4f 75 74 20 29 if( pOut )
1440: 7b 0a 20 20 20 20 20 20 20 20 20 20 62 6c 6f 62 {. blob
1450: 5f 61 70 70 65 6e 64 66 28 70 4f 75 74 2c 20 22 _appendf(pOut, "
1460: 69 67 6f 74 20 25 73 5c 6e 22 2c 20 7a 55 75 69 igot %s\n", zUui
1470: 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 d);. }els
1480: 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 e{. cgi
1490: 5f 70 72 69 6e 74 66 28 22 69 67 6f 74 20 25 73 _printf("igot %s
14a0: 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 \n", zUuid);.
14b0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 }. f
14c0: 72 65 65 28 7a 55 75 69 64 29 3b 0a 20 20 20 20 ree(zUuid);.
14d0: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 }. }. }.
14e0: 64 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b db_finalize(&q);
14f0: 0a 20 20 0a 20 20 2f 2a 20 44 65 6c 65 74 65 20 . . /* Delete
1500: 74 68 65 20 27 70 65 6e 64 69 6e 67 27 20 72 65 the 'pending' re
1510: 63 6f 72 64 73 20 66 6f 72 20 61 6c 6c 20 66 69 cords for all fi
1520: 6c 65 73 20 6a 75 73 74 20 73 65 6e 74 2e 20 4f les just sent. O
1530: 74 68 65 72 77 69 73 65 2c 0a 20 20 2a 2a 20 77 therwise,. ** w
1540: 65 20 63 61 6e 20 77 69 6e 64 20 75 70 20 73 65 e can wind up se
1550: 6e 64 69 6e 67 20 73 6f 6d 65 20 66 69 6c 65 73 nding some files
1560: 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2e more than once.
1570: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 53 65 6e . */. if( nSen
1580: 74 3e 30 20 29 7b 0a 20 20 20 20 64 62 5f 6d 75 t>0 ){. db_mu
1590: 6c 74 69 5f 65 78 65 63 28 22 44 45 4c 45 54 45 lti_exec("DELETE
15a0: 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 20 57 48 FROM pending WH
15b0: 45 52 45 20 72 69 64 20 3c 3d 20 25 64 22 2c 20 ERE rid <= %d",
15c0: 69 52 69 64 53 65 6e 74 29 3b 0a 20 20 7d 0a 0a iRidSent);. }..
15d0: 23 69 66 20 30 0a 20 20 64 62 5f 6d 75 6c 74 69 #if 0. db_multi
15e0: 5f 65 78 65 63 28 22 44 52 4f 50 20 54 41 42 4c _exec("DROP TABL
15f0: 45 20 70 72 69 6f 72 69 74 79 22 29 3b 0a 23 65 E priority");.#e
1600: 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 6e 53 ndif. return nS
1610: 65 6e 74 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 ent;.}.../*.** C
1620: 68 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74 75 heck the signatu
1630: 72 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63 61 re on an applica
1640: 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70 61 tion/x-fossil pa
1650: 79 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20 62 yload received b
1660: 79 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73 65 y.** the HTTP se
1670: 72 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e 61 rver. The signa
1680: 74 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20 6f ture is a line o
1690: 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 f the following
16a0: 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 form:.**.**
16b0: 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20 4e login LOGIN N
16c0: 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a 2a ONCE SIGNATURE.*
16d0: 2a 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20 69 *.** The NONCE i
16e0: 73 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e s a random strin
16f0: 67 2e 20 20 54 68 65 20 73 65 72 76 65 72 20 77 g. The server w
1700: 69 6c 6c 20 6e 65 76 65 72 20 61 63 63 65 70 74 ill never accept
1710: 20 61 0a 2a 2a 20 72 65 70 65 61 74 20 4e 4f 4e a.** repeat NON
1720: 43 45 2e 20 20 53 49 47 4e 41 54 55 52 45 20 69 CE. SIGNATURE i
1730: 73 20 74 68 65 20 53 48 41 31 20 63 68 65 63 6b s the SHA1 check
1740: 73 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45 sum of the NONCE
1750: 20 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 .** concatenate
1760: 64 20 77 69 74 68 20 74 68 65 20 75 73 65 72 73 d with the users
1770: 20 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a password..**.**
1780: 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 The parameters
1790: 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 to this routine
17a0: 61 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20 62 are ephermeral b
17b0: 6c 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 lobs holding the
17c0: 0a 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45 .** LOGIN, NONCE
17d0: 20 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e 0a and SIGNATURE..
17e0: 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 **.** This routi
17f0: 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c ne attempts to l
1800: 6f 63 61 74 65 20 74 68 65 20 75 73 65 72 20 61 ocate the user a
1810: 6e 64 20 76 65 72 69 66 79 20 74 68 65 20 73 69 nd verify the si
1820: 67 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65 gnature..** If e
1830: 76 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b 73 verything checks
1840: 20 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e 43 out, the USER.C
1850: 41 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68 AP column for th
1860: 65 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20 e USER table.**
1870: 69 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20 is consulted to
1880: 73 65 74 20 70 72 69 76 69 6c 65 67 65 73 20 69 set privileges i
1890: 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20 76 n the global g v
18a0: 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 ariable..**.** I
18b0: 66 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c 73 f anything fails
18c0: 20 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e to check out, n
18d0: 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 o changes are ma
18e0: 64 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65 73 de to privileges
18f0: 2e 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72 ..**.** Signatur
1900: 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20 e generation on
1910: 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 20 the client side
1920: 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74 68 is handled by th
1930: 65 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68 61 e .** http_excha
1940: 6e 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a nge() routine..*
1950: 2f 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67 /.void check_log
1960: 69 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c in(Blob *pLogin,
1970: 20 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42 Blob *pNonce, B
1980: 6c 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53 74 lob *pSig){. St
1990: 6d 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b 0a mt q;. int rc;.
19a0: 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74 73 . if( db_exists
19b0: 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 ("SELECT 1 FROM
19c0: 72 63 76 66 72 6f 6d 20 57 48 45 52 45 20 6e 6f rcvfrom WHERE no
19d0: 6e 63 65 3d 25 42 22 2c 20 70 4e 6f 6e 63 65 29 nce=%B", pNonce)
19e0: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 ){. return;
19f0: 20 2f 2a 20 4e 65 76 65 72 20 61 63 63 65 70 74 /* Never accept
1a00: 20 61 20 72 65 70 65 61 74 65 64 20 6e 6f 6e 63 a repeated nonc
1a10: 65 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 70 72 e */. }. db_pr
1a20: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 epare(&q, "SELEC
1a30: 54 20 70 77 2c 20 63 61 70 2c 20 75 69 64 20 46 T pw, cap, uid F
1a40: 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 6c ROM user WHERE l
1a50: 6f 67 69 6e 3d 25 42 22 2c 20 70 4c 6f 67 69 6e ogin=%B", pLogin
1a60: 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65 70 );. if( db_step
1a70: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 (&q)==SQLITE_ROW
1a80: 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 70 77 2c ){. Blob pw,
1a90: 20 63 6f 6d 62 69 6e 65 64 2c 20 68 61 73 68 3b combined, hash;
1aa0: 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 . blob_zero(&
1ab0: 70 77 29 3b 0a 20 20 20 20 64 62 5f 65 70 68 65 pw);. db_ephe
1ac0: 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 meral_blob(&q, 0
1ad0: 2c 20 26 70 77 29 3b 0a 20 20 20 20 62 6c 6f 62 , &pw);. blob
1ae0: 5f 7a 65 72 6f 28 26 63 6f 6d 62 69 6e 65 64 29 _zero(&combined)
1af0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79 28 ;. blob_copy(
1b00: 26 63 6f 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e 63 &combined, pNonc
1b10: 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 e);. blob_app
1b20: 65 6e 64 28 26 63 6f 6d 62 69 6e 65 64 2c 20 62 end(&combined, b
1b30: 6c 6f 62 5f 62 75 66 66 65 72 28 26 70 77 29 2c lob_buffer(&pw),
1b40: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29 29 blob_size(&pw))
1b50: 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c ;. sha1sum_bl
1b60: 6f 62 28 26 63 6f 6d 62 69 6e 65 64 2c 20 26 68 ob(&combined, &h
1b70: 61 73 68 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 ash);. rc = b
1b80: 6c 6f 62 5f 63 6f 6d 70 61 72 65 28 26 68 61 73 lob_compare(&has
1b90: 68 2c 20 70 53 69 67 29 3b 0a 20 20 20 20 62 6c h, pSig);. bl
1ba0: 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29 3b ob_reset(&hash);
1bb0: 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 . blob_reset(
1bc0: 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 &combined);.
1bd0: 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 if( rc==0 ){.
1be0: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a const char *z
1bf0: 43 61 70 3b 0a 20 20 20 20 20 20 7a 43 61 70 20 Cap;. zCap
1c00: 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 = db_column_text
1c10: 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 20 20 6c (&q, 1);. l
1c20: 6f 67 69 6e 5f 73 65 74 5f 63 61 70 61 62 69 6c ogin_set_capabil
1c30: 69 74 69 65 73 28 7a 43 61 70 29 3b 0a 20 20 20 ities(zCap);.
1c40: 20 20 20 67 2e 75 73 65 72 55 69 64 20 3d 20 64 g.userUid = d
1c50: 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c b_column_int(&q,
1c60: 20 32 29 3b 0a 20 20 20 20 20 20 67 2e 7a 4c 6f 2);. g.zLo
1c70: 67 69 6e 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 gin = mprintf("%
1c80: 62 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 20 b", pLogin);.
1c90: 20 20 20 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d 70 g.zNonce = mp
1ca0: 72 69 6e 74 66 28 22 25 62 22 2c 20 70 4e 6f 6e rintf("%b", pNon
1cb0: 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 ce);. }. }.
1cc0: 20 64 62 5f 72 65 73 65 74 28 26 71 29 3b 0a 7d db_reset(&q);.}
1cd0: 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 .../*.** If this
1ce0: 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 variable is set
1cf0: 2c 20 64 69 73 61 62 6c 65 20 6c 6f 67 69 6e 20 , disable login
1d00: 63 68 65 63 6b 73 2e 20 20 55 73 65 64 20 66 6f checks. Used fo
1d10: 72 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20 6f r debugging.** o
1d20: 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 nly..*/.static i
1d30: 6e 74 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 nt disableLogin
1d40: 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 = 0;../*.** WEBP
1d50: 41 47 45 3a 20 78 66 65 72 0a 2a 2a 0a 2a 2a 20 AGE: xfer.**.**
1d60: 54 68 69 73 20 69 73 20 74 68 65 20 74 72 61 6e This is the tran
1d70: 73 66 65 72 20 68 61 6e 64 6c 65 72 20 6f 6e 20 sfer handler on
1d80: 74 68 65 20 73 65 72 76 65 72 20 73 69 64 65 2e the server side.
1d90: 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a 2a The transfer.*
1da0: 2a 20 6d 65 73 73 61 67 65 20 68 61 73 20 62 65 * message has be
1db0: 65 6e 20 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 en uncompressed
1dc0: 61 6e 64 20 70 6c 61 63 65 64 20 69 6e 20 74 68 and placed in th
1dd0: 65 20 67 2e 63 67 69 49 6e 20 62 6c 6f 62 2e 0a e g.cgiIn blob..
1de0: 2a 2a 20 50 72 6f 63 65 73 73 20 74 68 69 73 20 ** Process this
1df0: 6d 65 73 73 61 67 65 20 61 6e 64 20 66 6f 72 6d message and form
1e00: 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 an appropriate
1e10: 72 65 70 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 70 reply..*/.void p
1e20: 61 67 65 5f 78 66 65 72 28 76 6f 69 64 29 7b 0a age_xfer(void){.
1e30: 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 int nToken;.
1e40: 69 6e 74 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a int isPull = 0;.
1e50: 20 20 69 6e 74 20 69 73 50 75 73 68 20 3d 20 30 int isPush = 0
1e60: 3b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 ;. int nErr = 0
1e70: 3b 0a 20 20 42 6c 6f 62 20 6c 69 6e 65 2c 20 65 ;. Blob line, e
1e80: 72 72 6d 73 67 2c 20 61 54 6f 6b 65 6e 5b 35 5d rrmsg, aToken[5]
1e90: 3b 0a 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 ;.. db_begin_tr
1ea0: 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20 62 ansaction();. b
1eb0: 6c 6f 62 61 72 72 61 79 5f 7a 65 72 6f 28 61 54 lobarray_zero(aT
1ec0: 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b oken, count(aTok
1ed0: 65 6e 29 29 3b 0a 20 20 63 67 69 5f 73 65 74 5f en));. cgi_set_
1ee0: 63 6f 6e 74 65 6e 74 5f 74 79 70 65 28 67 2e 7a content_type(g.z
1ef0: 43 6f 6e 74 65 6e 74 54 79 70 65 29 3b 0a 20 20 ContentType);.
1f00: 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 72 72 6d 73 blob_zero(&errms
1f10: 67 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 g);. db_multi_e
1f20: 78 65 63 28 0a 20 20 20 20 20 22 43 52 45 41 54 xec(. "CREAT
1f30: 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 E TEMP TABLE onr
1f40: 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45 47 45 emote(rid INTEGE
1f50: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 R PRIMARY KEY);"
1f60: 20 2f 2a 20 43 6c 69 65 6e 74 20 68 61 73 20 2a /* Client has *
1f70: 2f 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54 /. "CREATE T
1f80: 45 4d 50 20 54 41 42 4c 45 20 70 65 6e 64 69 6e EMP TABLE pendin
1f90: 67 28 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 g(rid INTEGER PR
1fa0: 49 4d 41 52 59 20 4b 45 59 29 3b 22 20 20 2f 2a IMARY KEY);" /*
1fb0: 20 43 6c 69 65 6e 74 20 6e 65 65 64 73 20 2a 2f Client needs */
1fc0: 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 62 . );. while( b
1fd0: 6c 6f 62 5f 6c 69 6e 65 28 26 67 2e 63 67 69 49 lob_line(&g.cgiI
1fe0: 6e 2c 20 26 6c 69 6e 65 29 20 29 7b 0a 20 20 20 n, &line) ){.
1ff0: 20 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 nToken = blob_t
2000: 6f 6b 65 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61 okenize(&line, a
2010: 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f Token, count(aTo
2020: 6b 65 6e 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 20 ken));.. /*
2030: 20 66 69 6c 65 20 55 55 49 44 20 53 49 5a 45 20 file UUID SIZE
2040: 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a \n CONTENT. *
2050: 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20 44 45 * file UUID DE
2060: 4c 54 41 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 LTASRC SIZE \n C
2070: 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 0a 20 20 ONTENT. **.
2080: 20 20 2a 2a 20 41 63 63 65 70 74 20 61 20 66 69 ** Accept a fi
2090: 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 6c 69 65 le from the clie
20a0: 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 nt.. */. i
20b0: 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b f( blob_eq(&aTok
20c0: 65 6e 5b 30 5d 2c 20 22 66 69 6c 65 22 29 20 26 en[0], "file") &
20d0: 26 20 6e 54 6f 6b 65 6e 3e 3d 32 20 26 26 20 6e & nToken>=2 && n
20e0: 54 6f 6b 65 6e 3c 3d 33 20 29 7b 0a 20 20 20 20 Token<=3 ){.
20f0: 20 20 69 66 28 20 21 69 73 50 75 73 68 20 29 7b if( !isPush ){
2100: 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 . cgi_res
2110: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 et_content();.
2120: 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f @ error no
2130: 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 t\sauthorized\st
2140: 6f 5c 73 77 72 69 74 65 0a 20 20 20 20 20 20 20 o\swrite.
2150: 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 nErr++;.
2160: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
2170: 20 20 20 20 20 20 78 66 65 72 5f 61 63 63 65 70 xfer_accep
2180: 74 5f 66 69 6c 65 28 26 67 2e 63 67 69 49 6e 2c t_file(&g.cgiIn,
2190: 20 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c aToken, nToken,
21a0: 20 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 &errmsg);.
21b0: 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 if( blob_size(&
21c0: 65 72 72 6d 73 67 29 20 29 7b 0a 20 20 20 20 20 errmsg) ){.
21d0: 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e cgi_reset_con
21e0: 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 tent();.
21f0: 40 20 65 72 72 6f 72 20 25 54 28 62 6c 6f 62 5f @ error %T(blob_
2200: 73 74 72 28 26 65 72 72 6d 73 67 29 29 0a 20 20 str(&errmsg)).
2210: 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 nErr++;.
2220: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 break;.
2230: 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a }. }else..
2240: 20 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 /* gimme U
2250: 55 49 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a UID. **. *
2260: 2a 20 43 6c 69 65 6e 74 20 69 73 20 72 65 71 75 * Client is requ
2270: 65 73 74 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 esting a file.
2280: 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f */. if( blo
2290: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c b_eq(&aToken[0],
22a0: 20 22 67 69 6d 6d 65 22 29 20 26 26 20 6e 54 6f "gimme") && nTo
22b0: 6b 65 6e 3d 3d 32 20 26 26 20 62 6c 6f 62 5f 69 ken==2 && blob_i
22c0: 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 s_uuid(&aToken[1
22d0: 5d 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 ]) ){. if(
22e0: 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 isPull ){.
22f0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 db_multi_exec(
2300: 0a 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 . "INSE
2310: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 RT OR IGNORE INT
2320: 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 O pending(rid) "
2330: 0a 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 . "SELE
2340: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 CT rid FROM blob
2350: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 20 41 WHERE uuid=%B A
2360: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 ND size>=0", &aT
2370: 6f 6b 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 oken[1].
2380: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d );. }. }
2390: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 69 else.. /* i
23a0: 67 6f 74 20 55 55 49 44 0a 20 20 20 20 2a 2a 20 got UUID. **
23b0: 20 20 6c 65 61 66 20 55 55 49 44 0a 20 20 20 20 leaf UUID.
23c0: 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74 **. ** Client
23d0: 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68 61 74 20 announces that
23e0: 69 74 20 68 61 73 20 61 20 70 61 72 74 69 63 75 it has a particu
23f0: 6c 61 72 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a lar file. */.
2400: 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d if( nToken==
2410: 32 0a 20 20 20 20 20 20 20 20 20 20 26 26 20 28 2. && (
2420: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b blob_eq(&aToken[
2430: 30 5d 2c 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 0], "igot") || b
2440: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 lob_eq(&aToken[0
2450: 5d 2c 22 6c 65 61 66 22 29 29 0a 20 20 20 20 20 ],"leaf")).
2460: 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f && blob_is_
2470: 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 uuid(&aToken[1])
2480: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 ){. if( is
2490: 50 75 6c 6c 20 7c 7c 20 69 73 50 75 73 68 20 29 Pull || isPush )
24a0: 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 69 {. int ri
24b0: 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 d = db_int(0, "S
24c0: 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62 ELECT rid FROM b
24d0: 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d 25 lob WHERE uuid=%
24e0: 42 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b B", &aToken[1]);
24f0: 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 69 64 . if( rid
2500: 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 >0 ){.
2510: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 db_multi_exec(.
2520: 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 "INSE
2530: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 RT OR IGNORE INT
2540: 4f 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29 20 O onremote(rid)
2550: 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 64 VALUES(%d)", rid
2560: 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 . );.
2570: 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50 75 if( isPu
2580: 6c 6c 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 ll && blob_eq(&a
2590: 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 Token[0], "leaf"
25a0: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 ) ){.
25b0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
25c0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 49 "I
25d0: 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 NSERT OR IGNORE
25e0: 49 4e 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 INTO pending(rid
25f0: 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 ) ".
2600: 20 20 22 53 45 4c 45 43 54 20 63 69 64 20 46 52 "SELECT cid FR
2610: 4f 4d 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 OM plink WHERE p
2620: 69 64 3d 25 64 22 2c 20 72 69 64 0a 20 20 20 20 id=%d", rid.
2630: 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 );.
2640: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d }. }
2650: 65 6c 73 65 20 69 66 28 20 69 73 50 75 73 68 20 else if( isPush
2660: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e ){. con
2670: 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 tent_put(0, blob
2680: 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 _str(&aToken[1])
2690: 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 );. }.
26a0: 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a }. }else..
26b0: 20 20 20 20 2f 2a 20 20 20 20 70 75 6c 6c 20 20 /* pull
26c0: 53 45 52 56 45 52 43 4f 44 45 20 20 50 52 4f 4a SERVERCODE PROJ
26d0: 45 43 54 43 4f 44 45 0a 20 20 20 20 2a 2a 20 20 ECTCODE. **
26e0: 20 20 70 75 73 68 20 20 53 45 52 56 45 52 43 4f push SERVERCO
26f0: 44 45 20 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a DE PROJECTCODE.
2700: 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 **. ** Th
2710: 65 20 63 6c 69 65 6e 74 20 77 61 6e 74 73 20 65 e client wants e
2720: 69 74 68 65 72 20 73 65 6e 64 20 6f 72 20 72 65 ither send or re
2730: 63 65 69 76 65 0a 20 20 20 20 2a 2f 0a 20 20 20 ceive. */.
2740: 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 if( nToken==3.
2750: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 &&
2760: 20 28 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 (blob_eq(&aToke
2770: 6e 5b 30 5d 2c 20 22 70 75 6c 6c 22 29 20 7c 7c n[0], "pull") ||
2780: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e blob_eq(&aToken
2790: 5b 30 5d 2c 20 22 70 75 73 68 22 29 29 0a 20 20 [0], "push")).
27a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
27b0: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
27c0: 6f 6b 65 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 oken[1]) && blob
27d0: 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e _is_uuid(&aToken
27e0: 5b 32 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 6f [2]) ){. co
27f0: 6e 73 74 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 nst char *zSCode
2800: 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 ;. const ch
2810: 61 72 20 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20 ar *zPCode;..
2820: 20 20 20 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 zSCode = db_g
2830: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 et("server-code"
2840: 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28 20 , 0);. if(
2850: 7a 53 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 zSCode==0 ){.
2860: 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 fossil_pani
2870: 63 28 22 6d 69 73 73 69 6e 67 20 73 65 72 76 65 c("missing serve
2880: 72 20 63 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 r code");.
2890: 7d 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 }. if( blob
28a0: 5f 65 71 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b _eq_str(&aToken[
28b0: 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 1], zSCode, -1)
28c0: 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 ){. cgi_r
28d0: 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a eset_content();.
28e0: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 @ error
28f0: 73 65 72 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 server\sloop.
2900: 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 nErr++;.
2910: 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 break;.
2920: 20 20 7d 0a 20 20 20 20 20 20 7a 50 43 6f 64 65 }. zPCode
2930: 20 3d 20 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 = db_get("proje
2940: 63 74 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 ct-code", 0);.
2950: 20 20 20 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d if( zPCode==
2960: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 0 ){. fos
2970: 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 73 73 69 sil_panic("missi
2980: 6e 67 20 70 72 6f 6a 65 63 74 20 63 6f 64 65 22 ng project code"
2990: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 );. }.
29a0: 20 69 66 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 if( !blob_eq_st
29b0: 72 28 26 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 r(&aToken[2], zP
29c0: 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 Code, -1) ){.
29d0: 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 cgi_reset_c
29e0: 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 ontent();.
29f0: 20 20 40 20 65 72 72 6f 72 20 77 72 6f 6e 67 5c @ error wrong\
2a00: 73 70 72 6f 6a 65 63 74 0a 20 20 20 20 20 20 20 sproject.
2a10: 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 nErr++;.
2a20: 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a break;. }.
2a30: 20 20 20 20 20 20 6c 6f 67 69 6e 5f 63 68 65 63 login_chec
2a40: 6b 5f 63 72 65 64 65 6e 74 69 61 6c 73 28 29 3b k_credentials();
2a50: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f . if( blob_
2a60: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 eq(&aToken[0], "
2a70: 70 75 6c 6c 22 29 20 29 7b 0a 20 20 20 20 20 20 pull") ){.
2a80: 20 20 69 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 if( !g.okRead
2a90: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 ){. cgi
2aa0: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 _reset_content()
2ab0: 3b 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72 ;. @ er
2ac0: 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 ror not\sauthori
2ad0: 7a 65 64 5c 73 74 6f 5c 73 72 65 61 64 0a 20 20 zed\sto\sread.
2ae0: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a nErr++;.
2af0: 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b break;
2b00: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 . }.
2b10: 20 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 isPull = 1;.
2b20: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 }else{.
2b30: 20 20 20 20 69 66 28 20 21 67 2e 6f 6b 57 72 69 if( !g.okWri
2b40: 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 te ){.
2b50: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e cgi_reset_conten
2b60: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 40 t();. @
2b70: 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 error not\sauth
2b80: 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74 orized\sto\swrit
2b90: 65 0a 20 20 20 20 20 20 20 20 20 20 6e 45 72 72 e. nErr
2ba0: 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 ++;. br
2bb0: 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 eak;. }.
2bc0: 20 20 20 20 20 20 20 69 73 50 75 73 68 20 3d 20 isPush =
2bd0: 31 3b 0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 1;.. }.
2be0: 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20 }else.. /*
2bf0: 20 63 6c 6f 6e 65 0a 20 20 20 20 2a 2a 0a 20 20 clone. **.
2c00: 20 20 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74 20 ** The client
2c10: 6b 6e 6f 77 73 20 6e 6f 74 68 69 6e 67 2e 20 20 knows nothing.
2c20: 54 65 6c 6c 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f Tell all.. */
2c30: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 . if( blob_eq
2c40: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c (&aToken[0], "cl
2c50: 6f 6e 65 22 29 20 29 7b 0a 20 20 20 20 20 20 6c one") ){. l
2c60: 6f 67 69 6e 5f 63 68 65 63 6b 5f 63 72 65 64 65 ogin_check_crede
2c70: 6e 74 69 61 6c 73 28 29 3b 0a 20 20 20 20 20 20 ntials();.
2c80: 69 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b if( !g.okRead ){
2c90: 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 . cgi_res
2ca0: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 et_content();.
2cb0: 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f @ error no
2cc0: 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 t\sauthorized\st
2cd0: 6f 5c 73 72 65 61 64 0a 20 20 20 20 20 20 20 20 o\sread.
2ce0: 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 nErr++;.
2cf0: 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 break;. }.
2d00: 20 20 20 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b isPull = 1;
2d10: 0a 20 20 20 20 20 20 40 20 70 75 73 68 20 25 73 . @ push %s
2d20: 28 64 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d (db_get("server-
2d30: 63 6f 64 65 22 2c 20 22 78 22 29 29 20 25 73 28 code", "x")) %s(
2d40: 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d db_get("project-
2d50: 63 6f 64 65 22 2c 20 22 78 22 29 29 0a 20 20 20 code", "x")).
2d60: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 db_multi_exec
2d70: 28 0a 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 (. "INSER
2d80: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f T OR IGNORE INTO
2d90: 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a pending(rid) ".
2da0: 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 "SELECT
2db0: 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 rid FROM blob WH
2dc0: 45 52 45 20 73 69 7a 65 3e 3d 30 22 0a 20 20 20 ERE size>=0".
2dd0: 20 20 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a );. }else.
2de0: 0a 20 20 20 20 2f 2a 20 20 20 20 6c 6f 67 69 6e . /* login
2df0: 20 20 55 53 45 52 20 20 4e 4f 4e 43 45 20 20 53 USER NONCE S
2e00: 49 47 4e 41 54 55 52 45 0a 20 20 20 20 2a 2a 0a IGNATURE. **.
2e10: 20 20 20 20 2a 2a 20 43 68 65 63 6b 20 66 6f 72 ** Check for
2e20: 20 61 20 76 61 6c 69 64 20 6c 6f 67 69 6e 2e 20 a valid login.
2e30: 20 54 68 69 73 20 68 61 73 20 74 6f 20 68 61 70 This has to hap
2e40: 70 65 6e 20 62 65 66 6f 72 65 20 61 6e 79 74 68 pen before anyth
2e50: 69 6e 67 20 65 6c 73 65 2e 0a 20 20 20 20 2a 2f ing else.. */
2e60: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 . if( blob_eq
2e70: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 6f (&aToken[0], "lo
2e80: 67 69 6e 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d gin") && nToken=
2e90: 3d 34 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 =4 ){. if(
2ea0: 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 29 7b 0a disableLogin ){.
2eb0: 20 20 20 20 20 20 20 20 67 2e 6f 6b 52 65 61 64 g.okRead
2ec0: 20 3d 20 67 2e 6f 6b 57 72 69 74 65 20 3d 20 31 = g.okWrite = 1
2ed0: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 ;. }else{.
2ee0: 20 20 20 20 20 20 20 63 68 65 63 6b 5f 6c 6f 67 check_log
2ef0: 69 6e 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 26 in(&aToken[1], &
2f00: 61 54 6f 6b 65 6e 5b 32 5d 2c 20 26 61 54 6f 6b aToken[2], &aTok
2f10: 65 6e 5b 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a en[3]);. }.
2f20: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f }else.. /
2f30: 2a 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 * Unknown messag
2f40: 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20 e. */. {.
2f50: 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 cgi_reset_c
2f60: 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 ontent();.
2f70: 40 20 65 72 72 6f 72 20 62 61 64 5c 73 63 6f 6d @ error bad\scom
2f80: 6d 61 6e 64 3a 5c 73 25 46 28 62 6c 6f 62 5f 73 mand:\s%F(blob_s
2f90: 74 72 28 26 6c 69 6e 65 29 29 0a 20 20 20 20 7d tr(&line)). }
2fa0: 0a 20 20 20 20 62 6c 6f 62 61 72 72 61 79 5f 72 . blobarray_r
2fb0: 65 73 65 74 28 61 54 6f 6b 65 6e 2c 20 6e 54 6f eset(aToken, nTo
2fc0: 6b 65 6e 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 ken);. }.. /*
2fd0: 54 68 65 20 69 6e 70 75 74 20 6d 65 73 73 61 67 The input messag
2fe0: 65 20 68 61 73 20 6e 6f 77 20 62 65 65 6e 20 70 e has now been p
2ff0: 72 6f 63 65 73 73 65 64 2e 20 20 47 65 6e 65 72 rocessed. Gener
3000: 61 74 65 20 61 20 72 65 70 6c 79 2e 20 2a 2f 0a ate a reply. */.
3010: 20 20 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a if( isPush ){.
3020: 20 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20 20 Stmt q;.
3030: 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 db_prepare(&q, "
3040: 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d SELECT uuid FROM
3050: 20 62 6c 6f 62 20 57 48 45 52 45 20 73 69 7a 65 blob WHERE size
3060: 3c 30 22 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 <0");. while(
3070: 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 db_step(&q)==SQ
3080: 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 LITE_ROW ){.
3090: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 const char *zU
30a0: 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f uid = db_column_
30b0: 74 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 text(&q, 0);.
30c0: 20 20 20 40 20 67 69 6d 6d 65 20 25 73 28 7a 55 @ gimme %s(zU
30d0: 75 69 64 29 0a 20 20 20 20 7d 0a 20 20 20 20 64 uid). }. d
30e0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a b_finalize(&q);.
30f0: 20 20 7d 0a 20 20 69 66 28 20 69 73 50 75 6c 6c }. if( isPull
3100: 20 29 7b 0a 20 20 20 20 73 65 6e 64 5f 61 6c 6c ){. send_all
3110: 5f 70 65 6e 64 69 6e 67 28 30 29 3b 0a 20 20 7d _pending(0);. }
3120: 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 . db_end_transa
3130: 63 74 69 6f 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a ction(0);.}../*.
3140: 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20 74 65 73 74 ** COMMAND: test
3150: 2d 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 -xfer.**.** This
3160: 20 63 6f 6d 6d 61 6e 64 20 69 73 20 75 73 65 64 command is used
3170: 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 74 for debugging t
3180: 68 65 20 73 65 72 76 65 72 2e 20 20 54 68 65 72 he server. Ther
3190: 65 20 69 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a e is a single.**
31a0: 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 argument which
31b0: 69 73 20 74 68 65 20 75 6e 63 6f 6d 70 72 65 73 is the uncompres
31c0: 73 65 64 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 sed content of a
31d0: 6e 20 22 78 66 65 72 22 20 6d 65 73 73 61 67 65 n "xfer" message
31e0: 0a 2a 2a 20 66 72 6f 6d 20 63 6c 69 65 6e 74 20 .** from client
31f0: 74 6f 20 73 65 72 76 65 72 2e 20 20 54 68 69 73 to server. This
3200: 20 63 6f 6d 6d 61 6e 64 20 69 6e 74 65 72 70 72 command interpr
3210: 65 74 73 20 74 68 61 74 20 6d 65 73 73 61 67 65 ets that message
3220: 20 61 73 0a 2a 2a 20 69 66 20 68 61 64 20 62 65 as.** if had be
3230: 65 6e 20 72 65 63 65 69 76 65 64 20 62 79 20 74 en received by t
3240: 68 65 20 73 65 72 76 65 72 2e 0a 2a 2a 0a 2a 2a he server..**.**
3250: 20 4f 6e 20 74 68 65 20 63 6c 69 65 6e 74 20 73 On the client s
3260: 69 64 65 2c 20 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20 ide, run:.**.**
3270: 20 20 20 20 20 66 6f 73 73 69 6c 20 70 75 73 68 fossil push
3280: 20 68 74 74 70 3a 2f 2f 62 6f 67 75 73 2f 20 2d http://bogus/ -
3290: 2d 68 74 74 70 74 72 61 63 65 0a 2a 2a 0a 2a 2a -httptrace.**.**
32a0: 20 4f 72 20 61 20 73 69 6d 69 6c 61 72 20 63 6f Or a similar co
32b0: 6d 6d 61 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 mmand to provide
32c0: 20 74 68 65 20 6f 75 74 70 75 74 2e 20 20 54 68 the output. Th
32d0: 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 e content of the
32e0: 0a 2a 2a 20 6d 65 73 73 61 67 65 20 77 69 6c 6c .** message will
32f0: 20 61 70 70 65 61 72 20 6f 6e 20 73 74 61 6e 64 appear on stand
3300: 61 72 64 20 6f 75 74 70 75 74 2e 20 20 43 61 70 ard output. Cap
3310: 74 75 72 65 20 74 68 69 73 20 6d 65 73 73 61 67 ture this messag
3320: 65 0a 2a 2a 20 69 6e 74 6f 20 61 20 66 69 6c 65 e.** into a file
3330: 20 6e 61 6d 65 64 20 28 66 6f 72 20 65 78 61 6d named (for exam
3340: 70 6c 65 29 20 6f 75 74 2e 74 78 74 2e 20 20 54 ple) out.txt. T
3350: 68 65 6e 20 72 75 6e 20 74 68 65 0a 2a 2a 20 73 hen run the.** s
3360: 65 72 76 65 72 20 69 6e 20 67 64 62 3a 0a 2a 2a erver in gdb:.**
3370: 0a 2a 2a 20 20 20 20 20 67 64 62 20 66 6f 73 73 .** gdb foss
3380: 69 6c 0a 2a 2a 20 20 20 20 20 72 20 74 65 73 74 il.** r test
3390: 2d 78 66 65 72 20 6f 75 74 2e 74 78 74 0a 2a 2f -xfer out.txt.*/
33a0: 0a 76 6f 69 64 20 63 6d 64 5f 74 65 73 74 5f 78 .void cmd_test_x
33b0: 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 fer(void){. int
33c0: 20 6e 6f 74 55 73 65 64 3b 0a 20 20 69 66 28 20 notUsed;. if(
33d0: 67 2e 61 72 67 63 21 3d 32 20 26 26 20 67 2e 61 g.argc!=2 && g.a
33e0: 72 67 63 21 3d 33 20 29 7b 0a 20 20 20 20 75 73 rgc!=3 ){. us
33f0: 61 67 65 28 22 3f 4d 45 53 53 41 47 45 46 49 4c age("?MESSAGEFIL
3400: 45 3f 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d E?");. }. db_m
3410: 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 ust_be_within_tr
3420: 65 65 28 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 ee();. blob_zer
3430: 6f 28 26 67 2e 63 67 69 49 6e 29 3b 0a 20 20 62 o(&g.cgiIn);. b
3440: 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 lob_read_from_fi
3450: 6c 65 28 26 67 2e 63 67 69 49 6e 2c 20 67 2e 61 le(&g.cgiIn, g.a
3460: 72 67 63 3d 3d 32 20 3f 20 22 2d 22 20 3a 20 67 rgc==2 ? "-" : g
3470: 2e 61 72 67 76 5b 32 5d 29 3b 0a 20 20 64 69 73 .argv[2]);. dis
3480: 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 31 3b 0a 20 ableLogin = 1;.
3490: 20 70 61 67 65 5f 78 66 65 72 28 29 3b 0a 20 20 page_xfer();.
34a0: 70 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c 20 63 printf("%s\n", c
34b0: 67 69 5f 65 78 74 72 61 63 74 5f 63 6f 6e 74 65 gi_extract_conte
34c0: 6e 74 28 26 6e 6f 74 55 73 65 64 29 29 3b 0a 7d nt(¬Used));.}
34d0: 0a 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 6f .../*.** Sync to
34e0: 20 74 68 65 20 68 6f 73 74 20 69 64 65 6e 74 69 the host identi
34f0: 66 69 65 64 20 69 6e 20 67 2e 75 72 6c 4e 61 6d fied in g.urlNam
3500: 65 20 61 6e 64 20 67 2e 75 72 6c 50 61 74 68 2e e and g.urlPath.
3510: 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e This.** routin
3520: 65 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 e is called by t
3530: 68 65 20 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a he client..**.**
3540: 20 52 65 63 6f 72 64 73 20 61 72 65 20 70 75 73 Records are pus
3550: 68 65 64 20 74 6f 20 74 68 65 20 73 65 72 76 65 hed to the serve
3560: 72 20 69 66 20 70 75 73 68 46 6c 61 67 20 69 73 r if pushFlag is
3570: 20 74 72 75 65 2e 20 20 52 65 63 6f 72 64 73 0a true. Records.
3580: 2a 2a 20 61 72 65 20 70 75 6c 6c 65 64 20 69 66 ** are pulled if
3590: 20 70 75 6c 6c 46 6c 61 67 20 69 73 20 74 72 75 pullFlag is tru
35a0: 65 2e 20 20 41 20 66 75 6c 6c 20 73 79 6e 63 20 e. A full sync
35b0: 6f 63 63 75 72 73 20 69 66 20 62 6f 74 68 20 61 occurs if both a
35c0: 72 65 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 76 re.** true..*/.v
35d0: 6f 69 64 20 63 6c 69 65 6e 74 5f 73 79 6e 63 28 oid client_sync(
35e0: 69 6e 74 20 70 75 73 68 46 6c 61 67 2c 20 69 6e int pushFlag, in
35f0: 74 20 70 75 6c 6c 46 6c 61 67 2c 20 69 6e 74 20 t pullFlag, int
3600: 63 6c 6f 6e 65 46 6c 61 67 29 7b 0a 20 20 69 6e cloneFlag){. in
3610: 74 20 67 6f 20 3d 20 31 3b 20 20 20 20 20 20 20 t go = 1;
3620: 20 2f 2a 20 4c 6f 6f 70 20 75 6e 74 69 6c 20 7a /* Loop until z
3630: 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f ero */. int nTo
3640: 6b 65 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 ken;. const cha
3650: 72 20 2a 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 r *zSCode = db_g
3660: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 et("server-code"
3670: 2c 20 22 78 22 29 3b 0a 20 20 63 6f 6e 73 74 20 , "x");. const
3680: 63 68 61 72 20 2a 7a 50 43 6f 64 65 20 3d 20 64 char *zPCode = d
3690: 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 b_get("project-c
36a0: 6f 64 65 22 2c 20 30 29 3b 0a 20 20 69 6e 74 20 ode", 0);. int
36b0: 6e 46 69 6c 65 20 3d 20 30 3b 0a 20 20 69 6e 74 nFile = 0;. int
36c0: 20 6e 4d 73 67 20 3d 20 30 3b 0a 20 20 69 6e 74 nMsg = 0;. int
36d0: 20 6e 52 65 71 20 3d 20 30 3b 0a 20 20 42 6c 6f nReq = 0;. Blo
36e0: 62 20 73 65 6e 64 3b 20 20 20 20 20 20 20 20 2f b send; /
36f0: 2a 20 54 65 78 74 20 77 65 20 61 72 65 20 73 65 * Text we are se
3700: 6e 64 69 6e 67 20 74 6f 20 74 68 65 20 73 65 72 nding to the ser
3710: 76 65 72 20 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 ver */. Blob re
3720: 63 76 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 cv; /* Re
3730: 70 6c 79 20 77 65 20 67 6f 74 20 62 61 63 6b 20 ply we got back
3740: 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65 72 20 from the server
3750: 2a 2f 0a 20 20 42 6c 6f 62 20 6c 69 6e 65 3b 20 */. Blob line;
3760: 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 /* A sing
3770: 6c 65 20 6c 69 6e 65 20 6f 66 20 74 68 65 20 72 le line of the r
3780: 65 70 6c 79 20 2a 2f 0a 20 20 42 6c 6f 62 20 61 eply */. Blob a
3790: 54 6f 6b 65 6e 5b 35 5d 3b 20 20 20 2f 2a 20 41 Token[5]; /* A
37a0: 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 6f 66 tokenization of
37b0: 20 6c 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 line */. Blob
37c0: 65 72 72 6d 73 67 3b 20 20 20 20 20 20 2f 2a 20 errmsg; /*
37d0: 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f Error message */
37e0: 0a 0a 20 20 61 73 73 65 72 74 28 20 70 75 73 68 .. assert( push
37f0: 46 6c 61 67 20 7c 7c 20 70 75 6c 6c 46 6c 61 67 Flag || pullFlag
3800: 20 7c 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29 3b || cloneFlag );
3810: 0a 20 20 61 73 73 65 72 74 28 20 21 67 2e 75 72 . assert( !g.ur
3820: 6c 49 73 46 69 6c 65 20 29 3b 20 20 20 20 20 20 lIsFile );
3830: 20 20 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 /* This only
3840: 20 77 6f 72 6b 73 20 66 6f 72 20 6e 65 74 77 6f works for netwo
3850: 72 6b 69 6e 67 20 2a 2f 0a 0a 20 20 64 62 5f 62 rking */.. db_b
3860: 65 67 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e egin_transaction
3870: 28 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 ();. db_multi_e
3880: 78 65 63 28 0a 20 20 20 20 2f 2a 20 52 65 63 6f xec(. /* Reco
3890: 72 64 73 20 77 68 69 63 68 20 77 65 20 6b 6e 6f rds which we kno
38a0: 77 20 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 w the other side
38b0: 20 61 6c 73 6f 20 68 61 73 20 2a 2f 0a 20 20 20 also has */.
38c0: 20 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 "CREATE TEMP TA
38d0: 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 BLE onremote(rid
38e0: 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 INTEGER PRIMARY
38f0: 20 4b 45 59 29 3b 22 0a 20 20 20 20 2f 2a 20 52 KEY);". /* R
3900: 65 63 6f 72 64 73 20 77 65 20 6b 6e 6f 77 20 74 ecords we know t
3910: 68 65 20 6f 74 68 65 72 20 73 69 64 65 20 6e 65 he other side ne
3920: 65 64 73 20 2a 2f 0a 20 20 20 20 22 43 52 45 41 eds */. "CREA
3930: 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 70 65 TE TEMP TABLE pe
3940: 6e 64 69 6e 67 28 72 69 64 20 49 4e 54 45 47 45 nding(rid INTEGE
3950: 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 R PRIMARY KEY);"
3960: 0a 20 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 . );. blobarra
3970: 79 5f 7a 65 72 6f 28 61 54 6f 6b 65 6e 2c 20 63 y_zero(aToken, c
3980: 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 20 ount(aToken));.
3990: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 73 65 6e 64 blob_zero(&send
39a0: 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 );. blob_zero(&
39b0: 72 65 63 76 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 recv);. blob_ze
39c0: 72 6f 28 26 65 72 72 6d 73 67 29 3b 0a 0a 0a 20 ro(&errmsg);...
39d0: 20 77 68 69 6c 65 28 20 67 6f 20 29 7b 0a 20 20 while( go ){.
39e0: 20 20 67 6f 20 3d 20 30 3b 0a 20 20 20 20 6e 46 go = 0;. nF
39f0: 69 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 ile = nReq = nMs
3a00: 67 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20 47 g = 0;.. /* G
3a10: 65 6e 65 72 61 74 65 20 61 20 72 65 71 75 65 73 enerate a reques
3a20: 74 20 74 6f 20 62 65 20 73 65 6e 74 20 74 6f 20 t to be sent to
3a30: 74 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 the server..
3a40: 2a 2a 20 41 6c 77 61 79 73 20 62 65 67 69 6e 20 ** Always begin
3a50: 77 69 74 68 20 61 20 63 6c 6f 6e 65 2c 20 70 75 with a clone, pu
3a60: 6c 6c 2c 20 6f 72 20 70 75 73 68 20 6d 65 73 73 ll, or push mess
3a70: 61 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 0a age. */. .
3a80: 20 20 20 20 69 66 28 20 63 6c 6f 6e 65 46 6c 61 if( cloneFla
3a90: 67 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62 5f g ){. blob_
3aa0: 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 appendf(&send, "
3ab0: 63 6c 6f 6e 65 5c 6e 22 29 3b 0a 20 20 20 20 20 clone\n");.
3ac0: 20 70 75 73 68 46 6c 61 67 20 3d 20 30 3b 0a 20 pushFlag = 0;.
3ad0: 20 20 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 pullFlag =
3ae0: 30 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0;. nMsg++;
3af0: 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 . }else if( p
3b00: 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 ullFlag ){.
3b10: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 blob_appendf(&s
3b20: 65 6e 64 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 end, "pull %s %s
3b30: 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 \n", zSCode, zPC
3b40: 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 4d 73 67 ode);. nMsg
3b50: 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 ++;. }. if
3b60: 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 ( pushFlag ){.
3b70: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 blob_appendf
3b80: 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20 25 73 (&send, "push %s
3b90: 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20 %s\n", zSCode,
3ba0: 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e zPCode);. n
3bb0: 4d 73 67 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 Msg++;. }..
3bc0: 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 if( pullFlag )
3bd0: 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e 64 20 {. /* Send
3be0: 67 69 6d 6d 65 20 6d 65 73 73 61 67 65 20 66 6f gimme message fo
3bf0: 72 20 65 76 65 72 79 20 70 68 61 6e 74 6f 6d 20 r every phantom
3c00: 74 68 61 74 20 77 65 20 68 6f 6c 64 2e 0a 20 20 that we hold..
3c10: 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 53 74 6d */. Stm
3c20: 74 20 71 3b 0a 20 20 20 20 20 20 64 62 5f 70 72 t q;. db_pr
3c30: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43 epare(&q, "SELEC
3c40: 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 T uuid FROM blob
3c50: 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22 29 3b WHERE size<0");
3c60: 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 64 62 . while( db
3c70: 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 _step(&q)==SQLIT
3c80: 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 20 E_ROW ){.
3c90: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75 const char *zUu
3ca0: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 id = db_column_t
3cb0: 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 ext(&q, 0);.
3cc0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 blob_appendf
3cd0: 28 26 73 65 6e 64 2c 22 67 69 6d 6d 65 20 25 73 (&send,"gimme %s
3ce0: 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 \n", zUuid);.
3cf0: 20 20 20 20 20 6e 52 65 71 2b 2b 3b 0a 20 20 20 nReq++;.
3d00: 20 20 20 7d 0a 20 20 20 20 20 20 64 62 5f 66 69 }. db_fi
3d10: 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 20 20 nalize(&q);.
3d20: 7d 0a 0a 20 20 20 20 69 66 28 20 70 75 73 68 46 }.. if( pushF
3d30: 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 lag ){. /*
3d40: 53 65 6e 64 20 74 68 65 20 73 65 72 76 65 72 20 Send the server
3d50: 61 6e 79 20 66 69 6c 65 73 20 74 68 61 74 20 74 any files that t
3d60: 68 65 20 73 65 72 76 65 72 20 68 61 73 20 72 65 he server has re
3d70: 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 20 20 20 quested */.
3d80: 20 6e 46 69 6c 65 20 2b 3d 20 73 65 6e 64 5f 61 nFile += send_a
3d90: 6c 6c 5f 70 65 6e 64 69 6e 67 28 26 73 65 6e 64 ll_pending(&send
3da0: 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 );. }.. if
3db0: 28 20 70 75 6c 6c 46 6c 61 67 20 7c 7c 20 70 75 ( pullFlag || pu
3dc0: 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 shFlag ){.
3dd0: 2f 2a 20 41 6c 77 61 79 73 20 73 65 6e 64 20 6f /* Always send o
3de0: 75 72 20 6c 65 61 76 65 73 20 2a 2f 0a 20 20 20 ur leaves */.
3df0: 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20 20 20 Stmt q;.
3e00: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 db_prepare(&q,
3e10: 0a 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 . "SELEC
3e20: 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62 T uuid FROM blob
3e30: 20 57 48 45 52 45 20 72 69 64 20 49 4e 22 0a 20 WHERE rid IN".
3e40: 20 20 20 20 20 20 20 20 22 20 20 28 53 45 4c 45 " (SELE
3e50: 43 54 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e CT cid FROM plin
3e60: 6b 20 45 58 43 45 50 54 20 53 45 4c 45 43 54 20 k EXCEPT SELECT
3e70: 70 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 29 22 pid FROM plink)"
3e80: 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 . );.
3e90: 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 while( db_step(&
3ea0: 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 q)==SQLITE_ROW )
3eb0: 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 {. const
3ec0: 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 char *zUuid = db
3ed0: 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71 2c _column_text(&q,
3ee0: 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 0);. blo
3ef0: 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c b_appendf(&send,
3f00: 20 22 6c 65 61 66 20 25 73 5c 6e 22 2c 20 7a 55 "leaf %s\n", zU
3f10: 75 69 64 29 3b 0a 20 20 20 20 20 20 20 20 6e 4d uid);. nM
3f20: 73 67 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 sg++;. }.
3f30: 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a 65 28 db_finalize(
3f40: 26 71 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 &q);. }..
3f50: 2f 2a 20 45 78 63 68 61 6e 67 65 20 6d 65 73 73 /* Exchange mess
3f60: 61 67 65 73 20 77 69 74 68 20 74 68 65 20 73 65 ages with the se
3f70: 72 76 65 72 20 2a 2f 0a 20 20 20 20 70 72 69 6e rver */. prin
3f80: 74 66 28 22 53 65 6e 64 3a 20 20 20 20 20 20 25 tf("Send: %
3f90: 64 20 66 69 6c 65 73 2c 20 25 64 20 72 65 71 75 d files, %d requ
3fa0: 65 73 74 73 2c 20 25 64 20 6f 74 68 65 72 20 6d ests, %d other m
3fb0: 65 73 73 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 essages\n",.
3fc0: 20 20 20 20 20 20 20 20 6e 46 69 6c 65 2c 20 6e nFile, n
3fd0: 52 65 71 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20 Req, nMsg);.
3fe0: 6e 46 69 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e nFile = nReq = n
3ff0: 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 68 74 74 Msg = 0;. htt
4000: 70 5f 65 78 63 68 61 6e 67 65 28 26 73 65 6e 64 p_exchange(&send
4010: 2c 20 26 72 65 63 76 29 3b 0a 20 20 20 20 62 6c , &recv);. bl
4020: 6f 62 5f 72 65 73 65 74 28 26 73 65 6e 64 29 3b ob_reset(&send);
4030: 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65 73 73 .. /* Process
4040: 20 74 68 65 20 72 65 70 6c 79 20 74 68 61 74 20 the reply that
4050: 63 61 6d 65 20 62 61 63 6b 20 66 72 6f 6d 20 74 came back from t
4060: 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20 20 he server */.
4070: 20 77 68 69 6c 65 28 20 62 6c 6f 62 5f 6c 69 6e while( blob_lin
4080: 65 28 26 72 65 63 76 2c 20 26 6c 69 6e 65 29 20 e(&recv, &line)
4090: 29 7b 0a 20 20 20 20 20 20 6e 54 6f 6b 65 6e 20 ){. nToken
40a0: 3d 20 62 6c 6f 62 5f 74 6f 6b 65 6e 69 7a 65 28 = blob_tokenize(
40b0: 26 6c 69 6e 65 2c 20 61 54 6f 6b 65 6e 2c 20 63 &line, aToken, c
40c0: 6f 75 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 0a ount(aToken));..
40d0: 20 20 20 20 20 20 2f 2a 20 20 20 66 69 6c 65 20 /* file
40e0: 55 55 49 44 20 53 49 5a 45 20 5c 6e 20 43 4f 4e UUID SIZE \n CON
40f0: 54 45 4e 54 0a 20 20 20 20 20 20 2a 2a 20 20 20 TENT. **
4100: 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41 53 file UUID DELTAS
4110: 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 RC SIZE \n CONTE
4120: 4e 54 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 NT. **.
4130: 20 20 2a 2a 20 52 65 63 65 69 76 65 20 61 20 66 ** Receive a f
4140: 69 6c 65 20 74 72 61 6e 73 6d 69 74 74 65 64 20 ile transmitted
4150: 66 72 6f 6d 20 74 68 65 20 6f 74 68 65 72 20 73 from the other s
4160: 69 64 65 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 ide. */.
4170: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 if( blob_eq(&
4180: 61 54 6f 6b 65 6e 5b 30 5d 2c 22 66 69 6c 65 22 aToken[0],"file"
4190: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 78 66 65 ) ){. xfe
41a0: 72 5f 61 63 63 65 70 74 5f 66 69 6c 65 28 26 72 r_accept_file(&r
41b0: 65 63 76 2c 20 61 54 6f 6b 65 6e 2c 20 6e 54 6f ecv, aToken, nTo
41c0: 6b 65 6e 2c 20 26 65 72 72 6d 73 67 29 3b 0a 20 ken, &errmsg);.
41d0: 20 20 20 20 20 20 20 6e 46 69 6c 65 2b 2b 3b 0a nFile++;.
41e0: 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a go = 1;.
41f0: 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 }else..
4200: 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 /* gimme UU
4210: 49 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 ID. **.
4220: 20 20 2a 2a 20 53 65 72 76 65 72 20 69 73 20 72 ** Server is r
4230: 65 71 75 65 73 74 69 6e 67 20 61 20 66 69 6c 65 equesting a file
4240: 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 . */.
4250: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f if( blob_eq(&aTo
4260: 6b 65 6e 5b 30 5d 2c 20 22 67 69 6d 6d 65 22 29 ken[0], "gimme")
4270: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20 20 && nToken==2.
4280: 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20 &&
4290: 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 blob_is_uuid(&aT
42a0: 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20 20 20 oken[1]) ){.
42b0: 20 20 20 20 6e 52 65 71 2b 2b 3b 0a 20 20 20 20 nReq++;.
42c0: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 if( pushFlag
42d0: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 ){. db
42e0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 _multi_exec(.
42f0: 20 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 "INSERT
4300: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 OR IGNORE INTO
4310: 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 pending(rid) ".
4320: 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45 "SELE
4330: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 CT rid FROM blob
4340: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 20 41 WHERE uuid=%B A
4350: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 ND size>=0", &aT
4360: 6f 6b 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 oken[1].
4370: 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 67 );. g
4380: 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d o = 1;. }
4390: 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a . }else. .
43a0: 20 20 20 20 20 20 2f 2a 20 20 20 69 67 6f 74 20 /* igot
43b0: 55 55 49 44 0a 20 20 20 20 20 20 2a 2a 20 20 20 UUID. **
43c0: 6c 65 61 66 20 55 55 49 44 0a 20 20 20 20 20 20 leaf UUID.
43d0: 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76 **. ** Serv
43e0: 65 72 20 70 72 6f 63 6c 61 69 6d 73 20 74 68 61 er proclaims tha
43f0: 74 20 69 74 20 68 61 73 20 61 20 70 61 72 74 69 t it has a parti
4400: 63 75 6c 61 72 20 66 69 6c 65 2e 20 20 41 20 6c cular file. A l
4410: 65 61 66 20 6d 65 73 73 61 67 65 0a 20 20 20 20 eaf message.
4420: 20 20 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 ** means that
4430: 74 68 65 20 66 69 6c 65 20 69 73 20 61 20 6c 65 the file is a le
4440: 61 66 20 6d 61 6e 69 66 65 73 74 20 6f 6e 20 74 af manifest on t
4450: 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 20 he server..
4460: 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 6e 54 */. if( nT
4470: 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20 20 oken==2.
4480: 20 20 20 20 20 20 26 26 20 28 62 6c 6f 62 5f 65 && (blob_e
4490: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 69 q(&aToken[0], "i
44a0: 67 6f 74 22 29 20 7c 7c 20 62 6c 6f 62 5f 65 71 got") || blob_eq
44b0: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 (&aToken[0], "le
44c0: 61 66 22 29 29 0a 20 20 20 20 20 20 20 20 20 20 af")).
44d0: 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 && blob_is_u
44e0: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 uid(&aToken[1])
44f0: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 ){. int r
4500: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 id = db_int(0, "
4510: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 SELECT rid FROM
4520: 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d blob WHERE uuid=
4530: 25 42 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 %B", &aToken[1])
4540: 3b 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b ;. nMsg++
4550: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 69 ;. if( ri
4560: 64 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 d>0 ){.
4570: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a db_multi_exec(.
4580: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 "INS
4590: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e ERT OR IGNORE IN
45a0: 54 4f 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29 TO onremote(rid)
45b0: 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69 VALUES(%d)", ri
45c0: 64 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 d. );.
45d0: 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 20 /* Add
45e0: 74 6f 20 74 68 65 20 70 65 6e 64 69 6e 67 20 73 to the pending s
45f0: 65 74 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 et all children
4600: 6f 66 20 74 68 65 20 73 65 72 76 65 72 27 73 20 of the server's
4610: 6c 65 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 20 leaves */.
4620: 20 20 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 if( pushFlag
4630: 20 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f && blob_eq(&aTo
4640: 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 ken[0], "leaf")
4650: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 ){. d
4660: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 b_multi_exec(.
4670: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 "INS
4680: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e ERT OR IGNORE IN
4690: 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 TO pending(rid)
46a0: 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 ".
46b0: 22 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d "SELECT cid FROM
46c0: 20 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64 plink WHERE pid
46d0: 3d 25 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20 =%d", rid.
46e0: 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 );.
46f0: 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 go = 1;.
4700: 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 }.
4710: 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20 if( pullFlag
4720: 26 26 20 21 67 6f 20 26 26 20 0a 20 20 20 20 20 && !go && .
4730: 20 20 20 20 20 20 20 20 20 64 62 5f 65 78 69 73 db_exis
4740: 74 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f ts("SELECT 1 FRO
4750: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64 M blob WHERE rid
4760: 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22 =%d AND size>=0"
4770: 2c 20 72 69 64 29 20 29 7b 0a 20 20 20 20 20 20 , rid) ){.
4780: 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 go = 1;.
4790: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
47a0: 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c }else if( pull
47b0: 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 Flag ){.
47c0: 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 go = 1;.
47d0: 20 20 20 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 content_put(
47e0: 30 2c 20 62 6c 6f 62 5f 73 74 72 28 26 61 54 6f 0, blob_str(&aTo
47f0: 6b 65 6e 5b 31 5d 29 29 3b 0a 20 20 20 20 20 20 ken[1]));.
4800: 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a }. }else.
4810: 20 20 0a 20 20 20 20 20 20 2f 2a 20 20 20 70 75 . /* pu
4820: 73 68 20 20 53 45 52 56 45 52 43 4f 44 45 20 20 sh SERVERCODE
4830: 50 52 4f 44 55 43 54 43 4f 44 45 0a 20 20 20 20 PRODUCTCODE.
4840: 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 68 **. ** Sh
4850: 6f 75 6c 64 20 6f 6e 6c 79 20 68 61 70 70 65 6e ould only happen
4860: 20 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 in response to
4870: 61 20 63 6c 6f 6e 65 2e 0a 20 20 20 20 20 20 2a a clone.. *
4880: 2f 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 /. if( blob
4890: 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 _eq(&aToken[0],"
48a0: 70 75 73 68 22 29 20 26 26 20 6e 54 6f 6b 65 6e push") && nToken
48b0: 3d 3d 33 20 26 26 20 63 6c 6f 6e 65 46 6c 61 67 ==3 && cloneFlag
48c0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 . &
48d0: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 & blob_is_uuid(&
48e0: 61 54 6f 6b 65 6e 5b 31 5d 29 20 26 26 20 62 6c aToken[1]) && bl
48f0: 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b ob_is_uuid(&aTok
4900: 65 6e 5b 32 5d 29 20 29 7b 0a 0a 20 20 20 20 20 en[2]) ){..
4910: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 5f 73 if( blob_eq_s
4920: 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 7a tr(&aToken[1], z
4930: 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20 SCode, -1) ){.
4940: 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 fossil_f
4950: 61 74 61 6c 28 22 73 65 72 76 65 72 20 6c 6f 6f atal("server loo
4960: 70 22 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 p");. }.
4970: 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 nMsg++;.
4980: 20 20 20 20 20 20 20 69 66 28 20 7a 50 43 6f 64 if( zPCod
4990: 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 e==0 ){.
49a0: 20 20 7a 50 43 6f 64 65 20 3d 20 6d 70 72 69 6e zPCode = mprin
49b0: 74 66 28 22 25 62 22 2c 20 26 61 54 6f 6b 65 6e tf("%b", &aToken
49c0: 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 20 20 [2]);.
49d0: 64 62 5f 73 65 74 28 22 70 72 6f 6a 65 63 74 2d db_set("project-
49e0: 63 6f 64 65 22 2c 20 7a 50 43 6f 64 65 29 3b 0a code", zPCode);.
49f0: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 }.
4a00: 20 20 63 6c 6f 6e 65 46 6c 61 67 20 3d 20 30 3b cloneFlag = 0;
4a10: 0a 20 20 20 20 20 20 20 20 70 75 6c 6c 46 6c 61 . pullFla
4a20: 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c g = 1;. }el
4a30: 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 65 se.. /* e
4a40: 72 72 6f 72 20 4d 45 53 53 41 47 45 0a 20 20 20 rror MESSAGE.
4a50: 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 52 **. ** R
4a60: 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 0a 20 eport an error.
4a70: 20 20 20 20 20 2a 2f 20 20 20 20 20 20 20 20 0a */ .
4a80: 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 if( blob_e
4a90: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22 65 72 q(&aToken[0],"er
4aa0: 72 6f 72 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d ror") && nToken=
4ab0: 3d 32 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 =2 ){. ch
4ac0: 61 72 20 2a 7a 4d 73 67 20 3d 20 62 6c 6f 62 5f ar *zMsg = blob_
4ad0: 74 65 72 6d 69 6e 61 74 65 28 26 61 54 6f 6b 65 terminate(&aToke
4ae0: 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 64 n[1]);. d
4af0: 65 66 6f 73 73 69 6c 69 7a 65 28 7a 4d 73 67 29 efossilize(zMsg)
4b00: 3b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 ;. blob_a
4b10: 70 70 65 6e 64 66 28 26 65 72 72 6d 73 67 2c 20 ppendf(&errmsg,
4b20: 22 73 65 72 76 65 72 20 73 61 79 73 3a 20 25 73 "server says: %s
4b30: 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 ", zMsg);.
4b40: 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 }else.. /*
4b50: 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65 20 Unknown message
4b60: 2a 2f 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 */. {.
4b70: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 blob_appendf(
4b80: 26 65 72 72 6d 73 67 2c 20 22 75 6e 6b 6e 6f 77 &errmsg, "unknow
4b90: 6e 20 63 6f 6d 6d 61 6e 64 3a 20 25 62 22 2c 20 n command: %b",
4ba0: 26 61 54 6f 6b 65 6e 5b 30 5d 29 3b 0a 20 20 20 &aToken[0]);.
4bb0: 20 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 }.. if(
4bc0: 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 72 6d 73 blob_size(&errms
4bd0: 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f g) ){. fo
4be0: 73 73 69 6c 5f 66 61 74 61 6c 28 22 25 62 22 2c ssil_fatal("%b",
4bf0: 20 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 &errmsg);.
4c00: 20 7d 0a 20 20 20 20 20 20 62 6c 6f 62 61 72 72 }. blobarr
4c10: 61 79 5f 72 65 73 65 74 28 61 54 6f 6b 65 6e 2c ay_reset(aToken,
4c20: 20 6e 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 7d 0a nToken);. }.
4c30: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 blob_reset(&
4c40: 72 65 63 76 29 3b 0a 20 20 20 20 70 72 69 6e 74 recv);. print
4c50: 66 28 22 52 65 63 65 69 76 65 64 3a 20 20 25 64 f("Received: %d
4c60: 20 66 69 6c 65 73 2c 20 25 64 20 72 65 71 75 65 files, %d reque
4c70: 73 74 73 2c 20 25 64 20 6f 74 68 65 72 20 6d 65 sts, %d other me
4c80: 73 73 61 67 65 73 5c 6e 22 2c 0a 20 20 20 20 20 ssages\n",.
4c90: 20 20 20 20 20 20 20 6e 46 69 6c 65 2c 20 6e 52 nFile, nR
4ca0: 65 71 2c 20 6e 4d 73 67 29 3b 0a 20 20 20 20 6e eq, nMsg);. n
4cb0: 46 69 6c 65 20 3d 20 6e 52 65 71 20 3d 20 6e 4d File = nReq = nM
4cc0: 73 67 20 3d 20 30 3b 0a 20 20 7d 3b 0a 20 20 68 sg = 0;. };. h
4cd0: 74 74 70 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 64 ttp_close();. d
4ce0: 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f b_end_transactio
4cf0: 6e 28 30 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 n(0);. db_multi
4d00: 5f 65 78 65 63 28 0a 20 20 20 20 22 44 52 4f 50 _exec(. "DROP
4d10: 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f 74 65 3b TABLE onremote;
4d20: 22 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c ". "DROP TABL
4d30: 45 20 70 65 6e 64 69 6e 67 3b 22 0a 20 20 29 3b E pending;". );
4d40: 0a 7d 0a .}.