mirror of
https://github.com/fumiama/simple-http-server.git
synced 2026-06-08 20:10:43 +08:00
改变命令行,增加以uid执行和414状态码
This commit is contained in:
18
README.md
18
README.md
@@ -6,30 +6,31 @@ Created November 1999 by J. David Blackstone.
|
|||||||
Modified June 2021 by Fumiama(源文雨)
|
Modified June 2021 by Fumiama(源文雨)
|
||||||
|
|
||||||
# Protocol
|
# Protocol
|
||||||
|
A necessary subset of `HTTP 1.0` with following options of request header being supported.
|
||||||
|
|
||||||
A necessary subset of `HTTP 1.0` with following options of request header being supported
|
|
||||||
### From client
|
### From client
|
||||||
- Content-Length
|
- Content-Length
|
||||||
|
|
||||||
### From server
|
### From server
|
||||||
- Content-Length
|
- Content-Length
|
||||||
- Content-Type (only support text/plain image/x-icon text/css text/html)
|
- Content-Type (only support text/plain image/x-icon text/css text/html)
|
||||||
- Server
|
- Server
|
||||||
|
|
||||||
### Code
|
### Code
|
||||||
- 200 OK
|
- 200 OK
|
||||||
- 400 BAD REQUEST
|
- 400 BAD REQUEST
|
||||||
- 404 NOT FOUND
|
- 404 NOT FOUND
|
||||||
|
- 414 Request-URI Too Long
|
||||||
- 500 Internal Server Error
|
- 500 Internal Server Error
|
||||||
- 501 Method Not Implemented
|
- 501 Method Not Implemented
|
||||||
|
|
||||||
# Features
|
# Features
|
||||||
|
|
||||||
1. Serve files
|
1. Serve files
|
||||||
2. CGI
|
2. CGI
|
||||||
3. Listen on `ipv6`
|
3. Listen on `ipv6`
|
||||||
4. Multi-thread
|
4. Multi-thread
|
||||||
|
|
||||||
# Compile
|
# Compile
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git clone https://github.com/fumiama/simple-http-server.git
|
git clone https://github.com/fumiama/simple-http-server.git
|
||||||
cd simple-http-server
|
cd simple-http-server
|
||||||
@@ -41,17 +42,16 @@ make install
|
|||||||
```
|
```
|
||||||
|
|
||||||
# Command line usage
|
# Command line usage
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
simple-http-server -d port chdir
|
simple-http-server [-d] [-p <port>] [-r <rootdir>] [-u <uid>]
|
||||||
```
|
```
|
||||||
|
|
||||||
- **-d** - run as daemon
|
- **-d**: run as daemon.
|
||||||
- **port** - bind server on this port (0 for a random one)
|
- **-p**: if not set, choose a random one.
|
||||||
- **chdir** - change root dir to here
|
- **-r**: http root dir.
|
||||||
|
- **-u**: run as this uid.
|
||||||
|
|
||||||
# CGI usage
|
# CGI usage
|
||||||
|
|
||||||
When you put an executable file into the web path, the server will call `execl` to run it while passing 3 parameters as below
|
When you put an executable file into the web path, the server will call `execl` to run it while passing 3 parameters as below
|
||||||
|
|
||||||
```c
|
```c
|
||||||
|
|||||||
73
server.c
73
server.c
@@ -58,6 +58,7 @@ static void headers(int, const char *);
|
|||||||
static void not_found(int);
|
static void not_found(int);
|
||||||
static void serve_file(int, const char *);
|
static void serve_file(int, const char *);
|
||||||
static int startup(u_int16_t *);
|
static int startup(u_int16_t *);
|
||||||
|
static void toolong(int);
|
||||||
static void unimplemented(int);
|
static void unimplemented(int);
|
||||||
|
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
@@ -65,6 +66,11 @@ static void unimplemented(int);
|
|||||||
* return. Process the request appropriately.
|
* return. Process the request appropriately.
|
||||||
* Parameters: the socket connected to the client */
|
* Parameters: the socket connected to the client */
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
|
|
||||||
|
/* read & discard headers */
|
||||||
|
#define discard(client) \
|
||||||
|
while((numchars > 0) && strcmp("\n", buf)) numchars = get_line(client, buf, sizeof(buf))
|
||||||
|
|
||||||
static void accept_request(void *cli) {
|
static void accept_request(void *cli) {
|
||||||
pthread_detach(pthread_self());
|
pthread_detach(pthread_self());
|
||||||
int client =(int)cli;
|
int client =(int)cli;
|
||||||
@@ -95,6 +101,7 @@ static void accept_request(void *cli) {
|
|||||||
|
|
||||||
if(strcasecmp(method, "POST") == 0) {
|
if(strcasecmp(method, "POST") == 0) {
|
||||||
if(method_type == GET) {
|
if(method_type == GET) {
|
||||||
|
discard(client);
|
||||||
unimplemented(client);
|
unimplemented(client);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
@@ -104,11 +111,14 @@ static void accept_request(void *cli) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(ISspace(buf[j]) &&(j < sizeof(buf))) j++;
|
while(ISspace(buf[j]) && (j < sizeof(buf))) j++;
|
||||||
while(!ISspace(buf[j]) &&(i < sizeof(url) - 1) &&(j < sizeof(buf))) {
|
for(; !ISspace(buf[j]) && (i < sizeof(url) - 1) && (j < sizeof(buf)); i++, j++) {
|
||||||
url[i] = buf[j];
|
url[i] = buf[j];
|
||||||
i++;
|
}
|
||||||
j++;
|
if(!ISspace(buf[j])) {
|
||||||
|
discard(client);
|
||||||
|
toolong(client);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
url[i] = '\0';
|
url[i] = '\0';
|
||||||
|
|
||||||
@@ -127,16 +137,24 @@ static void accept_request(void *cli) {
|
|||||||
while((*url_start == '.' || *url_start == '/' || *url_start == '#') && *url_start != 0) url_start++;
|
while((*url_start == '.' || *url_start == '/' || *url_start == '#') && *url_start != 0) url_start++;
|
||||||
path[0] = '.'; path[1] = '/'; path[2] = 0;
|
path[0] = '.'; path[1] = '/'; path[2] = 0;
|
||||||
if(*url_start) strncat(path, url_start, 499);
|
if(*url_start) strncat(path, url_start, 499);
|
||||||
printf("%s: %s with query: %s\n", method, path, query_string);
|
printf("%s %s with query '%s': ", method, path, query_string);
|
||||||
|
//花括号不可省略
|
||||||
if(stat(path, &st) == -1) {
|
if(stat(path, &st) == -1) {
|
||||||
/* read & discard headers */
|
discard(client);
|
||||||
while((numchars > 0) && strcmp("\n", buf)) numchars = get_line(client, buf, sizeof(buf));
|
|
||||||
not_found(client);
|
not_found(client);
|
||||||
} else {
|
} else {
|
||||||
if((st.st_mode & S_IFMT) == S_IFDIR) strcat(path, "/index.html");
|
if((st.st_mode & S_IFMT) == S_IFDIR) {
|
||||||
else if((st.st_mode & S_IXUSR) ||(st.st_mode & S_IXGRP) ||(st.st_mode & S_IXOTH)) cgi = 1;
|
strcat(path, "/index.html");
|
||||||
/* read & discard headers */
|
//花括号不可省略
|
||||||
|
if(stat(path, &st) == -1) {
|
||||||
|
discard(client);
|
||||||
|
not_found(client);
|
||||||
|
close(client);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
int content_length = 0;
|
int content_length = 0;
|
||||||
|
if((st.st_mode & S_IXUSR) ||(st.st_mode & S_IXGRP) ||(st.st_mode & S_IXOTH)) cgi = 1;
|
||||||
while((numchars > 0) && strcmp("\n", buf)) {
|
while((numchars > 0) && strcmp("\n", buf)) {
|
||||||
numchars = get_line(client, buf, sizeof(buf));
|
numchars = get_line(client, buf, sizeof(buf));
|
||||||
if(!content_length && strcasestr(buf, "Content-Length: ")) {
|
if(!content_length && strcasestr(buf, "Content-Length: ")) {
|
||||||
@@ -391,6 +409,7 @@ static void headers(int client, const char *filepath) {
|
|||||||
ADD_HERDER_PARAM(CONTENT_TYPE, EXTNM_IS_NOT("html")?(EXTNM_IS_NOT(".css")?(EXTNM_IS_NOT(".ico")?"text/plain":"image/x-icon"):"text/css"):"text/html");
|
ADD_HERDER_PARAM(CONTENT_TYPE, EXTNM_IS_NOT("html")?(EXTNM_IS_NOT(".css")?(EXTNM_IS_NOT(".ico")?"text/plain":"image/x-icon"):"text/css"):"text/html");
|
||||||
ADD_HERDER_PARAM(CONTENT_LEN "\r\n", get_file_size(filepath, client));
|
ADD_HERDER_PARAM(CONTENT_LEN "\r\n", get_file_size(filepath, client));
|
||||||
send(client, buf, offset, 0);
|
send(client, buf, offset, 0);
|
||||||
|
puts("200 OK.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
@@ -399,6 +418,7 @@ static void headers(int client, const char *filepath) {
|
|||||||
#define HTTP404 "HTTP/1.0 404 NOT FOUND\r\n" SERVER_STRING "Content-Type: text/html\r\n\r\n<HTML><TITLE>Not Found</TITLE>\r\n<BODY><P>The server could not fulfill\r\nyour request because the resource specified\r\nis unavailable or nonexistent.\r\n</BODY></HTML>\r\n"
|
#define HTTP404 "HTTP/1.0 404 NOT FOUND\r\n" SERVER_STRING "Content-Type: text/html\r\n\r\n<HTML><TITLE>Not Found</TITLE>\r\n<BODY><P>The server could not fulfill\r\nyour request because the resource specified\r\nis unavailable or nonexistent.\r\n</BODY></HTML>\r\n"
|
||||||
static void not_found(int client) {
|
static void not_found(int client) {
|
||||||
send(client, HTTP404, sizeof(HTTP404)-1, 0);
|
send(client, HTTP404, sizeof(HTTP404)-1, 0);
|
||||||
|
puts("404 Not Found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
@@ -466,6 +486,16 @@ static int startup(uint16_t *port) {
|
|||||||
return(httpd);
|
return(httpd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**********************************************************************/
|
||||||
|
/* Inform the client that the requested web method has not been
|
||||||
|
* implemented.
|
||||||
|
* Parameter: the client socket */
|
||||||
|
/**********************************************************************/
|
||||||
|
#define HTTP414 "HTTP/1.0 414 Request-URI Too Long\r\n" SERVER_STRING "Content-Type: text/html\r\n\r\n<HTML><HEAD><TITLE>Request-URI Too Long\r\n</TITLE></HEAD>\r\n<BODY><P>HTTP request url length is over 254 chars.\r\n</BODY></HTML>\r\n"
|
||||||
|
static void toolong(int client) {
|
||||||
|
send(client, HTTP414, sizeof(HTTP414)-1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
/* Inform the client that the requested web method has not been
|
/* Inform the client that the requested web method has not been
|
||||||
* implemented.
|
* implemented.
|
||||||
@@ -492,23 +522,36 @@ static void unimplemented(int client) {
|
|||||||
error_die("accept");\
|
error_die("accept");\
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define argequ(arg) (*(uint16_t*)argv[i] == *(uint16_t*)(arg))
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if(argc != 3 && argc != 4) puts("Usage: simple-http-server -d port chdir");
|
if(argc > 8) puts("Usage:\tsimple-http-server [-d] [-p <port>] [-r <rootdir>] [-u <uid>]\n -d:\trun as daemon.\n -p:\tif not set, choose a random one.\n -r:\thttp root dir.\n -u:\trun as this uid.");
|
||||||
else {
|
else {
|
||||||
int as_daemon = *(uint16_t*)argv[1] == *(uint16_t*)"-d";
|
int as_daemon = 0;
|
||||||
|
uint16_t port = 0;
|
||||||
|
char *cdir = "./";
|
||||||
|
uid_t uid = -1;
|
||||||
int server_sock = -1;
|
int server_sock = -1;
|
||||||
uint16_t port =(uint16_t)atoi(argv[as_daemon?2:1]);
|
|
||||||
int client_sock = -1;
|
int client_sock = -1;
|
||||||
int pid = -1;
|
int pid = -1;
|
||||||
struct sockaddr_in client_name;
|
struct sockaddr_in client_name;
|
||||||
socklen_t client_name_len = sizeof(client_name);
|
socklen_t client_name_len = sizeof(client_name);
|
||||||
pthread_t newthread;
|
pthread_t newthread;
|
||||||
|
|
||||||
char *cdir = argv[as_daemon?3:2];
|
for(int i = 1; i < argc; i++) {
|
||||||
|
if(!as_daemon && argequ("-d")) as_daemon = 1;
|
||||||
|
else if(argequ("-p")) port = (uint16_t)atoi(argv[i + 1]);
|
||||||
|
else if(argequ("-r")) cdir = argv[i + 1];
|
||||||
|
else if(argequ("-u")) uid = atoi(argv[i + 1]);
|
||||||
|
}
|
||||||
|
|
||||||
if(chdir(cdir)) error_die("chdir");
|
if(chdir(cdir)) error_die("chdir");
|
||||||
server_sock = startup(&port);
|
server_sock = startup(&port);
|
||||||
printf("httpd running on port %d\n", port);
|
printf("httpd running on 0.0.0.0:%d at %s\n", port, cdir);
|
||||||
if(as_daemon) {
|
if(as_daemon) {
|
||||||
|
if(uid > 0) {
|
||||||
|
setuid(uid);
|
||||||
|
setgid(uid);
|
||||||
|
}
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if(pid == 0) pid = fork();
|
if(pid == 0) pid = fork();
|
||||||
else return 0;
|
else return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user