' Cheapie http file snatcher for Rapid-Q (Windows) by William Yu
' Fixed a header problem, sorry about that, should work for all webpages now.
' This program also demonstrates how to create non-blocking calls.
' If WMessage of QSOCKET is > 1024, this indicates that we want non-blocking
' calls.

$APPTYPE GUI
$TYPECHECK ON
$INCLUDE "RAPIDQ.INC"
$ESCAPECHARS ON

DECLARE SUB ButtonClick

CONST PortNum = 80        '' Standard HTTP port.
CONST TimeOut = 5         '' 5 seconds
CONST WM_SOCK = 10000     '' Make sure it's > 1024, whatever you want
' Define flags to be used with the WSAAsyncSelect() call.
'
$DEFINE FD_READ         &H01
$DEFINE FD_WRITE        &H02
$DEFINE FD_OOB          &H04
$DEFINE FD_ACCEPT       &H08
$DEFINE FD_CONNECT      &H10
$DEFINE FD_CLOSE        &H20

DIM Sock AS INTEGER
DIM Connected AS LONG

SUB FormWndProc (Hwnd&, uMsg&, wParam&, lParam&)
    IF uMsg& = WM_SOCK THEN
       '-- Should check lParam& for FD_CONNECT, but since this is our
       '-- only message, nevermind.
       Connected = 1
    END IF
END SUB


CREATE Form AS QForm
  Width = 400
  Height = 400
  Center
  Caption = "Cheapie http file snatcher"
  CREATE Socket AS QSocket
    WMessage = WM_SOCK
  END CREATE
  CREATE URLLabel AS QLabel
    Left = 8
    Top = 12
    Caption = "URL: "
  END CREATE
  CREATE Edit AS QEdit
    Left = 45
    Top = 8
    Width = 230
'    Text = "http://www.yahoo.com/"
    Text = "http://rapidq.awardspace.com/"
  END CREATE
  CREATE Button AS QButton
    Left = 300
    Top = 6
    Caption = "&Get it!"
    OnClick = ButtonClick
  END CREATE
  CREATE RichEdit AS QRichEdit
    Top = 45
    Width = Form.ClientWidth
    Height = 307
    PlainText = True
    WordWrap = False
    ScrollBars = ssBoth
  END CREATE
  CREATE StatusBar AS QStatusBar
    AddPanels "",""
    Panel(0).Width = 130
    Panel(0).Caption = str$(StatusBar.Panel(0).Width)
  END CREATE
  WndProc = FormWndProc
  ShowModal
END CREATE


SUB ButtonClick
  DIM Text AS STRING
  DIM Bytes AS LONG
  DIM Server AS STRING, PathToFile AS STRING
  DIM I AS INTEGER
  DIM T# AS DOUBLE

  PathToFile = ""
  Server = Edit.Text - "http://"       '' Take out http://

  I = INSTR(Server, "/")               '' get next / after .net, .com, etc.
  IF I THEN
    PathToFile = MID$(Server, I, LEN(Server)+1-I)  ''/folders/files
    Server = LEFT$(Server, I-1)        '' should be www.xxxx.yyy or whatever
  END IF

  Sock = Socket.Connect(Server, PortNum)
  'ShowMessage(Socket.GetPeerName(Sock))
  Connected = 0

  T# = TIMER
  DO
    DoEvents
  LOOP UNTIL Connected OR TIMER - T# >= TimeOut


  IF Connected = 0 THEN
    ShowMessage "Could not make connection"
    EXIT SUB
  ELSE
    Sock = Socket.MySocket
  END IF

  StatusBar.Panel(1).Caption = "Getting "+PathToFile+" from "+Server
  '-- Send request, end with a blank line.
  Socket.WriteLine(Sock, "GET "+ PathToFile +" HTTP/1.1")
  '-- These lines are optional...
  Socket.WriteLine(Sock, "Accept: _
       image/gif, _
       image/x-xbitmap, _
       image/jpeg, _
       image/pjpeg, _
       application/vnd.ms-powerpoint, _
       application/vnd.ms-excel, _
       application/msword, _
       application/x-shockwave-flash, */*")
 Socket.WriteLine(Sock, "Accept-Language: en-us")
' Socket.WriteLine(Sock, "Accept-Encoding: gzip, deflate")
' Socket.WriteLine(Sock, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 2.0.50727)")
  Socket.WriteLine(Sock, "Host: "+Server )'  need this line!!
' Socket.WriteLine(Sock, "Connection: Keep-Alive")' allows you to keep sending info, but will hang in this example
 Socket.WriteLine(Sock, "")    '-- End request

  Text = ""
  Bytes = 0
  DO
    IF Socket.IsServerReady(Sock) THEN
       Text = Text + Socket.Read(Sock, 32000)     '' 32000 bytes... whatever they give us
       Bytes = Bytes + Socket.Transferred
       StatusBar.Panel(0).Caption = "Bytes Read: " + STR$(Bytes)
    END IF
  LOOP UNTIL Socket.Transferred = 0
  RichEdit.Clear
  RichEdit.Text = Text
  Socket.Close(Sock)
END SUB
