-
Notifications
You must be signed in to change notification settings - Fork 2
/
MNCUtils2Service.bas
129 lines (105 loc) · 2.85 KB
/
MNCUtils2Service.bas
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
116
117
118
119
120
121
122
123
124
125
126
127
128
Type=Service
Version=5.02
ModulesStructureVersion=1
B4A=true
@EndOfDesignText@
#Region Module Attributes
#StartAtBoot: False
#End Region
'HttpUtils2 version 2.01
'Service module
Sub Process_Globals
Private hc As HttpClient
Private TaskIdToJob As Map
Public TempFolder As String
Private taskCounter As Int
Private tmpMap As Map
End Sub
Sub Service_Create
TempFolder = File.DirInternalCache
If hc.IsInitialized = False Then hc.Initialize("hc")
' hc.SetHttpParameter("http.protocol.handle-redirects", False)
TaskIdToJob.Initialize
tmpMap.Initialize
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub Service_Destroy
End Sub
Public Sub SubmitJob(job As MNCHttpJob) As Int
taskCounter = taskCounter + 1
TaskIdToJob.Put(taskCounter, job)
If job.Username <> "" And job.Password <> "" Then
hc.ExecuteCredentials(job.GetRequest, taskCounter, job.Username, job.Password)
Else
hc.Execute(job.GetRequest, taskCounter)
End If
hc.SetHttpParameter("http.protocol.handle-redirects", False)
Return taskCounter
End Sub
Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim z() As String
Dim z1() As String
Dim lstCookie As List
tmpMap.Clear
tmpMap = Response.GetHeaders
If tmpMap.ContainsKey("Set-Cookie") Then
lstCookie = tmpMap.Get("Set-Cookie")
z = Regex.Split(";", lstCookie.Get(0))
z1 = Regex.Split("=", z(0))
tmpMap.Put("JSESSIONID", z1(1))
End If
If File.Exists(TempFolder, TaskId) Then File.Delete(TempFolder, TaskId)
Response.GetAsynchronously("response", File.OpenOutput(TempFolder, TaskId, False), True, TaskId)
End Sub
Sub Response_StreamFinish (Success As Boolean, TaskId As Int)
If Success Then
CompleteJob(TaskId, Success, "")
Else
CompleteJob(TaskId, Success, LastException.Message)
End If
End Sub
Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
Dim i As Int = 0
Dim k As Int = 0
Dim z() As String
Dim z1() As String
Dim lstCookie As List
If Response <> Null Then
tmpMap.Clear
tmpMap = Response.GetHeaders
tmpMap.Put("StatusCode", StatusCode)
If tmpMap.ContainsKey("Set-Cookie") Then
lstCookie = tmpMap.Get("Set-Cookie")
z = Regex.Split(";", lstCookie.Get(0))
z1 = Regex.Split("=", z(0))
tmpMap.Put("JSESSIONID", z1(1))
End If
Try
Log(Response.GetString("UTF8"))
Catch
Log("Failed to read error message.")
End Try
If Floor(StatusCode / 100) = 3 Then
i = 1
End If
End If
If i = 0 Then
CompleteJob(TaskId, False, Reason)
Else
CompleteJob(TaskId, True, "")
End If
End Sub
Sub CompleteJob(TaskId As Int, success As Boolean, errorMessage As String)
Dim job As MNCHttpJob
Try
job = TaskIdToJob.Get(TaskId)
TaskIdToJob.Remove(TaskId)
job.success = success
job.errorMessage = errorMessage
job.Complete(TaskId)
job.ResMap = tmpMap
Catch
Log(errorMessage)
End Try
End Sub