Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
comp2023-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
Saleh Ahmed Khan
comp2023-2018-project-1
Commits
d6e59d18
Commit
d6e59d18
authored
Apr 18, 2018
by
Saleh Ahmed Khan
Browse files
Options
Downloads
Patches
Plain Diff
Code cleaned up, better separation of functions
parent
9d282279
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
.vscode/settings.json
+6
-1
6 additions, 1 deletion
.vscode/settings.json
get.c
+67
-19
67 additions, 19 deletions
get.c
get.h
+7
-3
7 additions, 3 deletions
get.h
server.c
+10
-33
10 additions, 33 deletions
server.c
with
91 additions
and
56 deletions
.gitignore
+
1
−
0
View file @
d6e59d18
...
@@ -3,3 +3,4 @@ test/*
...
@@ -3,3 +3,4 @@ test/*
.vscode/c_cpp_properties.json
.vscode/c_cpp_properties.json
.vscode/settings.json
.vscode/settings.json
home/file.txt
home/file.txt
server
This diff is collapsed.
Click to expand it.
.vscode/settings.json
+
6
−
1
View file @
d6e59d18
...
@@ -5,6 +5,11 @@
...
@@ -5,6 +5,11 @@
"iostream"
:
"c"
,
"iostream"
:
"c"
,
"get.h"
:
"c"
,
"get.h"
:
"c"
,
"system_error"
:
"c"
,
"system_error"
:
"c"
,
"ostream"
:
"c"
"ostream"
:
"c"
,
"initializer_list"
:
"c"
,
"type_traits"
:
"c"
,
"xstring"
:
"c"
,
"xutility"
:
"c"
,
"stdio.h"
:
"c"
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
get.c
+
67
−
19
View file @
d6e59d18
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n"
/* add \n*/
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#include
"get.h"
#include
"get.h"
#include
<stdio.h>
#include
<stdio.h>
#include
<string.h>
#include
<string.h>
#include
<pthread.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<unistd.h>
#include
<sys/sendfile.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<fcntl.h>
#define FOUND
0
#define FOUND
1
#define NOT_FOUND
-1
#define NOT_FOUND
0
void
get
(
char
*
req
,
char
*
root
,
FILE
**
content
)
{
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
)
{
// Extract just the file path from the request message into the char array 'path'.
const
char
*
path_start
=
strstr
(
buffer
,
"GET"
)
+
4
;
const
char
*
path_start
=
strstr
(
req
,
"GET"
)
+
4
;
const
char
*
path_end
=
strstr
(
buffer
,
"HTTP"
)
-
1
;
const
char
*
path_end
=
strstr
(
req
,
"HTTP"
)
-
1
;
char
path
[
path_end
-
path_start
];
char
path
[
path_end
-
path_start
];
// int l = (path_end-path_start);
// printf("%c", l);
strncpy
(
path
,
path_start
,
path_end
-
path_start
);
strncpy
(
path
,
path_start
,
path_end
-
path_start
);
path
[
sizeof
(
path
)]
=
'\0'
;
// null terminator
path
[
sizeof
(
path
)]
=
'\0'
;
// null terminator
char
full_path
[
sizeof
(
path
)
+
sizeof
(
root
)];
char
full_path
[
sizeof
(
path
)
+
sizeof
(
root
)];
strcpy
(
full_path
,
root
);
strcpy
(
full_path
,
root
);
strcat
(
full_path
,
path
);
strcat
(
full_path
,
path
);
printf
(
"%s
\n
_____
\n
"
,
full_path
);
if
(
find_file
(
full_path
)
==
FOUND
)
open_file
(
content
,
full_path
);
else
*
content
=
NULL
;
/* If the file is found, repond with the 200 header and the file */
if
(
file_found
(
full_path
))
respond_OK_and_send_file
(
full_path
,
newsockfd
);
/* otherwise, respond with a 404 NOT FOUND response */
else
respond_NOTFOUND
(
newsockfd
);
}
}
int
find_file
(
char
*
path_to_file
)
{
int
file_found
(
char
*
path_to_file
)
{
FILE
*
fp
;
/* Try to open the file at given path, check
fp
=
fopen
(
path_to_file
,
"r"
);
if a non-NULL FILE pointer is returned */
FILE
*
fp
=
fopen
(
path_to_file
,
"r"
);
if
(
fp
!=
NULL
)
{
if
(
fp
!=
NULL
)
{
fclose
(
fp
);
fclose
(
fp
);
printf
(
"FOUND
\n
"
);
return
FOUND
;
return
FOUND
;
}
else
{
}
else
{
printf
(
"NOT FOUND
\n
"
);
return
NOT_FOUND
;
return
NOT_FOUND
;
}
}
}
}
void
open_file
(
FILE
**
file
,
char
*
path_to_file
)
{
void
respond_OK_and_send_file
(
char
*
full_path
,
int
newsockfd
)
{
*
file
=
fopen
(
path_to_file
,
"r"
);
/* First open the file, get the information for
the header (size, mime type) and send the file */
int
fd
=
open
(
full_path
,
O_RDONLY
);
size_t
size
=
get_size
(
fd
);
char
response
[
128
];
make_response_header
(
response
,
full_path
,
size
);
write
(
newsockfd
,
response
,
strlen
(
response
));
sendfile
(
newsockfd
,
fd
,
NULL
,
size
);
close
(
fd
);
}
void
respond_NOTFOUND
(
int
newsockfd
)
{
/* Send a 404 NOT FOUND response to the client */
write
(
newsockfd
,
NOT_FOUND_RESPONSE
,
strlen
(
NOT_FOUND_RESPONSE
));
}
size_t
get_size
(
int
fd
)
{
struct
stat
st
;
fstat
(
fd
,
&
st
);
return
st
.
st_size
;
}
void
get_mime_type
(
char
*
path
,
char
*
mime
)
{
if
(
strstr
(
path
,
".jpg"
)
!=
NULL
)
{
strcpy
(
mime
,
"image/jpeg"
);
}
else
if
(
strstr
(
path
,
".html"
)
!=
NULL
)
{
strcpy
(
mime
,
"text/html"
);
}
else
if
(
strstr
(
path
,
".css"
)
!=
NULL
)
{
strcpy
(
mime
,
"text/css"
);
}
else
if
(
strstr
(
path
,
".js"
)
!=
NULL
)
{
strcpy
(
mime
,
"text/javascript"
);
}
else
{
strcpy
(
mime
,
"unknown"
);
}
}
void
make_response_header
(
char
*
response
,
char
*
path
,
size_t
size
)
{
char
mime
[
64
];
get_mime_type
(
path
,
mime
);
sprintf
(
response
,
"%sContent-Length: %zu
\r\n
Content-Type: %s
\r\n\r\n
"
,
FOUND_RESPONSE
,
size
,
mime
);
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
get.h
+
7
−
3
View file @
d6e59d18
...
@@ -5,8 +5,12 @@
...
@@ -5,8 +5,12 @@
#include
<stdio.h>
#include
<stdio.h>
/* Prototypes for the functions */
/* Prototypes for the functions */
void
get
(
char
*
req
,
char
*
root
,
FILE
**
content
);
// means everything incl header
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
);
int
find_file
(
char
*
path_to_file
);
int
file_found
(
char
*
path_to_file
);
void
open_file
(
FILE
**
file
,
char
*
path_to_file
);
void
respond_OK_and_send_file
(
char
*
full_path
,
int
newsockfd
);
void
respond_NOTFOUND
(
int
newsockfd
);
void
get_mime_type
(
char
*
path
,
char
*
mime
);
void
make_response_header
(
char
*
response
,
char
*
full_path
,
size_t
size
);
size_t
get_size
(
int
fd
);
#endif
#endif
\ No newline at end of file
This diff is collapsed.
Click to expand it.
server.c
+
10
−
33
View file @
d6e59d18
...
@@ -4,11 +4,13 @@
...
@@ -4,11 +4,13 @@
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n"
/* add \n*/
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n"
/* add \n*/
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#define FOUND 0
#define BUF 100000
#define BUF 100000
#include
<stdio.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
<stdlib.h>
#include
<string.h>
#include
<string.h>
#include
<pthread.h>
#include
<sys/types.h>
#include
<sys/types.h>
#include
<sys/socket.h>
#include
<sys/socket.h>
#include
<netinet/in.h>
#include
<netinet/in.h>
...
@@ -27,7 +29,7 @@ int main(int argc, char *argv[])
...
@@ -27,7 +29,7 @@ int main(int argc, char *argv[])
socklen_t
clilen
;
socklen_t
clilen
;
int
n
;
int
n
;
if
(
argc
<
2
)
{
if
(
argc
<
2
)
{
// change this
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
...
@@ -41,9 +43,7 @@ int main(int argc, char *argv[])
...
@@ -41,9 +43,7 @@ int main(int argc, char *argv[])
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
bzero
((
char
*
)
&
serv_addr
,
sizeof
(
serv_addr
));
portno
=
atoi
(
argv
[
1
]);
portno
=
atoi
(
argv
[
1
]);
/* Create address we're going to listen on (given port number)
/* Create address we're going to listen on (given port number)
- converted to network byte order & any IP address for
- converted to network byte order & any IP address for
this machine */
this machine */
...
@@ -66,7 +66,6 @@ int main(int argc, char *argv[])
...
@@ -66,7 +66,6 @@ int main(int argc, char *argv[])
be accepted. Get back a new file descriptor to communicate on. */
be accepted. Get back a new file descriptor to communicate on. */
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
newsockfd
=
accept
(
sockfd
,
(
struct
sockaddr
*
)
&
cli_addr
,
&
clilen
);
&
clilen
);
if
(
newsockfd
<
0
)
{
if
(
newsockfd
<
0
)
{
perror
(
"ERROR on accept"
);
perror
(
"ERROR on accept"
);
exit
(
1
);
exit
(
1
);
...
@@ -77,38 +76,16 @@ int main(int argc, char *argv[])
...
@@ -77,38 +76,16 @@ int main(int argc, char *argv[])
then process */
then process */
n
=
read
(
newsockfd
,
buffer
,
BUF
-
1
);
n
=
read
(
newsockfd
,
buffer
,
BUF
-
1
);
/* *************** using the get.c functionality here: */
/* ******************************************************************************** */
/* ******************************************************************************** */
/* ******************************************************************************** */
char
*
root
=
argv
[
2
];
char
*
root
=
argv
[
2
];
// Extract just the file path from the request message into the char array 'path'.
// Extract just the file path from the request message into the char array 'path'.
const
char
*
path_start
=
strstr
(
buffer
,
"GET"
)
+
4
;
parse_request_and_send_response
(
root
,
buffer
,
newsockfd
);
const
char
*
path_end
=
strstr
(
buffer
,
"HTTP"
)
-
1
;
char
path
[
path_end
-
path_start
];
strncpy
(
path
,
path_start
,
path_end
-
path_start
);
path
[
sizeof
(
path
)]
=
'\0'
;
// null terminator
char
full_path
[
sizeof
(
path
)
+
sizeof
(
root
)];
strcpy
(
full_path
,
root
);
strcat
(
full_path
,
path
);
strcpy
(
full_path
,
strchr
(
strchr
(
full_path
,
'/'
)
+
1
,
'/'
)
+
1
);
printf
(
"%s
\n
(_(_(_)_)_)
\n
"
,
full_path
);
if
(
find_file
(
full_path
)
==
0
)
{
// 0 =found
int
fd
=
open
(
full_path
,
O_RDONLY
);
struct
stat
st
;
fstat
(
fd
,
&
st
);
size_t
size
=
st
.
st_size
;
char
response
[
256
];
sprintf
(
response
,
"%sContent-Length: %zu
\r\n\r\n
"
,
FOUND_RESPONSE
,
size
);
//
printf
(
"(%zu) Hellooo
\n
%s"
,
size
,
response
);
write
(
newsockfd
,
response
,
strlen
(
response
));
sendfile
(
newsockfd
,
fd
,
NULL
,
size
);
}
else
{
n
=
write
(
newsockfd
,
NOT_FOUND_RESPONSE
,
strlen
(
NOT_FOUND_RESPONSE
));
}
printf
(
"Here is the message: %s
\n
"
,
buffer
);
//
printf("Here is the message: %s\n",buffer);
printf
(
"Here is the path calculated: %s + %s = %s
\n
"
,
root
,
path
,
full_path
);
//
printf("Here is the path calculated: %s + %s = %s\n", root, path, full_path);
if
(
n
<
0
)
{
if
(
n
<
0
)
{
perror
(
"ERROR writing to socket"
);
perror
(
"ERROR writing to socket"
);
...
...
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