|
737e76a…
|
drh
|
1 |
/* |
|
c19f34c…
|
drh
|
2 |
** Copyright (c) 2009 D. Richard Hipp |
|
737e76a…
|
drh
|
3 |
** |
|
737e76a…
|
drh
|
4 |
** This program is free software; you can redistribute it and/or |
|
c06edd2…
|
drh
|
5 |
** modify it under the terms of the Simplified BSD License (also |
|
c06edd2…
|
drh
|
6 |
** known as the "2-Clause License" or "FreeBSD License".) |
|
c06edd2…
|
drh
|
7 |
|
|
737e76a…
|
drh
|
8 |
** This program is distributed in the hope that it will be useful, |
|
c06edd2…
|
drh
|
9 |
** but without any warranty; without even the implied warranty of |
|
c06edd2…
|
drh
|
10 |
** merchantability or fitness for a particular purpose. |
|
737e76a…
|
drh
|
11 |
** |
|
737e76a…
|
drh
|
12 |
** Author contact information: |
|
737e76a…
|
drh
|
13 |
** [email protected] |
|
737e76a…
|
drh
|
14 |
** http://www.hwaci.com/drh/ |
|
737e76a…
|
drh
|
15 |
** |
|
737e76a…
|
drh
|
16 |
******************************************************************************* |
|
737e76a…
|
drh
|
17 |
** |
|
737e76a…
|
drh
|
18 |
** This module implements the transport layer for the client side HTTP |
|
737e76a…
|
drh
|
19 |
** connection. The purpose of this layer is to provide a common interface |
|
737e76a…
|
drh
|
20 |
** for both HTTP and HTTPS and to provide a common "fetch one line" |
|
737e76a…
|
drh
|
21 |
** interface that is used for parsing the reply. |
|
737e76a…
|
drh
|
22 |
*/ |
|
737e76a…
|
drh
|
23 |
#include "config.h" |
|
737e76a…
|
drh
|
24 |
#include "http_transport.h" |
|
737e76a…
|
drh
|
25 |
|
|
737e76a…
|
drh
|
26 |
/* |
|
737e76a…
|
drh
|
27 |
** State information |
|
737e76a…
|
drh
|
28 |
*/ |
|
737e76a…
|
drh
|
29 |
static struct { |
|
945ecd1…
|
drh
|
30 |
int isOpen; /* True when the transport layer is open */ |
|
945ecd1…
|
drh
|
31 |
char *pBuf; /* Buffer used to hold the reply */ |
|
945ecd1…
|
drh
|
32 |
int nAlloc; /* Space allocated for transportBuf[] */ |
|
945ecd1…
|
drh
|
33 |
int nUsed ; /* Space of transportBuf[] used */ |
|
945ecd1…
|
drh
|
34 |
int iCursor; /* Next unread by in transportBuf[] */ |
|
a9e05e2…
|
drh
|
35 |
i64 nSent; /* Number of bytes sent */ |
|
a9e05e2…
|
drh
|
36 |
i64 nRcvd; /* Number of bytes received */ |
|
945ecd1…
|
drh
|
37 |
FILE *pFile; /* File I/O for FILE: */ |
|
945ecd1…
|
drh
|
38 |
char *zOutFile; /* Name of outbound file for FILE: */ |
|
945ecd1…
|
drh
|
39 |
char *zInFile; /* Name of inbound file for FILE: */ |
|
abe8923…
|
drh
|
40 |
FILE *pLog; /* Log output here */ |
|
c3bd7c3…
|
drh
|
41 |
int bTest; /* file:// testing */ |
|
737e76a…
|
drh
|
42 |
} transport = { |
|
c3bd7c3…
|
drh
|
43 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
|
737e76a…
|
drh
|
44 |
}; |
|
0a51263…
|
drh
|
45 |
|
|
0a51263…
|
drh
|
46 |
/* |
|
0a51263…
|
drh
|
47 |
** Information about the connection to the SSH subprocess when |
|
0a51263…
|
drh
|
48 |
** using the ssh:// sync method. |
|
0a51263…
|
drh
|
49 |
*/ |
|
0a51263…
|
drh
|
50 |
static int sshPid; /* Process id of ssh subprocess */ |
|
0a51263…
|
drh
|
51 |
static int sshIn; /* From ssh subprocess to this process */ |
|
0a51263…
|
drh
|
52 |
static FILE *sshOut; /* From this to ssh subprocess */ |
|
0a51263…
|
drh
|
53 |
|
|
737e76a…
|
drh
|
54 |
|
|
737e76a…
|
drh
|
55 |
/* |
|
737e76a…
|
drh
|
56 |
** Return the current transport error message. |
|
737e76a…
|
drh
|
57 |
*/ |
|
1311841…
|
jan.nijtmans
|
58 |
const char *transport_errmsg(UrlData *pUrlData){ |
|
16e703b…
|
drh
|
59 |
#ifdef FOSSIL_ENABLE_SSL |
|
1311841…
|
jan.nijtmans
|
60 |
if( pUrlData->isHttps ){ |
|
16e703b…
|
drh
|
61 |
return ssl_errmsg(); |
|
16e703b…
|
drh
|
62 |
} |
|
16e703b…
|
drh
|
63 |
#endif |
|
737e76a…
|
drh
|
64 |
return socket_errmsg(); |
|
737e76a…
|
drh
|
65 |
} |
|
737e76a…
|
drh
|
66 |
|
|
737e76a…
|
drh
|
67 |
/* |
|
79be902…
|
drh
|
68 |
** Retrieve send/receive counts from the transport layer. If "resetFlag" |
|
79be902…
|
drh
|
69 |
** is true, then reset the counts. |
|
79be902…
|
drh
|
70 |
*/ |
|
a9e05e2…
|
drh
|
71 |
void transport_stats(i64 *pnSent, i64 *pnRcvd, int resetFlag){ |
|
79be902…
|
drh
|
72 |
if( pnSent ) *pnSent = transport.nSent; |
|
79be902…
|
drh
|
73 |
if( pnRcvd ) *pnRcvd = transport.nRcvd; |
|
79be902…
|
drh
|
74 |
if( resetFlag ){ |
|
79be902…
|
drh
|
75 |
transport.nSent = 0; |
|
79be902…
|
drh
|
76 |
transport.nRcvd = 0; |
|
79be902…
|
drh
|
77 |
} |
|
79be902…
|
drh
|
78 |
} |
|
79be902…
|
drh
|
79 |
|
|
79be902…
|
drh
|
80 |
/* |
|
cb43937…
|
drh
|
81 |
** Check zFossil to see if it is a reasonable "fossil" command to |
|
cb43937…
|
drh
|
82 |
** run on the server. Do not allow an attacker to substitute something |
|
cb43937…
|
drh
|
83 |
** like "/bin/rm". |
|
cb43937…
|
drh
|
84 |
*/ |
|
cb43937…
|
drh
|
85 |
static int is_safe_fossil_command(const char *zFossil){ |
|
8f70cca…
|
drh
|
86 |
static const char *const azSafe[] = { "*/fossil", "*/fossil.exe", "*/echo" }; |
|
cb43937…
|
drh
|
87 |
int i; |
|
53db40e…
|
drh
|
88 |
for(i=0; i<(int)(sizeof(azSafe)/sizeof(azSafe[0])); i++){ |
|
cb43937…
|
drh
|
89 |
if( sqlite3_strglob(azSafe[i], zFossil)==0 ) return 1; |
|
cb43937…
|
drh
|
90 |
if( strcmp(azSafe[i]+2, zFossil)==0 ) return 1; |
|
cb43937…
|
drh
|
91 |
} |
|
cb43937…
|
drh
|
92 |
return 0; |
|
cb43937…
|
drh
|
93 |
} |
|
cb43937…
|
drh
|
94 |
|
|
cb43937…
|
drh
|
95 |
/* |
|
0cdb640…
|
drh
|
96 |
** Default SSH command |
|
0e42cc1…
|
drh
|
97 |
*/ |
|
3856723…
|
drh
|
98 |
#if 0 /* was: defined(_WIN32). Windows generally has ssh now. */ |
|
f3acbe4…
|
drh
|
99 |
static const char zDefaultSshCmd[] = "plink -ssh"; |
|
dbb5e2d…
|
drh
|
100 |
#else |
|
f3acbe4…
|
drh
|
101 |
static const char zDefaultSshCmd[] = "ssh -e none"; |
|
dbb5e2d…
|
drh
|
102 |
#endif |
|
e0d6110…
|
drh
|
103 |
|
|
e0d6110…
|
drh
|
104 |
/* |
|
e0d6110…
|
drh
|
105 |
** Initialize a Blob to the name of the configured SSH command. |
|
e0d6110…
|
drh
|
106 |
*/ |
|
e0d6110…
|
drh
|
107 |
void transport_ssh_command(Blob *p){ |
|
e0d6110…
|
drh
|
108 |
char *zSsh; /* The base SSH command */ |
|
4c6e394…
|
andybradford
|
109 |
zSsh = g.zSshCmd; |
|
4c6e394…
|
andybradford
|
110 |
if( zSsh==0 || zSsh[0]==0 ){ |
|
4c6e394…
|
andybradford
|
111 |
zSsh = db_get("ssh-command", zDefaultSshCmd); |
|
4c6e394…
|
andybradford
|
112 |
} |
|
e0d6110…
|
drh
|
113 |
blob_init(p, zSsh, -1); |
|
e0d6110…
|
drh
|
114 |
} |
|
dbb5e2d…
|
drh
|
115 |
|
|
dbb5e2d…
|
drh
|
116 |
/* |
|
dbb5e2d…
|
drh
|
117 |
** SSH initialization of the transport layer |
|
dbb5e2d…
|
drh
|
118 |
*/ |
|
1311841…
|
jan.nijtmans
|
119 |
int transport_ssh_open(UrlData *pUrlData){ |
|
5330d10…
|
jan.nijtmans
|
120 |
/* For SSH we need to create and run SSH fossil http |
|
dbb5e2d…
|
drh
|
121 |
** to talk to the remote machine. |
|
dbb5e2d…
|
drh
|
122 |
*/ |
|
dbb5e2d…
|
drh
|
123 |
Blob zCmd; /* The SSH command */ |
|
dbb5e2d…
|
drh
|
124 |
char *zHost; /* The host name to contact */ |
|
1311841…
|
jan.nijtmans
|
125 |
|
|
0b7af9d…
|
drh
|
126 |
fossil_free(g.zIpAddr); |
|
4c3e172…
|
danield
|
127 |
g.zIpAddr = fossil_strdup(pUrlData->name); |
|
e0d6110…
|
drh
|
128 |
transport_ssh_command(&zCmd); |
|
1311841…
|
jan.nijtmans
|
129 |
if( pUrlData->port!=pUrlData->dfltPort && pUrlData->port ){ |
|
1311841…
|
jan.nijtmans
|
130 |
blob_appendf(&zCmd, " -p %d", pUrlData->port); |
|
1311841…
|
jan.nijtmans
|
131 |
} |
|
f3acbe4…
|
drh
|
132 |
blob_appendf(&zCmd, " -T --"); /* End of switches */ |
|
1311841…
|
jan.nijtmans
|
133 |
if( pUrlData->user && pUrlData->user[0] ){ |
|
1311841…
|
jan.nijtmans
|
134 |
zHost = mprintf("%s@%s", pUrlData->user, pUrlData->name); |
|
4f83d06…
|
drh
|
135 |
blob_append_escaped_arg(&zCmd, zHost, 0); |
|
3b191c9…
|
drh
|
136 |
fossil_free(zHost); |
|
dbb5e2d…
|
drh
|
137 |
}else{ |
|
4f83d06…
|
drh
|
138 |
blob_append_escaped_arg(&zCmd, pUrlData->name, 0); |
|
cb43937…
|
drh
|
139 |
} |
|
cbf27ec…
|
drh
|
140 |
if( (pUrlData->flags & URL_SSH_EXE)!=0 |
|
cbf27ec…
|
drh
|
141 |
&& !is_safe_fossil_command(pUrlData->fossil) |
|
cbf27ec…
|
drh
|
142 |
){ |
|
31c7bdb…
|
larrybr
|
143 |
fossil_fatal("the ssh:// URL is asking to run an unsafe command [%s] on " |
|
cb43937…
|
drh
|
144 |
"the server.", pUrlData->fossil); |
|
805c931…
|
drh
|
145 |
} |
|
cbf27ec…
|
drh
|
146 |
if( (pUrlData->flags & URL_SSH_EXE)==0 |
|
1862890…
|
stephan
|
147 |
&& (pUrlData->flags & URL_SSH_PATH)!=0 |
|
cbf27ec…
|
drh
|
148 |
){ |
|
efd3a5e…
|
drh
|
149 |
ssh_add_path_argument(&zCmd); |
|
cbf27ec…
|
drh
|
150 |
} |
|
4f83d06…
|
drh
|
151 |
blob_append_escaped_arg(&zCmd, pUrlData->fossil, 1); |
|
dbb5e2d…
|
drh
|
152 |
blob_append(&zCmd, " test-http", 10); |
|
1311841…
|
jan.nijtmans
|
153 |
if( pUrlData->path && pUrlData->path[0] ){ |
|
4f83d06…
|
drh
|
154 |
blob_append_escaped_arg(&zCmd, pUrlData->path, 1); |
|
3b191c9…
|
drh
|
155 |
}else{ |
|
31c7bdb…
|
larrybr
|
156 |
fossil_fatal("ssh:// URI does not specify a path to the repository"); |
|
3b191c9…
|
drh
|
157 |
} |
|
505d9d4…
|
drh
|
158 |
if( g.fSshTrace || g.fHttpTrace ){ |
|
505d9d4…
|
drh
|
159 |
fossil_print("RUN %s\n", blob_str(&zCmd)); /* Show the whole SSH command */ |
|
3b191c9…
|
drh
|
160 |
} |
|
ec56c69…
|
drh
|
161 |
popen2(blob_str(&zCmd), &sshIn, &sshOut, &sshPid, 0); |
|
dbb5e2d…
|
drh
|
162 |
if( sshPid==0 ){ |
|
dbb5e2d…
|
drh
|
163 |
socket_set_errmsg("cannot start ssh tunnel using [%b]", &zCmd); |
|
dbb5e2d…
|
drh
|
164 |
} |
|
dbb5e2d…
|
drh
|
165 |
blob_reset(&zCmd); |
|
dbb5e2d…
|
drh
|
166 |
return sshPid==0; |
|
dbb5e2d…
|
drh
|
167 |
} |
|
dbb5e2d…
|
drh
|
168 |
|
|
dbb5e2d…
|
drh
|
169 |
/* |
|
c3bd7c3…
|
drh
|
170 |
** Return true if the filename given matches the pattern that means |
|
c3bd7c3…
|
drh
|
171 |
** it is a test-reply. |
|
c3bd7c3…
|
drh
|
172 |
*/ |
|
c3bd7c3…
|
drh
|
173 |
static int transport_is_test(const char *zFilename){ |
|
c3bd7c3…
|
drh
|
174 |
return sqlite3_strglob("*.http-test",zFilename)==0; |
|
c3bd7c3…
|
drh
|
175 |
} |
|
c3bd7c3…
|
drh
|
176 |
|
|
c3bd7c3…
|
drh
|
177 |
/* |
|
737e76a…
|
drh
|
178 |
** Open a connection to the server. The server is defined by the following |
|
5fdad9b…
|
drh
|
179 |
** variables: |
|
5fdad9b…
|
drh
|
180 |
** |
|
09908ab…
|
drh
|
181 |
** pUrlData->name Name of the server. Ex: fossil-scm.org |
|
c3bd7c3…
|
drh
|
182 |
** |
|
5fdad9b…
|
drh
|
183 |
** pUrlData->port TCP/IP port. Ex: 80 |
|
c3bd7c3…
|
drh
|
184 |
** |
|
5fdad9b…
|
drh
|
185 |
** pUrlData->isHttps Use TLS for the connection |
|
c3bd7c3…
|
drh
|
186 |
** |
|
c3bd7c3…
|
drh
|
187 |
** pUrlData->isSsh Use an SSH connection to the server. name is |
|
c3bd7c3…
|
drh
|
188 |
** the SSH host name. subpath is the server |
|
c3bd7c3…
|
drh
|
189 |
** method name (ex: timeline) |
|
c3bd7c3…
|
drh
|
190 |
** |
|
c3bd7c3…
|
drh
|
191 |
** pUrlData->isFile The .name is a filename in the local filesystem. |
|
c3bd7c3…
|
drh
|
192 |
** If that file is a Fossil repo, then launch |
|
c3bd7c3…
|
drh
|
193 |
** "fossil http" as a subprocess to handle the request. |
|
62dc86c…
|
danshearer
|
194 |
** Or if filename ends with ".http-test" then the |
|
c3bd7c3…
|
drh
|
195 |
** content of that file becomes the HTTP reply. |
|
c3bd7c3…
|
drh
|
196 |
** (The latter is used for testing only.) |
|
c3bd7c3…
|
drh
|
197 |
** |
|
c3bd7c3…
|
drh
|
198 |
** pUrlData->subpath The method name on the request for SSH and FILE |
|
737e76a…
|
drh
|
199 |
** |
|
737e76a…
|
drh
|
200 |
** Return the number of errors. |
|
737e76a…
|
drh
|
201 |
*/ |
|
1311841…
|
jan.nijtmans
|
202 |
int transport_open(UrlData *pUrlData){ |
|
737e76a…
|
drh
|
203 |
int rc = 0; |
|
327823e…
|
drh
|
204 |
if( transport.isOpen==0 ){ |
|
1311841…
|
jan.nijtmans
|
205 |
if( pUrlData->isSsh ){ |
|
1311841…
|
jan.nijtmans
|
206 |
rc = transport_ssh_open(pUrlData); |
|
1311841…
|
jan.nijtmans
|
207 |
if( rc==0 ) transport.isOpen = 1; |
|
1311841…
|
jan.nijtmans
|
208 |
}else if( pUrlData->isHttps ){ |
|
1ad56ce…
|
drh
|
209 |
#ifdef FOSSIL_ENABLE_SSL |
|
f6263bb…
|
drh
|
210 |
rc = ssl_open_client(pUrlData); |
|
1311841…
|
jan.nijtmans
|
211 |
if( rc==0 ) transport.isOpen = 1; |
|
1ad56ce…
|
drh
|
212 |
#else |
|
16e703b…
|
drh
|
213 |
socket_set_errmsg("HTTPS: Fossil has been compiled without SSL support"); |
|
945ecd1…
|
drh
|
214 |
rc = 1; |
|
1ad56ce…
|
drh
|
215 |
#endif |
|
1311841…
|
jan.nijtmans
|
216 |
}else if( pUrlData->isFile ){ |
|
c3bd7c3…
|
drh
|
217 |
if( !file_isfile(pUrlData->name, ExtFILE) ){ |
|
c3bd7c3…
|
drh
|
218 |
fossil_fatal("no such file: \"%s\"", pUrlData->name); |
|
505d9d4…
|
drh
|
219 |
} |
|
505d9d4…
|
drh
|
220 |
transport.zOutFile = fossil_temp_filename(); |
|
505d9d4…
|
drh
|
221 |
transport.zInFile = fossil_temp_filename(); |
|
c3bd7c3…
|
drh
|
222 |
if( !db_looks_like_a_repository(pUrlData->name) ){ |
|
c3bd7c3…
|
drh
|
223 |
if( transport_is_test(pUrlData->name) ){ |
|
c3bd7c3…
|
drh
|
224 |
/* If the URI filename ends with ".http-test" then the content |
|
c3bd7c3…
|
drh
|
225 |
** of that file becomes the HTTP reply and the request is stored |
|
c3bd7c3…
|
drh
|
226 |
** in a file with the same name but with ".request" added to the |
|
c3bd7c3…
|
drh
|
227 |
** end. Used for testing only */ |
|
c3bd7c3…
|
drh
|
228 |
transport.bTest = 1; |
|
c3bd7c3…
|
drh
|
229 |
sqlite3_free(transport.zOutFile); |
|
c3bd7c3…
|
drh
|
230 |
transport.zOutFile = sqlite3_mprintf("%s.request",pUrlData->name); |
|
c3bd7c3…
|
drh
|
231 |
sqlite3_free(transport.zInFile); |
|
c3bd7c3…
|
drh
|
232 |
transport.zInFile = sqlite3_mprintf("%s",pUrlData->name); |
|
c3bd7c3…
|
drh
|
233 |
}else{ |
|
c3bd7c3…
|
drh
|
234 |
fossil_fatal("not a fossil repository: \"%s\"", pUrlData->name); |
|
c3bd7c3…
|
drh
|
235 |
} |
|
c3bd7c3…
|
drh
|
236 |
} |
|
b5aab69…
|
drh
|
237 |
transport.pFile = fossil_fopen(transport.zOutFile, "wb"); |
|
945ecd1…
|
drh
|
238 |
if( transport.pFile==0 ){ |
|
31c7bdb…
|
larrybr
|
239 |
fossil_fatal("cannot output temporary file: %s", transport.zOutFile); |
|
945ecd1…
|
drh
|
240 |
} |
|
945ecd1…
|
drh
|
241 |
transport.isOpen = 1; |
|
945ecd1…
|
drh
|
242 |
}else{ |
|
1311841…
|
jan.nijtmans
|
243 |
rc = socket_open(pUrlData); |
|
327823e…
|
drh
|
244 |
if( rc==0 ) transport.isOpen = 1; |
|
327823e…
|
drh
|
245 |
} |
|
737e76a…
|
drh
|
246 |
} |
|
737e76a…
|
drh
|
247 |
return rc; |
|
737e76a…
|
drh
|
248 |
} |
|
737e76a…
|
drh
|
249 |
|
|
737e76a…
|
drh
|
250 |
/* |
|
737e76a…
|
drh
|
251 |
** Close the current connection |
|
737e76a…
|
drh
|
252 |
*/ |
|
1311841…
|
jan.nijtmans
|
253 |
void transport_close(UrlData *pUrlData){ |
|
737e76a…
|
drh
|
254 |
if( transport.isOpen ){ |
|
737e76a…
|
drh
|
255 |
free(transport.pBuf); |
|
737e76a…
|
drh
|
256 |
transport.pBuf = 0; |
|
737e76a…
|
drh
|
257 |
transport.nAlloc = 0; |
|
737e76a…
|
drh
|
258 |
transport.nUsed = 0; |
|
737e76a…
|
drh
|
259 |
transport.iCursor = 0; |
|
abe8923…
|
drh
|
260 |
if( transport.pLog ){ |
|
abe8923…
|
drh
|
261 |
fclose(transport.pLog); |
|
abe8923…
|
drh
|
262 |
transport.pLog = 0; |
|
abe8923…
|
drh
|
263 |
} |
|
1311841…
|
jan.nijtmans
|
264 |
if( pUrlData->isSsh ){ |
|
dbb5e2d…
|
drh
|
265 |
transport_ssh_close(); |
|
1311841…
|
jan.nijtmans
|
266 |
}else if( pUrlData->isHttps ){ |
|
16e703b…
|
drh
|
267 |
#ifdef FOSSIL_ENABLE_SSL |
|
f6263bb…
|
drh
|
268 |
ssl_close_client(); |
|
16e703b…
|
drh
|
269 |
#endif |
|
1311841…
|
jan.nijtmans
|
270 |
}else if( pUrlData->isFile ){ |
|
4e092e0…
|
jan.nijtmans
|
271 |
if( transport.pFile ){ |
|
945ecd1…
|
drh
|
272 |
fclose(transport.pFile); |
|
945ecd1…
|
drh
|
273 |
transport.pFile = 0; |
|
945ecd1…
|
drh
|
274 |
} |
|
c3bd7c3…
|
drh
|
275 |
if( !transport.bTest ){ |
|
c3bd7c3…
|
drh
|
276 |
file_delete(transport.zInFile); |
|
c3bd7c3…
|
drh
|
277 |
file_delete(transport.zOutFile); |
|
c3bd7c3…
|
drh
|
278 |
} |
|
505d9d4…
|
drh
|
279 |
sqlite3_free(transport.zInFile); |
|
505d9d4…
|
drh
|
280 |
sqlite3_free(transport.zOutFile); |
|
327823e…
|
drh
|
281 |
}else{ |
|
327823e…
|
drh
|
282 |
socket_close(); |
|
327823e…
|
drh
|
283 |
} |
|
327823e…
|
drh
|
284 |
transport.isOpen = 0; |
|
737e76a…
|
drh
|
285 |
} |
|
737e76a…
|
drh
|
286 |
} |
|
737e76a…
|
drh
|
287 |
|
|
737e76a…
|
drh
|
288 |
/* |
|
737e76a…
|
drh
|
289 |
** Send content over the wire. |
|
737e76a…
|
drh
|
290 |
*/ |
|
1862890…
|
stephan
|
291 |
void transport_send(UrlData const *pUrlData, const Blob *toSend){ |
|
945ecd1…
|
drh
|
292 |
char *z = blob_buffer(toSend); |
|
945ecd1…
|
drh
|
293 |
int n = blob_size(toSend); |
|
79be902…
|
drh
|
294 |
transport.nSent += n; |
|
1311841…
|
jan.nijtmans
|
295 |
if( pUrlData->isSsh ){ |
|
d8bbe4a…
|
drh
|
296 |
fwrite(z, 1, n, sshOut); |
|
0a51263…
|
drh
|
297 |
fflush(sshOut); |
|
1311841…
|
jan.nijtmans
|
298 |
}else if( pUrlData->isHttps ){ |
|
1ad56ce…
|
drh
|
299 |
#ifdef FOSSIL_ENABLE_SSL |
|
16e703b…
|
drh
|
300 |
int sent; |
|
16e703b…
|
drh
|
301 |
while( n>0 ){ |
|
16e703b…
|
drh
|
302 |
sent = ssl_send(0, z, n); |
|
16e703b…
|
drh
|
303 |
/* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */ |
|
16e703b…
|
drh
|
304 |
if( sent<=0 ) break; |
|
16e703b…
|
drh
|
305 |
n -= sent; |
|
4e092e0…
|
jan.nijtmans
|
306 |
} |
|
1ad56ce…
|
drh
|
307 |
#endif |
|
1311841…
|
jan.nijtmans
|
308 |
}else if( pUrlData->isFile ){ |
|
945ecd1…
|
drh
|
309 |
fwrite(z, 1, n, transport.pFile); |
|
327823e…
|
drh
|
310 |
}else{ |
|
327823e…
|
drh
|
311 |
int sent; |
|
327823e…
|
drh
|
312 |
while( n>0 ){ |
|
327823e…
|
drh
|
313 |
sent = socket_send(0, z, n); |
|
767ae79…
|
drh
|
314 |
/* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */ |
|
327823e…
|
drh
|
315 |
if( sent<=0 ) break; |
|
327823e…
|
drh
|
316 |
n -= sent; |
|
327823e…
|
drh
|
317 |
} |
|
327823e…
|
drh
|
318 |
} |
|
327823e…
|
drh
|
319 |
} |
|
327823e…
|
drh
|
320 |
|
|
327823e…
|
drh
|
321 |
/* |
|
63ef585…
|
drh
|
322 |
** This routine is called when the outbound message is complete and |
|
2209f55…
|
drh
|
323 |
** it is time to begin receiving a reply. |
|
c3bd7c3…
|
drh
|
324 |
** |
|
c3bd7c3…
|
drh
|
325 |
** This is a no-op for http:, https:, and ssh:. It only does this |
|
c3bd7c3…
|
drh
|
326 |
** for file:. If the file is a Fossil database, then an instance of |
|
c3bd7c3…
|
drh
|
327 |
** "fossil http" is invoked in a subprocess to handle the request. |
|
c3bd7c3…
|
drh
|
328 |
** Or if the filename ends with ".http-test" then the text of that |
|
c3bd7c3…
|
drh
|
329 |
** file becomes the reply. The ".http-test" hack is for testing purposes. |
|
945ecd1…
|
drh
|
330 |
*/ |
|
1311841…
|
jan.nijtmans
|
331 |
void transport_flip(UrlData *pUrlData){ |
|
1311841…
|
jan.nijtmans
|
332 |
if( pUrlData->isFile ){ |
|
945ecd1…
|
drh
|
333 |
char *zCmd; |
|
945ecd1…
|
drh
|
334 |
fclose(transport.pFile); |
|
c3bd7c3…
|
drh
|
335 |
if( !transport.bTest ){ |
|
c3bd7c3…
|
drh
|
336 |
zCmd = mprintf("%$ http --in %$ --out %$ --ipaddr 127.0.0.1" |
|
c3bd7c3…
|
drh
|
337 |
" %$ --localauth", |
|
c3bd7c3…
|
drh
|
338 |
g.nameOfExe, transport.zOutFile, transport.zInFile, pUrlData->name |
|
c3bd7c3…
|
drh
|
339 |
); |
|
c3bd7c3…
|
drh
|
340 |
if( g.fHttpTrace ) fossil_print("RUN %s\n", zCmd); |
|
c3bd7c3…
|
drh
|
341 |
fossil_system(zCmd); |
|
c3bd7c3…
|
drh
|
342 |
free(zCmd); |
|
c3bd7c3…
|
drh
|
343 |
} |
|
b5aab69…
|
drh
|
344 |
transport.pFile = fossil_fopen(transport.zInFile, "rb"); |
|
c3bd7c3…
|
drh
|
345 |
if( transport.pFile==0 ){ |
|
c3bd7c3…
|
drh
|
346 |
fossil_fatal("unable to open HTTP reply: \"%s\"",transport.zInFile); |
|
c3bd7c3…
|
drh
|
347 |
} |
|
abe8923…
|
drh
|
348 |
} |
|
abe8923…
|
drh
|
349 |
} |
|
abe8923…
|
drh
|
350 |
|
|
abe8923…
|
drh
|
351 |
/* |
|
abe8923…
|
drh
|
352 |
** Log all input to a file. The transport layer will take responsibility |
|
abe8923…
|
drh
|
353 |
** for closing the log file when it is done. |
|
abe8923…
|
drh
|
354 |
*/ |
|
abe8923…
|
drh
|
355 |
void transport_log(FILE *pLog){ |
|
abe8923…
|
drh
|
356 |
if( transport.pLog ){ |
|
abe8923…
|
drh
|
357 |
fclose(transport.pLog); |
|
abe8923…
|
drh
|
358 |
transport.pLog = 0; |
|
0eb08b8…
|
drh
|
359 |
} |
|
abe8923…
|
drh
|
360 |
transport.pLog = pLog; |
|
63ef585…
|
drh
|
361 |
} |
|
63ef585…
|
drh
|
362 |
|
|
63ef585…
|
drh
|
363 |
/* |
|
63ef585…
|
drh
|
364 |
** This routine is called when the inbound message has been received |
|
63ef585…
|
drh
|
365 |
** and it is time to start sending again. |
|
63ef585…
|
drh
|
366 |
*/ |
|
1311841…
|
jan.nijtmans
|
367 |
void transport_rewind(UrlData *pUrlData){ |
|
1311841…
|
jan.nijtmans
|
368 |
if( pUrlData->isFile ){ |
|
1311841…
|
jan.nijtmans
|
369 |
transport_close(pUrlData); |
|
0a51263…
|
drh
|
370 |
} |
|
0a51263…
|
drh
|
371 |
} |
|
0a51263…
|
drh
|
372 |
|
|
0a51263…
|
drh
|
373 |
/* |
|
0a51263…
|
drh
|
374 |
** Read N bytes of content directly from the wire and write into |
|
0a51263…
|
drh
|
375 |
** the buffer. |
|
0a51263…
|
drh
|
376 |
*/ |
|
1311841…
|
jan.nijtmans
|
377 |
static int transport_fetch(UrlData *pUrlData, char *zBuf, int N){ |
|
0a51263…
|
drh
|
378 |
int got; |
|
cbf27ec…
|
drh
|
379 |
if( pUrlData->isSsh ){ |
|
0a51263…
|
drh
|
380 |
int x; |
|
0a51263…
|
drh
|
381 |
int wanted = N; |
|
0a51263…
|
drh
|
382 |
got = 0; |
|
0a51263…
|
drh
|
383 |
while( wanted>0 ){ |
|
0a51263…
|
drh
|
384 |
x = read(sshIn, &zBuf[got], wanted); |
|
0a51263…
|
drh
|
385 |
if( x<=0 ) break; |
|
0a51263…
|
drh
|
386 |
got += x; |
|
0a51263…
|
drh
|
387 |
wanted -= x; |
|
0a51263…
|
drh
|
388 |
} |
|
1311841…
|
jan.nijtmans
|
389 |
}else if( pUrlData->isHttps ){ |
|
0a51263…
|
drh
|
390 |
#ifdef FOSSIL_ENABLE_SSL |
|
0a51263…
|
drh
|
391 |
got = ssl_receive(0, zBuf, N); |
|
0a51263…
|
drh
|
392 |
#else |
|
0a51263…
|
drh
|
393 |
got = 0; |
|
0a51263…
|
drh
|
394 |
#endif |
|
1311841…
|
jan.nijtmans
|
395 |
}else if( pUrlData->isFile ){ |
|
0a51263…
|
drh
|
396 |
got = fread(zBuf, 1, N, transport.pFile); |
|
0a51263…
|
drh
|
397 |
}else{ |
|
0a201f7…
|
drh
|
398 |
got = socket_receive(0, zBuf, N, 0); |
|
0a51263…
|
drh
|
399 |
} |
|
7c85190…
|
drh
|
400 |
/* printf("received %d of %d bytes\n", got, N); fflush(stdout); */ |
|
abe8923…
|
drh
|
401 |
if( transport.pLog ){ |
|
abe8923…
|
drh
|
402 |
fwrite(zBuf, 1, got, transport.pLog); |
|
abe8923…
|
drh
|
403 |
fflush(transport.pLog); |
|
abe8923…
|
drh
|
404 |
} |
|
0a51263…
|
drh
|
405 |
return got; |
|
63ef585…
|
drh
|
406 |
} |
|
63ef585…
|
drh
|
407 |
|
|
63ef585…
|
drh
|
408 |
/* |
|
737e76a…
|
drh
|
409 |
** Read N bytes of content from the wire and store in the supplied buffer. |
|
737e76a…
|
drh
|
410 |
** Return the number of bytes actually received. |
|
737e76a…
|
drh
|
411 |
*/ |
|
1311841…
|
jan.nijtmans
|
412 |
int transport_receive(UrlData *pUrlData, char *zBuf, int N){ |
|
737e76a…
|
drh
|
413 |
int onHand; /* Bytes current held in the transport buffer */ |
|
737e76a…
|
drh
|
414 |
int nByte = 0; /* Bytes of content received */ |
|
737e76a…
|
drh
|
415 |
|
|
737e76a…
|
drh
|
416 |
onHand = transport.nUsed - transport.iCursor; |
|
2163cd9…
|
drh
|
417 |
if( g.fSshTrace){ |
|
2163cd9…
|
drh
|
418 |
printf("Reading %d bytes with %d on hand... ", N, onHand); |
|
2163cd9…
|
drh
|
419 |
fflush(stdout); |
|
2163cd9…
|
drh
|
420 |
} |
|
737e76a…
|
drh
|
421 |
if( onHand>0 ){ |
|
737e76a…
|
drh
|
422 |
int toMove = onHand; |
|
737e76a…
|
drh
|
423 |
if( toMove>N ) toMove = N; |
|
767ae79…
|
drh
|
424 |
/* printf("bytes on hand: %d of %d\n", toMove, N); fflush(stdout); */ |
|
737e76a…
|
drh
|
425 |
memcpy(zBuf, &transport.pBuf[transport.iCursor], toMove); |
|
737e76a…
|
drh
|
426 |
transport.iCursor += toMove; |
|
737e76a…
|
drh
|
427 |
if( transport.iCursor>=transport.nUsed ){ |
|
737e76a…
|
drh
|
428 |
transport.nUsed = 0; |
|
737e76a…
|
drh
|
429 |
transport.iCursor = 0; |
|
737e76a…
|
drh
|
430 |
} |
|
737e76a…
|
drh
|
431 |
N -= toMove; |
|
737e76a…
|
drh
|
432 |
zBuf += toMove; |
|
737e76a…
|
drh
|
433 |
nByte += toMove; |
|
737e76a…
|
drh
|
434 |
} |
|
737e76a…
|
drh
|
435 |
if( N>0 ){ |
|
1311841…
|
jan.nijtmans
|
436 |
int got = transport_fetch(pUrlData, zBuf, N); |
|
737e76a…
|
drh
|
437 |
if( got>0 ){ |
|
79be902…
|
drh
|
438 |
nByte += got; |
|
79be902…
|
drh
|
439 |
transport.nRcvd += got; |
|
737e76a…
|
drh
|
440 |
} |
|
737e76a…
|
drh
|
441 |
} |
|
2163cd9…
|
drh
|
442 |
if( g.fSshTrace ) printf("Got %d bytes\n", nByte); |
|
737e76a…
|
drh
|
443 |
return nByte; |
|
737e76a…
|
drh
|
444 |
} |
|
737e76a…
|
drh
|
445 |
|
|
737e76a…
|
drh
|
446 |
/* |
|
737e76a…
|
drh
|
447 |
** Load up to N new bytes of content into the transport.pBuf buffer. |
|
737e76a…
|
drh
|
448 |
** The buffer itself might be moved. And the transport.iCursor value |
|
737e76a…
|
drh
|
449 |
** might be reset to 0. |
|
737e76a…
|
drh
|
450 |
*/ |
|
1311841…
|
jan.nijtmans
|
451 |
static void transport_load_buffer(UrlData *pUrlData, int N){ |
|
737e76a…
|
drh
|
452 |
int i, j; |
|
737e76a…
|
drh
|
453 |
if( transport.nAlloc==0 ){ |
|
737e76a…
|
drh
|
454 |
transport.nAlloc = N; |
|
8f41b2f…
|
drh
|
455 |
transport.pBuf = fossil_malloc( N ); |
|
737e76a…
|
drh
|
456 |
transport.iCursor = 0; |
|
737e76a…
|
drh
|
457 |
transport.nUsed = 0; |
|
737e76a…
|
drh
|
458 |
} |
|
737e76a…
|
drh
|
459 |
if( transport.iCursor>0 ){ |
|
737e76a…
|
drh
|
460 |
for(i=0, j=transport.iCursor; j<transport.nUsed; i++, j++){ |
|
737e76a…
|
drh
|
461 |
transport.pBuf[i] = transport.pBuf[j]; |
|
737e76a…
|
drh
|
462 |
} |
|
737e76a…
|
drh
|
463 |
transport.nUsed -= transport.iCursor; |
|
737e76a…
|
drh
|
464 |
transport.iCursor = 0; |
|
737e76a…
|
drh
|
465 |
} |
|
737e76a…
|
drh
|
466 |
if( transport.nUsed + N > transport.nAlloc ){ |
|
737e76a…
|
drh
|
467 |
char *pNew; |
|
737e76a…
|
drh
|
468 |
transport.nAlloc = transport.nUsed + N; |
|
8f41b2f…
|
drh
|
469 |
pNew = fossil_realloc(transport.pBuf, transport.nAlloc); |
|
737e76a…
|
drh
|
470 |
transport.pBuf = pNew; |
|
737e76a…
|
drh
|
471 |
} |
|
737e76a…
|
drh
|
472 |
if( N>0 ){ |
|
1311841…
|
jan.nijtmans
|
473 |
i = transport_fetch(pUrlData, &transport.pBuf[transport.nUsed], N); |
|
737e76a…
|
drh
|
474 |
if( i>0 ){ |
|
085b6a1…
|
drh
|
475 |
transport.nRcvd += i; |
|
737e76a…
|
drh
|
476 |
transport.nUsed += i; |
|
737e76a…
|
drh
|
477 |
} |
|
737e76a…
|
drh
|
478 |
} |
|
737e76a…
|
drh
|
479 |
} |
|
737e76a…
|
drh
|
480 |
|
|
737e76a…
|
drh
|
481 |
/* |
|
737e76a…
|
drh
|
482 |
** Fetch a single line of input where a line is all text up to the next |
|
737e76a…
|
drh
|
483 |
** \n character or until the end of input. Remove all trailing whitespace |
|
737e76a…
|
drh
|
484 |
** from the received line and zero-terminate the result. Return a pointer |
|
737e76a…
|
drh
|
485 |
** to the line. |
|
737e76a…
|
drh
|
486 |
** |
|
737e76a…
|
drh
|
487 |
** Each call to this routine potentially overwrites the returned buffer. |
|
737e76a…
|
drh
|
488 |
*/ |
|
1311841…
|
jan.nijtmans
|
489 |
char *transport_receive_line(UrlData *pUrlData){ |
|
737e76a…
|
drh
|
490 |
int i; |
|
737e76a…
|
drh
|
491 |
int iStart; |
|
737e76a…
|
drh
|
492 |
|
|
737e76a…
|
drh
|
493 |
i = iStart = transport.iCursor; |
|
737e76a…
|
drh
|
494 |
while(1){ |
|
737e76a…
|
drh
|
495 |
if( i >= transport.nUsed ){ |
|
57f0f89…
|
andybradford
|
496 |
transport_load_buffer(pUrlData, pUrlData->isSsh ? 2 : 10000); |
|
737e76a…
|
drh
|
497 |
i -= iStart; |
|
737e76a…
|
drh
|
498 |
iStart = 0; |
|
737e76a…
|
drh
|
499 |
if( i >= transport.nUsed ){ |
|
737e76a…
|
drh
|
500 |
transport.pBuf[i] = 0; |
|
737e76a…
|
drh
|
501 |
transport.iCursor = i; |
|
737e76a…
|
drh
|
502 |
break; |
|
737e76a…
|
drh
|
503 |
} |
|
737e76a…
|
drh
|
504 |
} |
|
737e76a…
|
drh
|
505 |
if( transport.pBuf[i]=='\n' ){ |
|
737e76a…
|
drh
|
506 |
transport.iCursor = i+1; |
|
2fac809…
|
drh
|
507 |
while( i>=iStart && fossil_isspace(transport.pBuf[i]) ){ |
|
737e76a…
|
drh
|
508 |
transport.pBuf[i] = 0; |
|
737e76a…
|
drh
|
509 |
i--; |
|
737e76a…
|
drh
|
510 |
} |
|
737e76a…
|
drh
|
511 |
break; |
|
737e76a…
|
drh
|
512 |
} |
|
737e76a…
|
drh
|
513 |
i++; |
|
737e76a…
|
drh
|
514 |
} |
|
2163cd9…
|
drh
|
515 |
if( g.fSshTrace ) printf("Got line: [%s]\n", &transport.pBuf[iStart]); |
|
737e76a…
|
drh
|
516 |
return &transport.pBuf[iStart]; |
|
16e703b…
|
drh
|
517 |
} |
|
16e703b…
|
drh
|
518 |
|
|
dbb5e2d…
|
drh
|
519 |
/* |
|
dbb5e2d…
|
drh
|
520 |
** Global transport shutdown |
|
dbb5e2d…
|
drh
|
521 |
*/ |
|
1311841…
|
jan.nijtmans
|
522 |
void transport_global_shutdown(UrlData *pUrlData){ |
|
1311841…
|
jan.nijtmans
|
523 |
if( pUrlData->isSsh ){ |
|
dbb5e2d…
|
drh
|
524 |
transport_ssh_close(); |
|
dbb5e2d…
|
drh
|
525 |
} |
|
1311841…
|
jan.nijtmans
|
526 |
if( pUrlData->isHttps ){ |
|
16e703b…
|
drh
|
527 |
#ifdef FOSSIL_ENABLE_SSL |
|
16e703b…
|
drh
|
528 |
ssl_global_shutdown(); |
|
16e703b…
|
drh
|
529 |
#endif |
|
16e703b…
|
drh
|
530 |
}else{ |
|
16e703b…
|
drh
|
531 |
socket_global_shutdown(); |
|
dbb5e2d…
|
drh
|
532 |
} |
|
dbb5e2d…
|
drh
|
533 |
} |
|
dbb5e2d…
|
drh
|
534 |
|
|
dbb5e2d…
|
drh
|
535 |
/* |
|
dbb5e2d…
|
drh
|
536 |
** Close SSH transport. |
|
dbb5e2d…
|
drh
|
537 |
*/ |
|
dbb5e2d…
|
drh
|
538 |
void transport_ssh_close(void){ |
|
dbb5e2d…
|
drh
|
539 |
if( sshPid ){ |
|
dbb5e2d…
|
drh
|
540 |
/*printf("Closing SSH tunnel: ");*/ |
|
dbb5e2d…
|
drh
|
541 |
fflush(stdout); |
|
dbb5e2d…
|
drh
|
542 |
pclose2(sshIn, sshOut, sshPid); |
|
dbb5e2d…
|
drh
|
543 |
sshPid = 0; |
|
16e703b…
|
drh
|
544 |
} |
|
737e76a…
|
drh
|
545 |
} |