Skip to content
Snippets Groups Projects
Commit 0ce76738 authored by Neesergparajuli's avatar Neesergparajuli
Browse files

headers, yet to check on necar instance

parent 6cb01363
Branches
No related tags found
No related merge requests found
#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);
......
#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];
};
......
#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
#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;
}
}
No preview for this file type
#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;
......
#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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment