Fossil SCM

fossil-scm / src / http.c
Source Blame History 1099 lines
dbda8d6… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2007 D. Richard Hipp
dbda8d6… drh 3 **
dbda8d6… 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
dbda8d6… 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.
dbda8d6… drh 11 **
dbda8d6… drh 12 ** Author contact information:
dbda8d6… drh 13 ** [email protected]
dbda8d6… drh 14 ** http://www.hwaci.com/drh/
dbda8d6… drh 15 **
dbda8d6… drh 16 *******************************************************************************
dbda8d6… drh 17 **
dbda8d6… drh 18 ** This file contains code that implements the client-side HTTP protocol
dbda8d6… drh 19 */
dbda8d6… drh 20 #include "config.h"
dbda8d6… drh 21 #include "http.h"
737e76a… drh 22 #include <assert.h>
737e76a… drh 23
5d536c5… drh 24 #ifdef _WIN32
5d536c5… drh 25 #include <io.h>
5d536c5… drh 26 #endif
1ad56ce… drh 27
1ad56ce… drh 28
1ad56ce… drh 29 #if INTERFACE
1ad56ce… drh 30 /*
1ad56ce… drh 31 ** Bits of the mHttpFlags parameter to http_exchange()
1ad56ce… drh 32 */
1ad56ce… drh 33 #define HTTP_USE_LOGIN 0x00001 /* Add a login card to the sync message */
1ad56ce… drh 34 #define HTTP_GENERIC 0x00002 /* Generic HTTP request */
1ad56ce… drh 35 #define HTTP_VERBOSE 0x00004 /* HTTP status messages */
1ad56ce… drh 36 #define HTTP_QUIET 0x00008 /* No surplus output */
c31ff76… drh 37 #define HTTP_NOCOMPRESS 0x00010 /* Omit payload compression */
5d536c5… drh 38 #endif
5d536c5… drh 39
5d536c5… drh 40 /* Maximum number of HTTP Authorization attempts */
5d536c5… drh 41 #define MAX_HTTP_AUTH 2
5d536c5… drh 42
5d536c5… drh 43 /* Keep track of HTTP Basic Authorization failures */
5d536c5… drh 44 static int fSeenHttpAuth = 0;
5d536c5… drh 45
a8e33fb… drh 46 /* The N value for most recent http-request-N.txt and http-reply-N.txt
a8e33fb… drh 47 ** trace files.
a8e33fb… drh 48 */
a8e33fb… drh 49 static int traceCnt = 0;
a8e33fb… drh 50
737e76a… drh 51 /*
737e76a… drh 52 ** Construct the "login" card with the client credentials.
737e76a… drh 53 **
737e76a… drh 54 ** login LOGIN NONCE SIGNATURE
737e76a… drh 55 **
737e76a… drh 56 ** The LOGIN is the user id of the client. NONCE is the sha1 checksum
1862890… stephan 57 ** of all payload that follows the login card. Randomness for the
1862890… stephan 58 ** NONCE must be provided in the payload (in xfer.c) (e.g. by
1862890… stephan 59 ** appending a timestamp or random bytes as a comment line to the
1862890… stephan 60 ** payload). SIGNATURE is the sha1 checksum of the nonce followed by
1862890… stephan 61 ** the fossil-hashed version of the user's password.
737e76a… drh 62 **
1862890… stephan 63 ** Write the constructed login card into pLogin. The result does not
1862890… stephan 64 ** have an EOL added to it because which type of EOL it needs has to
1862890… stephan 65 ** be determined later. pLogin is initialized by this routine.
737e76a… drh 66 */
1862890… stephan 67 static void http_build_login_card(Blob * const pPayload, Blob * const pLogin){
596f3c1… drh 68 Blob nonce; /* The nonce */
596f3c1… drh 69 const char *zLogin; /* The user login name */
596f3c1… drh 70 const char *zPw; /* The user password */
596f3c1… drh 71 Blob pw; /* The nonce with user password appended */
596f3c1… drh 72 Blob sig; /* The signature field */
737e76a… drh 73
390b414… drh 74 blob_zero(pLogin);
5fdad9b… drh 75 if( g.url.user==0 || fossil_strcmp(g.url.user, "anonymous")==0 ){
1862890… stephan 76 return; /* No login card for users "nobody" and "anonymous" */
0a51263… drh 77 }
5fdad9b… drh 78 if( g.url.isSsh ){
1862890… stephan 79 return; /* No login card for SSH: */
390b414… drh 80 }
737e76a… drh 81 blob_zero(&nonce);
737e76a… drh 82 blob_zero(&pw);
737e76a… drh 83 sha1sum_blob(pPayload, &nonce);
737e76a… drh 84 blob_copy(&pw, &nonce);
5fdad9b… drh 85 zLogin = g.url.user;
5fdad9b… drh 86 if( g.url.passwd ){
5fdad9b… drh 87 zPw = g.url.passwd;
96722b6… drh 88 }else if( g.cgiOutput ){
96722b6… drh 89 /* Password failure while doing a sync from the web interface */
96722b6… drh 90 cgi_printf("*** incorrect or missing password for user %h\n", zLogin);
96722b6… drh 91 zPw = 0;
390b414… drh 92 }else{
96722b6… drh 93 /* Password failure while doing a sync from the command-line interface */
390b414… drh 94 url_prompt_for_password();
5fdad9b… drh 95 zPw = g.url.passwd;
5d536c5… drh 96 }
5d536c5… drh 97
41ba6ea… drh 98 /* The login card wants the SHA1 hash of the password (as computed by
41ba6ea… drh 99 ** sha1_shared_secret()), not the original password. So convert the
41ba6ea… drh 100 ** password to its SHA1 encoding if it isn't already a SHA1 hash.
41ba6ea… drh 101 **
41ba6ea… drh 102 ** We assume that a hexadecimal string of exactly 40 characters is a
41ba6ea… drh 103 ** SHA1 hash, not an original password. If a user has a password which
41ba6ea… drh 104 ** just happens to be a 40-character hex string, then this routine won't
41ba6ea… drh 105 ** be able to distinguish it from a hash, the translation will not be
275da70… danield 106 ** performed, and the sync won't work.
5d536c5… drh 107 */
41ba6ea… drh 108 if( zPw && zPw[0] && (strlen(zPw)!=40 || !validate16(zPw,40)) ){
41ba6ea… drh 109 const char *zProjectCode = 0;
41ba6ea… drh 110 if( g.url.flags & URL_USE_PARENT ){
41ba6ea… drh 111 zProjectCode = db_get("parent-project-code", 0);
41ba6ea… drh 112 }else{
41ba6ea… drh 113 zProjectCode = db_get("project-code", 0);
41ba6ea… drh 114 }
41ba6ea… drh 115 zPw = sha1_shared_secret(zPw, zLogin, zProjectCode);
2916ec9… florian 116 if( g.url.pwConfig!=0 && (g.url.flags & URL_REMEMBER_PW)!=0 ){
41ba6ea… drh 117 char *x = obscure(zPw);
41ba6ea… drh 118 db_set(g.url.pwConfig/*works-like:"x"*/, x, 0);
41ba6ea… drh 119 fossil_free(x);
41ba6ea… drh 120 }
41ba6ea… drh 121 fossil_free(g.url.passwd);
41ba6ea… drh 122 g.url.passwd = fossil_strdup(zPw);
41ba6ea… drh 123 }
596f3c1… drh 124
596f3c1… drh 125 blob_append(&pw, zPw, -1);
596f3c1… drh 126 sha1sum_blob(&pw, &sig);
1862890… stephan 127 blob_appendf(pLogin, "login %F %b %b", zLogin, &nonce, &sig);
737e76a… drh 128 blob_reset(&pw);
737e76a… drh 129 blob_reset(&sig);
596f3c1… drh 130 blob_reset(&nonce);
596f3c1… drh 131 }
596f3c1… drh 132
596f3c1… drh 133 /*
c4dec3a… danshearer 134 ** Append zPath to pBlob as the target of an HTTP request line (RFC 9112
c4dec3a… danshearer 135 ** section 3.2). zPath is the decoded path from the URL, so it is
c4dec3a… danshearer 136 ** percent-encoded again here: each byte outside ASCII and each
c4dec3a… danshearer 137 ** character that RFC 3986 section 3.3 does not allow in a path is
c4dec3a… danshearer 138 ** written as %XX. '+' is encoded too, because the server decodes it
c4dec3a… danshearer 139 ** as a space.
c4dec3a… danshearer 140 */
c4dec3a… danshearer 141 static void http_append_request_target(Blob *pBlob, const char *zPath){
c4dec3a… danshearer 142 for(; *zPath; zPath++){
c4dec3a… danshearer 143 unsigned char c = (unsigned char)*zPath;
c4dec3a… danshearer 144 if( c<=0x20 || c>=0x7f || strchr("\"#%+<>?[\\]^`{|}", c)!=0 ){
c4dec3a… danshearer 145 blob_appendf(pBlob, "%%%02X", c);
c4dec3a… danshearer 146 }else{
c4dec3a… danshearer 147 blob_append_char(pBlob, (char)c);
c4dec3a… danshearer 148 }
c4dec3a… danshearer 149 }
c4dec3a… danshearer 150 }
c4dec3a… danshearer 151
c4dec3a… danshearer 152 /*
737e76a… drh 153 ** Construct an appropriate HTTP request header. Write the header
737e76a… drh 154 ** into pHdr. This routine initializes the pHdr blob. pPayload is
1862890… stephan 155 ** the complete payload (including the login card if pLogin is NULL or
1862890… stephan 156 ** empty) already compressed.
737e76a… drh 157 */
1ad56ce… drh 158 static void http_build_header(
1ad56ce… drh 159 Blob *pPayload, /* the payload that will be sent */
1ad56ce… drh 160 Blob *pHdr, /* construct the header here */
1862890… stephan 161 Blob *pLogin, /* Login card header value or NULL */
1ad56ce… drh 162 const char *zAltMimetype /* Alternative mimetype */
1ad56ce… drh 163 ){
1ad56ce… drh 164 int nPayload = pPayload ? blob_size(pPayload) : 0;
737e76a… drh 165
737e76a… drh 166 blob_zero(pHdr);
c4dec3a… danshearer 167 blob_appendf(pHdr, "%s ", nPayload>0 ? "POST" : "GET");
2f9920a… drh 168 if( g.url.subpath ){
c4dec3a… danshearer 169 /* The subpath is already percent-encoded. */
c4dec3a… danshearer 170 blob_append(pHdr, g.url.subpath, -1);
2f9920a… drh 171 }else if( g.url.path==0 || g.url.path[0]==0 ){
c4dec3a… danshearer 172 blob_append_char(pHdr, '/');
2f9920a… drh 173 }else{
c4dec3a… danshearer 174 http_append_request_target(pHdr, g.url.path);
2f9920a… drh 175 }
c4dec3a… danshearer 176 blob_appendf(pHdr, " HTTP/1.1\r\n");
5fdad9b… drh 177 if( g.url.proxyAuth ){
5fdad9b… drh 178 blob_appendf(pHdr, "Proxy-Authorization: %s\r\n", g.url.proxyAuth);
5d536c5… drh 179 }
5d536c5… drh 180 if( g.zHttpAuth && g.zHttpAuth[0] ){
5d536c5… drh 181 const char *zCredentials = g.zHttpAuth;
c1506ad… drh 182 char *zEncoded = encode64(zCredentials, -1);
c1506ad… drh 183 blob_appendf(pHdr, "Authorization: Basic %s\r\n", zEncoded);
c1506ad… drh 184 fossil_free(zEncoded);
1311841… jan.nijtmans 185 }
5fdad9b… drh 186 blob_appendf(pHdr, "Host: %s\r\n", g.url.hostname);
c3bd7c3… drh 187 blob_appendf(pHdr, "Connection: close\r\n");
1311841… jan.nijtmans 188 blob_appendf(pHdr, "User-Agent: %s\r\n", get_user_agent());
5fdad9b… drh 189 if( g.url.isSsh ) blob_appendf(pHdr, "X-Fossil-Transport: SSH\r\n");
1862890… stephan 190 if( g.syncInfo.fLoginCardMode>0
1862890… stephan 191 && nPayload>0 && pLogin && blob_size(pLogin) ){
1862890… stephan 192 /* Add sync login card via a transient cookie. We can only do this
1862890… stephan 193 if we know the remote supports it. */
1862890… stephan 194 blob_appendf(pHdr, "Cookie: x-f-l-c=%T\r\n", blob_str(pLogin));
1862890… stephan 195 }
1ad56ce… drh 196 if( nPayload ){
1ad56ce… drh 197 if( zAltMimetype ){
1ad56ce… drh 198 blob_appendf(pHdr, "Content-Type: %s\r\n", zAltMimetype);
1ad56ce… drh 199 }else if( g.fHttpTrace ){
1ad56ce… drh 200 blob_appendf(pHdr, "Content-Type: application/x-fossil-debug\r\n");
1ad56ce… drh 201 }else{
1ad56ce… drh 202 blob_appendf(pHdr, "Content-Type: application/x-fossil\r\n");
1ad56ce… drh 203 }
1ad56ce… drh 204 blob_appendf(pHdr, "Content-Length: %d\r\n", blob_size(pPayload));
5d536c5… drh 205 }
1ad56ce… drh 206 blob_append(pHdr, "\r\n", 2);
5d536c5… drh 207 }
5d536c5… drh 208
5d536c5… drh 209 /*
5d536c5… drh 210 ** Use Fossil credentials for HTTP Basic Authorization prompt
5d536c5… drh 211 */
5d536c5… drh 212 static int use_fossil_creds_for_httpauth_prompt(void){
5d536c5… drh 213 Blob x;
5d536c5… drh 214 char c;
5d536c5… drh 215 prompt_user("Use Fossil username and password (y/N)? ", &x);
5d536c5… drh 216 c = blob_str(&x)[0];
5d536c5… drh 217 blob_reset(&x);
5d536c5… drh 218 return ( c=='y' || c=='Y' );
5d536c5… drh 219 }
5d536c5… drh 220
5d536c5… drh 221 /*
5d536c5… drh 222 ** Prompt to save HTTP Basic Authorization information
5d536c5… drh 223 */
5d536c5… drh 224 static int save_httpauth_prompt(void){
5d536c5… drh 225 Blob x;
5d536c5… drh 226 char c;
5fdad9b… drh 227 if( (g.url.flags & URL_REMEMBER)==0 ) return 0;
5d536c5… drh 228 prompt_user("Remember Basic Authorization credentials (Y/n)? ", &x);
5d536c5… drh 229 c = blob_str(&x)[0];
5d536c5… drh 230 blob_reset(&x);
5d536c5… drh 231 return ( c!='n' && c!='N' );
5d536c5… drh 232 }
5d536c5… drh 233
5d536c5… drh 234 /*
5330d10… jan.nijtmans 235 ** Get the HTTP Basic Authorization credentials from the user
5d536c5… drh 236 ** when 401 is received.
5d536c5… drh 237 */
5d536c5… drh 238 char *prompt_for_httpauth_creds(void){
5d536c5… drh 239 Blob x;
5d536c5… drh 240 char *zUser;
5d536c5… drh 241 char *zPw;
5d536c5… drh 242 char *zPrompt;
5d536c5… drh 243 char *zHttpAuth = 0;
898a70c… stephan 244 if( !fossil_isatty(fossil_fileno(stdin)) ) return 0;
5d536c5… drh 245 zPrompt = mprintf("\n%s authorization required by\n%s\n",
5fdad9b… drh 246 g.url.isHttps==1 ? "Encrypted HTTPS" : "Unencrypted HTTP", g.url.canonical);
49b0ff1… drh 247 fossil_print("%s", zPrompt);
5d536c5… drh 248 free(zPrompt);
5fdad9b… drh 249 if ( g.url.user && g.url.passwd && use_fossil_creds_for_httpauth_prompt() ){
5fdad9b… drh 250 zHttpAuth = mprintf("%s:%s", g.url.user, g.url.passwd);
5d536c5… drh 251 }else{
5d536c5… drh 252 prompt_user("Basic Authorization user: ", &x);
5d536c5… drh 253 zUser = mprintf("%b", &x);
5d536c5… drh 254 zPrompt = mprintf("HTTP password for %b: ", &x);
5d536c5… drh 255 blob_reset(&x);
f423b10… drh 256 prompt_for_password(zPrompt, &x, 0);
5d536c5… drh 257 zPw = mprintf("%b", &x);
5d536c5… drh 258 zHttpAuth = mprintf("%s:%s", zUser, zPw);
5d536c5… drh 259 free(zUser);
5d536c5… drh 260 free(zPw);
5d536c5… drh 261 free(zPrompt);
5d536c5… drh 262 blob_reset(&x);
5d536c5… drh 263 }
5d536c5… drh 264 if( save_httpauth_prompt() ){
5d536c5… drh 265 set_httpauth(zHttpAuth);
5d536c5… drh 266 }
5d536c5… drh 267 return zHttpAuth;
5d536c5… drh 268 }
5d536c5… drh 269
5d536c5… drh 270 /*
33d3bf3… km 271 ** Send content pSend to the server identified by g.url using the
3cd412e… drh 272 ** external program given by g.zHttpCmd. Capture the reply from that
3cd412e… drh 273 ** program and load it into pReply.
3cd412e… drh 274 **
3cd412e… drh 275 ** This routine implements the --transport-command option for "fossil sync".
3cd412e… drh 276 */
3cd412e… drh 277 static int http_exchange_external(
3cd412e… drh 278 Blob *pSend, /* Message to be sent */
3cd412e… drh 279 Blob *pReply, /* Write the reply here */
3cd412e… drh 280 int mHttpFlags, /* Flags. See above */
3cd412e… drh 281 const char *zAltMimetype /* Alternative mimetype if not NULL */
3cd412e… drh 282 ){
3cd412e… drh 283 char *zUplink;
3cd412e… drh 284 char *zDownlink;
3cd412e… drh 285 char *zCmd;
f485a16… drh 286 char *zFullUrl;
3cd412e… drh 287 int rc;
3cd412e… drh 288
3cd412e… drh 289 zUplink = fossil_temp_filename();
3cd412e… drh 290 zDownlink = fossil_temp_filename();
f485a16… drh 291 zFullUrl = url_full(&g.url);
f485a16… drh 292 zCmd = mprintf("%s %$ %$ %$", g.zHttpCmd, zFullUrl,zUplink,zDownlink);
f485a16… drh 293 fossil_free(zFullUrl);
3cd412e… drh 294 blob_write_to_file(pSend, zUplink);
3cd412e… drh 295 if( g.fHttpTrace ){
505d9d4… drh 296 fossil_print("RUN %s\n", zCmd);
3cd412e… drh 297 }
3cd412e… drh 298 rc = fossil_system(zCmd);
3cd412e… drh 299 if( rc ){
3cd412e… drh 300 fossil_warning("Transport command failed: %s\n", zCmd);
275da70… danield 301 }
3cd412e… drh 302 fossil_free(zCmd);
3cd412e… drh 303 file_delete(zUplink);
3cd412e… drh 304 if( file_size(zDownlink, ExtFILE)<0 ){
3cd412e… drh 305 blob_zero(pReply);
3cd412e… drh 306 }else{
3cd412e… drh 307 blob_read_from_file(pReply, zDownlink, ExtFILE);
3cd412e… drh 308 file_delete(zDownlink);
3cd412e… drh 309 }
275da70… danield 310 return rc;
275da70… danield 311 }
275da70… danield 312
efd3a5e… drh 313 /* If iTruth<0 then guess as to whether or not a PATH= argument is required
ac52d12… drh 314 ** when using ssh to run fossil on a remote machine name zHostname. Return
ac52d12… drh 315 ** true if a PATH= should be provided and 0 if not.
efd3a5e… drh 316 **
efd3a5e… drh 317 ** If iTruth is 1 or 0 then that means that the PATH= is or is not required,
efd3a5e… drh 318 ** respectively. Record this fact for future reference.
eb135ef… drh 319 **
ac52d12… drh 320 ** If iTruth is 99 or more, then toggle the value that will be returned
ac52d12… drh 321 ** for future iTruth==(-1) queries.
efd3a5e… drh 322 */
efd3a5e… drh 323 int ssh_needs_path_argument(const char *zHostname, int iTruth){
efd3a5e… drh 324 int ans = 0; /* Default to "no" */
efd3a5e… drh 325 char *z = mprintf("use-path-for-ssh:%s", zHostname);
efd3a5e… drh 326 if( iTruth<0 ){
efd3a5e… drh 327 if( db_get_boolean(z/*works-like:"x"*/, 0) ) ans = 1;
efd3a5e… drh 328 }else{
eb135ef… drh 329 if( iTruth>=99 ){
eb135ef… drh 330 iTruth = !db_get_boolean(z/*works-like:"x"*/, 0);
eb135ef… drh 331 }
eb135ef… drh 332 if( iTruth ){
eb135ef… drh 333 ans = 1;
ed6495b… drh 334 db_set(z/*works-like:"x"*/, "1", 1);
eb135ef… drh 335 }else{
ed6495b… drh 336 db_unset(z/*works-like:"x"*/, 1);
eb135ef… drh 337 }
efd3a5e… drh 338 }
efd3a5e… drh 339 fossil_free(z);
efd3a5e… drh 340 return ans;
efd3a5e… drh 341 }
efd3a5e… drh 342
eb135ef… drh 343 /*
eb135ef… drh 344 ** COMMAND: test-ssh-needs-path
eb135ef… drh 345 **
ac52d12… drh 346 ** Usage: fossil test-ssh-needs-path ?HOSTNAME? ?BOOLEAN?
eb135ef… drh 347 **
eb135ef… drh 348 ** With one argument, show whether or not the PATH= argument is included
eb135ef… drh 349 ** by default for HOSTNAME. If the second argument is a boolean, then
eb135ef… drh 350 ** change the value.
eb135ef… drh 351 **
eb135ef… drh 352 ** With no arguments, show all hosts for which ssh-needs-path is true.
eb135ef… drh 353 */
eb135ef… drh 354 void test_ssh_needs_path(void){
46e7855… drh 355 db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
46e7855… drh 356 db_open_config(0,0);
eb135ef… drh 357 if( g.argc>=3 ){
eb135ef… drh 358 const char *zHost = g.argv[2];
eb135ef… drh 359 int a = -1;
eb135ef… drh 360 int rc;
eb135ef… drh 361 if( g.argc>=4 ) a = is_truth(g.argv[3]);
eb135ef… drh 362 rc = ssh_needs_path_argument(zHost, a);
eb135ef… drh 363 fossil_print("%-20s %s\n", zHost, rc ? "yes" : "no");
eb135ef… drh 364 }else{
eb135ef… drh 365 Stmt s;
46e7855… drh 366 db_swap_connections();
46e7855… drh 367 db_prepare(&s, "SELECT substr(name,18) FROM global_config"
eb135ef… drh 368 " WHERE name GLOB 'use-path-for-ssh:*'");
eb135ef… drh 369 while( db_step(&s)==SQLITE_ROW ){
eb135ef… drh 370 const char *zHost = db_column_text(&s,0);
eb135ef… drh 371 fossil_print("%-20s yes\n", zHost);
eb135ef… drh 372 }
eb135ef… drh 373 db_finalize(&s);
46e7855… drh 374 db_swap_connections();
eb135ef… drh 375 }
eb135ef… drh 376 }
eb135ef… drh 377
e2bdc10… danield 378 /* Add an appropriate PATH= argument to the SSH command under construction
efd3a5e… drh 379 ** in pCmd.
ac52d12… drh 380 **
ac52d12… drh 381 ** About This Feature
ac52d12… drh 382 ** ==================
ac52d12… drh 383 **
ac52d12… drh 384 ** On some ssh servers (Macs in particular are guilty of this) the PATH
ac52d12… drh 385 ** variable in the shell that runs the command that is sent to the remote
ac52d12… drh 386 ** host contains a limited number of read-only system directories:
ac52d12… drh 387 **
ac52d12… drh 388 ** /usr/bin:/bin:/usr/sbin:/sbin
ac52d12… drh 389 **
ac52d12… drh 390 ** The fossil executable cannot be installed into any of those directories
ac52d12… drh 391 ** because they are locked down, and so the "fossil" command cannot run.
ac52d12… drh 392 **
ac52d12… drh 393 ** To work around this, the fossil command is prefixed with the PATH=
ac52d12… drh 394 ** argument, inserted by this function, to augment the PATH with additional
ac52d12… drh 395 ** directories in which the fossil executable is often found.
ac52d12… drh 396 **
ac52d12… drh 397 ** But other ssh servers are confused by this initial PATH= argument.
ac52d12… drh 398 ** Some ssh servers have a list of programs that they are allowed to run
ac52d12… drh 399 ** and will fail if the first argument is not on that list, and PATH=....
ac52d12… drh 400 ** is not on that list.
ac52d12… drh 401 **
ac52d12… drh 402 ** So that various commands that use ssh can run seamlessly on a variety
ac52d12… drh 403 ** of systems (commands that use ssh include "fossil sync" with an ssh:
ac52d12… drh 404 ** URL and the "fossil patch pull" and "fossil patch push" commands where
ac52d12… drh 405 ** the destination directory starts with HOSTNAME: or USER@HOSTNAME:.)
ac52d12… drh 406 ** the following algorithm is used:
ac52d12… drh 407 **
ac52d12… drh 408 ** * First try running the fossil without any PATH= argument. If that
ac52d12… drh 409 ** works (and it does on a majority of systems) then we are done.
ac52d12… drh 410 **
ac52d12… drh 411 ** * If the first attempt fails, then try again after adding the
ac52d12… drh 412 ** PATH= prefix argument. (This function is what adds that
ac52d12… drh 413 ** argument.) If the retry works, then remember that fact using
ac52d12… drh 414 ** the use-path-for-ssh:HOSTNAME setting so that the first step
ac52d12… drh 415 ** is skipped on subsequent uses of the same command.
ac52d12… drh 416 **
ac52d12… drh 417 ** See the forum thread at
ac52d12… drh 418 ** https://fossil-scm.org/forum/forumpost/4903cb4b691af7ce for more
ac52d12… drh 419 ** background.
ac52d12… drh 420 **
ac52d12… drh 421 ** See also:
ac52d12… drh 422 **
ac52d12… drh 423 ** * The ssh_needs_path_argument() function above.
ac52d12… drh 424 ** * The test-ssh-needs-path command that shows the settings
ac52d12… drh 425 ** that cache whether or not a PATH= is needed for a particular
ac52d12… drh 426 ** HOSTNAME.
efd3a5e… drh 427 */
efd3a5e… drh 428 void ssh_add_path_argument(Blob *pCmd){
1862890… stephan 429 blob_append_escaped_arg(pCmd,
efd3a5e… drh 430 "PATH=$HOME/bin:/usr/local/bin:/opt/homebrew/bin:$PATH", 1);
a8e33fb… drh 431 }
a8e33fb… drh 432
a8e33fb… drh 433 /*
a8e33fb… drh 434 ** Return the complete text of the last HTTP reply as saved in the
a8e33fb… drh 435 ** http-reply-N.txt file. This only works if run using --httptrace.
a8e33fb… drh 436 ** Without the --httptrace option, this routine returns a NULL pointer.
a8e33fb… drh 437 ** It still might return a NULL pointer if for some reason it cannot
a8e33fb… drh 438 ** find and open the last http-reply-N.txt file.
a8e33fb… drh 439 */
a8e33fb… drh 440 char *http_last_trace_reply(void){
a8e33fb… drh 441 Blob x;
a8e33fb… drh 442 int n;
a8e33fb… drh 443 char *zFilename;
a8e33fb… drh 444 if( g.fHttpTrace==0 ) return 0;
a8e33fb… drh 445 zFilename = mprintf("http-reply-%d.txt", traceCnt);
a8e33fb… drh 446 n = blob_read_from_file(&x, zFilename, ExtFILE);
a8e33fb… drh 447 fossil_free(zFilename);
a8e33fb… drh 448 if( n<=0 ) return 0;
a8e33fb… drh 449 return blob_str(&x);
1862890… stephan 450 }
1862890… stephan 451
1862890… stephan 452 /*
2343135… danshearer 453 ** Return true if z is a valud extension according to RFC 9112 s 7.1.1.
2343135… danshearer 454 ** In addition to the ordinary checks, bare CR anywhere is invalid
2343135… danshearer 455 ** (section 2.2).
2343135… danshearer 456 */
2343135… danshearer 457 static int http_chunk_ext_ok(const char *z){
2343135… danshearer 458 if( strchr(z, '\r')!=0 ) return 0;
2343135… danshearer 459 while( *z==' ' || *z=='\t' ) z++;
2343135… danshearer 460 if( *z==0 ) return 1;
2343135… danshearer 461 if( *z!=';' ) return 0;
2343135… danshearer 462 z++;
2343135… danshearer 463 while( *z==' ' || *z=='\t' ) z++;
2343135… danshearer 464 return fossil_isalnum(*z)
2343135… danshearer 465 || (*z!=0 && strchr("!#$%&'*+-.^_`|~", *z)!=0);
2343135… danshearer 466 }
2343135… danshearer 467
2343135… danshearer 468 /*
dbda8d6… drh 469 ** Sign the content in pSend, compress it, and send it to the server
737e76a… drh 470 ** via HTTP or HTTPS. Get a reply, uncompress the reply, and store the reply
dbda8d6… drh 471 ** in pRecv. pRecv is assumed to be uninitialized when
dbda8d6… drh 472 ** this routine is called - this routine will initialize it.
dbda8d6… drh 473 **
dbda8d6… drh 474 ** The server address is contain in the "g" global structure. The
dbda8d6… drh 475 ** url_parse() routine should have been called prior to this routine
dbda8d6… drh 476 ** in order to fill this structure appropriately.
dbda8d6… drh 477 */
1ad56ce… drh 478 int http_exchange(
1ad56ce… drh 479 Blob *pSend, /* Message to be sent */
1ad56ce… drh 480 Blob *pReply, /* Write the reply here */
1ad56ce… drh 481 int mHttpFlags, /* Flags. See above */
1ad56ce… drh 482 int maxRedirect, /* Max number of redirects */
1ad56ce… drh 483 const char *zAltMimetype /* Alternative mimetype if not NULL */
1ad56ce… drh 484 ){
737e76a… drh 485 Blob login; /* The login card */
737e76a… drh 486 Blob payload; /* The complete payload including login card */
737e76a… drh 487 Blob hdr; /* The HTTP request header */
737e76a… drh 488 int closeConnection; /* True to close the connection when done */
e1893dc… andybradford 489 int iLength; /* Expected length of the reply payload */
6306914… drh 490 int rc = 0; /* Result code */
737e76a… drh 491 int iHttpVersion; /* Which version of HTTP protocol server uses */
973e3e9… danshearer 492 int nBlank = 0; /* Empty lines to skip after an interim reply */
737e76a… drh 493 char *zLine; /* A single line of the reply header */
737e76a… drh 494 int i; /* Loop counter */
0a51263… drh 495 int isError = 0; /* True if the reply is an error message */
0b6c414… drh 496 int isCompressed = 1; /* True if the reply is compressed */
c3bd7c3… drh 497 int isChunked = 0; /* True if Transfer-Encoding: chunked */
3cd412e… drh 498
3cd412e… drh 499 if( g.zHttpCmd!=0 ){
3cd412e… drh 500 /* Handle the --transport-command option for "fossil sync" and similar */
3cd412e… drh 501 return http_exchange_external(pSend,pReply,mHttpFlags,zAltMimetype);
3cd412e… drh 502 }
3cd412e… drh 503
cbf27ec… drh 504 /* Activate the PATH= auxiliary argument to the ssh command if that
cbf27ec… drh 505 ** is called for.
cbf27ec… drh 506 */
efd3a5e… drh 507 if( g.url.isSsh
efd3a5e… drh 508 && (g.url.flags & URL_SSH_RETRY)==0
2f9920a… drh 509 && g.db!=0
efd3a5e… drh 510 && ssh_needs_path_argument(g.url.hostname, -1)
efd3a5e… drh 511 ){
efd3a5e… drh 512 g.url.flags |= URL_SSH_PATH;
cbf27ec… drh 513 }
cbf27ec… drh 514
5fdad9b… drh 515 if( transport_open(&g.url) ){
49b0ff1… drh 516 fossil_warning("%s", transport_errmsg(&g.url));
9286aaf… drh 517 return 1;
9286aaf… drh 518 }
737e76a… drh 519 /* Construct the login card and prepare the complete payload */
1862890… stephan 520 blob_zero(&login);
1ad56ce… drh 521 if( blob_size(pSend)==0 ){
1ad56ce… drh 522 blob_zero(&payload);
737e76a… drh 523 }else{
1ad56ce… drh 524 if( mHttpFlags & HTTP_USE_LOGIN ) http_build_login_card(pSend, &login);
1862890… stephan 525 if( g.syncInfo.fLoginCardMode ){
1862890… stephan 526 /* The login card will be sent via an HTTP header and/or URL flag. */
1862890… stephan 527 if( g.fHttpTrace || (mHttpFlags & HTTP_NOCOMPRESS)!=0 ){
1862890… stephan 528 /* Maintenance note: we cannot blob_swap(pSend,&payload) here
1862890… stephan 529 ** because the HTTP 401 and redirect response handling below
1862890… stephan 530 ** needs pSend unmodified. payload won't be modified after
1862890… stephan 531 ** this point, so we can make it a proxy for pSend for
1862890… stephan 532 ** zero heap memory. */
1862890… stephan 533 blob_init(&payload, blob_buffer(pSend), blob_size(pSend));
1862890… stephan 534 }else{
1862890… stephan 535 blob_compress(pSend, &payload);
1862890… stephan 536 }
1ad56ce… drh 537 }else{
1862890… stephan 538 /* Prepend the login card (if set) to the payload */
1862890… stephan 539 if( blob_size(&login) ){
1862890… stephan 540 blob_append_char(&login, '\n');
1862890… stephan 541 }
1862890… stephan 542 if( g.fHttpTrace || (mHttpFlags & HTTP_NOCOMPRESS)!=0 ){
1862890… stephan 543 payload = login;
1862890… stephan 544 login = empty_blob/*transfer ownership*/;
1862890… stephan 545 blob_append(&payload, blob_buffer(pSend), blob_size(pSend));
1862890… stephan 546 }else{
1862890… stephan 547 blob_compress2(&login, pSend, &payload);
1862890… stephan 548 blob_reset(&login);
1862890… stephan 549 }
1ad56ce… drh 550 }
737e76a… drh 551 }
737e76a… drh 552
737e76a… drh 553 /* Construct the HTTP request header */
1862890… stephan 554 http_build_header(&payload, &hdr, &login, zAltMimetype);
737e76a… drh 555
737e76a… drh 556 /* When tracing, write the transmitted HTTP message both to standard
737e76a… drh 557 ** output and into a file. The file can then be used to drive the
737e76a… drh 558 ** server-side like this:
737e76a… drh 559 **
abe8923… drh 560 ** ./fossil test-http <http-request-1.txt
737e76a… drh 561 */
737e76a… drh 562 if( g.fHttpTrace ){
dbda8d6… drh 563 char *zOutFile;
dbda8d6… drh 564 FILE *out;
dbda8d6… drh 565 traceCnt++;
abe8923… drh 566 zOutFile = mprintf("http-request-%d.txt", traceCnt);
1e39808… mistachkin 567 out = fopen(zOutFile, "wb");
dbda8d6… drh 568 if( out ){
dbda8d6… drh 569 fwrite(blob_buffer(&hdr), 1, blob_size(&hdr), out);
dbda8d6… drh 570 fwrite(blob_buffer(&payload), 1, blob_size(&payload), out);
dbda8d6… drh 571 fclose(out);
dbda8d6… drh 572 }
abe8923… drh 573 free(zOutFile);
abe8923… drh 574 zOutFile = mprintf("http-reply-%d.txt", traceCnt);
1e39808… mistachkin 575 out = fopen(zOutFile, "wb");
abe8923… drh 576 transport_log(out);
abe8923… drh 577 free(zOutFile);
dbda8d6… drh 578 }
737e76a… drh 579
737e76a… drh 580 /*
737e76a… drh 581 ** Send the request to the server.
737e76a… drh 582 */
1ad56ce… drh 583 if( mHttpFlags & HTTP_VERBOSE ){
1ad56ce… drh 584 fossil_print("URL: %s\n", g.url.canonical);
1ad56ce… drh 585 fossil_print("Sending %d byte header and %d byte payload\n",
1ad56ce… drh 586 blob_size(&hdr), blob_size(&payload));
1ad56ce… drh 587 }
5fdad9b… drh 588 transport_send(&g.url, &hdr);
5fdad9b… drh 589 transport_send(&g.url, &payload);
dbda8d6… drh 590 blob_reset(&hdr);
dbda8d6… drh 591 blob_reset(&payload);
5fdad9b… drh 592 transport_flip(&g.url);
136dbe9… drh 593 if( mHttpFlags & HTTP_VERBOSE ){
136dbe9… drh 594 fossil_print("IP-Address: %s\n", g.zIpAddr);
136dbe9… drh 595 }
5330d10… jan.nijtmans 596
737e76a… drh 597 /*
737e76a… drh 598 ** Read and interpret the server reply
737e76a… drh 599 */
737e76a… drh 600 closeConnection = 1;
737e76a… drh 601 iLength = -1;
a8e33fb… drh 602 iHttpVersion = -1;
973e3e9… danshearer 603 while( (zLine = transport_receive_line(&g.url))!=0 ){
973e3e9… danshearer 604 if( zLine[0]==0 ){
973e3e9… danshearer 605 if( nBlank>0 ){
973e3e9… danshearer 606 /* A server may send an empty line after an interim reply,
973e3e9… danshearer 607 ** before the status line of the final reply. */
973e3e9… danshearer 608 nBlank--;
973e3e9… danshearer 609 continue;
973e3e9… danshearer 610 }
973e3e9… danshearer 611 break;
973e3e9… danshearer 612 }
973e3e9… danshearer 613 nBlank = 0;
1ad56ce… drh 614 if( mHttpFlags & HTTP_VERBOSE ){
1ad56ce… drh 615 fossil_print("Read: [%s]\n", zLine);
c272f25… danshearer 616 }
c272f25… danshearer 617 if( zLine[0]==' ' || zLine[0]=='\t' ){
c272f25… danshearer 618 /* A line starting with a space or tab continues the previous
c272f25… danshearer 619 ** header (obsolete line folding, RFC 9112 section 5.2) and in this
c272f25… danshearer 620 ** case we disregard the RFC because it gets messy. This is what
c272f25… danshearer 621 ** the llhttp project and others do. */
c272f25… danshearer 622 fossil_warning("server sent a folded header line");
c272f25… danshearer 623 goto write_err;
095976a… danshearer 624 }
0b6c414… drh 625 if( fossil_strnicmp(zLine, "http/1.", 7)==0 ){
737e76a… drh 626 if( sscanf(zLine, "HTTP/1.%d %d", &iHttpVersion, &rc)!=2 ) goto write_err;
095976a… danshearer 627 if( rc/100==1 ){
2cdf86f… danshearer 628 /* Parse and discard an unexpected 1xx interim reply, most commonly
2cdf86f… danshearer 629 ** "100 Continue". Skip the remaining header lines of the reply.
2cdf86f… danshearer 630 ** A 1xx reply has no body.) */
095976a… danshearer 631 while( (zLine = transport_receive_line(&g.url))!=0 && zLine[0]!=0 ){}
bd777e5… danshearer 632 iHttpVersion = -1; /* Ensures correct error msg if connection drops */
095976a… danshearer 633 if( zLine==0 ) goto write_err;
973e3e9… danshearer 634 nBlank = 4;
095976a… danshearer 635 continue;
095976a… danshearer 636 }
5d536c5… drh 637 if( rc==401 ){
5d536c5… drh 638 if( fSeenHttpAuth++ < MAX_HTTP_AUTH ){
5d536c5… drh 639 if( g.zHttpAuth ){
5d536c5… drh 640 if( g.zHttpAuth ) free(g.zHttpAuth);
5d536c5… drh 641 }
5d536c5… drh 642 g.zHttpAuth = prompt_for_httpauth_creds();
5fdad9b… drh 643 transport_close(&g.url);
1ad56ce… drh 644 return http_exchange(pSend, pReply, mHttpFlags,
1ad56ce… drh 645 maxRedirect, zAltMimetype);
5d536c5… drh 646 }
5d536c5… drh 647 }
f2ca449… drh 648 if( rc!=200 && rc!=301 && rc!=302 && rc!=307 && rc!=308 ){
0690aa1… drh 649 int ii;
0690aa1… drh 650 for(ii=7; zLine[ii] && zLine[ii]!=' '; ii++){}
6454153… drh 651 while( zLine[ii]==' ' ) ii++;
8fa8fe8… drh 652 if( (mHttpFlags & HTTP_QUIET)==0 ){
8fa8fe8… drh 653 fossil_warning("server says: %s", &zLine[ii]);
8fa8fe8… drh 654 }
0690aa1… drh 655 goto write_err;
0690aa1… drh 656 }
737e76a… drh 657 if( iHttpVersion==0 ){
737e76a… drh 658 closeConnection = 1;
737e76a… drh 659 }else{
737e76a… drh 660 closeConnection = 0;
737e76a… drh 661 }
5fdad9b… drh 662 }else if( g.url.isSsh && fossil_strnicmp(zLine, "status:", 7)==0 ){
dbb5e2d… drh 663 if( sscanf(zLine, "Status: %d", &rc)!=1 ) goto write_err;
f2ca449… drh 664 if( rc!=200 && rc!=301 && rc!=302 && rc!=307 && rc!=308 ){
dbb5e2d… drh 665 int ii;
dbb5e2d… drh 666 for(ii=7; zLine[ii] && zLine[ii]!=' '; ii++){}
dbb5e2d… drh 667 while( zLine[ii]==' ' ) ii++;
dbb5e2d… drh 668 fossil_warning("server says: %s", &zLine[ii]);
dbb5e2d… drh 669 goto write_err;
dbb5e2d… drh 670 }
a8e33fb… drh 671 if( iHttpVersion<0 ) iHttpVersion = 1;
dbb5e2d… drh 672 closeConnection = 0;
0b6c414… drh 673 }else if( fossil_strnicmp(zLine, "content-length:", 15)==0 ){
4df4f6c… danshearer 674 /* The value is one or more digits, optionally followed by
4df4f6c… danshearer 675 ** spaces, and fits in an int. Any other value is an error, and
4df4f6c… danshearer 676 ** so is a second Content-Length with a different value. */
4df4f6c… danshearer 677 sqlite3_int64 n = 0;
4df4f6c… danshearer 678 int iStart;
2fac809… drh 679 for(i=15; fossil_isspace(zLine[i]); i++){}
4df4f6c… danshearer 680 for(iStart=i; fossil_isdigit(zLine[i]) && n<=0x7fffffff; i++){
4df4f6c… danshearer 681 n = n*10 + zLine[i] - '0';
4df4f6c… danshearer 682 }
4df4f6c… danshearer 683 if( i==iStart || n>0x7fffffff ){
4df4f6c… danshearer 684 fossil_warning("server sent a bad content-length: %s", zLine);
4df4f6c… danshearer 685 goto write_err;
4df4f6c… danshearer 686 }
4df4f6c… danshearer 687 for(; fossil_isspace(zLine[i]); i++){}
4df4f6c… danshearer 688 if( zLine[i]!=0 ){
4df4f6c… danshearer 689 fossil_warning("server sent a bad content-length: %s", zLine);
4df4f6c… danshearer 690 goto write_err;
4df4f6c… danshearer 691 }
4df4f6c… danshearer 692 if( iLength>=0 && iLength!=(int)n ){
4df4f6c… danshearer 693 fossil_warning("server sent two different content-length values");
4df4f6c… danshearer 694 goto write_err;
4df4f6c… danshearer 695 }
4df4f6c… danshearer 696 iLength = (int)n;
c3bd7c3… drh 697 }else if( fossil_strnicmp(zLine, "transfer-encoding:", 18)==0 ){
b176932… danshearer 698 /* Fossil never sends a "transfer-encoding:" request header, so we may
b176932… danshearer 699 ** reject any encoding other than "chunked", including "gzip, chunked",
b176932… danshearer 700 ** "superchunked", "deflate" etc. */
b176932… danshearer 701 int bOk = 0;
b176932… danshearer 702 for(i=18; fossil_isspace(zLine[i]); i++){}
b176932… danshearer 703 if( fossil_strnicmp(&zLine[i], "chunked", 7)==0 ){
b176932… danshearer 704 bOk = zLine[i+7]==0;
b176932… danshearer 705 }
b176932… danshearer 706 if( !bOk ){
b176932… danshearer 707 fossil_warning("unsupported transfer-encoding: %s", &zLine[18]);
c272f25… danshearer 708 goto write_err;
c272f25… danshearer 709 }
c272f25… danshearer 710 if( isChunked ){
c272f25… danshearer 711 /* (RFC 9110 section 5.3), chunked may be applied only once */
c272f25… danshearer 712 fossil_warning("server sent two transfer-encoding headers");
b176932… danshearer 713 goto write_err;
c3bd7c3… drh 714 }
b176932… danshearer 715 isChunked = 1;
0b6c414… drh 716 }else if( fossil_strnicmp(zLine, "connection:", 11)==0 ){
5f47bb5… drh 717 if( sqlite3_strlike("%close%", &zLine[11], 0)==0 ){
737e76a… drh 718 closeConnection = 1;
5f47bb5… drh 719 }else if( sqlite3_strlike("%keep-alive%", &zLine[11], 0)==0 ){
737e76a… drh 720 closeConnection = 0;
737e76a… drh 721 }
f2ca449… drh 722 }else if( ( rc==301 || rc==302 || rc==307 || rc==308 ) &&
2678d87… andybradford 723 fossil_strnicmp(zLine, "location:", 9)==0 ){
6454153… drh 724 int i, j;
c19f782… drh 725 int wasHttps;
754e2c1… drh 726 int priorUrlFlags;
fe56e5a… jan.nijtmans 727
fe56e5a… jan.nijtmans 728 if ( --maxRedirect == 0){
8fa8fe8… drh 729 if( (mHttpFlags & HTTP_QUIET)==0 ){
8fa8fe8… drh 730 fossil_warning("redirect limit exceeded");
8fa8fe8… drh 731 }
16da1b6… andybradford 732 goto write_err;
fe56e5a… jan.nijtmans 733 }
6454153… drh 734 for(i=9; zLine[i] && zLine[i]==' '; i++){}
16da1b6… andybradford 735 if( zLine[i]==0 ){
16da1b6… andybradford 736 fossil_warning("malformed redirect: %s", zLine);
16da1b6… andybradford 737 goto write_err;
16da1b6… andybradford 738 }
5330d10… jan.nijtmans 739 j = strlen(zLine) - 1;
32ad9a1… drh 740 while( j>4 && fossil_strcmp(&zLine[j-4],"/xfer")==0 ){
a921be7… drh 741 j -= 4;
a921be7… drh 742 zLine[j] = 0;
a921be7… drh 743 }
1ad56ce… drh 744 if( (mHttpFlags & HTTP_QUIET)==0 ){
1ad56ce… drh 745 fossil_print("redirect with status %d to %s\n", rc, &zLine[i]);
1ad56ce… drh 746 }
f741baa… drh 747 if( g.url.isFile || g.url.isSsh ){
8fa8fe8… drh 748 if( (mHttpFlags & HTTP_QUIET)==0 ){
8fa8fe8… drh 749 fossil_warning("cannot redirect from %s to %s", g.url.canonical,
8fa8fe8… drh 750 &zLine[i]);
8fa8fe8… drh 751 }
f741baa… drh 752 goto write_err;
f741baa… drh 753 }
c19f782… drh 754 wasHttps = g.url.isHttps;
754e2c1… drh 755 priorUrlFlags = g.url.flags;
df12c1b… drh 756 url_parse(&zLine[i], 0);
c19f782… drh 757 if( wasHttps && !g.url.isHttps ){
8fa8fe8… drh 758 if( (mHttpFlags & HTTP_QUIET)==0 ){
8fa8fe8… drh 759 fossil_warning("cannot redirect from HTTPS to HTTP");
8fa8fe8… drh 760 }
f741baa… drh 761 goto write_err;
f741baa… drh 762 }
f741baa… drh 763 if( g.url.isSsh || g.url.isFile ){
8fa8fe8… drh 764 if( (mHttpFlags & HTTP_QUIET)==0 ){
8fa8fe8… drh 765 fossil_warning("cannot redirect to %s", &zLine[i]);
8fa8fe8… drh 766 }
c19f782… drh 767 goto write_err;
f741baa… drh 768 }
3a00b61… drh 769 transport_close(&g.url);
3a00b61… drh 770 transport_global_shutdown(&g.url);
5d536c5… drh 771 fSeenHttpAuth = 0;
5d536c5… drh 772 if( g.zHttpAuth ) free(g.zHttpAuth);
5d536c5… drh 773 g.zHttpAuth = get_httpauth();
754e2c1… drh 774 if( (rc==301 || rc==308) && (priorUrlFlags & URL_REMEMBER)!=0 ){
754e2c1… drh 775 g.url.flags |= URL_REMEMBER;
754e2c1… drh 776 url_remember();
754e2c1… drh 777 }
1ad56ce… drh 778 return http_exchange(pSend, pReply, mHttpFlags,
1ad56ce… drh 779 maxRedirect, zAltMimetype);
0b6c414… drh 780 }else if( fossil_strnicmp(zLine, "content-type: ", 14)==0 ){
0b6c414… drh 781 if( fossil_strnicmp(&zLine[14], "application/x-fossil-debug", -1)==0 ){
0b6c414… drh 782 isCompressed = 0;
5330d10… jan.nijtmans 783 }else if( fossil_strnicmp(&zLine[14],
0b6c414… drh 784 "application/x-fossil-uncompressed", -1)==0 ){
0b6c414… drh 785 isCompressed = 0;
1ad56ce… drh 786 }else{
66ef906… drh 787 if( mHttpFlags & HTTP_GENERIC ){
66ef906… drh 788 if( mHttpFlags & HTTP_NOCOMPRESS ) isCompressed = 0;
66ef906… drh 789 }else if( fossil_strnicmp(&zLine[14], "application/x-fossil", -1)!=0 ){
1ad56ce… drh 790 isError = 1;
1ad56ce… drh 791 }
1e39808… mistachkin 792 }
1e39808… mistachkin 793 }
1e39808… mistachkin 794 }
a8e33fb… drh 795 if( iHttpVersion<0 ){
cbf27ec… drh 796 /* We got nothing back from the server. If using the ssh: protocol,
cbf27ec… drh 797 ** this might mean we need to add or remove the PATH=... argument
cbf27ec… drh 798 ** to the SSH command being sent. If that is the case, retry the
cbf27ec… drh 799 ** request after adding or removing the PATH= argument.
cbf27ec… drh 800 */
cbf27ec… drh 801 if( g.url.isSsh /* This is an SSH: sync */
cbf27ec… drh 802 && (g.url.flags & URL_SSH_EXE)==0 /* Does not have ?fossil=.... */
cbf27ec… drh 803 && (g.url.flags & URL_SSH_RETRY)==0 /* Not retried already */
cbf27ec… drh 804 ){
cbf27ec… drh 805 /* Retry after flipping the SSH_PATH setting */
cbf27ec… drh 806 transport_close(&g.url);
2f9920a… drh 807 if( (mHttpFlags & HTTP_QUIET)==0 ){
2f9920a… drh 808 fossil_print(
2f9920a… drh 809 "First attempt to run fossil on %s using SSH failed.\n"
2f9920a… drh 810 "Retrying %s the PATH= argument.\n",
2f9920a… drh 811 g.url.hostname,
2f9920a… drh 812 (g.url.flags & URL_SSH_PATH)!=0 ? "without" : "with"
2f9920a… drh 813 );
2f9920a… drh 814 }
cbf27ec… drh 815 g.url.flags ^= URL_SSH_PATH|URL_SSH_RETRY;
cbf27ec… drh 816 rc = http_exchange(pSend,pReply,mHttpFlags,0,zAltMimetype);
2f9920a… drh 817 if( rc==0 && g.db!=0 ){
efd3a5e… drh 818 (void)ssh_needs_path_argument(g.url.hostname,
efd3a5e… drh 819 (g.url.flags & URL_SSH_PATH)!=0);
cbf27ec… drh 820 }
cbf27ec… drh 821 return rc;
cbf27ec… drh 822 }else{
cbf27ec… drh 823 /* The problem could not be corrected by retrying. Report the
cbf27ec… drh 824 ** the error. */
8fa8fe8… drh 825 if( mHttpFlags & HTTP_QUIET ){
8fa8fe8… drh 826 /* no-op */
8fa8fe8… drh 827 }else if( g.url.isSsh && !g.fSshTrace ){
fb156a1… drh 828 fossil_warning("server did not reply: "
fb156a1… drh 829 " rerun with --sshtrace for diagnostics");
fb156a1… drh 830 }else{
fb156a1… drh 831 fossil_warning("server did not reply");
fb156a1… drh 832 }
cbf27ec… drh 833 goto write_err;
cbf27ec… drh 834 }
6454153… drh 835 }
6454153… drh 836 if( rc!=200 ){
8fa8fe8… drh 837 if( mHttpFlags & HTTP_QUIET ) goto write_err;
2678d87… andybradford 838 fossil_warning("\"location:\" missing from %d redirect reply", rc);
8fa8fe8… drh 839 goto write_err;
8fa8fe8… drh 840 }
8fa8fe8… drh 841
63826bf… danshearer 842 if( isChunked && iLength>=0 ){
63826bf… danshearer 843 /* RFC 7230 says to reject in this case */
63826bf… danshearer 844 fossil_warning("reply has both content-length and transfer-encoding");
63826bf… danshearer 845 goto write_err;
63826bf… danshearer 846 }
63826bf… danshearer 847
737e76a… drh 848 /*
737e76a… drh 849 ** Extract the reply payload that follows the header
737e76a… drh 850 */
0a51263… drh 851 blob_zero(pReply);
c3bd7c3… drh 852 if( isChunked ){
c3bd7c3… drh 853 /* Decode an HTTP/1.1 "Transfer-Encoding: chunked" reply body. Each
c3bd7c3… drh 854 ** chunk is a hex length on its own line (optionally followed by a
c3bd7c3… drh 855 ** ";extension" that is ignored), then that many payload bytes, then a
c3bd7c3… drh 856 ** bare CRLF. A zero-length chunk terminates the body, after which any
c3bd7c3… drh 857 ** trailer header lines are read and discarded up to the blank line. */
c3bd7c3… drh 858 char *zChunk;
c3bd7c3… drh 859 int sawTerminator = 0; /* True once the 0-length chunk is seen */
c3bd7c3… drh 860 while( (zChunk = transport_receive_line(&g.url))!=0 && zChunk[0]!=0 ){
2343135… danshearer 861 i64 nChunk = 0; /* Size of this chunk in bytes (wide, unclamped) */
c3bd7c3… drh 862 i64 nPrior; /* Bytes already in pReply (matches blob nUsed) */
2343135… danshearer 863 int j; /* Number of hex digits in the chunk size */
2343135… danshearer 864 /* The size is one or more hex digits (RFC 9112 section 7.1),
2343135… danshearer 865 ** with no sign, "0x" or leading space. */
2343135… danshearer 866 for(j=0; fossil_isXdigit(zChunk[j]) && nChunk<=0x7fffffff; j++){
2343135… danshearer 867 nChunk = nChunk*16 + hex_digit_value(zChunk[j]);
2343135… danshearer 868 }
2343135… danshearer 869 if( j==0 ){
c3bd7c3… drh 870 /* No hex digit consumed: a blank or malformed chunk-size line, which
c3bd7c3… drh 871 ** is the symptom of a connection that closed mid-stream. Treat it as
c3bd7c3… drh 872 ** a truncated (failed) */
c3bd7c3… drh 873 fossil_warning("chunked reply: missing or malformed chunk size");
c3bd7c3… drh 874 goto write_err;
c3bd7c3… drh 875 }
2343135… danshearer 876 if( nChunk>0x7fffffff ){
2343135… danshearer 877 /* Larger than we will ever accept in one chunk. */
c3bd7c3… drh 878 fossil_warning("chunked reply: invalid chunk size");
2343135… danshearer 879 goto write_err;
2343135… danshearer 880 }
2343135… danshearer 881 if( !http_chunk_ext_ok(&zChunk[j]) ){
2343135… danshearer 882 fossil_warning("chunked reply: malformed chunk size line");
c3bd7c3… drh 883 goto write_err;
c3bd7c3… drh 884 }
c3bd7c3… drh 885 if( nChunk==0 ){
c3bd7c3… drh 886 /* Final chunk: consume trailer lines up to the terminating blank. */
c3bd7c3… drh 887 sawTerminator = 1;
c3bd7c3… drh 888 while( (zChunk = transport_receive_line(&g.url))!=0 && zChunk[0]!=0 ){}
c3bd7c3… drh 889 break;
c3bd7c3… drh 890 }
c3bd7c3… drh 891 nPrior = blob_size(pReply);
b63d654… danshearer 892 /* Grow the reply buffer, then restore nUsed, so that on error the
b63d654… danshearer 893 ** blob's reported size is bytes received not claimed chunk length */
b63d654… danshearer 894 blob_resize(pReply, (u64)(nPrior+nChunk));
b63d654… danshearer 895 pReply->nUsed = (unsigned int)nPrior;
c3bd7c3… drh 896 {
c3bd7c3… drh 897 unsigned int nRemaining = (unsigned int)nChunk;
c3bd7c3… drh 898 /* transport_receive() may return short; loop until the chunk is
c3bd7c3… drh 899 ** full. */
c3bd7c3… drh 900 while( nRemaining>0 ){
c3bd7c3… drh 901 int nGot;
c3bd7c3… drh 902 nGot = transport_receive(&g.url, &pReply->aData[nPrior], nRemaining);
c3bd7c3… drh 903 if( nGot<=0 ){
c3bd7c3… drh 904 fossil_warning("chunked reply truncated");
c3bd7c3… drh 905 goto write_err;
c3bd7c3… drh 906 }
c3bd7c3… drh 907 nPrior += nGot;
c3bd7c3… drh 908 nRemaining -= nGot;
c3bd7c3… drh 909 pReply->nUsed = (unsigned int)nPrior;
c3bd7c3… drh 910 }
c3bd7c3… drh 911 }
e288a41… danshearer 912 /* The chunk data ends with CRLF (RFC 9112 section 7.1), which
e288a41… danshearer 913 ** reads as an empty line. Anything else means the chunk size
e288a41… danshearer 914 ** was wrong. */
e288a41… danshearer 915 zChunk = transport_receive_line(&g.url);
e288a41… danshearer 916 if( zChunk==0 || zChunk[0]!=0 ){
e288a41… danshearer 917 fossil_warning("chunked reply: no line end after chunk data");
e288a41… danshearer 918 goto write_err;
e288a41… danshearer 919 }
c3bd7c3… drh 920 }
c3bd7c3… drh 921 if( !sawTerminator ){
c3bd7c3… drh 922 /* The loop exited without ever seeing the 0-length terminator chunk,
c3bd7c3… drh 923 ** meaning the peer closed before the body was complete. A truncated
c3bd7c3… drh 924 ** sync must be an error, never silent success. */
c3bd7c3… drh 925 fossil_warning("chunked reply ended without terminator");
c3bd7c3… drh 926 goto write_err;
c3bd7c3… drh 927 }
c3bd7c3… drh 928 }else if( iLength==0 ){
a8e33fb… drh 929 /* No content to read */
a8e33fb… drh 930 }else if( iLength>0 ){
a8e33fb… drh 931 /* Read content of a known length */
a8e33fb… drh 932 int iRecvLen; /* Received length of the reply payload */
a8e33fb… drh 933 blob_resize(pReply, iLength);
a8e33fb… drh 934 iRecvLen = transport_receive(&g.url, blob_buffer(pReply), iLength);
a8e33fb… drh 935 if( mHttpFlags & HTTP_VERBOSE ){
a8e33fb… drh 936 fossil_print("Reply received: %d of %d bytes\n", iRecvLen, iLength);
a8e33fb… drh 937 }
a8e33fb… drh 938 if( iRecvLen != iLength ){
8fa8fe8… drh 939 if( mHttpFlags & HTTP_QUIET ) goto write_err;
a8e33fb… drh 940 fossil_warning("response truncated: got %d bytes of %d",
a8e33fb… drh 941 iRecvLen, iLength);
a8e33fb… drh 942 goto write_err;
a8e33fb… drh 943 }
aa7a087… drh 944 }else{
a8e33fb… drh 945 /* Read content until end-of-file */
a8e33fb… drh 946 int iRecvLen; /* Received length of the reply payload */
a8e33fb… drh 947 unsigned int nReq = 1000;
a8e33fb… drh 948 unsigned int nPrior = 0;
aa7a087… drh 949 closeConnection = 1;
a8e33fb… drh 950 do{
a8e33fb… drh 951 nReq *= 2;
a8e33fb… drh 952 blob_resize(pReply, nPrior+nReq);
a8e33fb… drh 953 iRecvLen = transport_receive(&g.url, &pReply->aData[nPrior], (int)nReq);
a8e33fb… drh 954 nPrior += iRecvLen;
a8e33fb… drh 955 pReply->nUsed = nPrior;
a8e33fb… drh 956 }while( iRecvLen==nReq && nReq<0x20000000 );
a8e33fb… drh 957 if( mHttpFlags & HTTP_VERBOSE ){
a8e33fb… drh 958 fossil_print("Reply received: %u bytes (w/o content-length)\n", nPrior);
a8e33fb… drh 959 }
e1893dc… andybradford 960 }
0a51263… drh 961 if( isError ){
0a51263… drh 962 char *z;
0a51263… drh 963 int i, j;
0a51263… drh 964 z = blob_str(pReply);
0a51263… drh 965 for(i=j=0; z[i]; i++, j++){
0a51263… drh 966 if( z[i]=='<' ){
0a51263… drh 967 while( z[i] && z[i]!='>' ) i++;
0a51263… drh 968 if( z[i]==0 ) break;
0a51263… drh 969 }
0a51263… drh 970 z[j] = z[i];
0a51263… drh 971 }
0a51263… drh 972 z[j] = 0;
8fa8fe8… drh 973 if( mHttpFlags & HTTP_QUIET ){
8fa8fe8… drh 974 /* no-op */
8fa8fe8… drh 975 }else if( mHttpFlags & HTTP_VERBOSE ){
8fa8fe8… drh 976 fossil_warning("server sends error: %s", z);
8fa8fe8… drh 977 }else{
8fa8fe8… drh 978 fossil_warning("server sends error");
8fa8fe8… drh 979 }
16da1b6… andybradford 980 goto write_err;
737e76a… drh 981 }
0b6c414… drh 982 if( isCompressed ) blob_uncompress(pReply, pReply);
737e76a… drh 983
737e76a… drh 984 /*
737e76a… drh 985 ** Close the connection to the server if appropriate.
767ae79… drh 986 **
767ae79… drh 987 ** FIXME: There is some bug in the lower layers that prevents the
767ae79… drh 988 ** connection from remaining open. The easiest fix for now is to
767ae79… drh 989 ** simply close and restart the connection for each round-trip.
dbb5e2d… drh 990 **
dbb5e2d… drh 991 ** For SSH we will leave the connection open.
737e76a… drh 992 */
5fdad9b… drh 993 if( ! g.url.isSsh ) closeConnection = 1; /* FIX ME */
737e76a… drh 994 if( closeConnection ){
5fdad9b… drh 995 transport_close(&g.url);
63ef585… drh 996 }else{
5fdad9b… drh 997 transport_rewind(&g.url);
737e76a… drh 998 }
9286aaf… drh 999 return 0;
737e76a… drh 1000
5330d10… jan.nijtmans 1001 /*
737e76a… drh 1002 ** Jump to here if an error is seen.
737e76a… drh 1003 */
737e76a… drh 1004 write_err:
8fa8fe8… drh 1005 g.iResultCode = 1;
5fdad9b… drh 1006 transport_close(&g.url);
5330d10… jan.nijtmans 1007 return 1;
1ad56ce… drh 1008 }
1ad56ce… drh 1009
1ad56ce… drh 1010 /*
1ad56ce… drh 1011 ** COMMAND: test-httpmsg
1ad56ce… drh 1012 **
450cfbb… drh 1013 ** Usage: %fossil test-httpmsg ?OPTIONS? URL ?PAYLOAD? ?OUTPUT?
1ad56ce… drh 1014 **
1ad56ce… drh 1015 ** Send an HTTP message to URL and get the reply. PAYLOAD is a file containing
9cabc13… florian 1016 ** the payload, or "-" to read payload from standard input. A POST message
1ad56ce… drh 1017 ** is sent if PAYLOAD is specified and is non-empty. If PAYLOAD is omitted
1ad56ce… drh 1018 ** or is an empty file, then a GET message is sent.
1ad56ce… drh 1019 **
450cfbb… drh 1020 ** If a second filename (OUTPUT) is given after PAYLOAD, then the reply
450cfbb… drh 1021 ** is written into that second file instead of being written on standard
450cfbb… drh 1022 ** output. Use the "--out OUTPUT" option to specify an output file for
450cfbb… drh 1023 ** a GET request where there is no PAYLOAD.
1ad56ce… drh 1024 **
450cfbb… drh 1025 ** Options:
c31ff76… drh 1026 ** --compress Use ZLIB compression on the payload
1ad56ce… drh 1027 ** --mimetype TYPE Mimetype of the payload
3380c7f… drh 1028 ** --no-cert-verify Disable TLS cert verification
1ad56ce… drh 1029 ** --out FILE Store the reply in FILE
2f9920a… drh 1030 ** --subpath PATH HTTP request path for ssh: and file: URLs
1ad56ce… drh 1031 ** -v Verbose output
450cfbb… drh 1032 ** --xfer PAYLOAD in a Fossil xfer protocol message
1ad56ce… drh 1033 */
77be177… andygoth 1034 void test_httpmsg_command(void){
1ad56ce… drh 1035 const char *zMimetype;
1ad56ce… drh 1036 const char *zInFile;
1ad56ce… drh 1037 const char *zOutFile;
2f9920a… drh 1038 const char *zSubpath;
1ad56ce… drh 1039 Blob in, out;
c31ff76… drh 1040 unsigned int mHttpFlags = HTTP_GENERIC|HTTP_NOCOMPRESS;
1ad56ce… drh 1041
1ad56ce… drh 1042 zMimetype = find_option("mimetype",0,1);
1ad56ce… drh 1043 zOutFile = find_option("out","o",1);
1ad56ce… drh 1044 if( find_option("verbose","v",0)!=0 ) mHttpFlags |= HTTP_VERBOSE;
c31ff76… drh 1045 if( find_option("compress",0,0)!=0 ) mHttpFlags &= ~HTTP_NOCOMPRESS;
3380c7f… drh 1046 if( find_option("no-cert-verify",0,0)!=0 ){
aa0b696… florian 1047 #ifdef FOSSIL_ENABLE_SSL
3380c7f… drh 1048 ssl_disable_cert_verification();
aa0b696… florian 1049 #endif
3380c7f… drh 1050 }
450cfbb… drh 1051 if( find_option("xfer",0,0)!=0 ){
450cfbb… drh 1052 mHttpFlags |= HTTP_USE_LOGIN;
450cfbb… drh 1053 mHttpFlags &= ~HTTP_GENERIC;
450cfbb… drh 1054 }
6f55dbd… drh 1055 if( find_option("ipv4",0,0) ) g.eIPvers = 1;
6f55dbd… drh 1056 if( find_option("ipv6",0,0) ) g.eIPvers = 2;
2f9920a… drh 1057 zSubpath = find_option("subpath",0,1);
450cfbb… drh 1058 verify_all_options();
450cfbb… drh 1059 if( g.argc<3 || g.argc>5 ){
450cfbb… drh 1060 usage("URL ?PAYLOAD? ?OUTPUT?");
1ad56ce… drh 1061 }
3cd412e… drh 1062 zInFile = g.argc>=4 ? g.argv[3] : 0;
450cfbb… drh 1063 if( g.argc==5 ){
450cfbb… drh 1064 if( zOutFile ){
450cfbb… drh 1065 fossil_fatal("output file specified twice: \"--out %s\" and \"%s\"",
450cfbb… drh 1066 zOutFile, g.argv[4]);
450cfbb… drh 1067 }
450cfbb… drh 1068 zOutFile = g.argv[4];
450cfbb… drh 1069 }
1ad56ce… drh 1070 url_parse(g.argv[2], 0);
1ad56ce… drh 1071 if( g.url.protocol[0]!='h' ){
2f9920a… drh 1072 if( zSubpath==0 ){
2f9920a… drh 1073 fossil_fatal("the --subpath option is required for %s://",g.url.protocol);
2f9920a… drh 1074 }else{
2f9920a… drh 1075 g.url.subpath = fossil_strdup(zSubpath);
2f9920a… drh 1076 }
1ad56ce… drh 1077 }
1ad56ce… drh 1078 if( zInFile ){
1ad56ce… drh 1079 blob_read_from_file(&in, zInFile, ExtFILE);
450cfbb… drh 1080 if( zMimetype==0 && (mHttpFlags & HTTP_GENERIC)!=0 ){
1ad56ce… drh 1081 if( fossil_strcmp(zInFile,"-")==0 ){
1ad56ce… drh 1082 zMimetype = "application/x-unknown";
1ad56ce… drh 1083 }else{
1ad56ce… drh 1084 zMimetype = mimetype_from_name(zInFile);
1ad56ce… drh 1085 }
1ad56ce… drh 1086 }
1ad56ce… drh 1087 }else{
1ad56ce… drh 1088 blob_init(&in, 0, 0);
1ad56ce… drh 1089 }
1ad56ce… drh 1090 blob_init(&out, 0, 0);
1ad56ce… drh 1091 if( (mHttpFlags & HTTP_VERBOSE)==0 && zOutFile==0 ){
1ad56ce… drh 1092 zOutFile = "-";
1ad56ce… drh 1093 mHttpFlags |= HTTP_QUIET;
1ad56ce… drh 1094 }
1ad56ce… drh 1095 http_exchange(&in, &out, mHttpFlags, 4, zMimetype);
1ad56ce… drh 1096 if( zOutFile ) blob_write_to_file(&out, zOutFile);
1ad56ce… drh 1097 blob_zero(&in);
1ad56ce… drh 1098 blob_zero(&out);
dbda8d6… drh 1099 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button