summaryrefslogtreecommitdiff
path: root/Year_3/TSDWL
diff options
context:
space:
mode:
Diffstat (limited to 'Year_3/TSDWL')
-rw-r--r--Year_3/TSDWL/webserver/server.c64
1 files changed, 63 insertions, 1 deletions
diff --git a/Year_3/TSDWL/webserver/server.c b/Year_3/TSDWL/webserver/server.c
index e2f6a3e..5eedf70 100644
--- a/Year_3/TSDWL/webserver/server.c
+++ b/Year_3/TSDWL/webserver/server.c
@@ -3,11 +3,14 @@
* [1] https://it.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Richiesta_GET_e_risposta_di_successo
*/
#include <arpa/inet.h>
+#include <fcntl.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
#define DEFAULT_WWW "./www"
@@ -109,6 +112,10 @@ parse_first_line(request_t* request, char* header, size_t len)
break;
}
case 1: {
+ if (token[0] != '/') {
+ fprintf(stderr, "'%s' must starts with a '/'\n", token);
+ goto parse_error;
+ }
strcpy(request->path, token);
break;
}
@@ -142,6 +149,57 @@ parse_error:
return -1;
}
+void
+read_file(char* root, char* file, int client)
+{
+ /* FIXME: In Linux the max name length is 255, but here we include the
+ * root, and I don't want to use an array of 511 bytes (255*2+1). So I am
+ * supposing a max path of 300 chars. */
+ char path[300];
+ char buffer[1024];
+ int n;
+ int fd;
+ int root_len;
+ int file_len;
+
+ root_len = strlen(root);
+ file_len = strlen(file);
+
+ if (root[root_len - 1] == '/') {
+ root_len--;
+ }
+
+ /* Considers this kind of path like `/index.html` */
+ if (file_len == 1) {
+ file = "/index.html";
+ file_len = strlen(file);
+ }
+
+ strncpy(path, root, root_len);
+ strncpy(path + root_len, file, file_len);
+
+ if ((fd = open(path, O_RDONLY)) == -1) {
+ strcpy(buffer, "Error opening the file on server");
+
+ /* Write the error to the client and also to the server */
+ write(client, buffer, strlen(buffer) + 1);
+ perror(buffer);
+ return;
+ }
+
+ while ((n = read(fd, buffer, sizeof(buffer))) != 0) {
+ if (n == -1) {
+ perror("Error reading the file");
+ break;
+ }
+
+ buffer[n] = '\0';
+ write(client, buffer, strlen(buffer) + 1);
+ }
+
+ close(fd);
+}
+
int
main(int argc, char* argv[])
{
@@ -203,7 +261,11 @@ main(int argc, char* argv[])
write(clientfd, buffer, strlen(buffer) + 1);
}
- parse_first_line(&request, headers[0], strlen(headers[0]));
+ if (parse_first_line(&request, headers[0], strlen(headers[0])) > -1) {
+ if (request.method == GET) {
+ read_file(www_path, request.path, clientfd);
+ }
+ }
for (i = 0; i < headers_num; ++i) {
free(headers + i);