diff --git a/server.c b/server.c index d201fc090779d4624c2be19e1fd8e5c37c006ba4..e11cc9aff70f847992ca8d70c18aa9d48acc54a1 100644 --- a/server.c +++ b/server.c @@ -367,8 +367,6 @@ bool show_modified_page(int sockfd, const char* htmldir, char* added, // copy the username strncpy(buff + p2, added, added_length); - print_buff(buff); - if (write(sockfd, buff, size) < 0) { perror("write"); return false; @@ -378,15 +376,11 @@ bool show_modified_page(int sockfd, const char* htmldir, char* added, } bool show_start_page(int sockfd, char* username) { - printf("user=%s\r\n", username); int username_length = strlen(username); - printf("user_len = %d\r\n", username_length); char* added_prefix = "<p>"; int added_prefix_length = strlen(added_prefix); - printf("added_prefix_len = %d\r\n", added_prefix_length); char* added_suffix = "</p>\n\n"; - int added_suffix_length = strlen(added_suffix); - printf("added_suffix_len = %d\r\n", added_prefix_length); + int added_suffix_length = sizeof(added_suffix); // the length needs to include the html tags for the username int added_length = username_length + 9; @@ -394,15 +388,9 @@ bool show_start_page(int sockfd, char* username) { // create added string 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); - 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, 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); } diff --git a/test.c b/test.c deleted file mode 100644 index 4db6d5995bb62803276292d4ca3db01daf394818..0000000000000000000000000000000000000000 --- a/test.c +++ /dev/null @@ -1,46 +0,0 @@ -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