Kiowa - ein kleiner HTTP-Server mit teilweiser PHP Unterstützung

  • Ich habe in der While-Schleife aus zwei for-Schleifen eine gemacht und mit 3000 Verbindungen anschließend getestet. Läuft sehr schnell :)
    Zudem bei Fehlerseiten stand immer Port 80, auch wenn man was anderes verwendet hat.

    Spoiler anzeigen
    [autoit]


    Global $NTDLL = DllOpen("ntdll.dll") ;Open ntdll.dll, it WILL get used a lot

    [/autoit] [autoit][/autoit] [autoit]

    While 1
    Do
    For $i = 1 To $users
    $accepted[$i] = TCPAccept($socket)
    If $accepted[$i] <> -1 Then $input = 1
    ;Next
    ;$input = 0
    ;For $i = 1 To $users
    If $accepted[$i] <> -1 Then
    $receivedData = TCPRecv($accepted[$i], 2048)
    WriteLog("Data: " & $receivedData)
    $auth = _StringBetween($receivedData, "Basic ", @CRLF)
    If @error Or $auth[0] = "Og" Then
    $auth = ""
    Else
    $auth[0] = $auth[0]
    $auth = _Base64Decode($auth[0])
    EndIf
    $url = _StringBetween($receivedData, "GET ", " HTTP")
    If Not @error Then
    $url = _UnicodeURLDecode($url[0])
    $urlorg = $url
    $url = StringReplace($url, "/", "")
    $url = $homedir & $url
    $urltodisplay = $url
    _PathSplit($url, $szDrive, $szDir, $szFName, $szExt)
    If $szExt = "" Then
    If StringRight($url, 1) <> "" Then $url = $url & ""
    $urltodisplay = $url
    For $o = 1 To $index[0]
    If FileExists($url & $index[$o]) Then
    $url = $url & $index[$o]
    EndIf
    Next
    If $url = $urltodisplay Then $url = $url & "" & $index[1]

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    WriteLog(SocketToIP($accepted[$i]) & " fordert an:" & $urltodisplay)

    [/autoit] [autoit][/autoit] [autoit]

    If FileExists($url) Then
    $autherror = 0
    $sections = IniReadSectionNames("auth.ini")
    If IsArray($sections) Then
    $array = _ArraySearch($sections, StringReplace($url, $homedir, ""))
    If $array <> -1 And $auth = "" Then
    WriteLog($url & " ist geschützt - Authentifizierung angefordert")
    $error = '<head><title>Error 401</title><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></head><body><h1>401 Unauthorized</h1>Die Anfrage kann nicht ohne gültige Authentifizierung durchgeführt werden.<hr><span style="font-style: italic;">Server: ' & $ServerName & ', Port 80</span></body></html>'
    _HTTP_SendData($accepted[$i], $error, "text/html", "401 Authorization Required", IniRead("auth.ini", $sections[$array], "Relam", "Geschützter Bereich"))
    $autherror = 1
    ElseIf $auth <> "" And $array <> -1 Then
    $userpw = StringSplit($auth, ":")
    If IsArray($userpw) Then
    If $userpw[0] <> 2 Then
    _HTTP_SendData($accepted[$i], '<head><title>Error 401</title><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></head><body><h1>401 Unauthorized</h1>Die Anfrage kann nicht ohne gültige Authentifizierung durchgeführt werden.<hr><span style="font-style: italic;">Server: ' & $ServerName & ', Port 80</span></body></html>', "text/html", "401 Authorization Required")
    WriteLog("Authentifizierung fehlgeschlagen")
    $autherror = 1
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    If $userpw[1] <> IniRead("auth.ini", $sections[$array], "User", "") Or $userpw[2] <> IniRead("auth.ini", $sections[$array], "PW", "") Then
    _HTTP_SendData($accepted[$i], '<head><title>Error 401</title><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"></head><body><h1>401 Unauthorized</h1>Die Anfrage kann nicht ohne gültige Authentifizierung durchgeführt werden.<hr><span style="font-style: italic;">Server: ' & $ServerName & ', Port 80</span></body></html>', "text/html", "403 Forbidden")
    WriteLog("Authentifizierung fehlgeschlagen")
    $autherror = 1
    Else
    WriteLog("Authentifizierung gelungen")
    EndIf
    EndIf
    EndIf
    If $autherror = 0 Then
    $befehl = _StringBetween(FileRead($url), "[|-", "-|]")
    If IsArray($befehl) Then
    If $befehl[0] = "list" Then
    _PathSplit($url, $szDrive, $szDir, $szFName, $szExt)
    $MimeType = "text/html"
    list()
    WriteLog("Datei enthielt Befehl [|-list-|] - eine Dateiauflistung wurde gesendet.")
    ElseIf StringInStr($befehl[0], "help") <> 0 Then
    _HTTP_SendData($accepted[$i], FileRead("Kiowa Dokumentation.pdf"), "application/pdf")
    ElseIf StringInStr($befehl[0], "redirect") <> 0 Then
    $redirectto = _StringBetween($befehl[0], "~", "~")
    If IsArray($redirectto) Then $redirectto = $redirectto[0]

    [/autoit] [autoit][/autoit] [autoit]

    $sPacket = "HTTP/1.1 307 Temporary Redirect" & @CRLF & _
    "Location: " & $redirectto & @CRLF & _
    "Content-Lenght: 3" & @CRLF & _
    "Content-Type: text/html" & @CRLF
    TCPSend($accepted[$i], $sPacket)
    WriteLog("Client wurde zu folgender Adresse umgeleitet: " & $redirectto)
    Else
    $datensting = PHP($url)
    _HTTP_SendData($accepted[$i], $datensting, MimeType($url))

    [/autoit] [autoit][/autoit] [autoit]

    WriteLog("Datei wurden gesendet.")
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Else
    $datensting = PHP($url)
    _HTTP_SendData($accepted[$i], $datensting, MimeType($url))

    [/autoit] [autoit][/autoit] [autoit]

    WriteLog("Datei wurden gesendet.")
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    EndIf
    Else
    _PathSplit($url, $szDrive, $szDir, $szFName, $szExt)
    If DirGetSize($szDrive & $szDir) <> -1 And $szFName & $szExt = $index[1] Then
    list()
    WriteLog("Keine zu indexierende Datei im Verzeichnis gefunden, Dateiauflistung wurde gesendet.")
    Else
    If FileExists($homedir & $404) Then
    $binaerstring = PHP($homedir & $404)
    _PathSplit($homedir & "" & $404, $szDrive, $szDir, $szFName, $szExt)
    $MimeType = MimeType($homedir & "" & $404)
    _HTTP_SendData($accepted[$i], $binaerstring, $MimeType)
    Else
    _HTTP_SendData($accepted[$i], '<html><head><title>Error 404</title></head><body><h1>404 Not Found</h1>Die angeforderte Ressource wurde nicht gefunden.<hr><span style="font-style: italic;">Server: WebServer, Port ' & $port & '</span></body></html>', "text/html")
    EndIf
    WriteLog("Datei wurde nicht gefunden, 404 Error Dokument wurde gesendet.")
    EndIf
    EndIf
    Else
    If FileExists($homedir & "" & $400) Then
    $binaerstring = PHP($homedir & "" & $400)
    _HTTP_SendData($accepted[$i], $binaerstring, MimeType($homedir & "" & $400))
    Else
    _HTTP_SendData($accepted[$i], '<html><head><title>Error 400</title></head><body><h1>400 Bad Request</h1>Die Anfrage-Nachricht war fehlerhaft aufgebaut.<hr><span style="font-style: italic;">Server: WebServer, Port ' & $port & '</span></body></html>', "text/html")
    WriteLog("Die Anfrage von " & SocketToIP($accepted[$i]) & " konnte nicht verarbeitet werden - das 400 Error Dokument wurde gesendet.")
    EndIf
    EndIf
    EndIf
    TCPCloseSocket($accepted[$i])
    USleep(5000, $NTDLL)
    Next
    Until $input = 1
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func USleep($iUsec, $hDLL = "ntdll.dll") ;A rewrite of the _HighPrecisionSleep function made by monoceres (Thanks!)
    Local $hStruct = DllStructCreate("int64")
    DllStructSetData($hStruct, 1, -1 * ($iUsec * 10))
    DllCall($hDLL, "dword", "ZwDelayExecution", "int", 0, "ptr", DllStructGetPtr($hStruct))
    EndFunc ;==>USleep

    [/autoit] [autoit][/autoit] [autoit][/autoit]

    :)