/* ** parser.c */ #include #include #include #include #include #include #include #include #include #include #include #define MYPORT 3499 // the port users will be connecting to #define BACKLOG 10 // how many pending connections queue will hold #define MAXLEN 1024 //upper limit of the length of the string int readline(int fd, char *buf, int maxlen); void sendSomething(int fd, char * msg); int main(void) { char input[MAXLEN]; //the line that is read from the client char * token1; //GET request char * token2; //filepath char tmpstring[MAXLEN]; //filesize int sockfd, new_fd; // listen on sock_fd, new connection on new_fd, file open on file_fd struct sockaddr_in my_addr; // my address information struct sockaddr_in their_addr; // connector's address information int sin_size; int yes=1; int n; //the amount of read characters from the client if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } my_addr.sin_family = AF_INET; // host byte order my_addr.sin_port = htons(MYPORT); // short, network byte order my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { perror("bind"); exit(1); } if (listen(sockfd, BACKLOG) == -1) { perror("listen"); exit(1); } while(1) { // main accept() loop sin_size = sizeof(struct sockaddr_in); if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { perror("accept"); continue; } printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr)); n = readline(new_fd, input, MAXLEN); //n is the amount of read characters if (n == -1) { perror("Unable to read line"); } //Check if it is a GET message token1 = strtok(input," "); if(strcmp(token1, "GET") != 0) { sendSomething(new_fd, "Bad request\n"); } else { //Retrieve the file path token2 = strtok(NULL, " "); if(token2 == NULL) { sendSomething(new_fd, "File path not specified\n"); //Check if filename is empty } sendSomething(new_fd, token2); //test printf("%s", token2); if(token2[0] == '/') //remove the initial slash memmove(token2, token2 + 1, strlen(token2)); sendSomething(new_fd, token2); //test sendSomething(new_fd, "test.html"); //test to compare //Check if file is on the server if(open(token2, O_RDONLY) < 0) //Error opening file { if(errno == EACCES) sendSomething(new_fd, "Access error\n"); else { perror("Current problem: "); sendSomething(new_fd, "Not existed\n"); } } else { FILE * requested_file = fopen(token2, "r"); if(requested_file == NULL) // { sendSomething(new_fd, "Error in fopen\n"); } else { sendSomething(new_fd, "File found\n"); //successful } fseek(requested_file, 0, SEEK_END); // move to the end of the file int end= ftell(requested_file); // get the position of the end of file int stringlen = sprintf(tmpstring, "file size: %d\n", end); send(new_fd, tmpstring, stringlen, 0); } } close(new_fd); //close connection } return 0; } //helper function for recieving text int readline(int fd, char *buf, int maxlen) { int n, rc; char c; for (n = 1; n < maxlen; n++) { if ((rc = read(fd, &c, 1)) == 1) { *buf++ = c; if (c == '\n') break; } else if (rc == 0) { if (n == 1) return 0; // EOF, no data read else break; // EOF, read some data } else return -1; // error } *buf = '\0'; // null-terminate return n; } void sendSomething(int fd, char * msg) { int length = strlen(msg); send(fd, msg, length, 0); }