Page 1 of 1

Adapting ReceiveHTTPMemory to Proxy Server

Posted: Tue Jul 02, 2013 2:41 pm
by CalamityJames
I would like to alter ReceiveHTTPMemory() by ts-soft as adapted by beo6 to use a socks5 proxy (I want to be in a different country without going anywhere) at 127.0.0.1:9150 which is already set up when the program runs. The example below fails to open the network connection (presumably because it is already open). How do I use the existing connection and then send a URL? Or is the whole thing doomed to failure because of Socks5?

Code: Select all

Procedure.i ReceiveHTTPMemory(URL.s, _Data$, _Cookie$, BufferSize = 4096, Timeout = 5000)
    Protected Connection, Time, Time2, Event, Size, Size2, SizeAll, pos
    Protected.s Server
    Protected String$
    Protected *Mem, *Buffer, *Mem2
    Size = 1
 
    If LCase(Left(URL, 7)) <> "http://" : URL = "http://" + URL : EndIf
    Server = GetURLPart(URL, #PB_URL_Site)
    If Server = "" : ProcedureReturn #False : EndIf
    ;Connection = OpenNetworkConnection(Server, 80, #PB_Network_TCP) ;original line which works
    Connection = OpenNetworkConnection("127.0.0.1", 9150, #PB_Network_TCP) ; my change, doesn't work
    If Not Connection : ProcedureReturn #False : EndIf
    If BufferSize <= 0 : BufferSize = 4096 : EndIf
    *Buffer = AllocateMemory(BufferSize)
    If Not *Buffer : ProcedureReturn #False : EndIf
   
   
    ;Build header
     
    If _Data$ <> ""
      String$ + "POST " + URL + " HTTP/1.1" + #CRLF$
      String$ + "Content-Length: " + Str(Len(_Data$)) + #CRLF$
    Else
      String$ + "GET " + URL + " HTTP/1.1" + #CRLF$
    EndIf
 
    String$ + "Host: " + Server + #CRLF$
    If _Cookie$ <> ""
      String$ + "Cookie: " + _Cookie$ + #CRLF$
    EndIf
    String$ + "Content-Type: application/x-www-form-urlencoded" + #CRLF$
    String$ + "Connection: close" + #CRLF$
    String$ + #CRLF$
    ; header is finished
   
    String$ + _Data$ + #CRLF$
   
 
    ;SendNetworkString(Connection, "GET " + URL + " HTTP/1.0" + #LFCR$ + #LFCR$)
    SendNetworkString(Connection, String$)
    Time = ElapsedMilliseconds()
    Repeat
      Event = NetworkClientEvent(Connection)
      If Event = #PB_NetworkEvent_Data
        Repeat
          Size = ReceiveNetworkData(Connection, *Buffer, BufferSize)
          If Size > 0
            Time = ElapsedMilliseconds()
            SizeAll + Size
            *Mem = ReAllocateMemory(*Mem, SizeAll)
            If *Mem
              CopyMemory(*Buffer, *Mem + (SizeAll - Size), Size)
            Else
              CloseNetworkConnection(Connection)
              FreeMemory(*Buffer)
              ProcedureReturn #False
            EndIf
          EndIf
        Until Size <= 0
      EndIf
      Time2 = ElapsedMilliseconds() - Time
    Until Time2 > Timeout Or Size <= 0
    CloseNetworkConnection(Connection)
    FreeMemory(*Buffer)
    If Time2 > Timeout
      If *Mem : FreeMemory(*Mem) : EndIf
      ProcedureReturn #False
    EndIf
    pos = FindString(PeekS(*mem, -1, #PB_UTF8), #CRLF$ + #CRLF$, 1) - 1
    pos = Len(#CRLF$ + #CRLF$) + pos
    Size2 = MemorySize(*Mem) - pos
    *Mem2 = AllocateMemory(Size2)
    If *Mem2
      CopyMemory(*Mem + pos, *Mem2, Size2)
      FreeMemory(*Mem)
      ProcedureReturn *Mem2
    EndIf
    FreeMemory(*Mem)
    ProcedureReturn #False
  EndProcedure
InitNetwork()
MemPtr =  ReceiveHTTPMemory("http://forums.purebasic.com", "", "")
If MemPtr <> 0
 Debug MemorySize(MemPtr)
 Debug PeekS(MemPtr) 
Else
  Debug "failed"
EndIf  
Edit: corrected error noted in next post

Re: Adapting ReceiveHTTPMemory to Proxy Server

Posted: Tue Jul 02, 2013 3:35 pm
by Oxyandy
127.0.01 ?
Does 127.0.0.1 work better :D ?

Re: Adapting ReceiveHTTPMemory to Proxy Server

Posted: Tue Jul 02, 2013 3:55 pm
by CalamityJames
Oops! But no it still doesn't work. I think I introduced this error after testing while changing the program slightly for this post.

Re: Adapting ReceiveHTTPMemory to Proxy Server

Posted: Tue Jul 02, 2013 4:16 pm
by Oxyandy
If I run my proxy server, which uses "127.0.0.1", 8580

Compile with Debugger, I see a connection through the proxy to my home server in the logs..

Image

6992 Bytes sent & received = matching Debug output..

EDIT so you know that code will not support a Unicode compile

Re: Adapting ReceiveHTTPMemory to Proxy Server

Posted: Tue Jul 02, 2013 4:39 pm
by Oxyandy
Swapping these 2 lines around, I see an error, a bad request 400 is created

Connection = OpenNetworkConnection(Server, 80, #PB_Network_TCP) ;original line which works
;Connection = OpenNetworkConnection("127.0.0.1", 8580, #PB_Network_TCP) ; my change, doesn't work

it should be
11:18:14 PM 192.168.1.1:18607 Requested GET /
11:18:14 PM 192.168.1.1:18607 Request dump
> GET / HTTP/1.0

But instead is this,

Image

which I'd say is correct as you probably should be passing the domain through to the proxy