Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
N
nirmalathasa_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
Abhisha Nirmalathas
nirmalathasa_comp30023_2019_project-1
Commits
b7721e47
Commit
b7721e47
authored
Apr 18, 2019
by
Abhisha Nirmalathas
Browse files
Options
Downloads
Patches
Plain Diff
refactoring code
parent
5f7132ec
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
http-server.c
+39
-36
39 additions, 36 deletions
http-server.c
http-server.o
+0
-0
0 additions, 0 deletions
http-server.o
image_tagger
+0
-0
0 additions, 0 deletions
image_tagger
user.c
+11
-0
11 additions, 0 deletions
user.c
user.h
+3
-1
3 additions, 1 deletion
user.h
user.o
+0
-0
0 additions, 0 deletions
user.o
with
53 additions
and
37 deletions
http-server.c
+
39
−
36
View file @
b7721e47
...
@@ -127,12 +127,12 @@ bool get_request(char* buff, int sockfd, char* file_name){
...
@@ -127,12 +127,12 @@ bool get_request(char* buff, int sockfd, char* file_name){
bool
post_request
(
char
*
buff
,
int
sockfd
,
char
*
file_name
){
bool
post_request
(
char
*
buff
,
int
sockfd
,
char
*
file_name
){
// 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
// copied to another buffer using strcpy 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.
printf
(
"
\n\n
1. the bufferis: %s
\n\n
"
,
buff
);
//
printf("\n\n1. the bufferis: %s\n\n", buff);
char
*
username
=
strcpy
(
buff
,
"user="
)
+
5
;
char
*
username
=
strcpy
(
buff
,
"user="
)
+
5
;
int
username_length
=
strlen
(
username
);
int
username_length
=
strlen
(
username
);
// the length needs to include the ", " before the username
// the length needs to include the ", " before the username
long
added_length
=
username_length
+
2
;
long
added_length
=
username_length
+
2
;
printf
(
"
\n\n
2. the bufferis: %s
\n\n
"
,
buff
);
//
printf("\n\n2. the bufferis: %s\n\n", buff);
// get the size of the file
// get the size of the file
struct
stat
st
;
struct
stat
st
;
stat
(
file_name
,
&
st
);
stat
(
file_name
,
&
st
);
...
@@ -140,7 +140,7 @@ bool post_request(char *buff, int sockfd, char* file_name){
...
@@ -140,7 +140,7 @@ bool post_request(char *buff, int sockfd, char* file_name){
long
size
=
st
.
st_size
+
added_length
;
long
size
=
st
.
st_size
+
added_length
;
int
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
size
);
int
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
size
);
// send the header first
// send the header first
printf
(
"
\n\n
3. the bufferis: %s
\n\n
"
,
buff
);
//
printf("\n\n3. the bufferis: %s\n\n", buff);
if
(
write
(
sockfd
,
buff
,
n
)
<
0
)
if
(
write
(
sockfd
,
buff
,
n
)
<
0
)
{
{
perror
(
"write"
);
perror
(
"write"
);
...
@@ -149,9 +149,11 @@ bool post_request(char *buff, int sockfd, char* file_name){
...
@@ -149,9 +149,11 @@ bool post_request(char *buff, int sockfd, char* file_name){
// read the content of the HTML file
// read the content of the HTML file
int
filefd
=
open
(
file_name
,
O_RDONLY
);
int
filefd
=
open
(
file_name
,
O_RDONLY
);
n
=
read
(
filefd
,
buff
,
size
);
n
=
read
(
filefd
,
buff
,
size
);
// const char needle[10] = "</html>";
const
char
needle
[
10
]
=
"</html>"
;
// buff = strstr(buff, needle)+strlen(needle);
char
*
result
=
strstr
(
buff
,
needle
)
+
strlen
(
needle
);
printf
(
"
\n\n
4. the bufferis: %s
\n\n
"
,
buff
);
int
position
=
result
-
buff
;
memcpy
(
buff
,
buff
,
position
);
// printf("\n\n4. the bufferis: %s\n\n", buff);
if
(
n
<
0
)
if
(
n
<
0
)
{
{
perror
(
"read"
);
perror
(
"read"
);
...
@@ -196,21 +198,16 @@ bool dynamic_post_request(char *buff, int sockfd, char* file_name, User *user){
...
@@ -196,21 +198,16 @@ bool dynamic_post_request(char *buff, int sockfd, char* file_name, User *user){
int
filefd
=
open
(
file_name
,
O_RDONLY
);
int
filefd
=
open
(
file_name
,
O_RDONLY
);
n
=
read
(
filefd
,
buff
,
2048
);
n
=
read
(
filefd
,
buff
,
2048
);
// printf("%s\n", buff);
// printf("%s\n", buff);
char
insert
[
60
];
//
char insert[60];
strcpy
(
insert
,
""
);
//
strcpy(insert, "");
for
(
int
i
=
0
;
i
<
user
->
n_keywords
;
i
++
){
//
for(int i=0; i< user->n_keywords; i++){
strcat
(
insert
,
user
->
keywords
[
i
]);
//
strcat(insert, user->keywords[i]);
strcat
(
insert
,
","
);
//
strcat(insert,",");
}
//
}
insert
[
strlen
(
insert
)
-
1
]
=
'\0'
;
//
insert[strlen(insert)-1] = '\0';
// printf("the html to be inserted is %s\n", insert);
// printf("the html to be inserted is %s\n", insert);
const
char
needle
[
10
]
=
"</form>"
;
char
*
ret
;
ret
=
strstr
(
buff
,
needle
)
+
strlen
(
needle
);
int
position
=
ret
-
buff
;
// printf("The substring is: %s\n", ret);
// printf("The substring is: %s\n", ret);
insert_substring
(
buff
,
insert
,
position
);
if
(
n
<
0
)
if
(
n
<
0
)
{
{
perror
(
"read"
);
perror
(
"read"
);
...
@@ -218,6 +215,12 @@ bool dynamic_post_request(char *buff, int sockfd, char* file_name, User *user){
...
@@ -218,6 +215,12 @@ bool dynamic_post_request(char *buff, int sockfd, char* file_name, User *user){
return
false
;
return
false
;
}
}
printf
(
"dynamic %s
\n
"
,
buff
);
printf
(
"dynamic %s
\n
"
,
buff
);
char
*
keywords
=
return_all_keywords
(
user
);
const
char
needle
[
10
]
=
"</html>"
;
char
*
result
=
strstr
(
buff
,
needle
)
+
strlen
(
needle
);
int
position
=
result
-
buff
;
insert_substring
(
buff
,
keywords
,
position
);
memcpy
(
buff
,
buff
,
position
);
close
(
filefd
);
close
(
filefd
);
// move the trailing part backward
// move the trailing part backward
// int p1, p2;
// int p1, p2;
...
@@ -230,6 +233,7 @@ bool dynamic_post_request(char *buff, int sockfd, char* file_name, User *user){
...
@@ -230,6 +233,7 @@ bool dynamic_post_request(char *buff, int sockfd, char* file_name, User *user){
// // copy the username
// // copy the username
strncpy
(
buff
,
username
,
username_length
);
strncpy
(
buff
,
username
,
username_length
);
if
(
write
(
sockfd
,
buff
,
size
)
<
0
)
if
(
write
(
sockfd
,
buff
,
size
)
<
0
)
free
(
keywords
);
{
{
perror
(
"write"
);
perror
(
"write"
);
return
false
;
return
false
;
...
@@ -272,23 +276,6 @@ static bool handle_http_request(int sockfd, User_list *users)
...
@@ -272,23 +276,6 @@ static bool handle_http_request(int sockfd, User_list *users)
}
}
if
(
req
->
method
==
POST
){
if
(
req
->
method
==
POST
){
if
(
strncmp
(
req
->
body
,
"keyword="
,
8
)
==
0
){
if
(
strncmp
(
req
->
body
,
"keyword="
,
8
)
==
0
){
// printf("strncmp with keywoprds is successful");
// printf("the numer of users is %d\n", users->n_users);
for
(
int
i
=
0
;
i
<
users
->
n_users
;
i
++
){
// printf("USER ID %d", users->users[i]->id);
if
(
users
->
users
[
i
]
->
status
==
READY
){
// printf("is ready\n");
}
if
(
users
->
users
[
i
]
->
status
==
WAIT
){
// printf("is wait\n");
}
if
(
users
->
users
[
i
]
->
status
==
QUIT
){
// printf("is quit\n");
}
if
(
users
->
users
[
i
]
->
status
==
COMPLETE
){
// printf("is complete\n");
}
}
if
(
player_won
(
users
)){
if
(
player_won
(
users
)){
post_request
(
buff
,
sockfd
,
"6_endgame.html"
);
post_request
(
buff
,
sockfd
,
"6_endgame.html"
);
}
}
...
@@ -345,6 +332,22 @@ static bool handle_http_request(int sockfd, User_list *users)
...
@@ -345,6 +332,22 @@ static bool handle_http_request(int sockfd, User_list *users)
// never used, just for completeness
// never used, just for completeness
fprintf
(
stderr
,
"no other methods supported"
);
fprintf
(
stderr
,
"no other methods supported"
);
}
}
printf
(
"the numer of users is %d
\n
"
,
users
->
n_users
);
for
(
int
i
=
0
;
i
<
users
->
n_users
;
i
++
){
printf
(
"USER ID %d"
,
users
->
users
[
i
]
->
id
);
if
(
users
->
users
[
i
]
->
status
==
READY
){
printf
(
"is ready
\n
"
);
}
if
(
users
->
users
[
i
]
->
status
==
WAIT
){
printf
(
"is wait
\n
"
);
}
if
(
users
->
users
[
i
]
->
status
==
QUIT
){
printf
(
"is quit
\n
"
);
}
if
(
users
->
users
[
i
]
->
status
==
COMPLETE
){
printf
(
"is complete
\n
"
);
}
}
// send 404
// send 404
else
if
(
write
(
sockfd
,
HTTP_404
,
HTTP_404_LENGTH
)
<
0
)
else
if
(
write
(
sockfd
,
HTTP_404
,
HTTP_404_LENGTH
)
<
0
)
{
{
...
...
This diff is collapsed.
Click to expand it.
http-server.o
+
0
−
0
View file @
b7721e47
No preview for this file type
This diff is collapsed.
Click to expand it.
image_tagger
+
0
−
0
View file @
b7721e47
No preview for this file type
This diff is collapsed.
Click to expand it.
user.c
+
11
−
0
View file @
b7721e47
...
@@ -155,3 +155,14 @@ User* get_current_user(User_list* users, char* keyword, int id){
...
@@ -155,3 +155,14 @@ User* get_current_user(User_list* users, char* keyword, int id){
}
}
return
user
;
return
user
;
}
}
char
*
return_all_keywords
(
User
*
user
){
char
keywords
[
60
];
for
(
int
i
=
0
;
i
<
user
->
n_keywords
;
i
++
){
strcat
(
keywords
,
user
->
keywords
[
i
]);
strcat
(
keywords
,
","
);
}
keywords
[
strlen
(
keywords
)
-
1
]
=
'\0'
;
printf
(
"**** %s
\n
"
,
keywords
);
return
keywords
;
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
user.h
+
3
−
1
View file @
b7721e47
...
@@ -51,3 +51,5 @@ bool has_match_ended(User_list* users, char* keyword, int id);
...
@@ -51,3 +51,5 @@ bool has_match_ended(User_list* users, char* keyword, int id);
bool
player_won
(
User_list
*
users
);
bool
player_won
(
User_list
*
users
);
User
*
get_current_user
(
User_list
*
users
,
char
*
keyword
,
int
id
);
User
*
get_current_user
(
User_list
*
users
,
char
*
keyword
,
int
id
);
char
*
return_all_keywords
(
User
*
user
);
\ No newline at end of file
This diff is collapsed.
Click to expand it.
user.o
+
0
−
0
View file @
b7721e47
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