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
de890bf2
Commit
de890bf2
authored
Apr 11, 2019
by
Abhisha Nirmalathas
Browse files
Options
Downloads
Plain Diff
merge c onflict
parents
ee84e6f2
52b8d9a2
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
http-server.c
+62
-49
62 additions, 49 deletions
http-server.c
with
62 additions
and
49 deletions
http-server.c
+
62
−
49
View file @
de890bf2
...
...
@@ -65,6 +65,54 @@ bool get_request(char* buff, int sockfd, char* file_name){
return
true
;
}
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
// copied to another buffer using strcpy or strncpy to ensure that it will not be overwritten.
char
*
username
=
strstr
(
buff
,
"user="
)
+
5
;
int
username_length
=
strlen
(
username
);
// the length needs to include the ", " before the username
long
added_length
=
username_length
+
2
;
// get the size of the file
struct
stat
st
;
stat
(
file_name
,
&
st
);
// increase file size to accommodate the username
long
size
=
st
.
st_size
+
added_length
;
int
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
size
);
// send the header first
if
(
write
(
sockfd
,
buff
,
n
)
<
0
)
{
perror
(
"write"
);
return
false
;
}
// read the content of the HTML file
int
filefd
=
open
(
file_name
,
O_RDONLY
);
n
=
read
(
filefd
,
buff
,
2048
);
if
(
n
<
0
)
{
perror
(
"read"
);
close
(
filefd
);
return
false
;
}
close
(
filefd
);
// 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"
);
return
false
;
}
return
true
;
}
static
bool
handle_http_request
(
int
sockfd
)
{
// try to read the request
...
...
@@ -105,8 +153,16 @@ static bool handle_http_request(int sockfd)
// sanitise the URI
while
(
*
curr
==
'.'
||
*
curr
==
'/'
)
++
curr
;
printf
(
"**************THE CURR IS %s
\n\n\n
"
,
curr
);
if
(
strncmp
(
curr
,
"?start=Start"
,
12
)
==
0
){
printf
(
"matches start"
);
if
(
method
==
GET
){
get_request
(
buff
,
sockfd
,
"3_first_turn.html"
);
}
}
// assume the only valid request URI is "/" but it can be modified to accept more files
if
(
*
curr
==
' '
)
else
if
(
*
curr
==
' '
)
if
(
method
==
GET
)
{
if
(
strncmp
(
curr
,
"start=Start"
,
11
)
==
0
)
...
...
@@ -118,50 +174,7 @@ static bool handle_http_request(int sockfd)
}
else
if
(
method
==
POST
)
{
// 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.
char
*
username
=
strstr
(
buff
,
"user="
)
+
5
;
int
username_length
=
strlen
(
username
);
// the length needs to include the ", " before the username
long
added_length
=
username_length
+
2
;
// get the size of the file
struct
stat
st
;
stat
(
"2_start.html"
,
&
st
);
// increase file size to accommodate the username
long
size
=
st
.
st_size
+
added_length
;
n
=
sprintf
(
buff
,
HTTP_200_FORMAT
,
size
);
// send the header first
if
(
write
(
sockfd
,
buff
,
n
)
<
0
)
{
perror
(
"write"
);
return
false
;
}
// read the content of the HTML file
int
filefd
=
open
(
"2_start.html"
,
O_RDONLY
);
n
=
read
(
filefd
,
buff
,
2048
);
if
(
n
<
0
)
{
perror
(
"read"
);
close
(
filefd
);
return
false
;
}
close
(
filefd
);
// 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"
);
return
false
;
}
post_request
(
buff
,
sockfd
,
"2_start.html"
);
}
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