Skip to content
Snippets Groups Projects
Commit 33f46355 authored by ChouTatsumi's avatar ChouTatsumi
Browse files

start_page method complete

parent 2f6e3fe5
Branches
No related tags found
No related merge requests found
...@@ -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);
} }
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment