Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
comp30023_2019_project-1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Weijie Lin
comp30023_2019_project-1
Commits
9ec8bae9
Commit
9ec8bae9
authored
6 years ago
by
weijiel6
Browse files
Options
Downloads
Patches
Plain Diff
final submit one
parent
40353e6d
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
http-server.c
+57
-197
57 additions, 197 deletions
http-server.c
weijiel6_comp30023_2019_project-1.rar
+0
-0
0 additions, 0 deletions
weijiel6_comp30023_2019_project-1.rar
with
57 additions
and
197 deletions
http-server.c
+
57
−
197
View file @
9ec8bae9
...
@@ -25,6 +25,8 @@
...
@@ -25,6 +25,8 @@
// used to check whether the game is finished
// used to check whether the game is finished
bool
check_end
(
int
sockfd
,
char
*
word_putin
);
bool
check_end
(
int
sockfd
,
char
*
word_putin
);
bool
make_file
(
int
sockfd
,
char
*
filepath
);
// constants
// constants
static
char
const
*
const
HTTP_200_FORMAT
=
"HTTP/1.1 200 OK
\r\n
\
static
char
const
*
const
HTTP_200_FORMAT
=
"HTTP/1.1 200 OK
\r\n
\
Content-Type: text/html
\r\n
\
Content-Type: text/html
\r\n
\
...
@@ -123,30 +125,8 @@ static bool handle_http_request(int sockfd)
...
@@ -123,30 +125,8 @@ static bool handle_http_request(int sockfd)
if
(
*
curr
==
' '
)
if
(
*
curr
==
' '
)
if
(
method
==
GET
)
if
(
method
==
GET
)
{
{
// get the size of the file
// send the intro html
struct
stat
st
;
make_file
(
sockfd
,
"1_intro.html"
);
stat
(
"1_intro.html"
,
&
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
(
"1_intro.html"
,
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
)
{
{
...
@@ -161,30 +141,8 @@ static bool handle_http_request(int sockfd)
...
@@ -161,30 +141,8 @@ static bool handle_http_request(int sockfd)
player2_r
=
false
;
player2_r
=
false
;
quit_game
=
true
;
quit_game
=
true
;
}
}
// get the size of the file
// send the game over html
struct
stat
st
;
make_file
(
sockfd
,
"7_gameover.html"
);
stat
(
"7_gameover.html"
,
&
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
(
"7_gameover.html"
,
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
(
strstr
(
buff
,
"user="
)
!=
NULL
)
{
else
if
(
strstr
(
buff
,
"user="
)
!=
NULL
)
{
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be
...
@@ -249,36 +207,14 @@ static bool handle_http_request(int sockfd)
...
@@ -249,36 +207,14 @@ static bool handle_http_request(int sockfd)
else
if
(
*
curr
==
'?'
)
{
else
if
(
*
curr
==
'?'
)
{
if
(
method
==
GET
)
if
(
method
==
GET
)
{
{
// change the players state
// change the players
active
state
if
(
sockfd
==
player1_s
)
{
if
(
sockfd
==
player1_s
)
{
player1_r
=
true
;
player1_r
=
true
;
}
else
if
(
sockfd
==
player2_s
)
{
}
else
if
(
sockfd
==
player2_s
)
{
player2_r
=
true
;
player2_r
=
true
;
}
}
// get the size of the file
// send the first turn in html
struct
stat
st
;
make_file
(
sockfd
,
"3_first_turn.html"
);
stat
(
"3_first_turn.html"
,
&
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
(
"3_first_turn.html"
,
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
)
{
{
...
@@ -293,30 +229,8 @@ static bool handle_http_request(int sockfd)
...
@@ -293,30 +229,8 @@ static bool handle_http_request(int sockfd)
player2_r
=
false
;
player2_r
=
false
;
quit_game
=
true
;
quit_game
=
true
;
}
}
// get the size of the file
// send the game over html
struct
stat
st
;
make_file
(
sockfd
,
"7_gameover.html"
);
stat
(
"7_gameover.html"
,
&
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
(
"7_gameover.html"
,
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
(
strstr
(
buff
,
"guess="
)
!=
NULL
)
{
else
if
(
strstr
(
buff
,
"guess="
)
!=
NULL
)
{
...
@@ -325,30 +239,9 @@ static bool handle_http_request(int sockfd)
...
@@ -325,30 +239,9 @@ static bool handle_http_request(int sockfd)
// if the quit game flag is true would quit the game
// if the quit game flag is true would quit the game
if
(
quit_game
)
{
if
(
quit_game
)
{
// get the size of the file
struct
stat
st
;
// send the game over html
stat
(
"7_gameover.html"
,
&
st
);
make_file
(
sockfd
,
"7_gameover.html"
);
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
(
"7_gameover.html"
,
O_RDONLY
);
do
{
n
=
sendfile
(
sockfd
,
filefd
,
NULL
,
2048
);
}
while
(
n
>
0
);
if
(
n
<
0
)
{
perror
(
"sendfile"
);
close
(
filefd
);
return
false
;
}
close
(
filefd
);
}
}
// if the end game flag is ture would end the game
// if the end game flag is ture would end the game
...
@@ -362,30 +255,8 @@ static bool handle_http_request(int sockfd)
...
@@ -362,30 +255,8 @@ static bool handle_http_request(int sockfd)
player2_r
=
false
;
player2_r
=
false
;
player2_w_len
=
0
;
player2_w_len
=
0
;
}
}
// get the size of the file
// send the end game html
struct
stat
st
;
make_file
(
sockfd
,
"6_endgame.html"
);
stat
(
"6_endgame.html"
,
&
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
(
"6_endgame.html"
,
O_RDONLY
);
do
{
n
=
sendfile
(
sockfd
,
filefd
,
NULL
,
2048
);
}
while
(
n
>
0
);
if
(
n
<
0
)
{
perror
(
"sendfile"
);
close
(
filefd
);
return
false
;
}
close
(
filefd
);
}
}
// if both players are ready, would send accepted file
// if both players are ready, would send accepted file
...
@@ -404,32 +275,12 @@ static bool handle_http_request(int sockfd)
...
@@ -404,32 +275,12 @@ static bool handle_http_request(int sockfd)
player2_r
=
false
;
player2_r
=
false
;
player2_w_len
=
0
;
player2_w_len
=
0
;
}
}
// get the size of the file
// send the end game html
struct
stat
st
;
make_file
(
sockfd
,
"6_endgame.html"
);
stat
(
"6_endgame.html"
,
&
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
(
"6_endgame.html"
,
O_RDONLY
);
do
{
n
=
sendfile
(
sockfd
,
filefd
,
NULL
,
2048
);
}
while
(
n
>
0
);
if
(
n
<
0
)
{
perror
(
"sendfile"
);
close
(
filefd
);
return
false
;
}
close
(
filefd
);
}
else
{
}
else
{
// keep put ward in and guess
// keep put ward in and guess
// get the size of the file
// get the size of the file
struct
stat
st
;
struct
stat
st
;
stat
(
"4_accepted.html"
,
&
st
);
stat
(
"4_accepted.html"
,
&
st
);
...
@@ -457,9 +308,31 @@ static bool handle_http_request(int sockfd)
...
@@ -457,9 +308,31 @@ static bool handle_http_request(int sockfd)
}
}
}
else
{
}
else
{
// not both players active so send discarded file
// not both players active so send discarded file
// send the end game html
make_file
(
sockfd
,
"5_discarded.html"
);
}
}
else
// never used, just for completeness
fprintf
(
stderr
,
"no other methods supported"
);
}
}
// send 404
else
if
(
write
(
sockfd
,
HTTP_404
,
HTTP_404_LENGTH
)
<
0
)
{
perror
(
"write"
);
return
false
;
}
return
true
;
}
bool
make_file
(
int
sockfd
,
char
*
filepath
)
{
char
buff
[
2049
];
int
n
;
// get the size of the file
// get the size of the file
struct
stat
st
;
struct
stat
st
;
stat
(
"5_discarded.html"
,
&
st
);
stat
(
filepath
,
&
st
);
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
st
.
st_size
);
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
st
.
st_size
);
// send the header first
// send the header first
if
(
write
(
sockfd
,
buff
,
n
)
<
0
)
if
(
write
(
sockfd
,
buff
,
n
)
<
0
)
...
@@ -468,7 +341,7 @@ static bool handle_http_request(int sockfd)
...
@@ -468,7 +341,7 @@ static bool handle_http_request(int sockfd)
return
false
;
return
false
;
}
}
// send the file
// send the file
int
filefd
=
open
(
"5_discarded.html"
,
O_RDONLY
);
int
filefd
=
open
(
filepath
,
O_RDONLY
);
do
do
{
{
n
=
sendfile
(
sockfd
,
filefd
,
NULL
,
2048
);
n
=
sendfile
(
sockfd
,
filefd
,
NULL
,
2048
);
...
@@ -481,23 +354,10 @@ static bool handle_http_request(int sockfd)
...
@@ -481,23 +354,10 @@ static bool handle_http_request(int sockfd)
return
false
;
return
false
;
}
}
close
(
filefd
);
close
(
filefd
);
}
}
else
// never used, just for completeness
fprintf
(
stderr
,
"no other methods supported"
);
}
}
// send 404
else
if
(
write
(
sockfd
,
HTTP_404
,
HTTP_404_LENGTH
)
<
0
)
{
perror
(
"write"
);
return
false
;
}
return
true
;
return
true
;
}
}
// check the word list and change the end game flag and put the word into list
// check the word list and change the end game flag and put the word into list
bool
check_end
(
int
sockfd
,
char
*
word_putin
)
{
bool
check_end
(
int
sockfd
,
char
*
word_putin
)
{
if
(
sockfd
==
player1_s
)
{
if
(
sockfd
==
player1_s
)
{
...
...
This diff is collapsed.
Click to expand it.
weijiel6_comp30023_2019_project-1.rar
+
0
−
0
View file @
9ec8bae9
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment