| | @@ -29,10 +29,21 @@ |
| 29 | 29 | #ifndef fileno |
| 30 | 30 | #define fileno(s) _fileno(s) |
| 31 | 31 | #endif |
| 32 | 32 | #endif |
| 33 | 33 | |
| 34 | + |
| 35 | +#if INTERFACE |
| 36 | +/* |
| 37 | +** Bits of the mHttpFlags parameter to http_exchange() |
| 38 | +*/ |
| 39 | +#define HTTP_USE_LOGIN 0x00001 /* Add a login card to the sync message */ |
| 40 | +#define HTTP_GENERIC 0x00002 /* Generic HTTP request */ |
| 41 | +#define HTTP_VERBOSE 0x00004 /* HTTP status messages */ |
| 42 | +#define HTTP_QUIET 0x00008 /* No surplus output */ |
| 43 | +#endif |
| 44 | + |
| 34 | 45 | /* Maximum number of HTTP Authorization attempts */ |
| 35 | 46 | #define MAX_HTTP_AUTH 2 |
| 36 | 47 | |
| 37 | 48 | /* Keep track of HTTP Basic Authorization failures */ |
| 38 | 49 | static int fSeenHttpAuth = 0; |
| | @@ -97,22 +108,22 @@ |
| 97 | 108 | /* |
| 98 | 109 | ** Construct an appropriate HTTP request header. Write the header |
| 99 | 110 | ** into pHdr. This routine initializes the pHdr blob. pPayload is |
| 100 | 111 | ** the complete payload (including the login card) already compressed. |
| 101 | 112 | */ |
| 102 | | -static void http_build_header(Blob *pPayload, Blob *pHdr){ |
| 113 | +static void http_build_header( |
| 114 | + Blob *pPayload, /* the payload that will be sent */ |
| 115 | + Blob *pHdr, /* construct the header here */ |
| 116 | + const char *zAltMimetype /* Alternative mimetype */ |
| 117 | +){ |
| 103 | 118 | int i; |
| 104 | | - const char *zSep; |
| 119 | + int nPayload = pPayload ? blob_size(pPayload) : 0; |
| 105 | 120 | |
| 106 | 121 | blob_zero(pHdr); |
| 107 | 122 | i = strlen(g.url.path); |
| 108 | | - if( i>0 && g.url.path[i-1]=='/' ){ |
| 109 | | - zSep = ""; |
| 110 | | - }else{ |
| 111 | | - zSep = "/"; |
| 112 | | - } |
| 113 | | - blob_appendf(pHdr, "POST %s%s HTTP/1.1\r\n", g.url.path, zSep); |
| 123 | + blob_appendf(pHdr, "%s %s HTTP/1.0\r\n", |
| 124 | + nPayload>0 ? "POST" : "GET", g.url.path); |
| 114 | 125 | if( g.url.proxyAuth ){ |
| 115 | 126 | blob_appendf(pHdr, "Proxy-Authorization: %s\r\n", g.url.proxyAuth); |
| 116 | 127 | } |
| 117 | 128 | if( g.zHttpAuth && g.zHttpAuth[0] ){ |
| 118 | 129 | const char *zCredentials = g.zHttpAuth; |
| | @@ -121,16 +132,21 @@ |
| 121 | 132 | fossil_free(zEncoded); |
| 122 | 133 | } |
| 123 | 134 | blob_appendf(pHdr, "Host: %s\r\n", g.url.hostname); |
| 124 | 135 | blob_appendf(pHdr, "User-Agent: %s\r\n", get_user_agent()); |
| 125 | 136 | if( g.url.isSsh ) blob_appendf(pHdr, "X-Fossil-Transport: SSH\r\n"); |
| 126 | | - if( g.fHttpTrace ){ |
| 127 | | - blob_appendf(pHdr, "Content-Type: application/x-fossil-debug\r\n"); |
| 128 | | - }else{ |
| 129 | | - blob_appendf(pHdr, "Content-Type: application/x-fossil\r\n"); |
| 137 | + if( nPayload ){ |
| 138 | + if( zAltMimetype ){ |
| 139 | + blob_appendf(pHdr, "Content-Type: %s\r\n", zAltMimetype); |
| 140 | + }else if( g.fHttpTrace ){ |
| 141 | + blob_appendf(pHdr, "Content-Type: application/x-fossil-debug\r\n"); |
| 142 | + }else{ |
| 143 | + blob_appendf(pHdr, "Content-Type: application/x-fossil\r\n"); |
| 144 | + } |
| 145 | + blob_appendf(pHdr, "Content-Length: %d\r\n", blob_size(pPayload)); |
| 130 | 146 | } |
| 131 | | - blob_appendf(pHdr, "Content-Length: %d\r\n\r\n", blob_size(pPayload)); |
| 147 | + blob_append(pHdr, "\r\n", 2); |
| 132 | 148 | } |
| 133 | 149 | |
| 134 | 150 | /* |
| 135 | 151 | ** Use Fossil credentials for HTTP Basic Authorization prompt |
| 136 | 152 | */ |
| | @@ -200,11 +216,17 @@ |
| 200 | 216 | ** |
| 201 | 217 | ** The server address is contain in the "g" global structure. The |
| 202 | 218 | ** url_parse() routine should have been called prior to this routine |
| 203 | 219 | ** in order to fill this structure appropriately. |
| 204 | 220 | */ |
| 205 | | -int http_exchange(Blob *pSend, Blob *pReply, int useLogin, int maxRedirect){ |
| 221 | +int http_exchange( |
| 222 | + Blob *pSend, /* Message to be sent */ |
| 223 | + Blob *pReply, /* Write the reply here */ |
| 224 | + int mHttpFlags, /* Flags. See above */ |
| 225 | + int maxRedirect, /* Max number of redirects */ |
| 226 | + const char *zAltMimetype /* Alternative mimetype if not NULL */ |
| 227 | +){ |
| 206 | 228 | Blob login; /* The login card */ |
| 207 | 229 | Blob payload; /* The complete payload including login card */ |
| 208 | 230 | Blob hdr; /* The HTTP request header */ |
| 209 | 231 | int closeConnection; /* True to close the connection when done */ |
| 210 | 232 | int iLength; /* Expected length of the reply payload */ |
| | @@ -220,22 +242,26 @@ |
| 220 | 242 | fossil_warning("%s", transport_errmsg(&g.url)); |
| 221 | 243 | return 1; |
| 222 | 244 | } |
| 223 | 245 | |
| 224 | 246 | /* Construct the login card and prepare the complete payload */ |
| 225 | | - blob_zero(&login); |
| 226 | | - if( useLogin ) http_build_login_card(pSend, &login); |
| 227 | | - if( g.fHttpTrace ){ |
| 228 | | - payload = login; |
| 229 | | - blob_append(&payload, blob_buffer(pSend), blob_size(pSend)); |
| 247 | + if( blob_size(pSend)==0 ){ |
| 248 | + blob_zero(&payload); |
| 230 | 249 | }else{ |
| 231 | | - blob_compress2(&login, pSend, &payload); |
| 232 | | - blob_reset(&login); |
| 250 | + blob_zero(&login); |
| 251 | + if( mHttpFlags & HTTP_USE_LOGIN ) http_build_login_card(pSend, &login); |
| 252 | + if( g.fHttpTrace ){ |
| 253 | + payload = login; |
| 254 | + blob_append(&payload, blob_buffer(pSend), blob_size(pSend)); |
| 255 | + }else{ |
| 256 | + blob_compress2(&login, pSend, &payload); |
| 257 | + blob_reset(&login); |
| 258 | + } |
| 233 | 259 | } |
| 234 | 260 | |
| 235 | 261 | /* Construct the HTTP request header */ |
| 236 | | - http_build_header(&payload, &hdr); |
| 262 | + http_build_header(&payload, &hdr, zAltMimetype); |
| 237 | 263 | |
| 238 | 264 | /* When tracing, write the transmitted HTTP message both to standard |
| 239 | 265 | ** output and into a file. The file can then be used to drive the |
| 240 | 266 | ** server-side like this: |
| 241 | 267 | ** |
| | @@ -261,10 +287,15 @@ |
| 261 | 287 | } |
| 262 | 288 | |
| 263 | 289 | /* |
| 264 | 290 | ** Send the request to the server. |
| 265 | 291 | */ |
| 292 | + if( mHttpFlags & HTTP_VERBOSE ){ |
| 293 | + fossil_print("URL: %s\n", g.url.canonical); |
| 294 | + fossil_print("Sending %d byte header and %d byte payload\n", |
| 295 | + blob_size(&hdr), blob_size(&payload)); |
| 296 | + } |
| 266 | 297 | transport_send(&g.url, &hdr); |
| 267 | 298 | transport_send(&g.url, &payload); |
| 268 | 299 | blob_reset(&hdr); |
| 269 | 300 | blob_reset(&payload); |
| 270 | 301 | transport_flip(&g.url); |
| | @@ -273,21 +304,24 @@ |
| 273 | 304 | ** Read and interpret the server reply |
| 274 | 305 | */ |
| 275 | 306 | closeConnection = 1; |
| 276 | 307 | iLength = -1; |
| 277 | 308 | while( (zLine = transport_receive_line(&g.url))!=0 && zLine[0]!=0 ){ |
| 278 | | - /* printf("[%s]\n", zLine); fflush(stdout); */ |
| 309 | + if( mHttpFlags & HTTP_VERBOSE ){ |
| 310 | + fossil_print("Read: [%s]\n", zLine); |
| 311 | + } |
| 279 | 312 | if( fossil_strnicmp(zLine, "http/1.", 7)==0 ){ |
| 280 | 313 | if( sscanf(zLine, "HTTP/1.%d %d", &iHttpVersion, &rc)!=2 ) goto write_err; |
| 281 | 314 | if( rc==401 ){ |
| 282 | 315 | if( fSeenHttpAuth++ < MAX_HTTP_AUTH ){ |
| 283 | 316 | if( g.zHttpAuth ){ |
| 284 | 317 | if( g.zHttpAuth ) free(g.zHttpAuth); |
| 285 | 318 | } |
| 286 | 319 | g.zHttpAuth = prompt_for_httpauth_creds(); |
| 287 | 320 | transport_close(&g.url); |
| 288 | | - return http_exchange(pSend, pReply, useLogin, maxRedirect); |
| 321 | + return http_exchange(pSend, pReply, mHttpFlags, |
| 322 | + maxRedirect, zAltMimetype); |
| 289 | 323 | } |
| 290 | 324 | } |
| 291 | 325 | if( rc!=200 && rc!=301 && rc!=302 && rc!=307 && rc!=308 ){ |
| 292 | 326 | int ii; |
| 293 | 327 | for(ii=7; zLine[ii] && zLine[ii]!=' '; ii++){} |
| | @@ -338,27 +372,34 @@ |
| 338 | 372 | j = strlen(zLine) - 1; |
| 339 | 373 | while( j>4 && fossil_strcmp(&zLine[j-4],"/xfer")==0 ){ |
| 340 | 374 | j -= 4; |
| 341 | 375 | zLine[j] = 0; |
| 342 | 376 | } |
| 343 | | - fossil_print("redirect with status %d to %s\n", rc, &zLine[i]); |
| 377 | + if( (mHttpFlags & HTTP_QUIET)==0 ){ |
| 378 | + fossil_print("redirect with status %d to %s\n", rc, &zLine[i]); |
| 379 | + } |
| 344 | 380 | url_parse(&zLine[i], 0); |
| 345 | 381 | transport_close(&g.url); |
| 346 | 382 | transport_global_shutdown(&g.url); |
| 347 | 383 | fSeenHttpAuth = 0; |
| 348 | 384 | if( g.zHttpAuth ) free(g.zHttpAuth); |
| 349 | 385 | g.zHttpAuth = get_httpauth(); |
| 350 | 386 | if( rc==301 || rc==308 ) url_remember(); |
| 351 | | - return http_exchange(pSend, pReply, useLogin, maxRedirect); |
| 387 | + return http_exchange(pSend, pReply, mHttpFlags, |
| 388 | + maxRedirect, zAltMimetype); |
| 352 | 389 | }else if( fossil_strnicmp(zLine, "content-type: ", 14)==0 ){ |
| 353 | 390 | if( fossil_strnicmp(&zLine[14], "application/x-fossil-debug", -1)==0 ){ |
| 354 | 391 | isCompressed = 0; |
| 355 | 392 | }else if( fossil_strnicmp(&zLine[14], |
| 356 | 393 | "application/x-fossil-uncompressed", -1)==0 ){ |
| 357 | 394 | isCompressed = 0; |
| 358 | | - }else if( fossil_strnicmp(&zLine[14], "application/x-fossil", -1)!=0 ){ |
| 359 | | - isError = 1; |
| 395 | + }else{ |
| 396 | + if( (mHttpFlags & HTTP_GENERIC)==0 |
| 397 | + && fossil_strnicmp(&zLine[14], "application/x-fossil", -1)!=0 |
| 398 | + ){ |
| 399 | + isError = 1; |
| 400 | + } |
| 360 | 401 | } |
| 361 | 402 | } |
| 362 | 403 | } |
| 363 | 404 | if( iLength<0 ){ |
| 364 | 405 | fossil_warning("server did not reply"); |
| | @@ -419,5 +460,62 @@ |
| 419 | 460 | */ |
| 420 | 461 | write_err: |
| 421 | 462 | transport_close(&g.url); |
| 422 | 463 | return 1; |
| 423 | 464 | } |
| 465 | + |
| 466 | +/* |
| 467 | +** COMMAND: test-httpmsg |
| 468 | +** |
| 469 | +** Usage: %fossil test-httpmsg URL ?PAYLOAD? ?OPTIONS? |
| 470 | +** |
| 471 | +** Send an HTTP message to URL and get the reply. PAYLOAD is a file containing |
| 472 | +** the payload, or "-" to read payload from standard input. a POST message |
| 473 | +** is sent if PAYLOAD is specified and is non-empty. If PAYLOAD is omitted |
| 474 | +** or is an empty file, then a GET message is sent. |
| 475 | +** |
| 476 | +** Options: |
| 477 | +** |
| 478 | +** --mimetype TYPE Mimetype of the payload |
| 479 | +** --out FILE Store the reply in FILE |
| 480 | +** -v Verbose output |
| 481 | +*/ |
| 482 | +void test_wget_command(void){ |
| 483 | + const char *zMimetype; |
| 484 | + const char *zInFile; |
| 485 | + const char *zOutFile; |
| 486 | + Blob in, out; |
| 487 | + unsigned int mHttpFlags = HTTP_GENERIC; |
| 488 | + |
| 489 | + zMimetype = find_option("mimetype",0,1); |
| 490 | + zOutFile = find_option("out","o",1); |
| 491 | + if( find_option("verbose","v",0)!=0 ) mHttpFlags |= HTTP_VERBOSE; |
| 492 | + if( g.argc!=3 && g.argc!=4 ){ |
| 493 | + usage("URL ?PAYLOAD?"); |
| 494 | + } |
| 495 | + zInFile = g.argc==4 ? g.argv[3] : 0; |
| 496 | + url_parse(g.argv[2], 0); |
| 497 | + if( g.url.protocol[0]!='h' ){ |
| 498 | + fossil_fatal("the %s command supports only http: and https:", g.argv[1]); |
| 499 | + } |
| 500 | + if( zInFile ){ |
| 501 | + blob_read_from_file(&in, zInFile, ExtFILE); |
| 502 | + if( zMimetype==0 ){ |
| 503 | + if( fossil_strcmp(zInFile,"-")==0 ){ |
| 504 | + zMimetype = "application/x-unknown"; |
| 505 | + }else{ |
| 506 | + zMimetype = mimetype_from_name(zInFile); |
| 507 | + } |
| 508 | + } |
| 509 | + }else{ |
| 510 | + blob_init(&in, 0, 0); |
| 511 | + } |
| 512 | + blob_init(&out, 0, 0); |
| 513 | + if( (mHttpFlags & HTTP_VERBOSE)==0 && zOutFile==0 ){ |
| 514 | + zOutFile = "-"; |
| 515 | + mHttpFlags |= HTTP_QUIET; |
| 516 | + } |
| 517 | + http_exchange(&in, &out, mHttpFlags, 4, zMimetype); |
| 518 | + if( zOutFile ) blob_write_to_file(&out, zOutFile); |
| 519 | + blob_zero(&in); |
| 520 | + blob_zero(&out); |
| 521 | +} |
| 424 | 522 | |