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
1bb3e4bd
Commit
1bb3e4bd
authored
Apr 19, 2018
by
Saleh Ahmed Khan
Browse files
Options
Downloads
Patches
Plain Diff
Comments improved. Credit and introduction added. Headers and libraries organised
parent
cf3ad90a
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
.vscode/settings.json
+3
-1
3 additions, 1 deletion
.vscode/settings.json
get.c
+1
-16
1 addition, 16 deletions
get.c
get.h
+12
-2
12 additions, 2 deletions
get.h
server.c
+29
-37
29 additions, 37 deletions
server.c
with
45 additions
and
56 deletions
.vscode/settings.json
+
3
−
1
View file @
1bb3e4bd
...
@@ -11,6 +11,8 @@
...
@@ -11,6 +11,8 @@
"xstring"
:
"c"
,
"xstring"
:
"c"
,
"xutility"
:
"c"
,
"xutility"
:
"c"
,
"stdio.h"
:
"c"
,
"stdio.h"
:
"c"
,
"stat.h"
:
"c"
"stat.h"
:
"c"
,
"server.h"
:
"c"
,
"types.h"
:
"c"
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
get.c
+
1
−
16
View file @
1bb3e4bd
#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
<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 1
#define NOT_FOUND 0
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
)
{
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
)
{
const
char
*
path_start
=
strstr
(
buffer
,
"GET"
)
+
4
;
const
char
*
path_start
=
strstr
(
buffer
,
"GET"
)
+
4
;
...
...
This diff is collapsed.
Click to expand it.
get.h
+
12
−
2
View file @
1bb3e4bd
/*
the i
nclude guards */
/*
I
nclude guards */
#ifndef GET_H_INCLUDED
#ifndef GET_H_INCLUDED
#define GET_H_INCLUDED
#define GET_H_INCLUDED
#include
<stdio.h>
/* 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 */
#include
"server.h"
/* Prototypes for the functions */
/* Prototypes for the functions */
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
);
void
parse_request_and_send_response
(
char
*
root
,
char
*
buffer
,
int
newsockfd
);
...
...
This diff is collapsed.
Click to expand it.
server.c
+
29
−
37
View file @
1bb3e4bd
/*
/*
* Server program based on sample code
|**************************************************|
|*| Computer Systems (COMP20023) 2018 Semester 1 |*|
|*| Assignment 1 - Multithreaded HTTP/1.0 Server |*|
|**************************************************|
|**| Student ID: |******| Name: |*******|
|**| 798839 |******| Saleh Ahmed Khan |*******|
|**************************************************|
|* Credit: Used Sample code from LMS for sockets *|
*/
*/
#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 0
#define BUF 100000
#include
<stdio.h>
#include
"server.h"
#include
<stdlib.h>
#include
<string.h>
int
main
(
int
argc
,
char
*
argv
[])
{
#include
<pthread.h>
/* Sample code starts here, not changed
#include
<sys/types.h>
or commented as per clarification
#include
<sys/socket.h>
with the tutor via email */
#include
<netinet/in.h>
#include
<unistd.h>
#include
<sys/sendfile.h>
#include
<sys/types.h>
#include
<sys/stat.h>
#include
<fcntl.h>
#include
"get.h"
int
main
(
int
argc
,
char
*
argv
[])
{
int
sockfd
,
newsockfd
,
portno
;
int
sockfd
,
newsockfd
,
portno
;
char
buffer
[
BUF
];
char
buffer
[
BUF
];
struct
sockaddr_in
serv_addr
,
cli_addr
;
struct
sockaddr_in
serv_addr
,
cli_addr
;
socklen_t
clilen
;
socklen_t
clilen
;
int
n
;
int
n
;
if
(
argc
<
2
)
{
// change this
if
(
argc
<
2
)
{
fprintf
(
stderr
,
"ERROR, no port provided
\n
"
);
fprintf
(
stderr
,
"Usage
\"
./name <port> ./<root>
\"
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
...
@@ -61,37 +55,35 @@ int main(int argc, char *argv[])
...
@@ -61,37 +55,35 @@ int main(int argc, char *argv[])
incoming connection requests will be queued */
incoming connection requests will be queued */
listen
(
sockfd
,
5
);
listen
(
sockfd
,
5
);
clilen
=
sizeof
(
cli_addr
);
clilen
=
sizeof
(
cli_addr
);
/* End of sample code. Most of my own code starts here: */
char
*
root
=
argv
[
2
];
char
*
root
=
argv
[
2
];
while
(
1
)
{
while
(
INFINITE_LOOP
)
{
/* Accept a connection - block until a connection is ready to
/* Accept a connection - block until a connection is ready to
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"
);
// exit(1);
continue
;
continue
;
}
}
/* Multithreading using fork(), based on clarification
by Professor this is a valid alternative to pthreads.
Based on comparative testing vs pthreads this works
efficiently and makes code shorter and more readable.
*/
if
(
!
fork
())
{
if
(
!
fork
())
{
close
(
sockfd
);
close
(
sockfd
);
printf
(
"closed sockfd
\n
"
);
// bzero(buffer,BUF);
/* Read characters from the connection, then process */
/* Read characters from the connection, then process */
n
=
read
(
newsockfd
,
buffer
,
BUF
-
1
);
read
(
newsockfd
,
buffer
,
BUF
-
1
);
parse_request_and_send_response
(
root
,
buffer
,
newsockfd
);
parse_request_and_send_response
(
root
,
buffer
,
newsockfd
);
close
(
newsockfd
);
close
(
newsockfd
);
printf
(
"closed newsockfd
\n
"
);
}
}
close
(
newsockfd
);
close
(
newsockfd
);
printf
(
"closed sockfd in parent
\n
"
);
}
}
if
(
n
<
0
)
{
perror
(
"ERROR writing to socket"
);
exit
(
1
);
}
/* close socket */
/* close socket */
//
close(sockfd);
close
(
sockfd
);
return
0
;
return
0
;
}
}
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