Skip to content
Snippets Groups Projects
Commit ab8e23d1 authored by red_lebowski's avatar red_lebowski
Browse files

works pretty well, no threading though

parent 05ebf666
Branches
No related tags found
No related merge requests found
...@@ -66,14 +66,10 @@ char *get_path(char header[]){ ...@@ -66,14 +66,10 @@ char *get_path(char header[]){
} }
} }
printf("from inside get path: %s", path);
return slice(path, 0, point_in_string); return slice(path, 0, point_in_string);
} }
void *connection_handler(void *sock){
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int sockfd, newsockfd, portno; int sockfd, newsockfd, portno;
...@@ -101,28 +97,23 @@ int main(int argc, char **argv) ...@@ -101,28 +97,23 @@ int main(int argc, char **argv)
portno = atoi(argv[1]); portno = atoi(argv[1]);
// create address to connect // create address to connect
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno); // store in machine-neutral format serv_addr.sin_port = htons(portno);
/* Bind address to the socket */
// Bind away
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{ {
perror("ERROR on binding"); perror("ERROR on binding");
exit(1); exit(1);
} }
/* Listen on socket - means we're ready to accept connections - // listen and queue other connections
incoming connection requests will be queued */
listen(sockfd,5); listen(sockfd,5);
clilen = sizeof(cli_addr); clilen = sizeof(cli_addr);
/* Accept a connection - block until a connection is ready to // accept a connection
be accepted. Get back a new file descriptor to communicate on. */
newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen); newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) if (newsockfd < 0)
...@@ -133,8 +124,7 @@ int main(int argc, char **argv) ...@@ -133,8 +124,7 @@ int main(int argc, char **argv)
bzero(buffer, 256); bzero(buffer, 256);
/* Read characters from the connection, //read what the message asks
then process */
n = read(newsockfd,buffer,255); n = read(newsockfd,buffer,255);
if (n < 0) { if (n < 0) {
...@@ -148,18 +138,16 @@ int main(int argc, char **argv) ...@@ -148,18 +138,16 @@ int main(int argc, char **argv)
char path[80]; char path[80];
strcpy(path, argv[2]); strcpy(path, argv[2]);
strcat(path, get_path(buffer)); strcat(path, get_path(buffer));
printf("This is the path: |%s| %d\n", path, strlen(path));
printf("Here is the message: %s\n",buffer);
FILE *file; FILE *file;
file = fopen(path, "r"); file = fopen(path, "r");
char msg[1024]; char msg[1024];
if(!file){ if(!file){
printf("the file is not there"); // file not found, generate a 404 message
strncpy(msg, unfound_header, 1024); strncpy(msg, unfound_header, 1024);
} else{ } else{
// file found, generate a proper header with the info
strncpy(msg, found_header, 1024); strncpy(msg, found_header, 1024);
char *mime; char *mime;
mime = get_mime(path); mime = get_mime(path);
...@@ -170,7 +158,6 @@ int main(int argc, char **argv) ...@@ -170,7 +158,6 @@ int main(int argc, char **argv)
while(fgets(line, 1024, file)){ while(fgets(line, 1024, file)){
strcat(msg, line); strcat(msg, line);
} }
printf("we got the file: %s\n", msg);
} }
n = write(newsockfd,msg,strlen(msg)); n = write(newsockfd,msg,strlen(msg));
...@@ -179,9 +166,6 @@ int main(int argc, char **argv) ...@@ -179,9 +166,6 @@ int main(int argc, char **argv)
perror("ERROR writing to socket"); perror("ERROR writing to socket");
exit(1); exit(1);
} }
/* close socket */
close(sockfd); close(sockfd);
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment