B"H net_io: http: check if file can be sent
This commit is contained in:
parent
a82df07c0c
commit
eb41be3884
18
net_io.c
18
net_io.c
|
@ -741,14 +741,26 @@ int handleHTTPRequest(struct client *c, char *p) {
|
||||||
} else {
|
} else {
|
||||||
struct stat sbuf;
|
struct stat sbuf;
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
|
char *rp, *hrp;
|
||||||
|
|
||||||
|
rp = realpath(getFile, NULL);
|
||||||
|
hrp = realpath(HTMLPATH, NULL);
|
||||||
|
hrp = (hrp ? hrp : HTMLPATH);
|
||||||
|
clen = -1;
|
||||||
|
content = "Server error";
|
||||||
|
if (rp && (!strncmp(hrp, rp, strlen(hrp)))) {
|
||||||
if (stat(getFile, &sbuf) != -1 && (fd = open(getFile, O_RDONLY)) != -1) {
|
if (stat(getFile, &sbuf) != -1 && (fd = open(getFile, O_RDONLY)) != -1) {
|
||||||
content = (char *) malloc(sbuf.st_size);
|
content = (char *) malloc(sbuf.st_size);
|
||||||
if (read(fd, content, sbuf.st_size) == -1) {
|
if (read(fd, content, sbuf.st_size) != -1) {
|
||||||
snprintf(content, sbuf.st_size, "Error reading from file: %s", strerror(errno));
|
|
||||||
}
|
|
||||||
clen = sbuf.st_size;
|
clen = sbuf.st_size;
|
||||||
|
free(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
errno = ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (clen < 0) {
|
||||||
char buf[128];
|
char buf[128];
|
||||||
clen = snprintf(buf,sizeof(buf),"Error opening HTML file: %s", strerror(errno));
|
clen = snprintf(buf,sizeof(buf),"Error opening HTML file: %s", strerror(errno));
|
||||||
content = strdup(buf);
|
content = strdup(buf);
|
||||||
|
|
|
@ -75,6 +75,7 @@ _inline uint64_t strtoll(const char *p, void *e, UINT32 base) {return _atoi64(p)
|
||||||
_inline int inet_aton(const char * cp, DWORD * ulAddr) { *ulAddr = inet_addr(cp); return 0;}
|
_inline int inet_aton(const char * cp, DWORD * ulAddr) { *ulAddr = inet_addr(cp); return 0;}
|
||||||
#define snprintf _snprintf
|
#define snprintf _snprintf
|
||||||
#define vsnprintf _vsnprintf
|
#define vsnprintf _vsnprintf
|
||||||
|
#define realpath(A, B) _fullpath(B, A, _MAX_PATH)
|
||||||
|
|
||||||
_inline void cls() {
|
_inline void cls() {
|
||||||
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
|
Loading…
Reference in a new issue