Skip to content
Snippets Groups Projects
Commit 8c512ca2 authored by Neeserg Parajuli's avatar Neeserg Parajuli
Browse files

not working with tests but working sending and recieving using clients.

parent fc433e8c
Branches
No related tags found
No related merge requests found
......@@ -54,7 +54,6 @@ int sendresponse(int sockfd, struct GET_header* header){
fp = fopen(header->filepath, "r");
struct RESP_header resphead;
char buffer[BUFFERSIZE];
printf("%zu\n", sizeof(buffer));
memset(buffer, 0, BUFFERSIZE);
if (fp == NULL)
{
......@@ -78,8 +77,21 @@ int sendresponse(int sockfd, struct GET_header* header){
return send(sockfd, buffer, BUFFERSIZE, 0);
}
formresponse(buffer, &resphead);
fclose(fp);
return send(sockfd, buffer, BUFFERSIZE, 0);
if(send(sockfd, buffer, BUFFERSIZE, 0)<0){
return -1;
}
while(!feof(fp)){
memset(buffer, 0, BUFFERSIZE);
int n_bytes = fread(buffer, 1, BUFFERSIZE, fp);
if(n_bytes <=0){
break;
}
if(send(sockfd, buffer,n_bytes,0)<0){
return -1;
}
}
return 0;
}
fclose(fp);
......
No preview for this file type
......@@ -16,7 +16,6 @@ int main(int argc, char *argv[]){
fprintf(stderr,"ERROR, no port provided\n");//to check if there is no port provided at start
exit(1);
}
printf("%s\n", argv[2]);
strcpy(root, argv[2]);
memset(&info, 0, sizeof(info));
info.ai_family = AF_UNSPEC; ///sets it to unspecified
......@@ -93,9 +92,10 @@ void* handleclient(void * newsockfd){
char buffer[BUFFERSIZE];
bzero(buffer, BUFFERSIZE);
if ((numbytes = recv(sockfd, buffer, 255, 0)) <0)
if ((numbytes = recv(sockfd, buffer, BUFFERSIZE, 0)) <0)
{
perror("ERROR recieving");
close(sockfd);
return NULL;
}
struct GET_header header;
......@@ -105,6 +105,7 @@ void* handleclient(void * newsockfd){
if (sendresponse(sockfd, &header) <0)
{
perror("ERROR sending");
close(sockfd);
return NULL;
}
......
<html>
<body>
<p>hello</p>
</body>
</html>
\ No newline at end of file
test/directory/sample.jpg

17.4 KiB

function test(){
alert("hello world!");
}
body {
background-color: lightblue;
}
\ No newline at end of file
<html>
<body>
<p>hello</p>
</body>
</html>
\ No newline at end of file
test/sample.jpg

17.4 KiB

function test(){
alert("hello world!");
}
body {
background-color: lightblue;
}
\ No newline at end of file
ERROR sending: Connection reset by peer
ERROR sending: Connection reset by peer
ERROR sending: Connection reset by peer
ERROR sending: Connection reset by peer
ERROR sending: Connection reset by peer
ERROR sending: Connection reset by peer
ERROR sending: Connection reset by peer
ERROR sending: Connection reset by peer
#!/bin/bash
if [ "$#" -ne 2 ]; then
echo "Usage: $0 server_name port" >&2
exit 1
fi
./$1 $2 ./test &>test_log.txt &
echo "Starting server, saving log: $temp_log_file"
sleep 1s
server_pid=$!
if ps -p $server_pid > /dev/null
then
echo "Server started with PID: $server_pid"
else
echo "Error starting server, check test_log.txt"
exit 1
fi
base_url="http://127.0.0.1:$2/"
sub_url="${base_url}directory/"
web_root="./test/"
sub_root=${web_root}"directory/"
index_file="index.html"
css_file="style.css"
javascript_file="script.js"
jpeg_file="sample.jpg"
mime_html="Content-Type: text/html$"
mime_jpeg="Content-Type: image/jpeg$"
mime_css="Content-Type: text/css$"
mime_javascript="Content-Type: text/javascript$|Content-Type: application/javascript$"
do_http_get () {
test_num=$1
test_desc=$2
test_url=$3
test_file=$4
test_resp=$5
test_mime=$6
temp_file="$(mktemp /tmp/myscript.XXXXXX)"
temp_header="$(mktemp /tmp/myscript.XXXXXX)"
header_pass=false
get_pass=false
mime_pass=false
wget -q --server-response -O $temp_file $test_url 2> $temp_header
if [ "$test_resp" == "404" ];then
if grep -Eiq 'HTTP/1.0 404|HTTP/1.1 404' $temp_header
then
header_pass=true
fi
get_pass=true
mime_pass=true
else
if grep -Eiq 'HTTP/1.0 200 OK$|HTTP/1.1 200 OK$' $temp_header
then
header_pass=true
fi
if grep -Eiq "$test_mime" $temp_header
then
mime_pass=true
fi
diff $test_file $temp_file &>/dev/null
diff_res=$?
if [ $diff_res -eq 0 ];
then
get_pass=true
else
get_pass=false
diff $test_file $temp_file
fi
fi
rm -f "$temp_file"
rm -f "$temp_header"
if $header_pass && $get_pass && $mime_pass;
then
echo "Test $test_num: $test_desc: PASS"
else
echo "Test $test_num: $test_desc: FAIL: header:$header_pass, get:$get_pass, mime:$mime_pass"
fi
}
do_http_get 1 "GET HTML file in root" $base_url$index_file $web_root$index_file "200" "$mime_html"
do_http_get 2 "GET Non-existent HTML file in root" $base_url"junk.html" $web_root$index_file "404"
do_http_get 3 "GET CSS file in root" $base_url$css_file $web_root$css_file "200" "$mime_css"
do_http_get 4 "GET JavaScript file in root" $base_url$javascript_file $web_root$javascript_file "200" "$mime_javascript"
do_http_get 5 "GET JPEG file in root" $base_url$jpeg_file $web_root$jpeg_file "200" "$mime_jpeg"
do_http_get 6 "GET HTML file in directory" "$sub_url$index_file" "$sub_root$index_file" "200" "$mime_html"
do_http_get 7 "GET CSS file in directory" "$sub_url$css_file" "$sub_root$css_file" "200" "$mime_css"
do_http_get 8 "GET JavaScript file in directory" "$sub_url$javascript_file" "$sub_root$javascript_file" "200" "$mime_javascript"
do_http_get 9 "GET JPEG file in directory" "$sub_url$jpeg_file" "$sub_root$jpeg_file" "200" "$mime_jpeg"
kill $server_pid
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment