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 73 68 61 31 73 75 6d 5f 62 6c 6f 62 ;. sha1sum_blob
14b0: 28 70 53 65 6e 64 2c 20 26 6e 6f 6e 63 65 29 3b (pSend, &nonce);
14c0: 0a 20 20 62 6c 6f 62 5f 63 6f 70 79 28 26 70 77 . blob_copy(&pw
14d0: 2c 20 26 6e 6f 6e 63 65 29 3b 0a 20 20 62 6c 6f , &nonce);. blo
14e0: 62 5f 7a 65 72 6f 28 26 6c 6f 67 69 6e 29 3b 0a b_zero(&login);.
14f0: 20 20 69 66 28 20 67 2e 75 72 6c 55 73 65 72 3d if( g.urlUser=
1500: 3d 30 20 29 7b 0a 20 20 20 20 75 73 65 72 5f 73 =0 ){. user_s
1510: 65 6c 65 63 74 28 29 3b 0a 20 20 20 20 64 62 5f elect();. db_
1520: 62 6c 6f 62 28 26 70 77 2c 20 22 53 45 4c 45 43 blob(&pw, "SELEC
1530: 54 20 70 77 20 46 52 4f 4d 20 75 73 65 72 20 57 T pw FROM user W
1540: 48 45 52 45 20 75 69 64 3d 25 64 22 2c 20 67 2e HERE uid=%d", g.
1550: 75 73 65 72 55 69 64 29 3b 0a 20 20 20 20 73 68 userUid);. sh
1560: 61 31 73 75 6d 5f 62 6c 6f 62 28 26 70 77 2c 20 a1sum_blob(&pw,
1570: 26 73 69 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f &sig);. blob_
1580: 61 70 70 65 6e 64 66 28 26 6c 6f 67 69 6e 2c 20 appendf(&login,
1590: 22 6c 6f 67 69 6e 20 25 73 20 25 62 20 25 62 5c "login %s %b %b\
15a0: 6e 22 2c 20 67 2e 7a 4c 6f 67 69 6e 2c 20 26 6e n", g.zLogin, &n
15b0: 6f 6e 63 65 2c 20 26 73 69 67 29 3b 0a 20 20 7d once, &sig);. }
15c0: 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 67 2e else{. if( g.
15d0: 75 72 6c 50 61 73 73 77 64 3d 3d 30 20 29 7b 0a urlPasswd==0 ){.
15e0: 20 20 20 20 20 20 69 66 28 20 73 74 72 63 6d 70 if( strcmp
15f0: 28 67 2e 75 72 6c 55 73 65 72 2c 22 61 6e 6f 6e (g.urlUser,"anon
1600: 79 6d 6f 75 73 22 29 21 3d 30 20 29 7b 0a 20 20 ymous")!=0 ){.
1610: 20 20 20 20 20 20 63 68 61 72 20 2a 7a 50 72 6f char *zPro
1620: 6d 70 74 20 3d 20 6d 70 72 69 6e 74 66 28 22 70 mpt = mprintf("p
1630: 61 73 73 77 6f 72 64 20 66 6f 72 20 25 73 3a 20 assword for %s:
1640: 22 2c 20 67 2e 75 72 6c 55 73 65 72 29 3b 0a 20 ", g.urlUser);.
1650: 20 20 20 20 20 20 20 42 6c 6f 62 20 78 3b 0a 20 Blob x;.
1660: 20 20 20 20 20 20 20 70 72 6f 6d 70 74 5f 66 6f prompt_fo
1670: 72 5f 70 61 73 73 77 6f 72 64 28 7a 50 72 6f 6d r_password(zProm
1680: 70 74 2c 20 26 78 2c 20 30 29 3b 0a 20 20 20 20 pt, &x, 0);.
1690: 20 20 20 20 66 72 65 65 28 7a 50 72 6f 6d 70 74 free(zPrompt
16a0: 29 3b 0a 20 20 20 20 20 20 20 20 67 2e 75 72 6c );. g.url
16b0: 50 61 73 73 77 64 20 3d 20 62 6c 6f 62 5f 73 74 Passwd = blob_st
16c0: 72 28 26 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c r(&x);. }el
16d0: 73 65 7b 0a 20 20 20 20 20 20 20 20 67 2e 75 72 se{. g.ur
16e0: 6c 50 61 73 73 77 64 20 3d 20 22 22 3b 0a 20 20 lPasswd = "";.
16f0: 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 }. }.
1700: 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 70 77 2c blob_append(&pw,
1710: 20 67 2e 75 72 6c 50 61 73 73 77 64 2c 20 2d 31 g.urlPasswd, -1
1720: 29 3b 0a 20 20 20 20 2f 2a 20 70 72 69 6e 74 66 );. /* printf
1730: 28 22 70 72 65 73 69 67 3d 5b 25 73 5d 5c 6e 22 ("presig=[%s]\n"
1740: 2c 20 62 6c 6f 62 5f 73 74 72 28 26 70 77 29 29 , blob_str(&pw))
1750: 3b 20 2a 2f 0a 20 20 20 20 73 68 61 31 73 75 6d ; */. sha1sum
1760: 5f 62 6c 6f 62 28 26 70 77 2c 20 26 73 69 67 29 _blob(&pw, &sig)
1770: 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e ;. blob_appen
1780: 64 66 28 26 6c 6f 67 69 6e 2c 20 22 6c 6f 67 69 df(&login, "logi
1790: 6e 20 25 73 20 25 62 20 25 62 5c 6e 22 2c 20 67 n %s %b %b\n", g
17a0: 2e 75 72 6c 55 73 65 72 2c 20 26 6e 6f 6e 63 65 .urlUser, &nonce
17b0: 2c 20 26 73 69 67 29 3b 0a 20 20 7d 20 20 20 20 , &sig);. }
17c0: 20 20 20 20 0a 20 20 62 6c 6f 62 5f 72 65 73 65 . blob_rese
17d0: 74 28 26 6e 6f 6e 63 65 29 3b 0a 20 20 62 6c 6f t(&nonce);. blo
17e0: 62 5f 72 65 73 65 74 28 26 70 77 29 3b 0a 20 20 b_reset(&pw);.
17f0: 62 6c 6f 62 5f 72 65 73 65 74 28 26 73 69 67 29 blob_reset(&sig)
1800: 3b 0a 20 20 69 66 28 20 67 2e 66 48 74 74 70 54 ;. if( g.fHttpT
1810: 72 61 63 65 20 29 7b 0a 20 20 20 20 70 61 79 6c race ){. payl
1820: 6f 61 64 20 3d 20 6c 6f 67 69 6e 3b 0a 20 20 20 oad = login;.
1830: 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 26 70 61 blob_append(&pa
1840: 79 6c 6f 61 64 2c 20 62 6c 6f 62 5f 62 75 66 66 yload, blob_buff
1850: 65 72 28 70 53 65 6e 64 29 2c 20 62 6c 6f 62 5f er(pSend), blob_
1860: 73 69 7a 65 28 70 53 65 6e 64 29 29 3b 0a 20 20 size(pSend));.
1870: 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f }else{. blob_
1880: 63 6f 6d 70 72 65 73 73 32 28 26 6c 6f 67 69 6e compress2(&login
1890: 2c 20 70 53 65 6e 64 2c 20 26 70 61 79 6c 6f 61 , pSend, &payloa
18a0: 64 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 72 65 73 d);. blob_res
18b0: 65 74 28 26 6c 6f 67 69 6e 29 3b 0a 20 20 7d 0a et(&login);. }.
18c0: 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 68 64 72 blob_zero(&hdr
18d0: 29 3b 0a 20 20 69 20 3d 20 73 74 72 6c 65 6e 28 );. i = strlen(
18e0: 67 2e 75 72 6c 50 61 74 68 29 3b 0a 20 20 69 66 g.urlPath);. if
18f0: 28 20 69 3e 30 20 26 26 20 67 2e 75 72 6c 50 61 ( i>0 && g.urlPa
1900: 74 68 5b 69 2d 31 5d 3d 3d 27 2f 27 20 29 7b 0a th[i-1]=='/' ){.
1910: 20 20 20 20 7a 53 65 70 20 3d 20 22 22 3b 0a 20 zSep = "";.
1920: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 }else{. zSep
1930: 20 3d 20 22 2f 22 3b 0a 20 20 7d 0a 20 20 62 6c = "/";. }. bl
1940: 6f 62 5f 61 70 70 65 6e 64 66 28 26 68 64 72 2c ob_appendf(&hdr,
1950: 20 22 50 4f 53 54 20 25 73 25 73 78 66 65 72 20 "POST %s%sxfer
1960: 48 54 54 50 2f 31 2e 31 5c 72 5c 6e 22 2c 20 67 HTTP/1.1\r\n", g
1970: 2e 75 72 6c 50 61 74 68 2c 20 7a 53 65 70 29 3b .urlPath, zSep);
1980: 0a 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 . blob_appendf(
1990: 26 68 64 72 2c 20 22 48 6f 73 74 3a 20 25 73 3a &hdr, "Host: %s:
19a0: 25 64 5c 72 5c 6e 22 2c 20 67 2e 75 72 6c 4e 61 %d\r\n", g.urlNa
19b0: 6d 65 2c 20 67 2e 75 72 6c 50 6f 72 74 29 3b 0a me, g.urlPort);.
19c0: 20 20 69 66 28 20 67 2e 66 48 74 74 70 54 72 61 if( g.fHttpTra
19d0: 63 65 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f 61 ce ){. blob_a
19e0: 70 70 65 6e 64 66 28 26 68 64 72 2c 20 22 43 6f ppendf(&hdr, "Co
19f0: 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 70 6c ntent-Type: appl
1a00: 69 63 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 6c ication/x-fossil
1a10: 2d 64 65 62 75 67 5c 72 5c 6e 22 29 3b 0a 20 20 -debug\r\n");.
1a20: 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f }else{. blob_
1a30: 61 70 70 65 6e 64 66 28 26 68 64 72 2c 20 22 43 appendf(&hdr, "C
1a40: 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 70 ontent-Type: app
1a50: 6c 69 63 61 74 69 6f 6e 2f 78 2d 66 6f 73 73 69 lication/x-fossi
1a60: 6c 5c 72 5c 6e 22 29 3b 0a 20 20 7d 0a 20 20 62 l\r\n");. }. b
1a70: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 68 64 72 lob_appendf(&hdr
1a80: 2c 20 22 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67 74 , "Content-Lengt
1a90: 68 3a 20 25 64 5c 72 5c 6e 5c 72 5c 6e 22 2c 20 h: %d\r\n\r\n",
1aa0: 62 6c 6f 62 5f 73 69 7a 65 28 26 70 61 79 6c 6f blob_size(&paylo
1ab0: 61 64 29 29 3b 0a 0a 20 20 69 66 28 20 67 2e 66 ad));.. if( g.f
1ac0: 48 74 74 70 54 72 61 63 65 20 29 7b 0a 20 20 20 HttpTrace ){.
1ad0: 20 2f 2a 20 57 68 65 6e 20 74 72 61 63 69 6e 67 /* When tracing
1ae0: 2c 20 77 72 69 74 65 20 74 68 65 20 74 72 61 6e , write the tran
1af0: 73 6d 69 74 74 65 64 20 48 54 54 50 20 6d 65 73 smitted HTTP mes
1b00: 73 61 67 65 20 62 6f 74 68 20 74 6f 20 73 74 61 sage both to sta
1b10: 6e 64 61 72 64 0a 20 20 20 20 2a 2a 20 6f 75 74 ndard. ** out
1b20: 70 75 74 20 61 6e 64 20 69 6e 74 6f 20 61 20 66 put and into a f
1b30: 69 6c 65 2e 20 20 54 68 65 20 66 69 6c 65 20 63 ile. The file c
1b40: 61 6e 20 74 68 65 6e 20 62 65 20 75 73 65 64 20 an then be used
1b50: 74 6f 20 64 72 69 76 65 20 74 68 65 0a 20 20 20 to drive the.
1b60: 20 2a 2a 20 73 65 72 76 65 72 2d 73 69 64 65 20 ** server-side
1b70: 6c 69 6b 65 20 74 68 69 73 3a 0a 20 20 20 20 2a like this:. *
1b80: 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 2e 2f *. ** ./
1b90: 66 6f 73 73 69 6c 20 68 74 74 70 20 3c 68 74 74 fossil http <htt
1ba0: 70 2d 74 72 61 63 65 2d 31 2e 74 78 74 0a 20 20 p-trace-1.txt.
1bb0: 20 20 2a 2f 0a 20 20 20 20 73 74 61 74 69 63 20 */. static
1bc0: 69 6e 74 20 74 72 61 63 65 43 6e 74 20 3d 20 30 int traceCnt = 0
1bd0: 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 4f 75 74 ;. char *zOut
1be0: 46 69 6c 65 3b 0a 20 20 20 20 46 49 4c 45 20 2a File;. FILE *
1bf0: 6f 75 74 3b 0a 20 20 20 20 74 72 61 63 65 43 6e out;. traceCn
1c00: 74 2b 2b 3b 0a 20 20 20 20 7a 4f 75 74 46 69 6c t++;. zOutFil
1c10: 65 20 3d 20 6d 70 72 69 6e 74 66 28 22 68 74 74 e = mprintf("htt
1c20: 70 2d 74 72 61 63 65 2d 25 64 2e 74 78 74 22 2c p-trace-%d.txt",
1c30: 20 74 72 61 63 65 43 6e 74 29 3b 0a 20 20 20 20 traceCnt);.
1c40: 70 72 69 6e 74 66 28 22 48 54 54 50 20 53 45 4e printf("HTTP SEN
1c50: 44 3a 20 28 25 73 29 5c 6e 25 73 25 73 3d 3d 3d D: (%s)\n%s%s===
1c60: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d ================
1c70: 3d 3d 3d 3d 5c 6e 22 2c 20 0a 20 20 20 20 20 20 ====\n", .
1c80: 20 20 7a 4f 75 74 46 69 6c 65 2c 20 62 6c 6f 62 zOutFile, blob
1c90: 5f 73 74 72 28 26 68 64 72 29 2c 20 62 6c 6f 62 _str(&hdr), blob
1ca0: 5f 73 74 72 28 26 70 61 79 6c 6f 61 64 29 29 3b _str(&payload));
1cb0: 0a 20 20 20 20 6f 75 74 20 3d 20 66 6f 70 65 6e . out = fopen
1cc0: 28 7a 4f 75 74 46 69 6c 65 2c 20 22 77 22 29 3b (zOutFile, "w");
1cd0: 0a 20 20 20 20 69 66 28 20 6f 75 74 20 29 7b 0a . if( out ){.
1ce0: 20 20 20 20 20 20 66 77 72 69 74 65 28 62 6c 6f fwrite(blo
1cf0: 62 5f 62 75 66 66 65 72 28 26 68 64 72 29 2c 20 b_buffer(&hdr),
1d00: 31 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 68 64 1, blob_size(&hd
1d10: 72 29 2c 20 6f 75 74 29 3b 0a 20 20 20 20 20 20 r), out);.
1d20: 66 77 72 69 74 65 28 62 6c 6f 62 5f 62 75 66 66 fwrite(blob_buff
1d30: 65 72 28 26 70 61 79 6c 6f 61 64 29 2c 20 31 2c er(&payload), 1,
1d40: 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 61 79 6c blob_size(&payl
1d50: 6f 61 64 29 2c 20 6f 75 74 29 3b 0a 20 20 20 20 oad), out);.
1d60: 20 20 66 63 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 fclose(out);.
1d70: 20 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 63 }. }. for(c
1d80: 6e 74 3d 30 3b 20 63 6e 74 3c 32 3b 20 63 6e 74 nt=0; cnt<2; cnt
1d90: 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 68 74 74 ++){. if( htt
1da0: 70 5f 73 65 6e 64 5f 72 65 63 76 28 26 68 64 72 p_send_recv(&hdr
1db0: 2c 20 26 70 61 79 6c 6f 61 64 2c 20 70 52 65 63 , &payload, pRec
1dc0: 76 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d 0a v) ) break;. }.
1dd0: 20 20 69 66 28 20 63 6e 74 3e 3d 32 20 29 7b 0a if( cnt>=2 ){.
1de0: 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 fossil_panic
1df0: 28 22 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 ("connection to
1e00: 73 65 72 76 65 72 20 66 61 69 6c 65 64 22 29 3b server failed");
1e10: 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 72 65 73 65 . }. blob_rese
1e20: 74 28 26 68 64 72 29 3b 0a 20 20 62 6c 6f 62 5f t(&hdr);. blob_
1e30: 72 65 73 65 74 28 26 70 61 79 6c 6f 61 64 29 3b reset(&payload);
1e40: 0a 20 20 69 66 28 20 67 2e 66 48 74 74 70 54 72 . if( g.fHttpTr
1e50: 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74 ace ){. print
1e60: 66 28 22 48 54 54 50 20 52 45 43 45 49 56 45 3a f("HTTP RECEIVE:
1e70: 5c 6e 25 73 5c 6e 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d \n%s\n==========
1e80: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 5c 6e 22 =============\n"
1e90: 2c 20 62 6c 6f 62 5f 73 74 72 28 70 52 65 63 76 , blob_str(pRecv
1ea0: 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 ));. }else{.
1eb0: 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 72 65 73 73 blob_uncompress
1ec0: 28 70 52 65 63 76 2c 20 70 52 65 63 76 29 3b 0a (pRecv, pRecv);.
1ed0: 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 }.}.../*.** Ma
1ee0: 6b 65 20 73 75 72 65 20 74 68 65 20 73 6f 63 6b ke sure the sock
1ef0: 65 74 20 74 6f 20 74 68 65 20 48 54 54 50 20 73 et to the HTTP s
1f00: 65 72 76 65 72 20 69 73 20 63 6c 6f 73 65 64 20 erver is closed
1f10: 0a 2a 2f 0a 76 6f 69 64 20 68 74 74 70 5f 63 6c .*/.void http_cl
1f20: 6f 73 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 ose(void){. if(
1f30: 20 70 53 6f 63 6b 65 74 20 29 7b 0a 20 20 20 20 pSocket ){.
1f40: 66 63 6c 6f 73 65 28 70 53 6f 63 6b 65 74 29 3b fclose(pSocket);
1f50: 0a 20 20 20 20 70 53 6f 63 6b 65 74 20 3d 20 30 . pSocket = 0
1f60: 3b 0a 20 20 7d 0a 7d 0a ;. }.}.