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
Xun Zhang
comp30023-2019-project-1
Commits
33f46355
Commit
33f46355
authored
Apr 29, 2019
by
ChouTatsumi
Browse files
Options
Downloads
Patches
Plain Diff
start_page method complete
parent
2f6e3fe5
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
server.c
+1
-13
1 addition, 13 deletions
server.c
test.c
+0
-46
0 additions, 46 deletions
test.c
with
1 addition
and
59 deletions
server.c
+
1
−
13
View file @
33f46355
...
@@ -367,8 +367,6 @@ bool show_modified_page(int sockfd, const char* htmldir, char* added,
...
@@ -367,8 +367,6 @@ bool show_modified_page(int sockfd, const char* htmldir, char* added,
// copy the username
// copy the username
strncpy
(
buff
+
p2
,
added
,
added_length
);
strncpy
(
buff
+
p2
,
added
,
added_length
);
print_buff
(
buff
);
if
(
write
(
sockfd
,
buff
,
size
)
<
0
)
{
if
(
write
(
sockfd
,
buff
,
size
)
<
0
)
{
perror
(
"write"
);
perror
(
"write"
);
return
false
;
return
false
;
...
@@ -378,15 +376,11 @@ bool show_modified_page(int sockfd, const char* htmldir, char* added,
...
@@ -378,15 +376,11 @@ bool show_modified_page(int sockfd, const char* htmldir, char* added,
}
}
bool
show_start_page
(
int
sockfd
,
char
*
username
)
{
bool
show_start_page
(
int
sockfd
,
char
*
username
)
{
printf
(
"user=%s
\r\n
"
,
username
);
int
username_length
=
strlen
(
username
);
int
username_length
=
strlen
(
username
);
printf
(
"user_len = %d
\r\n
"
,
username_length
);
char
*
added_prefix
=
"<p>"
;
char
*
added_prefix
=
"<p>"
;
int
added_prefix_length
=
strlen
(
added_prefix
);
int
added_prefix_length
=
strlen
(
added_prefix
);
printf
(
"added_prefix_len = %d
\r\n
"
,
added_prefix_length
);
char
*
added_suffix
=
"</p>
\n\n
"
;
char
*
added_suffix
=
"</p>
\n\n
"
;
int
added_suffix_length
=
strlen
(
added_suffix
);
int
added_suffix_length
=
sizeof
(
added_suffix
);
printf
(
"added_suffix_len = %d
\r\n
"
,
added_prefix_length
);
// the length needs to include the html tags for the username
// the length needs to include the html tags for the username
int
added_length
=
username_length
+
9
;
int
added_length
=
username_length
+
9
;
...
@@ -394,15 +388,9 @@ bool show_start_page(int sockfd, char* username) {
...
@@ -394,15 +388,9 @@ bool show_start_page(int sockfd, char* username) {
// create added string
// create added string
strncpy
(
added
,
added_prefix
,
added_prefix_length
);
strncpy
(
added
,
added_prefix
,
added_prefix_length
);
printf
(
"added is now %s
\r\n
"
,
added
);
printf
(
"added_len = %ld
\r\n
"
,
strlen
(
added
));
strncpy
(
added
+
added_prefix_length
,
username
,
username_length
);
strncpy
(
added
+
added_prefix_length
,
username
,
username_length
);
printf
(
"added is now %s
\r\n
"
,
added
);
printf
(
"added_len = %ld
\r\n
"
,
strlen
(
added
));
strncpy
(
added
+
added_prefix_length
+
username_length
,
added_suffix
,
strncpy
(
added
+
added_prefix_length
+
username_length
,
added_suffix
,
added_suffix_length
);
added_suffix_length
);
printf
(
"added is now %s
\r\n
"
,
added
);
printf
(
"added_len = %ld
\r\n
"
,
strlen
(
added
));
return
show_modified_page
(
sockfd
,
START_PAGE
,
added
,
212
);
return
show_modified_page
(
sockfd
,
START_PAGE
,
added
,
212
);
}
}
...
...
This diff is collapsed.
Click to expand it.
test.c
deleted
100644 → 0
+
0
−
46
View file @
2f6e3fe5
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
// 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
+
9
;
// 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
);
// printf("let me see what in the file\n");
// move the trailing part backward
int
p1
,
p2
;
for
(
p1
=
size
-
1
,
p2
=
p1
-
added_length
;
p1
>=
size
-
212
;
--
p1
,
--
p2
)
buff
[
p1
]
=
buff
[
p2
];
// copy the username
char
*
buf
=
"<p>"
;
int
buf_length
=
strlen
(
buf
);
strncpy
(
buff
+
p2
,
buf
,
buf_length
);
strncpy
(
buff
+
p2
+
buf_length
,
username
,
username_length
);
char
*
endbuf
=
"</p >
\n\n
"
;
int
endbuf_length
=
strlen
(
endbuf
);
strncpy
(
buff
+
p2
+
buf_length
+
username_length
,
endbuf
,
endbuf_length
);
buff
[
p2
+
buf_length
+
username_length
+
endbuf_length
]
=
'<'
;
/*
for
(
int
i
\ No newline at end of file
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