mirror of
https://github.com/hermitcore/libhermit.git
synced 2025-03-09 00:00:03 +01:00
add helper functions to check if all data is written to the socket
This commit is contained in:
parent
3ba88256f1
commit
8bc778b6fe
1 changed files with 20 additions and 11 deletions
|
@ -25,10 +25,6 @@
|
||||||
#define FORBIDDEN 403
|
#define FORBIDDEN 403
|
||||||
#define NOTFOUND 404
|
#define NOTFOUND 404
|
||||||
|
|
||||||
#ifdef __hermit__
|
|
||||||
void sys_msleep(unsigned int ms);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
char *ext;
|
char *ext;
|
||||||
char *filetype;
|
char *filetype;
|
||||||
|
@ -45,6 +41,19 @@ struct {
|
||||||
{"html","text/html" },
|
{"html","text/html" },
|
||||||
{0,0} };
|
{0,0} };
|
||||||
|
|
||||||
|
static ssize_t nweb_write(int fd, const char* buf, size_t len)
|
||||||
|
{
|
||||||
|
size_t pos = 0;
|
||||||
|
|
||||||
|
while (pos < len) {
|
||||||
|
ssize_t r = write(fd, buf+pos, len-pos);
|
||||||
|
if (r > 0)
|
||||||
|
pos += r;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
static void logger(int type, char *s1, char *s2, int socket_fd)
|
static void logger(int type, char *s1, char *s2, int socket_fd)
|
||||||
{
|
{
|
||||||
int fd ;
|
int fd ;
|
||||||
|
@ -54,19 +63,19 @@ static void logger(int type, char *s1, char *s2, int socket_fd)
|
||||||
case ERROR: (void)sprintf(logbuffer,"ERROR: %s:%s Errno=%d exiting pid=%d",s1, s2, errno,getpid());
|
case ERROR: (void)sprintf(logbuffer,"ERROR: %s:%s Errno=%d exiting pid=%d",s1, s2, errno,getpid());
|
||||||
break;
|
break;
|
||||||
case FORBIDDEN:
|
case FORBIDDEN:
|
||||||
(void)write(socket_fd, "HTTP/1.1 403 Forbidden\nContent-Length: 185\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>403 Forbidden</title>\n</head><body>\n<h1>Forbidden</h1>\nThe requested URL, file type or operation is not allowed on this simple static file webserver.\n</body></html>\n",271);
|
(void)nweb_write(socket_fd, "HTTP/1.1 403 Forbidden\nContent-Length: 185\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>403 Forbidden</title>\n</head><body>\n<h1>Forbidden</h1>\nThe requested URL, file type or operation is not allowed on this simple static file webserver.\n</body></html>\n",271);
|
||||||
(void)sprintf(logbuffer,"FORBIDDEN: %s:%s",s1, s2);
|
(void)sprintf(logbuffer,"FORBIDDEN: %s:%s",s1, s2);
|
||||||
break;
|
break;
|
||||||
case NOTFOUND:
|
case NOTFOUND:
|
||||||
(void)write(socket_fd, "HTTP/1.1 404 Not Found\nContent-Length: 136\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\nThe requested URL was not found on this server.\n</body></html>\n",224);
|
(void)nweb_write(socket_fd, "HTTP/1.1 404 Not Found\nContent-Length: 136\nConnection: close\nContent-Type: text/html\n\n<html><head>\n<title>404 Not Found</title>\n</head><body>\n<h1>Not Found</h1>\nThe requested URL was not found on this server.\n</body></html>\n",224);
|
||||||
(void)sprintf(logbuffer,"NOT FOUND: %s:%s",s1, s2);
|
(void)sprintf(logbuffer,"NOT FOUND: %s:%s",s1, s2);
|
||||||
break;
|
break;
|
||||||
case LOG: (void)sprintf(logbuffer," INFO: %s:%s:%d",s1, s2,socket_fd); break;
|
case LOG: (void)sprintf(logbuffer," INFO: %s:%s:%d",s1, s2,socket_fd); break;
|
||||||
}
|
}
|
||||||
/* No checks here, nothing can be done with a failure anyway */
|
/* No checks here, nothing can be done with a failure anyway */
|
||||||
if((fd = open("nweb.log", O_CREAT| O_WRONLY | O_APPEND,0644)) >= 0) {
|
if((fd = open("nweb.log", O_CREAT| O_WRONLY | O_APPEND,0644)) >= 0) {
|
||||||
(void)write(fd,logbuffer,strlen(logbuffer));
|
(void)nweb_write(fd,logbuffer,strlen(logbuffer));
|
||||||
(void)write(fd,"\n",1);
|
(void)nweb_write(fd,"\n",1);
|
||||||
(void)close(fd);
|
(void)close(fd);
|
||||||
}
|
}
|
||||||
if(type == ERROR || type == NOTFOUND || type == FORBIDDEN) exit(3);
|
if(type == ERROR || type == NOTFOUND || type == FORBIDDEN) exit(3);
|
||||||
|
@ -78,7 +87,7 @@ static void web(int fd, int hit)
|
||||||
int j, file_fd, buflen;
|
int j, file_fd, buflen;
|
||||||
long i, ret, len;
|
long i, ret, len;
|
||||||
char * fstr;
|
char * fstr;
|
||||||
static char buffer[BUFSIZE+1]; /* static so zero filled */
|
char buffer[BUFSIZE+1]; /* static so zero filled */
|
||||||
|
|
||||||
ret =read(fd,buffer,BUFSIZE); /* read Web request in one go */
|
ret =read(fd,buffer,BUFSIZE); /* read Web request in one go */
|
||||||
if(ret == 0 || ret == -1) { /* read failure stop now */
|
if(ret == 0 || ret == -1) { /* read failure stop now */
|
||||||
|
@ -127,11 +136,11 @@ static void web(int fd, int hit)
|
||||||
(void)lseek(file_fd, (off_t)0, SEEK_SET); /* lseek back to the file start ready for reading */
|
(void)lseek(file_fd, (off_t)0, SEEK_SET); /* lseek back to the file start ready for reading */
|
||||||
(void)sprintf(buffer,"HTTP/1.1 200 OK\nServer: nweb/%d.0\nContent-Length: %ld\nConnection: close\nContent-Type: %s\n\n", VERSION, len, fstr); /* Header + a blank line */
|
(void)sprintf(buffer,"HTTP/1.1 200 OK\nServer: nweb/%d.0\nContent-Length: %ld\nConnection: close\nContent-Type: %s\n\n", VERSION, len, fstr); /* Header + a blank line */
|
||||||
logger(LOG,"Header",buffer,hit);
|
logger(LOG,"Header",buffer,hit);
|
||||||
(void)write(fd,buffer,strlen(buffer));
|
(void)nweb_write(fd,buffer,strlen(buffer));
|
||||||
|
|
||||||
/* send file in 8KB block - last block may be smaller */
|
/* send file in 8KB block - last block may be smaller */
|
||||||
while ( (ret = read(file_fd, buffer, BUFSIZE)) > 0 ) {
|
while ( (ret = read(file_fd, buffer, BUFSIZE)) > 0 ) {
|
||||||
(void)write(fd,buffer,ret);
|
(void)nweb_write(fd,buffer,ret);
|
||||||
}
|
}
|
||||||
#ifdef __hermit__
|
#ifdef __hermit__
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
Loading…
Add table
Reference in a new issue