Fossil SCM

Enhance TH1 'redirect' command to support for HTTP redirects with a status code of 307.

mistachkin 2016-12-19 07:04 trunk
Commit bee6dbde5498e07d7876abe206792b2b461d03dc
3 files changed +12 -2 +20 -6 +7 -3
+12 -2
--- src/cgi.c
+++ src/cgi.c
@@ -400,11 +400,15 @@
400400
/*
401401
** Do a redirect request to the URL given in the argument.
402402
**
403403
** The URL must be relative to the base of the fossil server.
404404
*/
405
-NORETURN void cgi_redirect(const char *zURL){
405
+NORETURN static void cgi_redirect_with_status(
406
+ const char *zURL,
407
+ int iStat,
408
+ const char *zStat
409
+){
406410
char *zLocation;
407411
CGIDEBUG(("redirect to %s\n", zURL));
408412
if( strncmp(zURL,"http:",5)==0 || strncmp(zURL,"https:",6)==0 ){
409413
zLocation = mprintf("Location: %s\r\n", zURL);
410414
}else if( *zURL=='/' ){
@@ -416,15 +420,21 @@
416420
zLocation = mprintf("Location: %s/%s\r\n", g.zBaseURL, zURL);
417421
}
418422
cgi_append_header(zLocation);
419423
cgi_reset_content();
420424
cgi_printf("<html>\n<p>Redirect to %h</p>\n</html>\n", zLocation);
421
- cgi_set_status(302, "Moved Temporarily");
425
+ cgi_set_status(iStat, zStat);
422426
free(zLocation);
423427
cgi_reply();
424428
fossil_exit(0);
425429
}
430
+NORETURN void cgi_redirect(const char *zURL){
431
+ cgi_redirect_with_status(zURL, 302, "Moved Temporarily");
432
+}
433
+NORETURN void cgi_redirect_with_method(const char *zURL){
434
+ cgi_redirect_with_status(zURL, 307, "Temporary Redirect");
435
+}
426436
NORETURN void cgi_redirectf(const char *zFormat, ...){
427437
va_list ap;
428438
va_start(ap, zFormat);
429439
cgi_redirect(vmprintf(zFormat, ap));
430440
va_end(ap);
431441
--- src/cgi.c
+++ src/cgi.c
@@ -400,11 +400,15 @@
400 /*
401 ** Do a redirect request to the URL given in the argument.
402 **
403 ** The URL must be relative to the base of the fossil server.
404 */
405 NORETURN void cgi_redirect(const char *zURL){
 
 
 
 
406 char *zLocation;
407 CGIDEBUG(("redirect to %s\n", zURL));
408 if( strncmp(zURL,"http:",5)==0 || strncmp(zURL,"https:",6)==0 ){
409 zLocation = mprintf("Location: %s\r\n", zURL);
410 }else if( *zURL=='/' ){
@@ -416,15 +420,21 @@
416 zLocation = mprintf("Location: %s/%s\r\n", g.zBaseURL, zURL);
417 }
418 cgi_append_header(zLocation);
419 cgi_reset_content();
420 cgi_printf("<html>\n<p>Redirect to %h</p>\n</html>\n", zLocation);
421 cgi_set_status(302, "Moved Temporarily");
422 free(zLocation);
423 cgi_reply();
424 fossil_exit(0);
425 }
 
 
 
 
 
 
426 NORETURN void cgi_redirectf(const char *zFormat, ...){
427 va_list ap;
428 va_start(ap, zFormat);
429 cgi_redirect(vmprintf(zFormat, ap));
430 va_end(ap);
431
--- src/cgi.c
+++ src/cgi.c
@@ -400,11 +400,15 @@
400 /*
401 ** Do a redirect request to the URL given in the argument.
402 **
403 ** The URL must be relative to the base of the fossil server.
404 */
405 NORETURN static void cgi_redirect_with_status(
406 const char *zURL,
407 int iStat,
408 const char *zStat
409 ){
410 char *zLocation;
411 CGIDEBUG(("redirect to %s\n", zURL));
412 if( strncmp(zURL,"http:",5)==0 || strncmp(zURL,"https:",6)==0 ){
413 zLocation = mprintf("Location: %s\r\n", zURL);
414 }else if( *zURL=='/' ){
@@ -416,15 +420,21 @@
420 zLocation = mprintf("Location: %s/%s\r\n", g.zBaseURL, zURL);
421 }
422 cgi_append_header(zLocation);
423 cgi_reset_content();
424 cgi_printf("<html>\n<p>Redirect to %h</p>\n</html>\n", zLocation);
425 cgi_set_status(iStat, zStat);
426 free(zLocation);
427 cgi_reply();
428 fossil_exit(0);
429 }
430 NORETURN void cgi_redirect(const char *zURL){
431 cgi_redirect_with_status(zURL, 302, "Moved Temporarily");
432 }
433 NORETURN void cgi_redirect_with_method(const char *zURL){
434 cgi_redirect_with_status(zURL, 307, "Temporary Redirect");
435 }
436 NORETURN void cgi_redirectf(const char *zFormat, ...){
437 va_list ap;
438 va_start(ap, zFormat);
439 cgi_redirect(vmprintf(zFormat, ap));
440 va_end(ap);
441
+20 -6
--- src/th_main.c
+++ src/th_main.c
@@ -432,26 +432,40 @@
432432
sendText((char*)argv[1], argl[1], *(unsigned int*)pConvert);
433433
return TH_OK;
434434
}
435435
436436
/*
437
-** TH1 command: redirect URL
437
+** TH1 command: redirect URL ?withMethod?
438438
**
439
-** Issues an HTTP redirect (302) to the specified URL and then exits the
440
-** process.
439
+** Issues an HTTP redirect to the specified URL and then exits the process.
440
+** By default, an HTTP status code of 302 is used. If the optional withMethod
441
+** argument is present and non-zero, an HTTP status code of 307 is used, which
442
+** should force the user agent to preserve the original method for the request
443
+** (e.g. GET, POST) instead of (possibly) forcing the user agent to change the
444
+** method to GET.
441445
*/
442446
static int redirectCmd(
443447
Th_Interp *interp,
444448
void *p,
445449
int argc,
446450
const char **argv,
447451
int *argl
448452
){
449
- if( argc!=2 ){
450
- return Th_WrongNumArgs(interp, "redirect URL");
453
+ int withMethod = 0;
454
+ if( argc!=2 && argc!=3 ){
455
+ return Th_WrongNumArgs(interp, "redirect URL ?withMethod?");
451456
}
452
- cgi_redirect(argv[1]);
457
+ if( argc==3 ){
458
+ if( Th_ToInt(interp, argv[2], argl[2], &withMethod) ){
459
+ return TH_ERROR;
460
+ }
461
+ }
462
+ if( withMethod ){
463
+ cgi_redirect_with_method(argv[1]);
464
+ }else{
465
+ cgi_redirect(argv[1]);
466
+ }
453467
Th_SetResult(interp, argv[1], argl[1]); /* NOT REACHED */
454468
return TH_OK;
455469
}
456470
457471
/*
458472
--- src/th_main.c
+++ src/th_main.c
@@ -432,26 +432,40 @@
432 sendText((char*)argv[1], argl[1], *(unsigned int*)pConvert);
433 return TH_OK;
434 }
435
436 /*
437 ** TH1 command: redirect URL
438 **
439 ** Issues an HTTP redirect (302) to the specified URL and then exits the
440 ** process.
 
 
 
 
441 */
442 static int redirectCmd(
443 Th_Interp *interp,
444 void *p,
445 int argc,
446 const char **argv,
447 int *argl
448 ){
449 if( argc!=2 ){
450 return Th_WrongNumArgs(interp, "redirect URL");
 
451 }
452 cgi_redirect(argv[1]);
 
 
 
 
 
 
 
 
 
453 Th_SetResult(interp, argv[1], argl[1]); /* NOT REACHED */
454 return TH_OK;
455 }
456
457 /*
458
--- src/th_main.c
+++ src/th_main.c
@@ -432,26 +432,40 @@
432 sendText((char*)argv[1], argl[1], *(unsigned int*)pConvert);
433 return TH_OK;
434 }
435
436 /*
437 ** TH1 command: redirect URL ?withMethod?
438 **
439 ** Issues an HTTP redirect to the specified URL and then exits the process.
440 ** By default, an HTTP status code of 302 is used. If the optional withMethod
441 ** argument is present and non-zero, an HTTP status code of 307 is used, which
442 ** should force the user agent to preserve the original method for the request
443 ** (e.g. GET, POST) instead of (possibly) forcing the user agent to change the
444 ** method to GET.
445 */
446 static int redirectCmd(
447 Th_Interp *interp,
448 void *p,
449 int argc,
450 const char **argv,
451 int *argl
452 ){
453 int withMethod = 0;
454 if( argc!=2 && argc!=3 ){
455 return Th_WrongNumArgs(interp, "redirect URL ?withMethod?");
456 }
457 if( argc==3 ){
458 if( Th_ToInt(interp, argv[2], argl[2], &withMethod) ){
459 return TH_ERROR;
460 }
461 }
462 if( withMethod ){
463 cgi_redirect_with_method(argv[1]);
464 }else{
465 cgi_redirect(argv[1]);
466 }
467 Th_SetResult(interp, argv[1], argl[1]); /* NOT REACHED */
468 return TH_OK;
469 }
470
471 /*
472
+7 -3
--- www/th1.md
+++ www/th1.md
@@ -440,14 +440,18 @@
440440
omitted, use a value of 10.
441441
442442
<a name="redirect"></a>TH1 redirect Command
443443
-------------------------------------------
444444
445
- * redirect URL
445
+ * redirect URL ?withMethod?
446446
447
-Issues an HTTP redirect (302) to the specified URL and then exits the
448
-process.
447
+Issues an HTTP redirect to the specified URL and then exits the process.
448
+By default, an HTTP status code of 302 is used. If the optional withMethod
449
+argument is present and non-zero, an HTTP status code of 307 is used, which
450
+should force the user agent to preserve the original method for the request
451
+(e.g. GET, POST) instead of (possibly) forcing the user agent to change the
452
+method to GET.
449453
450454
<a name="regexp"></a>TH1 regexp Command
451455
---------------------------------------
452456
453457
* regexp ?-nocase? ?--? exp string
454458
--- www/th1.md
+++ www/th1.md
@@ -440,14 +440,18 @@
440 omitted, use a value of 10.
441
442 <a name="redirect"></a>TH1 redirect Command
443 -------------------------------------------
444
445 * redirect URL
446
447 Issues an HTTP redirect (302) to the specified URL and then exits the
448 process.
 
 
 
 
449
450 <a name="regexp"></a>TH1 regexp Command
451 ---------------------------------------
452
453 * regexp ?-nocase? ?--? exp string
454
--- www/th1.md
+++ www/th1.md
@@ -440,14 +440,18 @@
440 omitted, use a value of 10.
441
442 <a name="redirect"></a>TH1 redirect Command
443 -------------------------------------------
444
445 * redirect URL ?withMethod?
446
447 Issues an HTTP redirect to the specified URL and then exits the process.
448 By default, an HTTP status code of 302 is used. If the optional withMethod
449 argument is present and non-zero, an HTTP status code of 307 is used, which
450 should force the user agent to preserve the original method for the request
451 (e.g. GET, POST) instead of (possibly) forcing the user agent to change the
452 method to GET.
453
454 <a name="regexp"></a>TH1 regexp Command
455 ---------------------------------------
456
457 * regexp ?-nocase? ?--? exp string
458

Keyboard Shortcuts

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