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 0a 2f 2a 0a 2a inet/in.h>../*.*
0460: 2a 20 50 65 72 73 69 73 74 65 6e 74 20 69 6e 66 * Persistent inf
0470: 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 ormation about t
0480: 68 65 20 48 54 54 50 20 63 6f 6e 6e 65 63 74 69 he HTTP connecti
0490: 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46 49 on..*/.static FI
04a0: 4c 45 20 2a 70 53 6f 63 6b 65 74 20 3d 20 30 3b LE *pSocket = 0;
04b0: 20 20 20 2f 2a 20 54 68 65 20 73 6f 63 6b 65 74 /* The socket
04c0: 20 6f 6e 20 77 68 69 63 68 20 77 65 20 74 61 6c on which we tal
04d0: 6b 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 20 k to the server
04e0: 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 */../*.** Open a
04f0: 20 73 6f 63 6b 65 74 20 63 6f 6e 6e 65 63 74 69 socket connecti
0500: 6f 6e 20 74 6f 20 74 68 65 20 73 65 72 76 65 72 on to the server
0510: 2e 20 20 52 65 74 75 72 6e 20 30 20 6f 6e 20 73 . Return 0 on s
0520: 75 63 63 65 73 73 20 61 6e 64 0a 2a 2a 20 6e 6f uccess and.** no
0530: 6e 2d 7a 65 72 6f 20 69 66 20 61 6e 20 65 72 72 n-zero if an err
0540: 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74 or occurs..*/.st
0550: 61 74 69 63 20 69 6e 74 20 68 74 74 70 5f 6f 70 atic int http_op
0560: 65 6e 5f 73 6f 63 6b 65 74 28 76 6f 69 64 29 7b en_socket(void){
0570: 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63 74 . static struct
0580: 20 73 6f 63 6b 61 64 64 72 5f 69 6e 20 61 64 64 sockaddr_in add
0590: 72 3b 20 20 2f 2a 20 54 68 65 20 73 65 72 76 65 r; /* The serve
05a0: 72 20 61 64 64 72 65 73 73 20 2a 2f 0a 20 20 73 r address */. s
05b0: 74 61 74 69 63 20 69 6e 74 20 61 64 64 72 49 73 tatic int addrIs
05c0: 49 6e 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20 Init = 0;
05d0: 2f 2a 20 54 72 75 65 20 6f 6e 63 65 20 61 64 64 /* True once add
05e0: 72 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 r is initialized
05f0: 20 2a 2f 0a 20 20 69 6e 74 20 73 3b 0a 0a 20 20 */. int s;..
0600: 69 66 28 20 21 61 64 64 72 49 73 49 6e 69 74 20 if( !addrIsInit
0610: 29 7b 0a 20 20 20 20 61 64 64 72 2e 73 69 6e 5f ){. addr.sin_
0620: 66 61 6d 69 6c 79 20 3d 20 41 46 5f 49 4e 45 54 family = AF_INET
0630: 3b 0a 20 20 20 20 61 64 64 72 2e 73 69 6e 5f 70 ;. addr.sin_p
0640: 6f 72 74 20 3d 20 68 74 6f 6e 73 28 67 2e 75 72 ort = htons(g.ur
0650: 6c 50 6f 72 74 29 3b 0a 20 20 20 20 2a 28 69 6e lPort);. *(in
0660: 74 2a 29 26 61 64 64 72 2e 73 69 6e 5f 61 64 64 t*)&addr.sin_add
0670: 72 20 3d 20 69 6e 65 74 5f 61 64 64 72 28 67 2e r = inet_addr(g.
0680: 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 urlName);. if
0690: 28 20 2d 31 20 3d 3d 20 2a 28 69 6e 74 2a 29 26 ( -1 == *(int*)&
06a0: 61 64 64 72 2e 73 69 6e 5f 61 64 64 72 20 29 7b addr.sin_addr ){
06b0: 0a 23 69 66 6e 64 65 66 20 46 4f 53 53 49 4c 5f .#ifndef FOSSIL_
06c0: 53 54 41 54 49 43 5f 4c 49 4e 4b 0a 20 20 20 20 STATIC_LINK.
06d0: 20 20 73 74 72 75 63 74 20 68 6f 73 74 65 6e 74 struct hostent
06e0: 20 2a 70 48 6f 73 74 3b 0a 20 20 20 20 20 20 70 *pHost;. p
06f0: 48 6f 73 74 20 3d 20 67 65 74 68 6f 73 74 62 79 Host = gethostby
0700: 6e 61 6d 65 28 67 2e 75 72 6c 4e 61 6d 65 29 3b name(g.urlName);
0710: 0a 20 20 20 20 20 20 69 66 28 20 70 48 6f 73 74 . if( pHost
0720: 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6d !=0 ){. m
0730: 65 6d 63 70 79 28 26 61 64 64 72 2e 73 69 6e 5f emcpy(&addr.sin_
0740: 61 64 64 72 2c 70 48 6f 73 74 2d 3e 68 5f 61 64 addr,pHost->h_ad
0750: 64 72 5f 6c 69 73 74 5b 30 5d 2c 70 48 6f 73 74 dr_list[0],pHost
0760: 2d 3e 68 5f 6c 65 6e 67 74 68 29 3b 0a 20 20 20 ->h_length);.
0770: 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a }else.#endif.
0780: 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 {.
0790: 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 63 61 fossil_panic("ca
07a0: 6e 27 74 20 72 65 73 6f 6c 76 65 20 68 6f 73 74 n't resolve host
07b0: 20 6e 61 6d 65 3a 20 25 73 5c 6e 22 2c 20 67 2e name: %s\n", g.
07c0: 75 72 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 urlName);.
07d0: 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 }. }. addr
07e0: 49 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a IsInit = 1;. }.
07f0: 20 20 73 20 3d 20 73 6f 63 6b 65 74 28 41 46 5f s = socket(AF_
0800: 49 4e 45 54 2c 53 4f 43 4b 5f 53 54 52 45 41 4d INET,SOCK_STREAM
0810: 2c 30 29 3b 0a 20 20 69 66 28 20 73 3c 30 20 29 ,0);. if( s<0 )
0820: 7b 0a 20 20 20 20 66 6f 73 73 69 6c 5f 70 61 6e {. fossil_pan
0830: 69 63 28 22 63 61 6e 6e 6f 74 20 63 72 65 61 74 ic("cannot creat
0840: 65 20 61 20 73 6f 63 6b 65 74 22 29 3b 0a 20 20 e a socket");.
0850: 7d 0a 20 20 69 66 28 20 63 6f 6e 6e 65 63 74 28 }. if( connect(
0860: 73 2c 28 73 74 72 75 63 74 20 73 6f 63 6b 61 64 s,(struct sockad
0870: 64 72 2a 29 26 61 64 64 72 2c 73 69 7a 65 6f 66 dr*)&addr,sizeof
0880: 28 61 64 64 72 29 29 3c 30 20 29 7b 0a 20 20 20 (addr))<0 ){.
0890: 20 66 6f 73 73 69 6c 5f 70 61 6e 69 63 28 22 63 fossil_panic("c
08a0: 61 6e 6e 6f 74 20 63 6f 6e 6e 65 63 74 20 74 6f annot connect to
08b0: 20 68 6f 73 74 20 25 73 3a 25 64 22 2c 20 67 2e host %s:%d", g.
08c0: 75 72 6c 4e 61 6d 65 2c 20 67 2e 75 72 6c 50 6f urlName, g.urlPo
08d0: 72 74 29 3b 0a 20 20 7d 0a 20 20 70 53 6f 63 6b rt);. }. pSock
08e0: 65 74 20 3d 20 66 64 6f 70 65 6e 28 73 2c 22 72 et = fdopen(s,"r
08f0: 2b 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b +");. return 0;
0900: 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 .}../*.** Make a
0910: 20 73 69 6e 67 6c 65 20 61 74 74 65 6d 70 74 20 single attempt
0920: 74 6f 20 74 61 6c 6b 20 74 6f 20 74 68 65 20 73 to talk to the s
0930: 65 72 76 65 72 2e 20 20 52 65 74 75 72 6e 20 54 erver. Return T
0940: 52 55 45 20 6f 6e 20 73 75 63 63 65 73 73 0a 2a RUE on success.*
0950: 2a 20 61 6e 64 20 46 41 4c 53 45 20 6f 6e 20 61 * and FALSE on a
0960: 20 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 failure..**.**
0970: 70 48 65 61 64 65 72 20 63 6f 6e 74 61 69 6e 73 pHeader contains
0980: 20 74 68 65 20 48 54 54 50 20 68 65 61 64 65 72 the HTTP header
0990: 2e 20 20 70 50 61 79 6c 6f 61 64 20 63 6f 6e 74 . pPayload cont
09a0: 61 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 6e 74 ains the content
09b0: 2e 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74 ..** The content
09c0: 20 6f 66 20 74 68 65 20 72 65 70 6c 79 20 69 73 of the reply is
09d0: 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 70 52 written into pR
09e0: 65 70 6c 79 2e 20 20 70 52 65 70 6c 79 20 69 73 eply. pReply is
09f0: 20 61 73 73 75 6d 65 64 0a 2a 2a 20 74 6f 20 62 assumed.** to b
0a00: 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 e uninitialized
0a10: 70 72 69 6f 72 20 74 6f 20 74 68 69 73 20 63 61 prior to this ca
0a20: 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 ll..**.** If an
0a30: 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68 error occurs, th
0a40: 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 is routine retur
0a50: 6e 20 66 61 6c 73 65 2c 20 72 65 73 65 74 73 20 n false, resets
0a60: 70 52 65 70 6c 79 20 61 6e 64 0a 2a 2a 20 63 6c pReply and.** cl
0a70: 6f 73 65 73 20 74 68 65 20 70 65 72 73 69 73 74 oses the persist
0a80: 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 ent connection,
0a90: 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 61 74 69 if any..*/.stati
0aa0: 63 20 69 6e 74 20 68 74 74 70 5f 73 65 6e 64 5f c int http_send_
0ab0: 72 65 63 76 28 42 6c 6f 62 20 2a 70 48 65 61 64 recv(Blob *pHead
0ac0: 65 72 2c 20 42 6c 6f 62 20 2a 70 50 61 79 6c 6f er, Blob *pPaylo
0ad0: 61 64 2c 20 42 6c 6f 62 20 2a 70 52 65 70 6c 79 ad, Blob *pReply
0ae0: 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 ){. int rc;. i
0af0: 6e 74 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 nt closeConnecti
0b00: 6f 6e 3b 0a 20 20 69 6e 74 20 69 4c 65 6e 67 74 on;. int iLengt
0b10: 68 3b 0a 20 20 69 6e 74 20 69 48 74 74 70 56 65 h;. int iHttpVe
0b20: 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a rsion;. int i;.
0b30: 20 20 69 6e 74 20 6e 52 65 61 64 3b 0a 20 20 63 int nRead;. c
0b40: 68 61 72 20 7a 4c 69 6e 65 5b 32 30 30 30 5d 3b har zLine[2000];
0b50: 0a 0a 20 20 69 66 28 20 70 53 6f 63 6b 65 74 3d .. if( pSocket=
0b60: 3d 30 20 26 26 20 68 74 74 70 5f 6f 70 65 6e 5f =0 && http_open_
0b70: 73 6f 63 6b 65 74 28 29 20 29 7b 0a 20 20 20 20 socket() ){.
0b80: 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 return 0;. }.
0b90: 72 63 20 3d 20 66 77 72 69 74 65 28 62 6c 6f 62 rc = fwrite(blob
0ba0: 5f 62 75 66 66 65 72 28 70 48 65 61 64 65 72 29 _buffer(pHeader)
0bb0: 2c 20 31 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 70 , 1, blob_size(p
0bc0: 48 65 61 64 65 72 29 2c 20 70 53 6f 63 6b 65 74 Header), pSocket
0bd0: 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 62 6c 6f );. if( rc!=blo
0be0: 62 5f 73 69 7a 65 28 70 48 65 61 64 65 72 29 20 b_size(pHeader)
0bf0: 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 ) goto write_err
0c00: 3b 0a 20 20 72 63 20 3d 20 66 77 72 69 74 65 28 ;. rc = fwrite(
0c10: 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 50 61 79 blob_buffer(pPay
0c20: 6c 6f 61 64 29 2c 20 31 2c 20 62 6c 6f 62 5f 73 load), 1, blob_s
0c30: 69 7a 65 28 70 50 61 79 6c 6f 61 64 29 2c 20 70 ize(pPayload), p
0c40: 53 6f 63 6b 65 74 29 3b 0a 20 20 69 66 28 20 72 Socket);. if( r
0c50: 63 21 3d 62 6c 6f 62 5f 73 69 7a 65 28 70 50 61 c!=blob_size(pPa
0c60: 79 6c 6f 61 64 29 20 29 20 67 6f 74 6f 20 77 72 yload) ) goto wr
0c70: 69 74 65 5f 65 72 72 3b 0a 20 20 69 66 28 20 66 ite_err;. if( f
0c80: 66 6c 75 73 68 28 70 53 6f 63 6b 65 74 29 20 29 flush(pSocket) )
0c90: 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 3b goto write_err;
0ca0: 0a 20 20 69 66 28 20 66 67 65 74 73 28 7a 4c 69 . if( fgets(zLi
0cb0: 6e 65 2c 20 73 69 7a 65 6f 66 28 7a 4c 69 6e 65 ne, sizeof(zLine
0cc0: 29 2c 20 70 53 6f 63 6b 65 74 29 3d 3d 30 20 29 ), pSocket)==0 )
0cd0: 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 72 3b goto write_err;
0ce0: 0a 20 20 69 66 28 20 73 73 63 61 6e 66 28 7a 4c . if( sscanf(zL
0cf0: 69 6e 65 2c 20 22 48 54 54 50 2f 31 2e 25 64 20 ine, "HTTP/1.%d
0d00: 25 64 22 2c 20 26 69 48 74 74 70 56 65 72 73 69 %d", &iHttpVersi
0d10: 6f 6e 2c 20 26 72 63 29 21 3d 32 20 29 20 67 6f on, &rc)!=2 ) go
0d20: 74 6f 20 77 72 69 74 65 5f 65 72 72 3b 0a 20 20 to write_err;.
0d30: 69 66 28 20 72 63 21 3d 32 30 30 20 29 20 67 6f if( rc!=200 ) go
0d40: 74 6f 20 77 72 69 74 65 5f 65 72 72 3b 0a 20 20 to write_err;.
0d50: 69 66 28 20 69 48 74 74 70 56 65 72 73 69 6f 6e if( iHttpVersion
0d60: 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6c 6f 73 65 ==0 ){. close
0d70: 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 31 3b 0a Connection = 1;.
0d80: 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 6c 6f }else{. clo
0d90: 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 seConnection = 0
0da0: 3b 0a 20 20 7d 0a 20 20 69 4c 65 6e 67 74 68 20 ;. }. iLength
0db0: 3d 20 2d 31 3b 0a 20 20 77 68 69 6c 65 28 20 66 = -1;. while( f
0dc0: 67 65 74 73 28 7a 4c 69 6e 65 2c 20 73 69 7a 65 gets(zLine, size
0dd0: 6f 66 28 7a 4c 69 6e 65 29 2c 20 70 53 6f 63 6b of(zLine), pSock
0de0: 65 74 29 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 et) ){. for(i
0df0: 3d 30 3b 20 7a 4c 69 6e 65 5b 69 5d 20 26 26 20 =0; zLine[i] &&
0e00: 7a 4c 69 6e 65 5b 69 5d 21 3d 27 5c 6e 27 20 26 zLine[i]!='\n' &
0e10: 26 20 7a 4c 69 6e 65 5b 69 5d 21 3d 27 5c 72 27 & zLine[i]!='\r'
0e20: 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 69 66 28 ; i++){}. if(
0e30: 20 69 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 i==0 ) break;.
0e40: 20 20 20 7a 4c 69 6e 65 5b 69 5d 20 3d 20 30 3b zLine[i] = 0;
0e50: 0a 20 20 20 20 69 66 28 20 73 74 72 6e 63 61 73 . if( strncas
0e60: 65 63 6d 70 28 7a 4c 69 6e 65 2c 20 22 63 6f 6e ecmp(zLine, "con
0e70: 74 65 6e 74 2d 6c 65 6e 67 74 68 3a 22 2c 31 35 tent-length:",15
0e80: 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 4c )==0 ){. iL
0e90: 65 6e 67 74 68 20 3d 20 61 74 6f 69 28 26 7a 4c ength = atoi(&zL
0ea0: 69 6e 65 5b 31 36 5d 29 3b 0a 20 20 20 20 7d 65 ine[16]);. }e
0eb0: 6c 73 65 20 69 66 28 20 73 74 72 6e 63 61 73 65 lse if( strncase
0ec0: 63 6d 70 28 7a 4c 69 6e 65 2c 20 22 63 6f 6e 6e cmp(zLine, "conn
0ed0: 65 63 74 69 6f 6e 3a 22 2c 20 31 31 29 3d 3d 30 ection:", 11)==0
0ee0: 20 29 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d ){. for(i=
0ef0: 31 32 3b 20 69 73 73 70 61 63 65 28 7a 4c 69 6e 12; isspace(zLin
0f00: 65 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 e[i]); i++){}.
0f10: 20 20 20 20 69 66 28 20 7a 4c 69 6e 65 5b 69 5d if( zLine[i]
0f20: 3d 3d 27 63 27 20 7c 7c 20 7a 4c 69 6e 65 5b 69 =='c' || zLine[i
0f30: 5d 3d 3d 27 43 27 20 29 7b 0a 20 20 20 20 20 20 ]=='C' ){.
0f40: 20 20 63 6c 6f 73 65 43 6f 6e 6e 65 63 74 69 6f closeConnectio
0f50: 6e 20 3d 20 31 3b 20 20 20 2f 2a 20 43 6f 6e 6e n = 1; /* Conn
0f60: 65 63 74 69 6f 6e 3a 20 63 6c 6f 73 65 20 2a 2f ection: close */
0f70: 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 . }else if(
0f80: 20 7a 4c 69 6e 65 5b 69 5d 3d 3d 27 6b 27 20 7c zLine[i]=='k' |
0f90: 7c 20 7a 4c 69 6e 65 5b 69 5d 3d 3d 27 4b 27 20 | zLine[i]=='K'
0fa0: 29 7b 0a 20 20 20 20 20 20 20 20 63 6c 6f 73 65 ){. close
0fb0: 43 6f 6e 6e 65 63 74 69 6f 6e 20 3d 20 30 3b 20 Connection = 0;
0fc0: 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 3a /* Connection:
0fd0: 20 6b 65 65 70 2d 61 6c 69 76 65 20 2a 2f 0a 20 keep-alive */.
0fe0: 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d }. }. }
0ff0: 0a 20 20 69 66 28 20 69 4c 65 6e 67 74 68 3c 30 . if( iLength<0
1000: 20 29 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 ) goto write_er
1010: 72 3b 0a 20 20 6e 52 65 61 64 20 3d 20 62 6c 6f r;. nRead = blo
1020: 62 5f 72 65 61 64 5f 66 72 6f 6d 5f 63 68 61 6e b_read_from_chan
1030: 6e 65 6c 28 70 52 65 70 6c 79 2c 20 70 53 6f 63 nel(pReply, pSoc
1040: 6b 65 74 2c 20 69 4c 65 6e 67 74 68 29 3b 0a 20 ket, iLength);.
1050: 20 69 66 28 20 6e 52 65 61 64 21 3d 69 4c 65 6e if( nRead!=iLen
1060: 67 74 68 20 29 7b 0a 20 20 20 20 62 6c 6f 62 5f gth ){. blob_
1070: 72 65 73 65 74 28 70 52 65 70 6c 79 29 3b 0a 20 reset(pReply);.
1080: 20 20 20 67 6f 74 6f 20 77 72 69 74 65 5f 65 72 goto write_er
1090: 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 63 6c 6f r;. }. if( clo
10a0: 73 65 43 6f 6e 6e 65 63 74 69 6f 6e 20 29 7b 0a seConnection ){.
10b0: 20 20 20 20 68 74 74 70 5f 63 6c 6f 73 65 28 29 http_close()
10c0: 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 ;. }. return 1
10d0: 3b 20 20 0a 0a 77 72 69 74 65 5f 65 72 72 3a 0a ; ..write_err:.
10e0: 20 20 68 74 74 70 5f 63 6c 6f 73 65 28 29 3b 0a http_close();.
10f0: 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f return 0;.}../
1100: 2a 0a 2a 2a 20 53 69 67 6e 20 74 68 65 20 63 6f *.** Sign the co
1110: 6e 74 65 6e 74 20 69 6e 20 70 53 65 6e 64 2c 20 ntent in pSend,
1120: 63 6f 6d 70 72 65 73 73 20 69 74 2c 20 61 6e 64 compress it, and
1130: 20 73 65 6e 64 20 69 74 20 74 6f 20 74 68 65 20 send it to the
1140: 73 65 72 76 65 72 0a 2a 2a 20 76 69 61 20 48 54 server.** via HT
1150: 54 50 2e 20 20 47 65 74 20 61 20 72 65 70 6c 79 TP. Get a reply
1160: 2c 20 75 6e 63 6f 6d 70 72 65 73 73 20 74 68 65 , uncompress the
1170: 20 72 65 70 6c 79 2c 20 61 6e 64 20 73 74 6f 72 reply, and stor
1180: 65 20 74 68 65 20 72 65 70 6c 79 0a 2a 2a 20 69 e the reply.** i
1190: 6e 20 70 52 65 63 76 2e 20 20 70 52 65 63 76 20 n pRecv. pRecv
11a0: 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65 is assumed to be
11b0: 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 77 uninitialized w
11c0: 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 hen.** this rout
11d0: 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 2d 20 ine is called -
11e0: 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c this routine wil
11f0: 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 74 2e l initialize it.
1200: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 72 76 65 .**.** The serve
1210: 72 20 61 64 64 72 65 73 73 20 69 73 20 63 6f 6e r address is con
1220: 74 61 69 6e 20 69 6e 20 74 68 65 20 22 67 22 20 tain in the "g"
1230: 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72 65 global structure
1240: 2e 20 20 54 68 65 0a 2a 2a 20 75 72 6c 5f 70 61 . The.** url_pa
1250: 72 73 65 28 29 20 72 6f 75 74 69 6e 65 20 73 68 rse() routine sh
1260: 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63 ould have been c
1270: 61 6c 6c 65 64 20 70 72 69 6f 72 20 74 6f 20 74 alled prior to t
1280: 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69 his routine.** i
1290: 6e 20 6f 72 64 65 72 20 74 6f 20 66 69 6c 6c 20 n order to fill
12a0: 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 61 this structure a
12b0: 70 70 72 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f ppropriately..*/
12c0: 0a 76 6f 69 64 20 68 74 74 70 5f 65 78 63 68 61 .void http_excha
12d0: 6e 67 65 28 42 6c 6f 62 20 2a 70 53 65 6e 64 2c nge(Blob *pSend,
12e0: 20 42 6c 6f 62 20 2a 70 52 65 63 76 29 7b 0a 20 Blob *pRecv){.
12f0: 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 6f const char *zLo
1300: 67 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 gin;
1310: 20 20 2f 2a 20 55 73 65 72 6e 61 6d 65 20 74 6f /* Username to
1320: 20 73 65 6e 64 20 74 6f 20 73 65 72 76 65 72 20 send to server
1330: 2a 2f 20 0a 20 20 42 6c 6f 62 20 6c 6f 67 69 6e */ . Blob login
1340: 2c 20 6e 6f 6e 63 65 2c 20 73 69 67 2c 20 70 77 , nonce, sig, pw
1350: 2c 20 70 61 79 6c 6f 61 64 2c 20 68 64 72 3b 0a , payload, hdr;.
1360: 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 const char *zS
1370: 65 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 ep;. int i;. i
1380: 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 0a 20 20 62 nt cnt = 0;.. b
1390: 6c 6f 62 5f 7a 65 72 6f 28 26 6e 6f 6e 63 65 29 lob_zero(&nonce)
13a0: 3b 0a 20 20 62 6c 6f 62 5f 7a 65 72 6f 28 26 70 ;. blob_zero(&p
13b0: 77 29 3b 0a 20 20 64 62 5f 62 6c 6f 62 28 26 6e w);. db_blob(&n
13c0: 6f 6e 63 65 2c 20 22 53 45 4c 45 43 54 20 68 65 once, "SELECT he
13d0: 78 28 72 61 6e 64 6f 6d 62 6c 6f 62 28 32 30 29 x(randomblob(20)
13e0: 29 22 29 3b 0a 20 20 62 6c 6f 62 5f 63 6f 70 79 )");. blob_copy
13f0: 28 26 70 77 2c 20 26 6e 6f 6e 63 65 29 3b 0a 0a (&pw, &nonce);..
1400: 20 20 69 66 28 20 67 2e 75 72 6c 55 73 65 72 6e if( g.urlUsern
1410: 61 6d 65 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e ame ){. /* In
1420: 20 74 68 69 73 20 63 61 73 65 2c 20 61 20 75 73 this case, a us
1430: 65 72 6e 61 6d 65 20 61 6e 64 20 6f 70 74 69 6f ername and optio
1440: 6e 61 6c 6c 79 20 61 20 70 61 73 73 77 6f 72 64 nally a password
1450: 20 77 65 72 65 20 73 70 65 63 69 66 69 65 64 20 were specified
1460: 0a 20 20 20 20 2a 2a 20 61 73 20 70 61 72 74 20 . ** as part
1470: 6f 66 20 74 68 65 20 55 52 49 20 74 6f 20 63 6f of the URI to co
1480: 6e 74 61 63 74 2e 20 54 68 69 73 20 6f 76 65 72 ntact. This over
1490: 72 69 64 65 73 20 74 68 65 20 64 65 66 61 75 6c rides the defaul
14a0: 74 20 75 73 65 72 20 0a 20 20 20 20 2a 2a 20 61 t user . ** a
14b0: 6e 64 20 2d 75 73 65 72 20 6f 70 74 69 6f 6e 20 nd -user option
14c0: 28 69 66 20 61 6e 79 29 2e 20 49 66 20 6e 6f 20 (if any). If no
14d0: 70 61 73 73 77 6f 72 64 20 77 61 73 20 73 70 65 password was spe
14e0: 63 69 66 69 65 64 20 61 73 20 70 61 72 74 20 0a cified as part .
14f0: 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 55 52 ** of the UR
1500: 49 2c 20 75 73 65 20 61 6e 20 65 6d 70 74 79 20 I, use an empty
1510: 73 74 72 69 6e 67 20 28 22 22 29 20 61 73 20 74 string ("") as t
1520: 68 65 20 70 61 73 73 77 6f 72 64 2e 0a 20 20 20 he password..
1530: 20 2a 2f 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 */. blob_app
1540: 65 6e 64 28 26 70 77 2c 20 67 2e 75 72 6c 50 61 end(&pw, g.urlPa
1550: 73 73 77 6f 72 64 20 3f 20 67 2e 75 72 6c 50 61 ssword ? g.urlPa
1560: 73 73 77 6f 72 64 20 3a 20 22 22 2c 20 2d 31 29 ssword : "", -1)
1570: 3b 0a 20 20 20 20 7a 4c 6f 67 69 6e 20 3d 20 67 ;. zLogin = g
1580: 2e 75 72 6c 55 73 65 72 6e 61 6d 65 3b 0a 20 20 .urlUsername;.
1590: 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 4f 74 }else{. /* Ot
15a0: 68 65 72 77 69 73 65 2c 20 75 73 65 20 65 69 74 herwise, use eit
15b0: 68 65 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 her the default
15c0: 75 73 65 72 20 6f 72 20 74 68 65 20 75 73 65 72 user or the user
15d0: 20 73 70 65 63 69 66 69 65 64 20 77 69 74 68 0a specified with.
15e0: 20 20 20 20 2a 2a 20 74 68 65 20 2d 75 73 65 72 ** the -user
15f0: 20 6f 70 74 69 6f 6e 2e 20 50 75 6c 6c 20 74 68 option. Pull th
1600: 65 20 70 61 73 73 77 6f 72 64 20 66 72 6f 6d 20 e password from
1610: 74 68 65 20 6c 6f 63 61 6c 20 72 65 70 6f 73 69 the local reposi
1620: 74 6f 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 tory.. */.
1630: 20 75 73 65 72 5f 73 65 6c 65 63 74 28 29 3b 0a user_select();.
1640: 20 20 20 20 64 62 5f 62 6c 6f 62 28 26 70 77 2c db_blob(&pw,
1650: 20 22 53 45 4c 45 43 54 20 70 77 20 46 52 4f 4d "SELECT pw FROM
1660: 20 75 73 65 72 20 57 48 45 52 45 20 75 69 64 3d user WHERE uid=
1670: 25 64 22 2c 20 67 2e 75 73 65 72 55 69 64 29 3b %d", g.userUid);
1680: 0a 20 20 20 20 7a 4c 6f 67 69 6e 20 3d 20 67 2e . zLogin = g.
1690: 7a 4c 6f 67 69 6e 3b 0a 20 20 7d 0a 0a 20 20 73 zLogin;. }.. s
16a0: 68 61 31 73 75 6d 5f 62 6c 6f 62 28 26 70 77 2c ha1sum_blob(&pw,
16b0: 20 26 73 69 67 29 3b 0a 20 20 62 6c 6f 62 5f 7a &sig);. blob_z
16c0: 65 72 6f 28 26 6c 6f 67 69 6e 29 3b 0a 20 20 62 ero(&login);. b
16d0: 6c 6f 62 5f 61 70 70 65 6e 64 66 28 26 6c 6f 67 lob_appendf(&log
16e0: 69 6e 2c 20 22 6c 6f 67 69 6e 20 25 73 20 25 62 in, "login %s %b
16f0: 20 25 62 5c 6e 22 2c 20 7a 4c 6f 67 69 6e 2c 20 %b\n", zLogin,
1700: 26 6e 6f 6e 63 65 2c 20 26 73 69 67 29 3b 0a 20 &nonce, &sig);.
1710: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 6e 6f 6e blob_reset(&non
1720: 63 65 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 ce);. blob_rese
1730: 74 28 26 70 77 29 3b 0a 20 20 62 6c 6f 62 5f 72 t(&pw);. blob_r
1740: 65 73 65 74 28 26 73 69 67 29 3b 0a 20 20 69 66 eset(&sig);. if
1750: 28 20 67 2e 66 48 74 74 70 54 72 61 63 65 20 29 ( g.fHttpTrace )
1760: 7b 0a 20 20 20 20 70 61 79 6c 6f 61 64 20 3d 20 {. payload =
1770: 6c 6f 67 69 6e 3b 0a 20 20 20 20 62 6c 6f 62 5f login;. blob_
1780: 61 70 70 65 6e 64 28 26 70 61 79 6c 6f 61 64 2c append(&payload,
1790: 20 62 6c 6f 62 5f 62 75 66 66 65 72 28 70 53 65 blob_buffer(pSe
17a0: 6e 64 29 2c 20 62 6c 6f 62 5f 73 69 7a 65 28 70 nd), blob_size(p
17b0: 53 65 6e 64 29 29 3b 0a 20 20 7d 65 6c 73 65 7b Send));. }else{
17c0: 0a 20 20 20 20 62 6c 6f 62 5f 63 6f 6d 70 72 65 . blob_compre
17d0: 73 73 32 28 26 6c 6f 67 69 6e 2c 20 70 53 65 6e ss2(&login, pSen
17e0: 64 2c 20 26 70 61 79 6c 6f 61 64 29 3b 0a 20 20 d, &payload);.
17f0: 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 6c 6f blob_reset(&lo
1800: 67 69 6e 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 gin);. }. blob
1810: 5f 7a 65 72 6f 28 26 68 64 72 29 3b 0a 20 20 69 _zero(&hdr);. i
1820: 20 3d 20 73 74 72 6c 65 6e 28 67 2e 75 72 6c 50 = strlen(g.urlP
1830: 61 74 68 29 3b 0a 20 20 69 66 28 20 69 3e 30 20 ath);. if( i>0
1840: 26 26 20 67 2e 75 72 6c 50 61 74 68 5b 69 2d 31 && g.urlPath[i-1
1850: 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 7a 53 ]=='/' ){. zS
1860: 65 70 20 3d 20 22 22 3b 0a 20 20 7d 65 6c 73 65 ep = "";. }else
1870: 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20 22 2f 22 {. zSep = "/"
1880: 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 61 70 70 ;. }. blob_app
1890: 65 6e 64 66 28 26 68 64 72 2c 20 22 47 45 54 20 endf(&hdr, "GET
18a0: 25 73 25 73 78 66 65 72 20 48 54 54 50 2f 31 2e %s%sxfer HTTP/1.
18b0: 31 5c 72 5c 6e 22 2c 20 67 2e 75 72 6c 50 61 74 1\r\n", g.urlPat
18c0: 68 2c 20 7a 53 65 70 29 3b 0a 20 20 69 66 28 20 h, zSep);. if(
18d0: 67 2e 66 48 74 74 70 54 72 61 63 65 20 29 7b 0a g.fHttpTrace ){.
18e0: 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 66 blob_appendf
18f0: 28 26 68 64 72 2c 20 22 43 6f 6e 74 65 6e 74 2d (&hdr, "Content-
1900: 54 79 70 65 3a 20 61 70 70 6c 69 63 61 74 69 6f Type: applicatio
1910: 6e 2f 78 2d 66 6f 73 73 69 6c 2d 64 65 62 75 67 n/x-fossil-debug
1920: 5c 72 5c 6e 22 29 3b 0a 20 20 7d 65 6c 73 65 7b \r\n");. }else{
1930: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64 . blob_append
1940: 66 28 26 68 64 72 2c 20 22 43 6f 6e 74 65 6e 74 f(&hdr, "Content
1950: 2d 54 79 70 65 3a 20 61 70 70 6c 69 63 61 74 69 -Type: applicati
1960: 6f 6e 2f 78 2d 66 6f 73 73 69 6c 5c 72 5c 6e 22 on/x-fossil\r\n"
1970: 29 3b 0a 20 20 7d 0a 20 20 62 6c 6f 62 5f 61 70 );. }. blob_ap
1980: 70 65 6e 64 66 28 26 68 64 72 2c 20 22 43 6f 6e pendf(&hdr, "Con
1990: 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 25 64 5c tent-Length: %d\
19a0: 72 5c 6e 5c 72 5c 6e 22 2c 20 62 6c 6f 62 5f 73 r\n\r\n", blob_s
19b0: 69 7a 65 28 26 70 61 79 6c 6f 61 64 29 29 3b 0a ize(&payload));.
19c0: 0a 20 20 69 66 28 20 67 2e 66 48 74 74 70 54 72 . if( g.fHttpTr
19d0: 61 63 65 20 29 7b 0a 20 20 20 20 2f 2a 20 57 68 ace ){. /* Wh
19e0: 65 6e 20 74 72 61 63 69 6e 67 2c 20 77 72 69 74 en tracing, writ
19f0: 65 20 74 68 65 20 74 72 61 6e 73 6d 69 74 74 65 e the transmitte
1a00: 64 20 48 54 54 50 20 6d 65 73 73 61 67 65 20 62 d HTTP message b
1a10: 6f 74 68 20 74 6f 20 73 74 61 6e 64 61 72 64 0a oth to standard.
1a20: 20 20 20 20 2a 2a 20 6f 75 74 70 75 74 20 61 6e ** output an
1a30: 64 20 69 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 d into a file.
1a40: 54 68 65 20 66 69 6c 65 20 63 61 6e 20 74 68 65 The file can the
1a50: 6e 20 62 65 20 75 73 65 64 20 74 6f 20 64 72 69 n be used to dri
1a60: 76 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 73 65 ve the. ** se
1a70: 72 76 65 72 2d 73 69 64 65 20 6c 69 6b 65 20 74 rver-side like t
1a80: 68 69 73 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 his:. **.
1a90: 2a 2a 20 20 20 20 20 20 2e 2f 66 6f 73 73 69 6c ** ./fossil
1aa0: 20 68 74 74 70 20 3c 68 74 74 70 2d 74 72 61 63 http <http-trac
1ab0: 65 2d 31 2e 74 78 74 0a 20 20 20 20 2a 2f 0a 20 e-1.txt. */.
1ac0: 20 20 20 73 74 61 74 69 63 20 69 6e 74 20 74 72 static int tr
1ad0: 61 63 65 43 6e 74 20 3d 20 30 3b 0a 20 20 20 20 aceCnt = 0;.
1ae0: 63 68 61 72 20 2a 7a 4f 75 74 46 69 6c 65 3b 0a char *zOutFile;.
1af0: 20 20 20 20 46 49 4c 45 20 2a 6f 75 74 3b 0a 20 FILE *out;.
1b00: 20 20 20 74 72 61 63 65 43 6e 74 2b 2b 3b 0a 20 traceCnt++;.
1b10: 20 20 20 7a 4f 75 74 46 69 6c 65 20 3d 20 6d 70 zOutFile = mp
1b20: 72 69 6e 74 66 28 22 68 74 74 70 2d 74 72 61 63 rintf("http-trac
1b30: 65 2d 25 64 2e 74 78 74 22 2c 20 74 72 61 63 65 e-%d.txt", trace
1b40: 43 6e 74 29 3b 0a 20 20 20 20 70 72 69 6e 74 66 Cnt);. printf
1b50: 28 22 48 54 54 50 20 53 45 4e 44 3a 20 28 25 73 ("HTTP SEND: (%s
1b60: 29 5c 6e 25 73 25 73 3d 3d 3d 3d 3d 3d 3d 3d 3d )\n%s%s=========
1b70: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 5c 6e ==============\n
1b80: 22 2c 20 0a 20 20 20 20 20 20 20 20 7a 4f 75 74 ", . zOut
1b90: 46 69 6c 65 2c 20 62 6c 6f 62 5f 73 74 72 28 26 File, blob_str(&
1ba0: 68 64 72 29 2c 20 62 6c 6f 62 5f 73 74 72 28 26 hdr), blob_str(&
1bb0: 70 61 79 6c 6f 61 64 29 29 3b 0a 20 20 20 20 6f payload));. o
1bc0: 75 74 20 3d 20 66 6f 70 65 6e 28 7a 4f 75 74 46 ut = fopen(zOutF
1bd0: 69 6c 65 2c 20 22 77 22 29 3b 0a 20 20 20 20 69 ile, "w");. i
1be0: 66 28 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 f( out ){.
1bf0: 66 77 72 69 74 65 28 62 6c 6f 62 5f 62 75 66 66 fwrite(blob_buff
1c00: 65 72 28 26 68 64 72 29 2c 20 31 2c 20 62 6c 6f er(&hdr), 1, blo
1c10: 62 5f 73 69 7a 65 28 26 68 64 72 29 2c 20 6f 75 b_size(&hdr), ou
1c20: 74 29 3b 0a 20 20 20 20 20 20 66 77 72 69 74 65 t);. fwrite
1c30: 28 62 6c 6f 62 5f 62 75 66 66 65 72 28 26 70 61 (blob_buffer(&pa
1c40: 79 6c 6f 61 64 29 2c 20 31 2c 20 62 6c 6f 62 5f yload), 1, blob_
1c50: 73 69 7a 65 28 26 70 61 79 6c 6f 61 64 29 2c 20 size(&payload),
1c60: 6f 75 74 29 3b 0a 20 20 20 20 20 20 66 63 6c 6f out);. fclo
1c70: 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20 se(out);. }.
1c80: 20 7d 0a 20 20 66 6f 72 28 63 6e 74 3d 30 3b 20 }. for(cnt=0;
1c90: 63 6e 74 3c 32 3b 20 63 6e 74 2b 2b 29 7b 0a 20 cnt<2; cnt++){.
1ca0: 20 20 20 69 66 28 20 68 74 74 70 5f 73 65 6e 64 if( http_send
1cb0: 5f 72 65 63 76 28 26 68 64 72 2c 20 26 70 61 79 _recv(&hdr, &pay
1cc0: 6c 6f 61 64 2c 20 70 52 65 63 76 29 20 29 20 62 load, pRecv) ) b
1cd0: 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 reak;. }. if(
1ce0: 63 6e 74 3e 3d 32 20 29 7b 0a 20 20 20 20 66 6f cnt>=2 ){. fo
1cf0: 73 73 69 6c 5f 70 61 6e 69 63 28 22 63 6f 6e 6e ssil_panic("conn
1d00: 65 63 74 69 6f 6e 20 74 6f 20 73 65 72 76 65 72 ection to server
1d10: 20 66 61 69 6c 65 64 22 29 3b 0a 20 20 7d 0a 20 failed");. }.
1d20: 20 62 6c 6f 62 5f 72 65 73 65 74 28 26 68 64 72 blob_reset(&hdr
1d30: 29 3b 0a 20 20 62 6c 6f 62 5f 72 65 73 65 74 28 );. blob_reset(
1d40: 26 70 61 79 6c 6f 61 64 29 3b 0a 20 20 69 66 28 &payload);. if(
1d50: 20 67 2e 66 48 74 74 70 54 72 61 63 65 20 29 7b g.fHttpTrace ){
1d60: 0a 20 20 20 20 70 72 69 6e 74 66 28 22 48 54 54 . printf("HTT
1d70: 50 20 52 45 43 45 49 56 45 3a 5c 6e 25 73 5c 6e P RECEIVE:\n%s\n
1d80: 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d 3d ================
1d90: 3d 3d 3d 3d 3d 3d 3d 5c 6e 22 2c 20 62 6c 6f 62 =======\n", blob
1da0: 5f 73 74 72 28 70 52 65 63 76 29 29 3b 0a 20 20 _str(pRecv));.
1db0: 7d 65 6c 73 65 7b 0a 20 20 20 20 62 6c 6f 62 5f }else{. blob_
1dc0: 75 6e 63 6f 6d 70 72 65 73 73 28 70 52 65 63 76 uncompress(pRecv
1dd0: 2c 20 70 52 65 63 76 29 3b 0a 20 20 7d 0a 7d 0a , pRecv);. }.}.
1de0: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 ../*.** Make sur
1df0: 65 20 74 68 65 20 73 6f 63 6b 65 74 20 74 6f 20 e the socket to
1e00: 74 68 65 20 48 54 54 50 20 73 65 72 76 65 72 20 the HTTP server
1e10: 69 73 20 63 6c 6f 73 65 64 20 0a 2a 2f 0a 76 6f is closed .*/.vo
1e20: 69 64 20 68 74 74 70 5f 63 6c 6f 73 65 28 76 6f id http_close(vo
1e30: 69 64 29 7b 0a 20 20 69 66 28 20 70 53 6f 63 6b id){. if( pSock
1e40: 65 74 20 29 7b 0a 20 20 20 20 66 63 6c 6f 73 65 et ){. fclose
1e50: 28 70 53 6f 63 6b 65 74 29 3b 0a 20 20 20 20 70 (pSocket);. p
1e60: 53 6f 63 6b 65 74 20 3d 20 30 3b 0a 20 20 7d 0a Socket = 0;. }.
1e70: 7d 0a }.