diff --git a/GETheader.c b/GETheader.c index 7cf041c77e54582a701975bbba0d17d346bc1a33..a51c9eb13dfa2858edf3e50feb01375939321e44 100644 --- a/GETheader.c +++ b/GETheader.c @@ -1,26 +1,32 @@ #include "GETheader.h" +#include "server.h" +#include "RESPheader.h" #include <stdio.h> #include <string.h> -int main(int argc, char const *argv[]) +/*int main(int argc, char const *argv[]) { char getrequest[BUFFERSIZE]; + root = "/home/neeserg/Dropbox/CompSys/practice"; memset(getrequest, 0, BUFFERSIZE); fread(getrequest, 1, BUFFERSIZE, stdin); - + struct GET_header header; parse_GET_request(getrequest, &header); - printf("%s\n", header.filepath); - printf("%s\n", header.httpversion); + sendresponse(0, &header); + + + + return 0; } - +*/ int parse_GET_request(char* message, struct GET_header* header){ char workingcopy[BUFFERSIZE]; char delimeter[6] = " \r\n\t\v"; @@ -48,8 +54,9 @@ int parse_GET_request(char* message, struct GET_header* header){ header->status = FILENOTFOUND; return -1; } - - strcpy(header->filepath, temp); + memset(header->filepath, 0, PATHLEN); + strcat(header->filepath, root); + strcat(header->filepath, temp); diff --git a/GETheader.h b/GETheader.h index 4b7d51285aa5b9c46592ef2455b785f2826f7f77..d289322a2b6cfadc2260d31c24ad0c13b0bc67a0 100644 --- a/GETheader.h +++ b/GETheader.h @@ -1,15 +1,15 @@ #ifndef GETheader #define GETheader -#define BUFFERSIZE 1000 #define BADREQUEST 400 #define FILENOTFOUND 404 +#define PATHLEN 128 struct GET_header { char httpversion[10]; int status; - char filepath[128]; + char filepath[PATHLEN]; }; diff --git a/RESPheader.h b/RESPheader.h new file mode 100644 index 0000000000000000000000000000000000000000..f52b312330a854f7a1df4819228a3bda413312d0 --- /dev/null +++ b/RESPheader.h @@ -0,0 +1,20 @@ +#ifndef RESPheader +#define RESPheader + +#include "GETheader.h" + +#define CONTENTTYPESIZE 40 +struct RESP_header{ + char httpversion[10]; + int status; + char contenttype [40]; + int content_length; +}; +int sendresponse(int sockfd, struct GET_header* header); +int getfiletype(const char *filepath, struct RESP_header* resphead); + + + + + +#endif \ No newline at end of file diff --git a/form_response.c b/form_response.c new file mode 100644 index 0000000000000000000000000000000000000000..945a1f749e6a05bed9b14a9eec3405b8d2f4367e --- /dev/null +++ b/form_response.c @@ -0,0 +1,124 @@ +#include "RESPheader.h" +#include "GETheader.h" +#include "server.h" + +const char* html = "Content-Type: text/html\n\n"; +const char* htmlext = ".html"; +const char* javascript = "Content-Type: application/javascript\n\n"; +const char* jsext = ".js"; +const char* jpeg = "Content-Type: image/jpeg\n\n"; +const char* jpegext= ".jpg"; +const char* css = "Content-Type: text/css\n\n"; +const char* cssext= ".css"; + + +int formresponse(char buffer[BUFFERSIZE], struct RESP_header* header){ + strcat(buffer, header->httpversion); + strcat(buffer, " "); + + if(header->status == 200){ + char status[4]; + sprintf(status, "%d", header->status); + strcat(buffer, status); + strcat(buffer, " OK\n"); + char content_length[30]; + sprintf(content_length, "Content-Length: %d\n", header->content_length); + strcat(buffer, content_length); + strcat(buffer, header->contenttype); + return 0; + + } + + else if (header->status ==404) + { + char status[4]; + sprintf(status, "%d", header->status); + strcat(buffer, status); + strcat(buffer, " Not Found\n\n"); + return 1; + } + + else{ + char status[4]; + sprintf(status, "%d", header->status); + strcat(buffer, status); + strcat(buffer, " Error\n\n"); + } + + return 2; +} + + +int sendresponse(int sockfd, struct GET_header* header){ + FILE *fp; + fp = fopen(header->filepath, "r"); + struct RESP_header resphead; + char buffer[BUFFERSIZE]; + printf("%zu\n", sizeof(buffer)); + memset(buffer, 0, BUFFERSIZE); + if (fp == NULL) + { + strcpy(resphead.httpversion, header->httpversion); + resphead.status = 404; + formresponse(buffer, &resphead); + return send(sockfd, buffer, BUFFERSIZE, 0); + } + + else{ + resphead.status = 200, + strcpy(resphead.httpversion, header->httpversion); + + fseek(fp, 0, SEEK_END); + int size = ftell(fp); + fseek(fp, 0,SEEK_SET); + resphead.content_length = size; + if(getfiletype(header->filepath, &resphead)!=0){ + formresponse(buffer, &resphead); + fclose(fp); + return send(sockfd, buffer, BUFFERSIZE, 0); + } + formresponse(buffer, &resphead); + fclose(fp); + return send(sockfd, buffer, BUFFERSIZE, 0); + + } + fclose(fp); + return -1; +} + + +int getfiletype(const char *filepath, struct RESP_header* resphead){ + if (strstr(filepath, htmlext) != NULL) + { + strcpy(resphead->contenttype, html); + return 0; + } + else if (strstr(filepath, jsext) != NULL) + { + + strcpy(resphead->contenttype, javascript); + return 0; + } + + else if (strstr(filepath, cssext) != NULL) + { + strcpy(resphead->contenttype, css); + return 0; + } + + else if (strstr(filepath, jpegext) != NULL) + { + strcpy(resphead->contenttype, jpeg); + return 0; + } + + else{ + resphead-> status = 400; + return -1; + } +} + + + + + diff --git a/sample b/sample index 90ae967dd8c8ad29b067d9a47f9f7361e88538d0..98067b0fb98aa4bf835cb299210e69b51d864995 100755 Binary files a/sample and b/sample differ diff --git a/server.c b/server.c index 797d6a2dc4c609399ac6a8392e4cab22e1639f5a..eb181bcf41082a6d97f75cd22db2206c8d291112 100644 --- a/server.c +++ b/server.c @@ -1,13 +1,7 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <arpa/inet.h> -#include <netdb.h> -#include <netinet/in.h> -#include <unistd.h> # include <pthread.h> +#include "server.h" +#include "GETheader.h" +#include "RESPheader.h" #define MAXUSER 5 @@ -18,13 +12,10 @@ int main(int argc, char *argv[]){ int sockfd, newsockfd; struct sockaddr_storage clien_addr; socklen_t clien_size; - char ipstr[INET6_ADDRSTRLEN]; - if (argc <2){ fprintf(stderr,"ERROR, no port provided\n");//to check if there is no port provided at start exit(1); } - memset(&info, 0, sizeof(info)); info.ai_family = AF_UNSPEC; ///sets it to unspecified info.ai_socktype = SOCK_STREAM; //TCP connection stream oriented @@ -97,18 +88,19 @@ void* handleclient(void * newsockfd){ int *sockfd_ptr = (int*) newsockfd; int numbytes; int sockfd = *sockfd_ptr; - char buffer[256]; - bzero(buffer, 256); + char buffer[BUFFERSIZE]; + bzero(buffer, BUFFERSIZE); if ((numbytes = recv(sockfd, buffer, 255, 0)) <0) { perror("ERROR recieving"); return NULL; } + struct GET_header header; - printf("%s\n", buffer); + parse_GET_request(buffer, &header); - if (send(sockfd, "hello world", 13, 0) <0) + if (sendresponse(sockfd, &header) <0) { perror("ERROR sending"); return NULL; diff --git a/server.h b/server.h index decc240ebe946a64eefc218d941825dac3edeb77..1517830ef94d98320242a6cf46a248dfb6fd1a0f 100644 --- a/server.h +++ b/server.h @@ -1,6 +1,19 @@ #ifndef SERVER #define SERVER -char root[32]; + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <netdb.h> +#include <netinet/in.h> +#include <unistd.h> + +#define BUFFERSIZE 2048 + +char *root; #endif \ No newline at end of file