I'm trying to create a very simple HTTP proxy wich forward only 'GET' request and transmit the response, but my code has bug and I don't know how to fix it. Have you an idea or do you know a better way to make it ?
Thanks
(I can translate comments if it helps you)
Code: Select all
If InitNetwork() = 0
MessageRequester("Erreur", "Initialisation du réseau impossible.")
End
EndIf
;- ********** Declarations ************************
; Constantes
#Max = 1
#T_Buffer_R_Nabaz = 2000
; Structures
Structure S_Serveur
Event.l
Port.l
EndStructure
Structure S_Client
ID.l
*Buffer.l
Taille.l
Commande.s
Cookie.s
Accept.s
Host.s
ContentType.s
ContentLength.s
Adresse.s
Connexion.l
Port.s
EndStructure
; Listes
NewList Clients.S_Client()
; Variables
Sortie.b
Define.l i, Length
Serveur.S_Serveur
Define.s Eol, Temp
;- ********** Initialisations ****************
; Serveur
Serveur\Port = 4444
; Autres
Eol = Chr(13) + Chr(10)
; Création du serveur
If CreateNetworkServer(0, Serveur\Port) = 0
MessageRequester("Erreur", "Impossible de créer le serveur," + Eol + "le port '" + Str(Port) + "' est-il libre ?")
End
EndIf
;- ********** Fonctions ******************
Procedure FillMemory(*Buffer,Taille.l,Car.l)
For i.l = 0 To Taille - 1
PokeL(*Buffer + i,Car)
Next i
EndProcedure
If CreateFile(0,"Debug_Proxy.txt")
;- ********** Création de la fenêtre *************
OpenWindow(0, 0, 0, 800, 600, "Nabaztag", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
CreateGadgetList(WindowID(0))
Repeat
Select WindowEvent()
Case #PB_Event_CloseWindow
Sortie = 1
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
EndSelect
Serveur\Event = NetworkServerEvent()
If Serveur\Event
Select Serveur\Event
Case #PB_NetworkEvent_Connect
AddElement(Clients())
Clients()\ID = EventClient()
Clients()\Buffer = AllocateMemory(#T_Buffer_R_Nabaz)
WriteStringN(0,"Nouveau client connecté, ClientID : " + Str(Clients()\ID))
Case #PB_NetworkEvent_Disconnect
WriteStringN(0,"Un client c'est déconnecté, ClientID : " + Str(EventClient()))
ResetList(Clients())
While NextElement(Clients())
If Clients()\ID = EventClient()
If Clients()\Connexion
CloseNetworkConnection(Clients()\Connexion)
EndIf
FreeMemory(Clients()\Buffer)
DeleteElement(Clients())
Break
EndIf
Wend
Case #PB_NetworkEvent_Data
FillMemory(Clients()\Buffer, #T_Buffer_R_Nabaz, 0)
ResetList(Clients())
While NextElement(Clients())
If Clients()\ID = EventClient()
Clients()\Taille = ReceiveNetworkData(Clients()\ID, Clients()\Buffer, #T_Buffer_R_Nabaz)
If PeekS(Clients()\Buffer,3) = "GET" ; Si on reçoit la commande 'GET'
; On détermine le header envoyé
Clients()\Accept = ""
Clients()\Host = ""
Clients()\Cookie = ""
Clients()\ContentType = ""
Clients()\ContentLength = ""
Clients()\Commande = StringField(PeekS(Clients()\Buffer,Clients()\Taille),1,Eol)
For i = 1 To CountString(PeekS(Clients()\Buffer,Clients()\Taille),Eol) + 1
Temp = StringField(PeekS(Clients()\Buffer,Clients()\Taille),i,Eol)
If FindString(Temp,"Accept: ",1) : Clients()\Accept = Temp : EndIf
If FindString(Temp,"Host: ",1) : Clients()\Host = Temp : EndIf
If FindString(Temp,"Cookie: ",1) : Clients()\Cookie = Temp : EndIf
If FindString(Temp,"Content-Type: ",1) : Clients()\ContentType = Temp : EndIf
If FindString(Temp,"Content-Length: ",1) : Clients()\ContentLength = Temp : EndIf
Next i
WriteStringN(0,"--- Commande reçue ---")
WriteStringN(0,Clients()\Commande)
WriteStringN(0,"Accept: [" + Clients()\Accept + "]")
WriteStringN(0,"Host: [" + Clients()\Host + "]")
WriteStringN(0,"Cookie: [" + Clients()\Cookie + "]")
WriteStringN(0,"Content-Type: [" + Clients()\ContentType + "]")
WriteStringN(0,"Content-Length: [" + Clients()\ContentLength + "]")
WriteStringN(0,"---")
; On récupère le contenu du champ 'Host' pour s'y connecter
Clients()\Adresse = Right(Clients()\Host, Len(Clients()\Host) - 6)
Clients()\Port = StringField(Clients()\Adresse,2,":")
If Clients()\Port = "" : Clients()\Port = "80" : EndIf
Clients()\Adresse = StringField(Clients()\Adresse,1,":")
Clients()\Adresse = RemoveString(Clients()\Adresse, " ")
WriteStringN(0,"Adresse : " + Clients()\Adresse + " | Port : " + Clients()\Port)
WriteStringN(0,"")
Clients()\Connexion = OpenNetworkConnection(Clients()\Adresse,Val(Clients()\Port))
; Construction de la commande 'GET' à envoyer au serveur
FillMemory(Clients()\Buffer, #T_Buffer_R_Nabaz, 0)
Clients()\Commande = RemoveString(Clients()\Commande,":80")
Debug Clients()\Commande
Length = PokeS(Clients()\Buffer, Clients()\Commande + Eol)
If Clients()\Host : Length + PokeS(Clients()\Buffer + Length, Clients()\Host + Eol) : EndIf
Length + PokeS(Clients()\Buffer + Length, "User-Agent: ONabz-Proxy/1.0" + Eol)
If Clients()\Cookie : Length + PokeS(Clients()\Buffer + Length, Clients()\Cookie + Eol) : EndIf
If Clients()\Accept : Length + PokeS(Clients()\Buffer + Length, Clients()\Accept + Eol) : EndIf
Length + PokeS(Clients()\Buffer + Length, "Connection: close" + Eol)
WriteStringN(0,"--- Get construit ---")
WriteStringN(0,PeekS(Clients()\Buffer))
WriteStringN(0,"")
; On se connecte au serveur
If Clients()\Connexion
SendNetworkData(Clients()\Connexion, Clients()\Buffer, Length)
Else
WriteStringN(0,"Impossible de se connecter au serveur.")
WriteStringN(0,"")
EndIf
Else
Debug "Pas GET..."
EndIf
Break
EndIf
Wend
EndSelect
EndIf
ResetList(Clients())
While NextElement(Clients())
If Clients()\Connexion
If NetworkClientEvent(Clients()\Connexion)
Repeat
FillMemory(Clients()\Buffer, #T_Buffer_R_Nabaz, 0)
Clients()\Taille = ReceiveNetworkData(Clients()\Connexion, Clients()\Buffer, #T_Buffer_R_Nabaz)
tmp.l = SendNetworkData(Clients()\ID, Clients()\Buffer, Clients()\Taille)
If tmp = -1 : Debug "--- Retour de -1 ---" : EndIf
Delay(100)
Until Clients()\Taille < #T_Buffer_R_Nabaz
WriteStringN(0,"--- Données reçues ---")
WriteStringN(0,Str(Clients()\Taille))
WriteStringN(0,"")
EndIf
EndIf
Wend
Delay(10)
Until Sortie = 1
CloseFile(0)
Else
Debug "Impossible d'ouvrir"
EndIf
End