diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-30 08:13:11 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-08-30 08:13:11 +0300 |
commit | b61d11ad95b13154fe0d8f28cdced09254ccf7e5 (patch) | |
tree | c44aa6fa2bdf7f8e5e8050f57c3e63c4742e135f /src | |
parent | e023c5c7823db3b4d853bc939b76d88001a51dc4 (diff) |
DocumentWidget: Check for non-Gemini redirects
Diffstat (limited to 'src')
-rw-r--r-- | src/gmutil.c | 5 | ||||
-rw-r--r-- | src/gmutil.h | 1 | ||||
-rw-r--r-- | src/ui/documentwidget.c | 38 |
3 files changed, 30 insertions, 14 deletions
diff --git a/src/gmutil.c b/src/gmutil.c index 07bf3016..41f0dd20 100644 --- a/src/gmutil.c +++ b/src/gmutil.c | |||
@@ -227,6 +227,11 @@ static const struct { | |||
227 | "Invalid Redirect", | 227 | "Invalid Redirect", |
228 | "The server responded with a redirect but did not provide a valid destination URL. " | 228 | "The server responded with a redirect but did not provide a valid destination URL. " |
229 | "Perhaps the server is malfunctioning." } }, | 229 | "Perhaps the server is malfunctioning." } }, |
230 | { nonGeminiRedirect_GmStatusCode, | ||
231 | { 0x27a0, /* dashed arrow */ | ||
232 | "Redirect to Non-Gemini URL", | ||
233 | "The server attempted to redirect us to a non-Gemini URL. Here is the link so you " | ||
234 | "can open it manually if appropriate."} }, | ||
230 | { temporaryFailure_GmStatusCode, | 235 | { temporaryFailure_GmStatusCode, |
231 | { 0x1f50c, /* electric plug */ | 236 | { 0x1f50c, /* electric plug */ |
232 | "Temporary Failure", | 237 | "Temporary Failure", |
diff --git a/src/gmutil.h b/src/gmutil.h index 72ca5fed..dc73ce6e 100644 --- a/src/gmutil.h +++ b/src/gmutil.h | |||
@@ -33,6 +33,7 @@ enum iGmStatusCode { | |||
33 | /* clientside status codes */ | 33 | /* clientside status codes */ |
34 | clientSide_GmStatusCode = -100, | 34 | clientSide_GmStatusCode = -100, |
35 | invalidRedirect_GmStatusCode, | 35 | invalidRedirect_GmStatusCode, |
36 | nonGeminiRedirect_GmStatusCode, | ||
36 | invalidHeader_GmStatusCode, | 37 | invalidHeader_GmStatusCode, |
37 | unsupportedMimeType_GmStatusCode, | 38 | unsupportedMimeType_GmStatusCode, |
38 | failedToOpenFile_GmStatusCode, | 39 | failedToOpenFile_GmStatusCode, |
diff --git a/src/ui/documentwidget.c b/src/ui/documentwidget.c index 43b1664f..7f76a436 100644 --- a/src/ui/documentwidget.c +++ b/src/ui/documentwidget.c | |||
@@ -503,23 +503,27 @@ static void setSource_DocumentWidget_(iDocumentWidget *d, const iString *source) | |||
503 | refresh_Widget(as_Widget(d)); | 503 | refresh_Widget(as_Widget(d)); |
504 | } | 504 | } |
505 | 505 | ||
506 | static void showErrorPage_DocumentWidget_(iDocumentWidget *d, enum iGmStatusCode code) { | 506 | static void showErrorPage_DocumentWidget_(iDocumentWidget *d, enum iGmStatusCode code, |
507 | const iString *meta) { | ||
507 | iString *src = collectNewCStr_String("# "); | 508 | iString *src = collectNewCStr_String("# "); |
508 | const iGmError *msg = get_GmError(code); | 509 | const iGmError *msg = get_GmError(code); |
509 | appendChar_String(src, msg->icon ? msg->icon : 0x2327); /* X in a box */ | 510 | appendChar_String(src, msg->icon ? msg->icon : 0x2327); /* X in a box */ |
510 | appendFormat_String(src, " %s\n%s", msg->title, msg->info); | 511 | appendFormat_String(src, " %s\n%s", msg->title, msg->info); |
511 | if (d->request) { | 512 | if (meta) { |
512 | switch (code) { | 513 | switch (code) { |
514 | case nonGeminiRedirect_GmStatusCode: | ||
515 | appendFormat_String(src, "\n=> %s\n", cstr_String(meta)); | ||
516 | break; | ||
513 | case failedToOpenFile_GmStatusCode: | 517 | case failedToOpenFile_GmStatusCode: |
514 | case certificateNotValid_GmStatusCode: | 518 | case certificateNotValid_GmStatusCode: |
515 | appendFormat_String(src, "\n\n%s", cstr_String(meta_GmRequest(d->request))); | 519 | appendFormat_String(src, "\n\n%s", cstr_String(meta)); |
516 | break; | 520 | break; |
517 | case unsupportedMimeType_GmStatusCode: | 521 | case unsupportedMimeType_GmStatusCode: |
518 | appendFormat_String(src, "\n```\n%s\n```\n", cstr_String(meta_GmRequest(d->request))); | 522 | appendFormat_String(src, "\n```\n%s\n```\n", cstr_String(meta)); |
519 | break; | 523 | break; |
520 | case slowDown_GmStatusCode: | 524 | case slowDown_GmStatusCode: |
521 | appendFormat_String(src, "\n\nWait %s seconds before your next request.", | 525 | appendFormat_String(src, "\n\nWait %s seconds before your next request.", |
522 | cstr_String(meta_GmRequest(d->request))); | 526 | cstr_String(meta)); |
523 | break; | 527 | break; |
524 | default: | 528 | default: |
525 | break; | 529 | break; |
@@ -599,7 +603,7 @@ static void updateDocument_DocumentWidget_(iDocumentWidget *d, const iGmResponse | |||
599 | } | 603 | } |
600 | } | 604 | } |
601 | if (docFormat == undefined_GmDocumentFormat) { | 605 | if (docFormat == undefined_GmDocumentFormat) { |
602 | showErrorPage_DocumentWidget_(d, unsupportedMimeType_GmStatusCode); | 606 | showErrorPage_DocumentWidget_(d, unsupportedMimeType_GmStatusCode, &response->meta); |
603 | deinit_String(&str); | 607 | deinit_String(&str); |
604 | return; | 608 | return; |
605 | } | 609 | } |
@@ -840,27 +844,33 @@ static void checkResponse_DocumentWidget_(iDocumentWidget *d) { | |||
840 | break; | 844 | break; |
841 | case categoryRedirect_GmStatusCode: | 845 | case categoryRedirect_GmStatusCode: |
842 | if (isEmpty_String(meta_GmRequest(d->request))) { | 846 | if (isEmpty_String(meta_GmRequest(d->request))) { |
843 | showErrorPage_DocumentWidget_(d, invalidRedirect_GmStatusCode); | 847 | showErrorPage_DocumentWidget_(d, invalidRedirect_GmStatusCode, NULL); |
844 | } | 848 | } |
845 | else { | 849 | else { |
846 | /* TODO: only accept redirects that use gemini scheme */ | 850 | /* Only accept redirects that use gemini scheme. */ |
847 | postCommandf_App( | 851 | const iString *dstUrl = absoluteUrl_String(d->mod.url, meta_GmRequest(d->request)); |
848 | "open redirect:1 url:%s", | 852 | if (equalCase_Rangecc(urlScheme_String(dstUrl), "gemini")) { |
849 | cstr_String(absoluteUrl_String(d->mod.url, meta_GmRequest(d->request)))); | 853 | postCommandf_App("open redirect:1 url:%s", cstr_String(dstUrl)); |
854 | } | ||
855 | else { | ||
856 | showErrorPage_DocumentWidget_(d, nonGeminiRedirect_GmStatusCode, dstUrl); | ||
857 | } | ||
850 | iReleasePtr(&d->request); | 858 | iReleasePtr(&d->request); |
851 | } | 859 | } |
852 | break; | 860 | break; |
853 | default: | 861 | default: |
854 | if (isDefined_GmError(statusCode)) { | 862 | if (isDefined_GmError(statusCode)) { |
855 | showErrorPage_DocumentWidget_(d, statusCode); | 863 | showErrorPage_DocumentWidget_(d, statusCode, meta_GmRequest(d->request)); |
856 | } | 864 | } |
857 | else if (category_GmStatusCode(statusCode) == | 865 | else if (category_GmStatusCode(statusCode) == |
858 | categoryTemporaryFailure_GmStatusCode) { | 866 | categoryTemporaryFailure_GmStatusCode) { |
859 | showErrorPage_DocumentWidget_(d, temporaryFailure_GmStatusCode); | 867 | showErrorPage_DocumentWidget_( |
868 | d, temporaryFailure_GmStatusCode, meta_GmRequest(d->request)); | ||
860 | } | 869 | } |
861 | else if (category_GmStatusCode(statusCode) == | 870 | else if (category_GmStatusCode(statusCode) == |
862 | categoryPermanentFailure_GmStatusCode) { | 871 | categoryPermanentFailure_GmStatusCode) { |
863 | showErrorPage_DocumentWidget_(d, permanentFailure_GmStatusCode); | 872 | showErrorPage_DocumentWidget_( |
873 | d, permanentFailure_GmStatusCode, meta_GmRequest(d->request)); | ||
864 | } | 874 | } |
865 | break; | 875 | break; |
866 | } | 876 | } |