diff --git a/server.c b/server.c index 3a27814f083c9d0a83a1eeba5a1ad59152da0994..a499be3de8cbec772c1dc30f1b50fb34941b11ea 100644 --- a/server.c +++ b/server.c @@ -384,7 +384,7 @@ bool show_start_page(int sockfd, char* username) { // the length needs to include the html tags for the username long added_length = username_length + 9; - char added[2048]; + char added[added_length]; // create added string strncpy(added, added_prefix, added_prefix_length); diff --git a/test.c b/test.c new file mode 100644 index 0000000000000000000000000000000000000000..4db6d5995bb62803276292d4ca3db01daf394818 --- /dev/null +++ b/test.c @@ -0,0 +1,46 @@ +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 diff --git a/testresult b/testresult deleted file mode 100644 index 3445448f34a4b1b1c7d542bf114718da4cc42731..0000000000000000000000000000000000000000 --- a/testresult +++ /dev/null @@ -1,95 +0,0 @@ - -xunz4@None:~/comp30023/comp30023-2019-project-1$ ./image_tagger 172.26.37.133 8000 -image_tagger server is now running at IP: 172.26.37.133 on port 8000 -new connection from 10.9.131.233 on socket 4 -New request from 4 -GET / HTTP/1.1 -Host: 172.26.37.133:8000 -Connection: keep-alive -Purpose: prefetch -Upgrade-Insecure-Requests: 1 -User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 -Accept-Encoding: gzip, deflate -Accept-Language: zh-CN,zh;q=0.9,en;q=0.8 - - -END -new connection from 10.9.131.233 on socket 5 -socket 5 close the connection -New request from 4 -POST / HTTP/1.1 -Host: 172.26.37.133:8000 -Connection: keep-alive -Content-Length: 9 -Cache-Control: max-age=0 -Origin: http://172.26.37.133:8000 -Upgrade-Insecure-Requests: 1 -Content-Type: application/x-www-form-urlencoded -User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 -Referer: http://172.26.37.133:8000/ -Accept-Encoding: gzip, deflate -Accept-Language: zh-CN,zh;q=0.9,en;q=0.8 - -user=test -END -new connection from 10.9.131.233 on socket 5 -new connection from 10.9.131.233 on socket 6 -New request from 6 -GET / HTTP/1.1 -User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134 -Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,ja;q=0.6,en-AU;q=0.4,en;q=0.2 -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 -Upgrade-Insecure-Requests: 1 -Accept-Encoding: gzip, deflate -Host: 172.26.37.133:8000 -Connection: Keep-Alive - - -END -socket 5 close the connection -New request from 6 -POST / HTTP/1.1 -Referer: http://172.26.37.133:8000/ -Cache-Control: max-age=0 -Content-Type: application/x-www-form-urlencoded -User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134 -Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,ja;q=0.6,en-AU;q=0.4,en;q=0.2 -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 -Upgrade-Insecure-Requests: 1 -Accept-Encoding: gzip, deflate -Host: 172.26.37.133:8000 -Content-Length: 9 -Connection: Keep-Alive - -user=user -END -New request from 6 -GET /?start=Start HTTP/1.1 -Referer: http://172.26.37.133:8000/ -Cache-Control: max-age=0 -User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134 -Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,ja;q=0.6,en-AU;q=0.4,en;q=0.2 -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 -Upgrade-Insecure-Requests: 1 -Accept-Encoding: gzip, deflate -Host: 172.26.37.133:8000 -Connection: Keep-Alive - - -END -New request from 4 -GET /?start=Start HTTP/1.1 -Host: 172.26.37.133:8000 -Connection: keep-alive -Upgrade-Insecure-Requests: 1 -User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36 -Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 -Referer: http://172.26.37.133:8000/ -Accept-Encoding: gzip, deflate -Accept-Language: zh-CN,zh;q=0.9,en;q=0.8 - - -END -new connection from 10.9.131.233 on socket 5