Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
comp30023-2018-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
Model registry
Operate
Environments
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
Neeserg Parajuli
comp30023-2018-project-1
Commits
559c10c8
Commit
559c10c8
authored
Apr 19, 2018
by
Neeserg Parajuli
Browse files
Options
Downloads
Patches
Plain Diff
Added comments
parent
c68de2aa
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
GETheader.c
+4
-24
4 additions, 24 deletions
GETheader.c
form_response.c
+9
-7
9 additions, 7 deletions
form_response.c
server.c
+27
-13
27 additions, 13 deletions
server.c
with
40 additions
and
44 deletions
GETheader.c
+
4
−
24
View file @
559c10c8
...
@@ -6,27 +6,7 @@
...
@@ -6,27 +6,7 @@
/*int main(int argc, char const *argv[])
{
char getrequest[BUFFERSIZE];
root = "/home/neeserg/Dropbox/CompSys/practice";
memset(getrequest, 0, BUFFERSIZE);
fread(getrequest, 1, BUFFERSIZE, stdin);
struct GET_header header;
parse_GET_request(getrequest, &header);
printf("%s\n", header.filepath);
sendresponse(0, &header);
return 0;
}
*/
int
parse_GET_request
(
char
*
message
,
struct
GET_header
*
header
){
int
parse_GET_request
(
char
*
message
,
struct
GET_header
*
header
){
char
workingcopy
[
BUFFERSIZE
];
char
workingcopy
[
BUFFERSIZE
];
char
delimeter
[
6
]
=
"
\r\n\t\v
"
;
char
delimeter
[
6
]
=
"
\r\n\t\v
"
;
...
@@ -35,13 +15,13 @@ int parse_GET_request(char* message, struct GET_header* header){
...
@@ -35,13 +15,13 @@ int parse_GET_request(char* message, struct GET_header* header){
char
*
get
;
char
*
get
;
if
((
get
=
strtok
(
workingcopy
,
delimeter
))
==
NULL
){
if
((
get
=
strtok
(
workingcopy
,
delimeter
))
==
NULL
){
//there is anything
perror
(
"NOT VALID"
);
perror
(
"NOT VALID"
);
header
->
status
=
BADREQUEST
;
header
->
status
=
BADREQUEST
;
return
-
1
;
return
-
1
;
}
}
if
(
strcmp
(
get
,
"GET"
)
!=
0
)
if
(
strcmp
(
get
,
"GET"
)
!=
0
)
//check if it has a get
{
{
perror
(
"NOT VALID"
);
perror
(
"NOT VALID"
);
header
->
status
=
BADREQUEST
;
header
->
status
=
BADREQUEST
;
...
@@ -49,7 +29,7 @@ int parse_GET_request(char* message, struct GET_header* header){
...
@@ -49,7 +29,7 @@ int parse_GET_request(char* message, struct GET_header* header){
}
}
char
*
temp
;
char
*
temp
;
if
((
temp
=
strtok
(
NULL
,
delimeter
))
==
NULL
){
if
((
temp
=
strtok
(
NULL
,
delimeter
))
==
NULL
){
//checks for file path
perror
(
"NOT VALID"
);
perror
(
"NOT VALID"
);
header
->
status
=
FILENOTFOUND
;
header
->
status
=
FILENOTFOUND
;
return
-
1
;
return
-
1
;
...
@@ -57,7 +37,7 @@ int parse_GET_request(char* message, struct GET_header* header){
...
@@ -57,7 +37,7 @@ int parse_GET_request(char* message, struct GET_header* header){
memset
(
header
->
filepath
,
0
,
PATHLEN
);
memset
(
header
->
filepath
,
0
,
PATHLEN
);
strcat
(
header
->
filepath
,
root
);
strcat
(
header
->
filepath
,
root
);
strcat
(
header
->
filepath
,
temp
);
strcat
(
header
->
filepath
,
temp
);
//saves the file path
...
...
This diff is collapsed.
Click to expand it.
form_response.c
+
9
−
7
View file @
559c10c8
...
@@ -16,7 +16,7 @@ int formresponse(char buffer[BUFFERSIZE], struct RESP_header* header){
...
@@ -16,7 +16,7 @@ int formresponse(char buffer[BUFFERSIZE], struct RESP_header* header){
strcat
(
buffer
,
header
->
httpversion
);
strcat
(
buffer
,
header
->
httpversion
);
strcat
(
buffer
,
" "
);
strcat
(
buffer
,
" "
);
if
(
header
->
status
==
200
){
if
(
header
->
status
==
SUCCESS
){
//form responce header if it is valid
char
status
[
4
];
char
status
[
4
];
sprintf
(
status
,
"%d"
,
header
->
status
);
sprintf
(
status
,
"%d"
,
header
->
status
);
strcat
(
buffer
,
status
);
strcat
(
buffer
,
status
);
...
@@ -29,7 +29,7 @@ int formresponse(char buffer[BUFFERSIZE], struct RESP_header* header){
...
@@ -29,7 +29,7 @@ int formresponse(char buffer[BUFFERSIZE], struct RESP_header* header){
}
}
else
if
(
header
->
status
==
404
)
else
if
(
header
->
status
==
BADREQUEST
)
// 404 response header
{
{
char
status
[
4
];
char
status
[
4
];
sprintf
(
status
,
"%d"
,
header
->
status
);
sprintf
(
status
,
"%d"
,
header
->
status
);
...
@@ -38,7 +38,7 @@ int formresponse(char buffer[BUFFERSIZE], struct RESP_header* header){
...
@@ -38,7 +38,7 @@ int formresponse(char buffer[BUFFERSIZE], struct RESP_header* header){
return
1
;
return
1
;
}
}
else
{
else
{
// other response header
char
status
[
4
];
char
status
[
4
];
sprintf
(
status
,
"%d"
,
header
->
status
);
sprintf
(
status
,
"%d"
,
header
->
status
);
strcat
(
buffer
,
status
);
strcat
(
buffer
,
status
);
...
@@ -58,13 +58,13 @@ int sendresponse(int sockfd, struct GET_header* header){
...
@@ -58,13 +58,13 @@ int sendresponse(int sockfd, struct GET_header* header){
if
(
fp
==
NULL
)
if
(
fp
==
NULL
)
{
{
strcpy
(
resphead
.
httpversion
,
header
->
httpversion
);
strcpy
(
resphead
.
httpversion
,
header
->
httpversion
);
resphead
.
status
=
404
;
resphead
.
status
=
BADREQUEST
;
formresponse
(
buffer
,
&
resphead
);
formresponse
(
buffer
,
&
resphead
);
return
send
(
sockfd
,
buffer
,
strlen
(
buffer
),
0
);
return
send
(
sockfd
,
buffer
,
strlen
(
buffer
),
0
);
}
}
// sends the 404 response
else
{
else
{
resphead
.
status
=
200
,
resphead
.
status
=
SUCCESS
;
strcpy
(
resphead
.
httpversion
,
header
->
httpversion
);
strcpy
(
resphead
.
httpversion
,
header
->
httpversion
);
fseek
(
fp
,
0
,
SEEK_END
);
fseek
(
fp
,
0
,
SEEK_END
);
...
@@ -77,9 +77,11 @@ int sendresponse(int sockfd, struct GET_header* header){
...
@@ -77,9 +77,11 @@ int sendresponse(int sockfd, struct GET_header* header){
return
send
(
sockfd
,
buffer
,
strlen
(
buffer
),
0
);
return
send
(
sockfd
,
buffer
,
strlen
(
buffer
),
0
);
}
}
formresponse
(
buffer
,
&
resphead
);
formresponse
(
buffer
,
&
resphead
);
if
(
send
(
sockfd
,
buffer
,
strlen
(
buffer
),
0
)
<
0
){
if
(
send
(
sockfd
,
buffer
,
strlen
(
buffer
),
0
)
<
0
){
return
-
1
;
return
-
1
;
}
}
//sends the desired file
while
(
!
feof
(
fp
)){
while
(
!
feof
(
fp
)){
memset
(
buffer
,
0
,
BUFFERSIZE
);
memset
(
buffer
,
0
,
BUFFERSIZE
);
int
n_bytes
=
fread
(
buffer
,
1
,
BUFFERSIZE
,
fp
);
int
n_bytes
=
fread
(
buffer
,
1
,
BUFFERSIZE
,
fp
);
...
@@ -98,7 +100,7 @@ int sendresponse(int sockfd, struct GET_header* header){
...
@@ -98,7 +100,7 @@ int sendresponse(int sockfd, struct GET_header* header){
return
-
1
;
return
-
1
;
}
}
//determining mime type
int
getfiletype
(
const
char
*
filepath
,
struct
RESP_header
*
resphead
){
int
getfiletype
(
const
char
*
filepath
,
struct
RESP_header
*
resphead
){
if
(
strstr
(
filepath
,
htmlext
)
!=
NULL
)
if
(
strstr
(
filepath
,
htmlext
)
!=
NULL
)
{
{
...
...
This diff is collapsed.
Click to expand it.
server.c
+
27
−
13
View file @
559c10c8
...
@@ -8,26 +8,36 @@
...
@@ -8,26 +8,36 @@
void
*
handleclient
(
void
*
newsockfd
);
void
*
handleclient
(
void
*
newsockfd
);
int
main
(
int
argc
,
char
*
argv
[]){
int
main
(
int
argc
,
char
*
argv
[]){
struct
addrinfo
info
,
*
res
,
*
p
;
int
sockfd
,
newsockfd
;
struct
addrinfo
info
,
*
res
,
*
p
;
//struct to hold all the server adress info
struct
sockaddr_storage
clien_addr
;
int
sockfd
,
newsockfd
;
//socket number to hold socket ids
socklen_t
clien_size
;
struct
sockaddr_storage
clien_addr
;
//holds client adress info
socklen_t
clien_size
;
//size of clien adress
if
(
argc
<
3
){
if
(
argc
<
3
){
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
//to check if there is no port provided at start
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
//to check if there is no port provided at start
exit
(
1
);
exit
(
1
);
}
}
strcpy
(
root
,
argv
[
2
]);
strcpy
(
root
,
argv
[
2
]);
// sets root directory
memset
(
&
info
,
0
,
sizeof
(
info
));
memset
(
&
info
,
0
,
sizeof
(
info
));
info
.
ai_family
=
AF_UNSPEC
;
///sets it to unspecified
info
.
ai_family
=
AF_UNSPEC
;
///sets it to unspecified
info
.
ai_socktype
=
SOCK_STREAM
;
//TCP connection stream oriented
info
.
ai_socktype
=
SOCK_STREAM
;
//TCP connection stream oriented
info
.
ai_flags
=
AI_PASSIVE
;
//listen on on all ip interface of machine
info
.
ai_flags
=
AI_PASSIVE
;
//listen on on all ip interface of machine
info
.
ai_protocol
=
0
;
//set this to zero as default
info
.
ai_protocol
=
0
;
//set this to zero as default
if
((
getaddrinfo
(
NULL
,
argv
[
1
],
&
info
,
&
res
)
!=
0
)){
if
((
getaddrinfo
(
NULL
,
argv
[
1
],
&
info
,
&
res
)
!=
0
))
// gets a lnked list of adress infos
{
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
//to check if getaddrinfo gets any addrinfos
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
//to check if getaddrinfo gets any addrinfos
exit
(
1
);
exit
(
1
);
}
}
//loops through the linked list for a connection
for
(
p
=
res
;
p
!=
NULL
;
p
=
p
->
ai_next
)
for
(
p
=
res
;
p
!=
NULL
;
p
=
p
->
ai_next
)
{
{
if
((
sockfd
=
socket
(
p
->
ai_family
,
p
->
ai_socktype
,
p
->
ai_protocol
))
<
0
){
if
((
sockfd
=
socket
(
p
->
ai_family
,
p
->
ai_socktype
,
p
->
ai_protocol
))
<
0
){
...
@@ -44,35 +54,37 @@ int main(int argc, char *argv[]){
...
@@ -44,35 +54,37 @@ int main(int argc, char *argv[]){
break
;
break
;
}
}
if
(
p
==
NULL
)
if
(
p
==
NULL
)
//if no connection established
{
{
perror
(
"ERROR on creating and binding"
);
perror
(
"ERROR on creating and binding"
);
return
2
;
return
2
;
}
}
if
(
listen
(
sockfd
,
MAXUSER
)
<
0
){
if
(
listen
(
sockfd
,
MAXUSER
)
<
0
){
//listens on that socket
perror
(
"ERROR listening"
);
perror
(
"ERROR listening"
);
return
2
;
return
2
;
}
while
(
1
){
while
(
1
){
clien_size
=
sizeof
(
clien_addr
);
clien_size
=
sizeof
(
clien_addr
);
//accepts connections, opening a new scoket for sending and recieving
if
((
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
clien_addr
,
&
clien_size
))
<
0
){
if
((
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
clien_addr
,
&
clien_size
))
<
0
)
{
perror
(
"ERROR accepting"
);
perror
(
"ERROR accepting"
);
return
3
;
return
3
;
}
}
pthread_t
tid
;
pthread_t
tid
;
if
(
pthread_create
(
&
tid
,
NULL
,
handleclient
,
&
newsockfd
)
<
0
)
if
(
pthread_create
(
&
tid
,
NULL
,
handleclient
,
&
newsockfd
)
<
0
)
//create a worker thread
{
{
perror
(
"ERROR creating thread"
);
perror
(
"ERROR creating thread"
);
return
3
;
return
3
;
}
}
pthread_detach
(
tid
);
pthread_detach
(
tid
);
// free all the resource associated with thread
}
}
...
@@ -92,6 +104,7 @@ void* handleclient(void * newsockfd){
...
@@ -92,6 +104,7 @@ void* handleclient(void * newsockfd){
char
buffer
[
BUFFERSIZE
];
char
buffer
[
BUFFERSIZE
];
bzero
(
buffer
,
BUFFERSIZE
);
bzero
(
buffer
,
BUFFERSIZE
);
//recieve the message
if
((
numbytes
=
recv
(
sockfd
,
buffer
,
BUFFERSIZE
,
0
))
<
0
)
if
((
numbytes
=
recv
(
sockfd
,
buffer
,
BUFFERSIZE
,
0
))
<
0
)
{
{
perror
(
"ERROR recieving"
);
perror
(
"ERROR recieving"
);
...
@@ -99,9 +112,10 @@ void* handleclient(void * newsockfd){
...
@@ -99,9 +112,10 @@ void* handleclient(void * newsockfd){
return
NULL
;
return
NULL
;
}
}
struct
GET_header
header
;
struct
GET_header
header
;
//parse the message
parse_GET_request
(
buffer
,
&
header
);
parse_GET_request
(
buffer
,
&
header
);
//send the appropriate message according to get response
if
(
sendresponse
(
sockfd
,
&
header
)
<
0
)
if
(
sendresponse
(
sockfd
,
&
header
)
<
0
)
{
{
perror
(
"ERROR sending"
);
perror
(
"ERROR sending"
);
...
...
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