-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendRequest.m
89 lines (80 loc) · 3.02 KB
/
sendRequest.m
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
function response = sendRequest(uri,request)
% uri: matlab.net.URI
% request: matlab.net.http.RequestMessage
% response: matlab.net.http.ResponseMessage
% matlab.net.http.HTTPOptions persists across requests to reuse previous
% Credentials in it for subsequent authentications
persistent options
% infos is a containers.Map object where:
% key is uri.Host;
% value is "info" struct containing:
% cookies: vector of matlab.net.http.Cookie or empty
% uri: target matlab.net.URI if redirect, or empty
persistent infos
if isempty(options)
options = matlab.net.http.HTTPOptions('ConnectTimeout',20);
end
if isempty(infos)
infos = containers.Map;
end
host = string(uri.Host); % get Host from URI
try
% get info struct for host in map
info = infos(host);
if ~isempty(info.uri)
% If it has a uri field, it means a redirect previously
% took place, so replace requested URI with redirect URI.
uri = info.uri;
end
if ~isempty(info.cookies)
% If it has cookies, it means we previously received cookies from this host.
% Add Cookie header field containing all of them.
request = request.addFields(matlab.net.http.field.CookieField(info.cookies));
end
catch
% no previous redirect or cookies for this host
info = [];
end
% Send request and get response and history of transaction.
[response, ~, history] = request.send(uri, options);
if response.StatusCode ~= matlab.net.http.StatusCode.OK
return
end
% Get the Set-Cookie header fields from response message in
% each history record and save them in the map.
arrayfun(@addCookies, history)
% If the last URI in the history is different from the URI sent in the original
% request, then this was a redirect. Save the new target URI in the host info struct.
targetURI = history(end).URI;
if ~isequal(targetURI, uri)
if isempty(info)
% no previous info for this host in map, create new one
infos(char(host)) = struct('cookies',[],'uri',targetURI);
else
% change URI in info for this host and put it back in map
info.uri = targetURI;
infos(char(host)) = info;
end
end
function addCookies(record)
% Add cookies in Response message in history record
% to the map entry for the host to which the request was directed.
%
ahost = record.URI.Host; % the host the request was sent to
cookieFields = record.Response.getFields('Set-Cookie');
if isempty(cookieFields)
return
end
cookieData = cookieFields.convert(); % get array of Set-Cookie structs
cookies = [cookieData.Cookie]; % get array of Cookies from all structs
try
% If info for this host was already in the map, add its cookies to it.
ainfo = infos(ahost);
ainfo.cookies = [ainfo.cookies cookies];
infos(char(ahost)) = ainfo;
catch
% Not yet in map, so add new info struct.
infos(char(ahost)) = struct('cookies',cookies,'uri',[]);
end
end
end