Hex Artifact Content
Not logged in

Artifact c7f9d7fa7d3b25c3809935f2ae49abcf8e93b9ab:

File src/http.c part of check-in [09d6a6eb16] - If no userid is specified in the URL then the client omits the login card from its request. by drh on 2009-03-29 11:41:52.

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 66 64 65 66 20 5f 5f 4d  tp.h".#ifdef __M
03e0: 49 4e 47 57 33 32 5f 5f 0a 23 20 20 69 6e 63 6c  INGW32__.#  incl
03f0: 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a  ude <windows.h>.
0400: 23 20 20 69 6e 63 6c 75 64 65 20 3c 77 69 6e 73  #  include <wins
0410: 6f 63 6b 32 2e 68 3e 0a 23 65 6c 73 65 0a 23 20  ock2.h>.#else.# 
0420: 20 69 6e 63 6c 75 64 65 20 3c 61 72 70 61 2f 69   include <arpa/i
0430: 6e 65 74 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64  net.h>.#  includ
0440: 65 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 3e  e <sys/socket.h>
0450: 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 6e 65 74  .#  include <net
0460: 64 62 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65  db.h>.#  include
0470: 20 3c 6e 65 74 69 6e 65 74 2f 69 6e 2e 68 3e 0a   <netinet/in.h>.
0480: 23 65 6e 64 69 66 0a 23 69 6e 63 6c 75 64 65 20  #endif.#include 
0490: 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c  <assert.h>.#incl
04a0: 75 64 65 20 3c 73 79 73 2f 74 79 70 65 73 2e 68  ude <sys/types.h
04b0: 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 69 67 6e  >.#include <sign
04c0: 61 6c 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  al.h>../*.** Per
04d0: 73 69 73 74 65 6e 74 20 69 6e 66 6f 72 6d 61 74  sistent informat
04e0: 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 48 54  ion about the HT
04f0: 54 50 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  TP connection..*
0500: 2f 0a 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e 47  /..#ifdef __MING
0510: 57 33 32 5f 5f 0a 73 74 61 74 69 63 20 57 53 41  W32__.static WSA
0520: 44 41 54 41 20 77 73 5f 69 6e 66 6f 3b 0a 73 74  DATA ws_info;.st
0530: 61 74 69 63 20 69 6e 74 20 70 53 6f 63 6b 65 74  atic int pSocket
0540: 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20 54 68 65   = 0;     /* The
0550: 20 73 6f 63 6b 65 74 20 6f 6e 20 77 68 69 63 68   socket on which
0560: 20 77 65 20 74 61 6c 6b 20 74 6f 20 74 68 65 20   we talk to the 
0570: 73 65 72 76 65 72 20 6f 6e 20 2a 2f 0a 23 65 6c  server on */.#el
0580: 73 65 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a  se.static FILE *
0590: 70 53 6f 63 6b 65 74 20 3d 20 30 3b 20 20 20 2f  pSocket = 0;   /
05a0: 2a 20 54 68 65 20 73 6f 63 6b 65 74 20 66 69 6c  * The socket fil
05b0: 65 68 61 6e 64 6c 65 20 6f 6e 20 77 68 69 63 68  ehandle on which
05c0: 20 77 65 20 74 61 6c 6b 20 74 6f 20 74 68 65 20   we talk to the 
05d0: 73 65 72 76 65 72 20 2a 2f 0a 23 65 6e 64 69 66  server */.#endif
05e0: 0a 0a 2f 2a 0a 2a 2a 20 57 69 6e 73 6f 63 6b 20  ../*.** Winsock 
05f0: 6d 75 73 74 20 62 65 20 69 6e 69 74 69 61 6c 69  must be initiali
0600: 7a 65 20 62 65 66 6f 72 65 20 75 73 65 2e 20 20  ze before use.  
0610: 54 68 69 73 20 68 65 6c 70 65 72 20 6d 65 74 68  This helper meth
0620: 6f 64 20 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a  od allows us to.
0630: 2a 2a 20 61 6c 77 61 79 73 20 63 61 6c 6c 20 77  ** always call w
0640: 73 5f 69 6e 69 74 20 69 6e 20 6f 75 72 20 63 6f  s_init in our co
0650: 64 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  de regardless of
0660: 20 70 6c 61 74 66 6f 72 6d 20 62 75 74 20 6f 6e   platform but on
0670: 6c 79 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 69  ly actually.** i
0680: 6e 69 74 69 61 6c 69 7a 65 20 77 69 6e 73 6f 63  nitialize winsoc
0690: 6b 20 6f 6e 20 74 68 65 20 77 69 6e 64 6f 77 73  k on the windows
06a0: 20 70 6c 61 74 66 6f 72 6d 2e 0a 2a 2f 0a 73 74   platform..*/.st
06b0: 61 74 69 63 20 76 6f 69 64 20 77 73 5f 69 6e 69  atic void ws_ini
06c0: 74 28 29 7b 0a 23 69 66 64 65 66 20 5f 5f 4d 49  t(){.#ifdef __MI
06d0: 4e 47 57 33 32 5f 5f 0a 20 20 69 66 20 28 57 53  NGW32__.  if (WS
06e0: 41 53 74 61 72 74 75 70 28 4d 41 4b 45 57 4f 52  AStartup(MAKEWOR
06f0: 44 28 32 2c 30 29 2c 20 26 77 73 5f 69 6e 66 6f  D(2,0), &ws_info
0700: 29 20 21 3d 20 30 29 7b 0a 20 20 20 20 66 6f 73  ) != 0){.    fos
0710: 73 69 6c 5f 70 61 6e 69 63 28 22 63 61 6e 27 74  sil_panic("can't
0720: 20 69 6e 69 74 69 61 6c 69 7a 65 20 77 69 6e 73   initialize wins
0730: 6f 63 6b 22 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  ock");.  }.#endi
0740: 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20  f.}../*.** Like 
0750: 77 73 5f 69 6e 69 74 2c 20 77 69 6e 73 6f 63 6b  ws_init, winsock
0760: 20 6d 75 73 74 20 61 6c 73 6f 20 62 65 20 63 6c   must also be cl
0770: 65 61 6e 65 64 20 75 70 20 61 66 74 65 72 2e 0a  eaned up after..
0780: 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77  */.static void w
0790: 73 5f 63 6c 65 61 6e 75 70 28 29 7b 0a 23 69 66  s_cleanup(){.#if
07a0: 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f 0a  def __MINGW32__.
07b0: 20 20 57 53 41 43 6c 65 61 6e 75 70 28 29 3b 0a    WSACleanup();.
07c0: 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  #endif.}../*.** 
07d0: 4f 70 65 6e 20 61 20 73 6f 63 6b 65 74 20 63 6f  Open a socket co
07e0: 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20  nnection to the 
07f0: 73 65 72 76 65 72 2e 20 20 52 65 74 75 72 6e 20  server.  Return 
0800: 30 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64  0 on success and
0810: 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20  .** non-zero if 
0820: 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  an error occurs.
0830: 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68  .*/.static int h
0840: 74 74 70 5f 6f 70 65 6e 5f 73 6f 63 6b 65 74 28  ttp_open_socket(
0850: 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
0860: 73 74 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f  struct sockaddr_
0870: 69 6e 20 61 64 64 72 3b 20 20 2f 2a 20 54 68 65  in addr;  /* The
0880: 20 73 65 72 76 65 72 20 61 64 64 72 65 73 73 20   server address 
0890: 2a 2f 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20  */.  static int 
08a0: 61 64 64 72 49 73 49 6e 69 74 20 3d 20 30 3b 20  addrIsInit = 0; 
08b0: 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e        /* True on
08c0: 63 65 20 61 64 64 72 20 69 73 20 69 6e 69 74 69  ce addr is initi
08d0: 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20  alized */.  int 
08e0: 73 3b 0a 0a 20 20 69 66 28 20 67 2e 75 72 6c 49  s;..  if( g.urlI
08f0: 73 48 74 74 70 73 20 29 7b 0a 20 20 20 20 66 6f  sHttps ){.    fo
0900: 73 73 69 6c 5f 66 61 74 61 6c 28 22 53 53 4c 2f  ssil_fatal("SSL/
0910: 54 4c 53 20 69 73 20 6e 6f 74 20 79 65 74 20 69  TLS is not yet i
0920: 6d 70 6c 65 6d 65 6e 74 65 64 2e 22 29 3b 0a 20  mplemented.");. 
0930: 20 7d 0a 20 20 77 73 5f 69 6e 69 74 28 29 3b 0a   }.  ws_init();.
0940: 20 20 69 66 28 20 21 61 64 64 72 49 73 49 6e 69    if( !addrIsIni
0950: 74 20 29 7b 0a 20 20 20 20 61 64 64 72 2e 73 69  t ){.    addr.si
0960: 6e 5f 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e  n_family = AF_IN
0970: 45 54 3b 0a 20 20 20 20 61 64 64 72 2e 73 69 6e  ET;.    addr.sin
0980: 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 28 67 2e  _port = htons(g.
0990: 75 72 6c 50 6f 72 74 29 3b 0a 20 20 20 20 2a 28  urlPort);.    *(
09a0: 69 6e 74 2a 29 26 61 64 64 72 2e 73 69 6e 5f 61  int*)&addr.sin_a
09b0: 64 64 72 20 3d 20 69 6e 65 74 5f 61 64 64 72 28  ddr = inet_addr(
09c0: 67 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 20 20  g.urlName);.    
09d0: 69 66 28 20 2d 31 20 3d 3d 20 2a 28 69 6e 74 2a  if( -1 == *(int*
09e0: 29 26 61 64 64 72 2e 73 69 6e 5f 61 64 64 72 20  )&addr.sin_addr 
09f0: 29 7b 0a 23 69 66 6e 64 65 66 20 46 4f 53 53 49  ){.#ifndef FOSSI
0a00: 4c 5f 53 54 41 54 49 43 5f 4c 49 4e 4b 0a 20 20  L_STATIC_LINK.  
0a10: 20 20 20 20 73 74 72 75 63 74 20 68 6f 73 74 65      struct hoste
0a20: 6e 74 20 2a 70 48 6f 73 74 3b 0a 20 20 20 20 20  nt *pHost;.     
0a30: 20 70 48 6f 73 74 20 3d 20 67 65 74 68 6f 73 74   pHost = gethost
0a40: 62 79 6e 61 6d 65 28 67 2e 75 72 6c 4e 61 6d 65  byname(g.urlName
0a50: 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48 6f  );.      if( pHo
0a60: 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  st!=0 ){.       
0a70: 20 6d 65 6d 63 70 79 28 26 61 64 64 72 2e 73 69   memcpy(&addr.si
0a80: 6e 5f 61 64 64 72 2c 70 48 6f 73 74 2d 3e 68 5f  n_addr,pHost->h_
0a90: 61 64 64 72 5f 6c 69 73 74 5b 30 5d 2c 70 48 6f  addr_list[0],pHo
0aa0: 73 74 2d 3e 68 5f 6c 65 6e 67 74 68 29 3b 0a 20  st->h_length);. 
0ab0: 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69       }else.#endi
0ac0: 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  f.      {.      
0ad0: 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22    fossil_panic("
0ae0: 63 61 6e 27 74 20 72 65 73 6f 6c 76 65 20 68 6f  can't resolve ho
0af0: 73 74 20 6e 61 6d 65 3a 20 25 73 5c 6e 22 2c 20  st name: %s\n", 
0b00: 67 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 20 20  g.urlName);.    
0b10: 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 64    }.    }.    ad
0b20: 64 72 49 73 49 6e 69 74 20 3d 20 31 3b 0a 0a 20  drIsInit = 1;.. 
0b30: 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 47 6c     /* Set the Gl
0b40: 6f 62 61 6c 2e 7a 49 70 41 64 64 72 20 76 61 72  obal.zIpAddr var
0b50: 69 61 62 6c 65 20 74 6f 20 74 68 65 20 73 65 72  iable to the ser
0b60: 76 65 72 20 77 65 20 61 72 65 20 74 61 6c 6b 69  ver we are talki
0b70: 6e 67 20 74 6f 2e 0a 20 20 20 20 2a 2a 20 54 68  ng to..    ** Th
0b80: 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 70 6f  is is used to po
0b90: 70 75 6c 61 74 65 20 74 68 65 20 69 70 61 64 64  pulate the ipadd
0ba0: 72 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  r column of the 
0bb0: 72 63 76 66 72 6f 6d 20 74 61 62 6c 65 2c 0a 20  rcvfrom table,. 
0bc0: 20 20 20 2a 2a 20 69 66 20 61 6e 79 20 66 69 6c     ** if any fil
0bd0: 65 73 20 61 72 65 20 72 65 63 65 69 76 65 64 20  es are received 
0be0: 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65 72 2e  from the server.
0bf0: 0a 20 20 20 20 2a 2f 0a 20 20 20 20 67 2e 7a 49  .    */.    g.zI
0c00: 70 41 64 64 72 20 3d 20 6d 70 72 69 6e 74 66 28  pAddr = mprintf(
0c10: 22 25 73 22 2c 20 69 6e 65 74 5f 6e 74 6f 61 28  "%s", inet_ntoa(
0c20: 61 64 64 72 2e 73 69 6e 5f 61 64 64 72 29 29 3b  addr.sin_addr));
0c30: 0a 20 20 7d 0a 20 20 73 20 3d 20 73 6f 63 6b 65  .  }.  s = socke
0c40: 74 28 41 46 5f 49 4e 45 54 2c 53 4f 43 4b 5f 53  t(AF_INET,SOCK_S
0c50: 54 52 45 41 4d 2c 30 29 3b 0a 20 20 69 66 28 20  TREAM,0);.  if( 
0c60: 73 3c 30 20 29 7b 0a 20 20 20 20 66 6f 73 73 69  s<0 ){.    fossi
0c70: 6c 5f 70 61 6e 69 63 28 22 63 61 6e 6e 6f 74 20  l_panic("cannot 
0c80: 63 72 65 61 74 65 20 61 20 73 6f 63 6b 65 74 22  create a socket"
0c90: 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6f 6e  );.  }.  if( con
0ca0: 6e 65 63 74 28 73 2c 28 73 74 72 75 63 74 20 73  nect(s,(struct s
0cb0: 6f 63 6b 61 64 64 72 2a 29 26 61 64 64 72 2c 73  ockaddr*)&addr,s
0cc0: 69 7a 65 6f 66 28 61 64 64 72 29 29 3c 30 20 29  izeof(addr))<0 )
0cd0: 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e  {.    fossil_pan
0ce0: 69 63 28 22 63 61 6e 6e 6f 74 20 63 6f 6e 6e 65  ic("cannot conne
0cf0: 63 74 20 74 6f 20 68 6f 73 74 20 25 73 3a 25 64  ct to host %s:%d
0d00: 22 2c 20 67 2e 75 72 6c 4e 61 6d 65 2c 20 67 2e  ", g.urlName, g.
0d10: 75 72 6c 50 6f 72 74 29 3b 0a 20 20 7d 0a 23 69  urlPort);.  }.#i
0d20: 66 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f  fdef __MINGW32__
0d30: 0a 20 20 70 53 6f 63 6b 65 74 20 3d 20 73 3b 0a  .  pSocket = s;.
0d40: 23 65 6c 73 65 0a 20 20 70 53 6f 63 6b 65 74 20  #else.  pSocket 
0d50: 3d 20 66 64 6f 70 65 6e 28 73 2c 22 72 2b 22 29  = fdopen(s,"r+")
0d60: 3b 0a 20 20 73 69 67 6e 61 6c 28 53 49 47 50 49  ;.  signal(SIGPI
0d70: 50 45 2c 20 53 49 47 5f 49 47 4e 29 3b 0a 23 65  PE, SIG_IGN);.#e
0d80: 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 30 3b  ndif.  return 0;
0d90: 0a 7d 0a 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e  .}..#ifdef __MIN
0da0: 47 57 33 32 5f 5f 0a 2f 2a 0a 2a 2a 20 52 65 61  GW32__./*.** Rea
0db0: 64 20 74 68 65 20 73 6f 63 6b 65 74 20 75 6e 74  d the socket unt
0dc0: 69 6c 20 61 20 6e 65 77 6c 69 6e 65 20 27 5c 6e  il a newline '\n
0dd0: 27 20 69 73 20 66 6f 75 6e 64 2e 20 20 52 65 74  ' is found.  Ret
0de0: 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a  urn the number.*
0df0: 2a 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20  * of characters 
0e00: 72 65 61 64 2e 20 70 53 6f 63 6b 49 64 20 63 6f  read. pSockId co
0e10: 6e 74 61 69 6e 73 20 74 68 65 20 73 6f 63 6b 65  ntains the socke
0e20: 74 20 68 61 6e 64 65 6c 2e 20 20 70 4f 75 74 0a  t handel.  pOut.
0e30: 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f  ** contains a po
0e40: 69 6e 74 65 72 20 74 6f 20 74 68 65 20 62 75 66  inter to the buf
0e50: 66 65 72 20 74 6f 20 77 72 69 74 65 20 74 6f 2e  fer to write to.
0e60: 20 20 70 4f 75 74 53 69 7a 65 20 63 6f 6e 74 61    pOutSize conta
0e70: 69 6e 73 0a 2a 2a 20 74 68 65 20 6d 61 78 69 6d  ins.** the maxim
0e80: 75 6d 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c  um size of the l
0e90: 69 6e 65 20 74 68 61 74 20 70 4f 75 74 20 63 61  ine that pOut ca
0ea0: 6e 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61  n handle..*/.sta
0eb0: 74 69 63 20 69 6e 74 20 73 6f 63 6b 65 74 5f 72  tic int socket_r
0ec0: 65 63 76 5f 6c 69 6e 65 28 69 6e 74 20 70 53 6f  ecv_line(int pSo
0ed0: 63 6b 49 64 2c 20 63 68 61 72 2a 20 70 4f 75 74  ckId, char* pOut
0ee0: 2c 20 69 6e 74 20 70 4f 75 74 53 69 7a 65 29 7b  , int pOutSize){
0ef0: 0a 20 20 69 6e 74 20 72 65 63 65 69 76 65 64 3d  .  int received=
0f00: 30 3b 0a 20 20 63 68 61 72 20 6c 65 74 74 65 72  0;.  char letter
0f10: 3b 0a 20 20 6d 65 6d 73 65 74 28 70 4f 75 74 2c  ;.  memset(pOut,
0f20: 30 2c 70 4f 75 74 53 69 7a 65 29 3b 0a 20 20 66  0,pOutSize);.  f
0f30: 6f 72 28 3b 20 72 65 63 65 69 76 65 64 3c 70 4f  or(; received<pO
0f40: 75 74 53 69 7a 65 2d 31 3b 72 65 63 65 69 76 65  utSize-1;receive
0f50: 64 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 72 65  d++){.    if( re
0f60: 63 76 28 70 53 6f 63 6b 49 64 2c 28 63 68 61 72  cv(pSockId,(char
0f70: 2a 29 26 6c 65 74 74 65 72 2c 31 2c 30 29 3e 30  *)&letter,1,0)>0
0f80: 20 29 7b 0a 20 20 20 20 20 20 70 4f 75 74 5b 72   ){.      pOut[r
0f90: 65 63 65 69 76 65 64 5d 3d 6c 65 74 74 65 72 3b  eceived]=letter;
0fa0: 0a 20 20 20 20 20 20 69 66 28 20 6c 65 74 74 65  .      if( lette
0fb0: 72 3d 3d 27 5c 6e 27 20 29 7b 0a 20 20 20 20 20  r=='\n' ){.     
0fc0: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
0fd0: 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
0fe0: 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
0ff0: 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 65 63    }.  return rec
1000: 65 69 76 65 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  eived;.}../*.** 
1010: 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 62 6c 6f  Initialize a blo
1020: 62 20 74 6f 20 74 68 65 20 64 61 74 61 20 6f 6e  b to the data on
1030: 20 61 6e 20 69 6e 70 75 74 20 73 6f 63 6b 65 74   an input socket
1040: 2e 20 20 72 65 74 75 72 6e 0a 2a 2a 20 74 68 65  .  return.** the
1050: 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
1060: 20 72 65 61 64 20 69 6e 74 6f 20 74 68 65 20 62   read into the b
1070: 6c 6f 62 2e 20 20 41 6e 79 20 70 72 69 6f 72 20  lob.  Any prior 
1080: 63 6f 6e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68  content.** of th
1090: 65 20 62 6c 6f 62 20 69 73 20 64 69 73 63 61 72  e blob is discar
10a0: 64 65 64 2c 20 6e 6f 74 20 66 72 65 65 64 2e 0a  ded, not freed..
10b0: 2a 2a 0a 2a 2a 20 54 68 65 20 66 75 6e 63 74 69  **.** The functi
10c0: 6f 6e 20 77 61 73 20 70 6c 61 63 65 64 20 68 65  on was placed he
10d0: 72 65 20 69 6e 20 68 74 74 70 2e 63 20 64 75 65  re in http.c due
10e0: 20 74 6f 20 69 74 27 73 20 73 6f 63 6b 65 74 0a   to it's socket.
10f0: 2a 2a 20 6e 61 74 75 72 65 20 61 6e 64 20 77 65  ** nature and we
1100: 20 64 69 64 20 6e 6f 74 20 77 61 6e 74 20 74 6f   did not want to
1110: 20 69 6e 74 72 6f 64 75 63 65 20 73 6f 63 6b 65   introduce socke
1120: 74 20 68 65 61 64 65 72 73 20 69 6e 74 6f 0a 2a  t headers into.*
1130: 2a 20 74 68 65 20 73 6f 63 6b 65 74 20 6e 65 75  * the socket neu
1140: 74 72 61 6c 20 62 6c 6f 62 2e 63 20 66 69 6c 65  tral blob.c file
1150: 2e 0a 2a 2f 0a 69 6e 74 20 73 6f 63 6b 65 74 5f  ..*/.int socket_
1160: 72 65 61 64 5f 62 6c 6f 62 28 42 6c 6f 62 20 2a  read_blob(Blob *
1170: 70 42 6c 6f 62 2c 20 69 6e 74 20 70 53 6f 63 6b  pBlob, int pSock
1180: 49 64 2c 20 69 6e 74 20 6e 54 6f 52 65 61 64 29  Id, int nToRead)
1190: 7b 0a 20 20 69 6e 74 20 69 3d 30 2c 72 65 61 64  {.  int i=0,read
11a0: 3d 30 3b 0a 20 20 63 68 61 72 20 72 62 75 66 5b  =0;.  char rbuf[
11b0: 35 30 5d 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f  50];.  blob_zero
11c0: 28 70 42 6c 6f 62 29 3b 0a 20 20 77 68 69 6c 65  (pBlob);.  while
11d0: 20 28 20 69 3c 6e 54 6f 52 65 61 64 20 29 7b 0a   ( i<nToRead ){.
11e0: 20 20 20 20 72 65 61 64 20 3d 20 72 65 63 76 28      read = recv(
11f0: 70 53 6f 63 6b 49 64 2c 20 72 62 75 66 2c 20 35  pSockId, rbuf, 5
1200: 30 2c 20 30 29 3b 0a 20 20 20 20 69 20 2b 3d 20  0, 0);.    i += 
1210: 72 65 61 64 3b 0a 20 20 20 20 69 66 28 20 72 65  read;.    if( re
1220: 61 64 3c 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  ad<=0 ){.      r
1230: 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20  eturn 0;.    }. 
1240: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 70     blob_append(p
1250: 42 6c 6f 62 2c 20 72 62 75 66 2c 20 72 65 61 64  Blob, rbuf, read
1260: 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
1270: 62 6c 6f 62 5f 73 69 7a 65 28 70 42 6c 6f 62 29  blob_size(pBlob)
1280: 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
1290: 2a 20 7a 4c 69 6e 65 20 69 73 20 61 20 6c 69 6e  * zLine is a lin
12a0: 65 20 66 72 6f 6d 20 74 68 65 20 68 65 61 64 65  e from the heade
12b0: 72 20 6f 66 20 61 6e 20 48 54 54 50 20 72 65 70  r of an HTTP rep
12c0: 6c 79 2e 20 20 46 69 6e 64 20 74 68 65 0a 2a 2a  ly.  Find the.**
12d0: 20 22 61 72 67 75 6d 65 6e 74 22 20 74 6f 20 74   "argument" to t
12e0: 68 69 73 20 68 65 61 64 65 72 20 6c 69 6e 65 2e  his header line.
12f0: 20 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69    The argument i
1300: 73 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e 2d  s the first non-
1310: 77 68 69 74 65 73 70 61 63 65 0a 2a 2a 20 63 68  whitespace.** ch
1320: 61 72 61 63 74 65 72 20 66 6f 6c 6c 6f 77 69 6e  aracter followin
1330: 67 20 74 68 65 20 66 69 72 73 74 20 22 3a 22 20  g the first ":" 
1340: 69 6e 20 74 68 65 20 6c 69 6e 65 2e 20 20 54 68  in the line.  Th
1350: 65 20 61 72 67 75 6d 65 6e 74 20 63 6f 6e 73 69  e argument consi
1360: 73 74 73 20 6f 66 0a 2a 2a 20 6e 6f 6e 2d 77 68  sts of.** non-wh
1370: 69 74 65 73 70 61 63 65 20 61 6e 64 20 6e 6f 6e  itespace and non
1380: 2d 73 65 6d 69 63 6f 6c 6f 6e 20 63 68 61 72 61  -semicolon chara
1390: 63 74 65 72 73 2e 20 20 5a 65 72 6f 2d 74 65 72  cters.  Zero-ter
13a0: 6d 69 6e 61 74 65 20 74 68 65 20 61 72 67 75 6d  minate the argum
13b0: 65 6e 74 0a 2a 2a 20 62 65 66 6f 72 65 20 72 65  ent.** before re
13c0: 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74  turning..*/.stat
13d0: 69 63 20 63 68 61 72 20 2a 68 65 61 64 65 72 5f  ic char *header_
13e0: 61 72 67 28 63 68 61 72 20 2a 7a 49 6e 29 7b 0a  arg(char *zIn){.
13f0: 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 77 68 69    char *z;.  whi
1400: 6c 65 28 20 2a 7a 49 6e 20 26 26 20 2a 7a 49 6e  le( *zIn && *zIn
1410: 21 3d 27 3a 27 20 29 7b 20 7a 49 6e 2b 2b 3b 20  !=':' ){ zIn++; 
1420: 7d 0a 20 20 69 66 28 20 2a 7a 49 6e 3d 3d 27 3a  }.  if( *zIn==':
1430: 27 20 29 7b 20 7a 49 6e 2b 2b 3b 20 7d 0a 20 20  ' ){ zIn++; }.  
1440: 77 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a  while( isspace(*
1450: 7a 49 6e 29 20 29 7b 20 7a 49 6e 2b 2b 3b 20 7d  zIn) ){ zIn++; }
1460: 0a 20 20 66 6f 72 28 7a 3d 7a 49 6e 3b 20 2a 7a  .  for(z=zIn; *z
1470: 20 26 26 20 2a 7a 21 3d 27 3b 27 20 26 26 20 21   && *z!=';' && !
1480: 69 73 73 70 61 63 65 28 2a 7a 29 3b 20 7a 2b 2b  isspace(*z); z++
1490: 29 7b 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20 20  ){}.  *z = 0;.  
14a0: 72 65 74 75 72 6e 20 7a 49 6e 3b 0a 7d 0a 0a 2f  return zIn;.}../
14b0: 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 69 6e 67  *.** Make a sing
14c0: 6c 65 20 61 74 74 65 6d 70 74 20 74 6f 20 74 61  le attempt to ta
14d0: 6c 6b 20 74 6f 20 74 68 65 20 73 65 72 76 65 72  lk to the server
14e0: 2e 20 20 52 65 74 75 72 6e 20 54 52 55 45 20 6f  .  Return TRUE o
14f0: 6e 20 73 75 63 63 65 73 73 0a 2a 2a 20 61 6e 64  n success.** and
1500: 20 46 41 4c 53 45 20 6f 6e 20 61 20 66 61 69 6c   FALSE on a fail
1510: 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 70 48 65 61 64  ure..**.** pHead
1520: 65 72 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  er contains the 
1530: 48 54 54 50 20 68 65 61 64 65 72 2e 20 20 70 50  HTTP header.  pP
1540: 61 79 6c 6f 61 64 20 63 6f 6e 74 61 69 6e 73 20  ayload contains 
1550: 74 68 65 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 20  the content..** 
1560: 54 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  The content of t
1570: 68 65 20 72 65 70 6c 79 20 69 73 20 77 72 69 74  he reply is writ
1580: 74 65 6e 20 69 6e 74 6f 20 70 52 65 70 6c 79 2e  ten into pReply.
1590: 20 20 70 52 65 70 6c 79 20 69 73 20 61 73 73 75    pReply is assu
15a0: 6d 65 64 0a 2a 2a 20 74 6f 20 62 65 20 75 6e 69  med.** to be uni
15b0: 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72  nitialized prior
15c0: 20 74 6f 20 74 68 69 73 20 63 61 6c 6c 2e 0a 2a   to this call..*
15d0: 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 68 61  *.** pPayload ha
15e0: 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  s already been c
15f0: 6f 6d 70 72 65 73 73 65 64 20 61 6e 64 2f 6f 72  ompressed and/or
1600: 20 65 6e 63 72 79 70 74 65 64 20 70 72 69 6f 72   encrypted prior
1610: 20 74 6f 20 62 65 69 6e 67 0a 2a 2a 20 70 61 73   to being.** pas
1620: 73 65 64 20 69 6e 74 6f 20 74 68 69 73 20 66 75  sed into this fu
1630: 6e 63 74 69 6f 6e 2e 20 20 70 52 65 70 6c 79 20  nction.  pReply 
1640: 77 69 6c 6c 20 62 65 20 64 65 63 72 79 70 74 65  will be decrypte
1650: 64 20 61 6e 64 2f 6f 72 20 64 65 63 6f 6d 70 72  d and/or decompr
1660: 65 73 73 65 64 0a 2a 2a 20 61 73 20 72 65 71 75  essed.** as requ
1670: 69 72 65 64 20 62 79 20 74 68 69 73 20 66 75 6e  ired by this fun
1680: 63 74 69 6f 6e 2c 20 62 61 73 65 64 20 6f 6e 20  ction, based on 
1690: 74 68 65 20 43 6f 6e 74 65 6e 74 2d 54 79 70 65  the Content-Type
16a0: 20 6f 66 20 74 68 65 20 72 65 70 6c 79 2e 0a 2a   of the reply..*
16b0: 2a 0a 2a 2a 20 49 66 20 61 6e 20 65 72 72 6f 72  *.** If an error
16c0: 20 6f 63 63 75 72 73 2c 20 74 68 69 73 20 72 6f   occurs, this ro
16d0: 75 74 69 6e 65 20 72 65 74 75 72 6e 20 66 61 6c  utine return fal
16e0: 73 65 2c 20 72 65 73 65 74 73 20 70 52 65 70 6c  se, resets pRepl
16f0: 79 20 61 6e 64 0a 2a 2a 20 63 6c 6f 73 65 73 20  y and.** closes 
1700: 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 63  the persistent c
1710: 6f 6e 6e 65 63 74 69 6f 6e 2c 20 69 66 20 61 6e  onnection, if an
1720: 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  y..*/.static int
1730: 20 68 74 74 70 5f 73 65 6e 64 5f 72 65 63 76 28   http_send_recv(
1740: 42 6c 6f 62 20 2a 70 48 65 61 64 65 72 2c 20 42  Blob *pHeader, B
1750: 6c 6f 62 20 2a 70 50 61 79 6c 6f 61 64 2c 20 42  lob *pPayload, B
1760: 6c 6f 62 20 2a 70 52 65 70 6c 79 29 7b 0a 20 20  lob *pReply){.  
1770: 69 6e 74 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74  int closeConnect
1780: 69 6f 6e 3d 31 3b 20 20 20 2f 2a 20 64 65 66 61  ion=1;   /* defa
1790: 75 6c 74 20 74 6f 20 63 6c 6f 73 69 6e 67 20 74  ult to closing t
17a0: 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  he connection */
17b0: 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
17c0: 20 69 4c 65 6e 67 74 68 3b 0a 20 20 69 6e 74 20   iLength;.  int 
17d0: 69 48 74 74 70 56 65 72 73 69 6f 6e 3b 0a 20 20  iHttpVersion;.  
17e0: 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65  int i;.  int nRe
17f0: 61 64 3b 0a 20 20 69 6e 74 20 6e 65 65 64 44 65  ad;.  int needDe
1800: 63 72 79 70 74 20 3d 20 30 3b 0a 20 20 69 6e 74  crypt = 0;.  int
1810: 20 6e 65 65 64 44 65 63 6f 6d 70 72 65 73 73 20   needDecompress 
1820: 3d 20 30 3b 0a 20 20 63 68 61 72 20 7a 4c 69 6e  = 0;.  char zLin
1830: 65 5b 32 30 30 30 5d 3b 0a 0a 20 20 69 66 28 20  e[2000];..  if( 
1840: 70 53 6f 63 6b 65 74 3d 3d 30 20 26 26 20 68 74  pSocket==0 && ht
1850: 74 70 5f 6f 70 65 6e 5f 73 6f 63 6b 65 74 28 29  tp_open_socket()
1860: 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
1870: 3b 0a 20 20 7d 0a 20 20 69 4c 65 6e 67 74 68 20  ;.  }.  iLength 
1880: 3d 20 2d 31 3b 0a 23 69 66 64 65 66 20 5f 5f 4d  = -1;.#ifdef __M
1890: 49 4e 47 57 33 32 5f 5f 0a 20 20 2f 2a 0a 20 20  INGW32__.  /*.  
18a0: 2a 2a 20 55 73 65 20 72 65 63 76 2f 73 65 6e 64  ** Use recv/send
18b0: 20 6f 6e 20 74 68 65 20 77 69 6e 64 6f 77 73 20   on the windows 
18c0: 70 6c 61 74 66 6f 72 6d 20 61 73 20 77 69 6e 73  platform as wins
18d0: 6f 63 6b 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c  ock does not all
18e0: 6f 77 0a 20 20 2a 2a 20 73 6f 63 6b 65 74 73 20  ow.  ** sockets 
18f0: 74 6f 20 62 65 20 75 73 65 64 20 61 73 20 46 49  to be used as FI
1900: 4c 45 20 68 61 6e 64 6c 65 73 2c 20 74 68 75 73  LE handles, thus
1910: 20 66 64 6f 70 65 6e 2c 20 66 77 72 69 74 65 2c   fdopen, fwrite,
1920: 20 66 67 65 74 73 0a 20 20 2a 2a 20 64 6f 65 73   fgets.  ** does
1930: 20 6e 6f 74 20 66 75 6e 63 74 69 6f 6e 20 6f 6e   not function on
1940: 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 73 6f 63   windows for soc
1950: 6b 65 74 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20  kets..  */.  rc 
1960: 3d 20 73 65 6e 64 28 70 53 6f 63 6b 65 74 2c 20  = send(pSocket, 
1970: 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 48 65 61  blob_buffer(pHea
1980: 64 65 72 29 2c 20 62 6c 6f 62 5f 73 69 7a 65 28  der), blob_size(
1990: 70 48 65 61 64 65 72 29 2c 20 30 29 3b 0a 20 20  pHeader), 0);.  
19a0: 69 66 28 20 72 63 21 3d 62 6c 6f 62 5f 73 69 7a  if( rc!=blob_siz
19b0: 65 28 70 48 65 61 64 65 72 29 20 29 20 67 6f 74  e(pHeader) ) got
19c0: 6f 20 77 72 69 74 65 5f 65 72 72 3b 0a 20 20 72  o write_err;.  r
19d0: 63 20 3d 20 73 65 6e 64 28 70 53 6f 63 6b 65 74  c = send(pSocket
19e0: 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 50  , blob_buffer(pP
19f0: 61 79 6c 6f 61 64 29 2c 20 62 6c 6f 62 5f 73 69  ayload), blob_si
1a00: 7a 65 28 70 50 61 79 6c 6f 61 64 29 2c 20 30 29  ze(pPayload), 0)
1a10: 3b 0a 20 20 69 66 28 20 72 63 21 3d 62 6c 6f 62  ;.  if( rc!=blob
1a20: 5f 73 69 7a 65 28 70 50 61 79 6c 6f 61 64 29 20  _size(pPayload) 
1a30: 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72  ) goto write_err
1a40: 3b 0a 20 20 0a 20 20 2f 2a 20 52 65 61 64 20 74  ;.  .  /* Read t
1a50: 68 65 20 72 65 73 70 6f 6e 73 65 20 2a 2f 0a 20  he response */. 
1a60: 20 77 68 69 6c 65 28 20 73 6f 63 6b 65 74 5f 72   while( socket_r
1a70: 65 63 76 5f 6c 69 6e 65 28 70 53 6f 63 6b 65 74  ecv_line(pSocket
1a80: 2c 20 7a 4c 69 6e 65 2c 20 32 30 30 30 29 20 29  , zLine, 2000) )
1a90: 7b 0a 20 20 20 20 66 6f 72 28 20 69 3d 30 3b 20  {.    for( i=0; 
1aa0: 7a 4c 69 6e 65 5b 69 5d 20 26 26 20 7a 4c 69 6e  zLine[i] && zLin
1ab0: 65 5b 69 5d 21 3d 27 5c 6e 27 20 26 26 20 7a 4c  e[i]!='\n' && zL
1ac0: 69 6e 65 5b 69 5d 21 3d 27 5c 72 27 3b 20 69 2b  ine[i]!='\r'; i+
1ad0: 2b 20 29 7b 7d 0a 20 20 20 20 69 66 28 20 69 3d  + ){}.    if( i=
1ae0: 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
1af0: 7a 4c 69 6e 65 5b 69 5d 20 3d 20 30 3b 0a 20 20  zLine[i] = 0;.  
1b00: 20 20 69 66 28 20 73 74 72 6e 63 61 73 65 63 6d    if( strncasecm
1b10: 70 28 7a 4c 69 6e 65 2c 20 22 68 74 74 70 2f 31  p(zLine, "http/1
1b20: 2e 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  .", 7)==0 ){.   
1b30: 20 20 20 69 66 28 20 73 73 63 61 6e 66 28 7a 4c     if( sscanf(zL
1b40: 69 6e 65 2c 20 22 48 54 54 50 2f 31 2e 25 64 20  ine, "HTTP/1.%d 
1b50: 25 64 22 2c 20 26 69 48 74 74 70 56 65 72 73 69  %d", &iHttpVersi
1b60: 6f 6e 2c 20 26 72 63 29 21 3d 32 20 29 20 67 6f  on, &rc)!=2 ) go
1b70: 74 6f 20 77 72 69 74 65 5f 65 72 72 3b 0a 20 20  to write_err;.  
1b80: 20 20 20 20 69 66 28 20 72 63 21 3d 32 30 30 20      if( rc!=200 
1b90: 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72  ) goto write_err
1ba0: 3b 0a 20 20 20 20 20 20 69 66 28 20 69 48 74 74  ;.      if( iHtt
1bb0: 70 56 65 72 73 69 6f 6e 3d 3d 30 20 29 7b 0a 20  pVersion==0 ){. 
1bc0: 20 20 20 20 20 20 20 63 6c 6f 73 65 43 6f 6e 6e         closeConn
1bd0: 65 63 74 69 6f 6e 20 3d 20 31 3b 0a 20 20 20 20  ection = 1;.    
1be0: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1bf0: 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e   closeConnection
1c00: 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
1c10: 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 6e    }else if( strn
1c20: 63 61 73 65 63 6d 70 28 7a 4c 69 6e 65 2c 22 63  casecmp(zLine,"c
1c30: 6f 6e 74 65 6e 74 2d 6c 65 6e 67 74 68 3a 22 2c  ontent-length:",
1c40: 31 35 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  15)==0 ){.      
1c50: 69 4c 65 6e 67 74 68 20 3d 20 61 74 6f 69 28 68  iLength = atoi(h
1c60: 65 61 64 65 72 5f 61 72 67 28 7a 4c 69 6e 65 29  eader_arg(zLine)
1c70: 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
1c80: 20 73 74 72 6e 63 61 73 65 63 6d 70 28 7a 4c 69   strncasecmp(zLi
1c90: 6e 65 2c 20 22 63 6f 6e 6e 65 63 74 69 6f 6e 3a  ne, "connection:
1ca0: 22 2c 20 31 31 29 3d 3d 30 20 29 7b 0a 20 20 20  ", 11)==0 ){.   
1cb0: 20 20 20 69 6e 74 20 63 20 3d 20 68 65 61 64 65     int c = heade
1cc0: 72 5f 61 72 67 28 7a 4c 69 6e 65 29 5b 30 5d 3b  r_arg(zLine)[0];
1cd0: 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 63  .      if( c=='c
1ce0: 27 20 7c 7c 20 63 3d 3d 27 43 27 20 29 7b 0a 20  ' || c=='C' ){. 
1cf0: 20 20 20 20 20 20 20 63 6c 6f 73 65 43 6f 6e 6e         closeConn
1d00: 65 63 74 69 6f 6e 20 3d 20 31 3b 20 20 20 2f 2a  ection = 1;   /*
1d10: 20 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f   Connection: clo
1d20: 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73  se */.      }els
1d30: 65 20 69 66 28 20 63 3d 3d 27 6b 27 20 7c 7c 20  e if( c=='k' || 
1d40: 63 3d 3d 27 4b 27 20 29 7b 0a 20 20 20 20 20 20  c=='K' ){.      
1d50: 20 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f    closeConnectio
1d60: 6e 20 3d 20 30 3b 20 20 20 2f 2a 20 43 6f 6e 6e  n = 0;   /* Conn
1d70: 65 63 74 69 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69  ection: keep-ali
1d80: 76 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  ve */.      }.  
1d90: 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 6e    }else if( strn
1da0: 63 61 73 65 63 6d 70 28 7a 4c 69 6e 65 2c 20 22  casecmp(zLine, "
1db0: 63 6f 6e 74 65 6e 74 2d 74 79 70 65 3a 22 2c 20  content-type:", 
1dc0: 31 33 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  13)==0 ){.      
1dd0: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70  const char *zTyp
1de0: 65 20 3d 20 68 65 61 64 65 72 5f 61 72 67 28 7a  e = header_arg(z
1df0: 4c 69 6e 65 29 3b 0a 20 20 20 20 20 20 69 66 28  Line);.      if(
1e00: 20 73 74 72 63 6d 70 28 7a 54 79 70 65 2c 20 22   strcmp(zType, "
1e10: 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66 6f  application/x-fo
1e20: 73 73 69 6c 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ssil")==0 ){.   
1e30: 20 20 20 20 20 6e 65 65 64 44 65 63 6f 6d 70 72       needDecompr
1e40: 65 73 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ess = 1;.       
1e50: 20 6e 65 65 64 44 65 63 72 79 70 74 20 3d 20 30   needDecrypt = 0
1e60: 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
1e70: 28 20 73 74 72 63 6d 70 28 7a 54 79 70 65 2c 20  ( strcmp(zType, 
1e80: 22 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66  "application/x-f
1e90: 6f 73 73 69 6c 2d 73 65 63 75 72 65 22 29 3d 3d  ossil-secure")==
1ea0: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 65 65  0 ){.        nee
1eb0: 64 44 65 63 6f 6d 70 72 65 73 73 20 3d 20 31 3b  dDecompress = 1;
1ec0: 0a 20 20 20 20 20 20 20 20 6e 65 65 64 44 65 63  .        needDec
1ed0: 72 79 70 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  rypt = 1;.      
1ee0: 7d 0a 20 20 20 20 7d 20 20 20 20 20 20 0a 20 20  }.    }      .  
1ef0: 7d 0a 20 20 69 66 28 20 69 4c 65 6e 67 74 68 3c  }.  if( iLength<
1f00: 30 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65  0 ) goto write_e
1f10: 72 72 3b 0a 20 20 6e 52 65 61 64 20 3d 20 73 6f  rr;.  nRead = so
1f20: 63 6b 65 74 5f 72 65 61 64 5f 62 6c 6f 62 28 70  cket_read_blob(p
1f30: 52 65 70 6c 79 2c 20 70 53 6f 63 6b 65 74 2c 20  Reply, pSocket, 
1f40: 69 4c 65 6e 67 74 68 29 3b 0a 23 65 6c 73 65 0a  iLength);.#else.
1f50: 20 20 72 63 20 3d 20 66 77 72 69 74 65 28 62 6c    rc = fwrite(bl
1f60: 6f 62 5f 62 75 66 66 65 72 28 70 48 65 61 64 65  ob_buffer(pHeade
1f70: 72 29 2c 20 31 2c 20 62 6c 6f 62 5f 73 69 7a 65  r), 1, blob_size
1f80: 28 70 48 65 61 64 65 72 29 2c 20 70 53 6f 63 6b  (pHeader), pSock
1f90: 65 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 62  et);.  if( rc!=b
1fa0: 6c 6f 62 5f 73 69 7a 65 28 70 48 65 61 64 65 72  lob_size(pHeader
1fb0: 29 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65  ) ) goto write_e
1fc0: 72 72 3b 0a 20 20 72 63 20 3d 20 66 77 72 69 74  rr;.  rc = fwrit
1fd0: 65 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 50  e(blob_buffer(pP
1fe0: 61 79 6c 6f 61 64 29 2c 20 31 2c 20 62 6c 6f 62  ayload), 1, blob
1ff0: 5f 73 69 7a 65 28 70 50 61 79 6c 6f 61 64 29 2c  _size(pPayload),
2000: 20 70 53 6f 63 6b 65 74 29 3b 0a 20 20 69 66 28   pSocket);.  if(
2010: 20 72 63 21 3d 62 6c 6f 62 5f 73 69 7a 65 28 70   rc!=blob_size(p
2020: 50 61 79 6c 6f 61 64 29 20 29 20 67 6f 74 6f 20  Payload) ) goto 
2030: 77 72 69 74 65 5f 65 72 72 3b 0a 20 20 69 66 28  write_err;.  if(
2040: 20 66 66 6c 75 73 68 28 70 53 6f 63 6b 65 74 29   fflush(pSocket)
2050: 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72   ) goto write_er
2060: 72 3b 0a 20 20 69 66 28 20 66 67 65 74 73 28 7a  r;.  if( fgets(z
2070: 4c 69 6e 65 2c 20 73 69 7a 65 6f 66 28 7a 4c 69  Line, sizeof(zLi
2080: 6e 65 29 2c 20 70 53 6f 63 6b 65 74 29 3d 3d 30  ne), pSocket)==0
2090: 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72   ) goto write_er
20a0: 72 3b 0a 20 20 69 66 28 20 73 73 63 61 6e 66 28  r;.  if( sscanf(
20b0: 7a 4c 69 6e 65 2c 20 22 48 54 54 50 2f 31 2e 25  zLine, "HTTP/1.%
20c0: 64 20 25 64 22 2c 20 26 69 48 74 74 70 56 65 72  d %d", &iHttpVer
20d0: 73 69 6f 6e 2c 20 26 72 63 29 21 3d 32 20 29 20  sion, &rc)!=2 ) 
20e0: 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 3b 0a  goto write_err;.
20f0: 20 20 69 66 28 20 72 63 21 3d 32 30 30 20 29 20    if( rc!=200 ) 
2100: 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 3b 0a  goto write_err;.
2110: 20 20 69 66 28 20 69 48 74 74 70 56 65 72 73 69    if( iHttpVersi
2120: 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c 6f  on==0 ){.    clo
2130: 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 31  seConnection = 1
2140: 3b 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f  ;   /* Connectio
2150: 6e 3a 20 63 6c 6f 73 65 20 2a 2f 0a 20 20 7d 65  n: close */.  }e
2160: 6c 73 65 7b 0a 20 20 20 20 63 6c 6f 73 65 43 6f  lse{.    closeCo
2170: 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 20 20 20  nnection = 0;   
2180: 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b  /* Connection: k
2190: 65 65 70 2d 61 6c 69 76 65 20 2a 2f 0a 20 20 7d  eep-alive */.  }
21a0: 0a 20 20 77 68 69 6c 65 28 20 66 67 65 74 73 28  .  while( fgets(
21b0: 7a 4c 69 6e 65 2c 20 73 69 7a 65 6f 66 28 7a 4c  zLine, sizeof(zL
21c0: 69 6e 65 29 2c 20 70 53 6f 63 6b 65 74 29 20 29  ine), pSocket) )
21d0: 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 7a  {.    for(i=0; z
21e0: 4c 69 6e 65 5b 69 5d 20 26 26 20 7a 4c 69 6e 65  Line[i] && zLine
21f0: 5b 69 5d 21 3d 27 5c 6e 27 20 26 26 20 7a 4c 69  [i]!='\n' && zLi
2200: 6e 65 5b 69 5d 21 3d 27 5c 72 27 3b 20 69 2b 2b  ne[i]!='\r'; i++
2210: 29 7b 7d 0a 20 20 20 20 69 66 28 20 69 3d 3d 30  ){}.    if( i==0
2220: 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7a 4c   ) break;.    zL
2230: 69 6e 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20  ine[i] = 0;.    
2240: 69 66 28 20 73 74 72 6e 63 61 73 65 63 6d 70 28  if( strncasecmp(
2250: 7a 4c 69 6e 65 2c 22 63 6f 6e 74 65 6e 74 2d 6c  zLine,"content-l
2260: 65 6e 67 74 68 3a 22 2c 31 35 29 3d 3d 30 20 29  ength:",15)==0 )
2270: 7b 0a 20 20 20 20 20 20 69 4c 65 6e 67 74 68 20  {.      iLength 
2280: 3d 20 61 74 6f 69 28 68 65 61 64 65 72 5f 61 72  = atoi(header_ar
2290: 67 28 7a 4c 69 6e 65 29 29 3b 0a 20 20 20 20 7d  g(zLine));.    }
22a0: 65 6c 73 65 20 69 66 28 20 73 74 72 6e 63 61 73  else if( strncas
22b0: 65 63 6d 70 28 7a 4c 69 6e 65 2c 20 22 63 6f 6e  ecmp(zLine, "con
22c0: 6e 65 63 74 69 6f 6e 3a 22 2c 20 31 31 29 3d 3d  nection:", 11)==
22d0: 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 63  0 ){.      int c
22e0: 20 3d 20 68 65 61 64 65 72 5f 61 72 67 28 7a 4c   = header_arg(zL
22f0: 69 6e 65 29 5b 30 5d 3b 0a 20 20 20 20 20 20 69  ine)[0];.      i
2300: 66 28 20 63 3d 3d 27 63 27 20 7c 7c 20 63 3d 3d  f( c=='c' || c==
2310: 27 43 27 20 29 7b 0a 20 20 20 20 20 20 20 20 63  'C' ){.        c
2320: 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d  loseConnection =
2330: 20 31 3b 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74   1;   /* Connect
2340: 69 6f 6e 3a 20 63 6c 6f 73 65 20 2a 2f 0a 20 20  ion: close */.  
2350: 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
2360: 3d 27 6b 27 20 7c 7c 20 63 3d 3d 27 4b 27 20 29  ='k' || c=='K' )
2370: 7b 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 43  {.        closeC
2380: 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 20 20  onnection = 0;  
2390: 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20   /* Connection: 
23a0: 6b 65 65 70 2d 61 6c 69 76 65 20 2a 2f 0a 20 20  keep-alive */.  
23b0: 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20      }.    }else 
23c0: 69 66 28 20 73 74 72 6e 63 61 73 65 63 6d 70 28  if( strncasecmp(
23d0: 7a 4c 69 6e 65 2c 20 22 63 6f 6e 74 65 6e 74 2d  zLine, "content-
23e0: 74 79 70 65 3a 22 2c 20 31 33 29 3d 3d 30 20 29  type:", 13)==0 )
23f0: 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
2400: 61 72 20 2a 7a 54 79 70 65 20 3d 20 68 65 61 64  ar *zType = head
2410: 65 72 5f 61 72 67 28 7a 4c 69 6e 65 29 3b 0a 20  er_arg(zLine);. 
2420: 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 28       if( strcmp(
2430: 7a 54 79 70 65 2c 20 22 61 70 70 6c 69 63 61 74  zType, "applicat
2440: 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 22 29 3d 3d  ion/x-fossil")==
2450: 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 65 65  0 ){.        nee
2460: 64 44 65 63 6f 6d 70 72 65 73 73 20 3d 20 31 3b  dDecompress = 1;
2470: 0a 20 20 20 20 20 20 20 20 6e 65 65 64 44 65 63  .        needDec
2480: 72 79 70 74 20 3d 20 30 3b 0a 20 20 20 20 20 20  rypt = 0;.      
2490: 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70  }else if( strcmp
24a0: 28 7a 54 79 70 65 2c 20 22 61 70 70 6c 69 63 61  (zType, "applica
24b0: 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 2d 73 65  tion/x-fossil-se
24c0: 63 75 72 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  cure")==0 ){.   
24d0: 20 20 20 20 20 6e 65 65 64 44 65 63 6f 6d 70 72       needDecompr
24e0: 65 73 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 20  ess = 1;.       
24f0: 20 6e 65 65 64 44 65 63 72 79 70 74 20 3d 20 31   needDecrypt = 1
2500: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20  ;.      }.    } 
2510: 20 20 20 20 20 0a 20 20 7d 0a 20 20 69 66 28 20       .  }.  if( 
2520: 69 4c 65 6e 67 74 68 3c 30 20 29 20 67 6f 74 6f  iLength<0 ) goto
2530: 20 77 72 69 74 65 5f 65 72 72 3b 0a 20 20 6e 52   write_err;.  nR
2540: 65 61 64 20 3d 20 62 6c 6f 62 5f 72 65 61 64 5f  ead = blob_read_
2550: 66 72 6f 6d 5f 63 68 61 6e 6e 65 6c 28 70 52 65  from_channel(pRe
2560: 70 6c 79 2c 20 70 53 6f 63 6b 65 74 2c 20 69 4c  ply, pSocket, iL
2570: 65 6e 67 74 68 29 3b 0a 23 65 6e 64 69 66 0a 20  ength);.#endif. 
2580: 20 69 66 28 20 6e 52 65 61 64 21 3d 69 4c 65 6e   if( nRead!=iLen
2590: 67 74 68 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  gth ){.    blob_
25a0: 72 65 73 65 74 28 70 52 65 70 6c 79 29 3b 0a 20  reset(pReply);. 
25b0: 20 20 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72     goto write_er
25c0: 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6c 6f  r;.  }.  if( clo
25d0: 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 7b 0a  seConnection ){.
25e0: 20 20 20 20 68 74 74 70 5f 63 6c 6f 73 65 28 29      http_close()
25f0: 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64  ;.  }.  if( need
2600: 44 65 63 72 79 70 74 20 29 7b 0a 20 20 20 20 62  Decrypt ){.    b
2610: 6c 6f 62 5f 64 65 63 72 79 70 74 28 70 52 65 70  lob_decrypt(pRep
2620: 6c 79 2c 20 67 2e 75 72 6c 50 61 73 73 77 64 2c  ly, g.urlPasswd,
2630: 20 70 52 65 70 6c 79 29 3b 0a 20 20 7d 0a 20 20   pReply);.  }.  
2640: 69 66 28 20 6e 65 65 64 44 65 63 6f 6d 70 72 65  if( needDecompre
2650: 73 73 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 75  ss ){.    blob_u
2660: 6e 63 6f 6d 70 72 65 73 73 28 70 52 65 70 6c 79  ncompress(pReply
2670: 2c 20 70 52 65 70 6c 79 29 3b 0a 20 20 7d 0a 20  , pReply);.  }. 
2680: 20 72 65 74 75 72 6e 20 31 3b 20 20 0a 0a 77 72   return 1;  ..wr
2690: 69 74 65 5f 65 72 72 3a 0a 20 20 68 74 74 70 5f  ite_err:.  http_
26a0: 63 6c 6f 73 65 28 29 3b 0a 20 20 72 65 74 75 72  close();.  retur
26b0: 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69  n 0;.}../*.** Si
26c0: 67 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69  gn the content i
26d0: 6e 20 70 53 65 6e 64 2c 20 63 6f 6d 70 72 65 73  n pSend, compres
26e0: 73 20 69 74 20 28 69 66 20 63 6f 6d 70 72 65 73  s it (if compres
26f0: 73 69 6f 6e 20 69 73 20 74 75 72 6e 65 64 20 6f  sion is turned o
2700: 6e 29 2c 0a 2a 2a 20 65 6e 63 72 79 70 74 20 69  n),.** encrypt i
2710: 74 20 28 69 66 20 73 65 63 75 72 69 74 79 20 69  t (if security i
2720: 73 20 74 75 72 6e 65 64 20 6f 6e 29 2c 20 61 6e  s turned on), an
2730: 64 20 73 65 6e 64 20 69 74 20 74 6f 20 74 68 65  d send it to the
2740: 20 73 65 72 76 65 72 0a 2a 2a 20 76 69 61 20 48   server.** via H
2750: 54 54 50 2e 20 20 47 65 74 20 61 20 72 65 70 6c  TTP.  Get a repl
2760: 79 2c 20 64 65 63 72 79 70 74 20 61 6e 64 20 75  y, decrypt and u
2770: 6e 63 6f 6d 70 72 65 73 73 20 74 68 65 20 72 65  ncompress the re
2780: 70 6c 79 2c 20 61 6e 64 20 73 74 6f 72 65 20 0a  ply, and store .
2790: 2a 2a 20 74 68 65 20 72 65 70 6c 79 20 69 6e 20  ** the reply in 
27a0: 70 52 65 63 76 2e 20 20 70 52 65 63 76 20 69 73  pRecv.  pRecv is
27b0: 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 20 75   assumed to be u
27c0: 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65  ninitialized whe
27d0: 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  n.** this routin
27e0: 65 20 69 73 20 63 61 6c 6c 65 64 20 2d 20 74 68  e is called - th
27f0: 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20  is routine will 
2800: 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e 0a 2a  initialize it..*
2810: 2a 0a 2a 2a 20 54 68 65 20 73 65 72 76 65 72 20  *.** The server 
2820: 61 64 64 72 65 73 73 20 69 73 20 63 6f 6e 74 61  address is conta
2830: 69 6e 20 69 6e 20 74 68 65 20 22 67 22 20 67 6c  in in the "g" gl
2840: 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 2e 20  obal structure. 
2850: 20 54 68 65 0a 2a 2a 20 75 72 6c 5f 70 61 72 73   The.** url_pars
2860: 65 28 29 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  e() routine shou
2870: 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c  ld have been cal
2880: 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69  led prior to thi
2890: 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 6e 20  s routine.** in 
28a0: 6f 72 64 65 72 20 74 6f 20 66 69 6c 6c 20 74 68  order to fill th
28b0: 69 73 20 73 74 72 75 63 74 75 72 65 20 61 70 70  is structure app
28c0: 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 76  ropriately..*/.v
28d0: 6f 69 64 20 68 74 74 70 5f 65 78 63 68 61 6e 67  oid http_exchang
28e0: 65 28 42 6c 6f 62 20 2a 70 53 65 6e 64 2c 20 42  e(Blob *pSend, B
28f0: 6c 6f 62 20 2a 70 52 65 63 76 29 7b 0a 20 20 42  lob *pRecv){.  B
2900: 6c 6f 62 20 6c 6f 67 69 6e 3b 20 20 20 20 20 20  lob login;      
2910: 2f 2a 20 54 68 65 20 22 6c 6f 67 69 6e 22 20 63  /* The "login" c
2920: 61 72 64 20 61 74 20 74 68 65 20 62 65 67 69 6e  ard at the begin
2930: 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 79 6c  ning of the payl
2940: 6f 61 64 20 2a 2f 0a 20 20 42 6c 6f 62 20 6e 6f  oad */.  Blob no
2950: 6e 63 65 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  nce;      /* The
2960: 20 70 61 73 73 77 6f 72 64 20 76 65 72 69 66 69   password verifi
2970: 63 61 74 69 6e 20 6e 6f 6e 63 65 20 6f 6e 20 74  catin nonce on t
2980: 68 65 20 6c 6f 67 69 6e 20 63 61 72 64 20 2a 2f  he login card */
2990: 0a 20 20 42 6c 6f 62 20 73 69 67 3b 20 20 20 20  .  Blob sig;    
29a0: 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e 61      /* The signa
29b0: 74 75 72 65 20 6f 6e 20 74 68 65 20 6c 6f 67 69  ture on the logi
29c0: 6e 20 63 61 72 64 20 2a 2f 0a 20 20 42 6c 6f 62  n card */.  Blob
29d0: 20 70 77 3b 20 20 20 20 20 20 20 20 20 2f 2a 20   pw;         /* 
29e0: 54 68 65 20 75 73 65 72 20 70 61 73 73 77 6f 72  The user passwor
29f0: 64 20 70 72 65 66 69 78 65 64 20 62 79 20 74 68  d prefixed by th
2a00: 65 20 6e 6f 6e 63 65 20 2a 2f 0a 20 20 42 6c 6f  e nonce */.  Blo
2a10: 62 20 70 61 79 6c 6f 61 64 3b 20 20 20 20 2f 2a  b payload;    /*
2a20: 20 54 68 65 20 48 54 54 50 20 72 65 71 75 65 73   The HTTP reques
2a30: 74 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20 42  t payload */.  B
2a40: 6c 6f 62 20 68 64 72 3b 20 20 20 20 20 20 20 20  lob hdr;        
2a50: 2f 2a 20 54 68 65 20 48 54 54 50 20 72 65 71 75  /* The HTTP requ
2a60: 65 73 74 20 68 65 61 64 65 72 20 2a 2f 0a 20 20  est header */.  
2a70: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70  const char *zSep
2a80: 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74  ;.  int i;.  int
2a90: 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20   cnt = 0;..  /* 
2aa0: 49 66 20 61 20 75 73 65 72 69 64 20 69 73 20 73  If a userid is s
2ab0: 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20  pecified in the 
2ac0: 55 52 4c 2c 20 74 68 65 6e 20 63 6f 6d 70 75 74  URL, then comput
2ad0: 65 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  e the correspond
2ae0: 69 6e 67 0a 20 20 2a 2a 20 6c 6f 67 69 6e 20 63  ing.  ** login c
2af0: 61 72 64 2e 20 20 54 68 69 73 20 63 61 72 64 20  ard.  This card 
2b00: 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 3a 0a  is of the form:.
2b10: 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 6c    **.  **      l
2b20: 6f 67 69 6e 20 55 53 45 52 49 44 20 4e 4f 4e 43  ogin USERID NONC
2b30: 45 20 53 49 47 4e 41 54 55 52 45 0a 20 20 2a 2a  E SIGNATURE.  **
2b40: 0a 20 20 2a 2a 20 54 68 65 20 4e 4f 4e 43 45 20  .  ** The NONCE 
2b50: 69 73 20 61 20 75 6e 69 71 75 65 20 73 74 72 69  is a unique stri
2b60: 6e 67 20 2d 20 6e 65 76 65 72 20 74 6f 20 62 65  ng - never to be
2b70: 20 72 65 75 73 65 64 2e 20 20 49 6e 20 74 68 69   reused.  In thi
2b80: 73 20 63 61 73 65 2c 0a 20 20 2a 2a 20 74 68 65  s case,.  ** the
2b90: 20 6e 6f 6e 63 65 20 69 73 20 74 68 65 20 53 48   nonce is the SH
2ba0: 41 31 20 68 61 73 68 20 6f 66 20 74 68 65 20 72  A1 hash of the r
2bb0: 65 73 74 20 6f 66 20 74 68 65 20 70 61 79 6c 6f  est of the paylo
2bc0: 61 64 2e 20 20 54 68 65 20 53 49 47 4e 41 54 55  ad.  The SIGNATU
2bd0: 52 45 0a 20 20 2a 2a 20 69 73 20 74 68 65 20 53  RE.  ** is the S
2be0: 48 41 31 20 68 61 73 68 20 6f 66 20 74 68 65 20  HA1 hash of the 
2bf0: 4e 4f 4e 43 45 20 61 6e 64 20 74 68 65 20 75 73  NONCE and the us
2c00: 65 72 20 70 61 73 73 77 6f 72 64 20 63 6f 6e 63  er password conc
2c10: 61 74 65 6e 61 74 65 64 2e 0a 20 20 2a 2f 0a 20  atenated..  */. 
2c20: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 6c 6f 67 69   blob_zero(&logi
2c30: 6e 29 3b 0a 20 20 69 66 28 20 67 2e 75 72 6c 55  n);.  if( g.urlU
2c40: 73 65 72 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f  ser ){.    blob_
2c50: 7a 65 72 6f 28 26 6e 6f 6e 63 65 29 3b 0a 20 20  zero(&nonce);.  
2c60: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 77 29    blob_zero(&pw)
2c70: 3b 0a 20 20 20 20 73 68 61 31 73 75 6d 5f 62 6c  ;.    sha1sum_bl
2c80: 6f 62 28 70 53 65 6e 64 2c 20 26 6e 6f 6e 63 65  ob(pSend, &nonce
2c90: 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 70 79  );.    blob_copy
2ca0: 28 26 70 77 2c 20 26 6e 6f 6e 63 65 29 3b 0a 20  (&pw, &nonce);. 
2cb0: 20 20 20 69 66 28 20 67 2e 75 72 6c 50 61 73 73     if( g.urlPass
2cc0: 77 64 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69  wd==0 ){.      i
2cd0: 66 28 20 73 74 72 63 6d 70 28 67 2e 75 72 6c 55  f( strcmp(g.urlU
2ce0: 73 65 72 2c 22 61 6e 6f 6e 79 6d 6f 75 73 22 29  ser,"anonymous")
2cf0: 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63  !=0 ){.        c
2d00: 68 61 72 20 2a 7a 50 72 6f 6d 70 74 20 3d 20 6d  har *zPrompt = m
2d10: 70 72 69 6e 74 66 28 22 70 61 73 73 77 6f 72 64  printf("password
2d20: 20 66 6f 72 20 25 73 3a 20 22 2c 20 67 2e 75 72   for %s: ", g.ur
2d30: 6c 55 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20  lUser);.        
2d40: 42 6c 6f 62 20 78 3b 0a 20 20 20 20 20 20 20 20  Blob x;.        
2d50: 70 72 6f 6d 70 74 5f 66 6f 72 5f 70 61 73 73 77  prompt_for_passw
2d60: 6f 72 64 28 7a 50 72 6f 6d 70 74 2c 20 26 78 2c  ord(zPrompt, &x,
2d70: 20 30 29 3b 0a 20 20 20 20 20 20 20 20 66 72 65   0);.        fre
2d80: 65 28 7a 50 72 6f 6d 70 74 29 3b 0a 20 20 20 20  e(zPrompt);.    
2d90: 20 20 20 20 67 2e 75 72 6c 50 61 73 73 77 64 20      g.urlPasswd 
2da0: 3d 20 62 6c 6f 62 5f 73 74 72 28 26 78 29 3b 0a  = blob_str(&x);.
2db0: 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2dc0: 20 20 20 20 20 67 2e 75 72 6c 50 61 73 73 77 64       g.urlPasswd
2dd0: 20 3d 20 22 22 3b 0a 20 20 20 20 20 20 7d 0a 20   = "";.      }. 
2de0: 20 20 20 7d 0a 20 20 20 20 62 6c 6f 62 5f 61 70     }.    blob_ap
2df0: 70 65 6e 64 28 26 70 77 2c 20 67 2e 75 72 6c 50  pend(&pw, g.urlP
2e00: 61 73 73 77 64 2c 20 2d 31 29 3b 0a 20 20 20 20  asswd, -1);.    
2e10: 2f 2a 20 70 72 69 6e 74 66 28 22 70 72 65 73 69  /* printf("presi
2e20: 67 3d 5b 25 73 5d 5c 6e 22 2c 20 62 6c 6f 62 5f  g=[%s]\n", blob_
2e30: 73 74 72 28 26 70 77 29 29 3b 20 2a 2f 0a 20 20  str(&pw)); */.  
2e40: 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 26    sha1sum_blob(&
2e50: 70 77 2c 20 26 73 69 67 29 3b 0a 20 20 20 20 62  pw, &sig);.    b
2e60: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6c 6f 67  lob_appendf(&log
2e70: 69 6e 2c 20 22 6c 6f 67 69 6e 20 25 73 20 25 62  in, "login %s %b
2e80: 20 25 62 5c 6e 22 2c 20 67 2e 75 72 6c 55 73 65   %b\n", g.urlUse
2e90: 72 2c 20 26 6e 6f 6e 63 65 2c 20 26 73 69 67 29  r, &nonce, &sig)
2ea0: 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74  ;.    blob_reset
2eb0: 28 26 6e 6f 6e 63 65 29 3b 0a 20 20 20 20 62 6c  (&nonce);.    bl
2ec0: 6f 62 5f 72 65 73 65 74 28 26 70 77 29 3b 0a 20  ob_reset(&pw);. 
2ed0: 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73     blob_reset(&s
2ee0: 69 67 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ig);.  }else{.  
2ef0: 20 20 67 2e 75 72 6c 50 61 73 73 77 64 20 3d 20    g.urlPasswd = 
2f00: 22 22 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f  "";.  }..  /* Co
2f10: 6e 73 74 72 75 63 74 20 74 68 65 20 70 61 79 6c  nstruct the payl
2f20: 6f 61 64 2c 20 77 68 69 63 68 20 69 6e 63 6c 75  oad, which inclu
2f30: 64 65 73 20 74 68 65 20 6c 6f 67 69 6e 20 63 61  des the login ca
2f40: 72 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67  rd..  */.  if( g
2f50: 2e 66 48 74 74 70 53 65 63 75 72 65 20 26 26 20  .fHttpSecure && 
2f60: 67 2e 75 72 6c 50 61 73 73 77 64 5b 30 5d 20 29  g.urlPasswd[0] )
2f70: 7b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72  {.    blob_compr
2f80: 65 73 73 28 70 53 65 6e 64 2c 20 70 53 65 6e 64  ess(pSend, pSend
2f90: 29 3b 0a 20 20 20 20 70 61 79 6c 6f 61 64 20 3d  );.    payload =
2fa0: 20 6c 6f 67 69 6e 3b 0a 20 20 20 20 62 6c 6f 62   login;.    blob
2fb0: 5f 65 6e 63 72 79 70 74 28 70 53 65 6e 64 2c 20  _encrypt(pSend, 
2fc0: 67 2e 75 72 6c 50 61 73 73 77 64 2c 20 26 70 61  g.urlPasswd, &pa
2fd0: 79 6c 6f 61 64 29 3b 0a 20 20 7d 65 6c 73 65 20  yload);.  }else 
2fe0: 69 66 28 20 67 2e 66 48 74 74 70 54 72 61 63 65  if( g.fHttpTrace
2ff0: 20 29 7b 0a 20 20 20 20 70 61 79 6c 6f 61 64 20   ){.    payload 
3000: 3d 20 6c 6f 67 69 6e 3b 0a 20 20 20 20 62 6c 6f  = login;.    blo
3010: 62 5f 61 70 70 65 6e 64 28 26 70 61 79 6c 6f 61  b_append(&payloa
3020: 64 2c 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 70  d, blob_buffer(p
3030: 53 65 6e 64 29 2c 20 62 6c 6f 62 5f 73 69 7a 65  Send), blob_size
3040: 28 70 53 65 6e 64 29 29 3b 0a 20 20 7d 65 6c 73  (pSend));.  }els
3050: 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70  e{.    blob_comp
3060: 72 65 73 73 32 28 26 6c 6f 67 69 6e 2c 20 70 53  ress2(&login, pS
3070: 65 6e 64 2c 20 26 70 61 79 6c 6f 61 64 29 3b 0a  end, &payload);.
3080: 20 20 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26      blob_reset(&
3090: 6c 6f 67 69 6e 29 3b 0a 20 20 7d 0a 0a 20 20 2f  login);.  }..  /
30a0: 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * Construct the 
30b0: 48 54 54 50 20 72 65 71 75 65 73 74 20 68 65 61  HTTP request hea
30c0: 64 65 72 0a 20 20 2a 2f 0a 20 20 62 6c 6f 62 5f  der.  */.  blob_
30d0: 7a 65 72 6f 28 26 68 64 72 29 3b 0a 20 20 69 20  zero(&hdr);.  i 
30e0: 3d 20 73 74 72 6c 65 6e 28 67 2e 75 72 6c 50 61  = strlen(g.urlPa
30f0: 74 68 29 3b 0a 20 20 69 66 28 20 69 3e 30 20 26  th);.  if( i>0 &
3100: 26 20 67 2e 75 72 6c 50 61 74 68 5b 69 2d 31 5d  & g.urlPath[i-1]
3110: 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 7a 53 65  =='/' ){.    zSe
3120: 70 20 3d 20 22 22 3b 0a 20 20 7d 65 6c 73 65 7b  p = "";.  }else{
3130: 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 2f 22 3b  .    zSep = "/";
3140: 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 61 70 70 65  .  }.  blob_appe
3150: 6e 64 66 28 26 68 64 72 2c 20 22 50 4f 53 54 20  ndf(&hdr, "POST 
3160: 25 73 25 73 78 66 65 72 20 48 54 54 50 2f 31 2e  %s%sxfer HTTP/1.
3170: 31 5c 72 5c 6e 22 2c 20 67 2e 75 72 6c 50 61 74  1\r\n", g.urlPat
3180: 68 2c 20 7a 53 65 70 29 3b 0a 20 20 62 6c 6f 62  h, zSep);.  blob
3190: 5f 61 70 70 65 6e 64 66 28 26 68 64 72 2c 20 22  _appendf(&hdr, "
31a0: 48 6f 73 74 3a 20 25 73 5c 72 5c 6e 22 2c 20 67  Host: %s\r\n", g
31b0: 2e 75 72 6c 48 6f 73 74 6e 61 6d 65 29 3b 0a 20  .urlHostname);. 
31c0: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 68   blob_appendf(&h
31d0: 64 72 2c 20 22 55 73 65 72 2d 41 67 65 6e 74 3a  dr, "User-Agent:
31e0: 20 46 6f 73 73 69 6c 2f 22 20 4d 41 4e 49 46 45   Fossil/" MANIFE
31f0: 53 54 5f 56 45 52 53 49 4f 4e 20 22 5c 72 5c 6e  ST_VERSION "\r\n
3200: 22 29 3b 0a 20 20 69 66 28 20 67 2e 66 48 74 74  ");.  if( g.fHtt
3210: 70 53 65 63 75 72 65 20 26 26 20 67 2e 75 72 6c  pSecure && g.url
3220: 50 61 73 73 77 64 5b 30 5d 20 29 7b 0a 20 20 20  Passwd[0] ){.   
3230: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 68   blob_appendf(&h
3240: 64 72 2c 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70  dr, "Content-Typ
3250: 65 3a 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78  e: application/x
3260: 2d 66 6f 73 73 69 6c 2d 73 65 63 75 72 65 5c 72  -fossil-secure\r
3270: 5c 6e 22 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  \n");.  }else if
3280: 28 20 67 2e 66 48 74 74 70 54 72 61 63 65 20 29  ( g.fHttpTrace )
3290: 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e  {.    blob_appen
32a0: 64 66 28 26 68 64 72 2c 20 22 43 6f 6e 74 65 6e  df(&hdr, "Conten
32b0: 74 2d 54 79 70 65 3a 20 61 70 70 6c 69 63 61 74  t-Type: applicat
32c0: 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 2d 64 65 62  ion/x-fossil-deb
32d0: 75 67 5c 72 5c 6e 22 29 3b 0a 20 20 7d 65 6c 73  ug\r\n");.  }els
32e0: 65 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65  e{.    blob_appe
32f0: 6e 64 66 28 26 68 64 72 2c 20 22 43 6f 6e 74 65  ndf(&hdr, "Conte
3300: 6e 74 2d 54 79 70 65 3a 20 61 70 70 6c 69 63 61  nt-Type: applica
3310: 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c 5c 72 5c  tion/x-fossil\r\
3320: 6e 22 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f  n");.  }.  blob_
3330: 61 70 70 65 6e 64 66 28 26 68 64 72 2c 20 22 43  appendf(&hdr, "C
3340: 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25  ontent-Length: %
3350: 64 5c 72 5c 6e 5c 72 5c 6e 22 2c 20 62 6c 6f 62  d\r\n\r\n", blob
3360: 5f 73 69 7a 65 28 26 70 61 79 6c 6f 61 64 29 29  _size(&payload))
3370: 3b 0a 0a 20 20 69 66 28 20 67 2e 66 48 74 74 70  ;..  if( g.fHttp
3380: 54 72 61 63 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Trace ){.    /* 
3390: 57 68 65 6e 20 74 72 61 63 69 6e 67 2c 20 77 72  When tracing, wr
33a0: 69 74 65 20 74 68 65 20 74 72 61 6e 73 6d 69 74  ite the transmit
33b0: 74 65 64 20 48 54 54 50 20 6d 65 73 73 61 67 65  ted HTTP message
33c0: 20 62 6f 74 68 20 74 6f 20 73 74 61 6e 64 61 72   both to standar
33d0: 64 0a 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20  d.    ** output 
33e0: 61 6e 64 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e  and into a file.
33f0: 20 20 54 68 65 20 66 69 6c 65 20 63 61 6e 20 74    The file can t
3400: 68 65 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64  hen be used to d
3410: 72 69 76 65 20 74 68 65 0a 20 20 20 20 2a 2a 20  rive the.    ** 
3420: 73 65 72 76 65 72 2d 73 69 64 65 20 6c 69 6b 65  server-side like
3430: 20 74 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20   this:.    **.  
3440: 20 20 2a 2a 20 20 20 20 20 20 2e 2f 66 6f 73 73    **      ./foss
3450: 69 6c 20 68 74 74 70 20 3c 68 74 74 70 2d 74 72  il http <http-tr
3460: 61 63 65 2d 31 2e 74 78 74 0a 20 20 20 20 2a 2f  ace-1.txt.    */
3470: 0a 20 20 20 20 73 74 61 74 69 63 20 69 6e 74 20  .    static int 
3480: 74 72 61 63 65 43 6e 74 20 3d 20 30 3b 0a 20 20  traceCnt = 0;.  
3490: 20 20 63 68 61 72 20 2a 7a 4f 75 74 46 69 6c 65    char *zOutFile
34a0: 3b 0a 20 20 20 20 46 49 4c 45 20 2a 6f 75 74 3b  ;.    FILE *out;
34b0: 0a 20 20 20 20 74 72 61 63 65 43 6e 74 2b 2b 3b  .    traceCnt++;
34c0: 0a 20 20 20 20 7a 4f 75 74 46 69 6c 65 20 3d 20  .    zOutFile = 
34d0: 6d 70 72 69 6e 74 66 28 22 68 74 74 70 2d 74 72  mprintf("http-tr
34e0: 61 63 65 2d 25 64 2e 74 78 74 22 2c 20 74 72 61  ace-%d.txt", tra
34f0: 63 65 43 6e 74 29 3b 0a 20 20 20 20 70 72 69 6e  ceCnt);.    prin
3500: 74 66 28 22 48 54 54 50 20 53 45 4e 44 3a 20 28  tf("HTTP SEND: (
3510: 25 73 29 5c 6e 25 73 25 73 3d 3d 3d 3d 3d 3d 3d  %s)\n%s%s=======
3520: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
3530: 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 7a 4f  \n", .        zO
3540: 75 74 46 69 6c 65 2c 20 62 6c 6f 62 5f 73 74 72  utFile, blob_str
3550: 28 26 68 64 72 29 2c 20 62 6c 6f 62 5f 73 74 72  (&hdr), blob_str
3560: 28 26 70 61 79 6c 6f 61 64 29 29 3b 0a 20 20 20  (&payload));.   
3570: 20 6f 75 74 20 3d 20 66 6f 70 65 6e 28 7a 4f 75   out = fopen(zOu
3580: 74 46 69 6c 65 2c 20 22 77 22 29 3b 0a 20 20 20  tFile, "w");.   
3590: 20 69 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20   if( out ){.    
35a0: 20 20 66 77 72 69 74 65 28 62 6c 6f 62 5f 62 75    fwrite(blob_bu
35b0: 66 66 65 72 28 26 68 64 72 29 2c 20 31 2c 20 62  ffer(&hdr), 1, b
35c0: 6c 6f 62 5f 73 69 7a 65 28 26 68 64 72 29 2c 20  lob_size(&hdr), 
35d0: 6f 75 74 29 3b 0a 20 20 20 20 20 20 66 77 72 69  out);.      fwri
35e0: 74 65 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 26  te(blob_buffer(&
35f0: 70 61 79 6c 6f 61 64 29 2c 20 31 2c 20 62 6c 6f  payload), 1, blo
3600: 62 5f 73 69 7a 65 28 26 70 61 79 6c 6f 61 64 29  b_size(&payload)
3610: 2c 20 6f 75 74 29 3b 0a 20 20 20 20 20 20 66 63  , out);.      fc
3620: 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d  lose(out);.    }
3630: 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 6e 64 20  .  }..  /* Send 
3640: 74 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 70  the header and p
3650: 61 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 73 65  ayload to the se
3660: 72 76 65 72 2e 20 20 47 65 74 20 74 68 65 20 72  rver.  Get the r
3670: 65 70 6c 79 2e 20 20 49 66 0a 20 20 2a 2a 20 74  eply.  If.  ** t
3680: 68 65 20 66 69 72 73 74 20 61 74 74 65 6d 70 74  he first attempt
3690: 20 69 73 20 75 6e 73 75 63 63 65 73 73 66 75 6c   is unsuccessful
36a0: 2c 20 64 6f 20 61 20 73 65 63 6f 6e 64 20 61 74  , do a second at
36b0: 74 65 6d 70 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f  tempt..  */.  fo
36c0: 72 28 63 6e 74 3d 30 3b 20 63 6e 74 3c 32 3b 20  r(cnt=0; cnt<2; 
36d0: 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  cnt++){.    if( 
36e0: 68 74 74 70 5f 73 65 6e 64 5f 72 65 63 76 28 26  http_send_recv(&
36f0: 68 64 72 2c 20 26 70 61 79 6c 6f 61 64 2c 20 70  hdr, &payload, p
3700: 52 65 63 76 29 20 29 20 62 72 65 61 6b 3b 0a 20  Recv) ) break;. 
3710: 20 7d 0a 20 20 69 66 28 20 63 6e 74 3e 3d 32 20   }.  if( cnt>=2 
3720: 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 66 61  ){.    fossil_fa
3730: 74 61 6c 28 22 63 6f 6e 6e 65 63 74 69 6f 6e 20  tal("connection 
3740: 74 6f 20 73 65 72 76 65 72 20 66 61 69 6c 65 64  to server failed
3750: 22 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72  ");.  }.  blob_r
3760: 65 73 65 74 28 26 68 64 72 29 3b 0a 20 20 62 6c  eset(&hdr);.  bl
3770: 6f 62 5f 72 65 73 65 74 28 26 70 61 79 6c 6f 61  ob_reset(&payloa
3780: 64 29 3b 0a 0a 20 20 2f 2a 20 50 72 6f 63 65 73  d);..  /* Proces
3790: 73 20 74 68 65 20 72 65 70 6c 79 2e 20 20 70 52  s the reply.  pR
37a0: 65 63 76 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c  ecv contains onl
37b0: 79 20 74 68 65 20 70 61 79 6c 6f 61 64 20 6f 66  y the payload of
37c0: 20 74 68 65 0a 20 20 2a 2a 20 72 65 70 6c 79 20   the.  ** reply 
37d0: 6d 65 73 73 61 67 65 2c 20 6e 6f 74 20 74 68 65  message, not the
37e0: 20 68 65 61 64 65 72 2e 20 20 70 52 65 63 76 20   header.  pRecv 
37f0: 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
3800: 20 64 65 63 72 79 70 74 65 64 0a 20 20 2a 2a 20   decrypted.  ** 
3810: 61 6e 64 20 64 65 63 6f 6d 70 72 65 73 73 65 64  and decompressed
3820: 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 2e 66 48  .  */.  if( g.fH
3830: 74 74 70 54 72 61 63 65 20 29 7b 0a 20 20 20 20  ttpTrace ){.    
3840: 70 72 69 6e 74 66 28 22 48 54 54 50 20 52 45 43  printf("HTTP REC
3850: 45 49 56 45 3a 5c 6e 25 73 5c 6e 3d 3d 3d 3d 3d  EIVE:\n%s\n=====
3860: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d  ================
3870: 3d 3d 5c 6e 22 2c 20 62 6c 6f 62 5f 73 74 72 28  ==\n", blob_str(
3880: 70 52 65 63 76 29 29 3b 0a 20 20 7d 0a 7d 0a 0a  pRecv));.  }.}..
3890: 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65  ./*.** Make sure
38a0: 20 74 68 65 20 73 6f 63 6b 65 74 20 74 6f 20 74   the socket to t
38b0: 68 65 20 48 54 54 50 20 73 65 72 76 65 72 20 69  he HTTP server i
38c0: 73 20 63 6c 6f 73 65 64 20 0a 2a 2f 0a 76 6f 69  s closed .*/.voi
38d0: 64 20 68 74 74 70 5f 63 6c 6f 73 65 28 76 6f 69  d http_close(voi
38e0: 64 29 7b 0a 20 20 69 66 28 20 70 53 6f 63 6b 65  d){.  if( pSocke
38f0: 74 20 29 7b 0a 23 69 66 64 65 66 20 5f 5f 4d 49  t ){.#ifdef __MI
3900: 4e 47 57 33 32 5f 5f 0a 20 20 20 20 63 6c 6f 73  NGW32__.    clos
3910: 65 73 6f 63 6b 65 74 28 70 53 6f 63 6b 65 74 29  esocket(pSocket)
3920: 3b 0a 23 65 6c 73 65 0a 20 20 20 20 66 63 6c 6f  ;.#else.    fclo
3930: 73 65 28 70 53 6f 63 6b 65 74 29 3b 0a 23 65 6e  se(pSocket);.#en
3940: 64 69 66 0a 20 20 20 20 70 53 6f 63 6b 65 74 20  dif.    pSocket 
3950: 3d 20 30 3b 0a 20 20 7d 0a 20 20 2f 2a 0a 20 20  = 0;.  }.  /*.  
3960: 2a 2a 20 54 68 69 73 20 69 73 20 63 6f 75 6e 74  ** This is count
3970: 65 72 20 70 72 6f 64 75 63 74 69 76 65 2e 20 45  er productive. E
3980: 61 63 68 20 74 69 6d 65 20 77 65 20 6f 70 65 6e  ach time we open
3990: 20 61 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 77 65   a connection we
39a0: 20 69 6e 69 74 69 61 6c 69 7a 65 0a 20 20 2a 2a   initialize.  **
39b0: 20 77 69 6e 73 6f 63 6b 20 61 6e 64 20 74 68 65   winsock and the
39c0: 6e 20 77 68 65 6e 20 63 6c 6f 73 69 6e 67 20 77  n when closing w
39d0: 65 20 63 6c 65 61 6e 75 70 2e 20 49 74 20 77 6f  e cleanup. It wo
39e0: 75 6c 64 20 62 65 20 62 65 74 74 65 72 20 74 6f  uld be better to
39f0: 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65  .  ** initialize
3a00: 20 77 69 6e 73 6f 63 6b 20 6f 6e 63 65 20 61 74   winsock once at
3a10: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 74 61   application sta
3a20: 72 74 20 77 68 65 6e 20 77 65 20 6b 6e 6f 77 20  rt when we know 
3a30: 77 65 20 61 72 65 20 67 6f 69 6e 67 20 74 6f 0a  we are going to.
3a40: 20 20 2a 2a 20 75 73 65 20 74 68 65 20 73 6f 63    ** use the soc
3a50: 6b 65 74 20 69 6e 74 65 72 66 61 63 65 20 61 6e  ket interface an
3a60: 64 20 74 68 65 6e 20 63 6c 65 61 6e 75 70 20 6f  d then cleanup o
3a70: 6e 63 65 20 61 74 20 61 70 70 6c 69 63 61 74 69  nce at applicati
3a80: 6f 6e 20 65 78 69 74 20 77 68 65 6e 0a 20 20 2a  on exit when.  *
3a90: 2a 20 77 65 20 61 72 65 20 61 6c 6c 20 64 6f 6e  * we are all don
3aa0: 65 20 77 69 74 68 20 61 6c 6c 20 73 6f 63 6b 65  e with all socke
3ab0: 74 20 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 20 20  t operations..  
3ac0: 2a 2f 0a 20 20 77 73 5f 63 6c 65 61 6e 75 70 28  */.  ws_cleanup(
3ad0: 29 3b 0a 7d 0a                                   );.}.