Skip to content

Commit

Permalink
Update http mulcore server. compare with nginx.
Browse files Browse the repository at this point in the history
  • Loading branch information
wangbojing committed Aug 20, 2018
1 parent ba0fd45 commit 4c3c59c
Showing 1 changed file with 72 additions and 14 deletions.
86 changes: 72 additions & 14 deletions nty_http_server_mulcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static int global_semid = -1;

typedef struct _shm_area {
int total;
int lock;
int accept_lock;
struct timeval tv_begin;
} shm_area;

Expand All @@ -97,6 +97,11 @@ int sub_shmvalue(void);

int init_shmvalue(void);

int lock_accept_mutex(void);
int unlock_accept_mutex(void);




unsigned long cmpxchg(void *addr, unsigned long _old, unsigned long _new, int size) {
unsigned long prev;
Expand Down Expand Up @@ -192,9 +197,9 @@ void accept_request(void *arg)

char buf[1024];
size_t numchars;
char method[255];
char url[255];
char path[512];
char method[16];
char url[32];
char path[64];
size_t i, j;
struct stat st;
int cgi = 0; /* becomes true if server decides this is a CGI
Expand Down Expand Up @@ -316,7 +321,7 @@ void bad_request(int client)

void cat(int client, FILE *resource)
{
#if 0
#if ENABLE_NTYCO
char buf[1024] = HTML_PAGE;

send(client, buf, strlen(buf), 0);
Expand Down Expand Up @@ -520,16 +525,28 @@ void headers(int client, const char *filename)
{
char buf[1024] = {0};
(void)filename; /* could use filename to determine file type */

#if ENABLE_NTYCO


strcpy(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, SERVER_STRING);
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-Type: text/html\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, "\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, "HTTP/1.0 200 OK\r\n");
strcat(buf, SERVER_STRING);
strcat(buf, "Content-Type: text/html\r\n");
strcat(buf, "\r\n");

send(client, buf, strlen(buf), 0);

#else
strcpy(buf, "HTTP/1.0 200 OK\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, SERVER_STRING);
send(client, buf, strlen(buf), 0);
sprintf(buf, "Content-Type: text/html\r\n");
send(client, buf, strlen(buf), 0);
strcpy(buf, "\r\n");
send(client, buf, strlen(buf), 0);

#endif
}

/**********************************************************************/
Expand All @@ -538,7 +555,21 @@ void headers(int client, const char *filename)
void not_found(int client)
{
char buf[1024];
#if ENABLE_NTYCO

sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");
strcat(buf, SERVER_STRING);
strcat(buf, "Content-Type: text/html\r\n");
strcat(buf, "\r\n");
strcat(buf, "<HTML><TITLE>Not Found</TITLE>\r\n");
strcat(buf, "<BODY><P>The server could not fulfill\r\n");
strcat(buf, "your request because the resource specified\r\n");
strcat(buf, "is unavailable or nonexistent.\r\n");
strcat(buf, "</BODY></HTML>\r\n");

send(client, buf, strlen(buf), 0);

#else
sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, SERVER_STRING);
Expand All @@ -557,6 +588,7 @@ void not_found(int client)
send(client, buf, strlen(buf), 0);
sprintf(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
#endif
}

/**********************************************************************/
Expand Down Expand Up @@ -638,8 +670,19 @@ int startup(u_short *port)
void unimplemented(int client)
{
char buf[1024];

#if ENABLE_NTYCO
sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n");
strcat(buf, SERVER_STRING);
strcat(buf, "Content-Type: text/html\r\n");
strcat(buf, "\r\n");
strcat(buf, "<HTML><HEAD><TITLE>Method Not Implemented\r\n");
strcat(buf, "</TITLE></HEAD>\r\n");
strcat(buf, "<BODY><P>HTTP request method not supported.\r\n");
strcat(buf, "</BODY></HTML>\r\n");

send(client, buf, strlen(buf), 0);
#else
sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n");
send(client, buf, strlen(buf), 0);
sprintf(buf, SERVER_STRING);
send(client, buf, strlen(buf), 0);
Expand All @@ -655,6 +698,7 @@ void unimplemented(int client)
send(client, buf, strlen(buf), 0);
sprintf(buf, "</BODY></HTML>\r\n");
send(client, buf, strlen(buf), 0);
#endif
}

void server(void *arg) {
Expand Down Expand Up @@ -774,6 +818,20 @@ int sub_shmvalue(void) {
return global_shmaddr->total;
}

int lock_accept_mutex(void) {

int ret = cmpxchg(&global_shmaddr->accept_lock, (unsigned long)0, (unsigned long)1, 4);

return ret; //success 0, failed 1.
}

int unlock_accept_mutex(void) {

int ret = cmpxchg(&global_shmaddr->accept_lock, (unsigned long)1, (unsigned long)0, 4);

return ret; //unlock 1, lock 0
}


int init_shmvalue(void) {

Expand Down

0 comments on commit 4c3c59c

Please sign in to comment.