Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Implement <exe> tags in HTM files
Browse files Browse the repository at this point in the history
  • Loading branch information
minexew committed Nov 13, 2020
1 parent e36abc0 commit 348386b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 9 deletions.
87 changes: 78 additions & 9 deletions Demo/Network/HtServ.HC
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions Www/Index.HTM
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 348386b

Please sign in to comment.