Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

Commit 4fcc62a

Browse files
author
kevidryon2
committed
Added error codes
1 parent b4defd6 commit 4fcc62a

8 files changed

+27
-16
lines changed

build/bnsload-x86_64.o

0 Bytes
Binary file not shown.

build/main-x86_64.o

112 Bytes
Binary file not shown.

build/netc-x86_64_dynamic

0 Bytes
Binary file not shown.

build/tiger-x86_64.o

4.66 KB
Binary file not shown.

build/tiger-x86_64_dynamic

4.13 KB
Binary file not shown.

src/main.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -349,12 +349,12 @@ int main(int argc, char **argv, char **envp) {
349349
SetColor16(COLOR_RED);
350350
printf("%s ", reqdata->truepath);
351351
ResetColor16();
352-
snprintf(resbuff, BUFSIZ, "HTTP/1.0 404 Not Found\nServer: Tiger/"TIGER_VERS"\n\nError: File %s not found.\n", reqdata->truepath);
352+
TigerErrorHandler(404, resbuff, *reqdata, rootpath);
353353
} else {
354354
SetColor16(COLOR_RED);
355-
perror(reqdata->truepath);
356355
ResetColor16();
357-
snprintf(resbuff, BUFSIZ, "HTTP/1.0 500 Internal Server Error\nServer: Tiger/"TIGER_VERS"\n\nError: Recieved errno %d while trying to read file %s.\n", errno, reqdata->truepath);
356+
printf("ERROR %d ", errno);
357+
TigerErrorHandler(500, resbuff, *reqdata, rootpath);
358358
}
359359
write(csock, resbuff, strlen(resbuff));
360360
goto endreq;
@@ -380,7 +380,7 @@ int main(int argc, char **argv, char **envp) {
380380
#endif
381381
goto response;
382382
} else {
383-
snprintf(resbuff, BUFSIZ, "HTTP/1.0 500 Internal Server Error\nServer: Tiger/"TIGER_VERS"\n\nCan't compile PHP file");
383+
TigerErrorHandler(500, resbuff, *reqdata, rootpath);
384384
goto endreq;
385385
}
386386
}

src/server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "bns.h"
2121

2222
/* Tiger Version String */
23-
#define TIGER_VERS "X.1.03"
23+
#define TIGER_VERS "X.1.04"
2424

2525
#define max(a,b) ((a)>(b)?(a):(b))
2626
#define min(a,b) ((b)>(a)?(a):(b))

src/tiger.c

+22-11
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ loadFile_returnData TigerLoadFile(char *pubpath, char *cachepath) {
197197
pubfile = fopen(pubpath, "r");
198198
cachefile = fopen(cachepath, "w");
199199
if (!cachefile) {
200-
perror(cachepath);
201200
return (loadFile_returnData){0};
202201
}
203202

@@ -231,21 +230,33 @@ loadFile_returnData TigerLoadFile(char *pubpath, char *cachepath) {
231230
return data;
232231
}
233232

233+
char *defaulthandlertxt[] = {
234+
[400] = "Sorry, but your request could not be understood.",
235+
[401] = "Sorry, but you are not authorized to view this resource.",
236+
[403] = "Sorry, but you are forbidden from viewing this resource.",
237+
[404] = "Sorry, but the requested resource could not be found.",
238+
[410] = "Sorry, but the requested resource is not and will never be available again.",
239+
[418] = "Sorry, but this server only brews tea. The server is a teapot.",
240+
[451] = "Sorry, but the requested resource is not available due to legal reasons.",
241+
[500] = "Sorry, but the server had a stroke trying to figure out what to do.",
242+
[503] = "Sorry, but the server is overloaded and cannot handle the request.",
243+
[505] = "Sorry, but your HTTP Version was not supported.",
244+
};
245+
234246
void TigerErrorHandler(int status, char *response, RequestData reqdata, char *rootpath) {;
235247
sprintf(response, "HTTP/1.0 %d %s\nServer: Tiger/"TIGER_VERS"\r\n\r\n", status, httpcodes[status]);
236-
char *filename = malloc(9); //<status>.html
237-
sprintf(filename, "%03d.html", status);
238-
239-
void *tmp1, *tmp2;
248+
char filename[BUFSIZ]; //<status>.html
249+
sprintf(filename, "/%03d.html", status);
240250

241-
loadFile_returnData data = TigerLoadFile(tmp1 = combine(rootpath, "/public/404.html"), tmp2 = combine(rootpath, "/public/404.html"));
251+
char public_path[BUFSIZ];
252+
char cache_path[BUFSIZ];
253+
254+
loadFile_returnData data = TigerLoadFile(public_path, cache_path);
255+
242256
if (errno) {
243-
printf("E%d %s ", errno, reqdata.path);
244-
return (loadFile_returnData){0};
257+
//can't access error handler
258+
sprintf(response, "%s<html><body>%s</body></html>", response, defaulthandlertxt[status]);
245259
}
246-
247-
free(tmp1);
248-
free(tmp2);
249260
}
250261

251262
int TigerCallPHP(char *source_path, char *output_path, RequestData data, loadFile_returnData *output) {

0 commit comments

Comments
 (0)