EDIT: Got it to work. The following is an example of a login page including the other example i gathered from this thread.
Code: Select all
#Persistent
#SingleInstance, Force
SetBatchLines, -1
global SiteContents
global UserLogin
global UserPass
global StoredLogin
global StoredPass
global StoredReqBody
global val1
global val2
SiteContents =
(LTrim
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>http-Server</title>
<link href='http://fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<style>
html {
font-family: Ubuntu;
}
body {
margin:25px;
margin-top:5px;
}
h4 {
font-size: 20px;
color:green;
}
h5 {
font-size: 16px;
color:teal;
}
</style>
<script type="text/javascript">
[ScriptTagContents]
</script>
</head>
<body>
[BodyTagContents]
</body>
</html>
)
StoredLogin = admin
StoredPass = 1234
paths := {}
paths["404"] := Func("NotFound")
paths["/"] := Func("LoginPage")
paths["/page"] := Func("MainPage")
paths["/reply"] := Func("ResponsePage")
Server := new HttpServer()
Server.LoadMimes(A_ScriptDir . "\mime.types")
Server.SetPaths(paths)
Server.Serve(8000)
StartUpURL := "http://localhost:8000/"
Run, %StartUpURL%
Return
NotFound(ByRef req, ByRef res)
{
res.SetBodyText("Page not found")
}
LoginPage(ByRef req, ByRef res)
{
ContentsOfScript = <!--...-->
ContentsToDisplay =
(LTrim
<div class="container" align="center">
<section id="content">
<form action="/page" method="post">
<h4>Log in</h4>
<div id="username">
<input type="text" placeholder="Username" required="" name="username" />
</div>
<div id="password">
<input type="password" placeholder="Password" required="" name="password" />
</div>
<br />
<div id="button1">
<input type="submit" value="Log in" />
</div>
</form>
<div class="button">
</div>
</section>
</div>
)
StringReplace, ServeTemp, SiteContents, [ScriptTagContents], %ContentsOfScript%, All
StringReplace, Serve, ServeTemp, [BodyTagContents], %ContentsToDisplay%, All
res.SetBodyText(Serve)
res.status := 200
}
MainPage(ByRef req, ByRef res)
{
UserLogin = ;Wipe all contents
UserPass = ;Wipe all contents
HttpReqBodyArray := "" ;Release the object
HttpReqBodyArray := Object()
For each, Pair in StrSplit(req.body,"&")
{
Part := StrSplit(Pair, "=")
HttpReqBodyArray.Push([Part[1], Part[2]])
}
UserLogin := HttpReqBodyArray[1,2]
UserPass := HttpReqBodyArray[2,2]
If (UserLogin != StoredLogin) || (UserPass != StoredPass)
{
ContentsToDisplay = <p>Wrong username or password</p>
}
Else
{
StoredReqBody := req.body
IniRead, val1, %A_ScriptDir%\output.ini, Response, param1, "5.5"
IniRead, val2, %A_ScriptDir%\output.ini, Response, param2, "3.14"
ContentsOfScript =
(LTrim
function sendIt() {
var prt1 = document.getElementById("firstid").value;
var prt2 = document.getElementById("secondid").value;
if (prt1 == "" || prt2 == "") {
alert("Fill in some values into both fields.");
return false;
}
else if ((isNaN(prt1)) || (isNaN(prt2))) {
alert("Values in form fields have to be numeric.");
return false;
}
else if (prt1.length > 8 || prt2.length > 8) {
alert("Too many characters in the entry.");
return false;
}
else {
return true;
}
}
)
ContentsToDisplay =
(LTrim
<h4>Set any number to each form field</h4>
<ul>
<li>Use dot "." as a delimiter</li><br />
<form action="/reply" method="post">
<input type=text name=param1 value=%val1% id="firstid" />
+
<input type=text name=param2 value=%val2% id="secondid" />
<input type=submit onClick="return sendIt();" />
</form>
</ul>
)
}
StringReplace, ServeTemp, SiteContents, [ScriptTagContents], %ContentsOfScript%, All
StringReplace, Serve, ServeTemp, [BodyTagContents], %ContentsToDisplay%, All
res.SetBodyText(Serve)
res.status := 200
}
ResponsePage(ByRef req, ByRef res)
{
UserLogin = ;Wipe all contents
UserPass = ;Wipe all contents
HttpReqBodyArray := "" ;Release the object
ReqBodyWithAuth := StoredReqBody . "&" . req.body
HttpReqBodyArray := Object()
For each, Pair in StrSplit(ReqBodyWithAuth,"&")
{
Part := StrSplit(Pair, "=")
HttpReqBodyArray.Push([Part[1], Part[2]])
}
UserLogin := HttpReqBodyArray[1,2]
UserPass := HttpReqBodyArray[2,2]
If (UserLogin != StoredLogin) || (UserPass != StoredPass) || (HttpReqBodyArray[3,2] = "") || (HttpReqBodyArray[4,2] = "")
{
ContentsToDisplay = <p>Restricted</p>
}
Else
{
val1 := HttpReqBodyArray[3,2]
val2 := HttpReqBodyArray[4,2]
answer := val1 + val2
ContentsOfScript = <!--...-->
ContentsToDisplay =
(LTrim
<h5>%val1% + %val2% = <b>%answer%</b></h5>
<p>Data written to <b>output.ini</b> file</p>
)
IniWrite, %val1%, %A_ScriptDir%\output.ini, Response, param1
IniWrite, %val2%, %A_ScriptDir%\output.ini, Response, param2
IniWrite, %answer%, %A_ScriptDir%\output.ini, Response, answer
}
StringReplace, ServeTemp, SiteContents, [ScriptTagContents], %ContentsOfScript%, All
StringReplace, Serve, ServeTemp, [BodyTagContents], %ContentsToDisplay%, All
res.SetBodyText(Serve)
res.status := 200
}
#Include, %A_ScriptDir%\AHKhttp.ahk ; https://github.com/Skiouros/AHKhttp/blob/master/AHKhttp.ahk
#Include, %A_ScriptDir%\AHKsock.ahk ; https://github.com/jleb/AHKsock/blob/master/AHKsock.ahk