Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
chen4_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
Anqi Chen
chen4_comp30023_2019_project-1
Commits
5dda478c
Commit
5dda478c
authored
6 years ago
by
Anqi Chen
Browse files
Options
Downloads
Patches
Plain Diff
change the logic
parent
a2f6ca27
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#1779
canceled
6 years ago
Stage: build
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
h.c
+51
-66
51 additions, 66 deletions
h.c
with
51 additions
and
66 deletions
h.c
+
51
−
66
View file @
5dda478c
/*
**
code by Angela Chan
**
http-server.c
*/
#include
<errno.h>
...
...
@@ -41,7 +41,7 @@ static char const * const HTTP_404 = "HTTP/1.1 404 Not Found\r\nContent-Length:
static
int
const
HTTP_404_LENGTH
=
45
;
// 3 kind of methods
typedef
enum
{
GET
,
...
...
@@ -80,13 +80,15 @@ struct User* newUser() {
}
void
clear_user_keyword
(
struct
User
*
user
){
user
->
stage
=
0
;
user
->
isOnline
=
0
;
user
->
n_word
=
0
;
for
(
int
i
=
0
;
i
<
MAX_BUFF
;
i
++
)
{
user
->
keywords
[
i
]
=
NULL
;
}
}
// abstract the cookie from http response in buff
int
find_cookie
(
char
*
buff
)
{
char
*
cookie_id
=
strstr
(
buff
,
"cookie_id="
);
...
...
@@ -98,7 +100,7 @@ int find_cookie(char* buff)
else
{
temp
=
malloc
(
sizeof
(
char
)
*
strlen
(
cookie_id
)
+
1
);
str
n
cpy
(
temp
,
cookie_id
,
strlen
(
cookie_id
)
);
strcpy
(
temp
,
cookie_id
);
temp
+=
10
;
}
printf
(
"find_cookie in buff %s
\n
"
,
cookie_id
);
...
...
@@ -110,7 +112,7 @@ int find_cookie(char* buff)
return
atoi
(
temp
);
}
//find the current user
struct
User
*
find_curr
(
struct
User
*
user_arr
[],
char
*
buff
)
{
int
cookie_id
=
find_cookie
(
buff
);
...
...
@@ -146,7 +148,6 @@ struct User* find_curr(struct User* user_arr[], char* buff)
}
// get the other user
struct
User
*
the_other_user
(
struct
User
*
curr_user
,
struct
User
*
user_arr
[]){
for
(
int
i
=
0
;
i
<
2
;
i
++
){
if
(
user_arr
[
i
]
!=
curr_user
){
...
...
@@ -155,7 +156,17 @@ struct User* the_other_user(struct User* curr_user, struct User* user_arr[]){
}
}
// simply load the original html
bool
match
(
char
*
w1
,
char
**
w2
,
int
n_word
){
for
(
int
i
=
0
;
i
<
n_word
;
i
++
){
if
(
w2
[
i
]
==
w1
){
return
true
;
}
}
return
false
;
}
void
simple_load_html
(
char
*
html
,
int
sockfd
,
char
*
buff
,
int
n
,
struct
User
*
curr_user
){
struct
stat
st
;
stat
(
html
,
&
st
);
...
...
@@ -180,19 +191,19 @@ void simple_load_html(char* html, int sockfd, char* buff, int n, struct User* cu
close
(
filefd
);
}
// plug in
the new word
s
in
new buff to the old buff
/* adds
the new word in
the html (between the <body> */
void
plug_in_html
(
char
*
newword
,
char
*
old_buffer
,
char
*
new_buffer
){
// s
p
li
t buff accord
ing to
</body>
// sli
ce the html str
ing
in
to
2 parts
char
start
[
MAX_BUFF
];
char
*
end
=
strstr
(
old_buffer
,
"</body>"
);
//
copy and close up
//
front part
strncpy
(
start
,
old_buffer
,
strlen
(
old_buffer
)
-
strlen
(
end
));
start
[
strlen
(
start
)]
=
'\0'
;
//
merge the buffs
str
n
cpy
(
new_buffer
,
start
,
strlen
(
start
)
);
//
combine
strcpy
(
new_buffer
,
start
);
strcat
(
new_buffer
,
newword
);
strcat
(
new_buffer
,
end
);
...
...
@@ -200,7 +211,7 @@ void plug_in_html(char* newword, char* old_buffer, char* new_buffer){
}
// if the html is rearranged, load it with the new content
void
edit_load_html
(
char
*
html
,
int
sockfd
,
char
*
buff
,
char
*
newword
,
struct
User
*
curr_user
){
// get the size of the file
...
...
@@ -240,29 +251,17 @@ void edit_load_html(char* html, int sockfd, char* buff, char* newword,struct Use
free
(
new_buffer
);
}
// read the new guess word into the user keyword array
void
record_keyword
(
struct
User
*
curr_user
,
char
*
new_guess
){
for
(
int
i
=
0
;
i
<=
curr_user
->
n_word
;
i
++
){
if
(
curr_user
->
keywords
[
curr_user
->
n_word
]
==
NULL
){
curr_user
->
keywords
[
curr_user
->
n_word
]
=
malloc
((
1
+
strlen
(
new_guess
))
*
sizeof
(
char
*
));
str
n
cpy
(
curr_user
->
keywords
[
curr_user
->
n_word
],
new_guess
,
strlen
(
new_guess
)
);
strcpy
(
curr_user
->
keywords
[
curr_user
->
n_word
],
new_guess
);
curr_user
->
n_word
++
;
break
;
}
}
}
// find if word is in array, knowing the length of the array
bool
match
(
char
*
w1
,
char
*
w2
[],
int
n_word
){
for
(
int
i
=
0
;
i
<
n_word
;
i
++
){
if
(
w2
[
i
]
==
w1
){
return
true
;
}
}
return
false
;
}
static
bool
handle_http_request
(
int
sockfd
,
struct
User
*
user_arr
[])
{
// try to read the request
...
...
@@ -303,33 +302,32 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
printf
(
"buff1:
\n
"
);
printf
(
"%s
\n
"
,
buff
);
// get current user
struct
User
*
curr_user
=
find_curr
(
user_arr
,
buff
);
// sanitise the URI
while
(
*
curr
==
'.'
||
*
curr
==
'/'
)
++
curr
;
printf
(
"stage%d
\n
"
,
curr_user
->
stage
);
// assume the only valid request URI is "/" but it can be modified to accept more files
if
(
*
curr
==
' '
||
*
curr
==
'?'
)
//Stage1: when user name is not registed
if
(
curr_user
->
stage
==
0
){
// the only GET method before registing name is the intro page
if
(
method
==
GET
)
{
printf
(
"curr_user->username%s == %d
\n
"
,
curr_user
->
username
,
curr_user
->
username
==
NULL
);
simple_load_html
(
"1_intro.html"
,
sockfd
,
buff
,
n
,
curr_user
);
}
// the only POST method before registing name is the start page
else
if
(
method
==
POST
)
{
printf
(
"curr_user->username%s == %d
\n
"
,
curr_user
->
username
,
curr_user
->
username
==
NULL
);
// locate the username, it is safe to do so in this sample code, but usually the result is expected to be
// copied to another buffer using str
n
cpy or strncpy to ensure that it will not be overwritten.
// copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten.
char
*
user
=
strstr
(
buff
,
"user="
)
+
5
;
char
username
[
MAX_BUFF
];
str
n
cpy
(
username
,
"<br>"
,
strlen
(
"<br>"
)
);
strcpy
(
username
,
"<br>"
);
strcat
(
username
,
user
);
username
[
strlen
(
username
)]
=
'\0'
;
edit_load_html
(
"2_start.html"
,
sockfd
,
buff
,
username
,
curr_user
);
...
...
@@ -347,6 +345,7 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
if
(
strstr
(
buff
,
"quit=Quit"
)
!=
NULL
)
{
curr_user
->
isOnline
=
0
;
simple_load_html
(
"7_gameover.html"
,
sockfd
,
buff
,
n
,
curr_user
);
curr_user
->
stage
=
3
;
}
// start is the only GET mathod for ther stage 1
...
...
@@ -354,58 +353,47 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
{
curr_user
->
isOnline
=
1
;
simple_load_html
(
"3_first_turn.html"
,
sockfd
,
buff
,
n
,
curr_user
);
curr_user
->
stage
=
2
;
curr_user
->
isOnline
=
1
;
}
}
// Stage 2: in the process of guessing
else
if
(
curr_user
->
stage
==
2
){
// ready to guess
char
*
new_guess
=
strstr
(
buff
,
"keyword="
)
+
8
;
// if quit, offline and quit
// clear all user info
if
(
strstr
(
buff
,
"quit=Quit"
)
!=
NULL
)
{
curr_user
->
isOnline
=
0
;
simple_load_html
(
"7_gameover.html"
,
sockfd
,
buff
,
n
,
curr_user
);
clear_user_keyword
(
curr_user
);
}
else
if
(
the_other_user
(
curr_user
,
user_arr
)
->
stage
==
3
){
simple_load_html
(
"7_gameover.html"
,
sockfd
,
buff
,
n
,
curr_user
);
clear_user_keyword
(
curr_user
);
}
// other user offline, discard
if
(
the_other_user
(
curr_user
,
user_arr
)
->
isOnline
==
0
){
else
if
(
the_other_user
(
curr_user
,
user_arr
)
->
isOnline
==
0
){
simple_load_html
(
"5_discarded.html"
,
sockfd
,
buff
,
n
,
curr_user
);
clear_user_keyword
(
curr_user
);
}
// GET method can only do start in stage2
// clear the user keyword array
if
(
method
=
GET
)
{
clear_user_keyword
(
curr_user
);
simple_load_html
(
"3_first_turn.html"
,
sockfd
,
buff
,
n
,
curr_user
);
}
// two players guess match
if
(
new_guess
!=
NULL
)
{
//if match game end
else
if
(
new_guess
!=
NULL
){
if
(
match
(
new_guess
,
the_other_user
(
curr_user
,
user_arr
)
->
keywords
,
the_other_user
(
curr_user
,
user_arr
)
->
n_word
))
{
simple_load_html
(
"6_endgame.html"
,
sockfd
,
buff
,
n
,
curr_user
);
clear_user_keyword
(
curr_user
);
}
// if a new guess is take, not match, add it to array and display it
else
{
else
{
curr_user
->
stage
=
2
;
printf
(
"curr_user->n_word%d
\n
"
,
curr_user
->
n_word
);
// remember new guess
record_keyword
(
curr_user
,
new_guess
);
// display new guess
char
guess
[
MAX_BUFF
];
str
n
cpy
(
guess
,
"<br>"
,
strlen
(
"<br>"
)
);
strcpy
(
guess
,
"<br>"
);
if
(
curr_user
->
n_word
==
1
){
strcat
(
guess
,
new_guess
);
guess
[
strlen
(
new_guess
)]
=
'\0'
;
...
...
@@ -424,9 +412,6 @@ static bool handle_http_request(int sockfd, struct User* user_arr[])
}
else
// never used, just for completeness
...
...
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