aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-11-22 16:21:36 +0100
committerMarvin Borner2020-11-22 16:21:36 +0100
commit608fcc4075c1f28207aa177ec2d9408cc3e5e0da (patch)
treef526124c6ebcdb5895c074f2296eec5900622ef3
parent9b739062e9e0a10770d5f6c11d65649671031374 (diff)
Added HTTP response things
Very specific.
-rw-r--r--apps/server.c7
-rw-r--r--libnet/http.c24
-rw-r--r--libnet/inc/http.h67
-rw-r--r--res/index.html11
4 files changed, 107 insertions, 2 deletions
diff --git a/apps/server.c b/apps/server.c
index ceb5ed0..5c94a88 100644
--- a/apps/server.c
+++ b/apps/server.c
@@ -1,12 +1,13 @@
// MIT License, Copyright (c) 2020 Marvin Borner
#include <assert.h>
+#include <mem.h>
#include <net.h>
#include <print.h>
#include <str.h>
#define PORT 8000
-#define RESP "HTTP/1.1 200\r\nContent-Length: 14\r\nConnection: close\r\n\r\n<h1>Hallo</h1>"
+#define FILE "/res/index.html"
int main()
{
@@ -19,7 +20,9 @@ int main()
socket->state = S_CONNECTED;
char buf[4096] = { 0 };
net_receive(socket, buf, 4096);
- net_send(socket, strdup(RESP), strlen(RESP));
+ memset(buf, 0, 4096);
+ int l = http_response(HTTP_200, stat(FILE), read(FILE), buf);
+ net_send(socket, buf, l);
net_close(socket);
}
diff --git a/libnet/http.c b/libnet/http.c
index b1febbc..8cd0f31 100644
--- a/libnet/http.c
+++ b/libnet/http.c
@@ -1,6 +1,9 @@
// MIT License, Copyright (c) 2020 Marvin Borner
+#include <assert.h>
+#include <conv.h>
#include <def.h>
+#include <http.h>
#include <mem.h>
#include <print.h>
#include <str.h>
@@ -28,6 +31,27 @@ char *http_code(char *r)
return code;
}
+u32 http_response(const char *http_code, u32 content_length, const char *data, char *resp)
+{
+ char buf[16] = { 0 };
+
+ resp[0] = '\0';
+ strcat(resp, "HTTP/1.1 ");
+ strcat(resp, buf);
+ strcat(resp, http_code);
+ strcat(resp, "\r\n");
+ strcat(resp, "Content-Length: ");
+ strcat(resp, conv_base(content_length, buf, 10, 0));
+ strcat(resp, "\r\n");
+ strcat(resp, "Server: Melvix\r\n");
+ strcat(resp, "Content-Type: text/html\r\n");
+ strcat(resp, "Connection: close\r\n\r\n");
+ u32 len = strlen(resp);
+ memcpy(&resp[len], data, content_length);
+
+ return len + content_length;
+}
+
char *http_query_get(const char *url, const char *path)
{
char *query = malloc(27 + strlen(url)); // TODO: Dynamic http length etc
diff --git a/libnet/inc/http.h b/libnet/inc/http.h
index 30de9ba..559ea3d 100644
--- a/libnet/inc/http.h
+++ b/libnet/inc/http.h
@@ -3,8 +3,75 @@
#ifndef HTTP_H
#define HTTP_H
+#include <def.h>
+
char *http_data(char *response);
+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);
+#define HTTP_100 "100 Continue"
+#define HTTP_101 "101 Switching Protocol"
+#define HTTP_102 "102 Processing"
+#define HTTP_103 "103 Early Hints"
+#define HTTP_200 "200 OK"
+#define HTTP_201 "201 Created"
+#define HTTP_202 "202 Accepted"
+#define HTTP_203 "203 Non-Authoritative Information"
+#define HTTP_204 "204 No Content"
+#define HTTP_205 "205 Reset Content"
+#define HTTP_206 "206 Partial Content"
+#define HTTP_207 "207 Multi-Status"
+#define HTTP_208 "208 Already Reported"
+#define HTTP_226 "226 IM Used"
+#define HTTP_300 "300 Multiple Choice"
+#define HTTP_301 "301 Moved Permanently"
+#define HTTP_302 "302 Found"
+#define HTTP_303 "303 See Other"
+#define HTTP_304 "304 Not Modified"
+#define HTTP_305 "305 Use Proxy"
+#define HTTP_306 "306 Unused"
+#define HTTP_307 "307 Temporary Redirect"
+#define HTTP_308 "308 Permanent Redirect"
+#define HTTP_400 "400 Bad Request"
+#define HTTP_401 "401 Unauthorized"
+#define HTTP_402 "402 Payment Required"
+#define HTTP_403 "403 Forbidden"
+#define HTTP_404 "404 Not Found"
+#define HTTP_405 "405 Method Not Allowed"
+#define HTTP_406 "406 Not Acceptable"
+#define HTTP_407 "407 Proxy Authentication Required"
+#define HTTP_408 "408 Request Timeout"
+#define HTTP_409 "409 Conflict"
+#define HTTP_410 "410 Gone"
+#define HTTP_411 "411 Length Required"
+#define HTTP_412 "412 Precondition Failed"
+#define HTTP_413 "413 Payload Too Large"
+#define HTTP_414 "414 URI Too Long"
+#define HTTP_415 "415 Unsupported Media Type"
+#define HTTP_416 "416 Range Not Satisfiable"
+#define HTTP_417 "417 Expectation Failed"
+#define HTTP_418 "418 I'm a teapot"
+#define HTTP_421 "421 Misdirected Request"
+#define HTTP_422 "422 Unprocessable Entity"
+#define HTTP_423 "423 Locked"
+#define HTTP_424 "424 Failed Dependency"
+#define HTTP_425 "425 Too Early"
+#define HTTP_426 "426 Upgrade Required"
+#define HTTP_428 "428 Precondition Required"
+#define HTTP_429 "429 Too Many Request"
+#define HTTP_431 "431 Request Header Fields Too Large"
+#define HTTP_451 "451 Unavailable For Legal Reasons"
+#define HTTP_500 "500 Internal Server Error"
+#define HTTP_501 "501 Not Implemented"
+#define HTTP_502 "502 Bad Gateway"
+#define HTTP_503 "503 Service Unavailable"
+#define HTTP_504 "504 Gateway Timeout"
+#define HTTP_505 "505 HTTP Version Not Supported"
+#define HTTP_506 "506 Variant Also Negotiates"
+#define HTTP_507 "507 Insufficient Storage"
+#define HTTP_508 "508 Loop Detected"
+#define HTTP_510 "510 Not Extended"
+#define HTTP_511 "511 Network Authentication Required"
+
#endif
diff --git a/res/index.html b/res/index.html
new file mode 100644
index 0000000..3d7488b
--- /dev/null
+++ b/res/index.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="UTF-8" />
+ <meta name="viewport" content="width=device-width" />
+ <title>Melvix Server</title>
+ </head>
+ <body>
+ <h1>This is a test page.</h1>
+ </body>
+</html>