Jump to content

Web-based AutoIt! - New with AuCGI!


theguy0000
 Share

Recommended Posts

Ok, I figured out the problem with the web server. turns out, it couldn't start because MySpace IM for some reasons, does all of its communications on port 80! Wtf?? Anyway, It's fixed now.

Edited by theguy0000

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

Hi theguy0000,

Website down again. muttley

Anyway you can get this code up onto the forum storage.

Looks like you have done some good work here - just we can't see it !!

I am sorry. This will not happen again. It does work now.

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

I tryed to create my own web server. so far so so good.

(This is still in beta stages, dont expect too much of this)

; AutoK Web Server
; Made by Kris
; Last release @ 16 juli 2008
; Version 0.0.0.4
; 

; ======= AU3 CONFIG =======
;#NoTrayIcon
#RequireAdmin
#include <Array.au3>
#Include <Date.au3>

; ======= MAIN VARS ========
$Title      = "AutoK Web Server"
$Version    = "0.0.0.4"
$Version_HTTP = "1.1"
$Configfile = @ScriptDir&"\config.ini"
$Server_IP   = IniRead($Configfile, "server", "ip", @IPAddress1)
$Server_Port = IniRead($Configfile, "server", "port", 80)
$Server_Filetype = IniRead($Configfile, "server", "filetype", ".htm,.html,.aux")
$Server_Index = IniRead($Configfile, "server", "index", "index.htm,index.html,index.aux")
$Server_Httpdir   = IniRead($Configfile, "server", "httpdir", @ScriptDir&"\httpdocs")
$Messages_Level = IniRead($Configfile, "messages", "level", 2) ; 1=critical,2+=non-critical,3+=debug,4+=fulldebug
$Log_Level  = IniRead($Configfile, "log", "level", 5) ; 1=critical,2+=non-critical,3+=connections,4+=pages,5+=debug
$Log_Logdir = IniRead($Configfile, "log", "logdir", @ScriptDir&"\logs")
$Log_Timestamps = IniRead($Configfile, "log", "timestamps", 1)
$Time_GMT = IniRead($Configfile, "time", "gmt", "+1")
$Module_List = IniReadSection($Configfile, "module")
Global $Client_List[1], $Client_Current = 1

; ====== CHECK VARS =======
If Not _IsIP($Server_IP) Then
    $Server_IP = TCPNameToIP($Server_IP)
    If Not _IsIP($Server_IP) Then
        If $Messages_Level > 0 Then _Message("Invallid ip specified "&$Server_IP&":"&$Server_Port&"."&@CRLF&"Error code: 0x0100")
        If $Log_Level > 0 Then _Log("Invallid ip specified "&$Server_IP&":"&$Server_Port&". Error code: 0x0100")
        Exit
    EndIf
EndIf
If Not _IsPort($Server_Port) Then
    If $Messages_Level > 0 Then _Message("Invallid ip specified "&$Server_IP&":"&$Server_Port&"."&@CRLF&"Error code: 0x0101")
    If $Log_Level > 0 Then _Log("Invallid ip specified "&$Server_IP&":"&$Server_Port&". Error code: 0x0101")
    Exit
EndIf
$Server_Httpdir = _PathFix($Server_Httpdir)
$Log_Logdir = _PathFix($Log_Logdir)
If $Server_Index = "" Then
    If $Messages_Level > 1 Then _Message("Warning, no indexes specified.")
    If $Log_Level > 1 Then _Log("Warning, no indexes specified.")
EndIf
If $Server_Filetype = "" Then
    If $Messages_Level > 0 Then _Message("No filetypes specified."&@CRLF&"Error code: 0x0102")
    If $Log_Level > 0 Then _Log("No filetypes specified. Error code: 0x0102")
    Exit
EndIf
If Not IsArray($Module_List) Then
    Global $Module_List[2][2]
Else
    For $i = 1 To UBound($Module_List)-1
        If $Module_List[$i][1] <> "" Then $Module_List[$i][1] = _PathFix($Module_List[$i][1])
    Next
EndIf

; ====== TCP SERVICE ======
Opt("TCPTimeout", 100)
TCPStartup()
$Server_Connection = TCPListen($Server_IP, $Server_Port)
If @error Then
    If $Messages_Level > 0 Then _Message("Could not start the server at "&$Server_IP&":"&$Server_Port&"."&@CRLF&"Error code: 0x0103")
    If $Log_Level > 0 Then _Log("Could not start the server at "&$Server_IP&":"&$Server_Port&". Error code: 0x0103")
    Exit
