mORMot and Open Source friends
Check-in [3d7ebe536a]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:{366} Added some third-party code - published on http://synopse.info/forum/viewtopic.php?pid=13115#p13115 - of a working Android Java Client Any further input (e.g. writing a simple Java library and a mustache template) is welcome! Thanks ChinaPeng for sharing :)
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3d7ebe536adc8008b1db7459a35e214f25237ce4
User & Date: User 2014-10-21 13:40:34
Context
2014-10-21
20:05
{367} TSynInvokeableVariantType.SetProperty() will now convert any varOleStr into a RawUTF8/varString, and dereference any simple varByRef transmitted values so that we could safely use late-binding with any kind of value check-in: e3a0acb46f user: User tags: trunk
13:40
{366} Added some third-party code - published on http://synopse.info/forum/viewtopic.php?pid=13115#p13115 - of a working Android Java Client Any further input (e.g. writing a simple Java library and a mustache template) is welcome! Thanks ChinaPeng for sharing :) check-in: 3d7ebe536a user: User tags: trunk
13:21
{365} Added some third-party code - published on http://synopse.info/forum/viewtopic.php?pid=13115#p13115 - of a working Android Java Client Any further input (e.g. writing a simple Java library and a mustache template) is welcome! Thanks ChinaPeng for sharing :) check-in: 4c9fe09dc7 user: User tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to SynCrtSock.pas.

26
27
28
29
30
31
32

33
34
35
36
37
38
39
...
158
159
160
161
162
163
164

165
166
167
168
169
170
171
....
1255
1256
1257
1258
1259
1260
1261
1262
1263

1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275




1276
1277
1278
1279
1280
1281
1282
1283
1284
....
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
....
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
....
5763
5764
5765
5766
5767
5768
5769


5770
5771
5772
5773
5774
5775
5776
....
5857
5858
5859
5860
5861
5862
5863













5864
5865
5866




5867
5868
5869
5870
5871
5872
5873
....
5919
5920
5921
5922
5923
5924
5925
5926
  The Initial Developer of the Original Code is Arnaud Bouchez.

  Portions created by the Initial Developer are Copyright (C) 2014
  the Initial Developer. All Rights Reserved.

  Contributor(s):
  - Alfred Glaenzer (alf)

  - Pavel (mpv)
  
  Alternatively, the contents of this file may be used under the terms of
  either the GNU General Public License Version 2 or later (the "GPL"), or
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  in which case the provisions of the GPL or the LGPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
................................................................................
  - added ConnectionTimeOut, SendTimeout and ReceiveTimeout optional parameters
    to TWinHttpAPI constructors - feature request [bfe485b678]
  - added optional aCompressMinSize parameter to RegisterCompress() methods
  - added TWinHttpAPI.Get/Post/Put/Delete() class functions for easy remote
    resource retrieval using either WinHTTP or WinINet APIs
  - added TURI structure, ready to parse a supplied HTTP URI
  - added 'ConnectionID: 1234578' to the HTTP headers - request [0636eeec54]

  - fixed TCrtSocket.BytesIn and TCrtSocket.BytesOut properties
  - fixed ticket [82df275784] TWinHttpAPI with responses without Content-Length
  - fixed ticket [f0749956af] TWinINet does not work with HTTPS servers
  - fixed ticket [842a5ae15a] THttpApiServer.Execute/SendError message
  - fixed ticket [f2ae4022a4] EWinINet error handling
  - fixed ticket [73da2c17b1] about Accept-Encoding header in THttpApiServer
  - fixed ticket [cbcbb3b2fc] about PtrInt definition
................................................................................
     you can run "proxycfg -u" or "netsh winhttp import proxy source=ie" to use
     the current user's proxy settings for Internet Explorer (under 64 bit
     Vista/Seven, to configure applications using the 32 bit WinHttp settings,
     call netsh or proxycfg bits from %SystemRoot%\SysWOW64 folder explicitely)
   - Microsoft Windows HTTP Services (WinHTTP) is targeted at middle-tier and
     back-end server applications that require access to an HTTP client stack }
  TWinHTTP = class(TWinHttpAPI)
  private
  protected

    // those internal methods will raise an EOSError exception on error
    procedure InternalConnect(ConnectionTimeOut,SendTimeout,ReceiveTimeout: DWORD); override;
    procedure InternalRequest(const method, aURL: RawByteString); override;
    procedure InternalCloseRequest; override;
    procedure InternalAddHeader(const hdr: RawByteString); override;
    procedure InternalSendRequest(const aData: RawByteString); override;
    function InternalGetInfo(Info: DWORD): RawByteString; override;
    function InternalGetInfo32(Info: DWORD): DWORD; override;
    function InternalReadData(var Data: RawByteString; Read: integer): cardinal; override;
  public
    /// relase the connection
    destructor Destroy; override;




  end;

  /// type of a TWinHttpAPI class
  TWinHttpAPIClass = class of TWinHttpAPI;

  /// WinHTTP exception type
  EWinHTTP = class(Exception);

