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
c426b950
Commit
c426b950
authored
Apr 19, 2018
by
Saleh Ahmed Khan
Browse files
Options
Downloads
Patches
Plain Diff
Finalised code style and comment style, conforms to standards.
parent
ef0f156c
No related branches found
No related tags found
No related merge requests found
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
.vscode/settings.json
+2
-1
2 additions, 1 deletion
.vscode/settings.json
get.c
+20
-6
20 additions, 6 deletions
get.c
get.h
+0
-8
0 additions, 8 deletions
get.h
makefile
+2
-4
2 additions, 4 deletions
makefile
server.c
+1
-0
1 addition, 0 deletions
server.c
server.h
+14
-1
14 additions, 1 deletion
server.h
with
39 additions
and
20 deletions
.vscode/settings.json
+
2
−
1
View file @
c426b950
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
"stdio.h"
:
"c"
,
"stdio.h"
:
"c"
,
"stat.h"
:
"c"
,
"stat.h"
:
"c"
,
"server.h"
:
"c"
,
"server.h"
:
"c"
,
"types.h"
:
"c"
"types.h"
:
"c"
,
"xlocale"
:
"c"
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
get.c
+
20
−
6
View file @
c426b950
...
@@ -2,7 +2,11 @@
...
@@ -2,7 +2,11 @@
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
)
{
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
)
{
/* Get the [directory/filename.extension] from
the GET request recieved in the buffer, then
combine with root (from argv[2]) to get path. */
const
char
*
path_start
=
strstr
(
buffer
,
"GET"
)
+
4
;
const
char
*
path_start
=
strstr
(
buffer
,
"GET"
)
+
4
;
const
char
*
path_end
=
strstr
(
buffer
,
"HTTP"
)
-
1
;
const
char
*
path_end
=
strstr
(
buffer
,
"HTTP"
)
-
1
;
char
path
[
path_end
-
path_start
];
char
path
[
path_end
-
path_start
];
...
@@ -12,10 +16,14 @@ void parse_request_and_send_response(char* root, char* buffer, int newsockfd) {
...
@@ -12,10 +16,14 @@ void parse_request_and_send_response(char* root, char* buffer, int newsockfd) {
strcpy
(
full_path
,
root
);
strcpy
(
full_path
,
root
);
strcat
(
full_path
,
path
);
strcat
(
full_path
,
path
);
/* If the file is found, repond with the 200 header and the file */
/* If the file is found, repond with
if
(
file_found
(
full_path
))
respond_OK_and_send_file
(
full_path
,
newsockfd
);
the 200 header and the file */
/* otherwise, respond with a 404 NOT FOUND response */
if
(
file_found
(
full_path
))
else
respond_NOTFOUND
(
newsockfd
);
respond_OK_and_send_file
(
full_path
,
newsockfd
);
/* otherwise, respond with a
404 NOT FOUND response */
else
respond_NOTFOUND
(
newsockfd
);
}
}
int
file_found
(
char
*
path_to_file
)
{
int
file_found
(
char
*
path_to_file
)
{
...
@@ -48,12 +56,14 @@ void respond_NOTFOUND(int newsockfd) {
...
@@ -48,12 +56,14 @@ void respond_NOTFOUND(int newsockfd) {
}
}
size_t
get_size
(
int
fd
)
{
size_t
get_size
(
int
fd
)
{
/* Using fstat() to get filesize */
struct
stat
st
;
struct
stat
st
;
fstat
(
fd
,
&
st
);
fstat
(
fd
,
&
st
);
return
st
.
st_size
;
return
st
.
st_size
;
}
}
void
get_mime_type
(
char
*
path
,
char
*
mime
)
{
void
get_mime_type
(
char
*
path
,
char
*
mime
)
{
/* Get mime type from extension */
if
(
strstr
(
path
,
".jpg"
)
!=
NULL
)
{
if
(
strstr
(
path
,
".jpg"
)
!=
NULL
)
{
strcpy
(
mime
,
"image/jpeg"
);
strcpy
(
mime
,
"image/jpeg"
);
}
else
if
(
strstr
(
path
,
".html"
)
!=
NULL
)
{
}
else
if
(
strstr
(
path
,
".html"
)
!=
NULL
)
{
...
@@ -68,7 +78,11 @@ void get_mime_type(char* path, char* mime) {
...
@@ -68,7 +78,11 @@ void get_mime_type(char* path, char* mime) {
}
}
void
make_response_header
(
char
*
response
,
char
*
path
,
size_t
size
)
{
void
make_response_header
(
char
*
response
,
char
*
path
,
size_t
size
)
{
/* Our server reponds with a header containing
a mime type and file size */
char
mime
[
64
];
char
mime
[
64
];
get_mime_type
(
path
,
mime
);
get_mime_type
(
path
,
mime
);
sprintf
(
response
,
"%sContent-Length: %zu
\r\n
Content-Type: %s
\r\n\r\n
"
,
FOUND_RESPONSE
,
size
,
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
+
0
−
8
View file @
c426b950
...
@@ -2,14 +2,6 @@
...
@@ -2,14 +2,6 @@
#ifndef GET_H_INCLUDED
#ifndef GET_H_INCLUDED
#define GET_H_INCLUDED
#define GET_H_INCLUDED
/* Hash defines */
#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 FOUND 1
#define NOT_FOUND 0
#define BUF 100000
#define INFINITE_LOOP 1
/* Libraries used */
/* Libraries used */
#include
"server.h"
#include
"server.h"
...
...
This diff is collapsed.
Click to expand it.
makefile
+
2
−
4
View file @
c426b950
server
:
server.c get.c
server
:
server.c get.c
gcc
-o
server server.c get.c
-lpthread
gcc
-o
server server.c get.c
debug
:
server.c get.c
debug
:
server.c get.c
gcc
-o
server server.c get.c
-lpthread
-g
gcc
-o
server server.c get.c
-g
client
:
client.c
gcc
-o
client client.c
clean
:
clean
:
rm
server
rm
server
...
...
This diff is collapsed.
Click to expand it.
server.c
+
1
−
0
View file @
c426b950
...
@@ -11,6 +11,7 @@
...
@@ -11,6 +11,7 @@
#include
"server.h"
#include
"server.h"
#include
"get.h"
int
main
(
int
argc
,
char
*
argv
[])
{
int
main
(
int
argc
,
char
*
argv
[])
{
/* Sample code starts here, not changed
/* Sample code starts here, not changed
...
...
This diff is collapsed.
Click to expand it.
server.h
+
14
−
1
View file @
c426b950
/* Include guards */
#ifndef SERVER_H_INCLUDED
#define SERVER_H_INCLUDED
/* Hash defines */
#define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n"
#define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#define FOUND 1
#define NOT_FOUND 0
#define BUF 100000
#define INFINITE_LOOP 1
/* Libraries used */
/* Libraries used */
#include
<stdio.h>
#include
<stdio.h>
...
@@ -11,3 +22,5 @@
...
@@ -11,3 +22,5 @@
#include
<netinet/in.h>
#include
<netinet/in.h>
#include
<unistd.h>
#include
<unistd.h>
#include
<sys/sendfile.h>
#include
<sys/sendfile.h>
#endif
\ No newline at end of file
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