From 5fb32fcfca41d2e1b6eaaacaa6b6b37fc9e6d0b1 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Sat, 1 Aug 2020 22:50:07 +0300 Subject: Handling status codes by class --- src/gmutil.c | 198 ++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 109 insertions(+), 89 deletions(-) (limited to 'src/gmutil.c') diff --git a/src/gmutil.c b/src/gmutil.c index 2008ea36..bf08e960 100644 --- a/src/gmutil.c +++ b/src/gmutil.c @@ -146,97 +146,117 @@ void urlEncodeSpaces_String(iString *d) { } } +static const struct { + enum iGmStatusCode code; + iGmError err; +} errors_[] = { + { unknownStatusCode_GmStatusCode, /* keep this as the first one (fallback return value) */ + { 0x1f4ab, /* dizzy */ + "Unknown Status Code", + "The server responded with a status code that is not specified in the Gemini " + "protocol as known to this client. Maybe the server is from the future? Or " + "just malfunctioning."} }, + { failedToOpenFile_GmStatusCode, + { 0x1f4c1, /* file folder */ + "Failed to Open File", + "The requested file does not exist or is inaccessible. " + "Please check the file path." } }, + { unsupportedMimeType_GmStatusCode, + { 0x1f47d, /* alien */ + "Unsupported MIME Type", + "The received content is in an unsupported format and cannot be viewed with " + "this application." } }, + { invalidHeader_GmStatusCode, + { 0x1f4a9, /* pile of poo */ + "Invalid Header", + "The received header did not conform to the Gemini specification. " + "Perhaps the server is malfunctioning or you tried to contact a " + "non-Gemini server." } }, + { invalidRedirect_GmStatusCode, + { 0, /* */ + "Invalid Redirect", + "The server responded with a redirect but did not provide a valid destination URL. " + "Perhaps the server is malfunctioning." } }, + { temporaryFailure_GmStatusCode, + { 0x1f50c, /* electric plug */ + "Temporary Failure", + "The request has failed, but may succeed if you try again in the future." } }, + { serverUnavailable_GmStatusCode, + { 0x1f525, /* fire */ + "Server Unavailable", + "The server is unavailable due to overload or maintenance. Check back later." } }, + { cgiError_GmStatusCode, + { 0x1f4a5, /* collision */ + "CGI Error", + "Failure during dynamic content generation on the server. This may be due " + "to buggy serverside software." } }, + { proxyError_GmStatusCode, + { 0x1f310, /* globe */ + "Proxy Error", + "A proxy request failed because the server was unable to successfully " + "complete a transaction with the remote host. Perhaps there are difficulties " + "with network connectivity." } }, + { slowDown_GmStatusCode, + { 0x1f40c, /* snail */ + "Slow Down", + "The server is rate limiting requests. Please wait..." } }, + { permanentFailure_GmStatusCode, + { 0x1f6ab, /* no entry */ + "Permanent Failure", + "Your request has failed and will fail in the future as well if repeated." } }, + { notFound_GmStatusCode, + { 0x1f50d, /* magnifying glass */ + "Not Found", + "The requested resource could not be found at this time." } }, + { gone_GmStatusCode, + { 0x1f47b, /* ghost */ + "Gone", + "The resource requested is no longer available and will not be available again." } }, + { proxyRequestRefused_GmStatusCode, + { 0x1f6c2, /* passport control */ + "Proxy Request Refused", + "The request was for a resource at a domain not served by the server and the " + "server does not accept proxy requests." } }, + { badRequest_GmStatusCode, + { 0x1f44e, /* thumbs down */ + "Bad Request", + "The server was unable to parse your request, presumably due to the " + "request being malformed. Likely a bug in Lagrange." } }, + { clientCertificateRequired_GmStatusCode, + { 0x1f511, /* key */ + "Certificate Required", + "Access to the requested resource requires identification via " + "a client certificate." } }, + { certificateNotAuthorized_GmStatusCode, + { 0x1f512, /* lock */ + "Certificate Not Authorized", + "The provided client certificate is valid but is not authorized for accessing " + "the requested resource. " } }, + { certificateNotValid_GmStatusCode, + { 0x1f6a8, /* revolving light */ + "Invalid Certificate", + "The provided client certificate is expired or invalid." } }, +}; + +iBool isDefined_GmError(enum iGmStatusCode code) { + iForIndices(i, errors_) { + if (errors_[i].code == code) { + return iTrue; + } + } + return iFalse; +} + const iGmError *get_GmError(enum iGmStatusCode code) { static const iGmError none = { 0, "", "" }; - static const struct { - enum iGmStatusCode code; - iGmError err; - } errors[] = { - { failedToOpenFile_GmStatusCode, - { 0x1f4c1, /* file folder */ - "Failed to Open File", - "The requested file does not exist or is inaccessible. " - "Please check the file path." } }, - { unsupportedMimeType_GmStatusCode, - { 0x1f47d, /* alien */ - "Unsupported MIME Type", - "The received content is in an unsupported format and cannot be viewed with " - "this application." } }, - { invalidHeader_GmStatusCode, - { 0x1f4a9, /* pile of poo */ - "Invalid Header", - "The received header did not conform to the Gemini specification. " - "Perhaps the server is malfunctioning or you tried to contact a " - "non-Gemini server." } }, - { invalidRedirect_GmStatusCode, - { 0, /* */ - "Invalid Redirect", - "The server responded with a redirect but did not provide a valid destination URL. " - "Perhaps the server is malfunctioning." } }, - { temporaryFailure_GmStatusCode, - { 0x1f50c, /* electric plug */ - "Temporary Failure", - "The request has failed, but may succeed if you try again in the future." } }, - { serverUnavailable_GmStatusCode, - { 0x1f525, /* fire */ - "Server Unavailable", - "The server is unavailable due to overload or maintenance. Check back later." } }, - { cgiError_GmStatusCode, - { 0x1f4a5, /* collision */ - "CGI Error", - "Failure during dynamic content generation on the server. This may be due " - "to buggy serverside software." } }, - { proxyError_GmStatusCode, - { 0x1f310, /* globe */ - "Proxy Error", - "A proxy request failed because the server was unable to successfully " - "complete a transaction with the remote host. Perhaps there are difficulties " - "with network connectivity." } }, - { slowDown_GmStatusCode, - { 0x1f40c, /* snail */ - "Slow Down", - "The server is rate limiting requests. Please wait..." } }, - { permanentFailure_GmStatusCode, - { 0x1f6ab, /* no entry */ - "Permanent Failure", - "Your request has failed and will fail in the future as well if repeated." } }, - { notFound_GmStatusCode, - { 0x1f50d, /* magnifying glass */ - "Not Found", - "The requested resource could not be found at this time." } }, - { gone_GmStatusCode, - { 0x1f47b, /* ghost */ - "Gone", - "The resource requested is no longer available and will not be available again." } }, - { proxyRequestRefused_GmStatusCode, - { 0x1f6c2, /* passport control */ - "Proxy Request Refused", - "The request was for a resource at a domain not served by the server and the " - "server does not accept proxy requests." } }, - { badRequest_GmStatusCode, - { 0x1f44e, /* thumbs down */ - "Bad Request", - "The server was unable to parse your request, presumably due to the " - "request being malformed. Likely a bug in Lagrange." } }, - { clientCertificateRequired_GmStatusCode, - { 0x1f511, /* key */ - "Certificate Required", - "Access to the requested resource requires identification via " - "a client certificate." } }, - { certificateNotAuthorized_GmStatusCode, - { 0x1f512, /* lock */ - "Certificate Not Authorized", - "The provided client certificate is valid but is not authorized for accessing " - "the requested resource. " } }, - { certificateNotValid_GmStatusCode, - { 0x1f6a8, /* revolving light */ - "Invalid Certificate", - "The provided client certificate is expired or invalid." } }, - }; - iForIndices(i, errors) { - if (errors[i].code == code) { - return &errors[i].err; + if (code == 0) { + return &none; + } + iForIndices(i, errors_) { + if (errors_[i].code == code) { + return &errors_[i].err; } } - return &none; + iAssert(errors_[0].code == unknownStatusCode_GmStatusCode); + return &errors_[0].err; /* unknown */ } -- cgit v1.2.3