Else
    If $Messages_Level > 2 Then _Message("Server started at "&$Server_IP&":"&$Server_Port&".")
    If $Log_Level > 0 Then _Log("Server started at "&$Server_IP&":"&$Server_Port&".")
EndIf

; ======= MAIN LOOP =======
While 1
    $Client_New = TCPAccept($Server_Connection)
    If $Client_New > 0 And $Client_New <> "0" Then
        _ArrayAdd($Client_List, $Client_New)
        If $Messages_Level > 3 Then _Message("Client "&SocketToIP($Client_New)&" connected.")
        If $Log_Level > 2 Then _Log("Client "&SocketToIP($Client_New)&" connected.")
    EndIf
    
    If $Client_Current > UBound($Client_List)-1 Then
        $Client_Current = 1
    EndIf
    If UBound($Client_List) > 1 Then
        $Client_Data = TCPRecv($Client_List[$Client_Current], 2048)
        If @Error Then
            _ClientDisconnect($Client_Current)
        ElseIf $Client_Data <> "" Then
            If $Log_Level > 4 Then _Log("Recieved data from "&SocketToIP($Client_List[$Client_Current])&": "&$Client_Data)
            _Answer($Client_Current, $Client_Data)
        EndIf
    EndIf
    $Client_Current += 1
WEnd

; ====== FUNCTIONS =======
Func _IsIP($IP)
    $IP_Test = StringSplit($IP, ".")
    If $IP_Test[0] = 4 And $IP <> "0.0.0.0" Then
        If $IP_Test[1] < 256 And $IP_Test[2] < 256 And $IP_Test[3] < 256 And $IP_Test[4] < 256 Then Return 1
    EndIf
EndFunc

Func _IsPort($Port)
    If $Port > 0 And $Port < 65535 Then Return 1
EndFunc

Func _Message($Message)
    MsgBox(0, $Title&" - Message", $Message)
EndFunc

