-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.monkey
115 lines (93 loc) · 2.98 KB
/
http.monkey
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
Import mojo
Import brl.asynctcpstream
'
' Implment the following Interface to retrevie the data (when everything is completed)
'
Interface HTTPListener
Method OnHTTPPageComplete:Void(data$)
End
Class HTTPGetter Implements IOnConnectComplete, IOnReadComplete, IOnWriteComplete
Field host$
Field port%
Field listener:HTTPListener
Field stream:AsyncTcpStream
Field strqueue := New StringList
Field rbuf := New DataBuffer(1024)
Field wbuf := New DataBuffer(256)
Field page$ = ""
Field text$ = ""
Field dataflag? = False
FIeld fileNotFound?
Method GetPage:Void(host$, page$, port%, listener:HTTPListener)
Self.host = host
Self.port = port
Self.listener = listener
Self.page = page
Self.text = ""
Self.dataflag = False ' marker for start of web page data
Self.stream = New AsyncTcpStream
Self.stream.Connect(host, port, Self)
fileNotFound = False
End
Method Update?()
If stream Return stream.Update()
Return False
End
Private
Method Finish:Void()
If (text <> "") THen listener.OnHTTPPageComplete(text)
strqueue.Clear
stream.Close
stream=Null
End
Method ReadMore:Void()
stream.ReadAll(rbuf, 0, rbuf.Length, Self)
End
Method WriteMore:Void()
If strqueue.IsEmpty() Return
Local str := strqueue.RemoveFirst()
wbuf.PokeString(0, str)
stream.WriteAll(wbuf, 0, str.Length, Self)
End
Method WriteString:Void(str$)
strqueue.AddLast(str)
End
Method OnConnectComplete:Void(connected?, source:IAsyncEventSource)
If Not connected
Finish()
Return
End
WriteString("GET " + page + " HTTP/1.0~r~n")
WriteString("Host: " + host + "~r~n")
WriteString("~r~n")
WriteMore()
ReadMore()
End
Method OnReadComplete:Void(buf:DataBuffer, offset%,count%, source:IAsyncEventSource)
If Not count 'EOF!
Finish()
Return
End
OnHTTPDataReceived(buf,offset,count)
ReadMore()
End
Method OnWriteComplete:Void(buf:DataBuffer, offset%,count%, source:IAsyncEventSource )
WriteMore()
End
Method OnHTTPDataReceived:Void(data:DataBuffer, offset%, count% )
Local str:= data.PeekString(offset, count)
For Local line:=Eachin str.Split( "~n" )
' have we already gone past the start of data marker ?
If dataflag Then
text = text + line + "~n"
Else
' we at the data marker for data? (i.e. first blank line)
If line.Contains("404 Not Found") fileNotFound = True
If line = "~r" Then
dataflag = True ' from here on in comes data !
End
End
Next
If fileNotFound Then text = ""
End
End