#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <netdb.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
/*
gcc serv.c -o serv
./serv 9999
in chrome tolbar 127.0.0.1:9999
*/
/* C is not C++ use gcc*/
#define BUFSIZE 1024
void cerror(FILE *stream, char *cause, char *errno_str, char *shortmsg, char *longmsg) {
fprintf(stream, "HTTP/1.1 %s %s\nContent-type: text/html\n\n", errno_str, shortmsg);
fprintf(stream, "<html><body><h1>%s: %s</h1><p>%s: %s</p></body></html>", errno_str, shortmsg, longmsg, cause);
fflush(stream);
}
void serve_directory(FILE *stream, char *dirname) {
DIR *dir = opendir(dirname);
if (!dir) {
cerror(stream, dirname, "403", "Forbidden", "Cannot read directory");
return;
}
fprintf(stream, "HTTP/1.1 200 OK\nContent-type: text/html\n\n");
fprintf(stream, "<html><body><h1>Index of %s</h1><ul>", dirname);
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_name[0] == '.') continue;
// ZAWSZE generujemy link z wiodącym "/"
// Jeśli jesteśmy w ".", link to /nazwa. Jeśli w "./katalog", link to /katalog/nazwa
if (strcmp(dirname, ".") == 0) {
fprintf(stream, "<li><a href=\"/%s\">%s</a></li>", entry->d_name, entry->d_name);
} else {
fprintf(stream, "<li><a href=\"%s/%s\">%s</a></li>", dirname + 1, entry->d_name, entry->d_name);
}
}
fprintf(stream, "</ul></body></html>");
fflush(stream);
closedir(dir);
}
int main(int argc, char **argv) {
int parentfd, childfd, portno, optval;
socklen_t clientlen;
struct sockaddr_in serveraddr, clientaddr;
FILE *stream;
char buf[BUFSIZE], method[BUFSIZE], uri[BUFSIZE], version[BUFSIZE];
char filename[BUFSIZE], filetype[BUFSIZE];
struct stat sbuf;
if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); }
portno = atoi(argv[1]);
parentfd = socket(AF_INET, SOCK_STREAM, 0);
optval = 1;
setsockopt(parentfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int));
bzero((char *)&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
bind(parentfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
listen(parentfd, 5);
while (1) {
clientlen = sizeof(clientaddr);
childfd = accept(parentfd, (struct sockaddr *)&clientaddr, &clientlen);
stream = fdopen(childfd, "r+");
if (fgets(buf, BUFSIZE, stream) == NULL) { fclose(stream); continue; }
sscanf(buf, "%s %s %s", method, uri, version);
while(strcmp(buf, "\r\n") && strcmp(buf, "\n")) { fgets(buf, BUFSIZE, stream); }
if (strcasecmp(method, "GET")) {
cerror(stream, method, "501", "Not Implemented", "Tiny does not implement this method");
} else {
strcpy(filename, ".");
strcat(filename, uri);
if (stat(filename, &sbuf) < 0) {
cerror(stream, filename, "404", "Not found", "Couldn't find this file");
} else if (S_ISDIR(sbuf.st_mode)) {
serve_directory(stream, filename);
} else {
if (strstr(filename, ".html")) strcpy(filetype, "text/html");
else strcpy(filetype, "text/plain");
fprintf(stream, "HTTP/1.1 200 OK\nServer: Tiny Web Server\nContent-length: %d\nContent-type: %s\n\n", (int)sbuf.st_size, filetype);
fflush(stream);
int fd = open(filename, O_RDONLY);
void *p = mmap(0, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
fwrite(p, 1, sbuf.st_size, stream);
munmap(p, sbuf.st_size);
close(fd);
}
}
fclose(stream);
}
}
No comments:
Post a Comment