{$endif}
................................................................................

function Trim(const S: RawByteString): RawByteString;
{$ifdef PUREPASCAL}
var I, L: Integer;
begin
  L := Length(S);
  I := 1;
  while (I<=L) and (S[I]<=' ') do Inc(I);
  if I>L then
    Result := '' else
  if (I=1) and (S[L]>' ') then
    Result := S else begin
    while S[L]<=' ' do Dec(L);
    Result := Copy(S, I, L-I+1);
  end;
................................................................................
  BinToHexDisplay(@Request.ConnectionId,8,ConnectionID);
  // compute headers length
  if RemoteIP<>'' then
    L := (REMOTEIP_HEADERLEN+2)+length(RemoteIP) else
    L := 0;
  inc(L,(CONNECTIONID_HEADERLEN+2)+ord(ConnectionID[0]));
  for H := low(KNOWNHEADERS) to high(KNOWNHEADERS) do
    if Request.Headers.KnownHeaders[H].RawValueLength<>0 then
      inc(L,Request.Headers.KnownHeaders[H].RawValueLength+ord(KNOWNHEADERS[H][0])+4);
  P := Request.Headers.pUnknownHeaders;
  if P<>nil then
    for i := 1 to Request.Headers.UnknownHeaderCount do begin
      inc(L,P^.NameLength+P^.RawValueLength+4); // +4 for each ': '+#13#10
      inc(P);
    end;
  // set headers content
  SetString(result,nil,L);
  D := pointer(result);
  for H := low(KNOWNHEADERS) to high(KNOWNHEADERS) do
    if Request.Headers.KnownHeaders[H].RawValueLength<>0 then begin
      move(KNOWNHEADERS[H][1],D^,ord(KNOWNHEADERS[H][0]));
      inc(D,ord(KNOWNHEADERS[H][0]));
      PWord(D)^ := ord(':')+ord(' ')shl 8;
      inc(D,2);
      move(Request.Headers.KnownHeaders[H].pRawValue^,D^,
        Request.Headers.KnownHeaders[H].RawValueLength);
      inc(D,Request.Headers.KnownHeaders[H].RawValueLength);
      PWord(D)^ := 13+10 shl 8;
      inc(D,2);
    end;
  P := Request.Headers.pUnknownHeaders;
  if P<>nil then
    for i := 1 to Request.Headers.UnknownHeaderCount do begin
      move(P^.pName^,D^,P^.NameLength);
................................................................................
  lpReserved: Pointer): BOOL; stdcall; external winhttpdll;
function WinHttpQueryHeaders(hRequest: HINTERNET; dwInfoLevel: DWORD; pwszName: PWideChar;
  lpBuffer: Pointer; var lpdwBufferLength, lpdwIndex: DWORD): BOOL; stdcall; external winhttpdll;
function WinHttpReadData(hRequest: HINTERNET; lpBuffer: Pointer;
  dwNumberOfBytesToRead: DWORD; var lpdwNumberOfBytesRead: DWORD): BOOL; stdcall; external winhttpdll;
function WinHttpSetTimeouts(hInternet: HINTERNET; dwResolveTimeout: DWORD;
  dwConnectTimeout: DWORD; dwSendTimeout: DWORD; dwReceiveTimeout: DWORD): BOOL; stdcall; external winhttpdll;



destructor TWinHTTP.Destroy;
begin
  if fConnection<>nil then
    WinHttpCloseHandle(fConnection);
  if fSession<>nil then
    WinHttpCloseHandle(fSession);
................................................................................
    Flags := Flags or WINHTTP_FLAG_SECURE;
  fRequest := WinHttpOpenRequest(fConnection, pointer(Ansi7ToUnicode(method)),
    pointer(Ansi7ToUnicode(aURL)), nil, nil, @ALL_ACCEPT, Flags);
  if fRequest=nil then
    RaiseLastModuleError(winhttpdll,EWinHTTP);
end;














procedure TWinHTTP.InternalSendRequest(const aData: RawByteString);
var L: integer;
begin




  L := length(aData);
  if not WinHttpSendRequest(fRequest, nil, 0, pointer(aData), L, L, 0) or
     not WinHttpReceiveResponse(fRequest,nil) then
    RaiseLastModuleError(winhttpdll,EWinHTTP);
end;

{$endif}
................................................................................
  if Http.Module<>0 then begin
    FreeLibrary(Http.Module);
    Http.Module := 0;
  end;
  {$endif}
  DestroySocketInterface;
end.







>







 







>







 







<

>












>
>
>
>

|







 







|







 







|
|










|
|
|


|
|
|







 







>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>



>
>
>
>







 







