Skip to content
Snippets Groups Projects
Commit 9d282279 authored by Saleh Ahmed Khan's avatar Saleh Ahmed Khan
Browse files

Working server that sends http header properly too

parent 4ffe448f
No related merge requests found
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
* Server program based on sample code * Server program based on sample code
*/ */
#define FOUND_RESPONSE "HTTP/1.0 200 OK\n noicee\r\n" #define FOUND_RESPONSE "HTTP/1.0 200 OK\r\n" /* add \n*/
#define NOT_FOUND_RESPONSE "HTTP/1.0 404 File Not Found noob feeder enjoy 4x report\r\n" #define NOT_FOUND_RESPONSE "HTTP/1.0 404\r\n\r\n"
#define BUF 100000 #define BUF 100000
#include <stdio.h> #include <stdio.h>
...@@ -14,7 +14,9 @@ ...@@ -14,7 +14,9 @@
#include <netinet/in.h> #include <netinet/in.h>
#include <unistd.h> #include <unistd.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "get.h" #include "get.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
...@@ -75,43 +77,38 @@ int main(int argc, char *argv[]) ...@@ -75,43 +77,38 @@ int main(int argc, char *argv[])
then process */ then process */
n = read(newsockfd,buffer,BUF-1); n = read(newsockfd,buffer,BUF-1);
if (n < 0) { /* *************** using the get.c functionality here: */
perror("ERROR reading from socket");
exit(1);
}
/* using the get.c functionality here: */
char* root = argv[2]; char* root = argv[2];
FILE* content = NULL; // Extract just the file path from the request message into the char array 'path'.
ssize_t read; const char* path_start = strstr(buffer, "GET") + 4;
get(buffer, root, &content); const char* path_end = strstr(buffer, "HTTP")-1;
if (content) { char path[path_end - path_start];
// n = write(newsockfd, FOUND_RESPONSE, strlen(FOUND_RESPONSE)); strncpy(path, path_start, path_end - path_start);
// if (n < 0) { path[sizeof(path)] = '\0'; // null terminator
// perror("ERROR writing to socket"); char full_path[sizeof(path) + sizeof(root)];
// exit(1); strcpy(full_path, root);
// } strcat(full_path, path);
write(newsockfd, content, sizeof(content)); strcpy(full_path, strchr(strchr(full_path, '/')+1, '/')+1);
// sendfile(newsockfd, fileno(content), NULL, 5000); printf("%s\n (_(_(_)_)_) \n", full_path);
// close(fileno(content)); if (find_file(full_path) == 0) { // 0 =found
// char* line = NULL;
// size_t len = 0; int fd = open(full_path, O_RDONLY);
// while ((read = getline(&line, &len, content)) != -1) { struct stat st;
// printf("%s", line); fstat(fd, &st);
// n = write(newsockfd,"I got your message",18); size_t size = st.st_size;
// }
// n = write(newsockfd,"I got your mess3434age",18); 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{ } else{
n = write(newsockfd, NOT_FOUND_RESPONSE, strlen(NOT_FOUND_RESPONSE)); n = write(newsockfd, NOT_FOUND_RESPONSE, strlen(NOT_FOUND_RESPONSE));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
} }
printf("Here is the message: %s\n",buffer);
// n = write(newsockfd,"I got your message",18); printf("Here is the message: %s\n",buffer);
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");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment