Hex Artifact Content
Not logged in

Artifact 3ced5594d2e984d4cf3c7176ad0a0f54a8307cfd:

File src/http.c part of check-in [ee47021f51] - Support usernames containing spaces for the xfer protocol. Ticket 71c9b46198c04. by drh on 2009-09-10 14:07:54.

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: 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74  hat implements t
0390: 68 65 20 63 6c 69 65 6e 74 2d 73 69 64 65 20 48  he client-side H
03a0: 54 54 50 20 70 72 6f 74 6f 63 6f 6c 0a 2a 2f 0a  TTP protocol.*/.
03b0: 23 69 6e 63 6c 75 64 65 20 22 63 6f 6e 66 69 67  #include "config
03c0: 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 22 68 74  .h".#include "ht
03d0: 74 70 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20 3c  tp.h".#include <
03e0: 61 73 73 65 72 74 2e 68 3e 0a 0a 2f 2a 0a 2a 2a  assert.h>../*.**
03f0: 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20 22   Construct the "
0400: 6c 6f 67 69 6e 22 20 63 61 72 64 20 77 69 74 68  login" card with
0410: 20 74 68 65 20 63 6c 69 65 6e 74 20 63 72 65 64   the client cred
0420: 65 6e 74 69 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 20  entials..**.**  
0430: 20 20 20 20 20 6c 6f 67 69 6e 20 4c 4f 47 49 4e       login LOGIN
0440: 20 4e 4f 4e 43 45 20 53 49 47 4e 41 54 55 52 45   NONCE SIGNATURE
0450: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 4f 47 49 4e  .**.** The LOGIN
0460: 20 69 73 20 74 68 65 20 75 73 65 72 20 69 64 20   is the user id 
0470: 6f 66 20 74 68 65 20 63 6c 69 65 6e 74 2e 20 20  of the client.  
0480: 4e 4f 4e 43 45 20 69 73 20 74 68 65 20 73 68 61  NONCE is the sha
0490: 31 20 63 68 65 63 6b 73 75 6d 0a 2a 2a 20 6f 66  1 checksum.** of
04a0: 20 61 6c 6c 20 70 61 79 6c 6f 61 64 20 74 68 61   all payload tha
04b0: 74 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20 6c 6f  t follows the lo
04c0: 67 69 6e 20 63 61 72 64 2e 20 20 53 49 47 4e 41  gin card.  SIGNA
04d0: 54 55 52 45 20 69 73 20 74 68 65 20 73 68 61 31  TURE is the sha1
04e0: 0a 2a 2a 20 63 68 65 63 6b 73 75 6d 20 6f 66 20  .** checksum of 
04f0: 74 68 65 20 6e 6f 6e 63 65 20 66 6f 6c 6c 6f 77  the nonce follow
0500: 65 64 20 62 79 20 74 68 65 20 75 73 65 72 20 70  ed by the user p
0510: 61 73 73 77 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20 57  assword..**.** W
0520: 72 69 74 65 20 74 68 65 20 63 6f 6e 73 74 72 75  rite the constru
0530: 63 74 65 64 20 6c 6f 67 69 6e 20 63 61 72 64 20  cted login card 
0540: 69 6e 74 6f 20 70 4c 6f 67 69 6e 2e 20 20 70 4c  into pLogin.  pL
0550: 6f 67 69 6e 20 69 73 20 69 6e 69 74 69 61 6c 69  ogin is initiali
0560: 7a 65 64 0a 2a 2a 20 62 79 20 74 68 69 73 20 72  zed.** by this r
0570: 6f 75 74 69 6e 65 2e 0a 2a 2f 0a 73 74 61 74 69  outine..*/.stati
0580: 63 20 76 6f 69 64 20 68 74 74 70 5f 62 75 69 6c  c void http_buil
0590: 64 5f 6c 6f 67 69 6e 5f 63 61 72 64 28 42 6c 6f  d_login_card(Blo
05a0: 62 20 2a 70 50 61 79 6c 6f 61 64 2c 20 42 6c 6f  b *pPayload, Blo
05b0: 62 20 2a 70 4c 6f 67 69 6e 29 7b 0a 20 20 42 6c  b *pLogin){.  Bl
05c0: 6f 62 20 6e 6f 6e 63 65 3b 20 20 20 20 2f 2a 20  ob nonce;    /* 
05d0: 54 68 65 20 6e 6f 6e 63 65 20 2a 2f 0a 20 20 42  The nonce */.  B
05e0: 6c 6f 62 20 70 77 3b 20 20 20 20 20 20 20 2f 2a  lob pw;       /*
05f0: 20 54 68 65 20 75 73 65 72 20 70 61 73 73 77 6f   The user passwo
0600: 72 64 20 2a 2f 0a 20 20 42 6c 6f 62 20 73 69 67  rd */.  Blob sig
0610: 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69  ;      /* The si
0620: 67 6e 61 74 75 72 65 20 66 69 65 6c 64 20 2a 2f  gnature field */
0630: 0a 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 6e  ..  blob_zero(&n
0640: 6f 6e 63 65 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65  once);.  blob_ze
0650: 72 6f 28 26 70 77 29 3b 0a 20 20 73 68 61 31 73  ro(&pw);.  sha1s
0660: 75 6d 5f 62 6c 6f 62 28 70 50 61 79 6c 6f 61 64  um_blob(pPayload
0670: 2c 20 26 6e 6f 6e 63 65 29 3b 0a 20 20 62 6c 6f  , &nonce);.  blo
0680: 62 5f 63 6f 70 79 28 26 70 77 2c 20 26 6e 6f 6e  b_copy(&pw, &non
0690: 63 65 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f  ce);.  blob_zero
06a0: 28 70 4c 6f 67 69 6e 29 3b 0a 20 20 69 66 28 20  (pLogin);.  if( 
06b0: 67 2e 75 72 6c 55 73 65 72 3d 3d 30 20 29 7b 0a  g.urlUser==0 ){.
06c0: 20 20 20 20 75 73 65 72 5f 73 65 6c 65 63 74 28      user_select(
06d0: 29 3b 0a 20 20 20 20 64 62 5f 62 6c 6f 62 28 26  );.    db_blob(&
06e0: 70 77 2c 20 22 53 45 4c 45 43 54 20 70 77 20 46  pw, "SELECT pw F
06f0: 52 4f 4d 20 75 73 65 72 20 57 48 45 52 45 20 75  ROM user WHERE u
0700: 69 64 3d 25 64 22 2c 20 67 2e 75 73 65 72 55 69  id=%d", g.userUi
0710: 64 29 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f  d);.    sha1sum_
0720: 62 6c 6f 62 28 26 70 77 2c 20 26 73 69 67 29 3b  blob(&pw, &sig);
0730: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
0740: 66 28 70 4c 6f 67 69 6e 2c 20 22 6c 6f 67 69 6e  f(pLogin, "login
0750: 20 25 46 20 25 62 20 25 62 5c 6e 22 2c 20 67 2e   %F %b %b\n", g.
0760: 7a 4c 6f 67 69 6e 2c 20 26 6e 6f 6e 63 65 2c 20  zLogin, &nonce, 
0770: 26 73 69 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  &sig);.  }else{.
0780: 20 20 20 20 69 66 28 20 67 2e 75 72 6c 50 61 73      if( g.urlPas
0790: 73 77 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  swd==0 ){.      
07a0: 69 66 28 20 73 74 72 63 6d 70 28 67 2e 75 72 6c  if( strcmp(g.url
07b0: 55 73 65 72 2c 22 61 6e 6f 6e 79 6d 6f 75 73 22  User,"anonymous"
07c0: 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )!=0 ){.        
07d0: 63 68 61 72 20 2a 7a 50 72 6f 6d 70 74 20 3d 20  char *zPrompt = 
07e0: 6d 70 72 69 6e 74 66 28 22 70 61 73 73 77 6f 72  mprintf("passwor
07f0: 64 20 66 6f 72 20 25 73 3a 20 22 2c 20 67 2e 75  d for %s: ", g.u
0800: 72 6c 55 73 65 72 29 3b 0a 20 20 20 20 20 20 20  rlUser);.       
0810: 20 42 6c 6f 62 20 78 3b 0a 20 20 20 20 20 20 20   Blob x;.       
0820: 20 70 72 6f 6d 70 74 5f 66 6f 72 5f 70 61 73 73   prompt_for_pass
0830: 77 6f 72 64 28 7a 50 72 6f 6d 70 74 2c 20 26 78  word(zPrompt, &x
0840: 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 66 72  , 0);.        fr
0850: 65 65 28 7a 50 72 6f 6d 70 74 29 3b 0a 20 20 20  ee(zPrompt);.   
0860: 20 20 20 20 20 67 2e 75 72 6c 50 61 73 73 77 64       g.urlPasswd
0870: 20 3d 20 62 6c 6f 62 5f 73 74 72 28 26 78 29 3b   = blob_str(&x);
0880: 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
0890: 20 20 20 20 20 20 67 2e 75 72 6c 50 61 73 73 77        g.urlPassw
08a0: 64 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 7d 0a  d = "";.      }.
08b0: 20 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 61      }.    blob_a
08c0: 70 70 65 6e 64 28 26 70 77 2c 20 67 2e 75 72 6c  ppend(&pw, g.url
08d0: 50 61 73 73 77 64 2c 20 2d 31 29 3b 0a 20 20 20  Passwd, -1);.   
08e0: 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 26 70   sha1sum_blob(&p
08f0: 77 2c 20 26 73 69 67 29 3b 0a 20 20 20 20 62 6c  w, &sig);.    bl
0900: 6f 62 5f 61 70 70 65 6e 64 66 28 70 4c 6f 67 69  ob_appendf(pLogi
0910: 6e 2c 20 22 6c 6f 67 69 6e 20 25 46 20 25 62 20  n, "login %F %b 
0920: 25 62 5c 6e 22 2c 20 67 2e 75 72 6c 55 73 65 72  %b\n", g.urlUser
0930: 2c 20 26 6e 6f 6e 63 65 2c 20 26 73 69 67 29 3b  , &nonce, &sig);
0940: 0a 20 20 7d 20 20 20 20 20 20 20 20 0a 20 20 62  .  }        .  b
0950: 6c 6f 62 5f 72 65 73 65 74 28 26 6e 6f 6e 63 65  lob_reset(&nonce
0960: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28  );.  blob_reset(
0970: 26 70 77 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73  &pw);.  blob_res
0980: 65 74 28 26 73 69 67 29 3b 0a 7d 0a 0a 2f 2a 0a  et(&sig);.}../*.
0990: 2a 2a 20 43 6f 6e 73 74 72 75 63 74 20 61 6e 20  ** Construct an 
09a0: 61 70 70 72 6f 70 72 69 61 74 65 20 48 54 54 50  appropriate HTTP
09b0: 20 72 65 71 75 65 73 74 20 68 65 61 64 65 72 2e   request header.
09c0: 20 20 57 72 69 74 65 20 74 68 65 20 68 65 61 64    Write the head
09d0: 65 72 0a 2a 2a 20 69 6e 74 6f 20 70 48 64 72 2e  er.** into pHdr.
09e0: 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
09f0: 6e 69 74 69 61 6c 69 7a 65 73 20 74 68 65 20 70  nitializes the p
0a00: 48 64 72 20 62 6c 6f 62 2e 20 20 70 50 61 79 6c  Hdr blob.  pPayl
0a10: 6f 61 64 20 69 73 0a 2a 2a 20 74 68 65 20 63 6f  oad is.** the co
0a20: 6d 70 6c 65 74 65 20 70 61 79 6c 6f 61 64 20 28  mplete payload (
0a30: 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20 6c 6f  including the lo
0a40: 67 69 6e 20 63 61 72 64 29 20 61 6c 72 65 61 64  gin card) alread
0a50: 79 20 63 6f 6d 70 72 65 73 73 65 64 2e 0a 2a 2f  y compressed..*/
0a60: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 74 74  .static void htt
0a70: 70 5f 62 75 69 6c 64 5f 68 65 61 64 65 72 28 42  p_build_header(B
0a80: 6c 6f 62 20 2a 70 50 61 79 6c 6f 61 64 2c 20 42  lob *pPayload, B
0a90: 6c 6f 62 20 2a 70 48 64 72 29 7b 0a 20 20 69 6e  lob *pHdr){.  in
0aa0: 74 20 69 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  t i;.  const cha
0ab0: 72 20 2a 7a 53 65 70 3b 0a 0a 20 20 62 6c 6f 62  r *zSep;..  blob
0ac0: 5f 7a 65 72 6f 28 70 48 64 72 29 3b 0a 20 20 69  _zero(pHdr);.  i
0ad0: 20 3d 20 73 74 72 6c 65 6e 28 67 2e 75 72 6c 50   = strlen(g.urlP
0ae0: 61 74 68 29 3b 0a 20 20 69 66 28 20 69 3e 30 20  ath);.  if( i>0 
0af0: 26 26 20 67 2e 75 72 6c 50 61 74 68 5b 69 2d 31  && g.urlPath[i-1
0b00: 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 7a 53  ]=='/' ){.    zS
0b10: 65 70 20 3d 20 22 22 3b 0a 20 20 7d 65 6c 73 65  ep = "";.  }else
0b20: 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 2f 22  {.    zSep = "/"
0b30: 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 61 70 70  ;.  }.  blob_app
0b40: 65 6e 64 66 28 70 48 64 72 2c 20 22 50 4f 53 54  endf(pHdr, "POST
0b50: 20 25 73 25 73 78 66 65 72 20 48 54 54 50 2f 31   %s%sxfer HTTP/1
0b60: 2e 31 5c 72 5c 6e 22 2c 20 67 2e 75 72 6c 50 61  .1\r\n", g.urlPa
0b70: 74 68 2c 20 7a 53 65 70 29 3b 0a 20 20 62 6c 6f  th, zSep);.  blo
0b80: 62 5f 61 70 70 65 6e 64 66 28 70 48 64 72 2c 20  b_appendf(pHdr, 
0b90: 22 48 6f 73 74 3a 20 25 73 5c 72 5c 6e 22 2c 20  "Host: %s\r\n", 
0ba0: 67 2e 75 72 6c 48 6f 73 74 6e 61 6d 65 29 3b 0a  g.urlHostname);.
0bb0: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70    blob_appendf(p
0bc0: 48 64 72 2c 20 22 55 73 65 72 2d 41 67 65 6e 74  Hdr, "User-Agent
0bd0: 3a 20 46 6f 73 73 69 6c 2f 22 20 4d 41 4e 49 46  : Fossil/" MANIF
0be0: 45 53 54 5f 56 45 52 53 49 4f 4e 20 22 5c 72 5c  EST_VERSION "\r\
0bf0: 6e 22 29 3b 0a 20 20 69 66 28 20 67 2e 66 48 74  n");.  if( g.fHt
0c00: 74 70 54 72 61 63 65 20 29 7b 0a 20 20 20 20 62  tpTrace ){.    b
0c10: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 48 64 72  lob_appendf(pHdr
0c20: 2c 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a  , "Content-Type:
0c30: 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66   application/x-f
0c40: 6f 73 73 69 6c 2d 64 65 62 75 67 5c 72 5c 6e 22  ossil-debug\r\n"
0c50: 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
0c60: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 70 48 64  blob_appendf(pHd
0c70: 72 2c 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65  r, "Content-Type
0c80: 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d  : application/x-
0c90: 66 6f 73 73 69 6c 5c 72 5c 6e 22 29 3b 0a 20 20  fossil\r\n");.  
0ca0: 7d 0a 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66  }.  blob_appendf
0cb0: 28 70 48 64 72 2c 20 22 43 6f 6e 74 65 6e 74 2d  (pHdr, "Content-
0cc0: 4c 65 6e 67 74 68 3a 20 25 64 5c 72 5c 6e 5c 72  Length: %d\r\n\r
0cd0: 5c 6e 22 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 70  \n", blob_size(p
0ce0: 50 61 79 6c 6f 61 64 29 29 3b 0a 7d 0a 0a 2f 2a  Payload));.}../*
0cf0: 0a 2a 2a 20 53 69 67 6e 20 74 68 65 20 63 6f 6e  .** Sign the con
0d00: 74 65 6e 74 20 69 6e 20 70 53 65 6e 64 2c 20 63  tent in pSend, c
0d10: 6f 6d 70 72 65 73 73 20 69 74 2c 20 61 6e 64 20  ompress it, and 
0d20: 73 65 6e 64 20 69 74 20 74 6f 20 74 68 65 20 73  send it to the s
0d30: 65 72 76 65 72 0a 2a 2a 20 76 69 61 20 48 54 54  erver.** via HTT
0d40: 50 20 6f 72 20 48 54 54 50 53 2e 20 20 47 65 74  P or HTTPS.  Get
0d50: 20 61 20 72 65 70 6c 79 2c 20 75 6e 63 6f 6d 70   a reply, uncomp
0d60: 72 65 73 73 20 74 68 65 20 72 65 70 6c 79 2c 20  ress the reply, 
0d70: 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65  and store the re
0d80: 70 6c 79 0a 2a 2a 20 69 6e 20 70 52 65 63 76 2e  ply.** in pRecv.
0d90: 20 20 70 52 65 63 76 20 69 73 20 61 73 73 75 6d    pRecv is assum
0da0: 65 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69  ed to be uniniti
0db0: 61 6c 69 7a 65 64 20 77 68 65 6e 0a 2a 2a 20 74  alized when.** t
0dc0: 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63  his routine is c
0dd0: 61 6c 6c 65 64 20 2d 20 74 68 69 73 20 72 6f 75  alled - this rou
0de0: 74 69 6e 65 20 77 69 6c 6c 20 69 6e 69 74 69 61  tine will initia
0df0: 6c 69 7a 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  lize it..**.** T
0e00: 68 65 20 73 65 72 76 65 72 20 61 64 64 72 65 73  he server addres
0e10: 73 20 69 73 20 63 6f 6e 74 61 69 6e 20 69 6e 20  s is contain in 
0e20: 74 68 65 20 22 67 22 20 67 6c 6f 62 61 6c 20 73  the "g" global s
0e30: 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a  tructure.  The.*
0e40: 2a 20 75 72 6c 5f 70 61 72 73 65 28 29 20 72 6f  * url_parse() ro
0e50: 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 68 61 76  utine should hav
0e60: 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 70 72  e been called pr
0e70: 69 6f 72 20 74 6f 20 74 68 69 73 20 72 6f 75 74  ior to this rout
0e80: 69 6e 65 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  ine.** in order 
0e90: 74 6f 20 66 69 6c 6c 20 74 68 69 73 20 73 74 72  to fill this str
0ea0: 75 63 74 75 72 65 20 61 70 70 72 6f 70 72 69 61  ucture appropria
0eb0: 74 65 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 68 74  tely..*/.void ht
0ec0: 74 70 5f 65 78 63 68 61 6e 67 65 28 42 6c 6f 62  tp_exchange(Blob
0ed0: 20 2a 70 53 65 6e 64 2c 20 42 6c 6f 62 20 2a 70   *pSend, Blob *p
0ee0: 52 65 70 6c 79 29 7b 0a 20 20 42 6c 6f 62 20 6c  Reply){.  Blob l
0ef0: 6f 67 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20  ogin;           
0f00: 2f 2a 20 54 68 65 20 6c 6f 67 69 6e 20 63 61 72  /* The login car
0f10: 64 20 2a 2f 0a 20 20 42 6c 6f 62 20 70 61 79 6c  d */.  Blob payl
0f20: 6f 61 64 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  oad;         /* 
0f30: 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 70 61 79  The complete pay
0f40: 6c 6f 61 64 20 69 6e 63 6c 75 64 69 6e 67 20 6c  load including l
0f50: 6f 67 69 6e 20 63 61 72 64 20 2a 2f 0a 20 20 42  ogin card */.  B
0f60: 6c 6f 62 20 68 64 72 3b 20 20 20 20 20 20 20 20  lob hdr;        
0f70: 20 20 20 20 20 2f 2a 20 54 68 65 20 48 54 54 50       /* The HTTP
0f80: 20 72 65 71 75 65 73 74 20 68 65 61 64 65 72 20   request header 
0f90: 2a 2f 0a 20 20 69 6e 74 20 63 6c 6f 73 65 43 6f  */.  int closeCo
0fa0: 6e 6e 65 63 74 69 6f 6e 3b 20 20 2f 2a 20 54 72  nnection;  /* Tr
0fb0: 75 65 20 74 6f 20 63 6c 6f 73 65 20 74 68 65 20  ue to close the 
0fc0: 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 68 65 6e 20  connection when 
0fd0: 64 6f 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4c  done */.  int iL
0fe0: 65 6e 67 74 68 3b 20 20 20 20 20 20 20 20 20 20  ength;          
0ff0: 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74 68 65  /* Length of the
1000: 20 72 65 70 6c 79 20 70 61 79 6c 6f 61 64 20 2a   reply payload *
1010: 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20  /.  int rc;     
1020: 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 73            /* Res
1030: 75 6c 74 20 63 6f 64 65 20 2a 2f 0a 20 20 69 6e  ult code */.  in
1040: 74 20 69 48 74 74 70 56 65 72 73 69 6f 6e 3b 20  t iHttpVersion; 
1050: 20 20 20 20 2f 2a 20 57 68 69 63 68 20 76 65 72      /* Which ver
1060: 73 69 6f 6e 20 6f 66 20 48 54 54 50 20 70 72 6f  sion of HTTP pro
1070: 74 6f 63 6f 6c 20 73 65 72 76 65 72 20 75 73 65  tocol server use
1080: 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4c 69  s */.  char *zLi
1090: 6e 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ne;          /* 
10a0: 41 20 73 69 6e 67 6c 65 20 6c 69 6e 65 20 6f 66  A single line of
10b0: 20 74 68 65 20 72 65 70 6c 79 20 68 65 61 64 65   the reply heade
10c0: 72 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20  r */.  int i;   
10d0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
10e0: 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  Loop counter */.
10f0: 0a 20 20 69 66 28 20 74 72 61 6e 73 70 6f 72 74  .  if( transport
1100: 5f 6f 70 65 6e 28 29 20 29 7b 0a 20 20 20 20 66  _open() ){.    f
1110: 6f 73 73 69 6c 5f 66 61 74 61 6c 28 74 72 61 6e  ossil_fatal(tran
1120: 73 70 6f 72 74 5f 65 72 72 6d 73 67 28 29 29 3b  sport_errmsg());
1130: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74  .  }..  /* Const
1140: 72 75 63 74 20 74 68 65 20 6c 6f 67 69 6e 20 63  ruct the login c
1150: 61 72 64 20 61 6e 64 20 70 72 65 70 61 72 65 20  ard and prepare 
1160: 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 70 61 79  the complete pay
1170: 6c 6f 61 64 20 2a 2f 0a 20 20 68 74 74 70 5f 62  load */.  http_b
1180: 75 69 6c 64 5f 6c 6f 67 69 6e 5f 63 61 72 64 28  uild_login_card(
1190: 70 53 65 6e 64 2c 20 26 6c 6f 67 69 6e 29 3b 0a  pSend, &login);.
11a0: 20 20 69 66 28 20 67 2e 66 48 74 74 70 54 72 61    if( g.fHttpTra
11b0: 63 65 20 29 7b 0a 20 20 20 20 70 61 79 6c 6f 61  ce ){.    payloa
11c0: 64 20 3d 20 6c 6f 67 69 6e 3b 0a 20 20 20 20 62  d = login;.    b
11d0: 6c 6f 62 5f 61 70 70 65 6e 64 28 26 70 61 79 6c  lob_append(&payl
11e0: 6f 61 64 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72  oad, blob_buffer
11f0: 28 70 53 65 6e 64 29 2c 20 62 6c 6f 62 5f 73 69  (pSend), blob_si
1200: 7a 65 28 70 53 65 6e 64 29 29 3b 0a 20 20 7d 65  ze(pSend));.  }e
1210: 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f  lse{.    blob_co
1220: 6d 70 72 65 73 73 32 28 26 6c 6f 67 69 6e 2c 20  mpress2(&login, 
1230: 70 53 65 6e 64 2c 20 26 70 61 79 6c 6f 61 64 29  pSend, &payload)
1240: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
1250: 28 26 6c 6f 67 69 6e 29 3b 0a 20 20 7d 0a 0a 20  (&login);.  }.. 
1260: 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68   /* Construct th
1270: 65 20 48 54 54 50 20 72 65 71 75 65 73 74 20 68  e HTTP request h
1280: 65 61 64 65 72 20 2a 2f 0a 20 20 68 74 74 70 5f  eader */.  http_
1290: 62 75 69 6c 64 5f 68 65 61 64 65 72 28 26 70 61  build_header(&pa
12a0: 79 6c 6f 61 64 2c 20 26 68 64 72 29 3b 0a 0a 20  yload, &hdr);.. 
12b0: 20 2f 2a 20 57 68 65 6e 20 74 72 61 63 69 6e 67   /* When tracing
12c0: 2c 20 77 72 69 74 65 20 74 68 65 20 74 72 61 6e  , write the tran
12d0: 73 6d 69 74 74 65 64 20 48 54 54 50 20 6d 65 73  smitted HTTP mes
12e0: 73 61 67 65 20 62 6f 74 68 20 74 6f 20 73 74 61  sage both to sta
12f0: 6e 64 61 72 64 0a 20 20 2a 2a 20 6f 75 74 70 75  ndard.  ** outpu
1300: 74 20 61 6e 64 20 69 6e 74 6f 20 61 20 66 69 6c  t and into a fil
1310: 65 2e 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e  e.  The file can
1320: 20 74 68 65 6e 20 62 65 20 75 73 65 64 20 74 6f   then be used to
1330: 20 64 72 69 76 65 20 74 68 65 0a 20 20 2a 2a 20   drive the.  ** 
1340: 73 65 72 76 65 72 2d 73 69 64 65 20 6c 69 6b 65  server-side like
1350: 20 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a   this:.  **.  **
1360: 20 20 20 20 20 20 2e 2f 66 6f 73 73 69 6c 20 68        ./fossil h
1370: 74 74 70 20 3c 68 74 74 70 2d 74 72 61 63 65 2d  ttp <http-trace-
1380: 31 2e 74 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28  1.txt.  */.  if(
1390: 20 67 2e 66 48 74 74 70 54 72 61 63 65 20 29 7b   g.fHttpTrace ){
13a0: 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20  .    static int 
13b0: 74 72 61 63 65 43 6e 74 20 3d 20 30 3b 0a 20 20  traceCnt = 0;.  
13c0: 20 20 63 68 61 72 20 2a 7a 4f 75 74 46 69 6c 65    char *zOutFile
13d0: 3b 0a 20 20 20 20 46 49 4c 45 20 2a 6f 75 74 3b  ;.    FILE *out;
13e0: 0a 20 20 20 20 74 72 61 63 65 43 6e 74 2b 2b 3b  .    traceCnt++;
13f0: 0a 20 20 20 20 7a 4f 75 74 46 69 6c 65 20 3d 20  .    zOutFile = 
1400: 6d 70 72 69 6e 74 66 28 22 68 74 74 70 2d 74 72  mprintf("http-tr
1410: 61 63 65 2d 25 64 2e 74 78 74 22 2c 20 74 72 61  ace-%d.txt", tra
1420: 63 65 43 6e 74 29 3b 0a 20 20 20 20 70 72 69 6e  ceCnt);.    prin
1430: 74 66 28 22 48 54 54 50 20 53 45 4e 44 3a 20 28  tf("HTTP SEND: (
1440: 25 73 29 5c 6e 25 73 25 73 3d 3d 3d 3d 3d 3d 3d  %s)\n%s%s=======
1450: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
1460: 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 7a 4f  \n", .        zO
1470: 75 74 46 69 6c 65 2c 20 62 6c 6f 62 5f 73 74 72  utFile, blob_str
1480: 28 26 68 64 72 29 2c 20 62 6c 6f 62 5f 73 74 72  (&hdr), blob_str
1490: 28 26 70 61 79 6c 6f 61 64 29 29 3b 0a 20 20 20  (&payload));.   
14a0: 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 4f 75   out = fopen(zOu
14b0: 74 46 69 6c 65 2c 20 22 77 22 29 3b 0a 20 20 20  tFile, "w");.   
14c0: 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20   if( out ){.    
14d0: 20 20 66 77 72 69 74 65 28 62 6c 6f 62 5f 62 75    fwrite(blob_bu
14e0: 66 66 65 72 28 26 68 64 72 29 2c 20 31 2c 20 62  ffer(&hdr), 1, b
14f0: 6c 6f 62 5f 73 69 7a 65 28 26 68 64 72 29 2c 20  lob_size(&hdr), 
1500: 6f 75 74 29 3b 0a 20 20 20 20 20 20 66 77 72 69  out);.      fwri
1510: 74 65 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  te(blob_buffer(&
1520: 70 61 79 6c 6f 61 64 29 2c 20 31 2c 20 62 6c 6f  payload), 1, blo
1530: 62 5f 73 69 7a 65 28 26 70 61 79 6c 6f 61 64 29  b_size(&payload)
1540: 2c 20 6f 75 74 29 3b 0a 20 20 20 20 20 20 66 63  , out);.      fc
1550: 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d  lose(out);.    }
1560: 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
1570: 53 65 6e 64 20 74 68 65 20 72 65 71 75 65 73 74  Send the request
1580: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 2e 0a   to the server..
1590: 20 20 2a 2f 0a 20 20 74 72 61 6e 73 70 6f 72 74    */.  transport
15a0: 5f 73 65 6e 64 28 26 68 64 72 29 3b 0a 20 20 74  _send(&hdr);.  t
15b0: 72 61 6e 73 70 6f 72 74 5f 73 65 6e 64 28 26 70  ransport_send(&p
15c0: 61 79 6c 6f 61 64 29 3b 0a 20 20 62 6c 6f 62 5f  ayload);.  blob_
15d0: 72 65 73 65 74 28 26 68 64 72 29 3b 0a 20 20 62  reset(&hdr);.  b
15e0: 6c 6f 62 5f 72 65 73 65 74 28 26 70 61 79 6c 6f  lob_reset(&paylo
15f0: 61 64 29 3b 0a 20 20 74 72 61 6e 73 70 6f 72 74  ad);.  transport
1600: 5f 66 6c 69 70 28 29 3b 0a 20 20 0a 20 20 2f 2a  _flip();.  .  /*
1610: 0a 20 20 2a 2a 20 52 65 61 64 20 61 6e 64 20 69  .  ** Read and i
1620: 6e 74 65 72 70 72 65 74 20 74 68 65 20 73 65 72  nterpret the ser
1630: 76 65 72 20 72 65 70 6c 79 0a 20 20 2a 2f 0a 20  ver reply.  */. 
1640: 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e   closeConnection
1650: 20 3d 20 31 3b 0a 20 20 69 4c 65 6e 67 74 68 20   = 1;.  iLength 
1660: 3d 20 2d 31 3b 0a 20 20 77 68 69 6c 65 28 20 28  = -1;.  while( (
1670: 7a 4c 69 6e 65 20 3d 20 74 72 61 6e 73 70 6f 72  zLine = transpor
1680: 74 5f 72 65 63 65 69 76 65 5f 6c 69 6e 65 28 29  t_receive_line()
1690: 29 21 3d 30 20 26 26 20 7a 4c 69 6e 65 5b 30 5d  )!=0 && zLine[0]
16a0: 21 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 73  !=0 ){.    if( s
16b0: 74 72 6e 63 61 73 65 63 6d 70 28 7a 4c 69 6e 65  trncasecmp(zLine
16c0: 2c 20 22 68 74 74 70 2f 31 2e 22 2c 20 37 29 3d  , "http/1.", 7)=
16d0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  =0 ){.      if( 
16e0: 73 73 63 61 6e 66 28 7a 4c 69 6e 65 2c 20 22 48  sscanf(zLine, "H
16f0: 54 54 50 2f 31 2e 25 64 20 25 64 22 2c 20 26 69  TTP/1.%d %d", &i
1700: 48 74 74 70 56 65 72 73 69 6f 6e 2c 20 26 72 63  HttpVersion, &rc
1710: 29 21 3d 32 20 29 20 67 6f 74 6f 20 77 72 69 74  )!=2 ) goto writ
1720: 65 5f 65 72 72 3b 0a 20 20 20 20 20 20 69 66 28  e_err;.      if(
1730: 20 72 63 21 3d 32 30 30 20 29 20 67 6f 74 6f 20   rc!=200 ) goto 
1740: 77 72 69 74 65 5f 65 72 72 3b 0a 20 20 20 20 20  write_err;.     
1750: 20 69 66 28 20 69 48 74 74 70 56 65 72 73 69 6f   if( iHttpVersio
1760: 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n==0 ){.        
1770: 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20  closeConnection 
1780: 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = 1;.      }else
1790: 7b 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 43  {.        closeC
17a0: 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 0a 20  onnection = 0;. 
17b0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73       }.    } els
17c0: 65 20 69 66 28 20 73 74 72 6e 63 61 73 65 63 6d  e if( strncasecm
17d0: 70 28 7a 4c 69 6e 65 2c 20 22 63 6f 6e 74 65 6e  p(zLine, "conten
17e0: 74 2d 6c 65 6e 67 74 68 3a 22 2c 20 31 35 29 3d  t-length:", 15)=
17f0: 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28  =0 ){.      for(
1800: 69 3d 31 35 3b 20 69 73 73 70 61 63 65 28 7a 4c  i=15; isspace(zL
1810: 69 6e 65 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a  ine[i]); i++){}.
1820: 20 20 20 20 20 20 69 4c 65 6e 67 74 68 20 3d 20        iLength = 
1830: 61 74 6f 69 28 26 7a 4c 69 6e 65 5b 69 5d 29 3b  atoi(&zLine[i]);
1840: 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73  .    }else if( s
1850: 74 72 6e 63 61 73 65 63 6d 70 28 7a 4c 69 6e 65  trncasecmp(zLine
1860: 2c 20 22 63 6f 6e 6e 65 63 74 69 6f 6e 3a 22 2c  , "connection:",
1870: 20 31 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20   11)==0 ){.     
1880: 20 63 68 61 72 20 63 3b 0a 20 20 20 20 20 20 66   char c;.      f
1890: 6f 72 28 69 3d 31 31 3b 20 69 73 73 70 61 63 65  or(i=11; isspace
18a0: 28 7a 4c 69 6e 65 5b 69 5d 29 3b 20 69 2b 2b 29  (zLine[i]); i++)
18b0: 7b 7d 0a 20 20 20 20 20 20 63 20 3d 20 7a 4c 69  {}.      c = zLi
18c0: 6e 65 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  ne[i];.      if(
18d0: 20 63 3d 3d 27 63 27 20 7c 7c 20 63 3d 3d 27 43   c=='c' || c=='C
18e0: 27 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6c 6f  ' ){.        clo
18f0: 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 31  seConnection = 1
1900: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1910: 28 20 63 3d 3d 27 6b 27 20 7c 7c 20 63 3d 3d 27  ( c=='k' || c=='
1920: 4b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6c  K' ){.        cl
1930: 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20  oseConnection = 
1940: 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  0;.      }.    }
1950: 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
1960: 45 78 74 72 61 63 74 20 74 68 65 20 72 65 70 6c  Extract the repl
1970: 79 20 70 61 79 6c 6f 61 64 20 74 68 61 74 20 66  y payload that f
1980: 6f 6c 6c 6f 77 73 20 74 68 65 20 68 65 61 64 65  ollows the heade
1990: 72 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 4c 65  r.  */.  if( iLe
19a0: 6e 67 74 68 3c 30 20 29 20 67 6f 74 6f 20 77 72  ngth<0 ) goto wr
19b0: 69 74 65 5f 65 72 72 3b 0a 20 20 62 6c 6f 62 5f  ite_err;.  blob_
19c0: 7a 65 72 6f 28 70 52 65 70 6c 79 29 3b 0a 20 20  zero(pReply);.  
19d0: 62 6c 6f 62 5f 72 65 73 69 7a 65 28 70 52 65 70  blob_resize(pRep
19e0: 6c 79 2c 20 69 4c 65 6e 67 74 68 29 3b 0a 20 20  ly, iLength);.  
19f0: 69 4c 65 6e 67 74 68 20 3d 20 74 72 61 6e 73 70  iLength = transp
1a00: 6f 72 74 5f 72 65 63 65 69 76 65 28 62 6c 6f 62  ort_receive(blob
1a10: 5f 62 75 66 66 65 72 28 70 52 65 70 6c 79 29 2c  _buffer(pReply),
1a20: 20 69 4c 65 6e 67 74 68 29 3b 0a 20 20 62 6c 6f   iLength);.  blo
1a30: 62 5f 72 65 73 69 7a 65 28 70 52 65 70 6c 79 2c  b_resize(pReply,
1a40: 20 69 4c 65 6e 67 74 68 29 3b 0a 20 20 69 66 28   iLength);.  if(
1a50: 20 67 2e 66 48 74 74 70 54 72 61 63 65 20 29 7b   g.fHttpTrace ){
1a60: 0a 20 20 20 20 70 72 69 6e 74 66 28 22 48 54 54  .    printf("HTT
1a70: 50 20 52 45 43 45 49 56 45 3a 5c 6e 25 73 5c 6e  P RECEIVE:\n%s\n
1a80: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
1a90: 3d 3d 3d 3d 3d 3d 3d 5c 6e 22 2c 20 62 6c 6f 62  =======\n", blob
1aa0: 5f 73 74 72 28 70 52 65 70 6c 79 29 29 3b 0a 20  _str(pReply));. 
1ab0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62   }else{.    blob
1ac0: 5f 75 6e 63 6f 6d 70 72 65 73 73 28 70 52 65 70  _uncompress(pRep
1ad0: 6c 79 2c 20 70 52 65 70 6c 79 29 3b 0a 20 20 7d  ly, pReply);.  }
1ae0: 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 6c 6f 73  ..  /*.  ** Clos
1af0: 65 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  e the connection
1b00: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 69   to the server i
1b10: 66 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20  f appropriate.. 
1b20: 20 2a 2a 0a 20 20 2a 2a 20 46 49 58 4d 45 3a 20   **.  ** FIXME: 
1b30: 20 54 68 65 72 65 20 69 73 20 73 6f 6d 65 20 62   There is some b
1b40: 75 67 20 69 6e 20 74 68 65 20 6c 6f 77 65 72 20  ug in the lower 
1b50: 6c 61 79 65 72 73 20 74 68 61 74 20 70 72 65 76  layers that prev
1b60: 65 6e 74 73 20 74 68 65 0a 20 20 2a 2a 20 63 6f  ents the.  ** co
1b70: 6e 6e 65 63 74 69 6f 6e 20 66 72 6f 6d 20 72 65  nnection from re
1b80: 6d 61 69 6e 69 6e 67 20 6f 70 65 6e 2e 20 20 54  maining open.  T
1b90: 68 65 20 65 61 73 69 65 73 74 20 66 69 78 20 66  he easiest fix f
1ba0: 6f 72 20 6e 6f 77 20 69 73 20 74 6f 0a 20 20 2a  or now is to.  *
1bb0: 2a 20 73 69 6d 70 6c 79 20 63 6c 6f 73 65 20 61  * simply close a
1bc0: 6e 64 20 72 65 73 74 61 72 74 20 74 68 65 20 63  nd restart the c
1bd0: 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 65 61  onnection for ea
1be0: 63 68 20 72 6f 75 6e 64 2d 74 72 69 70 2e 0a 20  ch round-trip.. 
1bf0: 20 2a 2f 0a 20 20 63 6c 6f 73 65 43 6f 6e 6e 65   */.  closeConne
1c00: 63 74 69 6f 6e 20 3d 20 31 3b 20 2f 2a 20 46 49  ction = 1; /* FI
1c10: 58 20 4d 45 20 2a 2f 0a 20 20 69 66 28 20 63 6c  X ME */.  if( cl
1c20: 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 7b  oseConnection ){
1c30: 0a 20 20 20 20 74 72 61 6e 73 70 6f 72 74 5f 63  .    transport_c
1c40: 6c 6f 73 65 28 29 3b 0a 20 20 7d 65 6c 73 65 7b  lose();.  }else{
1c50: 0a 20 20 20 20 74 72 61 6e 73 70 6f 72 74 5f 72  .    transport_r
1c60: 65 77 69 6e 64 28 29 3b 0a 20 20 7d 0a 20 20 72  ewind();.  }.  r
1c70: 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 0a 20 20  eturn;..  /* .  
1c80: 2a 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20  ** Jump to here 
1c90: 69 66 20 61 6e 20 65 72 72 6f 72 20 69 73 20 73  if an error is s
1ca0: 65 65 6e 2e 0a 20 20 2a 2f 0a 77 72 69 74 65 5f  een..  */.write_
1cb0: 65 72 72 3a 0a 20 20 74 72 61 6e 73 70 6f 72 74  err:.  transport
1cc0: 5f 63 6c 6f 73 65 28 29 3b 0a 20 20 72 65 74 75  _close();.  retu
1cd0: 72 6e 3b 20 20 0a 7d 0a                          rn;  .}.