<
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
....
1257
1258
1259
1260
1261
1262
1263

1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
....
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
....
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
....
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
....
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
....
5944
5945
5946
5947
5948
5949
5950

  The Initial Developer of the Original Code is Arnaud Bouchez.

  Portions created by the Initial Developer are Copyright (C) 2014
  the Initial Developer. All Rights Reserved.

  Contributor(s):
  - Alfred Glaenzer (alf)
  - EMartin
  - Pavel (mpv)
  
  Alternatively, the contents of this file may be used under the terms of
  either the GNU General Public License Version 2 or later (the "GPL"), or
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  in which case the provisions of the GPL or the LGPL are applicable instead
  of those above. If you wish to allow use of your version of this file only
................................................................................
  - added ConnectionTimeOut, SendTimeout and ReceiveTimeout optional parameters
    to TWinHttpAPI constructors - feature request [bfe485b678]
  - added optional aCompressMinSize parameter to RegisterCompress() methods
  - added TWinHttpAPI.Get/Post/Put/Delete() class functions for easy remote
    resource retrieval using either WinHTTP or WinINet APIs
  - added TURI structure, ready to parse a supplied HTTP URI
  - added 'ConnectionID: 1234578' to the HTTP headers - request [0636eeec54]
  - added TWinHTTP.IgnoreSSLCertificates property (proposal by EMartin)
  - fixed TCrtSocket.BytesIn and TCrtSocket.BytesOut properties
  - fixed ticket [82df275784] TWinHttpAPI with responses without Content-Length
  - fixed ticket [f0749956af] TWinINet does not work with HTTPS servers
  - fixed ticket [842a5ae15a] THttpApiServer.Execute/SendError message
  - fixed ticket [f2ae4022a4] EWinINet error handling
  - fixed ticket [73da2c17b1] about Accept-Encoding header in THttpApiServer
  - fixed ticket [cbcbb3b2fc] about PtrInt definition
................................................................................
     you can run "proxycfg -u" or "netsh winhttp import proxy source=ie" to use
     the current user's proxy settings for Internet Explorer (under 64 bit
     Vista/Seven, to configure applications using the 32 bit WinHttp settings,
     call netsh or proxycfg bits from %SystemRoot%\SysWOW64 folder explicitely)
   - Microsoft Windows HTTP Services (WinHTTP) is targeted at middle-tier and
     back-end server applications that require access to an HTTP client stack }
  TWinHTTP = class(TWinHttpAPI)

  protected
    fIgnoreSSLCertificates: boolean;
    // those internal methods will raise an EOSError exception on error
    procedure InternalConnect(ConnectionTimeOut,SendTimeout,ReceiveTimeout: DWORD); override;
    procedure InternalRequest(const method, aURL: RawByteString); override;
    procedure InternalCloseRequest; override;
    procedure InternalAddHeader(const hdr: RawByteString); override;
    procedure InternalSendRequest(const aData: RawByteString); override;
    function InternalGetInfo(Info: DWORD): RawByteString; override;
    function InternalGetInfo32(Info: DWORD): DWORD; override;
    function InternalReadData(var Data: RawByteString; Read: integer): cardinal; override;
  public
    /// relase the connection
    destructor Destroy; override;
    /// allows to ignore untrusted SSL certificates
    // - similar to adding a security exception for a domain in the browser
    property IgnoreSSLCertificates: boolean
      read fIgnoreSSLCertificates write fIgnoreSSLCertificates;
  end;
        
  /// type of a TWinHttpAPI class
  TWinHttpAPIClass = class of TWinHttpAPI;

  /// WinHTTP exception type
  EWinHTTP = class(Exception);

{$endif}
................................................................................

function Trim(const S: RawByteString): RawByteString;
{$ifdef PUREPASCAL}
var I, L: Integer;
begin
  L := Length(S);
  I := 1;
  while (I<=L) and (S[i]<=' ') do Inc(I);
  if I>L then
    Result := '' else
  if (I=1) and (S[L]>' ') then
    Result := S else begin
    while S[L]<=' ' do Dec(L);
    Result := Copy(S, I, L-I+1);
  end;
