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 23 69 6e 63 6c 75 assert.h>.#inclu
03f0: 64 65 20 3c 61 72 70 61 2f 69 6e 65 74 2e 68 3e de <arpa/inet.h>
0400: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 74 .#include <sys/t
0410: 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 ypes.h>.#include
0420: 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 3e 0a <sys/socket.h>.
0430: 23 69 6e 63 6c 75 64 65 20 3c 6e 65 74 64 62 2e #include <netdb.
0440: 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 6e 65 74 h>.#include <net
0450: 69 6e 65 74 2f 69 6e 2e 68 3e 0a 23 69 6e 63 6c inet/in.h>.#incl
0460: 75 64 65 20 3c 73 69 67 6e 61 6c 2e 68 3e 0a 0a ude <signal.h>..
0470: 2f 2a 0a 2a 2a 20 50 65 72 73 69 73 74 65 6e 74 /*.** Persistent
0480: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f information abo
0490: 75 74 20 74 68 65 20 48 54 54 50 20 63 6f 6e 6e ut the HTTP conn
04a0: 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 ection..*/.stati
04b0: 63 20 46 49 4c 45 20 2a 70 53 6f 63 6b 65 74 20 c FILE *pSocket
04c0: 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 73 6f = 0; /* The so
04d0: 63 6b 65 74 20 6f 6e 20 77 68 69 63 68 20 77 65 cket on which we
04e0: 20 74 61 6c 6b 20 74 6f 20 74 68 65 20 73 65 72 talk to the ser
04f0: 76 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4f 70 ver */../*.** Op
0500: 65 6e 20 61 20 73 6f 63 6b 65 74 20 63 6f 6e 6e en a socket conn
0510: 65 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 73 65 ection to the se
0520: 72 76 65 72 2e 20 20 52 65 74 75 72 6e 20 30 20 rver. Return 0
0530: 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 0a 2a on success and.*
0540: 2a 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 6e * non-zero if an
0550: 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a error occurs..*
0560: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 74 74 /.static int htt
0570: 70 5f 6f 70 65 6e 5f 73 6f 63 6b 65 74 28 76 6f p_open_socket(vo
0580: 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 74 id){. static st
0590: 72 75 63 74 20 73 6f 63 6b 61 64 64 72 5f 69 6e ruct sockaddr_in
05a0: 20 61 64 64 72 3b 20 20 2f 2a 20 54 68 65 20 73 addr; /* The s
05b0: 65 72 76 65 72 20 61 64 64 72 65 73 73 20 2a 2f erver address */
05c0: 0a 20 20 73 74 61 74 69 63 20 69 6e 74 20 61 64 . static int ad
05d0: 64 72 49 73 49 6e 69 74 20 3d 20 30 3b 20 20 20 drIsInit = 0;
05e0: 20 20 20 20 2f 2a 20 54 72 75 65 20 6f 6e 63 65 /* True once
05f0: 20 61 64 64 72 20 69 73 20 69 6e 69 74 69 61 6c addr is initial
0600: 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 73 3b ized */. int s;
0610: 0a 0a 20 20 69 66 28 20 21 61 64 64 72 49 73 49 .. if( !addrIsI
0620: 6e 69 74 20 29 7b 0a 20 20 20 20 61 64 64 72 2e nit ){. addr.
0630: 73 69 6e 5f 66 61 6d 69 6c 79 20 3d 20 41 46 5f sin_family = AF_
0640: 49 4e 45 54 3b 0a 20 20 20 20 61 64 64 72 2e 73 INET;. addr.s
0650: 69 6e 5f 70 6f 72 74 20 3d 20 68 74 6f 6e 73 28 in_port = htons(
0660: 67 2e 75 72 6c 50 6f 72 74 29 3b 0a 20 20 20 20 g.urlPort);.
0670: 2a 28 69 6e 74 2a 29 26 61 64 64 72 2e 73 69 6e *(int*)&addr.sin
0680: 5f 61 64 64 72 20 3d 20 69 6e 65 74 5f 61 64 64 _addr = inet_add
0690: 72 28 67 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 r(g.urlName);.
06a0: 20 20 69 66 28 20 2d 31 20 3d 3d 20 2a 28 69 6e if( -1 == *(in
06b0: 74 2a 29 26 61 64 64 72 2e 73 69 6e 5f 61 64 64 t*)&addr.sin_add
06c0: 72 20 29 7b 0a 23 69 66 6e 64 65 66 20 46 4f 53 r ){.#ifndef FOS
06d0: 53 49 4c 5f 53 54 41 54 49 43 5f 4c 49 4e 4b 0a SIL_STATIC_LINK.
06e0: 20 20 20 20 20 20 73 74 72 75 63 74 20 68 6f 73 struct hos
06f0: 74 65 6e 74 20 2a 70 48 6f 73 74 3b 0a 20 20 20 tent *pHost;.
0700: 20 20 20 70 48 6f 73 74 20 3d 20 67 65 74 68 6f pHost = getho
0710: 73 74 62 79 6e 61 6d 65 28 67 2e 75 72 6c 4e 61 stbyname(g.urlNa
0720: 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 me);. if( p
0730: 48 6f 73 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 Host!=0 ){.
0740: 20 20 20 6d 65 6d 63 70 79 28 26 61 64 64 72 2e memcpy(&addr.
0750: 73 69 6e 5f 61 64 64 72 2c 70 48 6f 73 74 2d 3e sin_addr,pHost->
0760: 68 5f 61 64 64 72 5f 6c 69 73 74 5b 30 5d 2c 70 h_addr_list[0],p
0770: 48 6f 73 74 2d 3e 68 5f 6c 65 6e 67 74 68 29 3b Host->h_length);
0780: 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e . }else.#en
0790: 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 dif. {.
07a0: 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 fossil_panic
07b0: 28 22 63 61 6e 27 74 20 72 65 73 6f 6c 76 65 20 ("can't resolve
07c0: 68 6f 73 74 20 6e 61 6d 65 3a 20 25 73 5c 6e 22 host name: %s\n"
07d0: 2c 20 67 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 , g.urlName);.
07e0: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
07f0: 61 64 64 72 49 73 49 6e 69 74 20 3d 20 31 3b 0a addrIsInit = 1;.
0800: 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65 20 . /* Set the
0810: 47 6c 6f 62 61 6c 2e 7a 49 70 41 64 64 72 20 76 Global.zIpAddr v
0820: 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 73 ariable to the s
0830: 65 72 76 65 72 20 77 65 20 61 72 65 20 74 61 6c erver we are tal
0840: 6b 69 6e 67 20 74 6f 2e 0a 20 20 20 20 2a 2a 20 king to.. **
0850: 54 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 This is used to
0860: 70 6f 70 75 6c 61 74 65 20 74 68 65 20 69 70 61 populate the ipa
0870: 64 64 72 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 ddr column of th
0880: 65 20 72 63 76 66 72 6f 6d 20 74 61 62 6c 65 2c e rcvfrom table,
0890: 0a 20 20 20 20 2a 2a 20 69 66 20 61 6e 79 20 66 . ** if any f
08a0: 69 6c 65 73 20 61 72 65 20 72 65 63 65 69 76 65 iles are receive
08b0: 64 20 66 72 6f 6d 20 74 68 65 20 73 65 72 76 65 d from the serve
08c0: 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 67 2e r.. */. g.
08d0: 7a 49 70 41 64 64 72 20 3d 20 6d 70 72 69 6e 74 zIpAddr = mprint
08e0: 66 28 22 25 73 22 2c 20 69 6e 65 74 5f 6e 74 6f f("%s", inet_nto
08f0: 61 28 61 64 64 72 2e 73 69 6e 5f 61 64 64 72 29 a(addr.sin_addr)
0900: 29 3b 0a 20 20 7d 0a 20 20 73 20 3d 20 73 6f 63 );. }. s = soc
0910: 6b 65 74 28 41 46 5f 49 4e 45 54 2c 53 4f 43 4b ket(AF_INET,SOCK
0920: 5f 53 54 52 45 41 4d 2c 30 29 3b 0a 20 20 69 66 _STREAM,0);. if
0930: 28 20 73 3c 30 20 29 7b 0a 20 20 20 20 66 6f 73 ( s<0 ){. fos
0940: 73 69 6c 5f 70 61 6e 69 63 28 22 63 61 6e 6e 6f sil_panic("canno
0950: 74 20 63 72 65 61 74 65 20 61 20 73 6f 63 6b 65 t create a socke
0960: 74 22 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 t");. }. if( c
0970: 6f 6e 6e 65 63 74 28 73 2c 28 73 74 72 75 63 74 onnect(s,(struct
0980: 20 73 6f 63 6b 61 64 64 72 2a 29 26 61 64 64 72 sockaddr*)&addr
0990: 2c 73 69 7a 65 6f 66 28 61 64 64 72 29 29 3c 30 ,sizeof(addr))<0
09a0: 20 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 ){. fossil_p
09b0: 61 6e 69 63 28 22 63 61 6e 6e 6f 74 20 63 6f 6e anic("cannot con
09c0: 6e 65 63 74 20 74 6f 20 68 6f 73 74 20 25 73 3a nect to host %s:
09d0: 25 64 22 2c 20 67 2e 75 72 6c 4e 61 6d 65 2c 20 %d", g.urlName,
09e0: 67 2e 75 72 6c 50 6f 72 74 29 3b 0a 20 20 7d 0a g.urlPort);. }.
09f0: 20 20 70 53 6f 63 6b 65 74 20 3d 20 66 64 6f 70 pSocket = fdop
0a00: 65 6e 28 73 2c 22 72 2b 22 29 3b 0a 20 20 73 69 en(s,"r+");. si
0a10: 67 6e 61 6c 28 53 49 47 50 49 50 45 2c 20 53 49 gnal(SIGPIPE, SI
0a20: 47 5f 49 47 4e 29 3b 0a 20 20 72 65 74 75 72 6e G_IGN);. return
0a30: 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 0;.}../*.** Mak
0a40: 65 20 61 20 73 69 6e 67 6c 65 20 61 74 74 65 6d e a single attem
0a50: 70 74 20 74 6f 20 74 61 6c 6b 20 74 6f 20 74 68 pt to talk to th
0a60: 65 20 73 65 72 76 65 72 2e 20 20 52 65 74 75 72 e server. Retur
0a70: 6e 20 54 52 55 45 20 6f 6e 20 73 75 63 63 65 73 n TRUE on succes
0a80: 73 0a 2a 2a 20 61 6e 64 20 46 41 4c 53 45 20 6f s.** and FALSE o
0a90: 6e 20 61 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a n a failure..**.
0aa0: 2a 2a 20 70 48 65 61 64 65 72 20 63 6f 6e 74 61 ** pHeader conta
0ab0: 69 6e 73 20 74 68 65 20 48 54 54 50 20 68 65 61 ins the HTTP hea
0ac0: 64 65 72 2e 20 20 70 50 61 79 6c 6f 61 64 20 63 der. pPayload c
0ad0: 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 ontains the cont
0ae0: 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 ent..** The cont
0af0: 65 6e 74 20 6f 66 20 74 68 65 20 72 65 70 6c 79 ent of the reply
0b00: 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f is written into
0b10: 20 70 52 65 70 6c 79 2e 20 20 70 52 65 70 6c 79 pReply. pReply
0b20: 20 69 73 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 is assumed.** t
0b30: 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a o be uninitializ
0b40: 65 64 20 70 72 69 6f 72 20 74 6f 20 74 68 69 73 ed prior to this
0b50: 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 call..**.** If
0b60: 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c an error occurs,
0b70: 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 this routine re
0b80: 74 75 72 6e 20 66 61 6c 73 65 2c 20 72 65 73 65 turn false, rese
0b90: 74 73 20 70 52 65 70 6c 79 20 61 6e 64 0a 2a 2a ts pReply and.**
0ba0: 20 63 6c 6f 73 65 73 20 74 68 65 20 70 65 72 73 closes the pers
0bb0: 69 73 74 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f istent connectio
0bc0: 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 n, if any..*/.st
0bd0: 61 74 69 63 20 69 6e 74 20 68 74 74 70 5f 73 65 atic int http_se
0be0: 6e 64 5f 72 65 63 76 28 42 6c 6f 62 20 2a 70 48 nd_recv(Blob *pH
0bf0: 65 61 64 65 72 2c 20 42 6c 6f 62 20 2a 70 50 61 eader, Blob *pPa
0c00: 79 6c 6f 61 64 2c 20 42 6c 6f 62 20 2a 70 52 65 yload, Blob *pRe
0c10: 70 6c 79 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a ply){. int rc;.
0c20: 20 20 69 6e 74 20 63 6c 6f 73 65 43 6f 6e 6e 65 int closeConne
0c30: 63 74 69 6f 6e 3b 0a 20 20 69 6e 74 20 69 4c 65 ction;. int iLe
0c40: 6e 67 74 68 3b 0a 20 20 69 6e 74 20 69 48 74 74 ngth;. int iHtt
0c50: 70 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 pVersion;. int
0c60: 69 3b 0a 20 20 69 6e 74 20 6e 52 65 61 64 3b 0a i;. int nRead;.
0c70: 20 20 63 68 61 72 20 7a 4c 69 6e 65 5b 32 30 30 char zLine[200
0c80: 30 5d 3b 0a 0a 20 20 69 66 28 20 70 53 6f 63 6b 0];.. if( pSock
0c90: 65 74 3d 3d 30 20 26 26 20 68 74 74 70 5f 6f 70 et==0 && http_op
0ca0: 65 6e 5f 73 6f 63 6b 65 74 28 29 20 29 7b 0a 20 en_socket() ){.
0cb0: 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d return 0;. }
0cc0: 0a 20 20 72 63 20 3d 20 66 77 72 69 74 65 28 62 . rc = fwrite(b
0cd0: 6c 6f 62 5f 62 75 66 66 65 72 28 70 48 65 61 64 lob_buffer(pHead
0ce0: 65 72 29 2c 20 31 2c 20 62 6c 6f 62 5f 73 69 7a er), 1, blob_siz
0cf0: 65 28 70 48 65 61 64 65 72 29 2c 20 70 53 6f 63 e(pHeader), pSoc
0d00: 6b 65 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d ket);. if( rc!=
0d10: 62 6c 6f 62 5f 73 69 7a 65 28 70 48 65 61 64 65 blob_size(pHeade
0d20: 72 29 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f r) ) goto write_
0d30: 65 72 72 3b 0a 20 20 72 63 20 3d 20 66 77 72 69 err;. rc = fwri
0d40: 74 65 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 te(blob_buffer(p
0d50: 50 61 79 6c 6f 61 64 29 2c 20 31 2c 20 62 6c 6f Payload), 1, blo
0d60: 62 5f 73 69 7a 65 28 70 50 61 79 6c 6f 61 64 29 b_size(pPayload)
0d70: 2c 20 70 53 6f 63 6b 65 74 29 3b 0a 20 20 69 66 , pSocket);. if
0d80: 28 20 72 63 21 3d 62 6c 6f 62 5f 73 69 7a 65 28 ( rc!=blob_size(
0d90: 70 50 61 79 6c 6f 61 64 29 20 29 20 67 6f 74 6f pPayload) ) goto
0da0: 20 77 72 69 74 65 5f 65 72 72 3b 0a 20 20 69 66 write_err;. if
0db0: 28 20 66 66 6c 75 73 68 28 70 53 6f 63 6b 65 74 ( fflush(pSocket
0dc0: 29 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 ) ) goto write_e
0dd0: 72 72 3b 0a 20 20 69 66 28 20 66 67 65 74 73 28 rr;. if( fgets(
0de0: 7a 4c 69 6e 65 2c 20 73 69 7a 65 6f 66 28 7a 4c zLine, sizeof(zL
0df0: 69 6e 65 29 2c 20 70 53 6f 63 6b 65 74 29 3d 3d ine), pSocket)==
0e00: 30 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 0 ) goto write_e
0e10: 72 72 3b 0a 20 20 69 66 28 20 73 73 63 61 6e 66 rr;. if( sscanf
0e20: 28 7a 4c 69 6e 65 2c 20 22 48 54 54 50 2f 31 2e (zLine, "HTTP/1.
0e30: 25 64 20 25 64 22 2c 20 26 69 48 74 74 70 56 65 %d %d", &iHttpVe
0e40: 72 73 69 6f 6e 2c 20 26 72 63 29 21 3d 32 20 29 rsion, &rc)!=2 )
0e50: 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 3b goto write_err;
0e60: 0a 20 20 69 66 28 20 72 63 21 3d 32 30 30 20 29 . if( rc!=200 )
0e70: 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 3b goto write_err;
0e80: 0a 20 20 69 66 28 20 69 48 74 74 70 56 65 72 73 . if( iHttpVers
0e90: 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c ion==0 ){. cl
0ea0: 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 oseConnection =
0eb0: 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 1;. }else{.
0ec0: 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 closeConnection
0ed0: 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 4c 65 6e 67 = 0;. }. iLeng
0ee0: 74 68 20 3d 20 2d 31 3b 0a 20 20 77 68 69 6c 65 th = -1;. while
0ef0: 28 20 66 67 65 74 73 28 7a 4c 69 6e 65 2c 20 73 ( fgets(zLine, s
0f00: 69 7a 65 6f 66 28 7a 4c 69 6e 65 29 2c 20 70 53 izeof(zLine), pS
0f10: 6f 63 6b 65 74 29 20 29 7b 0a 20 20 20 20 66 6f ocket) ){. fo
0f20: 72 28 69 3d 30 3b 20 7a 4c 69 6e 65 5b 69 5d 20 r(i=0; zLine[i]
0f30: 26 26 20 7a 4c 69 6e 65 5b 69 5d 21 3d 27 5c 6e && zLine[i]!='\n
0f40: 27 20 26 26 20 7a 4c 69 6e 65 5b 69 5d 21 3d 27 ' && zLine[i]!='
0f50: 5c 72 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 \r'; i++){}.
0f60: 69 66 28 20 69 3d 3d 30 20 29 20 62 72 65 61 6b if( i==0 ) break
0f70: 3b 0a 20 20 20 20 7a 4c 69 6e 65 5b 69 5d 20 3d ;. zLine[i] =
0f80: 20 30 3b 0a 20 20 20 20 69 66 28 20 73 74 72 6e 0;. if( strn
0f90: 63 61 73 65 63 6d 70 28 7a 4c 69 6e 65 2c 20 22 casecmp(zLine, "
0fa0: 63 6f 6e 74 65 6e 74 2d 6c 65 6e 67 74 68 3a 22 content-length:"
0fb0: 2c 31 35 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 ,15)==0 ){.
0fc0: 20 69 4c 65 6e 67 74 68 20 3d 20 61 74 6f 69 28 iLength = atoi(
0fd0: 26 7a 4c 69 6e 65 5b 31 36 5d 29 3b 0a 20 20 20 &zLine[16]);.
0fe0: 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 6e 63 }else if( strnc
0ff0: 61 73 65 63 6d 70 28 7a 4c 69 6e 65 2c 20 22 63 asecmp(zLine, "c
1000: 6f 6e 6e 65 63 74 69 6f 6e 3a 22 2c 20 31 31 29 onnection:", 11)
1010: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 ==0 ){. for
1020: 28 69 3d 31 32 3b 20 69 73 73 70 61 63 65 28 7a (i=12; isspace(z
1030: 4c 69 6e 65 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d Line[i]); i++){}
1040: 0a 20 20 20 20 20 20 69 66 28 20 7a 4c 69 6e 65 . if( zLine
1050: 5b 69 5d 3d 3d 27 63 27 20 7c 7c 20 7a 4c 69 6e [i]=='c' || zLin
1060: 65 5b 69 5d 3d 3d 27 43 27 20 29 7b 0a 20 20 20 e[i]=='C' ){.
1070: 20 20 20 20 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 closeConnec
1080: 74 69 6f 6e 20 3d 20 31 3b 20 20 20 2f 2a 20 43 tion = 1; /* C
1090: 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 onnection: close
10a0: 20 2a 2f 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 */. }else
10b0: 69 66 28 20 7a 4c 69 6e 65 5b 69 5d 3d 3d 27 6b if( zLine[i]=='k
10c0: 27 20 7c 7c 20 7a 4c 69 6e 65 5b 69 5d 3d 3d 27 ' || zLine[i]=='
10d0: 4b 27 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6c K' ){. cl
10e0: 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 oseConnection =
10f0: 30 3b 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 0; /* Connecti
1100: 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69 76 65 20 2a on: keep-alive *
1110: 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a /. }. }.
1120: 20 20 7d 0a 20 20 69 66 28 20 69 4c 65 6e 67 74 }. if( iLengt
1130: 68 3c 30 20 29 20 67 6f 74 6f 20 77 72 69 74 65 h<0 ) goto write
1140: 5f 65 72 72 3b 0a 20 20 6e 52 65 61 64 20 3d 20 _err;. nRead =
1150: 62 6c 6f 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 63 blob_read_from_c
1160: 68 61 6e 6e 65 6c 28 70 52 65 70 6c 79 2c 20 70 hannel(pReply, p
1170: 53 6f 63 6b 65 74 2c 20 69 4c 65 6e 67 74 68 29 Socket, iLength)
1180: 3b 0a 20 20 69 66 28 20 6e 52 65 61 64 21 3d 69 ;. if( nRead!=i
1190: 4c 65 6e 67 74 68 20 29 7b 0a 20 20 20 20 62 6c Length ){. bl
11a0: 6f 62 5f 72 65 73 65 74 28 70 52 65 70 6c 79 29 ob_reset(pReply)
11b0: 3b 0a 20 20 20 20 67 6f 74 6f 20 77 72 69 74 65 ;. goto write
11c0: 5f 65 72 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 _err;. }. if(
11d0: 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 closeConnection
11e0: 29 7b 0a 20 20 20 20 68 74 74 70 5f 63 6c 6f 73 ){. http_clos
11f0: 65 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 e();. }. retur
1200: 6e 20 31 3b 20 20 0a 0a 77 72 69 74 65 5f 65 72 n 1; ..write_er
1210: 72 3a 0a 20 20 68 74 74 70 5f 63 6c 6f 73 65 28 r:. http_close(
1220: 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d );. return 0;.}
1230: 0a 0a 2f 2a 0a 2a 2a 20 53 69 67 6e 20 74 68 65 ../*.** Sign the
1240: 20 63 6f 6e 74 65 6e 74 20 69 6e 20 70 53 65 6e content in pSen
1250: 64 2c 20 63 6f 6d 70 72 65 73 73 20 69 74 2c 20 d, compress it,
1260: 61 6e 64 20 73 65 6e 64 20 69 74 20 74 6f 20 74 and send it to t
1270: 68 65 20 73 65 72 76 65 72 0a 2a 2a 20 76 69 61 he server.** via
1280: 20 48 54 54 50 2e 20 20 47 65 74 20 61 20 72 65 HTTP. Get a re
1290: 70 6c 79 2c 20 75 6e 63 6f 6d 70 72 65 73 73 20 ply, uncompress
12a0: 74 68 65 20 72 65 70 6c 79 2c 20 61 6e 64 20 73 the reply, and s
12b0: 74 6f 72 65 20 74 68 65 20 72 65 70 6c 79 0a 2a tore the reply.*
12c0: 2a 20 69 6e 20 70 52 65 63 76 2e 20 20 70 52 65 * in pRecv. pRe
12d0: 63 76 20 69 73 20 61 73 73 75 6d 65 64 20 74 6f cv is assumed to
12e0: 20 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 be uninitialize
12f0: 64 20 77 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 d when.** this r
1300: 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 outine is called
1310: 20 2d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 - this routine
1320: 77 69 6c 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 will initialize
1330: 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 it..**.** The se
1340: 72 76 65 72 20 61 64 64 72 65 73 73 20 69 73 20 rver address is
1350: 63 6f 6e 74 61 69 6e 20 69 6e 20 74 68 65 20 22 contain in the "
1360: 67 22 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 g" global struct
1370: 75 72 65 2e 20 20 54 68 65 0a 2a 2a 20 75 72 6c ure. The.** url
1380: 5f 70 61 72 73 65 28 29 20 72 6f 75 74 69 6e 65 _parse() routine
1390: 20 73 68 6f 75 6c 64 20 68 61 76 65 20 62 65 65 should have bee
13a0: 6e 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 n called prior t
13b0: 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a o this routine.*
13c0: 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 * in order to fi
13d0: 6c 6c 20 74 68 69 73 20 73 74 72 75 63 74 75 72 ll this structur
13e0: 65 20 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2e e appropriately.
13f0: 0a 2a 2f 0a 76 6f 69 64 20 68 74 74 70 5f 65 78 .*/.void http_ex
1400: 63 68 61 6e 67 65 28 42 6c 6f 62 20 2a 70 53 65 change(Blob *pSe
1410: 6e 64 2c 20 42 6c 6f 62 20 2a 70 52 65 63 76 29 nd, Blob *pRecv)
1420: 7b 0a 20 20 42 6c 6f 62 20 6c 6f 67 69 6e 2c 20 {. Blob login,
1430: 6e 6f 6e 63 65 2c 20 73 69 67 2c 20 70 77 2c 20 nonce, sig, pw,
1440: 70 61 79 6c 6f 61 64 2c 20 68 64 72 3b 0a 20 20 payload, hdr;.
1450: 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 65 70 const char *zSep
1460: 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 ;. int i;. int
1470: 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 62 6c 6f cnt = 0;.. blo
1480: 62 5f 7a 65 72 6f 28 26 6e 6f 6e 63 65 29 3b 0a b_zero(&nonce);.
1490: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 77 29 blob_zero(&pw)
14a0: 3b 0a 20 20 64 62 5f 62 6c 6f 62 28 26 6e 6f 6e ;. db_blob(&non
14b0: 63 65 2c 20 22 53 45 4c 45 43 54 20 68 65 78 28 ce, "SELECT hex(
14c0: 72 61 6e 64 6f 6d 62 6c 6f 62 28 32 30 29 29 22 randomblob(20))"
14d0: 29 3b 0a 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26 );. blob_copy(&
14e0: 70 77 2c 20 26 6e 6f 6e 63 65 29 3b 0a 20 20 62 pw, &nonce);. b
14f0: 6c 6f 62 5f 7a 65 72 6f 28 26 6c 6f 67 69 6e 29 lob_zero(&login)
1500: 3b 0a 20 20 69 66 28 20 67 2e 75 72 6c 55 73 65 ;. if( g.urlUse
1510: 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 73 65 72 r==0 ){. user
1520: 5f 73 65 6c 65 63 74 28 29 3b 0a 20 20 20 20 64 _select();. d
1530: 62 5f 62 6c 6f 62 28 26 70 77 2c 20 22 53 45 4c b_blob(&pw, "SEL
1540: 45 43 54 20 70 77 20 46 52 4f 4d 20 75 73 65 72 ECT pw FROM user
1550: 20 57 48 45 52 45 20 75 69 64 3d 25 64 22 2c 20 WHERE uid=%d",
1560: 67 2e 75 73 65 72 55 69 64 29 3b 0a 20 20 20 20 g.userUid);.
1570: 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 26 70 77 sha1sum_blob(&pw
1580: 2c 20 26 73 69 67 29 3b 0a 20 20 20 20 62 6c 6f , &sig);. blo
1590: 62 5f 61 70 70 65 6e 64 66 28 26 6c 6f 67 69 6e b_appendf(&login
15a0: 2c 20 22 6c 6f 67 69 6e 20 25 73 20 25 62 20 25 , "login %s %b %
15b0: 62 5c 6e 22 2c 20 67 2e 7a 4c 6f 67 69 6e 2c 20 b\n", g.zLogin,
15c0: 26 6e 6f 6e 63 65 2c 20 26 73 69 67 29 3b 0a 20 &nonce, &sig);.
15d0: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 }else{. if(
15e0: 67 2e 75 72 6c 50 61 73 73 77 64 3d 3d 30 20 29 g.urlPasswd==0 )
15f0: 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 72 63 {. if( strc
1600: 6d 70 28 67 2e 75 72 6c 55 73 65 72 2c 22 61 6e mp(g.urlUser,"an
1610: 6f 6e 79 6d 6f 75 73 22 29 21 3d 30 20 29 7b 0a onymous")!=0 ){.
1620: 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 50 char *zP
1630: 72 6f 6d 70 74 20 3d 20 6d 70 72 69 6e 74 66 28 rompt = mprintf(
1640: 22 70 61 73 73 77 6f 72 64 20 66 6f 72 20 25 73 "password for %s
1650: 3a 20 22 2c 20 67 2e 75 72 6c 55 73 65 72 29 3b : ", g.urlUser);
1660: 0a 20 20 20 20 20 20 20 20 42 6c 6f 62 20 78 3b . Blob x;
1670: 0a 20 20 20 20 20 20 20 20 70 72 6f 6d 70 74 5f . prompt_
1680: 66 6f 72 5f 70 61 73 73 77 6f 72 64 28 7a 50 72 for_password(zPr
1690: 6f 6d 70 74 2c 20 26 78 2c 20 30 29 3b 0a 20 20 ompt, &x, 0);.
16a0: 20 20 20 20 20 20 66 72 65 65 28 7a 50 72 6f 6d free(zProm
16b0: 70 74 29 3b 0a 20 20 20 20 20 20 20 20 67 2e 75 pt);. g.u
16c0: 72 6c 50 61 73 73 77 64 20 3d 20 62 6c 6f 62 5f rlPasswd = blob_
16d0: 73 74 72 28 26 78 29 3b 0a 20 20 20 20 20 20 7d str(&x);. }
16e0: 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 67 2e else{. g.
16f0: 75 72 6c 50 61 73 73 77 64 20 3d 20 22 22 3b 0a urlPasswd = "";.
1700: 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 }. }.
1710: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 70 blob_append(&p
1720: 77 2c 20 67 2e 75 72 6c 50 61 73 73 77 64 2c 20 w, g.urlPasswd,
1730: 2d 31 29 3b 0a 20 20 20 20 2f 2a 20 70 72 69 6e -1);. /* prin
1740: 74 66 28 22 70 72 65 73 69 67 3d 5b 25 73 5d 5c tf("presig=[%s]\
1750: 6e 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26 70 77 n", blob_str(&pw
1760: 29 29 3b 20 2a 2f 0a 20 20 20 20 73 68 61 31 73 )); */. sha1s
1770: 75 6d 5f 62 6c 6f 62 28 26 70 77 2c 20 26 73 69 um_blob(&pw, &si
1780: 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 g);. blob_app
1790: 65 6e 64 66 28 26 6c 6f 67 69 6e 2c 20 22 6c 6f endf(&login, "lo
17a0: 67 69 6e 20 25 73 20 25 62 20 25 62 5c 6e 22 2c gin %s %b %b\n",
17b0: 20 67 2e 75 72 6c 55 73 65 72 2c 20 26 6e 6f 6e g.urlUser, &non
17c0: 63 65 2c 20 26 73 69 67 29 3b 0a 20 20 7d 20 20 ce, &sig);. }
17d0: 20 20 20 20 20 20 0a 20 20 62 6c 6f 62 5f 72 65 . blob_re
17e0: 73 65 74 28 26 6e 6f 6e 63 65 29 3b 0a 20 20 62 set(&nonce);. b
17f0: 6c 6f 62 5f 72 65 73 65 74 28 26 70 77 29 3b 0a lob_reset(&pw);.
1800: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 69 blob_reset(&si
1810: 67 29 3b 0a 20 20 69 66 28 20 67 2e 66 48 74 74 g);. if( g.fHtt
1820: 70 54 72 61 63 65 20 29 7b 0a 20 20 20 20 70 61 pTrace ){. pa
1830: 79 6c 6f 61 64 20 3d 20 6c 6f 67 69 6e 3b 0a 20 yload = login;.
1840: 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 blob_append(&
1850: 70 61 79 6c 6f 61 64 2c 20 62 6c 6f 62 5f 62 75 payload, blob_bu
1860: 66 66 65 72 28 70 53 65 6e 64 29 2c 20 62 6c 6f ffer(pSend), blo
1870: 62 5f 73 69 7a 65 28 70 53 65 6e 64 29 29 3b 0a b_size(pSend));.
1880: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f }else{. blo
1890: 62 5f 63 6f 6d 70 72 65 73 73 32 28 26 6c 6f 67 b_compress2(&log
18a0: 69 6e 2c 20 70 53 65 6e 64 2c 20 26 70 61 79 6c in, pSend, &payl
18b0: 6f 61 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 oad);. blob_r
18c0: 65 73 65 74 28 26 6c 6f 67 69 6e 29 3b 0a 20 20 eset(&login);.
18d0: 7d 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 68 }. blob_zero(&h
18e0: 64 72 29 3b 0a 20 20 69 20 3d 20 73 74 72 6c 65 dr);. i = strle
18f0: 6e 28 67 2e 75 72 6c 50 61 74 68 29 3b 0a 20 20 n(g.urlPath);.
1900: 69 66 28 20 69 3e 30 20 26 26 20 67 2e 75 72 6c if( i>0 && g.url
1910: 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 2f 27 20 29 Path[i-1]=='/' )
1920: 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 22 3b {. zSep = "";
1930: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 53 . }else{. zS
1940: 65 70 20 3d 20 22 2f 22 3b 0a 20 20 7d 0a 20 20 ep = "/";. }.
1950: 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 68 64 blob_appendf(&hd
1960: 72 2c 20 22 50 4f 53 54 20 25 73 25 73 78 66 65 r, "POST %s%sxfe
1970: 72 20 48 54 54 50 2f 31 2e 31 5c 72 5c 6e 22 2c r HTTP/1.1\r\n",
1980: 20 67 2e 75 72 6c 50 61 74 68 2c 20 7a 53 65 70 g.urlPath, zSep
1990: 29 3b 0a 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 );. blob_append
19a0: 66 28 26 68 64 72 2c 20 22 48 6f 73 74 3a 20 25 f(&hdr, "Host: %
19b0: 73 3a 25 64 5c 72 5c 6e 22 2c 20 67 2e 75 72 6c s:%d\r\n", g.url
19c0: 4e 61 6d 65 2c 20 67 2e 75 72 6c 50 6f 72 74 29 Name, g.urlPort)
19d0: 3b 0a 20 20 69 66 28 20 67 2e 66 48 74 74 70 54 ;. if( g.fHttpT
19e0: 72 61 63 65 20 29 7b 0a 20 20 20 20 62 6c 6f 62 race ){. blob
19f0: 5f 61 70 70 65 6e 64 66 28 26 68 64 72 2c 20 22 _appendf(&hdr, "
1a00: 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 Content-Type: ap
1a10: 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 plication/x-foss
1a20: 69 6c 2d 64 65 62 75 67 5c 72 5c 6e 22 29 3b 0a il-debug\r\n");.
1a30: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f }else{. blo
1a40: 62 5f 61 70 70 65 6e 64 66 28 26 68 64 72 2c 20 b_appendf(&hdr,
1a50: 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 "Content-Type: a
1a60: 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66 6f 73 pplication/x-fos
1a70: 73 69 6c 5c 72 5c 6e 22 29 3b 0a 20 20 7d 0a 20 sil\r\n");. }.
1a80: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 68 blob_appendf(&h
1a90: 64 72 2c 20 22 43 6f 6e 74 65 6e 74 2d 4c 65 6e dr, "Content-Len
1aa0: 67 74 68 3a 20 25 64 5c 72 5c 6e 5c 72 5c 6e 22 gth: %d\r\n\r\n"
1ab0: 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 61 79 , blob_size(&pay
1ac0: 6c 6f 61 64 29 29 3b 0a 0a 20 20 69 66 28 20 67 load));.. if( g
1ad0: 2e 66 48 74 74 70 54 72 61 63 65 20 29 7b 0a 20 .fHttpTrace ){.
1ae0: 20 20 20 2f 2a 20 57 68 65 6e 20 74 72 61 63 69 /* When traci
1af0: 6e 67 2c 20 77 72 69 74 65 20 74 68 65 20 74 72 ng, write the tr
1b00: 61 6e 73 6d 69 74 74 65 64 20 48 54 54 50 20 6d ansmitted HTTP m
1b10: 65 73 73 61 67 65 20 62 6f 74 68 20 74 6f 20 73 essage both to s
1b20: 74 61 6e 64 61 72 64 0a 20 20 20 20 2a 2a 20 6f tandard. ** o
1b30: 75 74 70 75 74 20 61 6e 64 20 69 6e 74 6f 20 61 utput and into a
1b40: 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69 6c 65 file. The file
1b50: 20 63 61 6e 20 74 68 65 6e 20 62 65 20 75 73 65 can then be use
1b60: 64 20 74 6f 20 64 72 69 76 65 20 74 68 65 0a 20 d to drive the.
1b70: 20 20 20 2a 2a 20 73 65 72 76 65 72 2d 73 69 64 ** server-sid
1b80: 65 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 20 e like this:.
1b90: 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 **. **
1ba0: 2e 2f 66 6f 73 73 69 6c 20 68 74 74 70 20 3c 68 ./fossil http <h
1bb0: 74 74 70 2d 74 72 61 63 65 2d 31 2e 74 78 74 0a ttp-trace-1.txt.
1bc0: 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 */. stati
1bd0: 63 20 69 6e 74 20 74 72 61 63 65 43 6e 74 20 3d c int traceCnt =
1be0: 20 30 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4f 0;. char *zO
1bf0: 75 74 46 69 6c 65 3b 0a 20 20 20 20 46 49 4c 45 utFile;. FILE
1c00: 20 2a 6f 75 74 3b 0a 20 20 20 20 74 72 61 63 65 *out;. trace
1c10: 43 6e 74 2b 2b 3b 0a 20 20 20 20 7a 4f 75 74 46 Cnt++;. zOutF
1c20: 69 6c 65 20 3d 20 6d 70 72 69 6e 74 66 28 22 68 ile = mprintf("h
1c30: 74 74 70 2d 74 72 61 63 65 2d 25 64 2e 74 78 74 ttp-trace-%d.txt
1c40: 22 2c 20 74 72 61 63 65 43 6e 74 29 3b 0a 20 20 ", traceCnt);.
1c50: 20 20 70 72 69 6e 74 66 28 22 48 54 54 50 20 53 printf("HTTP S
1c60: 45 4e 44 3a 20 28 25 73 29 5c 6e 25 73 25 73 3d END: (%s)\n%s%s=
1c70: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d ================
1c80: 3d 3d 3d 3d 3d 3d 5c 6e 22 2c 20 0a 20 20 20 20 ======\n", .
1c90: 20 20 20 20 7a 4f 75 74 46 69 6c 65 2c 20 62 6c zOutFile, bl
1ca0: 6f 62 5f 73 74 72 28 26 68 64 72 29 2c 20 62 6c ob_str(&hdr), bl
1cb0: 6f 62 5f 73 74 72 28 26 70 61 79 6c 6f 61 64 29 ob_str(&payload)
1cc0: 29 3b 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 );. out = fop
1cd0: 65 6e 28 7a 4f 75 74 46 69 6c 65 2c 20 22 77 22 en(zOutFile, "w"
1ce0: 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 20 29 );. if( out )
1cf0: 7b 0a 20 20 20 20 20 20 66 77 72 69 74 65 28 62 {. fwrite(b
1d00: 6c 6f 62 5f 62 75 66 66 65 72 28 26 68 64 72 29 lob_buffer(&hdr)
1d10: 2c 20 31 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 , 1, blob_size(&
1d20: 68 64 72 29 2c 20 6f 75 74 29 3b 0a 20 20 20 20 hdr), out);.
1d30: 20 20 66 77 72 69 74 65 28 62 6c 6f 62 5f 62 75 fwrite(blob_bu
1d40: 66 66 65 72 28 26 70 61 79 6c 6f 61 64 29 2c 20 ffer(&payload),
1d50: 31 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 61 1, blob_size(&pa
1d60: 79 6c 6f 61 64 29 2c 20 6f 75 74 29 3b 0a 20 20 yload), out);.
1d70: 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b fclose(out);
1d80: 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 . }. }. for
1d90: 28 63 6e 74 3d 30 3b 20 63 6e 74 3c 32 3b 20 63 (cnt=0; cnt<2; c
1da0: 6e 74 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 68 nt++){. if( h
1db0: 74 74 70 5f 73 65 6e 64 5f 72 65 63 76 28 26 68 ttp_send_recv(&h
1dc0: 64 72 2c 20 26 70 61 79 6c 6f 61 64 2c 20 70 52 dr, &payload, pR
1dd0: 65 63 76 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 ecv) ) break;.
1de0: 7d 0a 20 20 69 66 28 20 63 6e 74 3e 3d 32 20 29 }. if( cnt>=2 )
1df0: 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e {. fossil_pan
1e00: 69 63 28 22 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 ic("connection t
1e10: 6f 20 73 65 72 76 65 72 20 66 61 69 6c 65 64 22 o server failed"
1e20: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 );. }. blob_re
1e30: 73 65 74 28 26 68 64 72 29 3b 0a 20 20 62 6c 6f set(&hdr);. blo
1e40: 62 5f 72 65 73 65 74 28 26 70 61 79 6c 6f 61 64 b_reset(&payload
1e50: 29 3b 0a 20 20 69 66 28 20 67 2e 66 48 74 74 70 );. if( g.fHttp
1e60: 54 72 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 Trace ){. pri
1e70: 6e 74 66 28 22 48 54 54 50 20 52 45 43 45 49 56 ntf("HTTP RECEIV
1e80: 45 3a 5c 6e 25 73 5c 6e 3d 3d 3d 3d 3d 3d 3d 3d E:\n%s\n========
1e90: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 5c ===============\
1ea0: 6e 22 2c 20 62 6c 6f 62 5f 73 74 72 28 70 52 65 n", blob_str(pRe
1eb0: 63 76 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 cv));. }else{.
1ec0: 20 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 blob_uncompre
1ed0: 73 73 28 70 52 65 63 76 2c 20 70 52 65 63 76 29 ss(pRecv, pRecv)
1ee0: 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 ;. }.}.../*.**
1ef0: 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 73 6f Make sure the so
1f00: 63 6b 65 74 20 74 6f 20 74 68 65 20 48 54 54 50 cket to the HTTP
1f10: 20 73 65 72 76 65 72 20 69 73 20 63 6c 6f 73 65 server is close
1f20: 64 20 0a 2a 2f 0a 76 6f 69 64 20 68 74 74 70 5f d .*/.void http_
1f30: 63 6c 6f 73 65 28 76 6f 69 64 29 7b 0a 20 20 69 close(void){. i
1f40: 66 28 20 70 53 6f 63 6b 65 74 20 29 7b 0a 20 20 f( pSocket ){.
1f50: 20 20 66 63 6c 6f 73 65 28 70 53 6f 63 6b 65 74 fclose(pSocket
1f60: 29 3b 0a 20 20 20 20 70 53 6f 63 6b 65 74 20 3d );. pSocket =
1f70: 20 30 3b 0a 20 20 7d 0a 7d 0a 0;. }.}.