mirror of
https://github.com/minexew/Shrine.git
synced 2026-05-26 05:48:36 +00:00
Implement <exe> tags in HTM files
This commit is contained in:
+78
-9
@@ -5,8 +5,8 @@
|
||||
#define PORT 80
|
||||
#define BASE_DIR "/Www"
|
||||
|
||||
// Set to 0 if you don't want to print anything
|
||||
#define SERVER_STRING "Server: HtServ ($TX+CX,"TempleOS",D="DD_OS_NAME_VERSION"$)\r\n"
|
||||
// Set to NULL if you don't want to print anything
|
||||
#define HTTP_SERVER_STRING "Server: HtServ ($TX+CX,"TempleOS",D="DD_OS_NAME_VERSION"$)\r\n"
|
||||
|
||||
#define STATE_TERM 0
|
||||
#define STATE_NEW 1
|
||||
@@ -23,25 +23,94 @@ class CHtServSession {
|
||||
U8* resource;
|
||||
};
|
||||
|
||||
// Messy global state!
|
||||
U8* new_output;
|
||||
|
||||
U0 StreamPrint(U8 *fmt,...)
|
||||
{//Injects text into the webpage stream. Used in <exe> tags.
|
||||
U8* buf = StrPrintJoin(NULL,fmt,argc,argv);
|
||||
U8* old_output = new_output;
|
||||
new_output = MStrPrint("%s%s", old_output, buf);
|
||||
Free(buf);
|
||||
Free(old_output);
|
||||
}
|
||||
|
||||
Bool IsDotHTM(U8 *filename)
|
||||
{//Does name end in .HTM?
|
||||
I64 len = StrLen(filename);
|
||||
if (len >= 4 && !StrNCmp(filename + len - 4, ".HTM", 4))
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
U8* PreprocessHTM(U8* body) {
|
||||
new_output = StrNew("");
|
||||
U8* body_ptr = body;
|
||||
|
||||
while (*body_ptr) {
|
||||
// Perhaps we should use something like <script type="text/x-holyc"> instead?
|
||||
U8* occ = StrFind("<exe>", body_ptr);
|
||||
|
||||
if (occ) {
|
||||
// Add body to output, up to the <exe> tag
|
||||
*occ = 0;
|
||||
StreamPrint("%s", body_ptr);
|
||||
|
||||
occ += 5;
|
||||
U8* end = StrFind("</exe>", occ);
|
||||
if (end) {
|
||||
*end = 0;
|
||||
}
|
||||
|
||||
// We can also pass filename, but it is meaningless without correct line number
|
||||
ExePutS(occ);
|
||||
|
||||
if (end)
|
||||
body_ptr = end + 6;
|
||||
else
|
||||
break;
|
||||
}
|
||||
else {
|
||||
// Add (rest of) body to output
|
||||
StreamPrint("%s", body_ptr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Free(body);
|
||||
return new_output;
|
||||
}
|
||||
|
||||
U0 HtServProcess(CHtServSession* sess) {
|
||||
// look for resource
|
||||
// TODO: prevent access to parent
|
||||
U8* path = MStrPrint("%s/%s", BASE_DIR, sess->resource);
|
||||
I64 size;
|
||||
U8* data = FileRead(path, &size);
|
||||
U8* data = FileRead(path, &size); // FIXME: never freed?
|
||||
U8 line[256];
|
||||
|
||||
if (data) {
|
||||
// If this is a .HTM file, we preprocess it first
|
||||
Bool is_htm = IsDotHTM(path);
|
||||
|
||||
if (is_htm) {
|
||||
// We assume that data is NUL-terminated
|
||||
data = PreprocessHTM(data);
|
||||
size = StrLen(data);
|
||||
}
|
||||
|
||||
// Send "200 OK"
|
||||
StrPrint(line, "%s 200 OK\r\n", sess->protocol); // FIXME possible overflow
|
||||
sendString(sess->sock, line, 0);
|
||||
StrPrint(line, "Content-Length: %d\r\n", size);
|
||||
sendString(sess->sock, line, 0);
|
||||
if (SERVER_STRING) {
|
||||
sendString(sess->sock, SERVER_STRING, 0);
|
||||
if (is_htm) {
|
||||
sendString(sess->sock, "Content-Type: text/html\r\n", 0);
|
||||
}
|
||||
if (HTTP_SERVER_STRING) {
|
||||
sendString(sess->sock, HTTP_SERVER_STRING, 0);
|
||||
}
|
||||
|
||||
// TODO: how about some Content-Type??
|
||||
|
||||
sendString(sess->sock, "\r\n", 0);
|
||||
sendall(sess->sock, data, size, 0);
|
||||
@@ -52,8 +121,8 @@ U0 HtServProcess(CHtServSession* sess) {
|
||||
StrPrint(line, "%s 404 Not Found\r\n", sess->protocol); // FIXME possible overflow
|
||||
sendString(sess->sock, line, 0);
|
||||
sendString(sess->sock, "Content-Length: 0\r\n", 0);
|
||||
if (SERVER_STRING) {
|
||||
sendString(sess->sock, SERVER_STRING, 0);
|
||||
if (HTTP_SERVER_STRING) {
|
||||
sendString(sess->sock, HTTP_SERVER_STRING, 0);
|
||||
}
|
||||
sendString(sess->sock, "\r\n", 0);
|
||||
sess->state = STATE_TERM;
|
||||
|
||||
@@ -67,6 +67,16 @@ hr {
|
||||
<h3>Connected and easy to use.</h3>
|
||||
</div>
|
||||
<hr>
|
||||
<div style="text-align: center; font-style: italic;">
|
||||
<exe>
|
||||
if (HTTP_SERVER_STRING)
|
||||
StreamPrint("%s<br>", HTTP_SERVER_STRING);
|
||||
|
||||
CDateStruct ds;
|
||||
Date2Struct(&ds,Now+local_time_offset);
|
||||
StreamPrint("Served on %02d/%02d %02d:%02d:%02d.", ds.mon, ds.day_of_mon, ds.hour, ds.min, ds.sec);
|
||||
</exe>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user