Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
T
tdenning_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
Terence Denning
tdenning_comp30023_2019_project-1
Commits
0b7200f9
Commit
0b7200f9
authored
Apr 28, 2019
by
Terence Denning
Browse files
Options
Downloads
Patches
Plain Diff
print guesses on the webpage
parent
0a45c437
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
+84
-34
84 additions, 34 deletions
http-server.c
image_tagger
+0
-0
0 additions, 0 deletions
image_tagger
with
84 additions
and
34 deletions
http-server.c
+
84
−
34
View file @
0b7200f9
...
@@ -31,6 +31,8 @@ void set_user(int sockfd);
...
@@ -31,6 +31,8 @@ void set_user(int sockfd);
void
user_ready
(
int
sockfd
);
void
user_ready
(
int
sockfd
);
void
print_details
();
void
print_details
();
void
check_win
(
int
user
,
char
*
keyword
);
void
check_win
(
int
user
,
char
*
keyword
);
char
*
substring
(
char
*
string
,
int
position
,
int
length
);
void
insert_substring
(
char
*
a
,
char
*
b
,
int
position
);
// 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
\
...
@@ -117,8 +119,11 @@ static bool handle_http_request(int sockfd)
...
@@ -117,8 +119,11 @@ static bool handle_http_request(int sockfd)
if
(
method
==
GET
)
if
(
method
==
GET
)
{
{
// get the size of the file
struct
stat
st
;
if
(
strstr
(
buff
,
"start=Start"
)
!=
NULL
){
if
(
strstr
(
buff
,
"start=Start"
)
!=
NULL
){
if
(
strcmp
(
webpage
,
"html/2_start.html"
)
){
if
(
strcmp
(
webpage
,
"html/2_start.html"
)
){
user_ready
(
sockfd
);
user_ready
(
sockfd
);
}
}
...
@@ -131,16 +136,10 @@ static bool handle_http_request(int sockfd)
...
@@ -131,16 +136,10 @@ static bool handle_http_request(int sockfd)
gameover
=
0
;
gameover
=
0
;
}
}
webpage
=
"html/3_first_turn.html"
;
webpage
=
"html/3_first_turn.html"
;
}
else
if
(
strstr
(
buff
,
"Cookie:"
)
!=
NULL
)
{
webpage
=
"html/1_intro.html"
;
}
else
{
}
else
{
webpage
=
"html/1_intro.html"
;
webpage
=
"html/1_intro.html"
;
}
}
// get the size of the file
struct
stat
st
;
stat
(
webpage
,
&
st
);
stat
(
webpage
,
&
st
);
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
st
.
st_size
);
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
st
.
st_size
);
...
@@ -165,7 +164,6 @@ static bool handle_http_request(int sockfd)
...
@@ -165,7 +164,6 @@ static bool handle_http_request(int sockfd)
return
false
;
return
false
;
}
}
close
(
filefd
);
close
(
filefd
);
}
}
else
if
(
method
==
POST
)
else
if
(
method
==
POST
)
{
{
...
@@ -218,7 +216,6 @@ static bool handle_http_request(int sockfd)
...
@@ -218,7 +216,6 @@ static bool handle_http_request(int sockfd)
n
=
sprintf
(
buff
,
HTTP_200_FORMAT_WITH_COOKIE
,
username
,
size
);
n
=
sprintf
(
buff
,
HTTP_200_FORMAT_WITH_COOKIE
,
username
,
size
);
}
}
else
if
(
strstr
(
buff
,
"guess=Guess"
)
!=
NULL
)
{
else
if
(
strstr
(
buff
,
"guess=Guess"
)
!=
NULL
)
{
char
*
keyword
=
strstr
(
buff
,
"keyword="
)
+
8
;
char
*
keyword
=
strstr
(
buff
,
"keyword="
)
+
8
;
int
keyword_length
=
strlen
(
keyword
);
int
keyword_length
=
strlen
(
keyword
);
keyword
[
keyword_length
-
12
]
=
'\0'
;
keyword
[
keyword_length
-
12
]
=
'\0'
;
...
@@ -230,17 +227,28 @@ static bool handle_http_request(int sockfd)
...
@@ -230,17 +227,28 @@ static bool handle_http_request(int sockfd)
strcpy
(
user1_guesses
[
user1_guess_number
],
keyword
);
strcpy
(
user1_guesses
[
user1_guess_number
],
keyword
);
user1_guess_number
++
;
user1_guess_number
++
;
check_win
(
1
,
keyword
);
check_win
(
1
,
keyword
);
username_length
=
strlen
(
username
);
added_length
=
username_length
+
2
;
size
=
st
.
st_size
+
added_length
;
n
=
sprintf
(
buff
,
HTTP_200_FORMAT_WITH_COOKIE
,
username
,
size
);
}
else
if
(
gameover
==
1
){
}
else
if
(
gameover
==
1
){
webpage
=
"html/6_endgame.html"
;
webpage
=
"html/6_endgame.html"
;
}
else
{
}
else
{
webpage
=
"html/5_discarded.html"
;
webpage
=
"html/5_discarded.html"
;
}
}
}
else
if
(
sockfd
==
user2
){
}
else
if
(
sockfd
==
user2
){
if
(
user1_start
==
1
){
if
(
user1_start
==
1
){
webpage
=
"html/4_accepted.html"
;
webpage
=
"html/4_accepted.html"
;
strcpy
(
user2_guesses
[
user2_guess_number
],
keyword
);
strcpy
(
user2_guesses
[
user2_guess_number
],
keyword
);
user2_guess_number
++
;
user2_guess_number
++
;
check_win
(
2
,
keyword
);
check_win
(
2
,
keyword
);
username_length
=
strlen
(
username
);
added_length
=
username_length
+
2
;
size
=
st
.
st_size
+
added_length
;
n
=
sprintf
(
buff
,
HTTP_200_FORMAT_WITH_COOKIE
,
username
,
size
);
}
else
if
(
gameover
==
1
){
}
else
if
(
gameover
==
1
){
webpage
=
"html/6_endgame.html"
;
webpage
=
"html/6_endgame.html"
;
}
else
{
}
else
{
...
@@ -276,7 +284,25 @@ static bool handle_http_request(int sockfd)
...
@@ -276,7 +284,25 @@ static bool handle_http_request(int sockfd)
close
(
filefd
);
close
(
filefd
);
if
(
strcmp
(
webpage
,
"html/4_accepted.html"
)
==
0
){
if
((
strlen
(
username
)
>
0
)
&&
(
strstr
(
buff
,
"user="
)
!=
NULL
)
){
// move the trailing part backward
int
p1
,
p2
;
for
(
p1
=
size
-
1
,
p2
=
p1
-
added_length
;
p1
>=
size
-
25
;
--
p1
,
--
p2
)
buff
[
p1
]
=
buff
[
p2
];
++
p2
;
buff
[
p2
++
]
=
' '
;
buff
[
p2
++
]
=
' '
;
// copy the username
strncpy
(
buff
+
p2
,
username
,
username_length
);
if
(
write
(
sockfd
,
buff
,
size
)
<
0
)
{
perror
(
"write"
);
return
false
;
}
}
else
if
(
strcmp
(
webpage
,
"html/4_accepted.html"
)
==
0
){
if
(
sockfd
==
user1
){
if
(
sockfd
==
user1
){
memset
(
user1_current_guesses
,
'\0'
,
10100
);
memset
(
user1_current_guesses
,
'\0'
,
10100
);
for
(
int
i
=
0
;
i
<
100
;
i
++
){
for
(
int
i
=
0
;
i
<
100
;
i
++
){
...
@@ -285,6 +311,7 @@ static bool handle_http_request(int sockfd)
...
@@ -285,6 +311,7 @@ static bool handle_http_request(int sockfd)
strncat
(
user1_current_guesses
,
", "
,
3
);
strncat
(
user1_current_guesses
,
", "
,
3
);
}
}
}
}
insert_substring
(
buff
,
user1_current_guesses
,
587
);
}
else
if
(
sockfd
==
user2
){
}
else
if
(
sockfd
==
user2
){
memset
(
user2_current_guesses
,
'\0'
,
10100
);
memset
(
user2_current_guesses
,
'\0'
,
10100
);
...
@@ -294,30 +321,17 @@ static bool handle_http_request(int sockfd)
...
@@ -294,30 +321,17 @@ static bool handle_http_request(int sockfd)
strncat
(
user2_current_guesses
,
", "
,
3
);
strncat
(
user2_current_guesses
,
", "
,
3
);
}
}
}
}
}
insert_substring
(
buff
,
user2_current_guesses
,
587
);
printf
(
"User1 guesses: %s, User2 guesses: %s
\n
"
,
user1_current_guesses
,
user2_current_guesses
);
}
}
if
(
write
(
sockfd
,
buff
,
st
.
st_size
)
<
0
)
if
((
strlen
(
username
)
>
0
)
&&
(
strstr
(
buff
,
"user="
)
!=
NULL
)
){
// move the trailing part backward
int
p1
,
p2
;
for
(
p1
=
size
-
1
,
p2
=
p1
-
added_length
;
p1
>=
size
-
25
;
--
p1
,
--
p2
)
buff
[
p1
]
=
buff
[
p2
];
++
p2
;
// put the separator
buff
[
p2
++
]
=
' '
;
buff
[
p2
++
]
=
' '
;
// copy the username
strncpy
(
buff
+
p2
,
username
,
username_length
);
if
(
write
(
sockfd
,
buff
,
size
)
<
0
)
{
{
perror
(
"write"
);
perror
(
"write"
);
return
false
;
return
false
;
}
}
printf
(
"
\n
User1 guesses: %s
\n
User2 guesses: %s
\n
"
,
user1_current_guesses
,
user2_current_guesses
);
}
else
{
}
else
{
if
(
write
(
sockfd
,
buff
,
st
.
st_size
)
<
0
)
if
(
write
(
sockfd
,
buff
,
st
.
st_size
)
<
0
)
{
{
...
@@ -518,6 +532,42 @@ void check_win(int user, char *keyword){
...
@@ -518,6 +532,42 @@ void check_win(int user, char *keyword){
}
}
}
}
void
insert_substring
(
char
*
a
,
char
*
b
,
int
position
)
{
char
*
f
,
*
e
;
int
length
;
length
=
strlen
(
a
);
f
=
substring
(
a
,
1
,
position
-
1
);
e
=
substring
(
a
,
position
,
length
-
position
+
1
);
strcpy
(
a
,
""
);
strcat
(
a
,
f
);
free
(
f
);
strcat
(
a
,
b
);
strcat
(
a
,
e
);
free
(
e
);
}
char
*
substring
(
char
*
string
,
int
position
,
int
length
)
{
char
*
pointer
;
int
c
;
pointer
=
malloc
(
length
+
1
);
if
(
pointer
==
NULL
)
exit
(
EXIT_FAILURE
);
for
(
c
=
0
;
c
<
length
;
c
++
)
*
(
pointer
+
c
)
=
*
((
string
+
position
-
1
)
+
c
);
*
(
pointer
+
c
)
=
'\0'
;
return
pointer
;
}
void
print_details
(){
void
print_details
(){
printf
(
"User1: %d, User2: %d, User1_start: %d, User2_start: %d
\n\n
"
,
user1
,
user2
,
user1_start
,
user2_start
);
printf
(
"User1: %d, User2: %d, User1_start: %d, User2_start: %d
\n\n
"
,
user1
,
user2
,
user1_start
,
user2_start
);
}
}
This diff is collapsed.
Click to expand it.
image_tagger
+
0
−
0
View file @
0b7200f9
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