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 2e 68 3e 0a 23 65 6c 73 65 0a 23 20 20 ock.h>.#else.#
0420: 69 6e 63 6c 75 64 65 20 3c 61 72 70 61 2f 69 6e include <arpa/in
0430: 65 74 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 et.h>.# include
0440: 20 3c 73 79 73 2f 73 6f 63 6b 65 74 2e 68 3e 0a <sys/socket.h>.
0450: 23 20 20 69 6e 63 6c 75 64 65 20 3c 6e 65 74 64 # include <netd
0460: 62 2e 68 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 b.h>.# include
0470: 3c 6e 65 74 69 6e 65 74 2f 69 6e 2e 68 3e 0a 23 <netinet/in.h>.#
0480: 65 6e 64 69 66 0a 23 69 6e 63 6c 75 64 65 20 3c endif.#include <
0490: 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c 75 assert.h>.#inclu
04a0: 64 65 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e de <sys/types.h>
04b0: 0a 23 69 6e 63 6c 75 64 65 20 3c 73 69 67 6e 61 .#include <signa
04c0: 6c 2e 68 3e 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 73 l.h>../*.** Pers
04d0: 69 73 74 65 6e 74 20 69 6e 66 6f 72 6d 61 74 69 istent informati
04e0: 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 48 54 54 on about the HTT
04f0: 50 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f P connection..*/
0500: 0a 73 74 61 74 69 63 20 46 49 4c 45 20 2a 70 53 .static FILE *pS
0510: 6f 63 6b 65 74 20 3d 20 30 3b 20 20 20 2f 2a 20 ocket = 0; /*
0520: 54 68 65 20 73 6f 63 6b 65 74 20 6f 6e 20 77 68 The socket on wh
0530: 69 63 68 20 77 65 20 74 61 6c 6b 20 74 6f 20 74 ich we talk to t
0540: 68 65 20 73 65 72 76 65 72 20 2a 2f 0a 0a 23 69 he server */..#i
0550: 66 64 65 66 20 5f 5f 4d 49 4e 47 57 33 32 5f 5f fdef __MINGW32__
0560: 0a 73 74 61 74 69 63 20 57 53 41 44 41 54 41 20 .static WSADATA
0570: 77 73 5f 69 6e 66 6f 3b 0a 23 65 6e 64 69 66 0a ws_info;.#endif.
0580: 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 73 5f .static void ws_
0590: 69 6e 69 74 28 29 7b 0a 23 69 66 64 65 66 20 5f init(){.#ifdef _
05a0: 5f 4d 49 4e 47 57 33 32 5f 5f 0a 20 20 69 66 20 _MINGW32__. if
05b0: 28 57 53 41 53 74 61 72 74 75 70 28 4d 41 4b 45 (WSAStartup(MAKE
05c0: 57 4f 52 44 28 31 2c 31 29 2c 20 26 77 73 5f 69 WORD(1,1), &ws_i
05d0: 6e 66 6f 29 20 21 3d 20 30 29 7b 0a 20 20 20 20 nfo) != 0){.
05e0: 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 63 61 fossil_panic("ca
05f0: 6e 27 74 20 69 6e 69 74 69 61 6c 69 7a 65 20 77 n't initialize w
0600: 69 6e 73 6f 63 6b 22 29 3b 0a 20 20 7d 0a 23 65 insock");. }.#e
0610: 6e 64 69 66 0a 7d 0a 0a 73 74 61 74 69 63 20 76 ndif.}..static v
0620: 6f 69 64 20 77 73 5f 63 6c 65 61 6e 75 70 28 29 oid ws_cleanup()
0630: 7b 0a 23 69 66 64 65 66 20 5f 5f 4d 49 4e 47 57 {.#ifdef __MINGW
0640: 33 32 5f 5f 0a 20 20 57 53 41 43 6c 65 61 6e 75 32__. WSACleanu
0650: 70 28 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f p();.#endif.}../
0660: 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 73 6f 63 6b *.** Open a sock
0670: 65 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f et connection to
0680: 20 74 68 65 20 73 65 72 76 65 72 2e 20 20 52 65 the server. Re
0690: 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73 turn 0 on succes
06a0: 73 20 61 6e 64 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 s and.** non-zer
06b0: 6f 20 69 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 o if an error oc
06c0: 63 75 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 curs..*/.static
06d0: 69 6e 74 20 68 74 74 70 5f 6f 70 65 6e 5f 73 6f int http_open_so
06e0: 63 6b 65 74 28 76 6f 69 64 29 7b 0a 20 20 73 74 cket(void){. st
06f0: 61 74 69 63 20 73 74 72 75 63 74 20 73 6f 63 6b atic struct sock
0700: 61 64 64 72 5f 69 6e 20 61 64 64 72 3b 20 20 2f addr_in addr; /
0710: 2a 20 54 68 65 20 73 65 72 76 65 72 20 61 64 64 * The server add
0720: 72 65 73 73 20 2a 2f 0a 20 20 73 74 61 74 69 63 ress */. static
0730: 20 69 6e 74 20 61 64 64 72 49 73 49 6e 69 74 20 int addrIsInit
0740: 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 = 0; /* Tr
0750: 75 65 20 6f 6e 63 65 20 61 64 64 72 20 69 73 20 ue once addr is
0760: 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 initialized */.
0770: 20 69 6e 74 20 73 3b 0a 20 20 0a 20 20 69 66 28 int s;. . if(
0780: 20 21 61 64 64 72 49 73 49 6e 69 74 20 29 7b 0a !addrIsInit ){.
0790: 20 20 20 20 77 73 5f 69 6e 69 74 28 29 3b 0a 0a ws_init();..
07a0: 20 20 20 20 61 64 64 72 2e 73 69 6e 5f 66 61 6d addr.sin_fam
07b0: 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 3b 0a 20 ily = AF_INET;.
07c0: 20 20 20 61 64 64 72 2e 73 69 6e 5f 70 6f 72 74 addr.sin_port
07d0: 20 3d 20 68 74 6f 6e 73 28 67 2e 75 72 6c 50 6f = htons(g.urlPo
07e0: 72 74 29 3b 0a 20 20 20 20 2a 28 69 6e 74 2a 29 rt);. *(int*)
07f0: 26 61 64 64 72 2e 73 69 6e 5f 61 64 64 72 20 3d &addr.sin_addr =
0800: 20 69 6e 65 74 5f 61 64 64 72 28 67 2e 75 72 6c inet_addr(g.url
0810: 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 2d Name);. if( -
0820: 31 20 3d 3d 20 2a 28 69 6e 74 2a 29 26 61 64 64 1 == *(int*)&add
0830: 72 2e 73 69 6e 5f 61 64 64 72 20 29 7b 0a 23 69 r.sin_addr ){.#i
0840: 66 6e 64 65 66 20 46 4f 53 53 49 4c 5f 53 54 41 fndef FOSSIL_STA
0850: 54 49 43 5f 4c 49 4e 4b 0a 20 20 20 20 20 20 73 TIC_LINK. s
0860: 74 72 75 63 74 20 68 6f 73 74 65 6e 74 20 2a 70 truct hostent *p
0870: 48 6f 73 74 3b 0a 20 20 20 20 20 20 70 48 6f 73 Host;. pHos
0880: 74 20 3d 20 67 65 74 68 6f 73 74 62 79 6e 61 6d t = gethostbynam
0890: 65 28 67 2e 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 e(g.urlName);.
08a0: 20 20 20 20 69 66 28 20 70 48 6f 73 74 21 3d 30 if( pHost!=0
08b0: 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 ){. memc
08c0: 70 79 28 26 61 64 64 72 2e 73 69 6e 5f 61 64 64 py(&addr.sin_add
08d0: 72 2c 70 48 6f 73 74 2d 3e 68 5f 61 64 64 72 5f r,pHost->h_addr_
08e0: 6c 69 73 74 5b 30 5d 2c 70 48 6f 73 74 2d 3e 68 list[0],pHost->h
08f0: 5f 6c 65 6e 67 74 68 29 3b 0a 20 20 20 20 20 20 _length);.
0900: 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 }else.#endif.
0910: 20 20 20 7b 0a 20 20 20 20 20 20 20 20 66 6f 73 {. fos
0920: 73 69 6c 5f 70 61 6e 69 63 28 22 63 61 6e 27 74 sil_panic("can't
0930: 20 72 65 73 6f 6c 76 65 20 68 6f 73 74 20 6e 61 resolve host na
0940: 6d 65 3a 20 25 73 5c 6e 22 2c 20 67 2e 75 72 6c me: %s\n", g.url
0950: 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 Name);. }.
0960: 20 20 20 7d 0a 20 20 20 20 61 64 64 72 49 73 49 }. addrIsI
0970: 6e 69 74 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a nit = 1;.. /*
0980: 20 53 65 74 20 74 68 65 20 47 6c 6f 62 61 6c 2e Set the Global.
0990: 7a 49 70 41 64 64 72 20 76 61 72 69 61 62 6c 65 zIpAddr variable
09a0: 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 77 to the server w
09b0: 65 20 61 72 65 20 74 61 6c 6b 69 6e 67 20 74 6f e are talking to
09c0: 2e 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 69 73 .. ** This is
09d0: 20 75 73 65 64 20 74 6f 20 70 6f 70 75 6c 61 74 used to populat
09e0: 65 20 74 68 65 20 69 70 61 64 64 72 20 63 6f 6c e the ipaddr col
09f0: 75 6d 6e 20 6f 66 20 74 68 65 20 72 63 76 66 72 umn of the rcvfr
0a00: 6f 6d 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a 2a om table,. **
0a10: 20 69 66 20 61 6e 79 20 66 69 6c 65 73 20 61 72 if any files ar
0a20: 65 20 72 65 63 65 69 76 65 64 20 66 72 6f 6d 20 e received from
0a30: 74 68 65 20 73 65 72 76 65 72 2e 0a 20 20 20 20 the server..
0a40: 2a 2f 0a 20 20 20 20 67 2e 7a 49 70 41 64 64 72 */. g.zIpAddr
0a50: 20 3d 20 6d 70 72 69 6e 74 66 28 22 25 73 22 2c = mprintf("%s",
0a60: 20 69 6e 65 74 5f 6e 74 6f 61 28 61 64 64 72 2e inet_ntoa(addr.
0a70: 73 69 6e 5f 61 64 64 72 29 29 3b 0a 20 20 7d 0a sin_addr));. }.
0a80: 20 20 73 20 3d 20 73 6f 63 6b 65 74 28 41 46 5f s = socket(AF_
0a90: 49 4e 45 54 2c 53 4f 43 4b 5f 53 54 52 45 41 4d INET,SOCK_STREAM
0aa0: 2c 30 29 3b 0a 20 20 69 66 28 20 73 3c 30 20 29 ,0);. if( s<0 )
0ab0: 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e {. fossil_pan
0ac0: 69 63 28 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 ic("cannot creat
0ad0: 65 20 61 20 73 6f 63 6b 65 74 22 29 3b 0a 20 20 e a socket");.
0ae0: 7d 0a 20 20 69 66 28 20 63 6f 6e 6e 65 63 74 28 }. if( connect(
0af0: 73 2c 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 s,(struct sockad
0b00: 64 72 2a 29 26 61 64 64 72 2c 73 69 7a 65 6f 66 dr*)&addr,sizeof
0b10: 28 61 64 64 72 29 29 3c 30 20 29 7b 0a 20 20 20 (addr))<0 ){.
0b20: 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 63 fossil_panic("c
0b30: 61 6e 6e 6f 74 20 63 6f 6e 6e 65 63 74 20 74 6f annot connect to
0b40: 20 68 6f 73 74 20 25 73 3a 25 64 22 2c 20 67 2e host %s:%d", g.
0b50: 75 72 6c 4e 61 6d 65 2c 20 67 2e 75 72 6c 50 6f urlName, g.urlPo
0b60: 72 74 29 3b 0a 20 20 7d 0a 20 20 70 53 6f 63 6b rt);. }. pSock
0b70: 65 74 20 3d 20 66 64 6f 70 65 6e 28 73 2c 22 72 et = fdopen(s,"r
0b80: 2b 22 29 3b 0a 23 69 66 6e 64 65 66 20 5f 5f 4d +");.#ifndef __M
0b90: 49 4e 47 57 33 32 5f 5f 0a 20 20 73 69 67 6e 61 INGW32__. signa
0ba0: 6c 28 53 49 47 50 49 50 45 2c 20 53 49 47 5f 49 l(SIGPIPE, SIG_I
0bb0: 47 4e 29 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 GN);.#endif. re
0bc0: 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a turn 0;.}../*.**
0bd0: 20 4d 61 6b 65 20 61 20 73 69 6e 67 6c 65 20 61 Make a single a
0be0: 74 74 65 6d 70 74 20 74 6f 20 74 61 6c 6b 20 74 ttempt to talk t
0bf0: 6f 20 74 68 65 20 73 65 72 76 65 72 2e 20 20 52 o the server. R
0c00: 65 74 75 72 6e 20 54 52 55 45 20 6f 6e 20 73 75 eturn TRUE on su
0c10: 63 63 65 73 73 0a 2a 2a 20 61 6e 64 20 46 41 4c ccess.** and FAL
0c20: 53 45 20 6f 6e 20 61 20 66 61 69 6c 75 72 65 2e SE on a failure.
0c30: 0a 2a 2a 0a 2a 2a 20 70 48 65 61 64 65 72 20 63 .**.** pHeader c
0c40: 6f 6e 74 61 69 6e 73 20 74 68 65 20 48 54 54 50 ontains the HTTP
0c50: 20 68 65 61 64 65 72 2e 20 20 70 50 61 79 6c 6f header. pPaylo
0c60: 61 64 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 ad contains the
0c70: 63 6f 6e 74 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 content..** The
0c80: 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 72 content of the r
0c90: 65 70 6c 79 20 69 73 20 77 72 69 74 74 65 6e 20 eply is written
0ca0: 69 6e 74 6f 20 70 52 65 70 6c 79 2e 20 20 70 52 into pReply. pR
0cb0: 65 70 6c 79 20 69 73 20 61 73 73 75 6d 65 64 0a eply is assumed.
0cc0: 2a 2a 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 ** to be uniniti
0cd0: 61 6c 69 7a 65 64 20 70 72 69 6f 72 20 74 6f 20 alized prior to
0ce0: 74 68 69 73 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a this call..**.**
0cf0: 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 If an error occ
0d00: 75 72 73 2c 20 74 68 69 73 20 72 6f 75 74 69 6e urs, this routin
0d10: 65 20 72 65 74 75 72 6e 20 66 61 6c 73 65 2c 20 e return false,
0d20: 72 65 73 65 74 73 20 70 52 65 70 6c 79 20 61 6e resets pReply an
0d30: 64 0a 2a 2a 20 63 6c 6f 73 65 73 20 74 68 65 20 d.** closes the
0d40: 70 65 72 73 69 73 74 65 6e 74 20 63 6f 6e 6e 65 persistent conne
0d50: 63 74 69 6f 6e 2c 20 69 66 20 61 6e 79 2e 0a 2a ction, if any..*
0d60: 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 68 74 74 /.static int htt
0d70: 70 5f 73 65 6e 64 5f 72 65 63 76 28 42 6c 6f 62 p_send_recv(Blob
0d80: 20 2a 70 48 65 61 64 65 72 2c 20 42 6c 6f 62 20 *pHeader, Blob
0d90: 2a 70 50 61 79 6c 6f 61 64 2c 20 42 6c 6f 62 20 *pPayload, Blob
0da0: 2a 70 52 65 70 6c 79 29 7b 0a 20 20 69 6e 74 20 *pReply){. int
0db0: 72 63 3b 0a 20 20 69 6e 74 20 63 6c 6f 73 65 43 rc;. int closeC
0dc0: 6f 6e 6e 65 63 74 69 6f 6e 3b 0a 20 20 69 6e 74 onnection;. int
0dd0: 20 69 4c 65 6e 67 74 68 3b 0a 20 20 69 6e 74 20 iLength;. int
0de0: 69 48 74 74 70 56 65 72 73 69 6f 6e 3b 0a 20 20 iHttpVersion;.
0df0: 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 int i;. int nRe
0e00: 61 64 3b 0a 20 20 63 68 61 72 20 7a 4c 69 6e 65 ad;. char zLine
0e10: 5b 32 30 30 30 5d 3b 0a 0a 20 20 69 66 28 20 70 [2000];.. if( p
0e20: 53 6f 63 6b 65 74 3d 3d 30 20 26 26 20 68 74 74 Socket==0 && htt
0e30: 70 5f 6f 70 65 6e 5f 73 6f 63 6b 65 74 28 29 20 p_open_socket()
0e40: 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b ){. return 0;
0e50: 0a 20 20 7d 0a 20 20 72 63 20 3d 20 66 77 72 69 . }. rc = fwri
0e60: 74 65 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 te(blob_buffer(p
0e70: 48 65 61 64 65 72 29 2c 20 31 2c 20 62 6c 6f 62 Header), 1, blob
0e80: 5f 73 69 7a 65 28 70 48 65 61 64 65 72 29 2c 20 _size(pHeader),
0e90: 70 53 6f 63 6b 65 74 29 3b 0a 20 20 69 66 28 20 pSocket);. if(
0ea0: 72 63 21 3d 62 6c 6f 62 5f 73 69 7a 65 28 70 48 rc!=blob_size(pH
0eb0: 65 61 64 65 72 29 20 29 20 67 6f 74 6f 20 77 72 eader) ) goto wr
0ec0: 69 74 65 5f 65 72 72 3b 0a 20 20 72 63 20 3d 20 ite_err;. rc =
0ed0: 66 77 72 69 74 65 28 62 6c 6f 62 5f 62 75 66 66 fwrite(blob_buff
0ee0: 65 72 28 70 50 61 79 6c 6f 61 64 29 2c 20 31 2c er(pPayload), 1,
0ef0: 20 62 6c 6f 62 5f 73 69 7a 65 28 70 50 61 79 6c blob_size(pPayl
0f00: 6f 61 64 29 2c 20 70 53 6f 63 6b 65 74 29 3b 0a oad), pSocket);.
0f10: 20 20 69 66 28 20 72 63 21 3d 62 6c 6f 62 5f 73 if( rc!=blob_s
0f20: 69 7a 65 28 70 50 61 79 6c 6f 61 64 29 20 29 20 ize(pPayload) )
0f30: 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 3b 0a goto write_err;.
0f40: 20 20 69 66 28 20 66 66 6c 75 73 68 28 70 53 6f if( fflush(pSo
0f50: 63 6b 65 74 29 20 29 20 67 6f 74 6f 20 77 72 69 cket) ) goto wri
0f60: 74 65 5f 65 72 72 3b 0a 20 20 69 66 28 20 66 67 te_err;. if( fg
0f70: 65 74 73 28 7a 4c 69 6e 65 2c 20 73 69 7a 65 6f ets(zLine, sizeo
0f80: 66 28 7a 4c 69 6e 65 29 2c 20 70 53 6f 63 6b 65 f(zLine), pSocke
0f90: 74 29 3d 3d 30 20 29 20 67 6f 74 6f 20 77 72 69 t)==0 ) goto wri
0fa0: 74 65 5f 65 72 72 3b 0a 20 20 69 66 28 20 73 73 te_err;. if( ss
0fb0: 63 61 6e 66 28 7a 4c 69 6e 65 2c 20 22 48 54 54 canf(zLine, "HTT
0fc0: 50 2f 31 2e 25 64 20 25 64 22 2c 20 26 69 48 74 P/1.%d %d", &iHt
0fd0: 74 70 56 65 72 73 69 6f 6e 2c 20 26 72 63 29 21 tpVersion, &rc)!
0fe0: 3d 32 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f =2 ) goto write_
0ff0: 65 72 72 3b 0a 20 20 69 66 28 20 72 63 21 3d 32 err;. if( rc!=2
1000: 30 30 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 00 ) goto write_
1010: 65 72 72 3b 0a 20 20 69 66 28 20 69 48 74 74 70 err;. if( iHttp
1020: 56 65 72 73 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 Version==0 ){.
1030: 20 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f closeConnectio
1040: 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a n = 1;. }else{.
1050: 20 20 20 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 closeConnect
1060: 69 6f 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 ion = 0;. }. i
1070: 4c 65 6e 67 74 68 20 3d 20 2d 31 3b 0a 20 20 77 Length = -1;. w
1080: 68 69 6c 65 28 20 66 67 65 74 73 28 7a 4c 69 6e hile( fgets(zLin
1090: 65 2c 20 73 69 7a 65 6f 66 28 7a 4c 69 6e 65 29 e, sizeof(zLine)
10a0: 2c 20 70 53 6f 63 6b 65 74 29 20 29 7b 0a 20 20 , pSocket) ){.
10b0: 20 20 66 6f 72 28 69 3d 30 3b 20 7a 4c 69 6e 65 for(i=0; zLine
10c0: 5b 69 5d 20 26 26 20 7a 4c 69 6e 65 5b 69 5d 21 [i] && zLine[i]!
10d0: 3d 27 5c 6e 27 20 26 26 20 7a 4c 69 6e 65 5b 69 ='\n' && zLine[i
10e0: 5d 21 3d 27 5c 72 27 3b 20 69 2b 2b 29 7b 7d 0a ]!='\r'; i++){}.
10f0: 20 20 20 20 69 66 28 20 69 3d 3d 30 20 29 20 62 if( i==0 ) b
1100: 72 65 61 6b 3b 0a 20 20 20 20 7a 4c 69 6e 65 5b reak;. zLine[
1110: 69 5d 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 i] = 0;. if(
1120: 73 74 72 6e 63 61 73 65 63 6d 70 28 7a 4c 69 6e strncasecmp(zLin
1130: 65 2c 20 22 63 6f 6e 74 65 6e 74 2d 6c 65 6e 67 e, "content-leng
1140: 74 68 3a 22 2c 31 35 29 3d 3d 30 20 29 7b 0a 20 th:",15)==0 ){.
1150: 20 20 20 20 20 69 4c 65 6e 67 74 68 20 3d 20 61 iLength = a
1160: 74 6f 69 28 26 7a 4c 69 6e 65 5b 31 36 5d 29 3b toi(&zLine[16]);
1170: 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 . }else if( s
1180: 74 72 6e 63 61 73 65 63 6d 70 28 7a 4c 69 6e 65 trncasecmp(zLine
1190: 2c 20 22 63 6f 6e 6e 65 63 74 69 6f 6e 3a 22 2c , "connection:",
11a0: 20 31 31 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 11)==0 ){.
11b0: 20 66 6f 72 28 69 3d 31 32 3b 20 69 73 73 70 61 for(i=12; isspa
11c0: 63 65 28 7a 4c 69 6e 65 5b 69 5d 29 3b 20 69 2b ce(zLine[i]); i+
11d0: 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20 7a +){}. if( z
11e0: 4c 69 6e 65 5b 69 5d 3d 3d 27 63 27 20 7c 7c 20 Line[i]=='c' ||
11f0: 7a 4c 69 6e 65 5b 69 5d 3d 3d 27 43 27 20 29 7b zLine[i]=='C' ){
1200: 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 43 6f . closeCo
1210: 6e 6e 65 63 74 69 6f 6e 20 3d 20 31 3b 20 20 20 nnection = 1;
1220: 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 3a 20 63 /* Connection: c
1230: 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 20 20 7d 65 lose */. }e
1240: 6c 73 65 20 69 66 28 20 7a 4c 69 6e 65 5b 69 5d lse if( zLine[i]
1250: 3d 3d 27 6b 27 20 7c 7c 20 7a 4c 69 6e 65 5b 69 =='k' || zLine[i
1260: 5d 3d 3d 27 4b 27 20 29 7b 0a 20 20 20 20 20 20 ]=='K' ){.
1270: 20 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f closeConnectio
1280: 6e 20 3d 20 30 3b 20 20 20 2f 2a 20 43 6f 6e 6e n = 0; /* Conn
1290: 65 63 74 69 6f 6e 3a 20 6b 65 65 70 2d 61 6c 69 ection: keep-ali
12a0: 76 65 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 ve */. }.
12b0: 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 69 4c }. }. if( iL
12c0: 65 6e 67 74 68 3c 30 20 29 20 67 6f 74 6f 20 77 ength<0 ) goto w
12d0: 72 69 74 65 5f 65 72 72 3b 0a 20 20 6e 52 65 61 rite_err;. nRea
12e0: 64 20 3d 20 62 6c 6f 62 5f 72 65 61 64 5f 66 72 d = blob_read_fr
12f0: 6f 6d 5f 63 68 61 6e 6e 65 6c 28 70 52 65 70 6c om_channel(pRepl
1300: 79 2c 20 70 53 6f 63 6b 65 74 2c 20 69 4c 65 6e y, pSocket, iLen
1310: 67 74 68 29 3b 0a 20 20 69 66 28 20 6e 52 65 61 gth);. if( nRea
1320: 64 21 3d 69 4c 65 6e 67 74 68 20 29 7b 0a 20 20 d!=iLength ){.
1330: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 70 52 65 blob_reset(pRe
1340: 70 6c 79 29 3b 0a 20 20 20 20 67 6f 74 6f 20 77 ply);. goto w
1350: 72 69 74 65 5f 65 72 72 3b 0a 20 20 7d 0a 20 20 rite_err;. }.
1360: 69 66 28 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 if( closeConnect
1370: 69 6f 6e 20 29 7b 0a 20 20 20 20 68 74 74 70 5f ion ){. http_
1380: 63 6c 6f 73 65 28 29 3b 0a 20 20 7d 0a 20 20 72 close();. }. r
1390: 65 74 75 72 6e 20 31 3b 20 20 0a 0a 77 72 69 74 eturn 1; ..writ
13a0: 65 5f 65 72 72 3a 0a 20 20 68 74 74 70 5f 63 6c e_err:. http_cl
13b0: 6f 73 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 ose();. return
13c0: 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69 67 6e 0;.}../*.** Sign
13d0: 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 6e 20 the content in
13e0: 70 53 65 6e 64 2c 20 63 6f 6d 70 72 65 73 73 20 pSend, compress
13f0: 69 74 2c 20 61 6e 64 20 73 65 6e 64 20 69 74 20 it, and send it
1400: 74 6f 20 74 68 65 20 73 65 72 76 65 72 0a 2a 2a to the server.**
1410: 20 76 69 61 20 48 54 54 50 2e 20 20 47 65 74 20 via HTTP. Get
1420: 61 20 72 65 70 6c 79 2c 20 75 6e 63 6f 6d 70 72 a reply, uncompr
1430: 65 73 73 20 74 68 65 20 72 65 70 6c 79 2c 20 61 ess the reply, a
1440: 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 70 nd store the rep
1450: 6c 79 0a 2a 2a 20 69 6e 20 70 52 65 63 76 2e 20 ly.** in pRecv.
1460: 20 70 52 65 63 76 20 69 73 20 61 73 73 75 6d 65 pRecv is assume
1470: 64 20 74 6f 20 62 65 20 75 6e 69 6e 69 74 69 61 d to be uninitia
1480: 6c 69 7a 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68 lized when.** th
1490: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 is routine is ca
14a0: 6c 6c 65 64 20 2d 20 74 68 69 73 20 72 6f 75 74 lled - this rout
14b0: 69 6e 65 20 77 69 6c 6c 20 69 6e 69 74 69 61 6c ine will initial
14c0: 69 7a 65 20 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 ize it..**.** Th
14d0: 65 20 73 65 72 76 65 72 20 61 64 64 72 65 73 73 e server address
14e0: 20 69 73 20 63 6f 6e 74 61 69 6e 20 69 6e 20 74 is contain in t
14f0: 68 65 20 22 67 22 20 67 6c 6f 62 61 6c 20 73 74 he "g" global st
1500: 72 75 63 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a ructure. The.**
1510: 20 75 72 6c 5f 70 61 72 73 65 28 29 20 72 6f 75 url_parse() rou
1520: 74 69 6e 65 20 73 68 6f 75 6c 64 20 68 61 76 65 tine should have
1530: 20 62 65 65 6e 20 63 61 6c 6c 65 64 20 70 72 69 been called pri
1540: 6f 72 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 or to this routi
1550: 6e 65 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 ne.** in order t
1560: 6f 20 66 69 6c 6c 20 74 68 69 73 20 73 74 72 75 o fill this stru
1570: 63 74 75 72 65 20 61 70 70 72 6f 70 72 69 61 74 cture appropriat
1580: 65 6c 79 2e 0a 2a 2f 0a 76 6f 69 64 20 68 74 74 ely..*/.void htt
1590: 70 5f 65 78 63 68 61 6e 67 65 28 42 6c 6f 62 20 p_exchange(Blob
15a0: 2a 70 53 65 6e 64 2c 20 42 6c 6f 62 20 2a 70 52 *pSend, Blob *pR
15b0: 65 63 76 29 7b 0a 20 20 42 6c 6f 62 20 6c 6f 67 ecv){. Blob log
15c0: 69 6e 2c 20 6e 6f 6e 63 65 2c 20 73 69 67 2c 20 in, nonce, sig,
15d0: 70 77 2c 20 70 61 79 6c 6f 61 64 2c 20 68 64 72 pw, payload, hdr
15e0: 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a ;. const char *
15f0: 7a 53 65 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 zSep;. int i;.
1600: 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 int cnt = 0;..
1610: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 6e 6f 6e 63 blob_zero(&nonc
1620: 65 29 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 e);. blob_zero(
1630: 26 70 77 29 3b 0a 20 20 73 68 61 31 73 75 6d 5f &pw);. sha1sum_
1640: 62 6c 6f 62 28 70 53 65 6e 64 2c 20 26 6e 6f 6e blob(pSend, &non
1650: 63 65 29 3b 0a 20 20 62 6c 6f 62 5f 63 6f 70 79 ce);. blob_copy
1660: 28 26 70 77 2c 20 26 6e 6f 6e 63 65 29 3b 0a 20 (&pw, &nonce);.
1670: 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 6c 6f 67 69 blob_zero(&logi
1680: 6e 29 3b 0a 20 20 69 66 28 20 67 2e 75 72 6c 55 n);. if( g.urlU
1690: 73 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 75 73 ser==0 ){. us
16a0: 65 72 5f 73 65 6c 65 63 74 28 29 3b 0a 20 20 20 er_select();.
16b0: 20 64 62 5f 62 6c 6f 62 28 26 70 77 2c 20 22 53 db_blob(&pw, "S
16c0: 45 4c 45 43 54 20 70 77 20 46 52 4f 4d 20 75 73 ELECT pw FROM us
16d0: 65 72 20 57 48 45 52 45 20 75 69 64 3d 25 64 22 er WHERE uid=%d"
16e0: 2c 20 67 2e 75 73 65 72 55 69 64 29 3b 0a 20 20 , g.userUid);.
16f0: 20 20 73 68 61 31 73 75 6d 5f 62 6c 6f 62 28 26 sha1sum_blob(&
1700: 70 77 2c 20 26 73 69 67 29 3b 0a 20 20 20 20 62 pw, &sig);. b
1710: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6c 6f 67 lob_appendf(&log
1720: 69 6e 2c 20 22 6c 6f 67 69 6e 20 25 73 20 25 62 in, "login %s %b
1730: 20 25 62 5c 6e 22 2c 20 67 2e 7a 4c 6f 67 69 6e %b\n", g.zLogin
1740: 2c 20 26 6e 6f 6e 63 65 2c 20 26 73 69 67 29 3b , &nonce, &sig);
1750: 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 . }else{. if
1760: 28 20 67 2e 75 72 6c 50 61 73 73 77 64 3d 3d 30 ( g.urlPasswd==0
1770: 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74 ){. if( st
1780: 72 63 6d 70 28 67 2e 75 72 6c 55 73 65 72 2c 22 rcmp(g.urlUser,"
1790: 61 6e 6f 6e 79 6d 6f 75 73 22 29 21 3d 30 20 29 anonymous")!=0 )
17a0: 7b 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a {. char *
17b0: 7a 50 72 6f 6d 70 74 20 3d 20 6d 70 72 69 6e 74 zPrompt = mprint
17c0: 66 28 22 70 61 73 73 77 6f 72 64 20 66 6f 72 20 f("password for
17d0: 25 73 3a 20 22 2c 20 67 2e 75 72 6c 55 73 65 72 %s: ", g.urlUser
17e0: 29 3b 0a 20 20 20 20 20 20 20 20 42 6c 6f 62 20 );. Blob
17f0: 78 3b 0a 20 20 20 20 20 20 20 20 70 72 6f 6d 70 x;. promp
1800: 74 5f 66 6f 72 5f 70 61 73 73 77 6f 72 64 28 7a t_for_password(z
1810: 50 72 6f 6d 70 74 2c 20 26 78 2c 20 30 29 3b 0a Prompt, &x, 0);.
1820: 20 20 20 20 20 20 20 20 66 72 65 65 28 7a 50 72 free(zPr
1830: 6f 6d 70 74 29 3b 0a 20 20 20 20 20 20 20 20 67 ompt);. g
1840: 2e 75 72 6c 50 61 73 73 77 64 20 3d 20 62 6c 6f .urlPasswd = blo
1850: 62 5f 73 74 72 28 26 78 29 3b 0a 20 20 20 20 20 b_str(&x);.
1860: 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 }else{.
1870: 67 2e 75 72 6c 50 61 73 73 77 64 20 3d 20 22 22 g.urlPasswd = ""
1880: 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a ;. }. }.
1890: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 28 blob_append(
18a0: 26 70 77 2c 20 67 2e 75 72 6c 50 61 73 73 77 64 &pw, g.urlPasswd
18b0: 2c 20 2d 31 29 3b 0a 20 20 20 20 2f 2a 20 70 72 , -1);. /* pr
18c0: 69 6e 74 66 28 22 70 72 65 73 69 67 3d 5b 25 73 intf("presig=[%s
18d0: 5d 5c 6e 22 2c 20 62 6c 6f 62 5f 73 74 72 28 26 ]\n", blob_str(&
18e0: 70 77 29 29 3b 20 2a 2f 0a 20 20 20 20 73 68 61 pw)); */. sha
18f0: 31 73 75 6d 5f 62 6c 6f 62 28 26 70 77 2c 20 26 1sum_blob(&pw, &
1900: 73 69 67 29 3b 0a 20 20 20 20 62 6c 6f 62 5f 61 sig);. blob_a
1910: 70 70 65 6e 64 66 28 26 6c 6f 67 69 6e 2c 20 22 ppendf(&login, "
1920: 6c 6f 67 69 6e 20 25 73 20 25 62 20 25 62 5c 6e login %s %b %b\n
1930: 22 2c 20 67 2e 75 72 6c 55 73 65 72 2c 20 26 6e ", g.urlUser, &n
1940: 6f 6e 63 65 2c 20 26 73 69 67 29 3b 0a 20 20 7d once, &sig);. }
1950: 20 20 20 20 20 20 20 20 0a 20 20 62 6c 6f 62 5f . blob_
1960: 72 65 73 65 74 28 26 6e 6f 6e 63 65 29 3b 0a 20 reset(&nonce);.
1970: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 70 77 29 blob_reset(&pw)
1980: 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 ;. blob_reset(&
1990: 73 69 67 29 3b 0a 20 20 69 66 28 20 67 2e 66 48 sig);. if( g.fH
19a0: 74 74 70 54 72 61 63 65 20 29 7b 0a 20 20 20 20 ttpTrace ){.
19b0: 70 61 79 6c 6f 61 64 20 3d 20 6c 6f 67 69 6e 3b payload = login;
19c0: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 . blob_append
19d0: 28 26 70 61 79 6c 6f 61 64 2c 20 62 6c 6f 62 5f (&payload, blob_
19e0: 62 75 66 66 65 72 28 70 53 65 6e 64 29 2c 20 62 buffer(pSend), b
19f0: 6c 6f 62 5f 73 69 7a 65 28 70 53 65 6e 64 29 29 lob_size(pSend))
1a00: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 ;. }else{. b
1a10: 6c 6f 62 5f 63 6f 6d 70 72 65 73 73 32 28 26 6c lob_compress2(&l
1a20: 6f 67 69 6e 2c 20 70 53 65 6e 64 2c 20 26 70 61 ogin, pSend, &pa
1a30: 79 6c 6f 61 64 29 3b 0a 20 20 20 20 62 6c 6f 62 yload);. blob
1a40: 5f 72 65 73 65 74 28 26 6c 6f 67 69 6e 29 3b 0a _reset(&login);.
1a50: 20 20 7d 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 }. blob_zero(
1a60: 26 68 64 72 29 3b 0a 20 20 69 20 3d 20 73 74 72 &hdr);. i = str
1a70: 6c 65 6e 28 67 2e 75 72 6c 50 61 74 68 29 3b 0a len(g.urlPath);.
1a80: 20 20 69 66 28 20 69 3e 30 20 26 26 20 67 2e 75 if( i>0 && g.u
1a90: 72 6c 50 61 74 68 5b 69 2d 31 5d 3d 3d 27 2f 27 rlPath[i-1]=='/'
1aa0: 20 29 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 ){. zSep = "
1ab0: 22 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 ";. }else{.
1ac0: 7a 53 65 70 20 3d 20 22 2f 22 3b 0a 20 20 7d 0a zSep = "/";. }.
1ad0: 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 blob_appendf(&
1ae0: 68 64 72 2c 20 22 50 4f 53 54 20 25 73 25 73 78 hdr, "POST %s%sx
1af0: 66 65 72 20 48 54 54 50 2f 31 2e 31 5c 72 5c 6e fer HTTP/1.1\r\n
1b00: 22 2c 20 67 2e 75 72 6c 50 61 74 68 2c 20 7a 53 ", g.urlPath, zS
1b10: 65 70 29 3b 0a 20 20 62 6c 6f 62 5f 61 70 70 65 ep);. blob_appe
1b20: 6e 64 66 28 26 68 64 72 2c 20 22 48 6f 73 74 3a ndf(&hdr, "Host:
1b30: 20 25 73 3a 25 64 5c 72 5c 6e 22 2c 20 67 2e 75 %s:%d\r\n", g.u
1b40: 72 6c 4e 61 6d 65 2c 20 67 2e 75 72 6c 50 6f 72 rlName, g.urlPor
1b50: 74 29 3b 0a 20 20 69 66 28 20 67 2e 66 48 74 74 t);. if( g.fHtt
1b60: 70 54 72 61 63 65 20 29 7b 0a 20 20 20 20 62 6c pTrace ){. bl
1b70: 6f 62 5f 61 70 70 65 6e 64 66 28 26 68 64 72 2c ob_appendf(&hdr,
1b80: 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 "Content-Type:
1b90: 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66 6f application/x-fo
1ba0: 73 73 69 6c 2d 64 65 62 75 67 5c 72 5c 6e 22 29 ssil-debug\r\n")
1bb0: 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 62 ;. }else{. b
1bc0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 68 64 72 lob_appendf(&hdr
1bd0: 2c 20 22 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a , "Content-Type:
1be0: 20 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 66 application/x-f
1bf0: 6f 73 73 69 6c 5c 72 5c 6e 22 29 3b 0a 20 20 7d ossil\r\n");. }
1c00: 0a 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 28 . blob_appendf(
1c10: 26 68 64 72 2c 20 22 43 6f 6e 74 65 6e 74 2d 4c &hdr, "Content-L
1c20: 65 6e 67 74 68 3a 20 25 64 5c 72 5c 6e 5c 72 5c ength: %d\r\n\r\
1c30: 6e 22 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 70 n", blob_size(&p
1c40: 61 79 6c 6f 61 64 29 29 3b 0a 0a 20 20 69 66 28 ayload));.. if(
1c50: 20 67 2e 66 48 74 74 70 54 72 61 63 65 20 29 7b g.fHttpTrace ){
1c60: 0a 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 72 61 . /* When tra
1c70: 63 69 6e 67 2c 20 77 72 69 74 65 20 74 68 65 20 cing, write the
1c80: 74 72 61 6e 73 6d 69 74 74 65 64 20 48 54 54 50 transmitted HTTP
1c90: 20 6d 65 73 73 61 67 65 20 62 6f 74 68 20 74 6f message both to
1ca0: 20 73 74 61 6e 64 61 72 64 0a 20 20 20 20 2a 2a standard. **
1cb0: 20 6f 75 74 70 75 74 20 61 6e 64 20 69 6e 74 6f output and into
1cc0: 20 61 20 66 69 6c 65 2e 20 20 54 68 65 20 66 69 a file. The fi
1cd0: 6c 65 20 63 61 6e 20 74 68 65 6e 20 62 65 20 75 le can then be u
1ce0: 73 65 64 20 74 6f 20 64 72 69 76 65 20 74 68 65 sed to drive the
1cf0: 0a 20 20 20 20 2a 2a 20 73 65 72 76 65 72 2d 73 . ** server-s
1d00: 69 64 65 20 6c 69 6b 65 20 74 68 69 73 3a 0a 20 ide like this:.
1d10: 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 **. **
1d20: 20 20 2e 2f 66 6f 73 73 69 6c 20 68 74 74 70 20 ./fossil http
1d30: 3c 68 74 74 70 2d 74 72 61 63 65 2d 31 2e 74 78 <http-trace-1.tx
1d40: 74 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 61 t. */. sta
1d50: 74 69 63 20 69 6e 74 20 74 72 61 63 65 43 6e 74 tic int traceCnt
1d60: 20 3d 20 30 3b 0a 20 20 20 20 63 68 61 72 20 2a = 0;. char *
1d70: 7a 4f 75 74 46 69 6c 65 3b 0a 20 20 20 20 46 49 zOutFile;. FI
1d80: 4c 45 20 2a 6f 75 74 3b 0a 20 20 20 20 74 72 61 LE *out;. tra
1d90: 63 65 43 6e 74 2b 2b 3b 0a 20 20 20 20 7a 4f 75 ceCnt++;. zOu
1da0: 74 46 69 6c 65 20 3d 20 6d 70 72 69 6e 74 66 28 tFile = mprintf(
1db0: 22 68 74 74 70 2d 74 72 61 63 65 2d 25 64 2e 74 "http-trace-%d.t
1dc0: 78 74 22 2c 20 74 72 61 63 65 43 6e 74 29 3b 0a xt", traceCnt);.
1dd0: 20 20 20 20 70 72 69 6e 74 66 28 22 48 54 54 50 printf("HTTP
1de0: 20 53 45 4e 44 3a 20 28 25 73 29 5c 6e 25 73 25 SEND: (%s)\n%s%
1df0: 73 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d s===============
1e00: 3d 3d 3d 3d 3d 3d 3d 3d 5c 6e 22 2c 20 0a 20 20 ========\n", .
1e10: 20 20 20 20 20 20 7a 4f 75 74 46 69 6c 65 2c 20 zOutFile,
1e20: 62 6c 6f 62 5f 73 74 72 28 26 68 64 72 29 2c 20 blob_str(&hdr),
1e30: 62 6c 6f 62 5f 73 74 72 28 26 70 61 79 6c 6f 61 blob_str(&payloa
1e40: 64 29 29 3b 0a 20 20 20 20 6f 75 74 20 3d 20 66 d));. out = f
1e50: 6f 70 65 6e 28 7a 4f 75 74 46 69 6c 65 2c 20 22 open(zOutFile, "
1e60: 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75 74 w");. if( out
1e70: 20 29 7b 0a 20 20 20 20 20 20 66 77 72 69 74 65 ){. fwrite
1e80: 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 68 64 (blob_buffer(&hd
1e90: 72 29 2c 20 31 2c 20 62 6c 6f 62 5f 73 69 7a 65 r), 1, blob_size
1ea0: 28 26 68 64 72 29 2c 20 6f 75 74 29 3b 0a 20 20 (&hdr), out);.
1eb0: 20 20 20 20 66 77 72 69 74 65 28 62 6c 6f 62 5f fwrite(blob_
1ec0: 62 75 66 66 65 72 28 26 70 61 79 6c 6f 61 64 29 buffer(&payload)
1ed0: 2c 20 31 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 26 , 1, blob_size(&
1ee0: 70 61 79 6c 6f 61 64 29 2c 20 6f 75 74 29 3b 0a payload), out);.
1ef0: 20 20 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 fclose(out
1f00: 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66 );. }. }. f
1f10: 6f 72 28 63 6e 74 3d 30 3b 20 63 6e 74 3c 32 3b or(cnt=0; cnt<2;
1f20: 20 63 6e 74 2b 2b 29 7b 0a 20 20 20 20 69 66 28 cnt++){. if(
1f30: 20 68 74 74 70 5f 73 65 6e 64 5f 72 65 63 76 28 http_send_recv(
1f40: 26 68 64 72 2c 20 26 70 61 79 6c 6f 61 64 2c 20 &hdr, &payload,
1f50: 70 52 65 63 76 29 20 29 20 62 72 65 61 6b 3b 0a pRecv) ) break;.
1f60: 20 20 7d 0a 20 20 69 66 28 20 63 6e 74 3e 3d 32 }. if( cnt>=2
1f70: 20 29 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 ){. fossil_p
1f80: 61 6e 69 63 28 22 63 6f 6e 6e 65 63 74 69 6f 6e anic("connection
1f90: 20 74 6f 20 73 65 72 76 65 72 20 66 61 69 6c 65 to server faile
1fa0: 64 22 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f d");. }. blob_
1fb0: 72 65 73 65 74 28 26 68 64 72 29 3b 0a 20 20 62 reset(&hdr);. b
1fc0: 6c 6f 62 5f 72 65 73 65 74 28 26 70 61 79 6c 6f lob_reset(&paylo
1fd0: 61 64 29 3b 0a 20 20 69 66 28 20 67 2e 66 48 74 ad);. if( g.fHt
1fe0: 74 70 54 72 61 63 65 20 29 7b 0a 20 20 20 20 70 tpTrace ){. p
1ff0: 72 69 6e 74 66 28 22 48 54 54 50 20 52 45 43 45 rintf("HTTP RECE
2000: 49 56 45 3a 5c 6e 25 73 5c 6e 3d 3d 3d 3d 3d 3d IVE:\n%s\n======
2010: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d ================
2020: 3d 5c 6e 22 2c 20 62 6c 6f 62 5f 73 74 72 28 70 =\n", blob_str(p
2030: 52 65 63 76 29 29 3b 0a 20 20 7d 65 6c 73 65 7b Recv));. }else{
2040: 0a 20 20 20 20 62 6c 6f 62 5f 75 6e 63 6f 6d 70 . blob_uncomp
2050: 72 65 73 73 28 70 52 65 63 76 2c 20 70 52 65 63 ress(pRecv, pRec
2060: 76 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a v);. }.}.../*.*
2070: 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 * Make sure the
2080: 73 6f 63 6b 65 74 20 74 6f 20 74 68 65 20 48 54 socket to the HT
2090: 54 50 20 73 65 72 76 65 72 20 69 73 20 63 6c 6f TP server is clo
20a0: 73 65 64 20 0a 2a 2f 0a 76 6f 69 64 20 68 74 74 sed .*/.void htt
20b0: 70 5f 63 6c 6f 73 65 28 76 6f 69 64 29 7b 0a 20 p_close(void){.
20c0: 20 69 66 28 20 70 53 6f 63 6b 65 74 20 29 7b 0a if( pSocket ){.
20d0: 20 20 20 20 66 63 6c 6f 73 65 28 70 53 6f 63 6b fclose(pSock
20e0: 65 74 29 3b 0a 20 20 20 20 70 53 6f 63 6b 65 74 et);. pSocket
20f0: 20 3d 20 30 3b 0a 20 20 20 20 77 73 5f 63 6c 65 = 0;. ws_cle
2100: 61 6e 75 70 28 29 3b 0a 20 20 7d 0a 7d 0a anup();. }.}.