Hex Artifact Content
Not logged in

Artifact 00d4a363f4f87ed2fff4a3ff7bd6de563cea10f2:

File src/xfer.c part of check-in [916b6e4b3b] - Improvements to web-based user management. by drh on 2007-07-21 19:32:06.

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 63 6f 6e 74 65 6e 74 29 2c 20 2d 31 29  r(&content), -1)
0950: 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70   ){.    blob_app
0960: 65 6e 64 66 28 70 45 72 72 2c 20 22 63 6f 6e 74  endf(pErr, "cont
0970: 65 6e 74 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74  ent does not mat
0980: 63 68 20 73 68 61 31 20 68 61 73 68 22 29 3b 0a  ch sha1 hash");.
0990: 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74    }.  blob_reset
09a0: 28 26 68 61 73 68 29 3b 0a 20 20 72 69 64 20 3d  (&hash);.  rid =
09b0: 20 63 6f 6e 74 65 6e 74 5f 70 75 74 28 26 63 6f   content_put(&co
09c0: 6e 74 65 6e 74 2c 20 30 29 3b 0a 20 20 6d 61 6e  ntent, 0);.  man
09d0: 69 66 65 73 74 5f 63 72 6f 73 73 6c 69 6e 6b 28  ifest_crosslink(
09e0: 72 69 64 2c 20 26 63 6f 6e 74 65 6e 74 29 3b 0a  rid, &content);.
09f0: 20 20 69 66 28 20 72 69 64 3d 3d 30 20 29 7b 0a    if( rid==0 ){.
0a00: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
0a10: 28 70 45 72 72 2c 20 22 25 73 22 2c 20 67 2e 7a  (pErr, "%s", g.z
0a20: 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 0a  ErrMsg);.  }.}..
0a30: 2f 2a 0a 2a 2a 20 53 65 6e 64 20 74 68 65 20 66  /*.** Send the f
0a40: 69 6c 65 20 69 64 65 6e 74 69 66 69 65 64 20 62  ile identified b
0a50: 79 20 72 69 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  y rid..**.** If 
0a60: 70 4f 75 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c  pOut is not NULL
0a70: 2c 20 74 68 65 6e 20 61 70 70 65 6e 64 20 74 68  , then append th
0a80: 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 65  e text of the se
0a90: 6e 64 20 6d 65 73 73 61 67 65 0a 2a 2a 20 74 6f  nd message.** to
0aa0: 20 70 4f 75 74 2e 20 20 4f 74 68 65 72 77 69 73   pOut.  Otherwis
0ab0: 65 2c 20 61 70 70 65 6e 64 20 74 68 65 20 74 65  e, append the te
0ac0: 78 74 20 74 6f 20 74 68 65 20 43 47 49 20 6f 75  xt to the CGI ou
0ad0: 74 70 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tput..*/.static 
0ae0: 69 6e 74 20 73 65 6e 64 5f 66 69 6c 65 28 69 6e  int send_file(in
0af0: 74 20 72 69 64 2c 20 42 6c 6f 62 20 2a 70 4f 75  t rid, Blob *pOu
0b00: 74 29 7b 0a 20 20 42 6c 6f 62 20 63 6f 6e 74 65  t){.  Blob conte
0b10: 6e 74 2c 20 75 75 69 64 3b 0a 20 20 69 6e 74 20  nt, uuid;.  int 
0b20: 73 69 7a 65 3b 0a 0a 23 69 66 20 30 0a 53 45 4c  size;..#if 0.SEL
0b30: 45 43 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64  ECT srcid FROM d
0b40: 65 6c 74 61 0a 20 57 48 45 52 45 20 72 69 64 3d  elta. WHERE rid=
0b50: 25 64 0a 20 20 20 41 4e 44 20 45 58 49 53 54 53  %d.   AND EXISTS
0b60: 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f  (SELECT 1 FROM o
0b70: 6e 72 65 6d 6f 74 65 20 57 48 45 52 45 20 72 69  nremote WHERE ri
0b80: 64 3d 73 72 63 69 64 29 0a 55 4e 49 4f 4e 20 41  d=srcid).UNION A
0b90: 4c 4c 0a 53 45 4c 45 43 54 20 69 64 20 46 52 4f  LL.SELECT id FRO
0ba0: 4d 20 64 65 6c 74 61 0a 20 57 48 45 52 45 20 73  M delta. WHERE s
0bb0: 72 63 69 64 3d 25 64 0a 20 20 20 41 4e 44 20 45  rcid=%d.   AND E
0bc0: 58 49 53 54 53 28 53 45 4c 45 43 54 20 31 20 46  XISTS(SELECT 1 F
0bd0: 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57 48 45  ROM onremote WHE
0be0: 52 45 20 72 69 64 3d 64 65 6c 74 61 2e 72 69 64  RE rid=delta.rid
0bf0: 29 0a 4c 49 4d 49 54 20 31 0a 23 65 6e 64 69 66  ).LIMIT 1.#endif
0c00: 0a 0a 20 20 2f 2a 20 54 4f 44 4f 3a 0a 20 20 2a  ..  /* TODO:.  *
0c10: 2a 20 43 68 65 63 6b 20 66 6f 72 20 72 65 6c 61  * Check for rela
0c20: 74 65 64 20 66 69 6c 65 73 20 69 6e 20 74 68 65  ted files in the
0c30: 20 6f 6e 72 65 6d 6f 74 65 20 54 45 4d 50 20 74   onremote TEMP t
0c40: 61 62 6c 65 2e 20 20 49 66 20 72 65 6c 61 74 65  able.  If relate
0c50: 64 0a 20 20 2a 2a 20 66 69 6c 65 73 20 61 72 65  d.  ** files are
0c60: 20 66 6f 75 6e 64 2c 20 74 68 65 6e 20 73 65 6e   found, then sen
0c70: 64 20 61 20 64 65 6c 74 61 20 72 61 74 68 65 72  d a delta rather
0c80: 20 74 68 61 6e 20 74 68 65 20 77 68 6f 6c 65 20   than the whole 
0c90: 66 69 6c 65 2e 0a 20 20 2a 2f 0a 0a 20 20 62 6c  file..  */..  bl
0ca0: 6f 62 5f 7a 65 72 6f 28 26 75 75 69 64 29 3b 0a  ob_zero(&uuid);.
0cb0: 20 20 64 62 5f 62 6c 6f 62 28 26 75 75 69 64 2c    db_blob(&uuid,
0cc0: 20 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52   "SELECT uuid FR
0cd0: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69  OM blob WHERE ri
0ce0: 64 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30  d=%d AND size>=0
0cf0: 22 2c 20 72 69 64 29 3b 0a 20 20 69 66 28 20 62  ", rid);.  if( b
0d00: 6c 6f 62 5f 73 69 7a 65 28 26 75 75 69 64 29 3d  lob_size(&uuid)=
0d10: 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
0d20: 20 30 3b 0a 20 20 7d 0a 20 20 63 6f 6e 74 65 6e   0;.  }.  conten
0d30: 74 5f 67 65 74 28 72 69 64 2c 20 26 63 6f 6e 74  t_get(rid, &cont
0d40: 65 6e 74 29 3b 0a 20 20 73 69 7a 65 20 3d 20 62  ent);.  size = b
0d50: 6c 6f 62 5f 73 69 7a 65 28 26 63 6f 6e 74 65 6e  lob_size(&conten
0d60: 74 29 3b 0a 20 20 69 66 28 20 70 4f 75 74 20 29  t);.  if( pOut )
0d70: 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e  {.    blob_appen
0d80: 64 66 28 70 4f 75 74 2c 20 22 66 69 6c 65 20 25  df(pOut, "file %
0d90: 62 20 25 64 5c 6e 22 2c 20 26 75 75 69 64 2c 20  b %d\n", &uuid, 
0da0: 73 69 7a 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f  size);.    blob_
0db0: 61 70 70 65 6e 64 28 70 4f 75 74 2c 20 62 6c 6f  append(pOut, blo
0dc0: 62 5f 62 75 66 66 65 72 28 26 63 6f 6e 74 65 6e  b_buffer(&conten
0dd0: 74 29 2c 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c  t), size);.  }el
0de0: 73 65 7b 0a 20 20 20 20 63 67 69 5f 70 72 69 6e  se{.    cgi_prin
0df0: 74 66 28 22 66 69 6c 65 20 25 62 20 25 64 5c 6e  tf("file %b %d\n
0e00: 22 2c 20 26 75 75 69 64 2c 20 73 69 7a 65 29 3b  ", &uuid, size);
0e10: 0a 20 20 20 20 63 67 69 5f 61 70 70 65 6e 64 5f  .    cgi_append_
0e20: 63 6f 6e 74 65 6e 74 28 62 6c 6f 62 5f 62 75 66  content(blob_buf
0e30: 66 65 72 28 26 63 6f 6e 74 65 6e 74 29 2c 20 73  fer(&content), s
0e40: 69 7a 65 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62  ize);.  }.  blob
0e50: 5f 72 65 73 65 74 28 26 63 6f 6e 74 65 6e 74 29  _reset(&content)
0e60: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26  ;.  blob_reset(&
0e70: 75 75 69 64 29 3b 0a 20 20 64 62 5f 6d 75 6c 74  uuid);.  db_mult
0e80: 69 5f 65 78 65 63 28 22 49 4e 53 45 52 54 20 4f  i_exec("INSERT O
0e90: 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e  R IGNORE INTO on
0ea0: 72 65 6d 6f 74 65 20 56 41 4c 55 45 53 28 25 64  remote VALUES(%d
0eb0: 29 22 2c 20 72 69 64 29 3b 0a 20 20 72 65 74 75  )", rid);.  retu
0ec0: 72 6e 20 73 69 7a 65 3b 0a 7d 0a 0a 0a 2f 2a 0a  rn size;.}.../*.
0ed0: 2a 2a 20 53 65 6e 64 20 61 6c 6c 20 70 65 6e 64  ** Send all pend
0ee0: 69 6e 67 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74  ing files..*/.st
0ef0: 61 74 69 63 20 76 6f 69 64 20 73 65 6e 64 5f 61  atic void send_a
0f00: 6c 6c 5f 70 65 6e 64 69 6e 67 28 42 6c 6f 62 20  ll_pending(Blob 
0f10: 2a 70 4f 75 74 29 7b 0a 20 20 69 6e 74 20 73 65  *pOut){.  int se
0f20: 6e 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6d 61  nt = 0;.  int ma
0f30: 78 53 69 7a 65 20 3d 20 64 62 5f 67 65 74 5f 69  xSize = db_get_i
0f40: 6e 74 28 22 68 74 74 70 2d 6d 73 67 2d 73 69 7a  nt("http-msg-siz
0f50: 65 22 2c 20 31 30 30 30 30 30 30 29 3b 0a 20 20  e", 1000000);.  
0f60: 53 74 6d 74 20 71 3b 0a 23 69 66 20 30 0a 20 20  Stmt q;.#if 0.  
0f70: 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20  db_multi_exec(. 
0f80: 20 20 20 22 43 52 45 41 54 45 20 54 45 4d 50 20     "CREATE TEMP 
0f90: 54 41 42 4c 45 20 70 72 69 6f 72 69 74 79 28 72  TABLE priority(r
0fa0: 69 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41  id INTEGER PRIMA
0fb0: 52 59 20 4b 45 59 29 3b 22 0a 20 20 20 20 22 49  RY KEY);".    "I
0fc0: 4e 53 45 52 54 20 49 4e 54 4f 20 70 72 69 6f 72  NSERT INTO prior
0fd0: 69 74 79 22 0a 20 20 20 20 22 20 53 45 4c 45 43  ity".    " SELEC
0fe0: 54 20 73 72 63 69 64 20 46 52 4f 4d 20 64 65 6c  T srcid FROM del
0ff0: 74 61 22 0a 20 20 20 20 22 20 20 57 48 45 52 45  ta".    "  WHERE
1000: 20 45 58 49 53 54 53 28 53 45 4c 45 43 54 20 31   EXISTS(SELECT 1
1010: 20 46 52 4f 4d 20 6f 6e 72 65 6d 6f 74 65 20 57   FROM onremote W
1020: 48 45 52 45 20 6f 6e 72 65 6d 6f 74 65 2e 72 69  HERE onremote.ri
1030: 64 3d 64 65 6c 74 61 2e 72 69 64 29 22 0a 20 20  d=delta.rid)".  
1040: 20 20 22 20 20 20 20 41 4e 44 20 45 58 49 53 54    "    AND EXIST
1050: 53 28 53 45 4c 45 43 54 20 31 20 46 52 4f 4d 20  S(SELECT 1 FROM 
1060: 70 65 6e 64 69 6e 67 20 57 48 45 52 45 20 64 65  pending WHERE de
1070: 6c 74 61 2e 73 72 63 69 64 3d 70 65 6e 64 69 6e  lta.srcid=pendin
1080: 67 2e 72 69 64 29 3b 22 0a 20 20 20 20 22 49 4e  g.rid);".    "IN
1090: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49  SERT OR IGNORE I
10a0: 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 20  NTO priority".  
10b0: 20 20 22 20 53 45 4c 45 43 54 20 72 69 64 20 46    " SELECT rid F
10c0: 52 4f 4d 20 64 65 6c 74 61 22 0a 20 20 20 20 22  ROM delta".    "
10d0: 20 20 57 48 45 52 45 20 45 58 49 53 54 53 28 53    WHERE EXISTS(S
10e0: 45 4c 45 43 54 20 31 20 46 52 4f 4d 20 6f 6e 72  ELECT 1 FROM onr
10f0: 65 6d 6f 74 65 20 57 48 45 52 45 20 6f 6e 72 65  emote WHERE onre
1100: 6d 6f 74 65 2e 72 69 64 3d 64 65 6c 74 61 2e 73  mote.rid=delta.s
1110: 72 63 69 64 29 22 0a 20 20 20 20 22 20 20 20 20  rcid)".    "    
1120: 41 4e 44 20 45 58 49 53 54 53 28 53 45 4c 45 43  AND EXISTS(SELEC
1130: 54 20 31 20 46 52 4f 4d 20 70 65 6e 64 69 6e 67  T 1 FROM pending
1140: 20 57 48 45 52 45 20 64 65 6c 74 61 2e 72 69 64   WHERE delta.rid
1150: 3d 70 65 6e 64 69 6e 67 2e 72 69 64 29 3b 22 0a  =pending.rid);".
1160: 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 73 65    );.  while( se
1170: 6e 74 3c 6d 61 78 53 69 7a 65 20 26 26 20 28 72  nt<maxSize && (r
1180: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22  id = db_int(0, "
1190: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20  SELECT rid FROM 
11a0: 70 72 69 6f 72 69 74 79 22 29 29 21 3d 30 20 29  priority"))!=0 )
11b0: 7b 0a 20 20 20 20 73 65 6e 74 20 2b 3d 20 73 65  {.    sent += se
11c0: 6e 64 5f 66 69 6c 65 28 72 69 64 2c 20 70 4f 75  nd_file(rid, pOu
11d0: 74 29 3b 0a 20 20 20 20 64 62 5f 6d 75 6c 74 69  t);.    db_multi
11e0: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 22 49 4e  _exec(.      "IN
11f0: 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49  SERT OR IGNORE I
1200: 4e 54 4f 20 70 72 69 6f 72 69 74 79 22 0a 20 20  NTO priority".  
1210: 20 20 20 20 22 20 53 45 4c 45 43 54 20 73 72 63      " SELECT src
1220: 69 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48  id FROM delta WH
1230: 45 52 45 20 72 69 64 3d 25 64 22 0a 20 20 20 20  ERE rid=%d".    
1240: 20 20 22 20 55 4e 49 4f 4e 20 41 4c 4c 22 0a 20    " UNION ALL". 
1250: 20 20 20 20 20 22 20 53 45 4c 45 43 54 20 72 69       " SELECT ri
1260: 64 20 46 52 4f 4d 20 64 65 6c 74 61 20 57 48 45  d FROM delta WHE
1270: 52 45 20 73 72 63 69 64 3d 25 64 22 2c 0a 20 20  RE srcid=%d",.  
1280: 20 20 20 20 72 69 64 2c 20 72 69 64 0a 20 20 20      rid, rid.   
1290: 20 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   );.  }.#endif. 
12a0: 20 64 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20   db_prepare(&q, 
12b0: 22 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d  "SELECT rid FROM
12c0: 20 70 65 6e 64 69 6e 67 22 29 3b 0a 20 20 77 68   pending");.  wh
12d0: 69 6c 65 28 20 64 62 5f 73 74 65 70 28 26 71 29  ile( db_step(&q)
12e0: 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
12f0: 20 20 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62      int rid = db
1300: 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71 2c 20  _column_int(&q, 
1310: 30 29 3b 0a 20 20 20 20 69 66 28 20 73 65 6e 74  0);.    if( sent
1320: 3c 6d 61 78 53 69 7a 65 20 29 7b 0a 20 20 20 20  <maxSize ){.    
1330: 20 20 73 65 6e 74 20 2b 3d 20 73 65 6e 64 5f 66    sent += send_f
1340: 69 6c 65 28 72 69 64 2c 20 70 4f 75 74 29 3b 0a  ile(rid, pOut);.
1350: 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1360: 20 63 68 61 72 20 2a 7a 55 75 69 64 20 3d 20 64   char *zUuid = d
1370: 62 5f 74 65 78 74 28 30 2c 0a 20 20 20 20 20 20  b_text(0,.      
1380: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1390: 22 53 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f  "SELECT uuid FRO
13a0: 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 72 69 64  M blob WHERE rid
13b0: 3d 25 64 20 41 4e 44 20 73 69 7a 65 3e 3d 30 22  =%d AND size>=0"
13c0: 2c 20 72 69 64 29 3b 0a 20 20 20 20 20 20 69 66  , rid);.      if
13d0: 28 20 7a 55 75 69 64 20 29 7b 0a 20 20 20 20 20  ( zUuid ){.     
13e0: 20 20 20 69 66 28 20 70 4f 75 74 20 29 7b 0a 20     if( pOut ){. 
13f0: 20 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61 70           blob_ap
1400: 70 65 6e 64 66 28 70 4f 75 74 2c 20 22 69 67 6f  pendf(pOut, "igo
1410: 74 20 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b  t %s\n", zUuid);
1420: 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  .        }else{.
1430: 20 20 20 20 20 20 20 20 20 20 63 67 69 5f 70 72            cgi_pr
1440: 69 6e 74 66 28 22 69 67 6f 74 20 25 73 5c 6e 22  intf("igot %s\n"
1450: 2c 20 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20  , zUuid);.      
1460: 20 20 7d 0a 20 20 20 20 20 20 20 20 66 72 65 65    }.        free
1470: 28 7a 55 75 69 64 29 3b 0a 20 20 20 20 20 20 7d  (zUuid);.      }
1480: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 64 62 5f  .    }.  }.  db_
1490: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20  finalize(&q);.  
14a0: 0a 23 69 66 20 30 0a 20 20 64 62 5f 6d 75 6c 74  .#if 0.  db_mult
14b0: 69 5f 65 78 65 63 28 22 44 52 4f 50 20 54 41 42  i_exec("DROP TAB
14c0: 4c 45 20 70 72 69 6f 72 69 74 79 22 29 3b 0a 23  LE priority");.#
14d0: 65 6e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  endif.}.../*.** 
14e0: 43 68 65 63 6b 20 74 68 65 20 73 69 67 6e 61 74  Check the signat
14f0: 75 72 65 20 6f 6e 20 61 6e 20 61 70 70 6c 69 63  ure on an applic
1500: 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 20 70  ation/x-fossil p
1510: 61 79 6c 6f 61 64 20 72 65 63 65 69 76 65 64 20  ayload received 
1520: 62 79 0a 2a 2a 20 74 68 65 20 48 54 54 50 20 73  by.** the HTTP s
1530: 65 72 76 65 72 2e 20 20 54 68 65 20 73 69 67 6e  erver.  The sign
1540: 61 74 75 72 65 20 69 73 20 61 20 6c 69 6e 65 20  ature is a line 
1550: 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
1560: 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   form:.**.**    
1570: 20 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e 20      login LOGIN 
1580: 4e 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45 0a  NONCE SIGNATURE.
1590: 2a 2a 0a 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20  **.** The NONCE 
15a0: 69 73 20 61 20 72 61 6e 64 6f 6d 20 73 74 72 69  is a random stri
15b0: 6e 67 2e 20 20 54 68 65 20 73 65 72 76 65 72 20  ng.  The server 
15c0: 77 69 6c 6c 20 6e 65 76 65 72 20 61 63 63 65 70  will never accep
15d0: 74 20 61 0a 2a 2a 20 72 65 70 65 61 74 20 4e 4f  t a.** repeat NO
15e0: 4e 43 45 2e 20 20 53 49 47 4e 41 54 55 52 45 20  NCE.  SIGNATURE 
15f0: 69 73 20 74 68 65 20 53 48 41 31 20 63 68 65 63  is the SHA1 chec
1600: 6b 73 75 6d 20 6f 66 20 74 68 65 20 4e 4f 4e 43  ksum of the NONC
1610: 45 20 0a 2a 2a 20 63 6f 6e 63 61 74 65 6e 61 74  E .** concatenat
1620: 65 64 20 77 69 74 68 20 74 68 65 20 75 73 65 72  ed with the user
1630: 73 20 70 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a  s password..**.*
1640: 2a 20 54 68 65 20 70 61 72 61 6d 65 74 65 72 73  * The parameters
1650: 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65   to this routine
1660: 20 61 72 65 20 65 70 68 65 72 6d 65 72 61 6c 20   are ephermeral 
1670: 62 6c 6f 62 73 20 68 6f 6c 64 69 6e 67 20 74 68  blobs holding th
1680: 65 0a 2a 2a 20 4c 4f 47 49 4e 2c 20 4e 4f 4e 43  e.** LOGIN, NONC
1690: 45 20 61 6e 64 20 53 49 47 4e 41 54 55 52 45 2e  E and SIGNATURE.
16a0: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
16b0: 69 6e 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20  ine attempts to 
16c0: 6c 6f 63 61 74 65 20 74 68 65 20 75 73 65 72 20  locate the user 
16d0: 61 6e 64 20 76 65 72 69 66 79 20 74 68 65 20 73  and verify the s
16e0: 69 67 6e 61 74 75 72 65 2e 0a 2a 2a 20 49 66 20  ignature..** If 
16f0: 65 76 65 72 79 74 68 69 6e 67 20 63 68 65 63 6b  everything check
1700: 73 20 6f 75 74 2c 20 74 68 65 20 55 53 45 52 2e  s out, the USER.
1710: 43 41 50 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 74  CAP column for t
1720: 68 65 20 55 53 45 52 20 74 61 62 6c 65 0a 2a 2a  he USER table.**
1730: 20 69 73 20 63 6f 6e 73 75 6c 74 65 64 20 74 6f   is consulted to
1740: 20 73 65 74 20 70 72 69 76 69 6c 65 67 65 73 20   set privileges 
1750: 69 6e 20 74 68 65 20 67 6c 6f 62 61 6c 20 67 20  in the global g 
1760: 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  variable..**.** 
1770: 49 66 20 61 6e 79 74 68 69 6e 67 20 66 61 69 6c  If anything fail
1780: 73 20 74 6f 20 63 68 65 63 6b 20 6f 75 74 2c 20  s to check out, 
1790: 6e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d  no changes are m
17a0: 61 64 65 20 74 6f 20 70 72 69 76 69 6c 65 67 65  ade to privilege
17b0: 73 2e 0a 2a 2a 0a 2a 2a 20 53 69 67 6e 61 74 75  s..**.** Signatu
17c0: 72 65 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 6e  re generation on
17d0: 20 74 68 65 20 63 6c 69 65 6e 74 20 73 69 64 65   the client side
17e0: 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79 20 74   is handled by t
17f0: 68 65 20 0a 2a 2a 20 68 74 74 70 5f 65 78 63 68  he .** http_exch
1800: 61 6e 67 65 28 29 20 72 6f 75 74 69 6e 65 2e 0a  ange() routine..
1810: 2a 2f 0a 76 6f 69 64 20 63 68 65 63 6b 5f 6c 6f  */.void check_lo
1820: 67 69 6e 28 42 6c 6f 62 20 2a 70 4c 6f 67 69 6e  gin(Blob *pLogin
1830: 2c 20 42 6c 6f 62 20 2a 70 4e 6f 6e 63 65 2c 20  , Blob *pNonce, 
1840: 42 6c 6f 62 20 2a 70 53 69 67 29 7b 0a 20 20 53  Blob *pSig){.  S
1850: 74 6d 74 20 71 3b 0a 20 20 69 6e 74 20 72 63 3b  tmt q;.  int rc;
1860: 0a 0a 20 20 69 66 28 20 64 62 5f 65 78 69 73 74  ..  if( db_exist
1870: 73 28 22 53 45 4c 45 43 54 20 31 20 46 52 4f 4d  s("SELECT 1 FROM
1880: 20 72 63 76 66 72 6f 6d 20 57 48 45 52 45 20 6e   rcvfrom WHERE n
1890: 6f 6e 63 65 3d 25 42 22 2c 20 70 4e 6f 6e 63 65  once=%B", pNonce
18a0: 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ) ){.    return;
18b0: 20 20 2f 2a 20 4e 65 76 65 72 20 61 63 63 65 70    /* Never accep
18c0: 74 20 61 20 72 65 70 65 61 74 65 64 20 6e 6f 6e  t a repeated non
18d0: 63 65 20 2a 2f 0a 20 20 7d 0a 20 20 64 62 5f 70  ce */.  }.  db_p
18e0: 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45  repare(&q, "SELE
18f0: 43 54 20 70 77 2c 20 63 61 70 2c 20 75 69 64 20  CT pw, cap, uid 
1900: 46 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20  FROM user WHERE 
1910: 6c 6f 67 69 6e 3d 25 42 22 2c 20 70 4c 6f 67 69  login=%B", pLogi
1920: 6e 29 3b 0a 20 20 69 66 28 20 64 62 5f 73 74 65  n);.  if( db_ste
1930: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
1940: 57 20 29 7b 0a 20 20 20 20 42 6c 6f 62 20 70 77  W ){.    Blob pw
1950: 2c 20 63 6f 6d 62 69 6e 65 64 2c 20 68 61 73 68  , combined, hash
1960: 3b 0a 20 20 20 20 62 6c 6f 62 5f 7a 65 72 6f 28  ;.    blob_zero(
1970: 26 70 77 29 3b 0a 20 20 20 20 64 62 5f 65 70 68  &pw);.    db_eph
1980: 65 6d 65 72 61 6c 5f 62 6c 6f 62 28 26 71 2c 20  emeral_blob(&q, 
1990: 30 2c 20 26 70 77 29 3b 0a 20 20 20 20 62 6c 6f  0, &pw);.    blo
19a0: 62 5f 7a 65 72 6f 28 26 63 6f 6d 62 69 6e 65 64  b_zero(&combined
19b0: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79  );.    blob_copy
19c0: 28 26 63 6f 6d 62 69 6e 65 64 2c 20 70 4e 6f 6e  (&combined, pNon
19d0: 63 65 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70  ce);.    blob_ap
19e0: 70 65 6e 64 28 26 63 6f 6d 62 69 6e 65 64 2c 20  pend(&combined, 
19f0: 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 70 77 29  blob_buffer(&pw)
1a00: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 77 29  , blob_size(&pw)
1a10: 29 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62  );.    sha1sum_b
1a20: 6c 6f 62 28 26 63 6f 6d 62 69 6e 65 64 2c 20 26  lob(&combined, &
1a30: 68 61 73 68 29 3b 0a 20 20 20 20 72 63 20 3d 20  hash);.    rc = 
1a40: 62 6c 6f 62 5f 63 6f 6d 70 61 72 65 28 26 68 61  blob_compare(&ha
1a50: 73 68 2c 20 70 53 69 67 29 3b 0a 20 20 20 20 62  sh, pSig);.    b
1a60: 6c 6f 62 5f 72 65 73 65 74 28 26 68 61 73 68 29  lob_reset(&hash)
1a70: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
1a80: 28 26 63 6f 6d 62 69 6e 65 64 29 3b 0a 20 20 20  (&combined);.   
1a90: 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20   if( rc==0 ){.  
1aa0: 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1ab0: 7a 43 61 70 3b 0a 20 20 20 20 20 20 7a 43 61 70  zCap;.      zCap
1ac0: 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78   = db_column_tex
1ad0: 74 28 26 71 2c 20 31 29 3b 0a 20 20 20 20 20 20  t(&q, 1);.      
1ae0: 6c 6f 67 69 6e 5f 73 65 74 5f 63 61 70 61 62 69  login_set_capabi
1af0: 6c 69 74 69 65 73 28 7a 43 61 70 29 3b 0a 20 20  lities(zCap);.  
1b00: 20 20 20 20 67 2e 75 73 65 72 55 69 64 20 3d 20      g.userUid = 
1b10: 64 62 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 28 26 71  db_column_int(&q
1b20: 2c 20 32 29 3b 0a 20 20 20 20 20 20 67 2e 7a 4c  , 2);.      g.zL
1b30: 6f 67 69 6e 20 3d 20 6d 70 72 69 6e 74 66 28 22  ogin = mprintf("
1b40: 25 62 22 2c 20 70 4c 6f 67 69 6e 29 3b 0a 20 20  %b", pLogin);.  
1b50: 20 20 20 20 67 2e 7a 4e 6f 6e 63 65 20 3d 20 6d      g.zNonce = m
1b60: 70 72 69 6e 74 66 28 22 25 62 22 2c 20 70 4e 6f  printf("%b", pNo
1b70: 6e 63 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  nce);.    }.  }.
1b80: 20 20 64 62 5f 72 65 73 65 74 28 26 71 29 3b 0a    db_reset(&q);.
1b90: 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 69  }.../*.** If thi
1ba0: 73 20 76 61 72 69 61 62 6c 65 20 69 73 20 73 65  s variable is se
1bb0: 74 2c 20 64 69 73 61 62 6c 65 20 6c 6f 67 69 6e  t, disable login
1bc0: 20 63 68 65 63 6b 73 2e 20 20 55 73 65 64 20 66   checks.  Used f
1bd0: 6f 72 20 64 65 62 75 67 67 69 6e 67 0a 2a 2a 20  or debugging.** 
1be0: 6f 6e 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  only..*/.static 
1bf0: 69 6e 74 20 64 69 73 61 62 6c 65 4c 6f 67 69 6e  int disableLogin
1c00: 20 3d 20 30 3b 0a 0a 2f 2a 0a 2a 2a 20 57 45 42   = 0;../*.** WEB
1c10: 50 41 47 45 3a 20 78 66 65 72 0a 2a 2a 0a 2a 2a  PAGE: xfer.**.**
1c20: 20 54 68 69 73 20 69 73 20 74 68 65 20 74 72 61   This is the tra
1c30: 6e 73 66 65 72 20 68 61 6e 64 6c 65 72 20 6f 6e  nsfer handler on
1c40: 20 74 68 65 20 73 65 72 76 65 72 20 73 69 64 65   the server side
1c50: 2e 20 20 54 68 65 20 74 72 61 6e 73 66 65 72 0a  .  The transfer.
1c60: 2a 2a 20 6d 65 73 73 61 67 65 20 68 61 73 20 62  ** message has b
1c70: 65 65 6e 20 75 6e 63 6f 6d 70 72 65 73 73 65 64  een uncompressed
1c80: 20 61 6e 64 20 70 6c 61 63 65 64 20 69 6e 20 74   and placed in t
1c90: 68 65 20 67 2e 63 67 69 49 6e 20 62 6c 6f 62 2e  he g.cgiIn blob.
1ca0: 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74 68 69 73  .** Process this
1cb0: 20 6d 65 73 73 61 67 65 20 61 6e 64 20 66 6f 72   message and for
1cc0: 6d 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  m an appropriate
1cd0: 20 72 65 70 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20   reply..*/.void 
1ce0: 70 61 67 65 5f 78 66 65 72 28 76 6f 69 64 29 7b  page_xfer(void){
1cf0: 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 0a 20  .  int nToken;. 
1d00: 20 69 6e 74 20 69 73 50 75 6c 6c 20 3d 20 30 3b   int isPull = 0;
1d10: 0a 20 20 69 6e 74 20 69 73 50 75 73 68 20 3d 20  .  int isPush = 
1d20: 30 3b 0a 20 20 69 6e 74 20 6e 45 72 72 20 3d 20  0;.  int nErr = 
1d30: 30 3b 0a 20 20 42 6c 6f 62 20 6c 69 6e 65 2c 20  0;.  Blob line, 
1d40: 65 72 72 6d 73 67 2c 20 61 54 6f 6b 65 6e 5b 35  errmsg, aToken[5
1d50: 5d 3b 0a 0a 20 20 64 62 5f 62 65 67 69 6e 5f 74  ];..  db_begin_t
1d60: 72 61 6e 73 61 63 74 69 6f 6e 28 29 3b 0a 20 20  ransaction();.  
1d70: 62 6c 6f 62 61 72 72 61 79 5f 7a 65 72 6f 28 61  blobarray_zero(a
1d80: 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f  Token, count(aTo
1d90: 6b 65 6e 29 29 3b 0a 20 20 63 67 69 5f 73 65 74  ken));.  cgi_set
1da0: 5f 63 6f 6e 74 65 6e 74 5f 74 79 70 65 28 67 2e  _content_type(g.
1db0: 7a 43 6f 6e 74 65 6e 74 54 79 70 65 29 3b 0a 20  zContentType);. 
1dc0: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 65 72 72 6d   blob_zero(&errm
1dd0: 73 67 29 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f  sg);.  db_multi_
1de0: 65 78 65 63 28 0a 20 20 20 20 20 22 43 52 45 41  exec(.     "CREA
1df0: 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 6f 6e  TE TEMP TABLE on
1e00: 72 65 6d 6f 74 65 28 72 69 64 20 49 4e 54 45 47  remote(rid INTEG
1e10: 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 29 3b  ER PRIMARY KEY);
1e20: 22 20 2f 2a 20 43 6c 69 65 6e 74 20 68 61 73 20  " /* Client has 
1e30: 2a 2f 0a 20 20 20 20 20 22 43 52 45 41 54 45 20  */.     "CREATE 
1e40: 54 45 4d 50 20 54 41 42 4c 45 20 70 65 6e 64 69  TEMP TABLE pendi
1e50: 6e 67 28 72 69 64 20 49 4e 54 45 47 45 52 20 50  ng(rid INTEGER P
1e60: 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 20 20 2f  RIMARY KEY);"  /
1e70: 2a 20 43 6c 69 65 6e 74 20 6e 65 65 64 73 20 2a  * Client needs *
1e80: 2f 0a 20 20 29 3b 0a 20 20 77 68 69 6c 65 28 20  /.  );.  while( 
1e90: 62 6c 6f 62 5f 6c 69 6e 65 28 26 67 2e 63 67 69  blob_line(&g.cgi
1ea0: 49 6e 2c 20 26 6c 69 6e 65 29 20 29 7b 0a 20 20  In, &line) ){.  
1eb0: 20 20 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f    nToken = blob_
1ec0: 74 6f 6b 65 6e 69 7a 65 28 26 6c 69 6e 65 2c 20  tokenize(&line, 
1ed0: 61 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54  aToken, count(aT
1ee0: 6f 6b 65 6e 29 29 3b 0a 0a 20 20 20 20 2f 2a 20  oken));..    /* 
1ef0: 20 20 66 69 6c 65 20 55 55 49 44 20 53 49 5a 45    file UUID SIZE
1f00: 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20   \n CONTENT.    
1f10: 2a 2a 20 20 20 66 69 6c 65 20 55 55 49 44 20 44  **   file UUID D
1f20: 45 4c 54 41 53 52 43 20 53 49 5a 45 20 5c 6e 20  ELTASRC SIZE \n 
1f30: 43 4f 4e 54 45 4e 54 0a 20 20 20 20 2a 2a 0a 20  CONTENT.    **. 
1f40: 20 20 20 2a 2a 20 41 63 63 65 70 74 20 61 20 66     ** Accept a f
1f50: 69 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 6c 69  ile from the cli
1f60: 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ent..    */.    
1f70: 69 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f  if( blob_eq(&aTo
1f80: 6b 65 6e 5b 30 5d 2c 20 22 66 69 6c 65 22 29 20  ken[0], "file") 
1f90: 26 26 20 6e 54 6f 6b 65 6e 3e 3d 32 20 26 26 20  && nToken>=2 && 
1fa0: 6e 54 6f 6b 65 6e 3c 3d 33 20 29 7b 0a 20 20 20  nToken<=3 ){.   
1fb0: 20 20 20 69 66 28 20 21 69 73 50 75 73 68 20 29     if( !isPush )
1fc0: 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f 72 65  {.        cgi_re
1fd0: 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20  set_content();. 
1fe0: 20 20 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e         @ error n
1ff0: 6f 74 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73  ot\sauthorized\s
2000: 74 6f 5c 73 77 72 69 74 65 0a 20 20 20 20 20 20  to\swrite.      
2010: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
2020: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2030: 0a 20 20 20 20 20 20 78 66 65 72 5f 61 63 63 65  .      xfer_acce
2040: 70 74 5f 66 69 6c 65 28 26 67 2e 63 67 69 49 6e  pt_file(&g.cgiIn
2050: 2c 20 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e  , aToken, nToken
2060: 2c 20 26 65 72 72 6d 73 67 29 3b 0a 20 20 20 20  , &errmsg);.    
2070: 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65 28    if( blob_size(
2080: 26 65 72 72 6d 73 67 29 20 29 7b 0a 20 20 20 20  &errmsg) ){.    
2090: 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f      cgi_reset_co
20a0: 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 20  ntent();.       
20b0: 20 40 20 65 72 72 6f 72 20 25 54 28 62 6c 6f 62   @ error %T(blob
20c0: 5f 73 74 72 28 26 65 72 72 6d 73 67 29 29 0a 20  _str(&errmsg)). 
20d0: 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20         nErr++;. 
20e0: 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
20f0: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a      }.    }else.
2100: 0a 20 20 20 20 2f 2a 20 20 20 67 69 6d 6d 65 20  .    /*   gimme 
2110: 55 55 49 44 0a 20 20 20 20 2a 2a 0a 20 20 20 20  UUID.    **.    
2120: 2a 2a 20 43 6c 69 65 6e 74 20 69 73 20 72 65 71  ** Client is req
2130: 75 65 73 74 69 6e 67 20 61 20 66 69 6c 65 0a 20  uesting a file. 
2140: 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 62 6c     */.    if( bl
2150: 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d  ob_eq(&aToken[0]
2160: 2c 20 22 67 69 6d 6d 65 22 29 20 26 26 20 6e 54  , "gimme") && nT
2170: 6f 6b 65 6e 3d 3d 32 20 26 26 20 62 6c 6f 62 5f  oken==2 && blob_
2180: 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b  is_uuid(&aToken[
2190: 31 5d 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28  1]) ){.      if(
21a0: 20 69 73 50 75 6c 6c 20 29 7b 0a 20 20 20 20 20   isPull ){.     
21b0: 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63     db_multi_exec
21c0: 28 0a 20 20 20 20 20 20 20 20 20 20 22 49 4e 53  (.          "INS
21d0: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e  ERT OR IGNORE IN
21e0: 54 4f 20 70 65 6e 64 69 6e 67 28 72 69 64 29 20  TO pending(rid) 
21f0: 22 0a 20 20 20 20 20 20 20 20 20 20 22 53 45 4c  ".          "SEL
2200: 45 43 54 20 72 69 64 20 46 52 4f 4d 20 62 6c 6f  ECT rid FROM blo
2210: 62 20 57 48 45 52 45 20 75 75 69 64 3d 25 42 20  b WHERE uuid=%B 
2220: 41 4e 44 20 73 69 7a 65 3e 3d 30 22 2c 20 26 61  AND size>=0", &a
2230: 54 6f 6b 65 6e 5b 31 5d 0a 20 20 20 20 20 20 20  Token[1].       
2240: 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   );.      }.    
2250: 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20  }else..    /*   
2260: 69 67 6f 74 20 55 55 49 44 0a 20 20 20 20 2a 2a  igot UUID.    **
2270: 20 20 20 6c 65 61 66 20 55 55 49 44 0a 20 20 20     leaf UUID.   
2280: 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 6c 69 65 6e   **.    ** Clien
2290: 74 20 61 6e 6e 6f 75 6e 63 65 73 20 74 68 61 74  t announces that
22a0: 20 69 74 20 68 61 73 20 61 20 70 61 72 74 69 63   it has a partic
22b0: 75 6c 61 72 20 66 69 6c 65 0a 20 20 20 20 2a 2f  ular file.    */
22c0: 0a 20 20 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d  .    if( nToken=
22d0: 3d 32 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  =2.          && 
22e0: 28 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e  (blob_eq(&aToken
22f0: 5b 30 5d 2c 20 22 69 67 6f 74 22 29 20 7c 7c 20  [0], "igot") || 
2300: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b  blob_eq(&aToken[
2310: 30 5d 2c 22 6c 65 61 66 22 29 29 0a 20 20 20 20  0],"leaf")).    
2320: 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73        && blob_is
2330: 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d  _uuid(&aToken[1]
2340: 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69  ) ){.      if( i
2350: 73 50 75 6c 6c 20 7c 7c 20 69 73 50 75 73 68 20  sPull || isPush 
2360: 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  ){.        int r
2370: 69 64 20 3d 20 64 62 5f 69 6e 74 28 30 2c 20 22  id = db_int(0, "
2380: 53 45 4c 45 43 54 20 72 69 64 20 46 52 4f 4d 20  SELECT rid FROM 
2390: 62 6c 6f 62 20 57 48 45 52 45 20 75 75 69 64 3d  blob WHERE uuid=
23a0: 25 42 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 29  %B", &aToken[1])
23b0: 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 69  ;.        if( ri
23c0: 64 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  d>0 ){.         
23d0: 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a   db_multi_exec(.
23e0: 20 20 20 20 20 20 20 20 20 20 20 20 22 49 4e 53              "INS
23f0: 45 52 54 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e  ERT OR IGNORE IN
2400: 54 4f 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 29  TO onremote(rid)
2410: 20 56 41 4c 55 45 53 28 25 64 29 22 2c 20 72 69   VALUES(%d)", ri
2420: 64 0a 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20  d.          );. 
2430: 20 20 20 20 20 20 20 20 20 69 66 28 20 69 73 50           if( isP
2440: 75 6c 6c 20 26 26 20 62 6c 6f 62 5f 65 71 28 26  ull && blob_eq(&
2450: 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 65 61 66  aToken[0], "leaf
2460: 22 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ") ){.          
2470: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
2480: 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22  .              "
2490: 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52 45  INSERT OR IGNORE
24a0: 20 49 4e 54 4f 20 70 65 6e 64 69 6e 67 28 72 69   INTO pending(ri
24b0: 64 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20  d) ".           
24c0: 20 20 20 22 53 45 4c 45 43 54 20 63 69 64 20 46     "SELECT cid F
24d0: 52 4f 4d 20 70 6c 69 6e 6b 20 57 48 45 52 45 20  ROM plink WHERE 
24e0: 70 69 64 3d 25 64 22 2c 20 72 69 64 0a 20 20 20  pid=%d", rid.   
24f0: 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20           );.    
2500: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2510: 7d 65 6c 73 65 20 69 66 28 20 69 73 50 75 73 68  }else if( isPush
2520: 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 6f   ){.          co
2530: 6e 74 65 6e 74 5f 70 75 74 28 30 2c 20 62 6c 6f  ntent_put(0, blo
2540: 62 5f 73 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d  b_str(&aToken[1]
2550: 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ));.        }.  
2560: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 0a      }.    }else.
2570: 0a 20 20 20 20 2f 2a 20 20 20 20 70 75 6c 6c 20  .    /*    pull 
2580: 20 53 45 52 56 45 52 43 4f 44 45 20 20 50 52 4f   SERVERCODE  PRO
2590: 4a 45 43 54 43 4f 44 45 0a 20 20 20 20 2a 2a 20  JECTCODE.    ** 
25a0: 20 20 20 70 75 73 68 20 20 53 45 52 56 45 52 43     push  SERVERC
25b0: 4f 44 45 20 20 50 52 4f 4a 45 43 54 43 4f 44 45  ODE  PROJECTCODE
25c0: 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
25d0: 68 65 20 63 6c 69 65 6e 74 20 77 61 6e 74 73 20  he client wants 
25e0: 65 69 74 68 65 72 20 73 65 6e 64 20 6f 72 20 72  either send or r
25f0: 65 63 65 69 76 65 0a 20 20 20 20 2a 2f 0a 20 20  eceive.    */.  
2600: 20 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 33 0a    if( nToken==3.
2610: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26                 &
2620: 26 20 28 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b  & (blob_eq(&aTok
2630: 65 6e 5b 30 5d 2c 20 22 70 75 6c 6c 22 29 20 7c  en[0], "pull") |
2640: 7c 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65  | blob_eq(&aToke
2650: 6e 5b 30 5d 2c 20 22 70 75 73 68 22 29 29 0a 20  n[0], "push")). 
2660: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26                &&
2670: 20 62 6c 6f 62 5f 69 73 5f 75 75 69 64 28 26 61   blob_is_uuid(&a
2680: 54 6f 6b 65 6e 5b 31 5d 29 20 26 26 20 62 6c 6f  Token[1]) && blo
2690: 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b 65  b_is_uuid(&aToke
26a0: 6e 5b 32 5d 29 20 29 7b 0a 20 20 20 20 20 20 63  n[2]) ){.      c
26b0: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 43 6f 64  onst char *zSCod
26c0: 65 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63  e;.      const c
26d0: 68 61 72 20 2a 7a 50 43 6f 64 65 3b 0a 0a 20 20  har *zPCode;..  
26e0: 20 20 20 20 7a 53 43 6f 64 65 20 3d 20 64 62 5f      zSCode = db_
26f0: 67 65 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65  get("server-code
2700: 22 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66 28  ", 0);.      if(
2710: 20 7a 53 43 6f 64 65 3d 3d 30 20 29 7b 0a 20 20   zSCode==0 ){.  
2720: 20 20 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e        fossil_pan
2730: 69 63 28 22 6d 69 73 73 69 6e 67 20 73 65 72 76  ic("missing serv
2740: 65 72 20 63 6f 64 65 22 29 3b 0a 20 20 20 20 20  er code");.     
2750: 20 7d 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f   }.      if( blo
2760: 62 5f 65 71 5f 73 74 72 28 26 61 54 6f 6b 65 6e  b_eq_str(&aToken
2770: 5b 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31 29  [1], zSCode, -1)
2780: 20 29 7b 0a 20 20 20 20 20 20 20 20 63 67 69 5f   ){.        cgi_
2790: 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29 3b  reset_content();
27a0: 0a 20 20 20 20 20 20 20 20 40 20 65 72 72 6f 72  .        @ error
27b0: 20 73 65 72 76 65 72 5c 73 6c 6f 6f 70 0a 20 20   server\sloop.  
27c0: 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20        nErr++;.  
27d0: 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
27e0: 20 20 20 7d 0a 20 20 20 20 20 20 7a 50 43 6f 64     }.      zPCod
27f0: 65 20 3d 20 64 62 5f 67 65 74 28 22 70 72 6f 6a  e = db_get("proj
2800: 65 63 74 2d 63 6f 64 65 22 2c 20 30 29 3b 0a 20  ect-code", 0);. 
2810: 20 20 20 20 20 69 66 28 20 7a 50 43 6f 64 65 3d       if( zPCode=
2820: 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f  =0 ){.        fo
2830: 73 73 69 6c 5f 70 61 6e 69 63 28 22 6d 69 73 73  ssil_panic("miss
2840: 69 6e 67 20 70 72 6f 6a 65 63 74 20 63 6f 64 65  ing project code
2850: 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ");.      }.    
2860: 20 20 69 66 28 20 21 62 6c 6f 62 5f 65 71 5f 73    if( !blob_eq_s
2870: 74 72 28 26 61 54 6f 6b 65 6e 5b 32 5d 2c 20 7a  tr(&aToken[2], z
2880: 50 43 6f 64 65 2c 20 2d 31 29 20 29 7b 0a 20 20  PCode, -1) ){.  
2890: 20 20 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f        cgi_reset_
28a0: 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20  content();.     
28b0: 20 20 20 40 20 65 72 72 6f 72 20 77 72 6f 6e 67     @ error wrong
28c0: 5c 73 70 72 6f 6a 65 63 74 0a 20 20 20 20 20 20  \sproject.      
28d0: 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20    nErr++;.      
28e0: 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
28f0: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
2900: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22  eq(&aToken[0], "
2910: 70 75 6c 6c 22 29 20 29 7b 0a 20 20 20 20 20 20  pull") ){.      
2920: 20 20 69 66 28 20 21 67 2e 6f 6b 52 65 61 64 20    if( !g.okRead 
2930: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 67 69  ){.          cgi
2940: 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e 74 28 29  _reset_content()
2950: 3b 0a 20 20 20 20 20 20 20 20 20 20 40 20 65 72  ;.          @ er
2960: 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68 6f 72 69  ror not\sauthori
2970: 7a 65 64 5c 73 74 6f 5c 73 72 65 61 64 0a 20 20  zed\sto\sread.  
2980: 20 20 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b 0a          nErr++;.
2990: 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
29a0: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29b0: 20 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a 20     isPull = 1;. 
29c0: 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
29d0: 20 20 20 20 69 66 28 20 21 67 2e 6f 6b 57 72 69      if( !g.okWri
29e0: 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  te ){.          
29f0: 63 67 69 5f 72 65 73 65 74 5f 63 6f 6e 74 65 6e  cgi_reset_conten
2a00: 74 28 29 3b 0a 20 20 20 20 20 20 20 20 20 20 40  t();.          @
2a10: 20 65 72 72 6f 72 20 6e 6f 74 5c 73 61 75 74 68   error not\sauth
2a20: 6f 72 69 7a 65 64 5c 73 74 6f 5c 73 77 72 69 74  orized\sto\swrit
2a30: 65 0a 20 20 20 20 20 20 20 20 20 20 6e 45 72 72  e.          nErr
2a40: 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72  ++;.          br
2a50: 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
2a60: 20 20 20 20 20 20 20 69 73 50 75 73 68 20 3d 20         isPush = 
2a70: 31 3b 0a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  1;..      }.    
2a80: 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a 20 20 20  }else..    /*   
2a90: 20 63 6c 6f 6e 65 0a 20 20 20 20 2a 2a 0a 20 20   clone.    **.  
2aa0: 20 20 2a 2a 20 54 68 65 20 63 6c 69 65 6e 74 20    ** The client 
2ab0: 6b 6e 6f 77 73 20 6e 6f 74 68 69 6e 67 2e 20 20  knows nothing.  
2ac0: 54 65 6c 6c 20 61 6c 6c 2e 0a 20 20 20 20 2a 2f  Tell all..    */
2ad0: 0a 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71  .    if( blob_eq
2ae0: 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 63 6c  (&aToken[0], "cl
2af0: 6f 6e 65 22 29 20 29 7b 0a 20 20 20 20 20 20 69  one") ){.      i
2b00: 66 28 20 21 67 2e 6f 6b 52 65 61 64 20 29 7b 0a  f( !g.okRead ){.
2b10: 20 20 20 20 20 20 20 20 63 67 69 5f 72 65 73 65          cgi_rese
2b20: 74 5f 63 6f 6e 74 65 6e 74 28 29 3b 0a 20 20 20  t_content();.   
2b30: 20 20 20 20 20 40 20 65 72 72 6f 72 20 6e 6f 74       @ error not
2b40: 5c 73 61 75 74 68 6f 72 69 7a 65 64 5c 73 74 6f  \sauthorized\sto
2b50: 5c 73 72 65 61 64 0a 20 20 20 20 20 20 20 20 6e  \sread.        n
2b60: 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62  Err++;.        b
2b70: 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2b80: 20 20 20 20 69 73 50 75 6c 6c 20 3d 20 31 3b 0a      isPull = 1;.
2b90: 20 20 20 20 20 20 40 20 70 75 73 68 20 25 73 28        @ push %s(
2ba0: 64 62 5f 67 65 74 28 22 73 65 72 76 65 72 2d 63  db_get("server-c
2bb0: 6f 64 65 22 2c 20 22 78 22 29 29 20 25 73 28 64  ode", "x")) %s(d
2bc0: 62 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63  b_get("project-c
2bd0: 6f 64 65 22 2c 20 22 78 22 29 29 0a 20 20 20 20  ode", "x")).    
2be0: 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65 63 28    db_multi_exec(
2bf0: 0a 20 20 20 20 20 20 20 20 22 49 4e 53 45 52 54  .        "INSERT
2c00: 20 4f 52 20 49 47 4e 4f 52 45 20 49 4e 54 4f 20   OR IGNORE INTO 
2c10: 70 65 6e 64 69 6e 67 28 72 69 64 29 20 22 0a 20  pending(rid) ". 
2c20: 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 72         "SELECT r
2c30: 69 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45  id FROM blob WHE
2c40: 52 45 20 73 69 7a 65 3e 3d 30 22 0a 20 20 20 20  RE size>=0".    
2c50: 20 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 0a 0a    );.    }else..
2c60: 20 20 20 20 2f 2a 20 20 20 20 6c 6f 67 69 6e 20      /*    login 
2c70: 20 55 53 45 52 20 20 4e 4f 4e 43 45 20 20 53 49   USER  NONCE  SI
2c80: 47 4e 41 54 55 52 45 0a 20 20 20 20 2a 2a 0a 20  GNATURE.    **. 
2c90: 20 20 20 2a 2a 20 43 68 65 63 6b 20 66 6f 72 20     ** Check for 
2ca0: 61 20 76 61 6c 69 64 20 6c 6f 67 69 6e 2e 20 20  a valid login.  
2cb0: 54 68 69 73 20 68 61 73 20 74 6f 20 68 61 70 70  This has to happ
2cc0: 65 6e 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69  en before anythi
2cd0: 6e 67 20 65 6c 73 65 2e 0a 20 20 20 20 2a 2f 0a  ng else..    */.
2ce0: 20 20 20 20 69 66 28 20 62 6c 6f 62 5f 65 71 28      if( blob_eq(
2cf0: 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22 6c 6f 67  &aToken[0], "log
2d00: 69 6e 22 29 20 26 26 20 6e 54 6f 6b 65 6e 3d 3d  in") && nToken==
2d10: 34 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 64  4 ){.      if( d
2d20: 69 73 61 62 6c 65 4c 6f 67 69 6e 20 29 7b 0a 20  isableLogin ){. 
2d30: 20 20 20 20 20 20 20 67 2e 6f 6b 52 65 61 64 20         g.okRead 
2d40: 3d 20 67 2e 6f 6b 57 72 69 74 65 20 3d 20 31 3b  = g.okWrite = 1;
2d50: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
2d60: 20 20 20 20 20 20 63 68 65 63 6b 5f 6c 6f 67 69        check_logi
2d70: 6e 28 26 61 54 6f 6b 65 6e 5b 31 5d 2c 20 26 61  n(&aToken[1], &a
2d80: 54 6f 6b 65 6e 5b 32 5d 2c 20 26 61 54 6f 6b 65  Token[2], &aToke
2d90: 6e 5b 33 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  n[3]);.      }. 
2da0: 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 2f 2a     }else..    /*
2db0: 20 55 6e 6b 6e 6f 77 6e 20 6d 65 73 73 61 67 65   Unknown message
2dc0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7b 0a 20 20  .    */.    {.  
2dd0: 20 20 20 20 63 67 69 5f 72 65 73 65 74 5f 63 6f      cgi_reset_co
2de0: 6e 74 65 6e 74 28 29 3b 0a 20 20 20 20 20 20 40  ntent();.      @
2df0: 20 65 72 72 6f 72 20 62 61 64 5c 73 63 6f 6d 6d   error bad\scomm
2e00: 61 6e 64 3a 5c 73 25 46 28 62 6c 6f 62 5f 73 74  and:\s%F(blob_st
2e10: 72 28 26 6c 69 6e 65 29 29 0a 20 20 20 20 7d 0a  r(&line)).    }.
2e20: 20 20 20 20 62 6c 6f 62 61 72 72 61 79 5f 72 65      blobarray_re
2e30: 73 65 74 28 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  set(aToken, nTok
2e40: 65 6e 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54  en);.  }..  /* T
2e50: 68 65 20 69 6e 70 75 74 20 6d 65 73 73 61 67 65  he input message
2e60: 20 68 61 73 20 6e 6f 77 20 62 65 65 6e 20 70 72   has now been pr
2e70: 6f 63 65 73 73 65 64 2e 20 20 47 65 6e 65 72 61  ocessed.  Genera
2e80: 74 65 20 61 20 72 65 70 6c 79 2e 20 2a 2f 0a 20  te a reply. */. 
2e90: 20 69 66 28 20 69 73 50 75 73 68 20 29 7b 0a 20   if( isPush ){. 
2ea0: 20 20 20 53 74 6d 74 20 71 3b 0a 20 20 20 20 64     Stmt q;.    d
2eb0: 62 5f 70 72 65 70 61 72 65 28 26 71 2c 20 22 53  b_prepare(&q, "S
2ec0: 45 4c 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20  ELECT uuid FROM 
2ed0: 62 6c 6f 62 20 57 48 45 52 45 20 73 69 7a 65 3c  blob WHERE size<
2ee0: 30 22 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  0");.    while( 
2ef0: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c  db_step(&q)==SQL
2f00: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
2f10: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 75   const char *zUu
2f20: 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 74  id = db_column_t
2f30: 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20  ext(&q, 0);.    
2f40: 20 20 40 20 67 69 6d 6d 65 20 25 73 28 7a 55 75    @ gimme %s(zUu
2f50: 69 64 29 0a 20 20 20 20 7d 0a 20 20 20 20 64 62  id).    }.    db
2f60: 5f 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20  _finalize(&q);. 
2f70: 20 7d 0a 20 20 69 66 28 20 69 73 50 75 6c 6c 20   }.  if( isPull 
2f80: 29 7b 0a 20 20 20 20 73 65 6e 64 5f 61 6c 6c 5f  ){.    send_all_
2f90: 70 65 6e 64 69 6e 67 28 30 29 3b 0a 20 20 7d 0a  pending(0);.  }.
2fa0: 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e 73 61 63    db_end_transac
2fb0: 74 69 6f 6e 28 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  tion(0);.}../*.*
2fc0: 2a 20 43 4f 4d 4d 41 4e 44 3a 20 74 65 73 74 2d  * COMMAND: test-
2fd0: 78 66 65 72 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  xfer.**.** This 
2fe0: 63 6f 6d 6d 61 6e 64 20 69 73 20 75 73 65 64 20  command is used 
2ff0: 66 6f 72 20 64 65 62 75 67 67 69 6e 67 20 74 68  for debugging th
3000: 65 20 73 65 72 76 65 72 2e 20 20 54 68 65 72 65  e server.  There
3010: 20 69 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20   is a single.** 
3020: 61 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69  argument which i
3030: 73 20 74 68 65 20 75 6e 63 6f 6d 70 72 65 73 73  s the uncompress
3040: 65 64 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e  ed content of an
3050: 20 22 78 66 65 72 22 20 6d 65 73 73 61 67 65 0a   "xfer" message.
3060: 2a 2a 20 66 72 6f 6d 20 63 6c 69 65 6e 74 20 74  ** from client t
3070: 6f 20 73 65 72 76 65 72 2e 20 20 54 68 69 73 20  o server.  This 
3080: 63 6f 6d 6d 61 6e 64 20 69 6e 74 65 72 70 72 65  command interpre
3090: 74 73 20 74 68 61 74 20 6d 65 73 73 61 67 65 20  ts that message 
30a0: 61 73 0a 2a 2a 20 69 66 20 68 61 64 20 62 65 65  as.** if had bee
30b0: 6e 20 72 65 63 65 69 76 65 64 20 62 79 20 74 68  n received by th
30c0: 65 20 73 65 72 76 65 72 2e 0a 2a 2a 0a 2a 2a 20  e server..**.** 
30d0: 4f 6e 20 74 68 65 20 63 6c 69 65 6e 74 20 73 69  On the client si
30e0: 64 65 2c 20 72 75 6e 3a 0a 2a 2a 0a 2a 2a 20 20  de, run:.**.**  
30f0: 20 20 20 20 66 6f 73 73 69 6c 20 70 75 73 68 20      fossil push 
3100: 68 74 74 70 3a 2f 2f 62 6f 67 75 73 2f 20 2d 2d  http://bogus/ --
3110: 68 74 74 70 74 72 61 63 65 0a 2a 2a 0a 2a 2a 20  httptrace.**.** 
3120: 4f 72 20 61 20 73 69 6d 69 6c 61 72 20 63 6f 6d  Or a similar com
3130: 6d 61 6e 64 20 74 6f 20 70 72 6f 76 69 64 65 20  mand to provide 
3140: 74 68 65 20 6f 75 74 70 75 74 2e 20 20 54 68 65  the output.  The
3150: 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a   content of the.
3160: 2a 2a 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20  ** message will 
3170: 61 70 70 65 61 72 20 6f 6e 20 73 74 61 6e 64 61  appear on standa
3180: 72 64 20 6f 75 74 70 75 74 2e 20 20 43 61 70 74  rd output.  Capt
3190: 75 72 65 20 74 68 69 73 20 6d 65 73 73 61 67 65  ure this message
31a0: 0a 2a 2a 20 69 6e 74 6f 20 61 20 66 69 6c 65 20  .** into a file 
31b0: 6e 61 6d 65 64 20 28 66 6f 72 20 65 78 61 6d 70  named (for examp
31c0: 6c 65 29 20 6f 75 74 2e 74 78 74 2e 20 20 54 68  le) out.txt.  Th
31d0: 65 6e 20 72 75 6e 20 74 68 65 0a 2a 2a 20 73 65  en run the.** se
31e0: 72 76 65 72 20 69 6e 20 67 64 62 3a 0a 2a 2a 0a  rver in gdb:.**.
31f0: 2a 2a 20 20 20 20 20 67 64 62 20 66 6f 73 73 69  **     gdb fossi
3200: 6c 0a 2a 2a 20 20 20 20 20 72 20 74 65 73 74 2d  l.**     r test-
3210: 78 66 65 72 20 6f 75 74 2e 74 78 74 0a 2a 2f 0a  xfer out.txt.*/.
3220: 76 6f 69 64 20 63 6d 64 5f 74 65 73 74 5f 78 66  void cmd_test_xf
3230: 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 6e 74 20  er(void){.  int 
3240: 6e 6f 74 55 73 65 64 3b 0a 20 20 69 66 28 20 67  notUsed;.  if( g
3250: 2e 61 72 67 63 21 3d 32 20 26 26 20 67 2e 61 72  .argc!=2 && g.ar
3260: 67 63 21 3d 33 20 29 7b 0a 20 20 20 20 75 73 61  gc!=3 ){.    usa
3270: 67 65 28 22 3f 4d 45 53 53 41 47 45 46 49 4c 45  ge("?MESSAGEFILE
3280: 3f 22 29 3b 0a 20 20 7d 0a 20 20 64 62 5f 6d 75  ?");.  }.  db_mu
3290: 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74 72 65  st_be_within_tre
32a0: 65 28 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f  e();.  blob_zero
32b0: 28 26 67 2e 63 67 69 49 6e 29 3b 0a 20 20 62 6c  (&g.cgiIn);.  bl
32c0: 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 66 69 6c  ob_read_from_fil
32d0: 65 28 26 67 2e 63 67 69 49 6e 2c 20 67 2e 61 72  e(&g.cgiIn, g.ar
32e0: 67 63 3d 3d 32 20 3f 20 22 2d 22 20 3a 20 67 2e  gc==2 ? "-" : g.
32f0: 61 72 67 76 5b 32 5d 29 3b 0a 20 20 64 69 73 61  argv[2]);.  disa
3300: 62 6c 65 4c 6f 67 69 6e 20 3d 20 31 3b 0a 20 20  bleLogin = 1;.  
3310: 70 61 67 65 5f 78 66 65 72 28 29 3b 0a 20 20 70  page_xfer();.  p
3320: 72 69 6e 74 66 28 22 25 73 5c 6e 22 2c 20 63 67  rintf("%s\n", cg
3330: 69 5f 65 78 74 72 61 63 74 5f 63 6f 6e 74 65 6e  i_extract_conten
3340: 74 28 26 6e 6f 74 55 73 65 64 29 29 3b 0a 7d 0a  t(&notUsed));.}.
3350: 0a 0a 2f 2a 0a 2a 2a 20 53 79 6e 63 20 74 6f 20  ../*.** Sync to 
3360: 74 68 65 20 68 6f 73 74 20 69 64 65 6e 74 69 66  the host identif
3370: 69 65 64 20 69 6e 20 67 2e 75 72 6c 4e 61 6d 65  ied in g.urlName
3380: 20 61 6e 64 20 67 2e 75 72 6c 50 61 74 68 2e 20   and g.urlPath. 
3390: 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65   This.** routine
33a0: 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
33b0: 65 20 63 6c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  e client..**.** 
33c0: 52 65 63 6f 72 64 73 20 61 72 65 20 70 75 73 68  Records are push
33d0: 65 64 20 74 6f 20 74 68 65 20 73 65 72 76 65 72  ed to the server
33e0: 20 69 66 20 70 75 73 68 46 6c 61 67 20 69 73 20   if pushFlag is 
33f0: 74 72 75 65 2e 20 20 52 65 63 6f 72 64 73 0a 2a  true.  Records.*
3400: 2a 20 61 72 65 20 70 75 6c 6c 65 64 20 69 66 20  * are pulled if 
3410: 70 75 6c 6c 46 6c 61 67 20 69 73 20 74 72 75 65  pullFlag is true
3420: 2e 20 20 41 20 66 75 6c 6c 20 73 79 6e 63 20 6f  .  A full sync o
3430: 63 63 75 72 73 20 69 66 20 62 6f 74 68 20 61 72  ccurs if both ar
3440: 65 0a 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 76 6f  e.** true..*/.vo
3450: 69 64 20 63 6c 69 65 6e 74 5f 73 79 6e 63 28 69  id client_sync(i
3460: 6e 74 20 70 75 73 68 46 6c 61 67 2c 20 69 6e 74  nt pushFlag, int
3470: 20 70 75 6c 6c 46 6c 61 67 2c 20 69 6e 74 20 63   pullFlag, int c
3480: 6c 6f 6e 65 46 6c 61 67 29 7b 0a 20 20 69 6e 74  loneFlag){.  int
3490: 20 67 6f 20 3d 20 31 3b 20 20 20 20 20 20 20 20   go = 1;        
34a0: 2f 2a 20 4c 6f 6f 70 20 75 6e 74 69 6c 20 7a 65  /* Loop until ze
34b0: 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 6b  ro */.  int nTok
34c0: 65 6e 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  en;.  const char
34d0: 20 2a 7a 53 43 6f 64 65 20 3d 20 64 62 5f 67 65   *zSCode = db_ge
34e0: 74 28 22 73 65 72 76 65 72 2d 63 6f 64 65 22 2c  t("server-code",
34f0: 20 22 78 22 29 3b 0a 20 20 63 6f 6e 73 74 20 63   "x");.  const c
3500: 68 61 72 20 2a 7a 50 43 6f 64 65 20 3d 20 64 62  har *zPCode = db
3510: 5f 67 65 74 28 22 70 72 6f 6a 65 63 74 2d 63 6f  _get("project-co
3520: 64 65 22 2c 20 30 29 3b 0a 20 20 42 6c 6f 62 20  de", 0);.  Blob 
3530: 73 65 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a 20  send;        /* 
3540: 54 65 78 74 20 77 65 20 61 72 65 20 73 65 6e 64  Text we are send
3550: 69 6e 67 20 74 6f 20 74 68 65 20 73 65 72 76 65  ing to the serve
3560: 72 20 2a 2f 0a 20 20 42 6c 6f 62 20 72 65 63 76  r */.  Blob recv
3570: 3b 20 20 20 20 20 20 20 20 2f 2a 20 52 65 70 6c  ;        /* Repl
3580: 79 20 77 65 20 67 6f 74 20 62 61 63 6b 20 66 72  y we got back fr
3590: 6f 6d 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f  om the server */
35a0: 0a 20 20 42 6c 6f 62 20 6c 69 6e 65 3b 20 20 20  .  Blob line;   
35b0: 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65       /* A single
35c0: 20 6c 69 6e 65 20 6f 66 20 74 68 65 20 72 65 70   line of the rep
35d0: 6c 79 20 2a 2f 0a 20 20 42 6c 6f 62 20 61 54 6f  ly */.  Blob aTo
35e0: 6b 65 6e 5b 35 5d 3b 20 20 20 2f 2a 20 41 20 74  ken[5];   /* A t
35f0: 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 6f 66 20 6c  okenization of l
3600: 69 6e 65 20 2a 2f 0a 20 20 42 6c 6f 62 20 65 72  ine */.  Blob er
3610: 72 6d 73 67 3b 20 20 20 20 20 20 2f 2a 20 45 72  rmsg;      /* Er
3620: 72 6f 72 20 6d 65 73 73 61 67 65 20 2a 2f 0a 0a  ror message */..
3630: 20 20 61 73 73 65 72 74 28 20 70 75 73 68 46 6c    assert( pushFl
3640: 61 67 20 7c 7c 20 70 75 6c 6c 46 6c 61 67 20 7c  ag || pullFlag |
3650: 7c 20 63 6c 6f 6e 65 46 6c 61 67 20 29 3b 0a 20  | cloneFlag );. 
3660: 20 61 73 73 65 72 74 28 20 21 67 2e 75 72 6c 49   assert( !g.urlI
3670: 73 46 69 6c 65 20 29 3b 20 20 20 20 20 20 20 20  sFile );        
3680: 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77    /* This only w
3690: 6f 72 6b 73 20 66 6f 72 20 6e 65 74 77 6f 72 6b  orks for network
36a0: 69 6e 67 20 2a 2f 0a 0a 20 20 64 62 5f 62 65 67  ing */..  db_beg
36b0: 69 6e 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 29  in_transaction()
36c0: 3b 0a 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78 65  ;.  db_multi_exe
36d0: 63 28 0a 20 20 20 20 2f 2a 20 52 65 63 6f 72 64  c(.    /* Record
36e0: 73 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20  s which we know 
36f0: 74 68 65 20 6f 74 68 65 72 20 73 69 64 65 20 61  the other side a
3700: 6c 73 6f 20 68 61 73 20 2a 2f 0a 20 20 20 20 22  lso has */.    "
3710: 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42 4c  CREATE TEMP TABL
3720: 45 20 6f 6e 72 65 6d 6f 74 65 28 72 69 64 20 49  E onremote(rid I
3730: 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
3740: 45 59 29 3b 22 0a 20 20 20 20 2f 2a 20 52 65 63  EY);".    /* Rec
3750: 6f 72 64 73 20 77 65 20 6b 6e 6f 77 20 74 68 65  ords we know the
3760: 20 6f 74 68 65 72 20 73 69 64 65 20 6e 65 65 64   other side need
3770: 73 20 2a 2f 0a 20 20 20 20 22 43 52 45 41 54 45  s */.    "CREATE
3780: 20 54 45 4d 50 20 54 41 42 4c 45 20 70 65 6e 64   TEMP TABLE pend
3790: 69 6e 67 28 72 69 64 20 49 4e 54 45 47 45 52 20  ing(rid INTEGER 
37a0: 50 52 49 4d 41 52 59 20 4b 45 59 29 3b 22 0a 20  PRIMARY KEY);". 
37b0: 20 29 3b 0a 20 20 62 6c 6f 62 61 72 72 61 79 5f   );.  blobarray_
37c0: 7a 65 72 6f 28 61 54 6f 6b 65 6e 2c 20 63 6f 75  zero(aToken, cou
37d0: 6e 74 28 61 54 6f 6b 65 6e 29 29 3b 0a 20 20 62  nt(aToken));.  b
37e0: 6c 6f 62 5f 7a 65 72 6f 28 26 73 65 6e 64 29 3b  lob_zero(&send);
37f0: 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 72 65  .  blob_zero(&re
3800: 63 76 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f  cv);.  blob_zero
3810: 28 26 65 72 72 6d 73 67 29 3b 0a 0a 0a 20 20 77  (&errmsg);...  w
3820: 68 69 6c 65 28 20 67 6f 20 29 7b 0a 20 20 20 20  hile( go ){.    
3830: 67 6f 20 3d 20 30 3b 0a 0a 20 20 20 20 2f 2a 20  go = 0;..    /* 
3840: 47 65 6e 65 72 61 74 65 20 61 20 72 65 71 75 65  Generate a reque
3850: 73 74 20 74 6f 20 62 65 20 73 65 6e 74 20 74 6f  st to be sent to
3860: 20 74 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20   the server..   
3870: 20 2a 2a 20 41 6c 77 61 79 73 20 62 65 67 69 6e   ** Always begin
3880: 20 77 69 74 68 20 61 20 63 6c 6f 6e 65 2c 20 70   with a clone, p
3890: 75 6c 6c 2c 20 6f 72 20 70 75 73 68 20 6d 65 73  ull, or push mes
38a0: 73 61 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20  sage.    */.    
38b0: 69 66 28 20 63 6c 6f 6e 65 46 6c 61 67 20 29 7b  if( cloneFlag ){
38c0: 0a 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  .      blob_appe
38d0: 6e 64 66 28 26 73 65 6e 64 2c 20 22 63 6c 6f 6e  ndf(&send, "clon
38e0: 65 5c 6e 22 29 3b 0a 20 20 20 20 20 20 70 75 73  e\n");.      pus
38f0: 68 46 6c 61 67 20 3d 20 30 3b 0a 20 20 20 20 20  hFlag = 0;.     
3900: 20 70 75 6c 6c 46 6c 61 67 20 3d 20 30 3b 0a 20   pullFlag = 0;. 
3910: 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 75 6c     }else if( pul
3920: 6c 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 62  lFlag ){.      b
3930: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 73 65 6e  lob_appendf(&sen
3940: 64 2c 20 22 70 75 6c 6c 20 25 73 20 25 73 5c 6e  d, "pull %s %s\n
3950: 22 2c 20 7a 53 43 6f 64 65 2c 20 7a 50 43 6f 64  ", zSCode, zPCod
3960: 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  e);.    }.    if
3970: 28 20 70 75 73 68 46 6c 61 67 20 29 7b 0a 20 20  ( pushFlag ){.  
3980: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66      blob_appendf
3990: 28 26 73 65 6e 64 2c 20 22 70 75 73 68 20 25 73  (&send, "push %s
39a0: 20 25 73 5c 6e 22 2c 20 7a 53 43 6f 64 65 2c 20   %s\n", zSCode, 
39b0: 7a 50 43 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 0a  zPCode);.    }..
39c0: 20 20 20 20 69 66 28 20 70 75 6c 6c 46 6c 61 67      if( pullFlag
39d0: 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 53 65 6e   ){.      /* Sen
39e0: 64 20 67 69 6d 6d 65 20 6d 65 73 73 61 67 65 20  d gimme message 
39f0: 66 6f 72 20 65 76 65 72 79 20 70 68 61 6e 74 6f  for every phanto
3a00: 6d 20 74 68 61 74 20 77 65 20 68 6f 6c 64 2e 0a  m that we hold..
3a10: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 53        */.      S
3a20: 74 6d 74 20 71 3b 0a 20 20 20 20 20 20 64 62 5f  tmt q;.      db_
3a30: 70 72 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c  prepare(&q, "SEL
3a40: 45 43 54 20 75 75 69 64 20 46 52 4f 4d 20 62 6c  ECT uuid FROM bl
3a50: 6f 62 20 57 48 45 52 45 20 73 69 7a 65 3c 30 22  ob WHERE size<0"
3a60: 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
3a70: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c  db_step(&q)==SQL
3a80: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
3a90: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
3aa0: 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e  Uuid = db_column
3ab0: 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20  _text(&q, 0);.  
3ac0: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
3ad0: 64 66 28 26 73 65 6e 64 2c 22 67 69 6d 6d 65 20  df(&send,"gimme 
3ae0: 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20  %s\n", zUuid);. 
3af0: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 5f       }.      db_
3b00: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20  finalize(&q);.  
3b10: 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 70 75 73    }..    if( pus
3b20: 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20 2f  hFlag ){.      /
3b30: 2a 20 53 65 6e 64 20 74 68 65 20 73 65 72 76 65  * Send the serve
3b40: 72 20 61 6e 79 20 66 69 6c 65 73 20 74 68 61 74  r any files that
3b50: 20 74 68 65 20 73 65 72 76 65 72 20 68 61 73 20   the server has 
3b60: 72 65 71 75 65 73 74 65 64 20 2a 2f 0a 20 20 20  requested */.   
3b70: 20 20 20 73 65 6e 64 5f 61 6c 6c 5f 70 65 6e 64     send_all_pend
3b80: 69 6e 67 28 26 73 65 6e 64 29 3b 0a 20 20 20 20  ing(&send);.    
3b90: 7d 0a 0a 20 20 20 20 69 66 28 20 70 75 6c 6c 46  }..    if( pullF
3ba0: 6c 61 67 20 7c 7c 20 70 75 73 68 46 6c 61 67 20  lag || pushFlag 
3bb0: 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6c 77 61  ){.      /* Alwa
3bc0: 79 73 20 73 65 6e 64 20 6f 75 72 20 6c 65 61 76  ys send our leav
3bd0: 65 73 20 2a 2f 0a 20 20 20 20 20 20 53 74 6d 74  es */.      Stmt
3be0: 20 71 3b 0a 20 20 20 20 20 20 64 62 5f 70 72 65   q;.      db_pre
3bf0: 70 61 72 65 28 26 71 2c 20 0a 20 20 20 20 20 20  pare(&q, .      
3c00: 20 20 20 22 53 45 4c 45 43 54 20 75 75 69 64 20     "SELECT uuid 
3c10: 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20  FROM blob WHERE 
3c20: 72 69 64 20 49 4e 22 0a 20 20 20 20 20 20 20 20  rid IN".        
3c30: 20 22 20 20 28 53 45 4c 45 43 54 20 63 69 64 20   "  (SELECT cid 
3c40: 46 52 4f 4d 20 70 6c 69 6e 6b 20 45 58 43 45 50  FROM plink EXCEP
3c50: 54 20 53 45 4c 45 43 54 20 70 69 64 20 46 52 4f  T SELECT pid FRO
3c60: 4d 20 70 6c 69 6e 6b 29 22 0a 20 20 20 20 20 20  M plink)".      
3c70: 29 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  );.      while( 
3c80: 64 62 5f 73 74 65 70 28 26 71 29 3d 3d 53 51 4c  db_step(&q)==SQL
3c90: 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20  ITE_ROW ){.     
3ca0: 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
3cb0: 55 75 69 64 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e  Uuid = db_column
3cc0: 5f 74 65 78 74 28 26 71 2c 20 30 29 3b 0a 20 20  _text(&q, 0);.  
3cd0: 20 20 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e        blob_appen
3ce0: 64 66 28 26 73 65 6e 64 2c 20 22 6c 65 61 66 20  df(&send, "leaf 
3cf0: 25 73 5c 6e 22 2c 20 7a 55 75 69 64 29 3b 0a 20  %s\n", zUuid);. 
3d00: 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 62 5f       }.      db_
3d10: 66 69 6e 61 6c 69 7a 65 28 26 71 29 3b 0a 20 20  finalize(&q);.  
3d20: 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 45 78 63 68    }..    /* Exch
3d30: 61 6e 67 65 20 6d 65 73 73 61 67 65 73 20 77 69  ange messages wi
3d40: 74 68 20 74 68 65 20 73 65 72 76 65 72 20 2a 2f  th the server */
3d50: 0a 20 20 20 20 68 74 74 70 5f 65 78 63 68 61 6e  .    http_exchan
3d60: 67 65 28 26 73 65 6e 64 2c 20 26 72 65 63 76 29  ge(&send, &recv)
3d70: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
3d80: 28 26 73 65 6e 64 29 3b 0a 0a 20 20 20 20 2f 2a  (&send);..    /*
3d90: 20 50 72 6f 63 65 73 73 20 74 68 65 20 72 65 70   Process the rep
3da0: 6c 79 20 74 68 61 74 20 63 61 6d 65 20 62 61 63  ly that came bac
3db0: 6b 20 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65  k from the serve
3dc0: 72 20 2a 2f 0a 20 20 20 20 77 68 69 6c 65 28 20  r */.    while( 
3dd0: 62 6c 6f 62 5f 6c 69 6e 65 28 26 72 65 63 76 2c  blob_line(&recv,
3de0: 20 26 6c 69 6e 65 29 20 29 7b 0a 20 20 20 20 20   &line) ){.     
3df0: 20 6e 54 6f 6b 65 6e 20 3d 20 62 6c 6f 62 5f 74   nToken = blob_t
3e00: 6f 6b 65 6e 69 7a 65 28 26 6c 69 6e 65 2c 20 61  okenize(&line, a
3e10: 54 6f 6b 65 6e 2c 20 63 6f 75 6e 74 28 61 54 6f  Token, count(aTo
3e20: 6b 65 6e 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  ken));..      /*
3e30: 20 20 20 66 69 6c 65 20 55 55 49 44 20 53 49 5a     file UUID SIZ
3e40: 45 20 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20  E \n CONTENT.   
3e50: 20 20 20 2a 2a 20 20 20 66 69 6c 65 20 55 55 49     **   file UUI
3e60: 44 20 44 45 4c 54 41 53 52 43 20 53 49 5a 45 20  D DELTASRC SIZE 
3e70: 5c 6e 20 43 4f 4e 54 45 4e 54 0a 20 20 20 20 20  \n CONTENT.     
3e80: 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 52 65 63   **.      ** Rec
3e90: 65 69 76 65 20 61 20 66 69 6c 65 20 74 72 61 6e  eive a file tran
3ea0: 73 6d 69 74 74 65 64 20 66 72 6f 6d 20 74 68 65  smitted from the
3eb0: 20 6f 74 68 65 72 20 73 69 64 65 0a 20 20 20 20   other side.    
3ec0: 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 62    */.      if( b
3ed0: 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b 30  lob_eq(&aToken[0
3ee0: 5d 2c 22 66 69 6c 65 22 29 20 29 7b 0a 20 20 20  ],"file") ){.   
3ef0: 20 20 20 20 20 78 66 65 72 5f 61 63 63 65 70 74       xfer_accept
3f00: 5f 66 69 6c 65 28 26 72 65 63 76 2c 20 61 54 6f  _file(&recv, aTo
3f10: 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 2c 20 26 65 72  ken, nToken, &er
3f20: 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 7d 65 6c  rmsg);.      }el
3f30: 73 65 0a 0a 20 20 20 20 20 20 2f 2a 20 20 20 67  se..      /*   g
3f40: 69 6d 6d 65 20 55 55 49 44 0a 20 20 20 20 20 20  imme UUID.      
3f50: 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 53 65 72 76  **.      ** Serv
3f60: 65 72 20 69 73 20 72 65 71 75 65 73 74 69 6e 67  er is requesting
3f70: 20 61 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f   a file.      */
3f80: 0a 20 20 20 20 20 20 69 66 28 20 62 6c 6f 62 5f  .      if( blob_
3f90: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22  eq(&aToken[0], "
3fa0: 67 69 6d 6d 65 22 29 20 26 26 20 6e 54 6f 6b 65  gimme") && nToke
3fb0: 6e 3d 3d 32 0a 20 20 20 20 20 20 20 20 20 20 20  n==2.           
3fc0: 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75      && blob_is_u
3fd0: 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 20  uid(&aToken[1]) 
3fe0: 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
3ff0: 75 73 68 46 6c 61 67 20 29 7b 0a 20 20 20 20 20  ushFlag ){.     
4000: 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65 78       db_multi_ex
4010: 65 63 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  ec(.            
4020: 22 49 4e 53 45 52 54 20 4f 52 20 49 47 4e 4f 52  "INSERT OR IGNOR
4030: 45 20 49 4e 54 4f 20 70 65 6e 64 69 6e 67 28 72  E INTO pending(r
4040: 69 64 29 20 22 0a 20 20 20 20 20 20 20 20 20 20  id) ".          
4050: 20 20 22 53 45 4c 45 43 54 20 72 69 64 20 46 52    "SELECT rid FR
4060: 4f 4d 20 62 6c 6f 62 20 57 48 45 52 45 20 75 75  OM blob WHERE uu
4070: 69 64 3d 25 42 20 41 4e 44 20 73 69 7a 65 3e 3d  id=%B AND size>=
4080: 30 22 2c 20 26 61 54 6f 6b 65 6e 5b 31 5d 0a 20  0", &aToken[1]. 
4090: 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20           );.    
40a0: 20 20 20 20 20 20 67 6f 20 3d 20 31 3b 0a 20 20        go = 1;.  
40b0: 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65        }.      }e
40c0: 6c 73 65 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20  lse.  .      /* 
40d0: 20 20 69 67 6f 74 20 55 55 49 44 0a 20 20 20 20    igot UUID.    
40e0: 20 20 2a 2a 20 20 20 6c 65 61 66 20 55 55 49 44    **   leaf UUID
40f0: 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
4100: 2a 2a 20 53 65 72 76 65 72 20 70 72 6f 63 6c 61  ** Server procla
4110: 69 6d 73 20 74 68 61 74 20 69 74 20 68 61 73 20  ims that it has 
4120: 61 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c  a particular fil
4130: 65 2e 20 20 41 20 6c 65 61 66 20 6d 65 73 73 61  e.  A leaf messa
4140: 67 65 0a 20 20 20 20 20 20 2a 2a 20 6d 65 61 6e  ge.      ** mean
4150: 73 20 74 68 61 74 20 74 68 65 20 66 69 6c 65 20  s that the file 
4160: 69 73 20 61 20 6c 65 61 66 20 6d 61 6e 69 66 65  is a leaf manife
4170: 73 74 20 6f 6e 20 74 68 65 20 73 65 72 76 65 72  st on the server
4180: 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
4190: 20 69 66 28 20 6e 54 6f 6b 65 6e 3d 3d 32 0a 20   if( nToken==2. 
41a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
41b0: 28 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e  (blob_eq(&aToken
41c0: 5b 30 5d 2c 20 22 69 67 6f 74 22 29 20 7c 7c 20  [0], "igot") || 
41d0: 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e 5b  blob_eq(&aToken[
41e0: 30 5d 2c 20 22 6c 65 61 66 22 29 29 0a 20 20 20  0], "leaf")).   
41f0: 20 20 20 20 20 20 20 20 20 20 20 26 26 20 62 6c             && bl
4200: 6f 62 5f 69 73 5f 75 75 69 64 28 26 61 54 6f 6b  ob_is_uuid(&aTok
4210: 65 6e 5b 31 5d 29 20 29 7b 0a 20 20 20 20 20 20  en[1]) ){.      
4220: 20 20 69 6e 74 20 72 69 64 20 3d 20 64 62 5f 69    int rid = db_i
4230: 6e 74 28 30 2c 20 22 53 45 4c 45 43 54 20 72 69  nt(0, "SELECT ri
4240: 64 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48 45 52  d FROM blob WHER
4250: 45 20 75 75 69 64 3d 25 42 22 2c 20 26 61 54 6f  E uuid=%B", &aTo
4260: 6b 65 6e 5b 31 5d 29 3b 0a 20 20 20 20 20 20 20  ken[1]);.       
4270: 20 69 66 28 20 72 69 64 3e 30 20 29 7b 0a 20 20   if( rid>0 ){.  
4280: 20 20 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69          db_multi
4290: 5f 65 78 65 63 28 0a 20 20 20 20 20 20 20 20 20  _exec(.         
42a0: 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47     "INSERT OR IG
42b0: 4e 4f 52 45 20 49 4e 54 4f 20 6f 6e 72 65 6d 6f  NORE INTO onremo
42c0: 74 65 28 72 69 64 29 20 56 41 4c 55 45 53 28 25  te(rid) VALUES(%
42d0: 64 29 22 2c 20 72 69 64 0a 20 20 20 20 20 20 20  d)", rid.       
42e0: 20 20 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20     );.          
42f0: 2f 2a 20 41 64 64 20 74 6f 20 74 68 65 20 70 65  /* Add to the pe
4300: 6e 64 69 6e 67 20 73 65 74 20 61 6c 6c 20 63 68  nding set all ch
4310: 69 6c 64 72 65 6e 20 6f 66 20 74 68 65 20 73 65  ildren of the se
4320: 72 76 65 72 27 73 20 6c 65 61 76 65 73 20 2a 2f  rver's leaves */
4330: 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
4340: 75 73 68 46 6c 61 67 20 26 26 20 62 6c 6f 62 5f  ushFlag && blob_
4350: 65 71 28 26 61 54 6f 6b 65 6e 5b 30 5d 2c 20 22  eq(&aToken[0], "
4360: 6c 65 61 66 22 29 20 29 7b 0a 20 20 20 20 20 20  leaf") ){.      
4370: 20 20 20 20 20 20 64 62 5f 6d 75 6c 74 69 5f 65        db_multi_e
4380: 78 65 63 28 0a 20 20 20 20 20 20 20 20 20 20 20  xec(.           
4390: 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20 49 47     "INSERT OR IG
43a0: 4e 4f 52 45 20 49 4e 54 4f 20 70 65 6e 64 69 6e  NORE INTO pendin
43b0: 67 28 72 69 64 29 20 22 0a 20 20 20 20 20 20 20  g(rid) ".       
43c0: 20 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 63         "SELECT c
43d0: 69 64 20 46 52 4f 4d 20 70 6c 69 6e 6b 20 57 48  id FROM plink WH
43e0: 45 52 45 20 70 69 64 3d 25 64 22 2c 20 72 69 64  ERE pid=%d", rid
43f0: 0a 20 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a  .            );.
4400: 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 20 3d              go =
4410: 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a   1;.          }.
4420: 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 75            if( pu
4430: 6c 6c 46 6c 61 67 20 26 26 20 21 67 6f 20 26 26  llFlag && !go &&
4440: 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
4450: 64 62 5f 65 78 69 73 74 73 28 22 53 45 4c 45 43  db_exists("SELEC
4460: 54 20 31 20 46 52 4f 4d 20 62 6c 6f 62 20 57 48  T 1 FROM blob WH
4470: 45 52 45 20 72 69 64 3d 25 64 20 41 4e 44 20 73  ERE rid=%d AND s
4480: 69 7a 65 3e 3d 30 22 2c 20 72 69 64 29 20 29 7b  ize>=0", rid) ){
4490: 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 20  .            go 
44a0: 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 1;.          }
44b0: 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69  .        }else i
44c0: 66 28 20 70 75 6c 6c 46 6c 61 67 20 29 7b 0a 20  f( pullFlag ){. 
44d0: 20 20 20 20 20 20 20 20 20 67 6f 20 3d 20 31 3b           go = 1;
44e0: 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65  .          conte
44f0: 6e 74 5f 70 75 74 28 30 2c 20 62 6c 6f 62 5f 73  nt_put(0, blob_s
4500: 74 72 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 29 3b  tr(&aToken[1]));
4510: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
4520: 20 7d 65 6c 73 65 0a 20 20 0a 20 20 20 20 20 20   }else.  .      
4530: 2f 2a 20 20 20 70 75 73 68 20 20 53 45 52 56 45  /*   push  SERVE
4540: 52 43 4f 44 45 20 20 50 52 4f 44 55 43 54 43 4f  RCODE  PRODUCTCO
4550: 44 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  DE.      **.    
4560: 20 20 2a 2a 20 53 68 6f 75 6c 64 20 6f 6e 6c 79    ** Should only
4570: 20 68 61 70 70 65 6e 20 69 6e 20 72 65 73 70 6f   happen in respo
4580: 6e 73 65 20 74 6f 20 61 20 63 6c 6f 6e 65 2e 0a  nse to a clone..
4590: 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
45a0: 66 28 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b  f( blob_eq(&aTok
45b0: 65 6e 5b 30 5d 2c 22 70 75 73 68 22 29 20 26 26  en[0],"push") &&
45c0: 20 6e 54 6f 6b 65 6e 3d 3d 32 20 26 26 20 63 6c   nToken==2 && cl
45d0: 6f 6e 65 46 6c 61 67 0a 20 20 20 20 20 20 20 20  oneFlag.        
45e0: 20 20 20 20 20 20 26 26 20 62 6c 6f 62 5f 69 73        && blob_is
45f0: 5f 75 75 69 64 28 26 61 54 6f 6b 65 6e 5b 31 5d  _uuid(&aToken[1]
4600: 29 20 26 26 20 62 6c 6f 62 5f 69 73 5f 75 75 69  ) && blob_is_uui
4610: 64 28 26 61 54 6f 6b 65 6e 5b 32 5d 29 20 29 7b  d(&aToken[2]) ){
4620: 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 6c  ..        if( bl
4630: 6f 62 5f 65 71 5f 73 74 72 28 26 61 54 6f 6b 65  ob_eq_str(&aToke
4640: 6e 5b 31 5d 2c 20 7a 53 43 6f 64 65 2c 20 2d 31  n[1], zSCode, -1
4650: 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 66  ) ){.          f
4660: 6f 73 73 69 6c 5f 66 61 74 61 6c 28 22 73 65 72  ossil_fatal("ser
4670: 76 65 72 20 6c 6f 6f 70 22 29 3b 0a 20 20 20 20  ver loop");.    
4680: 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
4690: 28 20 7a 50 43 6f 64 65 3d 3d 30 20 29 7b 0a 20  ( zPCode==0 ){. 
46a0: 20 20 20 20 20 20 20 20 20 7a 50 43 6f 64 65 20           zPCode 
46b0: 3d 20 6d 70 72 69 6e 74 66 28 22 25 62 22 2c 20  = mprintf("%b", 
46c0: 26 61 54 6f 6b 65 6e 5b 32 5d 29 3b 0a 20 20 20  &aToken[2]);.   
46d0: 20 20 20 20 20 20 20 64 62 5f 73 65 74 28 22 70         db_set("p
46e0: 72 6f 64 75 63 74 2d 63 6f 64 65 22 2c 20 7a 50  roduct-code", zP
46f0: 43 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 7d  Code);.        }
4700: 0a 20 20 20 20 20 20 20 20 63 6c 6f 6e 65 46 6c  .        cloneFl
4710: 61 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ag = 0;.        
4720: 70 75 6c 6c 46 6c 61 67 20 3d 20 31 3b 0a 20 20  pullFlag = 1;.  
4730: 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20 20 20 20      }else..     
4740: 20 2f 2a 20 20 20 65 72 72 6f 72 20 4d 45 53 53   /*   error MESS
4750: 41 47 45 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  AGE.      **.   
4760: 20 20 20 2a 2a 20 52 65 70 6f 72 74 20 61 6e 20     ** Report an 
4770: 65 72 72 6f 72 0a 20 20 20 20 20 20 2a 2f 20 20  error.      */  
4780: 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28        .      if(
4790: 20 62 6c 6f 62 5f 65 71 28 26 61 54 6f 6b 65 6e   blob_eq(&aToken
47a0: 5b 30 5d 2c 22 65 72 72 6f 72 22 29 20 26 26 20  [0],"error") && 
47b0: 6e 54 6f 6b 65 6e 3d 3d 32 20 29 7b 0a 20 20 20  nToken==2 ){.   
47c0: 20 20 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 20       char *zMsg 
47d0: 3d 20 62 6c 6f 62 5f 74 65 72 6d 69 6e 61 74 65  = blob_terminate
47e0: 28 26 61 54 6f 6b 65 6e 5b 31 5d 29 3b 0a 20 20  (&aToken[1]);.  
47f0: 20 20 20 20 20 20 64 65 66 6f 73 73 69 6c 69 7a        defossiliz
4800: 65 28 7a 4d 73 67 29 3b 0a 20 20 20 20 20 20 20  e(zMsg);.       
4810: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 65   blob_appendf(&e
4820: 72 72 6d 73 67 2c 20 22 73 65 72 76 65 72 20 73  rrmsg, "server s
4830: 61 79 73 3a 20 25 73 22 2c 20 7a 4d 73 67 29 3b  ays: %s", zMsg);
4840: 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 0a 20 20  .      }else..  
4850: 20 20 20 20 2f 2a 20 55 6e 6b 6e 6f 77 6e 20 6d      /* Unknown m
4860: 65 73 73 61 67 65 20 2a 2f 0a 20 20 20 20 20 20  essage */.      
4870: 7b 0a 20 20 20 20 20 20 20 20 62 6c 6f 62 5f 61  {.        blob_a
4880: 70 70 65 6e 64 66 28 26 65 72 72 6d 73 67 2c 20  ppendf(&errmsg, 
4890: 22 75 6e 6b 6e 6f 77 6e 20 63 6f 6d 6d 61 6e 64  "unknown command
48a0: 3a 20 25 62 22 2c 20 26 61 54 6f 6b 65 6e 5b 30  : %b", &aToken[0
48b0: 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ]);.      }..   
48c0: 20 20 20 69 66 28 20 62 6c 6f 62 5f 73 69 7a 65     if( blob_size
48d0: 28 26 65 72 72 6d 73 67 29 20 29 7b 0a 20 20 20  (&errmsg) ){.   
48e0: 20 20 20 20 20 66 6f 73 73 69 6c 5f 66 61 74 61       fossil_fata
48f0: 6c 28 22 25 62 22 2c 20 26 65 72 72 6d 73 67 29  l("%b", &errmsg)
4900: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
4910: 62 6c 6f 62 61 72 72 61 79 5f 72 65 73 65 74 28  blobarray_reset(
4920: 61 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b  aToken, nToken);
4930: 0a 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f  .    }.    blob_
4940: 72 65 73 65 74 28 26 72 65 63 76 29 3b 0a 20 20  reset(&recv);.  
4950: 7d 3b 0a 20 20 68 74 74 70 5f 63 6c 6f 73 65 28  };.  http_close(
4960: 29 3b 0a 20 20 64 62 5f 65 6e 64 5f 74 72 61 6e  );.  db_end_tran
4970: 73 61 63 74 69 6f 6e 28 30 29 3b 0a 20 20 64 62  saction(0);.  db
4980: 5f 6d 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20  _multi_exec(.   
4990: 20 22 44 52 4f 50 20 54 41 42 4c 45 20 6f 6e 72   "DROP TABLE onr
49a0: 65 6d 6f 74 65 3b 22 0a 20 20 20 20 22 44 52 4f  emote;".    "DRO
49b0: 50 20 54 41 42 4c 45 20 70 65 6e 64 69 6e 67 3b  P TABLE pending;
49c0: 22 0a 20 20 29 3b 0a 7d 0a                       ".  );.}.