Func _Log($Message)
    $Timestamp = ""
    If $Log_Timestamps = 1 Then $Timestamp = @YEAR&"-"&@MON&"-"&@MDAY&" "&@HOUR&":"&@MIN&":"&@SEC&" > "
    If $Log_Level > 4 Then
        ConsoleWrite($Timestamp&$Message&@CRLF)
        FileWrite($Log_Logdir&"\"&"Log_"&@YEAR&"-"&@MON&"-"&@MDAY&".log", $Timestamp&$Message&@CRLF)
        If @error Then
            If $Messages_Level > 1 Then _Message("Unable to create a log file at "&$Log_Logdir&"."&@CRLF&"Log is now disabled."&@CRLF&"Error code: 0x0200")
            $Log_Level = 0
        EndIf
    EndIf
EndFunc

Func _PathFix($Path)
    $Path = StringReplace($Path, "/", "\")
    If StringRight($Path, 1) = "\" Then $Path = StringTrimRight($Path, 1)
    If StringLeft($Path, 1) = "\" Or StringLeft($Path, 1) = ".\" Then
        $Path = @ScriptDir&"\"&StringTrimLeft($Path, StringInStr($Path, "\"))
    EndIf
    Return $Path
EndFunc

Func _DateCurrent()
    $WDay = @WDAY
    If $WDay = 1 Then $Date = "Sun"
    If $WDay = 2 Then $Date = "Mon"
    If $WDay = 3 Then $Date = "Tue"
    If $WDay = 4 Then $Date = "Wed"
    If $WDay = 5 Then $Date = "Thu"
    If $WDay = 6 Then $Date = "Fri"
    If $WDay = 7 Then $Date = "Sat"
    If $Time_GMT = 0 Then $Time_GMT = ""
    $Date &= ", "&@MDAY&" "&_DateToMonth(@MON)&" "&@YEAR&" "&@HOUR&":"&@MIN&":"&@SEC&" GMT"&$Time_GMT
    Return $Date
EndFunc

Func _FileLastmodified($Filepath)
    $Date = FileGetTime($Filepath)
    If Not @error Then Return $Date[0]&"-"&$Date[1]&"-"&$Date[2]&" "&$Date[3]&":"&$Date[4]&":"&$Date[5]
EndFunc

Func _Answer($Client, $Client_Data)
    $HeaderMain = StringSplit(_HeaderGet($Client_Data, ""), " ")
    If $HeaderMain[0] > 1 Then
        $Filepath = _FileGet($HeaderMain[2])
    Else
        $Filepath = _FileGet("/")
    EndIf
    If @error = 1 Then
        $Server_Data = _PageStatusSet("", 404)
        If $Log_Level > 4 Then _Log("Sending response to "&SocketToIP($Client_List[$Client])&": "&$Server_Data)
        TCPSend($Client_List[$Client], $Server_Data)
        _ClientDisconnect($Client)
        Return
    ElseIf @error = 2 Then
        $Server_Data = _PageStatusSet("", 202)
        If $Log_Level > 4 Then _Log("Sending response to "&SocketToIP($Client_List[$Client])&": "&$Server_Data)
        ; Download
        TCPSend($Client_List[$Client], $Server_Data)
        _ClientDisconnect($Client)
        Return
    EndIf
    
    $Source = _SourceGet(_PageStatusSet("", 200), $Filepath)
    If $Log_Level > 4 Then _Log("Sending response to "&SocketToIP($Client_List[$Client])&": "&$Source)
    TCPSend($Client_List[$Client], $Source)
    _ClientDisconnect($Client)
EndFunc

Func _SourceGet($Data, $Filepath)
    If $Module_List[1][1] = "" Then
        $Source = FileRead($Filepath)
        $Source_Length = StringLen(Binary($Source))
        $FileLastmodified = _FileLastmodified($Filepath)
        $Data = _HeaderSet($Data, "Last-Modified", $FileLastmodified)
        $Data = _HeaderSet($Data, "Content-Length", $Source_Length)
        $Data = _HeaderSet($Data, "Etag", StringReplace(StringReplace(StringStripWS($FileLastmodified, 8), "-", ""), ":", "")&$Source_Length)
        $Data = _HeaderSet($Data, "Source", $Source)
    Else
        ;
    EndIf
    Return $Data
EndFunc

Func _FileGet($Filepath) ; Error:1=not_found,2=exists_but_not_valid
    $Indexes = _IndexGet()
    $Filetypes = _FiletypeGet()
    If $Filepath = "" Then
        $Filepath = "/"
    EndIf
    $Filepath = $Server_Httpdir&$Filepath
    If $Indexes[1] <> "" And StringRight($Filepath, 1) = "/" Then
        For $i = 1 To $Indexes[0]
            If FileExists($Filepath&"\"&$Indexes[$i]) Then Return $Filepath&"\"&$Indexes[$i]
        Next
    ElseIf StringRight($Filepath, 1) <> "/" Then
        If FileExists($Filepath) Then
            For $i = 1 To $Filetypes[0]
                If StringRight($Filepath, StringLen($Filetypes[$i])) = $Filetypes[$i] Then
                    Return $Filepath
                EndIf
            Next
            SetError(2)
            Return
        EndIf
    EndIf
    SetError(1)
EndFunc

Func _ClientDisconnect($Client)
    $Client_ID = SocketToIP($Client_List[$Client])
    If StringLen($Client_ID) < 7 Then $Client_ID = "unknown"
    
    If $Messages_Level > 3 Then _Message("Client "&$Client_ID&" disconnected.")
    If $Log_Level > 2 Then _Log("Client "&$Client_ID&" disconnected.")
    TCPCloseSocket($Client_List[$Client])
    _ArrayDelete($Client_List, $Client)
EndFunc

Func _HeaderGet($Data, $Header)
    If $Header = "" Then Return StringLeft($Data, StringInStr($Data, @CRLF, 1)-1)
    If $Header <> "" Then
        If $Header = "Source" Then
            Return StringTrimLeft($Data, StringInStr($Data, @CRLF&@CRLF, 1))
        Else
            $Data2 = StringRegExp($Data, $Header&": (.*)"&@CRLF, 1)
            If Not @error Then
                Return $Data2[1]
            EndIf
        EndIf
    EndIf
    SetError(1)
    Return ""
EndFunc

Func _HeaderSet($Data, $Header, $Value, $Overwrite = 1)
    If $Data = "" And $Header = "" Then Return $Value&@CRLF
    If $Data <> "" And $Header = "" Then
        $Line1 = StringLeft($Data, StringInStr($Data, @CRLF)-1)
        If StringInStr($Line1, ":") Then
            Return $Value&@CRLF&$Data
        Else
            If $Overwrite Then Return $Value&StringTrimLeft($Data, StringInStr($Data, @CRLF)-1)
            Return $Data
        EndIf
    EndIf
    If $Data = "" And $Header <> "" Then Return $Header&": "&$Value&@CRLF
    $Data2 = StringSplit($Data, @CRLF&@CRLF, 1)
    If $Header = "Source" Then
        If $Overwrite Then Return $Data2[1]&@CRLF&@CRLF&$Value
        If $Data2[0] < 2 Then Return $Data2[1]&@CRLF&@CRLF&$Value
        Return $Data
    ElseIf $Header <> "" Then
        If StringInStr($Data2[1], $Header) Then
            If $Overwrite Then
                $Data2[1] = StringRegExpReplace($Data2[1], $Header&": (.*)"&@CRLF, $Header&": "&$Value&@CRLF)
                If $Data2[0] > 1 Then Return $Data2[1]&@CRLF&@CRLF&$Data2[2]
                Return $Data2[1]
            EndIf
            Return $Data
        Else
            $Data2[1] &= $Header&": "&$Value&@CRLF
            If $Data2[0] > 1 Then Return $Data2[1]&@CRLF&@CRLF&$Data2[2]
            Return $Data2[1]
        EndIf
    EndIf
EndFunc

Func _IndexGet()
    $Indexes = StringSplit(StringStripWS($Server_Index, 8), ",")
    Return $Indexes
EndFunc

Func _FiletypeGet()
    $Filetypes = StringSplit(StringStripWS($Server_Filetype, 8), ",")
    Return $Filetypes
EndFunc

Func _PageStatusSet($Data, $Code)
    Switch $Code
        Case 200
            $Data = _HeaderSet($Data, "", "HTTP/"&$Version_HTTP&" "&$Code&" OK")
        Case 202
            $Data = _HeaderSet($Data, "", "HTTP/"&$Version_HTTP&" "&$Code&" ACCEPTED")
        Case 404
            $Data = _HeaderSet($Data, "", "HTTP/"&$Version_HTTP&" "&$Code&" NOTFOUND")
        ;
    EndSwitch
    
    $Data = _HeaderSet($Data, "Date", _DateCurrent(), 0)
    $Data = _HeaderSet($Data, "Server", $Title&"/"&$Version&" (Win32)", 0)
    $Data = _HeaderSet($Data, "Accept-Ranges", "bytes", 0)
    $Data = _HeaderSet($Data, "Connection", "close", 0)
    $Data = _HeaderSet($Data, "Content-Type", "text/html; charset=UTF-8", 0)
    ;$Data = _HeaderSet($Data, "Content-Encoding", "gzip")
    
    Return $Data
EndFunc

Func SocketToIP($SHOCKET)
    Local $sockaddr = DLLStructCreate("short;ushort;uint;char[8]")

    Local $aRet = DLLCall("Ws2_32.dll","int","getpeername","int",$SHOCKET, _
            "ptr",DLLStructGetPtr($sockaddr),"int*",DLLStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DLLCall("Ws2_32.dll","str","inet_ntoa","int",DLLStructGetData($sockaddr,3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    
    $sockaddr = 0
    Return $aRet
EndFunc

Grtz,

Dellairon

Edited by Dellairion
Link to comment
Share on other sites

OK, recognizing that this may be a really stupid question, but I think I've done my homework, so here goes.

I searched this entire topic for anything referencing Apache. I found four pages, but all seem to address using AuCGI as a cgi. I need to use AuCGI in a <?au3 ?> context. How do I configure Apache for that use? Suggestion: what about a sticky topic that collects all the info scattered through the topic about configuring?

Here's what I'm trying to do.

I'm developing a USB application using PortableApps and Uniform Server (which, IMHO, is the best of the WAMPs, and the only one I could find that supported VirtualHosts.) The autorun.ini executes a batch file (Start.bat) which starts a Firefox page (howto.php) with instructions how to initialize the application, then starts (and waits for) RTFM.exe (an AutoIt3 process, so I can build the text dynamically) which tells the user to read the instructions and close RTFM when he is ready to continue. When the user exits RTFM (hopefully after reading the directions), Start.bat starts (and waits for) UniCenter (an AutoIt3 application incidentally, which led me to AuCGI). UniCenter controls starting and stopping Apache and MySql. The user has been instructed how to start these processes from UniCenter, and to exit UniCenter when that has been accomplished, which allows Start.bat to open the application (http://localhost). I know this will be confusing to a new, non-technical user. It confuses me sometimes, and I wrote it!!

What I would like to do is start UniCenter directly from within howto.php like this:

<html>
<head>
. . .
<?php blah(); ?>
</head>
<body>
. . . Tell user how to start servers . . .
<?au3 Run("G:\UniformServer\uc_server_control_2.exe"); ?>
. . . Tell user how to configure application . . .
<a href="http://localhost">Continue to application</a>
</body>
</html>

eliminating all the back-and-forth and the user won't even see UniCenter.

Edited by UnixWolf
Link to comment
Share on other sites

I've been thinking a bit. Perhaps to avoid conflict with PHP pages, it should be changed from <?au3 ?> to <au3></au3>. I know it's easy to do with a simple mod of the AuCGI.au3 source, but maybe you should distribute it like this. That way, people can set the AuCGI interpreter to all extensions (Just use an asterisk in the extensions box if you use Abyss) without conflict with anything. I tried this with the <?au3 ?> conventions, and it screwed with PHP pages and Gif images.

Thoughts?

Link to comment
Share on other sites

I've been thinking a bit. Perhaps to avoid conflict with PHP pages, it should be changed from <?au3 ?> to <au3></au3>. I know it's easy to do with a simple mod of the AuCGI.au3 source, but maybe you should distribute it like this. That way, people can set the AuCGI interpreter to all extensions (Just use an asterisk in the extensions box if you use Abyss) without conflict with anything. I tried this with the <?au3 ?> conventions, and it screwed with PHP pages and Gif images.

Thoughts?

that shouldn't screw up a [properly configured] php installation, but I do see your point.

On a side note, just so that people know before following Kenton's idea, that could WILL slow down the rendering of all pages, even ones that don't use autoit in them. By how much, I'm not sure, but I will definitely do some tests.

Also, changing that would break scripts that people have wrote, and while it's not that much of a pain to fix, I don't like doing that.

How about we plan on making that change in the next version I release. Until then, you're going to have to mod it yourself muttley

The cake is a lie.www.theguy0000.com is currentlyUP images.theguy0000.com is currentlyUP all other *.theguy0000.com sites are DOWN

Link to comment
Share on other sites

that shouldn't screw up a [properly configured] php installation, but I do see your point.

On a side note, just so that people know before following Kenton's idea, that could WILL slow down the rendering of all pages, even ones that don't use autoit in them. By how much, I'm not sure, but I will definitely do some tests.

Also, changing that would break scripts that people have wrote, and while it's not that much of a pain to fix, I don't like doing that.

How about we plan on making that change in the next version I release. Until then, you're going to have to mod it yourself muttley

I did do it myself.

SciTe's Find and Replace function is a lifesaver :)

Link to comment
Share on other sites

  • 2 weeks later...

I use as a server Dwebpro staff. But when I add this AuCGI.exe I do not want to run. I open a page. Html and your horses:

<?au3

msgbox(0,"","")

?>

but the script does not work, I get this message: "what are you trying to do?"

help?

[COLOR=green]

Link to comment
Share on other sites

  • 2 weeks later...

Ok, so whenever I run a script, I get a nice little popup window and a "boop". Webserver and everything is installed normally, scripts run just fine, but... msgbox and a beep whenever a page is loaded.

Not a huge deal, but... argh.

Anyone know how to fix it?

Link to comment
Share on other sites

Also, novi, use the _msgbox function in the Web.au3 udf in order to use the web based message boxes and things like that. :P

Link to comment
Share on other sites

Easy little demo that I'm getting the annoying beep on.

##WebApp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p><?au3 echo("Hi there")?></p>
<p>Test</p>
</body>
</html>
Link to comment
Share on other sites

I recompiled the AuCGI.au3 and replaced the downloaded version. Apparently, line 12 was uncommented and was sending a blank msgbox to the server, requiring it to be clicked before the rest of the page could execute.

Now for the fun stuff. posts, databases, and ajax time :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...