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

working server

parent 986c04ec
Branches
No related tags found
No related merge requests found
/.vscode client.c
client test/*
server \ No newline at end of file
debug
\ No newline at end of file
{
"configurations": [
{
"name": "Mac",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
]
},
{
"name": "Linux",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Win32",
"includePath": [
"${workspaceFolder}",
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/include/*",
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/atlmfc/include/*",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/ucrt",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/winrt"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceFolder}",
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/include/*",
"C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.12.25827/atlmfc/include/*",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/um",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/ucrt",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/shared",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.16299.0/winrt"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 3
}
\ No newline at end of file
{
"C_Cpp.errorSquiggles": "Enabled",
"C_Cpp.intelliSenseEngineFallback": "Enabled",
"files.associations": {
"iostream": "c",
"get.h": "c",
"system_error": "c",
"ostream": "c"
}
}
\ No newline at end of file
...@@ -67,7 +67,7 @@ int main(int argc, char* argv[]) ...@@ -67,7 +67,7 @@ int main(int argc, char* argv[])
if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
{ {
perror("ERROR connecting"); perror("ERROR connecting-");
exit(0); exit(0);
} }
......
...@@ -2,38 +2,40 @@ ...@@ -2,38 +2,40 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define FOUND 0
#define NOT_FOUND -1
void valid_get(char* buffer) { // checks if the request is valid void get(char* req, char* root, FILE** content) {
// const char* path_start = strchr(buffer, ' '); // Extract just the file path from the request message into the char array 'path'.
// const char* path_end = strrchr(req, ' '); const char* path_start = strstr(req, "GET") + 4;
const char* path_end = strstr(req, "HTTP")-1;
// printf("gucci\n");
}
void parse(char* req, char* root) {
// Extract just the file path from the request
// message into the char array 'path'.
const char* path_start = strchr(req, ' ') + 1;
const char* path_end = strrchr(req, ' ');
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
// printf("root %s\n", root);
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);
find_file(full_path); printf("%s\n _____ \n", full_path);
if (find_file(full_path) == FOUND) open_file(content, full_path);
else *content = NULL;
} }
void find_file(char* path_to_file) { int find_file(char* path_to_file) {
FILE *fp; FILE *fp;
fp = fopen(path_to_file, "r"); fp = fopen(path_to_file, "r");
if (fp != NULL) { if (fp != NULL) {
printf("success...\n");
fclose(fp); fclose(fp);
printf("FOUND\n");
return FOUND;
} else { } else {
printf("fail\n"); printf("NOT FOUND\n");
return NOT_FOUND;
}
} }
void open_file(FILE** file, char* path_to_file) {
*file = fopen(path_to_file, "r");
} }
\ No newline at end of file
...@@ -2,9 +2,11 @@ ...@@ -2,9 +2,11 @@
#ifndef GET_H_INCLUDED #ifndef GET_H_INCLUDED
#define GET_H_INCLUDED #define GET_H_INCLUDED
#include <stdio.h>
/* Prototypes for the functions */ /* Prototypes for the functions */
void valid_get(char* buffer); void get(char* req, char* root, FILE** content); // means everything incl header
void parse(char* req, char* root); int find_file(char* path_to_file);
void find_file(char* path_to_file); void open_file(FILE** file, char* path_to_file);
#endif #endif
\ No newline at end of file
Line 1
Line 2
ok
.
\ No newline at end of file
server: server.c server: server.c get.c
gcc -o server server.c get.c gcc -o server server.c get.c
client: client.c client: client.c
......
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
* 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 NOT_FOUND_RESPONSE "HTTP/1.0 404 File Not Found noob feeder enjoy 4x report\r\n"
#define BUF 100000
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
...@@ -9,13 +13,14 @@ ...@@ -9,13 +13,14 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <unistd.h> #include <unistd.h>
#include <sys/sendfile.h>
#include "get.h" #include "get.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int sockfd, newsockfd, portno; int sockfd, newsockfd, portno;
char buffer[256]; 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;
...@@ -65,10 +70,10 @@ int main(int argc, char *argv[]) ...@@ -65,10 +70,10 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
bzero(buffer,256); bzero(buffer,BUF);
/* Read characters from the connection, /* Read characters from the connection,
then process */ then process */
n = read(newsockfd,buffer,255); n = read(newsockfd,buffer,BUF-1);
if (n < 0) { if (n < 0) {
perror("ERROR reading from socket"); perror("ERROR reading from socket");
...@@ -77,11 +82,36 @@ int main(int argc, char *argv[]) ...@@ -77,11 +82,36 @@ int main(int argc, char *argv[])
/* using the get.c functionality here: */ /* using the get.c functionality here: */
char* root = argv[2]; char* root = argv[2];
parse(buffer, root); FILE* content = NULL;
ssize_t read;
get(buffer, root, &content);
if (content) {
// n = write(newsockfd, FOUND_RESPONSE, strlen(FOUND_RESPONSE));
// if (n < 0) {
// perror("ERROR writing to socket");
// exit(1);
// }
write(newsockfd, content, sizeof(content));
// sendfile(newsockfd, fileno(content), NULL, 5000);
// close(fileno(content));
// char* line = NULL;
// size_t len = 0;
// while ((read = getline(&line, &len, content)) != -1) {
// printf("%s", line);
// n = write(newsockfd,"I got your message",18);
// }
// n = write(newsockfd,"I got your mess3434age",18);
} else {
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); printf("Here is the message: %s\n",buffer);
n = write(newsockfd,"I got your message",18); // n = write(newsockfd,"I got your message",18);
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