Hex Artifact Content
Not logged in

Artifact 1d5b7d85e69e4e4857a16b9abfd083a8eef5f186:

File src/xfer.c part of check-in [0feed85090] - Always do another sync round if any file is received. by drh on 2007-07-23 20:42:40.

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 73 65 6e 74 20 3d  t){.  int sent =
0f20: 20 30 3b 0a 20 20 69 6e 74 20 6e 53 65 6e 74 20   0;.  int nSent 
0f30: 3d 20 30 3b 0a 20 20 69 6e 74 20 6d 61 78 53 69  = 0;.  int maxSi
0f40: 7a 65 20 3d 20 64 62 5f 67 65 74 5f 69 6e 74 28  ze = db_get_int(
0f50: 22 68 74 74 70 2d 6d 73 67 2d 73 69 7a 65 22 2c  "http-msg-size",
0f60: 20 31 30 30 30 30 30 30 29 3b 0a 20 20 53 74 6d   1000000);.  Stm
0f70: 74 20 71 3b 0a 23 69 66 20 30 0a 20 20 64 62 5f  t q;.#if 0.  db_
0f80: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
0f90: 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42  "CREATE TEMP TAB
0fa0: 4c 45 20 70 72 69 6f 72 69 74 79 28 72 69 64 20  LE priority(rid 
0fb0: 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
0fc0: 4b 45 59 29 3b 22 0a 20 20 20 20 22 49 4e 53 45  KEY);".    "INSE
0fd0: 52 54 20 49 4e 54 4f 20 70 72 69 6f 72 69 74 79  RT INTO priority
0fe0: 22 0a 20 20 20 20 22 20 53 45 4c 45 43 54 20 73  ".    " SELECT s
0ff0: 72 63 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 22  rcid FROM delta"
1000: 0a 20 20 20 20 22 20 20 57 48 45 52 45 20 45 58  .    "  WHERE EX
1010: 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46 52  ISTS(SELECT 1 FR
1020: 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45 52  OM onremote WHER
1030: 45 20 6f 6e 72 65 6d 6f 74 65 2e 72 69 64 3d 64  E onremote.rid=d
1040: 65 6c 74 61 2e 72 69 64 29 22 0a 20 20 20 20 22  elta.rid)".    "
1050: 20 20 20 20 41 4e 44 20 45 58 49 53 54 53 28 53      AND EXISTS(S
1060: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 70 65 6e  ELECT 1 FROM pen
1070: 64 69 6e 67 20 57 48 45 52 45 20 64 65 6c 74 61  ding WHERE delta
1080: 2e 73 72 63 69 64 3d 70 65 6e 64 69 6e 67 2e 72  .srcid=pending.r
1090: 69 64 29 3b 22 0a 20 20 20 20 22 49 4e 53 45 52  id);".    "INSER
10a0: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f  T OR IGNORE INTO
10b0: 20 70 72 69 6f 72 69 74 79 22 0a 20 20 20 20 22   priority".    "
10c0: 20 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d   SELECT rid FROM
10d0: 20 64 65 6c 74 61 22 0a 20 20 20 20 22 20 20 57   delta".    "  W
10e0: 48 45 52 45 20 45 58 49 53 54 53 28 53 45 4c 45  HERE EXISTS(SELE
10f0: 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f  CT 1 FROM onremo
1100: 74 65 20 57 48 45 52 45 20 6f 6e 72 65 6d 6f 74  te WHERE onremot
1110: 65 2e 72 69 64 3d 64 65 6c 74 61 2e 73 72 63 69  e.rid=delta.srci
1120: 64 29 22 0a 20 20 20 20 22 20 20 20 20 41 4e 44  d)".    "    AND
1130: 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31   EXISTS(SELECT 1
1140: 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67 20 57 48   FROM pending WH
1150: 45 52 45 20 64 65 6c 74 61 2e 72 69 64 3d 70 65  ERE delta.rid=pe
1160: 6e 64 69 6e 67 2e 72 69 64 29 3b 22 0a 20 20 29  nding.rid);".  )
1170: 3b 0a 20 20 77 68 69 6c 65 28 20 73 65 6e 74 3c  ;.  while( sent<
1180: 6d 61 78 53 69 7a 65 20 26 26 20 28 72 69 64 20  maxSize && (rid 
1190: 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c  = db_int(0, "SEL
11a0: 45 43 54 20 72 69 64 20 46 52 4f 4d 20 70 72 69  ECT rid FROM pri
11b0: 6f 72 69 74 79 22 29 29 21 3d 30 20 29 7b 0a 20  ority"))!=0 ){. 
11c0: 20 20 20 73 65 6e 74 20 2b 3d 20 73 65 6e 64 5f     sent += send_
11d0: 66 69 6c 65 28 72 69 64 2c 20 70 4f 75 74 29 3b  file(rid, pOut);
11e0: 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78  .    db_multi_ex
11f0: 65 63 28 0a 20 20 20 20 20 20 22 49 4e 53 45 52  ec(.      "INSER
1200: 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f  T OR IGNORE INTO
1210: 20 70 72 69 6f 72 69 74 79 22 0a 20 20 20 20 20   priority".     
1220: 20 22 20 53 45 4c 45 43 54 20 73 72 63 69 64 20   " SELECT srcid 
1230: 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45  FROM delta WHERE
1240: 20 72 69 64 3d 25 64 22 0a 20 20 20 20 20 20 22   rid=%d".      "
1250: 20 55 4e 49 4f 4e 20 41 4c 4c 22 0a 20 20 20 20   UNION ALL".    
1260: 20 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 46    " SELECT rid F
1270: 52 4f 4d 20 64 65 6c 74 61 20 57 48 45 52 45 20  ROM delta WHERE 
1280: 73 72 63 69 64 3d 25 64 22 2c 0a 20 20 20 20 20  srcid=%d",.     
1290: 20 72 69 64 2c 20 72 69 64 0a 20 20 20 20 29 3b   rid, rid.    );
12a0: 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 64 62  .  }.#endif.  db
12b0: 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45  _prepare(&q, "SE
12c0: 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20 70 65  LECT rid FROM pe
12d0: 6e 64 69 6e 67 22 29 3b 0a 20 20 77 68 69 6c 65  nding");.  while
12e0: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
12f0: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
1300: 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 63 6f   int rid = db_co
1310: 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 30 29 3b  lumn_int(&q, 0);
1320: 0a 20 20 20 20 69 66 28 20 73 65 6e 74 3c 6d 61  .    if( sent<ma
1330: 78 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 73  xSize ){.      s
1340: 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66 69 6c 65  ent += send_file
1350: 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a 20 20 20  (rid, pOut);.   
1360: 20 20 20 6e 53 65 6e 74 2b 2b 3b 0a 20 20 20 20     nSent++;.    
1370: 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 61  }else{.      cha
1380: 72 20 2a 7a 55 75 69 64 20 3d 20 64 62 5f 74 65  r *zUuid = db_te
1390: 78 74 28 30 2c 0a 20 20 20 20 20 20 20 20 20 20  xt(0,.          
13a0: 20 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c              "SEL
13b0: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
13c0: 6f 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 20  ob WHERE rid=%d 
13d0: 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69  AND size>=0", ri
13e0: 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 55  d);.      if( zU
13f0: 75 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 69  uid ){.        i
1400: 66 28 20 70 4f 75 74 20 29 7b 0a 20 20 20 20 20  f( pOut ){.     
1410: 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64       blob_append
1420: 66 28 70 4f 75 74 2c 20 22 69 67 6f 74 20 25 73  f(pOut, "igot %s
1430: 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20  \n", zUuid);.   
1440: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1450: 20 20 20 20 20 20 63 67 69 5f 70 72 69 6e 74 66        cgi_printf
1460: 28 22 69 67 6f 74 20 25 73 5c 6e 22 2c 20 7a 55  ("igot %s\n", zU
1470: 75 69 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  uid);.        }.
1480: 20 20 20 20 20 20 20 20 66 72 65 65 28 7a 55 75          free(zUu
1490: 69 64 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  id);.      }.   
14a0: 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61   }.  }.  db_fina
14b0: 6c 69 7a 65 28 26 71 29 3b 0a 20 20 0a 23 69 66  lize(&q);.  .#if
14c0: 20 30 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78   0.  db_multi_ex
14d0: 65 63 28 22 44 52 4f 50 20 54 41 42 4c 45 20 70  ec("DROP TABLE p
14e0: 72 69 6f 72 69 74 79 22 29 3b 0a 23 65 6e 64 69  riority");.#endi
14f0: 66 0a 20 20 72 65 74 75 72 6e 20 6e 53 65 6e 74  f.  return nSent
1500: 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  ;.}.../*.** Chec
1510: 6b 20 74 68 65 20 73 69 67 6e 61 74 75 72 65 20  k the signature 
1520: 6f 6e 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  on an applicatio
1530: 6e 2f 78 2d 66 6f 73 73 69 6c 20 70 61 79 6c 6f  n/x-fossil paylo
1540: 61 64 20 72 65 63 65 69 76 65 64 20 62 79 0a 2a  ad received by.*
1550: 2a 20 74 68 65 20 48 54 54 50 20 73 65 72 76 65  * the HTTP serve
1560: 72 2e 20 20 54 68 65 20 73 69 67 6e 61 74 75 72  r.  The signatur
1570: 65 20 69 73 20 61 20 6c 69 6e 65 20 6f 66 20 74  e is a line of t
1580: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72  he following for
1590: 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  m:.**.**        
15a0: 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20 4e 4f 4e 43  login LOGIN NONC
15b0: 45 20 53 49 47 4e 41 54 55 52 45 0a 2a 2a 0a 2a  E SIGNATURE.**.*
15c0: 2a 20 54 68 65 20 4e 4f 4e 43 45 20 69 73 20 61  * The NONCE is a
15d0: 20 72 61 6e 64 6f 6d 20 73 74 72 69 6e 67 2e 20   random string. 
15e0: 20 54 68 65 20 73 65 72 76 65 72 20 77 69 6c 6c   The server will
15f0: 20 6e 65 76 65 72 20 61 63 63 65 70 74 20 61 0a   never accept a.
1600: 2a 2a 20 72 65 70 65 61 74 20 4e 4f 4e 43 45 2e  ** repeat NONCE.
1610: 20 20 53 49 47 4e 41 54 55 52 45 20 69 73 20 74    SIGNATURE is t
1620: 68 65 20 53 48 41 31 20 63 68 65 63 6b 73 75 6d  he SHA1 checksum
1630: 20 6f 66 20 74 68 65 20 4e 4f 4e 43 45 20 0a 2a   of the NONCE .*
1640: 2a 20 63 6f 6e 63 61 74 65 6e 61 74 65 64 20 77  * concatenated w
1650: 69 74 68 20 74 68 65 20 75 73 65 72 73 20 70 61  ith the users pa
1660: 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  ssword..**.** Th
1670: 65 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20  e parameters to 
1680: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 72 65  this routine are
1690: 20 65 70 68 65 72 6d 65 72 61 6c 20 62 6c 6f 62   ephermeral blob
16a0: 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a  s holding the.**
16b0: 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43 45 20 61 6e   LOGIN, NONCE an
16c0: 64 20 53 49 47 4e 41 54 55 52 45 2e 0a 2a 2a 0a  d SIGNATURE..**.
16d0: 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
16e0: 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 6f 63 61  attempts to loca
16f0: 74 65 20 74 68 65 20 75 73 65 72 20 61 6e 64 20  te the user and 
1700: 76 65 72 69 66 79 20 74 68 65 20 73 69 67 6e 61  verify the signa
1710: 74 75 72 65 2e 0a 2a 2a 20 49 66 20 65 76 65 72  ture..** If ever
1720: 79 74 68 69 6e 67 20 63 68 65 63 6b 73 20 6f 75  ything checks ou
1730: 74 2c 20 74 68 65 20 55 53 45 52 2e 43 41 50 20  t, the USER.CAP 
1740: 63 6f 6c 75 6d 6e 20 66 6f 72 20 74 68 65 20 55  column for the U
1750: 53 45 52 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20  SER table.** is 
1760: 63 6f 6e 73 75 6c 74 65 64 20 74 6f 20 73 65 74  consulted to set
1770: 20 70 72 69 76 69 6c 65 67 65 73 20 69 6e 20 74   privileges in t
1780: 68 65 20 67 6c 6f 62 61 6c 20 67 20 76 61 72 69  he global g vari
1790: 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  able..**.** If a
17a0: 6e 79 74 68 69 6e 67 20 66 61 69 6c 73 20 74 6f  nything fails to
17b0: 20 63 68 65 63 6b 20 6f 75 74 2c 20 6e 6f 20 63   check out, no c
17c0: 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64 65 20  hanges are made 
17d0: 74 6f 20 70 72 69 76 69 6c 65 67 65 73 2e 0a 2a  to privileges..*
17e0: 2a 0a 2a 2a 20 53 69 67 6e 61 74 75 72 65 20 67  *.** Signature g
17f0: 65 6e 65 72 61 74 69 6f 6e 20 6f 6e 20 74 68 65  eneration on the
1800: 20 63 6c 69 65 6e 74 20 73 69 64 65 20 69 73 20   client side is 
1810: 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 20 0a  handled by the .
1820: 2a 2a 20 68 74 74 70 5f 65 78 63 68 61 6e 67 65  ** http_exchange
1830: 28 29 20 72 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 76  () routine..*/.v
1840: 6f 69 64 20 63 68 65 63 6b 5f 6c 6f 67 69 6e 28  oid check_login(
1850: 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e 2c 20 42 6c  Blob *pLogin, Bl
1860: 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20 42 6c 6f 62  ob *pNonce, Blob
1870: 20 2a 70 53 69 67 29 7b 0a 20 20 53 74 6d 74 20   *pSig){.  Stmt 
1880: 71 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  q;.  int rc;..  
1890: 69 66 28 20 64 62 5f 65 78 69 73 74 73 28 22 53  if( db_exists("S
18a0: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 72 63 76  ELECT 1 FROM rcv
18b0: 66 72 6f 6d 20 57 48 45 52 45 20 6e 6f 6e 63 65  from WHERE nonce
18c0: 3d 25 42 22 2c 20 70 4e 6f 6e 63 65 29 20 29 7b  =%B", pNonce) ){
18d0: 0a 20 20 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a  .    return;  /*
18e0: 20 4e 65 76 65 72 20 61 63 63 65 70 74 20 61 20   Never accept a 
18f0: 72 65 70 65 61 74 65 64 20 6e 6f 6e 63 65 20 2a  repeated nonce *
1900: 2f 0a 20 20 7d 0a 20 20 64 62 5f 70 72 65 70 61  /.  }.  db_prepa
1910: 72 65 28 26 71 2c 20 22 53 45 4c 45 43 54 20 70  re(&q, "SELECT p
1920: 77 2c 20 63 61 70 2c 20 75 69 64 20 46 52 4f 4d  w, cap, uid FROM
1930: 20 75 73 65 72 20 57 48 45 52 45 20 6c 6f 67 69   user WHERE logi
1940: 6e 3d 25 42 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a  n=%B", pLogin);.
1950: 20 20 69 66 28 20 64 62 5f 73 74 65 70 28 26 71    if( db_step(&q
1960: 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b  )==SQLITE_ROW ){
1970: 0a 20 20 20 20 42 6c 6f 62 20 70 77 2c 20 63 6f  .    Blob pw, co
1980: 6d 62 69 6e 65 64 2c 20 68 61 73 68 3b 0a 20 20  mbined, hash;.  
1990: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 77 29    blob_zero(&pw)
19a0: 3b 0a 20 20 20 20 64 62 5f 65 70 68 65 6d 65 72  ;.    db_ephemer
19b0: 61 6c 5f 62 6c 6f 62 28 26 71 2c 20 30 2c 20 26  al_blob(&q, 0, &
19c0: 70 77 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65  pw);.    blob_ze
19d0: 72 6f 28 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20  ro(&combined);. 
19e0: 20 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26 63 6f     blob_copy(&co
19f0: 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e 63 65 29 3b  mbined, pNonce);
1a00: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
1a10: 28 26 63 6f 6d 62 69 6e 65 64 2c 20 62 6c 6f 62  (&combined, blob
1a20: 5f 62 75 66 66 65 72 28 26 70 77 29 2c 20 62 6c  _buffer(&pw), bl
1a30: 6f 62 5f 73 69 7a 65 28 26 70 77 29 29 3b 0a 20  ob_size(&pw));. 
1a40: 20 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28     sha1sum_blob(
1a50: 26 63 6f 6d 62 69 6e 65 64 2c 20 26 68 61 73 68  &combined, &hash
1a60: 29 3b 0a 20 20 20 20 72 63 20 3d 20 62 6c 6f 62  );.    rc = blob
1a70: 5f 63 6f 6d 70 61 72 65 28 26 68 61 73 68 2c 20  _compare(&hash, 
1a80: 70 53 69 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  pSig);.    blob_
1a90: 72 65 73 65 74 28 26 68 61 73 68 29 3b 0a 20 20  reset(&hash);.  
1aa0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 63 6f    blob_reset(&co
1ab0: 6d 62 69 6e 65 64 29 3b 0a 20 20 20 20 69 66 28  mbined);.    if(
1ac0: 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   rc==0 ){.      
1ad0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 61 70  const char *zCap
1ae0: 3b 0a 20 20 20 20 20 20 7a 43 61 70 20 3d 20 64  ;.      zCap = d
1af0: 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71  b_column_text(&q
1b00: 2c 20 31 29 3b 0a 20 20 20 20 20 20 6c 6f 67 69  , 1);.      logi
1b10: 6e 5f 73 65 74 5f 63 61 70 61 62 69 6c 69 74 69  n_set_capabiliti
1b20: 65 73 28 7a 43 61 70 29 3b 0a 20 20 20 20 20 20  es(zCap);.      
1b30: 67 2e 75 73 65 72 55 69 64 20 3d 20 64 62 5f 63  g.userUid = db_c
1b40: 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20 32 29  olumn_int(&q, 2)
1b50: 3b 0a 20 20 20 20 20 20 67 2e 7a 4c 6f 67 69 6e  ;.      g.zLogin
1b60: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c   = mprintf("%b",
1b70: 20 70 4c 6f 67 69 6e 29 3b 0a 20 20 20 20 20 20   pLogin);.      
1b80: 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d 70 72 69 6e  g.zNonce = mprin
1b90: 74 66 28 22 25 62 22 2c 20 70 4e 6f 6e 63 65 29  tf("%b", pNonce)
1ba0: 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62  ;.    }.  }.  db
1bb0: 5f 72 65 73 65 74 28 26 71 29 3b 0a 7d 0a 0a 0a  _reset(&q);.}...
1bc0: 2f 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 76 61  /*.** If this va
1bd0: 72 69 61 62 6c 65 20 69 73 20 73 65 74 2c 20 64  riable is set, d
1be0: 69 73 61 62 6c 65 20 6c 6f 67 69 6e 20 63 68 65  isable login che
1bf0: 63 6b 73 2e 20 20 55 73 65 64 20 66 6f 72 20 64  cks.  Used for d
1c00: 65 62 75 67 67 69 6e 67 0a 2a 2a 20 6f 6e 6c 79  ebugging.** only
1c10: 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1c20: 64 69 73 61 62 6c 65 4c 6f 67 69 6e 20 3d 20 30  disableLogin = 0
1c30: 3b 0a 0a 2f 2a 0a 2a 2a 20 57 45 42 50 41 47 45  ;../*.** WEBPAGE
1c40: 3a 20 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69  : xfer.**.** Thi
1c50: 73 20 69 73 20 74 68 65 20 74 72 61 6e 73 66 65  s is the transfe
1c60: 72 20 68 61 6e 64 6c 65 72 20 6f 6e 20 74 68 65  r handler on the
1c70: 20 73 65 72 76 65 72 20 73 69 64 65 2e 20 20 54   server side.  T
1c80: 68 65 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 6d  he transfer.** m
1c90: 65 73 73 61 67 65 20 68 61 73 20 62 65 65 6e 20  essage has been 
1ca0: 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 61 6e 64  uncompressed and
1cb0: 20 70 6c 61 63 65 64 20 69 6e 20 74 68 65 20 67   placed in the g
1cc0: 2e 63 67 69 49 6e 20 62 6c 6f 62 2e 0a 2a 2a 20  .cgiIn blob..** 
1cd0: 50 72 6f 63 65 73 73 20 74 68 69 73 20 6d 65 73  Process this mes
1ce0: 73 61 67 65 20 61 6e 64 20 66 6f 72 6d 20 61 6e  sage and form an
1cf0: 20 61 70 70 72 6f 70 72 69 61 74 65 20 72 65 70   appropriate rep
1d00: 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 70 61 67 65  ly..*/.void page
1d10: 5f 78 66 65 72 28 76 6f 69 64 29 7b 0a 20 20 69  _xfer(void){.  i
1d20: 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74  nt nToken;.  int
1d30: 20 69 73 50 75 6c 6c 20 3d 20 30 3b 0a 20 20 69   isPull = 0;.  i
1d40: 6e 74 20 69 73 50 75 73 68 20 3d 20 30 3b 0a 20  nt isPush = 0;. 
1d50: 20 69 6e 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20   int nErr = 0;. 
1d60: 20 42 6c 6f 62 20 6c 69 6e 65 2c 20 65 72 72 6d   Blob line, errm
1d70: 73 67 2c 20 61 54 6f 6b 65 6e 5b 35 5d 3b 0a 0a  sg, aToken[5];..
1d80: 20 20 64 62 5f 62 65 67 69 6e 5f 74 72 61 6e 73    db_begin_trans
1d90: 61 63 74 69 6f 6e 28 29 3b 0a 20 20 62 6c 6f 62  action();.  blob
1da0: 61 72 72 61 79 5f 7a 65 72 6f 28 61 54 6f 6b 65  array_zero(aToke
1db0: 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e 29  n, count(aToken)
1dc0: 29 3b 0a 20 20 63 67 69 5f 73 65 74 5f 63 6f 6e  );.  cgi_set_con
1dd0: 74 65 6e 74 5f 74 79 70 65 28 67 2e 7a 43 6f 6e  tent_type(g.zCon
1de0: 74 65 6e 74 54 79 70 65 29 3b 0a 20 20 62 6c 6f  tentType);.  blo
1df0: 62 5f 7a 65 72 6f 28 26 65 72 72 6d 73 67 29 3b  b_zero(&errmsg);
1e00: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
1e10: 28 0a 20 20 20 20 20 22 43 52 45 41 54 45 20 54  (.     "CREATE T
1e20: 45 4d 50 20 54 41 42 4c 45 20 6f 6e 72 65 6d 6f  EMP TABLE onremo
1e30: 74 65 28 72 69 64 20 49 4e 54 45 47 45 52 20 50  te(rid INTEGER P
1e40: 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 20 2f 2a  RIMARY KEY);" /*
1e50: 20 43 6c 69 65 6e 74 20 68 61 73 20 2a 2f 0a 20   Client has */. 
1e60: 20 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50      "CREATE TEMP
1e70: 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67 28 72   TABLE pending(r
1e80: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
1e90: 52 59 20 4b 45 59 29 3b 22 20 20 2f 2a 20 43 6c  RY KEY);"  /* Cl
1ea0: 69 65 6e 74 20 6e 65 65 64 73 20 2a 2f 0a 20 20  ient needs */.  
1eb0: 29 3b 0a 20 20 77 68 69 6c 65 28 20 62 6c 6f 62  );.  while( blob
1ec0: 5f 6c 69 6e 65 28 26 67 2e 63 67 69 49 6e 2c 20  _line(&g.cgiIn, 
1ed0: 26 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 6e 54  &line) ){.    nT
1ee0: 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74 6f 6b 65  oken = blob_toke
1ef0: 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61 54 6f 6b  nize(&line, aTok
1f00: 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f 6b 65 6e  en, count(aToken
1f10: 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 20 20 66 69  ));..    /*   fi
1f20: 6c 65 20 55 55 49 44 20 53 49 5a 45 20 5c 6e 20  le UUID SIZE \n 
1f30: 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 20 20  CONTENT.    **  
1f40: 20 66 69 6c 65 20 55 55 49 44 20 44 45 4c 54 41   file UUID DELTA
1f50: 53 52 43 20 53 49 5a 45 20 5c 6e 20 43 4f 4e 54  SRC SIZE \n CONT
1f60: 45 4e 54 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ENT.    **.    *
1f70: 2a 20 41 63 63 65 70 74 20 61 20 66 69 6c 65 20  * Accept a file 
1f80: 66 72 6f 6d 20 74 68 65 20 63 6c 69 65 6e 74 2e  from the client.
1f90: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1fa0: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b  blob_eq(&aToken[
1fb0: 30 5d 2c 20 22 66 69 6c 65 22 29 20 26 26 20 6e  0], "file") && n
1fc0: 54 6f 6b 65 6e 3e 3d 32 20 26 26 20 6e 54 6f 6b  Token>=2 && nTok
1fd0: 65 6e 3c 3d 33 20 29 7b 0a 20 20 20 20 20 20 69  en<=3 ){.      i
1fe0: 66 28 20 21 69 73 50 75 73 68 20 29 7b 0a 20 20  f( !isPush ){.  
1ff0: 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f        cgi_reset_
2000: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20  content();.     
2010: 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73     @ error not\s
2020: 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73  authorized\sto\s
2030: 77 72 69 74 65 0a 20 20 20 20 20 20 20 20 6e 45  write.        nE
2040: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
2050: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2060: 20 20 20 78 66 65 72 5f 61 63 63 65 70 74 5f 66     xfer_accept_f
2070: 69 6c 65 28 26 67 2e 63 67 69 49 6e 2c 20 61 54  ile(&g.cgiIn, aT
2080: 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 26 65  oken, nToken, &e
2090: 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 69 66  rrmsg);.      if
20a0: 28 20 62 6c 6f 62 5f 73 69 7a 65 28 26 65 72 72  ( blob_size(&err
20b0: 6d 73 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20  msg) ){.        
20c0: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
20d0: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40 20 65  t();.        @ e
20e0: 72 72 6f 72 20 25 54 28 62 6c 6f 62 5f 73 74 72  rror %T(blob_str
20f0: 28 26 65 72 72 6d 73 67 29 29 0a 20 20 20 20 20  (&errmsg)).     
2100: 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20     nErr++;.     
2110: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2120: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
2130: 20 2f 2a 20 20 20 67 69 6d 6d 65 20 55 55 49 44   /*   gimme UUID
2140: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43  .    **.    ** C
2150: 6c 69 65 6e 74 20 69 73 20 72 65 71 75 65 73 74  lient is request
2160: 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 20 20 2a  ing a file.    *
2170: 2f 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65  /.    if( blob_e
2180: 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 67  q(&aToken[0], "g
2190: 69 6d 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65 6e  imme") && nToken
21a0: 3d 3d 32 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75  ==2 && blob_is_u
21b0: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20  uid(&aToken[1]) 
21c0: 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 50  ){.      if( isP
21d0: 75 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 64  ull ){.        d
21e0: 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20  b_multi_exec(.  
21f0: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
2200: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70  OR IGNORE INTO p
2210: 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20  ending(rid) ".  
2220: 20 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20          "SELECT 
2230: 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  rid FROM blob WH
2240: 45 52 45 20 75 75 69 64 3d 25 42 20 41 4e 44 20  ERE uuid=%B AND 
2250: 73 69 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b 65  size>=0", &aToke
2260: 6e 5b 31 5d 0a 20 20 20 20 20 20 20 20 29 3b 0a  n[1].        );.
2270: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2280: 65 0a 0a 20 20 20 20 2f 2a 20 20 20 69 67 6f 74  e..    /*   igot
2290: 20 55 55 49 44 0a 20 20 20 20 2a 2a 20 20 20 6c   UUID.    **   l
22a0: 65 61 66 20 55 55 49 44 0a 20 20 20 20 2a 2a 0a  eaf UUID.    **.
22b0: 20 20 20 20 2a 2a 20 43 6c 69 65 6e 74 20 61 6e      ** Client an
22c0: 6e 6f 75 6e 63 65 73 20 74 68 61 74 20 69 74 20  nounces that it 
22d0: 68 61 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  has a particular
22e0: 20 66 69 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20   file.    */.   
22f0: 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20   if( nToken==2. 
2300: 20 20 20 20 20 20 20 20 20 26 26 20 28 62 6c 6f           && (blo
2310: 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c  b_eq(&aToken[0],
2320: 20 22 69 67 6f 74 22 29 20 7c 7c 20 62 6c 6f 62   "igot") || blob
2330: 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 22  _eq(&aToken[0],"
2340: 6c 65 61 66 22 29 29 0a 20 20 20 20 20 20 20 20  leaf")).        
2350: 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69    && blob_is_uui
2360: 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b  d(&aToken[1]) ){
2370: 0a 20 20 20 20 20 20 69 66 28 20 69 73 50 75 6c  .      if( isPul
2380: 6c 20 7c 7c 20 69 73 50 75 73 68 20 29 7b 0a 20  l || isPush ){. 
2390: 20 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d         int rid =
23a0: 20 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45   db_int(0, "SELE
23b0: 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62  CT rid FROM blob
23c0: 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c   WHERE uuid=%B",
23d0: 20 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20   &aToken[1]);.  
23e0: 20 20 20 20 20 20 69 66 28 20 72 69 64 3e 30 20        if( rid>0 
23f0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f  ){.          db_
2400: 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20  multi_exec(.    
2410: 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20          "INSERT 
2420: 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f  OR IGNORE INTO o
2430: 6e 72 65 6d 6f 74 65 28 72 69 64 29 20 56 41 4c  nremote(rid) VAL
2440: 55 45 53 28 25 64 29 22 2c 20 72 69 64 0a 20 20  UES(%d)", rid.  
2450: 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
2460: 20 20 20 20 20 69 66 28 20 69 73 50 75 6c 6c 20       if( isPull 
2470: 26 26 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b  && blob_eq(&aTok
2480: 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 20 29  en[0], "leaf") )
2490: 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 64 62  {.            db
24a0: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
24b0: 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53 45             "INSE
24c0: 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54  RT OR IGNORE INT
24d0: 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22  O pending(rid) "
24e0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22  .              "
24f0: 53 45 4c 45 43 54 20 63 69 64 20 46 52 4f 4d 20  SELECT cid FROM 
2500: 70 6c 69 6e 6b 20 57 48 45 52 45 20 70 69 64 3d  plink WHERE pid=
2510: 25 64 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20  %d", rid.       
2520: 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20       );.        
2530: 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73    }.        }els
2540: 65 20 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a  e if( isPush ){.
2550: 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e            conten
2560: 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73 74  t_put(0, blob_st
2570: 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b 0a  r(&aToken[1]));.
2580: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2590: 7d 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20  }.    }else..   
25a0: 20 2f 2a 20 20 20 20 70 75 6c 6c 20 20 53 45 52   /*    pull  SER
25b0: 56 45 52 43 4f 44 45 20 20 50 52 4f 4a 45 43 54  VERCODE  PROJECT
25c0: 43 4f 44 45 0a 20 20 20 20 2a 2a 20 20 20 20 70  CODE.    **    p
25d0: 75 73 68 20 20 53 45 52 56 45 52 43 4f 44 45 20  ush  SERVERCODE 
25e0: 20 50 52 4f 4a 45 43 54 43 4f 44 45 0a 20 20 20   PROJECTCODE.   
25f0: 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63   **.    ** The c
2600: 6c 69 65 6e 74 20 77 61 6e 74 73 20 65 69 74 68  lient wants eith
2610: 65 72 20 73 65 6e 64 20 6f 72 20 72 65 63 65 69  er send or recei
2620: 76 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  ve.    */.    if
2630: 28 20 6e 54 6f 6b 65 6e 3d 3d 33 0a 20 20 20 20  ( nToken==3.    
2640: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 28 62             && (b
2650: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30  lob_eq(&aToken[0
2660: 5d 2c 20 22 70 75 6c 6c 22 29 20 7c 7c 20 62 6c  ], "pull") || bl
2670: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
2680: 2c 20 22 70 75 73 68 22 29 29 0a 20 20 20 20 20  , "push")).     
2690: 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f            && blo
26a0: 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65  b_is_uuid(&aToke
26b0: 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 5f 69 73  n[1]) && blob_is
26c0: 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32 5d  _uuid(&aToken[2]
26d0: 29 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74  ) ){.      const
26e0: 20 63 68 61 72 20 2a 7a 53 43 6f 64 65 3b 0a 20   char *zSCode;. 
26f0: 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2700: 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20 20 20 20 20  *zPCode;..      
2710: 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28  zSCode = db_get(
2720: 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 30  "server-code", 0
2730: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 53 43  );.      if( zSC
2740: 6f 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ode==0 ){.      
2750: 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22    fossil_panic("
2760: 6d 69 73 73 69 6e 67 20 73 65 72 76 65 72 20 63  missing server c
2770: 6f 64 65 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ode");.      }. 
2780: 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71       if( blob_eq
2790: 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c  _str(&aToken[1],
27a0: 20 7a 53 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a   zSCode, -1) ){.
27b0: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65          cgi_rese
27c0: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20  t_content();.   
27d0: 20 20 20 20 20 40 20 65 72 72 6f 72 20 73 65 72       @ error ser
27e0: 76 65 72 5c 73 6c 6f 6f 70 0a 20 20 20 20 20 20  ver\sloop.      
27f0: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
2800: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2810: 0a 20 20 20 20 20 20 7a 50 43 6f 64 65 20 3d 20  .      zPCode = 
2820: 64 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d  db_get("project-
2830: 63 6f 64 65 22 2c 20 30 29 3b 0a 20 20 20 20 20  code", 0);.     
2840: 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20 29   if( zPCode==0 )
2850: 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c  {.        fossil
2860: 5f 70 61 6e 69 63 28 22 6d 69 73 73 69 6e 67 20  _panic("missing 
2870: 70 72 6f 6a 65 63 74 20 63 6f 64 65 22 29 3b 0a  project code");.
2880: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2890: 28 20 21 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26  ( !blob_eq_str(&
28a0: 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a 50 43 6f 64  aToken[2], zPCod
28b0: 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  e, -1) ){.      
28c0: 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74    cgi_reset_cont
28d0: 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20 20 40  ent();.        @
28e0: 20 65 72 72 6f 72 20 77 72 6f 6e 67 5c 73 70 72   error wrong\spr
28f0: 6f 6a 65 63 74 0a 20 20 20 20 20 20 20 20 6e 45  oject.        nE
2900: 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62 72  rr++;.        br
2910: 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2920: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
2930: 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c  aToken[0], "pull
2940: 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ") ){.        if
2950: 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20  ( !g.okRead ){. 
2960: 20 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73           cgi_res
2970: 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20  et_content();.  
2980: 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20          @ error 
2990: 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c  not\sauthorized\
29a0: 73 74 6f 5c 73 72 65 61 64 0a 20 20 20 20 20 20  sto\sread.      
29b0: 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20      nErr++;.    
29c0: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
29d0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
29e0: 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20 20  sPull = 1;.     
29f0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2a00: 69 66 28 20 21 67 2e 6f 6b 57 72 69 74 65 20 29  if( !g.okWrite )
2a10: 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69 5f  {.          cgi_
2a20: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
2a30: 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72 72  .          @ err
2a40: 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69 7a  or not\sauthoriz
2a50: 65 64 5c 73 74 6f 5c 73 77 72 69 74 65 0a 20 20  ed\sto\swrite.  
2a60: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
2a70: 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
2a80: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2a90: 20 20 20 69 73 50 75 73 68 20 3d 20 31 3b 0a 0a     isPush = 1;..
2aa0: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
2ab0: 65 0a 0a 20 20 20 20 2f 2a 20 20 20 20 63 6c 6f  e..    /*    clo
2ac0: 6e 65 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a  ne.    **.    **
2ad0: 20 54 68 65 20 63 6c 69 65 6e 74 20 6b 6e 6f 77   The client know
2ae0: 73 20 6e 6f 74 68 69 6e 67 2e 20 20 54 65 6c 6c  s nothing.  Tell
2af0: 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20   all..    */.   
2b00: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54   if( blob_eq(&aT
2b10: 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c 6f 6e 65 22  oken[0], "clone"
2b20: 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21  ) ){.      if( !
2b30: 67 2e 6f 6b 52 65 61 64 20 29 7b 0a 20 20 20 20  g.okRead ){.    
2b40: 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f      cgi_reset_co
2b50: 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20  ntent();.       
2b60: 20 40 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75   @ error not\sau
2b70: 74 68 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 72 65  thorized\sto\sre
2b80: 61 64 0a 20 20 20 20 20 20 20 20 6e 45 72 72 2b  ad.        nErr+
2b90: 2b 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  +;.        break
2ba0: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2bb0: 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20 20 20 20  isPull = 1;.    
2bc0: 20 20 40 20 70 75 73 68 20 25 73 28 64 62 5f 67    @ push %s(db_g
2bd0: 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22  et("server-code"
2be0: 2c 20 22 78 22 29 29 20 25 73 28 64 62 5f 67 65  , "x")) %s(db_ge
2bf0: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22  t("project-code"
2c00: 2c 20 22 78 22 29 29 0a 20 20 20 20 20 20 64 62  , "x")).      db
2c10: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
2c20: 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20       "INSERT OR 
2c30: 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64  IGNORE INTO pend
2c40: 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20 20  ing(rid) ".     
2c50: 20 20 20 22 53 45 4c 45 43 54 20 72 69 64 20 46     "SELECT rid F
2c60: 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 73  ROM blob WHERE s
2c70: 69 7a 65 3e 3d 30 22 0a 20 20 20 20 20 20 29 3b  ize>=0".      );
2c80: 0a 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20  .    }else..    
2c90: 2f 2a 20 20 20 20 6c 6f 67 69 6e 20 20 55 53 45  /*    login  USE
2ca0: 52 20 20 4e 4f 4e 43 45 20 20 53 49 47 4e 41 54  R  NONCE  SIGNAT
2cb0: 55 52 45 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  URE.    **.    *
2cc0: 2a 20 43 68 65 63 6b 20 66 6f 72 20 61 20 76 61  * Check for a va
2cd0: 6c 69 64 20 6c 6f 67 69 6e 2e 20 20 54 68 69 73  lid login.  This
2ce0: 20 68 61 73 20 74 6f 20 68 61 70 70 65 6e 20 62   has to happen b
2cf0: 65 66 6f 72 65 20 61 6e 79 74 68 69 6e 67 20 65  efore anything e
2d00: 6c 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  lse..    */.    
2d10: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f  if( blob_eq(&aTo
2d20: 6b 65 6e 5b 30 5d 2c 20 22 6c 6f 67 69 6e 22 29  ken[0], "login")
2d30: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 34 20 29 7b   && nToken==4 ){
2d40: 0a 20 20 20 20 20 20 69 66 28 20 64 69 73 61 62  .      if( disab
2d50: 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20 20 20 20 20  leLogin ){.     
2d60: 20 20 20 67 2e 6f 6b 52 65 61 64 20 3d 20 67 2e     g.okRead = g.
2d70: 6f 6b 57 72 69 74 65 20 3d 20 31 3b 0a 20 20 20  okWrite = 1;.   
2d80: 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2d90: 20 20 63 68 65 63 6b 5f 6c 6f 67 69 6e 28 26 61    check_login(&a
2da0: 54 6f 6b 65 6e 5b 31 5d 2c 20 26 61 54 6f 6b 65  Token[1], &aToke
2db0: 6e 5b 32 5d 2c 20 26 61 54 6f 6b 65 6e 5b 33 5d  n[2], &aToken[3]
2dc0: 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
2dd0: 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 55 6e 6b  else..    /* Unk
2de0: 6e 6f 77 6e 20 6d 65 73 73 61 67 65 0a 20 20 20  nown message.   
2df0: 20 2a 2f 0a 20 20 20 20 7b 0a 20 20 20 20 20 20   */.    {.      
2e00: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
2e10: 74 28 29 3b 0a 20 20 20 20 20 20 40 20 65 72 72  t();.      @ err
2e20: 6f 72 20 62 61 64 5c 73 63 6f 6d 6d 61 6e 64 3a  or bad\scommand:
2e30: 5c 73 25 46 28 62 6c 6f 62 5f 73 74 72 28 26 6c  \s%F(blob_str(&l
2e40: 69 6e 65 29 29 0a 20 20 20 20 7d 0a 20 20 20 20  ine)).    }.    
2e50: 62 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74 28  blobarray_reset(
2e60: 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b  aToken, nToken);
2e70: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 69  .  }..  /* The i
2e80: 6e 70 75 74 20 6d 65 73 73 61 67 65 20 68 61 73  nput message has
2e90: 20 6e 6f 77 20 62 65 65 6e 20 70 72 6f 63 65 73   now been proces
2ea0: 73 65 64 2e 20 20 47 65 6e 65 72 61 74 65 20 61  sed.  Generate a
2eb0: 20 72 65 70 6c 79 2e 20 2a 2f 0a 20 20 69 66 28   reply. */.  if(
2ec0: 20 69 73 50 75 73 68 20 29 7b 0a 20 20 20 20 53   isPush ){.    S
2ed0: 74 6d 74 20 71 3b 0a 20 20 20 20 64 62 5f 70 72  tmt q;.    db_pr
2ee0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
2ef0: 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c 6f 62  T uuid FROM blob
2f00: 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22 29 3b   WHERE size<0");
2f10: 0a 20 20 20 20 77 68 69 6c 65 28 20 64 62 5f 73  .    while( db_s
2f20: 74 65 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f  tep(&q)==SQLITE_
2f30: 52 4f 57 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  ROW ){.      con
2f40: 73 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d  st char *zUuid =
2f50: 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28   db_column_text(
2f60: 26 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 40 20  &q, 0);.      @ 
2f70: 67 69 6d 6d 65 20 25 73 28 7a 55 75 69 64 29 0a  gimme %s(zUuid).
2f80: 20 20 20 20 7d 0a 20 20 20 20 64 62 5f 66 69 6e      }.    db_fin
2f90: 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20 7d 0a 20  alize(&q);.  }. 
2fa0: 20 69 66 28 20 69 73 50 75 6c 6c 20 29 7b 0a 20   if( isPull ){. 
2fb0: 20 20 20 73 65 6e 64 5f 61 6c 6c 5f 70 65 6e 64     send_all_pend
2fc0: 69 6e 67 28 30 29 3b 0a 20 20 7d 0a 20 20 64 62  ing(0);.  }.  db
2fd0: 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e  _end_transaction
2fe0: 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 4f  (0);.}../*.** CO
2ff0: 4d 4d 41 4e 44 3a 20 74 65 73 74 2d 78 66 65 72  MMAND: test-xfer
3000: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 63 6f 6d 6d  .**.** This comm
3010: 61 6e 64 20 69 73 20 75 73 65 64 20 66 6f 72 20  and is used for 
3020: 64 65 62 75 67 67 69 6e 67 20 74 68 65 20 73 65  debugging the se
3030: 72 76 65 72 2e 20 20 54 68 65 72 65 20 69 73 20  rver.  There is 
3040: 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 61 72 67 75  a single.** argu
3050: 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 74 68  ment which is th
3060: 65 20 75 6e 63 6f 6d 70 72 65 73 73 65 64 20 63  e uncompressed c
3070: 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 22 78 66  ontent of an "xf
3080: 65 72 22 20 6d 65 73 73 61 67 65 0a 2a 2a 20 66  er" message.** f
3090: 72 6f 6d 20 63 6c 69 65 6e 74 20 74 6f 20 73 65  rom client to se
30a0: 72 76 65 72 2e 20 20 54 68 69 73 20 63 6f 6d 6d  rver.  This comm
30b0: 61 6e 64 20 69 6e 74 65 72 70 72 65 74 73 20 74  and interprets t
30c0: 68 61 74 20 6d 65 73 73 61 67 65 20 61 73 0a 2a  hat message as.*
30d0: 2a 20 69 66 20 68 61 64 20 62 65 65 6e 20 72 65  * if had been re
30e0: 63 65 69 76 65 64 20 62 79 20 74 68 65 20 73 65  ceived by the se
30f0: 72 76 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 74  rver..**.** On t
3100: 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65 2c 20  he client side, 
3110: 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  run:.**.**      
3120: 66 6f 73 73 69 6c 20 70 75 73 68 20 68 74 74 70  fossil push http
3130: 3a 2f 2f 62 6f 67 75 73 2f 20 2d 2d 68 74 74 70  ://bogus/ --http
3140: 74 72 61 63 65 0a 2a 2a 0a 2a 2a 20 4f 72 20 61  trace.**.** Or a
3150: 20 73 69 6d 69 6c 61 72 20 63 6f 6d 6d 61 6e 64   similar command
3160: 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20   to provide the 
3170: 6f 75 74 70 75 74 2e 20 20 54 68 65 20 63 6f 6e  output.  The con
3180: 74 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20 6d  tent of the.** m
3190: 65 73 73 61 67 65 20 77 69 6c 6c 20 61 70 70 65  essage will appe
31a0: 61 72 20 6f 6e 20 73 74 61 6e 64 61 72 64 20 6f  ar on standard o
31b0: 75 74 70 75 74 2e 20 20 43 61 70 74 75 72 65 20  utput.  Capture 
31c0: 74 68 69 73 20 6d 65 73 73 61 67 65 0a 2a 2a 20  this message.** 
31d0: 69 6e 74 6f 20 61 20 66 69 6c 65 20 6e 61 6d 65  into a file name
31e0: 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20  d (for example) 
31f0: 6f 75 74 2e 74 78 74 2e 20 20 54 68 65 6e 20 72  out.txt.  Then r
3200: 75 6e 20 74 68 65 0a 2a 2a 20 73 65 72 76 65 72  un the.** server
3210: 20 69 6e 20 67 64 62 3a 0a 2a 2a 0a 2a 2a 20 20   in gdb:.**.**  
3220: 20 20 20 67 64 62 20 66 6f 73 73 69 6c 0a 2a 2a     gdb fossil.**
3230: 20 20 20 20 20 72 20 74 65 73 74 2d 78 66 65 72       r test-xfer
3240: 20 6f 75 74 2e 74 78 74 0a 2a 2f 0a 76 6f 69 64   out.txt.*/.void
3250: 20 63 6d 64 5f 74 65 73 74 5f 78 66 65 72 28 76   cmd_test_xfer(v
3260: 6f 69 64 29 7b 0a 20 20 69 6e 74 20 6e 6f 74 55  oid){.  int notU
3270: 73 65 64 3b 0a 20 20 69 66 28 20 67 2e 61 72 67  sed;.  if( g.arg
3280: 63 21 3d 32 20 26 26 20 67 2e 61 72 67 63 21 3d  c!=2 && g.argc!=
3290: 33 20 29 7b 0a 20 20 20 20 75 73 61 67 65 28 22  3 ){.    usage("
32a0: 3f 4d 45 53 53 41 47 45 46 49 4c 45 3f 22 29 3b  ?MESSAGEFILE?");
32b0: 0a 20 20 7d 0a 20 20 64 62 5f 6d 75 73 74 5f 62  .  }.  db_must_b
32c0: 65 5f 77 69 74 68 69 6e 5f 74 72 65 65 28 29 3b  e_within_tree();
32d0: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 67 2e  .  blob_zero(&g.
32e0: 63 67 69 49 6e 29 3b 0a 20 20 62 6c 6f 62 5f 72  cgiIn);.  blob_r
32f0: 65 61 64 5f 66 72 6f 6d 5f 66 69 6c 65 28 26 67  ead_from_file(&g
3300: 2e 63 67 69 49 6e 2c 20 67 2e 61 72 67 63 3d 3d  .cgiIn, g.argc==
3310: 32 20 3f 20 22 2d 22 20 3a 20 67 2e 61 72 67 76  2 ? "-" : g.argv
3320: 5b 32 5d 29 3b 0a 20 20 64 69 73 61 62 6c 65 4c  [2]);.  disableL
3330: 6f 67 69 6e 20 3d 20 31 3b 0a 20 20 70 61 67 65  ogin = 1;.  page
3340: 5f 78 66 65 72 28 29 3b 0a 20 20 70 72 69 6e 74  _xfer();.  print
3350: 66 28 22 25 73 5c 6e 22 2c 20 63 67 69 5f 65 78  f("%s\n", cgi_ex
3360: 74 72 61 63 74 5f 63 6f 6e 74 65 6e 74 28 26 6e  tract_content(&n
3370: 6f 74 55 73 65 64 29 29 3b 0a 7d 0a 0a 0a 2f 2a  otUsed));.}.../*
3380: 0a 2a 2a 20 53 79 6e 63 20 74 6f 20 74 68 65 20  .** Sync to the 
3390: 68 6f 73 74 20 69 64 65 6e 74 69 66 69 65 64 20  host identified 
33a0: 69 6e 20 67 2e 75 72 6c 4e 61 6d 65 20 61 6e 64  in g.urlName and
33b0: 20 67 2e 75 72 6c 50 61 74 68 2e 20 20 54 68 69   g.urlPath.  Thi
33c0: 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
33d0: 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 63 6c  called by the cl
33e0: 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 52 65 63 6f  ient..**.** Reco
33f0: 72 64 73 20 61 72 65 20 70 75 73 68 65 64 20 74  rds are pushed t
3400: 6f 20 74 68 65 20 73 65 72 76 65 72 20 69 66 20  o the server if 
3410: 70 75 73 68 46 6c 61 67 20 69 73 20 74 72 75 65  pushFlag is true
3420: 2e 20 20 52 65 63 6f 72 64 73 0a 2a 2a 20 61 72  .  Records.** ar
3430: 65 20 70 75 6c 6c 65 64 20 69 66 20 70 75 6c 6c  e pulled if pull
3440: 46 6c 61 67 20 69 73 20 74 72 75 65 2e 20 20 41  Flag is true.  A
3450: 20 66 75 6c 6c 20 73 79 6e 63 20 6f 63 63 75 72   full sync occur
3460: 73 20 69 66 20 62 6f 74 68 20 61 72 65 0a 2a 2a  s if both are.**
3470: 20 74 72 75 65 2e 0a 2a 2f 0a 76 6f 69 64 20 63   true..*/.void c
3480: 6c 69 65 6e 74 5f 73 79 6e 63 28 69 6e 74 20 70  lient_sync(int p
3490: 75 73 68 46 6c 61 67 2c 20 69 6e 74 20 70 75 6c  ushFlag, int pul
34a0: 6c 46 6c 61 67 2c 20 69 6e 74 20 63 6c 6f 6e 65  lFlag, int clone
34b0: 46 6c 61 67 29 7b 0a 20 20 69 6e 74 20 67 6f 20  Flag){.  int go 
34c0: 3d 20 31 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  = 1;        /* L
34d0: 6f 6f 70 20 75 6e 74 69 6c 20 7a 65 72 6f 20 2a  oop until zero *
34e0: 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a  /.  int nToken;.
34f0: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
3500: 43 6f 64 65 20 3d 20 64 62 5f 67 65 74 28 22 73  Code = db_get("s
3510: 65 72 76 65 72 2d 63 6f 64 65 22 2c 20 22 78 22  erver-code", "x"
3520: 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  );.  const char 
3530: 2a 7a 50 43 6f 64 65 20 3d 20 64 62 5f 67 65 74  *zPCode = db_get
3540: 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22 2c  ("project-code",
3550: 20 30 29 3b 0a 20 20 69 6e 74 20 6e 46 69 6c 65   0);.  int nFile
3560: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 4d 73 67   = 0;.  int nMsg
3570: 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 52 65 71   = 0;.  int nReq
3580: 20 3d 20 30 3b 0a 20 20 42 6c 6f 62 20 73 65 6e   = 0;.  Blob sen
3590: 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78  d;        /* Tex
35a0: 74 20 77 65 20 61 72 65 20 73 65 6e 64 69 6e 67  t we are sending
35b0: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 2a   to the server *
35c0: 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76 3b 20 20  /.  Blob recv;  
35d0: 20 20 20 20 20 20 2f 2a 20 52 65 70 6c 79 20 77        /* Reply w
35e0: 65 20 67 6f 74 20 62 61 63 6b 20 66 72 6f 6d 20  e got back from 
35f0: 74 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 20 20  the server */.  
3600: 42 6c 6f 62 20 6c 69 6e 65 3b 20 20 20 20 20 20  Blob line;      
3610: 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 6c 69    /* A single li
3620: 6e 65 20 6f 66 20 74 68 65 20 72 65 70 6c 79 20  ne of the reply 
3630: 2a 2f 0a 20 20 42 6c 6f 62 20 61 54 6f 6b 65 6e  */.  Blob aToken
3640: 5b 35 5d 3b 20 20 20 2f 2a 20 41 20 74 6f 6b 65  [5];   /* A toke
3650: 6e 69 7a 61 74 69 6f 6e 20 6f 66 20 6c 69 6e 65  nization of line
3660: 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72 72 6d 73   */.  Blob errms
3670: 67 3b 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72  g;      /* Error
3680: 20 6d 65 73 73 61 67 65 20 2a 2f 0a 0a 20 20 61   message */..  a
3690: 73 73 65 72 74 28 20 70 75 73 68 46 6c 61 67 20  ssert( pushFlag 
36a0: 7c 7c 20 70 75 6c 6c 46 6c 61 67 20 7c 7c 20 63  || pullFlag || c
36b0: 6c 6f 6e 65 46 6c 61 67 20 29 3b 0a 20 20 61 73  loneFlag );.  as
36c0: 73 65 72 74 28 20 21 67 2e 75 72 6c 49 73 46 69  sert( !g.urlIsFi
36d0: 6c 65 20 29 3b 20 20 20 20 20 20 20 20 20 20 2f  le );          /
36e0: 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77 6f 72 6b  * This only work
36f0: 73 20 66 6f 72 20 6e 65 74 77 6f 72 6b 69 6e 67  s for networking
3700: 20 2a 2f 0a 0a 20 20 64 62 5f 62 65 67 69 6e 5f   */..  db_begin_
3710: 74 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20  transaction();. 
3720: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a   db_multi_exec(.
3730: 20 20 20 20 2f 2a 20 52 65 63 6f 72 64 73 20 77      /* Records w
3740: 68 69 63 68 20 77 65 20 6b 6e 6f 77 20 74 68 65  hich we know the
3750: 20 6f 74 68 65 72 20 73 69 64 65 20 61 6c 73 6f   other side also
3760: 20 68 61 73 20 2a 2f 0a 20 20 20 20 22 43 52 45   has */.    "CRE
3770: 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f  ATE TEMP TABLE o
3780: 6e 72 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45  nremote(rid INTE
3790: 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29  GER PRIMARY KEY)
37a0: 3b 22 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64  ;".    /* Record
37b0: 73 20 77 65 20 6b 6e 6f 77 20 74 68 65 20 6f 74  s we know the ot
37c0: 68 65 72 20 73 69 64 65 20 6e 65 65 64 73 20 2a  her side needs *
37d0: 2f 0a 20 20 20 20 22 43 52 45 41 54 45 20 54 45  /.    "CREATE TE
37e0: 4d 50 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67  MP TABLE pending
37f0: 28 72 69 64 20 49 4e 54 45 47 45 52 20 50 52 49  (rid INTEGER PRI
3800: 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20 20 29 3b  MARY KEY);".  );
3810: 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f 7a 65 72  .  blobarray_zer
3820: 6f 28 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28  o(aToken, count(
3830: 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62 6c 6f 62  aToken));.  blob
3840: 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b 0a 20 20  _zero(&send);.  
3850: 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65 63 76 29  blob_zero(&recv)
3860: 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 65  ;.  blob_zero(&e
3870: 72 72 6d 73 67 29 3b 0a 0a 0a 20 20 77 68 69 6c  rrmsg);...  whil
3880: 65 28 20 67 6f 20 29 7b 0a 20 20 20 20 67 6f 20  e( go ){.    go 
3890: 3d 20 30 3b 0a 20 20 20 20 6e 46 69 6c 65 20 3d  = 0;.    nFile =
38a0: 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 3d 20 30   nReq = nMsg = 0
38b0: 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65 72 61  ;..    /* Genera
38c0: 74 65 20 61 20 72 65 71 75 65 73 74 20 74 6f 20  te a request to 
38d0: 62 65 20 73 65 6e 74 20 74 6f 20 74 68 65 20 73  be sent to the s
38e0: 65 72 76 65 72 2e 0a 20 20 20 20 2a 2a 20 41 6c  erver..    ** Al
38f0: 77 61 79 73 20 62 65 67 69 6e 20 77 69 74 68 20  ways begin with 
3900: 61 20 63 6c 6f 6e 65 2c 20 70 75 6c 6c 2c 20 6f  a clone, pull, o
3910: 72 20 70 75 73 68 20 6d 65 73 73 61 67 65 0a 20  r push message. 
3920: 20 20 20 2a 2f 0a 20 20 20 20 0a 20 20 20 20 69     */.    .    i
3930: 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20 29 7b 0a  f( cloneFlag ){.
3940: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
3950: 64 66 28 26 73 65 6e 64 2c 20 22 63 6c 6f 6e 65  df(&send, "clone
3960: 5c 6e 22 29 3b 0a 20 20 20 20 20 20 70 75 73 68  \n");.      push
3970: 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  Flag = 0;.      
3980: 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b 0a 20 20  pullFlag = 0;.  
3990: 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20      nMsg++;.    
39a0: 7d 65 6c 73 65 20 69 66 28 20 70 75 6c 6c 46 6c  }else if( pullFl
39b0: 61 67 20 29 7b 0a 20 20 20 20 20 20 62 6c 6f 62  ag ){.      blob
39c0: 5f 61 70 70 65 6e 64 66 28 26 73 65 6e 64 2c 20  _appendf(&send, 
39d0: 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e 22 2c 20  "pull %s %s\n", 
39e0: 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64 65 29 3b  zSCode, zPCode);
39f0: 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20  .      nMsg++;. 
3a00: 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 75 73     }.    if( pus
3a10: 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62  hFlag ){.      b
3a20: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
3a30: 64 2c 20 22 70 75 73 68 20 25 73 20 25 73 5c 6e  d, "push %s %s\n
3a40: 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64  ", zSCode, zPCod
3a50: 65 29 3b 0a 20 20 20 20 20 20 6e 4d 73 67 2b 2b  e);.      nMsg++
3a60: 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  ;.    }..    if(
3a70: 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20 20 20   pullFlag ){.   
3a80: 20 20 20 2f 2a 20 53 65 6e 64 20 67 69 6d 6d 65     /* Send gimme
3a90: 20 6d 65 73 73 61 67 65 20 66 6f 72 20 65 76 65   message for eve
3aa0: 72 79 20 70 68 61 6e 74 6f 6d 20 74 68 61 74 20  ry phantom that 
3ab0: 77 65 20 68 6f 6c 64 2e 0a 20 20 20 20 20 20 2a  we hold..      *
3ac0: 2f 0a 20 20 20 20 20 20 53 74 6d 74 20 71 3b 0a  /.      Stmt q;.
3ad0: 20 20 20 20 20 20 64 62 5f 70 72 65 70 61 72 65        db_prepare
3ae0: 28 26 71 2c 20 22 53 45 4c 45 43 54 20 75 75 69  (&q, "SELECT uui
3af0: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  d FROM blob WHER
3b00: 45 20 73 69 7a 65 3c 30 22 29 3b 0a 20 20 20 20  E size<0");.    
3b10: 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65 70    while( db_step
3b20: 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (&q)==SQLITE_ROW
3b30: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73   ){.        cons
3b40: 74 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20  t char *zUuid = 
3b50: 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26  db_column_text(&
3b60: 71 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62  q, 0);.        b
3b70: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
3b80: 64 2c 22 67 69 6d 6d 65 20 25 73 5c 6e 22 2c 20  d,"gimme %s\n", 
3b90: 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 20 20  zUuid);.        
3ba0: 6e 52 65 71 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a  nReq++;.      }.
3bb0: 20 20 20 20 20 20 64 62 5f 66 69 6e 61 6c 69 7a        db_finaliz
3bc0: 65 28 26 71 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  e(&q);.    }..  
3bd0: 20 20 69 66 28 20 70 75 73 68 46 6c 61 67 20 29    if( pushFlag )
3be0: 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e 64 20  {.      /* Send 
3bf0: 74 68 65 20 73 65 72 76 65 72 20 61 6e 79 20 66  the server any f
3c00: 69 6c 65 73 20 74 68 61 74 20 74 68 65 20 73 65  iles that the se
3c10: 72 76 65 72 20 68 61 73 20 72 65 71 75 65 73 74  rver has request
3c20: 65 64 20 2a 2f 0a 20 20 20 20 20 20 6e 46 69 6c  ed */.      nFil
3c30: 65 20 2b 3d 20 73 65 6e 64 5f 61 6c 6c 5f 70 65  e += send_all_pe
3c40: 6e 64 69 6e 67 28 26 73 65 6e 64 29 3b 0a 20 20  nding(&send);.  
3c50: 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 75 6c    }..    if( pul
3c60: 6c 46 6c 61 67 20 7c 7c 20 70 75 73 68 46 6c 61  lFlag || pushFla
3c70: 67 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6c  g ){.      /* Al
3c80: 77 61 79 73 20 73 65 6e 64 20 6f 75 72 20 6c 65  ways send our le
3c90: 61 76 65 73 20 2a 2f 0a 20 20 20 20 20 20 53 74  aves */.      St
3ca0: 6d 74 20 71 3b 0a 20 20 20 20 20 20 64 62 5f 70  mt q;.      db_p
3cb0: 72 65 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20  repare(&q, .    
3cc0: 20 20 20 20 20 22 53 45 4c 45 43 54 20 75 75 69       "SELECT uui
3cd0: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  d FROM blob WHER
3ce0: 45 20 72 69 64 20 49 4e 22 0a 20 20 20 20 20 20  E rid IN".      
3cf0: 20 20 20 22 20 20 28 53 45 4c 45 43 54 20 63 69     "  (SELECT ci
3d00: 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 45 58 43  d FROM plink EXC
3d10: 45 50 54 20 53 45 4c 45 43 54 20 70 69 64 20 46  EPT SELECT pid F
3d20: 52 4f 4d 20 70 6c 69 6e 6b 29 22 0a 20 20 20 20  ROM plink)".    
3d30: 20 20 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65    );.      while
3d40: 28 20 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53  ( db_step(&q)==S
3d50: 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
3d60: 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
3d70: 2a 7a 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75  *zUuid = db_colu
3d80: 6d 6e 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a  mn_text(&q, 0);.
3d90: 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70          blob_app
3da0: 65 6e 64 66 28 26 73 65 6e 64 2c 20 22 6c 65 61  endf(&send, "lea
3db0: 66 20 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b  f %s\n", zUuid);
3dc0: 0a 20 20 20 20 20 20 20 20 6e 4d 73 67 2b 2b 3b  .        nMsg++;
3dd0: 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
3de0: 62 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a  b_finalize(&q);.
3df0: 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 78      }..    /* Ex
3e00: 63 68 61 6e 67 65 20 6d 65 73 73 61 67 65 73 20  change messages 
3e10: 77 69 74 68 20 74 68 65 20 73 65 72 76 65 72 20  with the server 
3e20: 2a 2f 0a 20 20 20 20 70 72 69 6e 74 66 28 22 53  */.    printf("S
3e30: 65 6e 64 3a 20 20 20 20 20 20 25 64 20 66 69 6c  end:      %d fil
3e40: 65 73 2c 20 25 64 20 72 65 71 75 65 73 74 73 2c  es, %d requests,
3e50: 20 25 64 20 6f 74 68 65 72 20 6d 65 73 73 61 67   %d other messag
3e60: 65 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20  es\n",.         
3e70: 20 20 20 6e 46 69 6c 65 2c 20 6e 52 65 71 2c 20     nFile, nReq, 
3e80: 6e 4d 73 67 29 3b 0a 20 20 20 20 6e 46 69 6c 65  nMsg);.    nFile
3e90: 20 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 3d   = nReq = nMsg =
3ea0: 20 30 3b 0a 20 20 20 20 68 74 74 70 5f 65 78 63   0;.    http_exc
3eb0: 68 61 6e 67 65 28 26 73 65 6e 64 2c 20 26 72 65  hange(&send, &re
3ec0: 63 76 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65  cv);.    blob_re
3ed0: 73 65 74 28 26 73 65 6e 64 29 3b 0a 0a 20 20 20  set(&send);..   
3ee0: 20 2f 2a 20 50 72 6f 63 65 73 73 20 74 68 65 20   /* Process the 
3ef0: 72 65 70 6c 79 20 74 68 61 74 20 63 61 6d 65 20  reply that came 
3f00: 62 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 73 65  back from the se
3f10: 72 76 65 72 20 2a 2f 0a 20 20 20 20 77 68 69 6c  rver */.    whil
3f20: 65 28 20 62 6c 6f 62 5f 6c 69 6e 65 28 26 72 65  e( blob_line(&re
3f30: 63 76 2c 20 26 6c 69 6e 65 29 20 29 7b 0a 20 20  cv, &line) ){.  
3f40: 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f      nToken = blo
3f50: 62 5f 74 6f 6b 65 6e 69 7a 65 28 26 6c 69 6e 65  b_tokenize(&line
3f60: 2c 20 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28  , aToken, count(
3f70: 61 54 6f 6b 65 6e 29 29 3b 0a 0a 20 20 20 20 20  aToken));..     
3f80: 20 2f 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20   /*   file UUID 
3f90: 53 49 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a  SIZE \n CONTENT.
3fa0: 20 20 20 20 20 20 2a 2a 20 20 20 66 69 6c 65 20        **   file 
3fb0: 55 55 49 44 20 44 45 4c 54 41 53 52 43 20 53 49  UUID DELTASRC SI
3fc0: 5a 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20  ZE \n CONTENT.  
3fd0: 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
3fe0: 52 65 63 65 69 76 65 20 61 20 66 69 6c 65 20 74  Receive a file t
3ff0: 72 61 6e 73 6d 69 74 74 65 64 20 66 72 6f 6d 20  ransmitted from 
4000: 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 0a 20  the other side. 
4010: 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
4020: 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65  ( blob_eq(&aToke
4030: 6e 5b 30 5d 2c 22 66 69 6c 65 22 29 20 29 7b 0a  n[0],"file") ){.
4040: 20 20 20 20 20 20 20 20 78 66 65 72 5f 61 63 63          xfer_acc
4050: 65 70 74 5f 66 69 6c 65 28 26 72 65 63 76 2c 20  ept_file(&recv, 
4060: 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20  aToken, nToken, 
4070: 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20  &errmsg);.      
4080: 20 20 6e 46 69 6c 65 2b 2b 3b 0a 20 20 20 20 20    nFile++;.     
4090: 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20     go = 1;.     
40a0: 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20 20 2f 2a   }else..      /*
40b0: 20 20 20 67 69 6d 6d 65 20 55 55 49 44 0a 20 20     gimme UUID.  
40c0: 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
40d0: 53 65 72 76 65 72 20 69 73 20 72 65 71 75 65 73  Server is reques
40e0: 74 69 6e 67 20 61 20 66 69 6c 65 0a 20 20 20 20  ting a file.    
40f0: 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62    */.      if( b
4100: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30  lob_eq(&aToken[0
4110: 5d 2c 20 22 67 69 6d 6d 65 22 29 20 26 26 20 6e  ], "gimme") && n
4120: 54 6f 6b 65 6e 3d 3d 32 0a 20 20 20 20 20 20 20  Token==2.       
4130: 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f          && blob_
4140: 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b  is_uuid(&aToken[
4150: 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  1]) ){.        n
4160: 52 65 71 2b 2b 3b 0a 20 20 20 20 20 20 20 20 69  Req++;.        i
4170: 66 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a 20  f( pushFlag ){. 
4180: 20 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c 74           db_mult
4190: 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 20  i_exec(.        
41a0: 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49      "INSERT OR I
41b0: 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64 69  GNORE INTO pendi
41c0: 6e 67 28 72 69 64 29 20 22 0a 20 20 20 20 20 20  ng(rid) ".      
41d0: 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 72 69        "SELECT ri
41e0: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  d FROM blob WHER
41f0: 45 20 75 75 69 64 3d 25 42 20 41 4e 44 20 73 69  E uuid=%B AND si
4200: 7a 65 3e 3d 30 22 2c 20 26 61 54 6f 6b 65 6e 5b  ze>=0", &aToken[
4210: 31 5d 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a  1].          );.
4220: 20 20 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31            go = 1
4230: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
4240: 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20 20 20 20    }else.  .     
4250: 20 2f 2a 20 20 20 69 67 6f 74 20 55 55 49 44 0a   /*   igot UUID.
4260: 20 20 20 20 20 20 2a 2a 20 20 20 6c 65 61 66 20        **   leaf 
4270: 55 55 49 44 0a 20 20 20 20 20 20 2a 2a 0a 20 20  UUID.      **.  
4280: 20 20 20 20 2a 2a 20 53 65 72 76 65 72 20 70 72      ** Server pr
4290: 6f 63 6c 61 69 6d 73 20 74 68 61 74 20 69 74 20  oclaims that it 
42a0: 68 61 73 20 61 20 70 61 72 74 69 63 75 6c 61 72  has a particular
42b0: 20 66 69 6c 65 2e 20 20 41 20 6c 65 61 66 20 6d   file.  A leaf m
42c0: 65 73 73 61 67 65 0a 20 20 20 20 20 20 2a 2a 20  essage.      ** 
42d0: 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 66  means that the f
42e0: 69 6c 65 20 69 73 20 61 20 6c 65 61 66 20 6d 61  ile is a leaf ma
42f0: 6e 69 66 65 73 74 20 6f 6e 20 74 68 65 20 73 65  nifest on the se
4300: 72 76 65 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  rver..      */. 
4310: 20 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d       if( nToken=
4320: 3d 32 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  =2.             
4330: 20 26 26 20 28 62 6c 6f 62 5f 65 71 28 26 61 54   && (blob_eq(&aT
4340: 6f 6b 65 6e 5b 30 5d 2c 20 22 69 67 6f 74 22 29  oken[0], "igot")
4350: 20 7c 7c 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f   || blob_eq(&aTo
4360: 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66 22 29 29  ken[0], "leaf"))
4370: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26  .              &
4380: 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26  & blob_is_uuid(&
4390: 61 54 6f 6b 65 6e 5b 31 5d 29 20 29 7b 0a 20 20  aToken[1]) ){.  
43a0: 20 20 20 20 20 20 69 6e 74 20 72 69 64 20 3d 20        int rid = 
43b0: 64 62 5f 69 6e 74 28 30 2c 20 22 53 45 4c 45 43  db_int(0, "SELEC
43c0: 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20  T rid FROM blob 
43d0: 57 48 45 52 45 20 75 75 69 64 3d 25 42 22 2c 20  WHERE uuid=%B", 
43e0: 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20  &aToken[1]);.   
43f0: 20 20 20 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20       nMsg++;.   
4400: 20 20 20 20 20 69 66 28 20 72 69 64 3e 30 20 29       if( rid>0 )
4410: 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 6d  {.          db_m
4420: 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20  ulti_exec(.     
4430: 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f         "INSERT O
4440: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e  R IGNORE INTO on
4450: 72 65 6d 6f 74 65 28 72 69 64 29 20 56 41 4c 55  remote(rid) VALU
4460: 45 53 28 25 64 29 22 2c 20 72 69 64 0a 20 20 20  ES(%d)", rid.   
4470: 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
4480: 20 20 20 20 2f 2a 20 41 64 64 20 74 6f 20 74 68      /* Add to th
4490: 65 20 70 65 6e 64 69 6e 67 20 73 65 74 20 61 6c  e pending set al
44a0: 6c 20 63 68 69 6c 64 72 65 6e 20 6f 66 20 74 68  l children of th
44b0: 65 20 73 65 72 76 65 72 27 73 20 6c 65 61 76 65  e server's leave
44c0: 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69  s */.          i
44d0: 66 28 20 70 75 73 68 46 6c 61 67 20 26 26 20 62  f( pushFlag && b
44e0: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30  lob_eq(&aToken[0
44f0: 5d 2c 20 22 6c 65 61 66 22 29 20 29 7b 0a 20 20  ], "leaf") ){.  
4500: 20 20 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c            db_mul
4510: 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20  ti_exec(.       
4520: 20 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 4f         "INSERT O
4530: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 70 65  R IGNORE INTO pe
4540: 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20 20 20  nding(rid) ".   
4550: 20 20 20 20 20 20 20 20 20 20 20 22 53 45 4c 45             "SELE
4560: 43 54 20 63 69 64 20 46 52 4f 4d 20 70 6c 69 6e  CT cid FROM plin
4570: 6b 20 57 48 45 52 45 20 70 69 64 3d 25 64 22 2c  k WHERE pid=%d",
4580: 20 72 69 64 0a 20 20 20 20 20 20 20 20 20 20 20   rid.           
4590: 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   );.            
45a0: 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  go = 1;.        
45b0: 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69 66    }.          if
45c0: 28 20 70 75 6c 6c 46 6c 61 67 20 26 26 20 21 67  ( pullFlag && !g
45d0: 6f 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20  o && .          
45e0: 20 20 20 20 64 62 5f 65 78 69 73 74 73 28 22 53      db_exists("S
45f0: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 62 6c 6f  ELECT 1 FROM blo
4600: 62 20 57 48 45 52 45 20 72 69 64 3d 25 64 20 41  b WHERE rid=%d A
4610: 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 72 69 64  ND size>=0", rid
4620: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  ) ){.           
4630: 20 67 6f 20 3d 20 31 3b 0a 20 20 20 20 20 20 20   go = 1;.       
4640: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c     }.        }el
4650: 73 65 20 69 66 28 20 70 75 6c 6c 46 6c 61 67 20  se if( pullFlag 
4660: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 20  ){.          go 
4670: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 63  = 1;.          c
4680: 6f 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c  ontent_put(0, bl
4690: 6f 62 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31  ob_str(&aToken[1
46a0: 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ]));.        }. 
46b0: 20 20 20 20 20 7d 65 6c 73 65 0a 20 20 0a 20 20       }else.  .  
46c0: 20 20 20 20 2f 2a 20 20 20 70 75 73 68 20 20 53      /*   push  S
46d0: 45 52 56 45 52 43 4f 44 45 20 20 50 52 4f 44 55  ERVERCODE  PRODU
46e0: 43 54 43 4f 44 45 0a 20 20 20 20 20 20 2a 2a 0a  CTCODE.      **.
46f0: 20 20 20 20 20 20 2a 2a 20 53 68 6f 75 6c 64 20        ** Should 
4700: 6f 6e 6c 79 20 68 61 70 70 65 6e 20 69 6e 20 72  only happen in r
4710: 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 63 6c 6f  esponse to a clo
4720: 6e 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ne..      */.   
4730: 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26     if( blob_eq(&
4740: 61 54 6f 6b 65 6e 5b 30 5d 2c 22 70 75 73 68 22  aToken[0],"push"
4750: 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 33 20 26  ) && nToken==3 &
4760: 26 20 63 6c 6f 6e 65 46 6c 61 67 0a 20 20 20 20  & cloneFlag.    
4770: 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c 6f            && blo
4780: 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65  b_is_uuid(&aToke
4790: 6e 5b 31 5d 29 20 26 26 20 62 6c 6f 62 5f 69 73  n[1]) && blob_is
47a0: 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 32 5d  _uuid(&aToken[2]
47b0: 29 20 29 7b 0a 0a 20 20 20 20 20 20 20 20 69 66  ) ){..        if
47c0: 28 20 62 6c 6f 62 5f 65 71 5f 73 74 72 28 26 61  ( blob_eq_str(&a
47d0: 54 6f 6b 65 6e 5b 31 5d 2c 20 7a 53 43 6f 64 65  Token[1], zSCode
47e0: 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 20  , -1) ){.       
47f0: 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61 6c 28     fossil_fatal(
4800: 22 73 65 72 76 65 72 20 6c 6f 6f 70 22 29 3b 0a  "server loop");.
4810: 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
4820: 20 20 6e 4d 73 67 2b 2b 3b 0a 20 20 20 20 20 20    nMsg++;.      
4830: 20 20 69 66 28 20 7a 50 43 6f 64 65 3d 3d 30 20    if( zPCode==0 
4840: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 50 43  ){.          zPC
4850: 6f 64 65 20 3d 20 6d 70 72 69 6e 74 66 28 22 25  ode = mprintf("%
4860: 62 22 2c 20 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b  b", &aToken[2]);
4870: 0a 20 20 20 20 20 20 20 20 20 20 64 62 5f 73 65  .          db_se
4880: 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f 64 65 22  t("project-code"
4890: 2c 20 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 20  , zPCode);.     
48a0: 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 6c 6f     }.        clo
48b0: 6e 65 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20  neFlag = 0;.    
48c0: 20 20 20 20 70 75 6c 6c 46 6c 61 67 20 3d 20 31      pullFlag = 1
48d0: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20  ;.      }else.. 
48e0: 20 20 20 20 20 2f 2a 20 20 20 65 72 72 6f 72 20       /*   error 
48f0: 4d 45 53 53 41 47 45 0a 20 20 20 20 20 20 2a 2a  MESSAGE.      **
4900: 0a 20 20 20 20 20 20 2a 2a 20 52 65 70 6f 72 74  .      ** Report
4910: 20 61 6e 20 65 72 72 6f 72 0a 20 20 20 20 20 20   an error.      
4920: 2a 2f 20 20 20 20 20 20 20 20 0a 20 20 20 20 20  */        .     
4930: 20 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54   if( blob_eq(&aT
4940: 6f 6b 65 6e 5b 30 5d 2c 22 65 72 72 6f 72 22 29  oken[0],"error")
4950: 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d 32 20 29 7b   && nToken==2 ){
4960: 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
4970: 4d 73 67 20 3d 20 62 6c 6f 62 5f 74 65 72 6d 69  Msg = blob_termi
4980: 6e 61 74 65 28 26 61 54 6f 6b 65 6e 5b 31 5d 29  nate(&aToken[1])
4990: 3b 0a 20 20 20 20 20 20 20 20 64 65 66 6f 73 73  ;.        defoss
49a0: 69 6c 69 7a 65 28 7a 4d 73 67 29 3b 0a 20 20 20  ilize(zMsg);.   
49b0: 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64       blob_append
49c0: 66 28 26 65 72 72 6d 73 67 2c 20 22 73 65 72 76  f(&errmsg, "serv
49d0: 65 72 20 73 61 79 73 3a 20 25 73 22 2c 20 7a 4d  er says: %s", zM
49e0: 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  sg);.      }else
49f0: 0a 0a 20 20 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f  ..      /* Unkno
4a00: 77 6e 20 6d 65 73 73 61 67 65 20 2a 2f 0a 20 20  wn message */.  
4a10: 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 62 6c      {.        bl
4a20: 6f 62 5f 61 70 70 65 6e 64 66 28 26 65 72 72 6d  ob_appendf(&errm
4a30: 73 67 2c 20 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6d  sg, "unknown com
4a40: 6d 61 6e 64 3a 20 25 62 22 2c 20 26 61 54 6f 6b  mand: %b", &aTok
4a50: 65 6e 5b 30 5d 29 3b 0a 20 20 20 20 20 20 7d 0a  en[0]);.      }.
4a60: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
4a70: 73 69 7a 65 28 26 65 72 72 6d 73 67 29 20 29 7b  size(&errmsg) ){
4a80: 0a 20 20 20 20 20 20 20 20 66 6f 73 73 69 6c 5f  .        fossil_
4a90: 66 61 74 61 6c 28 22 25 62 22 2c 20 26 65 72 72  fatal("%b", &err
4aa0: 6d 73 67 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  msg);.      }.  
4ab0: 20 20 20 20 62 6c 6f 62 61 72 72 61 79 5f 72 65      blobarray_re
4ac0: 73 65 74 28 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  set(aToken, nTok
4ad0: 65 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 62  en);.    }.    b
4ae0: 6c 6f 62 5f 72 65 73 65 74 28 26 72 65 63 76 29  lob_reset(&recv)
4af0: 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 52 65  ;.    printf("Re
4b00: 63 65 69 76 65 64 3a 20 20 25 64 20 66 69 6c 65  ceived:  %d file
4b10: 73 2c 20 25 64 20 72 65 71 75 65 73 74 73 2c 20  s, %d requests, 
4b20: 25 64 20 6f 74 68 65 72 20 6d 65 73 73 61 67 65  %d other message
4b30: 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
4b40: 20 20 6e 46 69 6c 65 2c 20 6e 52 65 71 2c 20 6e    nFile, nReq, n
4b50: 4d 73 67 29 3b 0a 20 20 20 20 6e 46 69 6c 65 20  Msg);.    nFile 
4b60: 3d 20 6e 52 65 71 20 3d 20 6e 4d 73 67 20 3d 20  = nReq = nMsg = 
4b70: 30 3b 0a 20 20 7d 3b 0a 20 20 68 74 74 70 5f 63  0;.  };.  http_c
4b80: 6c 6f 73 65 28 29 3b 0a 20 20 64 62 5f 65 6e 64  lose();.  db_end
4b90: 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 30 29 3b  _transaction(0);
4ba0: 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63  .  db_multi_exec
4bb0: 28 0a 20 20 20 20 22 44 52 4f 50 20 54 41 42 4c  (.    "DROP TABL
4bc0: 45 20 6f 6e 72 65 6d 6f 74 65 3b 22 0a 20 20 20  E onremote;".   
4bd0: 20 22 44 52 4f 50 20 54 41 42 4c 45 20 70 65 6e   "DROP TABLE pen
4be0: 64 69 6e 67 3b 22 0a 20 20 29 3b 0a 7d 0a        ding;".  );.}.