................................................................................
  BinToHexDisplay(@Request.ConnectionId,8,ConnectionID);
  // compute headers length
  if RemoteIP<>'' then
    L := (REMOTEIP_HEADERLEN+2)+length(RemoteIP) else
    L := 0;
  inc(L,(CONNECTIONID_HEADERLEN+2)+ord(ConnectionID[0]));
  for H := low(KNOWNHEADERS) to high(KNOWNHEADERS) do
    if Request.Headers.KnownHeaders[h].RawValueLength<>0 then
      inc(L,Request.Headers.KnownHeaders[h].RawValueLength+ord(KNOWNHEADERS[h][0])+4);
  P := Request.Headers.pUnknownHeaders;
  if P<>nil then
    for i := 1 to Request.Headers.UnknownHeaderCount do begin
      inc(L,P^.NameLength+P^.RawValueLength+4); // +4 for each ': '+#13#10
      inc(P);
    end;
  // set headers content
  SetString(result,nil,L);
  D := pointer(result);
  for H := low(KNOWNHEADERS) to high(KNOWNHEADERS) do
    if Request.Headers.KnownHeaders[h].RawValueLength<>0 then begin
      move(KNOWNHEADERS[h][1],D^,ord(KNOWNHEADERS[h][0]));
      inc(D,ord(KNOWNHEADERS[h][0]));
      PWord(D)^ := ord(':')+ord(' ')shl 8;
      inc(D,2);
      move(Request.Headers.KnownHeaders[h].pRawValue^,D^,
        Request.Headers.KnownHeaders[h].RawValueLength);
      inc(D,Request.Headers.KnownHeaders[h].RawValueLength);
      PWord(D)^ := 13+10 shl 8;
      inc(D,2);
    end;
  P := Request.Headers.pUnknownHeaders;
  if P<>nil then
    for i := 1 to Request.Headers.UnknownHeaderCount do begin
      move(P^.pName^,D^,P^.NameLength);
................................................................................
  lpReserved: Pointer): BOOL; stdcall; external winhttpdll;
function WinHttpQueryHeaders(hRequest: HINTERNET; dwInfoLevel: DWORD; pwszName: PWideChar;
  lpBuffer: Pointer; var lpdwBufferLength, lpdwIndex: DWORD): BOOL; stdcall; external winhttpdll;
function WinHttpReadData(hRequest: HINTERNET; lpBuffer: Pointer;
  dwNumberOfBytesToRead: DWORD; var lpdwNumberOfBytesRead: DWORD): BOOL; stdcall; external winhttpdll;
function WinHttpSetTimeouts(hInternet: HINTERNET; dwResolveTimeout: DWORD;
  dwConnectTimeout: DWORD; dwSendTimeout: DWORD; dwReceiveTimeout: DWORD): BOOL; stdcall; external winhttpdll;
function WinHttpSetOption(hInternet: HINTERNET; dwOption: DWORD;
  lpBuffer: Pointer; dwBufferLength: DWORD): BOOL; stdcall; external winhttpdll;

destructor TWinHTTP.Destroy;
begin
  if fConnection<>nil then
    WinHttpCloseHandle(fConnection);
  if fSession<>nil then
    WinHttpCloseHandle(fSession);
................................................................................
    Flags := Flags or WINHTTP_FLAG_SECURE;
  fRequest := WinHttpOpenRequest(fConnection, pointer(Ansi7ToUnicode(method)),
    pointer(Ansi7ToUnicode(aURL)), nil, nil, @ALL_ACCEPT, Flags);
  if fRequest=nil then
    RaiseLastModuleError(winhttpdll,EWinHTTP);
end;

const
  // from http://www.tek-tips.com/faqs.cfm?fid=7493
  WINHTTP_OPTION_SECURITY_FLAGS = 31;
  SECURITY_FLAG_IGNORE_UNKNOWN_CA = $00000100;
  SECURITY_FLAG_IGNORE_CERT_DATE_INVALID = $00002000; // expired X509 Cert.
  SECURITY_FLAG_IGNORE_CERT_CN_INVALID = $00001000; // bad common name in X509 Cert.
  SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE = $00000200;
  SECURITY_FLAT_IGNORE_CERTIFICATES: DWORD =
    SECURITY_FLAG_IGNORE_UNKNOWN_CA or
    SECURITY_FLAG_IGNORE_CERT_DATE_INVALID or
    SECURITY_FLAG_IGNORE_CERT_CN_INVALID or
    SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;

procedure TWinHTTP.InternalSendRequest(const aData: RawByteString);
var L: integer;
begin
  if fHTTPS and fIgnoreSSLCertificates then 
    if not WinHttpSetOption(fRequest, WINHTTP_OPTION_SECURITY_FLAGS,
       @SECURITY_FLAT_IGNORE_CERTIFICATES, SizeOf(SECURITY_FLAT_IGNORE_CERTIFICATES)) then
      RaiseLastModuleError(winhttpdll,EWinHTTP);
  L := length(aData);
  if not WinHttpSendRequest(fRequest, nil, 0, pointer(aData), L, L, 0) or
     not WinHttpReceiveResponse(fRequest,nil) then
    RaiseLastModuleError(winhttpdll,EWinHTTP);
end;

{$endif}
................................................................................
  if Http.Module<>0 then begin
    FreeLibrary(Http.Module);
    Http.Module := 0;
  end;
  {$endif}
  DestroySocketInterface;
end.

Changes to synopseCommit.inc.

1
'1.18.365'
|
1
'1.18.366'