-
Notifications
You must be signed in to change notification settings - Fork 2
/
keepalive.d
53 lines (45 loc) · 2.42 KB
/
keepalive.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!rdmd -O
import std.stdio, std.compiler;
import std.file : copy;
import std.array : Appender, appender;
import std.string : format, indexOf, split, strip, toLower;
import api.danode;
void main(string[] args){
setGET(args);
Appender!(char[]) htmlpage; // Build the output before writing the headers
htmlpage.put("<html>");
htmlpage.put(" <head>");
htmlpage.put(" <title>DaNode 'user defined' CGI (D) test script</title>");
htmlpage.put(" <meta name='author' content='Danny Arends'>");
htmlpage.put(" </head>");
htmlpage.put(" <body>");
htmlpage.put(" DaNode 'user defined' CGI (D) test script<br/>");
htmlpage.put(format(" Server: <small>%s</small><br/>", SERVER));
htmlpage.put(format(" Config: <small>%s</small><br/>", CONFIG));
htmlpage.put(" <form action='keepalive.d' method='post' enctype='multipart/form-data'>");
htmlpage.put(" <table>");
htmlpage.put(format(" <tr><td><a href='keepalive.d?test=GET&do'>GET</a>: </td><td> %s</td></tr>", GET));
htmlpage.put(format(" <tr><td>POST: </td><td> %s</td></tr>", POST));
htmlpage.put(format(" <tr><td>FILES: </td><td> %s</td></tr>", FILES));
htmlpage.put(" <tr><td>Test: </td><td> <input name='test' type='text'></td></tr>");
htmlpage.put(" <tr><td>File: </td><td> <input name='file' type='file'></td></tr>");
htmlpage.put(" <tr><td> </td><td> <input type='submit' value='POST'></td></tr>");
htmlpage.put(" </table>");
htmlpage.put(" </form>");
foreach(file; FILES){ // Handle any files that being uploaded
string to = format("%s/%s", SERVER["DOCUMENT_ROOT"], file.name); // Choose a folder (here: root of the web folder) to save the uploads
move_upload_file(file.loc, to); // Move the uploaded file to somewhere
htmlpage.put(format("Uploaded: %s to %s", file.loc, to)); // Add a message to the HTML
}
htmlpage.put(" </body>");
htmlpage.put("</html>\n");
// Write headers
writeln("HTTP/1.1 200 OK");
writeln("Content-Type: text/html; charset=utf-8");
writeln("Connection: Keep-Alive"); // If Keep-Alive
writefln("Content-Length: %s", htmlpage.data.length); // Content.Length == Required, and should be correct !
writefln("Server: %s", SERVER["SERVER_SOFTWARE"]);
writefln("X-Powered-By: %s %s.%s\n", std.compiler.name, version_major, version_minor);
// Write html output
writeln(htmlpage.data);
}