-
Notifications
You must be signed in to change notification settings - Fork 5
/
readfile.c
46 lines (44 loc) · 989 Bytes
/
readfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "defines.h"
#include "utils.c"
int
main()
{
if(INIT_CGI_ENV()!=0) _500();//PWD not exist or access denied.
// _200();
char *path = getenv("QUERY_STRING");
if(strlen(path) <= 5){
_400();//no "data="
}
path += 5 * sizeof(char);//strip "data="
path = url_decode(path);
//printf(path);
if(check_blacklist(path, BLACKLIST_PREFIX))
_403();//not working
struct stat s;
if(stat(path, &s)==0){
if (s.st_mode&S_IFDIR) _403();
}
FILE *f;
if((f=fopen(path, "rb")) == NULL)
_404();
else{
HEADER("Content-type","application/octet-stream");
fseek(f, 0L, SEEK_END);
long len = ftell(f);
HEADER_L("Content-Length", len);
END_HEADERS();
rewind(f);
unsigned char b[CHUNK_SIZE];
int rc;
while ((rc=fread(b, sizeof(unsigned char), CHUNK_SIZE, f)) != 0){
fwrite(b, sizeof(unsigned char), rc, stdout);
}
fclose(f);
}
free(path);
return 0;
;}