MicahC Posted February 21, 2010 Share Posted February 21, 2010 I am looking for a way to delete the first character in a string. For example, I am running a server and need to get the page requested by the viewer. (e.x http://xxx.xxx.x.x/whatever). When a user requests /whatever a msgbox popups up on the server side saying they requested the page "/whatever". I would like to be able to remove the "/" so when the msgbox popups up it just says "whatever". Any help is appreciated. Thanks in advance.MicahServer Code:expandcollapse popup#cs Resources: Internet Assigned Number Authority - all Content-Types: http://www.iana.org/assignments/media-types/ World Wide Web Consortium - An overview of the HTTP protocol: http://www.w3.org/Protocols/ Credits: Manadar for starting on the webserver. Alek for adding POST and some fixes Creator for providing the "application/octet-stream" MIME type. #ce ; // OPTIONS HERE // Dim $sRootDir = @ScriptDir ; The absolute path to the root directory of the server. Dim $sIP = @IPAddress1 ; ip address as defined by AutoIt Dim $iPort = 8080 ; the listening port Dim $iMaxUsers = 1 ; Maximum number of users who can simultaneously get/post ; // END OF OPTIONS // Dim $aSocket[$iMaxUsers] ; Creates an array to store all the possible users Dim $sBuffer[$iMaxUsers] ; All these users have buffers when sending/receiving, so we need a place to store those For $x = 0 to UBound($aSocket)-1 ; Fills the entire socket array with -1 integers, so that the server knows they are empty. $aSocket[$x] = -1 Next TCPStartup() ; AutoIt needs to initialize the TCP functions $iMainSocket = TCPListen($sIP,$iPort) ;create main listening socket If @error Then ; if you fail creating a socket, exit the application MsgBox(0x20, "AutoIt Webserver", "Unable to create a socket on port " & $iPort & ".") ; notifies the user that the HTTP server will not run Exit ; if your server is part of a GUI that has nothing to do with the server, you'll need to remove the Exit keyword and notify the user that the HTTP server will not work. EndIf ConsoleWrite( "Server created on http://" & $sIP & "/" & @CRLF) ;; If you're in SciTE, While 1 $iNewSocket = TCPAccept($iMainSocket) ;; Tries to accept incoming connections If $iNewSocket >= 0 Then ; Verifies that there actually is an incoming connection For $x = 0 to UBound($aSocket)-1 ;; Attempts to store the incoming connection If $aSocket[$x] = -1 Then $aSocket[$x] = $iNewSocket ;store the new socket ExitLoop EndIf Next EndIf For $x = 0 to UBound($aSocket)-1 ; A big loop to receive data from everyone connected If $aSocket[$x] = -1 Then ContinueLoop ;; if the socket is empty, it will continue to the next iteration, doing nothing $sNewData = TCPRecv($aSocket[$x],1024) ; Receives a whole lot of data if possible If @error Then ;; Client has disconnected $aSocket[$x] = -1 ; Socket is freed so that a new user may join ContinueLoop ; Go to the next iteration of the loop, not really needed but looks oh so good ElseIf $sNewData Then ; data received $sBuffer[$x] &= $sNewData ;store it in the buffear If StringInStr(StringStripCR($sBuffer[$x]),@LF&@LF) Then ; if the request has ended .. $sFirstLine = StringLeft($sBuffer[$x],StringInStr($sBuffer[$x],@LF)) ;; helps to get the type of the request $sRequestType = StringLeft($sFirstLine,StringInStr($sFirstLine," ")-1) ;; gets the type of the request If $sRequestType = "GET" Then ;; user wants to download a file or whatever .. $sRequest = StringTrimRight(StringTrimLeft($sFirstLine,4),11) ;; let's see what file he actually wants If StringInStr(StringReplace($sRequest,"\","/"), "/.") Then ;; Disallow any attempts to go back a folder _SendError($aSocket[$x]) ;; sends back an error Else ; Here is where i need to be able to delete "/" If $sRequest = "/" Then ;; user has requested the root $sRequest = "/index.html" ;; instead of root we'll give him the index page Else msgbox(0,"",$sRequest) EndIf $sRequest = StringReplace($sRequest,"/","\") ; convert HTTP slashes to windows slashes, not really required because windows accepts both If FileExists($sRootDir & "\" & $sRequest) Then ;; makes sure the file that the user wants exists $sFileType = StringRight($sRequest,4) ;; determines the file type, so that we may choose what mine type to use Switch $sFileType Case "html", ".htm" ;; in case of normal HTML files _SendFile($sRootDir & "\" & $sRequest, "text/html", $aSocket[$x]) Case ".css" ;; in case of style sheets _SendFile($sRootDir & "\" & $sRequest, "text/css", $aSocket[$x]) Case ".jpg", "jpeg" ;; for common images _SendFile($sRootDir & "\" & $sRequest, "image/jpeg", $aSocket[$x]) Case ".png" ;; another common image format _SendFile($sRootDir & "\" & $sRequest, "image/png", $aSocket[$x]) Case Else ; this is for .exe, .zip, or anything else that is not supported is downloaded to the client using a application/octet-stream _SendFile($sRootDir & "\" & $sRequest, "application/octet-stream", $aSocket[$x]) EndSwitch Else _SendError($aSocket[$x]) ;; File does not exist, so we'll send back an error.. EndIf EndIf ElseIf EndIf $sBuffer[$x] = "" ;; clears the buffer because we just used to buffer and did some actions based on them TCPCloseSocket($aSocket[$x]) ;; we have defined connection: close, so we close the connection $aSocket[$x] = -1 ;; reset the socket so that we may accept new clients EndIf EndIf Next Sleep(10) WEnd Func _POST_ConvertString(ByRef $sString) ;; converts any characters like %20 into space 8) $sString = StringReplace($sString, '+', ' ') StringReplace($sString, '%', '') For $t = 0 To @extended $Find_Char = StringLeft( StringTrimLeft($sString, StringInStr($sString, '%')) ,2) $sString = StringReplace($sString, '%' & $Find_Char, Chr(Dec($Find_Char))) Next EndFunc Func _SendHTML($sHTML,$sSocket) ;; sends HTML data back to the client on X socket Local $iLen, $sPacket, $sSplit $iLen = StringLen($sHTML) $sPacket = Binary("HTTP/1.1 200 OK" & @CRLF & _ "Server: ManadarX/1.0 (" & @OSVersion & ") AutoIt " & @AutoItVersion & @CRLF & _ "Connection: close" & @CRLF & _ "Content-Lenght: " & $iLen & @CRLF & _ "Content-Type: text/html" & @CRLF & _ @CRLF & _ $sHTML) $sSplit = StringSplit($sPacket,"") $sPacket = "" For $i = 1 to $sSplit If Asc($sSplit[$i]) <> 0 Then ; Just make sure we don't send any null bytes, because they show up as ???? in your browser. $sPacket = $sPacket & $sSplit[$i] EndIf Next TCPSend($sSocket,$sPacket) EndFunc Func _SendFile($sAddress, $sType, $sSocket) ;; Sends a file back to the client on X socket, with X mime-type Local $hFile, $sImgBuffer, $sPacket, $a $hFile = FileOpen($sAddress,16) $sImgBuffer = FileRead($hFile) FileClose($hFile) $sPacket = Binary("HTTP/1.1 200 OK" & @CRLF & _ "Server: ManadarX/1.3.26 (" & @OSVersion & ") AutoIt " & @AutoItVersion & @CRLF & _ "Connection: close" & @CRLF & _ "Content-Type: " & $sType & @CRLF & _ @CRLF) TCPSend($sSocket,$sPacket) While BinaryLen($sImgbuffer) ;LarryDaLooza's idea to send in chunks to reduce stress on the application $a = TCPSend($sSocket,$sImgbuffer) $sImgbuffer = BinaryMid($sImgbuffer,$a+1,BinaryLen($sImgbuffer)-$a) WEnd $sPacket = Binary(@CRLF & _ @CRLF) TCPSend($sSocket,$sPacket) TCPCloseSocket($sSocket) EndFunc Func _SendError($sSocket) ;; Sends back a basic 404 error _SendHTML("404 Error: " & @CRLF & @CRLF & "The file you requested could not be found.", $sSocket) EndFunc Func _Get_Post($s_Buffer) ;; parses incoming POST data Local $sTempPost, $sLen, $sPostData, $sTemp ;Get the lenght of the data in the POST $sTempPost = StringTrimLeft($s_Buffer,StringInStr($s_Buffer,"Content-Length:")) $sLen = StringTrimLeft($sTempPost,StringInStr($sTempPost,": ")) ;Create the base struck $sPostData = StringSplit(StringRight($s_Buffer,$sLen),"&") Local $sReturn[$sPostData+1] For $t = 1 To $sPostData $sTemp = StringSplit($sPostData[$t],"=") If $sTemp >= 2 Then $sReturn[$t] = $sTemp $sReturn[$t] = $sTemp EndIf Next Return $sReturn EndFunc Func _POST($sName,$sArray) ;; Returns a POST variable based on their name and not their array index. This function basically makes up for the lack of associative arrays in Au3 For $i = 1 to UBound($sArray)-1 If $sArray[$i] = $sName Then Return $sArray[$i] EndIf Next Return "" EndFunc Link to comment Share on other sites More sharing options...
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
Already have an account? Sign in here.Sign In Now