1
0
mirror of https://github.com/fumiama/simple-http-server.git synced 2026-06-07 19:40:36 +08:00

cgi特判

This commit is contained in:
源文雨
2021-06-08 16:40:22 +08:00
parent 025f10abf5
commit 832f669fad

View File

@@ -44,7 +44,7 @@ static void handle_quit(int);
static void headers(int, const char *); 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_short *); static int startup(u_int16_t *);
static void unimplemented(int); static void unimplemented(int);
/**********************************************************************/ /**********************************************************************/
@@ -155,7 +155,7 @@ static void cat(int client, FILE *resource) {
rewind(resource); rewind(resource);
sendfile(client, fileno(resource), &len, file_size); sendfile(client, fileno(resource), &len, file_size);
#endif #endif
printf("Send %u bytes.\n", len); printf("Send %lu bytes.\n", len);
} }
/**********************************************************************/ /**********************************************************************/
@@ -264,19 +264,23 @@ static void execute_cgi(int client, const char *path, const char *method, const
} }
} }
uint32_t cnt = 0; uint32_t cnt = 0;
if(read(cgi_output[0], (char*)&cnt, sizeof(uint32_t)) > 0) { char* p = (char*)&cnt;
printf("cgi msg cnt: %u bytes.\n", cnt); while(p - (char*)&cnt < sizeof(uint32_t)) {
if(cnt > 0) { int offset = read(cgi_output[0], p, sizeof(uint32_t));
int len = 0; if(offset > 0) p += offset;
char* data = malloc(cnt); else cannot_execute(client);
while(len < cnt) { }
len += read(cgi_output[0], data, cnt); printf("cgi msg cnt: %u bytes.\n", cnt);
send(client, data, len, 0); if(cnt > 0) {
} int len = 0;
if(data) free(data); char* data = malloc(cnt);
printf("cgi send %d bytes\n", len); while(len < cnt) {
len += read(cgi_output[0], data, cnt);
send(client, data, len, 0);
} }
} else cannot_execute(client); if(data) free(data);
printf("cgi send %d bytes\n", len);
}
close(cgi_output[0]); close(cgi_output[0]);
close(cgi_input[1]); close(cgi_input[1]);
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
@@ -366,8 +370,8 @@ static void handle_quit(int signo) {
#define CONTENT_LEN "Content-Length: %d\r\n" #define CONTENT_LEN "Content-Length: %d\r\n"
static void headers(int client, const char *filepath) { static void headers(int client, const char *filepath) {
char buf[1024]; char buf[1024];
uint offset = 0; uint32_t offset = 0;
uint extpos = strlen(filepath) - 4; uint32_t extpos = strlen(filepath) - 4;
ADD_HERDER(HTTP200 SERVER_STRING); ADD_HERDER(HTTP200 SERVER_STRING);
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");
@@ -425,7 +429,7 @@ static void serve_file(int client, const char *filename) {
static struct sockaddr_in name; static struct sockaddr_in name;
#endif #endif
static int startup(u_short *port) { static int startup(uint16_t *port) {
int httpd = 0; int httpd = 0;
#ifdef LISTEN_ON_IPV6 #ifdef LISTEN_ON_IPV6
@@ -490,7 +494,7 @@ int main(int argc, char **argv) {
else { else {
int as_daemon = *(uint16_t*)argv[1] == *(uint16_t*)"-d"; int as_daemon = *(uint16_t*)argv[1] == *(uint16_t*)"-d";
int server_sock = -1; int server_sock = -1;
u_short port = (u_short)atoi(argv[as_daemon?2: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;