Skip to content
GitLab
Explore
Sign in
Commits on Source (2)
Resepct head method
· 34e6f589
Matthew Burket
authored
Apr 11, 2018
34e6f589
Move file serving to known method
· dca912b4
Matthew Burket
authored
Apr 11, 2018
dca912b4
Show whitespace changes
Inline
Side-by-side
main.c
View file @
dca912b4
...
...
@@ -18,7 +18,7 @@
#include
"http.h"
#define PORT 2688
7
#define PORT 2688
5
#define DEBUG 0
#define TRUE 1
...
...
@@ -32,6 +32,8 @@ void getRequest(int clientfd, GrowthBuffer *request);
int
checkFile
(
char
*
fileName
);
void
serveFile
(
int
clientfd
,
const
char
*
path
);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-noreturn"
...
...
@@ -121,18 +123,9 @@ int handle_client(int clientfd) {
char
*
basicHeader
=
"HTTP/1.1 200 OK
\r\n\r\n
"
;
appendBuffer
(
&
response
,
basicHeader
,
strlen
(
basicHeader
));
write
(
clientfd
,
response
.
array
,
strlen
(
response
.
array
));
FILE
*
fp
;
size_t
numBytesReadFile
;
fp
=
fopen
(
path
,
"r"
);
char
fileBuff
[
1024
];
if
(
fp
)
{
printf
(
"
\n
Got a fp
\n
"
);
while
((
numBytesReadFile
=
fread
(
fileBuff
,
1023
,
1
,
fp
))
>
0
)
{
printf
(
"%s"
,
fileBuff
);
write
(
clientfd
,
fileBuff
,
strlen
(
fileBuff
));
if
(
verb
==
HTTP_GET
)
{
serveFile
(
clientfd
,
path
);
}
}
fclose
(
fp
);
}
else
{
char
*
basicHeader
=
"HTTP/1.1 404 Not Found
\r\n\r\n
"
;
appendBuffer
(
&
response
,
basicHeader
,
strlen
(
basicHeader
));
...
...
@@ -149,6 +142,20 @@ int handle_client(int clientfd) {
#endif
}
void
serveFile
(
int
clientfd
,
const
char
*
path
)
{
FILE
*
fp
;
size_t
numBytesReadFile
;
fp
=
fopen
(
path
,
"r"
);
char
fileBuff
[
1024
];
if
(
fp
)
{
printf
(
"
\n
Got a fp
\n
"
);
while
((
numBytesReadFile
=
fread
(
fileBuff
,
1023
,
1
,
fp
))
>
0
)
{
write
(
clientfd
,
fileBuff
,
strlen
(
fileBuff
));
}
}
fclose
(
fp
);
}
void
getRequest
(
int
clientfd
,
GrowthBuffer
*
request
)
{
struct
pollfd
pollfd
=
{
.
fd
=
clientfd
,
...
...