Skip to content
Snippets Groups Projects
Commit 1d06a74c authored by ChouTatsumi's avatar ChouTatsumi
Browse files

test for intro page method

parent 6826ad96
No related branches found
No related tags found
No related merge requests found
...@@ -48,9 +48,10 @@ static char const* const GAMEOVER_PAGE = "./views/7_gameover.html"; ...@@ -48,9 +48,10 @@ static char const* const GAMEOVER_PAGE = "./views/7_gameover.html";
typedef enum { GET, POST, UNKNOWN } METHOD; typedef enum { GET, POST, UNKNOWN } METHOD;
// methods reference // methods reference
static bool handle_http_request(int sockfd);
void run_server(const char* ip, const int port); void run_server(const char* ip, const int port);
int create_server_socket(const char* ip, const int port); int create_server_socket(const char* ip, const int port);
static bool handle_http_request(int sockfd);
void show_intro_page(int sockfd);
int main(int argc, char** argv) { int main(int argc, char** argv) {
int port; int port;
...@@ -207,26 +208,7 @@ static bool handle_http_request(int sockfd) { ...@@ -207,26 +208,7 @@ static bool handle_http_request(int sockfd) {
// contents of each pages // contents of each pages
if (*curr == ' ') { if (*curr == ' ') {
if (method == GET) { if (method == GET) {
// get the size of the file show_intro_page(sockfd);
struct stat st;
stat(INTRO_PAGE, &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0) {
perror("write");
return false;
}
// send the file
int filefd = open(INTRO_PAGE, O_RDONLY);
do {
n = sendfile(sockfd, filefd, NULL, 2048);
} while (n > 0);
if (n < 0) {
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
} else if (method == POST) { } else if (method == POST) {
char* username = strstr(buff, "user=") + 5; char* username = strstr(buff, "user=") + 5;
int username_length = strlen(username); int username_length = strlen(username);
...@@ -356,3 +338,29 @@ static bool handle_http_request(int sockfd) { ...@@ -356,3 +338,29 @@ static bool handle_http_request(int sockfd) {
return true; return true;
} }
void show_intro_page(int sockfd) {
char buff[2049];
int n;
// get the size of the file
struct stat st;
stat(INTRO_PAGE, &st);
n = sprintf(buff, HTTP_200_FORMAT, st.st_size);
// send the header first
if (write(sockfd, buff, n) < 0) {
perror("write");
return false;
}
// send the file
int filefd = open(INTRO_PAGE, O_RDONLY);
do {
n = sendfile(sockfd, filefd, NULL, 2048);
} while (n > 0);
if (n < 0) {
perror("sendfile");
close(filefd);
return false;
}
close(filefd);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment