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[0] 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[0]+1][2] For $t = 1 To $sPostData[0] $sTemp = StringSplit($sPostData[$t],"=") If $sTemp[0] >= 2 Then $sReturn[$t][0] = $sTemp[1] $sReturn[$t][1] = $sTemp[2] 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][0] = $sName Then Return $sArray[$i][1] EndIf Next Return "" EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 21, 2010 Moderators Share Posted February 21, 2010 MicahC,How does StringTrimLeft sound? Could it be what you want? M23P.S. Details are in the Help file. Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
MicahC Posted February 21, 2010 Author Share Posted February 21, 2010 Wow! This works great, I cant believe it could be something as simple as that! Thanks so much for your help. Link to comment Share on other sites More sharing options...
MicahC Posted February 21, 2010 Author Share Posted February 21, 2010 One more quick question that comes to mind... Is it possible to delete certain characters out of a string? Example, I open the file "Paper Planes.mp3" on the server %20paper%20planes is returned. Can i remove the %20's and replace them with a space? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted February 21, 2010 Moderators Share Posted February 21, 2010 MicahC,Dog eaten your Help file?9 (yes, NINE) places above the last command in the Help file index is StringReplace.I mean.....make some effort! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
MicahC Posted February 21, 2010 Author Share Posted February 21, 2010 Sorry about that, i guess i should have checked the help file. Here is the problem, I cant tell it to replace the "/" and the "%20" at the same time. If i do only the %20 works. $result = StringTrimLeft($sRequest, 1) $result = StringReplace($sRequest, "%20", " ") Link to comment Share on other sites More sharing options...
bogQ Posted February 21, 2010 Share Posted February 21, 2010 $result holds the new data so do $result1 = StringReplace($result, "%20", " ") or do $result = StringReplace(StringTrimLeft($sRequest, 1), "%20", " ") TCP server and client - Learning about TCP servers and clients connectionAu3 oIrrlicht - Irrlicht projectAu3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related) There are those that believe that the perfect heist lies in the preparation.Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost. Link to comment Share on other sites More sharing options...
MicahC Posted February 21, 2010 Author Share Posted February 21, 2010 Thanks. The code works great! $result = StringReplace(StringTrimLeft($sRequest, 1), "%20", " ") Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now