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

init

parents
No related branches found
No related tags found
No related merge requests found
server:
gcc server.c -o server -lpthread
clean:
rm server
server.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
char *slice(char str[], int start, int finish){
char subarray[finish - start + 1];
int i;
for(i = start; i < finish; i ++){
subarray[i] = str[i];
}
return subarray;
}
char *slice_str(const char * str, size_t start, size_t last) {
size_t j = 0;
char buffer[strlen(str)];
for ( size_t i = start; i <= last; ++i ) {
buffer[j++] = str[i];
}
buffer[j] = 0;
return buffer;
}
char *get_mime(char path[]){
int end_slice = strlen(path);
if(strcmp(slice_str(path, end_slice-strlen("js"), end_slice), "js") == 0){
return "application/javascript";
}
else if(strcmp(slice_str(path, end_slice-strlen("html"), end_slice), "html") == 0){
return "text/html";
}else
if(strcmp(slice_str(path, end_slice-strlen("css"), end_slice), "css") == 0){
return "text/css";
}else
if(strcmp(slice_str(path, end_slice-strlen("jpg"), end_slice), "jpg") == 0 ){
return "image/jpeg";
}
else{
return "N/A";
}
}
// returns the address requested
char *get_path(char header[]){
int i, start_found = 0, point_in_string = 0;
char path[strlen(header)];
for (i = 0; i < 20; i++){
int h = header[i];
//find the start of the path
if(h == *"/" && start_found == 0){
start_found = 1;
printf("%c", header[i]);
}else if(start_found && h == *" "){
printf("\n");
break;
}else if( start_found){
path[point_in_string] = header[i];
point_in_string ++;
printf("%c", header[i]);
}
}
return slice(path, 0, point_in_string);
}
void *connection_handler(void *sock){
}
int main(int argc, char **argv)
{
int sockfd, newsockfd, portno;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
// create a TCP socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
// create address to connect
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno); // store in machine-neutral format
/* Bind address to the socket */
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
/* Listen on socket - means we're ready to accept connections -
incoming connection requests will be queued */
listen(sockfd,5);
clilen = sizeof(cli_addr);
/* Accept a connection - block until a connection is ready to
be accepted. Get back a new file descriptor to communicate on. */
newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
bzero(buffer, 256);
/* Read characters from the connection,
then process */
n = read(newsockfd,buffer,255);
if (n < 0) {
perror("ERROR reading from socket");
exit(1);
}
char found_header[] = "HTTP/1.1 200 OK";
char unfound_header[] = "HTTP/1.1 404 Not Found";
char path[80];
strcpy(path, argv[2]);
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 = fopen(path, "r");
char msg[1024];
if(!file){
printf("the file is not there");
strncpy(msg, unfound_header, 1024);
} else{
strncpy(msg, found_header, 1024);
char *mime;
mime = get_mime(path);
strcat(msg, "\nContent-Type: ");
strcat(msg, mime);
strcat(msg, "\r\r\n\n");
char line[100];
while(fgets(line, 1024, file)){
strcat(msg, line);
}
printf("we got the file: %s\n", msg);
}
n = write(newsockfd,msg,strlen(msg));
if (n < 0) {
perror("ERROR writing to socket");
exit(1);
}
/* close socket */
close(sockfd);
return 0;
}
\ 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