summaryrefslogtreecommitdiff
path: root/plugins/Watrack/myshows/i_cookies.inc
blob: 12584901999c317ac3a0ee668a6c22d03a8cc117 (plain)
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
{}
const
  cookies:pAnsiChar=nil;

function ExtractCookies(resp:PNETLIBHTTPREQUEST):integer;
var
  cnt,len:integer;
  p,pc:pAnsiChar;
begin
  result:=0;

  mFreeMem(cookies);
  mGetMem(cookies,1024);

  pc:=cookies;
  for cnt:=0 to resp^.headersCount-1 do
  begin
    with resp^.headers[cnt] do
      if StrCmp(szName,'Set-Cookie')=0 then
      begin
        len:=0;
        p:=szValue;
        while (p^<>#0) and (p^<>';') do
        begin
          inc(p);
          inc(len);
        end;
        if pc<>cookies then
        begin
          pc^:=';'; inc(pc);
          pc^:=' '; inc(pc);
        end;
        pc:=StrCopyE(pc,szValue,len);
        inc(result);
      end;
  end;
end;

function SendRequestCookies(url:PAnsiChar;useCookies:boolean):pAnsiChar;
var
  nlu:TNETLIBUSER;
  req :TNETLIBHTTPREQUEST;
  resp:PNETLIBHTTPREQUEST;
  hTmpNetLib:THANDLE;
  nlh:array [0..10] of TNETLIBHTTPHEADER;
begin
  result:=nil;

  FillChar(req,SizeOf(req),0);
  req.cbSize     :=NETLIBHTTPREQUEST_V1_SIZE;//SizeOf(req);
  req.requestType:=REQUEST_GET;
  req.szUrl      :=url;
  req.flags      :=NLHRF_NODUMP or NLHRF_HTTP11;

  if useCookies and (cookies<>nil) then
  begin
    nlh[0].szName :='Cookie';
    nlh[0].szValue:=cookies;

    req.headers     :=@nlh;
    req.headersCount:=1;
  end;

  FillChar(nlu,SizeOf(nlu),0);
  nlu.cbSize          :=SizeOf(nlu);
  nlu.flags           :=NUF_HTTPCONNS or NUF_NOHTTPSOPTION or NUF_OUTGOING or NUF_NOOPTIONS;
  nlu.szSettingsModule:='dummy';
  hTmpNetLib:=CallService(MS_NETLIB_REGISTERUSER,0,lparam(@nlu));

  resp:=pointer(CallService(MS_NETLIB_HTTPTRANSACTION,hTmpNetLib,lparam(@req)));

  if resp<>nil then
  begin
    if resp^.resultCode=200 then
    begin
      if resp.pData<>nil then
        StrDup(result,resp.pData,resp.dataLength)
      else
        result:=PAnsiChar(200);
      if not useCookies then
        ExtractCookies(resp);
    end
    else
    begin
      result:=pAnsiChar(int_ptr(resp^.resultCode and $0FFF));
    end;
    CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT,0,lparam(resp));
  end;

  CallService(MS_NETLIB_CLOSEHANDLE,hTmpNetLib,0);
end;