aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-11-22 19:22:31 +0100
committerMarvin Borner2020-11-22 19:22:31 +0100
commitd82b18c90710baf16239257272a740488fddf11c (patch)
tree9a59f9c4eb16a5d5050d430f1946d9997ae5e6db
parent608fcc4075c1f28207aa177ec2d9408cc3e5e0da (diff)
Added file-based HTTP server
And fixed/added some other things
-rw-r--r--apps/server.c22
-rw-r--r--kernel/features/fs.c19
-rw-r--r--libc/inc/str.h2
-rw-r--r--libc/str.c16
-rw-r--r--libnet/http.c22
-rw-r--r--libnet/inc/http.h3
-rw-r--r--res/www/404.html14
-rw-r--r--res/www/index.html (renamed from res/index.html)1
8 files changed, 84 insertions, 15 deletions
diff --git a/apps/server.c b/apps/server.c
index 5c94a88..fe2c17c 100644
--- a/apps/server.c
+++ b/apps/server.c
@@ -7,7 +7,9 @@
#include <str.h>
#define PORT 8000
-#define FILE "/res/index.html"
+#define FILE "/res/www/index.html"
+#define PATH "/res/www"
+#define ERROR "/res/www/404.html"
int main()
{
@@ -18,11 +20,25 @@ int main()
assert(socket);
socket->src_port = PORT;
socket->state = S_CONNECTED;
+
char buf[4096] = { 0 };
net_receive(socket, buf, 4096);
+
+ char path[128] = { 0 };
+ strcat(path, PATH);
+ http_query_path(buf, path);
+ if (strlen(path) == strlen(PATH) + 1)
+ strcat(path, "index.html");
+
memset(buf, 0, 4096);
- int l = http_response(HTTP_200, stat(FILE), read(FILE), buf);
- net_send(socket, buf, l);
+
+ u32 len = 0;
+ if ((len = stat(path)))
+ len = http_response(HTTP_200, len, read(path), buf);
+ else
+ len = http_response(HTTP_404, stat(ERROR), read(ERROR), buf);
+
+ net_send(socket, buf, len);
net_close(socket);
}
diff --git a/kernel/features/fs.c b/kernel/features/fs.c
index c17a721..480f199 100644
--- a/kernel/features/fs.c
+++ b/kernel/features/fs.c
@@ -63,7 +63,8 @@ void *read_inode(struct inode *in)
if (!num_blocks)
return NULL;
- u32 sz = BLOCK_SIZE * num_blocks;
+ /* u32 sz = BLOCK_SIZE * num_blocks; */
+ u32 sz = in->size;
void *buf = malloc(sz);
printf("Loading %dKiB\n", sz >> 10);
assert(buf != NULL);
@@ -164,22 +165,18 @@ struct inode *find_inode_by_path(char *path)
void *file_read(char *path)
{
- return read_inode(find_inode_by_path(path));
+ struct inode *in = find_inode_by_path(path);
+ if (in)
+ return read_inode(in);
+ else
+ return NULL;
}
u32 file_stat(char *path)
{
struct inode *in = find_inode_by_path(path);
- assert(in);
if (!in)
return 0;
- u32 num_blocks = in->blocks / (BLOCK_SIZE / SECTOR_SIZE);
-
- assert(num_blocks != 0);
- if (!num_blocks)
- return 0;
-
- u32 sz = BLOCK_SIZE * num_blocks;
- return sz;
+ return in->size;
}
diff --git a/libc/inc/str.h b/libc/inc/str.h
index 65774e7..3156fb2 100644
--- a/libc/inc/str.h
+++ b/libc/inc/str.h
@@ -7,8 +7,10 @@
u32 strlen(const char *s);
char *strcpy(char *dst, const char *src);
+char *strncpy(char *dst, const char *src, u32 n);
char *strchr(const char *s, int c);
char *strcat(char *dst, const char *src);
+char *strncat(char *dst, const char *src, u32 n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, u32 n);
char *strinv(char *s);
diff --git a/libc/str.c b/libc/str.c
index d1ee325..7f6d782 100644
--- a/libc/str.c
+++ b/libc/str.c
@@ -24,6 +24,16 @@ char *strcpy(char *dst, const char *src)
return dst;
}
+char *strncpy(char *dst, const char *src, u32 n)
+{
+ char *q = dst;
+
+ while (n-- && (*dst++ = *src++))
+ ;
+
+ return q;
+}
+
int strcmp(const char *s1, const char *s2)
{
const u8 *c1 = (const u8 *)s1;
@@ -73,6 +83,12 @@ char *strcat(char *dst, const char *src)
return dst;
}
+char *strncat(char *dst, const char *src, u32 n)
+{
+ strncpy(strchr(dst, '\0'), src, n);
+ return dst;
+}
+
char *strinv(char *s)
{
u32 s_str = strlen(s);
diff --git a/libnet/http.c b/libnet/http.c
index 8cd0f31..96dcd4d 100644
--- a/libnet/http.c
+++ b/libnet/http.c
@@ -65,3 +65,25 @@ char *http_query_get(const char *url, const char *path)
strcat(query, "\r\n\r\n");
return query;
}
+
+char *http_query_path(const char *query, char *path)
+{
+ u8 b = 0;
+ u32 s = 0;
+ u32 e = 0;
+
+ while (1) {
+ if (!b && query[e] == ' ' && query[++e]) {
+ s = e;
+ b = 1;
+ } else if (b && query[e] == ' ') {
+ strncat(path, &query[s], e - s);
+ break;
+ } else if (query[e] == '\0') {
+ return NULL;
+ }
+ e++;
+ }
+
+ return path;
+}
diff --git a/libnet/inc/http.h b/libnet/inc/http.h
index 559ea3d..223bdfc 100644
--- a/libnet/inc/http.h
+++ b/libnet/inc/http.h
@@ -6,9 +6,10 @@
#include <def.h>
char *http_data(char *response);
+char *http_code(char *r);
u32 http_response(const char *http_code, u32 content_length, const char *data, char *resp);
char *http_query_get(const char *url, const char *path);
-char *http_code(char *r);
+char *http_query_path(const char *query, char *path);
#define HTTP_100 "100 Continue"
#define HTTP_101 "101 Switching Protocol"
diff --git a/res/www/404.html b/res/www/404.html
new file mode 100644
index 0000000..fc9e927
--- /dev/null
+++ b/res/www/404.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<!-- MIT License, Copyright (c) 2020 Marvin Borner -->
+<html>
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width" />
+ <title>Melvix Server</title>
+ </head>
+ <body>
+ <center><h1>404 Not Found</h2></center>
+ <hr>
+ <center>Melvix Server</center>
+ </body>
+</html>
diff --git a/res/index.html b/res/www/index.html
index 3d7488b..49e1b07 100644
--- a/res/index.html
+++ b/res/www/index.html
@@ -1,4 +1,5 @@
<!DOCTYPE html>
+<!-- MIT License, Copyright (c) 2020 Marvin Borner -->
<html>
<head>
<meta charset="UTF-8" />