summaryrefslogtreecommitdiff
path: root/src/gmutil.c
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-08-01 22:50:07 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-08-01 22:50:07 +0300
commit5fb32fcfca41d2e1b6eaaacaa6b6b37fc9e6d0b1 (patch)
tree2e01c7226a558bb51d8c4e82c46600e57d74f3cb /src/gmutil.c
parenta05d181c72df33256e603f2e4543ee73665b242c (diff)
Handling status codes by class
Diffstat (limited to 'src/gmutil.c')
-rw-r--r--src/gmutil.c198
1 files changed, 109 insertions, 89 deletions
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) {
146 } 146 }
147} 147}
148 148
149static const struct {
150 enum iGmStatusCode code;
151 iGmError err;
152} errors_[] = {
153 { unknownStatusCode_GmStatusCode, /* keep this as the first one (fallback return value) */
154 { 0x1f4ab, /* dizzy */
155 "Unknown Status Code",
156 "The server responded with a status code that is not specified in the Gemini "
157 "protocol as known to this client. Maybe the server is from the future? Or "
158 "just malfunctioning."} },
159 { failedToOpenFile_GmStatusCode,
160 { 0x1f4c1, /* file folder */
161 "Failed to Open File",
162 "The requested file does not exist or is inaccessible. "
163 "Please check the file path." } },
164 { unsupportedMimeType_GmStatusCode,
165 { 0x1f47d, /* alien */
166 "Unsupported MIME Type",
167 "The received content is in an unsupported format and cannot be viewed with "
168 "this application." } },
169 { invalidHeader_GmStatusCode,
170 { 0x1f4a9, /* pile of poo */
171 "Invalid Header",
172 "The received header did not conform to the Gemini specification. "
173 "Perhaps the server is malfunctioning or you tried to contact a "
174 "non-Gemini server." } },
175 { invalidRedirect_GmStatusCode,
176 { 0, /* */
177 "Invalid Redirect",
178 "The server responded with a redirect but did not provide a valid destination URL. "
179 "Perhaps the server is malfunctioning." } },
180 { temporaryFailure_GmStatusCode,
181 { 0x1f50c, /* electric plug */
182 "Temporary Failure",
183 "The request has failed, but may succeed if you try again in the future." } },
184 { serverUnavailable_GmStatusCode,
185 { 0x1f525, /* fire */
186 "Server Unavailable",
187 "The server is unavailable due to overload or maintenance. Check back later." } },
188 { cgiError_GmStatusCode,
189 { 0x1f4a5, /* collision */
190 "CGI Error",
191 "Failure during dynamic content generation on the server. This may be due "
192 "to buggy serverside software." } },
193 { proxyError_GmStatusCode,
194 { 0x1f310, /* globe */
195 "Proxy Error",
196 "A proxy request failed because the server was unable to successfully "
197 "complete a transaction with the remote host. Perhaps there are difficulties "
198 "with network connectivity." } },
199 { slowDown_GmStatusCode,
200 { 0x1f40c, /* snail */
201 "Slow Down",
202 "The server is rate limiting requests. Please wait..." } },
203 { permanentFailure_GmStatusCode,
204 { 0x1f6ab, /* no entry */
205 "Permanent Failure",
206 "Your request has failed and will fail in the future as well if repeated." } },
207 { notFound_GmStatusCode,
208 { 0x1f50d, /* magnifying glass */
209 "Not Found",
210 "The requested resource could not be found at this time." } },
211 { gone_GmStatusCode,
212 { 0x1f47b, /* ghost */
213 "Gone",
214 "The resource requested is no longer available and will not be available again." } },
215 { proxyRequestRefused_GmStatusCode,
216 { 0x1f6c2, /* passport control */
217 "Proxy Request Refused",
218 "The request was for a resource at a domain not served by the server and the "
219 "server does not accept proxy requests." } },
220 { badRequest_GmStatusCode,
221 { 0x1f44e, /* thumbs down */
222 "Bad Request",
223 "The server was unable to parse your request, presumably due to the "
224 "request being malformed. Likely a bug in Lagrange." } },
225 { clientCertificateRequired_GmStatusCode,
226 { 0x1f511, /* key */
227 "Certificate Required",
228 "Access to the requested resource requires identification via "
229 "a client certificate." } },
230 { certificateNotAuthorized_GmStatusCode,
231 { 0x1f512, /* lock */
232 "Certificate Not Authorized",
233 "The provided client certificate is valid but is not authorized for accessing "
234 "the requested resource. " } },
235 { certificateNotValid_GmStatusCode,
236 { 0x1f6a8, /* revolving light */
237 "Invalid Certificate",
238 "The provided client certificate is expired or invalid." } },
239};
240
241iBool isDefined_GmError(enum iGmStatusCode code) {
242 iForIndices(i, errors_) {
243 if (errors_[i].code == code) {
244 return iTrue;
245 }
246 }
247 return iFalse;
248}
249
149const iGmError *get_GmError(enum iGmStatusCode code) { 250const iGmError *get_GmError(enum iGmStatusCode code) {
150 static const iGmError none = { 0, "", "" }; 251 static const iGmError none = { 0, "", "" };
151 static const struct { 252 if (code == 0) {
152 enum iGmStatusCode code; 253 return &none;
153 iGmError err; 254 }
154 } errors[] = { 255 iForIndices(i, errors_) {
155 { failedToOpenFile_GmStatusCode, 256 if (errors_[i].code == code) {
156 { 0x1f4c1, /* file folder */ 257 return &errors_[i].err;
157 "Failed to Open File",
158 "The requested file does not exist or is inaccessible. "
159 "Please check the file path." } },
160 { unsupportedMimeType_GmStatusCode,
161 { 0x1f47d, /* alien */
162 "Unsupported MIME Type",
163 "The received content is in an unsupported format and cannot be viewed with "
164 "this application." } },
165 { invalidHeader_GmStatusCode,
166 { 0x1f4a9, /* pile of poo */
167 "Invalid Header",
168 "The received header did not conform to the Gemini specification. "
169 "Perhaps the server is malfunctioning or you tried to contact a "
170 "non-Gemini server." } },
171 { invalidRedirect_GmStatusCode,
172 { 0, /* */
173 "Invalid Redirect",
174 "The server responded with a redirect but did not provide a valid destination URL. "
175 "Perhaps the server is malfunctioning." } },
176 { temporaryFailure_GmStatusCode,
177 { 0x1f50c, /* electric plug */
178 "Temporary Failure",
179 "The request has failed, but may succeed if you try again in the future." } },
180 { serverUnavailable_GmStatusCode,
181 { 0x1f525, /* fire */
182 "Server Unavailable",
183 "The server is unavailable due to overload or maintenance. Check back later." } },
184 { cgiError_GmStatusCode,
185 { 0x1f4a5, /* collision */
186 "CGI Error",
187 "Failure during dynamic content generation on the server. This may be due "
188 "to buggy serverside software." } },
189 { proxyError_GmStatusCode,
190 { 0x1f310, /* globe */
191 "Proxy Error",
192 "A proxy request failed because the server was unable to successfully "
193 "complete a transaction with the remote host. Perhaps there are difficulties "
194 "with network connectivity." } },
195 { slowDown_GmStatusCode,
196 { 0x1f40c, /* snail */
197 "Slow Down",
198 "The server is rate limiting requests. Please wait..." } },
199 { permanentFailure_GmStatusCode,
200 { 0x1f6ab, /* no entry */
201 "Permanent Failure",
202 "Your request has failed and will fail in the future as well if repeated." } },
203 { notFound_GmStatusCode,
204 { 0x1f50d, /* magnifying glass */
205 "Not Found",
206 "The requested resource could not be found at this time." } },
207 { gone_GmStatusCode,
208 { 0x1f47b, /* ghost */
209 "Gone",
210 "The resource requested is no longer available and will not be available again." } },
211 { proxyRequestRefused_GmStatusCode,
212 { 0x1f6c2, /* passport control */
213 "Proxy Request Refused",
214 "The request was for a resource at a domain not served by the server and the "
215 "server does not accept proxy requests." } },
216 { badRequest_GmStatusCode,
217 { 0x1f44e, /* thumbs down */
218 "Bad Request",
219 "The server was unable to parse your request, presumably due to the "
220 "request being malformed. Likely a bug in Lagrange." } },
221 { clientCertificateRequired_GmStatusCode,
222 { 0x1f511, /* key */
223 "Certificate Required",
224 "Access to the requested resource requires identification via "
225 "a client certificate." } },
226 { certificateNotAuthorized_GmStatusCode,
227 { 0x1f512, /* lock */
228 "Certificate Not Authorized",
229 "The provided client certificate is valid but is not authorized for accessing "
230 "the requested resource. " } },
231 { certificateNotValid_GmStatusCode,
232 { 0x1f6a8, /* revolving light */
233 "Invalid Certificate",
234 "The provided client certificate is expired or invalid." } },
235 };
236 iForIndices(i, errors) {
237 if (errors[i].code == code) {
238 return &errors[i].err;
239 } 258 }
240 } 259 }
241 return &none; 260 iAssert(errors_[0].code == unknownStatusCode_GmStatusCode);
261 return &errors_[0].err; /* unknown */
242} 262}